예제 #1
0
 def s_decode(self, stream: IO[bytes]) -> bytes:
     flag = stream.read(1)
     if flag == b'':
         raise DecodingError('TODO: MISSING FLAG')
     elif flag == b'\x00':
         return b''
     elif flag == b'\x01':
         return self.value_type.s_decode(stream)
     else:
         raise DecodingError('TODO: INVALID')
예제 #2
0
def decode_bool(data: bytes) -> bool:
    if data == b'\x01':
        return True
    elif data == b'\x00':
        return False
    else:
        raise DecodingError("TODO: INVALID")
예제 #3
0
def decode_bytes(data: bytes) -> bytes:
    from .parsers import parse_scalar
    stream = io.BytesIO(data)
    length = parse_scalar(32, stream)
    if stream.tell() + length != len(data):
        raise DecodingError("INVALID LENGTH")
    return data[-length:]
예제 #4
0
 def decode(self, data: bytes) -> Any:
     flag = data[0]
     if flag == 0:
         return b''
     elif flag == 1:
         return self.value_type.decode(data[1:])
     else:
         raise DecodingError('TODO: INVALID')
예제 #5
0
def decode_scalar(bit_size: int, data: bytes) -> int:

    max_length = (bit_size + 6) // 7
    if len(data) > max_length:
        raise Exception("Value encoded with empty bytes")

    if data[-1] & HIGH_MASK:
        raise DecodingError("Last bit should not have high bit set")

    head_components = tuple((byte & LOW_MASK) << (shift * 7)
                            for shift, byte in enumerate(data[:-1]))
    tail_component = (data[-1] & LOW_MASK) << ((len(data) - 1) * 7)
    return sum(head_components) + tail_component
예제 #6
0
 def s_decode(self, stream: IO[bytes]) -> bytes:
     value = stream.read(self.length)
     if len(value) != self.length:
         raise DecodingError("TODO: INVALID SIZE")
     return value
예제 #7
0
 def decode(self, data: bytes) -> bytes:
     if len(data) != self.length:
         raise DecodingError("TODO: INVALID SIZE")
     return data