base64_reader.h

Go to the documentation of this file.
00001 /* ./dumper/base64_reader.h 
00002 **********************************
00003 Copyright INRIA and CEA 
00004 
00005 author : Guillaume ANCIAUX (anciaux@labri.fr, g.anciaux@laposte.net)
00006 
00007 The LibMultiScale is a C++ parallel framework for the multiscale
00008 coupling methods dedicated to material simulations. This framework
00009 provides an API which makes it possible to program coupled simulations
00010 and integration of already existing codes.
00011 
00012 This Project is done in a collaboration between INRIA Futurs Bordeaux
00013 within ScAlApplix team and CEA/DPTA Ile de France. 
00014 
00015 This software is governed by the CeCILL-C license under French law and
00016 abiding by the rules of distribution of free software.  You can  use, 
00017 modify and/ or redistribute the software under the terms of the CeCILL-C
00018 license as circulated by CEA, CNRS and INRIA at the following URL
00019 "http://www.cecill.info". 
00020 
00021 As a counterpart to the access to the source code and  rights to copy,
00022 modify and redistribute granted by the license, users are provided only
00023 with a limited warranty  and the software's author,  the holder of the
00024 economic rights,  and the successive licensors  have only  limited
00025 liability. 
00026 
00027 In this respect, the user's attention is drawn to the risks associated
00028 with loading,  using,  modifying and/or developing or reproducing the
00029 software by the user in light of its specific status of free software,
00030 that may mean  that it is complicated to manipulate,  and  that  also
00031 therefore means  that it is reserved for developers  and  experienced
00032 professionals having in-depth computer knowledge. Users are therefore
00033 encouraged to load and test the software's suitability as regards their
00034 requirements in conditions enabling the security of their systems and/or 
00035 data to be ensured and,  more generally, to use and operate it in the 
00036 same conditions as regards security. 
00037 
00038 The fact that you are presently reading this means that you have had
00039 knowledge of the CeCILL-C license and that you accept its terms.
00040 ***********************************/
00041 
00042 #ifndef BASE64_READER_H
00043 #define BASE64_READER_H
00044 
00051 class Base64Reader{
00052 
00053  public:
00054   
00055   Base64Reader(){
00056     InitBase64Stuff();
00057     n = 0;
00058     n_coded = 0;
00059     read_bytes_in_double=0;
00060   };
00061   
00063   int PopDoubleInBase64(double & c);
00065   int PopIntegerInBase64(int & c);
00067   int PopByteInBase64(unsigned char & c);
00069   int Decode(unsigned char c0,unsigned char c1,unsigned char c2,unsigned char c3,
00070               unsigned char * r0,unsigned char * r1,unsigned char * r2);
00071 
00073   void SetStream(char * str,int l);
00074 
00075  private:
00076 
00078   void InitBase64Stuff();
00080   char dtable[256];
00082   char etable[256];
00084   int n;
00086   int n_coded;
00088   unsigned char igroup[3],ogroup[4];
00089 
00090 
00092   char * start_ptr;
00094   char * current_ptr;
00096   int len;
00098   unsigned char read_byte[3];
00100   char read_coded_byte[4];
00101     
00102 
00104   double temp_double;
00106   int read_bytes_in_double;
00107 };
00108 
00109 
00110 
00111 inline void Base64Reader::SetStream(char * str,int l){
00112   current_ptr = str;
00113   start_ptr = str;
00114   len = l;
00115 
00116   DUMP("n_coded " << n_coded,DBG_DETAIL);
00117   if (n_coded > 0){
00118     //je relie l'ancien stream et le nouveau
00119     
00120     if (n_coded == 1 && current_ptr < start_ptr+len) {
00121       read_coded_byte[1] = *current_ptr;
00122       ++n_coded;
00123       ++current_ptr;
00124     }
00125     DUMP("n_coded " << n_coded,DBG_DETAIL);
00126     if (n_coded == 2 && current_ptr < start_ptr+len) {
00127       read_coded_byte[2] = *current_ptr;
00128       ++n_coded;
00129       ++current_ptr;
00130     }
00131     DUMP("n_coded " << n_coded,DBG_DETAIL);
00132     if (n_coded == 3 && current_ptr < start_ptr+len) {
00133       read_coded_byte[3] = *current_ptr;
00134       ++n_coded;
00135       ++current_ptr;
00136     }
00137     DUMP("n_coded " << n_coded,DBG_DETAIL);
00138   }
00139   
00140 
00141 };
00142 
00143 
00144 inline int Base64Reader::PopIntegerInBase64(int & d){
00145   int read = 1;
00146 
00147   DUMP("pushing " << d << " ( n = " << n << " )",DBG_DETAIL);
00148   unsigned char * c = (unsigned char*)&d;
00149   for (unsigned int i = 0 ; i < sizeof(int) ; ++i){
00150     read &= PopByteInBase64(c[i]);
00151     if (!read) break;
00152   }
00153   return read;
00154 }
00155 
00156 /* inline void Base64Reader::PopStrInBase64(char * str){ */
00157 
00158 /*   FATAL("unimplemented for the moment"); */
00159 /* } */
00160 
00161 inline int Base64Reader::PopDoubleInBase64(double & d){
00162   int read = 1;
00163   DUMP("pop double , already red  = " << read_bytes_in_double,DBG_DETAIL);
00164 
00165   unsigned char * c = (unsigned char*)&temp_double;
00166   for (unsigned int i = read_bytes_in_double ; i < sizeof(double) ; ++i){
00167     read &= PopByteInBase64(c[i]);
00168     if (read) ++read_bytes_in_double;
00169     if (!read) break;
00170   }
00171 
00172   if (read) {
00173     read_bytes_in_double = 0;
00174     d = temp_double;
00175     DUMP("readed double " << d,DBG_DETAIL);
00176   }
00177   return read;
00178 }
00179 
00180 inline int Base64Reader::PopByteInBase64(unsigned char & c){
00181   //initialise les blocs
00182   if (n < 0 || n > 3) FATAL("this should not append check source code");
00183 
00184   DUMP("pop byte",DBG_DETAIL);
00185     
00186   // si je suis a n = 3 je dois relire 4 octets de base64
00187   if (n == 0){
00188 
00189     DUMP("n_coded " << n_coded,DBG_DETAIL);
00190     if (n_coded == 0){
00191       if (current_ptr < start_ptr+len) {read_coded_byte[0] = current_ptr[0];++n_coded;DUMP("n_coded " << n_coded,DBG_DETAIL);}
00192       else {DUMP("lala" << n_coded,DBG_DETAIL);return 0;}
00193       if (current_ptr+1 < start_ptr+len) {read_coded_byte[1] = current_ptr[1];++n_coded;DUMP("n_coded " << n_coded,DBG_DETAIL);}
00194       else {DUMP("lala" << n_coded,DBG_DETAIL);return 0;}
00195       if (current_ptr+2 < start_ptr+len) {read_coded_byte[2] = current_ptr[2];++n_coded;DUMP("n_coded " << n_coded,DBG_DETAIL);}
00196       else {DUMP("lala" << n_coded,DBG_DETAIL);return 0;}
00197       if (current_ptr+3 < start_ptr+len) {read_coded_byte[3] = current_ptr[3];++n_coded;DUMP("n_coded " << n_coded,DBG_DETAIL);}
00198       else {DUMP("lala" << n_coded,DBG_DETAIL);return 0;}
00199       current_ptr+=4;
00200     }
00201   }
00202   
00203   if (n_coded == 4)
00204     Decode(read_coded_byte[0],read_coded_byte[1],read_coded_byte[2],read_coded_byte[3],
00205            &read_byte[0],&read_byte[1],&read_byte[2]);
00206   
00207   c = read_byte[n];
00208 
00209   DUMP("readed charater "<< (int)c,DBG_DETAIL);
00210   ++n;
00211 
00212   if (n == 3) {
00213     n = 0;
00214     n_coded = 0;
00215   }
00216   return 1;
00217 
00218 }
00219 
00220 
00221 inline int Base64Reader::Decode(unsigned char c0,unsigned char c1,unsigned char c2,unsigned char c3,
00222                                  unsigned char * r0,unsigned char * r1,unsigned char * r2){
00223 
00224   unsigned char d0, d1, d2, d3;
00225 
00226   d0 = dtable[0+c0];
00227   d1 = dtable[0+c1];
00228   d2 = dtable[0+c2];
00229   d3 = dtable[0+c3];
00230 
00231   
00232 
00233   DUMP("d0 " << (int)d0 << " d1 " << (int)d1 << " d2 " << (int)d2 << " d3 " << (int)d3,DBG_DETAIL);
00234   
00235   // Decode the 3 bytes
00236 
00237   *r0 = ((d0 << 2) & 0xFC) | ((d1 >> 4) & 0x03);
00238   *r1 = ((d1 << 4) & 0xF0) | ((d2 >> 2) & 0x0F);
00239   *r2 = ((d2 << 6) & 0xC0) | ((d3 >> 0) & 0x3F);
00240 
00241   DUMP("r0 " << (int)*r0 << " r1 " << (int)*r1 << " r2 " << (int)*r2,DBG_DETAIL);
00242   
00243   // Return the number of bytes actually decoded
00244 
00245   if (c2 == '=') 
00246     { 
00247     return 1; 
00248     }
00249   if (c3 == '=') 
00250     { 
00251     return 2; 
00252     }
00253   return 3;
00254 }
00255 
00256 
00257 
00258 inline void Base64Reader::InitBase64Stuff(){
00259   memset(dtable,0xFF,256);
00260   memset(etable,0xFF,256);
00261 
00262   for(int i=0;i<9;i++){
00263     etable[i]= 'A'+i;
00264     dtable[0+etable[i]] = i;
00265     etable[i+9]= 'J'+i;
00266     dtable[0+etable[i+9]] = i+9;
00267     etable[26+i]= 'a'+i;
00268     dtable[0+etable[26+i]] = i + 26;
00269     etable[26+i+9]= 'j'+i;
00270     dtable[0+etable[26+i+9]] = i + 26 + 9;
00271   }
00272   for(int i= 0;i<8;i++){
00273     etable[i+18]= 'S'+i;
00274     dtable[0+etable[i+18]] = i + 18;
00275     etable[26+i+18]= 's'+i;
00276     dtable[0+etable[26+i+18]] = 26 + i + 18;
00277   }
00278   for(int i= 0;i<10;i++){
00279     etable[52+i]= '0'+i;
00280     dtable[0+etable[i+52]] = i + 52;
00281   }
00282   etable[62]= '+';
00283   dtable[0+etable[62]] = 62;
00284   etable[63]= '/';
00285   dtable[0+etable[63]] = 63;
00286 }
00287 
00288 #endif //BASE64_WRITER_H
00289 

Generated on Fri Sep 7 13:12:34 2007 for LibMultiScale by  doxygen 1.5.2