| 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 |
|
| |
| 8 |
|
| #include "varint.h" |
| 9 |
|
| |
| 10 |
0 |
| uintmax_t git_decode_varint(const unsigned char *bufp, size_t *varint_len) |
| 11 |
|
| { |
| 12 |
0 |
| const unsigned char *buf = bufp; |
| 13 |
0 |
| unsigned char c = *buf++; |
| 14 |
0 |
| uintmax_t val = c & 127; |
| 15 |
0 |
▶ | while (c & 128) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (c & 128) { |
0 |
0 |
0 |
|
| 16 |
0 |
| val += 1; |
| 17 |
0 |
▶ | if (!val || MSB(val, 7)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!val || MSB(val, 7)) { |
0 |
0 |
0 |
| if (!val || MSB(val, 7)) { |
0 |
0 |
0 |
|
| 18 |
|
| /* This is not a valid varint_len, so it signals |
| 19 |
|
| the error */ |
| 20 |
0 |
| *varint_len = 0; |
| 21 |
0 |
| return 0; /* overflow */ |
| 22 |
|
| } |
| 23 |
0 |
| c = *buf++; |
| 24 |
0 |
| val = (val << 7) + (c & 127); |
| 25 |
|
| } |
| 26 |
0 |
| *varint_len = buf - bufp; |
| 27 |
0 |
| return val; |
| 28 |
|
| } |
| 29 |
|
| |
| 30 |
0 |
| int git_encode_varint(unsigned char *buf, size_t bufsize, uintmax_t value) |
| 31 |
|
| { |
| 32 |
|
| unsigned char varint[16]; |
| 33 |
0 |
| unsigned pos = sizeof(varint) - 1; |
| 34 |
0 |
| varint[pos] = value & 127; |
| 35 |
0 |
▶ | while (value >>= 7) |
Branch: 1 branch
| Branch | True | False | Total |
| while (value >>= 7) |
0 |
0 |
0 |
|
| 36 |
0 |
| varint[--pos] = 128 | (--value & 127); |
| 37 |
0 |
▶ | if (buf) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (buf) { |
0 |
0 |
0 |
|
| 38 |
0 |
▶ | if (bufsize < (sizeof(varint) - pos)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (bufsize < (sizeof(varint) - pos)) |
0 |
0 |
0 |
|
| 39 |
0 |
| return -1; |
| 40 |
0 |
| memcpy(buf, varint + pos, sizeof(varint) - pos); |
| 41 |
|
| } |
| 42 |
0 |
| return sizeof(varint) - pos; |
| 43 |
|
| } |