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 BYTE __reverse_table[256] = {
32 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
33 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
34 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
35 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
36 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
37 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
38 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
39 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
40 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
41 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
42 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
43 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
44 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
45 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
46 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
47 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
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)
109 size_t nStrLength = TADC_IF_StrLen(pszString);
110 if (nStrLength % 4 != 0)
113 LPBYTE pbResult = (LPBYTE)TADC_IF_Malloc(nStrLength + 1);
114 if (pbResult == NULL)
117 TADC_IF_MemSet(pbResult, 0x00, nStrLength + 1);
119 int nOutputLength = 0;
121 for (size_t i = 0; i < nStrLength; i += 4) {
122 BYTE b1 = __reverse_table[static_cast<BYTE>(pszString[i])];
123 BYTE b2 = __reverse_table[static_cast<BYTE>(pszString[i + 1])];
124 BYTE b3 = __reverse_table[static_cast<BYTE>(pszString[i + 2])];
125 BYTE b4 = __reverse_table[static_cast<BYTE>(pszString[i + 3])];
127 pbResult[nOutputLength++] = (b1 << 2) | (b2 >> 4);
129 if (pszString[i + 2] == '=') {
130 pbResult[nOutputLength] = (b2 & 0x0F) << 4;
132 pbResult[nOutputLength++] = ((b2 & 0x0F) << 4) | (b3 >> 2);
134 if (pszString[i + 3] == '=')
135 pbResult[nOutputLength] = (b3 & 0x03) << 6;
137 pbResult[nOutputLength++] = ((b3 & 0x03) << 6) | b4;
141 *pnLength = nOutputLength;
145 // -------------------------- Base64 --------------------------------]]]]
147 int HEX2BIN(LPCSTR pszHex, LPBYTE baBin, int* pnLength )
151 int i = 0, nLength = 0;
153 nLength = TADC_IF_StrLen(pszHex);
160 if ((nLength % 2) == 0)
162 TADC_IF_StrNCpy(szHex, pszHex, nLength);
167 TADC_IF_StrNCpy(&szHex[ 1 ], pszHex, nLength);
171 *pnLength = nLength / 2;
174 for (i = 0 ; i < *pnLength ; i++ )
176 szTemp[ 0 ] = szHex[ i * 2 ];
177 szTemp[ 1 ] = szHex[ i * 2 + 1 ];
178 baBin[ i ] = (BYTE)strtoul( szTemp, NULL, 16 );
183 //Find String (2011.03.08)
184 //return : start position of find string or error (-1)
185 int FindString(unsigned char *in, int inLen, unsigned char *find, int findLen)
189 for (i = 0 ; i <= inLen - findLen ; i++)
191 if (!TADC_IF_MemCmp(in + i, find, findLen))