Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/varint.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / varint.c` 30 lines ยท 661 bytes Raw Blame History 1 #include "git-compat-util.h" 2 #include "varint.h" 3 4 uint64_t decode_varint(const unsigned char **bufp) 5 { 6 const unsigned char *buf = *bufp; 7 unsigned char c = *buf++; 8 uint64_t val = c & 127; 9 while (c & 128) { 10 val += 1; 11 if (!val || MSB(val, 7)) 12 return 0; /* overflow */ 13 c = *buf++; 14 val = (val << 7) + (c & 127); 15 } 16 *bufp = buf; 17 return val; 18 } 19 20 uint8_t encode_varint(uint64_t value, unsigned char *buf) 21 { 22 unsigned char varint[16]; 23 unsigned pos = sizeof(varint) - 1; 24 varint[pos] = value & 127; 25 while (value >>= 7) 26 varint[--pos] = 128 | (--value & 127); 27 if (buf) 28 memcpy(buf, varint + pos, sizeof(varint) - pos); 29 return sizeof(varint) - pos; 30 }