00001 /* 00002 * main.cpp 00003 * 00004 * Copyright (C) 2007,2011 Thomas A. Vaughan 00005 * All rights reserved. 00006 * 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions are met: 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * * Neither the name of the <organization> nor the 00016 * names of its contributors may be used to endorse or promote products 00017 * derived from this software without specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY 00020 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00022 * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY 00023 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00026 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 * 00030 * Bogus base64 encoding (only works when decoded using crypto::decodeBase64) 00031 */ 00032 00033 // includes -------------------------------------------------------------------- 00034 #include "wave-crypto/wave-crypto.h" 00035 00036 #include <iostream> 00037 #include <fstream> 00038 00039 #include "common/wave_ex.h" 00040 #include "perf/perf.h" 00041 00042 00043 //////////////////////////////////////////////////////////////////////////////// 00044 // 00045 // static helper methods 00046 // 00047 //////////////////////////////////////////////////////////////////////////////// 00048 00049 00050 //////////////////////////////////////////////////////////////////////////////// 00051 // 00052 // entry point 00053 // 00054 //////////////////////////////////////////////////////////////////////////////// 00055 00056 int 00057 main 00058 ( 00059 IN int argc, 00060 IN const char * argv[] 00061 ) 00062 { 00063 ASSERT(2 == argc, "usage: base64EncodeFile <path>"); 00064 const char * filename = argv[1]; 00065 DPRINTF(" base-64 encoding file '%s' using crypto::encodeBase64()", 00066 filename); 00067 00068 const std::streampos s_maxData = 0x00FFFFFF;// this isn't for huge files 00069 int retval = 0; 00070 try { 00071 // open file 00072 std::ifstream infile(filename); 00073 ASSERT_THROW(infile.good(), 00074 "Failed to open file: " << filename); 00075 00076 // get file length 00077 infile.seekg(0, std::ios::end); 00078 std::streampos pos = infile.tellg(); 00079 infile.seekg(0, std::ios::beg); 00080 ASSERT_THROW(pos <= s_maxData, 00081 "Stream is too big: " << pos << " bytes"); 00082 int bytes = (int) pos; 00083 DPRINTF(" byte count: %d", bytes); 00084 ASSERT(bytes >= 0, "should be non-negative"); 00085 00086 // allocate space for raw data 00087 byte_t * data = new byte_t[bytes]; 00088 ASSERT(data, "out of memory"); 00089 00090 // okay, read the whole file 00091 infile.read((char *) data, bytes); 00092 00093 // encode 00094 std::string out = crypto::encodeBase64(data, bytes, true); 00095 00096 // write 00097 std::cout << out; 00098 00099 } catch (std::exception& e) { 00100 DPRINTF("Exception: %s", e.what()); 00101 retval = 1; 00102 } catch (...) { 00103 DPRINTF("Unknown exception!"); 00104 retval = 2; 00105 } 00106 perf::dumpTimingSummary(std::cerr); 00107 00108 return retval; 00109 } 00110