#!/usr/bin/env python3

from argparse import ArgumentParser
import io
import json


def parse_args():
    parser = ArgumentParser()
    parser.add_argument("file_path", type=str)
    return parser.parse_args()


def main(file_path):
    with open(file_path, "rb") as binary_file:
        seek_to_sync_marker(binary_file)
        sync_marker = read_sync_marker(binary_file)

        while binary_file.read(1):
            # We haven't reached EOF, move back one byte
            binary_file.seek(-1, io.SEEK_CUR)

            byte_position = binary_file.tell()
            objects_in_block = read_long(binary_file)
            size_of_objects = read_long(binary_file)
            print(
                json.dumps(
                    {
                        "pos": byte_position,
                        "objects": objects_in_block,
                        "size": size_of_objects,
                    }
                )
            )

            binary_file.seek(size_of_objects, io.SEEK_CUR)
            block_sync_marker = read_sync_marker(binary_file)
            if block_sync_marker != sync_marker:
                raise Exception("Bad block sync marker: {block_sync_marker.hex()}")


def seek_to_sync_marker(byte_stream):
    byte_stream.seek(4)  # Seek past initial 'Obj1'
    key_value_pairs_in_block = read_long(byte_stream)
    if key_value_pairs_in_block < 0:
        read_long(byte_stream)  # block size, unused
        key_value_pairs_in_block = -1 * key_value_pairs_in_block

    while key_value_pairs_in_block > 0:
        for _ in range(key_value_pairs_in_block):
            length_of_key = read_long(byte_stream)
            byte_stream.seek(length_of_key, io.SEEK_CUR)
            length_of_value = read_long(byte_stream)
            byte_stream.seek(length_of_value, io.SEEK_CUR)

        key_value_pairs_in_block = read_long(byte_stream)
        if key_value_pairs_in_block < 0:
            read_long(byte_stream)  # block size, unused
            key_value_pairs_in_block = -1 * key_value_pairs_in_block


def read_long(byte_stream):
    zig_zag_encoded_long = 0
    byte_idx = 0
    high_order_bit = None
    while byte_idx == 0 or high_order_bit == 1:
        byte = byte_stream.read(1)
        # Endianness doesn't matter, we have just a single byte
        byte_as_num = int.from_bytes(byte, "big")
        lower_bits = byte_as_num & 0b01111111
        zig_zag_encoded_long += lower_bits << (byte_idx * 7)

        byte_idx += 1
        high_order_bit = byte_as_num >> 7

    abs_value = (zig_zag_encoded_long + 1) >> 1
    sign = -1 if zig_zag_encoded_long & 0b1 else 1
    return sign * abs_value


def read_sync_marker(byte_stream):
    return byte_stream.read(16)


if __name__ == "__main__":
    args = parse_args()
    main(args.file_path)

6fc7bed61163d4ee31c0e06f83c609378e48aa20
<mask token>


def parse_args():
    parser = ArgumentParser()
    parser.add_argument('file_path', type=str)
    return parser.parse_args()


def main(file_path):
    with open(file_path, 'rb') as binary_file:
        seek_to_sync_marker(binary_file)
        sync_marker = read_sync_marker(binary_file)
        while binary_file.read(1):
            binary_file.seek(-1, io.SEEK_CUR)
            byte_position = binary_file.tell()
            objects_in_block = read_long(binary_file)
            size_of_objects = read_long(binary_file)
            print(json.dumps({'pos': byte_position, 'objects':
                objects_in_block, 'size': size_of_objects}))
            binary_file.seek(size_of_objects, io.SEEK_CUR)
            block_sync_marker = read_sync_marker(binary_file)
            if block_sync_marker != sync_marker:
                raise Exception(
                    'Bad block sync marker: {block_sync_marker.hex()}')


<mask token>


def read_long(byte_stream):
    zig_zag_encoded_long = 0
    byte_idx = 0
    high_order_bit = None
    while byte_idx == 0 or high_order_bit == 1:
        byte = byte_stream.read(1)
        byte_as_num = int.from_bytes(byte, 'big')
        lower_bits = byte_as_num & 127
        zig_zag_encoded_long += lower_bits << byte_idx * 7
        byte_idx += 1
        high_order_bit = byte_as_num >> 7
    abs_value = zig_zag_encoded_long + 1 >> 1
    sign = -1 if zig_zag_encoded_long & 1 else 1
    return sign * abs_value


<mask token>

<mask token>


def parse_args():
    parser = ArgumentParser()
    parser.add_argument('file_path', type=str)
    return parser.parse_args()


