From: Brian Wellington Date: Tue, 30 Jun 2020 23:35:20 +0000 (-0700) Subject: Add Tokenizer.concatenate_remaining_identifiers() X-Git-Tag: v2.0.0rc2~32 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7c0accbb5412d4065446543077ab625daf9749af;p=thirdparty%2Fdnspython.git Add Tokenizer.concatenate_remaining_identifiers() Replace identical code duplicated in a number of rdatatype implementations that concatenated all of the remaining tokens in order to decode them. --- diff --git a/dns/rdtypes/ANY/CERT.py b/dns/rdtypes/ANY/CERT.py index 5e4ceb8c..98f8b67c 100644 --- a/dns/rdtypes/ANY/CERT.py +++ b/dns/rdtypes/ANY/CERT.py @@ -84,15 +84,7 @@ class CERT(dns.rdata.Rdata): algorithm = dns.dnssec.algorithm_from_text(tok.get_string()) if algorithm < 0 or algorithm > 255: raise dns.exception.SyntaxError("bad algorithm type") - chunks = [] - while 1: - t = tok.get().unescape() - if t.is_eol_or_eof(): - break - if not t.is_identifier(): - raise dns.exception.SyntaxError - chunks.append(t.value.encode()) - b64 = b''.join(chunks) + b64 = tok.concatenate_remaining_identifiers().encode() certificate = base64.b64decode(b64) return cls(rdclass, rdtype, certificate_type, key_tag, algorithm, certificate) diff --git a/dns/rdtypes/ANY/OPENPGPKEY.py b/dns/rdtypes/ANY/OPENPGPKEY.py index ddcd055c..eebe2a98 100644 --- a/dns/rdtypes/ANY/OPENPGPKEY.py +++ b/dns/rdtypes/ANY/OPENPGPKEY.py @@ -37,15 +37,7 @@ class OPENPGPKEY(dns.rdata.Rdata): @classmethod def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None): - chunks = [] - while 1: - t = tok.get().unescape() - if t.is_eol_or_eof(): - break - if not t.is_identifier(): - raise dns.exception.SyntaxError - chunks.append(t.value.encode()) - b64 = b''.join(chunks) + b64 = tok.concatenate_remaining_identifiers().encode() key = base64.b64decode(b64) return cls(rdclass, rdtype, key) diff --git a/dns/rdtypes/ANY/RRSIG.py b/dns/rdtypes/ANY/RRSIG.py index 0ee1c067..ddc6141c 100644 --- a/dns/rdtypes/ANY/RRSIG.py +++ b/dns/rdtypes/ANY/RRSIG.py @@ -99,15 +99,7 @@ class RRSIG(dns.rdata.Rdata): inception = sigtime_to_posixtime(tok.get_string()) key_tag = tok.get_int() signer = tok.get_name(origin, relativize, relativize_to) - chunks = [] - while 1: - t = tok.get().unescape() - if t.is_eol_or_eof(): - break - if not t.is_identifier(): - raise dns.exception.SyntaxError - chunks.append(t.value.encode()) - b64 = b''.join(chunks) + b64 = tok.concatenate_remaining_identifiers().encode() signature = base64.b64decode(b64) return cls(rdclass, rdtype, type_covered, algorithm, labels, original_ttl, expiration, inception, key_tag, signer, diff --git a/dns/rdtypes/ANY/SSHFP.py b/dns/rdtypes/ANY/SSHFP.py index 1c5269fd..49a1239b 100644 --- a/dns/rdtypes/ANY/SSHFP.py +++ b/dns/rdtypes/ANY/SSHFP.py @@ -48,15 +48,7 @@ class SSHFP(dns.rdata.Rdata): relativize_to=None): algorithm = tok.get_uint8() fp_type = tok.get_uint8() - chunks = [] - while 1: - t = tok.get().unescape() - if t.is_eol_or_eof(): - break - if not t.is_identifier(): - raise dns.exception.SyntaxError - chunks.append(t.value.encode()) - fingerprint = b''.join(chunks) + fingerprint = tok.concatenate_remaining_identifiers().encode() fingerprint = binascii.unhexlify(fingerprint) return cls(rdclass, rdtype, algorithm, fp_type, fingerprint) diff --git a/dns/rdtypes/ANY/TLSA.py b/dns/rdtypes/ANY/TLSA.py index c6db8623..3ffaaca6 100644 --- a/dns/rdtypes/ANY/TLSA.py +++ b/dns/rdtypes/ANY/TLSA.py @@ -51,15 +51,7 @@ class TLSA(dns.rdata.Rdata): usage = tok.get_uint8() selector = tok.get_uint8() mtype = tok.get_uint8() - cert_chunks = [] - while 1: - t = tok.get().unescape() - if t.is_eol_or_eof(): - break - if not t.is_identifier(): - raise dns.exception.SyntaxError - cert_chunks.append(t.value.encode()) - cert = b''.join(cert_chunks) + cert = tok.concatenate_remaining_identifiers().encode() cert = binascii.unhexlify(cert) return cls(rdclass, rdtype, usage, selector, mtype, cert) diff --git a/dns/rdtypes/IN/DHCID.py b/dns/rdtypes/IN/DHCID.py index cd5d0745..da834500 100644 --- a/dns/rdtypes/IN/DHCID.py +++ b/dns/rdtypes/IN/DHCID.py @@ -38,15 +38,7 @@ class DHCID(dns.rdata.Rdata): @classmethod def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True, relativize_to=None): - chunks = [] - while 1: - t = tok.get().unescape() - if t.is_eol_or_eof(): - break - if not t.is_identifier(): - raise dns.exception.SyntaxError - chunks.append(t.value.encode()) - b64 = b''.join(chunks) + b64 = tok.concatenate_remaining_identifiers().encode() data = base64.b64decode(b64) return cls(rdclass, rdtype, data) diff --git a/dns/rdtypes/IN/IPSECKEY.py b/dns/rdtypes/IN/IPSECKEY.py index 24dc652e..b5bc0b3d 100644 --- a/dns/rdtypes/IN/IPSECKEY.py +++ b/dns/rdtypes/IN/IPSECKEY.py @@ -81,15 +81,7 @@ class IPSECKEY(dns.rdata.Rdata): gateway = tok.get_name(origin, relativize, relativize_to) else: gateway = tok.get_string() - chunks = [] - while 1: - t = tok.get().unescape() - if t.is_eol_or_eof(): - break - if not t.is_identifier(): - raise dns.exception.SyntaxError - chunks.append(t.value.encode()) - b64 = b''.join(chunks) + b64 = tok.concatenate_remaining_identifiers().encode() key = base64.b64decode(b64) return cls(rdclass, rdtype, precedence, gateway_type, algorithm, gateway, key) diff --git a/dns/rdtypes/dnskeybase.py b/dns/rdtypes/dnskeybase.py index cfa34c2a..31fa0ecf 100644 --- a/dns/rdtypes/dnskeybase.py +++ b/dns/rdtypes/dnskeybase.py @@ -57,15 +57,7 @@ class DNSKEYBase(dns.rdata.Rdata): flags = tok.get_uint16() protocol = tok.get_uint8() algorithm = dns.dnssec.algorithm_from_text(tok.get_string()) - chunks = [] - while 1: - t = tok.get().unescape() - if t.is_eol_or_eof(): - break - if not t.is_identifier(): - raise dns.exception.SyntaxError - chunks.append(t.value.encode()) - b64 = b''.join(chunks) + b64 = tok.concatenate_remaining_identifiers().encode() key = base64.b64decode(b64) return cls(rdclass, rdtype, flags, protocol, algorithm, key) diff --git a/dns/rdtypes/dsbase.py b/dns/rdtypes/dsbase.py index 9f512a1c..dec16f3f 100644 --- a/dns/rdtypes/dsbase.py +++ b/dns/rdtypes/dsbase.py @@ -49,15 +49,7 @@ class DSBase(dns.rdata.Rdata): key_tag = tok.get_uint16() algorithm = dns.dnssec.algorithm_from_text(tok.get_string()) digest_type = tok.get_uint8() - chunks = [] - while 1: - t = tok.get().unescape() - if t.is_eol_or_eof(): - break - if not t.is_identifier(): - raise dns.exception.SyntaxError - chunks.append(t.value.encode()) - digest = b''.join(chunks) + digest = tok.concatenate_remaining_identifiers().encode() digest = binascii.unhexlify(digest) return cls(rdclass, rdtype, key_tag, algorithm, digest_type, digest) diff --git a/dns/tokenizer.py b/dns/tokenizer.py index cb5ebd37..3e5d2ba9 100644 --- a/dns/tokenizer.py +++ b/dns/tokenizer.py @@ -559,6 +559,25 @@ class Tokenizer: raise dns.exception.SyntaxError('expecting an identifier') return token.value + def concatenate_remaining_identifiers(self): + """Read the remaining tokens on the line, which should be identifiers. + + Raises dns.exception.SyntaxError if a token is seen that is not an + identifier. + + Returns a string containing a concatenation of the remaining + identifiers. + """ + s = "" + while True: + token = self.get().unescape() + if token.is_eol_or_eof(): + break + if not token.is_identifier(): + raise dns.exception.SyntaxError + s += token.value + return s + def as_name(self, token, origin=None, relativize=False, relativize_to=None): """Try to interpret the token as a DNS name.