def extract_graph_structure()

in scripts/parser.py [0:0]


def extract_graph_structure(fetcher, graph_off, graph_len):
    """
    Walk the GraphProto payload [graph_off..graph_off+graph_len) and
    collect only fields whose tag in WANTED_TAGS, skipping all others.
    Returns the concatenated bytes of just those wanted fields.
    """
    out = bytearray()
    pos = graph_off
    end = graph_off + graph_len

    while pos < end:
        # fetch minimal header (varint key + possible length prefix)
        hdr = fetcher.fetch(pos, pos + 19)
        hdr_stream = io.BytesIO(hdr)
        key, key_len = read_varint(hdr_stream)
        field_num, wire_type = key >> 3, key & 0x7

        length = 0; length_len = 0
        if wire_type == 2:
            length, length_len = read_varint(hdr_stream)

        # compute total bytes for this field
        if wire_type == 2:
            total = key_len + length_len + length
        elif wire_type == 0:
            total = key_len + skip_field(io.BytesIO(hdr[key_len:]), wire_type)
        elif wire_type == 1:
            total = key_len + 8
        elif wire_type == 5:
            total = key_len + 4
        else:
            raise ValueError(f"Bad wire type {wire_type}")

        # if it’s not one of the wanted tags, skip it entirely
        if field_num not in WANTED_TAGS:
            pos += total
            continue

        # otherwise fetch & append it in one go
        chunk = fetcher.fetch(pos, pos + total - 1)
        out.extend(chunk)
        pos += total

    return bytes(out)