Compare commits

..

No commits in common. "master" and "v0.1.0" have entirely different histories.

24 changed files with 67 additions and 195 deletions

View File

@ -1,48 +0,0 @@
os: linux
language: c
compiler: gcc
sudo: false
matrix:
include:
- compiler: ": Complete"
env: CONFIGURE_OPTS="--enable-tool --enable-static --enable-shared" MAKE_CHECK=1
addons:
apt:
packages:
- build-essential
- libgcrypt11-dev
- compiler: ": No tool/tests"
env: CONFIGURE_OPTS="--disable-tool --enable-static --enable-shared"
addons:
apt:
packages:
- build-essential
- compiler: ": Win32 - No tool/tests"
env: CONFIGURE_OPTS="--disable-tool --host=i686-w64-mingw32 --enable-static --enable-shared"
addons:
apt:
packages:
- gcc-mingw-w64-i686
- binutils-mingw-w64-i686
- mingw-w64-i686-dev
- compiler: ": Win64 - No tool/tests"
env: CONFIGURE_OPTS="--disable-tool --host=x86_64-w64-mingw32 --enable-static --enable-shared"
addons:
apt:
packages:
- gcc-mingw-w64-x86-64
- binutils-mingw-w64-x86-64
- mingw-w64-x86-64-dev
exclude:
- compiler: gcc
install:
- if [ -n "$PACKAGES" ]; then travis_retry sudo apt-get update; fi
- if [ -n "$PACKAGES" ]; then travis_retry sudo apt-get install --no-install-recommends --no-upgrade -qq $PACKAGES; fi
script:
- unset CC
- ./autogen.sh
- ./configure $CONFIGURE_OPTS || tail -n 1000 config.log
- make
- test -z "$MAKE_CHECK" || make check
- make install DESTDIR=$PWD/ii
- cd ii && find

View File

@ -1,2 +0,0 @@
Luke Dashjr <luke-jr+libbase58@utopios.org>
Huang Le <4tarhl@gmail.com>

19
COPYING
View File

@ -1,19 +0,0 @@
Copyright (c) 2014 Luke Dashjr
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

20
INSTALL
View File

@ -1,20 +0,0 @@
Installation
--------------------
# Install libgcrypt first, e.g. via `brew install libgcrypt` on OS X or libgcrypt-dev on Linux
# Generate the final build scripts
./autogen.sh
# Build the CLI and library
./configure && make
Dependencies
--------------------
Many of the test scripts depend on the "xxd" tool to convert between hexadecimal and binary.
If this tool is not installed on your Operating System, you can probably get it as part of
the "vim" editor from https://www.vim.org/ or by installing the vim-share package.
Troubleshooting
--------------------
If libgcrypt isn't found after install, set AM_PATH_LIBGCRYPT env var to libgcrypt path
prior to running autogen.sh. For example, on OS X with ver 1.7.6:
export AM_PATH_LIBGCRYPT="/usr/local/Cellar/libgcrypt/1.7.6/lib"

View File

@ -14,7 +14,7 @@ pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libbase58.pc
dist_noinst_SCRIPTS = autogen.sh
dist_doc_DATA = AUTHORS COPYING INSTALL README.md
dist_doc_DATA = README
if USE_TOOL
bin_PROGRAMS = base58
@ -33,12 +33,9 @@ TESTS = \
tests/decode-zero.sh \
tests/encode.sh \
tests/encode-b58c.sh \
tests/encode-b58c-high.sh \
tests/encode-fail.sh \
tests/encode-neg-index.sh \
tests/encode-small.sh
SH_LOG_COMPILER = /bin/sh
AM_TESTS_ENVIRONMENT = PATH='$(abs_top_builddir)':"$$PATH"; export PATH;
TESTS_ENVIRONMENT = $(AM_TESTS_ENVIRONMENT)
AM_TESTS_ENVIRONMENT = PATH='$(srcdir)':"$$PATH"; export PATH;
endif
TEST_EXTENSIONS = .sh

View File

