Skip to content

Commit c077bc6

Browse files
committed
build: cargo fmt for 2024 edition
1 parent d04e589 commit c077bc6

26 files changed

+246
-225
lines changed

examples/dotnet_pe_analysis.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/// Demonstrates how to read additional metadata (i.e. .Net runtime ones) from PE context
22
use goblin::container::Endian;
3+
use goblin::pe::PE;
34
use goblin::pe::data_directories::DataDirectory;
45
use goblin::pe::utils::get_data;
5-
use goblin::pe::PE;
6-
use scroll::ctx::TryFromCtx;
76
use scroll::Pread;
7+
use scroll::ctx::TryFromCtx;
88

99
#[repr(C)]
1010
#[derive(Debug, Pread)]

src/archive/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,12 @@ impl<'a> Archive<'a> {
481481
Index::parse_windows_linker_member(data)?
482482
}
483483
IndexType::BSD => {
484-
return Err(Error::Malformed("SysV index occurs after BSD index".into()))
484+
return Err(Error::Malformed("SysV index occurs after BSD index".into()));
485485
}
486486
IndexType::Windows => {
487487
return Err(Error::Malformed(
488488
"More than two Windows Linker members".into(),
489-
))
489+
));
490490
}
491491
}
492492
} else if member.bsd_name == Some(BSD_SYMDEF_NAME)

src/elf/gnu_hash.rs

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -97,61 +97,64 @@ macro_rules! elf_gnu_hash_impl {
9797
pub unsafe fn from_raw_table(
9898
hashtab: &'a [u8],
9999
dynsyms: &'a [Sym],
100-
) -> Result<Self, &'static str> { unsafe {
101-
if hashtab.as_ptr() as usize % INT_SIZE != 0 {
102-
return Err("hashtab is not aligned with 64-bit");
103-
}
104-
105-
if hashtab.len() <= 16 {
106-
return Err("failed to read in number of buckets");
107-
}
108-
109-
let [nbuckets, symindex, maskwords, shift2] =
110-
(hashtab.as_ptr() as *const u32 as *const [u32; 4]).read();
100+
) -> Result<Self, &'static str> {
101+
unsafe {
102+
if hashtab.as_ptr() as usize % INT_SIZE != 0 {
103+
return Err("hashtab is not aligned with 64-bit");
104+
}
111105

112-
if !maskwords.is_power_of_two() {
113-
return Err("maskwords must be a power of two");
114-
}
106+
if hashtab.len() <= 16 {
107+
return Err("failed to read in number of buckets");
108+
}
115109

116-
let hashtab = &hashtab[16..];
117-
{
118-
// SAFETY: Condition to check for an overflow
119-
// size_of(chains) + size_of(buckets) + size_of(bloom_filter) == size_of(hashtab)
110+
let [nbuckets, symindex, maskwords, shift2] =
111+
(hashtab.as_ptr() as *const u32 as *const [u32; 4]).read();
120112

121-
if dynsyms.len() <= symindex as usize {
122-
return Err("symindex must be smaller than dynsyms.len()");
113+
if !maskwords.is_power_of_two() {
114+
return Err("maskwords must be a power of two");
123115
}
124-
let chains_size = (dynsyms.len() - symindex as usize).checked_mul(U32_SIZE);
125-
let buckets_size = (nbuckets as usize).checked_mul(U32_SIZE);
126-
let bloom_size = (maskwords as usize).checked_mul(INT_SIZE);
127116

128-
let total_size = match (chains_size, buckets_size, bloom_size) {
129-
(Some(a), Some(b), Some(c)) => {
130-
a.checked_add(b).and_then(|t| t.checked_add(c))
117+
let hashtab = &hashtab[16..];
118+
{
119+
// SAFETY: Condition to check for an overflow
120+
// size_of(chains) + size_of(buckets) + size_of(bloom_filter) == size_of(hashtab)
121+
122+
if dynsyms.len() <= symindex as usize {
123+
return Err("symindex must be smaller than dynsyms.len()");
124+
}
125+
let chains_size = (dynsyms.len() - symindex as usize).checked_mul(U32_SIZE);
126+
let buckets_size = (nbuckets as usize).checked_mul(U32_SIZE);
127+
let bloom_size = (maskwords as usize).checked_mul(INT_SIZE);
128+
129+
let total_size = match (chains_size, buckets_size, bloom_size) {
130+
(Some(a), Some(b), Some(c)) => {
131+
a.checked_add(b).and_then(|t| t.checked_add(c))
132+
}
133+
_ => None,
134+
};
135+
match total_size {
136+
Some(size) if size == hashtab.len() => {}
137+
_ => return Err("index out of bound or non-complete hash section"),
131138
}
132-
_ => None,
133-
};
134-
match total_size {
135-
Some(size) if size == hashtab.len() => {}
136-
_ => return Err("index out of bound or non-complete hash section"),
137139
}
138-
}
139140

140-
let bloom_filter_ptr = hashtab.as_ptr() as *const $IntTy;
141-
let buckets_ptr = bloom_filter_ptr.add(maskwords as usize) as *const u32;
142-
let chains_ptr = buckets_ptr.add(nbuckets as usize);
143-
let bloom_filter = slice::from_raw_parts(bloom_filter_ptr, maskwords as usize);
144-
let buckets = slice::from_raw_parts(buckets_ptr, nbuckets as usize);
145-
let chains = slice::from_raw_parts(chains_ptr, dynsyms.len() - symindex as usize);
146-
Ok(Self {
147-
symindex,
148-
shift2,
149-
bloom_filter,
150-
buckets,
151-
chains,
152-
dynsyms,
153-
})
154-
}}
141+
let bloom_filter_ptr = hashtab.as_ptr() as *const $IntTy;
142+
let buckets_ptr = bloom_filter_ptr.add(maskwords as usize) as *const u32;
143+
let chains_ptr = buckets_ptr.add(nbuckets as usize);
144+
let bloom_filter = slice::from_raw_parts(bloom_filter_ptr, maskwords as usize);
145+
let buckets = slice::from_raw_parts(buckets_ptr, nbuckets as usize);
146+
let chains =
147+
slice::from_raw_parts(chains_ptr, dynsyms.len() - symindex as usize);
148+
Ok(Self {
149+
symindex,
150+
shift2,
151+
bloom_filter,
152+
buckets,
153+
chains,
154+
dynsyms,
155+
})
156+
}
157+
}
155158

