2 * Copyright (c) 2000-2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "TADC_Core.h"
18 #include "TADC_Util.h"
21 #include "TADC_ErrorCode.h"
23 // -------------------------- Base64 --------------------------------
24 static CHAR __base64_table[] = {
25 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
26 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
27 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
28 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0'
31 static int __reverse_table[] = {
32 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
33 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
34 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
35 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
36 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
37 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
38 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
39 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
40 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
47 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
50 static CHAR __base64_pad = '=';
52 LPSTR Base64Encode( LPBYTE pbData, int nLength )
54 int i = 0, result = 0;
55 LPSTR pszResult = NULL;
61 result = ( ( nLength + 3 - nLength % 3 ) * 4 / 3 + 1 );
63 pszResult = (LPSTR)TADC_IF_Malloc( result );
65 if (pszResult == NULL)
70 TADC_IF_MemSet(pszResult, 0x00, result);
76 /* keep going until we have less than 24 bits */
77 pszResult[ i++ ] = __base64_table[ pbData[ 0 ] >> 2 ];
78 pszResult[ i++ ] = __base64_table[ ( ( pbData[ 0 ] & 0x03 ) << 4 ) + ( pbData[ 1 ] >> 4 ) ];
79 pszResult[ i++ ] = __base64_table[ ( ( pbData[ 1 ] & 0x0f ) << 2 ) + ( pbData[ 2 ] >> 6 ) ];
80 pszResult[ i++ ] = __base64_table[ pbData[ 2 ] & 0x3f ];
83 nLength -= 3; /* we just handle 3 octets of data */
86 /* now deal with the tail end of things */
89 pszResult[ i++ ] = __base64_table[ pbData[ 0 ] >> 2 ];
92 pszResult[ i++ ] = __base64_table[ ( pbData[ 0 ] & 0x03 ) << 4 ];
93 pszResult[ i++ ] = __base64_pad;
94 pszResult[ i++ ] = __base64_pad;
98 pszResult[ i++ ] = __base64_table[ ( ( pbData[ 0 ] & 0x03 ) << 4 ) + ( pbData[ 1 ] >> 4 ) ];
99 pszResult[ i++ ] = __base64_table[ ( pbData[ 1 ] & 0x0f ) << 2 ];
100 pszResult[ i++ ] = __base64_pad;
107 LPBYTE Base64Decode(LPCSTR pszString, int* pnLength)
110 LPBYTE pbResult = NULL;
111 int nStrLength = 0, i = 0, nOutputLength = 0;
114 nStrLength = TADC_IF_StrLen(pszString);
115 if (nStrLength % 4 != 0)
121 pbResult = (LPBYTE)TADC_IF_Malloc(nStrLength + 1);
122 if (pbResult == NULL)
126 TADC_IF_MemSet(pbResult, 0x00, nStrLength + 1);
130 for (i = 0 ; i < nStrLength ; i += 4)
132 b1 = (BYTE)__reverse_table[ pszString[ i ] ];
133 b2 = (BYTE)__reverse_table[ pszString[ i + 1 ] ];
134 b3 = (BYTE)__reverse_table[ pszString[ i + 2 ] ];
135 b4 = (BYTE)__reverse_table[ pszString[ i + 3 ] ];
137 pbResult[ nOutputLength++ ] = (BYTE)( ( b1 << 2 ) | ( b2 >> 4 ) );
139 if (pszString[ i + 2 ] == '=')
141 pbResult[ nOutputLength ] = (BYTE)( ( b2 & 0x0F ) << 4 );
145 pbResult[ nOutputLength++ ] = (BYTE)( ( ( b2 & 0x0F ) << 4 ) | ( b3 >> 2 ) );
147 if (pszString[ i + 3 ] == '=')
149 pbResult[ nOutputLength ] = (BYTE)( ( b3 & 0x03 ) << 6 );
153 pbResult[ nOutputLength++ ] = (BYTE)( ( ( b3 & 0x03 ) << 6 ) | b4 );
157 *pnLength = nOutputLength;
163 TADC_IF_Free( pbResult );
168 // -------------------------- Base64 --------------------------------]]]]
170 int HEX2BIN(LPCSTR pszHex, LPBYTE baBin, int* pnLength )
174 int i = 0, nLength = 0;
176 nLength = TADC_IF_StrLen(pszHex);
183 if ((nLength % 2) == 0)
185 TADC_IF_StrNCpy(szHex, pszHex, nLength);
190 TADC_IF_StrNCpy(&szHex[ 1 ], pszHex, nLength);
194 *pnLength = nLength / 2;
197 for (i = 0 ; i < *pnLength ; i++ )
199 szTemp[ 0 ] = szHex[ i * 2 ];
200 szTemp[ 1 ] = szHex[ i * 2 + 1 ];
201 baBin[ i ] = (BYTE)strtoul( szTemp, NULL, 16 );
206 //Find String (2011.03.08)
207 //return : start position of find string or error (-1)
208 int FindString(unsigned char *in, int inLen, unsigned char *find, int findLen)
212 for (i = 0 ; i <= inLen - findLen ; i++)
214 if (!TADC_IF_MemCmp(in + i, find, findLen))