Index: trunk/src/cf_codec.c
===================================================================
--- trunk/src/cf_codec.c	(revision 129)
+++ trunk/src/cf_codec.c	(revision 151)
@@ -13,62 +13,10 @@
 #include <stdio.h>
 
+#define BASE64_PADDING_CHAR_INDEX	64
+
 #define ASSERT_ARGS(x)	\
 	if ((x))			\
 		return CF_ERROR_CODEC_INVALID_ARGS
 
-const static cf_byte g_ascii_HexDecode[] = {
-	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/*  00 -  15 */
-	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/*  16 -  31 */
-	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/*  32 -  47 */
-	 '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',0x00,0x00,0x00,0x00,0x00,0x00,	/*  48 -  63 */
-	0x00, 'A', 'A', 'A', 'A', 'A', 'A',0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/*  64 -  79 */
-	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/*  80 -  95 */
-	0x00, 'a', 'a', 'a', 'a', 'a', 'a',0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/*  96 - 111 */
-	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/* 112 - 127 */
-	/* end of ascii character */
-	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/* 128 - 143 */
-	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/* 144 - 159 */
-	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/* 160 - 175 */
-	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/* 176 - 191 */
-	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/* 192 - 207 */
-	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/* 208 - 223 */
-	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/* 224 - 239 */
-	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/* 240 - 255 */
-};
-
-const static char g_table_Base64Encode[] = {
-#define BASE64_PADDING_CHAR_INDEX	64
-	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',		/* 00 - 07 */
-	'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',		/* 08 - 15 */
-	'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',		/* 16 - 23 */
-	'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',		/* 24 - 31 */
-	'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',		/* 32 - 39 */
-	'o', 'p', 'q', 'r', 's', 't', 'u', 'v',		/* 40 - 47 */
-	'w', 'x', 'y', 'z', '0', '1', '2', '3',		/* 48 - 55 */
-	'4', '5', '6', '7', '8', '9', '+', '/',		/* 56 - 63 */
-	'='											/* padding */
-};
-
-const static cf_byte g_ascii_Base64Decode[] = {
-	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,	/*  00 -  15 */
-	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,	/*  16 -  31 */
-	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,  62,0xff,0xff,0xff,  63,	/*  32 -  47 */
-	  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,0xff,0xff,0xff,  64,0xff,0xff,	/*  48 -  63 */
-	0xff,   0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,	/*  64 -  79 */
-	  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,0xff,0xff,0xff,0xff,0xff,	/*  80 -  95 */
-	0xff,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,	/*  96 - 111 */
-	  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,0xff,0xff,0xff,0xff,0xff,	/* 112 - 127 */
-	/* end of ascii character */
-	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,	/* 128 - 143 */
-	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,	/* 144 - 159 */
-	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,	/* 160 - 175 */
-	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,	/* 176 - 191 */
-	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,	/* 192 - 207 */
-	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,	/* 208 - 223 */
-	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,	/* 224 - 239 */
-	0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,	/* 240 - 255 */
-};
-
-
 /**
  * hex-encode
@@ -88,23 +36,21 @@
 					 char			* hex)
 {
-	size_t	iter = 0;
-	size_t	hexlen = len * 2 + 1;
-
-	const cf_byte	* ptr = bin;
-
-	const static char	hexchar[] = {'0', '1', '2', '3',
-									 '4', '5', '6', '7',
-									 '8', '9', 'a', 'b',
-									 'c', 'd', 'e', 'f'};
+	const cf_byte	* src = bin;
+	char			* dst = hex;
+
+	const static char	hexenc[] = {
+		'0', '1', '2', '3', '4', '5', '6', '7',
+		'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
+	};
 
 	ASSERT_ARGS (bin == NULL);
 	ASSERT_ARGS (hex == NULL);
 
-	for (iter = 0 ; iter < hexlen ; iter += 2, ptr++)
-	{
-		hex[iter    ] = hexchar[((*(ptr)) >> 4) & 0x0f];
-		hex[iter + 1] = hexchar[((*(ptr))     ) & 0x0f];
-	}
-	hex[hexlen - 1] = '\0';
+	for ( ; *src ; src++)
+	{
+		*dst++ = hexenc[((*src) >> 4) & 0x0f];
+		*dst++ = hexenc[((*src)     ) & 0x0f];
+	}
+	*dst = '\0';
 
 	return CF_OK;
@@ -114,5 +60,5 @@
  * hex-decode
  *
- * \return 성공 시, 디코딩된 바이너리 데이터의 길이; 실패 시, 오류 코드
+ * \return 성공 시, CF_OK; 실패 시, 오류 코드
  *
  * \param hex	16진수 문자열
@@ -129,48 +75,61 @@
 {
 	size_t		length = 0;	/* absolutely even-number */
-	size_t		iter = 0;
-	size_t		binlen = 0;
-
-	const char	* ptr = hex;
+
+	const char	* src = hex;
+	cf_byte		* dst = bin;
 	char		buf = 0;
 	cf_byte		val = 0;
 	cf_byte		asciiHex = 0;
 
