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 namespace pw::base64 {
20 namespace {
21
22 // Encoding functions
23 constexpr size_t kEncodedGroupSize = 4;
24 constexpr char kChar62 = '+'; // URL safe encoding uses - instead
25 constexpr char kChar63 = '/'; // URL safe encoding uses _ instead
26 constexpr char kPadding = '=';
27
28 // Table that encodes a 6-bit pattern as a Base64 character
29 constexpr char encode_bits[64] = {
30 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
31 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
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 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', kChar62, kChar63};
35
BitGroup0Char(uint8_t byte0)36 constexpr char BitGroup0Char(uint8_t byte0) {
37 return encode_bits[(byte0 & 0b11111100) >> 2];
38 }
BitGroup1Char(uint8_t byte0,uint8_t byte1=0)39 constexpr char BitGroup1Char(uint8_t byte0, uint8_t byte1 = 0) {
40 return encode_bits[((byte0 & 0b00000011) << 4) | ((byte1 & 0b11110000) >> 4)];
41 }
BitGroup2Char(uint8_t byte1,uint8_t byte2=0)42 constexpr char BitGroup2Char(uint8_t byte1, uint8_t byte2 = 0) {
43 return encode_bits[((byte1 & 0b00001111) << 2) | ((byte2 & 0b11000000) >> 6)];
44 }
BitGroup3Char(uint8_t byte2)45 constexpr char BitGroup3Char(uint8_t byte2) {
46 return encode_bits[byte2 & 0b00111111];
47 }
48
49 // Decoding functions
50 constexpr char kMinValidChar = '+';
51 constexpr char kMaxValidChar = 'z';
52 constexpr uint8_t kX = 0xff; // Value used for invalid characters
53
54 // Table that decodes a Base64 character to its 6-bit value. Supports the
55 // standard (+/) and URL-safe (-_) alphabets. Starts from the lowest-value valid
56 // character, which is +.
57 constexpr uint8_t decode_char[] = {
58 62, kX, 62, kX, 63, 52, 53, 54, 55, 56, // 0 - 09
59 57, 58, 59, 60, 61, kX, kX, kX, 0, kX, // 10 - 19
60 kX, kX, 0, 1, 2, 3, 4, 5, 6, 7, // 20 - 29
61 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, // 30 - 39
62 18, 19, 20, 21, 22, 23, 24, 25, kX, kX, // 40 - 49
63 kX, kX, 63, kX, 26, 27, 28, 29, 30, 31, // 50 - 59
64 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, // 60 - 69
65 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 70 - 79
66 };
67
CharToBits(char ch)68 constexpr uint8_t CharToBits(char ch) {
69 return decode_char[ch - kMinValidChar];
70 }
71
Byte0(uint8_t bits0,uint8_t bits1)72 constexpr uint8_t Byte0(uint8_t bits0, uint8_t bits1) {
73 return (bits0 << 2) | ((bits1 & 0b110000) >> 4);
74 }
Byte1(uint8_t bits1,uint8_t bits2)75 constexpr uint8_t Byte1(uint8_t bits1, uint8_t bits2) {
76 return ((bits1 & 0b001111) << 4) | ((bits2 & 0b111100) >> 2);
77 }
Byte2(uint8_t bits2,uint8_t bits3)78 constexpr uint8_t Byte2(uint8_t bits2, uint8_t bits3) {
79 return ((bits2 & 0b000011) << 6) | bits3;
80 }
81
82 } // namespace
83
pw_Base64Encode(const void * binary_data,const size_t binary_size_bytes,char * output)84 extern "C" void pw_Base64Encode(const void* binary_data,
85 const size_t binary_size_bytes,
86 char* output) {
87 const uint8_t* bytes = static_cast<const uint8_t*>(binary_data);
88
89 // Encode groups of 3 source bytes into 4 output characters.
90 size_t remaining = binary_size_bytes;
91 for (; remaining >= 3u; remaining -= 3u, bytes += 3) {
92 *output++ = BitGroup0Char(bytes[0]);
93 *output++ = BitGroup1Char(bytes[0], bytes[1]);
94 *output++ = BitGroup2Char(bytes[1], bytes[2]);
95 *output++ = BitGroup3Char(bytes[2]);
96 }
97
98 // If the source data length isn't a multiple of 3, pad the end with either 1
99 // or 2 '=' characters, to stay Python-compatible.
100 if (remaining > 0u) {
101 *output++ = BitGroup0Char(bytes[0]);
102 if (remaining == 1u) {
103 *output++ = BitGroup1Char(bytes[0]);
104 *output++ = kPadding;
105 } else {
106 *output++ = BitGroup1Char(bytes[0], bytes[1]);
107 *output++ = BitGroup2Char(bytes[1]);
108 }
109 *output++ = kPadding;
110 }
111 }
112
pw_Base64Decode(const char * base64,size_t base64_size_bytes,void * output)113 extern "C" size_t pw_Base64Decode(const char* base64,
114 size_t base64_size_bytes,
115 void* output) {
116 // If too small, can't be valid input, due to likely missing padding
117 if (base64_size_bytes < 4) {
118 return 0;
119 }
120
121 uint8_t* binary = static_cast<uint8_t*>(output);
122 for (size_t ch = 0; ch < base64_size_bytes; ch += kEncodedGroupSize) {
123 const uint8_t char0 = CharToBits(base64[ch + 0]);
124 const uint8_t char1 = CharToBits(base64[ch + 1]);
125 const uint8_t char2 = CharToBits(base64[ch + 2]);
126 const uint8_t char3 = CharToBits(base64[ch + 3]);
127
128 *binary++ = Byte0(char0, char1);
129 *binary++ = Byte1(char1, char2);
130 *binary++ = Byte2(char2, char3);
131 }
132
133 size_t pad = 0;
134 if (base64[base64_size_bytes - 2] == kPadding) {
135 pad = 2;
136 } else if (base64[base64_size_bytes - 1] == kPadding) {
137 pad = 1;
138 }
139
140 return binary - static_cast<uint8_t*>(output) - pad;
141 }
142
pw_Base64IsValid(const char * base64_data,size_t base64_size)143 extern "C" bool pw_Base64IsValid(const char* base64_data, size_t base64_size) {
144 if (base64_size % kEncodedGroupSize != 0) {
145 return false;
146 }
147
148 for (size_t i = 0; i < base64_size; ++i) {
149 if (base64_data[i] < kMinValidChar || base64_data[i] > kMaxValidChar ||
150 CharToBits(base64_data[i]) == kX /* invalid char */) {
151 return false;
152 }
153 }
154 return true;
155 }
156
Encode(std::span<const std::byte> binary,std::span<char> output_buffer)157 size_t Encode(std::span<const std::byte> binary,
158 std::span<char> output_buffer) {
159 const size_t required_size = EncodedSize(binary.size_bytes());
160 if (output_buffer.size_bytes() < required_size) {
161 return 0;
162 }
163 pw_Base64Encode(binary.data(), binary.size_bytes(), output_buffer.data());
164 return required_size;
165 }
166
Decode(std::string_view base64,std::span<std::byte> output_buffer)167 size_t Decode(std::string_view base64, std::span<std::byte> output_buffer) {
168 if (output_buffer.size_bytes() < MaxDecodedSize(base64.size()) ||
169 !IsValid(base64)) {
170 return 0;
171 }
172 return Decode(base64, output_buffer.data());
173 }
174
175 } // namespace pw::base64
176