mirror of
https://github.com/copy/v86.git
synced 2025-12-31 04:23:15 +00:00
migrate 9pfs to use TextEncoder to fix 3 byte utf bug (#1139)
This commit is contained in:
parent
4061c8d762
commit
f3339aa78e
4 changed files with 16 additions and 82 deletions
|
|
@ -1338,7 +1338,7 @@ FS.prototype.FillDirectory = function(dirid) {
|
|||
let size = 0;
|
||||
for(const name of inode.direntries.keys())
|
||||
{
|
||||
size += 13 + 8 + 1 + 2 + UTF8.UTF8Length(name);
|
||||
size += 13 + 8 + 1 + 2 + texten.encode(name).length;
|
||||
}
|
||||
const data = this.inodedata[dirid] = new Uint8Array(size);
|
||||
inode.size = size;
|
||||
|
|
@ -1350,7 +1350,7 @@ FS.prototype.FillDirectory = function(dirid) {
|
|||
offset += marshall.Marshall(
|
||||
["Q", "d", "b", "s"],
|
||||
[child.qid,
|
||||
offset+13+8+1+2+UTF8.UTF8Length(name),
|
||||
offset+13+8+1+2+texten.encode(name).length,
|
||||
child.mode >> 12,
|
||||
name],
|
||||
data, offset);
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@
|
|||
|
||||
var marshall = {};
|
||||
|
||||
const textde = new TextDecoder();
|
||||
const texten = new TextEncoder();
|
||||
|
||||
// Inserts data from an array to a byte aligned struct in memory
|
||||
marshall.Marshall = function(typelist, input, struct, offset) {
|
||||
|
|
@ -48,14 +50,13 @@ 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;
|
||||
|
||||
struct[lengthoffset+0] = length & 0xFF;
|
||||
struct[lengthoffset+1] = (length >> 8) & 0xFF;
|
||||
break;
|
||||
|
|
@ -104,14 +105,10 @@ 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 stringBytes = struct.slice(offset, offset + len);
|
||||
offset += len;
|
||||
output.push(textde.decode(stringBytes));
|
||||
break;
|
||||
case "Q":
|
||||
state.offset = offset;
|
||||
|
|
|
|||
63
lib/utf8.js
63
lib/utf8.js
|
|
@ -1,63 +0,0 @@
|
|||
// -------------------------------------------------
|
||||
// ------------------ UTF8 Helpers -----------------
|
||||
// -------------------------------------------------
|
||||
|
||||
"use strict";
|
||||
|
||||
var UTF8 = {};
|
||||
|
||||
/** @constructor */
|
||||
function UTF8StreamToUnicode() {
|
||||
|
||||
this.stream = new Uint8Array(5);
|
||||
this.ofs = 0;
|
||||
|
||||
this.Put = function(key) {
|
||||
this.stream[this.ofs] = key;
|
||||
this.ofs++;
|
||||
switch(this.ofs) {
|
||||
case 1:
|
||||
if(this.stream[0] < 128) {
|
||||
this.ofs = 0;
|
||||
return this.stream[0];
|
||||
}
|
||||
break;
|
||||
|
||||
case 2:
|
||||
if((this.stream[0]&0xE0) === 0xC0)
|
||||
if((this.stream[1]&0xC0) === 0x80) {
|
||||
this.ofs = 0;
|
||||
return ((this.stream[0]&0x1F)<<6) | (this.stream[1]&0x3F);
|
||||
}
|
||||
break;
|
||||
|
||||
case 3:
|
||||
break;
|
||||
|
||||
case 4:
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
//this.ofs = 0;
|
||||
//break;
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
}
|
||||
|
||||
function UnicodeToUTF8Stream(key)
|
||||
{
|
||||
if(key < 0x80) return [key];
|
||||
if(key < 0x800) return [0xC0|((key>>6)&0x1F), 0x80|(key&0x3F)];
|
||||
}
|
||||
|
||||
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;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue