Beispiel #1
0
    def _read_node(self, node_number, index):
        base_offset = node_number * self.metadata.node_byte_size
        record_size = self.metadata.record_size

        if record_size == 24:
            offset = base_offset + index * 3
            node_bytes = b'\x00' + self.data[offset:offset + 3]

        elif record_size == 28:
            (middle,) = struct.unpack(b'!B', self.data[base_offset + 3:base_offset + 4])
            if index:
                middle &= 0x0F
            else:
                middle = (0xF0 & middle) >> 4

            offset = base_offset + index * 4
            node_bytes = byte_from_int(middle) + self.data[offset:offset + 3]

        elif record_size == 32:
            offset = base_offset + index * 4
            node_bytes = self.data[offset:offset + 4]

        else:
            raise Exception('Unknown record size: {0}'.format(record_size))

        return struct.unpack(b'!I', node_bytes)[0]
 def _decode_pointer(self, size, offset):
     pointer_size = ((size >> 3) & 0x3) + 1
     new_offset = offset + pointer_size
     pointer_bytes = self._buffer[offset:new_offset]
     packed = pointer_bytes if pointer_size == 4 else struct.pack(b"!c", byte_from_int(size & 0x7)) + pointer_bytes
     unpacked = int_from_bytes(packed)
     pointer = unpacked + self._pointer_base + self._pointer_value_offset[pointer_size]
     if self._pointer_test:
         return pointer, new_offset
     (value, _) = self.decode(pointer)
     return value, new_offset
 def generate_large_uint(self, bits):
     ctrl_byte = b'\x02' if bits == 64 else b'\x03'
     uints = {
         b'\x00' + ctrl_byte: 0,
         b'\x02' + ctrl_byte + b'\x01\xf4': 500,
         b'\x02' + ctrl_byte + b'\x2a\x78': 10872,
     }
     for power in range(bits // 8 + 1):
         expected = 2**(8 * power) - 1
         input = byte_from_int(power) + ctrl_byte + (b'\xff' * power)
         uints[input] = expected
     return uints
 def generate_large_uint(self, bits):
     ctrl_byte = b'\x02' if bits == 64 else b'\x03'
     uints = {
         b'\x00' + ctrl_byte: 0,
         b'\x02' + ctrl_byte + b'\x01\xf4': 500,
         b'\x02' + ctrl_byte + b'\x2a\x78': 10872,
     }
     for power in range(bits // 8 + 1):
         expected = 2 ** (8 * power) - 1
         input = byte_from_int(power) + ctrl_byte + (b'\xff' * power)
         uints[input] = expected
     return uints
 def generate_large_uint(self, bits):
     ctrl_byte = b"\x02" if bits == 64 else b"\x03"
     uints = {
         b"\x00" + ctrl_byte: 0,
         b"\x02" + ctrl_byte + b"\x01\xf4": 500,
         b"\x02" + ctrl_byte + b"\x2a\x78": 10872,
     }
     for power in range(bits // 8 + 1):
         expected = 2**(8 * power) - 1
         input = byte_from_int(power) + ctrl_byte + (b"\xff" * power)
         uints[input] = expected
     return uints
Beispiel #6
0
 def _decode_pointer(self, size, offset):
     pointer_size = ((size >> 3) & 0x3) + 1
     new_offset = offset + pointer_size
     pointer_bytes = self._buffer[offset:new_offset]
     packed = pointer_bytes if pointer_size == 4 else struct.pack(
         b'!c', byte_from_int(size & 0x7)) + pointer_bytes
     unpacked = int_from_bytes(packed)
     pointer = unpacked + self._pointer_base + \
         self._pointer_value_offset[pointer_size]
     if self._pointer_test:
         return pointer, new_offset
     (value, _) = self.decode(pointer)
     return value, new_offset
Beispiel #7
0
    def _decode_pointer(self, size, offset):
        pointer_size = (size >> 3) + 1

        buf = self._buffer[offset:offset + pointer_size]
        new_offset = offset + pointer_size

        if pointer_size == 1:
            buf = byte_from_int(size & 0x7) + buf
            pointer = struct.unpack(b'!H', buf)[0] + self._pointer_base
        elif pointer_size == 2:
            buf = b'\x00' + byte_from_int(size & 0x7) + buf
            pointer = struct.unpack(b'!I', buf)[0] + 2048 + self._pointer_base
        elif pointer_size == 3:
            buf = byte_from_int(size & 0x7) + buf
            pointer = struct.unpack(b'!I',
                                    buf)[0] + 526336 + self._pointer_base
        else:
            pointer = struct.unpack(b'!I', buf)[0] + self._pointer_base

        if self._pointer_test:
            return pointer, new_offset
        (value, _) = self.decode(pointer)
        return value, new_offset
 def test_byte(self):
     # Python 2.6 doesn't support dictionary comprehension
     b = dict((byte_from_int(0xc0 ^ int_from_byte(k[0])) + k[1:],
               v.encode('utf-8')) for k, v in self.strings.items())
     self.validate_type_decoding('byte', b)
 def test_byte(self):
     # Python 2.6 doesn't support dictionary comprehension
     b = dict((byte_from_int(0xc0 ^ int_from_byte(k[0])) + k[1:],
               v.encode('utf-8'))
              for k, v in self.strings.items())
     self.validate_type_decoding('byte', b)