dotscope 0.6.0

A high-performance, cross-platform framework for analyzing and reverse engineering .NET PE executables
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
//! ECMA-335 Blob Heap (`#Blob`) for .NET Metadata Storage
//!
//! This module provides access to the binary blob heap in .NET assemblies according to ECMA-335
//! Section II.24.2.4. The blob heap stores variable-length binary data including method signatures,
//! custom attribute values, field signatures, and other metadata that cannot be represented
//! as simple strings or primitive values.
//!
//! # Blob Heap Structure
//!
//! The `#Blob` heap is a sequence of variable-length binary chunks where each blob is prefixed
//! with its size encoded in a compressed format. The heap always starts with a null byte at
//! offset 0, and subsequent blobs are stored consecutively with no padding.
//!
//! ## Size Encoding Format
//!
//! Blob sizes are encoded using ECMA-335's compressed unsigned integer format:
//!
//! - **1-byte encoding**: `0bbbbbbb` → size = `bbbbbbb` (0-127 bytes)
//! - **2-byte encoding**: `10bbbbbb xxxxxxxx` → size = `(bbbbbb << 8) | xxxxxxxx` (128-16,383 bytes)
//! - **4-byte encoding**: `110bbbbb xxxxxxxx yyyyyyyy zzzzzzzz` → size = `(bbbbb << 24) | (x << 16) | (y << 8) | z` (16,384+ bytes)
//!
//! ## Memory Layout
//!
//! ```text
//! Offset | Content
//! -------|--------
//! 0x00   | 0x00 (null blob)
//! 0x01   | Size₁ | Data₁...
//! ...    | Size₂ | Data₂...
//! ...    | Size₃ | Data₃...
//! ```
//!
//! # Blob Content Types
//!
//! The blob heap stores various types of binary metadata:
//!
//! ## Method Signatures
//! Binary-encoded method signatures containing:
//! - Calling conventions and parameter counts
//! - Return type and parameter type information
//! - Generic parameter specifications
//!
//! ## Field Signatures
//! Type information for fields including:
//! - Base type specifications
//! - Custom modifiers and constraints
//! - Array and pointer type definitions
//!
//! ## Custom Attribute Values
//! Serialized constructor arguments and named parameters:
//! - Primitive value arrays
//! - String and enum values
//! - Complex nested structures
//!
//! ## Constant Values
//! Default values for fields and parameters:
//! - Primitive type values
//! - String constants
//! - Null references
//!
//! # Examples
//!
//! ## Basic Blob Access
//! ```rust
//! use dotscope::metadata::streams::Blob;
//!
//! # fn example() -> dotscope::Result<()> {
//! // Example blob heap with two entries
//! let data = &[
//!     0x00,                    // Null blob at offset 0
//!     0x03, 0x41, 0x42, 0x43, // 3-byte blob "ABC" at offset 1
//!     0x02, 0x44, 0x45        // 2-byte blob "DE" at offset 5
//! ];
//!
//! let blob_heap = Blob::from(data)?;
//!
//! // Access blob at offset 1 (referenced by metadata tables)
//! let first_blob = blob_heap.get(1)?;
//! assert_eq!(first_blob, b"ABC");
//!
//! // Access blob at offset 5
//! let second_blob = blob_heap.get(5)?;
//! assert_eq!(second_blob, b"DE");
//! # Ok(())
//! # }
//! ```
//!
//! ## Iterating Over All Blobs
//! ```rust
//! use dotscope::metadata::streams::Blob;
//!
//! # fn example() -> dotscope::Result<()> {
//! let data = &[0x00, 0x03, 0x41, 0x42, 0x43, 0x02, 0x44, 0x45];
//! let blob_heap = Blob::from(data)?;
//!
//! for (offset, blob_data) in blob_heap.iter() {
//!     println!("Blob at offset {}: {} bytes", offset, blob_data.len());
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Working with Large Blobs
//! ```rust
//! use dotscope::metadata::streams::Blob;
//!
//! # fn example() -> dotscope::Result<()> {
//! // Create blob heap with a large blob (2-byte size encoding)
//! let mut data = vec![0x00];
//! data.push(0x81);  // First byte: 10000001 (2-byte encoding)
//! data.push(0x00);  // Second byte: 00000000 (total size = 128)
//! data.extend(vec![0xFF; 128]); // 128 bytes of data
//!
//! let blob_heap = Blob::from(&data)?;
//! let large_blob = blob_heap.get(1)?;
//! assert_eq!(large_blob.len(), 128);
//! # Ok(())
//! # }
//! ```
//!
//! # Error Handling
//!
//! The blob heap API provides comprehensive error handling for:
//! - **Invalid Heap Format**: Missing null byte at offset 0
//! - **Out of Bounds Access**: Invalid blob offsets or corrupted size data
//! - **Truncated Data**: Incomplete blob entries
//! - **Malformed Size Encoding**: Invalid compressed integer format
//!
//! # ECMA-335 Compliance
//!
//! This implementation fully complies with ECMA-335 Partition II, Section 24.2.4:
//! - Supports all three size encoding formats
//! - Handles edge cases like empty blobs and maximum size limits
//! - Provides safe access with proper bounds checking
//! - Maintains the required null blob at offset 0
//!
//! # See Also
//!
//! - [`crate::metadata::streams::Strings`]: For string heap access
//! - [`crate::metadata::streams::UserStrings`]: For user string heap access
//! - [`crate::metadata::signatures`]: For parsing blob content as signatures
//! - [ECMA-335 Standard](https://ecma-international.org/wp-content/uploads/ECMA-335_6th_edition_june_2012.pdf): Official specification
//!
//! # References
//!
//! - **ECMA-335 II.24.2.4**: `#Blob` heap specification
//! - **ECMA-335 II.23.2**: Signature encoding formats stored in blobs

