MCS  0.3.3-alpha7
b64codec.cc
1 // ----------------------------------------------------------------------^
2 // Copyright (C) 2004, 2005, 2006, 2007, 2008 Giorgio Calderone
3 // (mailto: <gcalderone@ifc.inaf.it>)
4 //
5 // This file is part of MCS.
6 //
7 // MCS is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // MCS is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with MCS; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 //
21 // ----------------------------------------------------------------------$
22 #include <fcntl.h>
23 #include <unistd.h>
24 
25 #include <mcs.hh>
26 using namespace mcs;
27 
28 
29 void usage()
30 {
31  cout << "b64codec: A base 64 encoder/decoder based on MCS" << endl;
32  cout << "Giorgio Calderone <gcalderone@ifc.inaf.it>" << endl;
33  cout << endl;
34  cout << "Usage: b64codec [-e|-d] [FILEIN [FILEOUT]]" << endl;
35  cout << "Options:" << endl;
36  cout << " -e, --encode Encode data (default)" << endl;
37  cout << " -d, --decode Decode data" << endl;
38  cout << " -h, --help Shows this help" << endl;
39  cout << "" << endl;
40  cout << "If no file name is given stdin and stdout will be used." << endl;
41  cout << "If one file name is given it will be used for input and stdout for output." << endl;
42  cout << "If two file names are given they will be used respectively for input and output." << endl;
43  cout << "If FILEIN is - stdin will be used for input." << endl;
44 }
45 
46 
47 
48 #define BUFSIZE 2048
49 
50 int main(int argc, char* argv[])
51 {
52  unsigned int ret;
53  bool encode = true; //Default
54  string fin, fout;
55  int fdin, fdout;
56  char buf[BUFSIZE];
57 
58  //Parse arguments
59  for (ret=1; ret<(unsigned int) argc; ret++) {
60  if ((strcmp(argv[ret], "-e") == 0) ||
61  (strcmp(argv[ret], "--encode") == 0) )
62  encode = true;
63 
64  else if ((strcmp(argv[ret], "-d") == 0) ||
65  (strcmp(argv[ret], "--decode") == 0) )
66  encode = false;
67 
68  else if ((strcmp(argv[ret], "-h") == 0) ||
69  (strcmp(argv[ret], "--help") == 0) ) {
70  usage();
71  return 0;
72  }
73 
74  else {
75  if (fin.empty())
76  fin = argv[ret];
77 
78  else {
79  if (fout.empty())
80  fout = argv[ret];
81  else
82  break;
83  }
84  }
85  }
86 
87  if (fin == "-")
88  fin = "";
89 
90  if (fout == "-")
91  fout = "";
92 
93 
94  //Open input file, if needed
95  if (fin.empty())
96  fdin = 0;
97  else {
98  fdin = open(fin.c_str(), O_RDONLY);
99  if (fdin <=0 ) {
100  cerr << "Can't open file " << fin << " for input." << endl;
101  return 2;
102  }
103  }
104 
105 
106  //Open output file if needed
107  if (fout.empty())
108  fdout = 1;
109  else {
110  fdout = open(fout.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0);
111  if (fdout <=0 ) {
112  cerr << "Can't open file " << fout << " for output." << endl;
113  cerr << strerror(errno) << endl;
114  return 3;
115  }
116  fchmod(fdout, S_IREAD | S_IWRITE | S_IRGRP | S_IROTH);
117  }
118 
119 
120 
121  //Do the job
122  B64_Codec b64;
123  while ((ret = read(fdin, buf, BUFSIZE)) > 0) {
124  if (encode)
125  ret = b64.encode(buf, ret);
126  else
127  ret = b64.decode(buf, ret);
128 
129  ret = write(fdout, b64.buffer(), b64.bufUsed());
130 
131  if (ret != b64.bufUsed()) {
132  cerr << "Can't write on output file." << endl;
133  return 4;
134  }
135  }
136  if (encode)
137  ret = b64.encode(buf, 0);
138  else
139  ret = b64.decode(buf, 0);
140  ret = write(fdout, b64.buffer(), b64.bufUsed());
141 
142 
143  //Cleanup
144  if (! fin.empty())
145  close(fdin);
146 
147  if (! fout.empty())
148  close(fdout);
149 
150  return 0;
151 }
unsigned int decode(char *buf_in, int Length, char *par_buf_out=NULL)
Decode a block of data.
Definition: Utils.cc:609
unsigned int encode(char *buf_in, int Length, char *par_buf_out=NULL, unsigned int linesize=72)
Encode a block of data.
Definition: Utils.cc:523
A Base 64 encoder/decoder.
Definition: mcs.hh:7975
Main include file for all MCS based applications.
Namespace for MCS library.

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha7
Documentation generated on Mon May 28 07:39:41 UTC 2018