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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
macro_rules! elf_compression_header {
    () => {
        use plain;
        // Declare that this is a plain type.
        unsafe impl plain::Plain for CompressionHeader {}

        impl ::core::fmt::Debug for CompressionHeader {
            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
                f.debug_struct("CompressionHeader")
                    .field("ch_type", &self.ch_type)
                    .field("ch_size", &format_args!("0x{:x}", self.ch_size))
                    .field("ch_addralign", &format_args!("0x{:x}", self.ch_addralign))
                    .finish()
            }
        }
    }
}

/// ZLIB/DEFLATE algorithm.
pub const ELFCOMPRESS_ZLIB: u32 = 1;
/// Start of OS-specific.
pub const ELFCOMPRESS_LOOS: u32 = 0x6000_0000;
/// End of OS-specific.
pub const ELFCOMPRESS_HIOS: u32 = 0x6fff_ffff;
/// Start of processor-specific.
pub const ELFCOMPRESS_LOPROC: u32 = 0x7000_0000;
/// End of processor-specific.
pub const ELFCOMPRESS_HIPROC: u32 = 0x7fff_ffff;

macro_rules! elf_compression_header_std_impl { ($size:ty) => {

    #[cfg(test)]
    mod tests {
        use super::*;
        #[test]
        fn size_of() {
            assert_eq!(::std::mem::size_of::<CompressionHeader>(), SIZEOF_CHDR);
        }
    }

    if_alloc! {
        use crate::elf::compression_header::CompressionHeader as ElfCompressionHeader;

        use plain::Plain;

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

            use std::fs::File;
            use std::io::{Read, Seek};
            use std::io::SeekFrom::Start;
        }

        impl From<CompressionHeader> for ElfCompressionHeader {
            fn from(ch: CompressionHeader) -> Self {
                ElfCompressionHeader {
                    ch_type: ch.ch_type,
                    ch_size: u64::from(ch.ch_size),
                    ch_addralign: u64::from(ch.ch_addralign),
                }
            }
        }

        impl CompressionHeader {
            pub fn from_bytes(bytes: &[u8]) -> CompressionHeader {
                let mut chdr = CompressionHeader::default();
                chdr.copy_from_bytes(bytes).expect("buffer is too short for header");
                chdr
            }

            #[cfg(feature = "std")]
            pub fn from_fd(fd: &mut File, offset: u64) -> Result<CompressionHeader> {
                let mut chdr = CompressionHeader::default();
                fd.seek(Start(offset))?;
                unsafe {
                    fd.read_exact(plain::as_mut_bytes(&mut chdr))?;
                }
                Ok(chdr)
            }
        }
    } // end if_alloc
};}

#[cfg(feature = "alloc")]
use scroll::{Pread, Pwrite, SizeWith};

pub mod compression_header32 {
    pub use crate::elf::compression_header::*;

    #[repr(C)]
    #[derive(Copy, Clone, Eq, PartialEq, Default)]
    #[cfg_attr(feature = "alloc", derive(Pread, Pwrite, SizeWith))]
    /// The compression header is used at the start of SHF_COMPRESSED sections
    pub struct CompressionHeader {
        /// Compression format
        pub ch_type: u32,
        /// Uncompressed data size
        pub ch_size: u32,
        /// Uncompressed data alignment
        pub ch_addralign: u32,
    }

    elf_compression_header!();

    pub const SIZEOF_CHDR: usize = 12;

    elf_compression_header_std_impl!(u32);

    if_alloc! {
        impl From<ElfCompressionHeader> for CompressionHeader {
            fn from(ch: ElfCompressionHeader) -> Self {
                CompressionHeader {
                    ch_type: ch.ch_type,
                    ch_size: ch.ch_size as u32,
                    ch_addralign: ch.ch_addralign as u32,
                }
            }
        }
    }
}


pub mod compression_header64 {
    pub use crate::elf::compression_header::*;

    #[repr(C)]
    #[derive(Copy, Clone, Eq, PartialEq, Default)]
    #[cfg_attr(feature = "alloc", derive(Pread, Pwrite, SizeWith))]
    /// The compression header is used at the start of SHF_COMPRESSED sections
    pub struct CompressionHeader {
        /// Compression format
        pub ch_type: u32,
        pub ch_reserved: u32,
        /// Uncompressed data size
        pub ch_size: u64,
        /// Uncompressed data alignment
        pub ch_addralign: u64,
    }

