Skip to content

Commit

Permalink
Fix UTF8 related issues in virtio filer
Browse files Browse the repository at this point in the history
  • Loading branch information
ProgrammerIn-wonderland committed Aug 28, 2024
1 parent 0858e59 commit eb6b7c6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
40 changes: 24 additions & 16 deletions lib/marshall.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,20 @@ marshall.Marshall = function(typelist, input, struct, offset) {
struct[offset++] = 0; // set the length later
struct[offset++] = 0;
size += 2;
for(var j of item) {
var utf8 = UnicodeToUTF8Stream(j.charCodeAt(0));
utf8.forEach( function(c) {
struct[offset++] = c;
size += 1;
length++;
});
}

var stringBytes = texten.encode(item);
size += stringBytes.byteLength;
length += stringBytes.byteLength;
struct.set(stringBytes, offset);
offset += stringBytes.byteLength;
// for(var j of item) {
// var utf8 = UnicodeToUTF8Stream(j.charCodeAt(0));
// utf8.forEach( function(c) {
// struct[offset++] = c;
// size += 1;
// length++;
// });
// }
struct[lengthoffset+0] = length & 0xFF;
struct[lengthoffset+1] = (length >> 8) & 0xFF;
break;
Expand Down Expand Up @@ -104,14 +110,16 @@ marshall.Unmarshall = function(typelist, struct, state) {
case "s":
var len = struct[offset++];
len += struct[offset++] << 8;
var str = "";
var utf8converter = new UTF8StreamToUnicode();
for(var j=0; j < len; j++) {
var c = utf8converter.Put(struct[offset++]);
if(c === -1) continue;
str += String.fromCharCode(c);
}
output.push(str);
// var str = "";
// var utf8converter = new UTF8StreamToUnicode();

// for(var j=0; j < len; j++) {
// var c = utf8converter.Put(struct[offset++]);
// if(c === -1) continue;ofc
// str += String.fromCharCode(c);
// }
var stringBytes = struct.slice(offset, offset + len);
output.push(textde.decode(stringBytes));
break;
case "Q":
state.offset = offset;
Expand Down
10 changes: 3 additions & 7 deletions lib/utf8.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,9 @@ function UnicodeToUTF8Stream(key)
if(key < 0x80) return [key];
if(key < 0x800) return [0xC0|((key>>6)&0x1F), 0x80|(key&0x3F)];
}

const texten = new TextEncoder();
const textde = new TextDecoder();
UTF8.UTF8Length = function(s)
{
var length = 0;
for(var i=0; i<s.length; i++) {
var c = s.charCodeAt(i);
length += c<128?1:2;
}
return length;
return texten.encode(s).length;
};

0 comments on commit eb6b7c6

Please sign in to comment.