def main(file_path):
    with open(file_path, 'rb') as binary_file:
        seek_to_sync_marker(binary_file)
        sync_marker = read_sync_marker(binary_file)
        while binary_file.read(1):
            binary_file.seek(-1, io.SEEK_CUR)
            byte_position = binary_file.tell()
            objects_in_block = read_long(binary_file)
            size_of_objects = read_long(binary_file)
            print(json.dumps({'pos': byte_position, 'objects':
                objects_in_block, 'size': size_of_objects}))
            binary_file.seek(size_of_objects, io.SEEK_CUR)
            block_sync_marker = read_sync_marker(binary_file)
            if block_sync_marker != sync_marker:
                raise Exception(
                    'Bad block sync marker: {block_sync_marker.hex()}')


def seek_to_sync_marker(byte_stream):
    byte_stream.seek(4)
    key_value_pairs_in_block = read_long(byte_stream)
    if key_value_pairs_in_block < 0:
        read_long(byte_stream)
        key_value_pairs_in_block = -1 * key_value_pairs_in_block
    while key_value_pairs_in_block > 0:
        for _ in range(key_value_pairs_in_block):
            length_of_key = read_long(byte_stream)
            byte_stream.seek(length_of_key, io.SEEK_CUR)
            length_of_value = read_long(byte_stream)
            byte_stream.seek(length_of_value, io.SEEK_CUR)
        key_value_pairs_in_block = read_long(byte_stream)
        if key_value_pairs_in_block < 0:
            read_long(byte_stream)
            key_value_pairs_in_block = -1 * key_value_pairs_in_block


def read_long(byte_stream):
    zig_zag_encoded_long = 0
    byte_idx = 0
    high_order_bit = None
    while byte_idx == 0 or high_order_bit == 1:
        byte = byte_stream.read(1)
        byte_as_num = int.from_bytes(byte, 'big')
        lower_bits = byte_as_num & 127
        zig_zag_encoded_long += lower_bits << byte_idx * 7
        byte_idx += 1
        high_order_bit = byte_as_num >> 7
    abs_value = zig_zag_encoded_long + 1 >> 1
    sign = -1 if zig_zag_encoded_long & 1 else 1
    return sign * abs_value


<mask token>

<mask token>


def parse_args():
    parser = ArgumentParser()
    parser.add_argument('file_path', type=str)
    return parser.parse_args()


def main(file_path):
    with open(file_path, 'rb') as binary_file:
        seek_to_sync_marker(binary_file)
        sync_marker = read_sync_marker(binary_file)
        while binary_file.read(1):
            binary_file.seek(-1, io.SEEK_CUR)
            byte_position = binary_file.tell()
            objects_in_block = read_long(binary_file)
            size_of_objects = read_long(binary_file)
            print(json.dumps({'pos': byte_position, 'objects':
                objects_in_block, 'size': size_of_objects}))
            binary_file.seek(size_of_objects, io.SEEK_CUR)
            block_sync_marker = read_sync_marker(binary_file)
            if block_sync_marker != sync_marker:
                raise Exception(
                    'Bad block sync marker: {block_sync_marker.hex()}')


def seek_to_sync_marker(byte_stream):
    byte_stream.seek(4)
    key_value_pairs_in_block = read_long(byte_stream)
    if key_value_pairs_in_block < 0:
        read_long(byte_stream)
        key_value_pairs_in_block = -1 * key_value_pairs_in_block
    while key_value_pairs_in_block > 0:
        for _ in range(key_value_pairs_in_block):
            length_of_key = read_long(byte_stream)
            byte_stream.seek(length_of_key, io.SEEK_CUR)
            length_of_value = read_long(byte_stream)
            byte_stream.seek(length_of_value, io.SEEK_CUR)
        key_value_pairs_in_block = read_long(byte_stream)
        if key_value_pairs_in_block < 0:
            read_long(byte_stream)
            key_value_pairs_in_block = -1 * key_value_pairs_in_block


def read_long(byte_stream):
    zig_zag_encoded_long = 0
    byte_idx = 0
    high_order_bit = None
    while byte_idx == 0 or high_order_bit == 1:
        byte = byte_stream.read(1)
        byte_as_num = int.from_bytes(byte, 'big')
        lower_bits = byte_as_num & 127
        zig_zag_encoded_long += lower_bits << byte_idx * 7
        byte_idx += 1
        high_order_bit = byte_as_num >> 7
    abs_value = zig_zag_encoded_long + 1 >> 1
    sign = -1 if zig_zag_encoded_long & 1 else 1
    return sign * abs_value


def read_sync_marker(byte_stream):
    return byte_stream.read(16)


if __name__ == '__main__':
    args = parse_args()
    main(args.file_path)

from argparse import ArgumentParser
import io
import json


def parse_args():
    parser = ArgumentParser()
    parser.add_argument('file_path', type=str)
    return parser.parse_args()


