[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] | 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 */
|
---|
[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] | 35 | const 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] | 48 | const 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 바이너리 데이터
|
---|
[96] | 75 | * @param len 바이너리 데이터의 길이
|
---|
[66] | 76 | * @param hex 16진수 문자열을 저장할 주소
|
---|
| 77 | *
|
---|
[116] | 78 | * @remarks
|
---|
[91] | 79 | * hex는 할당된 메모리이며, 크기는 '\0'를 제외하고 len * 2
|
---|
[66] | 80 | */
|
---|
| 81 | int
|
---|
[86] | 82 | CF_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 바이너리 데이터를 저장할 주소
|
---|
[96] | 116 | * @param len 바이너리 데이터의 길이를 저장할 주소
|
---|
[66] | 117 | *
|
---|
[116] | 118 | * @remarks
|
---|
[88] | 119 | * bin는 할당된 메모리이며, 크기는 strlen (hex) / 2
|
---|
[66] | 120 | */
|
---|
| 121 | int
|
---|
[86] | 122 | CF_Codec_Hex_Decode (const char * hex,
|
---|
[96] | 123 | unsigned char * bin,
|
---|
| 124 | size_t * len)
|
---|
[66] | 125 | {
|
---|
[96] | 126 | size_t length = 0; /* absolutely even-number */
|
---|
[66] | 127 | size_t iter = 0;
|
---|
[96] | 128 | size_t binlen = 0;
|
---|
[66] | 129 |
|
---|
[88] | 130 | const char * ptr = hex;
|
---|
| 131 | char buf = 0;
|
---|
| 132 | unsigned char val = 0;
|
---|
| 133 | unsigned char asciiHex = 0;
|
---|
[66] | 134 |
|
---|
| 135 | ASSERT_ARGS (hex == NULL);
|
---|
| 136 | ASSERT_ARGS (bin == NULL);
|
---|
[96] | 137 | ASSERT_ARGS (len == NULL);
|
---|
[66] | 138 |
|
---|
[96] | 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++)
|
---|
[66] | 149 | {
|
---|
[88] | 150 | val = 0; /* init/re-init docoding-buffer */
|
---|
[66] | 151 |
|
---|
[88] | 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 | \
|
---|
[96] | 159 | val |= (unsigned char) \
|
---|
| 160 | (buf - asciiHex + (asciiHex == '0' ? 0 : 10)); \
|
---|
[88] | 161 | } while (0)
|
---|
[66] | 162 |
|
---|
[88] | 163 | /* decode one byte by decode two character */
|
---|
| 164 | DECODE_HEX (ptr++);
|
---|
| 165 | DECODE_HEX (ptr++);
|
---|
| 166 |
|
---|
[66] | 167 | bin[iter] = val;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[96] | 170 | * len = binlen;
|
---|
| 171 |
|
---|
| 172 | return CF_OK;
|
---|
[66] | 173 | }
|
---|
[88] | 174 |
|
---|
| 175 | /**
|
---|
| 176 | * Base64-encode
|
---|
| 177 | *
|
---|
| 178 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 179 | *
|
---|
| 180 | * @param bin 바이너리 데이터
|
---|
| 181 | * @param len 바이너리 데이터 길이
|
---|
| 182 | * @param base64 base64 문자열을 저장할 주소
|
---|
| 183 | *
|
---|
[116] | 184 | * @remarks
|
---|
[91] | 185 | * base64는 할당된 메모리이며, 크기는 '\0'를 제외하고 ((len + 2) / 3) * 4
|
---|
[88] | 186 | */
|
---|
| 187 | int
|
---|
| 188 | CF_Codec_Base64_Encode (const unsigned char * bin,
|
---|
| 189 | const size_t len,
|
---|
| 190 | char * base64)
|
---|
| 191 | {
|
---|
[90] | 192 | const unsigned char * src = bin;
|
---|
| 193 | char * dst = base64;
|
---|
[88] | 194 |
|
---|
[90] | 195 | ASSERT_ARGS (src == NULL);
|
---|
| 196 | ASSERT_ARGS (dst == NULL);
|
---|
[88] | 197 |
|
---|
[90] | 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)
|
---|
[88] | 204 |
|
---|
[90] | 205 | for ( ; src - bin < len - 2 ; src += 3)
|
---|
[88] | 206 | {
|
---|
[90] | 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)];
|
---|
[88] | 213 | }
|
---|
| 214 |
|
---|
[90] | 215 | if (src - bin < len)
|
---|
[88] | 216 | {
|
---|
[90] | 217 | *dst++ = g_table_Base64Encode[SEXTUPLE_E_1 (src)];
|
---|
[88] | 218 |
|
---|
[90] | 219 | if (src - bin == len - 1)
|
---|
[88] | 220 | {
|
---|
[91] | 221 | *dst++ = g_table_Base64Encode[SEXTUPLE_E_2 (src)];
|
---|
[90] | 222 | *dst++ = g_table_Base64Encode[BASE64_PADDING_CHAR_INDEX];
|
---|
[88] | 223 | }
|
---|
| 224 | else
|
---|
| 225 | {
|
---|
[91] | 226 | *dst++ = g_table_Base64Encode[SEXTUPLE_E_2 (src)|
|
---|
| 227 | SEXTUPLE_E_3 (src)];
|
---|
| 228 | *dst++ = g_table_Base64Encode[SEXTUPLE_E_4 (src)];
|
---|
[88] | 229 | }
|
---|
| 230 |
|
---|
[90] | 231 | *dst++ = g_table_Base64Encode[BASE64_PADDING_CHAR_INDEX];
|
---|
[88] | 232 | }
|
---|
[90] | 233 | *dst = '\0';
|
---|
[88] | 234 |
|
---|
| 235 | return CF_OK;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | /**
|
---|
| 239 | * Base64-decode
|
---|
| 240 | *
|
---|
[90] | 241 | * @return 성공 시, 디코딩된 바이너리 데이터의 길이; 실패 시, 오류 코드
|
---|
[88] | 242 | *
|
---|
| 243 | * @param base64 base64 문자열
|
---|
| 244 | * @param bin 바이너리 데이터를 저장할 주소
|
---|
[96] | 245 | * @param len 바이너리 데이터의 길이를 저장할 주소
|
---|
[88] | 246 | *
|
---|
[116] | 247 | * @remarks
|
---|
[91] | 248 | * base64는 할당된 메모리이며, 크기는 (strlen (base64)) / 4 * 3
|
---|
[88] | 249 | */
|
---|
| 250 | int
|
---|
| 251 | CF_Codec_Base64_Decode (const char * base64,
|
---|
[96] | 252 | unsigned char * bin,
|
---|
| 253 | size_t * len)
|
---|
[88] | 254 | {
|
---|
| 255 | const char * src = base64;
|
---|
| 256 | unsigned char * dst = bin;
|
---|
[91] | 257 | int remain = 0;
|
---|
[90] | 258 | int binlen = 0;
|
---|
[88] | 259 |
|
---|
[90] | 260 | ASSERT_ARGS (src == NULL);
|
---|
| 261 | ASSERT_ARGS (dst == NULL);
|
---|
[96] | 262 | ASSERT_ARGS (len == NULL);
|
---|
[88] | 263 |
|
---|
[96] | 264 | *len = 0;
|
---|
| 265 |
|
---|
[90] | 266 | while (g_ascii_Base64Decode[(int)*src] < BASE64_PADDING_CHAR_INDEX) src++;
|
---|
[88] | 267 |
|
---|
[91] | 268 | if (*src == 0xff)
|
---|
[88] | 269 | return CF_ERROR_CODEC_NOT_BASE64;
|
---|
| 270 |
|
---|
[90] | 271 | remain = (int)(src - base64);
|
---|
[91] | 272 | binlen = ((remain + 2/* max padding length */) / 4) * 3;
|
---|
[88] | 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 |
|
---|
[91] | 281 | for (src = base64 ; remain > 4 ; remain -= 4, src += 4)
|
---|
[88] | 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 |
|
---|
[92] | 295 | return binlen - (4 - remain);
|
---|
[88] | 296 | }
|
---|