|
1 | 1 | #include "base64.h"
|
2 | 2 |
|
3 |
| -#include <stdint.h> |
| 3 | +#include <cstddef> |
| 4 | +#include <cstdint> |
4 | 5 |
|
5 | 6 | namespace datadog {
|
6 | 7 | namespace tracing {
|
7 |
| -namespace base64 { |
8 | 8 |
|
9 |
| -#define _ 255 |
10 |
| -#define SENTINEL_VALUE _ |
11 |
| -#define EOL 0 |
| 9 | +constexpr uint8_t k_sentinel = 255; |
| 10 | +constexpr uint8_t _ = k_sentinel; // for brevity |
| 11 | +constexpr uint8_t k_eol = 0; |
12 | 12 |
|
13 |
| -/* |
14 |
| - * Lookup table mapping the base64 table. Invalid inputs are mapped |
15 |
| - * to the value 255. |
16 |
| - * `=` map to 0. |
17 |
| - */ |
| 13 | +// Invalid inputs are mapped to the value 255. '=' maps to 0. |
18 | 14 | constexpr uint8_t k_base64_table[]{
|
19 |
| - _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, |
20 |
| - _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, |
21 |
| - _, _, _, _, _, 62, _, _, _, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, |
22 |
| - 61, _, _, _, EOL, _, _, _, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
23 |
| - 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, _, _, _, _, |
24 |
| - _, _, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, |
25 |
| - 43, 44, 45, 46, 47, 48, 49, 50, 51, _, _, _, _, _, _, _, _, _, _, |
26 |
| - _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, |
27 |
| - _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, |
28 |
| - _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, |
29 |
| - _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, |
30 |
| - _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, |
31 |
| - _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, |
32 |
| - _, _, _, _, _, _, _, _, _}; |
33 |
| - |
34 |
| -// TODO: support input without padding? |
35 |
| -std::string decode(StringView in) { |
36 |
| - const std::size_t in_size = in.size(); |
37 |
| - |
38 |
| - std::string out; |
39 |
| - out.reserve(in_size); |
| 15 | + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, |
| 16 | + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, |
| 17 | + _, _, _, _, _, _, _, 62, _, _, _, 63, 52, 53, 54, 55, 56, 57, |
| 18 | + 58, 59, 60, 61, _, _, _, k_eol, _, _, _, 0, 1, 2, 3, 4, 5, 6, |
| 19 | + 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 20 | + 25, _, _, _, _, _, _, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, |
| 21 | + 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, _, _, _, |
| 22 | + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, |
| 23 | + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, |
| 24 | + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, |
| 25 | + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, |
| 26 | + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, |
| 27 | + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, |
| 28 | + _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, |
| 29 | + _, _, _, _}; |
| 30 | + |
| 31 | +std::string base64_decode(StringView input) { |
| 32 | + const size_t in_size = input.size(); |
| 33 | + |
| 34 | + std::string output; |
| 35 | + output.reserve(in_size); |
40 | 36 |
|
41 | 37 | union {
|
42 | 38 | uint32_t buffer;
|
43 | 39 | uint8_t bytes[4];
|
44 | 40 | } decoder;
|
45 | 41 |
|
46 |
| - std::size_t i = 0; |
| 42 | + size_t i = 0; |
47 | 43 |
|
48 | 44 | for (; i + 4 < in_size;) {
|
49 |
| - auto c0 = k_base64_table[static_cast<size_t>(in[i++])]; |
50 |
| - auto c1 = k_base64_table[static_cast<size_t>(in[i++])]; |
51 |
| - auto c2 = k_base64_table[static_cast<size_t>(in[i++])]; |
52 |
| - auto c3 = k_base64_table[static_cast<size_t>(in[i++])]; |
| 45 | + uint32_t c0 = k_base64_table[static_cast<size_t>(input[i++])]; |
| 46 | + uint32_t c1 = k_base64_table[static_cast<size_t>(input[i++])]; |
| 47 | + uint32_t c2 = k_base64_table[static_cast<size_t>(input[i++])]; |
| 48 | + uint32_t c3 = k_base64_table[static_cast<size_t>(input[i++])]; |
53 | 49 |
|
54 |
| - if (c0 == SENTINEL_VALUE || c1 == SENTINEL_VALUE || c2 == SENTINEL_VALUE || |
55 |
| - c3 == SENTINEL_VALUE) { |
| 50 | + if (c0 == k_sentinel || c1 == k_sentinel || c2 == k_sentinel || |
| 51 | + c3 == k_sentinel) { |
56 | 52 | return "";
|
57 | 53 | }
|
58 | 54 |
|
59 |
| - decoder.buffer = |
60 |
| - static_cast<uint32_t>(0) | static_cast<uint32_t>(c0) << 26 | |
61 |
| - static_cast<uint32_t>(c1) << 20 | static_cast<uint32_t>(c2) << 14 | |
62 |
| - static_cast<uint32_t>(c3) << 8; |
| 55 | + decoder.buffer = 0 | c0 << 26 | c1 << 20 | c2 << 14 | c3 << 8; |
63 | 56 |
|
64 |
| - // NOTE(@dmehala): It might seem confusion to read those bytes in reverse |
| 57 | + // NOTE(@dmehala): It might seem confusing to read those bytes input reverse |
65 | 58 | // order. It is related to the architecture endianess. For now the set of
|
66 | 59 | // architecture we support (x86_64 and arm64) are all little-endian.
|
67 |
| - out.push_back(decoder.bytes[3]); |
68 |
| - out.push_back(decoder.bytes[2]); |
69 |
| - out.push_back(decoder.bytes[1]); |
| 60 | + // TODO(@dgoffredo): I'd prefer an endian-agnostic implementation. |
| 61 | + // nginx-datadog targets x86_64 and arm64 in its binary releases, but |
| 62 | + // dd-trace-cpp targets any standard C++17 compiler. |
| 63 | + output.push_back(decoder.bytes[3]); |
| 64 | + output.push_back(decoder.bytes[2]); |
| 65 | + output.push_back(decoder.bytes[1]); |
70 | 66 | }
|
71 | 67 |
|
72 |
| - if ((in_size - i) < 4) return ""; // not padded input is not supported |
| 68 | + // If padding is missing, return the empty string in lieu of an Error. |
| 69 | + if ((in_size - i) < 4) return ""; |
73 | 70 |
|
74 |
| - auto c0 = k_base64_table[static_cast<size_t>(in[i++])]; |
75 |
| - auto c1 = k_base64_table[static_cast<size_t>(in[i++])]; |
76 |
| - auto c2 = k_base64_table[static_cast<size_t>(in[i++])]; |
77 |
| - auto c3 = k_base64_table[static_cast<size_t>(in[i++])]; |
| 71 | + uint32_t c0 = k_base64_table[static_cast<size_t>(input[i++])]; |
| 72 | + uint32_t c1 = k_base64_table[static_cast<size_t>(input[i++])]; |
| 73 | + uint32_t c2 = k_base64_table[static_cast<size_t>(input[i++])]; |
| 74 | + uint32_t c3 = k_base64_table[static_cast<size_t>(input[i++])]; |
78 | 75 |
|
79 |
| - if (c0 == SENTINEL_VALUE || c1 == SENTINEL_VALUE || c2 == SENTINEL_VALUE || |
80 |
| - c3 == SENTINEL_VALUE) { |
| 76 | + if (c0 == k_sentinel || c1 == k_sentinel || c2 == k_sentinel || |
| 77 | + c3 == k_sentinel) { |
81 | 78 | return "";
|
82 | 79 | }
|
83 | 80 |
|
84 |
| - decoder.buffer = static_cast<uint32_t>(0) | static_cast<uint32_t>(c0) << 26 | |
85 |
| - static_cast<uint32_t>(c1) << 20 | |
86 |
| - static_cast<uint32_t>(c2) << 14 | |
87 |
| - static_cast<uint32_t>(c3) << 8; |
| 81 | + decoder.buffer = 0 | c0 << 26 | c1 << 20 | c2 << 14 | c3 << 8; |
88 | 82 |
|
89 |
| - if (c2 == EOL) { |
90 |
| - out.push_back(decoder.bytes[3]); |
91 |
| - } else if (c3 == EOL) { |
92 |
| - out.push_back(decoder.bytes[3]); |
93 |
| - out.push_back(decoder.bytes[2]); |
| 83 | + if (c2 == k_eol) { |
| 84 | + output.push_back(decoder.bytes[3]); |
| 85 | + } else if (c3 == k_eol) { |
| 86 | + output.push_back(decoder.bytes[3]); |
| 87 | + output.push_back(decoder.bytes[2]); |
94 | 88 | } else {
|
95 |
| - out.push_back(decoder.bytes[3]); |
96 |
| - out.push_back(decoder.bytes[2]); |
97 |
| - out.push_back(decoder.bytes[1]); |
| 89 | + output.push_back(decoder.bytes[3]); |
| 90 | + output.push_back(decoder.bytes[2]); |
| 91 | + output.push_back(decoder.bytes[1]); |
98 | 92 | }
|
99 | 93 |
|
100 |
| - return out; |
| 94 | + return output; |
101 | 95 | }
|
102 | 96 |
|
103 |
| -#undef EOL |
104 |
| -#undef SENTINEL_VALUE |
105 |
| -#undef _ |
106 |
| - |
107 |
| -} // namespace base64 |
108 | 97 | } // namespace tracing
|
109 | 98 | } // namespace datadog
|
0 commit comments