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 |
|
---|
15 | const 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 |
|
---|
35 | const 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 |
|
---|
48 | const 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 | * @remarks
|
---|
79 | * hex는 할당된 메모리이며, 크기는 '\0'를 제외하고 len * 2
|
---|
80 | */
|
---|
81 | int
|
---|
82 | CF_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 | * @param len 바이너리 데이터의 길이를 저장할 주소
|
---|
117 | *
|
---|
118 | * @remarks
|
---|
119 | * bin는 할당된 메모리이며, 크기는 strlen (hex) / 2
|
---|
120 | */
|
---|
121 | int
|
---|
122 | CF_Codec_Hex_Decode (const char * hex,
|
---|
123 | unsigned char * bin,
|
---|
124 | size_t * len)
|
---|
125 | {
|
---|
126 | size_t length = 0; /* absolutely even-number */
|
---|
127 | size_t iter = 0;
|
---|
128 | size_t binlen = 0;
|
---|
129 |
|
---|
130 | const char * ptr = hex;
|
---|
131 | char buf = 0;
|
---|
132 | unsigned char val = 0;
|
---|
133 | unsigned char asciiHex = 0;
|
---|
134 |
|
---|
135 | ASSERT_ARGS (hex == NULL);
|
---|
136 | ASSERT_ARGS (bin == NULL);
|
---|
137 | ASSERT_ARGS (len == NULL);
|
---|
138 |
|
---|
139 | *len = 0;
|
---|
140 |
|
---|
141 | for ( ; g_ascii_HexDecode[(int)*ptr] && *ptr ; ptr++, length++);
|
---|
142 |
|
---|
143 | if (*ptr)
|
---|
144 | return CF_ERROR_CODEC_NOT_HEXSTRING;
|
---|
145 |
|
---|
146 | binlen = length / 2;
|
---|
147 |
|
---|
148 | for (iter = 0, ptr = hex ; *ptr ; iter++)
|
---|
149 | {
|
---|
150 | val = 0; /* init/re-init docoding-buffer */
|
---|
151 |
|
---|
152 | /* decode one character */
|
---|
153 | #define DECODE_HEX(x) \
|
---|
154 | do { \
|
---|
155 | buf = *(x); \
|
---|
156 | val = (unsigned char)(val << 4); \
|
---|
157 | asciiHex = g_ascii_HexDecode[(int)buf]; \
|
---|
158 | \
|
---|
159 | val |= (unsigned char) \
|
---|
160 | (buf - asciiHex + (asciiHex == '0' ? 0 : 10)); \
|
---|
161 | } while (0)
|
---|
162 |
|
---|
163 | /* decode one byte by decode two character */
|
---|
164 | DECODE_HEX (ptr++);
|
---|
165 | DECODE_HEX (ptr++);
|
---|
166 |
|
---|
167 | bin[iter] = val;
|
---|
168 | }
|
---|
169 |
|
---|
170 | * len = binlen;
|
---|
171 |
|
---|
172 | return CF_OK;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Base64-encode
|
---|
177 | *
|
---|
178 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
179 | *
|
---|
180 | * @param bin 바이너리 데이터
|
---|
181 | * @param len 바이너리 데이터 길이
|
---|
182 | * @param base64 base64 문자열을 저장할 주소
|
---|
183 | *
|
---|
184 | * @remarks
|
---|
185 | * base64는 할당된 메모리이며, 크기는 '\0'를 제외하고 ((len + 2) / 3) * 4
|
---|
186 | */
|
---|
187 | int
|
---|
188 | CF_Codec_Base64_Encode (const unsigned char * bin,
|
---|
189 | const size_t len,
|
---|
190 | char * base64)
|
---|
191 | {
|
---|
192 | const unsigned char * src = bin;
|
---|
193 | char * dst = base64;
|
---|
194 |
|
---|
195 | ASSERT_ARGS (src == NULL);
|
---|
196 | ASSERT_ARGS (dst == NULL);
|
---|
197 |
|
---|
198 | #define SEXTUPLE_E_1(__x) (((__x[0]) >> 2) & 0x3f)
|
---|
199 | #define SEXTUPLE_E_2(__x) (((__x[0]) << 4) & 0x30)
|
---|
200 | #define SEXTUPLE_E_3(__x) (((__x[1]) >> 4) & 0x0f)
|
---|
201 | #define SEXTUPLE_E_4(__x) (((__x[1]) << 2) & 0x3c)
|
---|
202 | #define SEXTUPLE_E_5(__x) (((__x[2]) >> 6) & 0x03)
|
---|
203 | #define SEXTUPLE_E_6(__x) (((__x[2]) ) & 0x3f)
|
---|
204 |
|
---|
205 | for ( ; src - bin < len - 2 ; src += 3)
|
---|
206 | {
|
---|
207 | *dst++ = g_table_Base64Encode[SEXTUPLE_E_1 (src)];
|
---|
208 | *dst++ = g_table_Base64Encode[SEXTUPLE_E_2 (src)|
|
---|
209 | SEXTUPLE_E_3 (src)];
|
---|
210 | *dst++ = g_table_Base64Encode[SEXTUPLE_E_4 (src)|
|
---|
211 | SEXTUPLE_E_5 (src)];
|
---|
212 | *dst++ = g_table_Base64Encode[SEXTUPLE_E_6 (src)];
|
---|
213 | }
|
---|
214 |
|
---|
215 | if (src - bin < len)
|
---|
216 | {
|
---|
217 | *dst++ = g_table_Base64Encode[SEXTUPLE_E_1 (src)];
|
---|
218 |
|
---|
219 | if (src - bin == len - 1)
|
---|
220 | {
|
---|
221 | *dst++ = g_table_Base64Encode[SEXTUPLE_E_2 (src)];
|
---|
222 | *dst++ = g_table_Base64Encode[BASE64_PADDING_CHAR_INDEX];
|
---|
223 | }
|
---|
224 | else
|
---|
225 | {
|
---|
226 | *dst++ = g_table_Base64Encode[SEXTUPLE_E_2 (src)|
|
---|
227 | SEXTUPLE_E_3 (src)];
|
---|
228 | *dst++ = g_table_Base64Encode[SEXTUPLE_E_4 (src)];
|
---|
229 | }
|
---|
230 |
|
---|
231 | *dst++ = g_table_Base64Encode[BASE64_PADDING_CHAR_INDEX];
|
---|
232 | }
|
---|
233 | *dst = '\0';
|
---|
234 |
|
---|
235 | return CF_OK;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Base64-decode
|
---|
240 | *
|
---|
241 | * @return 성공 시, 디코딩된 바이너리 데이터의 길이; 실패 시, 오류 코드
|
---|
242 | *
|
---|
243 | * @param base64 base64 문자열
|
---|
244 | * @param bin 바이너리 데이터를 저장할 주소
|
---|
245 | * @param len 바이너리 데이터의 길이를 저장할 주소
|
---|
246 | *
|
---|
247 | * @remarks
|
---|
248 | * base64는 할당된 메모리이며, 크기는 (strlen (base64)) / 4 * 3
|
---|
249 | */
|
---|
250 | int
|
---|
251 | CF_Codec_Base64_Decode (const char * base64,
|
---|
252 | unsigned char * bin,
|
---|
253 | size_t * len)
|
---|
254 | {
|
---|
255 | const char * src = base64;
|
---|
256 | unsigned char * dst = bin;
|
---|
257 | int remain = 0;
|
---|
258 | int binlen = 0;
|
---|
259 |
|
---|
260 | ASSERT_ARGS (src == NULL);
|
---|
261 | ASSERT_ARGS (dst == NULL);
|
---|
262 | ASSERT_ARGS (len == NULL);
|
---|
263 |
|
---|
264 | *len = 0;
|
---|
265 |
|
---|
266 | while (g_ascii_Base64Decode[(int)*src] < BASE64_PADDING_CHAR_INDEX) src++;
|
---|
267 |
|
---|
268 | if (*src == 0xff)
|
---|
269 | return CF_ERROR_CODEC_NOT_BASE64;
|
---|
270 |
|
---|
271 | remain = (int)(src - base64);
|
---|
272 | binlen = ((remain + 2/* max padding length */) / 4) * 3;
|
---|
273 |
|
---|
274 | #define SEXTUPLE_D_1(src) (g_ascii_Base64Decode[(int)src[0]] << 2)
|
---|
275 | #define SEXTUPLE_D_2(src) (g_ascii_Base64Decode[(int)src[1]] >> 4)
|
---|
276 | #define SEXTUPLE_D_3(src) (g_ascii_Base64Decode[(int)src[1]] << 4)
|
---|
277 | #define SEXTUPLE_D_4(src) (g_ascii_Base64Decode[(int)src[2]] >> 2)
|
---|
278 | #define SEXTUPLE_D_5(src) (g_ascii_Base64Decode[(int)src[2]] << 6)
|
---|
279 | #define SEXTUPLE_D_6(src) (g_ascii_Base64Decode[(int)src[3]] )
|
---|
280 |
|
---|
281 | for (src = base64 ; remain > 4 ; remain -= 4, src += 4)
|
---|
282 | {
|
---|
283 | *dst++ = (unsigned char)(SEXTUPLE_D_1 (src) | SEXTUPLE_D_2 (src));
|
---|
284 | *dst++ = (unsigned char)(SEXTUPLE_D_3 (src) | SEXTUPLE_D_4 (src));
|
---|
285 | *dst++ = (unsigned char)(SEXTUPLE_D_5 (src) | SEXTUPLE_D_6 (src));
|
---|
286 | }
|
---|
287 |
|
---|
288 | if (remain > 1)
|
---|
289 | *dst++ = (unsigned char)(SEXTUPLE_D_1 (src) | SEXTUPLE_D_2 (src));
|
---|
290 | if (remain > 2)
|
---|
291 | *dst++ = (unsigned char)(SEXTUPLE_D_3 (src) | SEXTUPLE_D_4 (src));
|
---|
292 | if (remain > 3)
|
---|
293 | *dst++ = (unsigned char)(SEXTUPLE_D_5 (src) | SEXTUPLE_D_6 (src));
|
---|
294 |
|
---|
295 | return binlen - (4 - remain);
|
---|
296 | }
|
---|