b64codec.cc

00001 // ----------------------------------------------------------------------^
00002 // Copyright (C) 2004, 2005, 2006, 2007, 2008 Giorgio Calderone
00003 // (mailto: <gcalderone@ifc.inaf.it>)
00004 // 
00005 // This file is part of MCS.
00006 // 
00007 // MCS is free software; you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation; either version 2 of the License, or
00010 // (at your option) any later version.
00011 // 
00012 // MCS is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 // 
00017 // You should have received a copy of the GNU General Public License
00018 // along with MCS; if not, write to the Free Software
00019 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 // 
00021 // ----------------------------------------------------------------------$
00022 #include <fcntl.h>
00023 #include <unistd.h>
00024 
00025 #include <mcs.hh>
00026 using namespace mcs;
00027 
00028 
00029 void usage()
00030 {
00031   cout << "b64codec: A base 64 encoder/decoder based on MCS" << endl;
00032   cout << "Giorgio Calderone <gcalderone@ifc.inaf.it>" << endl;
00033   cout << endl;
00034   cout << "Usage: b64codec [-e|-d] [FILEIN [FILEOUT]]" << endl;
00035   cout << "Options:" << endl;
00036   cout << "    -e, --encode  Encode data (default)" << endl;
00037   cout << "    -d, --decode  Decode data" << endl;
00038   cout << "    -h, --help    Shows this help" << endl;
00039   cout << "" << endl;
00040   cout << "If no file name is given stdin and stdout will be used." << endl;
00041   cout << "If one file name is given it will be used for input and stdout for output." << endl;
00042   cout << "If two file names are given they will be used respectively for input and output." << endl;
00043   cout << "If FILEIN is - stdin will be used for input." << endl;
00044 }
00045 
00046 
00047 
00048 #define BUFSIZE 2048
00049 
00050 int main(int argc, char* argv[])
00051 {
00052   unsigned int ret;
00053   bool encode = true;  //Default
00054   string fin, fout;
00055   int fdin, fdout;
00056   char buf[BUFSIZE];
00057 
00058   //Parse arguments
00059   for (ret=1; ret<(unsigned int) argc; ret++) {
00060     if ((strcmp(argv[ret], "-e") == 0)    ||
00061     (strcmp(argv[ret], "--encode") == 0)       )
00062       encode = true;
00063 
00064     else if ((strcmp(argv[ret], "-d") == 0)   ||
00065          (strcmp(argv[ret], "--decode") == 0)   )
00066       encode = false;
00067 
00068     else if ((strcmp(argv[ret], "-h") == 0)   ||
00069          (strcmp(argv[ret], "--help") == 0)       ) {
00070       usage();
00071       return 0;
00072     }
00073 
00074     else {
00075       if (fin.empty())
00076     fin = argv[ret];
00077 
00078       else {
00079     if (fout.empty())
00080       fout = argv[ret];
00081     else
00082       break;
00083       }
00084     }
00085   }
00086 
00087   if (fin == "-")
00088     fin = "";
00089 
00090   if (fout == "-")
00091     fout = "";
00092 
00093 
00094   //Open input file, if needed
00095   if (fin.empty())
00096     fdin = 0;
00097   else {
00098     fdin = open(fin.c_str(), O_RDONLY);
00099     if (fdin <=0 ) {
00100       cerr << "Can't open file " << fin << " for input." << endl;
00101       return 2;
00102     }
00103   }
00104 
00105 
00106   //Open output file if needed
00107   if (fout.empty())
00108     fdout = 1;
00109   else {
00110     fdout = open(fout.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0);
00111     if (fdout <=0 ) {
00112       cerr << "Can't open file " << fout << " for output." << endl;
00113       cerr << strerror(errno) << endl;
00114       return 3;
00115     }
00116     fchmod(fdout, S_IREAD | S_IWRITE | S_IRGRP | S_IROTH);
00117   }
00118 
00119 
00120 
00121   //Do the job
00122   B64_Codec b64;
00123   while ((ret = read(fdin, buf, BUFSIZE)) > 0) {
00124     if (encode)
00125       ret = b64.encode(buf, ret);
00126     else
00127       ret = b64.decode(buf, ret);
00128 
00129     ret = write(fdout, b64.buffer(), b64.bufUsed());
00130 
00131     if (ret != b64.bufUsed()) {
00132       cerr << "Can't write on output file." << endl;
00133       return 4;
00134     }
00135   }
00136   if (encode)
00137     ret = b64.encode(buf, 0);
00138   else
00139     ret = b64.decode(buf, 0);
00140   ret = write(fdout, b64.buffer(), b64.bufUsed());
00141 
00142 
00143   //Cleanup
00144   if (! fin.empty())
00145     close(fdin);
00146 
00147   if (! fout.empty())
00148     close(fdout);
00149 
00150   return 0;
00151 }

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha3
Documentation generated on Thu Mar 22 13:22:23 UTC 2012