-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moves note reader to a dedicated module (#1211)
- Loading branch information
1 parent
95906d8
commit 0a875cc
Showing
11 changed files
with
189 additions
and
164 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use bytes::{Buf, Bytes}; | ||
use std::cmp::min; | ||
use thiserror::Error; | ||
|
||
/// Iterator to enumerate ELF notes. | ||
pub struct Notes(Bytes); | ||
|
||
impl Notes { | ||
pub(super) fn new(data: impl Into<Bytes>) -> Self { | ||
Self(data.into()) | ||
} | ||
} | ||
|
||
impl Iterator for Notes { | ||
type Item = Result<Note, NoteError>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
// Check remaining data. | ||
let hdr = 4 * 3; | ||
|
||
if self.0.is_empty() { | ||
return None; | ||
} else if self.0.len() < hdr { | ||
return Some(Err(NoteError::InvalidHeader)); | ||
} | ||
|
||
// Parse header. | ||
let mut hdr = self.0.split_to(hdr); | ||
let nlen: usize = hdr.get_u32_ne().try_into().unwrap(); | ||
let dlen: usize = hdr.get_u32_ne().try_into().unwrap(); | ||
let ty = hdr.get_u32_ne(); | ||
|
||
if nlen == 0 { | ||
// Name is null-terminated so it should have at least 1 byte. | ||
return Some(Err(NoteError::InvalidName)); | ||
} else if nlen > self.0.len() { | ||
return Some(Err(NoteError::InvalidHeader)); | ||
} | ||
|
||
// Get name. | ||
let mut name = self.0.split_to(nlen); | ||
let len = nlen - 1; | ||
|
||
if name.iter().position(|&b| b == 0) != Some(len) { | ||
return Some(Err(NoteError::InvalidName)); | ||
} | ||
|
||
name.truncate(len); | ||
|
||
// Skip alignment. | ||
let skip = nlen.next_multiple_of(4) - nlen; | ||
|
||
self.0.advance(min(skip, self.0.len())); | ||
|
||
if dlen > self.0.len() { | ||
return Some(Err(NoteError::InvalidHeader)); | ||
} | ||
|
||
// Get description. | ||
let desc = self.0.split_to(dlen); | ||
let skip = dlen.next_multiple_of(4) - dlen; | ||
|
||
self.0.advance(min(skip, self.0.len())); | ||
|
||
Some(Ok(Note { name, desc, ty })) | ||
} | ||
} | ||
|
||
/// Parsed ELF program header. | ||
pub struct Note { | ||
pub name: Bytes, | ||
pub desc: Bytes, | ||
pub ty: u32, | ||
} | ||
|
||
/// Represents an error when [`Notes`] fails to enumerate next ELF note. | ||
#[derive(Debug, Error)] | ||
pub enum NoteError { | ||
#[error("invalid header")] | ||
InvalidHeader, | ||
|
||
#[error("invalid name")] | ||
InvalidName, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.