/[debian]/mimetic/trunk/mimetic/codec/base64.h
ViewVC logotype

Contents of /mimetic/trunk/mimetic/codec/base64.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 197 - (show annotations)
Sun Apr 16 12:21:05 2006 UTC (15 years, 3 months ago) by gregoa
File MIME type: text/plain
File size: 6702 byte(s)
New upstream release.

1 /***************************************************************************
2 copyright : (C) 2002-2005 by Stefano Barbato
3 email : stefano@codesink.org
4
5 $Id: base64.h,v 1.14 2006/03/12 12:04:36 tat Exp $
6 ***************************************************************************/
7
8 /***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16 #ifndef _MIMETIC_CODEC_BASE64_H_
17 #define _MIMETIC_CODEC_BASE64_H_
18 #include <mimetic/circular_buffer.h>
19 #include <mimetic/codec/codec_base.h>
20 #include <mimetic/codec/codec_chain.h>
21
22 namespace mimetic
23 {
24
25
26 class Base64
27 {
28 enum { LF = 0xA, CR = 0xD, NL = '\n' };
29 enum { default_maxlen = 76 };
30 enum { eq_sign = 100 };
31 static const char sEncTable[];
32 static const char sDecTable[];
33 static const int sDecTableSz;
34 public:
35 class Encoder; class Decoder;
36 typedef Encoder encoder_type;
37 typedef Decoder decoder_type;
38
39
40 /// Base64 encoder
41 /*!
42
43 \sa encode decode
44 */
45 class Encoder: public buffered_codec, public chainable_codec<Encoder>
46 {
47 enum { pad_idx = 64 };
48 char_type m_ch[3];
49 int m_cidx;
50 int m_pos, m_maxlen;
51
52 template<typename OutIt>
53 inline void writeBuf(OutIt& out)
54 {
55 int pad_count = 3 - m_cidx;
56 m_cidx = 0;
57 int idx[4];
58 idx[0] = m_ch[0] >> 2;
59 switch(pad_count)
60 {
61 case 0:
62 idx[1] = (((m_ch[0] & 3) << 4) | (m_ch[1] >> 4));
63 idx[2] = ((m_ch[1] & 0xf) << 2) | (m_ch[2] >> 6);
64 idx[3] = m_ch[2] & 0x3f;
65 break;
66 case 1:
67 idx[1] = (((m_ch[0] & 3) << 4) | (m_ch[1] >> 4));
68 idx[2] = (m_ch[1] & 0xf) << 2 ;
69 idx[3] = pad_idx;
70 break;
71 case 2:
72 idx[1] = (m_ch[0] & 3) << 4;
73 idx[2] = idx[3] = pad_idx;
74 break;
75 }
76 for(int i = 0; i < 4; ++i)
77 {
78 *out = sEncTable[ idx[i] ]; ++out;
79 if(m_maxlen && ++m_pos > m_maxlen)
80 {
81 *out = NL; ++out;
82 m_pos = 1;
83 }
84 }
85 }
86 public:
87 /*! return the multiplier of the required (max) size of the output buffer
88 * when encoding */
89 double codeSizeMultiplier() const
90 {
91 return 1.5;
92 }
93 /*! Constructor, maxlen is the maximum length of every encoded line */
94 Encoder(int maxlen = default_maxlen)
95 : m_cidx(0), m_pos(1), m_maxlen(maxlen)
96 {
97 }
98 /*! Returns the name of the codec ("Base64") */
99 const char* name() const { return "Base64"; }
100 /*!
101 Encodes [\p bit,\p eit) and write any encoded char to \p out.
102 */
103 template<typename InIt, typename OutIt>
104 void process(InIt bit, InIt eit, OutIt out)
105 {
106 for(; bit != eit; ++bit)
107 {
108 m_ch[m_cidx++] = (char_type)*bit;
109 if(m_cidx < 3)
110 continue;
111 writeBuf(out);
112 }
113 if(m_cidx > 0)
114 writeBuf(out);
115 }
116 /*!
117 Encodes \p c and write any encoded output char to \p out.
118 \warning You must call flush() when all chars have been
119 processed by the encode funcion.
120 \n
121 \code
122 while( (c = getchar()) != EOF )
123 b64.encode(c, out);
124 b64.flush();
125 \endcode
126 \n
127 \sa flush()
128 */
129 template<typename OutIt>
130 void process(char_type c, OutIt& out)
131 {
132 m_ch[m_cidx++] = c;
133 if(m_cidx < 3)
134 return;
135 writeBuf(out);
136 }
137 /*!
138 Write to \p out any buffered encoded char.
139 */
140 template<typename OutIt>
141 void flush(OutIt& out)
142 {
143 if(m_cidx > 0)
144 writeBuf(out);
145 }
146 };
147
148 /// Base64 decoder
149 /*!
150
151 \sa encode decode
152 */
153 class Decoder: public buffered_codec, public chainable_codec<Decoder>
154 {
155 int m_cidx;
156 char_type m_ch[4];
157
158 template<typename OutIt>
159 inline void writeBuf(OutIt& out)
160 {
161 if(m_cidx < 4)
162 { // malformed, missing chars will be cosidered pad
163 switch(m_cidx)
164 {
165 case 0:
166 case 1:
167 return; // ignore;
168 case 2:
169 m_ch[2] = m_ch[3] = eq_sign;
170 break;
171 case 3:
172 m_ch[3] = eq_sign;
173 break;
174 }
175 }
176 m_cidx = 0;
177 *out = (m_ch[0] << 2 | ((m_ch[1] >> 4) & 0x3) ); ++out;
178 if(m_ch[2] == eq_sign) return;
179 *out = (m_ch[1] << 4 | ((m_ch[2] >> 2) & 0xF) ); ++out;
180 if(m_ch[3] == eq_sign) return;
181 *out = (m_ch[2] << 6 | m_ch[3]); ++out;
182 }
183 public:
184 /*! Constructor */
185 Decoder()
186 : m_cidx(0)
187 {
188 }
189 /*! Returns the name of the codec ("Base64") */
190 const char* name() const { return "Base64"; }
191
192 /*!
193 Decodes [\p bit,\p eit) and write any decoded char to \p out.
194 */
195 template<typename InIt, typename OutIt>
196 inline void process(InIt bit, InIt eit, OutIt out)
197 {
198 char_type c;
199
200 for(; bit != eit; ++bit)
201 {
202 c = *bit;
203 if(c > sDecTableSz || sDecTable[c] == -1)
204 continue; // malformed or newline
205 m_ch[m_cidx++] = sDecTable[c];
206 if(m_cidx < 4)
207 continue;
208 writeBuf(out);
209 }
210 if(m_cidx > 0)
211 writeBuf(out);
212 }
213 /*!
214 Decodes \p c and write any decoded output char to \p out.
215
216 \warning You must call flush() when all chars have been
217 processed by the decode funcion.
218 \n
219 \code
220 while( (c = getchar()) != EOF )
221 b64.decode(c, out);
222 b64.flush();
223 \endcode
224 \n
225 \sa flush()
226 */
227 template<typename OutIt>
228 void process(char_type c, OutIt& out)
229 {
230 if(c > sDecTableSz || sDecTable[c] == -1)
231 return; // malformed or newline
232 m_ch[m_cidx++] = sDecTable[c];
233 if(m_cidx < 4)
234 return;
235 writeBuf(out);
236 }
237 /*!
238 Write to \p out any buffered decoded char.
239 */
240 template<typename OutIt>
241 void flush(OutIt& out)
242 {
243 if(m_cidx > 0)
244 writeBuf(out);
245 }
246 };
247
248 }; // Base64
249
250 }
251 #endif
252

  ViewVC Help
Powered by ViewVC 1.1.26