support/BRBase58.c
// returns the number of characters written to str including NULL terminator, or total strLen needed if str is NULL
returns the number of characters written to str including NULL terminator, or total strLen needed if str is NULL
returns the number of characters written to str including NULL terminator or total strLen needed if str is NULL
size_t BRBase58EncodeEx(char *str, size_t strLen, const uint8_t *data, size_t dataLen, const char *alphabet)
{
    const char * chars = alphabet;
    assert(strlen(alphabet) >= 58);
    size_t i, j, len, zcount = 0;
    assert(data != NULL);
    while (zcount < dataLen && data && data[zcount] == 0) zcount++; 
    uint8_t buf[(dataLen - zcount)*138/100 + 1]; 
    memset(buf, 0, sizeof(buf));
    for (i = zcount; data && i < dataLen; i++) {
        uint32_t carry = data[i];
        for (j = sizeof(buf); j > 0; j--) {
            carry += (uint32_t)buf[j - 1] << 8;
            buf[j - 1] = carry % 58;
            carry /= 58;
        }
        var_clean(&carry);
    }
    i = 0;
    while (i < sizeof(buf) && buf[i] == 0) i++; 
    len = (zcount + sizeof(buf) - i) + 1;
    if (str && len <= strLen) {
        while (zcount-- > 0) *(str++) = chars[0];
        while (i < sizeof(buf)) *(str++) = chars[buf[i++]];
        *str = '\0';
    }
    mem_clean(buf, sizeof(buf));
    return (! str || len <= strLen) ? len : 0;
}
size_t BRBase58EncodeEx ( char * str , size_t strLen , const uint8_t * data , size_t dataLen , const char * alphabet ) { const char * chars = alphabet ; assert ( strlen ( alphabet ) >= 58 ) ; size_t i , j , len , zcount = 0 ; assert ( data != NULL ) ; while ( zcount < dataLen && data && data [ zcount ] == 0 ) zcount ++ ; uint8_t buf [ ( dataLen - zcount ) * 138 / 100 + 1 ] ; memset ( buf , 0 , sizeof ( buf ) ) ; for ( i = zcount ; data && i < dataLen ; i ++ ) { uint32_t carry = data [ i ] ; for ( j = sizeof ( buf ) ; j > 0 ; j -- ) { carry += ( uint32_t ) buf [ j - 1 ] << 8 ; buf [ j - 1 ] = carry % 58 ; carry /= 58 ; } var_clean ( & carry ) ; } i = 0 ; while ( i < sizeof ( buf ) && buf [ i ] == 0 ) i ++ ; len = ( zcount + sizeof ( buf ) - i ) + 1 ; if ( str && len <= strLen ) { while ( zcount -- > 0 ) * ( str ++ ) = chars [ 0 ] ; while ( i < sizeof ( buf ) ) * ( str ++ ) = chars [ buf [ i ++ ] ] ; * str = ' \0 ' ; } mem_clean ( buf , sizeof ( buf ) ) ; return ( ! str || len <= strLen ) ? len : 0 ; }
returns the number of characters written to str including NULL terminator, or total strLen needed if str is NULL
returns the number of characters written to str including NULL terminator or total strLen needed if str is NULL
// count leading zeroes // log(256)/log(58), rounded up // skip leading zeroes