This commit is contained in:
Holly Stubbs 2024-11-26 15:08:21 +00:00
parent ceffc3eb87
commit 05162e0c7a
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
10 changed files with 153 additions and 28 deletions

View file

@ -51,7 +51,7 @@ export class ReaderBase {
return Boolean(this.readUByte()); return Boolean(this.readUByte());
} }
public readShortString() { public readUShortString() {
const length = this.readUByte(); const length = this.readUByte();
let text = ""; let text = "";
@ -62,6 +62,17 @@ export class ReaderBase {
return text; 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) { public readBytesAsString(bytesToRead:number) {
let text = ""; let text = "";

View file

@ -91,4 +91,24 @@ export class WriterBase {
return this; 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;
}
} }

11
package-lock.json generated
View file

@ -1,12 +1,12 @@
{ {
"name": "bufferstuff", "name": "bufferstuff",
"version": "1.5.1", "version": "1.6.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "bufferstuff", "name": "bufferstuff",
"version": "1.5.1", "version": "1.6.0",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"check-outdated": "^2.12.0", "check-outdated": "^2.12.0",
@ -235,10 +235,11 @@
"dev": true "dev": true
}, },
"node_modules/cross-spawn": { "node_modules/cross-spawn": {
"version": "6.0.5", "version": "6.0.6",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.5.tgz", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-6.0.6.tgz",
"integrity": "sha512-eTVLrBSt7fjbDygz805pMnstIs2VTBNkRm0qxZd+M7A5XDdxVRWO5MxGBXZhjY4cqLYLdtrGqRf8mBPmzwSpWQ==", "integrity": "sha512-VqCUuhcd1iB+dsv8gxPttb5iZh/D0iubSP21g36KXdEuf6I5JiioesUVjpCdHV9MZRUfVFlvwtIUyPfxo5trtw==",
"dev": true, "dev": true,
"license": "MIT",
"dependencies": { "dependencies": {
"nice-try": "^1.0.4", "nice-try": "^1.0.4",
"path-key": "^2.0.1", "path-key": "^2.0.1",

View file

@ -1,6 +1,6 @@
{ {
"name": "bufferstuff", "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", "description": "A set of utility classes for reading and writing binary data in NodeJS and the browser",
"main": "./lib/index.js", "main": "./lib/index.js",
"types": "./lib/index.d.ts", "types": "./lib/index.d.ts",

View file

@ -9,6 +9,7 @@ export interface IReader {
readByte(): number, readByte(): number,
readUByte(): number, readUByte(): number,
readBool(): boolean, readBool(): boolean,
readUShortString(): string,
readShortString(): string, readShortString(): string,
readBytesAsString(bytesToRead:number): string, readBytesAsString(bytesToRead:number): string,
readShort(): number, readShort(): number,
@ -19,7 +20,9 @@ export interface IReader {
readULong(): bigint, readULong(): bigint,
readFloat(): number, readFloat(): number,
readDouble(): number, readDouble(): number,
readUString(): string,
readString(): string, readString(): string,
readShortsAsString(shortsToRead:number): string, readShortsAsString(shortsToRead:number): string,
readUString16(): string,
readString16(): string, readString16(): string,
} }

View file

@ -36,7 +36,7 @@ export class ReaderBE extends ReaderBase implements IReader {
} }
public readULong() { public readULong() {
const value = this.buffer.readBigUint64BE(this.offset); const value = this.buffer.readBigUInt64BE(this.offset);
this.offset += 8; this.offset += 8;
return value; return value;
} }
@ -53,7 +53,7 @@ export class ReaderBE extends ReaderBase implements IReader {
return value; return value;
} }
public readString() { public readUString() {
const length = this.readUShort(); const length = this.readUShort();
let text = ""; let text = "";
@ -64,7 +64,18 @@ export class ReaderBE extends ReaderBase implements IReader {
return text; 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(); const length = this.readUShort();
let text = ""; let text = "";
@ -75,6 +86,17 @@ export class ReaderBE extends ReaderBase implements IReader {
return text; 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) { public readShortsAsString(shortsToRead:number) {
let text = ""; let text = "";

View file

@ -36,7 +36,7 @@ export class ReaderLE extends ReaderBase implements IReader {
} }
public readULong() { public readULong() {
const value = this.buffer.readBigUint64LE(this.offset); const value = this.buffer.readBigUInt64LE(this.offset);
this.offset += 8; this.offset += 8;
return value; return value;
} }
@ -53,7 +53,7 @@ export class ReaderLE extends ReaderBase implements IReader {
return value; return value;
} }
public readString() { public readUString() {
const length = this.readUShort(); const length = this.readUShort();
let text:string = ""; let text:string = "";
@ -64,7 +64,18 @@ export class ReaderLE extends ReaderBase implements IReader {
return text; return text;
} }
public readString16() { public readString() {
const length = this.readShort();
let text:string = "";
for (let i = 0; i < length; i++) {
text += String.fromCharCode(this.readByte());
}
return text;
}
public readUString16() {
const length = this.readUShort(); const length = this.readUShort();
let text:string = ""; let text:string = "";
@ -75,6 +86,17 @@ export class ReaderLE extends ReaderBase implements IReader {
return text; 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) { public readShortsAsString(shortsToRead:number) {
let text = ""; let text = "";

View file

@ -20,8 +20,12 @@ export interface IWriter {
writeULong(value:number|bigint): IWriter, writeULong(value:number|bigint): IWriter,
writeFloat(value:number): IWriter, writeFloat(value:number): IWriter,
writeDouble(value:number): IWriter, writeDouble(value:number): IWriter,
writeUShortString(text:string): IWriter,
writeShortString(text:string): IWriter, writeShortString(text:string): IWriter,
writeUString(text:string): IWriter,
writeString(text:string): IWriter, writeString(text:string): IWriter,
writeUString16(text:string): IWriter,
writeString16(text:string): IWriter, writeString16(text:string): IWriter,
writeStringAsShorts(text:string): IWriter writeStringAsShorts(text:string): IWriter,
writeJavaUTF(text:string): IWriter
} }

View file

@ -85,7 +85,7 @@ export class WriterBE extends WriterBase implements IWriter {
buffer.writeBigUint64BE(value); buffer.writeBigUint64BE(value);
this.writeBuffer(buffer); this.writeBuffer(buffer);
} else { } else {
this.buffer.writeBigUint64BE(value, this.offset); this.buffer.writeBigUInt64BE(value, this.offset);
this.offset += 8; this.offset += 8;
} }
@ -118,8 +118,8 @@ export class WriterBE extends WriterBase implements IWriter {
return this; return this;
} }
public writeShortString(text:string) { public writeUString(text:string) {
this.writeUByte(text.length); this.writeUShort(text.length);
for (let i = 0; i < text.length; i++) { for (let i = 0; i < text.length; i++) {
this.writeUByte(text.charCodeAt(i)); this.writeUByte(text.charCodeAt(i));
@ -129,20 +129,30 @@ export class WriterBE extends WriterBase implements IWriter {
} }
public writeString(text:string) { 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); this.writeUShort(text.length);
for (let i = 0; i < text.length; i++) { for (let i = 0; i < text.length; i++) {
this.writeUByte(text.charCodeAt(i)); this.writeUShort(text.charCodeAt(i));
} }
return this; return this;
} }
public writeString16(text:string) { public writeString16(text:string) {
this.writeUShort(text.length); this.writeShort(text.length);
for (let i = 0; i < text.length; i++) { for (let i = 0; i < text.length; i++) {
this.writeUShort(text.charCodeAt(i)); this.writeShort(text.charCodeAt(i));
} }
return this; return this;
@ -157,7 +167,23 @@ export class WriterBE extends WriterBase implements IWriter {
} }
for (let i = 0; i < text.length; i++) { 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; return this;

View file

@ -84,7 +84,7 @@ export class WriterLE extends WriterBase implements IWriter {
buffer.writeBigUint64LE(value); buffer.writeBigUint64LE(value);
this.writeBuffer(buffer); this.writeBuffer(buffer);
} else { } else {
this.buffer.writeBigUint64LE(value, this.offset); this.buffer.writeBigUInt64LE(value, this.offset);
this.offset += 8; this.offset += 8;
} }
@ -117,8 +117,8 @@ export class WriterLE extends WriterBase implements IWriter {
return this; return this;
} }
public writeShortString(text:string) { public writeUString(text:string) {
this.writeUByte(text.length); this.writeUShort(text.length);
for (let i = 0; i < text.length; i++) { for (let i = 0; i < text.length; i++) {
this.writeUByte(text.charCodeAt(i)); this.writeUByte(text.charCodeAt(i));
@ -128,20 +128,30 @@ export class WriterLE extends WriterBase implements IWriter {
} }
public writeString(text:string) { 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); this.writeUShort(text.length);
for (let i = 0; i < text.length; i++) { for (let i = 0; i < text.length; i++) {
this.writeUByte(text.charCodeAt(i)); this.writeUShort(text.charCodeAt(i));
} }
return this; return this;
} }
public writeString16(text:string) { public writeString16(text:string) {
this.writeUShort(text.length); this.writeShort(text.length);
for (let i = 0; i < text.length; i++) { for (let i = 0; i < text.length; i++) {
this.writeUShort(text.charCodeAt(i)); this.writeShort(text.charCodeAt(i));
} }
return this; return this;
@ -156,9 +166,15 @@ export class WriterLE extends WriterBase implements IWriter {
} }
for (let i = 0; i < text.length; i++) { for (let i = 0; i < text.length; i++) {
buffer.writeUint16LE(text.charCodeAt(i), i); buffer.writeUInt16LE(text.charCodeAt(i), i);
} }
return this; return this;
} }
public writeJavaUTF(text: string) {
throw "Not implemented in Little-Endian Writer";
return this;
}
} }