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
//! Writer implementation for `LocalScope` metadata table.
//!
//! This module provides the [`RowWritable`] trait implementation for the
//! [`LocalScopeRaw`] struct, enabling serialization of local scope information
//! rows back to binary format. This supports Portable PDB generation and
//! assembly modification scenarios where debug information needs to be preserved.
//!
//! # Binary Format
//!
//! Each `LocalScope` row consists of six fields:
//! - `method` (2/4 bytes): Simple index into MethodDef table
//! - `import_scope` (2/4 bytes): Simple index into ImportScope table (0 = no import scope)
//! - `variable_list` (2/4 bytes): Simple index into LocalVariable table (0 = no variables)
//! - `constant_list` (2/4 bytes): Simple index into LocalConstant table (0 = no constants)
//! - `start_offset` (4 bytes): IL instruction offset where scope begins
//! - `length` (4 bytes): Length of scope in IL instruction bytes
//!
//! # Row Layout
//!
//! `LocalScope` table rows are serialized with this binary structure:
//! - Method table index (2 or 4 bytes, depending on MethodDef table size)
//! - ImportScope table index (2 or 4 bytes, depending on ImportScope table size)
//! - LocalVariable table index (2 or 4 bytes, depending on LocalVariable table size)
//! - LocalConstant table index (2 or 4 bytes, depending on LocalConstant table size)
//! - Start offset (4 bytes, little-endian)
//! - Length (4 bytes, little-endian)
//! - Total row size varies based on table sizes
//!
//! # Architecture
//!
//! This implementation provides efficient serialization by writing data directly to the
//! target buffer without intermediate allocations. Index sizes are determined dynamically
//! based on the actual table sizes, matching the compression scheme used in .NET metadata.
//!
//! The writer maintains strict compatibility with the [`crate::metadata::tables::localscope::reader`]
//! module, ensuring that data serialized by this writer can be correctly deserialized.

use crate::{
    metadata::tables::{
        localscope::LocalScopeRaw,
        types::{RowWritable, TableInfoRef},
        TableId,
    },
    utils::{write_le_at, write_le_at_dyn},
    Result,
};

