|
@@ -86,7 +86,7 @@ const char* TokenTypeString(TokenType t)
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
-std::string AddOffset(const std::string& prefix, const std::string& text, unsigned int offset)
|
|
|
+std::string AddOffset(const std::string& prefix, const std::string& text, size_t offset)
|
|
|
{
|
|
|
return static_cast<std::string>( (Formatter::format() << prefix << " (offset 0x" << std::hex << offset << ") " << text) );
|
|
|
}
|
|
@@ -114,47 +114,66 @@ std::string AddTokenText(const std::string& prefix, const std::string& text, con
|
|
|
text) );
|
|
|
}
|
|
|
|
|
|
+// Generated by this formula: T["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[i]] = i;
|
|
|
static const uint8_t base64DecodeTable[128] = {
|
|
|
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63,
|
|
|
- 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 64, 0, 0,
|
|
|
- 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
|
|
- 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0,
|
|
|
- 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
|
|
- 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0
|
|
|
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
|
|
|
+ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255,
|
|
|
+ 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
|
|
|
+ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
|
|
|
+ 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
|
|
+ 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255
|
|
|
};
|
|
|
|
|
|
uint8_t DecodeBase64(char ch)
|
|
|
{
|
|
|
- return base64DecodeTable[size_t(ch)];
|
|
|
+ const auto idx = static_cast<uint8_t>(ch);
|
|
|
+ if (idx > 127)
|
|
|
+ return 255;
|
|
|
+ return base64DecodeTable[idx];
|
|
|
}
|
|
|
|
|
|
-size_t DecodeBase64(const char* in, size_t inLength, uint8_t*& out)
|
|
|
+size_t ComputeDecodedSizeBase64(const char* in, size_t inLength)
|
|
|
{
|
|
|
- if (inLength < 4) {
|
|
|
- out = 0;
|
|
|
+ if (inLength < 2)
|
|
|
+ {
|
|
|
return 0;
|
|
|
}
|
|
|
+ const size_t equals = size_t(in[inLength - 1] == '=') + size_t(in[inLength - 2] == '=');
|
|
|
+ const size_t full_length = (inLength * 3) >> 2; // div by 4
|
|
|
+ if (full_length < equals)
|
|
|
+ {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return full_length - equals;
|
|
|
+}
|
|
|
|
|
|
- const size_t outLength = (inLength * 3) / 4;
|
|
|
- out = new uint8_t[outLength];
|
|
|
- memset(out, 0, outLength);
|
|
|
-
|
|
|
- size_t i = 0;
|
|
|
- size_t j = 0;
|
|
|
- for (i = 0; i < inLength - 4; i += 4)
|
|
|
+size_t DecodeBase64(const char* in, size_t inLength, uint8_t* out, size_t maxOutLength)
|
|
|
+{
|
|
|
+ if (maxOutLength == 0 || inLength < 2) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ const size_t realLength = inLength - size_t(in[inLength - 1] == '=') - size_t(in[inLength - 2] == '=');
|
|
|
+ size_t dst_offset = 0;
|
|
|
+ int val = 0, valb = -8;
|
|
|
+ for (size_t src_offset = 0; src_offset < realLength; ++src_offset)
|
|
|
{
|
|
|
- uint8_t b0 = Util::DecodeBase64(in[i]);
|
|
|
- uint8_t b1 = Util::DecodeBase64(in[i + 1]);
|
|
|
- uint8_t b2 = Util::DecodeBase64(in[i + 2]);
|
|
|
- uint8_t b3 = Util::DecodeBase64(in[i + 3]);
|
|
|
-
|
|
|
- out[j++] = (uint8_t)((b0 << 2) | (b1 >> 4));
|
|
|
- out[j++] = (uint8_t)((b1 << 4) | (b2 >> 2));
|
|
|
- out[j++] = (uint8_t)((b2 << 6) | b3);
|
|
|
+ const uint8_t table_value = Util::DecodeBase64(in[src_offset]);
|
|
|
+ if (table_value == 255)
|
|
|
+ {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ val = (val << 6) + table_value;
|
|
|
+ valb += 6;
|
|
|
+ if (valb >= 0)
|
|
|
+ {
|
|
|
+ out[dst_offset++] = static_cast<uint8_t>((val >> valb) & 0xFF);
|
|
|
+ valb -= 8;
|
|
|
+ val &= 0xFFF;
|
|
|
+ }
|
|
|
}
|
|
|
- return outLength;
|
|
|
+ return dst_offset;
|
|
|
}
|
|
|
|
|
|
static const char to_base64_string[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|