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
//! # NestedClass Builder
//!
//! Provides a fluent API for building NestedClass table entries that define hierarchical relationships
//! between nested types and their enclosing types. The NestedClass table establishes type containment
//! structure essential for proper type visibility and scoping in .NET assemblies.
//!
//! ## Overview
//!
//! The `NestedClassBuilder` enables creation of nested class relationships with:
//! - Nested type specification (required)
//! - Enclosing type specification (required)  
//! - Validation of type relationships
//! - Automatic token generation and metadata management
//!
//! ## Usage
//!
//! ```rust,no_run
//! # use dotscope::prelude::*;
//! # use std::path::Path;
//! # fn main() -> dotscope::Result<()> {
//! # let view = CilAssemblyView::from_path(Path::new("test.dll"))?;
//! # let mut assembly = CilAssembly::new(view);
//!
//! // Create an enclosing type first
//! let outer_class_token = TypeDefBuilder::new()
//!     .name("OuterClass")
//!     .namespace("MyApp.Models")
//!     .public_class()
//!     .build(&mut assembly)?;
//!
//! // Create a nested type
//! let inner_class_token = TypeDefBuilder::new()
//!     .name("InnerClass")
//!     .namespace("MyApp.Models")
//!     .flags(TypeAttributes::NESTED_PUBLIC | TypeAttributes::CLASS)
//!     .build(&mut assembly)?;
//!
//! // Establish the nesting relationship
//! let nesting_token = NestedClassBuilder::new()
//!     .nested_class(inner_class_token.row())
//!     .enclosing_class(outer_class_token.row())
//!     .build(&mut assembly)?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Design
//!
//! The builder follows the established pattern with:
//! - **Validation**: Both nested and enclosing types are required
//! - **Relationship Validation**: Prevents invalid nesting scenarios
//! - **Token Generation**: Metadata tokens are created automatically
//! - **Type Safety**: Ensures proper TypeDef token validation

use crate::{
    cilassembly::{ChangeRefRc, CilAssembly},
    metadata::{
        tables::{NestedClassRaw, TableDataOwned, TableId},
        token::Token,
    },
    Error, Result,
};

/// Builder for creating NestedClass table entries.
///
/// `NestedClassBuilder` provides a fluent API for creating entries in the NestedClass
/// metadata table, which defines hierarchical relationships between nested types and
/// their enclosing types.
///
/// # Purpose
///
/// The NestedClass table serves several key functions:
/// - **Type Hierarchy**: Defines which types are nested within other types
/// - **Visibility Scoping**: Establishes access rules for nested types
/// - **Enclosing Context**: Links nested types to their containing types
/// - **Namespace Resolution**: Enables proper type resolution within nested contexts
/// - **Compilation Support**: Provides context for type compilation and loading
///
/// # Builder Pattern
///
/// The builder provides a fluent interface for constructing NestedClass entries:
///
/// ```rust,no_run
/// # use dotscope::prelude::*;
/// # use std::path::Path;
/// # let view = CilAssemblyView::from_path(Path::new("test.dll"))?;
/// # let mut assembly = CilAssembly::new(view);
/// # let outer_row: u32 = 1;
/// # let inner_row: u32 = 2;
///
/// let nesting_token = NestedClassBuilder::new()
///     .nested_class(inner_row)
///     .enclosing_class(outer_row)
///     .build(&mut assembly)?;
/// # Ok::<(), dotscope::Error>(())
/// ```
///
/// # Validation
///
/// The builder enforces the following constraints:
/// - **Nested Class Required**: A nested class token must be provided
/// - **Enclosing Class Required**: An enclosing class token must be provided
/// - **Token Validation**: Both tokens must be valid TypeDef tokens
/// - **Relationship Validation**: Prevents invalid nesting scenarios (self-nesting, etc.)
///
/// # Integration
///
/// NestedClass entries integrate with other metadata structures:
/// - **TypeDef**: Both nested and enclosing types must be TypeDef entries
/// - **Type Registry**: Establishes relationships in the type system
/// - **Visibility Rules**: Nested types inherit accessibility from their context
#[derive(Debug, Clone)]
pub struct NestedClassBuilder {
    /// The row index (or placeholder) of the nested type
    nested_class: Option<u32>,
    /// The row index (or placeholder) of the enclosing type
    enclosing_class: Option<u32>,
}

impl Default for NestedClassBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl NestedClassBuilder {
    /// Creates a new `NestedClassBuilder` instance.
    ///
    /// Returns a builder with all fields unset, ready for configuration
    /// through the fluent API methods.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use dotscope::prelude::*;
    /// let builder = NestedClassBuilder::new();
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self {
            nested_class: None,
            enclosing_class: None,
        }
    }

    /// Sets the row index of the nested type.
    ///
    /// The nested type must be a valid TypeDef row index or placeholder that represents
    /// the type being nested within the enclosing type.
    ///
    /// # Arguments
    ///
    /// * `nested_class_row` - Row index (or placeholder) of the TypeDef for the nested type
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use dotscope::prelude::*;
    /// # use std::path::Path;
    /// # fn main() -> dotscope::Result<()> {
    /// # let view = CilAssemblyView::from_path(Path::new("test.dll"))?;
    /// # let mut assembly = CilAssembly::new(view);
    /// let inner_token = TypeDefBuilder::new()
    ///     .name("InnerClass")
    ///     .flags(TypeAttributes::NESTED_PUBLIC | TypeAttributes::CLASS)
    ///     .build(&mut assembly)?;
    ///
    /// let builder = NestedClassBuilder::new()
    ///     .nested_class(inner_token.placeholder());
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn nested_class(mut self, nested_class_row: u32) -> Self {
        self.nested_class = Some(nested_class_row);
        self
    }

    /// Sets the row index of the enclosing type.
    ///
    /// The enclosing type must be a valid TypeDef row index or placeholder that represents
    /// the type containing the nested type.
    ///
    /// # Arguments
    ///
    /// * `enclosing_class_row` - Row index (or placeholder) of the TypeDef for the enclosing type
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use dotscope::prelude::*;
    /// # use std::path::Path;
    /// # fn main() -> dotscope::Result<()> {
    /// # let view = CilAssemblyView::from_path(Path::new("test.dll"))?;
    /// # let mut assembly = CilAssembly::new(view);
    /// let outer_token = TypeDefBuilder::new()
    ///     .name("OuterClass")
    ///     .public_class()
    ///     .build(&mut assembly)?;
    ///
    /// let builder = NestedClassBuilder::new()
    ///     .enclosing_class(outer_token.placeholder());
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    pub fn enclosing_class(mut self, enclosing_class_row: u32) -> Self {
        self.enclosing_class = Some(enclosing_class_row);
        self
    }

    /// Builds the NestedClass entry and adds it to the assembly.
    ///
    /// This method validates all required fields, verifies the type tokens are valid TypeDef
    /// tokens, validates the nesting relationship, creates the NestedClass table entry,
    /// and returns the metadata token for the new entry.
    ///
    /// # Arguments
    ///
    /// * `assembly` - The CilAssembly being modified
    ///
    /// # Returns
    ///
    /// Returns the metadata token for the newly created NestedClass entry.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The nested class token is not set
    /// - The enclosing class token is not set
    /// - Either token is not a valid TypeDef token
    /// - The tokens refer to the same type (self-nesting)
    /// - There are issues adding the table row
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use dotscope::prelude::*;
    /// # use std::path::Path;
    /// # let view = CilAssemblyView::from_path(Path::new("test.dll"))?;
    /// # let mut assembly = CilAssembly::new(view);
    /// # let outer_row: u32 = 1;
    /// # let inner_row: u32 = 2;
    ///
    /// let nesting_token = NestedClassBuilder::new()
    ///     .nested_class(inner_row)
    ///     .enclosing_class(outer_row)
    ///     .build(&mut assembly)?;
    ///
    /// println!("Created NestedClass with token: {:?}", nesting_token);
    /// # Ok::<(), dotscope::Error>(())
    /// ```
    pub fn build(self, assembly: &mut CilAssembly) -> Result<ChangeRefRc> {
        let nested_class_row = self.nested_class.ok_or_else(|| {
            Error::ModificationInvalid("Nested class row is required for NestedClass".to_string())
        })?;

        let enclosing_class_row = self.enclosing_class.ok_or_else(|| {
            Error::ModificationInvalid(
                "Enclosing class row is required for NestedClass".to_string(),
            )
        })?;

        // Note: Row validation is skipped for placeholders (high bit set)
        // Placeholders will be resolved during the write phase
        if nested_class_row == 0 {
            return Err(Error::ModificationInvalid(
                "Nested class row cannot be 0".to_string(),
            ));
        }

        if enclosing_class_row == 0 {
            return Err(Error::ModificationInvalid(
                "Enclosing class row cannot be 0".to_string(),
            ));
        }

        // Prevent self-nesting (only for non-placeholder values)
        if nested_class_row == enclosing_class_row {
            return Err(Error::ModificationInvalid(
                "A type cannot be nested within itself".to_string(),
            ));
        }

        let nested_class = NestedClassRaw {
            rid: 0,
            token: Token::new(0),
            offset: 0,
            nested_class: nested_class_row,
            enclosing_class: enclosing_class_row,
        };

        assembly.table_row_add(
            TableId::NestedClass,
            TableDataOwned::NestedClass(nested_class),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        cilassembly::ChangeRefKind,
        metadata::tables::{TableId, TypeAttributes},
        test::factories::table::assemblyref::get_test_assembly,
    };

    #[test]
    fn test_nested_class_builder_basic() -> Result<()> {
        let mut assembly = get_test_assembly()?;

        // Create TypeDefs for testing
        let outer_ref = crate::metadata::tables::TypeDefBuilder::new()
            .name("OuterClass")
            .public_class()
            .build(&mut assembly)?;

        let inner_ref = crate::metadata::tables::TypeDefBuilder::new()
            .name("InnerClass")
            .flags(TypeAttributes::NESTED_PUBLIC | TypeAttributes::CLASS)
            .build(&mut assembly)?;

        // Use placeholder values for the NestedClass builder
        let nested_ref = NestedClassBuilder::new()
            .nested_class(inner_ref.placeholder())
            .enclosing_class(outer_ref.placeholder())
            .build(&mut assembly)?;

        // Verify the reference has the correct table ID
        assert_eq!(
            nested_ref.kind(),
            ChangeRefKind::TableRow(TableId::NestedClass)
        );

        Ok(())
    }

    #[test]
    fn test_nested_class_builder_default() -> Result<()> {
        let builder = NestedClassBuilder::default();
        assert!(builder.nested_class.is_none());
        assert!(builder.enclosing_class.is_none());
        Ok(())
    }

    #[test]
    fn test_nested_class_builder_missing_nested_class() -> Result<()> {
        let mut assembly = get_test_assembly()?;

        // Create an enclosing type
        let outer_ref = crate::metadata::tables::TypeDefBuilder::new()
            .name("OuterClass")
            .public_class()
            .build(&mut assembly)?;

        let result = NestedClassBuilder::new()
            .enclosing_class(outer_ref.placeholder())
            .build(&mut assembly);

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Nested class row is required"));

        Ok(())
    }

    #[test]
    fn test_nested_class_builder_missing_enclosing_class() -> Result<()> {
        let mut assembly = get_test_assembly()?;

        // Create a nested type
        let inner_ref = crate::metadata::tables::TypeDefBuilder::new()
            .name("InnerClass")
            .flags(TypeAttributes::NESTED_PUBLIC | TypeAttributes::CLASS)
            .build(&mut assembly)?;

        let result = NestedClassBuilder::new()
            .nested_class(inner_ref.placeholder())
            .build(&mut assembly);

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Enclosing class row is required"));

        Ok(())
    }

    #[test]
    fn test_nested_class_builder_zero_nested_row() -> Result<()> {
        let mut assembly = get_test_assembly()?;

        // Create valid enclosing type
        let outer_ref = crate::metadata::tables::TypeDefBuilder::new()
            .name("OuterClass")
            .public_class()
            .build(&mut assembly)?;

        // Use zero row index which is invalid
        let result = NestedClassBuilder::new()
            .nested_class(0)
            .enclosing_class(outer_ref.placeholder())
            .build(&mut assembly);

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Nested class row cannot be 0"));

        Ok(())
    }

    #[test]
    fn test_nested_class_builder_zero_enclosing_row() -> Result<()> {
        let mut assembly = get_test_assembly()?;

        // Create valid nested type
        let inner_ref = crate::metadata::tables::TypeDefBuilder::new()
            .name("InnerClass")
            .flags(TypeAttributes::NESTED_PUBLIC | TypeAttributes::CLASS)
            .build(&mut assembly)?;

        // Use zero row index which is invalid
        let result = NestedClassBuilder::new()
            .nested_class(inner_ref.placeholder())
            .enclosing_class(0)
            .build(&mut assembly);

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Enclosing class row cannot be 0"));

        Ok(())
    }

    #[test]
    fn test_nested_class_builder_self_nesting() -> Result<()> {
        let mut assembly = get_test_assembly()?;

        // Create a type
        let type_ref = crate::metadata::tables::TypeDefBuilder::new()
            .name("SelfNestingClass")
            .public_class()
            .build(&mut assembly)?;

        let type_placeholder = type_ref.placeholder();

        // Try to nest it within itself
        let result = NestedClassBuilder::new()
            .nested_class(type_placeholder)
            .enclosing_class(type_placeholder)
            .build(&mut assembly);

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("A type cannot be nested within itself"));

        Ok(())
    }

    #[test]
    fn test_nested_class_builder_zero_row_nested() -> Result<()> {
        let mut assembly = get_test_assembly()?;

        // Create valid enclosing type
        let outer_ref = crate::metadata::tables::TypeDefBuilder::new()
            .name("OuterClass")
            .public_class()
            .build(&mut assembly)?;

        // Use a zero row value
        let result = NestedClassBuilder::new()
            .nested_class(0)
            .enclosing_class(outer_ref.placeholder())
            .build(&mut assembly);

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Nested class row cannot be 0"));

        Ok(())
    }

    #[test]
    fn test_nested_class_builder_zero_row_enclosing() -> Result<()> {
        let mut assembly = get_test_assembly()?;

        // Create valid nested type
        let inner_ref = crate::metadata::tables::TypeDefBuilder::new()
            .name("InnerClass")
            .flags(TypeAttributes::NESTED_PUBLIC | TypeAttributes::CLASS)
            .build(&mut assembly)?;

        // Use a zero row value
        let result = NestedClassBuilder::new()
            .nested_class(inner_ref.placeholder())
            .enclosing_class(0)
            .build(&mut assembly);

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Enclosing class row cannot be 0"));

        Ok(())
    }

    #[test]
    fn test_nested_class_builder_multiple_relationships() -> Result<()> {
        let mut assembly = get_test_assembly()?;

        // Create an outer class
        let outer_ref = crate::metadata::tables::TypeDefBuilder::new()
            .name("OuterClass")
            .public_class()
            .build(&mut assembly)?;

        // Create two inner classes
        let inner1_ref = crate::metadata::tables::TypeDefBuilder::new()
            .name("InnerClass1")
            .flags(TypeAttributes::NESTED_PUBLIC | TypeAttributes::CLASS)
            .build(&mut assembly)?;

        let inner2_ref = crate::metadata::tables::TypeDefBuilder::new()
            .name("InnerClass2")
            .flags(TypeAttributes::NESTED_PUBLIC | TypeAttributes::CLASS)
            .build(&mut assembly)?;

        // Create nesting relationships
        let nesting1_ref = NestedClassBuilder::new()
            .nested_class(inner1_ref.placeholder())
            .enclosing_class(outer_ref.placeholder())
            .build(&mut assembly)?;

        let nesting2_ref = NestedClassBuilder::new()
            .nested_class(inner2_ref.placeholder())
            .enclosing_class(outer_ref.placeholder())
            .build(&mut assembly)?;

        // Verify references are different and have correct kind
        assert!(!std::sync::Arc::ptr_eq(&nesting1_ref, &nesting2_ref));
        assert_eq!(
            nesting1_ref.kind(),
            ChangeRefKind::TableRow(TableId::NestedClass)
        );
        assert_eq!(
            nesting2_ref.kind(),
            ChangeRefKind::TableRow(TableId::NestedClass)
        );

        Ok(())
    }

    #[test]
    fn test_nested_class_builder_deep_nesting() -> Result<()> {
        let mut assembly = get_test_assembly()?;

        // Create a hierarchy: Outer -> Middle -> Inner
        let outer_ref = crate::metadata::tables::TypeDefBuilder::new()
            .name("OuterClass")
            .public_class()
            .build(&mut assembly)?;

        let middle_ref = crate::metadata::tables::TypeDefBuilder::new()
            .name("MiddleClass")
            .flags(TypeAttributes::NESTED_PUBLIC | TypeAttributes::CLASS)
            .build(&mut assembly)?;

        let inner_ref = crate::metadata::tables::TypeDefBuilder::new()
            .name("InnerClass")
            .flags(TypeAttributes::NESTED_PUBLIC | TypeAttributes::CLASS)
            .build(&mut assembly)?;

        // Create the nesting relationships
        let nesting1_ref = NestedClassBuilder::new()
            .nested_class(middle_ref.placeholder())
            .enclosing_class(outer_ref.placeholder())
            .build(&mut assembly)?;

        let nesting2_ref = NestedClassBuilder::new()
            .nested_class(inner_ref.placeholder())
            .enclosing_class(middle_ref.placeholder())
            .build(&mut assembly)?;

        assert_eq!(
            nesting1_ref.kind(),
            ChangeRefKind::TableRow(TableId::NestedClass)
        );
        assert_eq!(
            nesting2_ref.kind(),
            ChangeRefKind::TableRow(TableId::NestedClass)
        );

        Ok(())
    }

    #[test]
    fn test_nested_class_builder_fluent_api() -> Result<()> {
        let mut assembly = get_test_assembly()?;

        // Create types for testing
        let outer_ref = crate::metadata::tables::TypeDefBuilder::new()
            .name("FluentOuter")
            .public_class()
            .build(&mut assembly)?;

        let inner_ref = crate::metadata::tables::TypeDefBuilder::new()
            .name("FluentInner")
            .flags(TypeAttributes::NESTED_PUBLIC | TypeAttributes::CLASS)
            .build(&mut assembly)?;

        // Test fluent API chaining
        let nested_ref = NestedClassBuilder::new()
            .nested_class(inner_ref.placeholder())
            .enclosing_class(outer_ref.placeholder())
            .build(&mut assembly)?;

        assert_eq!(
            nested_ref.kind(),
            ChangeRefKind::TableRow(TableId::NestedClass)
        );

        Ok(())
    }

    #[test]
    fn test_nested_class_builder_clone() {
        let nested_row = 1u32;
        let enclosing_row = 2u32;

        let builder1 = NestedClassBuilder::new()
            .nested_class(nested_row)
            .enclosing_class(enclosing_row);
        let builder2 = builder1.clone();

        assert_eq!(builder1.nested_class, builder2.nested_class);
        assert_eq!(builder1.enclosing_class, builder2.enclosing_class);
    }

    #[test]
    fn test_nested_class_builder_debug() {
        let nested_row = 1u32;
        let enclosing_row = 2u32;

        let builder = NestedClassBuilder::new()
            .nested_class(nested_row)
            .enclosing_class(enclosing_row);
        let debug_str = format!("{builder:?}");
        assert!(debug_str.contains("NestedClassBuilder"));
    }
}