support/BRBase58.c
// returns the number of bytes written to data, or total dataLen needed if data is NULL
returns the number of bytes written to data, or total dataLen needed if data is NULL
returns the number of bytes written to data or total dataLen needed if data is NULL
size_t BRBase58Decode(uint8_t *data, size_t dataLen, const char *str)
{
    size_t i = 0, j, len, zcount = 0;
    assert(str != NULL);
    while (str && *str == '1') str++, zcount++; 
    uint8_t buf[(str) ? strlen(str)*733/1000 + 1 : 0]; 
    memset(buf, 0, sizeof(buf));
    while (str && *str) {
        uint32_t carry = *(const uint8_t *)(str++);
        switch (carry) {
            case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
                carry -= '1';
                break;
            case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H':
                carry += 9 - 'A';
                break;
            case 'J': case 'K': case 'L': case 'M': case 'N':
                carry += 17 - 'J';
                break;
            case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y':
            case 'Z':
                carry += 22 - 'P';
                break;
            case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j':
            case 'k':
                carry += 33 - 'a';
                break;
            case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v':
            case 'w': case 'x': case 'y': case 'z':
                carry += 44 - 'm';
                break;
            default:
                carry = UINT32_MAX;
        }
        if (carry >= 58) break; 
        for (j = sizeof(buf); j > 0; j--) {
            carry += (uint32_t)buf[j - 1]*58;
            buf[j - 1] = carry & 0xff;
            carry >>= 8;
        }
        var_clean(&carry);
    }
    while (i < sizeof(buf) && buf[i] == 0) i++; 
    len = zcount + sizeof(buf) - i;
    if (data && len <= dataLen) {
        if (zcount > 0) memset(data, 0, zcount);
        memcpy(&data[zcount], &buf[i], sizeof(buf) - i);
    }
    mem_clean(buf, sizeof(buf));
    return (! data || len <= dataLen) ? len : 0;
}
size_t BRBase58Decode ( uint8_t * data , size_t dataLen , const char * str ) { size_t i = 0 , j , len , zcount = 0 ; assert ( str != NULL ) ; while ( str && * str == ' ' ) str ++ , zcount ++ ; uint8_t buf [ ( str ) ? strlen ( str ) * 733 / 1000 + 1 : 0 ] ; memset ( buf , 0 , sizeof ( buf ) ) ; while ( str && * str ) { uint32_t carry = * ( const uint8_t * ) ( str ++ ) ; switch ( carry ) { case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : carry -= ' ' ; break ; case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : carry += 9 - ' ' ; break ; case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : carry += 17 - ' ' ; break ; case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : carry += 22 - ' ' ; break ; case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : carry += 33 - ' ' ; break ; case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : case ' ' : carry += 44 - ' ' ; break ; default : carry = UINT32_MAX ; } if ( carry >= 58 ) break ; for ( j = sizeof ( buf ) ; j > 0 ; j -- ) { carry += ( uint32_t ) buf [ j - 1 ] * 58 ; buf [ j - 1 ] = carry & 0xff ; carry >>= 8 ; } var_clean ( & carry ) ; } while ( i < sizeof ( buf ) && buf [ i ] == 0 ) i ++ ; len = zcount + sizeof ( buf ) - i ; if ( data && len <= dataLen ) { if ( zcount > 0 ) memset ( data , 0 , zcount ) ; memcpy ( & data [ zcount ] , & buf [ i ] , sizeof ( buf ) - i ) ; } mem_clean ( buf , sizeof ( buf ) ) ; return ( ! data || len <= dataLen ) ? len : 0 ; }
returns the number of bytes written to data, or total dataLen needed if data is NULL
returns the number of bytes written to data or total dataLen needed if data is NULL
// count leading zeroes // log(58)/log(256), rounded up // invalid base58 digit // skip leading zeroes