Skip to main content

IntelHex

Struct IntelHex 

Source
pub struct IntelHex { /* private fields */ }
Expand description

Main structure for handling Intel HEX format data

Implementations§

Source§

impl IntelHex

Source

pub fn new() -> IntelHex

Creates a new empty IntelHex instance

Initializes a new IntelHex object with:

  • An empty memory cells map (no data bytes stored)
  • No execution start address
§Returns

Returns a new empty IntelHex instance

§Examples
use intelhex::IntelHex;

let intel_hex = IntelHex::new();

// Memory cells map is empty
assert!(intel_hex.memory_cells().is_empty());

// No execution start address
assert!(intel_hex.execution_start_address().is_none());
Source

pub fn memory_cells(&self) -> &BTreeMap<u32, u8>

Returns a reference to the memory cells map

The memory cells are stored in a BTreeMap where:

  • Keys (u32) are memory addresses
  • Values (u8) are the data bytes stored at those addresses
  • Addresses are automatically sorted in ascending order
§Returns

Returns a reference to the BTreeMap containing all memory cells data

§Examples
use intelhex::IntelHex;

let hex_text = ":10010000214601360121470136007EFE09D2190140\n:00000001FF";
let intel_hex = IntelHex::from_intel_hex_text(hex_text).unwrap();

// Access memory cells
let memory = intel_hex.memory_cells();

// Check if an address contains data
if let Some(&value) = memory.get(&0x100) {
    println!("Value at 0x100: {:02X}", value);
}

// Iterate through all memory cells in address order
for (&addr, &value) in memory.iter() {
    println!("Address: {:08X}, Value: {:02X}", addr, value);
}
Source

pub fn execution_start_address(&self) -> Option<&ExecutionStartAddress>

Returns a reference to the execution start address if it exists

The execution start address can be either a segment address (CS:IP) or a linear address (EIP), as defined in the Intel HEX format.

§Returns
  • Some(&ExecutionStartAddress) - A reference to the execution start address if it was specified
  • None - If no execution start address was specified
§Examples
use intelhex::IntelHex;
use intelhex::ExecutionStartAddress;

let hex_text = ":0400000300100020C9\n:00000001FF";  // Contains a start segment address
let intel_hex = IntelHex::from_intel_hex_text(hex_text).unwrap();

if let Some(start_addr) = intel_hex.execution_start_address() {
    match start_addr {
        ExecutionStartAddress::StartSegmentAddress { cs, ip } => {
            println!("CS:{:04X}, IP:{:04X}", cs, ip)
        },
        ExecutionStartAddress::StartLinearAddress { eip } => {
            println!("EIP:{:08X}", eip)
        },
    }
}
Source

pub fn from_reader<R>(reader: R) -> Result<IntelHex, IntelHexError>
where R: Read,

Creates an IntelHex instance from a reader

§Arguments
  • reader - Any type that implements the Read trait
§Returns
  • Ok(IntelHex) - A new IntelHex instance containing the parsed data
  • Err(IntelHexError) - If there was an error reading or parsing the data
§Examples
use std::fs::File;
use intelhex::IntelHex;

let file = File::open("tests/fixtures/firmware.hex").unwrap();
let intel_hex = IntelHex::from_reader(file).unwrap();
Source

pub fn from_intel_hex_text(hex_text: &str) -> Result<IntelHex, IntelHexError>

Creates an IntelHex instance from Intel HEX format text

§Arguments
  • hex_text - A string slice containing Intel HEX formatted data
§Returns
  • Ok(IntelHex) - A new IntelHex instance containing the parsed data
  • Err(IntelHexError) - If the input string is not a valid Intel HEX format
§Examples
use intelhex::IntelHex;

let hex_text = ":10010000214601360121470136007EFE09D2190140\n:00000001FF";
let result = IntelHex::from_intel_hex_text(hex_text).unwrap();
Source

pub fn to_writer<W>(&self, writer: W, data_byte_count: u8) -> Result<(), Error>
where W: Write,

Writes Intel HEX format data to a writer

§Arguments
  • writer - Any type that implements the Write trait
  • data_byte_count - Number of data bytes per Data Record (1-255). A value of 16 is recommended for general use.
§Returns
  • Ok(()) - If the data was successfully written
  • Err(std::io::Error) - If there was an error writing the data
§Examples
use intelhex::IntelHex;
use std::fs::File;

let intel_hex = IntelHex::new();
let mut file = File::create("output.hex").unwrap();
intel_hex.to_writer(&mut file, 16).unwrap();  // 16 data bytes per data record
Source

pub fn to_intel_hex_text(&self) -> Result<String, Error>

Converts the IntelHex object to Intel HEX format text

§Returns
  • Ok(String) - A string containing the Intel HEX formatted data
  • Err(std::io::Error) - If there was an error writing the data
Source

pub fn memory_blocks(&self) -> Vec<MemoryDataBlock>

Converts memory data into a collection of continuous data blocks

§Returns

Returns a vector of MemoryDataBlock, each containing:

  • address: Starting address of the data block
  • data: Vector of continuous data bytes
Source

pub fn add_memory_block( &mut self, block: &MemoryDataBlock, ) -> Result<(), IntelHexError>

Adds data from a MemoryDataBlock to the memory cells

§Arguments
  • block - The MemoryDataBlock containing address and data to add
§Returns
  • Ok(()) - If the data was successfully added
  • Err(IntelHexError) - If there was an error adding the data
§Examples
use intelhex::{IntelHex, MemoryDataBlock};

let mut intel_hex = IntelHex::new();
let block = MemoryDataBlock {
    address: 0x1000,
    data: vec![0x12, 0x34, 0x56],
};

// Add the memory block
intel_hex.add_memory_block(&block).unwrap();

// Verify the data was added
let memory = intel_hex.memory_cells();
assert_eq!(memory.get(&0x1000), Some(&0x12));
assert_eq!(memory.get(&0x1001), Some(&0x34));
assert_eq!(memory.get(&0x1002), Some(&0x56));

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.