hexrec.formats.motorola.Record#

class hexrec.formats.motorola.Record(address, tag, data, checksum=Ellipsis)[source]#

Motorola S-record.

Variables:
  • address (int) – Tells where its data starts in the memory addressing space, or an address with a special meaning.

  • tag (int) – Defines the logical meaning of the address and data fields.

  • data (bytes) – Byte data as required by the tag.

  • count (int) – Counts its fields as required by the Record subclass implementation.

  • checksum (int) – Computes the checksum as required by most Record implementations.

Parameters:
  • address (int) – Record address field.

  • tag (int) – Record tag field.

  • data (bytes) – Record data field.

  • checksum (int) – Record checksum field. Ellipsis makes the constructor compute its actual value automatically. None assigns None.

Methods

__init__

build_count

Builds a count record.

build_data

Builds a data record.

build_header

Builds a header record.

build_standalone

Makes a sequence of data records standalone.

build_terminator

Builds a terminator record.

check

Performs consistency checks.

check_sequence

Consistency check of a sequence of records.

compute_checksum

Computes the checksum.

compute_count

Computes the count.

fit_count_tag

Fits the record count tag.

fit_data_tag

Fits a data tag by address.

fix_tags

Fix record tags.

get_header

Gets the header record.

get_metadata

Retrieves metadata from records.

is_data

Tells if it is a data record.

load_blocks

Loads blocks from a file.

load_memory

Loads a virtual memory from a file.

load_records

Loads records from a file.

marshal

Marshals a record for output.

overlaps

Checks if overlapping occurs.

parse_record

Parses a record from a text line.

read_blocks

Reads blocks from a stream.

read_memory

Reads a virtual memory from a stream.

read_records

Reads records from a stream.

readdress

Converts to flat addressing.

save_blocks

Saves blocks to a file.

save_memory

Saves a virtual memory to a file.

save_records

Saves records to a file.

set_header

Sets the header data.

split

Splits a chunk of data into records.

unmarshal

Unmarshals a record from input.

update_checksum

Updates the checksum field via compute_count().

update_count

Updates the count field via compute_count().

write_blocks

Writes blocks to a stream.

write_memory

Writes a virtual memory to a stream.

write_records

Saves records to a stream.

Attributes

tag

count

address

data

checksum

EXTENSIONS

File extensions typically mapped to this record type.

LINE_SEP

Separator between record lines.

MATCHING_TAG

Maps the terminator tag to its matching data tag.

REGEX

Regular expression for parsing a record text line.

TAG_TO_ADDRESS_LENGTH

Maps a tag to its address byte length, if available.

TAG_TO_COLUMN_SIZE

Maps a tag to its maximum column size, if available.

EXTENSIONS: Sequence[str] = ('.mot', '.s19', '.s28', '.s37', '.srec', '.exo')#

File extensions typically mapped to this record type.

LINE_SEP: Union[bytes, str] = '\n'#

Separator between record lines.

If subclass of bytes, it is considered as a binary file.

MATCHING_TAG: Sequence[Optional[int]] = (None, None, None, None, None, None, None, 3, 2, 1)#

Maps the terminator tag to its matching data tag.

REGEX = re.compile('^S[0-9]([0-9A-Fa-f]{2}){4,264}$')#

Regular expression for parsing a record text line.

TAG_TO_ADDRESS_LENGTH: Sequence[Optional[int]] = (2, 2, 3, 4, None, None, None, 4, 3, 2)#

Maps a tag to its address byte length, if available.

TAG_TO_COLUMN_SIZE: Sequence[Optional[int]] = (None, 252, 251, 250, None, None, None, None, None, None)#

Maps a tag to its maximum column size, if available.

TAG_TYPE#

Associated Python class for tags.

alias of Tag

__eq__(other)[source]#

Equality comparison.

Returns:

bool – The address, tag, and data fields are equal.

Examples

>>> from hexrec.formats.binary import Record as BinaryRecord
>>> record1 = BinaryRecord.build_data(0, b'Hello, World!')
>>> record2 = BinaryRecord.build_data(0, b'Hello, World!')
>>> record1 == record2
True
>>> from hexrec.formats.binary import Record as BinaryRecord
>>> record1 = BinaryRecord.build_data(0, b'Hello, World!')
>>> record2 = BinaryRecord.build_data(1, b'Hello, World!')
>>> record1 == record2
False
>>> from hexrec.formats.binary import Record as BinaryRecord
>>> record1 = BinaryRecord.build_data(0, b'Hello, World!')
>>> record2 = BinaryRecord.build_data(0, b'hello, world!')
>>> record1 == record2
False
>>> from hexrec.formats.motorola import Record as MotorolaRecord
>>> record1 = MotorolaRecord.build_header(b'Hello, World!')
>>> record2 = MotorolaRecord.build_data(0, b'hello, world!')
>>> record1 == record2
False
__hash__()[source]#

Computes the hash value.

Computes the hash of the Record fields. Useful to make the record hashable although it is a mutable class.

Returns:

int – Hash of the Record fields.

Warning

Be careful with hashable mutable objects!

Examples

>>> from hexrec.formats.binary import Record as BinaryRecord
>>> hash(BinaryRecord(0x1234, None, b'Hello, World!'))
... 
7668968047460943252
>>> from hexrec.formats.motorola import Record as MotorolaRecord
>>> from hexrec.formats.motorola import Tag as MotorolaTag
>>> hash(MotorolaRecord(0x1234, MotorolaTag.DATA_16,
...                             b'Hello, World!'))
... 
7668968047460943265
>>> from hexrec.formats.intel import Record as IntelRecord
>>> from hexrec.formats.intel import Tag as IntelTag
>>> hash(IntelRecord(0x1234, IntelTag.DATA, b'Hello, World!'))
... 
7668968047460943289
__init__(address, tag, data, checksum=Ellipsis)[source]#
__lt__(other)[source]#

Less-than comparison.

Returns:

booladdress less than other’s.

Examples

>>> from hexrec.formats.binary import Record as BinaryRecord
>>> record1 = BinaryRecord(0x1234, None, b'')
>>> record2 = BinaryRecord(0x4321, None, b'')
>>> record1 < record2
True
>>> from hexrec.formats.binary import Record as BinaryRecord
>>> record1 = BinaryRecord(0x4321, None, b'')
>>> record2 = BinaryRecord(0x1234, None, b'')
>>> record1 < record2
False
__repr__()[source]#

Return repr(self).

__str__()[source]#

Converts to text string.

Builds a printable text representation of the record, usually the same found in the saved record file as per its Record subclass requirements.

Returns:

str – A printable text representation of the record.

Examples

>>> from hexrec.formats.binary import Record as BinaryRecord
>>> str(BinaryRecord(0x1234, None, b'Hello, World!'))
'48656C6C6F2C20576F726C6421'
>>> from hexrec.formats.motorola import Record as MotorolaRecord
>>> from hexrec.formats.motorola import Tag as MotorolaTag
>>> str(MotorolaRecord(0x1234, MotorolaTag.DATA_16,
...                    b'Hello, World!'))
'S110123448656C6C6F2C20576F726C642140'
>>> from hexrec.formats.intel import Record as IntelRecord
>>> from hexrec.formats.intel import Tag as IntelTag
>>> str(IntelRecord(0x1234, IntelTag.DATA, b'Hello, World!'))
':0D12340048656C6C6F2C20576F726C642144'
__weakref__#

list of weak references to the object (if defined)

_get_checksum()[source]#

int: The checksum field itself if not None, the value computed by compute_count() otherwise.

classmethod _open_input(path)[source]#

Opens a file for input.

Parameters:

path (str) – File path.

Returns:

stream – An input stream handle.

classmethod _open_output(path)[source]#

Opens a file for output.

Parameters:

path (str) – File path.

Returns:

stream – An output stream handle.

classmethod build_count(record_count)[source]#

Builds a count record.

Parameters:

record_count (int) – Record count.

Returns:

record – Count record.

Raises:

ValueError – Count error.

Examples

>>> str(Record.build_count(0x1234))
'S5031234B6'
>>> str(Record.build_count(0x123456))
'S6041234565F'
classmethod build_data(address, data, tag=None)[source]#

Builds a data record.

Parameters:
  • address (int) – Record start address.

  • data (bytes) – Some program data.

  • tag (tag) – Data tag record. If None, automatically selects the fitting one.

Returns:

record – Data record.

Raises:

ValueError – Tag error.

Examples

>>> str(Record.build_data(0x1234, b'Hello, World!'))
'S110123448656C6C6F2C20576F726C642140'
>>> str(Record.build_data(0x1234, b'Hello, World!',
...                               tag=Tag.DATA_16))
'S110123448656C6C6F2C20576F726C642140'
>>> str(Record.build_data(0x123456, b'Hello, World!',
...                               tag=Tag.DATA_24))
'S21112345648656C6C6F2C20576F726C6421E9'
>>> str(Record.build_data(0x12345678, b'Hello, World!',
...                               tag=Tag.DATA_32))
'S3121234567848656C6C6F2C20576F726C642170'
classmethod build_header(data)[source]#

Builds a header record.

Parameters:

data (bytes) – Header string data.

Returns:

record – Header record.

Example

>>> str(Record.build_header(b'Hello, World!'))
'S010000048656C6C6F2C20576F726C642186'
classmethod build_standalone(data_records, start=None, tag=None, header=b'')[source]#

Makes a sequence of data records standalone.

Parameters:
  • data_records (list of records) – A sequence of data records.

  • start (int) – Program start address. If None, it is assigned the minimum data record address.

  • tag (tag) – Data tag record. If None, automatically selects the fitting one.

  • header (bytes) – Header byte data.

Yields:

record – Records for a standalone record file.

classmethod build_terminator(start, last_data_tag=Tag.DATA_16)[source]#

Builds a terminator record.

Parameters:
  • start (int) – Program start address.

  • last_data_tag (tag) – Last data record tag to match.

Returns:

record – Terminator record.

Examples

>>> str(Record.build_terminator(0x1234))
'S9031234B6'
>>> str(Record.build_terminator(0x1234, Tag.DATA_16))
'S9031234B6'
>>> str(Record.build_terminator(0x123456, Tag.DATA_24))
'S8041234565F'
>>> str(Record.build_terminator(0x12345678, Tag.DATA_32))
'S70512345678E6'
check()[source]#

Performs consistency checks.

Raises:

ValueError – a field is inconsistent.

classmethod check_sequence(records)[source]#

Consistency check of a sequence of records.

Parameters:

records (list of records) – Sequence of records.

Raises:

ValueError – A field is inconsistent.

compute_checksum()[source]#

Computes the checksum.

Returns:

intchecksum field value based on the current fields.

Examples

>>> from hexrec.formats.binary import Record as BinaryRecord
>>> record = BinaryRecord(0, None, b'Hello, World!')
>>> str(record)
'48656C6C6F2C20576F726C6421'
>>> hex(record.compute_checksum())
'0x69'
>>> from hexrec.formats.motorola import Record as MotorolaRecord
>>> from hexrec.formats.motorola import Tag as MotorolaTag
>>> record = MotorolaRecord(0, MotorolaTag.DATA_16,
...                         b'Hello, World!')
>>> str(record)
'S110000048656C6C6F2C20576F726C642186'
>>> hex(record.compute_checksum())
'0x86'
>>> from hexrec.formats.intel import Record as IntelRecord
>>> from hexrec.formats.intel import Tag as IntelTag
>>> record = IntelRecord(0, IntelTag.DATA, b'Hello, World!')
>>> str(record)
':0D00000048656C6C6F2C20576F726C64218A'
>>> hex(record.compute_checksum())
'0x8a'
compute_count()[source]#

Computes the count.

Returns:

boolcount field value based on the current fields.

Examples

>>> from hexrec.formats.binary import Record as BinaryRecord
>>> record = BinaryRecord(0, None, b'Hello, World!')
>>> str(record)
'48656C6C6F2C20576F726C6421'
>>> record.compute_count()
13
>>> from hexrec.formats.motorola import Record as MotorolaRecord
>>> from hexrec.formats.motorola import Tag as MotorolaTag
>>> record = MotorolaRecord(0, MotorolaTag.DATA_16,
...                         b'Hello, World!')
>>> str(record)
'S110000048656C6C6F2C20576F726C642186'
>>> record.compute_count()
16
>>> from hexrec.formats.intel import Record as IntelRecord
>>> from hexrec.formats.intel import Tag as IntelTag
>>> record = IntelRecord(0, IntelTag.DATA, b'Hello, World!')
>>> str(record)
':0D00000048656C6C6F2C20576F726C64218A'
>>> record.compute_count()
13
classmethod fit_count_tag(record_count)[source]#

Fits the record count tag.

Parameters:

record_count (int) – Record count.

Returns:

tag – Fitting record count tag.

Raises:

ValueError – Count overflow.

Examples

>>> Record.fit_count_tag(0x0000000)
<Tag.COUNT_16: 5>
>>> Record.fit_count_tag(0x00FFFF)
<Tag.COUNT_16: 5>
>>> Record.fit_count_tag(0x010000)
<Tag.COUNT_24: 6>
>>> Record.fit_count_tag(0xFFFFFF)
<Tag.COUNT_24: 6>
classmethod fit_data_tag(endex)[source]#

Fits a data tag by address.

Depending on the value of endex, get the data tag with the smallest supported address.

Parameters:

endex (int) – Exclusive end address of the data.

Returns:

tag – Fitting data tag.

Raises:

ValueError – Address overflow.

Examples

>>> Record.fit_data_tag(0x00000000)
<Tag.DATA_16: 1>
>>> Record.fit_data_tag(0x0000FFFF)
<Tag.DATA_16: 1>
>>> Record.fit_data_tag(0x00010000)
<Tag.DATA_16: 1>
>>> Record.fit_data_tag(0x00FFFFFF)
<Tag.DATA_24: 2>
>>> Record.fit_data_tag(0x01000000)
<Tag.DATA_24: 2>
>>> Record.fit_data_tag(0xFFFFFFFF)
<Tag.DATA_32: 3>
>>> Record.fit_data_tag(0x100000000)
<Tag.DATA_32: 3>
classmethod fix_tags(records)[source]#

Fix record tags.

Updates record tags to reflect modified size and count. All the checksums are updated too. Operates in-place.

Parameters:

records (list of records) – A sequence of records. Must be in-line mutable.

classmethod get_header(records)[source]#

Gets the header record.

Parameters:

records (list of records) – A sequence of records.

Returns:

record – The header record, or None.

classmethod get_metadata(records)[source]#

Retrieves metadata from records.

Collected metadata:

  • columns: maximum data columns per line found, or None.

  • start: program execution start address found, or None.

  • count: last count record found, or None.

  • header: last header record data found, or None.

Parameters:

records (list of records) – Records to scan for metadata.

Returns:

dict – Collected metadata.

is_data()[source]#

Tells if it is a data record.

Tells whether the record contains plain binary data, i.e. it is not a special record.

Returns:

bool – The record contains plain binary data.

Examples

>>> from hexrec.formats.binary import Record as BinaryRecord
>>> BinaryRecord(0, None, b'Hello, World!').is_data()
True
>>> from hexrec.formats.motorola import Record as MotorolaRecord
>>> from hexrec.formats.motorola import Tag as MotorolaTag
>>> MotorolaRecord(0, MotorolaTag.DATA_16,
...                b'Hello, World!').is_data()
True
>>> from hexrec.formats.motorola import Record as MotorolaRecord
>>> from hexrec.formats.motorola import Tag as MotorolaTag
>>> MotorolaRecord(0, MotorolaTag.HEADER,
...                b'Hello, World!').is_data()
False
>>> from hexrec.formats.intel import Record as IntelRecord
>>> from hexrec.formats.intel import Tag as IntelTag
>>> IntelRecord(0, IntelTag.DATA, b'Hello, World!').is_data()
True
>>> from hexrec.formats.intel import Record as IntelRecord
>>> from hexrec.formats.intel import Tag as IntelTag
>>> IntelRecord(0, IntelTag.END_OF_FILE, b'').is_data()
False
classmethod load_blocks(path)[source]#

Loads blocks from a file.

Each line of the input file is parsed via parse_block(), and collected into the returned sequence.

Parameters:

path (str) – Path of the record file to load.

Returns:

list of records – Sequence of parsed records.

Example

>>> from hexrec.formats.motorola import Record as MotorolaRecord
>>> with open('load_blocks.mot', 'wt') as f:
...     f.write('S0030000FC\n')
...     f.write('S1060000616263D3\n')
...     f.write('S1060010646566BA\n')
...     f.write('S5030002FA\n')
...     f.write('S9030000FC\n')
>>> MotorolaRecord.load_blocks('load_blocks.mot')
[[0, b'abc'], [16, b'def']]
classmethod load_memory(path)[source]#

