def encode(self, d, stream, endianness): write_int(d['type'], stream, 16, endianness=endianness) if d['type'] == NRB_RECORD_IPv4: raw = pack_ipv4(d['address']) raw += (b"\x00".join([s.encode() for s in d['names']])) + b"\x00" write_int(len(raw), stream, 16, endianness=endianness) write_bytes_padded(stream, raw) elif d['type'] == NRB_RECORD_IPv6: raw = pack_ipv6(d['address']) raw += (b'\x00'.join([s.encode() for s in d['names']])) + b"\x00" write_int(len(raw), stream, 16, endianness=endianness) write_bytes_padded(stream, raw) else: write_int(len(d['raw']), stream, 16, endianness=endianness) write_bytes_padded(stream, d['raw'])
def encode(self, d, stream, endianness): if d["type"] == NRB_RECORD_END: # Don't let the user add records of this type. # We take care of it in `encode_finish()` below return write_int(d["type"], stream, 16, endianness=endianness) if d["type"] == NRB_RECORD_IPv4: raw = pack_ipv4(d["address"]) raw += (b"\x00".join([s.encode() for s in d["names"]])) + b"\x00" write_int(len(raw), stream, 16, endianness=endianness) write_bytes_padded(stream, raw) elif d["type"] == NRB_RECORD_IPv6: raw = pack_ipv6(d["address"]) raw += (b"\x00".join([s.encode() for s in d["names"]])) + b"\x00" write_int(len(raw), stream, 16, endianness=endianness) write_bytes_padded(stream, raw) else: write_int(len(d["raw"]), stream, 16, endianness=endianness) write_bytes_padded(stream, d["raw"])
def _encode_value(self, value, ftype): if ftype is None: warnings.warn( DeprecationWarning( 'Field type should not be "None". Please explicitly ' "use TYPE_BYTES instead.")) assert isinstance(value, (bytes, bytearray)) return value if ftype == TYPE_BYTES: assert isinstance(value, (bytes, bytearray)) return value if hasattr(ftype, "__call__"): # TODO figure out how callable options work return ftype(value, self.endianness) if ftype == TYPE_STRING: return value.encode("utf-8") if ftype in ("str", "unicode"): warnings.warn( DeprecationWarning( 'The "{ftype}" field type is deprecated. Please use "string" ' "(TYPE_STRING) instead.".format(ftype=ftype))) return value.encode("utf-8") if ftype in _numeric_types: fmt = self.endianness + _numeric_types[ftype] return struct.pack(fmt, value) if ftype == TYPE_IPV4: return pack_ipv4(value) if ftype == TYPE_IPV4_MASK: return pack_ipv4(value[0]) + pack_ipv4(value[1]) if ftype == TYPE_IPV6: return pack_ipv6(value) if ftype == TYPE_IPV6_PREFIX: return pack_ipv6(value[0]) + value[1] if ftype == TYPE_MACADDR: return pack_macaddr(value) if ftype == TYPE_EUIADDR: return pack_euiaddr(value) if ftype == TYPE_TYPE_BYTES: return struct.pack("B", value[0]) + value[1] if ftype == TYPE_EPBFLAGS: fmt = self.endianness + _numeric_types[TYPE_U32] return struct.pack(fmt, int(value)) if ftype == TYPE_OPT_CUSTOM_STR: fmt = self.endianness + _numeric_types[TYPE_U32] return struct.pack(fmt, value[0]) + value[1].encode("utf-8") if ftype == TYPE_OPT_CUSTOM_BYTES: fmt = self.endianness + _numeric_types[TYPE_U32] return struct.pack(fmt, value[0]) + value[1] raise ValueError("Unsupported field type: {0}".format(ftype))