Skip to content

Commit cd783e6

Browse files
author
Alexander Belchenko
committed
Fixed bug with writing hex files with small chains of bytes.
1 parent 1b2e1b1 commit cd783e6

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

intelhex/__init__.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,17 +560,26 @@ def write_hex_file(self, f, write_start_addr=True):
560560
cur_ix,
561561
min(cur_ix+chain_len+1, addr_len))
562562
chain_len = ix - cur_ix # real chain_len
563+
# there could be small holes in the chain
564+
# but we will catch them by try-except later
565+
# so for big continuous files we will work
566+
# at maximum possible speed
563567
else:
564568
chain_len = 1 # real chain_len
565569

566570
bin = array('B', '\0'*(5+chain_len))
567-
bin[0] = chain_len
568571
bytes = divmod(low_addr, 256)
569572
bin[1] = bytes[0] # msb of low_addr
570573
bin[2] = bytes[1] # lsb of low_addr
571574
bin[3] = 0 # rectype
572-
for i in xrange(chain_len):
573-
bin[4+i] = self._buf[cur_addr+i]
575+
try: # if there is small holes we'll catch them
576+
for i in range(chain_len):
577+
bin[4+i] = self._buf[cur_addr+i]
578+
except KeyError:
579+
# we catch a hole so we should shrink the chain
580+
chain_len = i
581+
bin = bin[:5+i]
582+
bin[0] = chain_len
574583
bin[4+chain_len] = (-sum(bin)) & 0x0FF # chksum
575584
fwrite(':' + hexlify(bin.tostring()).translate(table) + '\n')
576585

intelhex/test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,12 @@
344344
:00000001FF
345345
"""
346346

347+
hex_bug_lp_341051 = """\
348+
:020FEC00E4E738
349+
:040FF00022E122E1F7
350+
:00000001FF
351+
"""
352+
347353

348354
##
349355
# Test cases
@@ -489,6 +495,14 @@ def test_write_hexfile(self):
489495
sio.close()
490496
self.assertEqualWrittenData(hex_simple, s)
491497

498+
def test_write_hex_bug_341051(self):
499+
ih = intelhex.IntelHex(StringIO(hex_bug_lp_341051))
500+
sio = StringIO()
501+
ih.tofile(sio, format='hex')
502+
s = sio.getvalue()
503+
sio.close()
504+
self.assertEqualWrittenData(hex_bug_lp_341051, s)
505+
492506
def test_write_hex_first_extended_linear_address(self):
493507
ih = IntelHex({0x20000: 0x01})
494508
sio = StringIO()

0 commit comments

Comments
 (0)