MCPcopy Create free account
hub / github.com/git/git / get_var_int

Function get_var_int

reftable/record.c:22–54  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20static void *reftable_record_data(struct reftable_record *rec);
21
22int get_var_int(uint64_t *dest, struct string_view *in)
23{
24 const unsigned char *buf = in->buf;
25 unsigned char c;
26 uint64_t val;
27
28 if (!in->len)
29 return -1;
30 c = *buf++;
31 val = c & 0x7f;
32
33 while (c & 0x80) {
34 /*
35 * We use a micro-optimization here: whenever we see that the
36 * 0x80 bit is set, we know that the remainder of the value
37 * cannot be 0. The zero-values thus doesn't need to be encoded
38 * at all, which is why we subtract 1 when encoding and add 1
39 * when decoding.
40 *
41 * This allows us to save a byte in some edge cases.
42 */
43 val += 1;
44 if (!val || (val & (uint64_t)(~0ULL << (64 - 7))))
45 return -1; /* overflow */
46 if (buf >= in->buf + in->len)
47 return -1;
48 c = *buf++;
49 val = (val << 7) + (c & 0x7f);
50 }
51
52 *dest = val;
53 return buf - in->buf;
54}
55
56int put_var_int(struct string_view *dest, uint64_t value)
57{

Calls

no outgoing calls