use crate::{file::parser::Parser, Result};

/// ECMA-335 binary blob heap providing indexed access to variable-length data.
///
/// The `Blob` struct represents the `#Blob` metadata heap according to ECMA-335 Section II.24.2.4.
/// It provides safe, zero-copy access to binary data referenced by metadata table entries
/// such as method signatures, field types, custom attributes, and constant values.
///
/// # Blob Heap Properties
///
/// ## Indexed Access Model
/// Blobs are accessed by offset rather than sequential iteration:
/// - Metadata tables store blob heap offsets as indices
/// - Each offset points to a size-prefixed binary chunk
/// - Size encoding uses compressed unsigned integers
/// - Random access enables efficient metadata parsing
///
/// ## Size Encoding Details
/// The blob size prefix uses ECMA-335 compressed format:
///
/// ```text
/// Format           | Size Range        | Encoding
/// -----------------|-------------------|------------------
/// 1-byte           | 0-127 bytes      | 0bbbbbbb
/// 2-byte           | 128-16,383 bytes | 10bbbbbb xxxxxxxx
/// 4-byte           | 16,384+ bytes    | 110bbbbb xxxxxxxx yyyyyyyy zzzzzzzz
/// ```
///
/// ## Memory Layout
/// ```text
/// Offset | Content
/// -------|------------------
/// 0      | 0x00 (null blob)
/// 1      | Size₁ | Data₁
/// N      | Size₂ | Data₂
/// M      | Size₃ | Data₃
/// ```
///
/// # Blob Content Categories
///
/// ## Signature Blobs
/// Method, field, and property signatures:
/// - Calling conventions and parameter information
/// - Type specifications and generic parameters
/// - Custom modifiers and constraints
///
/// ## Custom Attribute Blobs
/// Constructor arguments and named parameters:
/// - Primitive arrays and enum values
/// - Complex nested data structures
/// - String and object references
///
/// ## Constant Value Blobs
/// Default values for fields and parameters:
/// - Primitive type constants
/// - String literals and null values
/// - Binary data for complex constants
///
/// # Safety and Error Handling
///
/// The `Blob` struct provides comprehensive safety guarantees:
/// - Bounds checking for all offset and size calculations
/// - Validation of compressed size encoding format
/// - Protection against integer overflow in size calculations
/// - Graceful handling of truncated or malformed data
///
/// # Examples
///
/// ## Creating and Accessing Blobs
/// ```rust
/// use dotscope::metadata::streams::Blob;
///
/// # fn example() -> dotscope::Result<()> {
/// // Create blob heap with sample data
/// let heap_data = &[
///     0x00,                           // Required null blob at offset 0
///     0x04, 0x01, 0x02, 0x03, 0x04,  // 4-byte blob at offset 1
///     0x02, 0xFF, 0xFE,              // 2-byte blob at offset 6
/// ];
///
/// let blob_heap = Blob::from(heap_data)?;
///
/// // Access specific blobs by offset (as referenced by metadata tables)
/// let first_blob = blob_heap.get(1)?;
/// assert_eq!(first_blob, &[0x01, 0x02, 0x03, 0x04]);
///
/// let second_blob = blob_heap.get(6)?;
/// assert_eq!(second_blob, &[0xFF, 0xFE]);
/// # Ok(())
/// # }
/// ```
///
/// ## Handling Large Blobs
/// ```rust
/// use dotscope::metadata::streams::Blob;
///
/// # fn example() -> dotscope::Result<()> {
/// // Large blob with 2-byte size encoding
/// let mut heap_data = vec![0x00];                    // Null blob
/// heap_data.push(0x81);                             // Size: 10000001 (2-byte format)
/// heap_data.push(0x00);                             // Size: 00000000 (total = 128)
/// heap_data.extend(std::iter::repeat(0xAA).take(128)); // 128 bytes of data
///
/// let blob_heap = Blob::from(&heap_data)?;
/// let large_blob = blob_heap.get(1)?;
/// assert_eq!(large_blob.len(), 128);
/// assert!(large_blob.iter().all(|&b| b == 0xAA));
/// # Ok(())
/// # }
/// ```
///
/// ## Iterating Through All Blobs
/// ```rust
/// use dotscope::metadata::streams::Blob;
///
/// # fn example() -> dotscope::Result<()> {
/// let heap_data = &[0x00, 0x03, 0x41, 0x42, 0x43, 0x01, 0x44];
/// let blob_heap = Blob::from(heap_data)?;
///
/// for (offset, data) in &blob_heap {
///     println!("Blob at offset {}: {:02X?}", offset, data);
/// }
/// # Ok(())
/// # }
/// ```
///
/// # ECMA-335 Compliance
///
/// This implementation fully supports ECMA-335 requirements:
/// - Correct handling of all three size encoding formats
/// - Mandatory null blob at offset 0
/// - Proper bounds checking and error handling
/// - Support for maximum blob sizes (up to 2^29 bytes)
///
/// # See Also
/// - [`BlobIterator`]: For sequential access to all blobs
/// - [`crate::metadata::signatures`]: For parsing blob content as type signatures
/// - [`crate::file::parser::Parser`]: For reading compressed integers
/// - [ECMA-335 II.24.2.4](https://ecma-international.org/wp-content/uploads/ECMA-335_6th_edition_june_2012.pdf): Official specification
///
pub struct Blob<'a> {
    /// Raw bytes of the blob heap
    data: &'a [u8],
}

