00001
00002
00003
00004
00005
00006
00007
00008 #include <b64/encode.h>
00009 #include <b64/decode.h>
00010
00011 #include <iostream>
00012 #include <fstream>
00013 #include <string>
00014
00015 #include <stdlib.h>
00016
00017
00018 void usage()
00019 {
00020 std::cerr<< \
00021 "base64: Encodes and Decodes files using base64\n" \
00022 "Usage: base64 [-e|-d] [input] [output]\n" \
00023 " Where [-e] will encode the input file into the output file,\n" \
00024 " [-d] will decode the input file into the output file, and\n" \
00025 " [input] and [output] are the input and output files, respectively.\n";
00026 }
00027
00028 void usage(const std::string& message)
00029 {
00030 usage();
00031 std::cerr<<"Incorrect invocation of base64:\n";
00032 std::cerr<<message<<std::endl;
00033 }
00034
00035 int main(int argc, char** argv)
00036 {
00037
00038 if (argc == 1)
00039 {
00040 usage();
00041 exit(-1);
00042 }
00043 if (argc != 4)
00044 {
00045 usage("Wrong number of arguments!");
00046 exit(-1);
00047 }
00048
00049
00050 std::string input = argv[2];
00051
00052
00053
00054
00055 std::ifstream instream(input.c_str(), std::ios_base::in | std::ios_base::binary);
00056 if (!instream.is_open())
00057 {
00058 usage("Could not open input file!");
00059 exit(-1);
00060 }
00061
00062
00063 std::string output = argv[3];
00064
00065
00066
00067 std::ofstream outstream(output.c_str(), std::ios_base::out | std::ios_base::binary);
00068 if (!outstream.is_open())
00069 {
00070 usage("Could not open output file!");
00071 exit(-1);
00072 }
00073
00074
00075 std::string choice = argv[1];
00076 if (choice == "-d")
00077 {
00078 base64::decoder D;
00079 D.decode(instream, outstream);
00080 }
00081 else if (choice == "-e")
00082 {
00083 base64::encoder E;
00084 E.encode(instream, outstream);
00085 }
00086 else
00087 {
00088 std::cout<<"["<<choice<<"]"<<std::endl;
00089 usage("Please specify -d or -e as first argument!");
00090 }
00091
00092 return 0;
00093 }
00094