156159
/// Locate the hash chain, and corresponding hash value element.
157160
#[cold]

src/elf/program_header.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,9 @@ macro_rules! elf_program_header_std_impl {
341341
pub unsafe fn from_raw_parts<'a>(
342342
phdrp: *const ProgramHeader,
343343
phnum: usize,
344-
) -> &'a [ProgramHeader] { unsafe {
345-
slice::from_raw_parts(phdrp, phnum)
346-
}}
344+
) -> &'a [ProgramHeader] {
345+
unsafe { slice::from_raw_parts(phdrp, phnum) }
346+
}
347347

348348
#[cfg(feature = "std")]
349349
pub fn from_fd(fd: &mut File, offset: u64, count: usize) -> Result<Vec<ProgramHeader>> {

src/elf/sym.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ macro_rules! elf_sym_std_impl {
239239
///
240240
/// This function creates a `Sym` slice directly from a raw pointer
241241
#[inline]
242-
pub unsafe fn from_raw<'a>(symp: *const Sym, count: usize) -> &'a [Sym] { unsafe {
243-
slice::from_raw_parts(symp, count)
244-
}}
242+
pub unsafe fn from_raw<'a>(symp: *const Sym, count: usize) -> &'a [Sym] {
243+
unsafe { slice::from_raw_parts(symp, count) }
244+
}
245245

246246
if_std! {
247247
use crate::error::Result;

src/elf/symver.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
//! [lsb-verdef]: https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/symversion.html#SYMVERDEFS
6161
6262
use crate::container;
63-
use crate::elf::section_header::{SectionHeader, SHT_GNU_VERDEF, SHT_GNU_VERNEED, SHT_GNU_VERSYM};
63+
use crate::elf::section_header::{SHT_GNU_VERDEF, SHT_GNU_VERNEED, SHT_GNU_VERSYM, SectionHeader};
6464
use crate::error::Result;
6565
use core::iter::FusedIterator;
6666
use scroll::Pread;
@@ -831,7 +831,7 @@ pub struct Vernaux {
831831
#[cfg(test)]
832832
mod test {
833833
use super::{ElfVerdaux, ElfVerdef, ElfVernaux, ElfVerneed, ElfVersym};
834-
use super::{Versym, VERSYM_HIDDEN, VER_NDX_GLOBAL, VER_NDX_LOCAL};
834+
use super::{VER_NDX_GLOBAL, VER_NDX_LOCAL, VERSYM_HIDDEN, Versym};
835835
use core::mem::size_of;
836836

837837
#[test]

src/mach/constants.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,7 @@ pub mod cputype {
256256
pub const CPU_SUBTYPE_MC68030_ONLY: CpuSubType = 3;
257257

258258
macro_rules! CPU_SUBTYPE_INTEL {
259-
($f:expr, $m:expr) => {{
260-
($f) + (($m) << 4)
261-
}};
259+
($f:expr, $m:expr) => {{ ($f) + (($m) << 4) }};
262260
}
263261

264262
pub const CPU_SUBTYPE_I386_ALL: CpuSubType = CPU_SUBTYPE_INTEL!(3, 0);

src/mach/fat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if_std! {
88
}
99

1010
use crate::error;
11-
use crate::mach::constants::cputype::{CpuSubType, CpuType, CPU_ARCH_ABI64, CPU_SUBTYPE_MASK};
11+
use crate::mach::constants::cputype::{CPU_ARCH_ABI64, CPU_SUBTYPE_MASK, CpuSubType, CpuType};
1212
use scroll::{Pread, Pwrite, SizeWith};
1313

1414
pub const FAT_MAGIC: u32 = 0xcafe_babe;

src/mach/header.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use scroll::{Pread, Pwrite, SizeWith};
88

99
use crate::container::{self, Container};
1010
use crate::error;
11-
use crate::mach::constants::cputype::{CpuSubType, CpuType, CPU_SUBTYPE_MASK};
11+
use crate::mach::constants::cputype::{CPU_SUBTYPE_MASK, CpuSubType, CpuType};
1212

1313
// Constants for the flags field of the mach_header
1414
/// the object file has no undefined references

src/mach/load_command.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::error;
44
use core::convert::TryFrom;
55
use core::fmt::{self, Display};
6-
use scroll::{ctx, Endian};
6+
use scroll::{Endian, ctx};
77
use scroll::{IOread, IOwrite, Pread, Pwrite, SizeWith};
88

99
///////////////////////////////////////
@@ -1026,7 +1026,7 @@ impl TryFrom<u32> for Platform {
10261026
return Err(error::Error::Malformed(format!(
10271027
"unknown platform for load command: {:x}",
10281028
cmd
1029-
)))
1029+
)));
10301030
}
10311031
})
10321032
}

src/mach/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::fmt;
55
use log::debug;
66

77
use scroll::ctx::SizeWith;
8-
use scroll::{Pread, BE};
8+
use scroll::{BE, Pread};
99

1010
use crate::{archive, container};
1111
use crate::{error, take_hint_bytes};

src/mach/segment.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ use core::ops::{Deref, DerefMut};
1111
use crate::container;
1212
use crate::error;
1313

14-
use crate::mach::constants::{SECTION_TYPE, S_GB_ZEROFILL, S_THREAD_LOCAL_ZEROFILL, S_ZEROFILL};
14+
use crate::mach::constants::{S_GB_ZEROFILL, S_THREAD_LOCAL_ZEROFILL, S_ZEROFILL, SECTION_TYPE};
1515
use crate::mach::load_command::{
16-
Section32, Section64, SegmentCommand32, SegmentCommand64, LC_SEGMENT, LC_SEGMENT_64,
17-
SIZEOF_SECTION_32, SIZEOF_SECTION_64, SIZEOF_SEGMENT_COMMAND_32, SIZEOF_SEGMENT_COMMAND_64,
16+
LC_SEGMENT, LC_SEGMENT_64, SIZEOF_SECTION_32, SIZEOF_SECTION_64, SIZEOF_SEGMENT_COMMAND_32,
17+
SIZEOF_SEGMENT_COMMAND_64, Section32, Section64, SegmentCommand32, SegmentCommand64,
1818
};
1919
use crate::mach::relocation::RelocationInfo;
2020

src/pe/authenticode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use alloc::collections::VecDeque;
1212
use core::ops::Range;
1313
use log::debug;
1414

15-
use super::{section_table::SectionTable, PE};
15+
use super::{PE, section_table::SectionTable};
1616

1717
static PADDING: [u8; 7] = [0; 7];
1818

src/pe/certificate_table.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/// https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#the-attribute-certificate-table-image-only
44
/// https://learn.microsoft.com/en-us/windows/win32/api/wintrust/ns-wintrust-win_certificate
55
use crate::error;
6-
use scroll::{ctx, Pread, Pwrite};
6+
use scroll::{Pread, Pwrite, ctx};
77

88
use alloc::string::ToString;
99
use alloc::vec::Vec;
@@ -34,7 +34,7 @@ impl TryFrom<u16> for AttributeCertificateRevision {
3434
_ => {
3535
return Err(error::Error::Malformed(
3636
"Invalid certificate attribute revision".to_string(),
37-
))
37+
));
3838
}
3939
})
4040
}
@@ -71,7 +71,7 @@ impl TryFrom<u16> for AttributeCertificateType {
7171
_ => {
7272
return Err(error::Error::Malformed(
7373
"Invalid attribute certificate type".to_string(),
74-
))
74+
));
7575
}
7676
})
7777
}

src/pe/data_directories.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::error;
22
use scroll::{
3-
ctx::{self},
43
Pread, Pwrite, SizeWith,
4+
ctx::{self},
55
};
66

77
#[repr(C)]
@@ -61,7 +61,7 @@ impl TryFrom<usize> for DataDirectoryType {
6161
_ => {
6262
return Err(error::Error::Malformed(
6363
"Wrong data directory index number".into(),
64-
))
64+
));
6565
}
6666
})
6767
}

src/pe/debug.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ impl<'a> DebugData<'a> {
187187
if offset + dd.size as usize > bytes.len() {
188188
return Err(error::Error::Malformed(format!(
189189
"ImageDebugDirectory offset {:#x} and size {:#x} exceeds the bounds of the bytes size {:#x}",
190-
offset, dd.size, bytes.len()
190+
offset,
191+
dd.size,
192+
bytes.len()
191193
)));
192194
}
193195
let data = &bytes[offset..offset + dd.size as usize];
@@ -731,9 +733,9 @@ impl<'a> POGOInfo<'a> {
731733

732734
if idd.size_of_data as usize <= POGO_SIGNATURE_SIZE {
733735
return Err(error::Error::Malformed(format!(
734-
"ImageDebugDirectory size_of_data {:#x} is smaller or equal to POGO_SIGNATURE_SIZE {:#x}",
735-
idd.size_of_data, POGO_SIGNATURE_SIZE
736-
)));
736+
"ImageDebugDirectory size_of_data {:#x} is smaller or equal to POGO_SIGNATURE_SIZE {:#x}",
737+
idd.size_of_data, POGO_SIGNATURE_SIZE
738+
)));
737739
}
738740