impl<'a> Blob<'a> {
    /// Creates a new `Blob` heap accessor from raw metadata bytes.
    ///
    /// Validates that the provided data represents a well-formed ECMA-335 `#Blob` heap
    /// with the required null blob at offset 0. The blob heap stores variable-length
    /// binary data referenced by metadata table entries.
    ///
    /// # Parameters
    /// * `data` - Raw bytes containing the complete `#Blob` heap data
    ///
    /// # Returns
    /// A `Blob` instance providing safe access to the heap contents, or an error
    /// if the data format is invalid.
    ///
    /// # Errors
    /// Returns [`crate::Error`] in the following cases:
    /// - **Empty data**: The heap must contain at least one byte
    /// - **Missing null blob**: First byte must be 0x00 per ECMA-335 requirements
    /// - **Invalid format**: Data doesn't conform to blob heap structure
    ///
    /// # Examples
    ///
    /// ## Valid Blob Heap
    /// ```rust
    /// use dotscope::metadata::streams::Blob;
    ///
    /// # fn example() -> dotscope::Result<()> {
    /// let valid_data = &[
    ///     0x00,                    // Required null blob at offset 0
    ///     0x04, 0x01, 0x02, 0x03, 0x04,  // 4-byte blob
    ///     0x02, 0xFF, 0xFE,        // 2-byte blob
    /// ];
    ///
    /// let blob_heap = Blob::from(valid_data)?;
    /// assert_eq!(blob_heap.get(1)?, &[0x01, 0x02, 0x03, 0x04]);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// ## Invalid Blob Heap
    /// ```rust
    /// use dotscope::metadata::streams::Blob;
    ///
    /// // Missing required null byte at offset 0
    /// let invalid_data = &[0x01, 0x02, 0x03];
    /// assert!(Blob::from(invalid_data).is_err());
    ///
    /// // Empty data
    /// let empty_data = &[];
    /// assert!(Blob::from(empty_data).is_err());
    /// ```
    ///
    /// # Safety
    /// This method performs minimal validation for performance. Individual blob
    /// access via [`get`](Self::get) provides comprehensive bounds checking.
    ///
    /// # See Also
    /// - [`get`](Self::get): Access individual blobs by offset
    /// - [`iter`](Self::iter): Iterate over all blobs in the heap
    /// - [ECMA-335 II.24.2.4](https://ecma-international.org/wp-content/uploads/ECMA-335_6th_edition_june_2012.pdf): Blob heap specification
    pub fn from(data: &'a [u8]) -> Result<Blob<'a>> {
        if data.is_empty() || data[0] != 0 {
            return Err(malformed_error!("Invalid memory for #Blob heap"));
        }

        Ok(Blob { data })
    }

    /// Retrieves a blob from the heap by its offset.
    ///
    /// Returns a zero-copy view of the binary data stored at the specified offset.
    /// The offset typically comes from metadata table entries that reference blob
    /// heap data such as method signatures, custom attributes, or constant values.
    ///
    /// # Parameters
    /// * `index` - Byte offset within the blob heap (from metadata table references)
    ///
    /// # Returns
    /// A slice containing the blob data at the specified offset, or an error
    /// if the offset is invalid or the blob is malformed.
    ///
    /// # Errors
    /// Returns [`crate::Error`] in the following cases:
    /// - **Out of bounds**: `index` exceeds the heap size
    /// - **Invalid size encoding**: Blob size header is malformed
    /// - **Truncated data**: Blob extends beyond heap boundaries
    /// - **Integer overflow**: Size calculations exceed platform limits
    ///
    /// # Examples
    ///
    /// ## Basic Blob Access
    /// ```rust
    /// use dotscope::metadata::streams::Blob;
    ///
    /// # fn example() -> dotscope::Result<()> {
    /// let data = &[
    ///     0x00,                           // Null blob at offset 0
    ///     0x03, 0x41, 0x42, 0x43,        // "ABC" at offset 1
    ///     0x81, 0x02,                     // 2-byte size encoding for 258 bytes
    ///     // ... 258 bytes of data would follow
    /// ];
    ///
    /// let blob_heap = Blob::from(&data[..6])?; // Truncated for example
    ///
    /// // Access null blob (always empty)
    /// let null_blob = blob_heap.get(0)?;
    /// assert_eq!(null_blob, &[]);
    ///
    /// // Access first real blob
    /// let first_blob = blob_heap.get(1)?;
    /// assert_eq!(first_blob, b"ABC");
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// ## Error Handling
    /// ```rust
    /// use dotscope::metadata::streams::Blob;
    ///
    /// # fn example() -> dotscope::Result<()> {
    /// let data = &[0x00, 0x02, 0x41]; // Blob claims 2 bytes but only 1 available
    /// let blob_heap = Blob::from(data)?;
    ///
    /// // This will fail due to truncated data
    /// assert!(blob_heap.get(1).is_err());
    ///
    /// // Out of bounds access
    /// assert!(blob_heap.get(100).is_err());
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Size Encoding Details
    /// The blob size is encoded using ECMA-335 compressed unsigned integers:
    ///
    /// | Encoding | First Byte Pattern | Size Range |
    /// |----------|-------------------|------------|
    /// | 1-byte   | `0bbbbbbb`        | 0-127      |
    /// | 2-byte   | `10bbbbbb`        | 128-16,383 |
    /// | 4-byte   | `110bbbbb`        | 16,384+    |
    ///
    /// # See Also
    /// - [`iter`](Self::iter): Iterate over all blobs sequentially
    /// - [`crate::file::parser::Parser`]: For compressed integer parsing
    /// - [ECMA-335 II.23.2](https://ecma-international.org/wp-content/uploads/ECMA-335_6th_edition_june_2012.pdf): Compressed integer format
    pub fn get(&self, index: usize) -> Result<&'a [u8]> {
        if index >= self.data.len() {
            return Err(out_of_bounds_error!());
        }

        let mut parser = Parser::new(&self.data[index..]);
        let len = parser.read_compressed_uint()? as usize;
        let skip = parser.pos();

        let Some(data_start) = index.checked_add(skip) else {
            return Err(out_of_bounds_error!());
        };

        let Some(data_end) = data_start.checked_add(len) else {
            return Err(out_of_bounds_error!());
        };

        if data_start > self.data.len() || data_end > self.data.len() {
            return Err(out_of_bounds_error!());
        }

        Ok(&self.data[data_start..data_end])
    }

