-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
varint: make it available outside the context of pack
Signed-off-by: Junio C Hamano <gitster@pobox.com>
- Loading branch information
Junio C Hamano
committed
Apr 3, 2012
1 parent
e5056c0
commit d2c1898
Showing
3 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include "varint.h" | ||
|
||
uintmax_t decode_varint(const unsigned char **bufp) | ||
{ | ||
const unsigned char *buf = *bufp; | ||
unsigned char c = *buf++; | ||
uintmax_t val = c & 127; | ||
while (c & 128) { | ||
val += 1; | ||
if (!val || MSB(val, 7)) | ||
return 0; /* overflow */ | ||
c = *buf++; | ||
val = (val << 7) + (c & 127); | ||
} | ||
*bufp = buf; | ||
return val; | ||
} | ||
|
||
int encode_varint(uintmax_t value, unsigned char *buf) | ||
{ | ||
unsigned char varint[16]; | ||
unsigned pos = sizeof(varint) - 1; | ||
varint[pos] = value & 127; | ||
while (value >>= 7) | ||
varint[--pos] = 128 | (--value & 127); | ||
if (buf) | ||
memcpy(buf, varint + pos, sizeof(varint) - pos); | ||
return sizeof(varint) - pos; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef VARINT_H | ||
#define VARINT_H | ||
|
||
#include "git-compat-util.h" | ||
|
||
extern int encode_varint(uintmax_t, unsigned char *); | ||
extern uintmax_t decode_varint(const unsigned char **); | ||
|
||
#endif /* VARINT_H */ |