diff --git a/.gitignore b/.gitignore index ca5f9b3..1b14f1f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target /tests/pe .DS_Store +/.idea diff --git a/Cargo.lock b/Cargo.lock index ec424d0..7244040 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "aho-corasick" -version = "1.0.4" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6748e8def348ed4d14996fa801f4122cd763fff530258cdc03f64b25f89d3a5a" +checksum = "ea5d730647d4fadd988536d06fecce94b7b4f2a7efdae548f1cf4b63205518ab" dependencies = [ "memchr", ] @@ -27,9 +27,9 @@ dependencies = [ [[package]] name = "anstyle" -version = "1.0.1" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a30da5c5f2d5e72842e00bcb57657162cdabef0931f40e2deb9b4140440cecd" +checksum = "b84bf0a05bbb2a83e5eb6fa36bb6e87baa08193c35ff52bbf6b38d8af2890e46" [[package]] name = "anstyle-parse" @@ -82,9 +82,9 @@ dependencies = [ [[package]] name = "bytemuck_derive" -version = "1.4.1" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdde5c9cd29ebd706ce1b35600920a33550e402fc998a2e53ad3b42c3c47a192" +checksum = "965ab7eb5f8f97d2a083c799f3a1b994fc397b2fe2da5d1da1626ce15a39f2b1" dependencies = [ "proc-macro2", "quote", @@ -108,9 +108,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.4.3" +version = "4.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84ed82781cea27b43c9b106a979fe450a13a31aab0500595fb3fc06616de08e6" +checksum = "b1d7b8d5ec32af0fadc644bf1fd509a688c2103b185644bb1e29d164e0703136" dependencies = [ "clap_builder", "clap_derive", @@ -118,9 +118,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.4.2" +version = "4.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2bb9faaa7c2ef94b2743a21f5a29e6f0010dff4caa69ac8e9d6cf8b6fa74da08" +checksum = "5179bb514e4d7c2051749d8fcefa2ed6d06a9f4e6d69faf3805f5d80b8cf8d56" dependencies = [ "anstream", "anstyle", @@ -142,9 +142,9 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.5.0" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b" +checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961" [[package]] name = "colorchoice" @@ -172,15 +172,15 @@ checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] name = "hermit-abi" -version = "0.3.2" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b" +checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" [[package]] name = "libc" -version = "0.2.147" +version = "0.2.148" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" +checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b" [[package]] name = "libtest-mimic" @@ -195,9 +195,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.5.0" +version = "2.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" [[package]] name = "num-derive" @@ -244,27 +244,27 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.66" +version = "1.0.67" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" +checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.32" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" dependencies = [ "proc-macro2", ] [[package]] name = "regex" -version = "1.9.3" +version = "1.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81bc1d4caf89fac26a70747fe603c130093b53c773888797a6329091246d651a" +checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47" dependencies = [ "aho-corasick", "memchr", @@ -274,9 +274,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.3.6" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fed1ceff11a1dddaee50c9dc8e4938bd106e9d89ae372f192311e7da498e3b69" +checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795" dependencies = [ "aho-corasick", "memchr", @@ -285,9 +285,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.7.4" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2" +checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" [[package]] name = "same-file" @@ -306,9 +306,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "syn" -version = "2.0.28" +version = "2.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04361975b3f5e348b2189d8dc55bc942f278b2d482a6a0365de5bdd62d351567" +checksum = "7303ef2c05cd654186cb250d29049a24840ca25d2747c25c0381c8d9e2f582e8" dependencies = [ "proc-macro2", "quote", @@ -317,9 +317,9 @@ dependencies = [ [[package]] name = "termcolor" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" +checksum = "6093bad37da69aab9d123a8091e4be0aa4a03e4d601ec641c327398315f62b64" dependencies = [ "winapi-util", ] @@ -335,9 +335,9 @@ dependencies = [ [[package]] name = "unicode-ident" -version = "1.0.11" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[package]] name = "utf8parse" @@ -347,9 +347,9 @@ checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" [[package]] name = "walkdir" -version = "2.3.3" +version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36df944cda56c7d8d8b7496af378e6b16de9284591917d307c9b4d313c44e698" +checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" dependencies = [ "same-file", "winapi-util", @@ -373,9 +373,9 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-util" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" dependencies = [ "winapi", ] @@ -397,9 +397,9 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.48.2" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1eeca1c172a285ee6c2c84c341ccea837e7c01b12fbb2d0fe3c9e550ce49ec8" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", @@ -412,42 +412,42 @@ dependencies = [ [[package]] name = "windows_aarch64_gnullvm" -version = "0.48.2" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b10d0c968ba7f6166195e13d593af609ec2e3d24f916f081690695cf5eaffb2f" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" [[package]] name = "windows_aarch64_msvc" -version = "0.48.2" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "571d8d4e62f26d4932099a9efe89660e8bd5087775a2ab5cdd8b747b811f1058" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" [[package]] name = "windows_i686_gnu" -version = "0.48.2" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2229ad223e178db5fbbc8bd8d3835e51e566b8474bfca58d2e6150c48bb723cd" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" [[package]] name = "windows_i686_msvc" -version = "0.48.2" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "600956e2d840c194eedfc5d18f8242bc2e17c7775b6684488af3a9fff6fe3287" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" [[package]] name = "windows_x86_64_gnu" -version = "0.48.2" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea99ff3f8b49fb7a8e0d305e5aec485bd068c2ba691b6e277d29eaeac945868a" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" [[package]] name = "windows_x86_64_gnullvm" -version = "0.48.2" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f1a05a1ece9a7a0d5a7ccf30ba2c33e3a61a30e042ffd247567d1de1d94120d" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" [[package]] name = "windows_x86_64_msvc" -version = "0.48.2" +version = "0.48.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d419259aba16b663966e29e6d7c6ecfa0bb8425818bb96f6f1f3c3eb71a6e7b9" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" diff --git a/Cargo.toml b/Cargo.toml index 130aaba..14d6cd0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,12 +17,12 @@ default = ["std"] std = ["clap"] [dependencies] -bytemuck = {version = "1.14.0", features = ["derive"]} +bytemuck = { version = "1.14.0", features = ["derive"] } num-traits = { version = "0.2", default-features = false } num-derive = "0.4" bitflags = { version = "2.4.0", default-features = false } chrono = { version = "0.4.31", default-features = false } -clap = { version = "4.4.3", features = ["cargo"], optional = true } +clap = { version = "4.4.4", features = ["cargo"], optional = true } [dev-dependencies] datatest-stable = "0.2.3" diff --git a/src/coff.rs b/src/coff.rs index d4413d1..a17eee2 100644 --- a/src/coff.rs +++ b/src/coff.rs @@ -9,7 +9,7 @@ use crate::prelude::*; /// COFF File Header (Object and Image) #[derive(Copy, Clone, Pod, Zeroable, Default)] #[repr(C)] -pub struct coff_file_header { +pub struct CoffFileHeader { /// The number that identifies the type of target machine. pub machine: u16, /// The number of sections. This indicates the size of the section table, which immediately follows the headers. @@ -28,7 +28,7 @@ pub struct coff_file_header { pub characteristics: u16 } -impl fmt::Display for coff_file_header { +impl fmt::Display for CoffFileHeader { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let machine_type = self.get_machine_type() .expect("Failed to get machine type"); @@ -182,7 +182,7 @@ impl str::FromStr for Characteristics { } } -impl coff_file_header { +impl CoffFileHeader { /// Returns the machine type as an enum pub fn get_machine_type(&self) -> Option { MachineTypes::from_u16(self.machine) diff --git a/src/lib.rs b/src/lib.rs index cc7ca3e..b1cd203 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ //! # pe-parser: Blazing-fast, safe, Portable Executable parsing. //! -//! `pe-parser` provides a safe Rust-y way to parse Portable Exectuables quickly. +//! `pe-parser` provides a safe Rust-y way to parse Portable Executables quickly. //! - Everything parsed to native documented structs //! - Secondary parsing functions raw data into native Rust types //! - Every section can be printed with ease @@ -58,7 +58,7 @@ pub enum Error { MissingMagicNumber, } -impl core::fmt::Display for Error { +impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Error::OffsetOutOfRange => f.write_str("Offset out of range!"), diff --git a/src/optional.rs b/src/optional.rs index f780371..542d2db 100644 --- a/src/optional.rs +++ b/src/optional.rs @@ -19,43 +19,43 @@ pub enum Magic { /// Struct containing basic information (address and size) of each table. #[derive(Copy, Clone, Pod, Zeroable, Default)] #[repr(C)] -pub struct data_directories { +pub struct DataDirectories { /// The export table (.edata) address and size. (Image Only) - pub export_table: data_directory, + pub export_table: DataDirectory, /// The import table (.idata) address and size. - pub import_table: data_directory, + pub import_table: DataDirectory, /// The resource table (.rsrc) address and size. - pub resource_table: data_directory, + pub resource_table: DataDirectory, /// The exception table (.pdata) address and size. - pub exception_table: data_directory, + pub exception_table: DataDirectory, /// The attribute certificate table address and size. (Image Only) - pub certificate_table: data_directory, + pub certificate_table: DataDirectory, /// The base relocation table (.reloc) address and size. (Image Only) - pub base_relocation_table: data_directory, + pub base_relocation_table: DataDirectory, /// The debug data (.debug) starting address and size. - pub debug: data_directory, + pub debug: DataDirectory, /// Reserved, must be 0. - pub architecture: data_directory, + pub architecture: DataDirectory, /// The RVA of the value to be stored in the global pointer register. /// The size member of this structure must be set to zero. - pub global_ptr: data_directory, + pub global_ptr: DataDirectory, /// The thread local storage (TLS) table (.tls) address and size. - pub tls_table: data_directory, + pub tls_table: DataDirectory, /// The load configuration table address and size. (Image Only) - pub load_config_table: data_directory, + pub load_config_table: DataDirectory, /// The bound import table address and size. - pub bound_import: data_directory, + pub bound_import: DataDirectory, /// The import address table address and size. - pub import_address_table: data_directory, + pub import_address_table: DataDirectory, /// The delay import descriptor address and size. (Image Only) - pub delay_import_descriptor: data_directory, + pub delay_import_descriptor: DataDirectory, /// The CLR runtime header (.cormeta) address and size. (Object Only - pub clr_runtime_header: data_directory, + pub clr_runtime_header: DataDirectory, /// Reserved, must be zero. - pub reserved: data_directory + pub reserved: DataDirectory } -impl fmt::Display for data_directories { +impl fmt::Display for DataDirectories { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f, "Data Directories")?; writeln!(f, "----------------")?; @@ -85,7 +85,7 @@ impl fmt::Display for data_directories { /// A data directory is an 8-byte field that has the following declaration: #[derive(Copy, Clone, Pod, Zeroable, Default)] #[repr(C)] -pub struct data_directory { +pub struct DataDirectory { /// RVA of the table. The RVA is the address of the table relative to the base address of the image when the table is loaded. pub virtual_address: u32, /// Size of the table in bytes. @@ -95,7 +95,7 @@ pub struct data_directory { /// PE32 Optional Header (Image Only) #[derive(Copy, Clone, Pod, Zeroable, Default)] #[repr(C)] -pub struct optional_header_32 { +pub struct OptionalHeader32 { /// The unsigned integer that identifies the state of the image file. /// The most common number is 0x10B, which identifies it as a normal executable file. /// 0x107 identifies it as a ROM image, and 0x20B identifies it as a PE32+ executable. @@ -168,10 +168,10 @@ pub struct optional_header_32 { /// The number of data-directory entries in the remainder of the optional header. Each describes a location and size. pub number_of_rva_and_sizes: u32, /// Struct containing basic information (address and size) of each table. - pub data_directories: data_directories + pub data_directories: DataDirectories } -impl fmt::Display for optional_header_32 { +impl fmt::Display for OptionalHeader32 { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let subsystem = self.get_subsystem() .expect("Failed to get subsystem"); @@ -215,7 +215,7 @@ impl fmt::Display for optional_header_32 { /// PE32+ Optional Header (Image Only) #[derive(Copy, Clone, Pod, Zeroable, Default)] #[repr(C)] -pub struct optional_header_64 { +pub struct OptionalHeader64 { /// The unsigned integer that identifies the state of the image file. /// The most common number is 0x10B, which identifies it as a normal executable file. /// 0x107 identifies it as a ROM image, and 0x20B identifies it as a PE32+ executable. @@ -286,10 +286,10 @@ pub struct optional_header_64 { /// The number of data-directory entries in the remainder of the optional header. Each describes a location and size. pub number_of_rva_and_sizes: u32, /// Struct containing basic information (address and size) of each table. - pub data_directories: data_directories + pub data_directories: DataDirectories } -impl fmt::Display for optional_header_64 { +impl fmt::Display for OptionalHeader64 { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let subsystem = self.get_subsystem() .expect("Failed to get subsystem"); @@ -434,7 +434,7 @@ pub trait Optional: Sized { fn parse_optional_header(binary: &[u8], offset: &mut usize) -> Result; } -impl Optional for optional_header_32 { +impl Optional for OptionalHeader32 { fn get_subsystem(&self) -> Option { Subsystem::from_u16(self.subsystem) } @@ -452,7 +452,7 @@ impl Optional for optional_header_32 { } }; - let optional_header = try_from_bytes::(slice); + let optional_header = try_from_bytes::(slice); *offset += size; match optional_header.copied() { @@ -466,7 +466,7 @@ impl Optional for optional_header_32 { } } -impl Optional for optional_header_64 { +impl Optional for OptionalHeader64 { fn get_subsystem(&self) -> Option { Subsystem::from_u16(self.subsystem) } @@ -484,7 +484,7 @@ impl Optional for optional_header_64 { } }; - let optional_header = try_from_bytes::(slice); + let optional_header = try_from_bytes::(slice); *offset += size; match optional_header.copied() { Ok(header) => { diff --git a/src/pe.rs b/src/pe.rs index ea931b4..8b21c01 100644 --- a/src/pe.rs +++ b/src/pe.rs @@ -1,4 +1,4 @@ -use crate::{coff::coff_file_header, optional::{optional_header_32, optional_header_64, Magic, Optional}, section::{section_header, parse_section_table}, Error}; +use crate::{coff::CoffFileHeader, optional::{OptionalHeader32, OptionalHeader64, Magic, Optional}, section::{SectionHeader, parse_section_table}, Error}; use bytemuck::checked::try_from_bytes; use num_traits::FromPrimitive; use core::fmt; @@ -9,13 +9,13 @@ const IMAGE_DOS_PE_SIGNATURE_OFFSET: usize = 0x3c; /// Representation of the sections of a Portable Executable pub struct PortableExecutable { /// COFF File Header (Object and Image) - pub coff: coff_file_header, + pub coff: CoffFileHeader, /// PE32 Optional Header (Image Only) - pub optional_header_32: Option, + pub optional_header_32: Option, /// PE32+ Optional Header (Image Only) - pub optional_header_64: Option, + pub optional_header_64: Option, /// Table containing a list of section headers - pub section_table: Vec, + pub section_table: Vec, } /// Parse a Portable Executable from a given byte array @@ -43,7 +43,7 @@ pub fn parse_portable_executable(binary: &[u8]) -> Result Result(slice) { + pe.coff = match try_from_bytes::(slice) { Ok(coff) => *coff, Err(_) => { return Err(Error::MissingCoffHeader); @@ -75,10 +75,10 @@ pub fn parse_portable_executable(binary: &[u8]) -> Result { - pe.optional_header_32 = Some(optional_header_32::parse_optional_header(binary, &mut offset)?); + pe.optional_header_32 = Some(OptionalHeader32::parse_optional_header(binary, &mut offset)?); } Magic::PE64 => { - pe.optional_header_64 = Some(optional_header_64::parse_optional_header(binary, &mut offset)?); + pe.optional_header_64 = Some(OptionalHeader64::parse_optional_header(binary, &mut offset)?); } } } diff --git a/src/section.rs b/src/section.rs index d36e408..c392168 100644 --- a/src/section.rs +++ b/src/section.rs @@ -8,14 +8,14 @@ use crate::prelude::*; /// Parse the section table from a byte array at a given offset. /// `number_of_sections` should be equal to number of sections /// defined in the COFF header. -pub fn parse_section_table(binary: &[u8], offset: usize, number_of_sections: u16) -> Vec { +pub fn parse_section_table(binary: &[u8], offset: usize, number_of_sections: u16) -> Vec { let mut offset = offset; - let mut headers: Vec = Vec::new(); - let header_size = core::mem::size_of::(); + let mut headers: Vec = Vec::new(); + let header_size = core::mem::size_of::(); for _ in 0..number_of_sections { if let Some(slice) = binary.get(offset..offset+header_size) { - if let Some(header) = try_from_bytes::(slice).ok() { + if let Some(header) = try_from_bytes::(slice).ok() { headers.push(*header); } } @@ -29,7 +29,7 @@ pub fn parse_section_table(binary: &[u8], offset: usize, number_of_sections: u16 /// and location of a section in the binary #[derive(Copy, Clone, Pod, Zeroable, Default)] #[repr(C)] -pub struct section_header { +pub struct SectionHeader { /// An 8-byte, null-padded UTF-8 encoded string. /// If the string is exactly 8 characters long, there is no terminating null. /// For longer names, this field contains a slash (/) that is followed by an ASCII representation of a decimal number that is an offset into the string table. @@ -61,18 +61,18 @@ pub struct section_header { /// The file pointer to the beginning of line-number entries for the section. /// This is set to zero if there are no COFF line numbers. /// This value should be zero for an image because COFF debugging information is deprecated. - pub pointer_to_linenumbers: u32, + pub pointer_to_line_numbers: u32, /// The number of relocation entries for the section. /// This is set to zero for executable images. pub number_of_relocations: u16, /// The number of line-number entries for the section. /// This value should be zero for an image because COFF debugging information is deprecated. - pub number_of_linenumbers: u16, + pub number_of_line_numbers: u16, /// The flags that describe the characteristics of the section. pub characteristics: u32 } -impl fmt::Display for section_header { +impl fmt::Display for SectionHeader { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let name = self.get_name() .expect("Failed to get name"); @@ -87,9 +87,9 @@ impl fmt::Display for section_header { writeln!(f, "Size of Raw Data: {}", self.size_of_raw_data)?; writeln!(f, "Pointer to Raw Data: {}", self.pointer_to_raw_data)?; writeln!(f, "Pointer to Relocations: {}", self.pointer_to_relocations)?; - writeln!(f, "Pointer to Line-numbers: {}", self.pointer_to_linenumbers)?; + writeln!(f, "Pointer to Line-numbers: {}", self.pointer_to_line_numbers)?; writeln!(f, "Number of Relocations: {}", self.number_of_relocations)?; - writeln!(f, "Number of Line-numbers: {}", self.number_of_linenumbers)?; + writeln!(f, "Number of Line-numbers: {}", self.number_of_line_numbers)?; writeln!(f, "Characteristics: {}", characteristics)?; Ok(()) @@ -229,7 +229,7 @@ impl str::FromStr for SectionFlags { } } -impl section_header { +impl SectionHeader { /// Get the name of a section as a string. /// Note that this string may contain null characters. pub fn get_name(&self) -> Option {