/proc/self/cwd/pw_base64/base64.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2020 The Pigweed Authors |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
4 | | // use this file except in compliance with the License. You may obtain a copy of |
5 | | // the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
11 | | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
12 | | // License for the specific language governing permissions and limitations under |
13 | | // the License. |
14 | | |
15 | | #include "pw_base64/base64.h" |
16 | | |
17 | | #include <cstdint> |
18 | | |
19 | | #include "pw_assert/check.h" |
20 | | |
21 | | namespace pw::base64 { |
22 | | namespace { |
23 | | |
24 | | // Encoding functions |
25 | | constexpr size_t kEncodedGroupSize = 4; |
26 | | constexpr char kChar62 = '+'; // URL safe encoding uses - instead |
27 | | constexpr char kChar63 = '/'; // URL safe encoding uses _ instead |
28 | | constexpr char kPadding = '='; |
29 | | |
30 | | // Table that encodes a 6-bit pattern as a Base64 character |
31 | | constexpr char kEncodeTable[64] = { |
32 | | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |
33 | | 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |
34 | | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |
35 | | 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |
36 | | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', kChar62, kChar63}; |
37 | | |
38 | 0 | constexpr char BitGroup0Char(uint8_t byte0) { |
39 | 0 | return kEncodeTable[(byte0 & 0b11111100) >> 2]; |
40 | 0 | } |
41 | 0 | constexpr char BitGroup1Char(uint8_t byte0, uint8_t byte1 = 0) { |
42 | 0 | return kEncodeTable[((byte0 & 0b00000011) << 4) | |
43 | 0 | ((byte1 & 0b11110000) >> 4)]; |
44 | 0 | } |
45 | 0 | constexpr char BitGroup2Char(uint8_t byte1, uint8_t byte2 = 0) { |
46 | 0 | return kEncodeTable[((byte1 & 0b00001111) << 2) | |
47 | 0 | ((byte2 & 0b11000000) >> 6)]; |
48 | 0 | } |
49 | 0 | constexpr char BitGroup3Char(uint8_t byte2) { |
50 | 0 | return kEncodeTable[byte2 & 0b00111111]; |
51 | 0 | } |
52 | | |
53 | | // Decoding functions |
54 | | constexpr char kMinValidChar = '+'; |
55 | | constexpr char kMaxValidChar = 'z'; |
56 | | constexpr uint8_t kX = 0xff; // Value used for invalid characters |
57 | | |
58 | | // Table that decodes a Base64 character to its 6-bit value. Supports the |
59 | | // standard (+/) and URL-safe (-_) alphabets. Starts from the lowest-value valid |
60 | | // character, which is +. |
61 | | constexpr uint8_t kDecodeTable[] = { |
62 | | 62, kX, 62, kX, 63, 52, 53, 54, 55, 56, // 0 - 09 |
63 | | 57, 58, 59, 60, 61, kX, kX, kX, kX, kX, // 10 - 19 |
64 | | kX, kX, 0, 1, 2, 3, 4, 5, 6, 7, // 20 - 29 |
65 | | 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, // 30 - 39 |
66 | | 18, 19, 20, 21, 22, 23, 24, 25, kX, kX, // 40 - 49 |
67 | | kX, kX, 63, kX, 26, 27, 28, 29, 30, 31, // 50 - 59 |
68 | | 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, // 60 - 69 |
69 | | 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 70 - 79 |
70 | | }; |
71 | | |
72 | 0 | constexpr uint8_t CharToBits(char ch) { |
73 | 0 | return kDecodeTable[ch - kMinValidChar]; |
74 | 0 | } |
75 | | |
76 | 0 | constexpr uint8_t Byte0(uint8_t bits0, uint8_t bits1) { |
77 | 0 | return static_cast<uint8_t>(bits0 << 2) | ((bits1 & 0b110000) >> 4); |
78 | 0 | } |
79 | 0 | constexpr uint8_t Byte1(uint8_t bits1, uint8_t bits2) { |
80 | 0 | return static_cast<uint8_t>((bits1 & 0b001111) << 4) | |
81 | 0 | ((bits2 & 0b111100) >> 2); |
82 | 0 | } |
83 | 0 | constexpr uint8_t Byte2(uint8_t bits2, uint8_t bits3) { |
84 | 0 | return static_cast<uint8_t>((bits2 & 0b000011) << 6) | bits3; |
85 | 0 | } |
86 | | |
87 | | } // namespace |
88 | | |
89 | | extern "C" void pw_Base64Encode(const void* binary_data, |
90 | | const size_t binary_size_bytes, |
91 | 0 | char* output) { |
92 | 0 | const uint8_t* bytes = static_cast<const uint8_t*>(binary_data); |
93 | | |
94 | | // Encode groups of 3 source bytes into 4 output characters. |
95 | 0 | size_t remaining = binary_size_bytes; |
96 | 0 | for (; remaining >= 3u; remaining -= 3u, bytes += 3) { |
97 | 0 | *output++ = BitGroup0Char(bytes[0]); |
98 | 0 | *output++ = BitGroup1Char(bytes[0], bytes[1]); |
99 | 0 | *output++ = BitGroup2Char(bytes[1], bytes[2]); |
100 | 0 | *output++ = BitGroup3Char(bytes[2]); |
101 | 0 | } |
102 | | |
103 | | // If the source data length isn't a multiple of 3, pad the end with either 1 |
104 | | // or 2 '=' characters. |
105 | 0 | if (remaining > 0u) { |
106 | 0 | *output++ = BitGroup0Char(bytes[0]); |
107 | 0 | if (remaining == 1u) { |
108 | 0 | *output++ = BitGroup1Char(bytes[0]); |
109 | 0 | *output++ = kPadding; |
110 | 0 | } else { |
111 | 0 | *output++ = BitGroup1Char(bytes[0], bytes[1]); |
112 | 0 | *output++ = BitGroup2Char(bytes[1]); |
113 | 0 | } |
114 | 0 | *output++ = kPadding; |
115 | 0 | } |
116 | 0 | } |
117 | | |
118 | | extern "C" size_t pw_Base64Decode(const char* base64, |
119 | | const size_t base64_size_bytes, |
120 | 0 | void* const output) { |
121 | | // If empty or missing padding, return 0. |
122 | 0 | if (base64_size_bytes == 0 || base64_size_bytes % kEncodedGroupSize != 0) { |
123 | 0 | return 0; |
124 | 0 | } |
125 | | |
126 | 0 | uint8_t* binary = static_cast<uint8_t*>(output); |
127 | 0 | size_t ch = 0; |
128 | 0 | for (; ch < base64_size_bytes - kEncodedGroupSize; ch += kEncodedGroupSize) { |
129 | 0 | const uint8_t char0 = CharToBits(base64[ch + 0]); |
130 | 0 | const uint8_t char1 = CharToBits(base64[ch + 1]); |
131 | 0 | const uint8_t char2 = CharToBits(base64[ch + 2]); |
132 | 0 | const uint8_t char3 = CharToBits(base64[ch + 3]); |
133 | |
|
134 | 0 | binary[0] = Byte0(char0, char1); |
135 | 0 | binary[1] = Byte1(char1, char2); |
136 | 0 | binary[2] = Byte2(char2, char3); |
137 | 0 | binary += 3; |
138 | 0 | } |
139 | | |
140 | | // Decode the final group, which may include padding. |
141 | 0 | const uint8_t char0 = CharToBits(base64[ch + 0]); |
142 | 0 | const uint8_t char1 = CharToBits(base64[ch + 1]); |
143 | 0 | const uint8_t char2 = CharToBits(base64[ch + 2]); |
144 | 0 | const uint8_t char3 = CharToBits(base64[ch + 3]); |
145 | |
|
146 | 0 | *binary++ = Byte0(char0, char1); |
147 | |
|
148 | 0 | if (base64[ch + 2] != kPadding) { |
149 | 0 | *binary++ = Byte1(char1, char2); |
150 | 0 | if (base64[ch + 3] != kPadding) { |
151 | 0 | *binary++ = Byte2(char2, char3); |
152 | 0 | } |
153 | 0 | } |
154 | |
|
155 | 0 | return static_cast<size_t>(binary - static_cast<uint8_t*>(output)); |
156 | 0 | } |
157 | | |
158 | 0 | extern "C" bool pw_Base64IsValidChar(char base64_char) { |
159 | 0 | return !(base64_char < kMinValidChar || base64_char > kMaxValidChar || |
160 | 0 | CharToBits(base64_char) == kX /* invalid char */); |
161 | 0 | } |
162 | | |
163 | 0 | extern "C" bool pw_Base64IsValid(const char* base64_data, size_t base64_size) { |
164 | 0 | if (base64_size == 0u) { |
165 | 0 | return true; |
166 | 0 | } |
167 | | |
168 | 0 | if (base64_size % kEncodedGroupSize != 0) { |
169 | 0 | return false; |
170 | 0 | } |
171 | | |
172 | | // Check up to the last two characters, which are potentially padding. |
173 | 0 | for (size_t i = 0; i < base64_size - 2; ++i) { |
174 | 0 | if (!pw_Base64IsValidChar(base64_data[i])) { |
175 | 0 | return false; |
176 | 0 | } |
177 | 0 | } |
178 | | |
179 | | // Check the last two possibly padding characters. |
180 | 0 | if (base64_data[base64_size - 2] == kPadding) { |
181 | 0 | return base64_data[base64_size - 1] == kPadding; |
182 | 0 | } |
183 | | |
184 | 0 | return pw_Base64IsValidChar(base64_data[base64_size - 1]) || |
185 | 0 | base64_data[base64_size - 1] == kPadding; |
186 | 0 | } |
187 | | |
188 | 0 | size_t Encode(span<const std::byte> binary, span<char> output_buffer) { |
189 | 0 | const size_t required_size = EncodedSize(binary.size_bytes()); |
190 | 0 | if (output_buffer.size_bytes() < required_size) { |
191 | 0 | return 0; |
192 | 0 | } |
193 | 0 | pw_Base64Encode(binary.data(), binary.size_bytes(), output_buffer.data()); |
194 | 0 | return required_size; |
195 | 0 | } |
196 | | |
197 | 0 | size_t Decode(std::string_view base64, span<std::byte> output_buffer) { |
198 | 0 | if (output_buffer.size_bytes() < MaxDecodedSize(base64.size()) || |
199 | 0 | !IsValid(base64)) { |
200 | 0 | return 0; |
201 | 0 | } |
202 | 0 | return Decode(base64, output_buffer.data()); |
203 | 0 | } |
204 | | |
205 | 0 | void Encode(span<const std::byte> binary, InlineString<>& output) { |
206 | 0 | const size_t initial_size = output.size(); |
207 | 0 | const size_t final_size = initial_size + EncodedSize(binary.size()); |
208 | |
|
209 | 0 | PW_CHECK(final_size <= output.capacity()); |
210 | | |
211 | 0 | output.resize_and_overwrite([&](char* data, size_t) { |
212 | 0 | Encode(binary, data + initial_size); |
213 | 0 | return final_size; |
214 | 0 | }); |
215 | 0 | } |
216 | | |
217 | | } // namespace pw::base64 |