29#ifndef Pt_Base64Codec_h
30#define Pt_Base64Codec_h
34#include <Pt/TextCodec.h>
77 result do_in(MBState& s,
78 const char* fromBegin,
80 const char*& fromNext,
86 result do_out(MBState& s,
87 const char* fromBegin,
89 const char*& fromNext,
95 result do_unshift(MBState& state,
101 bool do_always_noconv()
const throw()
107 int do_length(MBState& s,
const char* fromBegin,
108 const char* fromEnd, std::size_t max)
const
110 const int from =
static_cast<int>( (fromEnd - fromBegin) / 4 );
111 const int to =
static_cast<int>( max / 3 );
112 return to > from ? from * 4 : to * 4;
116 int do_encoding()
const throw()
123 int do_max_length()
const throw()
133 static const char b64enc[]
134 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
140inline uint8_t fromBase64(
char b64)
143 = { 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
144 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
145 255,255,255,255,255,255,255,255,255,255,255,62,255,255,255,63,
146 52,53,54,55,56,57,58,59,60,61,255,255,255,64,255,255,
147 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
148 15,16,17,18,19,20,21,22,23,24,25,255,255,255,255,255,
149 255,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
150 41,42,43,44,45,46,47,48,49,50,51,255,255,255,255,255,
151 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
152 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
153 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
154 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
155 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
156 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
157 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
158 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255 };
160 return b64dec[(int)b64];
164inline Base64Codec::result Base64Codec::do_in(MBState& s,
165 const char* fromBegin,
167 const char*& fromNext,
172 fromNext = fromBegin;
175 while( (fromEnd - fromNext) >= 4 && (toEnd - toNext) >= 3 )
182 *(toNext++) = (first << 2) + (second >> 4);
185 *(toNext++) = (second << 4) + (third >> 2);
188 *(toNext++) = (third << 6) + (fourth);
191 if( fromEnd == fromNext )
192 return std::codecvt_base::ok;
194 return std::codecvt_base::partial;
198inline Base64Codec::result Base64Codec::do_out(Pt::MBState& state,
199 const char* fromBegin,
201 const char*& fromNext,
206 fromNext = fromBegin;
209 const char* first = 0;
210 const char* second = 0;
211 const char* third = 0;
213 if(fromEnd - fromNext < 1)
214 return std::codecvt_base::partial;
216 if(toEnd - toNext < 4)
217 return std::codecvt_base::partial;
222 first = &state.value.mbytes[0];
223 second = &state.value.mbytes[1];
228 if(fromEnd - fromNext < 2)
230 state.value.mbytes[1] = *fromNext++;
232 return std::codecvt_base::partial;
235 first = &state.value.mbytes[0];
241 if(fromEnd - fromNext == 1)
243 state.value.mbytes[0] = *fromNext++;
245 return std::codecvt_base::partial;
248 if(fromEnd - fromNext == 2)
250 state.value.mbytes[0] = *fromNext++;
251 state.value.mbytes[1] = *fromNext++;
253 return std::codecvt_base::partial;
264 *toNext++ = toBase64( (
uint8_t(*first) >> 2) & 0x3f );
265 *(toNext++) = toBase64( ((
uint8_t(*first) << 4) + (
uint8_t(*second) >> 4)) & 0x3f );
266 *(toNext++) = toBase64( ((
uint8_t(*second) << 2) + (
uint8_t(*third) >> 6)) & 0x3f );
267 *(toNext++) = toBase64(
uint8_t(*third) & 0x3f );
269 if(toEnd - toNext < 4)
272 return std::codecvt_base::partial;
275 if( fromEnd - fromNext < 3 )
283 switch( fromEnd - fromNext )
286 state.value.mbytes[0] = *fromNext++;
287 state.value.mbytes[1] = *fromNext++;
292 state.value.mbytes[0] = *fromNext++;
301 return std::codecvt_base::ok;
305inline Base64Codec::result Base64Codec::do_unshift(MBState& state,
312 if(toEnd - toBegin < 4)
314 return std::codecvt_base::partial;
320 *toNext++ = toBase64( (
uint8_t(state.value.mbytes[0]) >> 2) & 0x3f );
321 *(toNext++) = toBase64( ((
uint8_t(state.value.mbytes[0]) << 4) + (
uint8_t(state.value.mbytes[1]) >> 4)) & 0x3f );
322 *(toNext++) = toBase64( (
uint8_t(state.value.mbytes[1]) << 2) & 0x3f );
327 *toNext++ = toBase64( (
uint8_t(state.value.mbytes[0]) >> 2) & 0x3f );
328 *(toNext++) = toBase64( (
uint8_t(state.value.mbytes[0]) << 4) & 0x3f );
334 return std::codecvt_base::noconv;
338 return std::codecvt_base::ok;
virtual ~Base64Codec()
Destructor.
Definition Base64Codec.h:72
Base64Codec(std::size_t ref=0)
Default constructor.
Definition Base64Codec.h:66
TextCodec(std::size_t ref=0)
uint_type uint8_t
Unsigned 8-bit integer type.
Definition Api-Types.h:24
Core module.
Definition Allocator.h:33