Skip to content

Commit

Permalink
git varint decode
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed May 21, 2024
1 parent b838076 commit 1796233
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion analysis/code_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from collections import defaultdict
from dataclasses import dataclass, field
from pathlib import Path
from typing import List
from typing import List, Tuple

PUSH1 = 0x60
PUSH32 = 0x7f
Expand All @@ -16,6 +16,42 @@
CHUNK_LEN = 32


# uintmax_t decode_varint(const unsigned char **bufp)
# {
# const unsigned char *buf = *bufp;
# unsigned char c = *buf++;
# uintmax_t val = c & 127;
# while (c & 128) {
# val += 1;
# if (!val || MSB(val, 7))
# return 0; /* overflow */
# c = *buf++;
# val = (val << 7) + (c & 127);
# }
# *bufp = buf;
# return val;
# }

def decode_varint(buf: bytes) -> Tuple[int, int]:
i = 0
c = buf[i]
i += 1
val = c & 127
while c & 128:
val += 1
c = buf[i]
i += 1
val = (val << 7) + (c & 127)
return val, i


def test_decode_varint():
assert decode_varint(bytes([0])) == (0, 1)
assert decode_varint(bytes([1])) == (1, 1)
assert decode_varint(bytes([0x80, 0])) == (128, 2)
assert decode_varint(bytes([0xFF, 0xFF, 0x7f])) == (2113663, 3)


@dataclass
class Chunk:
first_instruction_offset: int
Expand Down

0 comments on commit 1796233

Please sign in to comment.