Coverage Report

Created: 2026-04-12 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libgit2/src/util/varint.c
Line
Count
Source
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
uintmax_t git_decode_varint(const unsigned char *bufp, size_t *varint_len)
11
0
{
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) {
16
0
    val += 1;
17
0
    if (!val || MSB(val, 7)) {
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
0
    }
23
0
    c = *buf++;
24
0
    val = (val << 7) + (c & 127);
25
0
  }
26
0
  *varint_len = buf - bufp;
27
0
  return val;
28
0
}
29
30
int git_encode_varint(unsigned char *buf, size_t bufsize, uintmax_t value)
31
0
{
32
0
  unsigned char varint[16];
33
0
  unsigned pos = sizeof(varint) - 1;
34
0
  varint[pos] = value & 127;
35
0
  while (value >>= 7)
36
0
    varint[--pos] = 128 | (--value & 127);
37
0
  if (buf) {
38
0
    if (bufsize < (sizeof(varint) - pos))
39
0
      return -1;
40
0
    memcpy(buf, varint + pos, sizeof(varint) - pos);
41
0
  }
42
0
  return sizeof(varint) - pos;
43
0
}