示例#1
0
ACE_TString b32h_encode(const ACE_TCHAR* decoded)
{
  static const ACE_TCHAR lookup[] =
    ACE_TEXT("0123456789ABCDEFGHIJKLMNOPQRSTUV");
  static const ACE_TCHAR padding[] = ACE_TEXT("======");
  static const size_t enc[] = {0, 2, 4, 5, 7}; // #input -> #non-padded output
  ACE_TString encoded;

  for (size_t len = ACE_OS::strlen(decoded); *decoded; decoded += 5, len -= 5) {
    ACE_UINT64 chunk = 0;

    for (size_t i(0); i < 5 && i < len; ++i) {
      chunk |= static_cast<ACE_UINT64>(decoded[i] & 0xFF) << ((4 - i) * 8);
    }

    size_t limit = (len < 5) ? enc[len] : 8;

    for (size_t i(0); i < limit; ++i) {
      unsigned char val =
        static_cast<unsigned char>(chunk >>((7 - i) * 5)) & 0x1F;
      encoded += lookup[val];
    }

    if (len < 5) {
      encoded.append(padding, 8 - enc[len]);
      return encoded;
    }
  }

  return encoded;
}