@k67/kaitai-struct-ts 0.6.0 → 0.7.1
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 +110 -37
- package/dist/cli.js +2776 -0
- package/dist/index.d.mts +197 -229
- package/dist/index.d.ts +197 -229
- package/dist/index.js +5 -110
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5 -110
- package/dist/index.mjs.map +1 -1
- package/package.json +23 -18
package/dist/index.mjs
CHANGED
@@ -151,7 +151,6 @@ var KaitaiStream = class _KaitaiStream {
|
|
151
151
|
if (buffer instanceof ArrayBuffer) {
|
152
152
|
this.buffer = new Uint8Array(buffer);
|
153
153
|
this.view = new DataView(buffer);
|
154
|
-
this._size = buffer.byteLength;
|
155
154
|
} else {
|
156
155
|
this.buffer = buffer;
|
157
156
|
this.view = new DataView(
|
@@ -159,7 +158,6 @@ var KaitaiStream = class _KaitaiStream {
|
|
159
158
|
buffer.byteOffset,
|
160
159
|
buffer.byteLength
|
161
160
|
);
|
162
|
-
this._size = buffer.byteLength;
|
163
161
|
}
|
164
162
|
}
|
165
163
|
/**
|
@@ -176,13 +174,13 @@ var KaitaiStream = class _KaitaiStream {
|
|
176
174
|
* Total size of the stream in bytes
|
177
175
|
*/
|
178
176
|
get size() {
|
179
|
-
return this.
|
177
|
+
return this.buffer.length;
|
180
178
|
}
|
181
179
|
/**
|
182
180
|
* Check if we've reached the end of the stream
|
183
181
|
*/
|
184
182
|
isEof() {
|
185
|
-
return this._pos >= this.
|
183
|
+
return this._pos >= this.buffer.length;
|
186
184
|
}
|
187
185
|
/**
|
188
186
|
* Seek to a specific position in the stream
|
@@ -602,7 +600,6 @@ var KsyParser = class {
|
|
602
600
|
throw new ParseError("KSY file must contain an object");
|
603
601
|
}
|
604
602
|
const schema = parsed;
|
605
|
-
this.processParametricTypes(schema);
|
606
603
|
if (validate) {
|
607
604
|
const result = this.validate(schema, { strict });
|
608
605
|
if (!result.valid) {
|
@@ -620,65 +617,6 @@ var KsyParser = class {
|
|
620
617
|
}
|
621
618
|
return schema;
|
622
619
|
}
|
623
|
-
/**
|
624
|
-
* Process parametric type syntax in schema.
|
625
|
-
* Converts "type_name(arg1, arg2)" to structured format.
|
626
|
-
*
|
627
|
-
* @param schema - Schema to process
|
628
|
-
* @private
|
629
|
-
*/
|
630
|
-
processParametricTypes(schema) {
|
631
|
-
if (schema.seq) {
|
632
|
-
for (const attr of schema.seq) {
|
633
|
-
if (typeof attr.type === "string") {
|
634
|
-
this.parseParametricType(attr);
|
635
|
-
}
|
636
|
-
}
|
637
|
-
}
|
638
|
-
if (schema.instances) {
|
639
|
-
for (const instance of Object.values(schema.instances)) {
|
640
|
-
if (typeof instance.type === "string") {
|
641
|
-
this.parseParametricType(instance);
|
642
|
-
}
|
643
|
-
}
|
644
|
-
}
|
645
|
-
if (schema.types) {
|
646
|
-
for (const type of Object.values(schema.types)) {
|
647
|
-
this.processParametricTypes(type);
|
648
|
-
}
|
649
|
-
}
|
650
|
-
}
|
651
|
-
/**
|
652
|
-
* Parse parametric type syntax from a type string.
|
653
|
-
* Converts "type_name(arg1, arg2)" to type + type-args.
|
654
|
-
*
|
655
|
-
* @param attr - Attribute to process
|
656
|
-
* @private
|
657
|
-
*/
|
658
|
-
parseParametricType(attr) {
|
659
|
-
if (typeof attr.type !== "string") return;
|
660
|
-
const match = attr.type.match(/^([a-z_][a-z0-9_]*)\((.*)\)$/i);
|
661
|
-
if (!match) return;
|
662
|
-
const [, typeName, argsStr] = match;
|
663
|
-
const args = [];
|
664
|
-
if (argsStr.trim()) {
|
665
|
-
const argParts = argsStr.split(",").map((s) => s.trim());
|
666
|
-
for (const arg of argParts) {
|
667
|
-
const num = Number(arg);
|
668
|
-
if (!isNaN(num)) {
|
669
|
-
args.push(num);
|
670
|
-
} else if (arg === "true") {
|
671
|
-
args.push(true);
|
672
|
-
} else if (arg === "false") {
|
673
|
-
args.push(false);
|
674
|
-
} else {
|
675
|
-
args.push(arg);
|
676
|
-
}
|
677
|
-
}
|
678
|
-
}
|
679
|
-
attr.type = typeName;
|
680
|
-
attr["type-args"] = args;
|
681
|
-
}
|
682
620
|
/**
|
683
621
|
* Validate a schema object.
|
684
622
|
*
|
@@ -1974,10 +1912,6 @@ var TypeInterpreter = class _TypeInterpreter {
|
|
1974
1912
|
constructor(schema, parentMeta) {
|
1975
1913
|
this.schema = schema;
|
1976
1914
|
this.parentMeta = parentMeta;
|
1977
|
-
// Performance optimization: cache for constant expressions
|
1978
|
-
// Limited size to prevent memory leaks
|
1979
|
-
this.expressionCache = /* @__PURE__ */ new Map();
|
1980
|
-
this.MAX_CACHE_SIZE = 1e3;
|
1981
1915
|
if (!schema.meta && !parentMeta) {
|
1982
1916
|
throw new ParseError("Schema must have meta section");
|
1983
1917
|
}
|
@@ -2082,10 +2016,7 @@ var TypeInterpreter = class _TypeInterpreter {
|
|
2082
2016
|
);
|
2083
2017
|
}
|
2084
2018
|
}
|
2085
|
-
const value = this.parseAttribute(
|
2086
|
-
instance,
|
2087
|
-
context
|
2088
|
-
);
|
2019
|
+
const value = this.parseAttribute(instance, context);
|
2089
2020
|
return value;
|
2090
2021
|
} finally {
|
2091
2022
|
if (instance.pos !== void 0) {
|
@@ -2122,11 +2053,6 @@ var TypeInterpreter = class _TypeInterpreter {
|
|
2122
2053
|
if (attr.io) {
|
2123
2054
|
throw new NotImplementedError("Custom I/O streams");
|
2124
2055
|
}
|
2125
|
-
if (!attr.type && !attr.size && !attr["size-eos"] && !attr.contents) {
|
2126
|
-
throw new ParseError(
|
2127
|
-
`Attribute "${attr.id || "unknown"}" must have type, size, size-eos, or contents`
|
2128
|
-
);
|
2129
|
-
}
|
2130
2056
|
if (attr.repeat) {
|
2131
2057
|
return this.parseRepeated(attr, context);
|
2132
2058
|
}
|
@@ -2176,13 +2102,7 @@ var TypeInterpreter = class _TypeInterpreter {
|
|
2176
2102
|
throw new ParseError("repeat-until expression is required");
|
2177
2103
|
}
|
2178
2104
|
let index = 0;
|
2179
|
-
const maxIterations = 1e6;
|
2180
2105
|
while (true) {
|
2181
|
-
if (index >= maxIterations) {
|
2182
|
-
throw new ParseError(
|
2183
|
-
`repeat-until exceeded maximum iterations (${maxIterations}). Possible infinite loop.`
|
2184
|
-
);
|
2185
|
-
}
|
2186
2106
|
context.set("_index", index);
|
2187
2107
|
const value = this.parseAttribute(
|
2188
2108
|
{ ...attr, repeat: void 0, "repeat-until": void 0 },
|
@@ -2257,11 +2177,6 @@ var TypeInterpreter = class _TypeInterpreter {
|
|
2257
2177
|
if (size < 0) {
|
2258
2178
|
throw new ParseError(`size must be non-negative, got ${size}`);
|
2259
2179
|
}
|
2260
|
-
if (stream.pos + size > stream.size) {
|
2261
|
-
throw new ParseError(
|
2262
|
-
`Not enough data: need ${size} bytes at position ${stream.pos}, but only ${stream.size - stream.pos} bytes available`
|
2263
|
-
);
|
2264
|
-
}
|
2265
2180
|
if (type === "str" || !type) {
|
2266
2181
|
const encoding = attr.encoding || this.schema.meta.encoding || "UTF-8";
|
2267
2182
|
let data;
|
@@ -2383,13 +2298,6 @@ var TypeInterpreter = class _TypeInterpreter {
|
|
2383
2298
|
const meta = this.schema.meta || this.parentMeta;
|
2384
2299
|
const metaEndian = meta?.endian;
|
2385
2300
|
const endian = typeEndian || (typeof metaEndian === "string" ? metaEndian : "le");
|
2386
|
-
if (type.startsWith("b") && type.length > 1) {
|
2387
|
-
const bits = parseInt(type.substring(1), 10);
|
2388
|
-
if (!isNaN(bits) && bits >= 1 && bits <= 64) {
|
2389
|
-
const value = endian === "be" ? stream.readBitsIntBe(bits) : stream.readBitsIntLe(bits);
|
2390
|
-
return bits <= 32 ? Number(value) : value;
|
2391
|
-
}
|
2392
|
-
}
|
2393
2301
|
if (isIntegerType(type)) {
|
2394
2302
|
return this.readInteger(base, endian, stream);
|
2395
2303
|
}
|
@@ -2485,21 +2393,8 @@ var TypeInterpreter = class _TypeInterpreter {
|
|
2485
2393
|
return value;
|
2486
2394
|
}
|
2487
2395
|
if (typeof value === "string") {
|
2488
|
-
if (!value.includes("_") && !value.includes(".")) {
|
2489
|
-
const cached = this.expressionCache.get(value);
|
2490
|
-
if (cached !== void 0) {
|
2491
|
-
return cached;
|
2492
|
-
}
|
2493
|
-
}
|
2494
2396
|
try {
|
2495
|
-
|
2496
|
-
if (!value.includes("_") && !value.includes(".")) {
|
2497
|
-
if (this.expressionCache.size >= this.MAX_CACHE_SIZE) {
|
2498
|
-
this.expressionCache.clear();
|
2499
|
-
}
|
2500
|
-
this.expressionCache.set(value, result);
|
2501
|
-
}
|
2502
|
-
return result;
|
2397
|
+
return evaluateExpression(value, context);
|
2503
2398
|
} catch (error) {
|
2504
2399
|
throw new ParseError(
|
2505
2400
|
`Failed to evaluate expression "${value}": ${error instanceof Error ? error.message : String(error)}`
|
@@ -2639,7 +2534,7 @@ export {
|
|
2639
2534
|
* @module kaitai-struct-ts
|
2640
2535
|
* @author Fabiano Pinto
|
2641
2536
|
* @license MIT
|
2642
|
-
* @version 0.
|
2537
|
+
* @version 0.2.0
|
2643
2538
|
*
|
2644
2539
|
* @description
|
2645
2540
|
* A runtime interpreter for Kaitai Struct binary format definitions in TypeScript.
|