Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for breaking changes in scroll #114

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 13 additions & 16 deletions src/elf/compression_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -210,8 +210,7 @@ if_alloc! {
}

impl ctx::SizeWith<Ctx> 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
Expand All @@ -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 => {
Expand All @@ -242,32 +240,31 @@ if_alloc! {

impl ctx::TryIntoCtx<Ctx> for CompressionHeader {
type Error = ::error::Error;
type Size = usize;
fn try_into_ctx(self, bytes: &mut [u8], Ctx {container, le}: Ctx) -> result::Result<Self::Size, Self::Error> {
fn try_into_ctx(&self, bytes: &mut [u8], Ctx {container, le}: Ctx) -> result::Result<usize, Self::Error> {
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<Ctx> 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();
}
}
}
Expand Down
17 changes: 7 additions & 10 deletions src/elf/dyn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -303,7 +303,6 @@ if_alloc! {
}

impl ctx::SizeWith<Ctx> for Dyn {
type Units = usize;
fn size_with(&Ctx { container, .. }: &Ctx) -> usize {
match container {
Container::Little => {
Expand All @@ -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 => {
Expand All @@ -335,17 +333,16 @@ if_alloc! {

impl ctx::TryIntoCtx<Ctx> for Dyn {
type Error = ::error::Error;
type Size = usize;
fn try_into_ctx(self, bytes: &mut [u8], Ctx { container, le}: Ctx) -> result::Result<Self::Size, Self::Error> {
fn try_into_ctx(&self, bytes: &mut [u8], Ctx { container, le}: Ctx) -> result::Result<usize, Self::Error> {
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)?)
}
}
}
Expand Down
57 changes: 26 additions & 31 deletions src/elf/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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()));
Expand Down Expand Up @@ -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<scroll::Endian> for Header {
type Error = ::error::Error;
type Size = usize;
fn try_into_ctx(self, bytes: &mut [u8], _ctx: scroll::Endian) -> Result<Self::Size, Self::Error> {
fn try_into_ctx(&self, bytes: &mut [u8], _ctx: scroll::Endian) -> Result<usize, Self::Error> {
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()
}
};
}
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -430,9 +426,8 @@ macro_rules! elf_header_std_impl {

impl ctx::TryIntoCtx<scroll::Endian> 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<Self::Size, Self::Error> {
fn try_into_ctx(&self, bytes: &mut [u8], _endianness: scroll::Endian) -> result::Result<usize, Self::Error> {
use scroll::{Pwrite};
let offset = &mut 0;
let endianness =
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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();
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/elf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand Down
3 changes: 1 addition & 2 deletions src/elf/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 7 additions & 10 deletions src/elf/program_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -178,7 +178,6 @@ if_alloc! {
}

impl ctx::SizeWith<Ctx> for ProgramHeader {
type Units = usize;
fn size_with(ctx: &Ctx) -> usize {
match ctx.container {
Container::Little => {
Expand All @@ -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 => {
Expand All @@ -210,17 +208,16 @@ if_alloc! {

impl ctx::TryIntoCtx<Ctx> for ProgramHeader {
type Error = ::error::Error;
type Size = usize;
fn try_into_ctx(self, bytes: &mut [u8], Ctx {container, le}: Ctx) -> result::Result<Self::Size, Self::Error> {
fn try_into_ctx(&self, bytes: &mut [u8], Ctx {container, le}: Ctx) -> result::Result<usize, Self::Error> {
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)?)
}
}
}
Expand Down
Loading