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: #include c@75: #include c@75: #include c@75: #include c@75: #include c@75: c@75: #include "basen.hpp" c@75: c@75: int main() c@75: { c@75: using namespace std; c@75: 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."; c@75: cout << in << endl; c@75: c@75: { c@75: string encoded; c@75: bn::encode_b64(in.begin(), in.end(), back_inserter(encoded)); c@75: bn::decode_b64(encoded.begin(), encoded.end(), ostream_iterator(cout, "")); c@75: cout << endl; c@75: } c@75: { c@75: string encoded; c@75: bn::encode_b32(in.begin(), in.end(), back_inserter(encoded)); c@75: bn::decode_b32(encoded.begin(), encoded.end(), ostream_iterator(cout, "")); c@75: cout << endl; c@75: } c@75: { c@75: string encoded; c@75: bn::encode_b16(in.begin(), in.end(), back_inserter(encoded)); c@75: bn::decode_b16(encoded.begin(), encoded.end(), ostream_iterator(cout, "")); c@75: cout << endl; c@75: } c@75: { c@75: string encoded = "#TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz\n" c@75: "IHNpbmd1bGFyIHBhc3Npb24gZnJvbS@BvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg\n" c@75: " dGhlIG1(pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu\n" c@75: "\rdWVkIGFuZCBpbmRlZmF0aWdhY*mxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo\n" c@75: "ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4"; c@75: bn::decode_b64(encoded.begin(), encoded.end(), ostream_iterator(cout, "")); c@75: cout << endl; c@75: } c@75: { c@75: // move the struct definition outside of main() for non-C++11 compilers c@75: struct b8_custom c@75: { c@75: static size_t group_length() c@75: { c@75: return 3; c@75: } c@75: c@75: static char encode(unsigned int index) c@75: { c@75: const char* const dictionary = "01234567"; 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 <= '7') { c@75: return c - '0'; c@75: } c@75: return -1; c@75: } c@75: }; c@75: string encoded; c@75: bn::impl::encode(in.begin(), in.end(), back_inserter(encoded)); c@75: bn::impl::decode(encoded.begin(), encoded.end(), ostream_iterator(cout, "")); c@75: cout << endl; c@75: } c@75: } c@75: