From 8688066af301f3eee5a6b3f6b766766a5af7c9d5 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Fri, 11 Apr 2025 05:26:12 +0200 Subject: [PATCH] cleaner hexa string --- morep.c | 4 ++-- morep_simulator.c | 6 ------ morep_valid.c | 3 +-- parse.c | 22 +++++++++++----------- script-cross_crypto.eth | 4 ++-- script-lo.eth | 4 ++-- script-local_crypto.eth | 4 ++-- script-prng.eth | 2 +- script-provisioning.eth | 2 +- script-rx.eth | 4 ++-- script-tx.eth | 4 ++-- 11 files changed, 26 insertions(+), 33 deletions(-) diff --git a/morep.c b/morep.c index eb7b1ac..05bbcef 100644 --- a/morep.c +++ b/morep.c @@ -372,7 +372,7 @@ int MOREP_Send (int index, uint8_t msgtype, uint8_t *buffer, int len) VERBOSE (morep, DEBUG, PRINTF ("flag/frag/length: %d/%d/%d\n", (pklen != len), nfrag, pklen)); uint16_t msglen = htons (pklen | (nfrag << 11) | ((pklen != len) << 15)); memcpy (morep->tx_buffer + ETHER_PREAMBLE + 2, (void *) &msglen, 2); - VERBOSE (morep, DEBUG, PRINTF ("sent preamble: "); int i; for (i = 0; i < ETHER_PREAMBLE + MOREP_PREAMBLE; i++) printf ("%02x:", morep->tx_buffer[i]); printf ("\n")); + VERBOSE (morep, DEBUG, PRINTF ("sent preamble: "); int i; for (i = 0; i < ETHER_PREAMBLE + MOREP_PREAMBLE; i++) printf ("%02x%c", morep->tx_buffer[i], (i == ETHER_PREAMBLE + MOREP_PREAMBLE - 1) ? '\n' : ':')); memcpy (morep->tx_buffer + ETHER_PREAMBLE + MOREP_PREAMBLE, buffer, pklen); int txlen = pklen + ETHER_PREAMBLE + MOREP_PREAMBLE; @@ -414,7 +414,7 @@ int MOREP_Receive (int index, uint8_t *msgtype, uint8_t *buffer, int *len) int flag = 0; do { rxlen = recvfrom (morep->sock, morep->rx_buffer, ETHER_PREAMBLE + MAX_ETHER_SIZE, 0, NULL, 0); - VERBOSE (morep, DEBUG, PRINTF ("rec. preamble: "); int i; for (i = 0; i < ETHER_PREAMBLE + MOREP_PREAMBLE; i++) printf ("%02x:", morep->rx_buffer[i]); printf ("\n")); + VERBOSE (morep, DEBUG, PRINTF ("rec. preamble: "); int i; for (i = 0; i < ETHER_PREAMBLE + MOREP_PREAMBLE; i++) printf ("%02x%c", morep->tx_buffer[i], (i == ETHER_PREAMBLE + MOREP_PREAMBLE - 1) ? '\n' : ':')); /* sequence number */ int pkgseqnum = morep->rx_buffer[ETHER_PREAMBLE + 1]; diff --git a/morep_simulator.c b/morep_simulator.c index 11cc979..b506fd8 100644 --- a/morep_simulator.c +++ b/morep_simulator.c @@ -604,16 +604,10 @@ int main (int argc, char **argv) /* test: morep_simulator.exe -t 2>&1 | grep 'url not specified' */ /* test: morep_simulator.exe -l - -t lo://00:00:00:00:00:00/0809 -v 4 script-lo.eth */ /* test: morep_simulator.exe -l script-cross_crypto.log -s CROSS_CRYPTO -t lo://00:00:00:00:00:00/0808 -r lo://00:00:00:00:00:00/0808 script-cross_crypto.eth */ -/* test: morep_simulator.exe -l script-cross_crypto.log -s CROSS_CRYPTO -t lo://00:00:00:00:00:00/0808 -r lo://00:00:00:00:00:00/0808 script-cross_crypto.eth | grep -v WARNING */ /* test: morep_simulator.exe -l script-local_crypto.log -s LOCAL_CRYPTO -t lo://00:00:00:00:00:00/0808 -r lo://00:00:00:00:00:00/0808 script-local_crypto.eth */ -/* test: morep_simulator.exe -l script-local_crypto.log -s LOCAL_CRYPTO -t lo://00:00:00:00:00:00/0808 -r lo://00:00:00:00:00:00/0808 script-local_crypto.eth | grep -v WARNING */ /* test: morep_simulator.exe -l script-provisioning.log -s PROVISIONING -t lo://00:00:00:00:00:00/0808 -r lo://00:00:00:00:00:00/0808 script-provisioning.eth */ -/* test: morep_simulator.exe -l script-provisioning.log -s PROVISIONING -t lo://00:00:00:00:00:00/0808 -r lo://00:00:00:00:00:00/0808 script-provisioning.eth | grep -v WARNING */ /* test: morep_simulator.exe -l script-prng.log -s PRNG -t lo://00:00:00:00:00:00/0808 -r lo://00:00:00:00:00:00/0808 script-prng.eth */ -/* test: morep_simulator.exe -l script-prng.log -s PRNG -t lo://00:00:00:00:00:00/0808 -r lo://00:00:00:00:00:00/0808 script-prng.eth | grep -v WARNING */ /* test: morep_simulator.exe -l script-bypass.log -s BYPASS -t lo://00:00:00:00:00:00/0808 -r lo://00:00:00:00:00:00/0808 script-bypass.eth */ -/* test: morep_simulator.exe -l script-bypass.log -s BYPASS -t lo://00:00:00:00:00:00/0808 -r lo://00:00:00:00:00:00/0808 script-bypass.eth | grep -v WARNING */ /* test: morep_simulator.exe -l script-control.log -s CONTROL -t lo://00:00:00:00:00:00/0808 -r lo://00:00:00:00:00:00/0808 script-control.eth -v 3 */ -/* test: morep_simulator.exe -l script-control.log -s CONTROL -t lo://00:00:00:00:00:00/0808 -r lo://00:00:00:00:00:00/0808 script-control.eth -v 3 | grep -v WARNING */ /* vim: set ts=4 sw=4 si et: */ diff --git a/morep_valid.c b/morep_valid.c index bfd84ad..04a0de8 100644 --- a/morep_valid.c +++ b/morep_valid.c @@ -90,10 +90,9 @@ void print_message (FILE *fd, char *etype, int mode, uint8_t msg, int seqnum, ui int i; fprintf (fd ? fd : stdout, " PAYLOAD="); for (i = 0; i < len; i++) { - fprintf (fd, "%02x", payload[i]); + fprintf (fd, "%02x%c", payload[i], (i == len - 1) ? '\n' : ':'); } } - fprintf (fd ? fd : stdout, "\n"); } typedef struct { diff --git a/parse.c b/parse.c index 8f6e443..5129d91 100644 --- a/parse.c +++ b/parse.c @@ -83,12 +83,12 @@ int parse_array (char *str, uint8_t *buffer, int maxlen) /* hexa payload: xxxxxx [0-9a-fA-F] */ else { - if (maxlen * 2 < slen) { - slen = maxlen * 2; + if (maxlen * 3 - 1 < slen) { + slen = maxlen * 3 - 1; } - if (slen % 2 == 0) { + if (slen % 3 == 2) { VERBOSE (morep, TRACE, PRINTF ("hexa payload: xxxxxx\n")); - len = slen / 2; + len = slen / 3 + 1; if (len > maxlen) { VERBOSE (morep, WARNING, PRINTF ("string too large (%d > %d) for '%s'\n", len, maxlen, str)); len = maxlen; @@ -96,11 +96,11 @@ int parse_array (char *str, uint8_t *buffer, int maxlen) for (int i = 0; i < len; i++) { char digit[3] = {0}; char *ptr = NULL; - digit[0] = str[2 * i]; - digit[1] = str[2 * i + 1]; + digit[0] = str[3 * i]; + digit[1] = str[3 * i + 1]; buffer[i] = strtol (digit, &ptr, 16); - if ((*ptr != '\0') && (*ptr != ' ') && (*ptr != '\t')) { - VERBOSE (morep, ERROR, PRINTF ("unrecognize hexa-string (%d) '%s'|%s\n", 2 * i, str, ptr)); + if ((*ptr != ':') && (*ptr != '\0') && (*ptr != ' ') && (*ptr != '\t')) { + VERBOSE (morep, ERROR, PRINTF ("unrecognize hexa-string (%d) '%s'\n", 3 * i, str)); break; } } @@ -116,11 +116,11 @@ int parse_array (char *str, uint8_t *buffer, int maxlen) int snprint_array (char *buffer, int size, char *name, uint8_t *data, int nb) { - int i; int len = snprintf (buffer, size, " %s[%d]=", name, nb); - for (i = 0; (i < nb) && (len < size); i++) { - len += snprintf (buffer + len, size - len, "%02x", data[i]); + for (int i = 0; (i < nb) && (len < size); i++) { + len += snprintf (buffer + len, size - len, "%02x:", data[i]); } + buffer[--len] = '\0'; return len; } diff --git a/script-cross_crypto.eth b/script-cross_crypto.eth index 90edfae..67b46ce 100644 --- a/script-cross_crypto.eth +++ b/script-cross_crypto.eth @@ -1,6 +1,6 @@ # Cross crypto test script -T:CROSS_CRYPTO ENCRYPT_CROSS_ASYNC CHANNELID=2 BYPASSLEN=4 BYPASS=11223344 DATA=@script-local_crypto.eth +T:CROSS_CRYPTO ENCRYPT_CROSS_ASYNC CHANNELID=2 BYPASSLEN=4 BYPASS=11:22:33:44 DATA=@script-local_crypto.eth R:CROSS_CRYPTO ENCRYPT_CROSS_ASYNC T:CROSS_CRYPTO ENCRYPTED_CROSS_ASYNC CHANNELID=9 BYPASSLEN=1 BYPASS=55 DATA=@script-local_crypto.eth @@ -9,5 +9,5 @@ R:CROSS_CRYPTO ENCRYPTED_CROSS_ASYNC T:CROSS_CRYPTO DECRYPT_CROSS_ASYNC CHANNELID=5 BYPASSLEN=0 DATA=@script-local_crypto.eth R:CROSS_CRYPTO DECRYPT_CROSS_ASYNC -T:CROSS_CRYPTO DECRYPTED_CROSS_ASYNC CHANNELID=0 BYPASSLEN=4 BYPASS=11223344 DATA=@script-cross_crypto.eth +T:CROSS_CRYPTO DECRYPTED_CROSS_ASYNC CHANNELID=0 BYPASSLEN=4 BYPASS=11:22:33:44 DATA=@script-cross_crypto.eth R:CROSS_CRYPTO DECRYPTED_CROSS_ASYNC diff --git a/script-lo.eth b/script-lo.eth index 4b57454..0daf6bc 100644 --- a/script-lo.eth +++ b/script-lo.eth @@ -5,8 +5,8 @@ R0808 MSG=42 DATA="Hello_world" SLEEP 1000 -T0808 MSG=24 DATA=42796520776f726c64 -R0808 MSG=24 DATA=42796520776f726c64 +T0808 MSG=24 DATA=42:79:65:20:77:6f:72:6c:64 +R0808 MSG=24 DATA=42:79:65:20:77:6f:72:6c:64 T0808 MSG=10 DATA=@script-lo.eth R0808 MSG=10 DATA=@script-lo.eth diff --git a/script-local_crypto.eth b/script-local_crypto.eth index cfca6d5..2b5ce91 100644 --- a/script-local_crypto.eth +++ b/script-local_crypto.eth @@ -1,6 +1,6 @@ # Local crypto test script -T:LOCAL_CRYPTO ENCRYPT_LOCAL_ASYNC CHANNELID=2 BYPASSLEN=4 BYPASS=11223344 DATA=@script-cross_crypto.eth +T:LOCAL_CRYPTO ENCRYPT_LOCAL_ASYNC CHANNELID=2 BYPASSLEN=3 BYPASS=11:22:33:44 DATA=@script-cross_crypto.eth R:LOCAL_CRYPTO ENCRYPT_LOCAL_ASYNC T:LOCAL_CRYPTO ENCRYPTED_LOCAL_ASYNC CHANNELID=9 BYPASSLEN=0 DATA=@script-local_crypto.eth @@ -9,5 +9,5 @@ R:LOCAL_CRYPTO ENCRYPTED_LOCAL_ASYNC T:LOCAL_CRYPTO DECRYPT_LOCAL_ASYNC CHANNELID=5 BYPASSLEN=0 DATA=@script-local_crypto.eth R:LOCAL_CRYPTO DECRYPT_LOCAL_ASYNC -T:LOCAL_CRYPTO DECRYPTED_LOCAL_ASYNC CHANNELID=0 BYPASSLEN=4 BYPASS=11223344 DATA=@script-cross_crypto.eth +T:LOCAL_CRYPTO DECRYPTED_LOCAL_ASYNC CHANNELID=0 BYPASSLEN=3 BYPASS=11:22:33:44 DATA=@script-cross_crypto.eth R:LOCAL_CRYPTO DECRYPTED_LOCAL_ASYNC diff --git a/script-prng.eth b/script-prng.eth index 517cce3..2e6a47d 100644 --- a/script-prng.eth +++ b/script-prng.eth @@ -1,6 +1,6 @@ # PRNG test script -T:PRNG RANDOM_REQ PRNGID=0 SEQLEN=64 SEED=0102030405060708090A0B0C0D0E0F10 +T:PRNG RANDOM_REQ PRNGID=0 SEQLEN=64 SEED=01:02:03:04:05:06:07:08:09:0A:0B:0C:0D:0E:0F:10 R:PRNG RANDOM_REQ T:PRNG RANDOM_RESP DATA=@script-prng.eth diff --git a/script-provisioning.eth b/script-provisioning.eth index cbcb1a1..d078eb3 100644 --- a/script-provisioning.eth +++ b/script-provisioning.eth @@ -1,6 +1,6 @@ # Provisioning test script -T:PROVISIONING LOAD_KEY_REQ KEYID=2 KEY=0102030405060708090A0B0C0D0E0F10 +T:PROVISIONING LOAD_KEY_REQ KEYID=2 KEY=01:02:03:04:05:06:07:08:09:0A:0B:0C:0D:0E:0F:10 R:PROVISIONING LOAD_KEY_REQ T:PROVISIONING LOAD_KEY_RESP STATUS=0 diff --git a/script-rx.eth b/script-rx.eth index 4d2c191..271be01 100644 --- a/script-rx.eth +++ b/script-rx.eth @@ -5,8 +5,8 @@ T0809 MSG=43 DATA="Bye" SLEEP 1000 -R0808 MSG=24 DATA=42796520776f726c64 -T0809 MSG=25 DATA=48656c6c6f +R0808 MSG=24 DATA=42:79:65:20:77:6f:72:6c:64 +T0809 MSG=25 DATA=48:65:6c:6c:6f R0808 MSG=10 DATA=@makefile T0809 MSG=11 DATA="OK" diff --git a/script-tx.eth b/script-tx.eth index c09411d..919bb0f 100644 --- a/script-tx.eth +++ b/script-tx.eth @@ -5,8 +5,8 @@ R0809 MSG=43 DATA="Bye" SLEEP 1000 -T0808 MSG=24 DATA=42796520776f726c64 -R0809 MSG=25 DATA=48656c6c6f +T0808 MSG=24 DATA=42:79:65:20:77:6f:72:6c:64 +R0809 MSG=25 DATA=48:65:6c:6c:6f T0808 MSG=10 DATA=@makefile R0809 MSG=11 DATA="OK" -- 2.30.2