impl RowWritable for LocalScopeRaw {
    /// Write a `LocalScope` table row to binary data
    ///
    /// Serializes one `LocalScope` table entry to the metadata tables stream format, handling
    /// variable-width table indexes based on the table size information.
    ///
    /// # Arguments
    /// * `data` - Target binary buffer for metadata tables stream
    /// * `offset` - Current write position (updated after writing)
    /// * `_rid` - Row identifier for this local scope entry (unused for `LocalScope`)
    /// * `sizes` - Table sizing information for writing table indexes
    ///
    /// # Returns
    /// * `Ok(())` - Successfully serialized local scope row
    /// * `Err(`[`crate::Error`]`)` - If buffer is too small or write fails
    ///
    /// # Binary Format
    /// Fields are written in the exact order specified by the Portable PDB specification:
    /// 1. Method table index (2/4 bytes, little-endian)
    /// 2. ImportScope table index (2/4 bytes, little-endian, 0 = no import scope)
    /// 3. LocalVariable table index (2/4 bytes, little-endian, 0 = no variables)
    /// 4. LocalConstant table index (2/4 bytes, little-endian, 0 = no constants)
    /// 5. Start offset (4 bytes, little-endian)
    /// 6. Length (4 bytes, little-endian)
    fn row_write(
        &self,
        data: &mut [u8],
        offset: &mut usize,
        _rid: u32,
        sizes: &TableInfoRef,
    ) -> Result<()> {
        // Write table indices
        write_le_at_dyn(
            data,
            offset,
            self.method,
            sizes.is_large(TableId::MethodDef),
        )?;
        write_le_at_dyn(
            data,
            offset,
            self.import_scope,
            sizes.is_large(TableId::ImportScope),
        )?;
        write_le_at_dyn(
            data,
            offset,
            self.variable_list,
            sizes.is_large(TableId::LocalVariable),
        )?;
        write_le_at_dyn(
            data,
            offset,
            self.constant_list,
            sizes.is_large(TableId::LocalConstant),
        )?;

        // Write fixed-size offset fields
        write_le_at::<u32>(data, offset, self.start_offset)?;
        write_le_at::<u32>(data, offset, self.length)?;

        Ok(())
    }
}

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

    #[test]
    fn test_round_trip_serialization_small_indices() {
        // Create test data with small table indices
        let original_row = LocalScopeRaw {
            rid: 1,
            token: Token::new(0x3200_0001),
            offset: 0,
            method: 5,
            import_scope: 3,
            variable_list: 10,
            constant_list: 7,
            start_offset: 0x1000,
            length: 0x500,
        };

        let table_info = std::sync::Arc::new(TableInfo::new_test(
            &[
                (crate::metadata::tables::TableId::MethodDef, 100),
                (crate::metadata::tables::TableId::ImportScope, 50),
                (crate::metadata::tables::TableId::LocalVariable, 200),
                (crate::metadata::tables::TableId::LocalConstant, 75),
            ],
            false,
            false,
            false,
        ));

        // Calculate buffer size and serialize
        let row_size = <LocalScopeRaw 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 = LocalScopeRaw::row_read(&buffer, &mut read_offset, 1, &table_info)
            .expect("Deserialization should succeed");

        // Compare all fields
        assert_eq!(original_row.method, deserialized_row.method);
        assert_eq!(original_row.import_scope, deserialized_row.import_scope);
        assert_eq!(original_row.variable_list, deserialized_row.variable_list);
        assert_eq!(original_row.constant_list, deserialized_row.constant_list);
        assert_eq!(original_row.start_offset, deserialized_row.start_offset);
        assert_eq!(original_row.length, deserialized_row.length);
        assert_eq!(offset, row_size, "Offset should match expected row size");
        assert_eq!(
            read_offset, row_size,
            "Read offset should match expected row size"
        );
    }

    #[test]
    fn test_round_trip_serialization_large_indices() {
        // Create test data with large table indices
        let original_row = LocalScopeRaw {
            rid: 2,
            token: Token::new(0x3200_0002),
            offset: 0,
            method: 0x1BEEF,
            import_scope: 0x2CAFE,
            variable_list: 0x3DEAD,
            constant_list: 0x4FACE,
            start_offset: 0x12345678,
            length: 0x9ABCDEF0,
        };

        let table_info = std::sync::Arc::new(TableInfo::new_test(
            &[
                (crate::metadata::tables::TableId::MethodDef, 100000),
                (crate::metadata::tables::TableId::ImportScope, 100000),
                (crate::metadata::tables::TableId::LocalVariable, 100000),
                (crate::metadata::tables::TableId::LocalConstant, 100000),
            ],
            true,
            true,
            true,
        ));

        // Calculate buffer size and serialize
        let row_size = <LocalScopeRaw 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, 2, &table_info)
            .expect("Serialization should succeed");

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

        // Compare all fields
        assert_eq!(original_row.method, deserialized_row.method);
        assert_eq!(original_row.import_scope, deserialized_row.import_scope);
        assert_eq!(original_row.variable_list, deserialized_row.variable_list);
        assert_eq!(original_row.constant_list, deserialized_row.constant_list);
        assert_eq!(original_row.start_offset, deserialized_row.start_offset);
        assert_eq!(original_row.length, deserialized_row.length);
        assert_eq!(offset, row_size, "Offset should match expected row size");
        assert_eq!(
            read_offset, row_size,
            "Read offset should match expected row size"
        );
    }

    #[test]
    fn test_known_binary_format_small_indices() {
        // Test with specific binary layout for small indices
        let local_scope = LocalScopeRaw {
            rid: 1,
            token: Token::new(0x3200_0001),
            offset: 0,
            method: 0x1234,
            import_scope: 0x5678,
            variable_list: 0x9ABC,
            constant_list: 0xDEF0,
            start_offset: 0x11223344,
            length: 0x55667788,
        };

        let table_info = std::sync::Arc::new(TableInfo::new_test(
            &[
                (crate::metadata::tables::TableId::MethodDef, 100),
                (crate::metadata::tables::TableId::ImportScope, 100),
                (crate::metadata::tables::TableId::LocalVariable, 100),
                (crate::metadata::tables::TableId::LocalConstant, 100),
            ],
            false,
            false,
            false,
        ));

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

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

        // Verify the binary format matches expected layout
        assert_eq!(
            row_size, 16,
            "Row size should be 16 bytes for small indices"
        );

        // Method index (0x1234) as little-endian
        assert_eq!(buffer[0], 0x34);
        assert_eq!(buffer[1], 0x12);

        // ImportScope index (0x5678) as little-endian
        assert_eq!(buffer[2], 0x78);
        assert_eq!(buffer[3], 0x56);

        // LocalVariable index (0x9ABC) as little-endian
        assert_eq!(buffer[4], 0xBC);
        assert_eq!(buffer[5], 0x9A);

        // LocalConstant index (0xDEF0) as little-endian
        assert_eq!(buffer[6], 0xF0);
        assert_eq!(buffer[7], 0xDE);

        // Start offset (0x11223344) as little-endian
        assert_eq!(buffer[8], 0x44);
        assert_eq!(buffer[9], 0x33);
        assert_eq!(buffer[10], 0x22);
        assert_eq!(buffer[11], 0x11);

        // Length (0x55667788) as little-endian
        assert_eq!(buffer[12], 0x88);
        assert_eq!(buffer[13], 0x77);
        assert_eq!(buffer[14], 0x66);
        assert_eq!(buffer[15], 0x55);
    }

    #[test]
    fn test_known_binary_format_large_indices() {
        // Test with specific binary layout for large indices
        let local_scope = LocalScopeRaw {
            rid: 1,
            token: Token::new(0x3200_0001),
            offset: 0,
            method: 0x12345678,
            import_scope: 0x9ABCDEF0,
            variable_list: 0x11223344,
            constant_list: 0x55667788,
            start_offset: 0xAABBCCDD,
            length: 0xEEFF0011,
        };

        let table_info = std::sync::Arc::new(TableInfo::new_test(
            &[
                (crate::metadata::tables::TableId::MethodDef, 100000),
                (crate::metadata::tables::TableId::ImportScope, 100000),
                (crate::metadata::tables::TableId::LocalVariable, 100000),
                (crate::metadata::tables::TableId::LocalConstant, 100000),
            ],
            true,
            true,
            true,
        ));

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

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

        // Verify the binary format matches expected layout
        assert_eq!(
            row_size, 24,
            "Row size should be 24 bytes for large indices"
        );

        // Method index (0x12345678) as little-endian
        assert_eq!(buffer[0], 0x78);
        assert_eq!(buffer[1], 0x56);
        assert_eq!(buffer[2], 0x34);
        assert_eq!(buffer[3], 0x12);

        // ImportScope index (0x9ABCDEF0) as little-endian
        assert_eq!(buffer[4], 0xF0);
        assert_eq!(buffer[5], 0xDE);
        assert_eq!(buffer[6], 0xBC);
        assert_eq!(buffer[7], 0x9A);

        // LocalVariable index (0x11223344) as little-endian
        assert_eq!(buffer[8], 0x44);
        assert_eq!(buffer[9], 0x33);
        assert_eq!(buffer[10], 0x22);
        assert_eq!(buffer[11], 0x11);

        // LocalConstant index (0x55667788) as little-endian
        assert_eq!(buffer[12], 0x88);
        assert_eq!(buffer[13], 0x77);
        assert_eq!(buffer[14], 0x66);
        assert_eq!(buffer[15], 0x55);

        // Start offset (0xAABBCCDD) as little-endian
        assert_eq!(buffer[16], 0xDD);
        assert_eq!(buffer[17], 0xCC);
        assert_eq!(buffer[18], 0xBB);
        assert_eq!(buffer[19], 0xAA);

        // Length (0xEEFF0011) as little-endian
        assert_eq!(buffer[20], 0x11);
        assert_eq!(buffer[21], 0x00);
        assert_eq!(buffer[22], 0xFF);
        assert_eq!(buffer[23], 0xEE);
    }

    #[test]
    fn test_null_optional_indices() {
        // Test with null/zero values for optional indices
        let local_scope = LocalScopeRaw {
            rid: 1,
            token: Token::new(0x3200_0001),
            offset: 0,
            method: 1,        // Required method reference
            import_scope: 0,  // No import scope
            variable_list: 0, // No variables
            constant_list: 0, // No constants
            start_offset: 0x100,
            length: 0x50,
        };

        let table_info = std::sync::Arc::new(TableInfo::new_test(
            &[
                (crate::metadata::tables::TableId::MethodDef, 100),
                (crate::metadata::tables::TableId::ImportScope, 100),
                (crate::metadata::tables::TableId::LocalVariable, 100),
                (crate::metadata::tables::TableId::LocalConstant, 100),
            ],
            false,
            false,
            false,
        ));

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

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

        // Verify that zero values are preserved
        let mut read_offset = 0;
        let deserialized_row = LocalScopeRaw::row_read(&buffer, &mut read_offset, 1, &table_info)
            .expect("Deserialization should succeed");

        assert_eq!(deserialized_row.method, 1);
        assert_eq!(deserialized_row.import_scope, 0);
        assert_eq!(deserialized_row.variable_list, 0);
        assert_eq!(deserialized_row.constant_list, 0);
        assert_eq!(deserialized_row.start_offset, 0x100);
        assert_eq!(deserialized_row.length, 0x50);
    }
}