Synchronize with tizen 2.4
[platform/core/security/drm-service-core-tizen.git] / tappsd / src / util / DTapps2Base64.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 /** 
18  * @file        DTapps2Base64.cpp
19  * @brief       This file includes functions relating to Base64 Encode Decode.
20  */
21
22 #include "DTapps2Base64.h"
23
24 const unsigned char MAP[128] = 
25 {
26         64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
27         64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
28         64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
29         52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
30         64,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
31         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
32         64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
33         41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
34 };
35
36 const char CODE[64] = 
37 {
38         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
39         'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
40         'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
41         'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
42         'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
43         'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
44         'w', 'x', 'y', 'z', '0', '1', '2', '3',
45         '4', '5', '6', '7', '8', '9', '+', '/' 
46 };
47
48 BOOL DTappsB64Encode(unsigned char* in, int in_size, unsigned char* out, int out_size)
49 {
50         int i, j;
51         unsigned char a, b, c;
52         int len = (in_size + 2) / 3 * 4;
53  
54         if( out_size < len )
55            return FALSE;
56         
57         for( i = 0, j = 0; j < len; i += 3, j += 4 )
58         {
59            a = b = c = 0;
60            a = *(in+i);
61            *(out+j) = CODE[(a >> 2) & 0x3F];
62            if( in_size - i > 2 )
63            {
64                   b = *(in+i+1);
65                   c = *(in+i+2);
66                   *(out+j+1) = CODE[((a << 4) & 0x30) + ((b >> 4) & 0xf)];
67                   *(out+j+2) = CODE[((b << 2) & 0x3c) + ((c >> 6) & 0x3)];
68                   *(out+j+3) = CODE[c & 0x3F];
69            }
70            else if( in_size - i  > 1 )
71            {
72                   b = *(in+i+1);
73                   *(out+j+1) = CODE[((a << 4) & 0x30) + ((b >> 4) & 0xf)];
74                   *(out+j+2) = CODE[((b << 2) & 0x3c) + ((c >> 6) & 0x3)];
75                   *(out+j+3) = '=';
76            }
77            else
78            {
79                   *(out+j+1) = CODE[((a << 4) & 0x30) + ((b >> 4) & 0xf)];
80                   *(out+j+2) = '=';
81                   *(out+j+3) = '=';
82            }
83         }
84
85         return TRUE;
86 }
87
88
89 int DTappsB64Decode(unsigned char* in, int in_size, unsigned char* out, int& out_size)
90 {
91         int i, j, k;
92         unsigned char c[4];
93         int read = 0;
94  
95         for( i=j=k=0; i < in_size; i++ )
96         {
97            if( in[i] > 128 )
98                   continue;
99            c[j] = MAP[(unsigned char)in[i]];
100            if( c[j] == 64 )
101                   continue;
102            j++;
103  
104            switch( j )
105            {
106            case 2:
107                   out[k++] = (c[0]<<2) | ((c[1]>>4) & 0x3);
108                   break;
109  
110            case 3:
111                   out[k++] = ((c[1]<<4) & 0xf0) | ((c[2]>>2) & 0xf);
112                   break;
113  
114            case 4:
115                   out[k++] = ((c[2]<<6) & 0xc0) | c[3];
116                   read = i;
117                   j = 0;
118                   break;
119            }
120         }
121          
122         out_size = k;
123
124         return read+1;
125 }