U mf\@sdZddlZddlZddlmZmZmZmZddlZ ddl Z ddl Z dddddd d hZ d hZ dZd Zd Zd ZdZdZdZGddde jjZGdddZGdddZdS)zTokenize DNS zone file formatN)AnyListOptionalTuple   ;()"c@seZdZdZdS)UngetBufferFullzDAn attempt was made to unget a token when the unget buffer was full.N)__name__ __module__ __qualname____doc__rr;/opt/hc_python/lib/python3.8/site-packages/dns/tokenizer.pyr(src@seZdZdZd#eeeeedddZ edd d Z edd d Z edd dZ edddZ edddZedddZedddZedddZddZddZddZdddd Zddd!d"ZdS)$TokenzA DNS zone file format token. ttype: The token type value: The token value has_escape: Does the token value contain escapes? FNttypevalue has_escapecommentcCs||_||_||_||_dS)zInitialize a token instance.Nr)selfrrrr rrr__init__4s zToken.__init__returncCs |jtkSN)rEOFr!rrris_eofBsz Token.is_eofcCs |jtkSr%)rEOLr'rrris_eolEsz Token.is_eolcCs |jtkSr%)r WHITESPACEr'rrr is_whitespaceHszToken.is_whitespacecCs |jtkSr%)r IDENTIFIERr'rrr is_identifierKszToken.is_identifiercCs |jtkSr%)r QUOTED_STRINGr'rrris_quoted_stringNszToken.is_quoted_stringcCs |jtkSr%)rCOMMENTr'rrr is_commentQszToken.is_commentcCs |jtkSr%)r DELIMITERr'rrr is_delimiterTszToken.is_delimitercCs|jtkp|jtkSr%)rr)r&r'rrr is_eol_or_eofWszToken.is_eol_or_eofcCs&t|tsdS|j|jko$|j|jkS)NF isinstancerrrr!otherrrr__eq__Zs z Token.__eq__cCs&t|tsdS|j|jkp$|j|jkS)NTr6r8rrr__ne___s z Token.__ne__cCsd|j|jfS)Nz%d "%s")rrr'rrr__str__dsz Token.__str__cCs|js |Sd}t|j}d}||kr|j|}|d7}|dkr||krPtjj|j|}|d7}|r||krztjj|j|}|d7}||krtjj|j|}|d7}|r|stjjt|dt|dt|}|dkrtjjt |}||7}qt |j |S)Nrrr \d ) rlenrdns exception UnexpectedEndisdigit SyntaxErrorintchrrrr!Z unescapedlicc2c3 codepointrrrunescapegs<        zToken.unescapecCs(d}t|j}d}||kr|j|}|d7}|dkr ||krHtjj|j|}|d7}|r||krrtjj|j|}|d7}||krtjj|j|}|d7}|r|stjjt|dt|dt|}|dkrtjj|d|7}n ||7}q||7}qt |j t |S) Nrr r=r>r?r@s%c) rArrBrCrDrErFrGencoderrbytesrIrrrunescape_to_bytess:        zToken.unescape_to_bytes)rFN)rrrrrGrboolrstrr"r(r*r,r.r0r2r4r5r:r;r<rPrTrrrrr,s.   rc@seZdZdZejddfeeeee j j dddZ edddZ eeefdd d Zedd d d ZedddZd>eeedddZeddddZddZeZddZd?eedddZeddd Zd@eedd!d"ZdAeedd#d$ZdBeedd%d&ZdCeeed'd(d)Zedd*d+Z dDeee!ed,d-d.Z"dEeed/d0d1Z#dFeee j j$eee j j$e j j$d2d3d4Z%dGee j j$eee j j$e j j$d5d6d7Z&edd8d9Z'edd:d;Z(eddzzrFr )r7rVioStringIOrSdecodesysstdinfile ungotten_charungotten_token multilinequotingeof _DELIMITERS delimiters line_numberAssertionErrorrYrBnameZ IDNA_2003rZ)r!rXrYrZrrrr"s2      zTokenizer.__init__r#cCsZ|jdkrJ|jrd}qV|jd}|dkr2d|_qV|dkrV|jd7_n |j}d|_|S)zRead a character from input.Nrr Tr)rarer`readrhr!rLrrr _get_chars  zTokenizer._get_charcCs |j|jfS)zReturn the current location in the input. Returns a (string, int) tuple. The first item is the filename of the input, the second is the current line number. )rYrhr'rrrwhere)szTokenizer.where)rLr$cCs|jdk rt||_dS)a%Unget a character. The unget buffer for characters is only one character large; it is an error to try to unget a character when the unget buffer is not empty. c: the character to unget raises UngetBufferFull: there is already an ungotten char N)rarrlrrr _unget_char2s zTokenizer._unget_charcCsFd}|}|dkr8|dkr8|dks*|js8|||S|d7}qdS)aFConsume input until a non-whitespace character is encountered. The non-whitespace character is then ungotten, and the number of whitespace characters consumed is returned. If the tokenizer is in multiline mode, then newlines are whitespace. Returns the number of characters skipped. rrrrr N)rmrcro)r!skippedrLrrrskip_whitespaceBs  zTokenizer.skip_whitespaceF) want_leading want_commentr$c Cs|jdk r>|j}d|_|r(|r>|Sn|r:|r>|Sn|S|}|r\|dkr\ttdSd}t}d}|}|dks||jkr|dkr|j rt j j |dkr|t kr|dkr|jd7_|qhq|dkr |jdkrt j j|jd8_|qhq|d krH|j s0d |_ t|_t }qhnd|_ t|_|qhn|d kr\ttd S|d kr|}|d ks|dkrq||7}qf|r||tt|S|dkr|jrt j d tt|dS|jr|d}qhnttd |dSn|}t}n ||qnb|j r8|d kr8t j dnB|dkrz||7}d }|}|dksr|d krz|j szt j j ||7}qh|dkr|t kr|jrt j d t}t|||S)aGet the next token. want_leading: If True, return a WHITESPACE token if the first character read is whitespace. The default is False. want_comment: If True, return a COMMENT token if the first token read is a comment. The default is False. Raises dns.exception.UnexpectedEnd: input ended prematurely Raises dns.exception.SyntaxError: input was badly formed Returns a Token. NrrrFr r r r Trr zunbalanced parentheses)r znewline in quoted stringr=)rbr,r2rqrr+r-rmrgrdrBrCrDr/rcrF_QUOTING_DELIMITERSrfr)ror1r&r3) r!rrrsZutokenrptokenrrrLrrrgetVs                  z Tokenizer.get)rur$cCs|jdk rt||_dS)aUnget a token. The unget buffer for tokens is only one token large; it is an error to try to unget a token when the unget buffer is not empty. token: the token to unget Raises UngetBufferFull: there is already an ungotten token N)rbrr!rurrrungets zTokenizer.ungetcCs|}|rt|S)zHReturn the next item in an iteration. Returns a Token. )rvr( StopIterationrwrrrnextszTokenizer.nextcCs|Sr%rr'rrr__iter__szTokenizer.__iter__r?)baser$cCsB|}|s tjd|js6tjdt|j|S)zRead the next token and interpret it as an unsigned integer. Raises dns.exception.SyntaxError if not an unsigned integer. Returns an int. expecting an identifierzexpecting an integer) rvrPr.rBrCrFrrErG)r!r|rurrrget_ints     zTokenizer.get_intcCs,|}|dks|dkr(tjd||S)zRead the next token and interpret it as an 8-bit unsigned integer. Raises dns.exception.SyntaxError if not an 8-bit unsigned integer. Returns an int. rr@z#%d is not an unsigned 8-bit integerr~rBrCrF)r!rrrr get_uint8s zTokenizer.get_uint8cCsJ|j|d}|dks|dkrF|dkr6tjd|ntjd||S)zRead the next token and interpret it as a 16-bit unsigned integer. Raises dns.exception.SyntaxError if not a 16-bit unsigned integer. Returns an int. r|riz*%o is not an octal unsigned 16-bit integerz$%d is not an unsigned 16-bit integerrr!r|rrrr get_uint16s zTokenizer.get_uint16cCs0|j|d}|dks|dkr,tjd||S)zRead the next token and interpret it as a 32-bit unsigned integer. Raises dns.exception.SyntaxError if not a 32-bit unsigned integer. Returns an int. rrlz$%d is not an unsigned 32-bit integerrrrrr get_uint32s zTokenizer.get_uint32cCs0|j|d}|dks|dkr,tjd||S)zRead the next token and interpret it as a 48-bit unsigned integer. Raises dns.exception.SyntaxError if not a 48-bit unsigned integer. Returns an int. rrlz$%d is not an unsigned 48-bit integerrrrrr get_uint48+s zTokenizer.get_uint48) max_lengthr$cCsL|}|s(|s(tjd|rFt|j|krFtjd|jS)zRead the next token and interpret it as a string. Raises dns.exception.SyntaxError if not a string. Raises dns.exception.SyntaxError if token value length exceeds max_length (if specified). Returns a string. zexpecting a stringzstring too long) rvrPr.r0rBrCrFrAr)r!rrurrr get_string;s   zTokenizer.get_stringcCs&|}|s tjd|jS)zRead the next token, which should be an identifier. Raises dns.exception.SyntaxError if not an identifier. Returns a string. r})rvrPr.rBrCrFrrwrrrget_identifierLs  zTokenizer.get_identifier) max_tokensr$cCs>g}|}|r ||q:||t||krq:q|S)zReturn the remaining tokens on the line, until an EOL or EOF is seen. max_tokens: If not None, stop after this number of tokens. Returns a list of tokens. )rvr5rxappendrA)r!rtokensrurrr get_remainingYs   zTokenizer.get_remaining) allow_emptyr$cCsXd}|}|r$||q@|s4tjj||j7}q|sT|sTtjd|S)aRead the remaining tokens on the line, which should be identifiers. Raises dns.exception.SyntaxError if there are no remaining tokens, unless `allow_empty=True` is given. Raises dns.exception.SyntaxError if a token is seen that is not an identifier. Returns a string containing a concatenation of the remaining identifiers. rzexpecting another identifier) rvrPr5rxr.rBrCrFr)r!rsrurrr!concatenate_remaining_identifiersls     z+Tokenizer.concatenate_remaining_identifiers)ruorigin relativize relativize_tor$cCs8|stjdtj|j||j}||p2||S)zTry to interpret the token as a DNS name. Raises dns.exception.SyntaxError if not a name. Returns a dns.name.Name. r}) r.rBrCrFrj from_textrrZZchoose_relativity)r!rurrrrjrrras_names  zTokenizer.as_name)rrrr$cCs|}|||||S)zRead the next token and interpret it as a DNS name. Raises dns.exception.SyntaxError if not a name. Returns a dns.name.Name. )rvr)r!rrrrurrrget_names zTokenizer.get_namecCs,|}|s(tjd|j|jf|S)znRead the next token and raise an exception if it isn't EOL or EOF. Returns a string. z expected EOL or EOF, got %d "%s")rvr5rBrCrFrrrwrrrget_eol_as_tokens zTokenizer.get_eol_as_tokencCs |jSr%)rrr'rrrget_eolszTokenizer.get_eolcCs.|}|s tjdtj|jS)zRead the next token and interpret it as a DNS TTL. Raises dns.exception.SyntaxError or dns.ttl.BadTTL if not an identifier or badly formed. Returns an int. r}) rvrPr.rBrCrFttlrrrwrrrget_ttls  zTokenizer.get_ttl)FF)r?)r?)r?)r?)N)N)F)NFN)NFN)*rrrrr^r_rrrVrBrjZ IDNACodecr"rmrrGrnrorqrUrrvrxrz__next__r{r~rrrrrrrrrNamerrrrrrrrrrWsb" 1 o        rW)rr[r^typingrrrrZ dns.exceptionrBZdns.nameZdns.ttlrfrtr&r)r+r-r/r1r3rCZ DNSExceptionrrrWrrrrs&