+	const static cf_byte hexdec[] = {
+		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/*  00 -  07 */
+		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/*  08 -  15 */
+		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/*  16 -  23 */
+		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/*  24 -  31 */
+		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/*  32 -  39 */
+		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/*  40 -  47 */
+		 '0', '0', '0', '0', '0', '0', '0', '0',	/*  48 -  55 */
+		 '0', '0',0x00,0x00,0x00,0x00,0x00,0x00,	/*  56 -  63 */
+		0x00, 'A', 'A', 'A', 'A', 'A', 'A',0x00,	/*  64 -  71 */
+		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/*  71 -  79 */
+		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/*  80 -  87 */
+		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/*  88 -  95 */
+		0x00, 'a', 'a', 'a', 'a', 'a', 'a',0x00,	/*  96 - 103 */
+		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/* 104 - 111 */
+		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,	/* 112 - 119 */
+		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00		/* 120 - 127 */
+	};
+
 	ASSERT_ARGS (hex == NULL);
 	ASSERT_ARGS (bin == NULL);
 	ASSERT_ARGS (len == NULL);
 
-	*len = 0;
-
-	for ( ; g_ascii_HexDecode[(int)*ptr] && *ptr ; ptr++, length++);
-
-	if (*ptr)
+	for ( ; hexdec[(int)*src] && *src ; src++, length++);
+
+	if (*src)
 		return CF_ERROR_CODEC_NOT_HEXSTRING;
 
-	binlen = length / 2;
-
-	for (iter = 0, ptr = hex ; *ptr ; iter++)
+	for (src = hex ; *src ; )
 	{
 		val = 0;	/* init/re-init docoding-buffer */
 
 		/* decode one character */
-#define DECODE_HEX(x)								\
-		do {										\
-		buf = *(x);									\
-		val = (cf_byte)(val << 4);			\
-		asciiHex = g_ascii_HexDecode[(int)buf];		\
-													\
-		val |=	(cf_byte)						\
+#define DECODE_HEX(x)					\
+		do {							\
+		buf = *(x);						\
+		val = (cf_byte)(val << 4);		\
+		asciiHex = hexdec[(int)buf];	\
+		\
+		val |=	(cf_byte)				\
 				(buf - asciiHex + (asciiHex == '0' ? 0 : 10));	\
 		} while (0)
 
 		/* decode one byte by decode two character */
-		DECODE_HEX (ptr++);
-		DECODE_HEX (ptr++);
-
-		bin[iter] = val;
-	}
-
-	* len = binlen;
+		DECODE_HEX (src++);
+		DECODE_HEX (src++);
+
+		*dst++ = val;
+	}
+	*len = length / 2;
 
 	return CF_OK;
@@ -196,4 +155,16 @@
 	const cf_byte	* src = bin;
 	char			* dst = base64;
+
+	const static char base64enc[] = {
+		'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',	/* 00 - 07 */
+		'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',	/* 08 - 15 */
+		'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',	/* 16 - 23 */
+		'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',	/* 24 - 31 */
+		'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',	/* 32 - 39 */
+		'o', 'p', 'q', 'r', 's', 't', 'u', 'v',	/* 40 - 47 */
+		'w', 'x', 'y', 'z', '0', '1', '2', '3',	/* 48 - 55 */
+		'4', '5', '6', '7', '8', '9', '+', '/',	/* 56 - 63 */
+		'='										/* padding */
+	};
 
 	ASSERT_ARGS (src == NULL);
@@ -207,31 +178,28 @@
 #define SEXTUPLE_E_6(__x)	(((__x[2])     ) & 0x3f)
 
-	for ( ; src - bin < len - 2 ; src += 3)
-	{
-		*dst++ = g_table_Base64Encode[SEXTUPLE_E_1 (src)];
-		*dst++ = g_table_Base64Encode[SEXTUPLE_E_2 (src)|
-									  SEXTUPLE_E_3 (src)];
-		*dst++ = g_table_Base64Encode[SEXTUPLE_E_4 (src)|
-									  SEXTUPLE_E_5 (src)];
-		*dst++ = g_table_Base64Encode[SEXTUPLE_E_6 (src)];
-	}
-
-	if (src - bin < len)
-	{
-		*dst++ = g_table_Base64Encode[SEXTUPLE_E_1 (src)];
+	for ( ; (size_t)(src - bin) < len - 2 ; src += 3)
+	{
+		*dst++ = base64enc[SEXTUPLE_E_1 (src)];
+		*dst++ = base64enc[SEXTUPLE_E_2 (src) | SEXTUPLE_E_3 (src)];
+		*dst++ = base64enc[SEXTUPLE_E_4 (src) | SEXTUPLE_E_5 (src)];
+		*dst++ = base64enc[SEXTUPLE_E_6 (src)];
+	}
+
+	if ((size_t)(src - bin) < len)
+	{
+		*dst++ = base64enc[SEXTUPLE_E_1 (src)];
 
 		if (src - bin == len - 1)
 		{
-			*dst++ = g_table_Base64Encode[SEXTUPLE_E_2 (src)];
-			*dst++ = g_table_Base64Encode[BASE64_PADDING_CHAR_INDEX];
+			*dst++ = base64enc[SEXTUPLE_E_2 (src)];
+			*dst++ = base64enc[BASE64_PADDING_CHAR_INDEX];
 		}
 		else
 		{
-			*dst++ = g_table_Base64Encode[SEXTUPLE_E_2 (src)|
-										  SEXTUPLE_E_3 (src)];
-			*dst++ = g_table_Base64Encode[SEXTUPLE_E_4 (src)];
+			*dst++ = base64enc[SEXTUPLE_E_2 (src) | SEXTUPLE_E_3 (src)];
+			*dst++ = base64enc[SEXTUPLE_E_4 (src)];
 		}
 
-		*dst++ = g_table_Base64Encode[BASE64_PADDING_CHAR_INDEX];
+		*dst++ = base64enc[BASE64_PADDING_CHAR_INDEX];
 	}
 	*dst = '\0';
@@ -243,5 +211,5 @@
  * Base64-decode
  *
- * \return 성공 시, 디코딩된 바이너리 데이터의 길이; 실패 시, 오류 코드
+ * \return 성공 시, CF_OK; 실패 시, 오류 코드
  *
  * \param base64	base64 문자열
@@ -259,6 +227,25 @@
 	const char	* src = base64;
 	cf_byte		* dst = bin;
-	int			remain = 0;
-	int			binlen = 0;
+	size_t		remain = 0;
+	size_t		binlen = 0;
+
+	const static cf_byte base64dec[] = {
+		0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,	/*  00 -  07 */
+		0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,	/*  08 -  15 */
+		0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,	/*  16 -  23 */
+		0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,	/*  24 -  31 */
+		0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,	/*  32 -  39 */
+		0xff,0xff,0xff,  62,0xff,0xff,0xff,  63,	/*  40 -  47 */
+		  52,  53,  54,  55,  56,  57,  58,  59,	/*  48 -  55 */
+		  60,  61,0xff,0xff,0xff,  64,0xff,0xff,	/*  56 -  63 */
+		0xff,   0,   1,   2,   3,   4,   5,   6,	/*  64 -  71 */
+	 	   7,   8,   9,  10,  11,  12,  13,  14,	/*  71 -  79 */
+		  15,  16,  17,  18,  19,  20,  21,  22,	/*  80 -  87 */
+		  23,  24,  25,0xff,0xff,0xff,0xff,0xff,	/*  88 -  95 */
+		0xff,  26,  27,  28,  29,  30,  31,  32,	/*  96 - 103 */
+	  	  33,  34,  35,  36,  37,  38,  39,  40,	/* 104 - 111 */
+		  41,  42,  43,  44,  45,  46,  47,  48,	/* 112 - 119 */
+		  49,  50,  51,0xff,0xff,0xff,0xff,0xff		/* 120 - 127 */
+	};
 
 	ASSERT_ARGS (src == NULL);
@@ -266,20 +253,18 @@
 	ASSERT_ARGS (len == NULL);
 
-	*len = 0;
-
-	while (g_ascii_Base64Decode[(int)*src] < BASE64_PADDING_CHAR_INDEX) src++;
+	while (base64dec[(int)*src] < BASE64_PADDING_CHAR_INDEX) src++;
 
 	if (*src == 0xff)
 		return CF_ERROR_CODEC_NOT_BASE64;
 
-	remain = (int)(src - base64);
+	remain = (size_t)(src - base64);
 	binlen = ((remain + 2/* max padding length */) / 4) * 3;
 
-#define SEXTUPLE_D_1(src)	(g_ascii_Base64Decode[(int)src[0]] << 2)
-#define SEXTUPLE_D_2(src)	(g_ascii_Base64Decode[(int)src[1]] >> 4)
-#define SEXTUPLE_D_3(src)	(g_ascii_Base64Decode[(int)src[1]] << 4)
-#define SEXTUPLE_D_4(src)	(g_ascii_Base64Decode[(int)src[2]] >> 2)
-#define SEXTUPLE_D_5(src)	(g_ascii_Base64Decode[(int)src[2]] << 6)
-#define SEXTUPLE_D_6(src)	(g_ascii_Base64Decode[(int)src[3]]     )
+#define SEXTUPLE_D_1(src)	(base64dec[(int)src[0]] << 2)
+#define SEXTUPLE_D_2(src)	(base64dec[(int)src[1]] >> 4)
+#define SEXTUPLE_D_3(src)	(base64dec[(int)src[1]] << 4)
+#define SEXTUPLE_D_4(src)	(base64dec[(int)src[2]] >> 2)
+#define SEXTUPLE_D_5(src)	(base64dec[(int)src[2]] << 6)
+#define SEXTUPLE_D_6(src)	(base64dec[(int)src[3]]     )
 
 	for (src = base64 ; remain > 4 ; remain -= 4, src += 4)
@@ -297,4 +282,6 @@
 		*dst++ = (cf_byte)(SEXTUPLE_D_5 (src) | SEXTUPLE_D_6 (src));
 
-	return binlen - (4 - remain);
-}
+	*len = binlen - (4 - remain);
+
+	return CF_OK;
+}
