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