Base64Transcoder.h 1.67 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
// Base64Transcoder.h
//
////////////////////////////////////////////////////////////////////////////////
Francois Gygi committed
18
// $Id: Base64Transcoder.h,v 1.7 2008-09-08 15:56:18 fgygi Exp $
Francois Gygi committed
19 20 21 22 23

#ifndef BASE64TRANSCODER_H
#define BASE64TRANSCODER_H

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

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

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

  public:
35

Francois Gygi committed
36 37 38 39 40
  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);
41 42
  int print(int nchars, const char* const buf, std::ostream& o);
  int print(const std::string buf, std::ostream& o);
43
  int print(int nchars, const char* const buf, FILE* outfile);
44
  int print(const std::string buf, FILE* outfile);
Francois Gygi committed
45 46 47 48 49 50 51 52

  // 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