$treeview $search $mathjax
Palabos  Version 1.1
$projectbrief
$projectbrief
$searchbox

base64.h

Go to the documentation of this file.
00001 /* This file is part of the Palabos library.
00002  *
00003  * Copyright (C) 2011 FlowKit Sarl
00004  * Avenue de Chailly 23
00005  * 1012 Lausanne, Switzerland
00006  * E-mail contact: contact@flowkit.com
00007  *
00008  * The most recent release of Palabos can be downloaded at 
00009  * <http://www.palabos.org/>
00010  *
00011  * The library Palabos is free software: you can redistribute it and/or
00012  * modify it under the terms of the GNU Affero General Public License as
00013  * published by the Free Software Foundation, either version 3 of the
00014  * License, or (at your option) any later version.
00015  *
00016  * The library is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Affero General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Affero General Public License
00022  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00023 */
00024 
00025 /* Acknowledgment: The strategy adopted here to encode
00026  * and decode Base64, and in particular the expression of the
00027  * arrays Base64Encoder::enc64 and Base64Decoder::dec64, 
00028  * are inspired by the open source library b64 by Bob Trower,
00029  * which is distributed under an MIT license at the address
00030  * http://base64.sourceforge.net/b64.c
00031  */
00032 
00033 
00034 #ifndef BASE64_H
00035 #define BASE64_H
00036 
00037 #include "core/globalDefs.h"
00038 #include <iosfwd>
00039 
00040 namespace plb {
00041 
00042 template<typename T>
00043 class Base64Encoder {
00044 public:
00045     Base64Encoder(std::ostream& ostr_, pluint fullLength_);
00046     void encode(const T* data, pluint length);
00047 private:
00048     void fillOverflow(const unsigned char* charData, pluint charLength, pluint& pos);
00049     void flushOverflow();
00050     void writeSize();
00051     void encodeBlock( const unsigned char* data);
00052     void encodeUnfinishedBlock( const unsigned char* data, plint length);
00053 private:
00054     static const char enc64[65];
00055 private:
00056     std::ostream& ostr;
00057     pluint charFullLength;
00058     pluint numWritten;
00059     plint numOverflow;
00060     unsigned char overflow[3];
00061 };
00062 
00063 template<typename T>
00064 class Base64Decoder {
00065 public:
00066     Base64Decoder(std::istream& istr_, pluint fullLength_);
00067     void decode(T* data, pluint length);
00068 private:
00069     void flushOverflow(unsigned char* charData, pluint charLength, pluint& pos);
00070     unsigned char getNext();
00071     void decodeBlock(unsigned char* data);
00072 private:
00073     static const char dec64[82];
00074 private:
00075     std::istream& istr;
00076     pluint charFullLength;
00077     pluint numRead;
00078     plint posOverflow;
00079     unsigned char overflow[3];
00080 };
00081 
00082 } // namespace plb
00083 
00084 #endif  // BASE64_H