@ -3,11 +3,8 @@ Initialisation
Before you can use libbase58 for base58check, you must provide a SHA256
function. The required function signature is:
bool my_sha256(void *digest, const void *data, size_t datasz)
Simply assign your function to b58_sha256_impl:
b58_sha256_impl = my_sha256;
This is only required if base58check is used. Raw base58 does not need SHA256.
@ -18,9 +15,7 @@ Decoding Base58
Simply allocate a buffer to store the binary data in, and set a variable with
the buffer size, and call the b58tobin function:
bool b58tobin(void *bin, size_t *binsz, const char *b58, size_t b58sz)
The "canonical" base58 byte length will be assigned to binsz on success, which
may be larger than the actual buffer if the input has many leading zeros.
Regardless of the canonical byte length, the full binary buffer will be used.
@ -33,9 +28,7 @@ Validating Base58Check
After calling b58tobin, you can validate base58check data using the b58check
function:
int b58check(const void *bin, size_t binsz, const char *b58, size_t b58sz)
Call it with the same buffers used for b58tobin. If the return value is
negative, an error occurred. Otherwise, the return value is the base58check
"version" byte from the decoded data.
@ -46,9 +39,7 @@ Encoding Base58
Allocate a string to store the base58 content, create a size_t variable with the
size of that allocation, and call:
bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz)
Note that you must pass a pointer to the string size variable, not the size
itself. When b58enc returns, the variable will be modified to contain the actual
number of bytes used (including the null terminator). If encoding fails for any
@ -59,8 +50,7 @@ return false. Otherwise, it returns true to indicate success.
Encoding Base58Check
--------------------
Targeting base58check is done similarly to raw base58 encoding, but you must
Targetting base58check is done similarly to raw base58 encoding, but you must
also provide a version byte:
bool b58check_enc(char *b58c, size_t *b58c_sz, uint8_t ver,
const void *data, size_t datasz)

View File

