proj home

Files   Classes   Functions   Hierarchy  

streamconversion.cpp

Go to the documentation of this file.
00001 #include <cassert>
00002 #include <iostream>
00003 #include <vector>
00004 #include <string>
00005 #include <sstream>
00006 
00007 using namespace std;
00008 
00009 
00010 #include <streamconversion.h>
00011 
00012 
00013 void asciitodig::forward( string & os, istream & is ) 
00014 {
00015   char ch;
00016   uint w;
00017   is.unsetf(ios::skipws);
00018   while (is.eof()==false)
00019   {
00020     is >> ch;
00021     if (is.eof())
00022       break;
00023     w = (uint)ch;
00024   
00025     stringstream ss;
00026     if (w==0)
00027       ss << "000";
00028     else
00029     if (w<10)
00030       ss << "00" << w;
00031     else
00032     if (w<100)
00033       ss << "0" << w;
00034     else
00035       ss << w;
00036 
00037     os += ss.str();
00038   }
00039 }
00040 
00041 void asciitodig::reverse
00042 (
00043   ostream & os,
00044   string const & in
00045 ) 
00046 {
00047   assert((in.size()%3)==0);
00048   uintc n = in.size()/3;
00049 
00050   string s;
00051   char c;
00052   uint x;
00053   for (uint k=0; k<n; ++k) 
00054   {
00055     s = in.substr(3*k,3);
00056     stringstream ss(s.c_str());
00057     ss >> x;
00058     c = (char)x;
00059     os << c;
00060   }
00061 }
00062 
00063 
00064   
00065 
00066 
00067 // digdiv::pad  Note:
00068 //   This is written out and is public when the object is saved.
00069 //   If this class is used for cryptography then this is only
00070 //   ok when the algorithm is safe against the enemy having both
00071 //   plain text and cipher text. eg RSA is ok.
00072 
00073 
00074 void digdiv::forward(string const & in )
00075 {
00076   assert(blksz>0);
00077 
00078   uintc sz = in.size();
00079   uint i=0;
00080   vi.clear();
00081   i += blksz;
00082   for ( ; i<=sz; )
00083   {
00084     vi.push_back(in.substr(i-blksz,blksz));
00085     i += blksz;
00086   }  
00087 
00088   if ((sz%blksz)!=0)
00089   {
00090     pad = blksz - (sz%blksz);
00091     string s;
00092     for (uint k=0; k<pad; ++k)
00093       s += '0';
00094     s += in.substr(i-blksz);
00095     vi.push_back(s);
00096   }
00097   else
00098     pad=0;
00099 }
00100     
00101 void digdiv::reverse( string & os ) 
00102 {
00103   if (vi.empty())
00104     return;
00105 
00106   os.clear();
00107   
00108   uintc sz = vi.size();
00109 
00110   // Correct the block sizes
00111   for (uint i=0; i<sz; ++i)
00112   {
00113     uintc si = vi[i].size();
00114     if (si != blksz)
00115     {
00116       if (si<blksz)
00117       {
00118         string tmp;
00119         for (uint k=si; k<blksz; ++k)
00120           tmp += '0';
00121         tmp += vi[i];
00122         vi[i] = tmp;
00123       }
00124       else
00125       {
00126         // A function should reduce the block size.
00127         assert(false);
00128       }
00129     }
00130   }
00131       
00132   if (pad==0)
00133   {
00134     for (uint i=0; i<sz; ++i)
00135       os += vi[i];
00136     return;
00137   }
00138 
00139   string last = vi[sz-1];
00140   last = last.substr(pad);
00141   for (uint i=0; i<sz-1; ++i)
00142     os += vi[i];
00143   os += last;
00144 }
00145   
00146 void digdiv::save(ostream & os) const
00147 {
00148   os << pad << endl;
00149   os << blksz << endl;
00150   for (uint i=0; i<vi.size(); ++i)
00151     os << vi[i] << endl;
00152 }
00153 
00154 void digdiv::restore(istream & in)
00155 {
00156   if (in.eof()==false)
00157     in >> pad;
00158   else
00159     return;
00160 
00161   if (in.eof()==false)
00162     in >> blksz;
00163   else
00164     return;
00165 
00166   string s;
00167   in >> s;
00168   while (in.eof()==false)
00169   {
00170     vi.push_back(s);
00171     s.clear();
00172     in >> s;
00173   }
00174 }
00175   
00176 
00177   
00178 
00179 
00180 
00181 

Generated on Fri Mar 4 00:49:31 2011 for Chelton Evans Source by  doxygen 1.5.8