File Coverage

deps/libgit2/src/varint.c
Criterion Covered Total %
statement 0 23 0.0
branch 0 12 0.0
condition n/a
subroutine n/a
pod n/a
total 0 35 0.0


line stmt bran cond sub pod time code
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 0         while (c & 128) {
16 0           val += 1;
17 0 0         if (!val || MSB(val, 7)) {
    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 0         while (value >>= 7)
36 0           varint[--pos] = 128 | (--value & 127);
37 0 0         if (buf) {
38 0 0         if (bufsize < (sizeof(varint) - pos))
39 0           return -1;
40 0           memcpy(buf, varint + pos, sizeof(varint) - pos);
41             }
42 0           return sizeof(varint) - pos;
43             }