ctr: Encode integers in hex

Replace the custom encoding with a hex encoding.  This makes it a
little easier to inspect the CTR conversions.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-08-22 11:08:51 -04:00
parent 69fc1e63b4
commit e59d875256
2 changed files with 11 additions and 8 deletions

View File

@ -125,9 +125,9 @@ Handlers.append(HandlerEnumerations)
def decode_integer(value):
value = value.strip()
if len(value) != 7 or value[0] not in '-+':
if len(value) != 11 or value[0] not in '-+' or value[1:3] != '0x':
error("Invalid encoded integer '%s'" % (value,))
out = sum([(ord(c) - 48) << (i*6) for i, c in enumerate(value[1:])])
out = int(value[1:], 0)
if value[0] == '-':
out -= 1<<32
return out

View File

@ -14,17 +14,20 @@
static char __PASTE(_DECLS_, __LINE__)[] __attribute__((used)) \
__section(".compile_time_request") = (REQUEST)
#define CTR_INT(V, S) ((((uint32_t)(V) >> S) & 0x3f) + 48)
// Helper macros for encoding an integer
#define CTR_HEX(H) ((H) > 9 ? (H) - 10 + 'A' : (H) + '0')
#define CTR_INT(V, S) CTR_HEX(((uint32_t)(V) >> (S)) & 0x0f)
// Declare a compile time request with an integer expression
#define DECL_CTR_INT(REQUEST, VALUE) \
static struct { char _r[sizeof(REQUEST)]; char _v[8]; } \
static struct { char _r[sizeof(REQUEST)]; char _v[12]; } \
__PASTE(_DECLI_, __LINE__) __attribute__((used)) \
__section(".compile_time_request") = { \
REQUEST " ", { \
(VALUE) < 0 ? '-' : '+', \
CTR_INT((VALUE),0), CTR_INT((VALUE),6), \
CTR_INT((VALUE),12), CTR_INT((VALUE),18), \
CTR_INT((VALUE),24), CTR_INT((VALUE),30), 0 } }
(VALUE) < 0 ? '-' : '+', '0', 'x', \
CTR_INT((VALUE),28), CTR_INT((VALUE),24), \
CTR_INT((VALUE),20), CTR_INT((VALUE),16), \
CTR_INT((VALUE),12), CTR_INT((VALUE),8), \
CTR_INT((VALUE),4), CTR_INT((VALUE),0), 0 } }
#endif // ctr.h