def _convert_value(self, value, ftype): assert isinstance(value, six.binary_type) if ftype is None: warnings.warn( DeprecationWarning( 'Field type should not be "None". Please explicitly ' 'use TYPE_BYTES instead.')) return value if ftype == TYPE_BYTES: return value if hasattr(ftype, '__call__'): return ftype(value, self.endianness) if ftype == TYPE_STRING: return value.decode('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 six.u(value) _numeric_types = { TYPE_U8: 'B', TYPE_I8: 'b', TYPE_U16: 'H', TYPE_I16: 'h', TYPE_U32: 'I', TYPE_I32: 'i', TYPE_U64: 'Q', TYPE_I64: 'q', } if ftype in _numeric_types: fmt = self.endianness + _numeric_types[ftype] return struct.unpack(fmt, value)[0] if ftype == TYPE_IPV4: return unpack_ipv4(value) if ftype == TYPE_IPV4_MASK: return unpack_ipv4(value[:4]), unpack_ipv4(value[4:8]) if ftype == TYPE_IPV6: return unpack_ipv6(value) if ftype == TYPE_IPV6_PREFIX: return (unpack_ipv6(value[:16]), struct.unpack(self.endianness + 'B', value[16])) if ftype == TYPE_MACADDR: return unpack_macaddr(value) if ftype == TYPE_EUIADDR: return unpack_euiaddr(value) raise ValueError('Unsupported field type: {0}'.format(ftype))
def _convert_value(self, value, ftype): assert isinstance(value, six.binary_type) if ftype is None: warnings.warn(DeprecationWarning( 'Field type should not be "None". Please explicitly ' 'use TYPE_BYTES instead.')) return value if ftype == TYPE_BYTES: return value if hasattr(ftype, '__call__'): return ftype(value, self.endianness) if ftype == TYPE_STRING: return value.decode('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 six.u(value) _numeric_types = { TYPE_U8: 'B', TYPE_I8: 'b', TYPE_U16: 'H', TYPE_I16: 'h', TYPE_U32: 'I', TYPE_I32: 'i', TYPE_U64: 'Q', TYPE_I64: 'q', } if ftype in _numeric_types: fmt = self.endianness + _numeric_types[ftype] return struct.unpack(fmt, value)[0] if ftype == TYPE_IPV4: return unpack_ipv4(value) if ftype == TYPE_IPV4_MASK: return unpack_ipv4(value[:4]), unpack_ipv4(value[4:8]) if ftype == TYPE_IPV6: return unpack_ipv6(value) if ftype == TYPE_IPV6_PREFIX: return (unpack_ipv6(value[:16]), struct.unpack(self.endianness + 'B', value[16])) if ftype == TYPE_MACADDR: return unpack_macaddr(value) if ftype == TYPE_EUIADDR: return unpack_euiaddr(value) raise ValueError('Unsupported field type: {0}'.format(ftype))
def load(self, stream, endianness, seen=None): record_type = read_int(stream, 16, False, endianness) record_length = read_int(stream, 16, False, endianness) if record_type == NRB_RECORD_END: raise StreamEmpty("End marker reached") data = read_bytes_padded(stream, record_length) if record_type == NRB_RECORD_IPv4: return { "type": record_type, "address": unpack_ipv4(data[:4]), "names": [x.decode() for x in data[4:].split(b"\x00") if x != b""], } if record_type == NRB_RECORD_IPv6: return { "type": record_type, "address": unpack_ipv6(data[:16]), "names": [x.decode() for x in data[16:].split(b"\x00") if x != b""], } return {"type": record_type, "raw": data}
def _convert_value(self, value, ftype): if ftype is None: return value if hasattr(ftype, '__call__'): return ftype(value, self.endianness) if ftype in ('str', 'string', 'unicode'): return value.decode('utf-8') _numeric_types = { 'u8': 'B', 'i8': 'b', 'u16': 'H', 'i16': 'h', 'u32': 'I', 'i32': 'i', 'u64': 'Q', 'i64': 'q', } if ftype in _numeric_types: return struct.unpack( self.endianness + _numeric_types[ftype], value)[0] if ftype == 'ipv4': return unpack_ipv4(value) if ftype == 'ipv4+mask': return unpack_ipv4(value[:4]), unpack_ipv4(value[4:8]) if ftype == 'ipv6': return unpack_ipv6(value) if ftype == 'ipv6+prefix': return (unpack_ipv6(value[:16]), struct.unpack(self.endianness + 'B', value[16])) if ftype == 'macaddr': return unpack_macaddr(value) if ftype == 'euiaddr': return unpack_euiaddr(value) raise ValueError('Unsupported field type: {0}'.format(ftype))
def load(self, stream, endianness, seen=None): record_type = read_int(stream, 16, False, endianness) record_length = read_int(stream, 16, False, endianness) if record_type == NRB_RECORD_END: raise StreamEmpty('End marker reached') data = read_bytes_padded(stream, record_length) if record_type == NRB_RECORD_IPv4: return { 'type': record_type, 'address': unpack_ipv4(data[:4]), 'names': [x.decode() for x in data[4:].split(b"\x00")], } if record_type == NRB_RECORD_IPv6: return { 'type': record_type, 'address': unpack_ipv6(data[:16]), 'names': [x.decode() for x in data[16:].split(b"\x00")], } return {'type': record_type, 'raw': data}
def _decode_value(self, value, ftype): assert isinstance(value, (bytes, bytearray)) if ftype is None: warnings.warn( DeprecationWarning( 'Field type should not be "None". Please explicitly ' "use TYPE_BYTES instead.")) return value if ftype == TYPE_BYTES: return value if hasattr(ftype, "__call__"): return ftype(value, self.endianness) if ftype == TYPE_STRING: return value.decode("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.decode("utf-8") if ftype in _numeric_types: fmt = self.endianness + _numeric_types[ftype] return struct.unpack(fmt, value)[0] if ftype == TYPE_IPV4: return unpack_ipv4(value) if ftype == TYPE_IPV4_MASK: return unpack_ipv4(value[:4]), unpack_ipv4(value[4:8]) if ftype == TYPE_IPV6: return unpack_ipv6(value) if ftype == TYPE_IPV6_PREFIX: return ( unpack_ipv6(value[:16]), struct.unpack(self.endianness + "B", value[16]), ) if ftype == TYPE_MACADDR: return unpack_macaddr(value) if ftype == TYPE_EUIADDR: return unpack_euiaddr(value) if ftype == TYPE_TYPE_BYTES: return (value[0], value[1:]) if ftype == TYPE_EPBFLAGS: fmt = self.endianness + _numeric_types[TYPE_U32] flg = struct.unpack(fmt, value)[0] return EPBFlags(flg) if ftype == TYPE_OPT_CUSTOM_STR: fmt = self.endianness + _numeric_types[TYPE_U32] return (struct.unpack(fmt, value[0:4]), value[4:].decode("utf-8")) if ftype == TYPE_OPT_CUSTOM_BYTES: fmt = self.endianness + _numeric_types[TYPE_U32] return (struct.unpack(fmt, value[0:4]), value[4:]) raise ValueError("Unsupported field type: {0}".format(ftype))
def test_unpack_ipv4(): assert unpack_ipv4(b'\x00\x00\x00\x00') == '0.0.0.0' assert unpack_ipv4(b'\xff\xff\xff\xff') == '255.255.255.255' assert unpack_ipv4(b'\x0a\x10\x20\x30') == '10.16.32.48'
def test_unpack_ipv4(): assert unpack_ipv4(b"\x00\x00\x00\x00") == "0.0.0.0" assert unpack_ipv4(b"\xff\xff\xff\xff") == "255.255.255.255" assert unpack_ipv4(b"\x0a\x10\x20\x30") == "10.16.32.48"
def test_unpack_ipv4(): assert unpack_ipv4('\x00\x00\x00\x00') == '0.0.0.0' assert unpack_ipv4('\xff\xff\xff\xff') == '255.255.255.255' assert unpack_ipv4('\x0a\x10\x20\x30') == '10.16.32.48'
def test_unpack_ipv4(): assert unpack_ipv4("\x00\x00\x00\x00") == "0.0.0.0" assert unpack_ipv4("\xff\xff\xff\xff") == "255.255.255.255" assert unpack_ipv4("\x0a\x10\x20\x30") == "10.16.32.48"