From 05162e0c7a538ee10a6e1bb301310b99a4408292 Mon Sep 17 00:00:00 2001 From: Holly Date: Tue, 26 Nov 2024 15:08:21 +0000 Subject: [PATCH] 1.7.0 --- base/ReaderBase.ts | 13 ++++++++++++- base/WriterBase.ts | 20 ++++++++++++++++++++ package-lock.json | 11 ++++++----- package.json | 2 +- readers/IReader.ts | 3 +++ readers/ReaderBE.ts | 28 +++++++++++++++++++++++++--- readers/ReaderLE.ts | 28 +++++++++++++++++++++++++--- writers/IWriter.ts | 6 +++++- writers/WriterBE.ts | 40 +++++++++++++++++++++++++++++++++------- writers/WriterLE.ts | 30 +++++++++++++++++++++++------- 10 files changed, 153 insertions(+), 28 deletions(-) diff --git a/base/ReaderBase.ts b/base/ReaderBase.ts index bd5ece0..e425152 100644 --- a/base/ReaderBase.ts +++ b/base/ReaderBase.ts @@ -51,7 +51,7 @@ export class ReaderBase { return Boolean(this.readUByte()); } - public readShortString() { + public readUShortString() { const length = this.readUByte(); let text = ""; @@ -62,6 +62,17 @@ export class ReaderBase { return text; } + public readShortString() { + const length = this.readByte(); + let text = ""; + + for (let i = 0; i < length; i++) { + text += String.fromCharCode(this.readByte()); + } + + return text; + } + public readBytesAsString(bytesToRead:number) { let text = ""; diff --git a/base/WriterBase.ts b/base/WriterBase.ts index e568e20..43e1cd4 100644 --- a/base/WriterBase.ts +++ b/base/WriterBase.ts @@ -91,4 +91,24 @@ export class WriterBase { return this; } + + public writeUShortString(text:string) { + this.writeUByte(text.length); + + for (let i = 0; i < text.length; i++) { + this.writeUByte(text.charCodeAt(i)); + } + + return this; + } + + public writeShortString(text:string) { + this.writeByte(text.length); + + for (let i = 0; i < text.length; i++) { + this.writeByte(text.charCodeAt(i)); + } + + return this; + } } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index e700a6e..44c5191 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "bufferstuff", - "version": "1.5.1", + "version": "1.6.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "bufferstuff", - "version": "1.5.1", + "version": "1.6.0", "license": "MIT", "devDependencies": { "check-outdated": "^2.12.0", @@ -235,10 +235,11 @@ "dev": true }, "node_modules/cross-spawn": { - "version": "6.0.5", - "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.5.tgz", - "integrity": "sha512-eTVLrBSt7fjbDygz805pMnstIs2VTBNkRm0qxZd+M7A5XDdxVRWO5MxGBXZhjY4cqLYLdtrGqRf8mBPmzwSpWQ==", + "version": "6.0.6", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.6.tgz", + "integrity": "sha512-VqCUuhcd1iB+dsv8gxPttb5iZh/D0iubSP21g36KXdEuf6I5JiioesUVjpCdHV9MZRUfVFlvwtIUyPfxo5trtw==", "dev": true, + "license": "MIT", "dependencies": { "nice-try": "^1.0.4", "path-key": "^2.0.1", diff --git a/package.json b/package.json index 3a61924..4d8ea6c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bufferstuff", - "version": "1.5.1", + "version": "1.6.0", "description": "A set of utility classes for reading and writing binary data in NodeJS and the browser", "main": "./lib/index.js", "types": "./lib/index.d.ts", diff --git a/readers/IReader.ts b/readers/IReader.ts index e8a5cf0..99abd77 100644 --- a/readers/IReader.ts +++ b/readers/IReader.ts @@ -9,6 +9,7 @@ export interface IReader { readByte(): number, readUByte(): number, readBool(): boolean, + readUShortString(): string, readShortString(): string, readBytesAsString(bytesToRead:number): string, readShort(): number, @@ -19,7 +20,9 @@ export interface IReader { readULong(): bigint, readFloat(): number, readDouble(): number, + readUString(): string, readString(): string, readShortsAsString(shortsToRead:number): string, + readUString16(): string, readString16(): string, } \ No newline at end of file diff --git a/readers/ReaderBE.ts b/readers/ReaderBE.ts index 6125967..a26e760 100644 --- a/readers/ReaderBE.ts +++ b/readers/ReaderBE.ts @@ -36,7 +36,7 @@ export class ReaderBE extends ReaderBase implements IReader { } public readULong() { - const value = this.buffer.readBigUint64BE(this.offset); + const value = this.buffer.readBigUInt64BE(this.offset); this.offset += 8; return value; } @@ -53,7 +53,7 @@ export class ReaderBE extends ReaderBase implements IReader { return value; } - public readString() { + public readUString() { const length = this.readUShort(); let text = ""; @@ -64,7 +64,18 @@ export class ReaderBE extends ReaderBase implements IReader { return text; } - public readString16() { + public readString() { + const length = this.readShort(); + let text = ""; + + for (let i = 0; i < length; i++) { + text += String.fromCharCode(this.readByte()); + } + + return text; + } + + public readUString16() { const length = this.readUShort(); let text = ""; @@ -75,6 +86,17 @@ export class ReaderBE extends ReaderBase implements IReader { return text; } + public readString16() { + const length = this.readShort(); + let text = ""; + + for (let i = 0; i < length; i++) { + text += String.fromCharCode(this.readUShort()); + } + + return text; + } + public readShortsAsString(shortsToRead:number) { let text = ""; diff --git a/readers/ReaderLE.ts b/readers/ReaderLE.ts index 0f224b7..e3a3e5e 100644 --- a/readers/ReaderLE.ts +++ b/readers/ReaderLE.ts @@ -36,7 +36,7 @@ export class ReaderLE extends ReaderBase implements IReader { } public readULong() { - const value = this.buffer.readBigUint64LE(this.offset); + const value = this.buffer.readBigUInt64LE(this.offset); this.offset += 8; return value; } @@ -53,7 +53,7 @@ export class ReaderLE extends ReaderBase implements IReader { return value; } - public readString() { + public readUString() { const length = this.readUShort(); let text:string = ""; @@ -63,8 +63,19 @@ export class ReaderLE extends ReaderBase implements IReader { return text; } + + public readString() { + const length = this.readShort(); + let text:string = ""; + + for (let i = 0; i < length; i++) { + text += String.fromCharCode(this.readByte()); + } + + return text; + } - public readString16() { + public readUString16() { const length = this.readUShort(); let text:string = ""; @@ -75,6 +86,17 @@ export class ReaderLE extends ReaderBase implements IReader { return text; } + public readString16() { + const length = this.readShort(); + let text:string = ""; + + for (let i = 0; i < length; i++) { + text += String.fromCharCode(this.readShort()); + } + + return text; + } + public readShortsAsString(shortsToRead:number) { let text = ""; diff --git a/writers/IWriter.ts b/writers/IWriter.ts index dd98088..a4a62b8 100644 --- a/writers/IWriter.ts +++ b/writers/IWriter.ts @@ -20,8 +20,12 @@ export interface IWriter { writeULong(value:number|bigint): IWriter, writeFloat(value:number): IWriter, writeDouble(value:number): IWriter, + writeUShortString(text:string): IWriter, writeShortString(text:string): IWriter, + writeUString(text:string): IWriter, writeString(text:string): IWriter, + writeUString16(text:string): IWriter, writeString16(text:string): IWriter, - writeStringAsShorts(text:string): IWriter + writeStringAsShorts(text:string): IWriter, + writeJavaUTF(text:string): IWriter } \ No newline at end of file diff --git a/writers/WriterBE.ts b/writers/WriterBE.ts index 171e6a0..ee8f760 100644 --- a/writers/WriterBE.ts +++ b/writers/WriterBE.ts @@ -85,7 +85,7 @@ export class WriterBE extends WriterBase implements IWriter { buffer.writeBigUint64BE(value); this.writeBuffer(buffer); } else { - this.buffer.writeBigUint64BE(value, this.offset); + this.buffer.writeBigUInt64BE(value, this.offset); this.offset += 8; } @@ -118,8 +118,8 @@ export class WriterBE extends WriterBase implements IWriter { return this; } - public writeShortString(text:string) { - this.writeUByte(text.length); + public writeUString(text:string) { + this.writeUShort(text.length); for (let i = 0; i < text.length; i++) { this.writeUByte(text.charCodeAt(i)); @@ -129,20 +129,30 @@ export class WriterBE extends WriterBase implements IWriter { } public writeString(text:string) { + this.writeShort(text.length); + + for (let i = 0; i < text.length; i++) { + this.writeByte(text.charCodeAt(i)); + } + + return this; + } + + public writeUString16(text:string) { this.writeUShort(text.length); for (let i = 0; i < text.length; i++) { - this.writeUByte(text.charCodeAt(i)); + this.writeUShort(text.charCodeAt(i)); } return this; } public writeString16(text:string) { - this.writeUShort(text.length); + this.writeShort(text.length); for (let i = 0; i < text.length; i++) { - this.writeUShort(text.charCodeAt(i)); + this.writeShort(text.charCodeAt(i)); } return this; @@ -157,7 +167,23 @@ export class WriterBE extends WriterBase implements IWriter { } for (let i = 0; i < text.length; i++) { - buffer.writeUint16BE(text.charCodeAt(i), i); + buffer.writeUInt16BE(text.charCodeAt(i), i); + } + + return this; + } + + public writeJavaUTF(text: string) { + this.writeUShort(text.length); + + for (let i = 0; i < text.length; i++) { + const val = text.charCodeAt(i); + if (val === 0) { + this.writeByte(0xC0); + this.writeByte(0X80); + } else { + this.writeByte(val); + } } return this; diff --git a/writers/WriterLE.ts b/writers/WriterLE.ts index 0759f3a..1100359 100644 --- a/writers/WriterLE.ts +++ b/writers/WriterLE.ts @@ -84,7 +84,7 @@ export class WriterLE extends WriterBase implements IWriter { buffer.writeBigUint64LE(value); this.writeBuffer(buffer); } else { - this.buffer.writeBigUint64LE(value, this.offset); + this.buffer.writeBigUInt64LE(value, this.offset); this.offset += 8; } @@ -117,8 +117,8 @@ export class WriterLE extends WriterBase implements IWriter { return this; } - public writeShortString(text:string) { - this.writeUByte(text.length); + public writeUString(text:string) { + this.writeUShort(text.length); for (let i = 0; i < text.length; i++) { this.writeUByte(text.charCodeAt(i)); @@ -128,20 +128,30 @@ export class WriterLE extends WriterBase implements IWriter { } public writeString(text:string) { + this.writeShort(text.length); + + for (let i = 0; i < text.length; i++) { + this.writeByte(text.charCodeAt(i)); + } + + return this; + } + + public writeUString16(text:string) { this.writeUShort(text.length); for (let i = 0; i < text.length; i++) { - this.writeUByte(text.charCodeAt(i)); + this.writeUShort(text.charCodeAt(i)); } return this; } public writeString16(text:string) { - this.writeUShort(text.length); + this.writeShort(text.length); for (let i = 0; i < text.length; i++) { - this.writeUShort(text.charCodeAt(i)); + this.writeShort(text.charCodeAt(i)); } return this; @@ -156,9 +166,15 @@ export class WriterLE extends WriterBase implements IWriter { } for (let i = 0; i < text.length; i++) { - buffer.writeUint16LE(text.charCodeAt(i), i); + buffer.writeUInt16LE(text.charCodeAt(i), i); } return this; } + + public writeJavaUTF(text: string) { + throw "Not implemented in Little-Endian Writer"; + + return this; + } } \ No newline at end of file