Skip to content

Commit

Permalink
Merge pull request #470 from bwelling/optimize-name
Browse files Browse the repository at this point in the history
Optimize name.to_digestable() and to_wire().
  • Loading branch information
rthalley committed May 15, 2020
2 parents 7142bff + df5c613 commit b303f39
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions dns/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,16 +594,17 @@ def to_digestable(self, origin=None):
Returns a ``bytes``.
"""

out = bytearray()
for label in self.labels:
out.append(len(label))
out += label.lower()
if not self.is_absolute():
if origin is None or not origin.is_absolute():
raise NeedAbsoluteNameOrOrigin
labels = list(self.labels)
labels.extend(list(origin.labels))
else:
labels = self.labels
dlabels = [struct.pack('!B%ds' % len(x), len(x), x.lower())
for x in labels]
return b''.join(dlabels)
for label in origin.labels:
out.append(len(label))
out += label.lower()
return bytes(out)

def to_wire(self, file=None, compress=None, origin=None):
"""Convert name to wire format, possibly compressing it.
Expand All @@ -629,10 +630,17 @@ def to_wire(self, file=None, compress=None, origin=None):
"""

if file is None:
file = io.BytesIO()
want_return = True
else:
want_return = False
out = bytearray()
for label in self.labels:
out.append(len(label))
out += label
if not self.is_absolute():
if origin is None or not origin.is_absolute():
raise NeedAbsoluteNameOrOrigin
for label in origin.labels:
out.append(len(label))
out += label
return bytes(out)

if not self.is_absolute():
if origin is None or not origin.is_absolute():
Expand Down Expand Up @@ -663,8 +671,6 @@ def to_wire(self, file=None, compress=None, origin=None):
file.write(struct.pack('!B', l))
if l > 0:
file.write(label)
if want_return:
return file.getvalue()

def __len__(self):
"""The length of the name (in labels).
Expand Down

0 comments on commit b303f39

Please sign in to comment.