是否可以在编译时将Base64编码的数据解码为二进制数据?
我想到了类似于这样的东西:
constexpr auto decoded = decodeBase64<"SGVsbG8=">();
或者
constexpr auto decoded = decodeBase64("SGVsbG8=");
对于decoded
的结果类型,我没有特殊要求。
是否可以在编译时将Base64编码的数据解码为二进制数据?
我想到了类似于这样的东西:
constexpr auto decoded = decodeBase64<"SGVsbG8=">();
或者
constexpr auto decoded = decodeBase64("SGVsbG8=");
对于decoded
的结果类型,我没有特殊要求。
/**
* The MIT License (MIT)
* Copyright (c) 2016 tomykaira
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
constexpr
函数中返回一个字符串,你需要返回一个char数组。由于你不能返回一个数组或者std::string
,所以std::array
是最好的选择。但是有一个问题——由于标准的疏忽,在C++17之前,std::array
的[]
运算符是非const的。然而你可以通过继承并添加一个构造函数来解决这个问题:template <size_t N>
struct fixed_string : std::array<char, N> {
constexpr fixed_string(const char (&input)[N]) : fixed_string(input, std::make_index_sequence<N>{}) {}
template <size_t... Is>
constexpr fixed_string(const char (&input)[N], std::index_sequence<Is...>) : std::array<char, N>{ input[Is]... } {}
};
更改解码器使用该方法而不是std::string
,似乎可用作constexpr。需要C++14,因为C++11的constexpr函数只能有一个返回语句:
template <size_t N>
constexpr const std::array<char, ((((N-1) >> 2) * 3) + 1)> decode(const char(&input)[N]) {
constexpr unsigned char kDecodingTable[] = {
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
64, 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, 64, 64, 64, 64, 64,
64, 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, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
};
static_assert(((N-1) & 3) == 0, "Input data size is not a multiple of 4");
char out[(((N-1) >> 2) * 3) + 1] {0};
size_t out_len = (N-1) / 4 * 3;
if (input[(N-1) - 1] == '=') out_len--;
if (input[(N-1) - 2] == '=') out_len--;
for (size_t i = 0, j = 0; i < N-1;) {
uint32_t a = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
uint32_t b = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
uint32_t c = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
uint32_t d = input[i] == '=' ? 0 & i++ : kDecodingTable[static_cast<int>(input[i++])];
uint32_t triple = (a << 3 * 6) + (b << 2 * 6) + (c << 1 * 6) + (d << 0 * 6);
if (j < out_len) out[j++] = (triple >> 2 * 8) & 0xFF;
if (j < out_len) out[j++] = (triple >> 1 * 8) & 0xFF;
if (j < out_len) out[j++] = (triple >> 0 * 8) & 0xFF;
}
return fixed_string<(((N-1) >> 2) * 3) + 1>(out);
}
使用方法:
constexpr auto x = decode("aGVsbG8gd29ybGQ=");
/*...*/
printf(x.data()); // hello world
演示: https://godbolt.org/z/HFdk6Z
更新以纠正Marek R和Frank的有益反馈意见
std::array
而不是自定义类。同时需要指出这段代码需要 c++14
。 - Marek Rstd::array
的索引运算符是非const的。不过如果你使用的是C++17或更高版本,那会更加清晰易懂。 - parktomatomistd::array
,使用它并返回它,但限制条件意味着我必须在 C 数组中构建输出,然后将其复制到 std::array
中。std::array
没有为此提供构造函数或任何用户构造函数,而是像 C 数组一样按聚合初始化。因此,现在 fixed_string
辅助类所做的所有事情都是添加一个构造函数,将 C 数组的内容转移至 std::array
的大括号初始化程序。constexpr
函数仍然输出一个 std::array
。 - parktomatomiparktomatomi的回答对于找到这个解决方案非常有帮助。 使用C++17和std :: array,这似乎可以工作。
base64解码器基于答案https://dev59.com/rHVC5IYBdhLWcg3wykej#34571089。
constexpr size_t decodeBase64Length(const char *s)
{
size_t len = std::char_traits<char>::length(s);
if (s[len - 2] == '=')
return (len / 4) * 3 - 2;
else if(s[len -1] == '=')
return (len / 4) * 3 - 1;
else
return (len / 4) * 3 ;
}
constexpr std::array<int, 256> prepareBase64DecodeTable() {
std::array<int, 256> T{ 0 }; // breaks constexpr: T.fill(-1) or missing initialization
for (int i = 0; i < 256; i++)
T[i] = -1;
for (int i = 0; i < 64; i++)
T["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[i]] = i;
return T;
}
// based on https://dev59.com/rHVC5IYBdhLWcg3wykej#34571089
template<int N>
constexpr std::array<std::byte, N> decodeBase64(const char *b64Str)
{
constexpr auto T = prepareBase64DecodeTable();
std::array<std::byte, N> out = { std::byte(0) };
int valb = -8;
for (size_t i = 0, val = 0, posOut = 0; i < std::char_traits<char>::length(b64Str) && T[b64Str[i]] != -1; i++) {
val = (val << 6) + T[b64Str[i]];
valb += 6;
if (valb >= 0) {
out[posOut++] = std::byte((val >> valb) & 0xFF);
valb -= 8;
}
}
return out;
}
用法不完美,因为我无法推断出生成的数组长度,除非将其显式传递作为模板参数:
#define B64c "SGVsbG8xMg=="
constexpr auto b64 = decodeBase64<decodeBase64Length(B64c)>(B64c); // array<byte,7>
const char *
,而是使用数组的引用来从参数中推断长度:const char (&b64Str)[N]
。 - parktomatomi
constexpr auto decoded = decodeBase64<"SGVsbG8=">();
- 不行,截至到C++17
,const char[]
不能作为非类型模板参数。constexpr auto decoded = decodeBase64("SGVsbG8=");
- 可以,如果decodeBase64
接受const char*
并且是一个constexpr
函数。 - Fureeishconst char*
或通过指向数组的指针或引用);只是你不能将字符串字面值用作它的模板参数。 - Davis Herring