c@75: /**
c@75:  * base-n, 1.0
c@75:  * Copyright (C) 2012 Andrzej Zawadzki (azawadzki@gmail.com)
c@75:  * 
c@75:  * Permission is hereby granted, free of charge, to any person obtaining a copy
c@75:  * of this software and associated documentation files (the "Software"), to deal
c@75:  * in the Software without restriction, including without limitation the rights
c@75:  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
c@75:  * copies of the Software, and to permit persons to whom the Software is
c@75:  * furnished to do so, subject to the following conditions:
c@75:  * 
c@75:  * The above copyright notice and this permission notice shall be included in
c@75:  * all copies or substantial portions of the Software.
c@75:  * 
c@75:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
c@75:  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
c@75:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
c@75:  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
c@75:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
c@75:  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
c@75:  * SOFTWARE.
c@75: **/
c@75: #ifndef BASEN_HPP
c@75: #define BASEN_HPP
c@75: 
c@75: #include <algorithm>
c@75: #include <cctype>
c@75: #include <cassert>
c@75: #include <cstring>
c@75: 
c@75: namespace bn
c@75: {
c@75: 
c@75: template<class Iter1, class Iter2>
c@75: void encode_b16(Iter1 start, Iter1 end, Iter2 out);
c@75: 
c@75: template<class Iter1, class Iter2>
c@75: void encode_b32(Iter1 start, Iter1 end, Iter2 out);
c@75: 
c@75: template<class Iter1, class Iter2>
c@75: void encode_b64(Iter1 start, Iter1 end, Iter2 out);
c@75: 
c@75: template<class Iter1, class Iter2>
c@75: void decode_b16(Iter1 start, Iter1 end, Iter2 out);
c@75: 
c@75: template<class Iter1, class Iter2>
c@75: void decode_b32(Iter1 start, Iter1 end, Iter2 out);
c@75: 
c@75: template<class Iter1, class Iter2>
c@75: void decode_b64(Iter1 start, Iter1 end, Iter2 out);
c@75: 
c@75: namespace impl
c@75: {
c@75: 
c@75: const int ERROR = -1;
c@75: 
c@75: namespace {
c@75: 
c@75: char extract_partial_bits(char value, unsigned int start_bit, unsigned int bits_count)
c@75: {
c@75:     assert(start_bit + bits_count < 8);
c@75:     // shift extracted bits to the beginning of the byte
c@75:     char t1 = value >> (8 - bits_count - start_bit);
c@75:     // mask out bits on the left
c@75:     char t2 = t1 & ~(-1U << bits_count);
c@75:     return t2;
c@75: }
c@75: 
c@75: char extract_overlapping_bits(char previous, char next, unsigned int start_bit, unsigned int bits_count)
c@75: {
c@75:     assert(start_bit + bits_count < 16);
c@75:     int bits_count_in_previous = 8 - start_bit;
c@75:     int bits_count_in_next = bits_count - bits_count_in_previous;
c@75:     char t1 = previous << bits_count_in_next;
c@75:     char t2 = next >> (8 - bits_count_in_next) & ~(-1U << bits_count_in_next) ;
c@75:     return (t1 | t2) & ~(-1U << bits_count);
c@75: }
c@75: 
c@75: }
c@75: 
c@75: struct b16_conversion_traits
c@75: {
c@75:     static size_t group_length()
c@75:     {
c@75:        return 4;
c@75:     }
c@75: 
c@75:     static char encode(unsigned int index)
c@75:     {
c@75:         const char* const dictionary = "0123456789ABCDEF";
c@75:         assert(index < strlen(dictionary));
c@75:         return dictionary[index];
c@75:     }
c@75: 
c@75:     static char decode(char c)
c@75:     {
c@75:         if (c >= '0' && c <= '9') {
c@75:             return c - '0';
c@75:         } else if (c >= 'A' && c <= 'F') {
c@75:             return c - 'A' + 10;
c@75:         }
c@75:         return ERROR;
c@75:     }
c@75: };
c@75: 
c@75: struct b32_conversion_traits
c@75: {
c@75:     static size_t group_length()
c@75:     {
c@75:        return 5;
c@75:     }
c@75: 
c@75:     static char encode(unsigned int index)
c@75:     {
c@75:         const char * dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
c@75:         assert(index < strlen(dictionary));
c@75:         return dictionary[index];
c@75:     }
c@75: 
c@75:     static char decode(char c)
c@75:     {
c@75:         if (c >= 'A' && c <= 'Z') {
c@75:             return c - 'A';
c@75:         } else if (c >= '2' && c <= '7') {
c@75:             return c - '2' + 26;
c@75:         }
c@75:         return ERROR;
c@75:     }
c@75: };
c@75: 
c@75: struct b64_conversion_traits
c@75: {
c@75:     static size_t group_length()
c@75:     {
c@75:        return 6;
c@75:     }
c@75: 
c@75:     static char encode(unsigned int index)
c@75:     {
c@75:         const char* const dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
c@75:         assert(index < strlen(dictionary));
c@75:         return dictionary[index];
c@75:     }
c@75: 
c@75:     static char decode(char c)
c@75:     {
c@75:         const int alph_len = 26;
c@75:         if (c >= 'A' && c <= 'Z') {
c@75:             return c - 'A';
c@75:         } else if (c >= 'a' && c <= 'z') {
c@75:             return c - 'a' + alph_len * 1;
c@75:         } else if (c >= '0' && c <= '9') {
c@75:             return c - '0' + alph_len * 2;
c@75:         } else if (c == '+') {
c@75:             return c - '+' + alph_len * 2 + 10;
c@75:         } else if (c == '/') {
c@75:             return c - '/' + alph_len * 2 + 11;
c@75:         }
c@75:         return ERROR;
c@75:     }
c@75: };
c@75: 
c@75: template<class ConversionTraits, class Iter1, class Iter2>
c@75: void decode(Iter1 start, Iter1 end, Iter2 out)
c@75: {
c@75:     Iter1 iter = start;
c@75:     int output_current_bit = 0;
c@75:     char buffer = 0;
c@75: 
c@75:     while (iter != end) {
c@75:         if (std::isspace(*iter)) {
c@75:             ++iter;
c@75:             continue;
c@75:         }
c@75:         char value = ConversionTraits::decode(*iter);
c@75:         if (value == ERROR) {
c@75:             // malformed data, but let's go on...
c@75:             ++iter;
c@75:             continue;
c@75:         }
c@75:         unsigned int bits_in_current_byte = std::min<int>(output_current_bit + ConversionTraits::group_length(), 8) - output_current_bit;
c@75:         if (bits_in_current_byte == ConversionTraits::group_length()) {
c@75:             // the value fits within current byte, so we can extract it directly
c@75:             buffer |= value << (8 - output_current_bit - ConversionTraits::group_length());
c@75:             output_current_bit += ConversionTraits::group_length();
c@75:             // check if we filled up current byte completely; in such case we flush output and continue
c@75:             if (output_current_bit == 8) {
c@75:                 *out++ = buffer;
c@75:                 buffer = 0;
c@75:                 output_current_bit = 0;
c@75:             }
c@75:         } else {
c@75:             // the value spans across the current and the next byte
c@75:             int bits_in_next_byte = ConversionTraits::group_length() - bits_in_current_byte;
c@75:             // fill the current byte and flush it to our output
c@75:             buffer |= value >> bits_in_next_byte;
c@75:             *out++ = buffer;
c@75:             buffer = 0;
c@75:             // save the remainder of our value in the buffer; it will be flushed
c@75:             // during next iterations
c@75:             buffer |= value << (8 - bits_in_next_byte);
c@75:             output_current_bit = bits_in_next_byte;
c@75:         }
c@75:         ++iter;
c@75:     }
c@75: }
c@75: 
c@75: template<class ConversionTraits, class Iter1, class Iter2>
c@75: void encode(Iter1 start, Iter1 end, Iter2 out)
c@75: {
c@75:     Iter1 iter = start;
c@75:     int start_bit = 0;
c@75:     bool has_backlog = false;
c@75:     char backlog = 0;
c@75: 
c@75:     while (has_backlog || iter != end) {
c@75:         if (!has_backlog) {
c@75:             if (start_bit + ConversionTraits::group_length() < 8) {
c@75:                 // the value fits within single byte, so we can extract it
c@75:                 // directly
c@75:                 char v = extract_partial_bits(*iter, start_bit, ConversionTraits::group_length());
c@75:                 *out++ = ConversionTraits::encode(v);
c@75:                 // since we know that start_bit + ConversionTraits::group_length() < 8 we don't need to go
c@75:                 // to the next byte
c@75:                 start_bit += ConversionTraits::group_length();
c@75:             } else {
c@75:                 // our bits are spanning across byte border; we need to keep the
c@75:                 // starting point and move over to next byte.
c@75:                 backlog = *iter++;
c@75:                 has_backlog = true;
c@75:             }
c@75:         } else {
c@75:             // encode value which is made from bits spanning across byte
c@75:             // boundary
c@75:             char v;
c@75:             if (iter == end)
c@75:                  v = extract_overlapping_bits(backlog, 0, start_bit, ConversionTraits::group_length());
c@75:             else
c@75:                  v = extract_overlapping_bits(backlog, *iter, start_bit, ConversionTraits::group_length());
c@75:             *out++ = ConversionTraits::encode(v);
c@75:             has_backlog = false;
c@75:             start_bit = (start_bit + ConversionTraits::group_length()) % 8;
c@75:         }
c@75:     }
c@75: }
c@75: 
c@75: } // impl
c@75: 
c@75: using namespace bn::impl;
c@75: 
c@75: template<class Iter1, class Iter2>
c@75: void encode_b16(Iter1 start, Iter1 end, Iter2 out)
c@75: {
c@75:     encode<b16_conversion_traits>(start, end, out);
c@75: }
c@75: 
c@75: template<class Iter1, class Iter2>
c@75: void encode_b32(Iter1 start, Iter1 end, Iter2 out)
c@75: {
c@75:     encode<b32_conversion_traits>(start, end, out);
c@75: }
c@75: 
c@75: template<class Iter1, class Iter2>
c@75: void encode_b64(Iter1 start, Iter1 end, Iter2 out)
c@75: {
c@75:     encode<b64_conversion_traits>(start, end, out);
c@75: }
c@75: 
c@75: template<class Iter1, class Iter2>
c@75: void decode_b16(Iter1 start, Iter1 end, Iter2 out)
c@75: {
c@75:     decode<b16_conversion_traits>(start, end, out);
c@75: }
c@75: 
c@75: template<class Iter1, class Iter2>
c@75: void decode_b32(Iter1 start, Iter1 end, Iter2 out)
c@75: {
c@75:     decode<b32_conversion_traits>(start, end, out);
c@75: }
c@75: 
c@75: template<class Iter1, class Iter2>
c@75: void decode_b64(Iter1 start, Iter1 end, Iter2 out)
c@75: {
c@75:     decode<b64_conversion_traits>(start, end, out);
c@75: }
c@75: 
c@75: } // bn
c@75: 
c@75: #endif // BASEN_HPP
c@75: