From 6155969620dbec240d2fcea7d38c3f218e46232e Mon Sep 17 00:00:00 2001 From: Jakub Skowron Date: Fri, 13 Jan 2017 13:05:55 +0100 Subject: [PATCH] [Filesystem] Fix UTF-8 decoding in readAsText Correctly decode multibyte characters at buffer boundary. Change-Id: Ie6ccb680e8fb78d1bfffaa1b06cd600d78c3b675 Signed-off-by: Jakub Skowron --- src/filesystem/js/base64.js | 19 +++++++------------ src/filesystem/js/file.js | 10 ++++++---- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/filesystem/js/base64.js b/src/filesystem/js/base64.js index 2324831..20a29b4 100755 --- a/src/filesystem/js/base64.js +++ b/src/filesystem/js/base64.js @@ -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; } diff --git a/src/filesystem/js/file.js b/src/filesystem/js/file.js index 0c4a721..4b07da6 100644 --- a/src/filesystem/js/file.js +++ b/src/filesystem/js/file.js @@ -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); } -- 2.7.4