-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
172 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,4 @@ | |
/Makefile.in | ||
/missing | ||
/stamp-h1 | ||
/test/varint |
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,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"); | ||
} |
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,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; | ||
} |
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,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_ */ |