From c568a69452c6082ef338a4e3bdbe30793703cb94 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sun, 6 Aug 2023 22:22:02 +0900 Subject: [PATCH] Return std::string from base64 encoding function (#2248) This is avoids manual memory allocations. --- src/common_auth.cpp | 13 ++----------- src/curl.cpp | 26 +++++--------------------- src/s3fs.cpp | 15 ++------------- src/string_util.cpp | 21 ++++++++------------- src/string_util.h | 4 ++-- src/test_string_util.cpp | 12 ++++++------ 6 files changed, 25 insertions(+), 66 deletions(-) diff --git a/src/common_auth.cpp b/src/common_auth.cpp index 64336b0..7c5a33e 100644 --- a/src/common_auth.cpp +++ b/src/common_auth.cpp @@ -30,20 +30,11 @@ std::string s3fs_get_content_md5(int fd) { md5_t md5; - char* base64; - std::string Signature; - if(!s3fs_md5_fd(fd, 0, -1, &md5)){ + // TODO: better return value? return std::string(""); } - if(nullptr == (base64 = s3fs_base64(md5.data(), md5.size()))){ - return std::string(""); // ENOMEM - } - - Signature = base64; - delete[] base64; - - return Signature; + return s3fs_base64(md5.data(), md5.size()); } std::string s3fs_sha256_hex_fd(int fd, off_t start, off_t size) diff --git a/src/curl.cpp b/src/curl.cpp index 959a92b..cb22a0f 100644 --- a/src/curl.cpp +++ b/src/curl.cpp @@ -815,16 +815,8 @@ bool S3fsCurl::PushbackSseKeys(const std::string& input) return false; } } else { - char* pbase64_key; - - if(nullptr != (pbase64_key = s3fs_base64(reinterpret_cast(onekey.c_str()), onekey.length()))) { - raw_key = onekey; - base64_key = pbase64_key; - delete[] pbase64_key; - } else { - S3FS_PRN_ERR("Failed to convert base64 from SSE-C key %s", onekey.c_str()); - return false; - } + base64_key = s3fs_base64(reinterpret_cast(onekey.c_str()), onekey.length()); + raw_key = onekey; } // make MD5 @@ -2767,16 +2759,9 @@ std::string S3fsCurl::CalcSignatureV2(const std::string& method, const std::stri s3fs_HMAC(key, key_len, sdata, sdata_len, &md, &md_len); - char* base64; - if(nullptr == (base64 = s3fs_base64(md, md_len))){ - delete[] md; - return std::string(""); // ENOMEM - } + Signature = s3fs_base64(md, md_len); delete[] md; - Signature = base64; - delete[] base64; - return Signature; } @@ -4124,9 +4109,8 @@ int S3fsCurl::UploadMultipartPostSetup(const char* tpath, int part_num, const st return -EIO; } partdata.etag = s3fs_hex_lower(md5raw.data(), md5raw.size()); - char* md5base64p = s3fs_base64(md5raw.data(), md5raw.size()); - requestHeaders = curl_slist_sort_insert(requestHeaders, "Content-MD5", md5base64p); - delete[] md5base64p; + std::string md5base64 = s3fs_base64(md5raw.data(), md5raw.size()); + requestHeaders = curl_slist_sort_insert(requestHeaders, "Content-MD5", md5base64.c_str()); } // make request diff --git a/src/s3fs.cpp b/src/s3fs.cpp index f2aadc8..8965d2d 100644 --- a/src/s3fs.cpp +++ b/src/s3fs.cpp @@ -3636,16 +3636,9 @@ static bool get_xattr_posix_key_value(const char* path, std::string& xattrvalue, } // convert value by base64 - char* base64val = s3fs_base64((iter->second)->pvalue, (iter->second)->length); - if(!base64val){ - free_xattrs(xattrs); - return false; - } + xattrvalue = s3fs_base64(iter->second->pvalue, iter->second->length); free_xattrs(xattrs); - xattrvalue = base64val; - delete[] base64val; - return true; } @@ -3771,11 +3764,7 @@ static std::string raw_build_xattrs(const xattrs_t& xattrs) strxattrs += "\":\""; if(iter->second){ - char* base64val = s3fs_base64((iter->second)->pvalue, (iter->second)->length); - if(base64val){ - strxattrs += base64val; - delete[] base64val; - } + strxattrs += s3fs_base64(iter->second->pvalue, iter->second->length); } strxattrs += '\"'; } diff --git a/src/string_util.cpp b/src/string_util.cpp index 85c59fa..a8c5a38 100644 --- a/src/string_util.cpp +++ b/src/string_util.cpp @@ -373,31 +373,26 @@ std::string s3fs_hex_upper(const unsigned char* input, size_t length) return s3fs_hex(input, length, "0123456789ABCDEF"); } -char* s3fs_base64(const unsigned char* input, size_t length) +std::string s3fs_base64(const unsigned char* input, size_t length) { static const char base[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; - char* result; - if(!input || 0 == length){ - return nullptr; - } - result = new char[((length + 3 - 1) / 3) * 4 + 1]; + std::string result; + result.reserve(((length + 3 - 1) / 3) * 4 + 1); unsigned char parts[4]; size_t rpos; - size_t wpos; - for(rpos = 0, wpos = 0; rpos < length; rpos += 3){ + for(rpos = 0; rpos < length; rpos += 3){ parts[0] = (input[rpos] & 0xfc) >> 2; parts[1] = ((input[rpos] & 0x03) << 4) | ((((rpos + 1) < length ? input[rpos + 1] : 0x00) & 0xf0) >> 4); parts[2] = (rpos + 1) < length ? (((input[rpos + 1] & 0x0f) << 2) | ((((rpos + 2) < length ? input[rpos + 2] : 0x00) & 0xc0) >> 6)) : 0x40; parts[3] = (rpos + 2) < length ? (input[rpos + 2] & 0x3f) : 0x40; - result[wpos++] = base[parts[0]]; - result[wpos++] = base[parts[1]]; - result[wpos++] = base[parts[2]]; - result[wpos++] = base[parts[3]]; + result += base[parts[0]]; + result += base[parts[1]]; + result += base[parts[2]]; + result += base[parts[3]]; } - result[wpos] = '\0'; return result; } diff --git a/src/string_util.h b/src/string_util.h index eceabc6..1e14a35 100644 --- a/src/string_util.h +++ b/src/string_util.h @@ -105,8 +105,8 @@ bool get_keyword_value(const std::string& target, const char* keyword, std::stri // std::string s3fs_hex_lower(const unsigned char* input, size_t length); std::string s3fs_hex_upper(const unsigned char* input, size_t length); -// TODO: return std::string -char* s3fs_base64(const unsigned char* input, size_t length); +std::string s3fs_base64(const unsigned char* input, size_t length); +// TODO: return std::unique_ptr unsigned char* s3fs_decode64(const char* input, size_t input_len, size_t* plength); // diff --git a/src/test_string_util.cpp b/src/test_string_util.cpp index 5063dc7..24ba9c9 100644 --- a/src/test_string_util.cpp +++ b/src/test_string_util.cpp @@ -56,30 +56,30 @@ void test_base64() unsigned char *buf; size_t len; - ASSERT_STREQUALS(s3fs_base64(nullptr, 0), nullptr); + ASSERT_STREQUALS(s3fs_base64(nullptr, 0).c_str(), nullptr); buf = s3fs_decode64(nullptr, 0, &len); ASSERT_BUFEQUALS(reinterpret_cast(buf), len, nullptr, 0); - ASSERT_STREQUALS(s3fs_base64(reinterpret_cast(""), 0), nullptr); + ASSERT_STREQUALS(s3fs_base64(reinterpret_cast(""), 0).c_str(), nullptr); buf = s3fs_decode64("", 0, &len); ASSERT_BUFEQUALS(reinterpret_cast(buf), len, nullptr, 0); - ASSERT_STREQUALS(s3fs_base64(reinterpret_cast("1"), 1), "MQ=="); + ASSERT_EQUALS(s3fs_base64(reinterpret_cast("1"), 1), std::string("MQ==")); buf = s3fs_decode64("MQ==", 4, &len); ASSERT_BUFEQUALS(reinterpret_cast(buf), len, "1", 1); ASSERT_EQUALS(len, static_cast(1)); - ASSERT_STREQUALS(s3fs_base64(reinterpret_cast("12"), 2), "MTI="); + ASSERT_EQUALS(s3fs_base64(reinterpret_cast("12"), 2), std::string("MTI=")); buf = s3fs_decode64("MTI=", 4, &len); ASSERT_BUFEQUALS(reinterpret_cast(buf), len, "12", 2); ASSERT_EQUALS(len, static_cast(2)); - ASSERT_STREQUALS(s3fs_base64(reinterpret_cast("123"), 3), "MTIz"); + ASSERT_EQUALS(s3fs_base64(reinterpret_cast("123"), 3), std::string("MTIz")); buf = s3fs_decode64("MTIz", 4, &len); ASSERT_BUFEQUALS(reinterpret_cast(buf), len, "123", 3); ASSERT_EQUALS(len, static_cast(3)); - ASSERT_STREQUALS(s3fs_base64(reinterpret_cast("1234"), 4), "MTIzNA=="); + ASSERT_EQUALS(s3fs_base64(reinterpret_cast("1234"), 4), std::string("MTIzNA==")); buf = s3fs_decode64("MTIzNA==", 8, &len); ASSERT_BUFEQUALS(reinterpret_cast(buf), len, "1234", 4); ASSERT_EQUALS(len, static_cast(4));