11#include " misc.hpp"
2+
3+ #include < assert.h>
24#include < ctype.h>
35#include < time.h>
46
@@ -29,18 +31,24 @@ const char ALPHABET_MAP[256] = {
2931const double iFactor = 1.36565823730976103695740418120764243208481439700722980119458355862779176747360903943915516885072037696111192757109 ;
3032
3133// 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+
3340 int zeros = 0 , length = 0 , pbegin = 0 , pend;
3441 if (!(pend = len)) return 0 ;
3542 while (pbegin != pend && !source[pbegin]) pbegin = ++zeros;
3643 int size = 1 + iFactor * (double )(pend - pbegin);
37- uint8_t b58[size];
44+ assert ( size > 0 );
45+ uint8_t b58[ static_cast < unsigned >( size ) ];
3846 for (int i = 0 ; i < size; i++) b58[i] = 0 ;
3947 while (pbegin != pend) {
4048 uint32_t carry = source[pbegin];
4149 int i = 0 ;
4250 for (int it1 = size - 1 ; (carry || i < length) && (it1 != -1 ); it1--,i++) {
43- carry += 256 * b58[it1];
51+ carry += 256U * b58[it1];
4452 b58[it1] = carry % 58 ;
4553 carry /= 58 ;
4654 }
@@ -61,6 +69,10 @@ int enc_base58(const uint8_t *source, int len, uint8_t result[], int reslen) {
6169// result must be declared (for the worst case): char result[len * 2];
6270int dec_base58 ( const uint8_t *str, int len, uint8_t *result)
6371{
72+ assert ( str );
73+ assert ( len >= 0 );
74+ assert ( result );
75+
6476 result[0 ] = 0 ;
6577 int resultlen = 1 ;
6678 for (int i = 0 ; i < len; i++) {
@@ -105,15 +117,15 @@ char *uint_to_str( uint64_t val, char *cptr )
105117 return cptr;
106118}
107119
108- uint64_t str_to_uint ( const char *val, int len )
120+ uint64_t str_to_uint ( const char *val, const unsigned len )
109121{
110122 uint64_t res = 0L ;
111123 if ( len ) {
112124 const char *cptr = val;
113125 const char *end = &val[len];
114126 for (; cptr != end; ++cptr ) {
115127 if ( isdigit ( *cptr ) ) {
116- res = res*10UL + (*cptr-' 0' );
128+ res = res*10UL + static_cast < unsigned > (*cptr-' 0' );
117129 } else {
118130 res = 0L ;
119131 break ;
@@ -143,7 +155,7 @@ char *int_to_str( int64_t val, char *cptr )
143155 return cptr;
144156}
145157
146- int64_t str_to_int ( const char *val, int len )
158+ int64_t str_to_int ( const char *val, const unsigned len )
147159{
148160 bool is_neg = false ;
149161 int64_t res = 0L ;
@@ -152,7 +164,7 @@ int64_t str_to_int( const char *val, int len )
152164 const char *end = &val[len];
153165 for (; cptr != end; ++cptr ) {
154166 if ( isdigit ( *cptr ) ) {
155- res = res*10UL + (*cptr-' 0' );
167+ res = res*10L + (*cptr-' 0' );
156168 } else if ( *cptr == ' -' ) {
157169 is_neg = true ;
158170 } else {
@@ -181,14 +193,14 @@ inline void a3_to_a4(uint8_t* a4, uint8_t* a3)
181193 a4[3 ] = (a3[2 ] & 0x3f );
182194}
183195
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)
185197{
186198 a3[0 ] = (a4[0 ] << 2 ) + ((a4[1 ] & 0x30 ) >> 4 );
187199 a3[1 ] = ((a4[1 ] & 0xf ) << 4 ) + ((a4[2 ] & 0x3c ) >> 2 );
188200 a3[2 ] = ((a4[2 ] & 0x3 ) << 6 ) + a4[3 ];
189201}
190202
191- inline uint8_t b64_lookup (char c)
203+ inline char b64_lookup (char c)
192204{
193205 if (c >=' A' && c <=' Z' ) return c - ' A' ;
194206 if (c >=' a' && c <=' z' ) return c - 71 ;
@@ -198,14 +210,15 @@ inline uint8_t b64_lookup(char c)
198210 return -1 ;
199211}
200212
201- int enc_base64_len ( int n )
213+ size_t enc_base64_len ( const size_t n )
202214{
203215 return (n + 2 - ((n + 2 ) % 3 )) / 3 * 4 ;
204216}
205217
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 )
207219{
208- int i = 0 , j = 0 , encLen = 0 ;
220+ int i = 0 , j = 0 ;
221+ size_t encLen = 0 ;
209222 uint8_t a3[3 ];
210223 uint8_t a4[4 ];
211224
@@ -236,22 +249,23 @@ int enc_base64( const uint8_t *inp, int len, uint8_t *out )
236249 return encLen;
237250}
238251
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 )
240253{
241- int i = 0 , j = 0 , decLen = 0 ;
254+ int i = 0 , j = 0 ;
255+ size_t decLen = 0 ;
242256 uint8_t a3[3 ] = { 0 ,0 ,0 };
243- uint8_t a4[4 ];
257+ char a4[4 ];
244258
245259 while ( len-- ) {
246260 if (*inp == ' =' ) {
247261 break ;
248262 }
249263 a4[i++] = *(inp++);
250264 if (i == 4 ) {
251- for (i = 0 ; i <4 ; i++) {
265+ for (i = 0 ; i < 4 ; i++) {
252266 a4[i] = b64_lookup (a4[i]);
253267 }
254- a4_to_a3 (a3,a4 );
268+ a4_to_a3 ( a3, reinterpret_cast < uint8_t * >( a4 ) );
255269 for (i = 0 ; i < 3 ; i++) {
256270 out[decLen++] = a3[i];
257271 }
@@ -266,7 +280,7 @@ int dec_base64( const uint8_t *inp, int len, uint8_t *out )
266280 for (j = 0 ; j <4 ; j++) {
267281 a4[j] = b64_lookup (a4[j]);
268282 }
269- a4_to_a3 (a3,a4 );
283+ a4_to_a3 ( a3, reinterpret_cast < uint8_t * >( a4 ) );
270284 for (j = 0 ; j < i - 1 ; j++) {
271285 out[decLen++] = a3[j];
272286 }
@@ -279,7 +293,7 @@ int64_t get_now()
279293 struct timespec ts[1 ];
280294 clock_gettime ( CLOCK_REALTIME, ts );
281295 int64_t res = ts->tv_sec ;
282- res *= 1000000000UL ;
296+ res *= 1000000000L ;
283297 res += ts->tv_nsec ;
284298 return res;
285299}
0 commit comments