-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more limited and defined ihex handling (#5)
* more limited and defined ihex handling * error adjustments * bump version
- Loading branch information
1 parent
047efe4
commit 94ac0b6
Showing
6 changed files
with
110 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use ihex::*; | ||
use thiserror::Error; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum UnpackingError { | ||
#[error("Unsupported record type")] | ||
UnsupportedRecordType(Record), | ||
#[error("Error while parsing IHEX records")] | ||
Parsing(#[from] ReaderError), | ||
#[error("Address ({0}) greater than binary size ({1})")] | ||
AddressTooHigh(usize, usize), | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
pub enum ConversionError { | ||
#[error("Error while unpacking IHEX into array")] | ||
Unpacking(#[from] UnpackingError), | ||
#[error("Errow while writing IHEX to string")] | ||
Serializing(#[from] WriterError) | ||
} | ||
|
||
pub fn to_ihex(byte_array: Vec<u8>) -> Result<String, ConversionError> { | ||
let mut result: Vec<Record> = vec![]; | ||
for (i, chunk) in byte_array.chunks(16).enumerate() { | ||
result.push(Record::Data { | ||
offset: (i as u16) * 16, | ||
value: chunk.to_vec(), | ||
}); | ||
} | ||
result.push(Record::EndOfFile); | ||
return create_object_file_representation(&result) | ||
.map_err(ConversionError::from); | ||
} | ||
|
||
pub fn from_ihex(ihex_string: &str, max_length: usize) -> Result<Vec<u8>, ConversionError> { | ||
let mut reader = Reader::new(ihex_string); | ||
return unpack_records(&mut reader, max_length) | ||
.map_err(ConversionError::from); | ||
} | ||
|
||
fn unpack_records( | ||
records: &mut impl Iterator<Item = Result<Record, ReaderError>>, | ||
max_length: usize, | ||
) -> Result<Vec<u8>, UnpackingError> { | ||
let mut result: Vec<u8> = vec![]; | ||
for rec in records { | ||
match rec { | ||
Ok(rec) => match rec { | ||
Record::Data { offset, value } => { | ||
let end_addr = offset as usize + value.len(); | ||
if end_addr > max_length { | ||
return Err(UnpackingError::AddressTooHigh(end_addr, max_length)); | ||
} | ||
if end_addr > result.len() { | ||
result.resize(end_addr, 0); | ||
} | ||
|
||
for (n, b) in value.iter().enumerate() { | ||
result[offset as usize + n] = *b; | ||
} | ||
} | ||
Record::ExtendedSegmentAddress(_base) => { | ||
return Err(UnpackingError::UnsupportedRecordType(rec)) | ||
} | ||
Record::ExtendedLinearAddress(_base) => { | ||
return Err(UnpackingError::UnsupportedRecordType(rec)) | ||
} | ||
Record::EndOfFile => break, | ||
Record::StartLinearAddress(_) | Record::StartSegmentAddress { .. } => {} | ||
}, | ||
Err(err) => return Err(UnpackingError::Parsing(err)), | ||
} | ||
} | ||
return Ok(result); | ||
} |
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