Skip to content

Commit

Permalink
Add varint implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rescrv committed Jun 29, 2016
1 parent f2a20cd commit 065ecc3
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@
/Makefile.in
/missing
/stamp-h1
/test/varint
6 changes: 6 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ libmacaroons_la_SOURCES += base64.c
libmacaroons_la_SOURCES += macaroons.c
libmacaroons_la_SOURCES += packet.c
libmacaroons_la_SOURCES += port.c
libmacaroons_la_SOURCES += varint.c
libmacaroons_la_SOURCES += explicit_bzero.c
libmacaroons_la_SOURCES += timingsafe_bcmp.c
libmacaroons_la_SOURCES += tweetnacl.c
Expand All @@ -69,6 +70,7 @@ EXTRA_DIST += test/python-hmac-sanity-check.sh
EXTRA_DIST += test/readme.sh

check_LTLIBRARIES = libmacaroons-shim.la
check_PROGRAMS = test/varint

libmacaroons_shim_la_SOURCES = shim.c explicit_bzero.c
libmacaroons_shim_la_LDFLAGS = -module -avoid-version -rpath /evil/libtool/hack/to/force/shared/lib/creation
Expand All @@ -78,6 +80,10 @@ if ENABLE_PYTHON_BINDINGS
TESTS += test/python-hmac-sanity-check.sh
TESTS += test/readme.sh
endif
TESTS += test/varint

test_varint_SOURCES = test/varint.c varint.c
test_varint_CFLAGS = $(AM_CFLAGS) $(CFLAGS)

#################################### Python ####################################

Expand Down
79 changes: 79 additions & 0 deletions test/varint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/* Copyright (c) 2016, Robert Escriva
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of this project nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

/* need to rely upon assert always asserting */
#ifdef NDEBUG
#undef NDEBUG
#endif

/* C */
#include <assert.h>
#include <stdint.h>
#include <string.h>

/* macaroons */
#include "varint.h"

void
varint_verify(uint64_t value, const char* representation)
{
const unsigned sz = strlen(representation);
assert(sz % 2 == 0);
unsigned char buf[VARINT_MAX_SIZE];
uint64_t eulav;
assert(packvarint(value, buf) == buf + sz / 2);
assert(unpackvarint(buf, buf + VARINT_MAX_SIZE, &eulav) == buf + sz / 2);
assert(value == eulav);

for (unsigned i = 0; i < sz / 2; ++i)
{
char hex[3];
snprintf(hex, 3, "%02x", buf[i] & 0xff);
assert(hex[0] == representation[2 * i]);
assert(hex[1] == representation[2 * i + 1]);
}
}

int
main(int argc, const char* argv[])
{
(void)argc;
(void)argv;
varint_verify(0ULL, "00");
varint_verify(5ULL, "05");
varint_verify(127ULL, "7f");
varint_verify(128ULL, "8001");
varint_verify(16383ULL, "ff7f");
varint_verify(16384ULL, "808001");
varint_verify(16385ULL, "818001");
varint_verify(16386ULL, "828001");
varint_verify(16387ULL, "838001");
varint_verify(16388ULL, "848001");
varint_verify(3735928559ULL, "effdb6f50d");
varint_verify(18446744073709551615ULL, "ffffffffffffffffff01");
}
58 changes: 58 additions & 0 deletions varint.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// This code is derived from code distributed as part of Google LevelDB.
// The original is available in leveldb as util/coding.cc.
// This file was retrieved Jul 15, 2013 by Robert Escriva and imported into
// libe. This code is copied/modified from libe.

// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

/* C */
#include <stdint.h>
#include <stdlib.h>

/* macaroons */
#include "varint.h"

unsigned char*
packvarint(uint64_t v, unsigned char* ptr)
{
const unsigned B = 128;

while (v >= B)
{
*(ptr++) = (v & (B-1)) | B;
v >>= 7;
}

*(ptr++) = (unsigned char)(v);
return ptr;
}

const unsigned char*
unpackvarint(const unsigned char* ptr,
const unsigned char* end,
uint64_t* value)
{
uint64_t result = 0;

for (unsigned shift = 0; shift <= 63 && ptr < end; shift += 7)
{
uint64_t byte = *ptr & 0xff;
ptr++;

if (byte & 128)
{
// More bytes are present
result |= ((byte & 127) << shift);
}
else
{
result |= (byte << shift);
*value = result;
return ptr;
}
}

return NULL;
}
28 changes: 28 additions & 0 deletions varint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This code is derived from code distributed as part of Google LevelDB.
// The original is available in leveldb as util/coding.h.
// This file was retrieved Jul 15, 2013 by Robert Escriva and imported into
// libe. This code is copied/modified from libe.

// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.
//
// Endian-neutral encoding:
// * Fixed-length numbers are encoded with least-significant byte first
// * In addition we support variable length "varint" encoding
// * Strings are encoded prefixed by their length in varint format

#ifndef macaroons_varint_h_
#define macaroons_varint_h_

#define VARINT_MAX_SIZE 10

unsigned char*
packvarint(uint64_t value, unsigned char* ptr);

const unsigned char*
unpackvarint(const unsigned char* ptr,
const unsigned char* end,
uint64_t* value);

#endif /* macaroons_varint_h_ */

0 comments on commit 065ecc3

Please sign in to comment.