fix padding
authorMazet Laurent <laurent.mazet@thalesgroup.com>
Tue, 25 Mar 2025 03:25:43 +0000 (04:25 +0100)
committerMazet Laurent <laurent.mazet@thalesgroup.com>
Tue, 25 Mar 2025 03:25:43 +0000 (04:25 +0100)
morep.c

diff --git a/morep.c b/morep.c
index fd1f6c90d23c13d6e0dd3e0d80b3cbc611b37916..8f3f61d77cccc839225fc9b7f9c4f4228b44a2a0 100644 (file)
--- a/morep.c
+++ b/morep.c
@@ -329,7 +329,13 @@ int MOREP_Send (int index, uint8_t msgtype, int len)
     memcpy (morep->tx_buffer + 16, (void *) &msglen, 2);
     VERBOSE (morep, DEBUG, PRINTF ("sent preamble: "); int i; for (i = 0; i < 18; i++) printf ("%02x:", morep->tx_buffer[i]); printf ("\n"));
 
-    int rc = sendto (morep->sock, morep->tx_buffer, len + 18, 0, NULL, 0);
+    int txlen = len + 18;
+    if (txlen < 64) {
+        VERBOSE (morep, DEBUG, PRINTF ("need padding: %d bytes\n",  64 - txlen));
+        memset (morep->tx_buffer + txlen, 0, 64 - txlen);
+        txlen = 64;
+    }
+    int rc = sendto (morep->sock, morep->tx_buffer, txlen, 0, NULL, 0);
 
     return (rc < 18) ? -1 : seqnum;
 }
@@ -372,7 +378,10 @@ int MOREP_Receive (int index, uint8_t *msgtype)
     uint16_t msglen = 0;
     memcpy ((void *) &msglen, morep->rx_buffer + 16, 2);
     morep->rxlen = htons (msglen);
-    if (morep->rxlen != len - 18) {
+    if (morep->rxlen < 64 - 18) {
+        VERBOSE (morep, DEBUG, PRINTF ("padding of %d bytes\n", 64 - 18 - morep->rxlen));
+        VERBOSE (morep, DEBUG, int i; for (i = morep->rxlen + 18; i < 64; i++) if (morep->rx_buffer[i] != 0) { VERBOSE (morep, WARNING, PRINTF ("incorrect padding\n")); break; });
+    } else if (morep->rxlen != len - 18) {
         VERBOSE (morep, WARNING, PRINTF ("incorrect size (%d/%d)\n", morep->rxlen, len - 18));
         morep->rxlen = len - 18;
     }
@@ -397,4 +406,4 @@ int MOREP_Close (int index)
     return 0;
 }
 
-/* vi:set tabstop=4 expandtab shiftwidth=4: this line set vi mode*/
+/* vim: set ts=4 sw=4 et: */