Wolframe, 0.0.3

base64.hpp
Go to the documentation of this file.
1 /************************************************************************
2 
3  Copyright (C) 2011 - 2014 Project Wolframe.
4  All rights reserved.
5 
6  This file is part of Project Wolframe.
7 
8  Commercial Usage
9  Licensees holding valid Project Wolframe Commercial licenses may
10  use this file in accordance with the Project Wolframe
11  Commercial License Agreement provided with the Software or,
12  alternatively, in accordance with the terms contained
13  in a written agreement between the licensee and Project Wolframe.
14 
15  GNU General Public License Usage
16  Alternatively, you can redistribute this file and/or modify it
17  under the terms of the GNU General Public License as published by
18  the Free Software Foundation, either version 3 of the License, or
19  (at your option) any later version.
20 
21  Wolframe is distributed in the hope that it will be useful,
22  but WITHOUT ANY WARRANTY; without even the implied warranty of
23  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  GNU General Public License for more details.
25 
26  You should have received a copy of the GNU General Public License
27  along with Wolframe. If not, see <http://www.gnu.org/licenses/>.
28 
29  If you have questions regarding the use of this file, please contact
30  Project Wolframe.
31 
32 ************************************************************************/
33 //
34 // base64.hpp - c++ wrapper for base64 functions
35 //
36 
37 #ifndef _BASE64_HPP_INCLUDED
38 #define _BASE64_HPP_INCLUDED
39 
40 #include <iostream>
41 #include "types/base64.h"
42 
43 namespace _Wolframe {
44 namespace base64 {
45 
46 inline size_t encodedSize( size_t dataSize, unsigned short lineLength )
47 {
48  return base64_encodedSize( dataSize, lineLength );
49 }
50 
51 inline int encode( const void* data, size_t dataSize,
52  char* encoded, size_t encodedMaxSize, unsigned short lineLength = 72 )
53 {
54  return base64_encode( data, dataSize, encoded, encodedMaxSize, lineLength );
55 }
56 
57 std::string encode( const void* data, size_t dataSize, unsigned short lineLength );
58 
61 class Encoder
62 {
63  static const size_t BUFFERSIZE = 512;
64 public:
65  Encoder( size_t bufferSize = BUFFERSIZE,
66  unsigned short lineLength = DEFAULT_BASE64_LINE_LENGTH )
67  : m_bufferSize( bufferSize )
68  {
69  base64_initEncodeState( &m_state, lineLength );
70  }
71 
72  size_t encodedSize( size_t dataSize )
73  {
74  return base64_encodedSize( dataSize, m_state.lineLength );
75  }
76 
77  int encodeChunk( const void* data, size_t dataSize,
78  char* encoded, size_t encodedMaxSize )
79  {
80  return base64_encodeChunk( &m_state, data, dataSize, encoded, encodedMaxSize );
81  }
82 
83  int encodeEndChunk( char* encoded, size_t encodedMaxSize )
84  {
85  return base64_encodeEndChunk( &m_state, encoded, encodedMaxSize );
86  }
87 
88  void encode( std::istream& input, std::ostream& output );
89 
90 private:
91  base64_EncodeState m_state;
92  const size_t m_bufferSize;
93 
94 #ifdef _WIN32
95 // prevents C4512 on Windows (the m_bufferSize is const in the class)
96 private:
97  Encoder& operator=( const Encoder &o );
98 #endif
99 };
100 
101 
102 inline int decode( const char* encoded, size_t encodedSize, void* data, size_t dataMaxSize )
103 {
104  return base64_decode( encoded, encodedSize, data, dataMaxSize );
105 }
106 
107 inline int decode( const std::string encoded, void* data, size_t dataMaxSize )
108 {
109  return base64_decode( encoded.data(), encoded.length(), data, dataMaxSize );
110 }
111 
112 
115 class Decoder
116 {
117  static const size_t BUFFERSIZE = 512;
118 public:
120  {
121  base64_initDecodeState( &m_state );
122  }
123 
124  Decoder( size_t bufferSize ) : m_bufferSize( bufferSize )
125  {
126  base64_initDecodeState( &m_state );
127  }
128 
129  int decode( const char* encoded, size_t encodedSize,
130  void* data, size_t dataMaxSize )
131  {
132  return base64_decodeChunk( &m_state, encoded, encodedSize, data, dataMaxSize );
133  }
134 
135  void decode( std::istream& input, std::ostream& output );
136 
137 private:
138  base64_DecodeState m_state;
139  const size_t m_bufferSize;
140 
141 #ifdef _WIN32
142 // prevents C4512 on Windows (the m_bufferSize is const in the class)
143 private:
144  Decoder& operator=( const Decoder &o );
145 #endif
146 };
147 
148 }} // namespace _Wolframe::base64
149 
150 #endif // _BASE64_HPP_INCLUDED
151 
base64_EncodeState m_state
Definition: base64.hpp:91
const size_t m_bufferSize
Definition: base64.hpp:139
static const size_t BUFFERSIZE
Definition: base64.hpp:117
size_t encodedSize(size_t dataSize)
Definition: base64.hpp:72
Base64 decoder class for chunkwise decoding.
Definition: base64.hpp:115
base64_DecodeState m_state
Definition: base64.hpp:138
Base64 encoder class for chunkwise encoding.
Definition: base64.hpp:61
Decoder()
Definition: base64.hpp:119
int decode(const char *encoded, size_t encodedSize, void *data, size_t dataMaxSize)
Definition: base64.hpp:102
void encode(std::istream &input, std::ostream &output)
size_t encodedSize(size_t dataSize, unsigned short lineLength)
Definition: base64.hpp:46
int encodeEndChunk(char *encoded, size_t encodedMaxSize)
Definition: base64.hpp:83
const size_t m_bufferSize
Definition: base64.hpp:92
Decoder(size_t bufferSize)
Definition: base64.hpp:124
int encode(const void *data, size_t dataSize, char *encoded, size_t encodedMaxSize, unsigned short lineLength=72)
Definition: base64.hpp:51
int encodeChunk(const void *data, size_t dataSize, char *encoded, size_t encodedMaxSize)
Definition: base64.hpp:77
Encoder(size_t bufferSize=BUFFERSIZE, unsigned short lineLength=DEFAULT_BASE64_LINE_LENGTH)
Definition: base64.hpp:65
static const size_t BUFFERSIZE
Definition: base64.hpp:63
int decode(const char *encoded, size_t encodedSize, void *data, size_t dataMaxSize)
Definition: base64.hpp:129