From 801d82fa5ccc97e527a6c37054c10b3f93f40d4a Mon Sep 17 00:00:00 2001
From: deuce <>
Date: Fri, 19 Mar 2010 08:28:11 +0000
Subject: [PATCH] Use UTF-8

---
 src/ZuulTerm/chrome/content/RLogin.js |  4 +-
 src/ZuulTerm/chrome/content/Util.js   | 65 +++++++++++++++++++++++++++
 2 files changed, 68 insertions(+), 1 deletion(-)

diff --git a/src/ZuulTerm/chrome/content/RLogin.js b/src/ZuulTerm/chrome/content/RLogin.js
index 72b41460dd..5612d0e5af 100644
--- a/src/ZuulTerm/chrome/content/RLogin.js
+++ b/src/ZuulTerm/chrome/content/RLogin.js
@@ -1,6 +1,7 @@
 function RLoginConnection(address, port, recvfunc)
 {
 	this.recvfunc=recvfunc;
+	this.unicode={remainder:''};
 
 	this.onStartRequest=function(request, context) {};
 	this.onStopRequest=function(request, context, status) {
@@ -8,7 +9,8 @@ function RLoginConnection(address, port, recvfunc)
 		endTerm();
 	};
 	this.onDataAvailable=function(request, context, inputStream, offset, count) {
-		this.recvfunc(this.sock.read(count, count, 0));
+		this.unicode=bytesToUnicode(this.unicode.remainder+this.sock.read(count, count, 0));
+		this.recvfunc(this.unicode.unicode);
 	};
 
 	this.write=function(data)
diff --git a/src/ZuulTerm/chrome/content/Util.js b/src/ZuulTerm/chrome/content/Util.js
index e826152ffb..a8d6bd756a 100644
--- a/src/ZuulTerm/chrome/content/Util.js
+++ b/src/ZuulTerm/chrome/content/Util.js
@@ -37,3 +37,68 @@ function nsWaitForDelay(delay) {
         thread.processNextEvent(true);
     }
 }
+
+function decode(encoded)
+{
+	var bytes=0;
+	var i;
+	var val=0xfffd;
+	var firstbyte=encoded.charCodeAt(0);
+
+	if(firstbyte & 0xe0 == 0xd0) {
+		bytes=2;
+		val=firstbyte & 0x1f;
+	}
+	else if(firstbyte & 0xf0 == 0xe0) {
+		bytes=3;
+		val=firstbyte & 0x0f;
+	}
+	else if(firstbyte & 0xf8 == 0xf0) {
+		bytes=4;
+		val=firstbyte & 0x07;
+	}
+	else if(firstbyte & 0xfc == 0xf8) {
+		bytes=5;
+		val=firstbyte & 0x03;
+	}
+	else if(firstbyte & 0xfe == 0xfc) {
+		bytes=6;
+		val=firstbyte & 0x01;
+	}
+	for(i=1; i<bytes; i++) {
+		if(encoded.charCodeAt(i) & 0xc0 != 0x80) {
+			val = 0xfffd;
+			break;
+		}
+		val <<= 6;
+		val |= (encoded.charCodeAt(i) & 0x3f);
+	}
+
+	return String.fromCharCode(val);
+}
+
+function bytesToUnicode(bytes)
+{
+	var ret={unicode:'',remainder:''};
+	var i;
+	var encoded='';
+
+	for(i=0; i<bytes.length; i++) {
+		if(bytes.charCodeAt(i) < 128) {
+			if(encoded.length > 0) {
+				ret.unicode += decode(encoded);
+				encoded='';
+			}
+			ret.unicode += bytes.charAt(i);
+		}
+		else {
+			if(encoded.legnth > 0 && (bytes.charCodeAt(i) & 0x0c) != 0x0c) {
+				ret.unicode += decode(encoded);
+				encoded='';
+			}
+			encoded += bytes.charAt(i);
+		}
+	}
+	ret.remainder=encoded;
+	return ret;
+}
-- 
GitLab