[Filesystem] Remove special case for zero in readBytes/writeBytes 19/168019/2
authorJakub Skowron <j.skowron@samsung.com>
Wed, 24 Jan 2018 08:44:34 +0000 (09:44 +0100)
committerJakub Skowron <j.skowron@samsung.com>
Wed, 24 Jan 2018 08:44:34 +0000 (09:44 +0100)
Null character in Unicode sequence is correctly interpreted
by picojson and crosswalk, so remove mapping 0 to 0x100.

Change-Id: I61988a1089ecdab006ce5cc2492960d526fb9455
Signed-off-by: Jakub Skowron <j.skowron@samsung.com>
src/filesystem/filesystem_instance.cc
src/filesystem/js/file_stream.js

index eaceadc..e600274 100644 (file)
@@ -137,26 +137,22 @@ void FilesystemInstance::FileRename(const picojson::value& args, picojson::objec
       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());
@@ -169,9 +165,12 @@ static void decode_binary_from_string(const std::string& str, std::vector<std::u
       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);
   }
 }
 
index bc3e9a6..31812bb 100644 (file)
@@ -104,28 +104,21 @@ function _checkWriteAccess(mode) {
 }
 
 /* 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;
 }
@@ -216,7 +209,7 @@ function readBytes() {
     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;
@@ -281,7 +274,7 @@ function writeBytes() {
     {
       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 */
     }
   ]);
 
@@ -296,7 +289,7 @@ function writeBytes() {
   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,
   };