Replace xattr_value with std::string

This commit is contained in:
Andrew Gaul 2023-08-18 16:33:56 +09:00 committed by Andrew Gaul
parent 9fb4c32c6a
commit 2102bea81b
6 changed files with 38 additions and 60 deletions

View File

@ -798,10 +798,8 @@ bool S3fsCurl::PushbackSseKeys(const std::string& input)
std::string base64_key;
std::string raw_key;
if(onekey.length() > 256 / 8){
size_t keylength;
std::unique_ptr<unsigned char[]> p_key(s3fs_decode64(onekey.c_str(), onekey.size(), &keylength));
raw_key = std::string(reinterpret_cast<char *>(p_key.get()), keylength);
std::string p_key(s3fs_decode64(onekey.c_str(), onekey.size()));
raw_key = p_key;
base64_key = onekey;
} else {
base64_key = s3fs_base64(reinterpret_cast<const unsigned char*>(onekey.c_str()), onekey.length());

View File

@ -137,7 +137,7 @@ static bool get_meta_xattr_value(const char* path, std::string& rawvalue);
static bool get_parent_meta_xattr_value(const char* path, std::string& rawvalue);
static bool get_xattr_posix_key_value(const char* path, std::string& xattrvalue, bool default_key);
static bool build_inherited_xattr_value(const char* path, std::string& xattrvalue);
static bool parse_xattr_keyval(const std::string& xattrpair, std::string& key, xattr_value* pval);
static bool parse_xattr_keyval(const std::string& xattrpair, std::string& key, std::string* pval);
static size_t parse_xattrs(const std::string& strxattrs, xattrs_t& xattrs);
static std::string raw_build_xattrs(const xattrs_t& xattrs);
static std::string build_xattrs(const xattrs_t& xattrs);
@ -3643,7 +3643,7 @@ static bool get_xattr_posix_key_value(const char* path, std::string& xattrvalue,
}
// convert value by base64
xattrvalue = s3fs_base64(iter->second.pvalue.get(), iter->second.length);
xattrvalue = s3fs_base64(reinterpret_cast<const unsigned char*>(iter->second.c_str()), iter->second.length());
return true;
}
@ -3686,7 +3686,7 @@ static bool build_inherited_xattr_value(const char* path, std::string& xattrvalu
return true;
}
static bool parse_xattr_keyval(const std::string& xattrpair, std::string& key, xattr_value* pval)
static bool parse_xattr_keyval(const std::string& xattrpair, std::string& key, std::string* pval)
{
// parse key and value
size_t pos;
@ -3703,8 +3703,7 @@ static bool parse_xattr_keyval(const std::string& xattrpair, std::string& key, x
return false;
}
pval->length = 0;
pval->pvalue = s3fs_decode64(tmpval.c_str(), tmpval.size(), &pval->length);
*pval = s3fs_decode64(tmpval.c_str(), tmpval.size());
return true;
}
@ -3735,7 +3734,7 @@ static size_t parse_xattrs(const std::string& strxattrs, xattrs_t& xattrs)
for(size_t pair_nextpos = restxattrs.find_first_of(','); !restxattrs.empty(); restxattrs = (pair_nextpos != std::string::npos ? restxattrs.substr(pair_nextpos + 1) : ""), pair_nextpos = restxattrs.find_first_of(',')){
std::string pair = pair_nextpos != std::string::npos ? restxattrs.substr(0, pair_nextpos) : restxattrs;
std::string key;
xattr_value val;
std::string val;
if(!parse_xattr_keyval(pair, key, &val)){
// something format error, so skip this.
continue;
@ -3759,7 +3758,7 @@ static std::string raw_build_xattrs(const xattrs_t& xattrs)
strxattrs += '\"';
strxattrs += iter->first;
strxattrs += "\":\"";
strxattrs += s3fs_base64(iter->second.pvalue.get(), iter->second.length);
strxattrs += s3fs_base64(reinterpret_cast<const unsigned char*>(iter->second.c_str()), iter->second.length());
strxattrs += '\"';
}
if(is_set){
@ -3806,12 +3805,7 @@ static int set_xattrs_to_header(headers_t& meta, const char* name, const char* v
parse_xattrs(strxattrs, xattrs);
// add name(do not care overwrite and empty name/value)
xattr_value val;
val.length = size;
if(0 < size){
val.pvalue.reset(new unsigned char[size]);
memcpy(val.pvalue.get(), value, size);
}
std::string val(value, size);
xattrs.emplace(name, std::move(val));
// build new strxattrs(not encoded) and set it to headers_t
@ -4013,8 +4007,8 @@ static int s3fs_getxattr(const char* path, const char* name, char* value, size_t
}
// decode
size_t length = xiter->second.length;
unsigned char* pvalue = xiter->second.pvalue.get();
size_t length = xiter->second.length();
const char* pvalue = xiter->second.c_str();
if(0 < size){
if(static_cast<size_t>(size) < length){

View File

@ -418,29 +418,28 @@ inline unsigned char char_decode64(const char ch)
return by;
}
std::unique_ptr<unsigned char[]> s3fs_decode64(const char* input, size_t input_len, size_t* plength)
std::string s3fs_decode64(const char* input, size_t input_len)
{
std::unique_ptr<unsigned char[]> result(new unsigned char[input_len / 4 * 3]);
std::string result;
result.reserve(input_len / 4 * 3);
unsigned char parts[4];
size_t rpos;
size_t wpos;
for(rpos = 0, wpos = 0; rpos < input_len; rpos += 4){
for(rpos = 0; rpos < input_len; rpos += 4){
parts[0] = char_decode64(input[rpos]);
parts[1] = (rpos + 1) < input_len ? char_decode64(input[rpos + 1]) : 64;
parts[2] = (rpos + 2) < input_len ? char_decode64(input[rpos + 2]) : 64;
parts[3] = (rpos + 3) < input_len ? char_decode64(input[rpos + 3]) : 64;
result[wpos++] = ((parts[0] << 2) & 0xfc) | ((parts[1] >> 4) & 0x03);
result += static_cast<char>(((parts[0] << 2) & 0xfc) | ((parts[1] >> 4) & 0x03));
if(64 == parts[2]){
break;
}
result[wpos++] = ((parts[1] << 4) & 0xf0) | ((parts[2] >> 2) & 0x0f);
result += static_cast<char>(((parts[1] << 4) & 0xf0) | ((parts[2] >> 2) & 0x0f));
if(64 == parts[3]){
break;
}
result[wpos++] = ((parts[2] << 6) & 0xc0) | (parts[3] & 0x3f);
result += static_cast<char>(((parts[2] << 6) & 0xc0) | (parts[3] & 0x3f));
}
*plength = wpos;
return result;
}

View File

@ -22,7 +22,7 @@
#define S3FS_STRING_UTIL_H_
#include <cstring>
#include <memory>
#include <string>
//
// A collection of string utilities for manipulating URLs and HTTP responses.
@ -107,7 +107,7 @@ 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);
std::string s3fs_base64(const unsigned char* input, size_t length);
std::unique_ptr<unsigned char[]> s3fs_decode64(const char* input, size_t input_len, size_t* plength);
std::string s3fs_decode64(const char* input, size_t input_len);
//
// WTF8

View File

@ -53,37 +53,36 @@ void test_trim()
void test_base64()
{
std::unique_ptr<unsigned char[]> buf;
std::string buf;
char tmpbuf = '\0';
size_t len;
ASSERT_EQUALS(s3fs_base64(nullptr, 0), std::string(""));
buf = s3fs_decode64(nullptr, 0, &len);
ASSERT_BUFEQUALS(reinterpret_cast<const char *>(buf.get()), len, &tmpbuf, 0);
buf = s3fs_decode64(nullptr, 0);
ASSERT_BUFEQUALS(buf.c_str(), buf.length(), &tmpbuf, 0);
ASSERT_EQUALS(s3fs_base64(reinterpret_cast<const unsigned char *>(""), 0), std::string(""));
buf = s3fs_decode64("", 0, &len);
ASSERT_BUFEQUALS(reinterpret_cast<const char *>(buf.get()), len, &tmpbuf, 0);
buf = s3fs_decode64("", 0);
ASSERT_BUFEQUALS(buf.c_str(), buf.length(), &tmpbuf, 0);
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.get()), len, "1", 1);
ASSERT_EQUALS(len, static_cast<size_t>(1));
buf = s3fs_decode64("MQ==", 4);
ASSERT_BUFEQUALS(buf.c_str(), buf.length(), "1", 1);
ASSERT_EQUALS(buf.length(), static_cast<size_t>(1));
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.get()), len, "12", 2);
ASSERT_EQUALS(len, static_cast<size_t>(2));
buf = s3fs_decode64("MTI=", 4);
ASSERT_BUFEQUALS(buf.c_str(), buf.length(), "12", 2);
ASSERT_EQUALS(buf.length(), static_cast<size_t>(2));
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.get()), len, "123", 3);
ASSERT_EQUALS(len, static_cast<size_t>(3));
buf = s3fs_decode64("MTIz", 4);
ASSERT_BUFEQUALS(buf.c_str(), buf.length(), "123", 3);
ASSERT_EQUALS(buf.length(), static_cast<size_t>(3));
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.get()), len, "1234", 4);
ASSERT_EQUALS(len, static_cast<size_t>(4));
buf = s3fs_decode64("MTIzNA==", 8);
ASSERT_BUFEQUALS(buf.c_str(), buf.length(), "1234", 4);
ASSERT_EQUALS(buf.length(), static_cast<size_t>(4));
// TODO: invalid input
}

View File

@ -25,7 +25,6 @@
#include <cstring>
#include <string>
#include <map>
#include <memory>
#include <list>
#include <vector>
@ -55,18 +54,7 @@
// This header is url encoded string which is json formatted.
// x-amz-meta-xattr:urlencode({"xattr-1":"base64(value-1)","xattr-2":"base64(value-2)","xattr-3":"base64(value-3)"})
//
struct xattr_value
{
std::unique_ptr<unsigned char[]> pvalue;
size_t length;
xattr_value() : pvalue(), length(0) {}
xattr_value(const xattr_value& xv) = delete;
xattr_value(xattr_value&& xv) : pvalue(std::move(xv.pvalue)), length(xv.length) {}
~xattr_value() {}
};
typedef std::map<std::string, xattr_value> xattrs_t;
typedef std::map<std::string, std::string> xattrs_t;
//-------------------------------------------------------------------
// acl_t