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
RevLine 
[66]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
[85]11#define ASSERT_ARGS(x) \
12 if ((x)) \
[66]13 return CF_ERROR_CODEC_INVALID_ARGS
14
[88]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 */
[89]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 */
[88]21 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 80 - 95 */
[89]22 0x00, 'a', 'a', 'a', 'a', 'a', 'a',0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 96 - 111 */
[88]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};
[66]34
[88]35const static char g_table_Base64Encode[] = {
[90]36#define BASE64_PADDING_CHAR_INDEX 64
[88]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};
[72]47
[88]48const static unsigned char g_ascii_Base64Decode[] = {
[89]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 */
[88]57 /* end of ascii character */
[89]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 */
[88]66};
67
68
[66]69/**
[88]70 * hex-encode
[66]71 *
72 * @return 성공 시, CF_OK; 실패 시, 오류 코드
73 *
74 * @param bin 바이너리 데이터
75 * @param len 바이너리 데이터 길이
76 * @param hex 16진수 문자열을 저장할 주소
77 *
[73]78 * @remark
[91]79 * hex는 할당된 메모리이며, 크기는 '\0'를 제외하고 len * 2
[66]80 */
81int
[86]82CF_Codec_Hex_Encode (const unsigned char * bin,
83 const size_t len,
84 char * hex)
[66]85{
[88]86 size_t iter = 0;
87 size_t hexlen = len * 2 + 1;
[66]88
[88]89 const unsigned char * ptr = bin;
[66]90
[88]91 const static char hexchar[] = {'0', '1', '2', '3',
92 '4', '5', '6', '7',
93 '8', '9', 'a', 'b',
94 'c', 'd', 'e', 'f'};
[66]95
96 ASSERT_ARGS (bin == NULL);
97 ASSERT_ARGS (hex == NULL);
98
[88]99 for (iter = 0 ; iter < hexlen ; iter += 2, ptr++)
[66]100 {
101 hex[iter ] = hexchar[((*(ptr)) >> 4) & 0x0f];
102 hex[iter + 1] = hexchar[((*(ptr)) ) & 0x0f];
103 }
[88]104 hex[hexlen - 1] = '\0';
[66]105
106 return CF_OK;
107}
108
109/**
[88]110 * hex-decode
[66]111 *
[90]112 * @return 성공 시, 디코딩된 바이너리 데이터의 길이; 실패 시, 오류 코드
[66]113 *
114 * @param hex 16진수 문자열
115 * @param bin 바이너리 데이터를 저장할 주소
116 *
[73]117 * @remark
[88]118 * bin는 할당된 메모리이며, 크기는 strlen (hex) / 2
[66]119 */
120int
[86]121CF_Codec_Hex_Decode (const char * hex,
[90]122 unsigned char * bin)
[66]123{
[88]124 size_t length = strlen (hex); /* absolutely even-number */
[66]125 size_t iter = 0;
[90]126 int binlen = (int)length / 2;
[66]127
[88]128 const char * ptr = hex;
129 char buf = 0;
130 unsigned char val = 0;
131 unsigned char asciiHex = 0;
[66]132
133 ASSERT_ARGS (hex == NULL);
134 ASSERT_ARGS (bin == NULL);
135
[88]136 for (iter = 0 ; iter < binlen ; iter++)
[66]137 {
[88]138 val = 0; /* init/re-init docoding-buffer */
[66]139
[88]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) \
[89]149 (buf - asciiHex + (asciiHex == '0' ? 0 : 10)); \
[88]150 else \
151 return CF_ERROR_CODEC_NOT_HEXSTRING; \
152 } while (0)
[66]153
[88]154 /* decode one byte by decode two character */
155 DECODE_HEX (ptr++);
156 DECODE_HEX (ptr++);
157
[66]158 bin[iter] = val;
159 }
160
[90]161 return binlen;
[66]162}
[88]163
164/**
165 * Base64-encode
166 *
167 * @return 성공 시, CF_OK; 실패 시, 오류 코드
168 *
169 * @param bin 바이너리 데이터
170 * @param len 바이너리 데이터 길이
171 * @param base64 base64 문자열을 저장할 주소
172 *
173 * @remark
[91]174 * base64는 할당된 메모리이며, 크기는 '\0'를 제외하고 ((len + 2) / 3) * 4
[88]175 */
176int
177CF_Codec_Base64_Encode (const unsigned char * bin,
178 const size_t len,
179 char * base64)
180{
[90]181 const unsigned char * src = bin;
182 char * dst = base64;
[88]183
[90]184 ASSERT_ARGS (src == NULL);
185 ASSERT_ARGS (dst == NULL);
[88]186
[90]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)
[88]193
[90]194 for ( ; src - bin < len - 2 ; src += 3)
[88]195 {
[90]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)];
[88]202 }
203
[90]204 if (src - bin < len)
[88]205 {
[90]206 *dst++ = g_table_Base64Encode[SEXTUPLE_E_1 (src)];
[88]207
[90]208 if (src - bin == len - 1)
[88]209 {
[91]210 *dst++ = g_table_Base64Encode[SEXTUPLE_E_2 (src)];
[90]211 *dst++ = g_table_Base64Encode[BASE64_PADDING_CHAR_INDEX];
[88]212 }
213 else
214 {
[91]215 *dst++ = g_table_Base64Encode[SEXTUPLE_E_2 (src)|
216 SEXTUPLE_E_3 (src)];
217 *dst++ = g_table_Base64Encode[SEXTUPLE_E_4 (src)];
[88]218 }
219
[90]220 *dst++ = g_table_Base64Encode[BASE64_PADDING_CHAR_INDEX];
[88]221 }
[90]222 *dst = '\0';
[88]223
224 return CF_OK;
225}
226
227/**
228 * Base64-decode
229 *
[90]230 * @return 성공 시, 디코딩된 바이너리 데이터의 길이; 실패 시, 오류 코드
[88]231 *
232 * @param base64 base64 문자열
233 * @param bin 바이너리 데이터를 저장할 주소
234 *
235 * @remark
[91]236 * base64는 할당된 메모리이며, 크기는 (strlen (base64)) / 4 * 3
[88]237 */
238int
239CF_Codec_Base64_Decode (const char * base64,
[90]240 unsigned char * bin)
[88]241{
242 const char * src = base64;
243 unsigned char * dst = bin;
[91]244 int remain = 0;
[90]245 int binlen = 0;
[88]246
[90]247 ASSERT_ARGS (src == NULL);
248 ASSERT_ARGS (dst == NULL);
[88]249
[90]250 while (g_ascii_Base64Decode[(int)*src] < BASE64_PADDING_CHAR_INDEX) src++;
[88]251
[91]252 if (*src == 0xff)
[88]253 return CF_ERROR_CODEC_NOT_BASE64;
254
[90]255 remain = (int)(src - base64);
[91]256 binlen = ((remain + 2/* max padding length */) / 4) * 3;
[88]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
[91]265 for (src = base64 ; remain > 4 ; remain -= 4, src += 4)
[88]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
[92]279 return binlen - (4 - remain);
[88]280}
Note: See TracBrowser for help on using the repository browser.