3 * Copyright (C) 2012 Andrzej Zawadzki (azawadzki@gmail.com)
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 template<class Iter1, class Iter2>
35 void encode_b16(Iter1 start, Iter1 end, Iter2 out);
37 template<class Iter1, class Iter2>
38 void encode_b32(Iter1 start, Iter1 end, Iter2 out);
40 template<class Iter1, class Iter2>
41 void encode_b64(Iter1 start, Iter1 end, Iter2 out);
43 template<class Iter1, class Iter2>
44 void decode_b16(Iter1 start, Iter1 end, Iter2 out);
46 template<class Iter1, class Iter2>
47 void decode_b32(Iter1 start, Iter1 end, Iter2 out);
49 template<class Iter1, class Iter2>
50 void decode_b64(Iter1 start, Iter1 end, Iter2 out);
59 char extract_partial_bits(char value, size_t start_bit, size_t bits_count)
61 assert(start_bit + bits_count < 8);
62 // shift extracted bits to the beginning of the byte
63 char t1 = value >> (8 - bits_count - start_bit);
64 // mask out bits on the left
65 char t2 = t1 & ~(0xff << bits_count);
69 char extract_overlapping_bits(char previous, char next, size_t start_bit, size_t bits_count)
71 assert(start_bit + bits_count < 16);
72 size_t bits_count_in_previous = 8 - start_bit;
73 size_t bits_count_in_next = bits_count - bits_count_in_previous;
74 char t1 = previous << bits_count_in_next;
75 char t2 = next >> (8 - bits_count_in_next) & ~(0xff << bits_count_in_next) ;
76 return (t1 | t2) & ~(0xff << bits_count);
81 struct b16_conversion_traits
83 static size_t group_length()
88 static char encode(unsigned int index)
90 const char* const dictionary = "0123456789ABCDEF";
91 assert(index < strlen(dictionary));
92 return dictionary[index];
95 static char decode(char c)
97 if (c >= '0' && c <= '9') {
99 } else if (c >= 'A' && c <= 'F') {
106 struct b32_conversion_traits
108 static size_t group_length()
113 static char encode(unsigned int index)
115 const char * dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
116 assert(index < strlen(dictionary));
117 return dictionary[index];
120 static char decode(char c)
122 if (c >= 'A' && c <= 'Z') {
124 } else if (c >= '2' && c <= '7') {
131 struct b64_conversion_traits
133 static size_t group_length()
138 static char encode(unsigned int index)
140 const char* const dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
141 assert(index < strlen(dictionary));
142 return dictionary[index];
145 static char decode(char c)
147 const int alph_len = 26;
148 if (c >= 'A' && c <= 'Z') {
150 } else if (c >= 'a' && c <= 'z') {
151 return c - 'a' + alph_len * 1;
152 } else if (c >= '0' && c <= '9') {
153 return c - '0' + alph_len * 2;
154 } else if (c == '+') {
155 return c - '+' + alph_len * 2 + 10;
156 } else if (c == '/') {
157 return c - '/' + alph_len * 2 + 11;
163 template<class ConversionTraits, class Iter1, class Iter2>
164 void decode(Iter1 start, Iter1 end, Iter2 out)
167 size_t output_current_bit = 0;
170 while (iter != end) {
171 if (std::isspace(*iter)) {
175 char value = ConversionTraits::decode(*iter);
176 if (value == Error) {
177 // malformed data, but let's go on...
181 size_t bits_in_current_byte = std::min<size_t>(output_current_bit + ConversionTraits::group_length(), 8) - output_current_bit;
182 if (bits_in_current_byte == ConversionTraits::group_length()) {
183 // the value fits within current byte, so we can extract it directly
184 buffer |= value << (8 - output_current_bit - ConversionTraits::group_length());
185 output_current_bit += ConversionTraits::group_length();
186 // check if we filled up current byte completely; in such case we flush output and continue
187 if (output_current_bit == 8) {
190 output_current_bit = 0;
193 // the value spans across the current and the next byte
194 size_t bits_in_next_byte = ConversionTraits::group_length() - bits_in_current_byte;
195 // fill the current byte and flush it to our output
196 buffer |= value >> bits_in_next_byte;
199 // save the remainder of our value in the buffer; it will be flushed
200 // during next iterations
201 buffer |= value << (8 - bits_in_next_byte);
202 output_current_bit = bits_in_next_byte;
208 template<class ConversionTraits, class Iter1, class Iter2>
209 void encode(Iter1 start, Iter1 end, Iter2 out)
212 size_t start_bit = 0;
213 bool has_backlog = false;
216 while (has_backlog || iter != end) {
218 if (start_bit + ConversionTraits::group_length() < 8) {
219 // the value fits within single byte, so we can extract it
221 char v = extract_partial_bits(*iter, start_bit, ConversionTraits::group_length());
222 *out++ = ConversionTraits::encode(v);
223 // since we know that start_bit + ConversionTraits::group_length() < 8 we don't need to go
225 start_bit += ConversionTraits::group_length();
227 // our bits are spanning across byte border; we need to keep the
228 // starting point and move over to next byte.
233 // encode value which is made from bits spanning across byte
237 v = extract_overlapping_bits(backlog, 0, start_bit, ConversionTraits::group_length());
239 v = extract_overlapping_bits(backlog, *iter, start_bit, ConversionTraits::group_length());
240 *out++ = ConversionTraits::encode(v);
242 start_bit = (start_bit + ConversionTraits::group_length()) % 8;
249 using namespace bn::impl;
251 template<class Iter1, class Iter2>
252 void encode_b16(Iter1 start, Iter1 end, Iter2 out)
254 encode<b16_conversion_traits>(start, end, out);
257 template<class Iter1, class Iter2>
258 void encode_b32(Iter1 start, Iter1 end, Iter2 out)
260 encode<b32_conversion_traits>(start, end, out);
263 template<class Iter1, class Iter2>
264 void encode_b64(Iter1 start, Iter1 end, Iter2 out)
266 encode<b64_conversion_traits>(start, end, out);
269 template<class Iter1, class Iter2>
270 void decode_b16(Iter1 start, Iter1 end, Iter2 out)
272 decode<b16_conversion_traits>(start, end, out);
275 template<class Iter1, class Iter2>
276 void decode_b32(Iter1 start, Iter1 end, Iter2 out)
278 decode<b32_conversion_traits>(start, end, out);
281 template<class Iter1, class Iter2>
282 void decode_b64(Iter1 start, Iter1 end, Iter2 out)
284 decode<b64_conversion_traits>(start, end, out);