base58.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright 2012-2014 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the standard MIT license. See COPYING for more details.
  6. */
  7. #include <string.h>
  8. #include <math.h>
  9. #include <stdint.h>
  10. #include <sys/types.h>
  11. static const char b58digits_ordered[] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
  12. static const int8_t b58digits_map[] = {
  13. -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
  14. -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
  15. -1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
  16. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1,-1,-1,-1,-1,
  17. -1, 9,10,11,12,13,14,15, 16,-1,17,18,19,20,21,-1,
  18. 22,23,24,25,26,27,28,29, 30,31,32,-1,-1,-1,-1,-1,
  19. -1,33,34,35,36,37,38,39, 40,41,42,43,-1,44,45,46,
  20. 47,48,49,50,51,52,53,54, 55,56,57,-1,-1,-1,-1,-1,
  21. };
  22. /**
  23. * convert a base58 encoded string into a binary array
  24. * @param b58 the base58 encoded string
  25. * @param base58_size the size of the encoded string
  26. * @param bin the results buffer
  27. * @param binszp the size of the results buffer
  28. * @returns true(1) on success
  29. */
  30. int multiaddr_encoding_base58_decode(const char* b58, size_t base58_size, unsigned char** bin, size_t* binszp)
  31. {
  32. size_t binsz = *binszp;
  33. const unsigned char* b58u = (const void*)b58;
  34. unsigned char* binu = *bin;
  35. size_t outisz = (binsz + 3) / 4;
  36. uint32_t outi[outisz];
  37. uint64_t t;
  38. uint32_t c;
  39. size_t i, j;
  40. uint8_t bytesleft = binsz % 4;
  41. uint32_t zeromask = bytesleft ? (0xffffffff << (bytesleft * 8)) : 0;
  42. unsigned zerocount = 0;
  43. size_t b58sz;
  44. b58sz = strlen(b58);
  45. memset(outi, 0, outisz * sizeof(*outi));
  46. // Leading zeros, just count
  47. for (i = 0; i < b58sz && !b58digits_map[b58u[i]]; ++i) {
  48. ++zerocount;
  49. }
  50. for (; i < b58sz; ++i) {
  51. if (b58u[i] & 0x80) {
  52. // High-bit set on invalid digit
  53. return 0;
  54. }
  55. if (b58digits_map[b58u[i]] == -1) {
  56. // Invalid base58 digit
  57. return 0;
  58. }
  59. c = (unsigned)b58digits_map[b58u[i]];
  60. for (j = outisz; j--;) {
  61. t = ((uint64_t)outi[j]) * 58 + c;
  62. c = (t & 0x3f00000000) >> 32;
  63. outi[j] = t & 0xffffffff;
  64. }
  65. if (c) {
  66. // Output number too big (carry to the next int32)
  67. memset(outi, 0, outisz * sizeof(*outi));
  68. return 0;
  69. }
  70. if (outi[0] & zeromask) {
  71. // Output number too big (last int32 filled too far)
  72. memset(outi, 0, outisz * sizeof(*outi));
  73. return 0;
  74. }
  75. }
  76. j = 0;
  77. switch (bytesleft) {
  78. case 3:
  79. *(binu++) = (outi[0] & 0xff0000) >> 16;
  80. case 2:
  81. *(binu++) = (outi[0] & 0xff00) >> 8;
  82. case 1:
  83. *(binu++) = (outi[0] & 0xff);
  84. ++j;
  85. default:
  86. break;
  87. }
  88. for (; j < outisz; ++j) {
  89. *(binu++) = (outi[j] >> 0x18) & 0xff;
  90. *(binu++) = (outi[j] >> 0x10) & 0xff;
  91. *(binu++) = (outi[j] >> 8) & 0xff;
  92. *(binu++) = (outi[j] >> 0) & 0xff;
  93. }
  94. // Count canonical base58 byte count
  95. binu = *bin;
  96. for (i = 0; i < binsz; ++i) {
  97. if (binu[i]) {
  98. break;
  99. }
  100. --*binszp;
  101. }
  102. *binszp += zerocount;
  103. memset(outi, 0, outisz * sizeof(*outi));
  104. return 1;
  105. }
  106. /**
  107. * encode an array of bytes into a base58 string
  108. * @param binary_data the data to be encoded
  109. * @param binary_data_size the size of the data to be encoded
  110. * @param base58 the results buffer
  111. * @param base58_size the size of the results buffer
  112. * @returns true(1) on success
  113. */
  114. int multiaddr_encoding_base58_encode(const unsigned char* data, size_t binsz, unsigned char** b58, size_t* b58sz)
  115. {
  116. const uint8_t* bin = data;
  117. int carry;
  118. ssize_t i, j, high, zcount = 0;
  119. size_t size;
  120. while (zcount < (ssize_t)binsz && !bin[zcount]) {
  121. ++zcount;
  122. }
  123. size = (binsz - zcount) * 138 / 100 + 1;
  124. uint8_t buf[size];
  125. memset(buf, 0, size);
  126. for (i = zcount, high = size - 1; i < (ssize_t)binsz; ++i, high = j) {
  127. for (carry = bin[i], j = size - 1; (j > high) || carry; --j) {
  128. carry += 256 * buf[j];
  129. buf[j] = carry % 58;
  130. carry /= 58;
  131. }
  132. }
  133. for (j = 0; j < (ssize_t)size && !buf[j]; ++j)
  134. ;
  135. if (*b58sz <= zcount + size - j) {
  136. *b58sz = zcount + size - j + 1;
  137. memset(buf, 0, size);
  138. return 0;
  139. }
  140. if (zcount) {
  141. memset(b58, '1', zcount);
  142. }
  143. for (i = zcount; j < (ssize_t)size; ++i, ++j) {
  144. (*b58)[i] = b58digits_ordered[buf[j]];
  145. }
  146. (*b58)[i] = '\0';
  147. *b58sz = i + 1;
  148. memset(buf, 0, size);
  149. return 1;
  150. }
  151. /***
  152. * calculate the size of the binary results based on an incoming base58 string
  153. * @param base58_string the string
  154. * @returns the size in bytes had the string been decoded
  155. */
  156. size_t multiaddr_encoding_base58_decode_size(const unsigned char* base58_string) {
  157. size_t string_length = strlen((char*)base58_string);
  158. size_t decoded_length = 0;
  159. size_t radix = strlen(b58digits_ordered);
  160. double bits_per_digit = log2(radix);
  161. decoded_length = floor(string_length * bits_per_digit / 8);
  162. return decoded_length;
  163. }
  164. /**
  165. * calculate the max length in bytes of an encoding of n source bits
  166. * @param base58_string the string
  167. * @returns the maximum size in bytes had the string been decoded
  168. */
  169. size_t multiaddr_encoding_base58_decode_max_size(const unsigned char* base58_string) {
  170. size_t string_length = strlen((char*)base58_string);
  171. size_t decoded_length = 0;
  172. size_t radix = strlen(b58digits_ordered);
  173. double bits_per_digit = log2(radix);
  174. decoded_length = ceil(string_length * bits_per_digit / 8);
  175. return decoded_length;
  176. }