@ -16,8 +16,6 @@
#include <stdint.h>
#include <string.h>
#include "libbase58.h"
bool (*b58_sha256_impl)(void *, const void *, size_t) = NULL;
static const int8_t b58digits_map[] = {
@ -31,34 +29,27 @@ static const int8_t b58digits_map[] = {
47,48,49,50,51,52,53,54, 55,56,57,-1,-1,-1,-1,-1,
};
typedef uint64_t b58_maxint_t;
typedef uint32_t b58_almostmaxint_t;
#define b58_almostmaxint_bits (sizeof(b58_almostmaxint_t) * 8)
static const b58_almostmaxint_t b58_almostmaxint_mask = ((((b58_maxint_t)1) << b58_almostmaxint_bits) - 1);
bool b58tobin(void *bin, size_t *binszp, const char *b58, size_t b58sz)
{
size_t binsz = *binszp;
const unsigned char *b58u = (void*)b58;
unsigned char *binu = bin;
size_t outisz = (binsz + sizeof(b58_almostmaxint_t) - 1) / sizeof(b58_almostmaxint_t);
b58_almostmaxint_t outi[outisz];
b58_maxint_t t;
b58_almostmaxint_t c;
size_t outisz = (binsz + 3) / 4;
uint32_t outi[outisz];
uint64_t t;
uint32_t c;
size_t i, j;
uint8_t bytesleft = binsz % sizeof(b58_almostmaxint_t);
b58_almostmaxint_t zeromask = bytesleft ? (b58_almostmaxint_mask << (bytesleft * 8)) : 0;
uint8_t bytesleft = binsz % 4;
uint32_t zeromask = ~((1 << ((bytesleft ?: 4) * 8)) - 1);
unsigned zerocount = 0;
if (!b58sz)
b58sz = strlen(b58);
for (i = 0; i < outisz; ++i) {
outi[i] = 0;
}
memset(outi, 0, outisz * sizeof(*outi));
// Leading zeros, just count
for (i = 0; i < b58sz && b58u[i] == '1'; ++i)
for (i = 0; i < b58sz && !b58digits_map[b58u[i]]; ++i)
++zerocount;
for ( ; i < b58sz; ++i)
@ -69,12 +60,12 @@ bool b58tobin(void *bin, size_t *binszp, const char *b58, size_t b58sz)
if (b58digits_map[b58u[i]] == -1)
// Invalid base58 digit
return false;
c = (unsigned)b58digits_map[b58u[i]];
c = b58digits_map[b58u[i]];
for (j = outisz; j--; )
{
t = ((b58_maxint_t)outi[j]) * 58 + c;
c = t >> b58_almostmaxint_bits;
outi[j] = t & b58_almostmaxint_mask;
t = ((uint64_t)outi[j]) * 58 + c;
c = (t & 0x3f00000000) >> 32;
outi[j] = t & 0xffffffff;
}
if (c)
// Output number too big (carry to the next int32)
@ -85,18 +76,24 @@ bool b58tobin(void *bin, size_t *binszp, const char *b58, size_t b58sz)
}
j = 0;
if (bytesleft) {
for (i = bytesleft; i > 0; --i) {
*(binu++) = (outi[0] >> (8 * (i - 1))) & 0xff;
}
++j;
switch (bytesleft) {
case 3:
*(binu++) = (outi[0] & 0xff0000) >> 16;
case 2:
*(binu++) = (outi[0] & 0xff00) >> 8;
case 1:
*(binu++) = (outi[0] & 0xff);
++j;
default:
break;
}
for (; j < outisz; ++j)
{
for (i = sizeof(*outi); i > 0; --i) {
*(binu++) = (outi[j] >> (8 * (i - 1))) & 0xff;
}
*(binu++) = outi[j] >> 0x18;
*(binu++) = outi[j] >> 0x10;
*(binu++) = outi[j] >> 8;
*(binu++) = outi[j];
}
// Count canonical base58 byte count
@ -145,8 +142,7 @@ static const char b58digits_ordered[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdef
bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz)
{
const uint8_t *bin = data;
int carry;
size_t i, j, high, zcount = 0;
int i, j, carry, high, zcount = 0;
size_t size;
while (zcount < binsz && !bin[zcount])
@ -163,10 +159,6 @@ bool b58enc(char *b58, size_t *b58sz, const void *data, size_t binsz)
carry += 256 * buf[j];
buf[j] = carry % 58;
carry /= 58;
if (!j) {
// Otherwise j wraps to maxint which is > high
break;
}
}
}

View File

@ -36,7 +36,7 @@ void usage(const char *prog)
int main(int argc, char **argv)
{
bool b58c = false;
size_t decode = 0;
int decode = 0;
int opt;
while ( (opt = getopt(argc, argv, "cd:h")) != -1)
{
@ -47,39 +47,34 @@ int main(int argc, char **argv)
b58_sha256_impl = my_sha256;
break;
case 'd':
{
int i = atoi(optarg);
if (i < 0 || (uintmax_t)i >= SIZE_MAX)
usage(argv[0]);
decode = (size_t)i;
decode = atoi(optarg);
break;
}
default:
usage(argv[0]);
}
}
size_t rt;
union {
uint8_t *b;
char *s;
} r;
void *r;
if (optind >= argc)
{
rt = 0;
r.b = NULL;
r = NULL;
while (!feof(stdin))
{
r.b = realloc(r.b, rt + 0x100);
rt += fread(&r.b[rt], 1, 0x100, stdin);
r = realloc(r, rt + 0x100);
rt += fread(r + rt, 1, 0x100, stdin);
}
if (decode)
while (isspace(r.s[rt-1]))
{
char *rs = r;
while (isspace(rs[rt-1]))
--rt;
}
}
else
{
r.s = argv[optind];
r = argv[optind];
rt = strlen(argv[optind]);
}
@ -87,11 +82,11 @@ int main(int argc, char **argv)
{
uint8_t bin[decode];
size_t ssz = decode;
if (!b58tobin(bin, &ssz, r.s, rt))
if (!b58tobin(bin, &ssz, r, rt))
return 2;
if (b58c)
{
int chk = b58check(bin, decode, r.s, rt);
int chk = b58check(bin, decode, r, rt);
if (chk < 0)
return chk;
if (fwrite(bin, decode, 1, stdout) != 1)
@ -120,11 +115,15 @@ int main(int argc, char **argv)
char s[ssz];
bool rv;
if (b58c)
rv = rt && b58check_enc(s, &ssz, r.b[0], &r.b[1], rt-1);
{
uint8_t *verbyte = r;
r += 1;
rv = rt && b58check_enc(s, &ssz, *verbyte, r, rt-1);
}
else
rv = b58enc(s, &ssz, r.b, rt);
rv = b58enc(s, &ssz, r, rt);
if (!rv)
return 2;
puts(s);
}
}
}