Loads a virtual memory from a file.

Parameters:

path (str) – Path of the record file to load.

Returns:

Memory – Loaded virtual memory.

Example

>>> from hexrec.formats.motorola import Record as MotorolaRecord
>>> with open('load_blocks.mot', 'wt') as f:
...     f.write('S0030000FC\n')
...     f.write('S1060000616263D3\n')
...     f.write('S1060010646566BA\n')
...     f.write('S5030002FA\n')
...     f.write('S9030000FC\n')
>>> memory = MotorolaRecord.load_memory('load_blocks.mot')
>>> memory.to_blocks()
[[0, b'abc'], [16, b'def']]
classmethod load_records(path)[source]#

Loads records from a file.

Each line of the input file is parsed via parse(), and collected into the returned sequence.

Parameters:

path (str) – Path of the record file to load.

Returns:

list of records – Sequence of parsed records.

Example

>>> from hexrec.formats.motorola import Record as MotorolaRecord
>>> with open('load_records.mot', 'wt') as f:
...     f.write('S0030000FC\n')
...     f.write('S1060000616263D3\n')
...     f.write('S1060010646566BA\n')
...     f.write('S5030002FA\n')
...     f.write('S9030000FC\n')
>>> records = MotorolaRecord.load_records('load_records.mot')
>>> records  
[Record(address=0x00000000, tag=<Tag.HEADER: 0>, count=3,
        data=b'', checksum=0xFC),
 Record(address=0x00000000, tag=<Tag.DATA_16: 1>, count=6,
        data=b'abc', checksum=0xD3),
 Record(address=0x00000010, tag=<Tag.DATA_16: 1>, count=6,
        data=b'def', checksum=0xBA),
 Record(address=0x00000000, tag=<Tag.COUNT_16: 5>, count=3,
        data=b'\x00\x02', checksum=0xFA),
 Record(address=0x00000000, tag=<Tag.START_16: 9>, count=3,
        data=b'', checksum=0xFC)]
marshal(*args, **kwargs)[source]#

Marshals a record for output.

Parameters:
  • args (tuple) – Further positional arguments for overriding.

  • kwargs (dict) – Further keyword arguments for overriding.

Returns:

bytes or str – Data for output, according to the file type.

overlaps(other)[source]#

Checks if overlapping occurs.

This record and another have overlapping data, when both address fields are not None.

Parameters:

other (record) – Record to compare with self.

Returns:

bool – Overlapping.

Examples

>>> from hexrec.formats.binary import Record as BinaryRecord
>>> record1 = BinaryRecord(0, None, b'abc')
>>> record2 = BinaryRecord(1, None, b'def')
>>> record1.overlaps(record2)
True
>>> from hexrec.formats.binary import Record as BinaryRecord
>>> record1 = BinaryRecord(0, None, b'abc')
>>> record2 = BinaryRecord(3, None, b'def')
>>> record1.overlaps(record2)
False
classmethod parse_record(line, *args, **kwargs)[source]#

Parses a record from a text line.

Parameters:
  • line (str) – Record line to parse.

  • args (tuple) – Further positional arguments for overriding.

  • kwargs (dict) – Further keyword arguments for overriding.

Returns:

record – Parsed record.

Note

This method must be overridden.

classmethod read_blocks(stream)[source]#

Reads blocks from a stream.

Read blocks from the input stream into the returned sequence.

Parameters:

stream (stream) – Input stream of the blocks to read.

Returns:

list of blocks – Sequence of parsed blocks.

Example

>>> import io
>>> from hexrec.formats.motorola import Record as MotorolaRecord
>>> blocks = [[0, b'abc'], [16, b'def']]
>>> stream = io.StringIO()
>>> MotorolaRecord.write_blocks(stream, blocks)
>>> _ = stream.seek(0, io.SEEK_SET)
>>> MotorolaRecord.read_blocks(stream)
[[0, b'abc'], [16, b'def']]
classmethod read_memory(stream)[source]#

Reads a virtual memory from a stream.

Read blocks from the input stream into the returned sequence.

