std::bind(&FilesystemManager::Rename, &fsm, oldPath, newPath, onSuccess, onError));
}
-/* str should be a reference to (possibly empty) std::string */
+/* 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 += 0xC0 | (byte >> 6);
- str += 0x80 | (byte & 0x3F);
- } else if (byte > 0) {
+ if (byte < 128) {
str += byte;
- } else {
- // zero as codepoint U+0100
- str += 0xC4;
- str += 0x80;
+ continue;
}
+ str += 0xC0 | (byte >> 6);
+ str += 0x80 | (byte & 0x3F);
}
}
-/* buf should be a reference to (possibly empty) vector */
+/* 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());
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 != 0x100 ? x : 0);
+ buf.push_back(x);
}
}
}
/* returns array of numbers */
-function string_to_binary( str ) {
+function string_to_array( str ) {
var output = [];
var len = str.length;
- var c;
for( var i = 0; i < len; i++ ) {
- // decode unicode codepoint U+0100 as zero byte
- c = str.charCodeAt(i);
- output.push( c == 0x100 ? 0 : c );
+ output.push( str.charCodeAt(i) );
}
return output;
}
/* receives array of numbers, returns string */
-function binary_to_string( data ) {
+function array_to_string( data ) {
var output = "";
var len = data.length;
- // endecode zero byte as unicode codepoint U+0100
- var zero = String.fromCharCode(0x100);
- var b;
for( var i = 0; i < len; i++ ) {
- b = data[i] & 0xFF; // conversion to octet
- output += b == 0 ? zero : String.fromCharCode(b);
+ output += String.fromCharCode(data[i] & 0xFF); // conversion to octet
}
return output;
}
throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR, 'Could not read');
}
- var decoded = string_to_binary( native_.getResultObject(result) );
+ var decoded = string_to_array( native_.getResultObject(result) );
if (decoded.length) {
can_change_size = true;
{
name: 'byteData',
type: types_.ARRAY,
- values: undefined /* was types_.OCTET, but checking moved to binary_to_string for performance */
+ values: undefined /* was types_.OCTET, but checking moved to array_to_string for performance */
}
]);
var data = {
location: commonFS_.toRealPath(this._file.fullPath),
offset: this.position,
- data: binary_to_string(args.byteData),
+ data: array_to_string(args.byteData),
rewrite: this._rewrite,
};