Return std::string from base64 encoding function (#2248)

This is avoids manual memory allocations.
This commit is contained in:
Andrew Gaul 2023-08-06 22:22:02 +09:00 committed by GitHub
parent 13ad53eef7
commit c568a69452
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 66 deletions

View File

@ -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)

View File

@ -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<const unsigned char*>(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<const unsigned char*>(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

View File

@ -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 += '\"';
}

View File

@ -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;
}

View File

@ -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);
//

View File

@ -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<const char *>(buf), len, nullptr, 0);
ASSERT_STREQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>(""), 0), nullptr);
ASSERT_STREQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>(""), 0).c_str(), nullptr);
buf = s3fs_decode64("", 0, &len);
ASSERT_BUFEQUALS(reinterpret_cast<const char *>(buf), len, nullptr, 0);
ASSERT_STREQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>("1"), 1), "MQ==");
ASSERT_EQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>("1"), 1), std::string("MQ=="));
buf = s3fs_decode64("MQ==", 4, &len);
ASSERT_BUFEQUALS(reinterpret_cast<const char *>(buf), len, "1", 1);
ASSERT_EQUALS(len, static_cast<size_t>(1));
ASSERT_STREQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>("12"), 2), "MTI=");
ASSERT_EQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>("12"), 2), std::string("MTI="));
buf = s3fs_decode64("MTI=", 4, &len);
ASSERT_BUFEQUALS(reinterpret_cast<const char *>(buf), len, "12", 2);
ASSERT_EQUALS(len, static_cast<size_t>(2));
ASSERT_STREQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>("123"), 3), "MTIz");
ASSERT_EQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>("123"), 3), std::string("MTIz"));
buf = s3fs_decode64("MTIz", 4, &len);
ASSERT_BUFEQUALS(reinterpret_cast<const char *>(buf), len, "123", 3);
ASSERT_EQUALS(len, static_cast<size_t>(3));
ASSERT_STREQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>("1234"), 4), "MTIzNA==");
ASSERT_EQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>("1234"), 4), std::string("MTIzNA=="));
buf = s3fs_decode64("MTIzNA==", 8, &len);
ASSERT_BUFEQUALS(reinterpret_cast<const char *>(buf), len, "1234", 4);
ASSERT_EQUALS(len, static_cast<size_t>(4));