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',
26 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
27 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
28 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
29 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
30 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
31 'w', 'x', 'y', 'z', '0', '1', '2', '3',
32 '4', '5', '6', '7', '8', '9', '+', '/',
36 static BYTE __reverse_table[256] = {
37 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
38 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
39 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
40 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
41 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
42 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f,
43 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
44 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
45 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
46 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
47 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
48 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff,
49 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
50 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
51 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30,
52 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff,
53 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
54 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
55 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
56 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
57 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
58 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
59 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
60 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
61 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
62 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
63 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
64 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
65 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
66 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
67 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
68 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
71 static CHAR __base64_pad = '=';
73 LPSTR Base64Encode(LPBYTE pbData, int nLength)
75 int i = 0, result = 0;
76 LPSTR pszResult = NULL;
81 result = ((nLength + 3 - nLength % 3) * 4 / 3 + 1);
83 pszResult = (LPSTR)TADC_IF_Malloc(result);
85 if (pszResult == NULL)
88 TADC_IF_MemSet(pszResult, 0x00, result);
93 /* keep going until we have less than 24 bits */
94 pszResult[i++] = __base64_table[pbData[0] >> 2];
95 pszResult[i++] = __base64_table[((pbData[0] & 0x03) << 4) +
97 pszResult[i++] = __base64_table[((pbData[1] & 0x0f) << 2) +
99 pszResult[i++] = __base64_table[pbData[2] & 0x3f];
102 nLength -= 3; /* we just handle 3 octets of data */
105 /* now deal with the tail end of things */
107 pszResult[i++] = __base64_table[pbData[0] >> 2];
110 pszResult[i++] = __base64_table[(pbData[0] & 0x03) << 4];
111 pszResult[i++] = __base64_pad;
112 pszResult[i++] = __base64_pad;
114 pszResult[i++] = __base64_table[((pbData[0] & 0x03) << 4) +
116 pszResult[i++] = __base64_table[(pbData[1] & 0x0f) << 2];
117 pszResult[i++] = __base64_pad;
125 LPBYTE Base64Decode(LPCSTR pszString, int *pnLength)
127 size_t nStrLength = TADC_IF_StrLen(pszString);
129 if (nStrLength % 4 != 0)
132 LPBYTE pbResult = (LPBYTE)TADC_IF_Malloc(nStrLength + 1);
134 if (pbResult == NULL)
137 TADC_IF_MemSet(pbResult, 0x00, nStrLength + 1);
139 int nOutputLength = 0;
141 for (size_t i = 0; i < nStrLength; i += 4) {
142 BYTE b1 = __reverse_table[static_cast<BYTE>(pszString[i])];
143 BYTE b2 = __reverse_table[static_cast<BYTE>(pszString[i + 1])];
144 BYTE b3 = __reverse_table[static_cast<BYTE>(pszString[i + 2])];
145 BYTE b4 = __reverse_table[static_cast<BYTE>(pszString[i + 3])];
147 pbResult[nOutputLength++] = (b1 << 2) | (b2 >> 4);
149 if (pszString[i + 2] == '=') {
150 pbResult[nOutputLength] = (b2 & 0x0F) << 4;
152 pbResult[nOutputLength++] = ((b2 & 0x0F) << 4) | (b3 >> 2);
154 if (pszString[i + 3] == '=')
155 pbResult[nOutputLength] = (b3 & 0x03) << 6;
157 pbResult[nOutputLength++] = ((b3 & 0x03) << 6) | b4;
161 *pnLength = nOutputLength;
165 // -------------------------- Base64 --------------------------------]]]]
167 int HEX2BIN(LPCSTR pszHex, LPBYTE baBin, int *pnLength)
171 int i = 0, nLength = 0;
173 nLength = TADC_IF_StrLen(pszHex);
178 if ((nLength % 2) == 0) {
179 TADC_IF_StrNCpy(szHex, pszHex, nLength);
182 TADC_IF_StrNCpy(&szHex[1], pszHex, nLength);
186 *pnLength = nLength / 2;
189 for (i = 0; i < *pnLength; i++) {
190 szTemp[0] = szHex[i * 2];
191 szTemp[1] = szHex[i * 2 + 1];
192 baBin[i] = (BYTE)strtoul(szTemp, NULL, 16);
198 //return : start position of find string or error (-1)
199 int FindString(unsigned char *in, int inLen, unsigned char *find, int findLen)
201 for (int i = 0; i <= inLen - findLen; i++)
202 if (!TADC_IF_MemCmp(in + i, find, findLen))