    elf_compression_header!();

    pub const SIZEOF_CHDR: usize = 24;

    elf_compression_header_std_impl!(u64);

    if_alloc! {
        impl From<ElfCompressionHeader> for CompressionHeader {
            fn from(ch: ElfCompressionHeader) -> Self {
                CompressionHeader {
                    ch_type: ch.ch_type,
                    ch_reserved: 0,
                    ch_size: ch.ch_size as u64,
                    ch_addralign: ch.ch_addralign as u64,
                }
            }
        }
    }
}

///////////////////////////////
// Std/analysis/Unified Structs
///////////////////////////////

if_alloc! {
    #[cfg(feature = "endian_fd")]
    use crate::error;
    use core::fmt;
    use core::result;
    use scroll::ctx;
    use crate::container::{Container, Ctx};

    #[derive(Default, PartialEq, Clone)]
    /// A unified CompressionHeader - convertable to and from 32-bit and 64-bit variants
    pub struct CompressionHeader {
        /// Compression format
        pub ch_type: u32,
        /// Uncompressed data size
        pub ch_size: u64,
        /// Uncompressed data alignment
        pub ch_addralign: u64,
    }

    impl CompressionHeader {
        /// Return the size of the underlying compression header, given a `container`
        #[inline]
        pub fn size(ctx: Ctx) -> usize {
            use scroll::ctx::SizeWith;
            Self::size_with(&ctx)
        }
        pub fn new() -> Self {
            CompressionHeader {
                ch_type: 0,
                ch_size: 0,
                ch_addralign: 2 << 8,
            }
        }
        /// Parse a compression header from `bytes` at `offset`, using the given `ctx`
        #[cfg(feature = "endian_fd")]
        pub fn parse(bytes: &[u8], mut offset: usize, ctx: Ctx) -> error::Result<CompressionHeader> {
            use scroll::Pread;
            bytes.gread_with(&mut offset, ctx)
        }
    }

    impl fmt::Debug for CompressionHeader {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            f.debug_struct("CompressionHeader")
                .field("ch_type", &self.ch_type)
                .field("ch_size", &format_args!("0x{:x}", self.ch_size))
                .field("ch_addralign", &format_args!("0x{:x}", self.ch_addralign))
                .finish()
        }
    }

    impl ctx::SizeWith<Ctx> for CompressionHeader {
        type Units = usize;
        fn size_with( &Ctx { container, .. }: &Ctx) -> Self::Units {
            match container {
                Container::Little => {
                    compression_header32::SIZEOF_CHDR
                },
                Container::Big => {
                    compression_header64::SIZEOF_CHDR
                },
            }
        }
    }

    impl<'a> ctx::TryFromCtx<'a, Ctx> for CompressionHeader {
        type Error = crate::error::Error;
        type Size = usize;
        fn try_from_ctx(bytes: &'a [u8], Ctx {container, le}: Ctx) -> result::Result<(Self, Self::Size), Self::Error> {
            use scroll::Pread;
            let res = match container {
                Container::Little => {
                    (bytes.pread_with::<compression_header32::CompressionHeader>(0, le)?.into(), compression_header32::SIZEOF_CHDR)
                },
                Container::Big => {
                    (bytes.pread_with::<compression_header64::CompressionHeader>(0, le)?.into(), compression_header64::SIZEOF_CHDR)
                }
            };
            Ok(res)
        }
    }

    impl ctx::TryIntoCtx<Ctx> for CompressionHeader {
        type Error = crate::error::Error;
        type Size = usize;
        fn try_into_ctx(self, bytes: &mut [u8], Ctx {container, le}: Ctx) -> result::Result<Self::Size, Self::Error> {
            use scroll::Pwrite;
            match container {
                Container::Little => {
                    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)?)
                }
            }
        }
    }
    impl ctx::IntoCtx<Ctx> for CompressionHeader {
        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();
                },
                Container::Big => {
                    let chdr: compression_header64::CompressionHeader = self.into();
                    bytes.pwrite_with(chdr, 0, le).unwrap();
                }
            }
        }
    }
} // end if_alloc