Пример #1
0
def retrieve_table(dest_file: io) -> dict:
    """
    Read the binary file, and return the translation table as a reversed
    dictionary.
    """
    offset, *_ = unpack("i", dest_file.read(_sizeof("i")))
    chars = dest_file.read(offset * _sizeof("c"))
    codes = dest_file.read(offset * _sizeof("L"))

    chars = unpack("{}c".format(offset), chars)
    codes = unpack("{}L".format(offset), codes)
    return {
        "b{0}".format(tobinary(code)): str(char, encoding=ENC)
        for char, code in zip(chars, codes)
    }
Пример #2
0
def _decode_block(
        binary_content: bytes, table: dict, block_length: int
) -> str:
    """Transform the compressed content of a block into the original text."""
    newchars = []
    cont = binascii.hexlify(binary_content)
    bitarray = tobinary(cont)
    # Ignore first bit, sentinel
    bitarray = bitarray[1:]
    window_start, window_end = 0, 1
    part = bitarray[window_start:window_end]
    restored = 0  # bytes
    while part:
        char = table.get(bitarray[window_start:window_end], False)
        while not char and bitarray[window_start:window_end]:
            window_end += 1
            char = table.get(bitarray[window_start:window_end], False)
        newchars.append(char)
        restored += 1
        if restored == block_length:
            break
        window_start, window_end = window_end, window_end + 1
        part = bitarray[window_start:window_end]
    return "".join(newchars)[:block_length]
Пример #3
0
def test_tobinary_string():
    assert tobinary('ff') == '11111111'
    assert tobinary('F') == tobinary(15)
Пример #4
0
def test_tobinary_bytes():
    assert tobinary(b'ff') == tobinary(255)
    assert tobinary(b'A') == '1010'
Пример #5
0
def test_tobinary_int():
    assert tobinary(0xff) == '11111111'
    assert tobinary(42) == '101010'