Base64Transcoder.C 7.28 KB
Newer Older
Francois Gygi committed
1 2 3 4 5 6 7 8 9
////////////////////////////////////////////////////////////////////////////////
//
// Base64Transcoder.C
//
////////////////////////////////////////////////////////////////////////////////
// $ Id: $

#include "Base64Transcoder.h"
#include <iostream>
10
#include <cstdio>
Francois Gygi committed
11 12 13 14 15 16 17 18 19
#include <string>
#include <cassert>
using namespace std;

////////////////////////////////////////////////////////////////////////////////
Base64Transcoder::Base64Transcoder()
{
  // initialize encoding/decoding tables
  
20 21
  for (int i = 0; i < 26; i++)
  {
Francois Gygi committed
22 23 24
      etable[i] = 'A' + i;
      etable[26 + i] = 'a' + i;
  }
25 26
  for (int i = 0; i < 10; i++)
  {
Francois Gygi committed
27 28 29 30 31
      etable[52 + i] = '0' + i;
  }
  etable[62] = '+';
  etable[63] = '/';

32 33
  for (int i = 0; i < 255; i++)
  {
Francois Gygi committed
34 35
      dtable[i] = 0x80;
  }
36 37
  for (int i = 'A'; i <= 'Z'; i++)
  {
Francois Gygi committed
38 39
      dtable[i] = 0 + (i - 'A');
  }
40 41
  for (int i = 'a'; i <= 'z'; i++)
  {
Francois Gygi committed
42 43
      dtable[i] = 26 + (i - 'a');
  }
44 45
  for (int i = '0'; i <= '9'; i++)
  {
Francois Gygi committed
46 47 48 49 50 51 52 53 54
      dtable[i] = 52 + (i - '0');
  }
  dtable['+'] = 62;
  dtable['/'] = 63;
  dtable['='] = 0;
}

////////////////////////////////////////////////////////////////////////////////
int Base64Transcoder::encode(int nbytes, const byte* const from, char* const to)
55
{
Francois Gygi committed
56 57 58 59
  const byte* fptr = from;
  char* tptr = to;
  
  int n3 = nbytes / 3; // number of groups of three bytes
60
  
Francois Gygi committed
61 62 63 64 65
  while ( n3-- > 0 )
  {
    byte ig0 = *fptr++;
    byte ig1 = *fptr++;
    byte ig2 = *fptr++;
66

Francois Gygi committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    *tptr++ = etable[ig0 >> 2];
    *tptr++ = etable[((ig0 & 3) << 4) | (ig1 >> 4)];
    *tptr++ = etable[((ig1 & 0xF) << 2) | (ig2 >> 6)];
    *tptr++ = etable[ig2 & 0x3F];    
  }
  
  int nr = nbytes % 3; // remaining bytes
  
  if ( nr == 2 )
  {
    byte ig0 = *fptr++;
    byte ig1 = *fptr++;
    byte ig2 = 0;
    
    *tptr++ = etable[ig0 >> 2];
    *tptr++ = etable[((ig0 & 3) << 4) | (ig1 >> 4)];
    *tptr++ = etable[((ig1 & 0xF) << 2) | (ig2 >> 6)];
    *tptr++ = '=';
  }
  else if ( nr == 1 )
  {
    byte ig0 = *fptr++;
    byte ig1 = 0;
    
    *tptr++ = etable[ig0 >> 2];
    *tptr++ = etable[((ig0 & 3) << 4) | (ig1 >> 4)];
    *tptr++ = '=';
    *tptr++ = '=';
  }
  
  return 0;  
}

////////////////////////////////////////////////////////////////////////////////
int Base64Transcoder::decode(const int nchars, const char* const from, 
  byte* const to)  
{
  // Decode Base64 chars in array "from" into bytes in array "to"
  // White space and new lines are skipped
  // extra characters at end that do not form a valid group of 4 chars are
  // ignored.
  // nchars: number of chars in array "from"
  // the number of bytes successfully translated is returned
  
  byte a0,a1,a2,a3,b0,b1,b2,b3;
  int c;
  const char* fptr = from;
  const char* const fptr_end = from+nchars+1;
  byte* tptr = to;
  
  while ( fptr < fptr_end-4 )
  {
    // get 4 valid characters from input string
    do
    {
      c = *fptr++;
    }
    while ( (c <= ' ') && (fptr < fptr_end) );
    if ( fptr >= fptr_end )
    {
#ifdef DEBUG
      cerr << " Base64Transcoder::decode: end of string reached reading c0 "
           << endl;
#endif
      break;
    }
    a0 = (byte) c;
    b0 = (byte) dtable[c];
 
    do
    {
      c = *fptr++;
    }
    while ( (c <= ' ') && (fptr < fptr_end) );
    if ( fptr >= fptr_end )
    {
#ifdef DEBUG
      cerr << " Base64Transcoder::decode: end of string reached reading c1 "
           << endl;
#endif
      break;
    }
    a1 = (byte) c;
    b1 = (byte) dtable[c];
 
    do
    {
      c = *fptr++;
    }
    while ( (c <= ' ') && (fptr < fptr_end) );
    if ( fptr >= fptr_end )
    {
#ifdef DEBUG
      cerr << " Base64Transcoder::decode: end of string reached reading c2 "
           << endl;
#endif
      break;
    }
    a2 = (byte) c;
    b2 = (byte) dtable[c];
 
    do
    {
      c = *fptr++;
    }
    while ( (c <= ' ') && (fptr < fptr_end) );
    if ( (c <= ' ') && fptr >= fptr_end )
    {
#ifdef DEBUG
      cerr << " Base64Transcoder::decode: end of string reached reading c3\n"
           << " (without reading a valid c3) " << endl;
#endif
      break;
    }
    a3 = (byte) c;
    b3 = (byte) dtable[c];
 
    if ((b0|b1|b2|b3) & 0x80)
    {
#ifdef DEBUG
      cerr << " Base64Transcoder::decode: Illegal character in input: "
           << endl;
#endif
      return tptr - to;
    }
      
    if ( a3 == '=' )
    {
      if ( a2 == '=' )
      {
        // write 1 byte only
        *tptr++ = (b0 << 2) | (b1 >> 4);
      }
      else
      {
        // write 2 bytes only
        *tptr++ = (b0 << 2) | (b1 >> 4);
        *tptr++ = (b1 << 4) | (b2 >> 2);
      }
    }
    else
    {
      // write 3 bytes
      *tptr++ = (b0 << 2) | (b1 >> 4);
      *tptr++ = (b1 << 4) | (b2 >> 2);
      *tptr++ = (b2 << 6) | b3;
    }
  
  }
#ifdef DEBUG
  if ( fptr >= fptr_end )
  {
    cerr << " Base64Transcoder::decode: end of string reached in input: "
         << endl;
  }
#endif
  
  return tptr - to;
}

////////////////////////////////////////////////////////////////////////////////
void Base64Transcoder::byteswap_double(size_t n, double* const x)
{
  if (n==0) return;
  unsigned char* c = (unsigned char*) x;
  while ( n-- > 0 )
  {
    unsigned char tmp;
    tmp = c[7]; c[7] = c[0]; c[0] = tmp;
    tmp = c[6]; c[6] = c[1]; c[1] = tmp;
    tmp = c[5]; c[5] = c[2]; c[2] = tmp;
    tmp = c[4]; c[4] = c[3]; c[3] = tmp;
    
    c+=8;
  }
}  

////////////////////////////////////////////////////////////////////////////////
void Base64Transcoder::byteswap_int(size_t n, int* const x)
{
  if (n==0) return;
  unsigned char* c = (unsigned char*) x;
  while ( n-- > 0 )
  {
    unsigned char tmp;
    tmp = c[3]; c[3] = c[0]; c[0] = tmp;
    tmp = c[2]; c[2] = c[1]; c[1] = tmp;
    
    c+=4;
  }
} 

////////////////////////////////////////////////////////////////////////////////
int Base64Transcoder::print(const string buf, ostream& o)
{
  const char* b = buf.c_str();
  int nchars = buf.size();
  int nl = nchars / 72;
  for ( int i = 0; i < nl; i++ )
  {
    o.write(b,72);
    o << endl;
    b += 72;
  }
  
  o.write(b,nchars%72);
  o << endl;
  return 0;
}

////////////////////////////////////////////////////////////////////////////////
int Base64Transcoder::print(int nchars, const char* const buf, ostream& o)
{
  const char* b = buf;
  int nl = nchars / 72;
  for ( int i = 0; i < nl; i++ )
  {
    o.write(b,72);
    o << endl;
    b += 72;
  }
  
  o.write(b,nchars%72);
  o << endl;
  return 0;
}
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
////////////////////////////////////////////////////////////////////////////////
int Base64Transcoder::print(const string buf, FILE* outfile)
{
  const char* b = buf.c_str();
  const char nlchar = '\n';
  int nchars = buf.size();
  int nl = nchars / 72;
  for ( int i = 0; i < nl; i++ )
  {
    fwrite(b,sizeof(char),72,outfile);
    fwrite(&nlchar,sizeof(char),1,outfile);
    b += 72;
  }
  
  if ( nchars%72 != 0 )
  {
    fwrite(b,sizeof(char),nchars%72,outfile);
    fwrite(&nlchar,sizeof(char),1,outfile);
  }
  return 0;
}

////////////////////////////////////////////////////////////////////////////////
int Base64Transcoder::print(int nchars, const char* const buf, FILE* outfile)
{
  const char* b = buf;
  const char nlchar = '\n';
  int nl = nchars / 72;
  for ( int i = 0; i < nl; i++ )
  {
    fwrite(b,sizeof(char),72,outfile);
    fwrite(&nlchar,sizeof(char),1,outfile);
    b += 72;
  }
  
  if ( nchars%72 != 0 )
  {
    fwrite(b,sizeof(char),nchars%72,outfile);
    fwrite(&nlchar,sizeof(char),1,outfile);
  }
  return 0;
}