diff --git a/MSVC/ring-daemon.vcxproj b/MSVC/ring-daemon.vcxproj index 0a819f73befafc5f229e9dad2d9e6032b342c257..360135cb3311b3d548de356b1c633d9eba131046 100644 --- a/MSVC/ring-daemon.vcxproj +++ b/MSVC/ring-daemon.vcxproj @@ -454,7 +454,7 @@ <ClInclude Include="..\src\account_schema.h" /> <ClInclude Include="..\src\archiver.h" /> <ClInclude Include="..\src\array_size.h" /> - <ClInclude Include="..\src\base64.h" /> + <ClInclude Include="..\src\ring_base64.h" /> <ClInclude Include="..\src\call.h" /> <ClInclude Include="..\src\call_factory.h" /> <ClInclude Include="..\src\client\ring_signal.h" /> @@ -593,7 +593,7 @@ <ClCompile Include="..\src\account.cpp" /> <ClCompile Include="..\src\account_factory.cpp" /> <ClCompile Include="..\src\archiver.cpp" /> - <ClCompile Include="..\src\base64.cpp" /> + <ClCompile Include="..\src\ring_base64.cpp" /> <ClCompile Include="..\src\buildinfo.cpp" /> <ClCompile Include="..\src\call.cpp" /> <ClCompile Include="..\src\call_factory.cpp" /> diff --git a/MSVC/ring-daemon.vcxproj.filters b/MSVC/ring-daemon.vcxproj.filters index 32f3c4684fc834d32401b9d2b638d09e8fa61c89..548351f9f634cd9d0d6d9c401fe2e5ff6d98576e 100644 --- a/MSVC/ring-daemon.vcxproj.filters +++ b/MSVC/ring-daemon.vcxproj.filters @@ -529,7 +529,7 @@ <ClInclude Include="..\src\thread_pool.h"> <Filter>Header Files</Filter> </ClInclude> - <ClInclude Include="..\src\base64.h"> + <ClInclude Include="..\src\ring_base64.h"> <Filter>Header Files</Filter> </ClInclude> </ItemGroup> @@ -846,7 +846,7 @@ <ClCompile Include="..\src\thread_pool.cpp"> <Filter>Source Files</Filter> </ClCompile> - <ClCompile Include="..\src\base64.cpp"> + <ClCompile Include="..\src\ring_base64.cpp"> <Filter>Source Files</Filter> </ClCompile> </ItemGroup> diff --git a/src/base64.cpp b/src/base64.cpp deleted file mode 100644 index d14812afd3f41e64717eb2a443c680e4ce8b4b43..0000000000000000000000000000000000000000 --- a/src/base64.cpp +++ /dev/null @@ -1,148 +0,0 @@ -/* - * 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 "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/base64.h b/src/base64.h deleted file mode 100644 index 80b53c3521cc34f8cb96111a2544646046ebf3c7..0000000000000000000000000000000000000000 --- a/src/base64.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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 diff --git a/src/ringdht/ringaccount.cpp b/src/ringdht/ringaccount.cpp index 41bc5f602f34e0aeba3ab438d4a1424c3d3ce5d3..5fc2121eb75cbc16dae36dd1d1952fd60cb0bf6a 100644 --- a/src/ringdht/ringaccount.cpp +++ b/src/ringdht/ringaccount.cpp @@ -59,7 +59,7 @@ #include "config/yamlparser.h" #include "security/certstore.h" #include "libdevcrypto/Common.h" -#include "base64.h" +#include "ring_base64.h" #include <yaml-cpp/yaml.h> #include <json/json.h> diff --git a/src/sip/sdp.cpp b/src/sip/sdp.cpp index 0ebf3df937460dd49995b899f7a9c78e75524a57..0d29ba3a545c445bc81a93abe001d1b6eea8bc59 100644 --- a/src/sip/sdp.cpp +++ b/src/sip/sdp.cpp @@ -30,7 +30,7 @@ #include "sipaccount.h" #include "sipvoiplink.h" #include "string_utils.h" -#include "base64.h" +#include "ring_base64.h" #include "manager.h" #include "logger.h"