Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberjunk committed Jan 4, 2025
1 parent c017529 commit 4f7ecc3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
15 changes: 10 additions & 5 deletions include/CppCore.Test/Encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -698,11 +698,16 @@ namespace CppCore { namespace Test
INLINE static bool encode()
{
std::string s;
CppCore::Base64::encode("1", s); if (s != "MQ==") return false;
CppCore::Base64::encode("12", s); if (s != "MTI=") return false;
CppCore::Base64::encode("123", s); if (s != "MTIz") return false;
CppCore::Base64::encode("1234", s); if (s != "MTIzNA==") return false;
CppCore::Base64::encode("", s); if (s != "") return false;
CppCore::Base64::encode("1", s); if (s != "MQ==") return false;
CppCore::Base64::encode("12", s); if (s != "MTI=") return false;
CppCore::Base64::encode("123", s); if (s != "MTIz") return false;
CppCore::Base64::encode("1234", s); if (s != "MTIzNA==") return false;
CppCore::Base64::encode("12345", s); if (s != "MTIzNDU=") return false;
CppCore::Base64::encode("123456", s); if (s != "MTIzNDU2") return false;
CppCore::Base64::encode("1234567", s); if (s != "MTIzNDU2Nw==") return false;
CppCore::Base64::encode("12345678", s); if (s != "MTIzNDU2Nzg=") return false;
CppCore::Base64::encode("123456789", s); if (s != "MTIzNDU2Nzg5") return false;
CppCore::Base64::encode("", s); if (s != "") return false;
uint8_t d1[] = { 0x01, 0x02, 0x03 }; CppCore::Base64::encode(d1, s); if (s != "AQID") return false;
uint8_t d2[] = { 0xFF }; CppCore::Base64::encode(d2, s); if (s != "/w==") return false;
return true;
Expand Down
41 changes: 40 additions & 1 deletion include/CppCore/Encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,46 @@ namespace CppCore
Base64::BINTOB64_URL :
Base64::BINTOB64_STD;
const uint8_t* p = (const uint8_t*)in;
while (len >= 3U)
while (len >= 6U)
{
// 8 symbols from 6 bytes
uint32_t v = CppCore::loadr32((uint32_t*)p);
#if defined(CPPCORE_CPUFEAT_BMI1)
uint32_t s1 = _bextr_u32(v, 26, 6);
uint32_t s2 = _bextr_u32(v, 20, 6);
uint32_t s3 = _bextr_u32(v, 14, 6);
uint32_t s4 = _bextr_u32(v, 8, 6);
#else
uint32_t s1 = (v >> 26);
uint32_t s2 = (v >> 20) & 0x3F;
uint32_t s3 = (v >> 14) & 0x3F;
uint32_t s4 = (v >> 8) & 0x3F;
#endif
*out++ = tbl[s1];
*out++ = tbl[s2];
*out++ = tbl[s3];
*out++ = tbl[s4];
p += 4;
v = CppCore::shrd32(CppCore::byteswap32(*(uint16_t*)p), v, 8U);
#if defined(CPPCORE_CPUFEAT_BMI1)
s1 = _bextr_u32(v, 26, 6);
s2 = _bextr_u32(v, 20, 6);
s3 = _bextr_u32(v, 14, 6);
s4 = _bextr_u32(v, 8, 6);
#else
s1 = (v >> 26);
s2 = (v >> 20) & 0x3F;
s3 = (v >> 14) & 0x3F;
s4 = (v >> 8) & 0x3F;
#endif
*out++ = tbl[s1];
*out++ = tbl[s2];
*out++ = tbl[s3];
*out++ = tbl[s4];
p += 2U;
len -= 6U;
}
if (len >= 3U)
{
// 4 symbols from 3 bytes
#if defined(CPPCORE_BASE64_NO_OPTIMIZATIONS)
Expand Down

0 comments on commit 4f7ecc3

Please sign in to comment.