diff --git a/examples/scroll.rs b/examples/scroll.rs index d9cdc4cfe..d1def7872 100644 --- a/examples/scroll.rs +++ b/examples/scroll.rs @@ -18,7 +18,7 @@ fn run () -> error::Result<()> { println!("header: {:?}", &header); // now lets write the header into some bytes let mut bytes = [0u8; elf64::header::SIZEOF_EHDR]; - bytes.pwrite(header, 0)?; + bytes.pwrite(&header, 0)?; // read it back out let header2: elf64::header::Header = bytes.pread(0)?; // they're the same diff --git a/src/elf/compression_header.rs b/src/elf/compression_header.rs index 6e86122d2..1cb3932d9 100644 --- a/src/elf/compression_header.rs +++ b/src/elf/compression_header.rs @@ -166,7 +166,7 @@ if_alloc! { use scroll::ctx; use container::{Container, Ctx}; - #[derive(Default, PartialEq, Clone)] + #[derive(Default, PartialEq, Clone, Copy)] /// A unified CompressionHeader - convertable to and from 32-bit and 64-bit variants pub struct CompressionHeader { /// Compression format @@ -210,8 +210,7 @@ if_alloc! { } impl ctx::SizeWith for CompressionHeader { - type Units = usize; - fn size_with( &Ctx { container, .. }: &Ctx) -> Self::Units { + fn size_with( &Ctx { container, .. }: &Ctx) -> usize { match container { Container::Little => { compression_header32::SIZEOF_CHDR @@ -225,8 +224,7 @@ if_alloc! { impl<'a> ctx::TryFromCtx<'a, Ctx> for CompressionHeader { type Error = ::error::Error; - type Size = usize; - fn try_from_ctx(bytes: &'a [u8], Ctx {container, le}: Ctx) -> result::Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(bytes: &'a [u8], Ctx {container, le}: Ctx) -> result::Result<(Self, usize), Self::Error> { use scroll::Pread; let res = match container { Container::Little => { @@ -242,32 +240,31 @@ if_alloc! { impl ctx::TryIntoCtx for CompressionHeader { type Error = ::error::Error; - type Size = usize; - fn try_into_ctx(self, bytes: &mut [u8], Ctx {container, le}: Ctx) -> result::Result { + fn try_into_ctx(&self, bytes: &mut [u8], Ctx {container, le}: Ctx) -> result::Result { use scroll::Pwrite; match container { Container::Little => { - let chdr: compression_header32::CompressionHeader = self.into(); - Ok(bytes.pwrite_with(chdr, 0, le)?) + let chdr: compression_header32::CompressionHeader = (*self).into(); + Ok(bytes.pwrite_with(&chdr, 0, le)?) }, Container::Big => { - let chdr: compression_header64::CompressionHeader = self.into(); - Ok(bytes.pwrite_with(chdr, 0, le)?) + let chdr: compression_header64::CompressionHeader = (*self).into(); + Ok(bytes.pwrite_with(&chdr, 0, le)?) } } } } impl ctx::IntoCtx for CompressionHeader { - fn into_ctx(self, bytes: &mut [u8], Ctx {container, le}: Ctx) { + fn into_ctx(&self, bytes: &mut [u8], Ctx {container, le}: Ctx) { use scroll::Pwrite; match container { Container::Little => { - let chdr: compression_header32::CompressionHeader = self.into(); - bytes.pwrite_with(chdr, 0, le).unwrap(); + let chdr: compression_header32::CompressionHeader = (*self).into(); + bytes.pwrite_with(&chdr, 0, le).unwrap(); }, Container::Big => { - let chdr: compression_header64::CompressionHeader = self.into(); - bytes.pwrite_with(chdr, 0, le).unwrap(); + let chdr: compression_header64::CompressionHeader = (*self).into(); + bytes.pwrite_with(&chdr, 0, le).unwrap(); } } } diff --git a/src/elf/dyn.rs b/src/elf/dyn.rs index 653f52def..586dfed23 100644 --- a/src/elf/dyn.rs +++ b/src/elf/dyn.rs @@ -279,7 +279,7 @@ if_alloc! { use strtab::Strtab; use alloc::vec::Vec; - #[derive(Default, PartialEq, Clone)] + #[derive(Default, PartialEq, Clone, Copy)] pub struct Dyn { pub d_tag: u64, pub d_val: u64, @@ -303,7 +303,6 @@ if_alloc! { } impl ctx::SizeWith for Dyn { - type Units = usize; fn size_with(&Ctx { container, .. }: &Ctx) -> usize { match container { Container::Little => { @@ -318,8 +317,7 @@ if_alloc! { impl<'a> ctx::TryFromCtx<'a, Ctx> for Dyn { type Error = ::error::Error; - type Size = usize; - fn try_from_ctx(bytes: &'a [u8], Ctx { container, le}: Ctx) -> result::Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(bytes: &'a [u8], Ctx { container, le}: Ctx) -> result::Result<(Self, usize), Self::Error> { use scroll::Pread; let dyn = match container { Container::Little => { @@ -335,17 +333,16 @@ if_alloc! { impl ctx::TryIntoCtx for Dyn { type Error = ::error::Error; - type Size = usize; - fn try_into_ctx(self, bytes: &mut [u8], Ctx { container, le}: Ctx) -> result::Result { + fn try_into_ctx(&self, bytes: &mut [u8], Ctx { container, le}: Ctx) -> result::Result { use scroll::Pwrite; match container { Container::Little => { - let dyn: dyn32::Dyn = self.into(); - Ok(bytes.pwrite_with(dyn, 0, le)?) + let dyn: dyn32::Dyn = (*self).into(); + Ok(bytes.pwrite_with(&dyn, 0, le)?) }, Container::Big => { - let dyn: dyn64::Dyn = self.into(); - Ok(bytes.pwrite_with(dyn, 0, le)?) + let dyn: dyn64::Dyn = (*self).into(); + Ok(bytes.pwrite_with(&dyn, 0, le)?) } } } diff --git a/src/elf/header.rs b/src/elf/header.rs index f020ff40f..56aa7d6e0 100644 --- a/src/elf/header.rs +++ b/src/elf/header.rs @@ -266,7 +266,6 @@ if_alloc! { } impl ctx::SizeWith<::container::Ctx> for Header { - type Units = usize; fn size_with(ctx: &::container::Ctx) -> usize { match ctx.container { Container::Little => { @@ -281,8 +280,7 @@ if_alloc! { impl<'a> ctx::TryFromCtx<'a, scroll::Endian> for Header { type Error = ::error::Error; - type Size = usize; - fn try_from_ctx(bytes: &'a [u8], _ctx: scroll::Endian) -> error::Result<(Self, Self::Size)> { + fn try_from_ctx(bytes: &'a [u8], _ctx: scroll::Endian) -> error::Result<(Self, usize)> { use scroll::Pread; if bytes.len() < SIZEOF_IDENT { return Err(error::Error::Malformed("Too small".to_string())); @@ -310,28 +308,27 @@ if_alloc! { // TODO: i think we should remove this forcing of the information in the header, it causes too many conflicts impl ctx::TryIntoCtx for Header { type Error = ::error::Error; - type Size = usize; - fn try_into_ctx(self, bytes: &mut [u8], _ctx: scroll::Endian) -> Result { + fn try_into_ctx(&self, bytes: &mut [u8], _ctx: scroll::Endian) -> Result { use scroll::Pwrite; match self.container()? { Container::Little => { - bytes.pwrite(header32::Header::from(self), 0) + bytes.pwrite(&header32::Header::from(*self), 0) }, Container::Big => { - bytes.pwrite(header64::Header::from(self), 0) + bytes.pwrite(&header64::Header::from(*self), 0) } } } } impl ctx::IntoCtx<::container::Ctx> for Header { - fn into_ctx(self, bytes: &mut [u8], ctx: ::container::Ctx) -> () { + fn into_ctx(&self, bytes: &mut [u8], ctx: ::container::Ctx) -> () { use scroll::Pwrite; match ctx.container { Container::Little => { - bytes.pwrite_with(header32::Header::from(self), 0, ctx.le).unwrap() + bytes.pwrite_with(&header32::Header::from(*self), 0, ctx.le).unwrap() }, Container::Big => { - bytes.pwrite_with(header64::Header::from(self), 0, ctx.le).unwrap() + bytes.pwrite_with(&header64::Header::from(*self), 0, ctx.le).unwrap() } }; } @@ -400,8 +397,7 @@ macro_rules! elf_header_std_impl { impl<'a> ctx::TryFromCtx<'a, scroll::Endian> for Header { type Error = ::error::Error; - type Size = usize; - fn try_from_ctx(bytes: &'a [u8], _: scroll::Endian) -> result::Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(bytes: &'a [u8], _: scroll::Endian) -> result::Result<(Self, usize), Self::Error> { let mut elf_header = Header::default(); let offset = &mut 0; bytes.gread_inout(offset, &mut elf_header.e_ident)?; @@ -430,9 +426,8 @@ macro_rules! elf_header_std_impl { impl ctx::TryIntoCtx for Header { type Error = ::error::Error; - type Size = usize; /// a Pwrite impl for Header: **note** we use the endianness value in the header, and not a parameter - fn try_into_ctx(self, bytes: &mut [u8], _endianness: scroll::Endian) -> result::Result { + fn try_into_ctx(&self, bytes: &mut [u8], _endianness: scroll::Endian) -> result::Result { use scroll::{Pwrite}; let offset = &mut 0; let endianness = @@ -442,21 +437,21 @@ macro_rules! elf_header_std_impl { d => return Err(Error::Malformed(format!("invalid ELF DATA type {:x}", d)).into()), }; for i in 0..self.e_ident.len() { - bytes.gwrite(self.e_ident[i], offset)?; + bytes.gwrite(&self.e_ident[i], offset)?; } - bytes.gwrite_with(self.e_type , offset, endianness)?; - bytes.gwrite_with(self.e_machine , offset, endianness)?; - bytes.gwrite_with(self.e_version , offset, endianness)?; - bytes.gwrite_with(self.e_entry , offset, endianness)?; - bytes.gwrite_with(self.e_phoff , offset, endianness)?; - bytes.gwrite_with(self.e_shoff , offset, endianness)?; - bytes.gwrite_with(self.e_flags , offset, endianness)?; - bytes.gwrite_with(self.e_ehsize , offset, endianness)?; - bytes.gwrite_with(self.e_phentsize , offset, endianness)?; - bytes.gwrite_with(self.e_phnum , offset, endianness)?; - bytes.gwrite_with(self.e_shentsize , offset, endianness)?; - bytes.gwrite_with(self.e_shnum , offset, endianness)?; - bytes.gwrite_with(self.e_shstrndx , offset, endianness)?; + bytes.gwrite_with(&self.e_type , offset, endianness)?; + bytes.gwrite_with(&self.e_machine , offset, endianness)?; + bytes.gwrite_with(&self.e_version , offset, endianness)?; + bytes.gwrite_with(&self.e_entry , offset, endianness)?; + bytes.gwrite_with(&self.e_phoff , offset, endianness)?; + bytes.gwrite_with(&self.e_shoff , offset, endianness)?; + bytes.gwrite_with(&self.e_flags , offset, endianness)?; + bytes.gwrite_with(&self.e_ehsize , offset, endianness)?; + bytes.gwrite_with(&self.e_phentsize , offset, endianness)?; + bytes.gwrite_with(&self.e_phnum , offset, endianness)?; + bytes.gwrite_with(&self.e_shentsize , offset, endianness)?; + bytes.gwrite_with(&self.e_shnum , offset, endianness)?; + bytes.gwrite_with(&self.e_shstrndx , offset, endianness)?; Ok(SIZEOF_EHDR) } } @@ -533,7 +528,7 @@ macro_rules! elf_header_test { assert_eq!(header.e_type, ET_REL); println!("header: {:?}", &header); let mut bytes = [0u8; SIZEOF_EHDR]; - bytes.pwrite(header, 0).unwrap(); + bytes.pwrite(&header, 0).unwrap(); let header2: Header = bytes.pread(0).unwrap(); assert_eq!(header, header2); } @@ -550,14 +545,14 @@ macro_rules! elf_header_test { println!("header: {:?}", &header); let mut bytes = [0u8; SIZEOF_EHDR]; let header_ = Header::from(header.clone()); - bytes.pwrite(header_, 0).unwrap(); + bytes.pwrite(&header_, 0).unwrap(); let header2: Header = bytes.pread(0).unwrap(); assert_eq!(header, header2); let header = ElfHeader::new(Ctx::from(container)); println!("header: {:?}", &header); let mut bytes = vec![0; 100]; - bytes.pwrite(header, 0).unwrap(); + bytes.pwrite(&header, 0).unwrap(); } } } diff --git a/src/elf/mod.rs b/src/elf/mod.rs index c056584ae..eec4cd298 100644 --- a/src/elf/mod.rs +++ b/src/elf/mod.rs @@ -347,8 +347,7 @@ if_sylvan! { impl<'a> ctx::TryFromCtx<'a, (usize, Endian)> for Elf<'a> { type Error = ::error::Error; - type Size = usize; - fn try_from_ctx(src: &'a [u8], (_, _): (usize, Endian)) -> Result<(Elf<'a>, Self::Size), Self::Error> { + fn try_from_ctx(src: &'a [u8], (_, _): (usize, Endian)) -> Result<(Elf<'a>, usize), Self::Error> { let elf = Elf::parse(src)?; Ok((elf, src.len())) } diff --git a/src/elf/note.rs b/src/elf/note.rs index 2ea894cba..b627799e2 100644 --- a/src/elf/note.rs +++ b/src/elf/note.rs @@ -166,8 +166,7 @@ if_alloc! { impl<'a> ctx::TryFromCtx<'a, (usize, container::Ctx)> for Note<'a> { type Error = error::Error; - type Size = usize; - fn try_from_ctx(bytes: &'a [u8], (alignment, ctx): (usize, container::Ctx)) -> Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(bytes: &'a [u8], (alignment, ctx): (usize, container::Ctx)) -> Result<(Self, usize), Self::Error> { let offset = &mut 0; let mut alignment = alignment; if alignment < 4 { diff --git a/src/elf/program_header.rs b/src/elf/program_header.rs index 69a907f4d..93c62c0b4 100644 --- a/src/elf/program_header.rs +++ b/src/elf/program_header.rs @@ -83,7 +83,7 @@ if_alloc! { use container::{Ctx, Container}; use alloc::vec::Vec; - #[derive(Default, PartialEq, Clone)] + #[derive(Default, PartialEq, Clone, Copy)] /// A unified ProgramHeader - convertable to and from 32-bit and 64-bit variants pub struct ProgramHeader { pub p_type : u32, @@ -178,7 +178,6 @@ if_alloc! { } impl ctx::SizeWith for ProgramHeader { - type Units = usize; fn size_with(ctx: &Ctx) -> usize { match ctx.container { Container::Little => { @@ -193,8 +192,7 @@ if_alloc! { impl<'a> ctx::TryFromCtx<'a, Ctx> for ProgramHeader { type Error = ::error::Error; - type Size = usize; - fn try_from_ctx(bytes: &'a [u8], Ctx { container, le}: Ctx) -> result::Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(bytes: &'a [u8], Ctx { container, le}: Ctx) -> result::Result<(Self, usize), Self::Error> { use scroll::Pread; let res = match container { Container::Little => { @@ -210,17 +208,16 @@ if_alloc! { impl ctx::TryIntoCtx for ProgramHeader { type Error = ::error::Error; - type Size = usize; - fn try_into_ctx(self, bytes: &mut [u8], Ctx {container, le}: Ctx) -> result::Result { + fn try_into_ctx(&self, bytes: &mut [u8], Ctx {container, le}: Ctx) -> result::Result { use scroll::Pwrite; match container { Container::Little => { - let phdr: program_header32::ProgramHeader = self.into(); - Ok(bytes.pwrite_with(phdr, 0, le)?) + let phdr: program_header32::ProgramHeader = (*self).into(); + Ok(bytes.pwrite_with(&phdr, 0, le)?) }, Container::Big => { - let phdr: program_header64::ProgramHeader = self.into(); - Ok(bytes.pwrite_with(phdr, 0, le)?) + let phdr: program_header64::ProgramHeader = (*self).into(); + Ok(bytes.pwrite_with(&phdr, 0, le)?) } } } diff --git a/src/elf/reloc.rs b/src/elf/reloc.rs index d37a9e3a4..9ec1d0530 100644 --- a/src/elf/reloc.rs +++ b/src/elf/reloc.rs @@ -284,8 +284,7 @@ if_alloc! { type RelocCtx = (bool, Ctx); impl ctx::SizeWith for Reloc { - type Units = usize; - fn size_with( &(is_rela, Ctx { container, .. }): &RelocCtx) -> Self::Units { + fn size_with( &(is_rela, Ctx { container, .. }): &RelocCtx) -> usize { match container { Container::Little => { if is_rela { reloc32::SIZEOF_RELA } else { reloc32::SIZEOF_REL } @@ -299,8 +298,7 @@ if_alloc! { impl<'a> ctx::TryFromCtx<'a, RelocCtx> for Reloc { type Error = ::error::Error; - type Size = usize; - fn try_from_ctx(bytes: &'a [u8], (is_rela, Ctx { container, le }): RelocCtx) -> result::Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(bytes: &'a [u8], (is_rela, Ctx { container, le }): RelocCtx) -> result::Result<(Self, usize), Self::Error> { use scroll::Pread; let reloc = match container { Container::Little => { @@ -324,28 +322,27 @@ if_alloc! { impl ctx::TryIntoCtx for Reloc { type Error = ::error::Error; - type Size = usize; // TODO: I think this is a bad idea /// Writes the relocation into `bytes` - fn try_into_ctx(self, bytes: &mut [u8], (is_rela, Ctx {container, le}): RelocCtx) -> result::Result { + fn try_into_ctx(&self, bytes: &mut [u8], (is_rela, Ctx {container, le}): RelocCtx) -> result::Result { use scroll::Pwrite; match container { Container::Little => { if is_rela { - let rela: reloc32::Rela = self.into(); - Ok(bytes.pwrite_with(rela, 0, le)?) + let rela: reloc32::Rela = (*self).into(); + Ok(bytes.pwrite_with(&rela, 0, le)?) } else { - let rel: reloc32::Rel = self.into(); - Ok(bytes.pwrite_with(rel, 0, le)?) + let rel: reloc32::Rel = (*self).into(); + Ok(bytes.pwrite_with(&rel, 0, le)?) } }, Container::Big => { if is_rela { - let rela: reloc64::Rela = self.into(); - Ok(bytes.pwrite_with(rela, 0, le)?) + let rela: reloc64::Rela = (*self).into(); + Ok(bytes.pwrite_with(&rela, 0, le)?) } else { - let rel: reloc64::Rel = self.into(); - Ok(bytes.pwrite_with(rel, 0, le)?) + let rel: reloc64::Rel = (*self).into(); + Ok(bytes.pwrite_with(&rel, 0, le)?) } }, } @@ -354,7 +351,7 @@ if_alloc! { impl ctx::IntoCtx<(bool, Ctx)> for Reloc { /// Writes the relocation into `bytes` - fn into_ctx(self, bytes: &mut [u8], ctx: RelocCtx) { + fn into_ctx(&self, bytes: &mut [u8], ctx: RelocCtx) { use scroll::Pwrite; bytes.pwrite_with(self, 0, ctx).unwrap(); } diff --git a/src/elf/section_header.rs b/src/elf/section_header.rs index 2834609a5..561732289 100644 --- a/src/elf/section_header.rs +++ b/src/elf/section_header.rs @@ -372,7 +372,7 @@ if_alloc! { #[cfg(feature = "endian_fd")] use alloc::vec::Vec; - #[derive(Default, PartialEq, Clone)] + #[derive(Default, PartialEq, Clone, Copy)] /// A unified SectionHeader - convertable to and from 32-bit and 64-bit variants pub struct SectionHeader { /// Section name (string tbl index) @@ -482,8 +482,7 @@ if_alloc! { } impl ctx::SizeWith for SectionHeader { - type Units = usize; - fn size_with( &Ctx { container, .. }: &Ctx) -> Self::Units { + fn size_with( &Ctx { container, .. }: &Ctx) -> usize { match container { Container::Little => { section_header32::SIZEOF_SHDR @@ -497,8 +496,7 @@ if_alloc! { impl<'a> ctx::TryFromCtx<'a, Ctx> for SectionHeader { type Error = ::error::Error; - type Size = usize; - fn try_from_ctx(bytes: &'a [u8], Ctx {container, le}: Ctx) -> result::Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(bytes: &'a [u8], Ctx {container, le}: Ctx) -> result::Result<(Self, usize), Self::Error> { use scroll::Pread; let res = match container { Container::Little => { @@ -514,32 +512,31 @@ if_alloc! { impl ctx::TryIntoCtx for SectionHeader { type Error = ::error::Error; - type Size = usize; - fn try_into_ctx(self, bytes: &mut [u8], Ctx {container, le}: Ctx) -> result::Result { + fn try_into_ctx(&self, bytes: &mut [u8], Ctx {container, le}: Ctx) -> result::Result { use scroll::Pwrite; match container { Container::Little => { - let shdr: section_header32::SectionHeader = self.into(); - Ok(bytes.pwrite_with(shdr, 0, le)?) + let shdr: section_header32::SectionHeader = (*self).into(); + Ok(bytes.pwrite_with(&shdr, 0, le)?) }, Container::Big => { - let shdr: section_header64::SectionHeader = self.into(); - Ok(bytes.pwrite_with(shdr, 0, le)?) + let shdr: section_header64::SectionHeader = (*self).into(); + Ok(bytes.pwrite_with(&shdr, 0, le)?) } } } } impl ctx::IntoCtx for SectionHeader { - fn into_ctx(self, bytes: &mut [u8], Ctx {container, le}: Ctx) { + fn into_ctx(&self, bytes: &mut [u8], Ctx {container, le}: Ctx) { use scroll::Pwrite; match container { Container::Little => { - let shdr: section_header32::SectionHeader = self.into(); - bytes.pwrite_with(shdr, 0, le).unwrap(); + let shdr: section_header32::SectionHeader = (*self).into(); + bytes.pwrite_with(&shdr, 0, le).unwrap(); }, Container::Big => { - let shdr: section_header64::SectionHeader = self.into(); - bytes.pwrite_with(shdr, 0, le).unwrap(); + let shdr: section_header64::SectionHeader = (*self).into(); + bytes.pwrite_with(&shdr, 0, le).unwrap(); } } } diff --git a/src/elf/sym.rs b/src/elf/sym.rs index ce620e3fd..7d5051366 100644 --- a/src/elf/sym.rs +++ b/src/elf/sym.rs @@ -280,7 +280,7 @@ if_alloc! { use error::Result; use alloc::vec::Vec; - #[derive(Default, PartialEq, Clone)] + #[derive(Default, PartialEq, Clone, Copy)] /// A unified Sym definition - convertable to and from 32-bit and 64-bit variants pub struct Sym { pub st_name: usize, @@ -352,7 +352,6 @@ if_alloc! { } impl ctx::SizeWith for Sym { - type Units = usize; #[inline] fn size_with(&Ctx {container, .. }: &Ctx) -> usize { match container { @@ -368,9 +367,8 @@ if_alloc! { impl<'a> ctx::TryFromCtx<'a, Ctx> for Sym { type Error = ::error::Error; - type Size = usize; #[inline] - fn try_from_ctx(bytes: &'a [u8], Ctx { container, le}: Ctx) -> result::Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(bytes: &'a [u8], Ctx { container, le}: Ctx) -> result::Result<(Self, usize), Self::Error> { use scroll::Pread; let sym = match container { Container::Little => { @@ -386,18 +384,17 @@ if_alloc! { impl ctx::TryIntoCtx for Sym { type Error = ::error::Error; - type Size = usize; #[inline] - fn try_into_ctx(self, bytes: &mut [u8], Ctx {container, le}: Ctx) -> result::Result { + fn try_into_ctx(&self, bytes: &mut [u8], Ctx {container, le}: Ctx) -> result::Result { use scroll::Pwrite; match container { Container::Little => { - let sym: sym32::Sym = self.into(); - Ok(bytes.pwrite_with(sym, 0, le)?) + let sym: sym32::Sym = (*self).into(); + Ok(bytes.pwrite_with(&sym, 0, le)?) }, Container::Big => { - let sym: sym64::Sym = self.into(); - Ok(bytes.pwrite_with(sym, 0, le)?) + let sym: sym64::Sym = (*self).into(); + Ok(bytes.pwrite_with(&sym, 0, le)?) } } } @@ -405,16 +402,16 @@ if_alloc! { impl ctx::IntoCtx for Sym { #[inline] - fn into_ctx(self, bytes: &mut [u8], Ctx {container, le}: Ctx) { + fn into_ctx(&self, bytes: &mut [u8], Ctx {container, le}: Ctx) { use scroll::Pwrite; match container { Container::Little => { - let sym: sym32::Sym = self.into(); - bytes.pwrite_with(sym, 0, le).unwrap(); + let sym: sym32::Sym = (*self).into(); + bytes.pwrite_with(&sym, 0, le).unwrap(); }, Container::Big => { - let sym: sym64::Sym = self.into(); - bytes.pwrite_with(sym, 0, le).unwrap(); + let sym: sym64::Sym = (*self).into(); + bytes.pwrite_with(&sym, 0, le).unwrap(); } } } diff --git a/src/mach/header.rs b/src/mach/header.rs index b41677010..3f8756537 100644 --- a/src/mach/header.rs +++ b/src/mach/header.rs @@ -340,7 +340,6 @@ impl Header { } impl ctx::SizeWith for Header { - type Units = usize; fn size_with(container: &container::Ctx) -> usize { match container.container { Container::Little => { @@ -354,7 +353,6 @@ impl ctx::SizeWith for Header { } impl ctx::SizeWith for Header { - type Units = usize; fn size_with(container: &Container) -> usize { match container { &Container::Little => { @@ -369,8 +367,7 @@ impl ctx::SizeWith for Header { impl<'a> ctx::TryFromCtx<'a, container::Ctx> for Header { type Error = ::error::Error; - type Size = usize; - fn try_from_ctx(bytes: &'a [u8], container::Ctx { le, container }: container::Ctx) -> error::Result<(Self, Self::Size)> { + fn try_from_ctx(bytes: &'a [u8], container::Ctx { le, container }: container::Ctx) -> error::Result<(Self, usize)> { let size = bytes.len(); if size < SIZEOF_HEADER_32 || size < SIZEOF_HEADER_64 { let error = error::Error::Malformed(format!("bytes size is smaller than a Mach-o header")); @@ -392,14 +389,13 @@ impl<'a> ctx::TryFromCtx<'a, container::Ctx> for Header { impl ctx::TryIntoCtx for Header { type Error = ::error::Error; - type Size = usize; - fn try_into_ctx(self, bytes: &mut [u8], ctx: container::Ctx) -> error::Result { + fn try_into_ctx(&self, bytes: &mut [u8], ctx: container::Ctx) -> error::Result { match ctx.container { Container::Little => { - bytes.pwrite_with(Header32::from(self), 0, ctx.le)?; + bytes.pwrite_with(&Header32::from(*self), 0, ctx.le)?; }, Container::Big => { - bytes.pwrite_with(Header64::from(self), 0, ctx.le)?; + bytes.pwrite_with(&Header64::from(*self), 0, ctx.le)?; } }; Ok(Header::size_with(&ctx)) @@ -407,7 +403,7 @@ impl ctx::TryIntoCtx for Header { } impl ctx::IntoCtx for Header { - fn into_ctx(self, bytes: &mut [u8], ctx: container::Ctx) { + fn into_ctx(&self, bytes: &mut [u8], ctx: container::Ctx) { bytes.pwrite_with(self, 0, ctx).unwrap(); } } diff --git a/src/mach/load_command.rs b/src/mach/load_command.rs index e9e4987ed..508372c70 100644 --- a/src/mach/load_command.rs +++ b/src/mach/load_command.rs @@ -496,8 +496,7 @@ impl ThreadCommand { impl<'a> ctx::TryFromCtx<'a, Endian> for ThreadCommand { type Error = ::error::Error; - type Size = usize; - fn try_from_ctx(bytes: &'a [u8], le: Endian) -> error::Result<(Self, Self::Size)> { + fn try_from_ctx(bytes: &'a [u8], le: Endian) -> error::Result<(Self, usize)> { use scroll::{Pread}; let lc = bytes.pread_with::(0, le)?; @@ -1311,8 +1310,7 @@ pub enum CommandVariant { impl<'a> ctx::TryFromCtx<'a, Endian> for CommandVariant { type Error = ::error::Error; - type Size = usize; - fn try_from_ctx(bytes: &'a [u8], le: Endian) -> error::Result<(Self, Self::Size)> { + fn try_from_ctx(bytes: &'a [u8], le: Endian) -> error::Result<(Self, usize)> { use scroll::{Pread}; use self::CommandVariant::*; let lc = bytes.pread_with::(0, le)?; diff --git a/src/mach/segment.rs b/src/mach/segment.rs index 4ced79b3c..ee01631bf 100644 --- a/src/mach/segment.rs +++ b/src/mach/segment.rs @@ -36,7 +36,7 @@ impl<'a> Iterator for RelocationIterator<'a> { } /// Generalized 32/64 bit Section -#[derive(Default)] +#[derive(Default, Copy, Clone)] pub struct Section { /// name of this section pub sectname: [u8; 16], @@ -168,8 +168,7 @@ impl From for Section { impl<'a> ctx::TryFromCtx<'a, container::Ctx> for Section { type Error = ::error::Error; - type Size = usize; - fn try_from_ctx(bytes: &'a [u8], ctx: container::Ctx) -> Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(bytes: &'a [u8], ctx: container::Ctx) -> Result<(Self, usize), Self::Error> { match ctx.container { container::Container::Little => { let section = Section::from(bytes.pread_with::(0, ctx.le)?); @@ -184,7 +183,6 @@ impl<'a> ctx::TryFromCtx<'a, container::Ctx> for Section { } impl ctx::SizeWith for Section { - type Units = usize; fn size_with(ctx: &container::Ctx) -> usize { match ctx.container { container::Container::Little => SIZEOF_SECTION_32, @@ -195,19 +193,18 @@ impl ctx::SizeWith for Section { impl ctx::TryIntoCtx for Section { type Error = ::error::Error; - type Size = usize; - fn try_into_ctx(self, bytes: &mut [u8], ctx: container::Ctx) -> Result { + fn try_into_ctx(&self, bytes: &mut [u8], ctx: container::Ctx) -> Result { if ctx.is_big () { - bytes.pwrite_with::(self.into(), 0, ctx.le)?; + bytes.pwrite_with::(&(*self).into(), 0, ctx.le)?; } else { - bytes.pwrite_with::(self.into(), 0, ctx.le)?; + bytes.pwrite_with::(&(*self).into(), 0, ctx.le)?; } Ok(Self::size_with(&ctx)) } } impl ctx::IntoCtx for Section { - fn into_ctx(self, bytes: &mut [u8], ctx: container::Ctx) { + fn into_ctx(&self, bytes: &mut [u8], ctx: container::Ctx) { bytes.pwrite_with(self, 0, ctx).unwrap(); } } @@ -276,6 +273,7 @@ impl<'a, 'b> IntoIterator for &'b Segment<'a> { } /// Generalized 32/64 bit Segment Command +#[derive(Copy,Clone)] pub struct Segment<'a> { pub cmd: u32, pub cmdsize: u32, @@ -353,7 +351,6 @@ impl<'a> fmt::Debug for Segment<'a> { } impl<'a> ctx::SizeWith for Segment<'a> { - type Units = usize; fn size_with(ctx: &container::Ctx) -> usize { match ctx.container { container::Container::Little => SIZEOF_SEGMENT_COMMAND_32, @@ -364,16 +361,15 @@ impl<'a> ctx::SizeWith for Segment<'a> { impl<'a> ctx::TryIntoCtx for Segment<'a> { type Error = ::error::Error; - type Size = usize; - fn try_into_ctx(self, bytes: &mut [u8], ctx: container::Ctx) -> Result { + fn try_into_ctx(&self, bytes: &mut [u8], ctx: container::Ctx) -> Result { let segment_size = Self::size_with(&ctx); // should be able to write the section data inline after this, but not working at the moment //let section_size = bytes.pwrite(data, segment_size)?; //debug!("Segment size: {} raw section data size: {}", segment_size, data.len()); if ctx.is_big () { - bytes.pwrite_with::(self.into(), 0, ctx.le)?; + bytes.pwrite_with::(&(*self).into(), 0, ctx.le)?; } else { - bytes.pwrite_with::(self.into(), 0, ctx.le)?; + bytes.pwrite_with::(&(*self).into(), 0, ctx.le)?; } //debug!("Section size: {}", section_size); Ok(segment_size) @@ -381,7 +377,7 @@ impl<'a> ctx::TryIntoCtx for Segment<'a> { } impl<'a> ctx::IntoCtx for Segment<'a> { - fn into_ctx(self, bytes: &mut [u8], ctx: container::Ctx) { + fn into_ctx(&self, bytes: &mut [u8], ctx: container::Ctx) { bytes.pwrite_with(self, 0, ctx).unwrap(); } } diff --git a/src/mach/symbols.rs b/src/mach/symbols.rs index a96536120..739d36a0c 100644 --- a/src/mach/symbols.rs +++ b/src/mach/symbols.rs @@ -151,7 +151,7 @@ impl Debug for Nlist64 { } } -#[derive(Debug, Clone,)] +#[derive(Debug, Clone, Copy)] pub struct Nlist { /// index into the string table pub n_strx: usize, @@ -189,7 +189,6 @@ impl Nlist { } impl ctx::SizeWith for Nlist { - type Units = usize; fn size_with(ctx: &container::Ctx) -> usize { use container::Container; match ctx.container { @@ -253,8 +252,7 @@ impl From for Nlist64 { impl<'a> ctx::TryFromCtx<'a, container::Ctx> for Nlist { type Error = ::error::Error; - type Size = usize; - fn try_from_ctx(bytes: &'a [u8], container::Ctx { container, le }: container::Ctx) -> ::error::Result<(Self, Self::Size)> { + fn try_from_ctx(bytes: &'a [u8], container::Ctx { container, le }: container::Ctx) -> ::error::Result<(Self, usize)> { let nlist = match container { Container::Little => { (bytes.pread_with::(0, le)?.into(), SIZEOF_NLIST_32) @@ -269,15 +267,14 @@ impl<'a> ctx::TryFromCtx<'a, container::Ctx> for Nlist { impl ctx::TryIntoCtx for Nlist { type Error = ::error::Error; - type Size = usize; - fn try_into_ctx(self, bytes: &mut [u8], container::Ctx { container, le }: container::Ctx) -> Result { + fn try_into_ctx(&self, bytes: &mut [u8], container::Ctx { container, le }: container::Ctx) -> Result { let size = match container { Container::Little => { - (bytes.pwrite_with::(self.into(), 0, le)?) + (bytes.pwrite_with::(&(*self).into(), 0, le)?) }, Container::Big => { - (bytes.pwrite_with::(self.into(), 0, le)?) + (bytes.pwrite_with::(&(*self).into(), 0, le)?) }, }; Ok(size) @@ -285,7 +282,7 @@ impl ctx::TryIntoCtx for Nlist { } impl ctx::IntoCtx for Nlist { - fn into_ctx(self, bytes: &mut [u8], ctx: container::Ctx) { + fn into_ctx(&self, bytes: &mut [u8], ctx: container::Ctx) { bytes.pwrite_with(self, 0, ctx).unwrap(); } } @@ -299,10 +296,9 @@ pub struct SymbolsCtx { impl<'a, T: ?Sized> ctx::TryFromCtx<'a, SymbolsCtx, T> for Symbols<'a> where T: AsRef<[u8]> { type Error = ::error::Error; - type Size = usize; fn try_from_ctx(bytes: &'a T, SymbolsCtx { nsyms, strtab, ctx - }: SymbolsCtx) -> ::error::Result<(Self, Self::Size)> { + }: SymbolsCtx) -> ::error::Result<(Self, usize)> { let data = bytes.as_ref(); Ok ((Symbols { data: data, diff --git a/src/pe/export.rs b/src/pe/export.rs index 06c0adecf..d635a7046 100644 --- a/src/pe/export.rs +++ b/src/pe/export.rs @@ -145,9 +145,8 @@ pub enum Reexport<'a> { impl<'a> scroll::ctx::TryFromCtx<'a, scroll::Endian> for Reexport<'a> { type Error = ::error::Error; - type Size = usize; #[inline] - fn try_from_ctx(bytes: &'a [u8], _ctx: scroll::Endian) -> Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(bytes: &'a [u8], _ctx: scroll::Endian) -> Result<(Self, usize), Self::Error> { use scroll::{Pread}; let reexport = bytes.pread::<&str>(0)?; let reexport_len = reexport.len(); @@ -216,9 +215,8 @@ struct ExportCtx<'a> { impl<'a, 'b> scroll::ctx::TryFromCtx<'a, ExportCtx<'b>> for Export<'a> { type Error = error::Error; - type Size = usize; #[inline] - fn try_from_ctx(bytes: &'a [u8], ExportCtx { ptr, idx, sections, file_alignment, addresses, ordinals }: ExportCtx<'b>) -> Result<(Self, Self::Size), Self::Error> { + fn try_from_ctx(bytes: &'a [u8], ExportCtx { ptr, idx, sections, file_alignment, addresses, ordinals }: ExportCtx<'b>) -> Result<(Self, usize), Self::Error> { use self::ExportAddressTableEntry::*; let name = utils::find_offset(ptr as usize, sections, file_alignment).map_or(None, |offset| bytes.pread::<&str>(offset).ok()); diff --git a/src/pe/import.rs b/src/pe/import.rs index afee0dc3b..fce6d7bdc 100644 --- a/src/pe/import.rs +++ b/src/pe/import.rs @@ -16,7 +16,7 @@ pub const IMPORT_BY_ORDINAL_64: u64 = 0x8000_0000_0000_0000; pub const IMPORT_RVA_MASK_32: u32 = 0x8fff_ffff; pub const IMPORT_RVA_MASK_64: u64 = 0x0000_0000_8fff_ffff; -pub trait Bitfield<'a>: Into + PartialEq + Eq + LowerHex + Debug + TryFromCtx<'a, scroll::Endian, Error=scroll::Error, Size=usize> { +pub trait Bitfield<'a>: Into + PartialEq + Eq + LowerHex + Debug + TryFromCtx<'a, scroll::Endian, Error=scroll::Error> { fn is_ordinal(&self) -> bool; fn to_ordinal(&self) -> u16; fn to_rva(&self) -> u32; diff --git a/src/pe/optional_header.rs b/src/pe/optional_header.rs index 8d063e0c0..999ae1424 100644 --- a/src/pe/optional_header.rs +++ b/src/pe/optional_header.rs @@ -262,8 +262,7 @@ impl OptionalHeader { impl<'a> ctx::TryFromCtx<'a, Endian> for OptionalHeader { type Error = ::error::Error; - type Size = usize; - fn try_from_ctx(bytes: &'a [u8], _: Endian) -> error::Result<(Self, Self::Size)> { + fn try_from_ctx(bytes: &'a [u8], _: Endian) -> error::Result<(Self, usize)> { let magic = bytes.pread_with::(0, LE)?; let offset = &mut 0; let (standard_fields, windows_fields): (StandardFields, WindowsFields) = match magic {