Fix build warning and add related gcc options
[platform/core/security/drm-service-core-tizen.git] / tadcore / TADCCore / TADC_Util.cpp
1 /*
2  * Copyright (c) 2000-2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  *     http://floralicense.org/license/
9  *
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.
15  */
16
17 #include "TADC_Core.h"
18 #include "TADC_Util.h"
19 #include "TADC_Sub.h"
20 #include "TADC_IF.h"
21 #include "TADC_ErrorCode.h"
22
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'
29 };
30
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
48 };
49
50 static CHAR __base64_pad = '=';
51
52 LPSTR Base64Encode( LPBYTE pbData,  int nLength )
53 {
54         int i = 0, result = 0;
55         LPSTR pszResult = NULL;
56
57         if (nLength <= 0)
58         {
59                 return NULL;
60         }
61         result = ( ( nLength + 3 - nLength % 3 ) * 4 / 3 + 1 );
62
63         pszResult = (LPSTR)TADC_IF_Malloc( result );
64
65         if (pszResult == NULL)
66         {
67                 return NULL;
68         }
69
70         TADC_IF_MemSet(pszResult, 0x00, result);
71
72         i = 0;
73
74         while (nLength > 2)
75         {
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 ];
81
82                 pbData += 3;
83                 nLength -= 3; /* we just handle 3 octets of data */
84         }
85
86         /* now deal with the tail end of things */
87         if (nLength != 0)
88         {
89                 pszResult[ i++ ] = __base64_table[ pbData[ 0 ] >> 2 ];
90                 if (nLength == 1)
91                 {
92                         pszResult[ i++ ] = __base64_table[ ( pbData[ 0 ] & 0x03 ) << 4 ];
93                         pszResult[ i++ ] = __base64_pad;
94                         pszResult[ i++ ] = __base64_pad;
95                 }
96                 else
97                 {
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;
101                 }
102         }
103         pszResult[ i ] = 0;
104         return pszResult;
105 }
106
107 LPBYTE Base64Decode(LPCSTR pszString, int* pnLength)
108 {
109         size_t nStrLength = TADC_IF_StrLen(pszString);
110         if (nStrLength % 4 != 0)
111                 return NULL;
112
113         LPBYTE pbResult = (LPBYTE)TADC_IF_Malloc(nStrLength + 1);
114         if (pbResult == NULL)
115                  return NULL;
116
117         TADC_IF_MemSet(pbResult, 0x00, nStrLength + 1);
118
119         int nOutputLength = 0;
120
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])];
126
127                 pbResult[nOutputLength++] = (b1 << 2) | (b2 >> 4);
128
129                 if (pszString[i + 2] == '=') {
130                         pbResult[nOutputLength] = (b2 & 0x0F) << 4;
131                 } else {
132                         pbResult[nOutputLength++] = ((b2 & 0x0F) << 4) | (b3 >> 2);
133
134                         if (pszString[i + 3] == '=')
135                                 pbResult[nOutputLength] = (b3 & 0x03) << 6;
136                         else
137                                 pbResult[nOutputLength++] = ((b3 & 0x03) << 6) | b4;
138                 }
139         }
140
141         *pnLength = nOutputLength;
142
143         return pbResult;
144 }
145 // -------------------------- Base64 --------------------------------]]]]
146
147 int HEX2BIN(LPCSTR pszHex, LPBYTE baBin, int* pnLength )
148 {
149         CHAR szTemp[ 3 ];
150         CHAR szHex[ 1024 ];
151         int i = 0, nLength = 0;
152
153         nLength = TADC_IF_StrLen(pszHex);
154
155         if (nLength <= 0)
156         {
157                 return -1;
158         }
159
160         if ((nLength % 2) == 0)
161         {
162                 TADC_IF_StrNCpy(szHex, pszHex, nLength);
163         }
164         else
165         {
166                 szHex[ 0 ] = '0';
167                 TADC_IF_StrNCpy(&szHex[ 1 ], pszHex, nLength);
168                 nLength += 1;
169         }
170
171         *pnLength = nLength / 2;
172         szTemp[ 2 ] = 0;
173
174         for (i = 0 ; i < *pnLength ; i++ )
175         {
176                 szTemp[ 0 ] = szHex[ i * 2 ];
177                 szTemp[ 1 ] = szHex[ i * 2 + 1 ];
178                 baBin[ i ] = (BYTE)strtoul( szTemp, NULL, 16 );
179         }
180         return 0;
181 }
182
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)
186 {
187         int i = 0;
188
189         for (i = 0 ; i <= inLen - findLen ; i++)
190         {
191                 if (!TADC_IF_MemCmp(in + i, find, findLen))
192                 return i;
193         }
194         return -1;
195 }