@k67/kaitai-struct-ts 0.3.0 → 0.6.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/dist/index.d.ts CHANGED
@@ -1,199 +1,3 @@
1
- /**
2
- * @fileoverview Binary stream reader for Kaitai Struct
3
- * @module stream/KaitaiStream
4
- * @author Fabiano Pinto
5
- * @license MIT
6
- */
7
- /**
8
- * KaitaiStream provides methods for reading binary data with proper type handling
9
- * and endianness support. It's the core class for parsing binary formats.
10
- *
11
- * Supports reading:
12
- * - Unsigned integers (u1, u2, u4, u8) in both little and big endian
13
- * - Signed integers (s1, s2, s4, s8) in both little and big endian
14
- * - Floating point numbers (f4, f8) in both little and big endian
15
- * - Byte arrays (fixed length, until terminator, or all remaining)
16
- * - Strings with various encodings
17
- * - Bit-level data
18
- *
19
- * @class KaitaiStream
20
- * @example
21
- * ```typescript
22
- * const buffer = new Uint8Array([0x01, 0x02, 0x03, 0x04])
23
- * const stream = new KaitaiStream(buffer)
24
- *
25
- * const byte = stream.readU1() // Read 1 byte
26
- * const word = stream.readU2le() // Read 2 bytes little-endian
27
- * const str = stream.readStr(5, 'UTF-8') // Read 5-byte string
28
- * ```
29
- */
30
- declare class KaitaiStream {
31
- private buffer;
32
- private view;
33
- private _pos;
34
- private _bits;
35
- private _bitsLeft;
36
- /**
37
- * Create a new KaitaiStream from a buffer
38
- * @param buffer - ArrayBuffer or Uint8Array containing the binary data
39
- */
40
- constructor(buffer: ArrayBuffer | Uint8Array);
41
- /**
42
- * Current position in the stream
43
- */
44
- get pos(): number;
45
- set pos(value: number);
46
- /**
47
- * Total size of the stream in bytes
48
- */
49
- get size(): number;
50
- /**
51
- * Check if we've reached the end of the stream
52
- */
53
- isEof(): boolean;
54
- /**
55
- * Seek to a specific position in the stream
56
- * @param pos - Position to seek to
57
- */
58
- seek(pos: number): void;
59
- /**
60
- * Ensure we have enough bytes available
61
- * @param count - Number of bytes needed
62
- */
63
- private ensureBytes;
64
- /**
65
- * Read 1-byte unsigned integer (0 to 255)
66
- */
67
- readU1(): number;
68
- /**
69
- * Read 2-byte unsigned integer, little-endian
70
- */
71
- readU2le(): number;
72
- /**
73
- * Read 2-byte unsigned integer, big-endian
74
- */
75
- readU2be(): number;
76
- /**
77
- * Read 4-byte unsigned integer, little-endian
78
- */
79
- readU4le(): number;
80
- /**
81
- * Read 4-byte unsigned integer, big-endian
82
- */
83
- readU4be(): number;
84
- /**
85
- * Read 8-byte unsigned integer, little-endian
86
- * Returns BigInt for values > Number.MAX_SAFE_INTEGER
87
- */
88
- readU8le(): bigint;
89
- /**
90
- * Read 8-byte unsigned integer, big-endian
91
- * Returns BigInt for values > Number.MAX_SAFE_INTEGER
92
- */
93
- readU8be(): bigint;
94
- /**
95
- * Read 1-byte signed integer (-128 to 127)
96
- */
97
- readS1(): number;
98
- /**
99
- * Read 2-byte signed integer, little-endian
100
- */
101
- readS2le(): number;
102
- /**
103
- * Read 2-byte signed integer, big-endian
104
- */
105
- readS2be(): number;
106
- /**
107
- * Read 4-byte signed integer, little-endian
108
- */
109
- readS4le(): number;
110
- /**
111
- * Read 4-byte signed integer, big-endian
112
- */
113
- readS4be(): number;
114
- /**
115
- * Read 8-byte signed integer, little-endian
116
- * Returns BigInt for values outside Number.MAX_SAFE_INTEGER range
117
- */
118
- readS8le(): bigint;
119
- /**
120
- * Read 8-byte signed integer, big-endian
121
- * Returns BigInt for values outside Number.MAX_SAFE_INTEGER range
122
- */
123
- readS8be(): bigint;
124
- /**
125
- * Read 4-byte IEEE 754 single-precision float, little-endian
126
- */
127
- readF4le(): number;
128
- /**
129
- * Read 4-byte IEEE 754 single-precision float, big-endian
130
- */
131
- readF4be(): number;
132
- /**
133
- * Read 8-byte IEEE 754 double-precision float, little-endian
134
- */
135
- readF8le(): number;
136
- /**
137
- * Read 8-byte IEEE 754 double-precision float, big-endian
138
- */
139
- readF8be(): number;
140
- /**
141
- * Read a fixed number of bytes
142
- * @param length - Number of bytes to read
143
- */
144
- readBytes(length: number): Uint8Array;
145
- /**
146
- * Read all remaining bytes until end of stream
147
- */
148
- readBytesFull(): Uint8Array;
149
- /**
150
- * Read bytes until a terminator byte is found
151
- * @param term - Terminator byte value
152
- * @param include - Include terminator in result
153
- * @param consume - Consume terminator from stream
154
- * @param eosError - Throw error if EOS reached before terminator
155
- */
156
- readBytesterm(term: number, include?: boolean, consume?: boolean, eosError?: boolean): Uint8Array;
157
- /**
158
- * Read a fixed-length string
159
- * @param length - Number of bytes to read
160
- * @param encoding - Character encoding (default: UTF-8)
161
- */
162
- readStr(length: number, encoding?: string): string;
163
- /**
164
- * Read a null-terminated string
165
- * @param encoding - Character encoding (default: UTF-8)
166
- * @param term - Terminator byte (default: 0)
167
- * @param include - Include terminator in result
168
- * @param consume - Consume terminator from stream
169
- * @param eosError - Throw error if EOS reached before terminator
170
- */
171
- readStrz(encoding?: string, term?: number, include?: boolean, consume?: boolean, eosError?: boolean): string;
172
- /**
173
- * Align bit reading to byte boundary
174
- */
175
- alignToByte(): void;
176
- /**
177
- * Read specified number of bits as unsigned integer (big-endian)
178
- * @param n - Number of bits to read (1-64)
179
- */
180
- readBitsIntBe(n: number): bigint;
181
- /**
182
- * Read specified number of bits as unsigned integer (little-endian)
183
- * @param n - Number of bits to read (1-64)
184
- */
185
- readBitsIntLe(n: number): bigint;
186
- /**
187
- * Get the underlying buffer
188
- */
189
- getBuffer(): Uint8Array;
190
- /**
191
- * Create a substream from current position with specified size
192
- * @param size - Size of the substream in bytes
193
- */
194
- substream(size: number): KaitaiStream;
195
- }
196
-
197
1
  /**
198
2
  * @fileoverview Type definitions for Kaitai Struct YAML schema (.ksy files)
199
3
  * @module parser/schema
@@ -299,6 +103,8 @@ interface AttributeSpec {
299
103
  id?: string;
300
104
  /** Data type to read */
