@ritsuwastaken/farchive 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <https://unlicense.org>
package/README.md CHANGED
@@ -1,3 +1,27 @@
1
- # Temporary Holding Version
1
+ # farchive
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ reader and writer for unreal engine 1, 2 and 3 binary data
4
+
5
+ ## Usage
6
+
7
+ ```ts
8
+ import { Reader, Writer } from "farchive";
9
+
10
+ const reader = new Reader(bytes, { version: 123 });
11
+ const count = reader.index();
12
+ const names = Array.from({ length: count }, () => reader.nameEntry());
13
+
14
+ const writer = new Writer({ version: 123 });
15
+ writer.index(names.length);
16
+ for (const entry of names) writer.nameEntry(entry);
17
+ const out = writer.result();
18
+ ```
19
+
20
+ ## 8-bit text
21
+
22
+ ```ts
23
+ import { decodeAnsi } from "farchive";
24
+
25
+ decodeAnsi(reader.str(), "euc-kr");
26
+ decodeAnsi(reader.str(), "windows-1251");
27
+ ```
@@ -0,0 +1,5 @@
1
+ export declare class ArchiveError extends Error {
2
+ readonly offset: number;
3
+ readonly detail: string;
4
+ constructor(message: string, offset?: number);
5
+ }
package/dist/error.js ADDED
@@ -0,0 +1,10 @@
1
+ export class ArchiveError extends Error {
2
+ offset;
3
+ detail;
4
+ constructor(message, offset = -1) {
5
+ super(offset >= 0 ? `${message} at offset ${offset}` : message);
6
+ this.name = "ArchiveError";
7
+ this.offset = offset;
8
+ this.detail = message;
9
+ }
10
+ }
@@ -0,0 +1,5 @@
1
+ export { ArchiveError } from "./error.ts";
2
+ export { Reader, usesCompactIndex, type ArchiveOptions, type NameEntry, type NameRef } from "./reader.ts";
3
+ export { ansiBytes, decodeAnsi } from "./text.ts";
4
+ export { versions } from "./versions.ts";
5
+ export { Writer } from "./writer.ts";
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export { ArchiveError } from "./error.js";
2
+ export { Reader, usesCompactIndex } from "./reader.js";
3
+ export { ansiBytes, decodeAnsi } from "./text.js";
4
+ export { versions } from "./versions.js";
5
+ export { Writer } from "./writer.js";
@@ -0,0 +1,48 @@
1
+ export interface ArchiveOptions {
2
+ version?: number;
3
+ licensee?: number;
4
+ }
5
+ export interface NameEntry {
6
+ name: string;
7
+ flags: bigint;
8
+ }
9
+ export interface NameRef {
10
+ index: number;
11
+ number: number;
12
+ }
13
+ export declare const usesCompactIndex: (version: number | undefined) => boolean;
14
+ export declare class Reader {
15
+ readonly bytes: Uint8Array;
16
+ readonly view: DataView;
17
+ readonly version: number | undefined;
18
+ readonly licensee: number | undefined;
19
+ offset: number;
20
+ constructor(bytes: Uint8Array, options?: ArchiveOptions & {
21
+ offset?: number;
22
+ });
23
+ get remaining(): number;
24
+ get atEnd(): boolean;
25
+ need(size: number): void;
26
+ seek(offset: number): void;
27
+ skip(size: number): void;
28
+ take(size: number): Uint8Array;
29
+ private fixed;
30
+ u8: () => number;
31
+ i8: () => number;
32
+ u16: () => number;
33
+ i16: () => number;
34
+ u32: () => number;
35
+ i32: () => number;
36
+ u64: () => bigint;
37
+ i64: () => bigint;
38
+ f32: () => number;
39
+ f64: () => number;
40
+ compactIndex(): number;
41
+ index(): number;
42
+ str(): string;
43
+ ansiz(): string;
44
+ nameEntry(): NameEntry;
45
+ name(): NameRef;
46
+ guid(): Uint8Array;
47
+ array<T>(read: (reader: this) => T): T[];
48
+ }
package/dist/reader.js ADDED
@@ -0,0 +1,129 @@
1
+ import { ArchiveError } from "./error.js";
2
+ import { versions } from "./versions.js";
3
+ const latin1 = new TextDecoder("latin1");
4
+ const utf16 = new TextDecoder("utf-16le");
5
+ export const usesCompactIndex = (version) => version === undefined || version < versions.int32Indexes;
6
+ export class Reader {
7
+ bytes;
8
+ view;
9
+ version;
10
+ licensee;
11
+ offset;
12
+ constructor(bytes, options = {}) {
13
+ this.bytes = bytes;
14
+ this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
15
+ this.version = options.version;
16
+ this.licensee = options.licensee;
17
+ this.offset = options.offset ?? 0;
18
+ }
19
+ get remaining() {
20
+ return this.bytes.length - this.offset;
21
+ }
22
+ get atEnd() {
23
+ return this.offset >= this.bytes.length;
24
+ }
25
+ need(size) {
26
+ if (size < 0 || this.offset + size > this.bytes.length)
27
+ throw new ArchiveError("Unexpected end of data", this.offset);
28
+ }
29
+ seek(offset) {
30
+ if (offset < 0 || offset > this.bytes.length)
31
+ throw new ArchiveError(`Cannot seek to ${offset}`, this.offset);
32
+ this.offset = offset;
33
+ }
34
+ skip(size) {
35
+ this.need(size);
36
+ this.offset += size;
37
+ }
38
+ take(size) {
39
+ this.need(size);
40
+ const value = this.bytes.slice(this.offset, this.offset + size);
41
+ this.offset += size;
42
+ return value;
43
+ }
44
+ fixed(size, read) {
45
+ this.need(size);
46
+ const value = read(this.offset);
47
+ this.offset += size;
48
+ return value;
49
+ }
50
+ u8 = () => this.fixed(1, (at) => this.view.getUint8(at));
51
+ i8 = () => this.fixed(1, (at) => this.view.getInt8(at));
52
+ u16 = () => this.fixed(2, (at) => this.view.getUint16(at, true));
53
+ i16 = () => this.fixed(2, (at) => this.view.getInt16(at, true));
54
+ u32 = () => this.fixed(4, (at) => this.view.getUint32(at, true));
55
+ i32 = () => this.fixed(4, (at) => this.view.getInt32(at, true));
56
+ u64 = () => this.fixed(8, (at) => this.view.getBigUint64(at, true));
57
+ i64 = () => this.fixed(8, (at) => this.view.getBigInt64(at, true));
58
+ f32 = () => this.fixed(4, (at) => this.view.getFloat32(at, true));
59
+ f64 = () => this.fixed(8, (at) => this.view.getFloat64(at, true));
60
+ compactIndex() {
61
+ const start = this.offset;
62
+ const b0 = this.u8();
63
+ let value = b0 & 0x3f;
64
+ if (b0 & 0x40) {
65
+ let shift = 6;
66
+ for (let i = 1; i < 5; i++) {
67
+ const byte = this.u8();
68
+ if (i === 4) {
69
+ value += byte * 2 ** shift;
70
+ break;
71
+ }
72
+ value += (byte & 0x7f) * 2 ** shift;
73
+ shift += 7;
74
+ if (!(byte & 0x80))
75
+ break;
76
+ }
77
+ }
78
+ if (value > 0x7fffffff)
79
+ throw new ArchiveError("Compact index out of range", start);
80
+ return b0 & 0x80 ? -value : value;
81
+ }
82
+ index() {
83
+ return usesCompactIndex(this.version) ? this.compactIndex() : this.i32();
84
+ }
85
+ str() {
86
+ const start = this.offset;
87
+ const length = this.index();
88
+ if (length === 0)
89
+ return "";
90
+ const wide = length < 0;
91
+ const size = wide ? -length * 2 : length;
92
+ this.need(size);
93
+ const end = this.offset + size;
94
+ const terminated = wide ? this.bytes[end - 2] === 0 && this.bytes[end - 1] === 0 : this.bytes[end - 1] === 0;
95
+ if (!terminated)
96
+ throw new ArchiveError("String is not terminated", start);
97
+ const text = (wide ? utf16 : latin1).decode(this.bytes.subarray(this.offset, end - (wide ? 2 : 1)));
98
+ this.offset = end;
99
+ return text;
100
+ }
101
+ ansiz() {
102
+ const end = this.bytes.indexOf(0, this.offset);
103
+ if (end < 0)
104
+ throw new ArchiveError("String is not terminated", this.offset);
105
+ const text = latin1.decode(this.bytes.subarray(this.offset, end));
106
+ this.offset = end + 1;
107
+ return text;
108
+ }
109
+ nameEntry() {
110
+ const name = this.version !== undefined && this.version < versions.lengthPrefixedNames ? this.ansiz() : this.str();
111
+ const flags = this.version !== undefined && this.version >= versions.wideNameFlags ? this.u64() : BigInt(this.u32());
112
+ return { name, flags };
113
+ }
114
+ name() {
115
+ const index = this.index();
116
+ const number = this.version !== undefined && this.version >= versions.nameNumbers ? this.i32() : 0;
117
+ return { index, number };
118
+ }
119
+ guid() {
120
+ return this.take(16);
121
+ }
122
+ array(read) {
123
+ const at = this.offset;
124
+ const count = this.index();
125
+ if (count < 0 || count > this.remaining)
126
+ throw new ArchiveError(`Invalid array length ${count}`, at);
127
+ return Array.from({ length: count }, () => read(this));
128
+ }
129
+ }
package/dist/text.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export declare const ansiBytes: (text: string) => Uint8Array;
2
+ export declare const decodeAnsi: (text: string, encoding: string) => string | null;
package/dist/text.js ADDED
@@ -0,0 +1,22 @@
1
+ export const ansiBytes = (text) => {
2
+ const bytes = new Uint8Array(text.length);
3
+ for (let i = 0; i < text.length; i++) {
4
+ const code = text.charCodeAt(i);
5
+ if (code > 0xff)
6
+ throw new RangeError(`Not an 8-bit string: ${JSON.stringify(text[i])}`);
7
+ bytes[i] = code;
8
+ }
9
+ return bytes;
10
+ };
11
+ export const decodeAnsi = (text, encoding) => {
12
+ if (!/[\x80-\xff]/.test(text))
13
+ return text;
14
+ if (/[^\x00-\xff]/.test(text))
15
+ return null;
16
+ try {
17
+ return new TextDecoder(encoding, { fatal: true }).decode(ansiBytes(text));
18
+ }
19
+ catch {
20
+ return null;
21
+ }
22
+ };
@@ -0,0 +1,6 @@
1
+ export declare const versions: {
2
+ readonly lengthPrefixedNames: 64;
3
+ readonly int32Indexes: 178;
4
+ readonly wideNameFlags: 195;
5
+ readonly nameNumbers: 343;
6
+ };
@@ -0,0 +1,6 @@
1
+ export const versions = {
2
+ lengthPrefixedNames: 64,
3
+ int32Indexes: 178,
4
+ wideNameFlags: 195,
5
+ nameNumbers: 343,
6
+ };
@@ -0,0 +1,32 @@
1
+ import { type ArchiveOptions, type NameEntry, type NameRef } from "./reader.ts";
2
+ export declare class Writer {
3
+ readonly version: number | undefined;
4
+ readonly licensee: number | undefined;
5
+ private buffer;
6
+ private view;
7
+ length: number;
8
+ constructor(options?: ArchiveOptions);
9
+ private reserve;
10
+ bytes(value: Uint8Array): void;
11
+ u8(value: number): void;
12
+ i8(value: number): void;
13
+ u16(value: number): void;
14
+ i16(value: number): void;
15
+ u32(value: number): void;
16
+ i32(value: number): void;
17
+ u64(value: bigint): void;
18
+ i64(value: bigint): void;
19
+ f32(value: number): void;
20
+ f64(value: number): void;
21
+ compactIndex(value: number): void;
22
+ index(value: number): void;
23
+ str(value: string): void;
24
+ ansiz(value: string): void;
25
+ nameEntry(entry: NameEntry): void;
26
+ name(ref: NameRef | number): void;
27
+ guid(value: Uint8Array): void;
28
+ array<T>(values: readonly T[], write: (writer: this, value: T) => void): void;
29
+ patchU32(at: number, value: number): void;
30
+ patchI32(at: number, value: number): void;
31
+ result(): Uint8Array;
32
+ }
package/dist/writer.js ADDED
@@ -0,0 +1,161 @@
1
+ import { ArchiveError } from "./error.js";
2
+ import { usesCompactIndex } from "./reader.js";
3
+ import { versions } from "./versions.js";
4
+ export class Writer {
5
+ version;
6
+ licensee;
7
+ buffer = new Uint8Array(1 << 12);
8
+ view = new DataView(this.buffer.buffer);
9
+ length = 0;
10
+ constructor(options = {}) {
11
+ this.version = options.version;
12
+ this.licensee = options.licensee;
13
+ }
14
+ reserve(size) {
15
+ const at = this.length;
16
+ if (at + size > this.buffer.length) {
17
+ let capacity = this.buffer.length * 2;
18
+ while (capacity < at + size)
19
+ capacity *= 2;
20
+ const next = new Uint8Array(capacity);
21
+ next.set(this.buffer.subarray(0, at));
22
+ this.buffer = next;
23
+ this.view = new DataView(next.buffer);
24
+ }
25
+ this.length += size;
26
+ return at;
27
+ }
28
+ bytes(value) {
29
+ const at = this.reserve(value.length);
30
+ this.buffer.set(value, at);
31
+ }
32
+ u8(value) {
33
+ const at = this.reserve(1);
34
+ this.view.setUint8(at, value);
35
+ }
36
+ i8(value) {
37
+ const at = this.reserve(1);
38
+ this.view.setInt8(at, value);
39
+ }
40
+ u16(value) {
41
+ const at = this.reserve(2);
42
+ this.view.setUint16(at, value, true);
43
+ }
44
+ i16(value) {
45
+ const at = this.reserve(2);
46
+ this.view.setInt16(at, value, true);
47
+ }
48
+ u32(value) {
49
+ const at = this.reserve(4);
50
+ this.view.setUint32(at, value >>> 0, true);
51
+ }
52
+ i32(value) {
53
+ const at = this.reserve(4);
54
+ this.view.setInt32(at, value, true);
55
+ }
56
+ u64(value) {
57
+ const at = this.reserve(8);
58
+ this.view.setBigUint64(at, value, true);
59
+ }
60
+ i64(value) {
61
+ const at = this.reserve(8);
62
+ this.view.setBigInt64(at, value, true);
63
+ }
64
+ f32(value) {
65
+ const at = this.reserve(4);
66
+ this.view.setFloat32(at, value, true);
67
+ }
68
+ f64(value) {
69
+ const at = this.reserve(8);
70
+ this.view.setFloat64(at, value, true);
71
+ }
72
+ compactIndex(value) {
73
+ if (!Number.isInteger(value) || Math.abs(value) > 0x7fffffff)
74
+ throw new ArchiveError(`Compact index out of range: ${value}`);
75
+ let rest = Math.abs(value);
76
+ const bytes = [(value < 0 ? 0x80 : 0) | (rest < 0x40 ? rest : (rest & 0x3f) | 0x40)];
77
+ if (rest >= 0x40) {
78
+ rest >>>= 6;
79
+ let i = 1;
80
+ for (; i < 4; i++) {
81
+ bytes.push(rest < 0x80 ? rest : (rest & 0x7f) | 0x80);
82
+ if (rest < 0x80)
83
+ break;
84
+ rest >>>= 7;
85
+ }
86
+ if (i === 4)
87
+ bytes.push(rest);
88
+ }
89
+ this.bytes(Uint8Array.from(bytes));
90
+ }
91
+ index(value) {
92
+ if (usesCompactIndex(this.version))
93
+ this.compactIndex(value);
94
+ else
95
+ this.i32(value);
96
+ }
97
+ str(value) {
98
+ if (value === "") {
99
+ this.index(0);
100
+ return;
101
+ }
102
+ const wide = /[^\x00-\xff]/.test(value);
103
+ const size = value.length + 1;
104
+ this.index(wide ? -size : size);
105
+ if (wide) {
106
+ const at = this.reserve(size * 2);
107
+ for (let i = 0; i < value.length; i++)
108
+ this.view.setUint16(at + i * 2, value.charCodeAt(i), true);
109
+ this.view.setUint16(at + value.length * 2, 0, true);
110
+ }
111
+ else {
112
+ const at = this.reserve(size);
113
+ for (let i = 0; i < value.length; i++)
114
+ this.buffer[at + i] = value.charCodeAt(i);
115
+ this.buffer[at + value.length] = 0;
116
+ }
117
+ }
118
+ ansiz(value) {
119
+ if (/[^\x01-\xff]/.test(value))
120
+ throw new ArchiveError("A null-terminated string must be Latin-1 without zero characters");
121
+ const at = this.reserve(value.length + 1);
122
+ for (let i = 0; i < value.length; i++)
123
+ this.buffer[at + i] = value.charCodeAt(i);
124
+ this.buffer[at + value.length] = 0;
125
+ }
126
+ nameEntry(entry) {
127
+ if (this.version !== undefined && this.version < versions.lengthPrefixedNames)
128
+ this.ansiz(entry.name);
129
+ else
130
+ this.str(entry.name);
131
+ if (this.version !== undefined && this.version >= versions.wideNameFlags)
132
+ this.u64(entry.flags);
133
+ else
134
+ this.u32(Number(entry.flags & 0xffffffffn));
135
+ }
136
+ name(ref) {
137
+ const { index, number } = typeof ref === "number" ? { index: ref, number: 0 } : ref;
138
+ this.index(index);
139
+ if (this.version !== undefined && this.version >= versions.nameNumbers)
140
+ this.i32(number);
141
+ }
142
+ guid(value) {
143
+ if (value.length !== 16)
144
+ throw new ArchiveError(`A GUID is 16 bytes, got ${value.length}`);
145
+ this.bytes(value);
146
+ }
147
+ array(values, write) {
148
+ this.index(values.length);
149
+ for (const value of values)
150
+ write(this, value);
151
+ }
152
+ patchU32(at, value) {
153
+ this.view.setUint32(at, value >>> 0, true);
154
+ }
155
+ patchI32(at, value) {
156
+ this.view.setInt32(at, value, true);
157
+ }
158
+ result() {
159
+ return this.buffer.slice(0, this.length);
160
+ }
161
+ }
package/package.json CHANGED
@@ -1,6 +1,51 @@
1
1
  {
2
- "name": "@ritsuwastaken/farchive",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@ritsuwastaken/farchive",
3
+ "version": "1.0.0",
4
+ "description": "reader and writer for unreal engine 1, 2 and 3 binary data",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "README.md"
18
+ ],
19
+ "keywords": [
20
+ "farchive",
21
+ "archive",
22
+ "serialization",
23
+ "compact index",
24
+ "fstring",
25
+ "binary",
26
+ "typescript",
27
+ "javascript",
28
+ "unreal",
29
+ "ue",
30
+ "ue2",
31
+ "ue3"
32
+ ],
33
+ "author": "ritsuwastaken",
34
+ "license": "Unlicense",
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "git+https://github.com/ritsuwastaken/farchive.git"
38
+ },
39
+ "engines": {
40
+ "node": ">=22"
41
+ },
42
+ "scripts": {
43
+ "build": "tsc -p tsconfig.json",
44
+ "test": "npm run build && node --test \"test/**/*.test.ts\"",
45
+ "prepack": "npm run build"
46
+ },
47
+ "devDependencies": {
48
+ "@types/node": "^22.20.5",
49
+ "typescript": "7.0.2"
50
+ }
51
+ }