#include <iostream>
#include <fstream>
using namespace std;

class tagTT
{
public:
  unsigned char buf;
  char *cont;
  int nlen;
};


unsigned char CtoNum(int c)
{
  if (c >= 'A' && c <= 'Z') return c-'A';
  if (c >= 'a' && c <= 'z') return (c - 'a')+26;
  if (c >= '0' && c <= '9') return (c - '0')+52;
  if (c == '+') return 62;
  if (c == '/') return 63;
  return 0;
}


int GetNumB64(tagTT &pWork)
{
  char wr;
  unsigned char w;
  unsigned char r;
  
  wr = *pWork.cont;
  pWork.cont++;
  if (wr=='\0' || wr=='=')
    return -1;
  
  while(wr=='\n' || wr=='\t')
    {
      wr = *pWork.cont;
      pWork.cont++;
    }

  w = CtoNum(wr);
  if (pWork.nlen == 0)
    {
      unsigned char w2;

      wr = *pWork.cont;
      pWork.cont++;
      if (wr!='\0' && wr!='=')
        w2 = CtoNum(wr);
      else
        w2 = 0;

      pWork.buf = w << 2;
      w = w2;
      pWork.nlen = 6;
    }

  pWork.nlen -= 2;     

  r = pWork.buf | (w >> pWork.nlen);

  pWork.buf = (w << (8 - pWork.nlen));
  return r;
}


//int decode(char *cont, char *file_name)
int decode(char *cont, FILE *outfile)
{
  int rw;
  tagTT work;

  //ofstream outfile(file_name);
  
  work.buf = 0;
  work.nlen = 0;
  work.cont = cont;
  
  rw = GetNumB64(work);
  while ( rw != -1 ) 
    {
      //outfile << (char)rw;
      putc(rw, outfile);
      rw = GetNumB64(work);
    }

  //outfile.close();

  return 0;
}