301
105
  type?: string | SwitchType;
106
+ /** Arguments for parametric types */
107
+ 'type-args'?: Array<string | number | boolean>;
302
108
  /** Size of the field (in bytes or expression) */
303
109
  size?: number | string;
304
110
  /** Size until specific byte value */
@@ -503,6 +309,203 @@ declare function isFloatType(type: string): boolean;
503
309
  */
504
310
  declare function isStringType(type: string): boolean;
505
311
 
312
+ /**
313
+ * @fileoverview Binary stream reader for Kaitai Struct
314
+ * @module stream/KaitaiStream
315
+ * @author Fabiano Pinto
316
+ * @license MIT
317
+ */
318
+ /**
319
+ * KaitaiStream provides methods for reading binary data with proper type handling
320
+ * and endianness support. It's the core class for parsing binary formats.
321
+ *
322
+ * Supports reading:
323
+ * - Unsigned integers (u1, u2, u4, u8) in both little and big endian
324
+ * - Signed integers (s1, s2, s4, s8) in both little and big endian
325
+ * - Floating point numbers (f4, f8) in both little and big endian
326
+ * - Byte arrays (fixed length, until terminator, or all remaining)
327
+ * - Strings with various encodings
328
+ * - Bit-level data
329
+ *
330
+ * @class KaitaiStream
331
+ * @example
332
+ * ```typescript
333
+ * const buffer = new Uint8Array([0x01, 0x02, 0x03, 0x04])
334
+ * const stream = new KaitaiStream(buffer)
335
+ *
336
+ * const byte = stream.readU1() // Read 1 byte
337
+ * const word = stream.readU2le() // Read 2 bytes little-endian
338
+ * const str = stream.readStr(5, 'UTF-8') // Read 5-byte string
339
+ * ```
340
+ */
341
+ declare class KaitaiStream {
342
+ private buffer;
343
+ private view;
344
+ private _pos;
345
+ private _bits;
346
+ private _bitsLeft;
347
+ private _size;
348
+ /**
349
+ * Create a new KaitaiStream from a buffer
350
+ * @param buffer - ArrayBuffer or Uint8Array containing the binary data
351
+ */
352
+ constructor(buffer: ArrayBuffer | Uint8Array);
353
+ /**
354
+ * Current position in the stream
355
+ */
356
+ get pos(): number;
357
+ set pos(value: number);
358
+ /**
359
+ * Total size of the stream in bytes
360
+ */
361
+ get size(): number;
362
+ /**
363
+ * Check if we've reached the end of the stream
364
+ */
365
+ isEof(): boolean;
366
+ /**
367
+ * Seek to a specific position in the stream
368
+ * @param pos - Position to seek to
369
+ */
370
+ seek(pos: number): void;
371
+ /**
372
+ * Ensure we have enough bytes available
373
+ * @param count - Number of bytes needed
374
+ */
375
+ private ensureBytes;
376
+ /**
377
+ * Read 1-byte unsigned integer (0 to 255)
378
+ */
379
+ readU1(): number;
380
+ /**
381
+ * Read 2-byte unsigned integer, little-endian
382
+ */
383
+ readU2le(): number;
384
+ /**
385
+ * Read 2-byte unsigned integer, big-endian
386
+ */
387
+ readU2be(): number;
388
+ /**
389
+ * Read 4-byte unsigned integer, little-endian
390
+ */
391
+ readU4le(): number;
392
+ /**
393
+ * Read 4-byte unsigned integer, big-endian
394
+ */
395
+ readU4be(): number;
396
+ /**
397
+ * Read 8-byte unsigned integer, little-endian
398
+ * Returns BigInt for values > Number.MAX_SAFE_INTEGER
399
+ */
400
+ readU8le(): bigint;
401
+ /**
402
+ * Read 8-byte unsigned integer, big-endian
403
+ * Returns BigInt for values > Number.MAX_SAFE_INTEGER
404
+ */
405
+ readU8be(): bigint;
406
+ /**
407
+ * Read 1-byte signed integer (-128 to 127)
408
+ */
409
+ readS1(): number;
410
+ /**
411
+ * Read 2-byte signed integer, little-endian
412
+ */
413
+ readS2le(): number;
414
+ /**
415
+ * Read 2-byte signed integer, big-endian
416
+ */
417
+ readS2be(): number;
418
+ /**
419
+ * Read 4-byte signed integer, little-endian
420
+ */
421
+ readS4le(): number;
422
+ /**
423
+ * Read 4-byte signed integer, big-endian
424
+ */
425
+ readS4be(): number;
426
+ /**
427
+ * Read 8-byte signed integer, little-endian
428
+ * Returns BigInt for values outside Number.MAX_SAFE_INTEGER range
429
+ */
430
+ readS8le(): bigint;
431
+ /**
432
+ * Read 8-byte signed integer, big-endian
433
+ * Returns BigInt for values outside Number.MAX_SAFE_INTEGER range
434
+ */
435
+ readS8be(): bigint;
436
+ /**
437
+ * Read 4-byte IEEE 754 single-precision float, little-endian
438
+ */
439
+ readF4le(): number;
440
+ /**
441
+ * Read 4-byte IEEE 754 single-precision float, big-endian
442
+ */
443
+ readF4be(): number;
444
+ /**
445
+ * Read 8-byte IEEE 754 double-precision float, little-endian
446
+ */
447
+ readF8le(): number;
448
+ /**
449
+ * Read 8-byte IEEE 754 double-precision float, big-endian
450
+ */
451
+ readF8be(): number;
452
+ /**
453
+ * Read a fixed number of bytes
454
+ * @param length - Number of bytes to read
455
+ */
456
+ readBytes(length: number): Uint8Array;
457
+ /**
458
+ * Read all remaining bytes until end of stream
459
+ */
460
+ readBytesFull(): Uint8Array;
461
+ /**
462
+ * Read bytes until a terminator byte is found
463
+ * @param term - Terminator byte value
464
+ * @param include - Include terminator in result
465
+ * @param consume - Consume terminator from stream
466
+ * @param eosError - Throw error if EOS reached before terminator
467
+ */
468
+ readBytesterm(term: number, include?: boolean, consume?: boolean, eosError?: boolean): Uint8Array;
469
+ /**
470
+ * Read a fixed-length string
471
+ * @param length - Number of bytes to read
472
+ * @param encoding - Character encoding (default: UTF-8)
473
+ */
474
+ readStr(length: number, encoding?: string): string;
475
+ /**
476
+ * Read a null-terminated string
477
+ * @param encoding - Character encoding (default: UTF-8)
478
+ * @param term - Terminator byte (default: 0)
479
+ * @param include - Include terminator in result
480
+ * @param consume - Consume terminator from stream
481
+ * @param eosError - Throw error if EOS reached before terminator
482
+ */
483
+ readStrz(encoding?: string, term?: number, include?: boolean, consume?: boolean, eosError?: boolean): string;
484
+ /**
485
+ * Align bit reading to byte boundary
486
+ */
487
+ alignToByte(): void;
488
+ /**
489
+ * Read specified number of bits as unsigned integer (big-endian)
490
+ * @param n - Number of bits to read (1-64)
491
+ */
492
+ readBitsIntBe(n: number): bigint;
493
+ /**
494
+ * Read specified number of bits as unsigned integer (little-endian)
495
+ * @param n - Number of bits to read (1-64)
496
+ */
497
+ readBitsIntLe(n: number): bigint;
498
+ /**
499
+ * Get the underlying buffer
500
+ */
501
+ getBuffer(): Uint8Array;
502
+ /**
503
+ * Create a substream from current position with specified size
504
+ * @param size - Size of the substream in bytes
505
+ */
506
+ substream(size: number): KaitaiStream;
507
+ }
508
+
506
509
  /**
507
510
  * @fileoverview Parser for Kaitai Struct YAML (.ksy) files
508
511
  * @module parser/KsyParser
@@ -532,6 +535,22 @@ declare class KsyParser {
532
535
  * @throws {ValidationError} If schema validation fails
533
536
  */
