From 682ae7543103650e93a849f4fed016a7c744596d Mon Sep 17 00:00:00 2001 From: dragonmux Date: Tue, 9 Aug 2022 00:25:02 +0100 Subject: [PATCH] remote: Rewrote remotehston using a for loop and a const-correct type signature --- src/remote.c | 35 ++++++++++++----------------------- src/remote.h | 2 +- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/src/remote.c b/src/remote.c index a38015e..086daa8 100644 --- a/src/remote.c +++ b/src/remote.c @@ -31,32 +31,21 @@ #include "target.h" #include "hex_utils.h" +#define NTOH(x) (((x) <= 9) ? (x) + '0' : 'a' + (x) - 10) +#define HTON(x) (((x) <= '9') ? (x) - '0' : ((TOUPPER(x)) - 'A' + 10)) +#define TOUPPER(x) ((((x) >= 'a') && ((x) <= 'z')) ? ((x) - ('a' - 'A')) : (x)) +#define ISHEX(x) ((((x) >= '0') && ((x) <= '9')) || (((x) >= 'A') && ((x) <= 'F')) || (((x) >= 'a') && ((x) <= 'f'))) -#define NTOH(x) ((x<=9)?x+'0':'a'+x-10) -#define HTON(x) ((x<='9')?x-'0':((TOUPPER(x))-'A'+10)) -#define TOUPPER(x) ((((x)>='a') && ((x)<='z'))?((x)-('a'-'A')):(x)) -#define ISHEX(x) ( \ - (((x)>='0') && ((x)<='9')) || \ - (((x)>='A') && ((x)<='F')) || \ - (((x)>='a') && ((x)<='f')) \ - ) - - -uint64_t remotehston(uint32_t limit, char *s) - -/* Return numeric version of string, until illegal hex digit, or limit */ - +/* Return numeric version of string, until illegal hex digit, or max */ +uint64_t remotehston(const uint32_t max, const char *const str) { - uint64_t ret=0L; - char c; - - while (limit--) { - c=*s++; - if (!ISHEX(c)) + uint64_t ret = 0; + for (size_t i = 0; i < max; ++i) { + const char value = str[i]; + if (!ISHEX(value)) return ret; - ret=(ret<<4)|HTON(c); - } - + ret = (ret << 4U) | HTON(value); + } return ret; } diff --git a/src/remote.h b/src/remote.h index eeb848b..f65d811 100644 --- a/src/remote.h +++ b/src/remote.h @@ -187,7 +187,7 @@ #define REMOTE_MEM_WRITE_SIZED_STR (char []){ REMOTE_SOM, REMOTE_HL_PACKET, REMOTE_AP_MEM_WRITE_SIZED, \ '%','0', '2', 'x', '%','0','2','x', HEX_U32(address), HEX_U32(count), 0} -uint64_t remotehston(uint32_t limit, char *s); +uint64_t remotehston(uint32_t limit, const char *s); void remotePacketProcess(unsigned int i, char *packet); #endif