refactor
This commit is contained in:
		| @@ -18,6 +18,7 @@ cppflags | |||||||
| cpphttplib | cpphttplib | ||||||
| cpptrace | cpptrace | ||||||
| cppvsdbg | cppvsdbg | ||||||
|  | crypto_aead_xchacha20poly1305_ietf_npubbytes | ||||||
| cstdint | cstdint | ||||||
| cxxflags | cxxflags | ||||||
| cxxstd | cxxstd | ||||||
|   | |||||||
| @@ -57,14 +57,13 @@ private: | |||||||
|   bool use_s3_path_style_{false}; |   bool use_s3_path_style_{false}; | ||||||
|  |  | ||||||
| public: | public: | ||||||
|   [[nodiscard]] static auto construct_url(CURL *curl, |   [[nodiscard]] static auto | ||||||
|                                           const std::string &relative_path, |   construct_url(CURL *curl, const std::string &relative_path, | ||||||
|                                           const host_config &cfg) |                 const host_config &cfg) -> std::string; | ||||||
|       -> std::string; |  | ||||||
|  |  | ||||||
|   [[nodiscard]] static auto create_host_config(const s3_config &cfg, |   [[nodiscard]] static auto | ||||||
|                                                bool use_s3_path_style) |   create_host_config(const s3_config &cfg, | ||||||
|       -> host_config; |                      bool use_s3_path_style) -> host_config; | ||||||
|  |  | ||||||
|   [[nodiscard]] static auto url_encode(CURL *curl, const std::string &data, |   [[nodiscard]] static auto url_encode(CURL *curl, const std::string &data, | ||||||
|                                        bool allow_slash) -> std::string; |                                        bool allow_slash) -> std::string; | ||||||
| @@ -72,8 +71,8 @@ public: | |||||||
|   template <typename request_type> |   template <typename request_type> | ||||||
|   [[nodiscard]] static auto |   [[nodiscard]] static auto | ||||||
|   make_encrypted_request(const host_config &cfg, const request_type &request, |   make_encrypted_request(const host_config &cfg, const request_type &request, | ||||||
|                          long &response_code, stop_type &stop_requested) |                          long &response_code, | ||||||
|       -> bool { |                          stop_type &stop_requested) -> bool { | ||||||
|     response_code = 0; |     response_code = 0; | ||||||
|  |  | ||||||
|     if (not request.decryption_token.has_value() || |     if (not request.decryption_token.has_value() || | ||||||
| @@ -94,7 +93,7 @@ public: | |||||||
|         utils::encryption::generate_key(request.decryption_token.value()); |         utils::encryption::generate_key(request.decryption_token.value()); | ||||||
|     const auto result = utils::encryption::read_encrypted_range( |     const auto result = utils::encryption::read_encrypted_range( | ||||||
|         request.range.value(), key, |         request.range.value(), key, | ||||||
|         [&](std::vector<char> &ct, std::uint64_t start_offset, |         [&](data_buffer &ct, std::uint64_t start_offset, | ||||||
|             std::uint64_t end_offset) -> api_error { |             std::uint64_t end_offset) -> api_error { | ||||||
|           auto encrypted_request = request; |           auto encrypted_request = request; | ||||||
|           encrypted_request.decryption_token = std::nullopt; |           encrypted_request.decryption_token = std::nullopt; | ||||||
| @@ -212,30 +211,26 @@ public: | |||||||
| public: | public: | ||||||
|   void enable_s3_path_style(bool enable) override; |   void enable_s3_path_style(bool enable) override; | ||||||
|  |  | ||||||
|   [[nodiscard]] auto make_request(const curl::requests::http_delete &del, |   [[nodiscard]] auto | ||||||
|                                   long &response_code, |   make_request(const curl::requests::http_delete &del, long &response_code, | ||||||
|                                   stop_type &stop_requested) const |                stop_type &stop_requested) const -> bool override; | ||||||
|       -> bool override; |  | ||||||
|  |  | ||||||
|   [[nodiscard]] auto make_request(const curl::requests::http_get &get, |   [[nodiscard]] auto | ||||||
|                                   long &response_code, |   make_request(const curl::requests::http_get &get, long &response_code, | ||||||
|                                   stop_type &stop_requested) const |                stop_type &stop_requested) const -> bool override; | ||||||
|       -> bool override; |  | ||||||
|  |  | ||||||
|   [[nodiscard]] auto make_request(const curl::requests::http_head &head, |   [[nodiscard]] auto | ||||||
|                                   long &response_code, |   make_request(const curl::requests::http_head &head, long &response_code, | ||||||
|                                   stop_type &stop_requested) const |                stop_type &stop_requested) const -> bool override; | ||||||
|       -> bool override; |  | ||||||
|  |  | ||||||
|   [[nodiscard]] auto make_request(const curl::requests::http_post &post_file, |   [[nodiscard]] auto | ||||||
|                                   long &response_code, |   make_request(const curl::requests::http_post &post_file, long &response_code, | ||||||
|                                   stop_type &stop_requested) const |                stop_type &stop_requested) const -> bool override; | ||||||
|       -> bool override; |  | ||||||
|  |  | ||||||
|   [[nodiscard]] auto make_request(const curl::requests::http_put_file &put_file, |   [[nodiscard]] auto | ||||||
|                                   long &response_code, |   make_request(const curl::requests::http_put_file &put_file, | ||||||
|                                   stop_type &stop_requested) const |                long &response_code, | ||||||
|       -> bool override; |                stop_type &stop_requested) const -> bool override; | ||||||
| }; | }; | ||||||
| } // namespace repertory | } // namespace repertory | ||||||
|  |  | ||||||
|   | |||||||
| @@ -41,8 +41,9 @@ inline const auto read_file_data = static_cast<read_callback>( | |||||||
|     [](char *buffer, size_t size, size_t nitems, void *instream) -> size_t { |     [](char *buffer, size_t size, size_t nitems, void *instream) -> size_t { | ||||||
|       auto *read_info = reinterpret_cast<read_file_info *>(instream); |       auto *read_info = reinterpret_cast<read_file_info *>(instream); | ||||||
|       std::size_t bytes_read{}; |       std::size_t bytes_read{}; | ||||||
|       auto ret = read_info->nf->read_bytes(buffer, size * nitems, |       auto ret = read_info->nf->read_bytes( | ||||||
|                                            read_info->offset, bytes_read); |           reinterpret_cast<unsigned char *>(buffer), size * nitems, | ||||||
|  |           read_info->offset, bytes_read); | ||||||
|       if (ret) { |       if (ret) { | ||||||
|         read_info->offset += bytes_read; |         read_info->offset += bytes_read; | ||||||
|       } |       } | ||||||
| @@ -73,9 +74,8 @@ struct http_request_base { | |||||||
|  |  | ||||||
|   [[nodiscard]] virtual auto get_path() const -> std::string { return path; } |   [[nodiscard]] virtual auto get_path() const -> std::string { return path; } | ||||||
|  |  | ||||||
|   [[nodiscard]] virtual auto set_method(CURL *curl, |   [[nodiscard]] virtual auto | ||||||
|                                         stop_type &stop_requested) const |   set_method(CURL *curl, stop_type &stop_requested) const -> bool = 0; | ||||||
|       -> bool = 0; |  | ||||||
| }; | }; | ||||||
| } // namespace repertory::curl::requests | } // namespace repertory::curl::requests | ||||||
|  |  | ||||||
|   | |||||||
| @@ -56,18 +56,18 @@ private: | |||||||
|   std::size_t decode_offset_ = 0U; |   std::size_t decode_offset_ = 0U; | ||||||
|  |  | ||||||
| public: | public: | ||||||
|   [[nodiscard]] static auto decode_json(packet &response, json &json_data) |   [[nodiscard]] static auto decode_json(packet &response, | ||||||
|       -> int; |                                         json &json_data) -> int; | ||||||
|  |  | ||||||
| public: | public: | ||||||
|   void clear(); |   void clear(); | ||||||
|  |  | ||||||
|   [[nodiscard]] auto current_pointer() -> char * { |   [[nodiscard]] auto current_pointer() -> unsigned char * { | ||||||
|     return (decode_offset_ < buffer_.size()) ? &buffer_[decode_offset_] |     return (decode_offset_ < buffer_.size()) ? &buffer_[decode_offset_] | ||||||
|                                              : nullptr; |                                              : nullptr; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   [[nodiscard]] auto current_pointer() const -> const char * { |   [[nodiscard]] auto current_pointer() const -> const unsigned char * { | ||||||
|     return (decode_offset_ < buffer_.size()) ? &buffer_[decode_offset_] |     return (decode_offset_ < buffer_.size()) ? &buffer_[decode_offset_] | ||||||
|                                              : nullptr; |                                              : nullptr; | ||||||
|   } |   } | ||||||
| @@ -110,19 +110,19 @@ public: | |||||||
|  |  | ||||||
|   [[nodiscard]] auto decode(remote::file_info &val) -> error_type; |   [[nodiscard]] auto decode(remote::file_info &val) -> error_type; | ||||||
|  |  | ||||||
|   [[nodiscard]] auto decrypt(const std::string &token) -> error_type; |   [[nodiscard]] auto decrypt(std::string_view token) -> error_type; | ||||||
|  |  | ||||||
|   void encode(const void *buffer, std::size_t size, bool should_reserve = true); |   void encode(const void *buffer, std::size_t size, bool should_reserve = true); | ||||||
|  |  | ||||||
|   void encode(const char *str) { |   void encode(const char *str) { | ||||||
|     encode(std::string(str == nullptr ? "" : str)); |     encode(std::string{str == nullptr ? "" : str}); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   void encode(const std::string &str); |   void encode(std::string_view str); | ||||||
|  |  | ||||||
|   void encode(const wchar_t *str); |   void encode(const wchar_t *str); | ||||||
|  |  | ||||||
|   void encode(const std::wstring &str); |   void encode(std::wstring_view str); | ||||||
|  |  | ||||||
|   void encode(void *ptr) { |   void encode(void *ptr) { | ||||||
|     encode(static_cast<std::uint64_t>(reinterpret_cast<std::uintptr_t>(ptr))); |     encode(static_cast<std::uint64_t>(reinterpret_cast<std::uintptr_t>(ptr))); | ||||||
| @@ -161,9 +161,9 @@ public: | |||||||
|   void encode_top(const void *buffer, std::size_t size, |   void encode_top(const void *buffer, std::size_t size, | ||||||
|                   bool should_reserve = true); |                   bool should_reserve = true); | ||||||
|  |  | ||||||
|   void encode_top(const std::string &str); |   void encode_top(std::string_view str); | ||||||
|  |  | ||||||
|   void encode_top(const std::wstring &str); |   void encode_top(std::wstring_view str); | ||||||
|  |  | ||||||
|   void encode_top(void *ptr) { |   void encode_top(void *ptr) { | ||||||
|     encode_top( |     encode_top( | ||||||
| @@ -200,7 +200,7 @@ public: | |||||||
|  |  | ||||||
|   void encode_top(remote::file_info val); |   void encode_top(remote::file_info val); | ||||||
|  |  | ||||||
|   void encrypt(const std::string &token); |   void encrypt(std::string_view token); | ||||||
|  |  | ||||||
|   [[nodiscard]] auto get_size() const -> std::uint32_t { |   [[nodiscard]] auto get_size() const -> std::uint32_t { | ||||||
|     return static_cast<std::uint32_t>(buffer_.size()); |     return static_cast<std::uint32_t>(buffer_.size()); | ||||||
| @@ -217,11 +217,12 @@ public: | |||||||
|  |  | ||||||
|   auto operator=(packet &&pkt) noexcept -> packet &; |   auto operator=(packet &&pkt) noexcept -> packet &; | ||||||
|  |  | ||||||
|   [[nodiscard]] auto operator[](std::size_t index) -> char & { |   [[nodiscard]] auto operator[](std::size_t index) -> unsigned char & { | ||||||
|     return buffer_[index]; |     return buffer_[index]; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   [[nodiscard]] auto operator[](std::size_t index) const -> const char & { |   [[nodiscard]] auto operator[](std::size_t index) const -> const | ||||||
|  |       unsigned char & { | ||||||
|     return buffer_.at(index); |     return buffer_.at(index); | ||||||
|   } |   } | ||||||
| }; | }; | ||||||
|   | |||||||
| @@ -1243,8 +1243,9 @@ public: | |||||||
|              remote::file_handle handle{}; |              remote::file_handle handle{}; | ||||||
|              DECODE_OR_RETURN(request, handle); |              DECODE_OR_RETURN(request, handle); | ||||||
|  |  | ||||||
|              ret = this->fuse_write(path.data(), buffer.data(), write_size, |              ret = this->fuse_write( | ||||||
|                                     write_offset, handle); |                  path.data(), reinterpret_cast<const char *>(buffer.data()), | ||||||
|  |                  write_size, write_offset, handle); | ||||||
|            } |            } | ||||||
|            return ret; |            return ret; | ||||||
|          }}); |          }}); | ||||||
| @@ -1268,7 +1269,7 @@ public: | |||||||
|            data_buffer buffer(write_size); |            data_buffer buffer(write_size); | ||||||
|            if ((ret = request->decode(buffer.data(), buffer.size())) == 0) { |            if ((ret = request->decode(buffer.data(), buffer.size())) == 0) { | ||||||
|              buffer = macaron::Base64::Decode( |              buffer = macaron::Base64::Decode( | ||||||
|                  std::string(buffer.begin(), buffer.end())); |                  std::string{buffer.begin(), buffer.end()}); | ||||||
|              write_size = buffer.size(); |              write_size = buffer.size(); | ||||||
|  |  | ||||||
|              remote::file_offset write_offset{}; |              remote::file_offset write_offset{}; | ||||||
| @@ -1277,8 +1278,9 @@ public: | |||||||
|              remote::file_handle handle{}; |              remote::file_handle handle{}; | ||||||
|              DECODE_OR_RETURN(request, handle); |              DECODE_OR_RETURN(request, handle); | ||||||
|  |  | ||||||
|              ret = this->fuse_write(path.data(), buffer.data(), write_size, |              ret = this->fuse_write(path.data(), | ||||||
|                                     write_offset, handle); |                                     reinterpret_cast<char *>(buffer.data()), | ||||||
|  |                                     write_size, write_offset, handle); | ||||||
|            } |            } | ||||||
|            return ret; |            return ret; | ||||||
|          }}); |          }}); | ||||||
|   | |||||||
| @@ -274,7 +274,7 @@ struct s3_config { | |||||||
|   std::string bucket; |   std::string bucket; | ||||||
|   std::uint16_t cache_timeout_secs{60U}; |   std::uint16_t cache_timeout_secs{60U}; | ||||||
|   std::string encryption_token; |   std::string encryption_token; | ||||||
|   std::string region = "any"; |   std::string region{"any"}; | ||||||
|   std::string secret_key; |   std::string secret_key; | ||||||
|   std::uint32_t timeout_ms{60000U}; |   std::uint32_t timeout_ms{60000U}; | ||||||
|   std::string url; |   std::string url; | ||||||
| @@ -282,7 +282,12 @@ struct s3_config { | |||||||
|   bool use_region_in_url{false}; |   bool use_region_in_url{false}; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| using data_buffer = std::vector<char>; | using data_buffer = std::vector<unsigned char>; | ||||||
|  | using key_type = std::array<unsigned char, 32U>; | ||||||
|  | using mutex_lock = std::lock_guard<std::mutex>; | ||||||
|  | using recur_mutex_lock = std::lock_guard<std::recursive_mutex>; | ||||||
|  | using unique_mutex_lock = std::unique_lock<std::mutex>; | ||||||
|  | using unique_recur_mutex_lock = std::unique_lock<std::recursive_mutex>; | ||||||
|  |  | ||||||
| using api_file_list = std::vector<api_file>; | using api_file_list = std::vector<api_file>; | ||||||
| using api_file_provider_callback = std::function<void(api_file &)>; | using api_file_provider_callback = std::function<void(api_file &)>; | ||||||
| @@ -292,11 +297,7 @@ using http_headers = std::unordered_map<std::string, std::string>; | |||||||
| using http_parameters = std::unordered_map<std::string, std::string>; | using http_parameters = std::unordered_map<std::string, std::string>; | ||||||
| using http_ranges = std::vector<http_range>; | using http_ranges = std::vector<http_range>; | ||||||
| using meta_provider_callback = std::function<void(directory_item &)>; | using meta_provider_callback = std::function<void(directory_item &)>; | ||||||
| using mutex_lock = std::lock_guard<std::mutex>; |  | ||||||
| using query_parameters = std::map<std::string, std::string>; | using query_parameters = std::map<std::string, std::string>; | ||||||
| using recur_mutex_lock = std::lock_guard<std::recursive_mutex>; |  | ||||||
| using unique_mutex_lock = std::unique_lock<std::mutex>; |  | ||||||
| using unique_recur_mutex_lock = std::unique_lock<std::recursive_mutex>; |  | ||||||
| } // namespace repertory | } // namespace repertory | ||||||
|  |  | ||||||
| #endif // INCLUDE_TYPES_REPERTORY_HPP_ | #endif // INCLUDE_TYPES_REPERTORY_HPP_ | ||||||
|   | |||||||
| @@ -38,43 +38,45 @@ | |||||||
| #pragma GCC diagnostic ignored "-Wuseless-cast" | #pragma GCC diagnostic ignored "-Wuseless-cast" | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  | #include <array> | ||||||
| #include <string> | #include <string> | ||||||
| #include <vector> | #include <vector> | ||||||
|  |  | ||||||
| namespace macaron::Base64 { | namespace macaron::Base64 { | ||||||
| static std::string Encode(const char *data, const size_t &len) { | static std::string Encode(const unsigned char *data, std::size_t len) { | ||||||
|   static constexpr char sEncodingTable[] = { |   static constexpr std::array<unsigned char, 64U> sEncodingTable{ | ||||||
|       'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', |       'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', | ||||||
|       'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', |       'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', | ||||||
|       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', |       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', | ||||||
|       'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', |       'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', | ||||||
|       '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'}; |       '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', | ||||||
|  |   }; | ||||||
|  |  | ||||||
|   size_t in_len = len; |   auto in_len{len}; | ||||||
|   std::string ret; |   std::string ret; | ||||||
|   if (in_len > 0) { |   if (in_len > 0) { | ||||||
|     size_t out_len = 4 * ((in_len + 2) / 3); |     std::size_t out_len{4U * ((in_len + 2U) / 3U)}; | ||||||
|     ret = std::string(out_len, '\0'); |     ret = std::string(out_len, '\0'); | ||||||
|     size_t i; |     std::size_t i; | ||||||
|     char *p = const_cast<char *>(ret.c_str()); |     auto *p = reinterpret_cast<unsigned char *>(ret.data()); | ||||||
|  |  | ||||||
|     for (i = 0; i < in_len - 2; i += 3) { |     for (i = 0U; i < in_len - 2U; i += 3U) { | ||||||
|       *p++ = sEncodingTable[(data[i] >> 2) & 0x3F]; |       *p++ = sEncodingTable[(data[i] >> 2U) & 0x3F]; | ||||||
|       *p++ = sEncodingTable[((data[i] & 0x3) << 4) | |       *p++ = sEncodingTable[((data[i] & 0x3) << 4U) | | ||||||
|                             ((int)(data[i + 1] & 0xF0) >> 4)]; |                             ((int)(data[i + 1U] & 0xF0) >> 4U)]; | ||||||
|       *p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) | |       *p++ = sEncodingTable[((data[i + 1] & 0xF) << 2) | | ||||||
|                             ((int)(data[i + 2] & 0xC0) >> 6)]; |                             ((int)(data[i + 2U] & 0xC0) >> 6U)]; | ||||||
|       *p++ = sEncodingTable[data[i + 2] & 0x3F]; |       *p++ = sEncodingTable[data[i + 2U] & 0x3F]; | ||||||
|     } |     } | ||||||
|     if (i < in_len) { |     if (i < in_len) { | ||||||
|       *p++ = sEncodingTable[(data[i] >> 2) & 0x3F]; |       *p++ = sEncodingTable[(data[i] >> 2U) & 0x3F]; | ||||||
|       if (i == (in_len - 1)) { |       if (i == (in_len - 1U)) { | ||||||
|         *p++ = sEncodingTable[((data[i] & 0x3) << 4)]; |         *p++ = sEncodingTable[((data[i] & 0x3) << 4U)]; | ||||||
|         *p++ = '='; |         *p++ = '='; | ||||||
|       } else { |       } else { | ||||||
|         *p++ = sEncodingTable[((data[i] & 0x3) << 4) | |         *p++ = sEncodingTable[((data[i] & 0x3) << 4U) | | ||||||
|                               ((int)(data[i + 1] & 0xF0) >> 4)]; |                               ((int)(data[i + 1U] & 0xF0) >> 4U)]; | ||||||
|         *p++ = sEncodingTable[((data[i + 1] & 0xF) << 2)]; |         *p++ = sEncodingTable[((data[i + 1U] & 0xF) << 2U)]; | ||||||
|       } |       } | ||||||
|       *p++ = '='; |       *p++ = '='; | ||||||
|     } |     } | ||||||
| @@ -83,12 +85,14 @@ static std::string Encode(const char *data, const size_t &len) { | |||||||
|   return ret; |   return ret; | ||||||
| } | } | ||||||
|  |  | ||||||
| [[maybe_unused]] static std::string Encode(const std::string &data) { | [[maybe_unused]] static std::string Encode(std::string_view data) { | ||||||
|   return Encode(&data[0], data.size()); |   return Encode(reinterpret_cast<const unsigned char *>(data.data()), | ||||||
|  |                 data.size()); | ||||||
| } | } | ||||||
|  |  | ||||||
| [[maybe_unused]] static std::vector<char> Decode(const std::string &input) { | [[maybe_unused]] static std::vector<unsigned char> | ||||||
|   static constexpr unsigned char kDecodingTable[] = { | Decode(std::string_view input) { | ||||||
|  |   static constexpr std::array<unsigned char, 256> kDecodingTable{ | ||||||
|       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||||||
|       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||||||
|       64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, 52, 53, 54, 55, 56, 57, |       64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, 52, 53, 54, 55, 56, 57, | ||||||
| @@ -103,45 +107,49 @@ static std::string Encode(const char *data, const size_t &len) { | |||||||
|       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||||||
|       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||||||
|       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |       64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, | ||||||
|       64, 64, 64, 64}; |       64, 64, 64, 64, | ||||||
|  |   }; | ||||||
|  |  | ||||||
|   std::vector<char> out; |   std::vector<unsigned char> out; | ||||||
|   if (not input.empty()) { |   if (not input.empty()) { | ||||||
|     size_t in_len = input.size(); |     auto in_len{input.size()}; | ||||||
|     if (in_len % 4 != 0) |     if (in_len % 4U != 0U) { | ||||||
|       throw std::runtime_error("Input data size is not a multiple of 4"); |       throw std::runtime_error("Input data size is not a multiple of 4"); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     size_t out_len = in_len / 4 * 3; |     std::size_t out_len{in_len / 4U * 3U}; | ||||||
|     if (input[in_len - 1] == '=') |     if (input[in_len - 1U] == '=') { | ||||||
|       out_len--; |       out_len--; | ||||||
|     if (input[in_len - 2] == '=') |     } | ||||||
|  |     if (input[in_len - 2U] == '=') { | ||||||
|       out_len--; |       out_len--; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     out.resize(out_len); |     out.resize(out_len); | ||||||
|  |  | ||||||
|     for (size_t i = 0, j = 0; i < in_len;) { |     for (std::size_t i = 0U, j = 0U; i < in_len;) { | ||||||
|       uint32_t a = input[i] == '=' |       std::uint32_t a = input[i] == '=' | ||||||
|                        ? 0 & i++ |                             ? 0U & i++ | ||||||
|                        : kDecodingTable[static_cast<int>(input[i++])]; |                             : kDecodingTable[static_cast<int>(input[i++])]; | ||||||
|       uint32_t b = input[i] == '=' |       std::uint32_t b = input[i] == '=' | ||||||
|                        ? 0 & i++ |                             ? 0U & i++ | ||||||
|                        : kDecodingTable[static_cast<int>(input[i++])]; |                             : kDecodingTable[static_cast<int>(input[i++])]; | ||||||
|       uint32_t c = input[i] == '=' |       std::uint32_t c = input[i] == '=' | ||||||
|                        ? 0 & i++ |                             ? 0U & i++ | ||||||
|                        : kDecodingTable[static_cast<int>(input[i++])]; |                             : kDecodingTable[static_cast<int>(input[i++])]; | ||||||
|       uint32_t d = input[i] == '=' |       std::uint32_t d = input[i] == '=' | ||||||
|                        ? 0 & i++ |                             ? 0U & i++ | ||||||
|                        : kDecodingTable[static_cast<int>(input[i++])]; |                             : kDecodingTable[static_cast<int>(input[i++])]; | ||||||
|  |  | ||||||
|       uint32_t triple = |       std::uint32_t triple = | ||||||
|           (a << 3 * 6) + (b << 2 * 6) + (c << 1 * 6) + (d << 0 * 6); |           (a << 3U * 6U) + (b << 2U * 6U) + (c << 1U * 6U) + (d << 0U * 6U); | ||||||
|  |  | ||||||
|       if (j < out_len) |       if (j < out_len) | ||||||
|         out[j++] = (triple >> 2 * 8) & 0xFF; |         out[j++] = (triple >> 2U * 8U) & 0xFF; | ||||||
|       if (j < out_len) |       if (j < out_len) | ||||||
|         out[j++] = (triple >> 1 * 8) & 0xFF; |         out[j++] = (triple >> 1U * 8U) & 0xFF; | ||||||
|       if (j < out_len) |       if (j < out_len) | ||||||
|         out[j++] = (triple >> 0 * 8) & 0xFF; |         out[j++] = (triple >> 0U * 8U) & 0xFF; | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -26,23 +26,20 @@ | |||||||
| #include "utils/file_utils.hpp" | #include "utils/file_utils.hpp" | ||||||
|  |  | ||||||
| namespace repertory::utils::encryption { | namespace repertory::utils::encryption { | ||||||
| using key_type = std::array<unsigned char, 32U>; |  | ||||||
|  |  | ||||||
| class encrypting_reader final { | class encrypting_reader final { | ||||||
| public: | public: | ||||||
|   encrypting_reader(const std::string &file_name, |   encrypting_reader(std::string_view file_name, std::string_view source_path, | ||||||
|                     const std::string &source_path, stop_type &stop_requested, |                     stop_type &stop_requested, std::string_view token, | ||||||
|                     const std::string &token, |                     std::optional<std::string_view> relative_parent_path, | ||||||
|                     std::optional<std::string> relative_parent_path, |  | ||||||
|                     std::size_t error_return = 0U); |                     std::size_t error_return = 0U); | ||||||
|  |  | ||||||
|   encrypting_reader(const std::string &encrypted_file_path, |   encrypting_reader(std::string_view encrypted_file_path, | ||||||
|                     const std::string &source_path, stop_type &stop_requested, |                     std::string_view source_path, stop_type &stop_requested, | ||||||
|                     const std::string &token, std::size_t error_return = 0U); |                     std::string_view token, std::size_t error_return = 0U); | ||||||
|  |  | ||||||
|   encrypting_reader( |   encrypting_reader( | ||||||
|       const std::string &encrypted_file_path, const std::string &source_path, |       std::string_view encrypted_file_path, std::string_view source_path, | ||||||
|       stop_type &stop_requested, const std::string &token, |       stop_type &stop_requested, std::string_view token, | ||||||
|       std::vector<std::array<unsigned char, |       std::vector<std::array<unsigned char, | ||||||
|                              crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>> |                              crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>> | ||||||
|           iv_list, |           iv_list, | ||||||
| @@ -61,9 +58,9 @@ public: | |||||||
|   using streambuf = std::basic_streambuf<char, std::char_traits<char>>; |   using streambuf = std::basic_streambuf<char, std::char_traits<char>>; | ||||||
|  |  | ||||||
| private: | private: | ||||||
|   const key_type key_; |   key_type key_; | ||||||
|   stop_type &stop_requested_; |   stop_type &stop_requested_; | ||||||
|   const size_t error_return_; |   size_t error_return_; | ||||||
|   std::unordered_map<std::size_t, data_buffer> chunk_buffers_; |   std::unordered_map<std::size_t, data_buffer> chunk_buffers_; | ||||||
|   std::string encrypted_file_name_; |   std::string encrypted_file_name_; | ||||||
|   std::string encrypted_file_path_; |   std::string encrypted_file_path_; | ||||||
| @@ -85,16 +82,16 @@ private: | |||||||
|   auto reader_function(char *buffer, size_t size, size_t nitems) -> size_t; |   auto reader_function(char *buffer, size_t size, size_t nitems) -> size_t; | ||||||
|  |  | ||||||
| public: | public: | ||||||
|   [[nodiscard]] static auto calculate_decrypted_size(std::uint64_t total_size) |   [[nodiscard]] static auto | ||||||
|       -> std::uint64_t; |   calculate_decrypted_size(std::uint64_t total_size) -> std::uint64_t; | ||||||
|  |  | ||||||
|   [[nodiscard]] static auto |   [[nodiscard]] static auto | ||||||
|   calculate_encrypted_size(const std::string &source_path) -> std::uint64_t; |   calculate_encrypted_size(std::string_view source_path) -> std::uint64_t; | ||||||
|  |  | ||||||
|   [[nodiscard]] auto create_iostream() const -> std::shared_ptr<iostream>; |   [[nodiscard]] auto create_iostream() const -> std::shared_ptr<iostream>; | ||||||
|  |  | ||||||
|   [[nodiscard]] static constexpr auto get_encrypted_chunk_size() |   [[nodiscard]] static constexpr auto | ||||||
|       -> std::size_t { |   get_encrypted_chunk_size() -> std::size_t { | ||||||
|     return encrypted_chunk_size_; |     return encrypted_chunk_size_; | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -118,8 +115,9 @@ public: | |||||||
|     return header_size_; |     return header_size_; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   [[nodiscard]] auto get_iv_list() -> std::vector< |   [[nodiscard]] auto get_iv_list() | ||||||
|       std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>> { |       -> std::vector<std::array<unsigned char, | ||||||
|  |                                 crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>> { | ||||||
|     return iv_list_; |     return iv_list_; | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -132,8 +130,8 @@ public: | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   [[nodiscard]] static auto reader_function(char *buffer, size_t size, |   [[nodiscard]] static auto reader_function(char *buffer, size_t size, | ||||||
|                                             size_t nitems, void *instream) |                                             size_t nitems, | ||||||
|       -> size_t { |                                             void *instream) -> size_t { | ||||||
|     return reinterpret_cast<encrypting_reader *>(instream)->reader_function( |     return reinterpret_cast<encrypting_reader *>(instream)->reader_function( | ||||||
|         buffer, size, nitems); |         buffer, size, nitems); | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -30,25 +30,24 @@ using reader_func = std::function<api_error(data_buffer &cypher_text, | |||||||
|                                             std::uint64_t start_offset, |                                             std::uint64_t start_offset, | ||||||
|                                             std::uint64_t end_offset)>; |                                             std::uint64_t end_offset)>; | ||||||
| // Prototypes | // Prototypes | ||||||
| [[nodiscard]] auto decrypt_file_path(const std::string &encryption_token, | [[nodiscard]] auto decrypt_file_path(std::string_view encryption_token, | ||||||
|                                      std::string &file_path) -> api_error; |                                      std::string &file_path) -> api_error; | ||||||
|  |  | ||||||
| [[nodiscard]] auto decrypt_file_name(const std::string &encryption_token, | [[nodiscard]] auto decrypt_file_name(std::string_view encryption_token, | ||||||
|                                      std::string &file_name) -> api_error; |                                      std::string &file_name) -> api_error; | ||||||
|  |  | ||||||
| [[nodiscard]] auto generate_key(const std::string &encryption_token) |  | ||||||
|     -> key_type; |  | ||||||
|  |  | ||||||
| [[nodiscard]] auto read_encrypted_range(const http_range &range, | [[nodiscard]] auto read_encrypted_range(const http_range &range, | ||||||
|                                         const key_type &key, reader_func reader, |                                         const key_type &key, reader_func reader, | ||||||
|                                         std::uint64_t total_size, |                                         std::uint64_t total_size, | ||||||
|                                         data_buffer &data) -> api_error; |                                         data_buffer &data) -> api_error; | ||||||
|  |  | ||||||
|  | [[nodiscard]] auto generate_key(std::string_view encryption_token) -> key_type; | ||||||
|  |  | ||||||
| // Implementations | // Implementations | ||||||
| template <typename result> | template <typename result> | ||||||
| [[nodiscard]] inline auto decrypt_data(const key_type &key, const char *buffer, | [[nodiscard]] inline auto | ||||||
|                                        std::size_t buffer_size, result &res) | decrypt_data(const key_type &key, const unsigned char *buffer, | ||||||
|     -> bool { |              std::size_t buffer_size, result &res) -> bool { | ||||||
|   const auto header_size = |   const auto header_size = | ||||||
|       static_cast<std::uint32_t>(encrypting_reader::get_header_size()); |       static_cast<std::uint32_t>(encrypting_reader::get_header_size()); | ||||||
|   if (buffer_size > header_size) { |   if (buffer_size > header_size) { | ||||||
| @@ -56,14 +55,11 @@ template <typename result> | |||||||
|         boost::endian::native_to_big(static_cast<std::uint32_t>(buffer_size)); |         boost::endian::native_to_big(static_cast<std::uint32_t>(buffer_size)); | ||||||
|     res.resize(buffer_size - header_size); |     res.resize(buffer_size - header_size); | ||||||
|     return crypto_aead_xchacha20poly1305_ietf_decrypt_detached( |     return crypto_aead_xchacha20poly1305_ietf_decrypt_detached( | ||||||
|                reinterpret_cast<unsigned char *>(&res[0u]), nullptr, |                reinterpret_cast<unsigned char *>(res.data()), nullptr, | ||||||
|                reinterpret_cast<const unsigned char *>(&buffer[header_size]), |                &buffer[header_size], res.size(), | ||||||
|                res.size(), |                &buffer[crypto_aead_xchacha20poly1305_IETF_NPUBBYTES], | ||||||
|                reinterpret_cast<const unsigned char *>( |  | ||||||
|                    &buffer[crypto_aead_xchacha20poly1305_IETF_NPUBBYTES]), |  | ||||||
|                reinterpret_cast<const unsigned char *>(&size), sizeof(size), |                reinterpret_cast<const unsigned char *>(&size), sizeof(size), | ||||||
|                reinterpret_cast<const unsigned char *>(buffer), |                buffer, key.data()) == 0; | ||||||
|                key.data()) == 0; |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   return false; |   return false; | ||||||
| @@ -72,20 +68,19 @@ template <typename result> | |||||||
| template <typename buffer, typename result> | template <typename buffer, typename result> | ||||||
| [[nodiscard]] inline auto decrypt_data(const key_type &key, const buffer &buf, | [[nodiscard]] inline auto decrypt_data(const key_type &key, const buffer &buf, | ||||||
|                                        result &res) -> bool { |                                        result &res) -> bool { | ||||||
|   return decrypt_data<result>(key, &buf[0u], buf.size(), res); |   return decrypt_data<result>(key, buf.data(), buf.size(), res); | ||||||
| } | } | ||||||
|  |  | ||||||
| template <typename buffer, typename result> | template <typename buffer, typename result> | ||||||
| [[nodiscard]] inline auto decrypt_data(const std::string &encryption_token, | [[nodiscard]] inline auto decrypt_data(std::string_view encryption_token, | ||||||
|                                        const buffer &buf, result &res) -> bool { |                                        const buffer &buf, result &res) -> bool { | ||||||
|   return decrypt_data<buffer, result>(generate_key(encryption_token), buf, res); |   return decrypt_data<buffer, result>(generate_key(encryption_token), buf, res); | ||||||
| } | } | ||||||
|  |  | ||||||
| template <typename result> | template <typename result> | ||||||
| [[nodiscard]] inline auto decrypt_data(const std::string &encryption_token, | [[nodiscard]] inline auto | ||||||
|                                        const char *buffer, | decrypt_data(std::string_view encryption_token, const unsigned char *buffer, | ||||||
|                                        std::size_t buffer_size, result &res) |              std::size_t buffer_size, result &res) -> bool { | ||||||
|     -> bool { |  | ||||||
|   return decrypt_data<result>(generate_key(encryption_token), buffer, |   return decrypt_data<result>(generate_key(encryption_token), buffer, | ||||||
|                               buffer_size, res); |                               buffer_size, res); | ||||||
| } | } | ||||||
| @@ -94,8 +89,8 @@ template <typename result> | |||||||
| inline void | inline void | ||||||
| encrypt_data(const std::array<unsigned char, | encrypt_data(const std::array<unsigned char, | ||||||
|                               crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> &iv, |                               crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> &iv, | ||||||
|              const key_type &key, const char *buffer, std::size_t buffer_size, |              const key_type &key, const unsigned char *buffer, | ||||||
|              result &res) { |              std::size_t buffer_size, result &res) { | ||||||
|   std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_ABYTES> mac{}; |   std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_ABYTES> mac{}; | ||||||
|  |  | ||||||
|   const auto header_size = |   const auto header_size = | ||||||
| @@ -109,18 +104,18 @@ encrypt_data(const std::array<unsigned char, | |||||||
|   unsigned long long mac_length{}; |   unsigned long long mac_length{}; | ||||||
|   if (crypto_aead_xchacha20poly1305_ietf_encrypt_detached( |   if (crypto_aead_xchacha20poly1305_ietf_encrypt_detached( | ||||||
|           reinterpret_cast<unsigned char *>(&res[header_size]), mac.data(), |           reinterpret_cast<unsigned char *>(&res[header_size]), mac.data(), | ||||||
|           &mac_length, reinterpret_cast<const unsigned char *>(buffer), |           &mac_length, buffer, buffer_size, | ||||||
|           buffer_size, reinterpret_cast<const unsigned char *>(&size), |           reinterpret_cast<const unsigned char *>(&size), sizeof(size), nullptr, | ||||||
|           sizeof(size), nullptr, iv.data(), key.data()) != 0) { |           iv.data(), key.data()) != 0) { | ||||||
|     throw std::runtime_error("encryption failed"); |     throw std::runtime_error("encryption failed"); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   std::memcpy(&res[0u], &iv[0u], iv.size()); |   std::memcpy(res.data(), iv.data(), iv.size()); | ||||||
|   std::memcpy(&res[iv.size()], &mac[0u], mac.size()); |   std::memcpy(&res[iv.size()], mac.data(), mac.size()); | ||||||
| } | } | ||||||
|  |  | ||||||
| template <typename result> | template <typename result> | ||||||
| inline void encrypt_data(const key_type &key, const char *buffer, | inline void encrypt_data(const key_type &key, const unsigned char *buffer, | ||||||
|                          std::size_t buffer_size, result &res) { |                          std::size_t buffer_size, result &res) { | ||||||
|   std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> iv{}; |   std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> iv{}; | ||||||
|   randombytes_buf(iv.data(), iv.size()); |   randombytes_buf(iv.data(), iv.size()); | ||||||
| @@ -129,23 +124,23 @@ inline void encrypt_data(const key_type &key, const char *buffer, | |||||||
| } | } | ||||||
|  |  | ||||||
| template <typename result> | template <typename result> | ||||||
| inline void encrypt_data(const std::string &encryption_token, | inline void encrypt_data(std::string_view encryption_token, | ||||||
|                          const char *buffer, std::size_t buffer_size, |                          const unsigned char *buffer, std::size_t buffer_size, | ||||||
|                          result &res) { |                          result &res) { | ||||||
|   encrypt_data<result>(generate_key(encryption_token), buffer, buffer_size, |   encrypt_data<result>(generate_key(encryption_token), buffer, buffer_size, | ||||||
|                        res); |                        res); | ||||||
| } | } | ||||||
|  |  | ||||||
| template <typename buffer, typename result> | template <typename buffer, typename result> | ||||||
| inline void encrypt_data(const std::string &encryption_token, const buffer &buf, | inline void encrypt_data(std::string_view encryption_token, const buffer &buf, | ||||||
|                          result &res) { |                          result &res) { | ||||||
|   encrypt_data<result>(generate_key(encryption_token), &buf[0u], buf.size(), |   encrypt_data<result>(generate_key(encryption_token), buf.data(), buf.size(), | ||||||
|                        res); |                        res); | ||||||
| } | } | ||||||
|  |  | ||||||
| template <typename buffer, typename result> | template <typename buffer, typename result> | ||||||
| inline void encrypt_data(const key_type &key, const buffer &buf, result &res) { | inline void encrypt_data(const key_type &key, const buffer &buf, result &res) { | ||||||
|   encrypt_data<result>(key, &buf[0u], buf.size(), res); |   encrypt_data<result>(key, buf.data(), buf.size(), res); | ||||||
| } | } | ||||||
|  |  | ||||||
| template <typename buffer, typename result> | template <typename buffer, typename result> | ||||||
| @@ -153,7 +148,7 @@ inline void | |||||||
| encrypt_data(const std::array<unsigned char, | encrypt_data(const std::array<unsigned char, | ||||||
|                               crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> &iv, |                               crypto_aead_xchacha20poly1305_IETF_NPUBBYTES> &iv, | ||||||
|              const key_type &key, const buffer &buf, result &res) { |              const key_type &key, const buffer &buf, result &res) { | ||||||
|   encrypt_data<result>(iv, key, &buf[0u], buf.size(), res); |   encrypt_data<result>(iv, key, buf.data(), buf.size(), res); | ||||||
| } | } | ||||||
| } // namespace repertory::utils::encryption | } // namespace repertory::utils::encryption | ||||||
|  |  | ||||||
|   | |||||||
| @@ -27,21 +27,21 @@ | |||||||
|  |  | ||||||
| namespace repertory::utils::file { | namespace repertory::utils::file { | ||||||
| // Prototypes | // Prototypes | ||||||
| [[nodiscard]] auto calculate_used_space(std::string path, bool recursive) | [[nodiscard]] auto calculate_used_space(std::string path, | ||||||
|     -> std::uint64_t; |                                         bool recursive) -> std::uint64_t; | ||||||
|  |  | ||||||
| void change_to_process_directory(); | void change_to_process_directory(); | ||||||
|  |  | ||||||
| [[nodiscard]] auto copy_directory_recursively(std::string from_path, | [[nodiscard]] auto copy_directory_recursively(std::string from_path, | ||||||
|                                               std::string to_path) -> bool; |                                               std::string to_path) -> bool; | ||||||
|  |  | ||||||
| [[nodiscard]] auto copy_file(std::string from_path, std::string to_path) | [[nodiscard]] auto copy_file(std::string from_path, | ||||||
|     -> bool; |                              std::string to_path) -> bool; | ||||||
|  |  | ||||||
| [[nodiscard]] auto create_full_directory_path(std::string path) -> bool; | [[nodiscard]] auto create_full_directory_path(std::string path) -> bool; | ||||||
|  |  | ||||||
| [[nodiscard]] auto delete_directory(std::string path, bool recursive = false) | [[nodiscard]] auto delete_directory(std::string path, | ||||||
|     -> bool; |                                     bool recursive = false) -> bool; | ||||||
|  |  | ||||||
| [[nodiscard]] auto delete_directory_recursively(std::string path) -> bool; | [[nodiscard]] auto delete_directory_recursively(std::string path) -> bool; | ||||||
|  |  | ||||||
| @@ -52,18 +52,18 @@ void change_to_process_directory(); | |||||||
| [[nodiscard]] auto get_accessed_time(const std::string &path, | [[nodiscard]] auto get_accessed_time(const std::string &path, | ||||||
|                                      std::uint64_t &accessed) -> bool; |                                      std::uint64_t &accessed) -> bool; | ||||||
|  |  | ||||||
| [[nodiscard]] auto get_directory_files(std::string path, bool oldest_first, | [[nodiscard]] auto | ||||||
|                                        bool recursive = false) | get_directory_files(std::string path, bool oldest_first, | ||||||
|     -> std::deque<std::string>; |                     bool recursive = false) -> std::deque<std::string>; | ||||||
|  |  | ||||||
| [[nodiscard]] auto get_free_drive_space(const std::string &path) | [[nodiscard]] auto | ||||||
|     -> std::uint64_t; | get_free_drive_space(const std::string &path) -> std::uint64_t; | ||||||
|  |  | ||||||
| [[nodiscard]] auto get_total_drive_space(const std::string &path) | [[nodiscard]] auto | ||||||
|     -> std::uint64_t; | get_total_drive_space(const std::string &path) -> std::uint64_t; | ||||||
|  |  | ||||||
| [[nodiscard]] auto get_file_size(std::string path, std::uint64_t &file_size) | [[nodiscard]] auto get_file_size(std::string_view path, | ||||||
|     -> bool; |                                  std::uint64_t &file_size) -> bool; | ||||||
|  |  | ||||||
| [[nodiscard]] auto get_modified_time(const std::string &path, | [[nodiscard]] auto get_modified_time(const std::string &path, | ||||||
|                                      std::uint64_t &modified) -> bool; |                                      std::uint64_t &modified) -> bool; | ||||||
| @@ -72,14 +72,14 @@ void change_to_process_directory(); | |||||||
|  |  | ||||||
| [[nodiscard]] auto is_file(const std::string &path) -> bool; | [[nodiscard]] auto is_file(const std::string &path) -> bool; | ||||||
|  |  | ||||||
| [[nodiscard]] auto is_modified_date_older_than(const std::string &path, | [[nodiscard]] auto | ||||||
|                                                const std::chrono::hours &hours) | is_modified_date_older_than(const std::string &path, | ||||||
|     -> bool; |                             const std::chrono::hours &hours) -> bool; | ||||||
|  |  | ||||||
| [[nodiscard]] auto move_file(std::string from, std::string to) -> bool; | [[nodiscard]] auto move_file(std::string from, std::string to) -> bool; | ||||||
|  |  | ||||||
| [[nodiscard]] auto read_file_lines(const std::string &path) | [[nodiscard]] auto | ||||||
|     -> std::vector<std::string>; | read_file_lines(const std::string &path) -> std::vector<std::string>; | ||||||
|  |  | ||||||
| [[nodiscard]] auto read_json_file(const std::string &path, json &data) -> bool; | [[nodiscard]] auto read_json_file(const std::string &path, json &data) -> bool; | ||||||
|  |  | ||||||
| @@ -89,8 +89,8 @@ void change_to_process_directory(); | |||||||
|  |  | ||||||
| [[nodiscard]] auto retry_delete_file(const std::string &file) -> bool; | [[nodiscard]] auto retry_delete_file(const std::string &file) -> bool; | ||||||
|  |  | ||||||
| [[nodiscard]] auto write_json_file(const std::string &path, const json &j) | [[nodiscard]] auto write_json_file(const std::string &path, | ||||||
|     -> bool; |                                    const json &j) -> bool; | ||||||
| } // namespace repertory::utils::file | } // namespace repertory::utils::file | ||||||
|  |  | ||||||
| #endif // INCLUDE_UTILS_FILE_UTILS_HPP_ | #endif // INCLUDE_UTILS_FILE_UTILS_HPP_ | ||||||
|   | |||||||
| @@ -42,17 +42,17 @@ public: | |||||||
|   [[nodiscard]] static auto |   [[nodiscard]] static auto | ||||||
|   clone(const native_file_ptr &ptr) -> native_file_ptr; |   clone(const native_file_ptr &ptr) -> native_file_ptr; | ||||||
|  |  | ||||||
|   [[nodiscard]] static auto create_or_open(const std::string &source_path, |   [[nodiscard]] static auto create_or_open(std::string_view source_path, | ||||||
|                                            bool read_only, |                                            bool read_only, | ||||||
|                                            native_file_ptr &ptr) -> api_error; |                                            native_file_ptr &ptr) -> api_error; | ||||||
|  |  | ||||||
|   [[nodiscard]] static auto create_or_open(const std::string &source_path, |   [[nodiscard]] static auto create_or_open(std::string_view source_path, | ||||||
|                                            native_file_ptr &ptr) -> api_error; |                                            native_file_ptr &ptr) -> api_error; | ||||||
|  |  | ||||||
|   [[nodiscard]] static auto open(const std::string &source_path, |   [[nodiscard]] static auto open(std::string_view source_path, | ||||||
|                                  native_file_ptr &ptr) -> api_error; |                                  native_file_ptr &ptr) -> api_error; | ||||||
|  |  | ||||||
|   [[nodiscard]] static auto open(const std::string &source_path, bool read_only, |   [[nodiscard]] static auto open(std::string_view source_path, bool read_only, | ||||||
|                                  native_file_ptr &ptr) -> api_error; |                                  native_file_ptr &ptr) -> api_error; | ||||||
|  |  | ||||||
| private: | private: | ||||||
| @@ -86,11 +86,11 @@ public: | |||||||
|   [[nodiscard]] auto get_handle() -> native_handle; |   [[nodiscard]] auto get_handle() -> native_handle; | ||||||
|  |  | ||||||
| #if defined(_WIN32) | #if defined(_WIN32) | ||||||
|   [[nodiscard]] auto read_bytes(char *buffer, std::size_t read_size, |   [[nodiscard]] auto read_bytes(unsigned char *buffer, std::size_t read_size, | ||||||
|                                 std::uint64_t read_offset, |                                 std::uint64_t read_offset, | ||||||
|                                 std::size_t &bytes_read) -> bool; |                                 std::size_t &bytes_read) -> bool; | ||||||
| #else | #else | ||||||
|   [[nodiscard]] auto read_bytes(char *buffer, std::size_t read_size, |   [[nodiscard]] auto read_bytes(unsigned char *buffer, std::size_t read_size, | ||||||
|                                 std::uint64_t read_offset, |                                 std::uint64_t read_offset, | ||||||
|                                 std::size_t &bytes_read) -> bool; |                                 std::size_t &bytes_read) -> bool; | ||||||
| #endif | #endif | ||||||
| @@ -99,11 +99,13 @@ public: | |||||||
|   [[nodiscard]] auto truncate(std::uint64_t file_size) -> bool; |   [[nodiscard]] auto truncate(std::uint64_t file_size) -> bool; | ||||||
|  |  | ||||||
| #if defined(_WIN32) | #if defined(_WIN32) | ||||||
|   [[nodiscard]] auto write_bytes(const char *buffer, std::size_t write_size, |   [[nodiscard]] auto write_bytes(const unsigned char *buffer, | ||||||
|  |                                  std::size_t write_size, | ||||||
|                                  std::uint64_t write_offset, |                                  std::uint64_t write_offset, | ||||||
|                                  std::size_t &bytes_written) -> bool; |                                  std::size_t &bytes_written) -> bool; | ||||||
| #else | #else | ||||||
|   [[nodiscard]] auto write_bytes(const char *buffer, std::size_t write_size, |   [[nodiscard]] auto write_bytes(const unsigned char *buffer, | ||||||
|  |                                  std::size_t write_size, | ||||||
|                                  std::uint64_t write_offset, |                                  std::uint64_t write_offset, | ||||||
|                                  std::size_t &bytes_written) -> bool; |                                  std::size_t &bytes_written) -> bool; | ||||||
| #endif | #endif | ||||||
|   | |||||||
| @@ -32,29 +32,28 @@ constexpr auto contains(std::string_view str, std::string_view search) -> bool { | |||||||
|   return (str.find(search) != std::string_view::npos); |   return (str.find(search) != std::string_view::npos); | ||||||
| } | } | ||||||
|  |  | ||||||
| [[nodiscard]] /* constexpr c++20 */ auto ends_with(std::string_view str, | [[nodiscard]] /* constexpr c++20 */ auto | ||||||
|                                                    std::string_view val) | ends_with(std::string_view str, std::string_view val) -> bool; | ||||||
|     -> bool; |  | ||||||
|  |  | ||||||
| [[nodiscard]] auto from_bool(bool val) -> std::string; | [[nodiscard]] auto from_bool(bool val) -> std::string; | ||||||
|  |  | ||||||
| [[nodiscard]] auto from_dynamic_bitset(const boost::dynamic_bitset<> &bitset) | [[nodiscard]] auto | ||||||
|     -> std::string; | from_dynamic_bitset(const boost::dynamic_bitset<> &bitset) -> std::string; | ||||||
|  |  | ||||||
| [[nodiscard]] auto from_utf8(const std::string &str) -> std::wstring; | [[nodiscard]] auto from_utf8(std::string_view str) -> std::wstring; | ||||||
|  |  | ||||||
| [[nodiscard]] /* constexpr c++20 */ auto is_numeric(std::string_view str) | [[nodiscard]] /* constexpr c++20 */ auto | ||||||
|     -> bool; | is_numeric(std::string_view str) -> bool; | ||||||
|  |  | ||||||
| [[nodiscard]] auto join(const std::vector<std::string> &arr, const char &delim) | [[nodiscard]] auto join(const std::vector<std::string> &arr, | ||||||
|     -> std::string; |                         const char &delim) -> std::string; | ||||||
|  |  | ||||||
| auto left_trim(std::string &str) -> std::string &; | auto left_trim(std::string &str) -> std::string &; | ||||||
|  |  | ||||||
| auto left_trim(std::string &str, const char &trim_ch) -> std::string &; | auto left_trim(std::string &str, const char &trim_ch) -> std::string &; | ||||||
|  |  | ||||||
| auto replace(std::string &src, const char &character, const char &with) | auto replace(std::string &src, const char &character, | ||||||
|     -> std::string &; |              const char &with) -> std::string &; | ||||||
|  |  | ||||||
| auto replace(std::string &src, const std::string &find, const std::string &with, | auto replace(std::string &src, const std::string &find, const std::string &with, | ||||||
|              size_t start_pos = 0) -> std::string &; |              size_t start_pos = 0) -> std::string &; | ||||||
| @@ -63,8 +62,8 @@ auto replace(std::string &src, const std::string &find, const std::string &with, | |||||||
|                                 const char &with) -> std::string; |                                 const char &with) -> std::string; | ||||||
|  |  | ||||||
| [[nodiscard]] auto replace_copy(std::string src, const std::string &find, | [[nodiscard]] auto replace_copy(std::string src, const std::string &find, | ||||||
|                                 const std::string &with, size_t start_pos = 0) |                                 const std::string &with, | ||||||
|     -> std::string; |                                 size_t start_pos = 0) -> std::string; | ||||||
|  |  | ||||||
| auto right_trim(std::string &str) -> std::string &; | auto right_trim(std::string &str) -> std::string &; | ||||||
|  |  | ||||||
| @@ -77,8 +76,8 @@ auto right_trim(std::string &str, const char &trim_ch) -> std::string &; | |||||||
|  |  | ||||||
| [[nodiscard]] auto to_double(const std::string &str) -> double; | [[nodiscard]] auto to_double(const std::string &str) -> double; | ||||||
|  |  | ||||||
| [[nodiscard]] auto to_dynamic_bitset(const std::string &val) | [[nodiscard]] auto | ||||||
|     -> boost::dynamic_bitset<>; | to_dynamic_bitset(const std::string &val) -> boost::dynamic_bitset<>; | ||||||
|  |  | ||||||
| [[nodiscard]] auto to_lower(std::string str) -> std::string; | [[nodiscard]] auto to_lower(std::string str) -> std::string; | ||||||
|  |  | ||||||
| @@ -98,9 +97,9 @@ auto right_trim(std::string &str, const char &trim_ch) -> std::string &; | |||||||
|  |  | ||||||
| [[nodiscard]] auto to_upper(std::string str) -> std::string; | [[nodiscard]] auto to_upper(std::string str) -> std::string; | ||||||
|  |  | ||||||
| [[nodiscard]] auto to_utf8(std::string str) -> std::string; | [[nodiscard]] auto to_utf8(std::string_view str) -> std::string; | ||||||
|  |  | ||||||
| [[nodiscard]] auto to_utf8(const std::wstring &str) -> std::string; | [[nodiscard]] auto to_utf8(std::wstring_view str) -> std::string; | ||||||
|  |  | ||||||
| auto trim(std::string &str) -> std::string &; | auto trim(std::string &str) -> std::string &; | ||||||
|  |  | ||||||
| @@ -108,8 +107,8 @@ auto trim(std::string &str, const char &trim_ch) -> std::string &; | |||||||
|  |  | ||||||
| [[nodiscard]] auto trim_copy(std::string str) -> std::string; | [[nodiscard]] auto trim_copy(std::string str) -> std::string; | ||||||
|  |  | ||||||
| [[nodiscard]] auto trim_copy(std::string str, const char &trim_ch) | [[nodiscard]] auto trim_copy(std::string str, | ||||||
|     -> std::string; |                              const char &trim_ch) -> std::string; | ||||||
| } // namespace repertory::utils::string | } // namespace repertory::utils::string | ||||||
|  |  | ||||||
| #endif // INCLUDE_UTILS_STRING_UTILS_HPP_ | #endif // INCLUDE_UTILS_STRING_UTILS_HPP_ | ||||||
|   | |||||||
| @@ -36,9 +36,9 @@ void packet::clear() { | |||||||
| } | } | ||||||
|  |  | ||||||
| auto packet::decode(std::string &data) -> packet::error_type { | auto packet::decode(std::string &data) -> packet::error_type { | ||||||
|   const auto *str = &buffer_[decode_offset_]; |   const auto *str = reinterpret_cast<const char *>(&buffer_[decode_offset_]); | ||||||
|   const auto length = strnlen(str, buffer_.size() - decode_offset_); |   const auto length = strnlen(str, buffer_.size() - decode_offset_); | ||||||
|   data = std::string(str, length); |   data = std::string{str, length}; | ||||||
|   decode_offset_ += (length + 1); |   decode_offset_ += (length + 1); | ||||||
|  |  | ||||||
|   return utils::from_api_error(api_error::success); |   return utils::from_api_error(api_error::success); | ||||||
| @@ -236,7 +236,7 @@ auto packet::decode_json(packet &response, json &json_data) -> int { | |||||||
|   return ret; |   return ret; | ||||||
| } | } | ||||||
|  |  | ||||||
| auto packet::decrypt(const std::string &token) -> packet::error_type { | auto packet::decrypt(std::string_view token) -> packet::error_type { | ||||||
|   constexpr const auto *function_name = static_cast<const char *>(__FUNCTION__); |   constexpr const auto *function_name = static_cast<const char *>(__FUNCTION__); | ||||||
|  |  | ||||||
|   auto ret = utils::from_api_error(api_error::success); |   auto ret = utils::from_api_error(api_error::success); | ||||||
| @@ -267,10 +267,10 @@ void packet::encode(const void *buffer, std::size_t size, bool should_reserve) { | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| void packet::encode(const std::string &str) { | void packet::encode(std::string_view str) { | ||||||
|   const auto len = strnlen(str.c_str(), str.size()); |   const auto len = str.size(); | ||||||
|   buffer_.reserve(len + 1 + buffer_.size()); |   buffer_.reserve(len + 1 + buffer_.size()); | ||||||
|   encode(str.c_str(), len, false); |   encode(str.data(), len, false); | ||||||
|   buffer_.emplace_back(0); |   buffer_.emplace_back(0); | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -278,7 +278,7 @@ void packet::encode(const wchar_t *str) { | |||||||
|   encode(utils::string::to_utf8(str == nullptr ? L"" : str)); |   encode(utils::string::to_utf8(str == nullptr ? L"" : str)); | ||||||
| } | } | ||||||
|  |  | ||||||
| void packet::encode(const std::wstring &str) { | void packet::encode(std::wstring_view str) { | ||||||
|   encode(utils::string::to_utf8(str)); |   encode(utils::string::to_utf8(str)); | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -400,14 +400,14 @@ void packet::encode_top(const void *buffer, std::size_t size, | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| void packet::encode_top(const std::string &str) { | void packet::encode_top(std::string_view str) { | ||||||
|   const auto len = strnlen(str.c_str(), str.size()); |   const auto len = str.size(); | ||||||
|   buffer_.reserve(len + 1U + buffer_.size()); |   buffer_.reserve(len + 1U + buffer_.size()); | ||||||
|   encode_top(str.c_str(), len, false); |   encode_top(str.data(), len, false); | ||||||
|   buffer_.insert(buffer_.begin() + static_cast<std::int32_t>(len), 0); |   buffer_.insert(buffer_.begin() + static_cast<std::int32_t>(len), 0); | ||||||
| } | } | ||||||
|  |  | ||||||
| void packet::encode_top(const std::wstring &str) { | void packet::encode_top(std::wstring_view str) { | ||||||
|   encode_top(utils::string::to_utf8(str)); |   encode_top(utils::string::to_utf8(str)); | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -518,7 +518,7 @@ void packet::encode_top(remote::file_info val) { | |||||||
|   encode_top(&val, sizeof(val), true); |   encode_top(&val, sizeof(val), true); | ||||||
| } | } | ||||||
|  |  | ||||||
| void packet::encrypt(const std::string &token) { | void packet::encrypt(std::string_view token) { | ||||||
|   constexpr const auto *function_name = static_cast<const char *>(__FUNCTION__); |   constexpr const auto *function_name = static_cast<const char *>(__FUNCTION__); | ||||||
|  |  | ||||||
|   try { |   try { | ||||||
|   | |||||||
| @@ -1018,8 +1018,10 @@ auto fuse_drive::setxattr_impl(std::string api_path, const char *name, | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   return provider_.set_item_meta(api_path, attribute_name, |   return provider_.set_item_meta( | ||||||
|                                  macaron::Base64::Encode(value, size)); |       api_path, attribute_name, | ||||||
|  |       macaron::Base64::Encode(reinterpret_cast<const unsigned char *>(value), | ||||||
|  |                               size)); | ||||||
| } | } | ||||||
| #endif // HAS_SETXATTR | #endif // HAS_SETXATTR | ||||||
|  |  | ||||||
|   | |||||||
| @@ -691,7 +691,9 @@ auto encrypt_provider::process_directory_entry( | |||||||
|          part_idx++) { |          part_idx++) { | ||||||
|       data_buffer encrypted_data; |       data_buffer encrypted_data; | ||||||
|       utils::encryption::encrypt_data( |       utils::encryption::encrypt_data( | ||||||
|           cfg.encryption_token, encrypted_parts.at(part_idx).c_str(), |           cfg.encryption_token, | ||||||
|  |           reinterpret_cast<const unsigned char *>( | ||||||
|  |               encrypted_parts.at(part_idx).c_str()), | ||||||
|           strnlen(encrypted_parts.at(part_idx).c_str(), |           strnlen(encrypted_parts.at(part_idx).c_str(), | ||||||
|                   encrypted_parts.at(part_idx).size()), |                   encrypted_parts.at(part_idx).size()), | ||||||
|           encrypted_data); |           encrypted_data); | ||||||
| @@ -917,8 +919,9 @@ auto encrypt_provider::read_file_bytes(const std::string &api_path, | |||||||
|   info->reader->set_read_position(offset); |   info->reader->set_read_position(offset); | ||||||
|   data.resize(size); |   data.resize(size); | ||||||
|  |  | ||||||
|   const auto res = info->reader->reader_function(data.data(), 1U, data.size(), |   const auto res = | ||||||
|                                                  info->reader.get()); |       info->reader->reader_function(reinterpret_cast<char *>(data.data()), 1U, | ||||||
|  |                                     data.size(), info->reader.get()); | ||||||
|   if (res == 0) { |   if (res == 0) { | ||||||
|     return api_error::os_error; |     return api_error::os_error; | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -33,8 +33,8 @@ class encrypting_streambuf final : public encrypting_reader::streambuf { | |||||||
| public: | public: | ||||||
|   encrypting_streambuf(const encrypting_streambuf &) = default; |   encrypting_streambuf(const encrypting_streambuf &) = default; | ||||||
|   encrypting_streambuf(encrypting_streambuf &&) = delete; |   encrypting_streambuf(encrypting_streambuf &&) = delete; | ||||||
|   auto operator=(const encrypting_streambuf &) |   auto | ||||||
|       -> encrypting_streambuf & = delete; |   operator=(const encrypting_streambuf &) -> encrypting_streambuf & = delete; | ||||||
|   auto operator=(encrypting_streambuf &&) -> encrypting_streambuf & = delete; |   auto operator=(encrypting_streambuf &&) -> encrypting_streambuf & = delete; | ||||||
|  |  | ||||||
|   explicit encrypting_streambuf(const encrypting_reader &reader) |   explicit encrypting_streambuf(const encrypting_reader &reader) | ||||||
| @@ -81,10 +81,9 @@ protected: | |||||||
|     return encrypting_reader::streambuf::seekoff(off, dir, which); |     return encrypting_reader::streambuf::seekoff(off, dir, which); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   auto seekpos(pos_type pos, |   auto seekpos(pos_type pos, std::ios_base::openmode which = | ||||||
|                std::ios_base::openmode which = std::ios_base::out | |                                  std::ios_base::out | | ||||||
|                                                std::ios_base::in) |                                  std::ios_base::in) -> pos_type override { | ||||||
|       -> pos_type override { |  | ||||||
|     return seekoff(pos, std::ios_base::beg, which); |     return seekoff(pos, std::ios_base::beg, which); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -164,29 +163,30 @@ const std::size_t encrypting_reader::encrypted_chunk_size_ = | |||||||
|     data_chunk_size_ + header_size_; |     data_chunk_size_ + header_size_; | ||||||
|  |  | ||||||
| encrypting_reader::encrypting_reader( | encrypting_reader::encrypting_reader( | ||||||
|     const std::string &file_name, const std::string &source_path, |     std::string_view file_name, std::string_view source_path, | ||||||
|     stop_type &stop_requested, const std::string &token, |     stop_type &stop_requested, std::string_view token, | ||||||
|     std::optional<std::string> relative_parent_path, std::size_t error_return) |     std::optional<std::string_view> relative_parent_path, | ||||||
|  |     std::size_t error_return) | ||||||
|     : key_(utils::encryption::generate_key(token)), |     : key_(utils::encryption::generate_key(token)), | ||||||
|       stop_requested_(stop_requested), |       stop_requested_(stop_requested), | ||||||
|       error_return_(error_return) { |       error_return_(error_return) { | ||||||
|   const auto res = native_file::create_or_open(source_path, true, source_file_); |   const auto res = native_file::create_or_open(source_path, true, source_file_); | ||||||
|   if (res != api_error::success) { |   if (res != api_error::success) { | ||||||
|     throw std::runtime_error("file open failed|src|" + source_path + '|' + |     throw std::runtime_error("file open failed|src|" + | ||||||
|  |                              std::string{source_path} + '|' + | ||||||
|                              api_error_to_string(res)); |                              api_error_to_string(res)); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   data_buffer result; |   data_buffer result; | ||||||
|   utils::encryption::encrypt_data(key_, file_name.c_str(), |   utils::encryption::encrypt_data( | ||||||
|                                   strnlen(file_name.c_str(), file_name.size()), |       key_, reinterpret_cast<const unsigned char *>(file_name.data()), | ||||||
|                                   result); |       file_name.size(), result); | ||||||
|   encrypted_file_name_ = utils::to_hex_string(result); |   encrypted_file_name_ = utils::to_hex_string(result); | ||||||
|  |  | ||||||
|   if (relative_parent_path.has_value()) { |   if (relative_parent_path.has_value()) { | ||||||
|     for (const auto &part : |     for (auto &&part : std::filesystem::path(relative_parent_path.value())) { | ||||||
|          std::filesystem::path(relative_parent_path.value())) { |  | ||||||
|       utils::encryption::encrypt_data( |       utils::encryption::encrypt_data( | ||||||
|           key_, part.string().c_str(), |           key_, reinterpret_cast<const unsigned char *>(part.string().c_str()), | ||||||
|           strnlen(part.string().c_str(), part.string().size()), result); |           strnlen(part.string().c_str(), part.string().size()), result); | ||||||
|       encrypted_file_path_ += '/' + utils::to_hex_string(result); |       encrypted_file_path_ += '/' + utils::to_hex_string(result); | ||||||
|     } |     } | ||||||
| @@ -195,7 +195,8 @@ encrypting_reader::encrypting_reader( | |||||||
|  |  | ||||||
|   std::uint64_t file_size{}; |   std::uint64_t file_size{}; | ||||||
|   if (not utils::file::get_file_size(source_path, file_size)) { |   if (not utils::file::get_file_size(source_path, file_size)) { | ||||||
|     throw std::runtime_error("get file size failed|src|" + source_path + '|' + |     throw std::runtime_error("get file size failed|src|" + | ||||||
|  |                              std::string{source_path} + '|' + | ||||||
|                              std::to_string(utils::get_last_error_code())); |                              std::to_string(utils::get_last_error_code())); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -214,17 +215,18 @@ encrypting_reader::encrypting_reader( | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| encrypting_reader::encrypting_reader(const std::string &encrypted_file_path, | encrypting_reader::encrypting_reader(std::string_view encrypted_file_path, | ||||||
|                                      const std::string &source_path, |                                      std::string_view source_path, | ||||||
|                                      stop_type &stop_requested, |                                      stop_type &stop_requested, | ||||||
|                                      const std::string &token, |                                      std::string_view token, | ||||||
|                                      std::size_t error_return) |                                      std::size_t error_return) | ||||||
|     : key_(utils::encryption::generate_key(token)), |     : key_(utils::encryption::generate_key(token)), | ||||||
|       stop_requested_(stop_requested), |       stop_requested_(stop_requested), | ||||||
|       error_return_(error_return) { |       error_return_(error_return) { | ||||||
|   const auto res = native_file::create_or_open(source_path, true, source_file_); |   const auto res = native_file::create_or_open(source_path, true, source_file_); | ||||||
|   if (res != api_error::success) { |   if (res != api_error::success) { | ||||||
|     throw std::runtime_error("file open failed|src|" + source_path + '|' + |     throw std::runtime_error("file open failed|src|" + | ||||||
|  |                              std::string{source_path} + '|' + | ||||||
|                              api_error_to_string(res)); |                              api_error_to_string(res)); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -234,7 +236,8 @@ encrypting_reader::encrypting_reader(const std::string &encrypted_file_path, | |||||||
|  |  | ||||||
|   std::uint64_t file_size{}; |   std::uint64_t file_size{}; | ||||||
|   if (not utils::file::get_file_size(source_path, file_size)) { |   if (not utils::file::get_file_size(source_path, file_size)) { | ||||||
|     throw std::runtime_error("get file size failed|src|" + source_path + '|' + |     throw std::runtime_error("get file size failed|src|" + | ||||||
|  |                              std::string{source_path} + '|' + | ||||||
|                              std::to_string(utils::get_last_error_code())); |                              std::to_string(utils::get_last_error_code())); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -254,8 +257,8 @@ encrypting_reader::encrypting_reader(const std::string &encrypted_file_path, | |||||||
| } | } | ||||||
|  |  | ||||||
| encrypting_reader::encrypting_reader( | encrypting_reader::encrypting_reader( | ||||||
|     const std::string &encrypted_file_path, const std::string &source_path, |     std::string_view encrypted_file_path, std::string_view source_path, | ||||||
|     stop_type &stop_requested, const std::string &token, |     stop_type &stop_requested, std::string_view token, | ||||||
|     std::vector< |     std::vector< | ||||||
|         std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>> |         std::array<unsigned char, crypto_aead_xchacha20poly1305_IETF_NPUBBYTES>> | ||||||
|         iv_list, |         iv_list, | ||||||
| @@ -265,7 +268,8 @@ encrypting_reader::encrypting_reader( | |||||||
|       error_return_(error_return) { |       error_return_(error_return) { | ||||||
|   const auto res = native_file::create_or_open(source_path, true, source_file_); |   const auto res = native_file::create_or_open(source_path, true, source_file_); | ||||||
|   if (res != api_error::success) { |   if (res != api_error::success) { | ||||||
|     throw std::runtime_error("file open failed|src|" + source_path + '|' + |     throw std::runtime_error("file open failed|src|" + | ||||||
|  |                              std::string{source_path} + '|' + | ||||||
|                              api_error_to_string(res)); |                              api_error_to_string(res)); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -275,7 +279,8 @@ encrypting_reader::encrypting_reader( | |||||||
|  |  | ||||||
|   std::uint64_t file_size{}; |   std::uint64_t file_size{}; | ||||||
|   if (not utils::file::get_file_size(source_path, file_size)) { |   if (not utils::file::get_file_size(source_path, file_size)) { | ||||||
|     throw std::runtime_error("get file size failed|src|" + source_path + '|' + |     throw std::runtime_error("get file size failed|src|" + | ||||||
|  |                              std::string{source_path} + '|' + | ||||||
|                              std::to_string(utils::get_last_error_code())); |                              std::to_string(utils::get_last_error_code())); | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @@ -319,11 +324,12 @@ auto encrypting_reader::calculate_decrypted_size(std::uint64_t total_size) | |||||||
|                        get_header_size()); |                        get_header_size()); | ||||||
| } | } | ||||||
|  |  | ||||||
| auto encrypting_reader::calculate_encrypted_size(const std::string &source_path) | auto encrypting_reader::calculate_encrypted_size(std::string_view source_path) | ||||||
|     -> std::uint64_t { |     -> std::uint64_t { | ||||||
|   std::uint64_t file_size{}; |   std::uint64_t file_size{}; | ||||||
|   if (not utils::file::get_file_size(source_path, file_size)) { |   if (not utils::file::get_file_size(source_path, file_size)) { | ||||||
|     throw std::runtime_error("get file size failed|src|" + source_path + '|' + |     throw std::runtime_error("get file size failed|src|" + | ||||||
|  |                              std::string{source_path} + '|' + | ||||||
|                              std::to_string(utils::get_last_error_code())); |                              std::to_string(utils::get_last_error_code())); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -28,7 +28,7 @@ | |||||||
| #include "utils/utils.hpp" | #include "utils/utils.hpp" | ||||||
|  |  | ||||||
| namespace repertory::utils::encryption { | namespace repertory::utils::encryption { | ||||||
| auto decrypt_file_path(const std::string &encryption_token, | auto decrypt_file_path(std::string_view encryption_token, | ||||||
|                        std::string &file_path) -> api_error { |                        std::string &file_path) -> api_error { | ||||||
|   std::string decrypted_file_path{}; |   std::string decrypted_file_path{}; | ||||||
|   for (const auto &part : std::filesystem::path(file_path)) { |   for (const auto &part : std::filesystem::path(file_path)) { | ||||||
| @@ -49,7 +49,7 @@ auto decrypt_file_path(const std::string &encryption_token, | |||||||
|   return api_error::success; |   return api_error::success; | ||||||
| } | } | ||||||
|  |  | ||||||
| auto decrypt_file_name(const std::string &encryption_token, | auto decrypt_file_name(std::string_view encryption_token, | ||||||
|                        std::string &file_name) -> api_error { |                        std::string &file_name) -> api_error { | ||||||
|   data_buffer buffer; |   data_buffer buffer; | ||||||
|   if (not utils::from_hex_string(file_name, buffer)) { |   if (not utils::from_hex_string(file_name, buffer)) { | ||||||
| @@ -65,7 +65,7 @@ auto decrypt_file_name(const std::string &encryption_token, | |||||||
|   return api_error::success; |   return api_error::success; | ||||||
| } | } | ||||||
|  |  | ||||||
| auto generate_key(const std::string &encryption_token) -> key_type { | auto generate_key(std::string_view encryption_token) -> key_type { | ||||||
|   crypto_hash_sha256_state state{}; |   crypto_hash_sha256_state state{}; | ||||||
|   auto res = crypto_hash_sha256_init(&state); |   auto res = crypto_hash_sha256_init(&state); | ||||||
|   if (res != 0) { |   if (res != 0) { | ||||||
| @@ -73,8 +73,8 @@ auto generate_key(const std::string &encryption_token) -> key_type { | |||||||
|                              std::to_string(res)); |                              std::to_string(res)); | ||||||
|   } |   } | ||||||
|   res = crypto_hash_sha256_update( |   res = crypto_hash_sha256_update( | ||||||
|       &state, reinterpret_cast<const unsigned char *>(encryption_token.c_str()), |       &state, reinterpret_cast<const unsigned char *>(encryption_token.data()), | ||||||
|       strnlen(encryption_token.c_str(), encryption_token.size())); |       encryption_token.size()); | ||||||
|   if (res != 0) { |   if (res != 0) { | ||||||
|     throw std::runtime_error("failed to update sha256|" + std::to_string(res)); |     throw std::runtime_error("failed to update sha256|" + std::to_string(res)); | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -82,19 +82,19 @@ void change_to_process_directory() { | |||||||
| #if defined(_WIN32) | #if defined(_WIN32) | ||||||
|   std::string file_name; |   std::string file_name; | ||||||
|   file_name.resize(MAX_PATH); |   file_name.resize(MAX_PATH); | ||||||
|   ::GetModuleFileNameA(nullptr, &file_name[0u], |   ::GetModuleFileNameA(nullptr, &file_name[0U], | ||||||
|                        static_cast<DWORD>(file_name.size())); |                        static_cast<DWORD>(file_name.size())); | ||||||
|  |  | ||||||
|   std::string path = file_name.c_str(); |   std::string path = file_name.c_str(); | ||||||
|   ::PathRemoveFileSpecA(&path[0u]); |   ::PathRemoveFileSpecA(&path[0U]); | ||||||
|   ::SetCurrentDirectoryA(&path[0u]); |   ::SetCurrentDirectoryA(&path[0U]); | ||||||
| #else | #else | ||||||
|   std::string path; |   std::string path; | ||||||
|   path.resize(PATH_MAX + 1); |   path.resize(PATH_MAX + 1); | ||||||
| #if defined(__APPLE__) | #if defined(__APPLE__) | ||||||
|   proc_pidpath(getpid(), &path[0u], path.size()); |   proc_pidpath(getpid(), &path[0U], path.size()); | ||||||
| #else | #else | ||||||
|   readlink("/proc/self/exe", &path[0u], path.size()); |   readlink("/proc/self/exe", &path[0U], path.size()); | ||||||
| #endif | #endif | ||||||
|   path = utils::path::get_parent_directory(path); |   path = utils::path::get_parent_directory(path); | ||||||
|   chdir(path.c_str()); |   chdir(path.c_str()); | ||||||
| @@ -174,9 +174,9 @@ auto create_full_directory_path(std::string path) -> bool { | |||||||
| #else | #else | ||||||
|   auto ret = true; |   auto ret = true; | ||||||
|   const auto paths = utils::string::split(utils::path::absolute(path), |   const auto paths = utils::string::split(utils::path::absolute(path), | ||||||
|                                           utils::path::directory_seperator[0u]); |                                           utils::path::directory_seperator[0U]); | ||||||
|   std::string current_path; |   std::string current_path; | ||||||
|   for (std::size_t i = 0u; ret && (i < paths.size()); i++) { |   for (std::size_t i = 0U; ret && (i < paths.size()); i++) { | ||||||
|     if (paths[i].empty()) { // Skip root |     if (paths[i].empty()) { // Skip root | ||||||
|       current_path = utils::path::directory_seperator; |       current_path = utils::path::directory_seperator; | ||||||
|     } else { |     } else { | ||||||
| @@ -282,8 +282,8 @@ auto generate_sha256(const std::string &file_path) -> std::string { | |||||||
|  |  | ||||||
|   { |   { | ||||||
|     data_buffer buffer(1048576u); |     data_buffer buffer(1048576u); | ||||||
|     std::uint64_t read_offset = 0u; |     std::uint64_t read_offset = 0U; | ||||||
|     std::size_t bytes_read = 0u; |     std::size_t bytes_read = 0U; | ||||||
|     while ( |     while ( | ||||||
|         nf->read_bytes(buffer.data(), buffer.size(), read_offset, bytes_read)) { |         nf->read_bytes(buffer.data(), buffer.size(), read_offset, bytes_read)) { | ||||||
|       if (not bytes_read) { |       if (not bytes_read) { | ||||||
| @@ -461,7 +461,7 @@ auto get_accessed_time(const std::string &path, | |||||||
| auto get_modified_time(const std::string &path, | auto get_modified_time(const std::string &path, | ||||||
|                        std::uint64_t &modified) -> bool { |                        std::uint64_t &modified) -> bool { | ||||||
|   auto ret = false; |   auto ret = false; | ||||||
|   modified = 0u; |   modified = 0U; | ||||||
| #if defined(_WIN32) | #if defined(_WIN32) | ||||||
|   struct _stat64 st {}; |   struct _stat64 st {}; | ||||||
|   if (_stat64(path.c_str(), &st) != -1) { |   if (_stat64(path.c_str(), &st) != -1) { | ||||||
| @@ -483,20 +483,20 @@ auto get_modified_time(const std::string &path, | |||||||
|   return ret; |   return ret; | ||||||
| } | } | ||||||
|  |  | ||||||
| auto get_file_size(std::string path, std::uint64_t &file_size) -> bool { | auto get_file_size(std::string_view path, std::uint64_t &file_size) -> bool { | ||||||
|   file_size = 0u; |   file_size = 0U; | ||||||
|   path = utils::path::absolute(path); |   auto abs_path = utils::path::absolute(std::string{path}); | ||||||
|  |  | ||||||
| #if defined(_WIN32) | #if defined(_WIN32) | ||||||
|   struct _stat64 st {}; |   struct _stat64 st {}; | ||||||
|   if (_stat64(path.c_str(), &st) != 0) { |   if (_stat64(abs_path.c_str(), &st) != 0) { | ||||||
| #else | #else | ||||||
| #if defined(__APPLE__) | #if defined(__APPLE__) | ||||||
|   struct stat st {}; |   struct stat st {}; | ||||||
|   if (stat(path.c_str(), &st) != 0) { |   if (stat(abs_path.c_str(), &st) != 0) { | ||||||
| #else | #else | ||||||
|   struct stat64 st {}; |   struct stat64 st {}; | ||||||
|   if (stat64(path.c_str(), &st) != 0) { |   if (stat64(abs_path.c_str(), &st) != 0) { | ||||||
| #endif | #endif | ||||||
| #endif | #endif | ||||||
|     return false; |     return false; | ||||||
| @@ -643,7 +643,7 @@ auto reset_modified_time(const std::string &path) -> bool { | |||||||
|  |  | ||||||
| auto retry_delete_directory(const std::string &dir) -> bool { | auto retry_delete_directory(const std::string &dir) -> bool { | ||||||
|   auto deleted = false; |   auto deleted = false; | ||||||
|   for (std::uint8_t i = 0u; not(deleted = delete_directory(dir)) && (i < 200u); |   for (std::uint8_t i = 0U; not(deleted = delete_directory(dir)) && (i < 200U); | ||||||
|        i++) { |        i++) { | ||||||
|     std::this_thread::sleep_for(10ms); |     std::this_thread::sleep_for(10ms); | ||||||
|   } |   } | ||||||
| @@ -653,7 +653,7 @@ auto retry_delete_directory(const std::string &dir) -> bool { | |||||||
|  |  | ||||||
| auto retry_delete_file(const std::string &file) -> bool { | auto retry_delete_file(const std::string &file) -> bool { | ||||||
|   auto deleted = false; |   auto deleted = false; | ||||||
|   for (std::uint8_t i = 0u; not(deleted = delete_file(file)) && (i < 200u); |   for (std::uint8_t i = 0U; not(deleted = delete_file(file)) && (i < 200U); | ||||||
|        i++) { |        i++) { | ||||||
|     std::this_thread::sleep_for(10ms); |     std::this_thread::sleep_for(10ms); | ||||||
|   } |   } | ||||||
| @@ -671,9 +671,10 @@ auto write_json_file(const std::string &path, const json &j) -> bool { | |||||||
|   native_file_ptr nf; |   native_file_ptr nf; | ||||||
|   auto ret = (native_file::create_or_open(path, nf) == api_error::success); |   auto ret = (native_file::create_or_open(path, nf) == api_error::success); | ||||||
|   if (ret) { |   if (ret) { | ||||||
|     std::size_t bytes_written = 0u; |     std::size_t bytes_written{0U}; | ||||||
|     ret = nf->truncate(0) && |     ret = nf->truncate(0) && | ||||||
|           nf->write_bytes(&data[0u], data.size(), 0u, bytes_written); |           nf->write_bytes(reinterpret_cast<const unsigned char *>(data.data()), | ||||||
|  |                           data.size(), 0U, bytes_written); | ||||||
|     nf->close(); |     nf->close(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -64,24 +64,24 @@ auto native_file::clone(const native_file_ptr &ptr) -> native_file_ptr { | |||||||
|   return clone; |   return clone; | ||||||
| } | } | ||||||
|  |  | ||||||
| auto native_file::create_or_open(const std::string &source_path, bool read_only, | auto native_file::create_or_open(std::string_view source_path, bool read_only, | ||||||
|                                  native_file_ptr &ptr) -> api_error { |                                  native_file_ptr &ptr) -> api_error { | ||||||
| #if defined(_WIN32) | #if defined(_WIN32) | ||||||
|   auto handle = |   auto handle = | ||||||
|       read_only |       read_only ? ::CreateFileA(std::string{source_path}.c_str(), GENERIC_READ, | ||||||
|           ? ::CreateFileA(source_path.c_str(), GENERIC_READ, |                                 FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, | ||||||
|                           FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, |                                 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr) | ||||||
|                           OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr) |                 : ::CreateFileA(std::string{source_path}.c_str(), | ||||||
|           : ::CreateFileA(source_path.c_str(), GENERIC_READ | GENERIC_WRITE, |                                 GENERIC_READ | GENERIC_WRITE, | ||||||
|                           FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, |                                 FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, | ||||||
|                           OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr); |                                 OPEN_ALWAYS, FILE_FLAG_RANDOM_ACCESS, nullptr); | ||||||
| #else | #else | ||||||
|   auto handle = |   auto handle = read_only ? ::open(std::string{source_path}.c_str(), | ||||||
|       read_only |                                    O_CREAT | O_RDONLY | O_CLOEXEC, 0600U) | ||||||
|           ? ::open(source_path.c_str(), O_CREAT | O_RDONLY | O_CLOEXEC, 0600U) |                           : ::open(std::string{source_path}.c_str(), | ||||||
|           : ::open(source_path.c_str(), O_CREAT | O_RDWR | O_CLOEXEC, 0600U); |                                    O_CREAT | O_RDWR | O_CLOEXEC, 0600U); | ||||||
|   if (not read_only) { |   if (not read_only) { | ||||||
|     chmod(source_path.c_str(), 0600U); |     chmod(std::string{source_path}.c_str(), 0600U); | ||||||
|   } |   } | ||||||
| #endif | #endif | ||||||
|   ptr = native_file::attach(handle); |   ptr = native_file::attach(handle); | ||||||
| @@ -89,32 +89,34 @@ auto native_file::create_or_open(const std::string &source_path, bool read_only, | |||||||
|                                                : api_error::success); |                                                : api_error::success); | ||||||
| } | } | ||||||
|  |  | ||||||
| auto native_file::create_or_open(const std::string &source_path, | auto native_file::create_or_open(std::string_view source_path, | ||||||
|                                  native_file_ptr &ptr) -> api_error { |                                  native_file_ptr &ptr) -> api_error { | ||||||
|   return create_or_open(source_path, false, ptr); |   return create_or_open(source_path, false, ptr); | ||||||
| } | } | ||||||
|  |  | ||||||
| auto native_file::open(const std::string &source_path, | auto native_file::open(std::string_view source_path, | ||||||
|                        native_file_ptr &ptr) -> api_error { |                        native_file_ptr &ptr) -> api_error { | ||||||
|   return open(source_path, false, ptr); |   return open(source_path, false, ptr); | ||||||
| } | } | ||||||
|  |  | ||||||
| auto native_file::open(const std::string &source_path, bool read_only, | auto native_file::open(std::string_view source_path, bool read_only, | ||||||
|                        native_file_ptr &ptr) -> api_error { |                        native_file_ptr &ptr) -> api_error { | ||||||
| #if defined(_WIN32) | #if defined(_WIN32) | ||||||
|   auto handle = |   auto handle = | ||||||
|       read_only |       read_only | ||||||
|           ? ::CreateFileA(source_path.c_str(), GENERIC_READ, |           ? ::CreateFileA(std::string{source_path}.c_str(), GENERIC_READ, | ||||||
|                           FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, |                           FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, | ||||||
|                           OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, nullptr) |                           OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, nullptr) | ||||||
|           : ::CreateFileA(source_path.c_str(), GENERIC_READ | GENERIC_WRITE, |           : ::CreateFileA(std::string{source_path}.c_str(), | ||||||
|  |                           GENERIC_READ | GENERIC_WRITE, | ||||||
|                           FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, |                           FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, | ||||||
|                           OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, nullptr); |                           OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, nullptr); | ||||||
| #else | #else | ||||||
|   auto handle = read_only ? ::open(source_path.c_str(), O_RDONLY | O_CLOEXEC) |   auto handle = | ||||||
|                           : ::open(source_path.c_str(), O_RDWR | O_CLOEXEC); |       read_only ? ::open(std::string{source_path}.c_str(), O_RDONLY | O_CLOEXEC) | ||||||
|  |                 : ::open(std::string{source_path}.c_str(), O_RDWR | O_CLOEXEC); | ||||||
|   if (not read_only) { |   if (not read_only) { | ||||||
|     chmod(source_path.c_str(), 0600U); |     chmod(std::string{source_path}.c_str(), 0600U); | ||||||
|   } |   } | ||||||
| #endif | #endif | ||||||
|   ptr = native_file::attach(handle); |   ptr = native_file::attach(handle); | ||||||
| @@ -216,7 +218,7 @@ auto native_file::get_file_size(std::uint64_t &file_size) -> bool { | |||||||
| } | } | ||||||
|  |  | ||||||
| #if defined(_WIN32) | #if defined(_WIN32) | ||||||
| auto native_file::read_bytes(char *buffer, std::size_t read_size, | auto native_file::read_bytes(unsigned char *buffer, std::size_t read_size, | ||||||
|                              std::uint64_t read_offset, |                              std::uint64_t read_offset, | ||||||
|                              std::size_t &bytes_read) -> bool { |                              std::size_t &bytes_read) -> bool { | ||||||
|   recur_mutex_lock l(read_write_mutex_); |   recur_mutex_lock l(read_write_mutex_); | ||||||
| @@ -243,7 +245,7 @@ auto native_file::read_bytes(char *buffer, std::size_t read_size, | |||||||
|   return ret; |   return ret; | ||||||
| } | } | ||||||
| #else | #else | ||||||
| auto native_file::read_bytes(char *buffer, std::size_t read_size, | auto native_file::read_bytes(unsigned char *buffer, std::size_t read_size, | ||||||
|                              std::uint64_t read_offset, |                              std::uint64_t read_offset, | ||||||
|                              std::size_t &bytes_read) -> bool { |                              std::size_t &bytes_read) -> bool { | ||||||
|   bytes_read = 0U; |   bytes_read = 0U; | ||||||
| @@ -272,7 +274,8 @@ auto native_file::truncate(std::uint64_t file_size) -> bool { | |||||||
| } | } | ||||||
|  |  | ||||||
| #if defined(_WIN32) | #if defined(_WIN32) | ||||||
| auto native_file::write_bytes(const char *buffer, std::size_t write_size, | auto native_file::write_bytes(const unsigned char *buffer, | ||||||
|  |                               std::size_t write_size, | ||||||
|                               std::uint64_t write_offset, |                               std::uint64_t write_offset, | ||||||
|                               std::size_t &bytes_written) -> bool { |                               std::size_t &bytes_written) -> bool { | ||||||
|   recur_mutex_lock l(read_write_mutex_); |   recur_mutex_lock l(read_write_mutex_); | ||||||
| @@ -295,7 +298,8 @@ auto native_file::write_bytes(const char *buffer, std::size_t write_size, | |||||||
|   return ret; |   return ret; | ||||||
| } | } | ||||||
| #else | #else | ||||||
| auto native_file::write_bytes(const char *buffer, std::size_t write_size, | auto native_file::write_bytes(const unsigned char *buffer, | ||||||
|  |                               std::size_t write_size, | ||||||
|                               std::uint64_t write_offset, |                               std::uint64_t write_offset, | ||||||
|                               std::size_t &bytes_written) -> bool { |                               std::size_t &bytes_written) -> bool { | ||||||
|   bytes_written = 0U; |   bytes_written = 0U; | ||||||
|   | |||||||
| @@ -22,8 +22,8 @@ | |||||||
| #include "utils/string_utils.hpp" | #include "utils/string_utils.hpp" | ||||||
|  |  | ||||||
| namespace repertory::utils::string { | namespace repertory::utils::string { | ||||||
| /* constexpr c++20 */ auto ends_with(std::string_view str, std::string_view val) | /* constexpr c++20 */ auto ends_with(std::string_view str, | ||||||
|     -> bool { |                                      std::string_view val) -> bool { | ||||||
|   if (val.size() > str.size()) { |   if (val.size() > str.size()) { | ||||||
|     return false; |     return false; | ||||||
|   } |   } | ||||||
| @@ -41,11 +41,11 @@ auto from_dynamic_bitset(const boost::dynamic_bitset<> &bitset) -> std::string { | |||||||
|   return stream.str(); |   return stream.str(); | ||||||
| } | } | ||||||
|  |  | ||||||
| auto from_utf8(const std::string &str) -> std::wstring { | auto from_utf8(std::string_view str) -> std::wstring { | ||||||
|   return str.empty() |   return str.empty() | ||||||
|              ? L"" |              ? L"" | ||||||
|              : std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t>() |              : std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t>() | ||||||
|                    .from_bytes(str); |                    .from_bytes(std::string{str}); | ||||||
| } | } | ||||||
|  |  | ||||||
| /* constexpr c++20 */ auto is_numeric(std::string_view str) -> bool { | /* constexpr c++20 */ auto is_numeric(std::string_view str) -> bool { | ||||||
| @@ -72,8 +72,8 @@ auto from_utf8(const std::string &str) -> std::wstring { | |||||||
|                       }) == str.end(); |                       }) == str.end(); | ||||||
| } | } | ||||||
|  |  | ||||||
| auto join(const std::vector<std::string> &arr, const char &delim) | auto join(const std::vector<std::string> &arr, | ||||||
|     -> std::string { |           const char &delim) -> std::string { | ||||||
|   if (arr.empty()) { |   if (arr.empty()) { | ||||||
|     return ""; |     return ""; | ||||||
|   } |   } | ||||||
| @@ -92,8 +92,8 @@ auto left_trim(std::string &str, const char &trim_ch) -> std::string & { | |||||||
|   return str; |   return str; | ||||||
| } | } | ||||||
|  |  | ||||||
| auto replace(std::string &src, const char &character, const char &with) | auto replace(std::string &src, const char &character, | ||||||
|     -> std::string & { |              const char &with) -> std::string & { | ||||||
|   std::replace(src.begin(), src.end(), character, with); |   std::replace(src.begin(), src.end(), character, with); | ||||||
|   return src; |   return src; | ||||||
| } | } | ||||||
| @@ -109,8 +109,8 @@ auto replace(std::string &src, const std::string &find, const std::string &with, | |||||||
|   return src; |   return src; | ||||||
| } | } | ||||||
|  |  | ||||||
| auto replace_copy(std::string src, const char &character, const char &with) | auto replace_copy(std::string src, const char &character, | ||||||
|     -> std::string { |                   const char &with) -> std::string { | ||||||
|   std::replace(src.begin(), src.end(), character, with); |   std::replace(src.begin(), src.end(), character, with); | ||||||
|   return src; |   return src; | ||||||
| } | } | ||||||
| @@ -129,8 +129,8 @@ auto right_trim(std::string &str, const char &trim_ch) -> std::string & { | |||||||
|   return str; |   return str; | ||||||
| } | } | ||||||
|  |  | ||||||
| auto split(const std::string &str, const char &delim, bool should_trim) | auto split(const std::string &str, const char &delim, | ||||||
|     -> std::vector<std::string> { |            bool should_trim) -> std::vector<std::string> { | ||||||
|   std::vector<std::string> ret; |   std::vector<std::string> ret; | ||||||
|   std::stringstream stream(str); |   std::stringstream stream(str); | ||||||
|   std::string item; |   std::string item; | ||||||
| @@ -203,13 +203,13 @@ auto to_upper(std::string str) -> std::string { | |||||||
|   return str; |   return str; | ||||||
| } | } | ||||||
|  |  | ||||||
| auto to_utf8(std::string str) -> std::string { return str; } | auto to_utf8(std::string_view str) -> std::string { return std::string{str}; } | ||||||
|  |  | ||||||
| auto to_utf8(const std::wstring &str) -> std::string { | auto to_utf8(std::wstring_view str) -> std::string { | ||||||
|   return str.empty() |   return str.empty() | ||||||
|              ? "" |              ? "" | ||||||
|              : std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t>() |              : std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t>() | ||||||
|                    .to_bytes(str); |                    .to_bytes(std::wstring{str}); | ||||||
| } | } | ||||||
|  |  | ||||||
| auto trim(std::string &str) -> std::string & { | auto trim(std::string &str) -> std::string & { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user