14
14
#ifndef BITCOIN_BASE58_H
15
15
#define BITCOIN_BASE58_H
16
16
17
- #include " bignum.h"
18
17
#include " chainparams.h"
19
18
#include " hash.h"
20
19
#include " key.h"
27
26
#include < boost/variant/apply_visitor.hpp>
28
27
#include < boost/variant/static_visitor.hpp>
29
28
30
- /* All alphanumeric characters except for "0", "I", "O", and "l" */
31
- static const char * pszBase58 = " 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" ;
32
-
33
29
/* *
34
- * Encode a byte sequence as a base58-encoded string
30
+ * Encode a byte sequence as a base58-encoded string.
31
+ * pbegin and pend cannot be NULL, unless both are.
35
32
*/
36
- inline std::string EncodeBase58 (const unsigned char * pbegin, const unsigned char * pend)
37
- {
38
- CAutoBN_CTX pctx;
39
- CBigNum bn58 = 58 ;
40
- CBigNum bn0 = 0 ;
41
-
42
- // Convert big endian data to little endian - the extra zero at the end will
43
- // ensure bignum interprets it as a positive number */
44
- std::vector<unsigned char > vchTmp (pend-pbegin+1 , 0 );
45
- reverse_copy (pbegin, pend, vchTmp.begin ());
46
-
47
- // Convert little endian data to bignum
48
- CBigNum bn;
49
- bn.setvch (vchTmp);
50
-
51
- // Convert bignum to std::string
52
- std::string str;
53
- // The expected size increase from base58 conversion is approximately 137%,
54
- // but use 138% to be safe
55
- str.reserve ((pend - pbegin) * 138 / 100 + 1 );
56
- CBigNum dv;
57
- CBigNum rem;
58
- while (bn > bn0)
59
- {
60
- if (!BN_div (&dv, &rem, &bn, &bn58, pctx))
61
- throw bignum_error (" EncodeBase58 : BN_div failed" );
62
- bn = dv;
63
- unsigned int c = rem.getulong ();
64
- str += pszBase58[c];
65
- }
66
-
67
- // Leading zeroes encoded as base58 zeros
68
- for (const unsigned char * p = pbegin; p < pend && *p == 0 ; p++)
69
- str += pszBase58[0 ];
70
-
71
- // Convert little endian std::string to big endian
72
- reverse (str.begin (), str.end ());
73
- return str;
74
- }
33
+ std::string EncodeBase58 (const unsigned char * pbegin, const unsigned char * pend);
75
34
76
35
/* *
77
36
* Encode a byte vector as a base58-encoded string
@@ -82,58 +41,15 @@ inline std::string EncodeBase58(const std::vector<unsigned char>& vch)
82
41
}
83
42
84
43
/* *
85
- * Decode a base58-encoded string (psz) into a byte vector (vchRet)
86
- * return true if decoding is successful
44
+ * Decode a base58-encoded string (psz) into a byte vector (vchRet).
45
+ * return true if decoding is successful.
46
+ * psz cannot be NULL.
87
47
*/
88
- inline bool DecodeBase58 (const char * psz, std::vector<unsigned char >& vchRet)
89
- {
90
- CAutoBN_CTX pctx;
91
- vchRet.clear ();
92
- CBigNum bn58 = 58 ;
93
- CBigNum bn = 0 ;
94
- CBigNum bnChar;
95
- while (isspace (*psz))
96
- psz++;
97
-
98
- // Convert big endian string to bignum
99
- for (const char * p = psz; *p; p++)
100
- {
101
- const char * p1 = strchr (pszBase58, *p);
102
- if (p1 == NULL )
103
- {
104
- while (isspace (*p))
105
- p++;
106
- if (*p != ' \0 ' )
107
- return false ;
108
- break ;
109
- }
110
- bnChar.setulong (p1 - pszBase58);
111
- if (!BN_mul (&bn, &bn, &bn58, pctx))
112
- throw bignum_error (" DecodeBase58 : BN_mul failed" );
113
- bn += bnChar;
114
- }
115
-
116
- // Get bignum as little endian data
117
- std::vector<unsigned char > vchTmp = bn.getvch ();
118
-
119
- // Trim off the sign byte if present
120
- if (vchTmp.size () >= 2 && vchTmp.end ()[-1 ] == 0 && vchTmp.end ()[-2 ] >= 0x80 )
121
- vchTmp.erase (vchTmp.end ()-1 );
122
-
123
- // Restore leading zeros
124
- int nLeadingZeros = 0 ;
125
- for (const char * p = psz; *p == pszBase58[0 ]; p++)
126
- nLeadingZeros++;
127
- vchRet.assign (nLeadingZeros + vchTmp.size (), 0 );
128
-
129
- // Convert little endian data to big endian
130
- reverse_copy (vchTmp.begin (), vchTmp.end (), vchRet.end () - vchTmp.size ());
131
- return true ;
132
- }
48
+ bool DecodeBase58 (const char * psz, std::vector<unsigned char >& vchRet);
133
49
134
50
/* *
135
- * Decode a base58-encoded string (str) into a byte vector (vchRet)
136
- * return true if decoding is successful
51
+ * Decode a base58-encoded string (str) into a byte vector (vchRet).
52
+ * return true if decoding is successful.
137
53
*/
138
54
inline bool DecodeBase58 (const std::string& str, std::vector<unsigned char >& vchRet)
139
55
{
0 commit comments