View File

@ -5,12 +5,12 @@ dnl * under the terms of the standard MIT license. See COPYING for more details
AC_INIT(
[libbase58],
[0.1.4],
[0.1],
[luke_libbase58@dashjr.org],
[libbase58])
AC_CONFIG_AUX_DIR([.])
AC_PREREQ([2.59])
AM_INIT_AUTOMAKE([1.11 -Wall dist-xz foreign])
AM_INIT_AUTOMAKE([1.12 -Wall dist-xz foreign])
m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
AC_PROG_CC_C99
@ -18,7 +18,7 @@ m4_ifdef([AM_PROG_AR], [AM_PROG_AR])
LT_INIT([])
# http://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
AC_SUBST([LIBBASE58_SO_VERSION], [0:2:0])
AC_SUBST([LIBBASE58_SO_VERSION], [0:0:0])
AC_CONFIG_FILES([Makefile
libbase58.pc:libbase58.pc.in
@ -26,24 +26,20 @@ AC_CONFIG_FILES([Makefile
AC_CHECK_LIB([ws2_32], [strchr])
m4_ifdef([AM_PATH_LIBGCRYPT], [
AC_ARG_ENABLE([tool],
[AC_HELP_STRING([--disable-tool],[Compile command line base58 tool (default enabled)])],
[use_tool=$enableval],
[use_tool=auto])
if test x$use_tool != xno; then
AM_PATH_LIBGCRYPT([],[
use_tool=yes
],[
if test x$use_tool = xyes; then
AC_MSG_ERROR([libgcrypt not found; use --disable-tool])
fi
use_tool=no
])
fi
],[
m4_warn([syntax], [AM_PATH_LIBGCRYPT missing; CLI tool will not be available])
])
AC_ARG_ENABLE([tool],
[AC_HELP_STRING([--disable-tool],[Compile command line base58 tool (default enabled)])],
[use_tool=$enableval],
[use_tool=auto])
if test x$use_tool != xno; then
AM_PATH_LIBGCRYPT([],[
use_tool=yes
],[
if test x$use_tool = xyes; then
AC_MSG_ERROR([libgcrypt not found; use --disable-tool])
fi
use_tool=no
])
fi
AM_CONDITIONAL([USE_TOOL], [test x$use_tool = xyes])
AC_OUTPUT

0
tests/decode-b58c-fail.sh Executable file → Normal file
View File

0
tests/decode-b58c-null.sh Executable file → Normal file
View File

0
tests/decode-b58c-toolong.sh Executable file → Normal file
View File

0
tests/decode-b58c-tooshort.sh Executable file → Normal file
View File

0
tests/decode-b58c.sh Executable file → Normal file
View File

View File

@ -1,3 +0,0 @@
#!/bin/sh
hex=$(echo 993233 | xxd -r -p | base58 -d 25 || echo FAIL)
test "x${hex}" = "xFAIL"

View File

@ -1,3 +0,0 @@
#!/bin/sh
hex=$(echo 319932 | xxd -r -p | base58 -d 25 || echo FAIL)
test "x${hex}" = "xFAIL"

0
tests/decode-small.sh Executable file → Normal file
View File

0
tests/decode-zero.sh Executable file → Normal file
View File

0
tests/decode.sh Executable file → Normal file
View File

View File

@ -1,3 +0,0 @@
#!/bin/sh
b58=$(echo 'ff5a1fc5dd9e6f03819fca94a2d89669469667f9a0' | xxd -r -p | base58 -c)
test x$b58 = x2mkQLxaN3Y4CwN5E9rdMWNgsXX7VS6UnfeT

0
tests/encode-b58c.sh Executable file → Normal file
View File

0
tests/encode-fail.sh Executable file → Normal file
View File

View File

@ -1,4 +0,0 @@
#!/bin/sh
# This input causes the loop iteration counter to go negative
b58=$(echo '00CEF022FA' | xxd -r -p | base58)
test x$b58 = x16Ho7Hs

0
tests/encode-small.sh Executable file → Normal file
View File