]>
Commit | Line | Data |
---|---|---|
aeb5ee5a DT |
1 | /* |
2 | * Copyright (C) the libgit2 contributors. All rights reserved. | |
3 | * | |
4 | * This file is part of libgit2, distributed under the GNU GPL v2 with | |
5 | * a Linking Exception. For full terms see the included COPYING file. | |
6 | */ | |
7 | ||
aeb5ee5a DT |
8 | #include "varint.h" |
9 | ||
10 | uintmax_t git_decode_varint(const unsigned char *bufp, size_t *varint_len) | |
11 | { | |
12 | const unsigned char *buf = bufp; | |
13 | unsigned char c = *buf++; | |
14 | uintmax_t val = c & 127; | |
15 | while (c & 128) { | |
16 | val += 1; | |
17 | if (!val || MSB(val, 7)) { | |
18 | /* This is not a valid varint_len, so it signals | |
19 | the error */ | |
20 | *varint_len = 0; | |
21 | return 0; /* overflow */ | |
22 | } | |
23 | c = *buf++; | |
24 | val = (val << 7) + (c & 127); | |
25 | } | |
26 | *varint_len = buf - bufp; | |
27 | return val; | |
28 | } | |
29 | ||
30 | int git_encode_varint(unsigned char *buf, size_t bufsize, uintmax_t value) | |
31 | { | |
32 | unsigned char varint[16]; | |
33 | unsigned pos = sizeof(varint) - 1; | |
34 | varint[pos] = value & 127; | |
35 | while (value >>= 7) | |
36 | varint[--pos] = 128 | (--value & 127); | |
37 | if (buf) { | |
8a5e7aae | 38 | if (bufsize < (sizeof(varint) - pos)) |
aeb5ee5a DT |
39 | return -1; |
40 | memcpy(buf, varint + pos, sizeof(varint) - pos); | |
41 | } | |
42 | return sizeof(varint) - pos; | |
43 | } |