source: libcf++/trunk/src/codec.cpp@ 4

Last change on this file since 4 was 4, checked in by cheese, 9 years ago

#1 commit prototype

File size: 8.6 KB
Line 
1/**
2 * @file codec.cpp
3 * @author myusgun@gmail.com
4 * @brief codec
5 */
6#include "cf/codec.h"
7#include "cf/memory.h"
8
9#define BASE64_PADDING_CHAR_INDEX 64
10
11cf::codec::base64 * cf::codec::base64::getInstance()
12{
13 static cf::codec::base64 instance;
14
15 return &instance;
16}
17
18std::string cf::codec::base64::encode(const cf::bin & in) const
19 throw (cf::exception)
20{
21 if (in.empty())
22 THROW_EXCEPTION("input is empty");
23 try
24 {
25 cf::size_t inlen = in.size();
26 cf::char_t * buf = NULL;
27 cf::size_t memsize = (((inlen + 2) / 3) * 4) + 1;
28 cf::memory mem(memsize);
29
30 buf = reinterpret_cast<cf::char_t *>(mem.buffer());
31
32 /*--------------------------------------------------------------*/
33 cf::char_t * dst = buf;
34 const cf::uint8_t * src = in.buffer();
35 const cf::uint8_t * ptr = src;
36
37 const static cf::char_t base64enc[] = {
38 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', /* 00 - 07 */
39 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', /* 08 - 15 */
40 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', /* 16 - 23 */
41 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', /* 24 - 31 */
42 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 32 - 39 */
43 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', /* 40 - 47 */
44 'w', 'x', 'y', 'z', '0', '1', '2', '3', /* 48 - 55 */
45 '4', '5', '6', '7', '8', '9', '+', '/', /* 56 - 63 */
46 '=' /* padding */
47 };
48
49#define SEXTUPLE_E_1(__x) (((__x[0]) >> 2) & 0x3f)
50#define SEXTUPLE_E_2(__x) (((__x[0]) << 4) & 0x30)
51#define SEXTUPLE_E_3(__x) (((__x[1]) >> 4) & 0x0f)
52#define SEXTUPLE_E_4(__x) (((__x[1]) << 2) & 0x3c)
53#define SEXTUPLE_E_5(__x) (((__x[2]) >> 6) & 0x03)
54#define SEXTUPLE_E_6(__x) (((__x[2]) ) & 0x3f)
55
56 for ( ; static_cast<cf::size_t>(ptr - src) < (inlen - 2) ; ptr += 3)
57 {
58 *dst++ = base64enc[SEXTUPLE_E_1 (ptr)];
59 *dst++ = base64enc[SEXTUPLE_E_2 (ptr) | SEXTUPLE_E_3 (ptr)];
60 *dst++ = base64enc[SEXTUPLE_E_4 (ptr) | SEXTUPLE_E_5 (ptr)];
61 *dst++ = base64enc[SEXTUPLE_E_6 (ptr)];
62 }
63
64 if (static_cast<cf::size_t>(ptr - src) < inlen)
65 {
66 *dst++ = base64enc[SEXTUPLE_E_1 (ptr)];
67
68 if (static_cast<cf::size_t>(ptr - src) == (inlen - 1))
69 {
70 *dst++ = base64enc[SEXTUPLE_E_2 (ptr)];
71 *dst++ = base64enc[BASE64_PADDING_CHAR_INDEX];
72 }
73 else
74 {
75 *dst++ = base64enc[SEXTUPLE_E_2 (ptr) | SEXTUPLE_E_3 (ptr)];
76 *dst++ = base64enc[SEXTUPLE_E_4 (ptr)];
77 }
78
79 *dst++ = base64enc[BASE64_PADDING_CHAR_INDEX];
80 }
81 *dst = '\0';
82 /*--------------------------------------------------------------*/
83
84 return std::string(buf);
85 }
86 catch (cf::exception &e)
87 {
88 FORWARD_EXCEPTION(e);
89 }
90}
91
92cf::bin cf::codec::base64::decode(const std::string & in) const
93 throw (cf::exception)
94{
95 if (in.empty())
96 THROW_EXCEPTION("base64 string is empty");
97
98 try
99 {
100 cf::uint8_t * buf = NULL;
101 cf::long_t outlen = 0;
102 cf::size_t memsize = in.size() / 4 * 3;
103 cf::memory mem(memsize);
104
105 buf = reinterpret_cast<cf::uint8_t *>(mem.buffer());
106
107 /*--------------------------------------------------------------*/
108 const cf::char_t * src = in.c_str();
109 const cf::char_t * ptr = src;
110 cf::uint8_t * dst = buf;
111 cf::long_t remain = 0;
112 cf::long_t binlen = 0;
113
114 const static cf::uint8_t base64dec[] = {
115 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 00 - 07 */
116 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 08 - 15 */
117 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 16 - 23 */
118 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 24 - 31 */
119 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 32 - 39 */
120 0xff,0xff,0xff, 62,0xff,0xff,0xff, 63, /* 40 - 47 */
121 52, 53, 54, 55, 56, 57, 58, 59, /* 48 - 55 */
122 60, 61,0xff,0xff,0xff, 64,0xff,0xff, /* 56 - 63 */
123 0xff, 0, 1, 2, 3, 4, 5, 6, /* 64 - 71 */
124 7, 8, 9, 10, 11, 12, 13, 14, /* 71 - 79 */
125 15, 16, 17, 18, 19, 20, 21, 22, /* 80 - 87 */
126 23, 24, 25,0xff,0xff,0xff,0xff,0xff, /* 88 - 95 */
127 0xff, 26, 27, 28, 29, 30, 31, 32, /* 96 - 103 */
128 33, 34, 35, 36, 37, 38, 39, 40, /* 104 - 111 */
129 41, 42, 43, 44, 45, 46, 47, 48, /* 112 - 119 */
130 49, 50, 51,0xff,0xff,0xff,0xff,0xff /* 120 - 127 */
131 };
132
133 while (base64dec[(int)*ptr] < BASE64_PADDING_CHAR_INDEX) ptr++;
134
135 if (*ptr == 0xff)
136 THROW_EXCEPTION("input string is not base64-encoded");
137
138 remain = ptr - src;
139 binlen = ((remain + 2/* max padding length */) / 4) * 3;
140
141#define SEXTUPLE_D_1(src) (base64dec[(int)src[0]] << 2)
142#define SEXTUPLE_D_2(src) (base64dec[(int)src[1]] >> 4)
143#define SEXTUPLE_D_3(src) (base64dec[(int)src[1]] << 4)
144#define SEXTUPLE_D_4(src) (base64dec[(int)src[2]] >> 2)
145#define SEXTUPLE_D_5(src) (base64dec[(int)src[2]] << 6)
146#define SEXTUPLE_D_6(src) (base64dec[(int)src[3]] )
147
148 for (ptr = src ; remain > 4 ; remain -= 4, ptr += 4)
149 {
150 *dst++ = (cf::uint8_t)(SEXTUPLE_D_1(ptr) | SEXTUPLE_D_2(ptr));
151 *dst++ = (cf::uint8_t)(SEXTUPLE_D_3(ptr) | SEXTUPLE_D_4(ptr));
152 *dst++ = (cf::uint8_t)(SEXTUPLE_D_5(ptr) | SEXTUPLE_D_6(ptr));
153 }
154
155 if (remain > 1)
156 *dst++ = (cf::uint8_t)(SEXTUPLE_D_1(ptr) | SEXTUPLE_D_2(ptr));
157 if (remain > 2)
158 *dst++ = (cf::uint8_t)(SEXTUPLE_D_3(ptr) | SEXTUPLE_D_4(ptr));
159 if (remain > 3)
160 *dst++ = (cf::uint8_t)(SEXTUPLE_D_5(ptr) | SEXTUPLE_D_6(ptr));
161
162 outlen = binlen - (4 - remain);
163 /*--------------------------------------------------------------*/
164
165 return cf::bin(buf, static_cast<cf::size_t>(outlen));
166 }
167 catch (cf::exception &e)
168 {
169 FORWARD_EXCEPTION(e);
170 }
171}
172
173cf::codec::hex * cf::codec::hex::getInstance()
174{
175 static cf::codec::hex instance;
176
177 return &instance;
178}
179
180std::string cf::codec::hex::encode(const cf::bin & in) const
181 throw (cf::exception)
182{
183 if (in.empty())
184 THROW_EXCEPTION("input is empty");
185
186 try
187 {
188 cf::char_t * buf = NULL;
189 cf::size_t memsize = in.size() * 2 + 1;
190 cf::memory mem(memsize);
191
192 buf = reinterpret_cast<cf::char_t *>(mem.buffer());
193
194 /*--------------------------------------------------------------*/
195 const cf::uint8_t * src = in.buffer();
196 cf::char_t * dst = buf;
197
198 const static cf::char_t hexenc[] = {
199 '0', '1', '2', '3', '4', '5', '6', '7',
200 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
201 };
202
203 for ( ; static_cast<cf::size_t>(src - in.buffer()) < in.size() ; src++)
204 {
205 *dst++ = hexenc[((*src) >> 4) & 0x0f];
206 *dst++ = hexenc[((*src) ) & 0x0f];
207 }
208 *dst = '\0';
209 /*--------------------------------------------------------------*/
210
211 return std::string(buf);
212 }
213 catch (cf::exception &e)
214 {
215 FORWARD_EXCEPTION(e);
216 }
217}
218
219cf::bin cf::codec::hex::decode(const std::string & in) const
220 throw (cf::exception)
221{
222 if (in.empty())
223 THROW_EXCEPTION("hex string is empty");
224
225 try
226 {
227 cf::uint8_t * buf = NULL;
228 cf::int32_t len = 0;
229 cf::size_t memsize = in.size() / 2;
230 cf::memory mem(memsize);
231
232 buf = reinterpret_cast<cf::uint8_t *>(mem.buffer());
233
234 /*--------------------------------------------------------------*/
235 cf::int32_t length = 0; /* even-number */
236
237 const cf::char_t * src = in.c_str();
238 const cf::char_t * ptr = src;
239 cf::uint8_t * dst = buf;
240 cf::uint8_t val = 0;
241 cf::uint8_t sum = 0;
242 cf::uint8_t asciiHex = 0;
243
244 const static cf::uint8_t hexdec[] = {
245 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 00 - 07 */
246 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 08 - 15 */
247 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16 - 23 */
248 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 24 - 31 */
249 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 32 - 39 */
250 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 40 - 47 */
251 '0', '0', '0', '0', '0', '0', '0', '0', /* 48 - 55 */
252 '0', '0',0x00,0x00,0x00,0x00,0x00,0x00, /* 56 - 63 */
253 0x00, 'A', 'A', 'A', 'A', 'A', 'A',0x00, /* 64 - 71 */
254 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 71 - 79 */
255 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 80 - 87 */
256 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 88 - 95 */
257 0x00, 'a', 'a', 'a', 'a', 'a', 'a',0x00, /* 96 - 103 */
258 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 104 - 111 */
259 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112 - 119 */
260 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 /* 120 - 127 */
261 };
262
263 for ( ; hexdec[(int)*ptr] && *ptr ; ptr++, length++);
264
265 if (*ptr)
266 THROW_EXCEPTION("input string is not hex-encoded");
267
268 for (ptr = src ; *ptr ; )
269 {
270 sum = 0; /* init/re-init decoding-buffer */
271
272 /* decode one character */
273#define DECODE_HEX(x) \
274 do { \
275 val = static_cast<cf::uint8_t>(*(x)); \
276 sum = (cf::uint8_t)(sum << 4); \
277 asciiHex = hexdec[val]; \
278 \
279 sum |= (cf::uint8_t) \
280 (val - asciiHex + (asciiHex == '0' ? 0 : 10)); \
281 } while (0)
282
283 /* decode one byte by decode two character */
284 DECODE_HEX(ptr++);
285 DECODE_HEX(ptr++);
286
287 *dst++ = sum;
288 }
289 len = length / 2;
290 /*--------------------------------------------------------------*/
291
292 return cf::bin(buf, len);
293 }
294 catch (cf::exception &e)
295 {
296 FORWARD_EXCEPTION(e);
297 }
298}
Note: See TracBrowser for help on using the repository browser.