summaryrefslogtreecommitdiff
path: root/dns
diff options
context:
space:
mode:
authorBrian Wellington <bwelling@xbill.org>2020-07-09 10:59:12 -0700
committerBrian Wellington <bwelling@xbill.org>2020-07-09 10:59:12 -0700
commitbac5775b07f7e10c1a593533a8f1786f8b2ea40b (patch)
tree3f46a3efb8002d3c3f77c8488c927df1860711ac /dns
parent033349257b72cb7bf5d65a536051bbe14b80e141 (diff)
downloaddnspython-bac5775b07f7e10c1a593533a8f1786f8b2ea40b.tar.gz
Generalize the word breaking code.
Refactor common code from _base64ify and _hexify, and also add support for _hexify to skip word breaks.
Diffstat (limited to 'dns')
-rw-r--r--dns/rdata.py27
1 files changed, 14 insertions, 13 deletions
diff --git a/dns/rdata.py b/dns/rdata.py
index fd9bea9..17636af 100644
--- a/dns/rdata.py
+++ b/dns/rdata.py
@@ -31,33 +31,34 @@ import dns.rdataclass
import dns.rdatatype
import dns.tokenizer
-_hex_chunksize = 32
+_chunksize = 32
-def _hexify(data, chunksize=_hex_chunksize):
- """Convert a binary string into its hex encoding, broken up into chunks
- of chunksize characters separated by a space.
+def _wordbreak(line, chunksize=_chunksize):
+ """Break a string into chunks of chunksize characters separated by a space.
"""
- line = binascii.hexlify(data)
+ if not chunksize:
+ return line
return b' '.join([line[i:i + chunksize]
for i
in range(0, len(line), chunksize)]).decode()
-_base64_chunksize = 32
+
+def _hexify(data, chunksize=_chunksize):
+ """Convert a binary string into its hex encoding, broken up into chunks
+ of chunksize characters separated by a space.
+ """
+
+ return _wordbreak(binascii.hexlify(data), chunksize)
-def _base64ify(data, chunksize=_base64_chunksize):
+def _base64ify(data, chunksize=_chunksize):
"""Convert a binary string into its base64 encoding, broken up into chunks
of chunksize characters separated by a space.
"""
- line = base64.b64encode(data)
- if not chunksize:
- return line
- return b' '.join([line[i:i + chunksize]
- for i
- in range(0, len(line), chunksize)]).decode()
+ return _wordbreak(base64.b64encode(data), chunksize)
__escaped = b'"\\'