Exemplo n.º 1
0
 def _read_int(self, size, signed=False):
     """
     Read an integer from the stream, using current endianness
     """
     return read_int(self.stream,
                     size,
                     signed=signed,
                     endianness=self.endianness)
Exemplo n.º 2
0
 def _read_int(self, size, signed=False):
     """
     Read an integer from the stream, using current endianness
     """
     return read_int(self.stream, size, signed=signed,
                     endianness=self.endianness)
Exemplo n.º 3
0
def test_read_int_empty_stream():
    with pytest.raises(StreamEmpty):
        read_int(io.BytesIO(''), 32)
Exemplo n.º 4
0
def test_read_int_truncated_stream():
    with pytest.raises(TruncatedFile):
        read_int(io.BytesIO('AB'), 32)
Exemplo n.º 5
0
def test_read_int():
    # 16bit, signed, positive
    assert read_int(io.BytesIO('\x12\x34'), 16, True, '>') == 0x1234
    assert read_int(io.BytesIO('\x12\x34'), 16, True, '<') == 0x3412

    assert read_int(io.BytesIO('\x12\x34extra'), 16, True, '>') == 0x1234
    assert read_int(io.BytesIO('\x12\x34extra'), 16, True, '<') == 0x3412

    # 16bit, signed, negative
    assert read_int(io.BytesIO('\xed\xcc'), 16, True, '>') == -0x1234
    assert read_int(io.BytesIO('\xcc\xed'), 16, True, '<') == -0x1234

    assert read_int(io.BytesIO('\xed\xccextra'), 16, True, '>') == -0x1234
    assert read_int(io.BytesIO('\xcc\xedextra'), 16, True, '<') == -0x1234

    # 16bit, unsigned
    assert read_int(io.BytesIO('\x12\x34'), 16, False, '>') == 0x1234
    assert read_int(io.BytesIO('\x12\x34'), 16, False, '<') == 0x3412

    assert read_int(io.BytesIO('\x12\x34extra'), 16, False, '>') == 0x1234
    assert read_int(io.BytesIO('\x12\x34extra'), 16, False, '<') == 0x3412

    # ..do we really need to test other sizes?
    assert read_int(io.BytesIO('\x12\x34\x56\x78'), 32, False, '>') == 0x12345678  # noqa
    assert read_int(io.BytesIO('\x12\x34\x56\x78'), 32, False, '<') == 0x78563412  # noqa
    assert read_int(io.BytesIO('\x12\x34\x56\x78'), 32, True, '>') == 0x12345678  # noqa
    assert read_int(io.BytesIO('\x12\x34\x56\x78'), 32, True, '<') == 0x78563412  # noqa
Exemplo n.º 6
0
def test_read_int():
    # 16bit, signed, positive
    assert read_int(io.BytesIO(b"\x12\x34"), 16, True, ">") == 0x1234
    assert read_int(io.BytesIO(b"\x12\x34"), 16, True, "<") == 0x3412

    assert read_int(io.BytesIO(b"\x12\x34extra"), 16, True, ">") == 0x1234
    assert read_int(io.BytesIO(b"\x12\x34extra"), 16, True, "<") == 0x3412

    # 16bit, signed, negative
    assert read_int(io.BytesIO(b"\xed\xcc"), 16, True, ">") == -0x1234
    assert read_int(io.BytesIO(b"\xcc\xed"), 16, True, "<") == -0x1234

    assert read_int(io.BytesIO(b"\xed\xccextra"), 16, True, ">") == -0x1234
    assert read_int(io.BytesIO(b"\xcc\xedextra"), 16, True, "<") == -0x1234

    # 16bit, unsigned
    assert read_int(io.BytesIO(b"\x12\x34"), 16, False, ">") == 0x1234
    assert read_int(io.BytesIO(b"\x12\x34"), 16, False, "<") == 0x3412

    assert read_int(io.BytesIO(b"\x12\x34extra"), 16, False, ">") == 0x1234
    assert read_int(io.BytesIO(b"\x12\x34extra"), 16, False, "<") == 0x3412

    # ..do we really need to test other sizes?
    assert (
        read_int(io.BytesIO(b"\x12\x34\x56\x78"), 32, False, ">") == 0x12345678
    )  # noqa
    assert (
        read_int(io.BytesIO(b"\x12\x34\x56\x78"), 32, False, "<") == 0x78563412
    )  # noqa
    assert (
        read_int(io.BytesIO(b"\x12\x34\x56\x78"), 32, True, ">") == 0x12345678
    )  # noqa
    assert (
        read_int(io.BytesIO(b"\x12\x34\x56\x78"), 32, True, "<") == 0x78563412
    )  # noqa
Exemplo n.º 7
0
def test_read_int_truncated_stream():
    with pytest.raises(TruncatedFile):
        read_int(io.BytesIO(b'AB'), 32)
Exemplo n.º 8
0
def test_read_int_empty_stream():
    with pytest.raises(StreamEmpty):
        read_int(io.BytesIO(b''), 32)
Exemplo n.º 9
0
def test_read_int():
    # 16bit, signed, positive
    assert read_int(io.BytesIO(b'\x12\x34'), 16, True, '>') == 0x1234
    assert read_int(io.BytesIO(b'\x12\x34'), 16, True, '<') == 0x3412

    assert read_int(io.BytesIO(b'\x12\x34extra'), 16, True, '>') == 0x1234
    assert read_int(io.BytesIO(b'\x12\x34extra'), 16, True, '<') == 0x3412

    # 16bit, signed, negative
    assert read_int(io.BytesIO(b'\xed\xcc'), 16, True, '>') == -0x1234
    assert read_int(io.BytesIO(b'\xcc\xed'), 16, True, '<') == -0x1234

    assert read_int(io.BytesIO(b'\xed\xccextra'), 16, True, '>') == -0x1234
    assert read_int(io.BytesIO(b'\xcc\xedextra'), 16, True, '<') == -0x1234

    # 16bit, unsigned
    assert read_int(io.BytesIO(b'\x12\x34'), 16, False, '>') == 0x1234
    assert read_int(io.BytesIO(b'\x12\x34'), 16, False, '<') == 0x3412

    assert read_int(io.BytesIO(b'\x12\x34extra'), 16, False, '>') == 0x1234
    assert read_int(io.BytesIO(b'\x12\x34extra'), 16, False, '<') == 0x3412

    # ..do we really need to test other sizes?
    assert read_int(io.BytesIO(b'\x12\x34\x56\x78'), 32, False, '>') == 0x12345678  # noqa
    assert read_int(io.BytesIO(b'\x12\x34\x56\x78'), 32, False, '<') == 0x78563412  # noqa
    assert read_int(io.BytesIO(b'\x12\x34\x56\x78'), 32, True, '>') == 0x12345678  # noqa
    assert read_int(io.BytesIO(b'\x12\x34\x56\x78'), 32, True, '<') == 0x78563412  # noqa