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
//! Implementation of `RowWritable` for `InterfaceImplRaw` metadata table entries.
//!
//! This module provides binary serialization support for the `InterfaceImpl` table (ID 0x09),
//! enabling writing of interface implementation metadata back to .NET PE files. The InterfaceImpl table
//! defines which interfaces are implemented by which types, including both true interface
//! implementations and interface-to-interface inheritance relationships.
//!
//! ## Table Structure (ECMA-335 §II.22.23)
//!
//! | Field | Type | Description |
//! |-------|------|-------------|
//! | `Class` | `TypeDef` table index | Type that implements the interface |
//! | `Interface` | `TypeDefOrRef` coded index | Interface being implemented |
//!
//! ## Interface Implementation Types
//!
//! The InterfaceImpl table handles both:
//! - **Interface Implementation**: Classes implementing interfaces
//! - **Interface Inheritance**: Interfaces extending other interfaces (compiler quirk)

use crate::{
    metadata::tables::{
        interfaceimpl::InterfaceImplRaw,
        types::{CodedIndexType, RowWritable, TableId, TableInfoRef},
    },
    utils::write_le_at_dyn,
    Result,
};

impl RowWritable for InterfaceImplRaw {
    /// Write an InterfaceImpl table row to binary data
    ///
    /// Serializes one InterfaceImpl table entry to the metadata tables stream format, handling
    /// variable-width indexes based on the table size information.
    ///
    /// # Field Serialization Order (ECMA-335)
    /// 1. `class` - `TypeDef` table index (2 or 4 bytes)
    /// 2. `interface` - `TypeDefOrRef` coded index (2 or 4 bytes)
    ///
    /// # Arguments
    /// * `data` - Target binary buffer for metadata tables stream
    /// * `offset` - Current write position (updated after writing)
    /// * `rid` - Row identifier (unused for InterfaceImpl serialization)
    /// * `sizes` - Table size information for determining index widths
    ///
    /// # Returns
    /// `Ok(())` on successful serialization, error if buffer is too small
    ///
    /// # Errors
    /// Returns an error if:
    /// - The target buffer is too small for the row data
    /// - The coded index cannot be encoded
    fn row_write(
        &self,
        data: &mut [u8],
        offset: &mut usize,
        _rid: u32,
        sizes: &TableInfoRef,
    ) -> Result<()> {
        // Write class TypeDef table index (2 or 4 bytes)
        write_le_at_dyn(data, offset, self.class, sizes.is_large(TableId::TypeDef))?;

        // Write interface coded index (2 or 4 bytes)
        let encoded_interface = sizes.encode_coded_index(
            self.interface.tag,
            self.interface.row,
            CodedIndexType::TypeDefOrRef,
        )?;
        write_le_at_dyn(
            data,
            offset,
            encoded_interface,
            sizes.coded_index_bits(CodedIndexType::TypeDefOrRef) > 16,
        )?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        metadata::tables::{
            types::{RowReadable, TableInfo, TableRow},
            CodedIndex, TableId,
        },
        metadata::token::Token,
    };
    use std::sync::Arc;

    #[test]
    fn test_row_size() {
        // Test with small tables
        let table_info = Arc::new(TableInfo::new_test(
            &[
                (TableId::TypeDef, 100),
                (TableId::TypeRef, 100),
                (TableId::TypeSpec, 100),
            ],
            false,
            false,
            false,
        ));

        let size = <InterfaceImplRaw as TableRow>::row_size(&table_info);
        // class(2) + interface(2) = 4
        assert_eq!(size, 4);

        // Test with large tables
        let table_info_large = Arc::new(TableInfo::new_test(
            &[
                (TableId::TypeDef, 70000),
                (TableId::TypeRef, 70000),
                (TableId::TypeSpec, 70000),
            ],
            false,
            false,
            false,
        ));

        let size_large = <InterfaceImplRaw as TableRow>::row_size(&table_info_large);
        // class(4) + interface(4) = 8
        assert_eq!(size_large, 8);
    }

