1 #include "git-compat-util.h"
4 uintmax_t decode_varint(const unsigned char **bufp
)
6 const unsigned char *buf
= *bufp
;
7 unsigned char c
= *buf
++;
8 uintmax_t val
= c
& 127;
11 if (!val
|| MSB(val
, 7))
12 return 0; /* overflow */
14 val
= (val
<< 7) + (c
& 127);
20 int encode_varint(uintmax_t value
, unsigned char *buf
)
22 unsigned char varint
[16];
23 unsigned pos
= sizeof(varint
) - 1;
24 varint
[pos
] = value
& 127;
26 varint
[--pos
] = 128 | (--value
& 127);
28 memcpy(buf
, varint
+ pos
, sizeof(varint
) - pos
);
29 return sizeof(varint
) - pos
;