/[debian]/mimetic/branches/upstream/current/mimetic/codec/base64.h
ViewVC logotype

Contents of /mimetic/branches/upstream/current/mimetic/codec/base64.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 128 - (show annotations)
Sat Feb 25 16:49:20 2006 UTC (15 years, 5 months ago) by gregoa
File MIME type: text/plain
File size: 6529 byte(s)
[svn-inject] Installing original source of mimetic
1 /***************************************************************************
2 copyright : (C) 2002-2005 by Stefano Barbato
3 email : stefano@codesink.org
4
5 $Id: base64.h,v 1.13 2005/02/24 11:51:10 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 /*! Constructor, maxlen is the maximum length of every encoded line */
88 Encoder(int maxlen = default_maxlen)
89 : m_cidx(0), m_pos(1), m_maxlen(maxlen)
90 {
91 }
92 /*! Returns the name of the codec ("Base64") */
93 const char* name() const { return "Base64"; }
94 /*!
95 Encodes [\p bit,\p eit) and write any encoded char to \p out.
96 */
97 template<typename InIt, typename OutIt>
98 void process(InIt bit, InIt eit, OutIt out)
99 {
100 for(; bit != eit; ++bit)
101 {
102 m_ch[m_cidx++] = (char_type)*bit;
103 if(m_cidx < 3)
104 continue;
105 writeBuf(out);
106 }
107 if(m_cidx > 0)
108 writeBuf(out);
109 }
110 /*!
111 Encodes \p c and write any encoded output char to \p out.
112 \warning You must call flush() when all chars have been
113 processed by the encode funcion.
114 \n
115 \code
116 while( (c = getchar()) != EOF )
117 b64.encode(c, out);
118 b64.flush();
119 \endcode
120 \n
121 \sa flush()
122 */
123 template<typename OutIt>
124 void process(char_type c, OutIt& out)
125 {
126 m_ch[m_cidx++] = c;
127 if(m_cidx < 3)
128 return;
129 writeBuf(out);
130 }
131 /*!
132 Write to \p out any buffered encoded char.
133 */
134 template<typename OutIt>
135 void flush(OutIt& out)
136 {
137 if(m_cidx > 0)
138 writeBuf(out);
139 }
140 };
141
142 /// Base64 decoder
143 /*!
144
145 \sa encode decode
146 */
147 class Decoder: public buffered_codec, public chainable_codec<Decoder>
148 {
149 int m_cidx;
150 char_type m_ch[4];
151
152 template<typename OutIt>
153 inline void writeBuf(OutIt& out)
154 {
155 if(m_cidx < 4)
156 { // malformed, missing chars will be cosidered pad
157 switch(m_cidx)
158 {
159 case 0:
160 case 1:
161 return; // ignore;
162 case 2:
163 m_ch[2] = m_ch[3] = eq_sign;
164 break;
165 case 3:
166 m_ch[3] = eq_sign;
167 break;
168 }
169 }
170 m_cidx = 0;
171 *out = (m_ch[0] << 2 | ((m_ch[1] >> 4) & 0x3) ); ++out;
172 if(m_ch[2] == eq_sign) return;
173 *out = (m_ch[1] << 4 | ((m_ch[2] >> 2) & 0xF) ); ++out;
174 if(m_ch[3] == eq_sign) return;
175 *out = (m_ch[2] << 6 | m_ch[3]); ++out;
176 }
177 public:
178 /*! Constructor */
179 Decoder()
180 : m_cidx(0)
181 {
182 }
183 /*! Returns the name of the codec ("Base64") */
184 const char* name() const { return "Base64"; }
185
186 /*!
187 Decodes [\p bit,\p eit) and write any decoded char to \p out.
188 */
189 template<typename InIt, typename OutIt>
190 inline void process(InIt bit, InIt eit, OutIt out)
191 {
192 char_type c;
193
194 for(; bit != eit; ++bit)
195 {
196 c = *bit;
197 if(c > sDecTableSz || sDecTable[c] == -1)
198 continue; // malformed or newline
199 m_ch[m_cidx++] = sDecTable[c];
200 if(m_cidx < 4)
201 continue;
202 writeBuf(out);
203 }
204 if(m_cidx > 0)
205 writeBuf(out);
206 }
207 /*!
208 Decodes \p c and write any decoded output char to \p out.
209
210 \warning You must call flush() when all chars have been
211 processed by the decode funcion.
212 \n
213 \code
214 while( (c = getchar()) != EOF )
215 b64.decode(c, out);
216 b64.flush();
217 \endcode
218 \n
219 \sa flush()
220 */
221 template<typename OutIt>
222 void process(char_type c, OutIt& out)
223 {
224 if(c > sDecTableSz || sDecTable[c] == -1)
225 return; // malformed or newline
226 m_ch[m_cidx++] = sDecTable[c];
227 if(m_cidx < 4)
228 return;
229 writeBuf(out);
230 }
231 /*!
232 Write to \p out any buffered decoded char.
233 */
234 template<typename OutIt>
235 void flush(OutIt& out)
236 {
237 if(m_cidx > 0)
238 writeBuf(out);
239 }
240 };
241
242 }; // Base64
243
244 }
245 #endif
246

  ViewVC Help
Powered by ViewVC 1.1.26