From b646a306dc578de2b3911006b793b9ebde5deb91 Mon Sep 17 00:00:00 2001 From: "Maciej S. Szmigiero" Date: Wed, 28 Sep 2016 19:35:13 +0200 Subject: [PATCH] Print size_t variables on properly on Windows OpenSC used SUSv3 "z" printf length modifier for printing size_t variables, however this modifier is not available on Windows ("I" must be used instead), at least for now. Introduce SC_FORMAT_LEN_SIZE_T define for that purpose and convert existing code to use it when printing size_t variables. This define can't go into libopensc/internal.h since tools use it, too. Signed-off-by: Maciej S. Szmigiero --- src/libopensc/card-piv.c | 12 +++++++----- src/libopensc/opensc.h | 6 ++++++ src/tools/cardos-tool.c | 13 ++++++++----- src/tools/gids-tool.c | 7 +------ src/tools/openpgp-tool.c | 3 ++- src/tools/westcos-tool.c | 9 +++++---- 6 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/libopensc/card-piv.c b/src/libopensc/card-piv.c index bf1e7375..6b69aebb 100644 --- a/src/libopensc/card-piv.c +++ b/src/libopensc/card-piv.c @@ -1676,8 +1676,9 @@ static int piv_general_mutual_authenticate(sc_card_t *card, plain_text_len += tmplen; if (plain_text_len != witness_len) { - sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Encrypted and decrypted lengths do not match: %zu:%zu\n", - witness_len, plain_text_len); + sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, + "Encrypted and decrypted lengths do not match: %"SC_FORMAT_LEN_SIZE_T"u:%"SC_FORMAT_LEN_SIZE_T"u\n", + witness_len, plain_text_len); r = SC_ERROR_INTERNAL; goto err; } @@ -1801,8 +1802,9 @@ static int piv_general_mutual_authenticate(sc_card_t *card, decrypted_reponse_len += tmplen; if (decrypted_reponse_len != nonce_len || memcmp(nonce, decrypted_reponse, nonce_len) != 0) { - sc_log(card->ctx, "mutual authentication failed, card returned wrong value %zu:%zu", - decrypted_reponse_len, nonce_len); + sc_log(card->ctx, + "mutual authentication failed, card returned wrong value %"SC_FORMAT_LEN_SIZE_T"u:%"SC_FORMAT_LEN_SIZE_T"u", + decrypted_reponse_len, nonce_len); r = SC_ERROR_DECRYPT_FAILED; goto err; } @@ -2005,7 +2007,7 @@ static int piv_general_external_authenticate(sc_card_t *card, /* Sanity check the lengths again */ if(output_len != (size_t)tmplen) { sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Allocated and computed lengths do not match! " - "Expected %zd, found: %d\n", output_len, tmplen); + "Expected %"SC_FORMAT_LEN_SIZE_T"d, found: %d\n", output_len, tmplen); r = SC_ERROR_INTERNAL; goto err; } diff --git a/src/libopensc/opensc.h b/src/libopensc/opensc.h index 01cbbc36..d7accfc4 100644 --- a/src/libopensc/opensc.h +++ b/src/libopensc/opensc.h @@ -44,6 +44,12 @@ extern "C" { #include "libopensc/sm.h" #endif +#if defined(_WIN32) +#define SC_FORMAT_LEN_SIZE_T "I" +#else +/* hope SUSv3 one works */ +#define SC_FORMAT_LEN_SIZE_T "z" +#endif #define SC_SEC_OPERATION_DECIPHER 0x0001 #define SC_SEC_OPERATION_SIGN 0x0002 diff --git a/src/tools/cardos-tool.c b/src/tools/cardos-tool.c index e271fed6..c3c6406b 100644 --- a/src/tools/cardos-tool.c +++ b/src/tools/cardos-tool.c @@ -396,8 +396,8 @@ static int cardos_sm4h(const unsigned char *in, size_t inlen, unsigned char unsigned int i,j; if (keylen != 16) { - printf("key has wrong size, need 16 bytes, got %zd. aborting.\n", - keylen); + printf("key has wrong size, need 16 bytes, got %"SC_FORMAT_LEN_SIZE_T"d. aborting.\n", + keylen); return 0; } @@ -593,7 +593,8 @@ static int cardos_format(const char *opt_startkey) if (check_apdu(&apdu)) return 1; if (apdu.resplen < 0x04) { - printf("expected 4-6 bytes form GET DATA for startkey data, but got only %zu\n", apdu.resplen); + printf("expected 4-6 bytes form GET DATA for startkey data, but got only %"SC_FORMAT_LEN_SIZE_T"u\n", + apdu.resplen); printf("aborting\n"); return 1; } @@ -914,7 +915,8 @@ static int cardos_change_startkey(const char *change_startkey_apdu) if (check_apdu(&apdu)) return 1; if (apdu.resplen < 0x04) { - printf("expected 4-6 bytes form GET DATA for startkey data, but got only %zu\n", apdu.resplen); + printf("expected 4-6 bytes form GET DATA for startkey data, but got only %"SC_FORMAT_LEN_SIZE_T"u\n", + apdu.resplen); printf("aborting\n"); return 1; } @@ -999,7 +1001,8 @@ change_startkey: if (check_apdu(&apdu)) return 1; if (apdu.resplen < 0x04) { - printf("expected 4-6 bytes form GET DATA for startkey data, but got only %zu\n", apdu.resplen); + printf("expected 4-6 bytes form GET DATA for startkey data, but got only %"SC_FORMAT_LEN_SIZE_T"u\n", + apdu.resplen); printf("aborting\n"); return 1; } diff --git a/src/tools/gids-tool.c b/src/tools/gids-tool.c index 029c53ff..8cd87f93 100644 --- a/src/tools/gids-tool.c +++ b/src/tools/gids-tool.c @@ -385,12 +385,7 @@ static int print_info(sc_card_t *card) { if (r < 0) { printf(" unable to read the file: %s\n", sc_strerror(r)); } else { -#ifdef _WIN32 - // visual studio doesn't support %zu - printf(" Size: %Iu\n", size); -#else - printf(" Size: %zu\n", size); -#endif + printf(" Size: %"SC_FORMAT_LEN_SIZE_T"u\n", size); } printf("\n"); if (strcmp(records[i].directory, "mscp") == 0 && strcmp(records[i].filename, "cmapfile") == 0 ) { diff --git a/src/tools/openpgp-tool.c b/src/tools/openpgp-tool.c index cb725dba..920a2dce 100644 --- a/src/tools/openpgp-tool.c +++ b/src/tools/openpgp-tool.c @@ -365,7 +365,8 @@ static int do_userinfo(sc_card_t *card) return EXIT_FAILURE; } if (r != (signed)count) { - fprintf(stderr, "%s: expecting %zd, got only %d bytes\n", openpgp_data[i].ef, count, r); + fprintf(stderr, "%s: expecting %"SC_FORMAT_LEN_SIZE_T"d, got only %d bytes\n", + openpgp_data[i].ef, count, r); return EXIT_FAILURE; } diff --git a/src/tools/westcos-tool.c b/src/tools/westcos-tool.c index 29b75dda..8885e9bd 100644 --- a/src/tools/westcos-tool.c +++ b/src/tools/westcos-tool.c @@ -656,8 +656,9 @@ int main(int argc, char *argv[]) file->path = path; - printf("File key creation %s, size %zd.\n", file->path.value, - file->size); + printf("File key creation %s, size %"SC_FORMAT_LEN_SIZE_T"d.\n", + file->path.value, + file->size); r = sc_create_file(card, file); if(r) goto out; @@ -672,7 +673,7 @@ int main(int argc, char *argv[]) } } - printf("Private key length is %zd\n", lg); + printf("Private key length is %"SC_FORMAT_LEN_SIZE_T"d\n", lg); printf("Write private key.\n"); r = sc_update_binary(card,0,pdata,lg,0); @@ -696,7 +697,7 @@ int main(int argc, char *argv[]) r = sc_pkcs15_encode_pubkey(ctx, &key, &pdata, &lg); if(r) goto out; - printf("Public key length %zd\n", lg); + printf("Public key length %"SC_FORMAT_LEN_SIZE_T"d\n", lg); sc_format_path("3F000002", &path); r = sc_select_file(card, &path, NULL);