Skip to content

Commit

Permalink
Try to fix printing formats for all platforms.
Browse files Browse the repository at this point in the history
  • Loading branch information
rben-dev committed Jun 5, 2023
1 parent 8afcf5b commit 83e61b6
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 21 deletions.
7 changes: 7 additions & 0 deletions src/external_deps/print.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
*/
#ifndef __PRINT_H__
#define __PRINT_H__

/* Explicitly include the inttypes here */
#ifdef WITH_STDLIB
/* We include inttypes.h for the PRI* macros */
#include <inttypes.h>
#endif

#if (__GNUC__ * 10 + __GNUC_MINOR__ >= 42)
#define LIBECC_FORMAT_FUNCTION(F,A) __attribute__((format(__printf__, F, A)))
#else
Expand Down
12 changes: 6 additions & 6 deletions src/tests/ec_self_tests_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ ATTRIBUTE_WARN_UNUSED_RET static int ec_import_export_test(const ec_test_case *c
ret = ec_verify_batch(signatures, signatures_len, pubkeys, messages, messages_len,
num_batch, c->sig_type, c->hash_type, adata, adata_len, NULL, NULL);
if(ret){
ext_printf("Error when verifying signature ec_verify_batch no memory for batch size %d\n", num_batch);
ext_printf("Error when verifying signature ec_verify_batch no memory for batch size %" PRIu32 "\n", num_batch);
goto err;
}
{
Expand All @@ -602,7 +602,7 @@ ATTRIBUTE_WARN_UNUSED_RET static int ec_import_export_test(const ec_test_case *c
ret = ec_verify_batch(signatures, signatures_len, pubkeys, messages, messages_len,
num_batch, c->sig_type, c->hash_type, adata, adata_len, NULL, &scratch_pad_area_len);
if(ret){
ext_printf("Error when getting scratch_pad_area length for ec_verify_batch optimized for batch size %d\n", num_batch);
ext_printf("Error when getting scratch_pad_area length for ec_verify_batch optimized for batch size %" PRIu32 "\n", num_batch);
goto err;
}
MUST_HAVE((scratch_pad_area_len <= sizeof(scratch_pad_area)), ret, err);
Expand All @@ -611,7 +611,7 @@ ATTRIBUTE_WARN_UNUSED_RET static int ec_import_export_test(const ec_test_case *c
ret = ec_verify_batch(signatures, signatures_len, pubkeys, messages, messages_len,
num_batch, c->sig_type, c->hash_type, adata, adata_len, scratch_pad_area, &scratch_pad_area_len);
if(ret){
ext_printf("Error when verifying signature ec_verify_batch optimized for batch size %d\n", num_batch);
ext_printf("Error when verifying signature ec_verify_batch optimized for batch size %" PRIu32 "\n", num_batch);
goto err;
}
}
Expand Down Expand Up @@ -1885,14 +1885,14 @@ ATTRIBUTE_WARN_UNUSED_RET static int perf_test_one(const ec_sig_mapping *sig, co
ret = ec_performance_test(&t, &n_perf_sign, &n_perf_verif, &n_perf_batch_verif, &batch_verify_ok);
OPENMP_LOCK();
if(batch_verify_ok == 1){
ext_printf("[%s] %30s perf: %d sign/s and %d verif/s, %d batch verif/s (for %d batch)\n",
ret ? "-" : "+", t.name, n_perf_sign, n_perf_verif, n_perf_batch_verif, PERF_BATCH_VERIFICATION);
ext_printf("[%s] %30s perf: %u sign/s and %u verif/s, %u batch verif/s (for %u batch)\n",
ret ? "-" : "+", t.name, n_perf_sign, n_perf_verif, n_perf_batch_verif, (unsigned int)PERF_BATCH_VERIFICATION);
if ((n_perf_sign == 0) || (n_perf_verif == 0) || (n_perf_batch_verif == 0)) {
ext_printf("\t(0 is less than one sig/verif per sec)\n");
}
}
else{
ext_printf("[%s] %30s perf: %d sign/s and %d verif/s\n",
ext_printf("[%s] %30s perf: %u sign/s and %u verif/s\n",
ret ? "-" : "+", t.name, n_perf_sign, n_perf_verif);
if ((n_perf_sign == 0) || (n_perf_verif == 0)) {
ext_printf("\t(0 is less than one sig/verif per sec)\n");
Expand Down
10 changes: 5 additions & 5 deletions src/tests/ec_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ ATTRIBUTE_WARN_UNUSED_RET static int dump_hdr_info(const metadata_hdr * hdr)

/* Dump the header */
printf("Metadata header info:\n");
printf(" magic = 0x%08" PRIx32 "\n", (unsigned long)hdr->magic);
printf(" magic = 0x%08" PRIx32 "\n", hdr->magic);
switch (hdr->type) {
case IMAGE_TYPE0:
printf(" type = IMAGE_TYPE0\n");
Expand All @@ -991,9 +991,9 @@ ATTRIBUTE_WARN_UNUSED_RET static int dump_hdr_info(const metadata_hdr * hdr)
printf(" type %" PRIu32 " unknown!\n", hdr->type);
break;
}
printf(" version = 0x%08" PRIx32 "\n", (unsigned long)hdr->version);
printf(" len = 0x%08" PRIx32 "\n", (unsigned long)hdr->len);
printf(" siglen = 0x%08" PRIx32 "\n", (unsigned long)hdr->siglen);
printf(" version = 0x%08" PRIx32 "\n", hdr->version);
printf(" len = 0x%08" PRIx32 "\n", hdr->len);
printf(" siglen = 0x%08" PRIx32 "\n", hdr->siglen);
ret = 0;

err:
Expand Down Expand Up @@ -1117,7 +1117,7 @@ ATTRIBUTE_WARN_UNUSED_RET static int verify_bin_file(const char *ec_name, const
if (hdr.magic != HDR_MAGIC) {
ret = -1;
printf("Error: got magic 0x%08" PRIx32 " instead of 0x%08x "
"from metadata header\n", (unsigned long)hdr.magic, (unsigned int)HDR_MAGIC);
"from metadata header\n", hdr.magic, (unsigned int)HDR_MAGIC);
goto err;
}

Expand Down
2 changes: 2 additions & 0 deletions src/words/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ typedef unsigned long long uint64_t;
*/
#ifdef __SIZEOF_INT__
#if(__SIZEOF_INT__ == 2)
#define UINT32_IS_LONG /* This will be useful for print formatting */
typedef unsigned long uint32_t;
#else
typedef unsigned int uint32_t;
Expand All @@ -121,6 +122,7 @@ typedef unsigned int uint32_t;
/* The user has provided WORDSIZE=16, so we guess that
* we have LP32 or ILP32: a long type would be 32-bit.
*/
#define UINT32_IS_LONG /* This will be useful for print formatting */
typedef unsigned long uint32_t;
#else
/* Wild guess for uint32_t mapping */
Expand Down
13 changes: 11 additions & 2 deletions src/words/words_16.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,21 @@ typedef uint8_t hword_t;
#define WORD_MAX UINT16_MAX
#define HWORD_MAX UINT8_MAX

/* Prefix for the specific case of u32 that can be either
* unsigned int or unsigned long
*/
#ifdef UINT32_IS_LONG
#define PRI32_PREFIX "l"
#else
#define PRI32_PREFIX
#endif

/* PRINTF_WORD_HEX_FMT: printf hex format string for word */
#ifndef PRIx16
#define PRIx16 "hx"
#endif
#ifndef PRIx32
#define PRIx32 "x"
#define PRIx32 PRI32_PREFIX "x"
#endif
#ifndef PRIx64
#define PRIx64 "llx"
Expand All @@ -55,7 +64,7 @@ typedef uint8_t hword_t;
#define PRIu16 "hu"
#endif
#ifndef PRIu32
#define PRIu32 "u"
#define PRIu32 PRI32_PREFIX "u"
#endif
#ifndef PRIu64
#define PRIu64 "llu"
Expand Down
13 changes: 11 additions & 2 deletions src/words/words_32.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,21 @@ typedef uint16_t hword_t;
#define WORD_MAX UINT32_MAX
#define HWORD_MAX UINT16_MAX

/* Prefix for the specific case of u32 that can be either
* unsigned int or unsigned long
*/
#ifdef UINT32_IS_LONG
#define PRI32_PREFIX "l"
#else
#define PRI32_PREFIX
#endif

/* PRINTF_WORD_HEX_FMT: printf hex format string for word */
#ifndef PRIx16
#define PRIx16 "hx"
#endif
#ifndef PRIx32
#define PRIx32 "x"
#define PRIx32 PRI32_PREFIX "x"
#endif
#ifndef PRIx64
#define PRIx64 "llx"
Expand All @@ -55,7 +64,7 @@ typedef uint16_t hword_t;
#define PRIu16 "hu"
#endif
#ifndef PRIu32
#define PRIu32 "u"
#define PRIu32 PRI32_PREFIX "u"
#endif
#ifndef PRIu64
#define PRIu64 "llu"
Expand Down
18 changes: 12 additions & 6 deletions src/words/words_64.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,24 @@ typedef uint32_t hword_t;
#define WORD_MAX UINT64_MAX
#define HWORD_MAX UINT32_MAX

/* Prefix for the specific case of u32 that can be either
* unsigned int or unsigned long
*/
#ifdef UINT32_IS_LONG
#define PRI32_PREFIX "l"
#else
#define PRI32_PREFIX
#endif

/* PRINTF_WORD_HEX_FMT: printf hex format string for word */
#ifndef PRIx16
#define PRIx16 "hx"
#endif
#ifndef PRIx32
#define PRIx32 "lx"
#define PRIx32 PRI32_PREFIX "x"
#endif
#ifndef PRIx64
#ifndef __PRI_64_LENGTH_MODIFIER__
#define __PRI_64_LENGTH_MODIFIER__ "ll"
#endif
#define PRIx64 __PRI_64_LENGTH_MODIFIER__ "x"
#define PRIx64 "llx"
#endif

#define PRINTF_WORD_HEX_FMT "%016" PRIx64
Expand All @@ -59,7 +65,7 @@ typedef uint32_t hword_t;
#define PRIu16 "hu"
#endif
#ifndef PRIu32
#define PRIu32 "u"
#define PRIu32 PRI32_PREFIX "u"
#endif
#ifndef PRIu64
#define PRIu64 "llu"
Expand Down

0 comments on commit 83e61b6

Please sign in to comment.