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

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

#1 fix argument order

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