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