]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Add Tokenizer.concatenate_remaining_identifiers()
authorBrian Wellington <bwelling@xbill.org>
Tue, 30 Jun 2020 23:35:20 +0000 (16:35 -0700)
committerBrian Wellington <bwelling@xbill.org>
Tue, 30 Jun 2020 23:35:20 +0000 (16:35 -0700)
Replace identical code duplicated in a number of rdatatype
implementations that concatenated all of the remaining tokens in order
to decode them.

dns/rdtypes/ANY/CERT.py
dns/rdtypes/ANY/OPENPGPKEY.py
dns/rdtypes/ANY/RRSIG.py
dns/rdtypes/ANY/SSHFP.py
dns/rdtypes/ANY/TLSA.py
dns/rdtypes/IN/DHCID.py
dns/rdtypes/IN/IPSECKEY.py
dns/rdtypes/dnskeybase.py
dns/rdtypes/dsbase.py
dns/tokenizer.py

index 5e4ceb8c7386cc83b4f1bf15184115966634eee1..98f8b67c7265a9011a91bc255f3ccdad81b574d0 100644 (file)
@@ -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)
index ddcd055c073775003aa12715d1a5fe845b1bf37d..eebe2a98792b335cab5288c9ba9586f10d7b9fdd 100644 (file)
@@ -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)
 
index 0ee1c0673e3caab491f5957d7ff916cb302a0e2a..ddc6141c2477ba984d34df5079b249c74fc03ae6 100644 (file)
@@ -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,
index 1c5269fd9ef67511461c30d3115279401502224a..49a1239bc04ef839a0e9f390aec88dc56c383c46 100644 (file)
@@ -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)
 
index c6db8623d16b863eb23bf9c16226006ac90c43ed..3ffaaca67efc70149f66ebe38c087d428a63964e 100644 (file)
@@ -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)
 
index cd5d07454a684bbd8264b29af893e1783695cf0a..da834500edc50302d6b1a7544270cb0eb68cde4e 100644 (file)
@@ -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)
 
index 24dc652eaf5f086a6cabefd65f2e3069aa8efe79..b5bc0b3d484ff07c20aee5ca7fce7a02832ab1fa 100644 (file)
@@ -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)
index cfa34c2a836d0d74d3b5a7ea433457fb4c3c2773..31fa0ecfa5ced527278cf8af114258ef792e493e 100644 (file)
@@ -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)
 
index 9f512a1c5f9cc88c73afe9e69a75a1d8ff21f293..dec16f3fe0032ad968856b8f248a6faef2c9a226 100644 (file)
@@ -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)
index cb5ebd375c8d0ee9ba9dc50721d00cdc10bf702e..3e5d2ba92e8762532c2b3c5d6cbd0170298b26c7 100644 (file)
@@ -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.