    #[test]
    fn test_round_trip_serialization() {
        // Create test data using same values as reader tests
        let original_row = InterfaceImplRaw {
            rid: 1,
            token: Token::new(0x09000001),
            offset: 0,
            class: 0x0101,
            interface: CodedIndex::new(TableId::TypeSpec, 0x80, CodedIndexType::TypeDefOrRef),
        };

        // Create minimal table info for testing
        let table_info = Arc::new(TableInfo::new_test(
            &[
                (TableId::TypeDef, 1000),
                (TableId::TypeRef, 1000),
                (TableId::TypeSpec, 1000),
            ],
            false,
            false,
            false,
        ));

        // Calculate buffer size and serialize
        let row_size = <InterfaceImplRaw as TableRow>::row_size(&table_info) as usize;
        let mut buffer = vec![0u8; row_size];
        let mut offset = 0;

        original_row
            .row_write(&mut buffer, &mut offset, 1, &table_info)
            .expect("Serialization should succeed");

        // Deserialize and verify round-trip
        let mut read_offset = 0;
        let deserialized_row =
            InterfaceImplRaw::row_read(&buffer, &mut read_offset, 1, &table_info)
                .expect("Deserialization should succeed");

        assert_eq!(deserialized_row.rid, original_row.rid);
        assert_eq!(deserialized_row.class, original_row.class);
        assert_eq!(deserialized_row.interface, original_row.interface);
        assert_eq!(offset, row_size, "Offset should match expected row size");
    }

    #[test]
    fn test_known_binary_format_small() {
        // Test with known binary data from reader tests
        let data = vec![
            0x01, 0x01, // class (0x0101)
            0x02, 0x02, // interface
        ];

        let table_info = Arc::new(TableInfo::new_test(
            &[(TableId::InterfaceImpl, 1)],
            false,
            false,
            false,
        ));

        // First read the original data to get a reference row
        let mut read_offset = 0;
        let reference_row = InterfaceImplRaw::row_read(&data, &mut read_offset, 1, &table_info)
            .expect("Reading reference data should succeed");

        // Now serialize and verify we get the same binary data
        let mut buffer = vec![0u8; data.len()];
        let mut write_offset = 0;
        reference_row
            .row_write(&mut buffer, &mut write_offset, 1, &table_info)
            .expect("Serialization should succeed");

        assert_eq!(
            buffer, data,
            "Serialized data should match original binary format"
        );
    }

    #[test]
    fn test_known_binary_format_large() {
        // Test with known binary data from reader tests (large variant)
        let data = vec![
            0x01, 0x01, 0x01, 0x01, // class (0x01010101)
            0x02, 0x02, 0x02, 0x02, // interface
        ];

        let table_info = Arc::new(TableInfo::new_test(
            &[(TableId::TypeDef, u16::MAX as u32 + 2)],
            true,
            true,
            true,
        ));

        // First read the original data to get a reference row
        let mut read_offset = 0;
        let reference_row = InterfaceImplRaw::row_read(&data, &mut read_offset, 1, &table_info)
            .expect("Reading reference data should succeed");

        // Now serialize and verify we get the same binary data
        let mut buffer = vec![0u8; data.len()];
        let mut write_offset = 0;
        reference_row
            .row_write(&mut buffer, &mut write_offset, 1, &table_info)
            .expect("Serialization should succeed");

        assert_eq!(
            buffer, data,
            "Serialized data should match original binary format"
        );
    }

    #[test]
    fn test_coded_index_types() {
        // Test different coded index target types
        let test_cases = vec![
            (TableId::TypeDef, "TypeDef"),
            (TableId::TypeRef, "TypeRef"),
            (TableId::TypeSpec, "TypeSpec"),
        ];

        let table_info = Arc::new(TableInfo::new_test(
            &[
                (TableId::TypeDef, 100),
                (TableId::TypeRef, 100),
                (TableId::TypeSpec, 100),
            ],
            false,
            false,
            false,
        ));

        for (table_id, description) in test_cases {
            let interface_impl_row = InterfaceImplRaw {
                rid: 1,
                token: Token::new(0x09000001),
                offset: 0,
                class: 1,
                interface: CodedIndex::new(table_id, 1, CodedIndexType::TypeDefOrRef),
            };

            let row_size = <InterfaceImplRaw as TableRow>::row_size(&table_info) as usize;
            let mut buffer = vec![0u8; row_size];
            let mut offset = 0;

            interface_impl_row
                .row_write(&mut buffer, &mut offset, 1, &table_info)
                .unwrap_or_else(|_| panic!("Serialization should succeed for {description}"));

            // Verify round-trip
            let mut read_offset = 0;
            let deserialized_row =
                InterfaceImplRaw::row_read(&buffer, &mut read_offset, 1, &table_info)
                    .unwrap_or_else(|_| panic!("Deserialization should succeed for {description}"));

            assert_eq!(
                deserialized_row.interface.tag, interface_impl_row.interface.tag,
                "Interface type tag should match for {description}"
            );
        }
    }

