Skip to content

Commit

Permalink
util: Add ulz
Browse files Browse the repository at this point in the history
  • Loading branch information
remko committed Nov 21, 2023
1 parent 58a656c commit 8c9c119
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 5 deletions.
98 changes: 97 additions & 1 deletion src/tests/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@ import { Uxn } from "../uxn";
import { expect } from "chai";
import testsTAL from "./tests.tal";
import opctestTAL from "./opctest.tal";
import { asm as asm_ } from "../util/index.js";
import {
asm as asm_,
decodeUlz as decodeUlz_,
encodeUlz as encodeUlz_,
} from "../util/index.js";

function asm(v) {
return Array.from(asm_(v));
}

function decodeUlz(v) {
return Array.from(decodeUlz_(v));
}

function encodeUlz(v) {
return Array.from(encodeUlz_(v));
}

const [
BRK,
INC,
Expand Down Expand Up @@ -814,6 +826,90 @@ function loadTests() {
expect(result).to.eql([LIT, 0x10]);
});
});

////////////////////////////////////////////////////////////////////////////////

describe("ulz", () => {
const tests = [
{
decoded: `Blue like my corvette its in and outside
Blue are the words I say
And what I think
Blue are the feelings
That live inside me
I'm blue
Da ba dee da ba di
Da ba dee da ba di
Da ba dee da ba di
Da ba dee da ba di`,
encoded: [
0x28, 0x42, 0x6c, 0x75, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20,
0x6d, 0x79, 0x20, 0x63, 0x6f, 0x72, 0x76, 0x65, 0x74, 0x74, 0x65,
0x20, 0x69, 0x74, 0x73, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x6e, 0x64,
0x20, 0x6f, 0x75, 0x74, 0x73, 0x69, 0x64, 0x65, 0x0a, 0x81, 0x28,
0x23, 0x61, 0x72, 0x65, 0x20, 0x74, 0x68, 0x65, 0x20, 0x77, 0x6f,
0x72, 0x64, 0x73, 0x20, 0x49, 0x20, 0x73, 0x61, 0x79, 0x0a, 0x41,
0x6e, 0x64, 0x20, 0x77, 0x68, 0x61, 0x74, 0x20, 0x49, 0x20, 0x74,
0x68, 0x69, 0x6e, 0x6b, 0x8a, 0x29, 0x09, 0x66, 0x65, 0x65, 0x6c,
0x69, 0x6e, 0x67, 0x73, 0x0a, 0x54, 0x80, 0x22, 0x06, 0x6c, 0x69,
0x76, 0x65, 0x20, 0x69, 0x6e, 0x80, 0x50, 0x17, 0x20, 0x6d, 0x65,
0x0a, 0x49, 0x27, 0x6d, 0x20, 0x62, 0x6c, 0x75, 0x65, 0x0a, 0x44,
0x61, 0x20, 0x62, 0x61, 0x20, 0x64, 0x65, 0x65, 0x20, 0x64, 0x82,
0x09, 0x00, 0x69, 0xb5, 0x12,
],
},
{
decoded: `abcd`,
encoded: [0x03, 0x61, 0x62, 0x63, 0x64],
},
{
decoded: `abcdabcdabcd`,
encoded: [0x03, 0x61, 0x62, 0x63, 0x64, 0x84, 0x03],
},
];

describe("decodeUlz", () => {
for (const t of tests) {
it(`should decode "${t.decoded.substr(0, 10)}"`, () => {
const result = decodeUlz(t.encoded);
expect(result.map((c) => String.fromCharCode(c)).join("")).to.eql(
t.decoded
);
});
}

it("should not allow out-of-bounds LIT", () => {
expect(() => decodeUlz([0x2, 0x1])).to.throw();
});

it("should not allow underflow CPY", () => {
expect(() => decodeUlz([0x81, 0x1])).to.throw();
});

it("should not allow incomplete CPY instruction", () => {
expect(() => decodeUlz([0x1, 0x2, 0x3, 0x81])).to.throw();
});

it("should not allow incomplete CPY2 instruction", () => {
expect(() => decodeUlz([0x1, 0x2, 0x3, 0xc1])).to.throw();
});

it("should not allow incomplete CPY2 instruction (2)", () => {
expect(() => decodeUlz([0x1, 0x2, 0x3, 0xc1, 0x1])).to.throw();
});
});

describe("encodeUlz", () => {
for (const t of tests) {
it(`should encode "${t.decoded.substr(0, 10)}"`, () => {
const result = encodeUlz(
t.decoded.split("").map((c, i) => c.charCodeAt(0))
);
expect(result).to.eql(t.encoded);
});
}
});
});
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/util/asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function expandLabel(label, currentLabel) {
return assertValidName(label);
}

