1
1
#include " misc.hpp"
2
+
3
+ #include < assert.h>
2
4
#include < ctype.h>
3
5
#include < time.h>
4
6
@@ -29,18 +31,24 @@ const char ALPHABET_MAP[256] = {
29
31
const double iFactor = 1.36565823730976103695740418120764243208481439700722980119458355862779176747360903943915516885072037696111192757109 ;
30
32
31
33
// reslen is the allocated length for result, feel free to overallocate
32
- int enc_base58 (const uint8_t *source, int len, uint8_t result[], int reslen) {
34
+ int enc_base58 (const uint8_t *source, int len, char result[], int reslen)
35
+ {
36
+ assert ( source );
37
+ assert ( len >= 0 );
38
+ assert ( reslen >= 0 );
39
+
33
40
int zeros = 0 , length = 0 , pbegin = 0 , pend;
34
41
if (!(pend = len)) return 0 ;
35
42
while (pbegin != pend && !source[pbegin]) pbegin = ++zeros;
36
43
int size = 1 + iFactor * (double )(pend - pbegin);
37
- uint8_t b58[size];
44
+ assert ( size > 0 );
45
+ uint8_t b58[ static_cast < unsigned >( size ) ];
38
46
for (int i = 0 ; i < size; i++) b58[i] = 0 ;
39
47
while (pbegin != pend) {
40
48
uint32_t carry = source[pbegin];
41
49
int i = 0 ;
42
50
for (int it1 = size - 1 ; (carry || i < length) && (it1 != -1 ); it1--,i++) {
43
- carry += 256 * b58[it1];
51
+ carry += 256U * b58[it1];
44
52
b58[it1] = carry % 58 ;
45
53
carry /= 58 ;
46
54
}
@@ -61,6 +69,10 @@ int enc_base58(const uint8_t *source, int len, uint8_t result[], int reslen) {
61
69
// result must be declared (for the worst case): char result[len * 2];
62
70
int dec_base58 ( const uint8_t *str, int len, uint8_t *result)
63
71
{
72
+ assert ( str );
73
+ assert ( len >= 0 );
74
+ assert ( result );
75
+
64
76
result[0 ] = 0 ;
65
77
int resultlen = 1 ;
66
78
for (int i = 0 ; i < len; i++) {
@@ -105,15 +117,15 @@ char *uint_to_str( uint64_t val, char *cptr )
105
117
return cptr;
106
118
}
107
119
108
- uint64_t str_to_uint ( const char *val, int len )
120
+ uint64_t str_to_uint ( const char *val, const unsigned len )
109
121
{
110
122
uint64_t res = 0L ;
111
123
if ( len ) {
112
124
const char *cptr = val;
113
125
const char *end = &val[len];
114
126
for (; cptr != end; ++cptr ) {
115
127
if ( isdigit ( *cptr ) ) {
116
- res = res*10UL + (*cptr-' 0' );
128
+ res = res*10UL + static_cast < unsigned > (*cptr-' 0' );
117
129
} else {
118
130
res = 0L ;
119
131
break ;
@@ -143,7 +155,7 @@ char *int_to_str( int64_t val, char *cptr )
143
155
return cptr;
144
156
}
145
157
146
- int64_t str_to_int ( const char *val, int len )
158
+ int64_t str_to_int ( const char *val, const unsigned len )
147
159
{
148
160
bool is_neg = false ;
149
161
int64_t res = 0L ;
@@ -152,7 +164,7 @@ int64_t str_to_int( const char *val, int len )
152
164
const char *end = &val[len];
153
165
for (; cptr != end; ++cptr ) {
154
166
if ( isdigit ( *cptr ) ) {
155
- res = res*10UL + (*cptr-' 0' );
167
+ res = res*10L + (*cptr-' 0' );
156
168
} else if ( *cptr == ' -' ) {
157
169
is_neg = true ;
158
170
} else {
@@ -181,14 +193,14 @@ inline void a3_to_a4(uint8_t* a4, uint8_t* a3)
181
193
a4[3 ] = (a3[2 ] & 0x3f );
182
194
}
183
195
184
- inline void a4_to_a3 ( uint8_t * a3, uint8_t * a4)
196
+ inline void a4_to_a3 ( uint8_t * a3, const uint8_t * a4)
185
197
{
186
198
a3[0 ] = (a4[0 ] << 2 ) + ((a4[1 ] & 0x30 ) >> 4 );
187
199
a3[1 ] = ((a4[1 ] & 0xf ) << 4 ) + ((a4[2 ] & 0x3c ) >> 2 );
188
200
a3[2 ] = ((a4[2 ] & 0x3 ) << 6 ) + a4[3 ];
189
201
}
190
202
191
- inline uint8_t b64_lookup (char c)
203
+ inline char b64_lookup (char c)
192
204
{
193
205
if (c >=' A' && c <=' Z' ) return c - ' A' ;
194
206
if (c >=' a' && c <=' z' ) return c - 71 ;
@@ -198,14 +210,15 @@ inline uint8_t b64_lookup(char c)
198
210
return -1 ;
199
211
}
200
212
201
- int enc_base64_len ( int n )
213
+ size_t enc_base64_len ( const size_t n )
202
214
{
203
215
return (n + 2 - ((n + 2 ) % 3 )) / 3 * 4 ;
204
216
}
205
217
206
- int enc_base64 ( const uint8_t *inp, int len, uint8_t *out )
218
+ size_t enc_base64 ( const uint8_t *inp, int len, char *out )
207
219
{
208
- int i = 0 , j = 0 , encLen = 0 ;
220
+ int i = 0 , j = 0 ;
221
+ size_t encLen = 0 ;
209
222
uint8_t a3[3 ];
210
223
uint8_t a4[4 ];
211
224
@@ -236,22 +249,23 @@ int enc_base64( const uint8_t *inp, int len, uint8_t *out )
236
249
return encLen;
237
250
}
238
251
239
- int dec_base64 ( const uint8_t *inp, int len, uint8_t *out )
252
+ size_t dec_base64 ( const char *inp, int len, uint8_t *out )
240
253
{
241
- int i = 0 , j = 0 , decLen = 0 ;
254
+ int i = 0 , j = 0 ;
255
+ size_t decLen = 0 ;
242
256
uint8_t a3[3 ] = { 0 ,0 ,0 };
243
- uint8_t a4[4 ];
257
+ char a4[4 ];
244
258
245
259
while ( len-- ) {
246
260
if (*inp == ' =' ) {
247
261
break ;
248
262
}
249
263
a4[i++] = *(inp++);
250
264
if (i == 4 ) {
251
- for (i = 0 ; i <4 ; i++) {
265
+ for (i = 0 ; i < 4 ; i++) {
252
266
a4[i] = b64_lookup (a4[i]);
253
267
}
254
- a4_to_a3 (a3,a4 );
268
+ a4_to_a3 ( a3, reinterpret_cast < uint8_t * >( a4 ) );
255
269
for (i = 0 ; i < 3 ; i++) {
256
270
out[decLen++] = a3[i];
257
271
}
@@ -266,7 +280,7 @@ int dec_base64( const uint8_t *inp, int len, uint8_t *out )
266
280
for (j = 0 ; j <4 ; j++) {
267
281
a4[j] = b64_lookup (a4[j]);
268
282
}
269
- a4_to_a3 (a3,a4 );
283
+ a4_to_a3 ( a3, reinterpret_cast < uint8_t * >( a4 ) );
270
284
for (j = 0 ; j < i - 1 ; j++) {
271
285
out[decLen++] = a3[j];
272
286
}
@@ -279,7 +293,7 @@ int64_t get_now()
279
293
struct timespec ts[1 ];
280
294
clock_gettime ( CLOCK_REALTIME, ts );
281
295
int64_t res = ts->tv_sec ;
282
- res *= 1000000000UL ;
296
+ res *= 1000000000L ;
283
297
res += ts->tv_nsec ;
284
298
return res;
285
299
}
0 commit comments