]>
git.proxmox.com Git - libgit2.git/blob - src/varint.c
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
10 uintmax_t git_decode_varint(const unsigned char *bufp
, size_t *varint_len
)
12 const unsigned char *buf
= bufp
;
13 unsigned char c
= *buf
++;
14 uintmax_t val
= c
& 127;
17 if (!val
|| MSB(val
, 7)) {
18 /* This is not a valid varint_len, so it signals
21 return 0; /* overflow */
24 val
= (val
<< 7) + (c
& 127);
26 *varint_len
= buf
- bufp
;
30 int git_encode_varint(unsigned char *buf
, size_t bufsize
, uintmax_t value
)
32 unsigned char varint
[16];
33 unsigned pos
= sizeof(varint
) - 1;
34 varint
[pos
] = value
& 127;
36 varint
[--pos
] = 128 | (--value
& 127);
38 if (bufsize
< (sizeof(varint
) - pos
))
40 memcpy(buf
, varint
+ pos
, sizeof(varint
) - pos
);
42 return sizeof(varint
) - pos
;