From 6671af7f80242ee6dd4e7b86f09245ac2974cfe3 Mon Sep 17 00:00:00 2001
From: atraczyk <andreastraczyk@gmail.com>
Date: Fri, 16 Sep 2016 15:13:32 -0400
Subject: [PATCH] upload ring_base64

Change-Id: I2e62920ac2da374670f5a12aae7f7be532a2a18f
---
 src/ring_base64.cpp | 148 ++++++++++++++++++++++++++++++++++++++++++++
 src/ring_base64.h   |  60 ++++++++++++++++++
 2 files changed, 208 insertions(+)
 create mode 100644 src/ring_base64.cpp
 create mode 100644 src/ring_base64.h

diff --git a/src/ring_base64.cpp b/src/ring_base64.cpp
new file mode 100644
index 0000000000..43c133df6a
--- /dev/null
+++ b/src/ring_base64.cpp
@@ -0,0 +1,148 @@
+/*
+ *  Copyright (C) 2004-2016 Savoir-faire Linux Inc.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
+ */
+
+#include "ring_base64.h"
+
+#include <stdint.h>
+#include <stdlib.h>
+
+/* Mainly based on the following stackoverflow question:
+ * http://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c
+ */
+static const char encoding_table[] = {
+    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
+    'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
+    'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
+    'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
+    's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2',
+    '3', '4', '5', '6', '7', '8', '9', '+', '/'
+};
+
+static const int mod_table[] = { 0, 2, 1 };
+
+char *ring_base64_encode(const uint8_t *input, size_t input_length,
+                             char *output, size_t *output_length)
+{
+    size_t i, j;
+    size_t out_sz = *output_length;
+    *output_length = 4 * ((input_length + 2) / 3);
+    if (out_sz < *output_length || output == NULL)
+        return NULL;
+
+    for (i = 0, j = 0; i < input_length; ) {
+        uint8_t octet_a = i < input_length ? input[i++] : 0;
+        uint8_t octet_b = i < input_length ? input[i++] : 0;
+        uint8_t octet_c = i < input_length ? input[i++] : 0;
+
+        uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
+
+        output[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
+        output[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
+        output[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
+        output[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
+    }
+
+    for (i = 0; i < mod_table[input_length % 3]; i++)
+        output[*output_length - 1 - i] = '=';
+
+    return output;
+}
+
+uint8_t *ring_base64_decode(const char *input, size_t input_length,
+                             uint8_t *output, size_t *output_length)
+{
+    size_t i, j;
+    uint8_t decoding_table[256];
+
+    uint8_t c;
+    for (c = 0; c < 64; c++)
+        decoding_table[encoding_table[c]] = c;
+
+    if (input_length % 4 != 0)
+        return NULL;
+
+    size_t out_sz = *output_length;
+    *output_length = input_length / 4 * 3;
+    if (input[input_length - 1] == '=')
+        (*output_length)--;
+    if (input[input_length - 2] == '=')
+        (*output_length)--;
+
+    if (out_sz < *output_length || output == NULL)
+        return NULL;
+
+    for (i = 0, j = 0; i < input_length;) {
+        uint8_t sextet_a = input[i] == '=' ? 0 & i++
+                                          : decoding_table[input[i++]];
+        uint8_t sextet_b = input[i] == '=' ? 0 & i++
+                                          : decoding_table[input[i++]];
+        uint8_t sextet_c = input[i] == '=' ? 0 & i++
+                                          : decoding_table[input[i++]];
+        uint8_t sextet_d = input[i] == '=' ? 0 & i++
+                                          : decoding_table[input[i++]];
+
+        uint32_t triple = (sextet_a << 3 * 6) +
+                          (sextet_b << 2 * 6) +
+                          (sextet_c << 1 * 6) +
+                          (sextet_d << 0 * 6);
+
+        if (j < *output_length)
+            output[j++] = (triple >> 2 * 8) & 0xFF;
+        if (j < *output_length)
+            output[j++] = (triple >> 1 * 8) & 0xFF;
+        if (j < *output_length)
+            output[j++] = (triple >> 0 * 8) & 0xFF;
+    }
+
+    return output;
+}
+
+namespace ring {
+namespace base64 {
+
+std::string
+encode(const std::vector<uint8_t>::const_iterator begin,
+       const std::vector<uint8_t>::const_iterator end)
+{
+    size_t output_length = 4 * ((std::distance(begin, end) + 2) / 3);
+    std::string out;
+    out.resize(output_length);
+    ring_base64_encode(&(*begin), std::distance(begin, end),
+                       &(*out.begin()), &output_length);
+    out.resize(output_length);
+    return out;
+}
+
+std::string
+encode(const std::vector<uint8_t>& dat)
+{
+    return encode(dat.cbegin(), dat.cend());
+}
+
+std::vector<uint8_t>
+decode(const std::string& str)
+{
+    size_t output_length = str.length() / 4 * 3 + 2;
+    std::vector<uint8_t> output;
+    output.resize(output_length);
+    ring_base64_decode(str.data(), str.size(), output.data(), &output_length);
+    output.resize(output_length);
+    return output;
+}
+
+}} // namespace ring::base64
diff --git a/src/ring_base64.h b/src/ring_base64.h
new file mode 100644
index 0000000000..80b53c3521
--- /dev/null
+++ b/src/ring_base64.h
@@ -0,0 +1,60 @@
+/*
+ *  Copyright (C) 2004-2016 Savoir-faire Linux Inc.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
+ */
+
+#pragma once
+
+#include <stdint.h>
+#include <stddef.h>
+
+/**
+ * Encode a buffer in base64.
+ *
+ * @param data          the input buffer
+ * @param input_length  the input length
+ * @param output_length the resulting output length
+ * @return              a base64-encoded buffer
+ *
+ * @note callers should free the returned memory
+ */
+char *ring_base64_encode(const uint8_t *input, size_t input_length,
+                         char *output, size_t *output_length);
+
+/**
+ * Decode a base64 buffer.
+ *
+ * @param data          the input buffer
+ * @param input_length  the input length
+ * @param output_length the resulting output length
+ * @return              a buffer
+ *
+ * @note callers should free the returned memory
+ */
+uint8_t *ring_base64_decode(const char *input, size_t input_length,
+                            uint8_t *output, size_t *output_length);
+
+#include <string>
+#include <vector>
+
+namespace ring {
+namespace base64 {
+
+std::string encode(const std::vector<uint8_t>::const_iterator begin, const std::vector<uint8_t>::const_iterator end);
+std::string encode(const std::vector<uint8_t>& dat);
+std::vector<uint8_t> decode(const std::string& str);
+
+}} // namespace ring::base64
-- 
GitLab