534
537
  parse(yaml: string, options?: ParseOptions$1): KsySchema;
538
+ /**
539
+ * Process parametric type syntax in schema.
540
+ * Converts "type_name(arg1, arg2)" to structured format.
541
+ *
542
+ * @param schema - Schema to process
543
+ * @private
544
+ */
545
+ private processParametricTypes;
546
+ /**
547
+ * Parse parametric type syntax from a type string.
548
+ * Converts "type_name(arg1, arg2)" to type + type-args.
549
+ *
550
+ * @param attr - Attribute to process
551
+ * @private
552
+ */
553
+ private parseParametricType;
535
554
  /**
536
555
  * Validate a schema object.
537
556
  *
@@ -740,6 +759,8 @@ declare class Context {
740
759
  declare class TypeInterpreter {
741
760
  private schema;
742
761
  private parentMeta?;
762
+ private expressionCache;
763
+ private readonly MAX_CACHE_SIZE;
743
764
  /**
744
765
  * Create a new type interpreter.
745
766
  *
@@ -756,9 +777,30 @@ declare class TypeInterpreter {
756
777
  *
757
778
  * @param stream - Binary stream to parse
758
779
  * @param parent - Parent object (for nested types)
780
+ * @param typeArgs - Arguments for parametric types
759
781
  * @returns Parsed object
760
782
  */
