2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * Copyright (c) 2003-2007, Bicom Systems Ltd.
19 * All rights reserved.
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions are met:
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.
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.
49 #include "Exception.h"
52 namespace WrtDeviceApis {
54 const std::string Base64::chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
55 "abcdefghijklmnopqrstuvwxyz"
58 bool Base64::is_base64(unsigned char c)
60 return (isalnum(c) || (c == '+') || (c == '/'));
63 std::string Base64::encode(unsigned char* data,
69 unsigned char char_array_3[3];
70 unsigned char char_array_4[4];
73 char_array_3[i++] = *(data++);
75 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
78 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
81 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
82 char_array_4[3] = char_array_3[2] & 0x3f;
84 for (i = 0; i < 4; ++i) {
85 ret += chars[char_array_4[i]];
92 for (j = i; j < 3; ++j) {
93 char_array_3[j] = '\0';
96 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
98 ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
100 ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
101 char_array_4[3] = char_array_3[2] & 0x3f;
103 for (j = 0; (j < i + 1); ++j) {
104 ret += chars[char_array_4[j]];
115 std::string Base64::decode(const std::string& str)
117 if (!is_base64_string(str)) {
118 ThrowMsg(InvalidArgumentException,
119 "Invalid length of base64 string.");
121 int in_len = str.size();
125 unsigned char char_array_4[4], char_array_3[3];
128 while (in_len-- && (str[in_] != '=')) {
129 if (!is_base64(str[in_])) {
130 ThrowMsg(InvalidArgumentException,
131 "Invalid characters in base64 string.");
133 char_array_4[i++] = str[in_];
136 for (i = 0; i < 4; ++i) {
137 char_array_4[i] = chars.find(char_array_4[i]);
141 (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
144 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
145 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
147 for (i = 0; i < 3; ++i) {
148 ret += char_array_3[i];
155 for (j = i; j < 4; ++j) {
159 for (j = 0; j < 4; ++j) {
160 char_array_4[j] = chars.find(char_array_4[j]);
164 (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
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];
169 for (j = 0; (j < i - 1); ++j) {
170 ret += char_array_3[j];
177 bool Base64::is_base64_string(const std::string& str)
179 return ((str.size() % 4) == 0);
182 } //WrtDeviceApisCommon