websockify-js icon indicating copy to clipboard operation
websockify-js copied to clipboard

websock.send_string doesn't convert to utf8

Open imuli opened this issue 10 years ago • 2 comments

Characters above U+00FF are sent modulo 256, due to (in binary mode) websock.js:164 "new Uint8Array(sQ)". I havn't tested it, but the base64 encoder appears to do the same thing in a different way.

imuli avatar Jun 25 '14 23:06 imuli

Patch

Note, this code will fail if passed unpaired UTF-16 surrogates, values 0xd800-0xdfff, but then it's not really a string.

diff --git a/include/util.js b/include/util.js
index 67d2133..e498f94 100644
--- a/include/util.js
+++ b/include/util.js
@@ -377,3 +377,11 @@ Util.Flash = (function(){
     version = v.match(/\d+/g);
     return {version: parseInt(version[0] || 0 + '.' + version[1], 10) || 0, build: parseInt(version[2], 10) || 0};
 }()); 
+
+Util.toUTF8 = (function(s){
+   return unescape(encodeURIComponent(s));
+}());
+
+Util.fromUTF8 = (function(s){
+   return decodeURIComponent(escape(s));
+}());
diff --git a/include/websock.js b/include/websock.js
index 01a24c3..e4a694e 100644
--- a/include/websock.js
+++ b/include/websock.js
@@ -119,7 +119,7 @@ function rQshiftStr(len) {
     if (typeof(len) === 'undefined') { len = rQlen(); }
     var arr = rQ.slice(rQi, rQi + len);
     rQi += len;
-    return String.fromCharCode.apply(null, arr);
+    return Util.fromUTF8(String.fromCharCode.apply(null, arr));
 }
 function rQshiftBytes(len) {
     if (typeof(len) === 'undefined') { len = rQlen(); }
@@ -216,7 +216,7 @@ function send(arr) {

 function send_string(str) {
     //Util.Debug(">> send_string: " + str);
-    api.send(str.split('').map(
+    api.send(Util.toUTF8(str).split('').map(
         function (chr) { return chr.charCodeAt(0); } ) );
 }

imuli avatar Jun 26 '14 00:06 imuli

Thanks. I'll look into this when I get a chance. Thanks!

DirectXMan12 avatar Jul 07 '14 16:07 DirectXMan12