Parameters:

stream (stream) – Input stream of the blocks to read.

Returns:

Memory – Loaded virtual memory.

Example

>>> import io
>>> from hexrec.formats.motorola import Record as MotorolaRecord
>>> blocks = [[0, b'abc'], [16, b'def']]
>>> stream = io.StringIO()
>>> MotorolaRecord.write_blocks(stream, blocks)
>>> _ = stream.seek(0, io.SEEK_SET)
>>> memory = MotorolaRecord.read_memory(stream)
>>> memory.to_blocks()
[[0, b'abc'], [16, b'def']]
classmethod read_records(stream)[source]#

Reads records from a stream.

For text files, each line of the input file is parsed via parse(), and collected into the returned sequence.

For binary files, everything to the end of the stream is parsed as a single record.

Parameters:

stream (stream) – Input stream of the records to read.

Returns:

list of records – Sequence of parsed records.

Example

>>> import io
>>> from hexrec.formats.motorola import Record as MotorolaRecord
>>> blocks = [[0, b'abc'], [16, b'def']]
>>> stream = io.StringIO()
>>> MotorolaRecord.write_blocks(stream, blocks)
>>> _ = stream.seek(0, io.SEEK_SET)
>>> records = MotorolaRecord.read_records(stream)
>>> records  
[Record(address=0x00000000, tag=<Tag.HEADER: 0>, count=3,
        data=b'', checksum=0xFC),
 Record(address=0x00000000, tag=<Tag.DATA_16: 1>, count=6,
        data=b'abc', checksum=0xD3),
 Record(address=0x00000010, tag=<Tag.DATA_16: 1>, count=6,
        data=b'def', checksum=0xBA),
 Record(address=0x00000000, tag=<Tag.COUNT_16: 5>, count=3,
        data=b'\x00\x02', checksum=0xFA),
 Record(address=0x00000000, tag=<Tag.START_16: 9>, count=3,
        data=b'', checksum=0xFC)]
classmethod readdress(records)[source]#

Converts to flat addressing.

Some record types, notably the Intel HEX, store records by some segment/offset addressing flavor. As this library adopts flat addressing instead, all the record addresses should be converted to flat addressing after loading. This procedure readdresses a sequence of records in-place.

Warning

Only the address field is modified. All the other fields hold their previous value.

Parameters:

records (list) – Sequence of records to be converted to flat addressing, in-place.

classmethod save_blocks(path, blocks, split_args=None, split_kwargs=None, build_args=None, build_kwargs=None)[source]#

Saves blocks to a file.

Each block of the blocks sequence is converted into a record via build_data() and written to the output file.

Parameters:

Example

>>> from hexrec.formats.motorola import Record as MotorolaRecord
>>> blocks = [[0, b'abc'], [16, b'def']]
>>> MotorolaRecord.save_blocks('save_blocks.mot', blocks)
>>> with open('save_blocks.mot', 'rt') as f: text = f.read()
>>> text
'S0030000FC\nS1060000616263D3\nS1060010646566BA\nS5030002FA\nS9030000FC\n'
classmethod save_memory(path, memory, split_args=None, split_kwargs=None, build_args=None, build_kwargs=None)[source]#

Saves a virtual memory to a file.

Parameters:

Example

>>> from hexrec.records import Memory
>>> from hexrec.formats.motorola import Record as MotorolaRecord
>>> memory = Memory(blocks=[[0, b'abc'], [16, b'def']])
>>> MotorolaRecord.save_memory('save_memory.mot', memory)
>>> with open('save_memory.mot', 'rt') as f: text = f.read()
>>> text
'S0030000FC\nS1060000616263D3\nS1060010646566BA\nS5030002FA\nS9030000FC\n'
classmethod save_records(path, records)[source]#

Saves records to a file.

Each record of the records sequence is converted into text via str(), and stored into the output text file.

Parameters:
  • path (str) – Path of the record file to save.

  • records (list) – Sequence of records to store.

Example

>>> from hexrec.formats.motorola import Record as MotorolaRecord
>>> from hexrec.records import blocks_to_records
>>> blocks = [[0, b'abc'], [16, b'def']]
>>> records = blocks_to_records(blocks, MotorolaRecord)
>>> MotorolaRecord.save_records('save_records.mot', records)
>>> with open('save_records.mot', 'rt') as f: text = f.read()
>>> text
'S0030000FC\nS1060000616263D3\nS1060010646566BA\nS5030002FA\nS9030000FC\n'
classmethod set_header(records, data)[source]#

