From 0b143e5bcd02dd9598eb03406149691d36cde0e4 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Mon, 14 Aug 2023 18:57:36 -0700 Subject: [PATCH] Add 5..8 byte versions for LE/BE conversions --- common/commonutil.c | 252 +++++++++++++++++++++++++++++++++++++------- common/commonutil.h | 19 ++++ 2 files changed, 235 insertions(+), 36 deletions(-) diff --git a/common/commonutil.c b/common/commonutil.c index 07b55f1f9..2e850fb39 100644 --- a/common/commonutil.c +++ b/common/commonutil.c @@ -161,75 +161,255 @@ uint64_t bytes_to_num(uint8_t *src, size_t len) { } uint16_t MemLeToUint2byte(const uint8_t *data) { - return (uint16_t)((uint16_t) - (data[1] << 8) + data[0] - ); + return (uint16_t)( + (((uint16_t)(data[1])) << (8*1)) + + (((uint16_t)(data[0])) << (8*0)) + ); } uint32_t MemLeToUint3byte(const uint8_t *data) { - return (uint32_t)((uint32_t) - (data[2] << 16) + (data[1] << 8) + data[0] - ); + return (uint32_t)( + (((uint32_t)(data[2])) << (8*2)) + + (((uint32_t)(data[1])) << (8*1)) + + (((uint32_t)(data[0])) << (8*0)) + ); } uint32_t MemLeToUint4byte(const uint8_t *data) { - return (uint32_t)((uint32_t) - (data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0] - ); + return (uint32_t)( + (((uint32_t)(data[3])) << (8*3)) + + (((uint32_t)(data[2])) << (8*2)) + + (((uint32_t)(data[1])) << (8*1)) + + (((uint32_t)(data[0])) << (8*0)) + ); +} + +uint64_t MemLeToUint5byte(const uint8_t *data) { + return (uint64_t)( + (((uint64_t)(data[4])) << (8*4)) + + (((uint64_t)(data[3])) << (8*3)) + + (((uint64_t)(data[2])) << (8*2)) + + (((uint64_t)(data[1])) << (8*1)) + + (((uint64_t)(data[0])) << (8*0)) + ); +} + +uint64_t MemLeToUint6byte(const uint8_t *data) { + return (uint64_t)( + (((uint64_t)(data[5])) << (8*5)) + + (((uint64_t)(data[4])) << (8*4)) + + (((uint64_t)(data[3])) << (8*3)) + + (((uint64_t)(data[2])) << (8*2)) + + (((uint64_t)(data[1])) << (8*1)) + + (((uint64_t)(data[0])) << (8*0)) + ); +} + +uint64_t MemLeToUint7byte(const uint8_t *data) { + return (uint64_t)( + (((uint64_t)(data[6])) << (8*6)) + + (((uint64_t)(data[5])) << (8*5)) + + (((uint64_t)(data[4])) << (8*4)) + + (((uint64_t)(data[3])) << (8*3)) + + (((uint64_t)(data[2])) << (8*2)) + + (((uint64_t)(data[1])) << (8*1)) + + (((uint64_t)(data[0])) << (8*0)) + ); +} + +uint64_t MemLeToUint8byte(const uint8_t *data) { + return (uint64_t)( + (((uint64_t)(data[7])) << (8*7)) + + (((uint64_t)(data[6])) << (8*6)) + + (((uint64_t)(data[5])) << (8*5)) + + (((uint64_t)(data[4])) << (8*4)) + + (((uint64_t)(data[3])) << (8*3)) + + (((uint64_t)(data[2])) << (8*2)) + + (((uint64_t)(data[1])) << (8*1)) + + (((uint64_t)(data[0])) << (8*0)) + ); } uint16_t MemBeToUint2byte(const uint8_t *data) { - return (uint16_t)((uint16_t) - (data[0] << 8) + data[1] - ); + return (uint16_t)( + (((uint16_t)(data[0])) << (8*1)) + + (((uint16_t)(data[1])) << (8*0)) + ); } uint32_t MemBeToUint3byte(const uint8_t *data) { - return (uint32_t)((uint32_t) - (data[0] << 16) + (data[1] << 8) + data[2] - ); + return (uint32_t)( + (((uint32_t)(data[0])) << (8*2)) + + (((uint32_t)(data[1])) << (8*1)) + + (((uint32_t)(data[2])) << (8*0)) + ); } -inline uint32_t MemBeToUint4byte(const uint8_t *data) { +uint32_t MemBeToUint4byte(const uint8_t *data) { return (uint32_t)( - (data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3] - ); + (((uint32_t)(data[0])) << (8*3)) + + (((uint32_t)(data[1])) << (8*2)) + + (((uint32_t)(data[2])) << (8*1)) + + (((uint32_t)(data[3])) << (8*0)) + ); +} + +uint64_t MemBeToUint5byte(const uint8_t *data) { + return (uint64_t)( + (((uint64_t)(data[0])) << (8*4)) + + (((uint64_t)(data[1])) << (8*3)) + + (((uint64_t)(data[2])) << (8*2)) + + (((uint64_t)(data[3])) << (8*1)) + + (((uint64_t)(data[4])) << (8*0)) + ); +} + +uint64_t MemBeToUint6byte(const uint8_t *data) { + return (uint64_t)( + (((uint64_t)(data[0])) << (8*5)) + + (((uint64_t)(data[1])) << (8*4)) + + (((uint64_t)(data[2])) << (8*3)) + + (((uint64_t)(data[3])) << (8*2)) + + (((uint64_t)(data[4])) << (8*1)) + + (((uint64_t)(data[5])) << (8*0)) + ); +} + +uint64_t MemBeToUint7byte(const uint8_t *data) { + return (uint64_t)( + (((uint64_t)(data[0])) << (8*6)) + + (((uint64_t)(data[1])) << (8*5)) + + (((uint64_t)(data[2])) << (8*4)) + + (((uint64_t)(data[3])) << (8*3)) + + (((uint64_t)(data[4])) << (8*2)) + + (((uint64_t)(data[5])) << (8*1)) + + (((uint64_t)(data[6])) << (8*0)) + ); +} + +uint64_t MemBeToUint8byte(const uint8_t *data) { + return (uint64_t)( + (((uint64_t)(data[0])) << (8*7)) + + (((uint64_t)(data[1])) << (8*6)) + + (((uint64_t)(data[2])) << (8*5)) + + (((uint64_t)(data[3])) << (8*4)) + + (((uint64_t)(data[4])) << (8*3)) + + (((uint64_t)(data[5])) << (8*2)) + + (((uint64_t)(data[6])) << (8*1)) + + (((uint64_t)(data[7])) << (8*0)) + ); } void Uint2byteToMemLe(uint8_t *data, uint16_t value) { - data[1] = (value >> 8) & 0xff; - data[0] = value & 0xff; + data[0] = (uint8_t)((value >> (8*0)) & 0xffu); + data[1] = (uint8_t)((value >> (8*1)) & 0xffu); } void Uint3byteToMemLe(uint8_t *data, uint32_t value) { - data[2] = (value >> 16) & 0xff; - data[1] = (value >> 8) & 0xff; - data[0] = value & 0xff; + data[0] = (uint8_t)((value >> (8*0)) & 0xffu); + data[1] = (uint8_t)((value >> (8*1)) & 0xffu); + data[2] = (uint8_t)((value >> (8*2)) & 0xffu); } void Uint4byteToMemLe(uint8_t *data, uint32_t value) { - data[3] = (value >> 24) & 0xff; - data[2] = (value >> 16) & 0xff; - data[1] = (value >> 8) & 0xff; - data[0] = value & 0xff; + data[0] = (uint8_t)((value >> (8*0)) & 0xffu); + data[1] = (uint8_t)((value >> (8*1)) & 0xffu); + data[2] = (uint8_t)((value >> (8*2)) & 0xffu); + data[3] = (uint8_t)((value >> (8*3)) & 0xffu); +} + +void Uint5byteToMemLe(uint8_t *data, uint64_t value) { + data[0] = (uint8_t)((value >> (8*0)) & 0xffu); + data[1] = (uint8_t)((value >> (8*1)) & 0xffu); + data[2] = (uint8_t)((value >> (8*2)) & 0xffu); + data[3] = (uint8_t)((value >> (8*3)) & 0xffu); + data[4] = (uint8_t)((value >> (8*4)) & 0xffu); +} + +void Uint6byteToMemLe(uint8_t *data, uint64_t value) { + data[0] = (uint8_t)((value >> (8*0)) & 0xffu); + data[1] = (uint8_t)((value >> (8*1)) & 0xffu); + data[2] = (uint8_t)((value >> (8*2)) & 0xffu); + data[3] = (uint8_t)((value >> (8*3)) & 0xffu); + data[4] = (uint8_t)((value >> (8*4)) & 0xffu); + data[5] = (uint8_t)((value >> (8*5)) & 0xffu); +} + +void Uint7byteToMemLe(uint8_t *data, uint64_t value) { + data[0] = (uint8_t)((value >> (8*0)) & 0xffu); + data[1] = (uint8_t)((value >> (8*1)) & 0xffu); + data[2] = (uint8_t)((value >> (8*2)) & 0xffu); + data[3] = (uint8_t)((value >> (8*3)) & 0xffu); + data[4] = (uint8_t)((value >> (8*4)) & 0xffu); + data[5] = (uint8_t)((value >> (8*5)) & 0xffu); + data[6] = (uint8_t)((value >> (8*6)) & 0xffu); +} + +void Uint8byteToMemLe(uint8_t *data, uint64_t value) { + data[0] = (uint8_t)((value >> (8*0)) & 0xffu); + data[1] = (uint8_t)((value >> (8*1)) & 0xffu); + data[2] = (uint8_t)((value >> (8*2)) & 0xffu); + data[3] = (uint8_t)((value >> (8*3)) & 0xffu); + data[4] = (uint8_t)((value >> (8*4)) & 0xffu); + data[5] = (uint8_t)((value >> (8*5)) & 0xffu); + data[6] = (uint8_t)((value >> (8*6)) & 0xffu); + data[7] = (uint8_t)((value >> (8*7)) & 0xffu); } void Uint2byteToMemBe(uint8_t *data, uint16_t value) { - data[0] = (value >> 8) & 0xff; - data[1] = value & 0xff; + data[0] = (uint8_t)((value >> (8*1)) & 0xffu); + data[1] = (uint8_t)((value >> (8*0)) & 0xffu); } void Uint3byteToMemBe(uint8_t *data, uint32_t value) { - data[0] = (value >> 16) & 0xff; - data[1] = (value >> 8) & 0xff; - data[2] = value & 0xff; + data[0] = (uint8_t)((value >> (8*2)) & 0xffu); + data[1] = (uint8_t)((value >> (8*1)) & 0xffu); + data[2] = (uint8_t)((value >> (8*0)) & 0xffu); } void Uint4byteToMemBe(uint8_t *data, uint32_t value) { - data[0] = (value >> 24) & 0xff; - data[1] = (value >> 16) & 0xff; - data[2] = (value >> 8) & 0xff; - data[3] = value & 0xff; + data[0] = (uint8_t)((value >> (8*3)) & 0xffu); + data[1] = (uint8_t)((value >> (8*2)) & 0xffu); + data[2] = (uint8_t)((value >> (8*1)) & 0xffu); + data[3] = (uint8_t)((value >> (8*0)) & 0xffu); +} + +void Uint5byteToMemBe(uint8_t *data, uint64_t value) { + data[0] = (uint8_t)((value >> (8*4)) & 0xffu); + data[1] = (uint8_t)((value >> (8*3)) & 0xffu); + data[2] = (uint8_t)((value >> (8*2)) & 0xffu); + data[3] = (uint8_t)((value >> (8*1)) & 0xffu); + data[4] = (uint8_t)((value >> (8*0)) & 0xffu); +} + +void Uint6byteToMemBe(uint8_t *data, uint64_t value) { + data[0] = (uint8_t)((value >> (8*5)) & 0xffu); + data[1] = (uint8_t)((value >> (8*4)) & 0xffu); + data[2] = (uint8_t)((value >> (8*3)) & 0xffu); + data[3] = (uint8_t)((value >> (8*2)) & 0xffu); + data[4] = (uint8_t)((value >> (8*1)) & 0xffu); + data[5] = (uint8_t)((value >> (8*0)) & 0xffu); +} + +void Uint7byteToMemBe(uint8_t *data, uint64_t value) { + data[0] = (uint8_t)((value >> (8*6)) & 0xffu); + data[1] = (uint8_t)((value >> (8*5)) & 0xffu); + data[2] = (uint8_t)((value >> (8*4)) & 0xffu); + data[3] = (uint8_t)((value >> (8*3)) & 0xffu); + data[4] = (uint8_t)((value >> (8*2)) & 0xffu); + data[5] = (uint8_t)((value >> (8*1)) & 0xffu); + data[6] = (uint8_t)((value >> (8*0)) & 0xffu); +} + +void Uint8byteToMemBe(uint8_t *data, uint64_t value) { + data[0] = (uint8_t)((value >> (8*7)) & 0xffu); + data[1] = (uint8_t)((value >> (8*6)) & 0xffu); + data[2] = (uint8_t)((value >> (8*5)) & 0xffu); + data[3] = (uint8_t)((value >> (8*4)) & 0xffu); + data[4] = (uint8_t)((value >> (8*3)) & 0xffu); + data[5] = (uint8_t)((value >> (8*2)) & 0xffu); + data[6] = (uint8_t)((value >> (8*1)) & 0xffu); + data[7] = (uint8_t)((value >> (8*0)) & 0xffu); } // RotateLeft - Ultralight, Desfire diff --git a/common/commonutil.h b/common/commonutil.h index 7beb95355..23dd27f85 100644 --- a/common/commonutil.h +++ b/common/commonutil.h @@ -65,15 +65,34 @@ uint64_t bytes_to_num(uint8_t *src, size_t len); uint16_t MemLeToUint2byte(const uint8_t *data); uint32_t MemLeToUint3byte(const uint8_t *data); uint32_t MemLeToUint4byte(const uint8_t *data); +uint64_t MemLeToUint5byte(const uint8_t *data); +uint64_t MemLeToUint6byte(const uint8_t *data); +uint64_t MemLeToUint7byte(const uint8_t *data); +uint64_t MemLeToUint8byte(const uint8_t *data); + uint16_t MemBeToUint2byte(const uint8_t *data); uint32_t MemBeToUint3byte(const uint8_t *data); uint32_t MemBeToUint4byte(const uint8_t *data); +uint64_t MemBeToUint5byte(const uint8_t *data); +uint64_t MemBeToUint6byte(const uint8_t *data); +uint64_t MemBeToUint7byte(const uint8_t *data); +uint64_t MemBeToUint8byte(const uint8_t *data); + void Uint2byteToMemLe(uint8_t *data, uint16_t value); void Uint3byteToMemLe(uint8_t *data, uint32_t value); void Uint4byteToMemLe(uint8_t *data, uint32_t value); +void Uint5byteToMemLe(uint8_t *data, uint64_t value); +void Uint6byteToMemLe(uint8_t *data, uint64_t value); +void Uint7byteToMemLe(uint8_t *data, uint64_t value); +void Uint8byteToMemLe(uint8_t *data, uint64_t value); + void Uint2byteToMemBe(uint8_t *data, uint16_t value); void Uint3byteToMemBe(uint8_t *data, uint32_t value); void Uint4byteToMemBe(uint8_t *data, uint32_t value); +void Uint5byteToMemBe(uint8_t *data, uint64_t value); +void Uint6byteToMemBe(uint8_t *data, uint64_t value); +void Uint7byteToMemBe(uint8_t *data, uint64_t value); +void Uint8byteToMemBe(uint8_t *data, uint64_t value); // rotate left byte array void rol(uint8_t *data, const size_t len);