739741
let offset_end = offset
@@ -748,9 +750,11 @@ impl<'a> POGOInfo<'a> {
748750

749751
if offset > bytes.len() || offset_end > bytes.len() {
750752
return Err(error::Error::Malformed(format!(
751-
"ImageDebugDirectory offset_start {:#x} or offset_end {:#x} exceed the bounds of the bytes size {:#x}",
752-
offset, offset_end, bytes.len()
753-
)));
753+
"ImageDebugDirectory offset_start {:#x} or offset_end {:#x} exceed the bounds of the bytes size {:#x}",
754+
offset,
755+
offset_end,
756+
bytes.len()
757+
)));
754758
}
755759

756760
let data = &bytes[offset..offset_end];
@@ -784,7 +788,7 @@ impl<'a> Iterator for POGOEntryIterator<'a> {
784788
if offset >= self.data.len() {
785789
return Some(Err(error::Error::Malformed(format!(
786790
"Offset {:#x} is too big for containing name field of POGO entry (rva {:#x} and size {:#X})",
787-
offset,rva, size
791+
offset, rva, size
788792
))));
789793
}
790794
let name = match self.data[offset..].iter().position(|&b| b == 0) {
@@ -820,12 +824,11 @@ impl FusedIterator for POGOEntryIterator<'_> {}
820824
#[cfg(test)]
821825
mod tests {
822826
use super::{
823-
ExDllCharacteristicsInfo, ImageDebugDirectory, POGOInfoEntry, ReproInfo, VCFeatureInfo,
824-
CODEVIEW_PDB70_MAGIC, IMAGE_DEBUG_POGO_SIGNATURE_LTCG, IMAGE_DEBUG_TYPE_CODEVIEW,
825-
IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS, IMAGE_DEBUG_TYPE_ILTCG, IMAGE_DEBUG_TYPE_POGO,
826-
IMAGE_DEBUG_TYPE_REPRO, IMAGE_DEBUG_TYPE_VC_FEATURE,
827+
CODEVIEW_PDB70_MAGIC, ExDllCharacteristicsInfo, IMAGE_DEBUG_POGO_SIGNATURE_LTCG,
828+
IMAGE_DEBUG_TYPE_CODEVIEW, IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS, IMAGE_DEBUG_TYPE_ILTCG,
829+
IMAGE_DEBUG_TYPE_POGO, IMAGE_DEBUG_TYPE_REPRO, IMAGE_DEBUG_TYPE_VC_FEATURE,
827830
IMAGE_DLLCHARACTERISTICS_EX_CET_COMPAT, IMAGE_DLLCHARACTERISTICS_EX_CET_COMPAT_STRICT_MODE,
828-
POGO_SIGNATURE_SIZE,
831+
ImageDebugDirectory, POGO_SIGNATURE_SIZE, POGOInfoEntry, ReproInfo, VCFeatureInfo,
829832
};
830833

831834
const NO_DEBUG_DIRECTORIES_BIN: &[u8] =

src/pe/header.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use alloc::vec::Vec;
22
use core::iter::FusedIterator;
33

4-
use scroll::{ctx, IOread, IOwrite, Pread, Pwrite, SizeWith};
4+
use scroll::{IOread, IOwrite, Pread, Pwrite, SizeWith, ctx};
55

66
use crate::error;
77
use crate::pe::{data_directories, debug, optional_header, section_table, symbol};
@@ -1380,14 +1380,14 @@ mod tests {
13801380
use crate::{
13811381
error,
13821382
pe::{
1383-
header::{DosStub, TeHeader},
13841383
Coff,
1384+
header::{DosStub, TeHeader},
13851385
},
13861386
};
13871387

13881388
use super::{
1389-
machine_to_str, DosHeader, Header, RichHeader, RichMetadata, COFF_MACHINE_X86, DOS_MAGIC,
1390-
PE_MAGIC,
1389+
COFF_MACHINE_X86, DOS_MAGIC, DosHeader, Header, PE_MAGIC, RichHeader, RichMetadata,
1390+
machine_to_str,
13911391
};
13921392

13931393
const CRSS_HEADER: [u8; 688] = [

0 commit comments

Comments
 (0)