utils: cleanup Base64Encode

This commit is contained in:
Peter Keresztes Schmidt 2021-04-04 00:39:40 +02:00
parent e330f8553d
commit 545f0dbb96
7 changed files with 26 additions and 30 deletions

View File

@ -273,7 +273,7 @@ int FfmpegCamera::OpenFfmpeg() {
// Set transport method as specified by method field, rtpUni is default // Set transport method as specified by method field, rtpUni is default
std::string protocol = mPath.substr(0, 4); std::string protocol = mPath.substr(0, 4);
string_toupper(protocol); StringToUpper(protocol);
if ( protocol == "RTSP" ) { if ( protocol == "RTSP" ) {
const std::string method = Method(); const std::string method = Method();
if ( method == "rtpMulti" ) { if ( method == "rtpMulti" ) {

View File

@ -83,7 +83,7 @@ void RemoteCamera::Initialise() {
if ( authIndex != std::string::npos ) { if ( authIndex != std::string::npos ) {
auth = host.substr( 0, authIndex ); auth = host.substr( 0, authIndex );
host.erase( 0, authIndex+1 ); host.erase( 0, authIndex+1 );
auth64 = base64Encode( auth ); auth64 = Base64Encode(auth);
authIndex = auth.rfind( ':' ); authIndex = auth.rfind( ':' );
username = auth.substr(0,authIndex); username = auth.substr(0,authIndex);

View File

@ -42,7 +42,7 @@ bool RtspThread::sendCommand(std::string message) {
message += stringtf("CSeq: %d\r\n\r\n", ++mSeq); message += stringtf("CSeq: %d\r\n\r\n", ++mSeq);
Debug(2, "Sending RTSP message: %s", message.c_str()); Debug(2, "Sending RTSP message: %s", message.c_str());
if ( mMethod == RTP_RTSP_HTTP ) { if ( mMethod == RTP_RTSP_HTTP ) {
message = base64Encode(message); message = Base64Encode(message);
Debug(2, "Sending encoded RTSP message: %s", message.c_str()); Debug(2, "Sending encoded RTSP message: %s", message.c_str());
if ( mRtspSocket2.send(message.c_str(), message.size()) != (int)message.length() ) { if ( mRtspSocket2.send(message.c_str(), message.size()) != (int)message.length() ) {
Error("Unable to send message '%s': %s", message.c_str(), strerror(errno)); Error("Unable to send message '%s': %s", message.c_str(), strerror(errno));

View File

@ -98,7 +98,7 @@ std::string Authenticator::quote( const std::string &src ) {
std::string Authenticator::getAuthHeader(std::string method, std::string uri) { std::string Authenticator::getAuthHeader(std::string method, std::string uri) {
std::string result = "Authorization: "; std::string result = "Authorization: ";
if ( fAuthMethod == AUTH_BASIC ) { if ( fAuthMethod == AUTH_BASIC ) {
result += "Basic " + base64Encode(username() + ":" + password()); result += "Basic " + Base64Encode(username() + ":" + password());
} else if ( fAuthMethod == AUTH_DIGEST ) { } else if ( fAuthMethod == AUTH_DIGEST ) {
result += std::string("Digest ") + result += std::string("Digest ") +
"username=\"" + quote(username()) + "\", realm=\"" + quote(realm()) + "\", " + "username=\"" + quote(username()) + "\", realm=\"" + quote(realm()) + "\", " +

View File

@ -21,7 +21,6 @@
#include "zm_config.h" #include "zm_config.h"
#include "zm_logger.h" #include "zm_logger.h"
#include <algorithm>
#include <array> #include <array>
#include <cstring> #include <cstring>
#include <fcntl.h> /* Definition of AT_* constants */ #include <fcntl.h> /* Definition of AT_* constants */
@ -121,7 +120,7 @@ std::string Join(const StringVector &values, const std::string &delim) {
return ss.str(); return ss.str();
} }
const std::string base64Encode(const std::string &inString) { std::string Base64Encode(const std::string &str) {
static char base64_table[64] = {'\0'}; static char base64_table[64] = {'\0'};
if (!base64_table[0]) { if (!base64_table[0]) {
@ -137,9 +136,9 @@ const std::string base64Encode(const std::string &inString) {
} }
std::string outString; std::string outString;
outString.reserve(2 * inString.size()); outString.reserve(2 * str.size());
const char *inPtr = inString.c_str(); const char *inPtr = str.c_str();
while (*inPtr) { while (*inPtr) {
unsigned char selection = *inPtr >> 2; unsigned char selection = *inPtr >> 2;
unsigned char remainder = (*inPtr++ & 0x03) << 4; unsigned char remainder = (*inPtr++ & 0x03) << 4;
@ -333,10 +332,6 @@ std::string UriDecode( const std::string &encoded ) {
return retbuf; return retbuf;
} }
void string_toupper( std::string& str) {
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
}
void touch(const char *pathname) { void touch(const char *pathname) {
int fd = open(pathname, int fd = open(pathname,
O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK,

View File

@ -20,6 +20,7 @@
#ifndef ZM_UTILS_H #ifndef ZM_UTILS_H
#define ZM_UTILS_H #define ZM_UTILS_H
#include <algorithm>
#include <chrono> #include <chrono>
#include <ctime> #include <ctime>
#include <map> #include <map>
@ -34,6 +35,7 @@ typedef std::vector<std::string> StringVector;
std::string Trim(const std::string &str, const std::string &char_set); std::string Trim(const std::string &str, const std::string &char_set);
inline std::string TrimSpaces(const std::string &str) { return Trim(str, " \t"); } 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); 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, char delim);
StringVector Split(const std::string &str, const std::string &delim, size_t limit = 0); 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 return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
} }
const std::string base64Encode( const std::string &inString ); std::string Base64Encode(const std::string &str);
void string_toupper(std::string& str);
void* sse2_aligned_memcpy(void* dest, const void* src, size_t bytes); void* sse2_aligned_memcpy(void* dest, const void* src, size_t bytes);
void timespec_diff(struct timespec *start, struct timespec *end, struct timespec *diff); void timespec_diff(struct timespec *start, struct timespec *end, struct timespec *diff);

View File

@ -140,14 +140,14 @@ TEST_CASE("Join") {
REQUIRE(Join({"a", "b"}, "") == "ab"); REQUIRE(Join({"a", "b"}, "") == "ab");
} }
TEST_CASE("base64Encode") { TEST_CASE("Base64Encode") {
REQUIRE(base64Encode("") == ""); REQUIRE(Base64Encode("") == "");
REQUIRE(base64Encode("f") == "Zg=="); REQUIRE(Base64Encode("f") == "Zg==");
REQUIRE(base64Encode("fo") == "Zm8="); REQUIRE(Base64Encode("fo") == "Zm8=");
REQUIRE(base64Encode("foo") == "Zm9v"); REQUIRE(Base64Encode("foo") == "Zm9v");
REQUIRE(base64Encode("foob") == "Zm9vYg=="); REQUIRE(Base64Encode("foob") == "Zm9vYg==");
REQUIRE(base64Encode("fooba") == "Zm9vYmE="); REQUIRE(Base64Encode("fooba") == "Zm9vYmE=");
REQUIRE(base64Encode("foobar") == "Zm9vYmFy"); REQUIRE(Base64Encode("foobar") == "Zm9vYmFy");
} }
TEST_CASE("UriDecode") { TEST_CASE("UriDecode") {