cannam@150: /** cannam@150: * base-n, 1.0 cannam@150: * Copyright (C) 2012 Andrzej Zawadzki (azawadzki@gmail.com) cannam@150: * cannam@150: * Permission is hereby granted, free of charge, to any person obtaining a copy cannam@150: * of this software and associated documentation files (the "Software"), to deal cannam@150: * in the Software without restriction, including without limitation the rights cannam@150: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cannam@150: * copies of the Software, and to permit persons to whom the Software is cannam@150: * furnished to do so, subject to the following conditions: cannam@150: * cannam@150: * The above copyright notice and this permission notice shall be included in cannam@150: * all copies or substantial portions of the Software. cannam@150: * cannam@150: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@150: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@150: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@150: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER cannam@150: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, cannam@150: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE cannam@150: * SOFTWARE. cannam@150: **/ cannam@150: #include cannam@150: #include cannam@150: #include cannam@150: #include cannam@150: #include cannam@150: cannam@150: #include "basen.hpp" cannam@150: cannam@150: int main() cannam@150: { cannam@150: using namespace std; cannam@150: string in = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure."; cannam@150: cout << in << endl; cannam@150: cannam@150: { cannam@150: string encoded; cannam@150: bn::encode_b64(in.begin(), in.end(), back_inserter(encoded)); cannam@150: bn::decode_b64(encoded.begin(), encoded.end(), ostream_iterator(cout, "")); cannam@150: cout << endl; cannam@150: } cannam@150: { cannam@150: string encoded; cannam@150: bn::encode_b32(in.begin(), in.end(), back_inserter(encoded)); cannam@150: bn::decode_b32(encoded.begin(), encoded.end(), ostream_iterator(cout, "")); cannam@150: cout << endl; cannam@150: } cannam@150: { cannam@150: string encoded; cannam@150: bn::encode_b16(in.begin(), in.end(), back_inserter(encoded)); cannam@150: bn::decode_b16(encoded.begin(), encoded.end(), ostream_iterator(cout, "")); cannam@150: cout << endl; cannam@150: } cannam@150: { cannam@150: string encoded = "#TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\n" cannam@150: "IHNpbmd1bGFyIHBhc3Npb24gZnJvbS@BvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\n" cannam@150: " dGhlIG1(pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\n" cannam@150: "\rdWVkIGFuZCBpbmRlZmF0aWdhY*mxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\n" cannam@150: "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4"; cannam@150: bn::decode_b64(encoded.begin(), encoded.end(), ostream_iterator(cout, "")); cannam@150: cout << endl; cannam@150: } cannam@150: { cannam@150: // move the struct definition outside of main() for non-C++11 compilers cannam@150: struct b8_custom cannam@150: { cannam@150: static size_t group_length() cannam@150: { cannam@150: return 3; cannam@150: } cannam@150: cannam@150: static char encode(unsigned int index) cannam@150: { cannam@150: const char* const dictionary = "01234567"; cannam@150: assert(index < strlen(dictionary)); cannam@150: return dictionary[index]; cannam@150: } cannam@150: cannam@150: static char decode(char c) cannam@150: { cannam@150: if (c >= '0' && c <= '7') { cannam@150: return c - '0'; cannam@150: } cannam@150: return -1; cannam@150: } cannam@150: }; cannam@150: string encoded; cannam@150: bn::impl::encode(in.begin(), in.end(), back_inserter(encoded)); cannam@150: bn::impl::decode(encoded.begin(), encoded.end(), ostream_iterator(cout, "")); cannam@150: cout << endl; cannam@150: } cannam@150: } cannam@150: