summaryrefslogtreecommitdiff
path: root/src/Commons/Base64.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Commons/Base64.cpp')
-rw-r--r--src/Commons/Base64.cpp182
1 files changed, 182 insertions, 0 deletions
diff --git a/src/Commons/Base64.cpp b/src/Commons/Base64.cpp
new file mode 100644
index 0000000..94ac901
--- /dev/null
+++ b/src/Commons/Base64.cpp
@@ -0,0 +1,182 @@
+/*
+ * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/*
+ * Copyright (c) 2003-2007, Bicom Systems Ltd.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * - Neither the name of the Bicom Systems Ltd nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * Denis Komadaric,
+ * Bicom Systems Ltd.
+ */
+#include <ctype.h>
+#include "Exception.h"
+#include "Base64.h"
+
+namespace WrtDeviceApis {
+namespace Commons {
+const std::string Base64::chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "0123456789+/";
+
+bool Base64::is_base64(unsigned char c)
+{
+ return (isalnum(c) || (c == '+') || (c == '/'));
+}
+
+std::string Base64::encode(unsigned char* data,
+ std::size_t num)
+{
+ std::string ret;
+ int i = 0;
+ int j = 0;
+ unsigned char char_array_3[3];
+ unsigned char char_array_4[4];
+
+ while (num--) {
+ char_array_3[i++] = *(data++);
+ if (i == 3) {
+ char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
+ char_array_4[1] =
+ ((char_array_3[0] &
+ 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
+ char_array_4[2] =
+ ((char_array_3[1] &
+ 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
+ char_array_4[3] = char_array_3[2] & 0x3f;
+
+ for (i = 0; i < 4; ++i) {
+ ret += chars[char_array_4[i]];
+ }
+ i = 0;
+ }
+ }
+
+ if (i != 0) {
+ for (j = i; j < 3; ++j) {
+ char_array_3[j] = '\0';
+ }
+
+ char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
+ char_array_4[1] =
+ ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
+ char_array_4[2] =
+ ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
+ char_array_4[3] = char_array_3[2] & 0x3f;
+
+ for (j = 0; (j < i + 1); ++j) {
+ ret += chars[char_array_4[j]];
+ }
+
+ while ((i++ < 3)) {
+ ret += '=';
+ }
+ }
+
+ return ret;
+}
+
+std::string Base64::decode(const std::string& str)
+{
+ if (!is_base64_string(str)) {
+ ThrowMsg(InvalidArgumentException,
+ "Invalid length of base64 string.");
+ }
+ int in_len = str.size();
+ int i = 0;
+ int j = 0;
+ int in_ = 0;
+ unsigned char char_array_4[4], char_array_3[3];
+ std::string ret;
+
+ while (in_len-- && (str[in_] != '=')) {
+ if (!is_base64(str[in_])) {
+ ThrowMsg(InvalidArgumentException,
+ "Invalid characters in base64 string.");
+ }
+ char_array_4[i++] = str[in_];
+ in_++;
+ if (i == 4) {
+ for (i = 0; i < 4; ++i) {
+ char_array_4[i] = chars.find(char_array_4[i]);
+ }
+
+ char_array_3[0] =
+ (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
+ char_array_3[1] =
+ ((char_array_4[1] &
+ 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
+ char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
+
+ for (i = 0; i < 3; ++i) {
+ ret += char_array_3[i];
+ }
+ i = 0;
+ }
+ }
+
+ if (i != 0) {
+ for (j = i; j < 4; ++j) {
+ char_array_4[j] = 0;
+ }
+
+ for (j = 0; j < 4; ++j) {
+ char_array_4[j] = chars.find(char_array_4[j]);
+ }
+
+ char_array_3[0] =
+ (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
+ char_array_3[1] =
+ ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
+ char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
+
+ for (j = 0; (j < i - 1); ++j) {
+ ret += char_array_3[j];
+ }
+ }
+
+ return ret;
+}
+
+bool Base64::is_base64_string(const std::string& str)
+{
+ return ((str.size() % 4) == 0);
+}
+}
+} //WrtDeviceApisCommon