Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_mobile / Commons / Base64.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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  * Copyright (c) 2003-2007, Bicom Systems Ltd.
18  *
19  * All rights reserved.
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions are met:
23  *
24  *   - Redistributions of source code must retain the above copyright notice,
25  *         this list of conditions and the following disclaimer.
26  *   - Redistributions in binary form must reproduce the above copyright
27  *         notice, this list of conditions and the following disclaimer in the
28  *         documentation and/or other materials provided with the distribution.
29  *   - Neither the name of the Bicom Systems Ltd nor the names of its
30  *         contributors may be used to endorse or promote products derived from
31  *         this software without specific prior written permission.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
34  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
37  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
38  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
39  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
40  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
41  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  * POSSIBILITY OF SUCH DAMAGE.
44  *
45  * Denis Komadaric,
46  * Bicom Systems Ltd.
47  */
48 #include <ctype.h>
49 #include "Exception.h"
50 #include "Base64.h"
51
52 namespace WrtDeviceApis {
53 namespace Commons {
54 const std::string Base64::chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
55                                   "abcdefghijklmnopqrstuvwxyz"
56                                   "0123456789+/";
57
58 bool Base64::is_base64(unsigned char c)
59 {
60     return (isalnum(c) || (c == '+') || (c == '/'));
61 }
62
63 std::string Base64::encode(unsigned char* data,
64                            std::size_t num)
65 {
66     std::string ret;
67     int i = 0;
68     int j = 0;
69     unsigned char char_array_3[3];
70     unsigned char char_array_4[4];
71
72     while (num--) {
73         char_array_3[i++] = *(data++);
74         if (i == 3) {
75             char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
76             char_array_4[1] =
77                 ((char_array_3[0] &
78                   0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
79             char_array_4[2] =
80                 ((char_array_3[1] &
81                   0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
82             char_array_4[3] = char_array_3[2] & 0x3f;
83
84             for (i = 0; i < 4; ++i) {
85                 ret += chars[char_array_4[i]];
86             }
87             i = 0;
88         }
89     }
90
91     if (i != 0) {
92         for (j = i; j < 3; ++j) {
93             char_array_3[j] = '\0';
94         }
95
96         char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
97         char_array_4[1] =
98             ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
99         char_array_4[2] =
100             ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
101         char_array_4[3] = char_array_3[2] & 0x3f;
102
103         for (j = 0; (j < i + 1); ++j) {
104             ret += chars[char_array_4[j]];
105         }
106
107         while ((i++ < 3)) {
108             ret += '=';
109         }
110     }
111
112     return ret;
113 }
114
115 std::string Base64::decode(const std::string& str)
116 {
117     if (!is_base64_string(str)) {
118         ThrowMsg(InvalidArgumentException,
119                  "Invalid length of base64 string.");
120     }
121     int in_len = str.size();
122     int i = 0;
123     int j = 0;
124     int in_ = 0;
125     unsigned char char_array_4[4], char_array_3[3];
126     std::string ret;
127
128     while (in_len-- && (str[in_] != '=')) {
129         if (!is_base64(str[in_])) {
130             ThrowMsg(InvalidArgumentException,
131                      "Invalid characters in base64 string.");
132         }
133         char_array_4[i++] = str[in_];
134         in_++;
135         if (i == 4) {
136             for (i = 0; i < 4; ++i) {
137                 char_array_4[i] = chars.find(char_array_4[i]);
138             }
139
140             char_array_3[0] =
141                 (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
142             char_array_3[1] =
143                 ((char_array_4[1] &
144                   0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
145             char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
146
147             for (i = 0; i < 3; ++i) {
148                 ret += char_array_3[i];
149             }
150             i = 0;
151         }
152     }
153
154     if (i != 0) {
155         for (j = i; j < 4; ++j) {
156             char_array_4[j] = 0;
157         }
158
159         for (j = 0; j < 4; ++j) {
160             char_array_4[j] = chars.find(char_array_4[j]);
161         }
162
163         char_array_3[0] =
164             (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
165         char_array_3[1] =
166             ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
167         char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
168
169         for (j = 0; (j < i - 1); ++j) {
170             ret += char_array_3[j];
171         }
172     }
173
174     return ret;
175 }
176
177 bool Base64::is_base64_string(const std::string& str)
178 {
179     return ((str.size() % 4) == 0);
180 }
181 }
182 } //WrtDeviceApisCommon