c-multiaddr/base58.c

202 lines
5.3 KiB
C
Raw Normal View History

2016-11-06 01:57:36 +00:00
/*
* Copyright 2012-2014 Luke Dashjr
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the standard MIT license. See COPYING for more details.
*/
#include <string.h>
2016-11-09 22:53:11 +00:00
#include <math.h>
#include <stdint.h>
#include <sys/types.h>
2016-11-06 01:57:36 +00:00
2016-11-09 22:53:11 +00:00
static const char b58digits_ordered[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
2016-11-06 01:57:36 +00:00
static const int8_t b58digits_map[] = {
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1,-1,-1,-1,-1,
-1, 9,10,11,12,13,14,15, 16,-1,17,18,19,20,21,-1,
22,23,24,25,26,27,28,29, 30,31,32,-1,-1,-1,-1,-1,
-1,33,34,35,36,37,38,39, 40,41,42,43,-1,44,45,46,
47,48,49,50,51,52,53,54, 55,56,57,-1,-1,-1,-1,-1,
};
2016-11-09 22:53:11 +00:00
/**
* convert a base58 encoded string into a binary array
* @param b58 the base58 encoded string
* @param base58_size the size of the encoded string
* @param bin the results buffer
* @param binszp the size of the results buffer
* @returns true(1) on success
*/
int multiaddr_encoding_base58_decode(const char* b58, size_t base58_size, unsigned char** bin, size_t* binszp)
2016-11-06 01:57:36 +00:00
{
size_t binsz = *binszp;
2016-11-09 22:53:11 +00:00
const unsigned char* b58u = (const void*)b58;
unsigned char* binu = *bin;
2016-11-06 01:57:36 +00:00
size_t outisz = (binsz + 3) / 4;
uint32_t outi[outisz];
uint64_t t;
uint32_t c;
size_t i, j;
uint8_t bytesleft = binsz % 4;
uint32_t zeromask = bytesleft ? (0xffffffff << (bytesleft * 8)) : 0;
unsigned zerocount = 0;
2016-11-09 22:53:11 +00:00
size_t b58sz;
2016-11-06 01:57:36 +00:00
2016-11-09 22:53:11 +00:00
b58sz = strlen(b58);
2016-11-06 01:57:36 +00:00
memset(outi, 0, outisz * sizeof(*outi));
// Leading zeros, just count
2016-11-09 22:53:11 +00:00
for (i = 0; i < b58sz && !b58digits_map[b58u[i]]; ++i) {
2016-11-06 01:57:36 +00:00
++zerocount;
2016-11-09 22:53:11 +00:00
}
2016-11-06 01:57:36 +00:00
2016-11-09 22:53:11 +00:00
for (; i < b58sz; ++i) {
if (b58u[i] & 0x80) {
2016-11-06 01:57:36 +00:00
// High-bit set on invalid digit
2016-11-09 22:53:11 +00:00
return 0;
}
if (b58digits_map[b58u[i]] == -1) {
2016-11-06 01:57:36 +00:00
// Invalid base58 digit
2016-11-09 22:53:11 +00:00
return 0;
}
2016-11-06 01:57:36 +00:00
c = (unsigned)b58digits_map[b58u[i]];
2016-11-09 22:53:11 +00:00
for (j = outisz; j--;) {
2016-11-06 01:57:36 +00:00
t = ((uint64_t)outi[j]) * 58 + c;
c = (t & 0x3f00000000) >> 32;
outi[j] = t & 0xffffffff;
}
2016-11-09 22:53:11 +00:00
if (c) {
2016-11-06 01:57:36 +00:00
// Output number too big (carry to the next int32)
2016-11-09 22:53:11 +00:00
memset(outi, 0, outisz * sizeof(*outi));
return 0;
}
if (outi[0] & zeromask) {
2016-11-06 01:57:36 +00:00
// Output number too big (last int32 filled too far)
2016-11-09 22:53:11 +00:00
memset(outi, 0, outisz * sizeof(*outi));
return 0;
}
2016-11-06 01:57:36 +00:00
}
j = 0;
switch (bytesleft) {
case 3:
2016-11-09 22:53:11 +00:00
*(binu++) = (outi[0] & 0xff0000) >> 16;
2016-11-06 01:57:36 +00:00
case 2:
2016-11-09 22:53:11 +00:00
*(binu++) = (outi[0] & 0xff00) >> 8;
2016-11-06 01:57:36 +00:00
case 1:
2016-11-09 22:53:11 +00:00
*(binu++) = (outi[0] & 0xff);
2016-11-06 01:57:36 +00:00
++j;
default:
break;
}
2016-11-09 22:53:11 +00:00
for (; j < outisz; ++j) {
2016-11-06 01:57:36 +00:00
*(binu++) = (outi[j] >> 0x18) & 0xff;
*(binu++) = (outi[j] >> 0x10) & 0xff;
2016-11-09 22:53:11 +00:00
*(binu++) = (outi[j] >> 8) & 0xff;
*(binu++) = (outi[j] >> 0) & 0xff;
2016-11-06 01:57:36 +00:00
}
// Count canonical base58 byte count
2016-11-09 22:53:11 +00:00
binu = *bin;
for (i = 0; i < binsz; ++i) {
if (binu[i]) {
2016-11-06 01:57:36 +00:00
break;
2016-11-09 22:53:11 +00:00
}
2016-11-06 01:57:36 +00:00
--*binszp;
}
*binszp += zerocount;
2016-11-09 22:53:11 +00:00
memset(outi, 0, outisz * sizeof(*outi));
return 1;
2016-11-06 01:57:36 +00:00
}
2016-11-09 22:53:11 +00:00
/**
* encode an array of bytes into a base58 string
* @param binary_data the data to be encoded
* @param binary_data_size the size of the data to be encoded
* @param base58 the results buffer
* @param base58_size the size of the results buffer
* @returns true(1) on success
*/
//int libp2p_crypto_encoding_base58_encode(const unsigned char* binary_data, size_t binary_data_size, unsigned char* base58, size_t* base58_size)
int multiaddr_encoding_base58_encode(const unsigned char* data, size_t binsz, unsigned char** b58, size_t* b58sz)
2016-11-06 01:57:36 +00:00
{
2016-11-09 22:53:11 +00:00
const uint8_t* bin = data;
2016-11-06 01:57:36 +00:00
int carry;
ssize_t i, j, high, zcount = 0;
size_t size;
2016-11-09 22:53:11 +00:00
while (zcount < (ssize_t)binsz && !bin[zcount]) {
2016-11-06 01:57:36 +00:00
++zcount;
2016-11-09 22:53:11 +00:00
}
2016-11-06 01:57:36 +00:00
size = (binsz - zcount) * 138 / 100 + 1;
uint8_t buf[size];
memset(buf, 0, size);
2016-11-09 22:53:11 +00:00
for (i = zcount, high = size - 1; i < (ssize_t)binsz; ++i, high = j) {
for (carry = bin[i], j = size - 1; (j > high) || carry; --j) {
2016-11-06 01:57:36 +00:00
carry += 256 * buf[j];
buf[j] = carry % 58;
carry /= 58;
}
}
2016-11-09 22:53:11 +00:00
for (j = 0; j < (ssize_t)size && !buf[j]; ++j)
;
2016-11-06 01:57:36 +00:00
2016-11-09 22:53:11 +00:00
if (*b58sz <= zcount + size - j) {
2016-11-06 01:57:36 +00:00
*b58sz = zcount + size - j + 1;
2016-11-09 22:53:11 +00:00
memset(buf, 0, size);
return 0;
2016-11-06 01:57:36 +00:00
}
2016-11-09 22:53:11 +00:00
if (zcount) {
2016-11-06 01:57:36 +00:00
memset(b58, '1', zcount);
2016-11-09 22:53:11 +00:00
}
for (i = zcount; j < (ssize_t)size; ++i, ++j) {
(*b58)[i] = b58digits_ordered[buf[j]];
}
(*b58)[i] = '\0';
2016-11-06 01:57:36 +00:00
*b58sz = i + 1;
2016-11-09 22:53:11 +00:00
memset(buf, 0, size);
return 1;
2016-11-06 01:57:36 +00:00
}
2016-11-09 22:53:11 +00:00
/***
* calculate the size of the binary results based on an incoming base58 string
* @param base58_string the string
* @returns the size in bytes had the string been decoded
*/
size_t multiaddr_encoding_base58_decode_size(const unsigned char* base58_string) {
2016-11-09 22:53:11 +00:00
size_t string_length = strlen((char*)base58_string);
size_t decoded_length = 0;
size_t radix = strlen(b58digits_ordered);
double bits_per_digit = log2(radix);
decoded_length = floor(string_length * bits_per_digit / 8);
return decoded_length;
}
/**
* calculate the max length in bytes of an encoding of n source bits
* @param base58_string the string
* @returns the maximum size in bytes had the string been decoded
*/
size_t multiaddr_encoding_base58_decode_max_size(const unsigned char* base58_string) {
2016-11-09 22:53:11 +00:00
size_t string_length = strlen((char*)base58_string);
size_t decoded_length = 0;
size_t radix = strlen(b58digits_ordered);
double bits_per_digit = log2(radix);
decoded_length = ceil(string_length * bits_per_digit / 8);
return decoded_length;
}