    /// Returns an iterator over all blobs in the heap.
    ///
    /// Provides sequential access to every blob stored in the heap, yielding
    /// both the offset and binary data for each entry. This is useful for
    /// comprehensive analysis, validation, or debugging of blob heap contents.
    ///
    /// # Returns
    /// A [`BlobIterator`] that yields `(usize, &[u8])` tuples containing:
    /// - **Offset**: Byte position of the blob within the heap
    /// - **Data**: Zero-copy slice of the blob's binary content
    ///
    /// # Iteration Behavior
    /// - **Sequential access**: Blobs returned in heap order (not offset order)
    /// - **Skips null blob**: Iterator starts at offset 1, skipping the null blob at 0
    /// - **Error handling**: Iterator stops on malformed blobs rather than continuing
    /// - **Zero-copy**: Each blob is a direct slice reference to heap data
    ///
    /// # Examples
    ///
    /// ## Basic Iteration
    /// ```rust
    /// use dotscope::metadata::streams::Blob;
    ///
    /// # fn example() -> dotscope::Result<()> {
    /// let data = &[
    ///     0x00,                    // Null blob (skipped by iterator)
    ///     0x03, 0x41, 0x42, 0x43, // "ABC" blob at offset 1
    ///     0x02, 0x44, 0x45,       // "DE" blob at offset 5
    ///     0x00,                   // Empty blob at offset 8
    /// ];
    ///
    /// let blob_heap = Blob::from(data)?;
    ///
    /// for (offset, blob_data) in blob_heap.iter() {
    ///     println!("Blob at offset {}: {} bytes", offset, blob_data.len());
    /// }
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// ## Error Handling During Iteration
    /// ```rust
    /// use dotscope::metadata::streams::Blob;
    ///
    /// # fn example() -> dotscope::Result<()> {
    /// let data = &[0x00, 0x05, 0x41, 0x42]; // Claims 5 bytes but only 2 available
    /// let blob_heap = Blob::from(data)?;
    ///
    /// for (offset, blob_data) in blob_heap.iter() {
    ///     println!("Valid blob at {}: {:02X?}", offset, blob_data);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// ## Collecting All Valid Blobs
    /// ```rust
    /// use dotscope::metadata::streams::Blob;
    ///
    /// # fn example() -> dotscope::Result<()> {
    /// let data = &[0x00, 0x02, 0x41, 0x42, 0x01, 0x43];
    /// let blob_heap = Blob::from(data)?;
    ///
    /// let blobs: Vec<_> = blob_heap.iter().collect();
    ///
    /// assert_eq!(blobs.len(), 2);
    /// assert_eq!(blobs[0], (1, &[0x41, 0x42][..]));
    /// assert_eq!(blobs[1], (4, &[0x43][..]));
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Error Handling
    /// If a malformed blob is encountered, the iterator stops and returns None.
    /// This design prioritizes data integrity over partial processing of
    /// potentially corrupted metadata.
    ///
    ///
    /// # See Also
    /// - [`BlobIterator`]: The iterator implementation details
    /// - [`get`](Self::get): Direct access to specific blobs by offset
    /// - [ECMA-335 II.24.2.4](https://ecma-international.org/wp-content/uploads/ECMA-335_6th_edition_june_2012.pdf): Blob heap specification
    #[must_use]
    pub fn iter(&self) -> BlobIterator<'_> {
        BlobIterator::new(self)
    }

    /// Returns the raw underlying data of the blob heap.
    ///
    /// This provides access to the complete heap data including the null byte at offset 0
    /// and all blob entries in their original binary format.
    #[must_use]
    pub fn data(&self) -> &[u8] {
        self.data
    }

    /// Checks if the blob heap contains a specific byte sequence.
    ///
    /// Searches through all blobs in the heap to determine if any entry matches
    /// the provided byte data exactly. This is useful for quick existence checks
    /// when looking for specific signatures or custom attribute values.
    ///
    /// # Arguments
    /// * `data` - The byte sequence to search for
    ///
    /// # Returns
    /// `true` if the blob exists in the heap, `false` otherwise.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use dotscope::metadata::streams::Blob;
    ///
    /// # fn example() -> dotscope::Result<()> {
    /// let heap_data = [
    ///     0x00,
    ///     0x03, 0x41, 0x42, 0x43,  // "ABC"
    ///     0x02, 0x44, 0x45,        // "DE"
    /// ];
    /// let blob = Blob::from(&heap_data)?;
    ///
    /// assert!(blob.contains(b"ABC"));
    /// assert!(blob.contains(b"DE"));
    /// assert!(!blob.contains(b"NotFound"));
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn contains(&self, data: &[u8]) -> bool {
        self.iter().any(|(_, value)| value == data)
    }

    /// Finds the first occurrence of a blob and returns its heap offset.
    ///
    /// Searches through all blobs in the heap and returns the byte offset of the
    /// first matching entry. This offset can be used with metadata table references.
    ///
    /// # Arguments
    /// * `data` - The byte sequence to search for
    ///
    /// # Returns
    /// `Some(offset)` if the blob is found, `None` otherwise.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use dotscope::metadata::streams::Blob;
    ///
    /// # fn example() -> dotscope::Result<()> {
    /// let heap_data = [
    ///     0x00,
    ///     0x03, 0x41, 0x42, 0x43,  // "ABC" at offset 1
    ///     0x02, 0x44, 0x45,        // "DE" at offset 5
    /// ];
    /// let blob = Blob::from(&heap_data)?;
    ///
    /// assert_eq!(blob.find(b"ABC"), Some(1));
    /// assert_eq!(blob.find(b"DE"), Some(5));
    /// assert_eq!(blob.find(b"NotFound"), None);
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn find(&self, data: &[u8]) -> Option<u32> {
        self.iter()
            .find(|(_, value)| *value == data)
            .and_then(|(offset, _)| u32::try_from(offset).ok())
    }

    /// Finds all occurrences of a blob and returns their heap offsets.
    ///
    /// Searches through all blobs in the heap and returns the byte offsets of all
    /// matching entries. This handles the case where duplicate blobs exist in the heap.
    ///
    /// # Arguments
    /// * `data` - The byte sequence to search for
    ///
    /// # Returns
    /// A vector of offsets where the blob was found. Empty if not found.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use dotscope::metadata::streams::Blob;
    ///
    /// # fn example() -> dotscope::Result<()> {
    /// let heap_data = [
    ///     0x00,
    ///     0x02, 0x41, 0x42,  // "AB" at offset 1
    ///     0x02, 0x41, 0x42,  // "AB" at offset 4
    /// ];
    /// let blob = Blob::from(&heap_data)?;
    ///
    /// let offsets = blob.find_all(b"AB");
    /// assert_eq!(offsets.len(), 2);
    /// assert_eq!(offsets[0], 1);
    /// assert_eq!(offsets[1], 4);
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn find_all(&self, data: &[u8]) -> Vec<u32> {
        self.iter()
            .filter(|(_, value)| *value == data)
            .filter_map(|(offset, _)| u32::try_from(offset).ok())
            .collect()
    }
}

