Skip to content

Commit 5b2d7a2

Browse files
committed
Require endianness specification for ints > 8 bits
1 parent 4c47f21 commit 5b2d7a2

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

tools/build_api.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,13 +369,18 @@ def _fill_header(region_list, current_region):
369369
_, type, subtype, data = member
370370
member_size = Config.header_member_size(member)
371371
if type == "const":
372-
fmt = {"8": "<B", "16": "<H", "32": "<L", "64": "<Q"}[subtype]
372+
fmt = {
373+
"8le": ">B", "16le": "<H", "32le": "<L", "64le": "<Q",
374+
"8be": "<B", "16be": ">H", "32be": ">L", "64be": ">Q"
375+
}[subtype]
373376
header.puts(start, struct.pack(fmt, int(data, 0)))
374377
elif type == "timestamp":
375-
fmt = {"32": "<L", "64": "<Q"}[subtype]
378+
fmt = {"32le": "<L", "64le": "<Q",
379+
"32be": ">L", "64be": ">Q"}[subtype]
376380
header.puts(start, struct.pack(fmt, time()))
377381
elif type == "size":
378-
fmt = {"32": "<L", "64": "<Q"}[subtype]
382+
fmt = {"32le": "<L", "64le": "<Q",
383+
"32be": ">L", "64be": ">Q"}[subtype]
379384
size = sum(_real_region_size(region_dict[r]) for r in data)
380385
header.puts(start, struct.pack(fmt, size))
381386
start += Config.header_member_size(member)
@@ -387,8 +392,9 @@ def _fill_header(region_list, current_region):
387392
ih = header
388393
else:
389394
ih = intelhex_offset(region_dict[data].filename, offset=region_dict[data].start)
390-
if subtype == "CRCITT32":
391-
header.puts(start, struct.pack("<l", zlib.crc32(ih.tobinarray())))
395+
if subtype.startswith("CRCITT32"):
396+
fmt = {"CRCITT32be": ">l", "CRCITT32le": "<l"}[subtype]
397+
header.puts(start, struct.pack(fmt, zlib.crc32(ih.tobinarray())))
392398
elif subtype.startswith("SHA"):
393399
if subtype == "SHA256":
394400
hash = hashlib.sha256()

tools/config/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,9 @@ def regions(self):
573573
def header_member_size(member):
574574
_, _, subtype, _ = member
575575
try:
576-
return int(subtype) // 8
576+
return int(subtype[:-2]) // 8
577577
except:
578-
if subtype == "CRCITT32":
578+
if subtype.startswith("CRCITT32"):
579579
return 32 // 8
580580
elif subtype == "SHA256":
581581
return 256 // 8

0 commit comments

Comments
 (0)