summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid Turner <dturner@twopensource.com>2016-05-17 15:40:46 -0400
committerDavid Turner <dturner@twosigma.com>2016-08-10 14:19:06 -0400
commitaeb5ee5ab50a062aac02ca084b02582430669808 (patch)
tree55b875ce84341eceb06724f070eacf47dfafc447 /src
parent26a8617d96ba96875304d56a7ce698a85e559c43 (diff)
downloadlibgit2-aeb5ee5ab50a062aac02ca084b02582430669808.tar.gz
varint: Add varint encoding/decoding
This code is ported from git.git Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: David Turner <dturner@twopensource.com>
Diffstat (limited to 'src')
-rw-r--r--src/varint.c44
-rw-r--r--src/varint.h15
2 files changed, 59 insertions, 0 deletions
diff --git a/src/varint.c b/src/varint.c
new file mode 100644
index 000000000..2f868607c
--- /dev/null
+++ b/src/varint.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "common.h"
+#include "varint.h"
+
+uintmax_t git_decode_varint(const unsigned char *bufp, size_t *varint_len)
+{
+ const unsigned char *buf = bufp;
+ unsigned char c = *buf++;
+ uintmax_t val = c & 127;
+ while (c & 128) {
+ val += 1;
+ if (!val || MSB(val, 7)) {
+ /* This is not a valid varint_len, so it signals
+ the error */
+ *varint_len = 0;
+ return 0; /* overflow */
+ }
+ c = *buf++;
+ val = (val << 7) + (c & 127);
+ }
+ *varint_len = buf - bufp;
+ return val;
+}
+
+int git_encode_varint(unsigned char *buf, size_t bufsize, uintmax_t value)
+{
+ unsigned char varint[16];
+ unsigned pos = sizeof(varint) - 1;
+ varint[pos] = value & 127;
+ while (value >>= 7)
+ varint[--pos] = 128 | (--value & 127);
+ if (buf) {
+ if (bufsize < pos)
+ return -1;
+ memcpy(buf, varint + pos, sizeof(varint) - pos);
+ }
+ return sizeof(varint) - pos;
+}
diff --git a/src/varint.h b/src/varint.h
new file mode 100644
index 000000000..650ec7d2a
--- /dev/null
+++ b/src/varint.h
@@ -0,0 +1,15 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_varint_h__
+#define INCLUDE_varint_h__
+
+#include <stdint.h>
+
+extern int git_encode_varint(unsigned char *, size_t, uintmax_t);
+extern uintmax_t git_decode_varint(const unsigned char *, size_t *);
+
+#endif