Synchronize with tizen 2.4
[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 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 
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         int                     result = 0; 
110         LPBYTE          pbResult = NULL; 
111         int                     nStrLength = 0, i = 0, nOutputLength = 0; 
112         BYTE            b1, b2, b3, b4; 
113  
114         nStrLength = TADC_IF_StrLen(pszString);
115         if (nStrLength % 4 != 0) 
116         { 
117                 result = -1; 
118                 goto finish; 
119         } 
120  
121         pbResult = (LPBYTE)TADC_IF_Malloc(nStrLength + 1);
122         if (pbResult == NULL)
123         {
124                  return NULL;
125         }
126         TADC_IF_MemSet(pbResult, 0x00, nStrLength + 1);
127          
128         nOutputLength = 0; 
129
130         for (i = 0 ; i < nStrLength ; i += 4) 
131         { 
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 ] ]; 
136  
137                 pbResult[ nOutputLength++ ] = (BYTE)( ( b1 << 2 ) | ( b2 >> 4 ) ); 
138                  
139                 if (pszString[ i + 2 ] == '=') 
140                 { 
141                         pbResult[ nOutputLength ] = (BYTE)( ( b2 & 0x0F ) << 4 ); 
142                 } 
143                 else 
144                 { 
145                         pbResult[ nOutputLength++ ] = (BYTE)( ( ( b2 & 0x0F ) << 4 ) | ( b3 >> 2 ) ); 
146
147                         if (pszString[ i + 3 ] == '=') 
148                         { 
149                                 pbResult[ nOutputLength ] = (BYTE)( ( b3 & 0x03 ) << 6 ); 
150                         } 
151                         else 
152                         { 
153                                 pbResult[ nOutputLength++ ] = (BYTE)( ( ( b3 & 0x03 ) << 6 ) | b4 ); 
154                         } 
155                 } 
156         } 
157         *pnLength = nOutputLength; 
158         result = 0; 
159  
160 finish: 
161         if (result != 0) 
162         { 
163                 TADC_IF_Free( pbResult );
164         } 
165  
166         return pbResult; 
167
168 // -------------------------- Base64 --------------------------------]]]]
169
170 int HEX2BIN(LPCSTR pszHex, LPBYTE baBin, int* pnLength ) 
171
172         CHAR            szTemp[ 3 ]; 
173         CHAR            szHex[ 1024 ]; 
174         int                     i = 0, nLength = 0; 
175          
176         nLength = TADC_IF_StrLen(pszHex);
177         
178         if (nLength <= 0)
179         {
180                 return -1;               
181         }
182                 
183         if ((nLength % 2) == 0) 
184         { 
185                 TADC_IF_StrNCpy(szHex, pszHex, nLength);
186         } 
187         else 
188         { 
189                 szHex[ 0 ] = '0'; 
190                 TADC_IF_StrNCpy(&szHex[ 1 ], pszHex, nLength);
191                 nLength += 1; 
192         } 
193  
194         *pnLength = nLength / 2; 
195         szTemp[ 2 ] = 0; 
196
197         for (i = 0 ; i < *pnLength ; i++ ) 
198         { 
199                 szTemp[ 0 ] = szHex[ i * 2 ]; 
200                 szTemp[ 1 ] = szHex[ i * 2 + 1 ]; 
201                 baBin[ i ] = (BYTE)strtoul( szTemp, NULL, 16 ); 
202         } 
203         return 0;
204
205
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)
209 {
210         int i = 0;
211
212         for (i = 0 ; i <= inLen - findLen ; i++)
213         {
214                 if (!TADC_IF_MemCmp(in + i, find, findLen))
215                 return i;
216         }
217         return -1;
218 }