From c8c027f762f5ffe09664f7e4b2d39e05b3c929b4 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Tue, 2 Mar 2021 16:38:40 +0100 Subject: [PATCH] Repair javascript integer codec --- implementations/javascript/package.json | 2 +- implementations/javascript/src/codec.ts | 6 +++--- tests/samples.bin | Bin 7996 -> 8033 bytes tests/samples.txt | 1 + 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/implementations/javascript/package.json b/implementations/javascript/package.json index d1c7af7..c6f3aa6 100644 --- a/implementations/javascript/package.json +++ b/implementations/javascript/package.json @@ -1,6 +1,6 @@ { "name": "preserves", - "version": "0.6.0", + "version": "0.6.1", "description": "Experimental data serialization format", "homepage": "https://gitlab.com/preserves/preserves", "license": "Apache-2.0", diff --git a/implementations/javascript/src/codec.ts b/implementations/javascript/src/codec.ts index d7be394..d69e478 100644 --- a/implementations/javascript/src/codec.ts +++ b/implementations/javascript/src/codec.ts @@ -128,7 +128,7 @@ export class Decoder { if (n === 0) return 0; let acc = this.nextbyte(); if (acc & 0x80) acc -= 256; - for (let i = 1; i < n; i++) acc = (acc << 8) | this.nextbyte(); + for (let i = 1; i < n; i++) acc = (acc * 256) + this.nextbyte(); return acc; } @@ -311,7 +311,7 @@ export class Encoder { encodeint(v: number) { // TODO: Bignums :-/ - const plain_bitcount = Math.floor(Math.log2(v > 0 ? v : ~v)) + 1; + const plain_bitcount = Math.floor(Math.log2(v > 0 ? v : -(1 + v))) + 1; const signed_bitcount = plain_bitcount + 1; const bytecount = (signed_bitcount + 7) >> 3; if (bytecount <= 16) { @@ -322,7 +322,7 @@ export class Encoder { } const enc = (n: number, x: number) => { if (n > 0) { - enc(n - 1, x >> 8); + enc(n - 1, Math.floor(x / 256)); this.emitbyte(x & 255); } }; diff --git a/tests/samples.bin b/tests/samples.bin index a832fc3641c963bab153de5eac4017f63db9e813..dba2dcba2c7471031272ef842e24f53c36bf7294 100644 GIT binary patch delta 47 ucmdmE_t0*`RB0vN%)AmKQv)d2vY90$wYX#x+Y*MUOg|aG int65536: int131072: + int2500000000: list0: list4: list4a: