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

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

#1 fix bug on codec and arrange file flag

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 + 1
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는 할당된 메모리이며, 크기는 ((len + 2) / 3) * 4
175 * (null-character 제외)
176 */
177int
178CF_Codec_Base64_Encode (const unsigned char * bin,
179 const size_t len,
180 char * base64)
181{
182 const unsigned char * src = bin;
183 char * dst = base64;
184
185 ASSERT_ARGS (src == NULL);
186 ASSERT_ARGS (dst == NULL);
187
188#define SEXTUPLE_E_1(__x) (((__x[0]) >> 2) & 0x3f)
189#define SEXTUPLE_E_2(__x) (((__x[0]) << 4) & 0x30)
190#define SEXTUPLE_E_3(__x) (((__x[1]) >> 4) & 0x0f)
191#define SEXTUPLE_E_4(__x) (((__x[1]) << 2) & 0x3c)
192#define SEXTUPLE_E_5(__x) (((__x[2]) >> 6) & 0x03)
193#define SEXTUPLE_E_6(__x) (((__x[2]) ) & 0x3f)
194
195 for ( ; src - bin < len - 2 ; src += 3)
196 {
197 *dst++ = g_table_Base64Encode[SEXTUPLE_E_1 (src)];
198 *dst++ = g_table_Base64Encode[SEXTUPLE_E_2 (src)|
199 SEXTUPLE_E_3 (src)];
200 *dst++ = g_table_Base64Encode[SEXTUPLE_E_4 (src)|
201 SEXTUPLE_E_5 (src)];
202 *dst++ = g_table_Base64Encode[SEXTUPLE_E_6 (src)];
203 }
204
205 if (src - bin < len)
206 {
207 *dst++ = g_table_Base64Encode[SEXTUPLE_E_1 (src)];
208
209 if (src - bin == len - 1)
210 {
211 *dst++ = g_table_Base64Encode[SEXTUPLE_E_2 (bin)];
212 *dst++ = g_table_Base64Encode[BASE64_PADDING_CHAR_INDEX];
213 }
214 else
215 {
216 *dst++ = g_table_Base64Encode[SEXTUPLE_E_2 (bin)|
217 SEXTUPLE_E_3 (bin)];
218 *dst++ = g_table_Base64Encode[SEXTUPLE_E_4 (bin)];
219 }
220
221 *dst++ = g_table_Base64Encode[BASE64_PADDING_CHAR_INDEX];
222 }
223 *dst = '\0';
224
225 return CF_OK;
226}
227
228/**
229 * Base64-decode
230 *
231 * @return 성공 시, 디코딩된 바이너리 데이터의 길이; 실패 시, 오류 코드
232 *
233 * @param base64 base64 문자열
234 * @param bin 바이너리 데이터를 저장할 주소
235 *
236 * @remark
237 * base64는 할당된 메모리이며, 크기는 (strlen (base64) - <패딩 길이>) / 4 * 3
238 */
239int
240CF_Codec_Base64_Decode (const char * base64,
241 unsigned char * bin)
242{
243 int remain = 0;
244 const char * src = base64;
245 unsigned char * dst = bin;
246 int binlen = 0;
247
248 ASSERT_ARGS (src == NULL);
249 ASSERT_ARGS (dst == NULL);
250
251 while (g_ascii_Base64Decode[(int)*src] < BASE64_PADDING_CHAR_INDEX) src++;
252
253 if (*src)
254 return CF_ERROR_CODEC_NOT_BASE64;
255
256 remain = (int)(src - base64);
257 binlen = ((remain + 3) / 4) * 3;
258
259#define SEXTUPLE_D_1(src) (g_ascii_Base64Decode[(int)src[0]] << 2)
260#define SEXTUPLE_D_2(src) (g_ascii_Base64Decode[(int)src[1]] >> 4)
261#define SEXTUPLE_D_3(src) (g_ascii_Base64Decode[(int)src[1]] << 4)
262#define SEXTUPLE_D_4(src) (g_ascii_Base64Decode[(int)src[2]] >> 2)
263#define SEXTUPLE_D_5(src) (g_ascii_Base64Decode[(int)src[2]] << 6)
264#define SEXTUPLE_D_6(src) (g_ascii_Base64Decode[(int)src[3]] )
265
266 for (src = base64, dst = bin ; remain > 4 ; remain -= 4, src += 4)
267 {
268 *dst++ = (unsigned char)(SEXTUPLE_D_1 (src) | SEXTUPLE_D_2 (src));
269 *dst++ = (unsigned char)(SEXTUPLE_D_3 (src) | SEXTUPLE_D_4 (src));
270 *dst++ = (unsigned char)(SEXTUPLE_D_5 (src) | SEXTUPLE_D_6 (src));
271 }
272
273 if (remain > 1)
274 *dst++ = (unsigned char)(SEXTUPLE_D_1 (src) | SEXTUPLE_D_2 (src));
275 if (remain > 2)
276 *dst++ = (unsigned char)(SEXTUPLE_D_3 (src) | SEXTUPLE_D_4 (src));
277 if (remain > 3)
278 *dst++ = (unsigned char)(SEXTUPLE_D_5 (src) | SEXTUPLE_D_6 (src));
279
280 return binlen;
281}
Note: See TracBrowser for help on using the repository browser.