Stop using MBEDTLS_PRINTF_SIZET

Since Mbed TLS 3.6.0, all officially supported versions of Visual Studio
a printf function family that is sufficiently compliant to C99 for our
purposes, in particular supporting `%zu` for `size_t`. The only platform
without `%zu` that we semi-officially support is older versions of MinGW,
still used in our CI. MinGW provides either a Windows legacy printf or a
standards-compliant printf depending on the value of
`__USE_MINGW_ANSI_STDIO` when compiling each C file. Force the use of the
compliant version. Don't rely on `MBEDTLS_PRINTF_SIZET`, which is defined in
`<mbedtls/debug.h>` and no longer considers the Windows legacy version in
Mbed TLS >= 4.1.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
diff --git a/psasim/src/aut_psa_key_agreement.c b/psasim/src/aut_psa_key_agreement.c
index 4a0aab1..4ab1bf4 100644
--- a/psasim/src/aut_psa_key_agreement.c
+++ b/psasim/src/aut_psa_key_agreement.c
@@ -69,7 +69,7 @@
         return EXIT_FAILURE;
     }
 
-    mbedtls_printf("Client Public Key (%" MBEDTLS_PRINTF_SIZET " bytes):\n", client_pk_len);
+    mbedtls_printf("Client Public Key (%zu bytes):\n", client_pk_len);
 
     for (size_t j = 0; j < client_pk_len; j++) {
         if (j % 8 == 0) {
@@ -108,7 +108,7 @@
         return EXIT_FAILURE;
     }
 
-    mbedtls_printf("Server Public Key (%" MBEDTLS_PRINTF_SIZET " bytes):\n", sizeof(server_pk));
+    mbedtls_printf("Server Public Key (%zu bytes):\n", sizeof(server_pk));
 
     for (size_t j = 0; j < sizeof(server_pk); j++) {
         if (j % 8 == 0) {
@@ -129,7 +129,7 @@
         return EXIT_FAILURE;
     }
 
-    mbedtls_printf("Derived Key (%" MBEDTLS_PRINTF_SIZET " bytes):\n", derived_key_len);
+    mbedtls_printf("Derived Key (%zu bytes):\n", derived_key_len);
 
     for (size_t j = 0; j < derived_key_len; j++) {
         if (j % 8 == 0) {
diff --git a/scripts/mbedtls_framework/c_wrapper_generator.py b/scripts/mbedtls_framework/c_wrapper_generator.py
index ea52d3a..bc77703 100644
--- a/scripts/mbedtls_framework/c_wrapper_generator.py
+++ b/scripts/mbedtls_framework/c_wrapper_generator.py
@@ -110,6 +110,17 @@
 
             ''')
 
+        if not header:
+            # On Mingw-w64, force the use of a C99-compliant printf() and friends.
+            # This is necessary on older versions of Mingw and/or Windows runtimes
+            # where snprintf does not always zero-terminate the buffer, and does
+            # not support formats such as "%zu" for size_t and "%lld" for long long.
+            prologue += strip_indentation(f'''
+                #if !defined(__USE_MINGW_ANSI_STDIO)
+                #define __USE_MINGW_ANSI_STDIO 1
+                #endif
+            ''')
+
         for include in self._INCLUDES:
             prologue += "#include {}\n".format(include)
 
@@ -403,7 +414,6 @@
 #if defined(MBEDTLS_FS_IO) && defined(MBEDTLS_TEST_HOOKS)
 #include <stdio.h>
 #include <inttypes.h>
-#include <mbedtls/debug.h> // for MBEDTLS_PRINTF_SIZET
 #include <mbedtls/platform.h> // for mbedtls_fprintf
 #endif /* defined(MBEDTLS_FS_IO) && defined(MBEDTLS_TEST_HOOKS) */
 """)
@@ -412,7 +422,7 @@
         'int': '%d',
         'long': '%ld',
         'long long': '%lld',
-        'size_t': '%"MBEDTLS_PRINTF_SIZET"',
+        'size_t': '%zu',
         'unsigned': '0x%08x',
         'unsigned int': '0x%08x',
         'unsigned long': '0x%08lx',
diff --git a/tests/programs/metatest.c b/tests/programs/metatest.c
index b862889..4345061 100644
--- a/tests/programs/metatest.c
+++ b/tests/programs/metatest.c
@@ -26,6 +26,14 @@
  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  */
 
+/* On Mingw-w64, force the use of a C99-compliant printf() and friends.
+ * This is necessary on older versions of Mingw and/or Windows runtimes
+ * where snprintf does not always zero-terminate the buffer, and does
+ * not support formats such as "%zu" for size_t and "%lld" for long long.
+ */
+#if !defined(__USE_MINGW_ANSI_STDIO)
+#define __USE_MINGW_ANSI_STDIO 1
+#endif
 
 #include <mbedtls/debug.h>
 #include <mbedtls/platform.h>
@@ -200,10 +208,7 @@
     size_t start = 0, offset = 0, count = 0;
     char direction = 'r';
     if (sscanf(name,
-               "%*[^0-9]%" MBEDTLS_PRINTF_SIZET
-               "%*[^0-9]%" MBEDTLS_PRINTF_SIZET
-               "%*[^0-9]%" MBEDTLS_PRINTF_SIZET
-               "_%c",
+               "%*[^0-9]%zu%*[^0-9]%zu%*[^0-9]%zu_%c",
                &start, &offset, &count, &direction) != 4) {
         mbedtls_fprintf(stderr, "%s: Bad name format: %s\n", __func__, name);
         return;
@@ -217,22 +222,19 @@
 
     if (start > sizeof(aligned.buf)) {
         mbedtls_fprintf(stderr,
-                        "%s: start=%" MBEDTLS_PRINTF_SIZET
-                        " > size=%" MBEDTLS_PRINTF_SIZET,
+                        "%s: start=%zu > size=%zu",
                         __func__, start, sizeof(aligned.buf));
         return;
     }
     if (start + count > sizeof(aligned.buf)) {
         mbedtls_fprintf(stderr,
-                        "%s: start+count=%" MBEDTLS_PRINTF_SIZET
-                        " > size=%" MBEDTLS_PRINTF_SIZET,
+                        "%s: start+count=%zu > size=%zu",
                         __func__, start + count, sizeof(aligned.buf));
         return;
     }
     if (offset >= count) {
         mbedtls_fprintf(stderr,
-                        "%s: offset=%" MBEDTLS_PRINTF_SIZET
-                        " >= count=%" MBEDTLS_PRINTF_SIZET,
+                        "%s: offset=%zu >= count=%zu",
                         __func__, offset, count);
         return;
     }