29 #ifndef Pt_Base64Codec_h
30 #define Pt_Base64Codec_h
34 #include <Pt/TextCodec.h>
58 result do_in(MBState& s,
59 const char* fromBegin,
61 const char*& fromNext,
67 result do_out(MBState& s,
68 const char* fromBegin,
70 const char*& fromNext,
76 result do_unshift(MBState& state,
82 bool do_always_noconv()
const throw()
88 int do_length(MBState& s,
const char* fromBegin,
89 const char* fromEnd, std::size_t max)
const
91 const int from =
static_cast<int>( (fromEnd - fromBegin) / 4 );
92 const int to =
static_cast<int>( max / 3 );
93 return to > from ? from * 4 : to * 4;
97 int do_encoding()
const throw()
104 int do_max_length()
const throw()
112 inline char toBase64(
uint8_t n)
114 static const char b64enc[]
115 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
121 inline uint8_t fromBase64(
char b64)
124 = { 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
125 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
126 255,255,255,255,255,255,255,255,255,255,255,62,255,255,255,63,
127 52,53,54,55,56,57,58,59,60,61,255,255,255,64,255,255,
128 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
129 15,16,17,18,19,20,21,22,23,24,25,255,255,255,255,255,
130 255,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
131 41,42,43,44,45,46,47,48,49,50,51,255,255,255,255,255,
132 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
133 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
134 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
135 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
136 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
137 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
138 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
139 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255 };
141 return b64dec[(int)b64];
145 inline Base64Codec::result Base64Codec::do_in(MBState& s,
146 const char* fromBegin,
148 const char*& fromNext,
153 fromNext = fromBegin;
156 while( (fromEnd - fromNext) >= 4 && (toEnd - toNext) >= 3 )
163 *(toNext++) = (first << 2) + (second >> 4);
166 *(toNext++) = (second << 4) + (third >> 2);
169 *(toNext++) = (third << 6) + (fourth);
172 if( fromEnd == fromNext )
173 return std::codecvt_base::ok;
175 return std::codecvt_base::partial;
179 inline Base64Codec::result Base64Codec::do_out(Pt::MBState& state,
180 const char* fromBegin,
182 const char*& fromNext,
187 fromNext = fromBegin;
190 const char* first = 0;
191 const char* second = 0;
192 const char* third = 0;
194 if(fromEnd - fromNext < 1)
195 return std::codecvt_base::partial;
197 if(toEnd - toNext < 4)
198 return std::codecvt_base::partial;
203 first = &state.value.mbytes[0];
204 second = &state.value.mbytes[1];
209 if(fromEnd - fromNext < 2)
211 state.value.mbytes[1] = *fromNext++;
213 return std::codecvt_base::partial;
216 first = &state.value.mbytes[0];
222 if(fromEnd - fromNext == 1)
224 state.value.mbytes[0] = *fromNext++;
226 return std::codecvt_base::partial;
229 if(fromEnd - fromNext == 2)
231 state.value.mbytes[0] = *fromNext++;
232 state.value.mbytes[1] = *fromNext++;
234 return std::codecvt_base::partial;
245 *toNext++ = toBase64( (
uint8_t(*first) >> 2) & 0x3f );
246 *(toNext++) = toBase64( ((
uint8_t(*first) << 4) + (
uint8_t(*second) >> 4)) & 0x3f );
247 *(toNext++) = toBase64( ((
uint8_t(*second) << 2) + (
uint8_t(*third) >> 6)) & 0x3f );
248 *(toNext++) = toBase64(
uint8_t(*third) & 0x3f );
250 if(toEnd - toNext < 4)
253 return std::codecvt_base::partial;
256 if( fromEnd - fromNext < 3 )
264 switch( fromEnd - fromNext )
267 state.value.mbytes[0] = *fromNext++;
268 state.value.mbytes[1] = *fromNext++;
273 state.value.mbytes[0] = *fromNext++;
282 return std::codecvt_base::ok;
286 inline Base64Codec::result Base64Codec::do_unshift(MBState& state,
293 if(toEnd - toBegin < 4)
295 return std::codecvt_base::partial;
301 *toNext++ = toBase64( (
uint8_t(state.value.mbytes[0]) >> 2) & 0x3f );
302 *(toNext++) = toBase64( ((
uint8_t(state.value.mbytes[0]) << 4) + (
uint8_t(state.value.mbytes[1]) >> 4)) & 0x3f );
303 *(toNext++) = toBase64( (
uint8_t(state.value.mbytes[1]) << 2) & 0x3f );
308 *toNext++ = toBase64( (
uint8_t(state.value.mbytes[0]) >> 2) & 0x3f );
309 *(toNext++) = toBase64( (
uint8_t(state.value.mbytes[0]) << 4) & 0x3f );
315 return std::codecvt_base::noconv;
319 return std::codecvt_base::ok;
Base64Codec(std::size_t ref=0)
Default constructor.
Definition: Base64Codec.h:47
A codec for base-64 encoding.
Definition: Base64Codec.h:42
Converts between character encodings.
Definition: TextCodec.h:38
virtual ~Base64Codec()
Destructor.
Definition: Base64Codec.h:53
uint_type uint8_t
Unsigned 8-bit integer type.
Definition: Types.h:18