@ritsuwastaken/farchive 1.0.0 → 1.0.2
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/README.md +6 -2
- package/dist/constants.d.ts +13 -0
- package/dist/constants.js +13 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/reader.d.ts +12 -12
- package/dist/reader.js +46 -25
- package/dist/text.d.ts +1 -0
- package/dist/text.js +4 -2
- package/dist/writer.js +20 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,8 +4,12 @@ reader and writer for unreal engine 1, 2 and 3 binary data
|
|
|
4
4
|
|
|
5
5
|
## Usage
|
|
6
6
|
|
|
7
|
+
```bash
|
|
8
|
+
npm i @ritsuwastaken/farchive
|
|
9
|
+
```
|
|
10
|
+
|
|
7
11
|
```ts
|
|
8
|
-
import { Reader, Writer } from "farchive";
|
|
12
|
+
import { Reader, Writer } from "@ritsuwastaken/farchive";
|
|
9
13
|
|
|
10
14
|
const reader = new Reader(bytes, { version: 123 });
|
|
11
15
|
const count = reader.index();
|
|
@@ -20,7 +24,7 @@ const out = writer.result();
|
|
|
20
24
|
## 8-bit text
|
|
21
25
|
|
|
22
26
|
```ts
|
|
23
|
-
import { decodeAnsi } from "farchive";
|
|
27
|
+
import { decodeAnsi } from "@ritsuwastaken/farchive";
|
|
24
28
|
|
|
25
29
|
decodeAnsi(reader.str(), "euc-kr");
|
|
26
30
|
decodeAnsi(reader.str(), "windows-1251");
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const packageTag = 2653586369;
|
|
2
|
+
export declare const guidSize = 16;
|
|
3
|
+
export declare const maxIndex = 2147483647;
|
|
4
|
+
export declare const compactIndex: {
|
|
5
|
+
readonly maxSize: 5;
|
|
6
|
+
readonly negative: 128;
|
|
7
|
+
readonly firstHasMore: 64;
|
|
8
|
+
readonly firstBits: 6;
|
|
9
|
+
readonly firstMask: 63;
|
|
10
|
+
readonly hasMore: 128;
|
|
11
|
+
readonly bits: 7;
|
|
12
|
+
readonly mask: 127;
|
|
13
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const packageTag = 0x9e2a83c1;
|
|
2
|
+
export const guidSize = 16;
|
|
3
|
+
export const maxIndex = 0x7fffffff;
|
|
4
|
+
export const compactIndex = {
|
|
5
|
+
maxSize: 5,
|
|
6
|
+
negative: 0x80,
|
|
7
|
+
firstHasMore: 0x40,
|
|
8
|
+
firstBits: 6,
|
|
9
|
+
firstMask: 0x3f,
|
|
10
|
+
hasMore: 0x80,
|
|
11
|
+
bits: 7,
|
|
12
|
+
mask: 0x7f,
|
|
13
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
export { compactIndex, guidSize, maxIndex, packageTag } from "./constants.ts";
|
|
1
2
|
export { ArchiveError } from "./error.ts";
|
|
2
3
|
export { Reader, usesCompactIndex, type ArchiveOptions, type NameEntry, type NameRef } from "./reader.ts";
|
|
3
|
-
export { ansiBytes, decodeAnsi } from "./text.ts";
|
|
4
|
+
export { ansiBytes, decodeAnsi, isAnsi } from "./text.ts";
|
|
4
5
|
export { versions } from "./versions.ts";
|
|
5
6
|
export { Writer } from "./writer.ts";
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
export { compactIndex, guidSize, maxIndex, packageTag } from "./constants.js";
|
|
1
2
|
export { ArchiveError } from "./error.js";
|
|
2
3
|
export { Reader, usesCompactIndex } from "./reader.js";
|
|
3
|
-
export { ansiBytes, decodeAnsi } from "./text.js";
|
|
4
|
+
export { ansiBytes, decodeAnsi, isAnsi } from "./text.js";
|
|
4
5
|
export { versions } from "./versions.js";
|
|
5
6
|
export { Writer } from "./writer.js";
|
package/dist/reader.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export interface NameRef {
|
|
|
13
13
|
export declare const usesCompactIndex: (version: number | undefined) => boolean;
|
|
14
14
|
export declare class Reader {
|
|
15
15
|
readonly bytes: Uint8Array;
|
|
16
|
-
readonly view
|
|
16
|
+
private readonly view;
|
|
17
17
|
readonly version: number | undefined;
|
|
18
18
|
readonly licensee: number | undefined;
|
|
19
19
|
offset: number;
|
|
@@ -26,17 +26,17 @@ export declare class Reader {
|
|
|
26
26
|
seek(offset: number): void;
|
|
27
27
|
skip(size: number): void;
|
|
28
28
|
take(size: number): Uint8Array;
|
|
29
|
-
private
|
|
30
|
-
u8
|
|
31
|
-
i8
|
|
32
|
-
u16
|
|
33
|
-
i16
|
|
34
|
-
u32
|
|
35
|
-
i32
|
|
36
|
-
u64
|
|
37
|
-
i64
|
|
38
|
-
f32
|
|
39
|
-
f64
|
|
29
|
+
private advance;
|
|
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
40
|
compactIndex(): number;
|
|
41
41
|
index(): number;
|
|
42
42
|
str(): string;
|
package/dist/reader.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { compactIndex as compact, guidSize, maxIndex } from "./constants.js";
|
|
1
2
|
import { ArchiveError } from "./error.js";
|
|
2
3
|
import { versions } from "./versions.js";
|
|
3
4
|
const latin1 = new TextDecoder("latin1");
|
|
@@ -41,43 +42,63 @@ export class Reader {
|
|
|
41
42
|
this.offset += size;
|
|
42
43
|
return value;
|
|
43
44
|
}
|
|
44
|
-
|
|
45
|
+
advance(size) {
|
|
45
46
|
this.need(size);
|
|
46
|
-
const
|
|
47
|
+
const at = this.offset;
|
|
47
48
|
this.offset += size;
|
|
48
|
-
return
|
|
49
|
+
return at;
|
|
50
|
+
}
|
|
51
|
+
u8() {
|
|
52
|
+
return this.view.getUint8(this.advance(1));
|
|
53
|
+
}
|
|
54
|
+
i8() {
|
|
55
|
+
return this.view.getInt8(this.advance(1));
|
|
56
|
+
}
|
|
57
|
+
u16() {
|
|
58
|
+
return this.view.getUint16(this.advance(2), true);
|
|
59
|
+
}
|
|
60
|
+
i16() {
|
|
61
|
+
return this.view.getInt16(this.advance(2), true);
|
|
62
|
+
}
|
|
63
|
+
u32() {
|
|
64
|
+
return this.view.getUint32(this.advance(4), true);
|
|
65
|
+
}
|
|
66
|
+
i32() {
|
|
67
|
+
return this.view.getInt32(this.advance(4), true);
|
|
68
|
+
}
|
|
69
|
+
u64() {
|
|
70
|
+
return this.view.getBigUint64(this.advance(8), true);
|
|
71
|
+
}
|
|
72
|
+
i64() {
|
|
73
|
+
return this.view.getBigInt64(this.advance(8), true);
|
|
74
|
+
}
|
|
75
|
+
f32() {
|
|
76
|
+
return this.view.getFloat32(this.advance(4), true);
|
|
77
|
+
}
|
|
78
|
+
f64() {
|
|
79
|
+
return this.view.getFloat64(this.advance(8), true);
|
|
49
80
|
}
|
|
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
81
|
compactIndex() {
|
|
61
82
|
const start = this.offset;
|
|
62
|
-
const
|
|
63
|
-
let value =
|
|
64
|
-
if (
|
|
65
|
-
let shift =
|
|
66
|
-
for (let i = 1; i <
|
|
83
|
+
const first = this.u8();
|
|
84
|
+
let value = first & compact.firstMask;
|
|
85
|
+
if (first & compact.firstHasMore) {
|
|
86
|
+
let shift = compact.firstBits;
|
|
87
|
+
for (let i = 1; i < compact.maxSize; i++) {
|
|
67
88
|
const byte = this.u8();
|
|
68
|
-
if (i ===
|
|
89
|
+
if (i === compact.maxSize - 1) {
|
|
69
90
|
value += byte * 2 ** shift;
|
|
70
91
|
break;
|
|
71
92
|
}
|
|
72
|
-
value += (byte &
|
|
73
|
-
shift +=
|
|
74
|
-
if (!(byte &
|
|
93
|
+
value += (byte & compact.mask) * 2 ** shift;
|
|
94
|
+
shift += compact.bits;
|
|
95
|
+
if (!(byte & compact.hasMore))
|
|
75
96
|
break;
|
|
76
97
|
}
|
|
77
98
|
}
|
|
78
|
-
if (value >
|
|
99
|
+
if (value > maxIndex)
|
|
79
100
|
throw new ArchiveError("Compact index out of range", start);
|
|
80
|
-
return
|
|
101
|
+
return first & compact.negative ? -value : value;
|
|
81
102
|
}
|
|
82
103
|
index() {
|
|
83
104
|
return usesCompactIndex(this.version) ? this.compactIndex() : this.i32();
|
|
@@ -117,7 +138,7 @@ export class Reader {
|
|
|
117
138
|
return { index, number };
|
|
118
139
|
}
|
|
119
140
|
guid() {
|
|
120
|
-
return this.take(
|
|
141
|
+
return this.take(guidSize);
|
|
121
142
|
}
|
|
122
143
|
array(read) {
|
|
123
144
|
const at = this.offset;
|
package/dist/text.d.ts
CHANGED
package/dist/text.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
const maxAnsi = 0xff;
|
|
2
|
+
export const isAnsi = (text) => !/[^\x00-\xff]/.test(text);
|
|
1
3
|
export const ansiBytes = (text) => {
|
|
2
4
|
const bytes = new Uint8Array(text.length);
|
|
3
5
|
for (let i = 0; i < text.length; i++) {
|
|
4
6
|
const code = text.charCodeAt(i);
|
|
5
|
-
if (code >
|
|
7
|
+
if (code > maxAnsi)
|
|
6
8
|
throw new RangeError(`Not an 8-bit string: ${JSON.stringify(text[i])}`);
|
|
7
9
|
bytes[i] = code;
|
|
8
10
|
}
|
|
@@ -11,7 +13,7 @@ export const ansiBytes = (text) => {
|
|
|
11
13
|
export const decodeAnsi = (text, encoding) => {
|
|
12
14
|
if (!/[\x80-\xff]/.test(text))
|
|
13
15
|
return text;
|
|
14
|
-
if (
|
|
16
|
+
if (!isAnsi(text))
|
|
15
17
|
return null;
|
|
16
18
|
try {
|
|
17
19
|
return new TextDecoder(encoding, { fatal: true }).decode(ansiBytes(text));
|
package/dist/writer.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
+
import { compactIndex as compact, guidSize, maxIndex } from "./constants.js";
|
|
1
2
|
import { ArchiveError } from "./error.js";
|
|
2
3
|
import { usesCompactIndex } from "./reader.js";
|
|
4
|
+
import { isAnsi } from "./text.js";
|
|
3
5
|
import { versions } from "./versions.js";
|
|
6
|
+
const initialCapacity = 4096;
|
|
7
|
+
const u32Mask = 0xffffffffn;
|
|
4
8
|
export class Writer {
|
|
5
9
|
version;
|
|
6
10
|
licensee;
|
|
7
|
-
buffer = new Uint8Array(
|
|
11
|
+
buffer = new Uint8Array(initialCapacity);
|
|
8
12
|
view = new DataView(this.buffer.buffer);
|
|
9
13
|
length = 0;
|
|
10
14
|
constructor(options = {}) {
|
|
@@ -70,21 +74,20 @@ export class Writer {
|
|
|
70
74
|
this.view.setFloat64(at, value, true);
|
|
71
75
|
}
|
|
72
76
|
compactIndex(value) {
|
|
73
|
-
if (!Number.isInteger(value) || Math.abs(value) >
|
|
77
|
+
if (!Number.isInteger(value) || Math.abs(value) > maxIndex)
|
|
74
78
|
throw new ArchiveError(`Compact index out of range: ${value}`);
|
|
75
79
|
let rest = Math.abs(value);
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
for (; i <
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
const fits = rest <= compact.firstMask;
|
|
81
|
+
const bytes = [(value < 0 ? compact.negative : 0) | (fits ? rest : (rest & compact.firstMask) | compact.firstHasMore)];
|
|
82
|
+
if (!fits) {
|
|
83
|
+
rest >>>= compact.firstBits;
|
|
84
|
+
for (let i = 1; i < compact.maxSize; i++) {
|
|
85
|
+
const last = i === compact.maxSize - 1 || rest <= compact.mask;
|
|
86
|
+
bytes.push(last ? rest : (rest & compact.mask) | compact.hasMore);
|
|
87
|
+
if (last)
|
|
83
88
|
break;
|
|
84
|
-
rest >>>=
|
|
89
|
+
rest >>>= compact.bits;
|
|
85
90
|
}
|
|
86
|
-
if (i === 4)
|
|
87
|
-
bytes.push(rest);
|
|
88
91
|
}
|
|
89
92
|
this.bytes(Uint8Array.from(bytes));
|
|
90
93
|
}
|
|
@@ -99,7 +102,7 @@ export class Writer {
|
|
|
99
102
|
this.index(0);
|
|
100
103
|
return;
|
|
101
104
|
}
|
|
102
|
-
const wide =
|
|
105
|
+
const wide = !isAnsi(value);
|
|
103
106
|
const size = value.length + 1;
|
|
104
107
|
this.index(wide ? -size : size);
|
|
105
108
|
if (wide) {
|
|
@@ -116,7 +119,7 @@ export class Writer {
|
|
|
116
119
|
}
|
|
117
120
|
}
|
|
118
121
|
ansiz(value) {
|
|
119
|
-
if (
|
|
122
|
+
if (!isAnsi(value) || value.includes("\0"))
|
|
120
123
|
throw new ArchiveError("A null-terminated string must be Latin-1 without zero characters");
|
|
121
124
|
const at = this.reserve(value.length + 1);
|
|
122
125
|
for (let i = 0; i < value.length; i++)
|
|
@@ -131,7 +134,7 @@ export class Writer {
|
|
|
131
134
|
if (this.version !== undefined && this.version >= versions.wideNameFlags)
|
|
132
135
|
this.u64(entry.flags);
|
|
133
136
|
else
|
|
134
|
-
this.u32(Number(entry.flags &
|
|
137
|
+
this.u32(Number(entry.flags & u32Mask));
|
|
135
138
|
}
|
|
136
139
|
name(ref) {
|
|
137
140
|
const { index, number } = typeof ref === "number" ? { index: ref, number: 0 } : ref;
|
|
@@ -140,8 +143,8 @@ export class Writer {
|
|
|
140
143
|
this.i32(number);
|
|
141
144
|
}
|
|
142
145
|
guid(value) {
|
|
143
|
-
if (value.length !==
|
|
144
|
-
throw new ArchiveError(`A GUID is
|
|
146
|
+
if (value.length !== guidSize)
|
|
147
|
+
throw new ArchiveError(`A GUID is ${guidSize} bytes, got ${value.length}`);
|
|
145
148
|
this.bytes(value);
|
|
146
149
|
}
|
|
147
150
|
array(values, write) {
|