@k67/kaitai-struct-ts 0.2.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.
@@ -0,0 +1,962 @@
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
+ /**
198
+ * @fileoverview Type definitions for Kaitai Struct YAML schema (.ksy files)
199
+ * @module parser/schema
200
+ * @author Fabiano Pinto
201
+ * @license MIT
202
+ */
203
+ /**
204
+ * Root schema definition for a Kaitai Struct format.
205
+ * Represents a complete .ksy file structure.
206
+ *
207
+ * @interface KsySchema
208
+ * @example
209
+ * ```typescript
210
+ * const schema: KsySchema = {
211
+ * meta: {
212
+ * id: 'my_format',
213
+ * endian: 'le'
214
+ * },
215
+ * seq: [
216
+ * { id: 'magic', contents: [0x4D, 0x5A] },
217
+ * { id: 'version', type: 'u2' }
218
+ * ]
219
+ * }
220
+ * ```
221
+ */
222
+ interface KsySchema {
223
+ /** Metadata about the format */
224
+ meta: MetaSpec;
225
+ /** Sequential attributes (fields read in order) */
226
+ seq?: AttributeSpec[];
227
+ /** Named instances (lazy-evaluated fields) */
228
+ instances?: Record<string, InstanceSpec>;
229
+ /** Nested type definitions */
230
+ types?: Record<string, KsySchema>;
231
+ /** Enum definitions (named integer constants) */
232
+ enums?: Record<string, EnumSpec>;
233
+ /** Documentation for this type */
234
+ doc?: string;
235
+ /** Reference to documentation */
236
+ 'doc-ref'?: string | string[];
237
+ /** Parameters for parametric types */
238
+ params?: ParamSpec[];
239
+ }
240
+ /**
241
+ * Metadata section of a Kaitai Struct schema.
242
+ * Contains information about the format itself.
243
+ *
244
+ * @interface MetaSpec
245
+ */
246
+ interface MetaSpec {
247
+ /** Identifier for this format (required) */
248
+ id: string;
249
+ /** Title/name of the format */
250
+ title?: string;
251
+ /** Application that uses this format */
252
+ application?: string | string[];
253
+ /** File extension(s) for this format */
254
+ 'file-extension'?: string | string[];
255
+ /** MIME type(s) for this format */
256
+ xref?: Record<string, string | string[]>;
257
+ /** License for the format specification */
258
+ license?: string;
259
+ /** KS compatibility version */
260
+ 'ks-version'?: string | number;
261
+ /** Debug mode flag */
262
+ 'ks-debug'?: boolean;
263
+ /** Opaque types flag */
264
+ 'ks-opaque-types'?: boolean;
265
+ /** Default endianness for the format */
266
+ endian?: Endianness | EndianExpression;
267
+ /** Default encoding for strings */
268
+ encoding?: string;
269
+ /** Imported type definitions */
270
+ imports?: string[];
271
+ /** Documentation */
272
+ doc?: string;
273
+ /** Reference to documentation */
274
+ 'doc-ref'?: string | string[];
275
+ }
276
+ /**
277
+ * Endianness specification.
278
+ */
279
+ type Endianness = 'le' | 'be';
280
+ /**
281
+ * Expression-based endianness (switch on expression).
282
+ *
283
+ * @interface EndianExpression
284
+ */
285
+ interface EndianExpression {
286
+ /** Expression to evaluate */
287
+ 'switch-on': string;
288
+ /** Cases mapping values to endianness */
289
+ cases: Record<string, Endianness>;
290
+ }
291
+ /**
292
+ * Attribute specification for sequential fields.
293
+ * Describes how to read a single field from the stream.
294
+ *
295
+ * @interface AttributeSpec
296
+ */
297
+ interface AttributeSpec {
298
+ /** Field identifier (name) */
299
+ id?: string;
300
+ /** Data type to read */
301
+ type?: string | SwitchType;
302
+ /** Size of the field (in bytes or expression) */
303
+ size?: number | string;
304
+ /** Size until specific byte value */
305
+ 'size-eos'?: boolean;
306
+ /** Repeat specification */
307
+ repeat?: RepeatSpec;
308
+ /** Number of repetitions (for repeat: expr) */
309
+ 'repeat-expr'?: string | number;
310
+ /** Condition for repetition (for repeat: until) */
311
+ 'repeat-until'?: string;
312
+ /** Conditional parsing */
313
+ if?: string;
314
+ /** Expected contents (validation) */
315
+ contents?: number[] | string;
316
+ /** String encoding */
317
+ encoding?: string;
318
+ /** Pad right to alignment */
319
+ 'pad-right'?: number;
320
+ /** Terminator byte for strings/arrays */
321
+ terminator?: number;
322
+ /** Include terminator in result */
323
+ include?: boolean;
324
+ /** Consume terminator from stream */
325
+ consume?: boolean;
326
+ /** Throw error if terminator not found */
327
+ 'eos-error'?: boolean;
328
+ /** Enum type name */
329
+ enum?: string;
330
+ /** Absolute position to seek to */
331
+ pos?: number | string;
332
+ /** Custom I/O stream */
333
+ io?: string;
334
+ /** Processing directive (compression, encryption, etc.) */
335
+ process?: string | ProcessSpec;
336
+ /** Documentation */
337
+ doc?: string;
338
+ /** Reference to documentation */
339
+ 'doc-ref'?: string | string[];
340
+ /** Original field ID (for reference) */
341
+ '-orig-id'?: string;
342
+ }
343
+ /**
344
+ * Instance specification for lazy-evaluated fields.
345
+ * Similar to AttributeSpec but for instances section.
346
+ *
347
+ * @interface InstanceSpec
348
+ */
349
+ interface InstanceSpec extends Omit<AttributeSpec, 'id'> {
350
+ /** Value instance (calculated field) */
351
+ value?: string | number | boolean;
352
+ /** Position in stream (for pos instances) */
353
+ pos?: number | string;
354
+ /** Custom I/O stream */
355
+ io?: string;
356
+ }
357
+ /**
358
+ * Repeat specification types.
359
+ */
360
+ type RepeatSpec = 'expr' | 'eos' | 'until';
361
+ /**
362
+ * Switch-based type selection.
363
+ * Allows different types based on an expression value.
364
+ *
365
+ * @interface SwitchType
366
+ */
367
+ interface SwitchType {
368
+ /** Expression to evaluate for switching */
369
+ 'switch-on': string;
370
+ /** Cases mapping values to types */
371
+ cases: Record<string, string>;
372
+ /** Default type if no case matches */
373
+ default?: string;
374
+ }
375
+ /**
376
+ * Processing specification for data transformation.
377
+ *
378
+ * @type ProcessSpec
379
+ */
380
+ type ProcessSpec = string | ProcessObject;
381
+ /**
382
+ * Processing object with algorithm and parameters.
383
+ *
384
+ * @interface ProcessObject
385
+ */
386
+ interface ProcessObject {
387
+ /** Processing algorithm */
388
+ algorithm: string;
389
+ /** Algorithm parameters */
390
+ [key: string]: unknown;
391
+ }
392
+ /**
393
+ * Enum specification (named integer constants).
394
+ *
395
+ * @type EnumSpec
396
+ */
397
+ type EnumSpec = Record<string | number, string | number>;
398
+ /**
399
+ * Parameter specification for parametric types.
400
+ *
401
+ * @interface ParamSpec
402
+ */
403
+ interface ParamSpec {
404
+ /** Parameter identifier */
405
+ id: string;
406
+ /** Parameter type */
407
+ type?: string;
408
+ /** Documentation */
409
+ doc?: string;
410
+ /** Reference to documentation */
411
+ 'doc-ref'?: string | string[];
412
+ }
413
+ /**
414
+ * Validation result for schema validation.
415
+ *
416
+ * @interface ValidationResult
417
+ */
418
+ interface ValidationResult {
419
+ /** Whether the schema is valid */
420
+ valid: boolean;
421
+ /** Validation errors */
422
+ errors: ValidationError$1[];
423
+ /** Validation warnings */
424
+ warnings: ValidationWarning[];
425
+ }
426
+ /**
427
+ * Validation error.
428
+ *
429
+ * @interface ValidationError
430
+ */
431
+ interface ValidationError$1 {
432
+ /** Error message */
433
+ message: string;
434
+ /** Path to the error in the schema */
435
+ path: string[];
436
+ /** Error code */
437
+ code: string;
438
+ }
439
+ /**
440
+ * Validation warning.
441
+ *
442
+ * @interface ValidationWarning
443
+ */
444
+ interface ValidationWarning {
445
+ /** Warning message */
446
+ message: string;
447
+ /** Path to the warning in the schema */
448
+ path: string[];
449
+ /** Warning code */
450
+ code: string;
451
+ }
452
+ /**
453
+ * Built-in Kaitai Struct types.
454
+ */
455
+ declare const BUILTIN_TYPES: readonly ["u1", "u2", "u2le", "u2be", "u4", "u4le", "u4be", "u8", "u8le", "u8be", "s1", "s2", "s2le", "s2be", "s4", "s4le", "s4be", "s8", "s8le", "s8be", "f4", "f4le", "f4be", "f8", "f8le", "f8be", "str", "strz"];
456
+ /**
457
+ * Type guard to check if a type is a built-in type.
458
+ *
459
+ * @param type - Type name to check
460
+ * @returns True if the type is built-in
461
+ */
462
+ declare function isBuiltinType(type: string): boolean;
463
+ /**
464
+ * Get the default endianness for a type.
465
+ * Returns the endianness suffix if present, otherwise undefined.
466
+ *
467
+ * @param type - Type name
468
+ * @returns Endianness ('le', 'be', or undefined for default)
469
+ */
470
+ declare function getTypeEndianness(type: string): Endianness | undefined;
471
+ /**
472
+ * Get the base type without endianness suffix.
473
+ *
474
+ * @param type - Type name
475
+ * @returns Base type name
476
+ * @example
477
+ * ```typescript
478
+ * getBaseType('u4le') // returns 'u4'
479
+ * getBaseType('s2be') // returns 's2'
480
+ * getBaseType('str') // returns 'str'
481
+ * ```
482
+ */
483
+ declare function getBaseType(type: string): string;
484
+ /**
485
+ * Check if a type is an integer type.
486
+ *
487
+ * @param type - Type name
488
+ * @returns True if the type is an integer
489
+ */
490
+ declare function isIntegerType(type: string): boolean;
491
+ /**
492
+ * Check if a type is a floating point type.
493
+ *
494
+ * @param type - Type name
495
+ * @returns True if the type is a float
496
+ */
497
+ declare function isFloatType(type: string): boolean;
498
+ /**
499
+ * Check if a type is a string type.
500
+ *
501
+ * @param type - Type name
502
+ * @returns True if the type is a string
503
+ */
504
+ declare function isStringType(type: string): boolean;
505
+
506
+ /**
507
+ * @fileoverview Parser for Kaitai Struct YAML (.ksy) files
508
+ * @module parser/KsyParser
509
+ * @author Fabiano Pinto
510
+ * @license MIT
511
+ */
512
+
513
+ /**
514
+ * Parser for Kaitai Struct YAML (.ksy) format definitions.
515
+ * Converts YAML text into typed schema objects and validates them.
516
+ *
517
+ * @class KsyParser
518
+ * @example
519
+ * ```typescript
520
+ * const parser = new KsyParser()
521
+ * const schema = parser.parse(ksyYamlString)
522
+ * ```
523
+ */
524
+ declare class KsyParser {
525
+ /**
526
+ * Parse a .ksy YAML string into a typed schema object.
527
+ *
528
+ * @param yaml - YAML string containing the .ksy definition
529
+ * @param options - Parsing options
530
+ * @returns Parsed and validated schema
531
+ * @throws {ParseError} If YAML parsing fails
532
+ * @throws {ValidationError} If schema validation fails
533
+ */
534
+ parse(yaml: string, options?: ParseOptions$1): KsySchema;
535
+ /**
536
+ * Validate a schema object.
537
+ *
538
+ * @param schema - Schema to validate
539
+ * @param options - Validation options
540
+ * @returns Validation result with errors and warnings
541
+ */
542
+ validate(schema: KsySchema, options?: ValidationOptions): ValidationResult;
543
+ /**
544
+ * Validate an attribute specification.
545
+ *
546
+ * @param attr - Attribute to validate
547
+ * @param path - Path to this attribute in the schema
548
+ * @param errors - Array to collect errors
549
+ * @param warnings - Array to collect warnings
550
+ * @param strict - Whether to be strict about warnings
551
+ * @private
552
+ */
553
+ private validateAttribute;
554
+ /**
555
+ * Parse multiple .ksy files and resolve imports.
556
+ *
557
+ * @param mainYaml - Main .ksy file content
558
+ * @param imports - Map of import names to their YAML content
559
+ * @param options - Parsing options
560
+ * @returns Parsed schema with resolved imports
561
+ */
562
+ parseWithImports(mainYaml: string, _imports: Map<string, string>, options?: ParseOptions$1): KsySchema;
563
+ }
564
+ /**
565
+ * Options for parsing .ksy files.
566
+ *
567
+ * @interface ParseOptions
568
+ */
569
+ interface ParseOptions$1 {
570
+ /** Whether to validate the schema (default: true) */
571
+ validate?: boolean;
572
+ /** Whether to treat warnings as errors (default: false) */
573
+ strict?: boolean;
574
+ }
575
+ /**
576
+ * Options for schema validation.
577
+ *
578
+ * @interface ValidationOptions
579
+ */
580
+ interface ValidationOptions {
581
+ /** Whether to treat warnings as errors (default: false) */
582
+ strict?: boolean;
583
+ /** Whether this is a nested type (meta section optional) (default: false) */
584
+ isNested?: boolean;
585
+ }
586
+
587
+ /**
588
+ * @fileoverview Execution context for Kaitai Struct parsing
589
+ * @module interpreter/Context
590
+ * @author Fabiano Pinto
591
+ * @license MIT
592
+ */
593
+
594
+ /**
595
+ * Execution context for parsing operations.
596
+ * Provides access to the current object, parent, root, and stream.
597
+ *
598
+ * @class Context
599
+ * @example
600
+ * ```typescript
601
+ * const context = new Context(stream, root)
602
+ * context.pushParent(currentObject)
603
+ * const value = evaluator.evaluate(expression, context)
604
+ * context.popParent()
605
+ * ```
606
+ */
607
+ declare class Context {
608
+ private _io;
609
+ private _root;
610
+ /** Stack of parent objects */
611
+ private parentStack;
612
+ /** Current object being parsed */
613
+ private _current;
614
+ /**
615
+ * Create a new execution context.
616
+ *
617
+ * @param _io - Binary stream being read
618
+ * @param _root - Root object of the parse tree
619
+ * @param _parent - Parent object (optional)
620
+ */
621
+ constructor(_io: KaitaiStream, _root?: unknown, _parent?: unknown);
622
+ /**
623
+ * Get the current I/O stream.
624
+ * Accessible in expressions as `_io`.
625
+ *
626
+ * @returns Current stream
627
+ */
628
+ get io(): KaitaiStream;
629
+ /**
630
+ * Get the root object.
631
+ * Accessible in expressions as `_root`.
632
+ *
633
+ * @returns Root object
634
+ */
635
+ get root(): unknown;
636
+ /**
637
+ * Get the parent object.
638
+ * Accessible in expressions as `_parent`.
639
+ *
640
+ * @returns Parent object or null if at root
641
+ */
642
+ get parent(): unknown;
643
+ /**
644
+ * Get the current object being parsed.
645
+ * Used to access fields defined earlier in the sequence.
646
+ *
647
+ * @returns Current object
648
+ */
649
+ get current(): Record<string, unknown>;
650
+ /**
651
+ * Set the current object.
652
+ *
653
+ * @param obj - Object to set as current
654
+ */
655
+ set current(obj: Record<string, unknown>);
656
+ /**
657
+ * Push a new parent onto the stack.
658
+ * Used when entering a nested type.
659
+ *
660
+ * @param parent - Parent object to push
661
+ */
662
+ pushParent(parent: unknown): void;
663
+ /**
664
+ * Pop the current parent from the stack.
665
+ * Used when exiting a nested type.
666
+ *
667
+ * @returns The popped parent object
668
+ */
669
+ popParent(): unknown;
670
+ /**
671
+ * Get a value from the context by path.
672
+ * Supports special names: _io, _root, _parent, _index.
673
+ *
674
+ * @param name - Name or path to resolve
675
+ * @returns Resolved value
676
+ */
677
+ resolve(name: string): unknown;
678
+ /**
679
+ * Set a value in the current object.
680
+ *
681
+ * @param name - Field name
682
+ * @param value - Value to set
683
+ */
684
+ set(name: string, value: unknown): void;
685
+ /**
686
+ * Create a child context for nested parsing.
687
+ * The current object becomes the parent in the child context.
688
+ *
689
+ * @param stream - Stream for the child context (defaults to current stream)
690
+ * @returns New child context
691
+ */
692
+ createChild(stream?: KaitaiStream): Context;
693
+ /**
694
+ * Clone this context.
695
+ * Creates a shallow copy with the same stream, root, and parent.
696
+ *
697
+ * @returns Cloned context
698
+ */
699
+ clone(): Context;
700
+ }
701
+
702
+ /**
703
+ * @fileoverview Type interpreter for executing Kaitai Struct schemas
704
+ * @module interpreter/TypeInterpreter
705
+ * @author Fabiano Pinto
706
+ * @license MIT
707
+ */
708
+
709
+ /**
710
+ * Interprets Kaitai Struct schemas and parses binary data.
711
+ * Executes schema definitions against binary streams to produce structured objects.
712
+ *
713
+ * @class TypeInterpreter
714
+ * @example
715
+ * ```typescript
716
+ * const interpreter = new TypeInterpreter(schema)
717
+ * const stream = new KaitaiStream(buffer)
718
+ * const result = interpreter.parse(stream)
719
+ * ```
720
+ */
721
+ declare class TypeInterpreter {
722
+ private schema;
723
+ private parentMeta?;
724
+ /**
725
+ * Create a new type interpreter.
726
+ *
727
+ * @param schema - Kaitai Struct schema to interpret
728
+ * @param parentMeta - Parent schema's meta (for nested types)
729
+ */
730
+ constructor(schema: KsySchema, parentMeta?: {
731
+ id: string;
732
+ endian?: Endianness | object;
733
+ encoding?: string;
734
+ } | undefined);
735
+ /**
736
+ * Parse binary data according to the schema.
737
+ *
738
+ * @param stream - Binary stream to parse
739
+ * @param parent - Parent object (for nested types)
740
+ * @returns Parsed object
741
+ */
742
+ parse(stream: KaitaiStream, parent?: unknown): Record<string, unknown>;
743
+ /**
744
+ * Parse a single attribute according to its specification.
745
+ *
746
+ * @param attr - Attribute specification
747
+ * @param context - Execution context
748
+ * @returns Parsed value
749
+ * @private
750
+ */
751
+ private parseAttribute;
752
+ /**
753
+ * Parse a repeated attribute.
754
+ *
755
+ * @param attr - Attribute specification with repeat
756
+ * @param context - Execution context
757
+ * @returns Array of parsed values
758
+ * @private
759
+ */
760
+ private parseRepeated;
761
+ /**
762
+ * Parse and validate contents.
763
+ *
764
+ * @param attr - Attribute specification with contents
765
+ * @param context - Execution context
766
+ * @returns The validated contents
767
+ * @private
768
+ */
769
+ private parseContents;
770
+ /**
771
+ * Parse a single value according to its type.
772
+ *
773
+ * @param attr - Attribute specification
774
+ * @param context - Execution context
775
+ * @returns Parsed value
776
+ * @private
777
+ */
778
+ private parseValue;
779
+ /**
780
+ * Parse a value of a specific type.
781
+ *
782
+ * @param type - Type name or switch specification
783
+ * @param stream - Stream to read from
784
+ * @param context - Execution context
785
+ * @returns Parsed value
786
+ * @private
787
+ */
788
+ private parseType;
789
+ /**
790
+ * Parse a built-in type.
791
+ *
792
+ * @param type - Built-in type name
793
+ * @param stream - Stream to read from
794
+ * @param context - Execution context
795
+ * @returns Parsed value
796
+ * @private
797
+ */
798
+ private parseBuiltinType;
799
+ /**
800
+ * Read an integer value.
801
+ *
802
+ * @param type - Integer type (u1, u2, u4, u8, s1, s2, s4, s8)
803
+ * @param endian - Endianness
804
+ * @param stream - Stream to read from
805
+ * @returns Integer value
806
+ * @private
807
+ */
808
+ private readInteger;
809
+ /**
810
+ * Read a floating point value.
811
+ *
812
+ * @param type - Float type (f4, f8)
813
+ * @param endian - Endianness
814
+ * @param stream - Stream to read from
815
+ * @returns Float value
816
+ * @private
817
+ */
818
+ private readFloat;
819
+ }
820
+
821
+ /**
822
+ * @fileoverview Custom error classes for Kaitai Struct parsing and validation
823
+ * @module utils/errors
824
+ * @author Fabiano Pinto
825
+ * @license MIT
826
+ */
827
+ /**
828
+ * Base error class for all Kaitai Struct errors.
829
+ * All custom errors in this library extend from this class.
830
+ *
831
+ * @class KaitaiError
832
+ * @extends Error
833
+ * @example
834
+ * ```typescript
835
+ * throw new KaitaiError('Something went wrong', 42)
836
+ * ```
837
+ */
838
+ declare class KaitaiError extends Error {
839
+ position?: number | undefined;
840
+ constructor(message: string, position?: number | undefined);
841
+ }
842
+ /**
843
+ * Error thrown when validation fails.
844
+ * Used when parsed data doesn't match expected constraints.
845
+ *
846
+ * @class ValidationError
847
+ * @extends KaitaiError
848
+ * @example
849
+ * ```typescript
850
+ * throw new ValidationError('Magic bytes mismatch', 0)
851
+ * ```
852
+ */
853
+ declare class ValidationError extends KaitaiError {
854
+ constructor(message: string, position?: number);
855
+ }
856
+ /**
857
+ * Error thrown when parsing fails.
858
+ * Used for general parsing errors that don't fit other categories.
859
+ *
860
+ * @class ParseError
861
+ * @extends KaitaiError
862
+ * @example
863
+ * ```typescript
864
+ * throw new ParseError('Invalid format structure', 100)
865
+ * ```
866
+ */
867
+ declare class ParseError extends KaitaiError {
868
+ constructor(message: string, position?: number);
869
+ }
870
+ /**
871
+ * Error thrown when end of stream is reached unexpectedly.
872
+ * Indicates an attempt to read beyond available data.
873
+ *
874
+ * @class EOFError
875
+ * @extends KaitaiError
876
+ * @example
877
+ * ```typescript
878
+ * throw new EOFError('Unexpected end of stream', 1024)
879
+ * ```
880
+ */
881
+ declare class EOFError extends KaitaiError {
882
+ constructor(message?: string, position?: number);
883
+ }
884
+ /**
885
+ * Error thrown when a required feature is not yet implemented.
886
+ * Used during development to mark incomplete functionality.
887
+ *
888
+ * @class NotImplementedError
889
+ * @extends KaitaiError
890
+ * @example
891
+ * ```typescript
892
+ * throw new NotImplementedError('Custom processing')
893
+ * ```
894
+ */
895
+ declare class NotImplementedError extends KaitaiError {
896
+ constructor(feature: string);
897
+ }
898
+
899
+ /**
900
+ * @fileoverview Main entry point for kaitai-struct-ts library
901
+ * @module kaitai-struct-ts
902
+ * @author Fabiano Pinto
903
+ * @license MIT
904
+ * @version 0.2.0
905
+ *
906
+ * @description
907
+ * A runtime interpreter for Kaitai Struct binary format definitions in TypeScript.
908
+ * Parse any binary data format by providing a .ksy (Kaitai Struct YAML) definition file.
909
+ *
910
+ * @example
911
+ * ```typescript
912
+ * import { parse } from 'kaitai-struct-ts'
913
+ *
914
+ * const ksyDefinition = `
915
+ * meta:
916
+ * id: my_format
917
+ * endian: le
918
+ * seq:
919
+ * - id: magic
920
+ * contents: [0x4D, 0x5A]
921
+ * - id: version
922
+ * type: u2
923
+ * `
924
+ *
925
+ * const buffer = new Uint8Array([0x4D, 0x5A, 0x01, 0x00])
926
+ * const result = parse(ksyDefinition, buffer)
927
+ * console.log(result.version) // 1
928
+ * ```
929
+ */
930
+
931
+ /**
932
+ * Parse binary data using a Kaitai Struct definition.
933
+ * This is the main convenience function for parsing.
934
+ *
935
+ * @param ksyYaml - YAML string containing the .ksy definition
936
+ * @param buffer - Binary data to parse (ArrayBuffer or Uint8Array)
937
+ * @param options - Parsing options
938
+ * @returns Parsed object with fields defined in the .ksy file
939
+ * @throws {ParseError} If YAML parsing fails
940
+ * @throws {ValidationError} If schema validation fails
941
+ * @throws {EOFError} If unexpected end of stream is reached
942
+ *
943
+ * @example
944
+ * ```typescript
945
+ * const result = parse(ksyYaml, binaryData)
946
+ * console.log(result.fieldName)
947
+ * ```
948
+ */
949
+ declare function parse(ksyYaml: string, buffer: ArrayBuffer | Uint8Array, options?: ParseOptions): Record<string, unknown>;
950
+ /**
951
+ * Options for the parse function.
952
+ *
953
+ * @interface ParseOptions
954
+ */
955
+ interface ParseOptions {
956
+ /** Whether to validate the schema (default: true) */
957
+ validate?: boolean;
958
+ /** Whether to treat warnings as errors (default: false) */
959
+ strict?: boolean;
960
+ }
961
+
962
+ 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 };