}
}
+/* Write to str buf bytes as if they were UTF-8 codepoints */
+void encode_binary_in_string(const std::vector<std::uint8_t>& 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<std::uint8_t>& 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
#include <sys/stat.h>
#include <system_error>
+#include "common/converter.h"
#include "common/logger.h"
#include "common/picojson.h"
#include "common/platform_exception.h"
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<std::uint8_t>& 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<std::uint8_t>& 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);
std::vector<std::uint8_t> out_data = read_file(location, offset, length);
out["result"] = picojson::value(picojson::string_type, true);
- encode_binary_in_string(out_data, out["result"].get<std::string>());
+ common::encode_binary_in_string(out_data, out["result"].get<std::string>());
ReportSuccess(out);
} catch (std::runtime_error& e) {
LoggerE("Cannot read file %s, cause: %s", location.c_str(), e.what());
try {
std::vector<std::uint8_t> 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());
try {
std::vector<std::uint8_t> data = read_file(handle->file_handle, size);
out["result"] = picojson::value(picojson::string_type, true);
- encode_binary_in_string(data, out["result"].get<std::string>());
+ common::encode_binary_in_string(data, out["result"].get<std::string>());
} catch (std::runtime_error& e) {
LoggerE("Cannot read, cause: %s", e.what());
LogAndReportError(IOException(e.what()), out);
auto logic = [str, handle](decltype(out) out) {
try {
std::vector<std::uint8_t> 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);
#include "metadata/metadata_instance.h"
+#include "common/converter.h"
#include "common/logger.h"
#include "common/picojson.h"
#include "common/platform_exception.h"
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<std::uint8_t>& 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();
picojson::object& result_artwork_obj = result_artwork.get<picojson::object>();
result_artwork_obj["encodedData"] = picojson::value(picojson::string_type, true);
- encode_binary_in_string(data, result_artwork_obj["encodedData"].get<std::string>());
+ common::encode_binary_in_string(data, result_artwork_obj["encodedData"].get<std::string>());
result_artwork_obj["mimeType"] = picojson::value(mime_type);
ReportSuccess(result_artwork, out);
}
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);
}
}
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);
}