Sets the header data.

If existing, the header record is updated in-place. If missing, the header record is prepended.

Parameters:
  • records (list of records) – A sequence of records.

  • data (bytes) – Optional header data.

Returns:

list of records – Updated record list.

classmethod split(data, address=0, columns=16, align=Ellipsis, standalone=True, start=0, tag=None, header=b'')[source]#

Splits a chunk of data into records.

Parameters:
  • data (bytes) – Byte data to split.

  • address (int) – Start address of the first data record being split.

  • columns (int) – Maximum number of columns per data record. Maximum columns: 252 for S1, 251 for S2, 250 for S3.

  • align (int) – Aligns record addresses to such number. If Ellipsis, its value is resolved after columns.

  • standalone (bool) – Generates a sequence of records that can be saved as a standalone record file.

  • start (int) – Program start address. If Ellipsis, it is assigned the minimum data record address. If None, no start address records are output.

  • tag (tag) – Data tag record. If None, automatically selects the fitting one.

  • header (bytes) – Header byte data.

Yields:

record – Data split into records.

Raises:

ValueError – Address, size, or column overflow.

classmethod unmarshal(data, *args, **kwargs)[source]#

Unmarshals a record from input.

Parameters:
  • data (bytes or str) – Input data, according to the file type.

  • args (tuple) – Further positional arguments for overriding.

  • kwargs (dict) – Further keyword arguments for overriding.

Returns:

record – Unmarshaled record.

update_checksum()[source]#

Updates the checksum field via compute_count().

update_count()[source]#

Updates the count field via compute_count().

classmethod write_blocks(stream, blocks, split_args=None, split_kwargs=None, build_args=None, build_kwargs=None)[source]#

Writes blocks to a stream.

Each block of the blocks sequence is converted into a record via build_data() and written to the output stream.

Parameters:
  • stream (stream) – Output stream of the records to write.

  • blocks (list of blocks) – Sequence of records to store.

  • split_args (list) – Positional arguments for Record.split().

  • split_kwargs (dict) – Keyword arguments for Record.split().

  • build_args (list) – Positional arguments for Record.build_standalone().

  • build_kwargs (dict) – Keyword arguments for Record.build_standalone().

Example

>>> import io
>>> from hexrec.formats.motorola import Record as MotorolaRecord
>>> blocks = [[0, b'abc'], [16, b'def']]
>>> stream = io.StringIO()
>>> MotorolaRecord.write_blocks(stream, blocks)
>>> stream.getvalue()
'S0030000FC\nS1060000616263D3\nS1060010646566BA\nS5030002FA\nS9030000FC\n'
classmethod write_memory(stream, memory, split_args=None, split_kwargs=None, build_args=None, build_kwargs=None)[source]#

Writes a virtual memory to a stream.

Parameters:

Example

>>> import io
>>> from hexrec.records import Memory
>>> from hexrec.formats.motorola import Record as MotorolaRecord
>>> memory = Memory.from_blocks([[0, b'abc'], [16, b'def']])
>>> stream = io.StringIO()
>>> MotorolaRecord.write_memory(stream, memory)
>>> stream.getvalue()
'S0030000FC\nS1060000616263D3\nS1060010646566BA\nS5030002FA\nS9030000FC\n'
classmethod write_records(stream, records)[source]#

Saves records to a stream.

Each record of the records sequence is stored into the output file.

Parameters:
  • stream (stream) – Output stream of the records to write.

  • records (list of records) – Sequence of records to store.

Example

>>> import io
>>> from hexrec.formats.motorola import Record as MotorolaRecord
>>> from hexrec.records import blocks_to_records
>>> blocks = [[0, b'abc'], [16, b'def']]
>>> records = blocks_to_records(blocks, MotorolaRecord)
>>> stream = io.StringIO()
>>> MotorolaRecord.write_records(stream, records)
>>> stream.getvalue()
'S0030000FC\nS1060000616263D3\nS1060010646566BA\nS5030002FA\nS9030000FC\n'