remote: Rewrote remotehston using a for loop and a const-correct type signature

This commit is contained in:
dragonmux 2022-08-09 00:25:02 +01:00 committed by Piotr Esden-Tempski
parent 52160db72b
commit 682ae75431
2 changed files with 13 additions and 24 deletions

View File

@ -31,32 +31,21 @@
#include "target.h" #include "target.h"
#include "hex_utils.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) /* Return numeric version of string, until illegal hex digit, or max */
#define HTON(x) ((x<='9')?x-'0':((TOUPPER(x))-'A'+10)) uint64_t remotehston(const uint32_t max, const char *const str)
#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 */
{ {
uint64_t ret=0L; uint64_t ret = 0;
char c; for (size_t i = 0; i < max; ++i) {
const char value = str[i];
while (limit--) { if (!ISHEX(value))
c=*s++;
if (!ISHEX(c))
return ret; return ret;
ret=(ret<<4)|HTON(c); ret = (ret << 4U) | HTON(value);
} }
return ret; return ret;
} }

View File

@ -187,7 +187,7 @@
#define REMOTE_MEM_WRITE_SIZED_STR (char []){ REMOTE_SOM, REMOTE_HL_PACKET, REMOTE_AP_MEM_WRITE_SIZED, \ #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} '%','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); void remotePacketProcess(unsigned int i, char *packet);
#endif #endif