def main(file_path):
    with open(file_path, 'rb') as binary_file:
        seek_to_sync_marker(binary_file)
        sync_marker = read_sync_marker(binary_file)
        while binary_file.read(1):
            binary_file.seek(-1, io.SEEK_CUR)
            byte_position = binary_file.tell()
            objects_in_block = read_long(binary_file)
            size_of_objects = read_long(binary_file)
            print(json.dumps({'pos': byte_position, 'objects':
                objects_in_block, 'size': size_of_objects}))
            binary_file.seek(size_of_objects, io.SEEK_CUR)
            block_sync_marker = read_sync_marker(binary_file)
            if block_sync_marker != sync_marker:
                raise Exception(
                    'Bad block sync marker: {block_sync_marker.hex()}')


def seek_to_sync_marker(byte_stream):
    byte_stream.seek(4)
    key_value_pairs_in_block = read_long(byte_stream)
    if key_value_pairs_in_block < 0:
        read_long(byte_stream)
        key_value_pairs_in_block = -1 * key_value_pairs_in_block
    while key_value_pairs_in_block > 0:
        for _ in range(key_value_pairs_in_block):
            length_of_key = read_long(byte_stream)
            byte_stream.seek(length_of_key, io.SEEK_CUR)
            length_of_value = read_long(byte_stream)
            byte_stream.seek(length_of_value, io.SEEK_CUR)
        key_value_pairs_in_block = read_long(byte_stream)
        if key_value_pairs_in_block < 0:
            read_long(byte_stream)
            key_value_pairs_in_block = -1 * key_value_pairs_in_block


def read_long(byte_stream):
    zig_zag_encoded_long = 0
    byte_idx = 0
    high_order_bit = None
    while byte_idx == 0 or high_order_bit == 1:
        byte = byte_stream.read(1)
        byte_as_num = int.from_bytes(byte, 'big')
        lower_bits = byte_as_num & 127
        zig_zag_encoded_long += lower_bits << byte_idx * 7
        byte_idx += 1
        high_order_bit = byte_as_num >> 7
    abs_value = zig_zag_encoded_long + 1 >> 1
    sign = -1 if zig_zag_encoded_long & 1 else 1
    return sign * abs_value


def read_sync_marker(byte_stream):
    return byte_stream.read(16)


if __name__ == '__main__':
    args = parse_args()
    main(args.file_path)

#!/usr/bin/env python3

from argparse import ArgumentParser
import io
import json


def parse_args():
    parser = ArgumentParser()
    parser.add_argument("file_path", type=str)
    return parser.parse_args()


def main(file_path):
    with open(file_path, "rb") as binary_file:
        seek_to_sync_marker(binary_file)
        sync_marker = read_sync_marker(binary_file)

        while binary_file.read(1):
            # We haven't reached EOF, move back one byte
            binary_file.seek(-1, io.SEEK_CUR)

            byte_position = binary_file.tell()
            objects_in_block = read_long(binary_file)
            size_of_objects = read_long(binary_file)
            print(
                json.dumps(
                    {
                        "pos": byte_position,
                        "objects": objects_in_block,
                        "size": size_of_objects,
                    }
                )
            )

            binary_file.seek(size_of_objects, io.SEEK_CUR)
            block_sync_marker = read_sync_marker(binary_file)
            if block_sync_marker != sync_marker:
                raise Exception("Bad block sync marker: {block_sync_marker.hex()}")


def seek_to_sync_marker(byte_stream):
    byte_stream.seek(4)  # Seek past initial 'Obj1'
    key_value_pairs_in_block = read_long(byte_stream)
    if key_value_pairs_in_block < 0:
        read_long(byte_stream)  # block size, unused
        key_value_pairs_in_block = -1 * key_value_pairs_in_block

    while key_value_pairs_in_block > 0:
        for _ in range(key_value_pairs_in_block):
            length_of_key = read_long(byte_stream)
            byte_stream.seek(length_of_key, io.SEEK_CUR)
            length_of_value = read_long(byte_stream)
            byte_stream.seek(length_of_value, io.SEEK_CUR)

        key_value_pairs_in_block = read_long(byte_stream)
        if key_value_pairs_in_block < 0:
            read_long(byte_stream)  # block size, unused
            key_value_pairs_in_block = -1 * key_value_pairs_in_block


def read_long(byte_stream):
    zig_zag_encoded_long = 0
    byte_idx = 0
    high_order_bit = None
    while byte_idx == 0 or high_order_bit == 1:
        byte = byte_stream.read(1)
        # Endianness doesn't matter, we have just a single byte
        byte_as_num = int.from_bytes(byte, "big")
        lower_bits = byte_as_num & 0b01111111
        zig_zag_encoded_long += lower_bits << (byte_idx * 7)

        byte_idx += 1
        high_order_bit = byte_as_num >> 7

    abs_value = (zig_zag_encoded_long + 1) >> 1
    sign = -1 if zig_zag_encoded_long & 0b1 else 1
    return sign * abs_value


def read_sync_marker(byte_stream):
    return byte_stream.read(16)


if __name__ == "__main__":
    args = parse_args()
    main(args.file_path)