    #[test]
    fn test_large_table_serialization() {
        // Test with large tables to ensure 4-byte indexes are handled correctly
        let original_row = InterfaceImplRaw {
            rid: 1,
            token: Token::new(0x09000001),
            offset: 0,
            class: 0x12345,
            interface: CodedIndex::new(TableId::TypeRef, 0x8000, CodedIndexType::TypeDefOrRef),
        };

        let table_info = Arc::new(TableInfo::new_test(
            &[
                (TableId::TypeDef, 70000),
                (TableId::TypeRef, 70000),
                (TableId::TypeSpec, 70000),
            ],
            false,
            false,
            false,
        ));

        let row_size = <InterfaceImplRaw as TableRow>::row_size(&table_info) as usize;
        let mut buffer = vec![0u8; row_size];
        let mut offset = 0;

        original_row
            .row_write(&mut buffer, &mut offset, 1, &table_info)
            .expect("Large table serialization should succeed");

        // Verify round-trip
        let mut read_offset = 0;
        let deserialized_row =
            InterfaceImplRaw::row_read(&buffer, &mut read_offset, 1, &table_info)
                .expect("Large table deserialization should succeed");

        assert_eq!(deserialized_row.class, original_row.class);
        assert_eq!(deserialized_row.interface, original_row.interface);
    }

    #[test]
    fn test_edge_cases() {
        // Test with minimal values
        let minimal_interface_impl = InterfaceImplRaw {
            rid: 1,
            token: Token::new(0x09000001),
            offset: 0,
            class: 1, // First type
            interface: CodedIndex::new(TableId::TypeDef, 1, CodedIndexType::TypeDefOrRef),
        };

        let table_info = Arc::new(TableInfo::new_test(
            &[
                (TableId::TypeDef, 100),
                (TableId::TypeRef, 100),
                (TableId::TypeSpec, 100),
            ],
            false,
            false,
            false,
        ));

        let row_size = <InterfaceImplRaw as TableRow>::row_size(&table_info) as usize;
        let mut buffer = vec![0u8; row_size];
        let mut offset = 0;

        minimal_interface_impl
            .row_write(&mut buffer, &mut offset, 1, &table_info)
            .expect("Minimal interface impl serialization should succeed");

        // Verify round-trip
        let mut read_offset = 0;
        let deserialized_row =
            InterfaceImplRaw::row_read(&buffer, &mut read_offset, 1, &table_info)
                .expect("Minimal interface impl deserialization should succeed");

        assert_eq!(deserialized_row.class, minimal_interface_impl.class);
        assert_eq!(deserialized_row.interface, minimal_interface_impl.interface);
    }

    #[test]
    fn test_different_table_combinations() {
        // Test with different combinations of table sizes
        let interface_impl_row = InterfaceImplRaw {
            rid: 1,
            token: Token::new(0x09000001),
            offset: 0,
            class: 0x8000,
            interface: CodedIndex::new(TableId::TypeDef, 0x4000, CodedIndexType::TypeDefOrRef),
        };

        // Test combinations: (large_typedef, large_other_tables, expected_size)
        let test_cases = vec![
            (1000, 1000, 4),   // small typedef, small coded: 2+2 = 4
            (70000, 1000, 8),  // large typedef, large coded (due to typedef): 4+4 = 8
            (1000, 70000, 6),  // small typedef, large coded: 2+4 = 6
            (70000, 70000, 8), // large typedef, large coded: 4+4 = 8
        ];

        for (typedef_size, other_size, expected_size) in test_cases {
            let table_info = Arc::new(TableInfo::new_test(
                &[
                    (TableId::TypeDef, typedef_size),
                    (TableId::TypeRef, other_size),
                    (TableId::TypeSpec, other_size),
                ],
                false, // string heap size doesn't matter
                false, // blob heap size doesn't matter
                false, // guid heap size doesn't matter
            ));

            let size = <InterfaceImplRaw as TableRow>::row_size(&table_info) as usize;
            assert_eq!(
                size, expected_size,
                "Row size should be {expected_size} for typedef_size={typedef_size}, other_size={other_size}"
            );

            let mut buffer = vec![0u8; size];
            let mut offset = 0;

            interface_impl_row
                .row_write(&mut buffer, &mut offset, 1, &table_info)
                .expect("Serialization should succeed");

            // Verify round-trip
            let mut read_offset = 0;
            let deserialized_row =
                InterfaceImplRaw::row_read(&buffer, &mut read_offset, 1, &table_info)
                    .expect("Deserialization should succeed");

            assert_eq!(deserialized_row.class, interface_impl_row.class);
            assert_eq!(
                deserialized_row.interface.tag,
                interface_impl_row.interface.tag
            );
        }
    }
}