[Filesystem] Fix UTF-8 decoding in readAsText 62/110262/1
authorJakub Skowron <j.skowron@samsung.com>
Fri, 13 Jan 2017 12:05:55 +0000 (13:05 +0100)
committerJakub Skowron <j.skowron@samsung.com>
Fri, 13 Jan 2017 12:05:55 +0000 (13:05 +0100)
Correctly decode  multibyte characters at buffer boundary.

Change-Id: Ie6ccb680e8fb78d1bfffaa1b06cd600d78c3b675
Signed-off-by: Jakub Skowron <j.skowron@samsung.com>
src/filesystem/js/base64.js
src/filesystem/js/file.js

index 23248313270798205dd244cbcdaa0ba8bccca679..20a29b49d9ca9ba301ff37a001b3ccfbbeba1765 100755 (executable)
@@ -54,12 +54,7 @@ var Base64 = {
     return output;
   },
   decodeString: function(data) {
-    data = this.decode(data);
-    var output = '';
-    for (var i = 0; i < data.length; ++i) {
-      output += String.fromCharCode(data[i]);
-    }
-    return this._utf8_decode(output);
+    return this._utf8_decode(this.decode(data));
   },
   decode: function(data) {
     var output = [];
@@ -118,26 +113,26 @@ var Base64 = {
 
     return utftext;
   },
-  _utf8_decode: function(utftext) {
+  _utf8_decode: function(utfarray) {
     var str = '';
     var i = 0, c = 0, c1 = 0, c2 = 0;
 
-    while (i < utftext.length) {
+    while (i < utfarray.length) {
 
-      c = utftext.charCodeAt(i);
+      c = utfarray[i];
 
       if (c < 128) {
         str += String.fromCharCode(c);
         i++;
       }
       else if ((c > 191) && (c < 224)) {
-        c1 = utftext.charCodeAt(i + 1);
+        c1 = utfarray[i + 1];
         str += String.fromCharCode(((c & 31) << 6) | (c1 & 63));
         i += 2;
       }
       else {
-        c1 = utftext.charCodeAt(i + 1);
-        c2 = utftext.charCodeAt(i + 2);
+        c1 = utfarray[i + 1];
+        c2 = utfarray[i + 2];
         str += String.fromCharCode(((c & 15) << 12) | ((c1 & 63) << 6) | (c2 & 63));
         i += 3;
       }
index 0c4a721e3606a8c00d03bc45eccf1ef22b812a9b..4b07da67402149dea077c0ceed4ca628bfb2a6e9 100644 (file)
@@ -316,12 +316,12 @@ function readAsText() {
   var data = {
     location: commonFS_.toRealPath(this.fullPath),
     offset: 0,
-    length: 1024,
+    length: 65536,
     encoding: args.encoding
   };
 
   function readFile() {
-    var result, encoded, str = '';
+    var result, encoded, buffers = [];
 
     do {
       result = native_.callSync('File_readSync', data);
@@ -333,13 +333,15 @@ function readAsText() {
       }
       encoded = native_.getResultObject(result);
       if (encoded.length) {
-        str += Base64.decodeString(encoded);
+        buffers.push( Base64.decode(encoded) );
         data.offset += data.length;
       }
     } while (encoded.length);
 
+    var buffer = Array.prototype.concat.apply([], buffers);
+
     setTimeout(function() {
-      native_.callIfPossible(args.onsuccess, str);
+      native_.callIfPossible(args.onsuccess, Base64._utf8_decode(buffer) );
     }, 0);
   }