return total_fize_size - static_cast<size_t>(file_position);
}
-static std::vector<std::uint8_t> read_file(FILE* file, std::size_t length = NPOS);
+static std::vector<std::uint8_t> read_file(FILE* file, std::size_t length = NPOS,
+ std::size_t* read_bytes = nullptr);
/**
* Returns a buffer. If length is NPOS, then it reads whole file, up to the end.
* Returns a buffer. If length is NPOS, then it reads whole file, up to the end.
* On failure throws std::runtime_error
*/
-static std::vector<std::uint8_t> read_file(FILE* file, std::size_t length /*= NPOS*/) {
+static std::vector<std::uint8_t> read_file(FILE* file, std::size_t length /*= NPOS*/,
+ std::size_t* read_bytes /* = nullptr*/) {
ScopeLogger();
// By default reads whole file. Get the file size.
break;
}
}
+ // read_file function is used in API since version 1.0.
+ // read_bytes was added in Tizen 5.0, with default value equal to nullptr, the behaviour is not
+ // changed.
+ // It is used to return the actual number of read bytes, because requested length might be bigger
+ // than possible bytes to be read.
+ if (nullptr != read_bytes) {
+ *read_bytes = std::distance(out_buf.data(), data_p);
+ }
return out_buf;
}
auto logic = [handle, count, encoding, whole_file](decltype(out) out) {
try {
- std::vector<std::uint8_t> buf = read_file(handle->file_handle, count);
+ size_t read_bytes = 0;
+ std::vector<std::uint8_t> buf = read_file(handle->file_handle, count, &read_bytes);
+ buf.resize(read_bytes); // this protects from reporting too big arrays to JS
if (encoding == kISOEncoding) { // for iso-8859-1 1 byte is equal to 1 character
out["result"] = picojson::value(picojson::string_type, true);
latin1::to_utf8(buf, out["result"].get<std::string>());
IOException("File doesn't contain UTF-8 encoded string with given length"), out);
}
}
- buf.push_back('\0');
const char* str = (const char*)buf.data();
- ReportSuccess(picojson::value{str}, out);
+ ReportSuccess(picojson::value{str, buf.size()}, out);
}
} catch (std::runtime_error& e) {
LoggerE("Cannot read, cause: %s", e.what());