sleepdefic1t
11/18/2019 - 10:21 PM

C++ Base58 - Basic Impl

C++ Base58 - Basic Impl


std::vector<uint8_t> decodeBase58(const uint8_t *in, const size_t length) {

unsigned char const BASE58TABLE[] = {
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0x0,  0x1,  0x2,  0x3,  0x4,  0x5,  0x6,  0x7,  0x8,  0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0x9,  0xa,  0xb,  0xc,  0xd,  0xe,  0xf,
    0x10, 0xff, 0x11, 0x12, 0x13, 0x14, 0x15, 0xff, 0x16, 0x17, 0x18, 0x19,
    0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
    0xff, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
    0x37, 0x38, 0x39, 0xff, 0xff, 0xff, 0xff, 0xff
  };

    unsigned char tmp[164];
    unsigned char buffer[164];
    unsigned char i;
    unsigned char j;
    unsigned char startAt;
    unsigned char zeroCount = 0;
  
    if (length > sizeof(tmp)) {
        return {};
    }

    memmove(tmp, in, length);
    
    for (i = 0; i < length; i++) {
        if (in[i] > 128) {
            return {};
        }

        tmp[i] = BASE58TABLE[in[i]];
        
        if (tmp[i] == 0xff) {
            return {};
        }
    }

    while ((zeroCount < length) && (tmp[zeroCount] == 0)) {
        ++zeroCount;
    }
    
    j = length;
    
    startAt = zeroCount;
    
    while (startAt < length) {
        unsigned short remainder = 0;
        unsigned char divLoop;

        for (divLoop = startAt; divLoop < length; divLoop++) {
            unsigned short digit256 = (unsigned short)(tmp[divLoop] & 0xff);
            unsigned short tmpDiv = remainder * 58 + digit256;
            tmp[divLoop] = (unsigned char)(tmpDiv / 256);
            remainder = (tmpDiv % 256);
        }

        if (tmp[startAt] == 0) {
            ++startAt;
        }
        
        buffer[--j] = (unsigned char)remainder;
    }

    while ((j < length) && (buffer[j] == 0)) {
        ++j;
    }
    
    size_t outLen = length - (j - zeroCount);
    
    std::vector<uint8_t> out;
    out.resize(outLen);
  
    memmove(&out, buffer + j - zeroCount, outLen);
    
    return out;
}

//////////////////////////////////////////////////////////////////////////////

static std::string encodeBase58(const uint8_t *in, const uint32_t length) {
  const uint8_t BASE58ALPHABET[] = {
      '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
      'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
      'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm',
      'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
  };

    uint8_t tmp[164];
    uint8_t buffer[164];
    uint8_t j;
    uint8_t startAt;
    uint8_t zeroCount = 0;

    if (length > sizeof(tmp)) {
        return {};
    }

    memmove(tmp, in, length);
    while ((zeroCount < length) && (tmp[zeroCount] == 0U)) {
        ++zeroCount;
    }

    j = 2U * length;
    startAt = zeroCount;
    while (startAt < length) {
        uint16_t remainder = 0U;
        uint8_t divLoop;

        for (divLoop = startAt; divLoop < length; divLoop++) {
            uint16_t digit256 = (uint16_t)(tmp[divLoop] & 0xff);
            uint16_t tmpDiv = remainder * 256U + digit256;
            tmp[divLoop] = (uint8_t)(tmpDiv / 58U);
            remainder = (tmpDiv % 58U);
        }

        if (tmp[startAt] == 0U) {
            ++startAt;
        }

        buffer[--j] = (uint8_t)BASE58ALPHABET[remainder];
    }

    while ((j < (2U * length)) && (buffer[j] == BASE58ALPHABET[0])) {
        ++j;
    }

    while (zeroCount-- > 0U) {
        buffer[--j] = BASE58ALPHABET[0];
    }

    auto outLength = 2U * length - j;

    return std::string((buffer + j), buffer + j + outLength);
}