From 66006ba48d1fbe51d9a413b2fffc063f036225a6 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Wed, 4 Aug 2021 07:28:51 +0900 Subject: [PATCH] Add dedicated upper- and lower-case hex functions (#1734) This makes the call sites more readable than a boolean parameter. --- src/common_auth.cpp | 2 +- src/curl.cpp | 8 ++++---- src/string_util.cpp | 20 +++++++++++++------- src/string_util.h | 3 ++- src/test_util.h | 8 ++++---- 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/common_auth.cpp b/src/common_auth.cpp index f67ed3c..c604fe9 100644 --- a/src/common_auth.cpp +++ b/src/common_auth.cpp @@ -62,7 +62,7 @@ std::string s3fs_sha256_hex_fd(int fd, off_t start, off_t size) return std::string(""); } - std::string sha256hex = s3fs_hex(sha256, digestlen); + std::string sha256hex = s3fs_hex_lower(sha256, digestlen); delete[] sha256; return sha256hex; diff --git a/src/curl.cpp b/src/curl.cpp index c5c6bb8..6b71521 100644 --- a/src/curl.cpp +++ b/src/curl.cpp @@ -2628,7 +2628,7 @@ std::string S3fsCurl::CalcSignature(const std::string& method, const std::string StringToSign = "AWS4-HMAC-SHA256\n"; StringToSign += date8601 + "\n"; StringToSign += strdate + "/" + endpoint + "/s3/aws4_request\n"; - StringToSign += s3fs_hex(sRequest, sRequest_len); + StringToSign += s3fs_hex_lower(sRequest, sRequest_len); delete[] sRequest; const unsigned char* cscope = reinterpret_cast(StringToSign.c_str()); @@ -2639,7 +2639,7 @@ std::string S3fsCurl::CalcSignature(const std::string& method, const std::string s3fs_HMAC256(kSigning, kSigning_len, cscope, cscope_len, &md, &md_len); delete[] kSigning; - Signature = s3fs_hex(md, md_len); + Signature = s3fs_hex_lower(md, md_len); delete[] md; return Signature; @@ -2660,7 +2660,7 @@ void S3fsCurl::insertV4Headers() unsigned char* sRequest = NULL; unsigned int sRequest_len = 0; s3fs_sha256(b_postdata, cRequest_len, &sRequest, &sRequest_len); - payload_hash = s3fs_hex(sRequest, sRequest_len); + payload_hash = s3fs_hex_lower(sRequest, sRequest_len); delete[] sRequest; break; } @@ -3751,7 +3751,7 @@ int S3fsCurl::UploadMultipartPostSetup(const char* tpath, int part_num, const st S3FS_PRN_ERR("Could not make md5 for file(part %d)", part_num); return -EIO; } - partdata.etag = s3fs_hex(md5raw, get_md5_digest_length()); + partdata.etag = s3fs_hex_lower(md5raw, get_md5_digest_length()); char* md5base64p = s3fs_base64(md5raw, get_md5_digest_length()); requestHeaders = curl_slist_sort_insert(requestHeaders, "Content-MD5", md5base64p); delete[] md5base64p; diff --git a/src/string_util.cpp b/src/string_util.cpp index a679a24..16bd77a 100644 --- a/src/string_util.cpp +++ b/src/string_util.cpp @@ -152,7 +152,7 @@ std::string urlEncode(const std::string &s) result += c; }else{ result += "%"; - result += s3fs_hex(&c, 1, false); + result += s3fs_hex_upper(&c, 1); } } return result; @@ -182,7 +182,7 @@ std::string urlEncode2(const std::string &s) result += c; }else{ result += "%"; - result += s3fs_hex(&c, 1, false); + result += s3fs_hex_upper(&c, 1); } } return result; @@ -362,12 +362,8 @@ bool convert_unixtime_from_option_arg(const char* argv, time_t& unixtime) return true; } -std::string s3fs_hex(const unsigned char* input, size_t length, bool lower) +static std::string s3fs_hex(const unsigned char* input, size_t length, const char *hexAlphabet) { - static const char hexLower[] = "0123456789abcdef"; - static const char hexUpper[] = "0123456789ABCDEF"; - - const char* hexAlphabet = (lower ? hexLower : hexUpper); std::string hex; for(size_t pos = 0; pos < length; ++pos){ hex += hexAlphabet[input[pos] / 16]; @@ -376,6 +372,16 @@ std::string s3fs_hex(const unsigned char* input, size_t length, bool lower) return hex; } +std::string s3fs_hex_lower(const unsigned char* input, size_t length) +{ + return s3fs_hex(input, length, "0123456789abcdef"); +} + +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) { static const char base[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; diff --git a/src/string_util.h b/src/string_util.h index 275aa56..8f2a2ac 100644 --- a/src/string_util.h +++ b/src/string_util.h @@ -96,7 +96,8 @@ bool get_keyword_value(const std::string& target, const char* keyword, std::stri // // For binary string // -std::string s3fs_hex(const unsigned char* input, size_t length, bool lower = true); +std::string s3fs_hex_lower(const unsigned char* input, size_t length); +std::string s3fs_hex_upper(const unsigned char* input, size_t length); char* s3fs_base64(const unsigned char* input, size_t length); unsigned char* s3fs_decode64(const char* input, size_t* plength); diff --git a/src/test_util.h b/src/test_util.h index c7d270a..b3b20c9 100644 --- a/src/test_util.h +++ b/src/test_util.h @@ -40,8 +40,8 @@ template <> void assert_equals(const std::string &x, const std::string &y, const { if (x != y) { std::cerr << x << " != " << y << " at " << file << ":" << line << std::endl; - std::cerr << s3fs_hex(reinterpret_cast(x.c_str()), x.size()) << std::endl; - std::cerr << s3fs_hex(reinterpret_cast(y.c_str()), y.size()) << std::endl; + std::cerr << s3fs_hex_lower(reinterpret_cast(x.c_str()), x.size()) << std::endl; + std::cerr << s3fs_hex_lower(reinterpret_cast(y.c_str()), y.size()) << std::endl; std::exit(1); } } @@ -59,8 +59,8 @@ template <> void assert_nequals(const std::string &x, const std::string &y, cons { if (x == y) { std::cerr << x << " == " << y << " at " << file << ":" << line << std::endl; - std::cerr << s3fs_hex(reinterpret_cast(x.c_str()), x.size()) << std::endl; - std::cerr << s3fs_hex(reinterpret_cast(y.c_str()), y.size()) << std::endl; + std::cerr << s3fs_hex_lower(reinterpret_cast(x.c_str()), x.size()) << std::endl; + std::cerr << s3fs_hex_lower(reinterpret_cast(y.c_str()), y.size()) << std::endl; std::exit(1); } }