Base64Transcoder.h 1.61 KB
Newer Older
Francois Gygi committed
1 2
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
3 4 5 6
// Copyright (c) 2008 The Regents of the University of California
//
// This file is part of Qbox
//
Francois Gygi committed
7 8
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
Francois Gygi committed
9 10 11 12 13 14
// the License, or (at your option) any later version.
// See the file COPYING in the root directory of this distribution
// or <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
15 16 17 18 19 20 21 22
// Base64Transcoder.h
//
////////////////////////////////////////////////////////////////////////////////

#ifndef BASE64TRANSCODER_H
#define BASE64TRANSCODER_H

#include <iostream>
23
#include <cstdio>
Francois Gygi committed
24
#include <string>
25

Francois Gygi committed
26 27 28 29
typedef unsigned char byte;

class Base64Transcoder
{
Francois Gygi committed
30
  char etable[64];  // encode table
Francois Gygi committed
31 32 33
  byte dtable[256]; // decode table

  public:
34

Francois Gygi committed
35 36 37 38 39
  Base64Transcoder();
  int encode(int nbytes, const byte* const from, char* const to);
  int decode(int nchars, const char* const from, byte* const to);
  void byteswap_double(size_t n, double* const x);
  void byteswap_int(size_t n, int* const x);
40 41
  int print(int nchars, const char* const buf, std::ostream& o);
  int print(const std::string buf, std::ostream& o);
42
  int print(int nchars, const char* const buf, FILE* outfile);
43
  int print(const std::string buf, FILE* outfile);
Francois Gygi committed
44 45 46 47 48 49 50 51

  // number of chars needed to encode nbytes bytes
  int nchars(int nbytes) { return 4 * ( ( nbytes + 2 ) / 3 ); }
  // number of bytes needed to decode nchars chars
  int nbytes(int nchars) { return 3 * ( nchars / 4 ); }
};

#endif