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
//! Metadata parsing and representation for .NET assemblies.
//!
//! This module contains the core metadata parsing infrastructure for .NET PE files,
//! providing comprehensive support for parsing metadata tables, streams, type systems,
//! and IL code according to the ECMA-335 standard. It serves as the foundation for
//! all .NET assembly analysis capabilities in dotscope.
//!
//! # Architecture Overview
//!
//! The metadata system is organized into several interconnected layers:
//!
//! ## Core Assembly Representation
//! - **Assembly Loading**: [`crate::metadata::cilobject`] provides the main [`crate::metadata::cilobject::CilObject`] type for loaded assemblies
//! - **PE Structure**: [`crate::metadata::cor20header`] handles the .NET-specific PE header information
//! - **Root Metadata**: [`crate::metadata::root`] manages the fundamental metadata directory structure
//!
//! ## Type System and Signatures
//! - **Type Resolution**: [`crate::metadata::typesystem`] provides complete .NET type system representation
//! - **Signature Parsing**: [`crate::metadata::signatures`] handles method and type signature decoding
//! - **Token Management**: [`crate::metadata::token`] provides metadata table row reference system
//!
//! ## Method and Code Analysis
//! - **Method Representation**: [`crate::metadata::method`] offers comprehensive method analysis with IL disassembly
//! - **Custom Attributes**: [`crate::metadata::customattributes`] handles .NET attribute parsing and representation
//! - **Security Model**: [`crate::metadata::security`] implements .NET declarative security parsing
//!
//! ## Metadata Streams and Tables
//! - **Stream Management**: [`crate::metadata::streams`] handles all metadata heap types (strings, blobs, GUIDs, user strings)
//! - **Table Processing**: [`crate::metadata::tables`] provides access to all ECMA-335 metadata tables
//! - **Data Loading**: Internal loader implements the metadata loading and caching infrastructure
//!
//! ## Interoperability and Resources
//! - **P/Invoke Support**: [`crate::metadata::imports`] and [`crate::metadata::exports`] handle native code interop
//! - **Type Marshalling**: [`crate::metadata::marshalling`] manages native type conversion specifications
//! - **Resource Access**: [`crate::metadata::resources`] provides .NET resource extraction and parsing
//!
//! ## Quality and Validation
//! - **Assembly Verification**: [`crate::metadata::identity`] handles strong name and authenticode validation
//! - **Metadata Validation**: [`crate::metadata::validation`] provides comprehensive metadata consistency checking
//!
//! # Key Components
//!
//! ## Primary Assembly Interface
//! - [`crate::metadata::cilobject::CilObject`] - Main assembly representation with metadata and IL code
//! - [`crate::metadata::method::Method`] - Complete method analysis with IL disassembly and control flow
//! - [`crate::metadata::typesystem::TypeRegistry`] - Comprehensive .NET type system representation
//!
//! ## Core Infrastructure
//! - [`crate::metadata::token::Token`] - Metadata table row references used throughout .NET
//! - [`crate::metadata::signatures`] - Method and type signature parsing infrastructure
//! - [`crate::metadata::streams`] - Metadata stream parsing (strings, blobs, GUIDs, etc.)
//!
//! # Usage Patterns
//!
//! ## Basic Assembly Loading and Analysis
//!
//! ```rust,no_run
//! use dotscope::CilObject;
//! use std::path::Path;
//!
//! // Load and parse a .NET assembly
//! let assembly = CilObject::from_path(Path::new("tests/samples/WindowsBase.dll"))?;
//!
//! // Access basic assembly information
//! if let Some(assembly_info) = assembly.assembly() {
//! println!("Assembly: {} (Version: {}.{})",
//! assembly_info.name, assembly_info.major_version, assembly_info.minor_version);
//! }
//!
//! // Get counts of major metadata elements
//! println!("Methods: {}", assembly.methods().len());
//! println!("Types: {}", assembly.types().len());
//! if let Some(module) = assembly.module() {
//! println!("Module name: {}", module.name);
//! }
//! # Ok::<(), dotscope::Error>(())
//! ```
//!
//! ## Method Analysis and IL Code Inspection
//!
//! ```rust,no_run
//! use dotscope::CilObject;
//! use std::path::Path;
//!
//! let assembly = CilObject::from_path(Path::new("tests/samples/WindowsBase.dll"))?;
//!
//! // Analyze methods with IL code
//! for entry in assembly.methods().iter().take(10) {
//! let method = entry.value();
//!
//! if method.instruction_count() > 0 {
//! println!("Method: {} ({} instructions)",
//! method.name, method.instruction_count());
//!
//! // Examine method characteristics
//! if method.flags_modifiers.contains(
//! dotscope::metadata::method::MethodModifiers::VIRTUAL) {
//! println!(" - Virtual method");
//! }
//!
//! if method.flags_modifiers.contains(
//! dotscope::metadata::method::MethodModifiers::STATIC) {
//! println!(" - Static method");
//! }
//! }
//! }
//! # Ok::<(), dotscope::Error>(())
//! ```
//!
//! ## Type System Exploration
//!
//! ```ignore
//! use dotscope::CilObject;
//! use std::path::Path;
//!
//! let assembly = CilObject::from_path(Path::new("tests/samples/WindowsBase.dll"))?;
//!
//! // Explore the type system
//! for entry in assembly.types().iter().take(10) {
//! let type_def = entry.value();
//!
//! println!("Type: {} (Namespace: {})",
//! type_def.name, type_def.namespace);
//!
//! // Show type characteristics using flags
//! if type_def.flags_visibility.contains(TypeVisibility::PUBLIC) {
//! println!(" - Public visibility");
//! }
//!
//! if type_def.flags_layout.contains(TypeLayout::ABSTRACT) {
//! println!(" - Abstract type");
//! }
//!
//! if type_def.flags_layout.contains(TypeLayout::SEALED) {
//! println!(" - Sealed type");
//! }
//!
//! println!(" - {} methods, {} fields",
//! type_def.methods.len(), type_def.fields.len());
//! }
//! # Ok::<(), dotscope::Error>(())
//! ```
//!
//! # Thread Safety
//!
//! All metadata types are designed for safe concurrent access:
//! - **Immutable Data**: Most metadata structures are read-only after parsing
//! - **Arc-based Sharing**: Reference counting enables safe multi-threaded access
//! - **Lazy Initialization**: `OnceLock` ensures thread-safe lazy loading
//!
//! # Error Handling
//!
//! The metadata system provides comprehensive error handling for malformed assemblies:
//! - **Format Validation**: ECMA-335 compliance checking
//! - **Bounds Checking**: Safe access to all metadata structures
//! - **Graceful Degradation**: Partial parsing when possible
//! - **Detailed Diagnostics**: Clear error messages for debugging
//!
//! # References
//!
//! - ECMA-335 6th Edition - Common Language Infrastructure (CLI)
//! - Microsoft .NET Framework PE Format Specification
//! - Windows PE/COFF Specification
/// Implementation of a raw assembly view for editing operations
/// Implementation of a loaded + parsed CIL binary
/// Implementation of the Header of CIL
/// Implementation of custom attribute parsing and representation
/// Implementation of custom debug information parsing for Portable PDB format
/// Multi-assembly dependency tracking and analysis system
/// Diagnostics collection for assembly loading and analysis
/// Implementation of 'Exports' by the loaded binary
/// Assembly identity and cryptographic verification system
/// Implementation of methods that are imported from other binaries (native or .net)
/// Implementation of import scope parsing for Portable PDB format
/// Implementation of our `MetaDataTable` loader
pub
/// Implementation of the type marshalling for native code invokations
/// Implementation of the MethodHeader of CIL
/// Composable query system for types and methods
/// Implementation of the .NET resources
/// Implementation of the root metadata structure
/// Implementation of the .NET security model
/// Implementation of sequence points in methods
/// Implementation of method and type signatures
/// Implementation of all metadata streams (tables, heaps, etc.)
/// Table field layout definitions for heap references
/// Implementation of the .NET metadata tables
/// Commonly used metadata token type
/// Implementation of the .NET type system
/// Metadata validation utilities