Skip to content

Commit

Permalink
migrate 9pfs to use TextEncoder to fix 3 byte utf bug (#1139)
Browse files Browse the repository at this point in the history
  • Loading branch information
ProgrammerIn-wonderland authored Aug 29, 2024
1 parent 4061c8d commit f3339aa
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 82 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ CORE_FILES=const.js config.js io.js main.js lib.js buffer.js ide.js pci.js flopp
state.js ne2k.js sb16.js virtio.js virtio_console.js bus.js log.js \
cpu.js debug.js \
elf.js kernel.js
LIB_FILES=9p.js filesystem.js jor1k.js marshall.js utf8.js
LIB_FILES=9p.js filesystem.js jor1k.js marshall.js
BROWSER_FILES=screen.js keyboard.js mouse.js speaker.js serial.js \
network.js starter.js worker_bus.js dummy_screen.js \
fake_network.js wisp_network.js fetch_network.js print_stats.js filestorage.js
Expand Down
4 changes: 2 additions & 2 deletions lib/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
29 changes: 13 additions & 16 deletions lib/marshall.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
63 changes: 0 additions & 63 deletions lib/utf8.js

This file was deleted.

0 comments on commit f3339aa

Please sign in to comment.