impl<'a> IntoIterator for &'a Blob<'a> {
    type Item = (usize, &'a [u8]);
    type IntoIter = BlobIterator<'a>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// Iterator providing sequential access to all blobs in the `#Blob` heap.
///
/// The `BlobIterator` traverses the blob heap in storage order, yielding each blob's
/// offset and binary data. It automatically handles the variable-length encoding
/// and provides comprehensive error reporting for malformed entries.
///
/// # Iteration Protocol
///
/// ## Yielded Items
/// Each successful iteration returns `Ok((offset, data))` where:
/// - **`offset`**: Byte position of the blob within the heap
/// - **`data`**: Zero-copy slice containing the blob's binary content
///
/// ## Error Handling
/// Malformed blobs yield `Err(Error)` with specific error information:
/// - **Truncated size encoding**: Invalid compressed integer format
/// - **Out of bounds data**: Blob extends beyond heap boundaries  
/// - **Parse failures**: Corrupted size headers or data alignment
///
/// ## Iteration Behavior
/// - **Starts at offset 1**: Skips the mandatory null blob at offset 0
/// - **Sequential processing**: Processes blobs in heap storage order
/// - **Lazy evaluation**: Size and data parsed only when accessed
/// - **Error recovery**: Attempts to continue after non-fatal errors
///
/// # Memory and Performance
///
/// - **Zero-copy access**: All blob data returned as direct slice references
/// - **Minimal state**: Iterator maintains only position and heap reference
/// - **Cache-friendly**: Sequential access pattern optimizes memory locality
/// - **Bounded iteration**: Always terminates when heap end is reached
///
/// # Examples
///
/// ## Basic Sequential Access
/// ```rust
/// use dotscope::metadata::streams::Blob;
///
/// # fn example() -> dotscope::Result<()> {
/// let data = &[0x00, 0x03, 0x41, 0x42, 0x43, 0x01, 0x44];
/// let blob_heap = Blob::from(data)?;
/// let mut iterator = blob_heap.iter();
///
/// // First blob: "ABC" at offset 1
/// let (offset1, blob1) = iterator.next().unwrap();
/// assert_eq!(offset1, 1);
/// assert_eq!(blob1, b"ABC");
///
/// // Second blob: "D" at offset 5
/// let (offset2, blob2) = iterator.next().unwrap();
/// assert_eq!(offset2, 5);
/// assert_eq!(blob2, b"D");
///
/// // No more blobs
/// assert!(iterator.next().is_none());
/// # Ok(())
/// # }
/// ```
///
/// ## Error Handling
/// ```rust
/// use dotscope::metadata::streams::Blob;
///
/// # fn example() -> dotscope::Result<()> {
/// // Malformed heap: blob claims 10 bytes but only 3 available
/// let data = &[0x00, 0x0A, 0x41, 0x42, 0x43];
/// let blob_heap = Blob::from(data)?;
///
/// for (offset, blob_data) in blob_heap.iter() {
///     println!("Valid blob at {}: {} bytes", offset, blob_data.len());
/// }
/// # Ok(())
/// # }
/// ```
///
/// ## Filtering and Processing
/// ```rust
/// use dotscope::metadata::streams::Blob;
///
/// # fn example() -> dotscope::Result<()> {
/// let data = &[0x00, 0x00, 0x03, 0x41, 0x42, 0x43, 0x00];
/// let blob_heap = Blob::from(data)?;
///
/// // Find all non-empty blobs
/// let non_empty_blobs: Vec<_> = blob_heap
///     .iter()
///     .filter(|(_, data)| !data.is_empty())
///     .collect();
///
/// assert_eq!(non_empty_blobs.len(), 1);
/// assert_eq!(non_empty_blobs[0].1, b"ABC");
/// # Ok(())
/// # }
/// ```
///
/// # Implementation Notes
///
/// - **State management**: Tracks current position and source blob reference
/// - **Size parsing**: Uses [`crate::file::parser::Parser`] for compressed integers
/// - **Bounds checking**: Validates all offset and length calculations
/// - **Memory safety**: Lifetime bounds ensure heap data remains valid
///
/// # See Also
/// - [`Blob::iter`]: Creates new iterator instances
/// - [`Blob::get`]: Direct access to specific blobs by offset
/// - [`crate::file::parser::Parser`]: Compressed integer parsing implementation
/// - [ECMA-335 II.24.2.4](https://ecma-international.org/wp-content/uploads/ECMA-335_6th_edition_june_2012.pdf): Blob heap specification
pub struct BlobIterator<'a> {
    /// Reference to the blob heap being iterated
    blob: &'a Blob<'a>,
    /// Current position within the blob heap
    position: usize,
}