export default function asm(
export function asm(
source,
options = {
include: () => {
Expand Down
5 changes: 2 additions & 3 deletions src/util/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import asm from "./asm.js";

export { asm };
export { asm } from "./asm.js";
export { decodeUlz, encodeUlz } from "./ulz.js";

export function peek16(mem, addr) {
return (mem[addr] << 8) | mem[addr + 1];
Expand Down
99 changes: 99 additions & 0 deletions src/util/ulz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
export function decodeUlz(src) {
const dst = [];
let sp = 0;
while (sp < src.length) {
const c = src[sp++];
if (c & 0x80) {
// CPY
let length;
if (c & 0x40) {
if (sp >= src.length) {
throw new Error(`incomplete CPY2`);
}
length = ((c & 0x3f) << 8) | src[sp++];
} else {
length = c & 0x3f;
}
if (sp >= src.length) {
throw new Error(`incomplete CPY`);
}
let cp = dst.length - (src[sp++] + 1);
if (cp < 0) {
throw new Error(`CPY underflow`);
}
for (let i = 0; i < length + 4; i++) {
dst.push(dst[cp++]);
}
} else {
// LIT
if (sp + c >= src.length) {
throw new Error(`LIT out of bounds: ${sp} + ${c} >= ${src.length}`);
}
for (let i = 0; i < c + 1; i++) {
dst.push(src[sp++]);
}
}
}
return new Uint8Array(dst);
}

const MIN_MAX_LENGTH = 4;

function findBestMatch(src, sp, dlen, slen) {
let bmlen = 0;
let bmp = 0;
let dp = sp - dlen;
for (; dlen; dp++, dlen--) {
let i = 0;
for (; ; i++) {
if (i == slen) {
return [dp, i];
}
if (src[sp + i] != src[dp + (i % dlen)]) {
break;
}
}
if (i > bmlen) {
bmlen = i;
bmp = dp;
}
}
return [bmp, bmlen];
}

export function encodeUlz(src) {
let dst = [];
let sp = 0;
let litp = -1;
while (sp < src.length) {
const dlen = Math.min(sp, 256);
const slen = Math.min(src.length - sp, 0x3fff + MIN_MAX_LENGTH);
const [bmp, bmlen] = findBestMatch(src, sp, dlen, slen);
if (bmlen >= MIN_MAX_LENGTH) {
// CPY
const bmctl = bmlen - MIN_MAX_LENGTH;
if (bmctl > 0x3f) {
// CPY2
dst.push((bmctl >> 8) | 0xc0);
dst.push(bmctl & 0xff);
} else {
dst.push(bmctl | 0x80);
}
dst.push(sp - bmp - 1);
sp += bmlen;
litp = -1;
} else {
// LIT
if (litp >= 0) {
if ((dst[litp] += 1) == 127) {
litp = -1;
}
} else {
dst.push(0);
litp = dst.length - 1;
}
dst.push(src[sp++]);
}
}
return new Uint8Array(dst);
}

0 comments on commit 8c9c119

Please sign in to comment.