761
- parse(stream: KaitaiStream, parent?: unknown): Record<string, unknown>;
783
+ parse(stream: KaitaiStream, parent?: unknown, typeArgs?: Array<string | number | boolean>): Record<string, unknown>;
784
+ /**
785
+ * Set up lazy-evaluated instance getters.
786
+ * Instances are computed on first access and cached.
787
+ *
788
+ * @param result - Result object to add getters to
789
+ * @param stream - Stream for parsing
790
+ * @param context - Execution context
791
+ * @private
792
+ */
793
+ private setupInstances;
794
+ /**
795
+ * Parse an instance (lazy-evaluated field).
796
+ *
797
+ * @param instance - Instance specification
798
+ * @param stream - Stream to read from
799
+ * @param context - Execution context
800
+ * @returns Parsed or calculated value
801
+ * @private
802
+ */
803
+ private parseInstance;
762
804
  /**
763
805
  * Parse a single attribute according to its specification.
764
806
  *
@@ -801,10 +843,21 @@ declare class TypeInterpreter {
801
843
  * @param type - Type name or switch specification
802
844
  * @param stream - Stream to read from
803
845
  * @param context - Execution context
846
+ * @param typeArgs - Arguments for parametric types
804
847
  * @returns Parsed value
805
848
  * @private
806
849
  */
