From 545f0dbb96fb584f805c32fc29ce4e025d26fe9b Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Sun, 4 Apr 2021 00:39:40 +0200 Subject: [PATCH] utils: cleanup Base64Encode --- src/zm_ffmpeg_camera.cpp | 2 +- src/zm_remote_camera.cpp | 2 +- src/zm_rtsp.cpp | 2 +- src/zm_rtsp_auth.cpp | 2 +- src/zm_utils.cpp | 27 +++++++++++---------------- src/zm_utils.h | 5 +++-- tests/zm_utils.cpp | 16 ++++++++-------- 7 files changed, 26 insertions(+), 30 deletions(-) diff --git a/src/zm_ffmpeg_camera.cpp b/src/zm_ffmpeg_camera.cpp index 2f89197cb..25ebdc6a6 100644 --- a/src/zm_ffmpeg_camera.cpp +++ b/src/zm_ffmpeg_camera.cpp @@ -273,7 +273,7 @@ int FfmpegCamera::OpenFfmpeg() { // Set transport method as specified by method field, rtpUni is default std::string protocol = mPath.substr(0, 4); - string_toupper(protocol); + StringToUpper(protocol); if ( protocol == "RTSP" ) { const std::string method = Method(); if ( method == "rtpMulti" ) { diff --git a/src/zm_remote_camera.cpp b/src/zm_remote_camera.cpp index c1a252c3d..5e3dfefae 100644 --- a/src/zm_remote_camera.cpp +++ b/src/zm_remote_camera.cpp @@ -83,7 +83,7 @@ void RemoteCamera::Initialise() { if ( authIndex != std::string::npos ) { auth = host.substr( 0, authIndex ); host.erase( 0, authIndex+1 ); - auth64 = base64Encode( auth ); + auth64 = Base64Encode(auth); authIndex = auth.rfind( ':' ); username = auth.substr(0,authIndex); diff --git a/src/zm_rtsp.cpp b/src/zm_rtsp.cpp index 3492e2318..57310a50f 100644 --- a/src/zm_rtsp.cpp +++ b/src/zm_rtsp.cpp @@ -42,7 +42,7 @@ bool RtspThread::sendCommand(std::string message) { message += stringtf("CSeq: %d\r\n\r\n", ++mSeq); Debug(2, "Sending RTSP message: %s", message.c_str()); if ( mMethod == RTP_RTSP_HTTP ) { - message = base64Encode(message); + message = Base64Encode(message); Debug(2, "Sending encoded RTSP message: %s", message.c_str()); if ( mRtspSocket2.send(message.c_str(), message.size()) != (int)message.length() ) { Error("Unable to send message '%s': %s", message.c_str(), strerror(errno)); diff --git a/src/zm_rtsp_auth.cpp b/src/zm_rtsp_auth.cpp index d947fc75e..ba47df1a9 100644 --- a/src/zm_rtsp_auth.cpp +++ b/src/zm_rtsp_auth.cpp @@ -98,7 +98,7 @@ std::string Authenticator::quote( const std::string &src ) { std::string Authenticator::getAuthHeader(std::string method, std::string uri) { std::string result = "Authorization: "; if ( fAuthMethod == AUTH_BASIC ) { - result += "Basic " + base64Encode(username() + ":" + password()); + result += "Basic " + Base64Encode(username() + ":" + password()); } else if ( fAuthMethod == AUTH_DIGEST ) { result += std::string("Digest ") + "username=\"" + quote(username()) + "\", realm=\"" + quote(realm()) + "\", " + diff --git a/src/zm_utils.cpp b/src/zm_utils.cpp index 4c5ece6e2..8bfd74317 100644 --- a/src/zm_utils.cpp +++ b/src/zm_utils.cpp @@ -21,7 +21,6 @@ #include "zm_config.h" #include "zm_logger.h" -#include #include #include #include /* Definition of AT_* constants */ @@ -121,36 +120,36 @@ std::string Join(const StringVector &values, const std::string &delim) { return ss.str(); } -const std::string base64Encode(const std::string &inString) { - static char base64_table[64] = { '\0' }; +std::string Base64Encode(const std::string &str) { + static char base64_table[64] = {'\0'}; - if ( !base64_table[0] ) { + if (!base64_table[0]) { int i = 0; - for ( char c = 'A'; c <= 'Z'; c++ ) + for (char c = 'A'; c <= 'Z'; c++) base64_table[i++] = c; - for ( char c = 'a'; c <= 'z'; c++ ) + for (char c = 'a'; c <= 'z'; c++) base64_table[i++] = c; - for ( char c = '0'; c <= '9'; c++ ) + for (char c = '0'; c <= '9'; c++) base64_table[i++] = c; base64_table[i++] = '+'; base64_table[i++] = '/'; } std::string outString; - outString.reserve(2 * inString.size()); + outString.reserve(2 * str.size()); - const char *inPtr = inString.c_str(); - while ( *inPtr ) { + const char *inPtr = str.c_str(); + while (*inPtr) { unsigned char selection = *inPtr >> 2; unsigned char remainder = (*inPtr++ & 0x03) << 4; outString += base64_table[selection]; - if ( *inPtr ) { + if (*inPtr) { selection = remainder | (*inPtr >> 4); remainder = (*inPtr++ & 0x0f) << 2; outString += base64_table[selection]; - if ( *inPtr ) { + if (*inPtr) { selection = remainder | (*inPtr >> 6); outString += base64_table[selection]; selection = (*inPtr++ & 0x3f); @@ -333,10 +332,6 @@ std::string UriDecode( const std::string &encoded ) { return retbuf; } -void string_toupper( std::string& str) { - std::transform(str.begin(), str.end(), str.begin(), ::toupper); -} - void touch(const char *pathname) { int fd = open(pathname, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, diff --git a/src/zm_utils.h b/src/zm_utils.h index 2aa33d546..1e7af22e1 100644 --- a/src/zm_utils.h +++ b/src/zm_utils.h @@ -20,6 +20,7 @@ #ifndef ZM_UTILS_H #define ZM_UTILS_H +#include #include #include #include @@ -34,6 +35,7 @@ typedef std::vector StringVector; std::string Trim(const std::string &str, const std::string &char_set); inline std::string TrimSpaces(const std::string &str) { return Trim(str, " \t"); } std::string ReplaceAll(std::string str, const std::string& old_value, const std::string& new_value); +inline void StringToUpper(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), ::toupper); } StringVector Split(const std::string &str, char delim); StringVector Split(const std::string &str, const std::string &delim, size_t limit = 0); @@ -56,8 +58,7 @@ std::string stringtf(const std::string &format, Args... args) { return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside } -const std::string base64Encode( const std::string &inString ); -void string_toupper(std::string& str); +std::string Base64Encode(const std::string &str); void* sse2_aligned_memcpy(void* dest, const void* src, size_t bytes); void timespec_diff(struct timespec *start, struct timespec *end, struct timespec *diff); diff --git a/tests/zm_utils.cpp b/tests/zm_utils.cpp index 8b021ef35..ffe7ce36f 100644 --- a/tests/zm_utils.cpp +++ b/tests/zm_utils.cpp @@ -140,14 +140,14 @@ TEST_CASE("Join") { REQUIRE(Join({"a", "b"}, "") == "ab"); } -TEST_CASE("base64Encode") { - REQUIRE(base64Encode("") == ""); - REQUIRE(base64Encode("f") == "Zg=="); - REQUIRE(base64Encode("fo") == "Zm8="); - REQUIRE(base64Encode("foo") == "Zm9v"); - REQUIRE(base64Encode("foob") == "Zm9vYg=="); - REQUIRE(base64Encode("fooba") == "Zm9vYmE="); - REQUIRE(base64Encode("foobar") == "Zm9vYmFy"); +TEST_CASE("Base64Encode") { + REQUIRE(Base64Encode("") == ""); + REQUIRE(Base64Encode("f") == "Zg=="); + REQUIRE(Base64Encode("fo") == "Zm8="); + REQUIRE(Base64Encode("foo") == "Zm9v"); + REQUIRE(Base64Encode("foob") == "Zm9vYg=="); + REQUIRE(Base64Encode("fooba") == "Zm9vYmE="); + REQUIRE(Base64Encode("foobar") == "Zm9vYmFy"); } TEST_CASE("UriDecode") {