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 <mail@maciej.szmigiero.name>
This commit is contained in:
Maciej S. Szmigiero 2016-09-28 19:35:13 +02:00 committed by Frank Morgner
parent 1168ca00f3
commit b646a306dc
6 changed files with 29 additions and 21 deletions

View File

@ -1676,8 +1676,9 @@ static int piv_general_mutual_authenticate(sc_card_t *card,
plain_text_len += tmplen; plain_text_len += tmplen;
if (plain_text_len != witness_len) { if (plain_text_len != witness_len) {
sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Encrypted and decrypted lengths do not match: %zu:%zu\n", sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE,
witness_len, plain_text_len); "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; r = SC_ERROR_INTERNAL;
goto err; goto err;
} }
@ -1801,8 +1802,9 @@ static int piv_general_mutual_authenticate(sc_card_t *card,
decrypted_reponse_len += tmplen; decrypted_reponse_len += tmplen;
if (decrypted_reponse_len != nonce_len || memcmp(nonce, decrypted_reponse, nonce_len) != 0) { 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", sc_log(card->ctx,
decrypted_reponse_len, nonce_len); "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; r = SC_ERROR_DECRYPT_FAILED;
goto err; goto err;
} }
@ -2005,7 +2007,7 @@ static int piv_general_external_authenticate(sc_card_t *card,
/* Sanity check the lengths again */ /* Sanity check the lengths again */
if(output_len != (size_t)tmplen) { if(output_len != (size_t)tmplen) {
sc_debug(card->ctx, SC_LOG_DEBUG_VERBOSE, "Allocated and computed lengths do not match! " 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; r = SC_ERROR_INTERNAL;
goto err; goto err;
} }

View File

@ -44,6 +44,12 @@ extern "C" {
#include "libopensc/sm.h" #include "libopensc/sm.h"
#endif #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_DECIPHER 0x0001
#define SC_SEC_OPERATION_SIGN 0x0002 #define SC_SEC_OPERATION_SIGN 0x0002

View File

@ -396,8 +396,8 @@ static int cardos_sm4h(const unsigned char *in, size_t inlen, unsigned char
unsigned int i,j; unsigned int i,j;
if (keylen != 16) { if (keylen != 16) {
printf("key has wrong size, need 16 bytes, got %zd. aborting.\n", printf("key has wrong size, need 16 bytes, got %"SC_FORMAT_LEN_SIZE_T"d. aborting.\n",
keylen); keylen);
return 0; return 0;
} }
@ -593,7 +593,8 @@ static int cardos_format(const char *opt_startkey)
if (check_apdu(&apdu)) if (check_apdu(&apdu))
return 1; return 1;
if (apdu.resplen < 0x04) { 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"); printf("aborting\n");
return 1; return 1;
} }
@ -914,7 +915,8 @@ static int cardos_change_startkey(const char *change_startkey_apdu)
if (check_apdu(&apdu)) if (check_apdu(&apdu))
return 1; return 1;
if (apdu.resplen < 0x04) { 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"); printf("aborting\n");
return 1; return 1;
} }
@ -999,7 +1001,8 @@ change_startkey:
if (check_apdu(&apdu)) if (check_apdu(&apdu))
return 1; return 1;
if (apdu.resplen < 0x04) { 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"); printf("aborting\n");
return 1; return 1;
} }

View File

@ -385,12 +385,7 @@ static int print_info(sc_card_t *card) {
if (r < 0) { if (r < 0) {
printf(" unable to read the file: %s\n", sc_strerror(r)); printf(" unable to read the file: %s\n", sc_strerror(r));
} else { } else {
#ifdef _WIN32 printf(" Size: %"SC_FORMAT_LEN_SIZE_T"u\n", size);
// visual studio doesn't support %zu
printf(" Size: %Iu\n", size);
#else
printf(" Size: %zu\n", size);
#endif
} }
printf("\n"); printf("\n");
if (strcmp(records[i].directory, "mscp") == 0 && strcmp(records[i].filename, "cmapfile") == 0 ) { if (strcmp(records[i].directory, "mscp") == 0 && strcmp(records[i].filename, "cmapfile") == 0 ) {

View File

@ -365,7 +365,8 @@ static int do_userinfo(sc_card_t *card)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (r != (signed)count) { 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; return EXIT_FAILURE;
} }

View File

@ -656,8 +656,9 @@ int main(int argc, char *argv[])
file->path = path; file->path = path;
printf("File key creation %s, size %zd.\n", file->path.value, printf("File key creation %s, size %"SC_FORMAT_LEN_SIZE_T"d.\n",
file->size); file->path.value,
file->size);
r = sc_create_file(card, file); r = sc_create_file(card, file);
if(r) goto out; 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"); printf("Write private key.\n");
r = sc_update_binary(card,0,pdata,lg,0); 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); r = sc_pkcs15_encode_pubkey(ctx, &key, &pdata, &lg);
if(r) goto out; 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); sc_format_path("3F000002", &path);
r = sc_select_file(card, &path, NULL); r = sc_select_file(card, &path, NULL);