From f55afdbe3cad75839a4374b70cca0d762556adbc Mon Sep 17 00:00:00 2001 From: Rafal Walczyna Date: Tue, 5 Jan 2021 14:27:05 +0100 Subject: [PATCH] [Common] Moved helper functions to decode/encode strings/binary to common [Verification] Built successfully. Metadata and filesystem auto tct 100%. Change-Id: I751e5cad95f8c7dc907214225f8f27cabbb3e5e7 Signed-off-by: Rafal Walczyna --- src/common/converter.cc | 37 ++++++++++++++++++++ src/common/converter.h | 3 ++ src/filesystem/filesystem_instance.cc | 50 ++++----------------------- src/metadata/metadata_instance.cc | 25 +++----------- 4 files changed, 51 insertions(+), 64 deletions(-) diff --git a/src/common/converter.cc b/src/common/converter.cc index 48ccd601..ef59df0e 100644 --- a/src/common/converter.cc +++ b/src/common/converter.cc @@ -43,4 +43,41 @@ long stol(const std::string& str, std::size_t* pos, int base) { } } +/* Write to str buf bytes as if they were UTF-8 codepoints */ +void encode_binary_in_string(const std::vector& buf, std::string& str) { + ScopeLogger(); + str.reserve(str.size() + buf.size()); + + for (std::uint8_t byte : buf) { + if (byte < 128) { + str += byte; + continue; + } + str += 0xC0 | (byte >> 6); + str += 0x80 | (byte & 0x3F); + } +} + +/* Decode (max 2-byte) UTF-8 characters to buf, throws std::runtime_error */ +void decode_binary_from_string(const std::string& str, std::vector& buf) { + ScopeLogger(); + buf.reserve(buf.size() + str.size()); + + const std::uint8_t* it = (std::uint8_t*)str.data(); + const std::uint8_t* end = it + str.size(); + while (it != end) { + if (*it < 128) { + buf.push_back(*it++); + continue; + } + auto b1 = *it++; + if (it == end) { + throw std::runtime_error("internal error (invalid UTF-8 sequence)"); + } + auto b2 = *it++; + unsigned int x = ((b1 & 0x1F) << 6) | (b2 & 0x3F); + buf.push_back(x); + } +} + } // webapi diff --git a/src/common/converter.h b/src/common/converter.h index 35b9e1f8..5ba7a6b5 100644 --- a/src/common/converter.h +++ b/src/common/converter.h @@ -59,6 +59,9 @@ const T &FromJson(const picojson::object &in, const char *name, Names... names) return FromJson(v.get(), names...); } +void encode_binary_in_string(const std::vector& buf, std::string& str); +void decode_binary_from_string(const std::string& str, std::vector& buf); + } // common #endif // COMMON_CONVERTER_H_ diff --git a/src/filesystem/filesystem_instance.cc b/src/filesystem/filesystem_instance.cc index da8b0ab4..010a202c 100644 --- a/src/filesystem/filesystem_instance.cc +++ b/src/filesystem/filesystem_instance.cc @@ -23,6 +23,7 @@ #include #include +#include "common/converter.h" #include "common/logger.h" #include "common/picojson.h" #include "common/platform_exception.h" @@ -235,48 +236,11 @@ void FilesystemInstance::FileRename(const picojson::value& args, picojson::objec std::bind(&FilesystemManager::Rename, &fsm, oldPath, newPath, onSuccess, onError)); } -/* Write to str buf bytes as if they were UTF-8 codepoints */ -static void encode_binary_in_string(const std::vector& buf, std::string& str) { - ScopeLogger(); - str.reserve(str.size() + buf.size()); - - for (std::uint8_t byte : buf) { - if (byte < 128) { - str += byte; - continue; - } - str += 0xC0 | (byte >> 6); - str += 0x80 | (byte & 0x3F); - } -} - -/* Decode (max 2-byte) UTF-8 characters to buf, throws std::runtime_error */ -static void decode_binary_from_string(const std::string& str, std::vector& buf) { - ScopeLogger(); - buf.reserve(buf.size() + str.size()); - - const std::uint8_t* it = (std::uint8_t*)str.data(); - const std::uint8_t* end = it + str.size(); - while (it != end) { - if (*it < 128) { - buf.push_back(*it++); - continue; - } - auto b1 = *it++; - if (it == end) { - throw std::runtime_error("internal error (invalid UTF-8 sequence)"); - } - auto b2 = *it++; - unsigned int x = ((b1 & 0x1F) << 6) | (b2 & 0x3F); - buf.push_back(x); - } -} - namespace latin1 { -static auto to_utf8 = &encode_binary_in_string; +static auto to_utf8 = &common::encode_binary_in_string; /* It does not check if UTF-8 values are representable by ISO-8859-1. Make proper checks and * substitute invalid characters in JavaScript before passing through crosswalk */ -static auto from_utf8 = &decode_binary_from_string; +static auto from_utf8 = &common::decode_binary_from_string; } static constexpr std::size_t NPOS = (std::size_t)(-1); @@ -727,7 +691,7 @@ void FilesystemInstance::FileReadBytes(const picojson::value& args, picojson::ob std::vector out_data = read_file(location, offset, length); out["result"] = picojson::value(picojson::string_type, true); - encode_binary_in_string(out_data, out["result"].get()); + common::encode_binary_in_string(out_data, out["result"].get()); ReportSuccess(out); } catch (std::runtime_error& e) { LoggerE("Cannot read file %s, cause: %s", location.c_str(), e.what()); @@ -796,7 +760,7 @@ void FilesystemInstance::FileWriteBytes(const picojson::value& args, picojson::o try { std::vector data; - decode_binary_from_string(str, data); + common::decode_binary_from_string(str, data); write_file(data.data(), data.size(), location, offset, mode); } catch (std::runtime_error& e) { LoggerE("Cannot write to %s, cause: %s", location.c_str(), e.what()); @@ -2142,7 +2106,7 @@ void FilesystemInstance::FileHandleReadData(const picojson::value& args, picojso try { std::vector data = read_file(handle->file_handle, size); out["result"] = picojson::value(picojson::string_type, true); - encode_binary_in_string(data, out["result"].get()); + common::encode_binary_in_string(data, out["result"].get()); } catch (std::runtime_error& e) { LoggerE("Cannot read, cause: %s", e.what()); LogAndReportError(IOException(e.what()), out); @@ -2188,7 +2152,7 @@ void FilesystemInstance::FileHandleWriteData(const picojson::value& args, picojs auto logic = [str, handle](decltype(out) out) { try { std::vector bytes; - decode_binary_from_string(str, bytes); + common::decode_binary_from_string(str, bytes); write_file(bytes.data(), bytes.size(), handle->file_handle); } catch (std::runtime_error& e) { LogAndReportError(IOException(e.what()), out); diff --git a/src/metadata/metadata_instance.cc b/src/metadata/metadata_instance.cc index eb8c02eb..36d6fdfb 100644 --- a/src/metadata/metadata_instance.cc +++ b/src/metadata/metadata_instance.cc @@ -16,6 +16,7 @@ #include "metadata/metadata_instance.h" +#include "common/converter.h" #include "common/logger.h" #include "common/picojson.h" #include "common/platform_exception.h" @@ -114,24 +115,6 @@ void MetadataInstance::MetadataFileHandleGet(const picojson::value& args, picojs ReportSuccess(picojson::value(value), out); } -// TODO copied from filesystem -// move to common?? -/* Write to str buf bytes as if they were UTF-8 codepoints */ -static void encode_binary_in_string(const std::vector& buf, std::string& str) { - ScopeLogger(); - str.reserve(str.size() + buf.size()); - - for (std::uint8_t byte : buf) { - if (byte < 128) { - str += byte; - continue; - } - str += 0xC0 | (byte >> 6); - str += 0x80 | (byte & 0x3F); - } -} -/////////////////////////////////// - void MetadataInstance::MetadataFileHandleGetArtwork(const picojson::value& args, picojson::object& out) { ScopeLogger(); @@ -165,7 +148,7 @@ void MetadataInstance::MetadataFileHandleGetArtwork(const picojson::value& args, picojson::object& result_artwork_obj = result_artwork.get(); result_artwork_obj["encodedData"] = picojson::value(picojson::string_type, true); - encode_binary_in_string(data, result_artwork_obj["encodedData"].get()); + common::encode_binary_in_string(data, result_artwork_obj["encodedData"].get()); result_artwork_obj["mimeType"] = picojson::value(mime_type); ReportSuccess(result_artwork, out); @@ -201,7 +184,7 @@ void MetadataInstance::MetadataFileHandleGetThumbnailFrame(const picojson::value } std::string result_artwork; - encode_binary_in_string(data, result_artwork); + common::encode_binary_in_string(data, result_artwork); ReportSuccess(picojson::value(result_artwork), out); } @@ -238,7 +221,7 @@ void MetadataInstance::MetadataFileHandleGetFrameAtTime(const picojson::value& a } std::string result_frame; - encode_binary_in_string(data, result_frame); + common::encode_binary_in_string(data, result_frame); ReportSuccess(picojson::value(result_frame), out); } -- 2.34.1