00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
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
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
00157
00158
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
00182 if (n < 0 || n > 3) FATAL("this should not append check source code");
00183
00184 DUMP("pop byte",DBG_DETAIL);
00185
00186
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
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
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