Browse Source

Moved base64 encoding to utils

Paul-Louis Ageneau 3 years ago
parent
commit
95a334a744
6 changed files with 43 additions and 103 deletions
  1. 0 2
      CMakeLists.txt
  2. 0 64
      src/impl/base64.cpp
  3. 0 34
      src/impl/base64.hpp
  4. 37 0
      src/impl/utils.cpp
  5. 4 0
      src/impl/utils.hpp
  6. 2 3
      src/impl/wshandshake.cpp

+ 0 - 2
CMakeLists.txt

@@ -118,7 +118,6 @@ set(LIBDATACHANNEL_IMPL_SOURCES
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/track.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/utils.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/processor.cpp
-	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/base64.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/sha.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/pollinterrupter.cpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/pollservice.cpp
@@ -150,7 +149,6 @@ set(LIBDATACHANNEL_IMPL_HEADERS
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/track.hpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/utils.hpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/processor.hpp
-	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/base64.hpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/sha.hpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/pollinterrupter.hpp
 	${CMAKE_CURRENT_SOURCE_DIR}/src/impl/pollservice.hpp

+ 0 - 64
src/impl/base64.cpp

@@ -1,64 +0,0 @@
-/**
- * Copyright (c) 2020 Paul-Louis Ageneau
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library 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
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#if RTC_ENABLE_WEBSOCKET
-
-#include "base64.hpp"
-
-namespace rtc::impl {
-
-using std::to_integer;
-
-string to_base64(const binary &data) {
-	static const char tab[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
-	string out;
-	out.reserve(3 * ((data.size() + 3) / 4));
-	int i = 0;
-	while (data.size() - i >= 3) {
-		auto d0 = to_integer<uint8_t>(data[i]);
-		auto d1 = to_integer<uint8_t>(data[i + 1]);
-		auto d2 = to_integer<uint8_t>(data[i + 2]);
-		out += tab[d0 >> 2];
-		out += tab[((d0 & 3) << 4) | (d1 >> 4)];
-		out += tab[((d1 & 0x0F) << 2) | (d2 >> 6)];
-		out += tab[d2 & 0x3F];
-		i += 3;
-	}
-
-	int left = int(data.size() - i);
-	if (left) {
-		auto d0 = to_integer<uint8_t>(data[i]);
-		out += tab[d0 >> 2];
-		if (left == 1) {
-			out += tab[(d0 & 3) << 4];
-			out += '=';
-		} else { // left == 2
-			auto d1 = to_integer<uint8_t>(data[i + 1]);
-			out += tab[((d0 & 3) << 4) | (d1 >> 4)];
-			out += tab[(d1 & 0x0F) << 2];
-		}
-		out += '=';
-	}
-
-	return out;
-}
-
-} // namespace rtc::impl
-
-#endif

+ 0 - 34
src/impl/base64.hpp

@@ -1,34 +0,0 @@
-/**
- * Copyright (c) 2020 Paul-Louis Ageneau
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library 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
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#ifndef RTC_IMPL_BASE64_H
-#define RTC_IMPL_BASE64_H
-
-#if RTC_ENABLE_WEBSOCKET
-
-#include "common.hpp"
-
-namespace rtc::impl {
-
-string to_base64(const binary &data);
-
-}
-
-#endif
-
-#endif

+ 37 - 0
src/impl/utils.cpp

@@ -27,6 +27,8 @@
 
 namespace rtc::impl::utils {
 
+using std::to_integer;
+
 std::vector<string> explode(const string &str, char delim) {
 	std::vector<std::string> result;
 	std::istringstream ss(str);
@@ -73,4 +75,39 @@ string url_decode(const string &str) {
 	return result;
 }
 
+string base64_encode(const binary &data) {
+	static const char tab[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+	string out;
+	out.reserve(3 * ((data.size() + 3) / 4));
+	int i = 0;
+	while (data.size() - i >= 3) {
+		auto d0 = to_integer<uint8_t>(data[i]);
+		auto d1 = to_integer<uint8_t>(data[i + 1]);
+		auto d2 = to_integer<uint8_t>(data[i + 2]);
+		out += tab[d0 >> 2];
+		out += tab[((d0 & 3) << 4) | (d1 >> 4)];
+		out += tab[((d1 & 0x0F) << 2) | (d2 >> 6)];
+		out += tab[d2 & 0x3F];
+		i += 3;
+	}
+
+	int left = int(data.size() - i);
+	if (left) {
+		auto d0 = to_integer<uint8_t>(data[i]);
+		out += tab[d0 >> 2];
+		if (left == 1) {
+			out += tab[(d0 & 3) << 4];
+			out += '=';
+		} else { // left == 2
+			auto d1 = to_integer<uint8_t>(data[i + 1]);
+			out += tab[((d0 & 3) << 4) | (d1 >> 4)];
+			out += tab[(d1 & 0x0F) << 2];
+		}
+		out += '=';
+	}
+
+	return out;
+}
+
 } // namespace rtc::impl::utils

+ 4 - 0
src/impl/utils.hpp

@@ -32,6 +32,10 @@ string implode(const std::vector<string> &tokens, char delim);
 // See https://www.rfc-editor.org/rfc/rfc3986.html#section-2.1
 string url_decode(const string &str);
 
+// Encode as base64 (RFC 4648)
+// See https://www.rfc-editor.org/rfc/rfc4648.html#section-4
+string base64_encode(const binary &data);
+
 } // namespace rtc::impl
 
 #endif

+ 2 - 3
src/impl/wshandshake.cpp

@@ -17,7 +17,6 @@
  */
 
 #include "wshandshake.hpp"
-#include "base64.hpp"
 #include "internals.hpp"
 #include "sha.hpp"
 #include "utils.hpp"
@@ -246,11 +245,11 @@ string WsHandshake::generateKey() {
 	binary key(16);
 	auto k = reinterpret_cast<uint8_t *>(key.data());
 	std::generate(k, k + key.size(), [&]() { return uint8_t(generator()); });
-	return to_base64(key);
+	return utils::base64_encode(key);
 }
 
 string WsHandshake::computeAcceptKey(const string &key) {
-	return to_base64(Sha1(string(key) + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"));
+	return utils::base64_encode(Sha1(string(key) + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"));
 }
 
 size_t WsHandshake::parseHttpLines(const byte *buffer, size_t size, std::list<string> &lines) {