Skip to content

Commit

Permalink
Print size_t variables on properly on Windows
Browse files Browse the repository at this point in the history
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).

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 <[email protected]>
  • Loading branch information
maciejsszmigiero committed Aug 14, 2016
1 parent bb8d5d6 commit 97bddec
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 21 deletions.
12 changes: 7 additions & 5 deletions src/libopensc/card-piv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1671,8 +1671,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;
}
Expand Down Expand Up @@ -1797,8 +1798,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;
}
Expand Down Expand Up @@ -1996,7 +1998,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;
}
Expand Down
6 changes: 6 additions & 0 deletions src/libopensc/opensc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions src/tools/cardos-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -999,7 +1001,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;
}
Expand Down
7 changes: 1 addition & 6 deletions src/tools/gids-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,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 ) {
Expand Down
3 changes: 2 additions & 1 deletion src/tools/openpgp-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
9 changes: 5 additions & 4 deletions src/tools/westcos-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -655,8 +655,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;
Expand All @@ -671,7 +672,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);
Expand All @@ -688,7 +689,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);
Expand Down

0 comments on commit 97bddec

Please sign in to comment.