Skip to content
Snippets Groups Projects
Commit 801d82fa authored by deuce's avatar deuce
Browse files

Use UTF-8

parent 79a7de7e
No related branches found
No related tags found
No related merge requests found
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)
......
......@@ -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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment