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
//! # PropertyMap Builder
//!
//! Provides a fluent API for building PropertyMap table entries that establish ownership relationships
//! between types and their properties. The PropertyMap table defines contiguous ranges of properties that
//! belong to specific types, enabling efficient enumeration and lookup of properties by owning type.
//!
//! ## Overview
//!
//! The `PropertyMapBuilder` enables creation of property map entries with:
//! - Parent type specification (required)
//! - Property list starting index specification (required)
//! - Validation of type row indices and property indices
//! - Automatic token generation and metadata management
//!
//! ## Usage
//!
//! ```rust,ignore
//! # 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 a type first
//! let type_ref = TypeDefBuilder::new()
//!     .name("MyClass")
//!     .namespace("MyApp")
//!     .public_class()
//!     .build(&mut assembly)?;
//!
//! // Create property signatures
//! let string_property_sig = &[0x08, 0x1C]; // PROPERTY calling convention + ELEMENT_TYPE_OBJECT
//! let int_property_sig = &[0x08, 0x08]; // PROPERTY calling convention + ELEMENT_TYPE_I4
//!
//! // Create properties
//! let prop1_ref = PropertyBuilder::new()
//!     .name("Name")
//!     .signature(string_property_sig)
//!     .build(&mut assembly)?;
//!
//! let prop2_ref = PropertyBuilder::new()
//!     .name("Count")
//!     .signature(int_property_sig)
//!     .build(&mut assembly)?;
//!
//! // Create a property map entry for the type
//! let property_map_ref = PropertyMapBuilder::new()
//!     .parent(type_ref.placeholder())
//!     .property_list(prop1_ref.placeholder()) // Starting property index
//!     .build(&mut assembly)?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Design
//!
//! The builder follows the established pattern with:
//! - **Validation**: Parent type and property list index are required and validated
//! - **Type Verification**: Ensures parent token is valid and points to TypeDef table
//! - **Token Generation**: Metadata tokens are created automatically
//! - **Range Support**: Supports defining contiguous property ranges for efficient lookup

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

/// Builder for creating PropertyMap table entries.
///
/// `PropertyMapBuilder` provides a fluent API for creating entries in the PropertyMap
/// metadata table, which establishes ownership relationships between types and their properties
/// through contiguous ranges of Property table entries.
///
/// # Purpose
///
/// The PropertyMap table serves several key functions:
/// - **Property Ownership**: Defines which types own which properties
/// - **Range Management**: Establishes contiguous ranges of properties owned by types
/// - **Efficient Lookup**: Enables O(log n) lookup of properties by owning type
/// - **Property Enumeration**: Supports efficient iteration through all properties of a type
/// - **Metadata Organization**: Maintains sorted order for optimal access patterns
///
/// # Builder Pattern
///
/// The builder provides a fluent interface for constructing PropertyMap entries:
///
/// ```rust,ignore
/// # use dotscope::prelude::*;
/// # use std::path::Path;
/// # let view = CilAssemblyView::from_path(Path::new("test.dll"))?;
/// # let mut assembly = CilAssembly::new(view);
/// # let type_ref = assembly.placeholder();
///
/// let property_map_ref = PropertyMapBuilder::new()
///     .parent(type_ref.placeholder())
///     .property_list(1) // Starting property index
///     .build(&mut assembly)?;
/// # Ok::<(), dotscope::Error>(())
/// ```
///
/// # Validation
///
/// The builder enforces the following constraints:
/// - **Parent Required**: A parent type row index must be provided
/// - **Parent Validation**: Parent row index cannot be 0
/// - **Property List Required**: A property list starting index must be provided
/// - **Index Validation**: Property list index must be greater than 0
///
/// # Integration
///
/// PropertyMap entries integrate with other metadata structures:
/// - **TypeDef**: References specific types in the TypeDef table as parent
/// - **Property**: Points to starting positions in the Property table for range definition
/// - **PropertyPtr**: Supports indirection through PropertyPtr table when present
/// - **Metadata Loading**: Establishes property ownership during type loading
#[derive(Debug, Clone)]
pub struct PropertyMapBuilder {
    /// The row index or placeholder of the parent type that owns the properties
    parent: Option<u32>,
    /// The starting index in the Property table for this type's properties
    property_list: Option<u32>,
}

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

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

    /// Sets the parent type row index that owns the properties.
    ///
    /// The parent must be a valid TypeDef row index that represents the type
    /// that declares and owns the properties in the specified range.
    ///
    /// # Arguments
    ///
    /// * `parent_row` - Row index or placeholder of the TypeDef table entry
    ///
    /// # 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 type_ref = TypeDefBuilder::new()
    ///     .name("PropertyfulClass")
    ///     .namespace("MyApp")
    ///     .public_class()
    ///     .build(&mut assembly)?;
    ///
    /// let builder = PropertyMapBuilder::new()
    ///     .parent(type_ref.placeholder());
    /// # Ok::<(), dotscope::Error>(())
    /// ```
    #[must_use]
    pub fn parent(mut self, parent_row: u32) -> Self {
        self.parent = Some(parent_row);
        self
    }

    /// Sets the starting index in the Property table for this type's properties.
    ///
    /// This index defines the beginning of the contiguous range of properties
    /// owned by the parent type. The range extends to the next PropertyMap entry's
    /// property_list index (or end of Property table for the final entry).
    ///
    /// # Arguments
    ///
    /// * `property_list_index` - 1-based index into the Property table
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use dotscope::prelude::*;
    /// let builder = PropertyMapBuilder::new()
    ///     .property_list(1); // Start from first property
    /// ```
    #[must_use]
    pub fn property_list(mut self, property_list_index: u32) -> Self {
        self.property_list = Some(property_list_index);
        self
    }

    /// Builds the PropertyMap entry and adds it to the assembly.
    ///
    /// This method validates all required fields, verifies the parent row is valid,
    /// validates the property list index, creates the PropertyMap table entry, and returns the
    /// change reference for the new entry.
    ///
    /// # Arguments
    ///
    /// * `assembly` - The CilAssembly being modified
    ///
    /// # Returns
    ///
    /// Returns the change reference for the newly created PropertyMap entry.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The parent row index is not set
    /// - The parent row index is 0
    /// - The property list index is not set
    /// - The property list index is 0
    /// - There are issues adding the table row
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// # use dotscope::prelude::*;
    /// # use std::path::Path;
    /// # let view = CilAssemblyView::from_path(Path::new("test.dll"))?;
    /// # let mut assembly = CilAssembly::new(view);
    /// # let type_ref = assembly.placeholder();
    ///
    /// let property_map_ref = PropertyMapBuilder::new()
    ///     .parent(type_ref.placeholder())
    ///     .property_list(1)
    ///     .build(&mut assembly)?;
    ///
    /// println!("Created PropertyMap with placeholder: {}", property_map_ref.placeholder());
    /// # Ok::<(), dotscope::Error>(())
    /// ```
    pub fn build(self, assembly: &mut CilAssembly) -> Result<ChangeRefRc> {
        let parent_row = self.parent.ok_or_else(|| {
            Error::ModificationInvalid("Parent row index is required for PropertyMap".to_string())
        })?;

        let property_list_index = self.property_list.ok_or_else(|| {
            Error::ModificationInvalid(
                "Property list index is required for PropertyMap".to_string(),
            )
        })?;

        if parent_row == 0 {
            return Err(Error::ModificationInvalid(
                "Parent row index cannot be 0".to_string(),
            ));
        }

        if property_list_index == 0 {
            return Err(Error::ModificationInvalid(
                "Property list index cannot be 0".to_string(),
            ));
        }

        let property_map = PropertyMapRaw {
            rid: 0,
            token: Token::new(0),
            offset: 0,
            parent: parent_row,
            property_list: property_list_index,
        };

        assembly.table_row_add(
            TableId::PropertyMap,
            TableDataOwned::PropertyMap(property_map),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test::factories::table::assemblyref::get_test_assembly;
    use std::sync::Arc;

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

        // Use a valid TypeDef row index for testing
        let type_row = 1u32;

        let _change_ref = PropertyMapBuilder::new()
            .parent(type_row)
            .property_list(1)
            .build(&mut assembly)?;

        Ok(())
    }

    #[test]
    fn test_property_map_builder_default() -> Result<()> {
        let builder = PropertyMapBuilder::default();
        assert!(builder.parent.is_none());
        assert!(builder.property_list.is_none());
        Ok(())
    }

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

        let result = PropertyMapBuilder::new()
            .property_list(1)
            .build(&mut assembly);

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

        Ok(())
    }

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

        // Use a valid TypeDef row index
        let type_row = 1u32;

        let result = PropertyMapBuilder::new()
            .parent(type_row)
            .build(&mut assembly);

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Property list index is required"));

        Ok(())
    }

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

        // Use a zero row index
        let zero_row = 0u32;

        let result = PropertyMapBuilder::new()
            .parent(zero_row)
            .property_list(1)
            .build(&mut assembly);

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

        Ok(())
    }

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

        // Use a valid TypeDef row index
        let type_row = 1u32;

        let result = PropertyMapBuilder::new()
            .parent(type_row)
            .property_list(0) // Zero property list index is invalid
            .build(&mut assembly);

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Property list index cannot be 0"));

        Ok(())
    }

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

        // Use valid TypeDef row indices
        let type1_row = 1u32;
        let type2_row = 2u32;

        let ref1 = PropertyMapBuilder::new()
            .parent(type1_row)
            .property_list(1)
            .build(&mut assembly)?;

        let ref2 = PropertyMapBuilder::new()
            .parent(type2_row)
            .property_list(3)
            .build(&mut assembly)?;

        // Verify change refs are different
        assert!(!Arc::ptr_eq(&ref1, &ref2));

        Ok(())
    }

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

        // Test with different property list indices
        let test_indices = [1, 5, 10, 20, 100];

        for (i, &index) in test_indices.iter().enumerate() {
            let type_row = 1u32 + i as u32;

            let _change_ref = PropertyMapBuilder::new()
                .parent(type_row)
                .property_list(index)
                .build(&mut assembly)?;
        }

        Ok(())
    }

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

        // Use a valid TypeDef row index
        let type_row = 1u32;

        // Test fluent API chaining
        let _change_ref = PropertyMapBuilder::new()
            .parent(type_row)
            .property_list(5)
            .build(&mut assembly)?;

        Ok(())
    }

    #[test]
    fn test_property_map_builder_clone() {
        let parent_row = 1u32;

        let builder1 = PropertyMapBuilder::new()
            .parent(parent_row)
            .property_list(1);
        let builder2 = builder1.clone();

        assert_eq!(builder1.parent, builder2.parent);
        assert_eq!(builder1.property_list, builder2.property_list);
    }

    #[test]
    fn test_property_map_builder_debug() {
        let parent_row = 1u32;

        let builder = PropertyMapBuilder::new()
            .parent(parent_row)
            .property_list(1);
        let debug_str = format!("{builder:?}");
        assert!(debug_str.contains("PropertyMapBuilder"));
    }
}