source: libcf/trunk/src/cf_codec.c@ 92

Last change on this file since 92 was 92, checked in by cheese, 11 years ago

#1 fix base64 decoding bug

File size: 9.4 KB
Line 
1/**
2 * @file cf_codec.c
3 * @author myusgun <myusgun@gmail.com>
4 */
5#include "cf_codec.h"
6#include "cf_error.h"
7
8#include <string.h>
9#include <stdio.h>
10
11#define ASSERT_ARGS(x) \
12 if ((x)) \
13 return CF_ERROR_CODEC_INVALID_ARGS
14
15const static unsigned char g_ascii_HexDecode[] = {
16 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 00 - 15 */
17 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 16 - 31 */
18 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 32 - 47 */
19 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',0x00,0x00,0x00,0x00,0x00,0x00, /* 48 - 63 */
20 0x00, 'A', 'A', 'A', 'A', 'A', 'A',0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 64 - 79 */
21 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 80 - 95 */
22 0x00, 'a', 'a', 'a', 'a', 'a', 'a',0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 96 - 111 */
23 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 112 - 127 */
24 /* end of ascii character */
25 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 128 - 143 */
26 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 144 - 159 */
27 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 160 - 175 */
28 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 176 - 191 */
29 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 192 - 207 */
30 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 208 - 223 */
31 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 224 - 239 */
32 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 240 - 255 */
33};
34
35const static char g_table_Base64Encode[] = {
36#define BASE64_PADDING_CHAR_INDEX 64
37 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', /* 00 - 07 */
38 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', /* 08 - 15 */
39 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', /* 16 - 23 */
40 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', /* 24 - 31 */
41 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 32 - 39 */
42 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', /* 40 - 47 */
43 'w', 'x', 'y', 'z', '0', '1', '2', '3', /* 48 - 55 */
44 '4', '5', '6', '7', '8', '9', '+', '/', /* 56 - 63 */
45 '=' /* padding */
46};
47
48const static unsigned char g_ascii_Base64Decode[] = {
49 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 00 - 15 */
50 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 16 - 31 */
51 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 62,0xff,0xff,0xff, 63, /* 32 - 47 */
52 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,0xff,0xff,0xff, 64,0xff,0xff, /* 48 - 63 */
53 0xff, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 64 - 79 */
54 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,0xff,0xff,0xff,0xff,0xff, /* 80 - 95 */
55 0xff, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 96 - 111 */
56 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,0xff,0xff,0xff,0xff,0xff, /* 112 - 127 */
57 /* end of ascii character */
58 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 128 - 143 */
59 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 144 - 159 */
60 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 160 - 175 */
61 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 176 - 191 */
62 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 192 - 207 */
63 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 208 - 223 */
64 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 224 - 239 */
65 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, /* 240 - 255 */
66};
67
68
69/**
70 * hex-encode
71 *
72 * @return 성공 시, CF_OK; 실패 시, 오류 코드
73 *
74 * @param bin 바이너리 데이터
75 * @param len 바이너리 데이터 길이
76 * @param hex 16진수 문자열을 저장할 주소
77 *
78 * @remark
79 * hex는 할당된 메모리이며, 크기는 '\0'를 제외하고 len * 2
80 */
81int
82CF_Codec_Hex_Encode (const unsigned char * bin,
83 const size_t len,
84 char * hex)
85{
86 size_t iter = 0;
87 size_t hexlen = len * 2 + 1;
88
89 const unsigned char * ptr = bin;
90
91 const static char hexchar[] = {'0', '1', '2', '3',
92 '4', '5', '6', '7',
93 '8', '9', 'a', 'b',
94 'c', 'd', 'e', 'f'};
95
96 ASSERT_ARGS (bin == NULL);
97 ASSERT_ARGS (hex == NULL);
98
99 for (iter = 0 ; iter < hexlen ; iter += 2, ptr++)
100 {
101 hex[iter ] = hexchar[((*(ptr)) >> 4) & 0x0f];
102 hex[iter + 1] = hexchar[((*(ptr)) ) & 0x0f];
103 }
104 hex[hexlen - 1] = '\0';
105
106 return CF_OK;
107}
108
109/**
110 * hex-decode
111 *
112 * @return 성공 시, 디코딩된 바이너리 데이터의 길이; 실패 시, 오류 코드
113 *
114 * @param hex 16진수 문자열
115 * @param bin 바이너리 데이터를 저장할 주소
116 *
117 * @remark
118 * bin는 할당된 메모리이며, 크기는 strlen (hex) / 2
119 */
120int
121CF_Codec_Hex_Decode (const char * hex,
122 unsigned char * bin)
123{
124 size_t length = strlen (hex); /* absolutely even-number */
125 size_t iter = 0;
126 int binlen = (int)length / 2;
127
128 const char * ptr = hex;
129 char buf = 0;
130 unsigned char val = 0;
131 unsigned char asciiHex = 0;
132
133 ASSERT_ARGS (hex == NULL);
134 ASSERT_ARGS (bin == NULL);
135
136 for (iter = 0 ; iter < binlen ; iter++)
137 {
138 val = 0; /* init/re-init docoding-buffer */
139
140 /* decode one character */
141#define DECODE_HEX(x) \
142 do { \
143 buf = *(x); \
144 val = (unsigned char)(val << 4); \
145 asciiHex = g_ascii_HexDecode[(int)buf]; \
146 \
147 if (buf) \
148 val |= (unsigned char) \
149 (buf - asciiHex + (asciiHex == '0' ? 0 : 10)); \
150 else \
151 return CF_ERROR_CODEC_NOT_HEXSTRING; \
152 } while (0)
153
154 /* decode one byte by decode two character */
155 DECODE_HEX (ptr++);
156 DECODE_HEX (ptr++);
157
158 bin[iter] = val;
159 }
160
161 return binlen;
162}
163
164/**
165 * Base64-encode
166 *
167 * @return 성공 시, CF_OK; 실패 시, 오류 코드
168 *
169 * @param bin 바이너리 데이터
170 * @param len 바이너리 데이터 길이
171 * @param base64 base64 문자열을 저장할 주소
172 *
173 * @remark
174 * base64는 할당된 메모리이며, 크기는 '\0'를 제외하고 ((len + 2) / 3) * 4
175 */
176int
177CF_Codec_Base64_Encode (const unsigned char * bin,
178 const size_t len,
179 char * base64)
180{
181 const unsigned char * src = bin;
182 char * dst = base64;
183
184 ASSERT_ARGS (src == NULL);
185 ASSERT_ARGS (dst == NULL);
186
187#define SEXTUPLE_E_1(__x) (((__x[0]) >> 2) & 0x3f)
188#define SEXTUPLE_E_2(__x) (((__x[0]) << 4) & 0x30)
189#define SEXTUPLE_E_3(__x) (((__x[1]) >> 4) & 0x0f)
190#define SEXTUPLE_E_4(__x) (((__x[1]) << 2) & 0x3c)
191#define SEXTUPLE_E_5(__x) (((__x[2]) >> 6) & 0x03)
192#define SEXTUPLE_E_6(__x) (((__x[2]) ) & 0x3f)
193
194 for ( ; src - bin < len - 2 ; src += 3)
195 {
196 *dst++ = g_table_Base64Encode[SEXTUPLE_E_1 (src)];
197 *dst++ = g_table_Base64Encode[SEXTUPLE_E_2 (src)|
198 SEXTUPLE_E_3 (src)];
199 *dst++ = g_table_Base64Encode[SEXTUPLE_E_4 (src)|
200 SEXTUPLE_E_5 (src)];
201 *dst++ = g_table_Base64Encode[SEXTUPLE_E_6 (src)];
202 }
203
204 if (src - bin < len)
205 {
206 *dst++ = g_table_Base64Encode[SEXTUPLE_E_1 (src)];
207
208 if (src - bin == len - 1)
209 {
210 *dst++ = g_table_Base64Encode[SEXTUPLE_E_2 (src)];
211 *dst++ = g_table_Base64Encode[BASE64_PADDING_CHAR_INDEX];
212 }
213 else
214 {
215 *dst++ = g_table_Base64Encode[SEXTUPLE_E_2 (src)|
216 SEXTUPLE_E_3 (src)];
217 *dst++ = g_table_Base64Encode[SEXTUPLE_E_4 (src)];
218 }
219
220 *dst++ = g_table_Base64Encode[BASE64_PADDING_CHAR_INDEX];
221 }
222 *dst = '\0';
223
224 return CF_OK;
225}
226
227/**
228 * Base64-decode
229 *
230 * @return 성공 시, 디코딩된 바이너리 데이터의 길이; 실패 시, 오류 코드
231 *
232 * @param base64 base64 문자열
233 * @param bin 바이너리 데이터를 저장할 주소
234 *
235 * @remark
236 * base64는 할당된 메모리이며, 크기는 (strlen (base64)) / 4 * 3
237 */
238int
239CF_Codec_Base64_Decode (const char * base64,
240 unsigned char * bin)
241{
242 const char * src = base64;
243 unsigned char * dst = bin;
244 int remain = 0;
245 int binlen = 0;
246
247 ASSERT_ARGS (src == NULL);
248 ASSERT_ARGS (dst == NULL);
249
250 while (g_ascii_Base64Decode[(int)*src] < BASE64_PADDING_CHAR_INDEX) src++;
251
252 if (*src == 0xff)
253 return CF_ERROR_CODEC_NOT_BASE64;
254
255 remain = (int)(src - base64);
256 binlen = ((remain + 2/* max padding length */) / 4) * 3;
257
258#define SEXTUPLE_D_1(src) (g_ascii_Base64Decode[(int)src[0]] << 2)
259#define SEXTUPLE_D_2(src) (g_ascii_Base64Decode[(int)src[1]] >> 4)
260#define SEXTUPLE_D_3(src) (g_ascii_Base64Decode[(int)src[1]] << 4)
261#define SEXTUPLE_D_4(src) (g_ascii_Base64Decode[(int)src[2]] >> 2)
262#define SEXTUPLE_D_5(src) (g_ascii_Base64Decode[(int)src[2]] << 6)
263#define SEXTUPLE_D_6(src) (g_ascii_Base64Decode[(int)src[3]] )
264
265 for (src = base64 ; remain > 4 ; remain -= 4, src += 4)
266 {
267 *dst++ = (unsigned char)(SEXTUPLE_D_1 (src) | SEXTUPLE_D_2 (src));
268 *dst++ = (unsigned char)(SEXTUPLE_D_3 (src) | SEXTUPLE_D_4 (src));
269 *dst++ = (unsigned char)(SEXTUPLE_D_5 (src) | SEXTUPLE_D_6 (src));
270 }
271
272 if (remain > 1)
273 *dst++ = (unsigned char)(SEXTUPLE_D_1 (src) | SEXTUPLE_D_2 (src));
274 if (remain > 2)
275 *dst++ = (unsigned char)(SEXTUPLE_D_3 (src) | SEXTUPLE_D_4 (src));
276 if (remain > 3)
277 *dst++ = (unsigned char)(SEXTUPLE_D_5 (src) | SEXTUPLE_D_6 (src));
278
279 return binlen - (4 - remain);
280}
Note: See TracBrowser for help on using the repository browser.