807
850
  private parseType;
851
+ /**
852
+ * Parse a switch type (type selection based on expression).
853
+ *
854
+ * @param switchType - Switch type specification
855
+ * @param stream - Stream to read from
856
+ * @param context - Execution context
857
+ * @returns Parsed value
858
+ * @private
859
+ */
860
+ private parseSwitchType;
808
861
  /**
809
862
  * Parse a built-in type.
810
863
  *
@@ -836,11 +889,19 @@ declare class TypeInterpreter {
836
889
  */
837
890
  private readFloat;
838
891
  /**
839
- * Evaluate an expression or return a literal value.
840
- * If the value is a string, it's treated as an expression.
841
- * If it's a number or boolean, it's returned as-is.
892
+ * Apply processing transformation to data.
893
+ * Supports basic transformations like zlib decompression.
842
894
  *
843
- * @param value - Expression string or literal value
895
+ * @param data - Data to process
896
+ * @param process - Processing specification
897
+ * @returns Processed data
898
+ * @private
899
+ */
900
+ private applyProcessing;
901
+ /**
902
+ * Evaluate a value that can be an expression or literal.
903
+ *
904
+ * @param value - Value to evaluate (expression string, number, or boolean)
844
905
  * @param context - Execution context
845
906
  * @returns Evaluated result
846
907
  * @private
@@ -931,7 +992,7 @@ declare class NotImplementedError extends KaitaiError {
931
992
  * @module kaitai-struct-ts
932
993
  * @author Fabiano Pinto
933
994
  * @license MIT
934
- * @version 0.2.0
995
+ * @version 0.6.0
935
996
  *
936
997
  * @description
937
998
  * A runtime interpreter for Kaitai Struct binary format definitions in TypeScript.
@@ -987,6 +1048,19 @@ interface ParseOptions {
987
1048
  validate?: boolean;
988
1049
  /** Whether to treat warnings as errors (default: false) */
989
1050
  strict?: boolean;
1051
+ /**
1052
+ * Imported schemas for type imports.
1053
+ * Map of schema IDs to their parsed schemas.
1054
+ * @example
1055
+ * ```typescript
1056
+ * const imports = {
1057
+ * 'common_types': parsedCommonSchema,
1058
+ * 'header': parsedHeaderSchema
1059
+ * }
1060
+ * parse(ksyYaml, buffer, { imports })
1061
+ * ```
1062
+ */
1063
+ imports?: Record<string, KsySchema>;
990
1064
  }
991
1065
 
992
1066
  export { type AttributeSpec, BUILTIN_TYPES, Context, EOFError, type EndianExpression, type Endianness, type EnumSpec, type InstanceSpec, KaitaiError, KaitaiStream, KsyParser, type KsySchema, type MetaSpec, NotImplementedError, type ParamSpec, ParseError, type ParseOptions, type ProcessObject, type ProcessSpec, type RepeatSpec, type ValidationError$1 as SchemaValidationError, type SwitchType, TypeInterpreter, ValidationError, type ValidationResult, type ValidationWarning, getBaseType, getTypeEndianness, isBuiltinType, isFloatType, isIntegerType, isStringType, parse };