impl<'a> BlobIterator<'a> {
    /// Creates a new iterator for the given blob heap.
    ///
    /// Initializes the iterator to begin at offset 1, automatically skipping
    /// the mandatory null blob at offset 0 as required by ECMA-335.
    ///
    /// # Parameters
    /// * `blob` - Reference to the blob heap to iterate over
    ///
    /// # Returns
    /// A new `BlobIterator` positioned at the first non-null blob entry.
    ///
    /// # Note
    /// This method is internal and called by [`Blob::iter`]. Users should
    /// prefer the public `iter()` method for creating iterators.
    pub(crate) fn new(blob: &'a Blob<'a>) -> Self {
        Self {
            blob,
            position: 1, // Skip the initial null byte at position 0
        }
    }
}

impl<'a> Iterator for BlobIterator<'a> {
    type Item = (usize, &'a [u8]);

    fn next(&mut self) -> Option<Self::Item> {
        if self.position >= self.blob.data.len() {
            return None;
        }

        let start_position = self.position;
        match self.blob.get(self.position) {
            Ok(blob_data) => {
                let mut parser = Parser::new(&self.blob.data[self.position..]);
                if parser.read_compressed_uint().is_ok() {
                    let length_bytes = parser.pos();
                    self.position += length_bytes + blob_data.len();
                    Some((start_position, blob_data))
                } else {
                    None
                }
            }
            Err(_) => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn crafted() {
        #[rustfmt::skip]
        let data = { 
            let mut data = vec![0xCC; 262143];
            /* i - 0    - should always be 0    */ data[0]          = 0b_00000000_u8;

            /* i - 1    - len 10                */ data[1]          = 0b_00001010_u8;
            /* i - 1    - len 10                */ data[2..12]      .copy_from_slice(&[0x0A; 10]);
    
            /* i - 12   - len 5                 */ data[12]         = 0b_00000101_u8;
            /* i - 12   - len 5                 */ data[13..18]     .copy_from_slice(&[0xAB; 5]);

            /* i - 18   - len 0 - invalid       */ data[18]         = 0b_11111111_u8;
    
            /* i - 19   - len 256               */ data[19]         = 0b_10000001_u8;
            /* i - 19   - len 256               */ data[20]         = 0b_00000001_u8;
            /* i - 19   - len 256               */ data[21..278]    .copy_from_slice(&[0xBA; 257]);
            /* i - 2070 - len 2048              */ data[278]        = 0b_11000000_u8;
            /* i - 2070 - len 2048              */ data[279]        = 0b_00000001_u8;
            /* i - 2070 - len 2048              */ data[280]        = 0b_00000001_u8;
            /* i - 2070 - len 2048              */ data[281]        = 0b_00000001_u8;
            /* i - 2070 - len 2048              */ data[282..66075]  .copy_from_slice(&[0xBA; 65793]);

            data
        };

        let blob = Blob::from(&data).unwrap();

        {
            let indexed = blob.get(0).unwrap();
            assert_eq!(indexed.len(), 0);
        }

        {
            let indexed = blob.get(1).unwrap();
            assert_eq!(indexed.len(), 10);
            assert_eq!(indexed, &[0x0A; 10]);
        }

        {
            let indexed = blob.get(12).unwrap();
            assert_eq!(indexed.len(), 5);
            assert_eq!(indexed, &[0xAB; 5]);
        }

        {
            if blob.get(18).is_ok() {
                panic!("This should not be valid!")
            }
        }

        {
            let indexed = blob.get(19).unwrap();
            assert_eq!(indexed.len(), 257);
            assert_eq!(indexed, &[0xBA; 257]);
        }

        {
            let indexed = blob.get(278).unwrap();
            assert_eq!(indexed.len(), 65793);
            assert_eq!(indexed, &[0xBA; 65793]);
        }
    }

    #[test]
    fn test_blob_iterator_basic() {
        let data = [0x00, 0x02, 0x41, 0x42, 0x01, 0x43];
        let blob = Blob::from(&data).unwrap();
        let mut iter = blob.iter();

        let first = iter.next().unwrap();
        assert_eq!(first.0, 1);
        assert_eq!(first.1.len(), 2);
        assert_eq!(first.1, &[0x41, 0x42]);

        let second = iter.next().unwrap();
        assert_eq!(second.0, 4);
        assert_eq!(second.1.len(), 1);
        assert_eq!(second.1, &[0x43]);

        assert!(iter.next().is_none());
    }

    #[test]
    fn test_blob_iterator_empty_blob() {
        let data = [0x00, 0x00, 0x02, 0x41, 0x42];
        let blob = Blob::from(&data).unwrap();
        let mut iter = blob.iter();

        let first = iter.next().unwrap();
        assert_eq!(first.0, 1);
        assert_eq!(first.1.len(), 0);
        assert_eq!(first.1, &[] as &[u8]);

        let second = iter.next().unwrap();
        assert_eq!(second.0, 2);
        assert_eq!(second.1.len(), 2);
        assert_eq!(second.1, &[0x41, 0x42]);

        assert!(iter.next().is_none());
    }

    #[test]
    fn test_blob_iterator_large_blob() {
        // Test with two-byte length encoding
        let mut data = vec![0x00, 0x81, 0x02]; // Length 258 (two-byte encoding)
        data.extend(vec![0xFF; 258]);
        data.push(0x01); // Single byte blob
        data.push(0xAA);

        let blob = Blob::from(&data).unwrap();
        let mut iter = blob.iter();

        let first = iter.next().unwrap();
        assert_eq!(first.0, 1);
        assert_eq!(first.1.len(), 258);
        assert_eq!(first.1, &vec![0xFF; 258]);

        let second = iter.next().unwrap();
        assert_eq!(second.0, 261);
        assert_eq!(second.1.len(), 1);
        assert_eq!(second.1, &[0xAA]);

        assert!(iter.next().is_none());
    }

    #[test]
    fn test_blob_iterator_truncated_data() {
        // Blob claims length 5 but only 3 bytes available
        let data = [0x00, 0x05, 0x41, 0x42, 0x43];
        let blob = Blob::from(&data).unwrap();
        let mut iter = blob.iter();

        assert!(iter.next().is_none());
    }

    #[test]
    fn test_blob_iterator_single_item() {
        let data = [0x00, 0x03, 0x41, 0x42, 0x43];
        let blob = Blob::from(&data).unwrap();
        let mut iter = blob.iter();

        let first = iter.next().unwrap();
        assert_eq!(first.0, 1);
        assert_eq!(first.1.len(), 3);
        assert_eq!(first.1, &[0x41, 0x42, 0x43]);

        assert!(iter.next().is_none());
    }
}