From 1de1832562b843da3f9afce04c6bd0ae08512906 Mon Sep 17 00:00:00 2001 From: Ludovic Rousseau Date: Sun, 3 Mar 2013 18:48:58 +0100 Subject: [PATCH 01/43] Add lintian options More options for more warnings or mode details. --- HACKING | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/HACKING b/HACKING index 4a02615..b6b8968 100644 --- a/HACKING +++ b/HACKING @@ -37,7 +37,9 @@ Here are some directions to get you started: $ make clean $ scan-build make 2.3 When Debianizing - $ lintian *deb + $ lintian --info --display-info --display-experimental *deb + or (shorter version) + $ lintian -iIE *deb 3. Preserve cross-platform compatility The source code should remain compilable across various platforms, From 232930c3d52635a7c1af33871b801aa3edbaed50 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Mon, 4 Mar 2013 01:06:10 +0100 Subject: [PATCH 02/43] Fix resource leaks detected by cppcheck [examples/pn53x-tamashell.c:94]: (error) Resource leak: input [utils/nfc-mfultralight.c:264]: (error) Resource leak: pfDump --- examples/pn53x-tamashell.c | 6 ++++++ utils/nfc-mfultralight.c | 1 + 2 files changed, 7 insertions(+) diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index f517610..b161347 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -91,12 +91,18 @@ int main(int argc, const char *argv[]) if (pnd == NULL) { ERR("%s", "Unable to open NFC device."); + if (input != NULL) { + fclose(input); + } return EXIT_FAILURE; } printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); + if (input != NULL) { + fclose(input); + } exit(EXIT_FAILURE); } diff --git a/utils/nfc-mfultralight.c b/utils/nfc-mfultralight.c index 0f3cd4a..1113a5a 100644 --- a/utils/nfc-mfultralight.c +++ b/utils/nfc-mfultralight.c @@ -261,6 +261,7 @@ main(int argc, const char *argv[]) } if (fwrite(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) { printf("Could not write to file: %s\n", argv[2]); + fclose(pfDump); return EXIT_FAILURE; } fclose(pfDump); From bece73faaf62d10b66023eb446c4beac1c520f92 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Tue, 5 Mar 2013 19:44:59 +0100 Subject: [PATCH 03/43] Error conditions in utils & examples: fix leaks, unify style (see details) * in main(): ** errx()/err()/return -> exit() ** return values -> EXIT_SUCCESS & EXIT_FAILURE * out of main: ** err()/errx()/exit() -> return ** change retval from size_t to int to allow returning errors ** don't use EXIT_SUCCESS / EXIT_FAILURE as retvals * add nfc_close() & nfc_exit() to exit() on errors * add missing fclose() on errors * add missing test if (pnd == NULL) * unify style if (pnd == / != NULL) * remove goto's * few related fixes * remove if(pnd!=NULL) test on nfc_close() calls --- examples/doc/quick_start_example1.c | 4 +- examples/doc/quick_start_example2.c | 4 +- examples/nfc-anticol.c | 15 +- examples/nfc-dep-initiator.c | 28 ++-- examples/nfc-dep-target.c | 35 +++-- examples/nfc-emulate-forum-tag2.c | 16 +- examples/nfc-emulate-tag.c | 13 +- examples/nfc-emulate-uid.c | 16 +- examples/nfc-mfsetuid.c | 16 +- examples/nfc-poll.c | 5 +- examples/nfc-relay.c | 35 ++++- examples/pn53x-diagnose.c | 11 +- examples/pn53x-sam.c | 46 ++++-- examples/pn53x-tamashell.c | 9 +- utils/nfc-emulate-forum-tag4.c | 58 ++++--- utils/nfc-list.c | 3 +- utils/nfc-mfclassic.c | 236 ++++++++++++++-------------- utils/nfc-mfultralight.c | 27 ++-- utils/nfc-read-forum-tag3.c | 69 +++++--- utils/nfc-relay-picc.c | 72 +++++---- utils/nfc-scan-device.c | 13 +- 21 files changed, 433 insertions(+), 298 deletions(-) diff --git a/examples/doc/quick_start_example1.c b/examples/doc/quick_start_example1.c index 77a8021..7d26a3d 100644 --- a/examples/doc/quick_start_example1.c +++ b/examples/doc/quick_start_example1.c @@ -50,7 +50,7 @@ main(int argc, const char *argv[]) if (pnd == NULL) { warnx("ERROR: %s", "Unable to open NFC device."); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } // Set opened NFC device to initiator mode if (nfc_initiator_init(pnd) < 0) { @@ -82,5 +82,5 @@ main(int argc, const char *argv[]) nfc_close(pnd); // Release the context nfc_exit(context); - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } diff --git a/examples/doc/quick_start_example2.c b/examples/doc/quick_start_example2.c index d98d62e..0532ca9 100644 --- a/examples/doc/quick_start_example2.c +++ b/examples/doc/quick_start_example2.c @@ -42,7 +42,7 @@ main(int argc, const char *argv[]) if (pnd == NULL) { ERR("%s", "Unable to open NFC device."); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } // Set opened NFC device to initiator mode if (nfc_initiator_init(pnd) < 0) { @@ -74,5 +74,5 @@ main(int argc, const char *argv[]) nfc_close(pnd); // Release the context nfc_exit(context); - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index f517230..217b267 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -155,30 +155,39 @@ main(int argc, char *argv[]) // Try to open the NFC reader pnd = nfc_open(context, NULL); - if (!pnd) { + if (pnd == NULL) { printf("Error opening NFC reader\n"); + nfc_exit(context); exit(EXIT_FAILURE); } // Initialise NFC device as "initiator" if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); + nfc_close(pnd); + nfc_exit(context); exit(EXIT_FAILURE); } // Configure the CRC if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); + nfc_close(pnd); + nfc_exit(context); exit(EXIT_FAILURE); } // Use raw send/receive methods if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); + nfc_close(pnd); + nfc_exit(context); exit(EXIT_FAILURE); } // Disable 14443-4 autoswitching if (nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); + nfc_close(pnd); + nfc_exit(context); exit(EXIT_FAILURE); } @@ -189,7 +198,7 @@ main(int argc, char *argv[]) printf("Error: No tag available\n"); nfc_close(pnd); nfc_exit(context); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } memcpy(abtAtqa, abtRx, 2); @@ -319,5 +328,5 @@ main(int argc, char *argv[]) nfc_close(pnd); nfc_exit(context); - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index e6737bc..5d5da9e 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -53,7 +53,7 @@ static nfc_device *pnd; static void stop_dep_communication(int sig) { (void) sig; - if (pnd) + if (pnd != NULL) nfc_abort_command(pnd); else exit(EXIT_FAILURE); @@ -68,16 +68,17 @@ main(int argc, const char *argv[]) if (argc > 1) { printf("Usage: %s\n", argv[0]); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } nfc_context *context; nfc_init(&context); pnd = nfc_open(context, NULL); - if (!pnd) { + if (pnd == NULL) { printf("Unable to open NFC device.\n"); - return EXIT_FAILURE; + nfc_exit(context); + exit(EXIT_FAILURE); } printf("NFC device: %s\n opened", nfc_device_get_name(pnd)); @@ -85,12 +86,16 @@ main(int argc, const char *argv[]) if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); - goto error; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } if (nfc_initiator_select_dep_target(pnd, NDM_PASSIVE, NBR_212, NULL, &nt, 1000) < 0) { nfc_perror(pnd, "nfc_initiator_select_dep_target"); - goto error; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } print_nfc_target(nt, false); @@ -98,7 +103,9 @@ main(int argc, const char *argv[]) int res; if ((res = nfc_initiator_transceive_bytes(pnd, abtTx, sizeof(abtTx), abtRx, sizeof(abtRx), 0)) < 0) { nfc_perror(pnd, "nfc_initiator_transceive_bytes"); - goto error; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } abtRx[res] = 0; @@ -106,11 +113,12 @@ main(int argc, const char *argv[]) if (nfc_initiator_deselect_target(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_deselect_target"); - goto error; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } -error: nfc_close(pnd); nfc_exit(context); - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 7bbb996..0853809 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -52,7 +52,7 @@ static nfc_device *pnd; static void stop_dep_communication(int sig) { (void) sig; - if (pnd) + if (pnd != NULL) nfc_abort_command(pnd); else exit(EXIT_FAILURE); @@ -65,6 +65,11 @@ main(int argc, const char *argv[]) int szRx; uint8_t abtTx[] = "Hello Mars!"; + if (argc > 1) { + printf("Usage: %s\n", argv[0]); + exit(EXIT_FAILURE); + } + nfc_context *context; nfc_init(&context); #define MAX_DEVICE_COUNT 2 @@ -80,12 +85,8 @@ main(int argc, const char *argv[]) pnd = nfc_open(context, connstrings[1]); } else { printf("No device found.\n"); - return EXIT_FAILURE; - } - - if (argc > 1) { - printf("Usage: %s\n", argv[0]); - return EXIT_FAILURE; + nfc_exit(context); + exit(EXIT_FAILURE); } nfc_target nt = { @@ -109,9 +110,10 @@ main(int argc, const char *argv[]) }, }; - if (!pnd) { + if (pnd == NULL) { printf("Unable to open NFC device.\n"); - return EXIT_FAILURE; + nfc_exit(context); + exit(EXIT_FAILURE); } printf("NFC device: %s opened\n", nfc_device_get_name(pnd)); @@ -123,13 +125,17 @@ main(int argc, const char *argv[]) printf("Waiting for initiator request...\n"); if ((szRx = nfc_target_init(pnd, &nt, abtRx, sizeof(abtRx), 0)) < 0) { nfc_perror(pnd, "nfc_target_init"); - goto error; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } printf("Initiator request received. Waiting for data...\n"); if ((szRx = nfc_target_receive_bytes(pnd, abtRx, sizeof(abtRx), 0)) < 0) { nfc_perror(pnd, "nfc_target_receive_bytes"); - goto error; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } abtRx[(size_t) szRx] = '\0'; printf("Received: %s\n", abtRx); @@ -137,12 +143,13 @@ main(int argc, const char *argv[]) printf("Sending: %s\n", abtTx); if (nfc_target_send_bytes(pnd, abtTx, sizeof(abtTx), 0) < 0) { nfc_perror(pnd, "nfc_target_send_bytes"); - goto error; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } printf("Data sent.\n"); -error: nfc_close(pnd); nfc_exit(context); - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } diff --git a/examples/nfc-emulate-forum-tag2.c b/examples/nfc-emulate-forum-tag2.c index 8240a4e..e569d78 100644 --- a/examples/nfc-emulate-forum-tag2.c +++ b/examples/nfc-emulate-forum-tag2.c @@ -79,7 +79,7 @@ static void stop_emulation(int sig) { (void)sig; - if (pnd) { + if (pnd != NULL) { nfc_abort_command(pnd); } else { exit(EXIT_FAILURE); @@ -191,6 +191,7 @@ main(int argc, char *argv[]) if (pnd == NULL) { ERR("Unable to open NFC device"); + nfc_exit(context); exit(EXIT_FAILURE); } @@ -198,18 +199,13 @@ main(int argc, char *argv[]) printf("Emulating NDEF tag now, please touch it with a second NFC device\n"); if (nfc_emulate_target(pnd, &emulator, 0) < 0) { - goto error; + nfc_perror(pnd, argv[0]); + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } nfc_close(pnd); nfc_exit(context); - exit(EXIT_SUCCESS); - -error: - if (pnd) { - nfc_perror(pnd, argv[0]); - nfc_close(pnd); - nfc_exit(context); - } } diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index c7267b4..aa73e7e 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -66,9 +66,7 @@ intr_hdlr(int sig) { (void) sig; printf("\nQuitting...\n"); - if (pnd != NULL) { - nfc_close(pnd); - } + nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } @@ -184,15 +182,16 @@ main(int argc, char *argv[]) nfc_init(&context); - // Try to open the NFC reader - pnd = nfc_open(context, NULL); - // Display libnfc version acLibnfcVersion = nfc_version(); printf("%s uses libnfc %s\n", argv[0], acLibnfcVersion); + // Try to open the NFC reader + pnd = nfc_open(context, NULL); + if (pnd == NULL) { ERR("Unable to open NFC device"); + nfc_exit(context); exit(EXIT_FAILURE); } @@ -267,6 +266,8 @@ main(int argc, char *argv[]) printf("NFC device (configured as target) is now emulating the tag, please touch it with a second NFC device (initiator)\n"); if (!nfc_target_emulate_tag(pnd, &nt)) { nfc_perror(pnd, "nfc_target_emulate_tag"); + nfc_close(pnd); + nfc_exit(context); exit(EXIT_FAILURE); } diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 1698ecd..11d861f 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -134,6 +134,7 @@ main(int argc, char *argv[]) if (pnd == NULL) { printf("Unable to open NFC device\n"); + nfc_exit(context); exit(EXIT_FAILURE); } @@ -162,13 +163,17 @@ main(int argc, char *argv[]) if ((szRecvBits = nfc_target_init(pnd, &nt, abtRecv, sizeof(abtRecv), 0)) < 0) { nfc_perror(pnd, "nfc_target_init"); ERR("Could not come out of auto-emulation, no command was received"); - goto error; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } printf("[+] Received initiator command: "); print_hex_bits(abtRecv, (size_t) szRecvBits); printf("[+] Configuring communication\n"); if ((nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) || (nfc_device_set_property_bool(pnd, NP_HANDLE_PARITY, true) < 0)) { nfc_perror(pnd, "nfc_device_set_property_bool"); + nfc_close(pnd); + nfc_exit(context); exit(EXIT_FAILURE); } printf("[+] Done, the emulated tag is initialized with UID: %02X%02X%02X%02X\n\n", abtUidBcc[0], abtUidBcc[1], @@ -211,7 +216,9 @@ main(int argc, char *argv[]) // Send and print the command to the screen if (nfc_target_send_bits(pnd, pbtTx, szTxBits, NULL) < 0) { nfc_perror(pnd, "nfc_target_send_bits"); - goto error; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } if (!quiet_output) { printf("T: "); @@ -223,9 +230,4 @@ main(int argc, char *argv[]) nfc_close(pnd); nfc_exit(context); exit(EXIT_SUCCESS); - -error: - nfc_close(pnd); - nfc_exit(context); - exit(EXIT_FAILURE); } diff --git a/examples/nfc-mfsetuid.c b/examples/nfc-mfsetuid.c index 3943a8a..9311873 100644 --- a/examples/nfc-mfsetuid.c +++ b/examples/nfc-mfsetuid.c @@ -183,30 +183,39 @@ main(int argc, char *argv[]) // Try to open the NFC reader pnd = nfc_open(context, NULL); - if (!pnd) { + if (pnd == NULL) { printf("Error opening NFC reader\n"); + nfc_exit(context); exit(EXIT_FAILURE); } // Initialise NFC device as "initiator" if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); + nfc_close(pnd); + nfc_exit(context); exit(EXIT_FAILURE); } // Configure the CRC if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); + nfc_close(pnd); + nfc_exit(context); exit(EXIT_FAILURE); } // Use raw send/receive methods if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); + nfc_close(pnd); + nfc_exit(context); exit(EXIT_FAILURE); } // Disable 14443-4 autoswitching if (nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); + nfc_close(pnd); + nfc_exit(context); exit(EXIT_FAILURE); } @@ -217,7 +226,7 @@ main(int argc, char *argv[]) printf("Error: No tag available\n"); nfc_close(pnd); nfc_exit(context); - return 1; + exit(EXIT_FAILURE); } memcpy(abtAtqa, abtRx, 2); @@ -353,8 +362,7 @@ main(int argc, char *argv[]) } } - nfc_close(pnd); nfc_exit(context); - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index 1507833..0bb1e8f 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -56,7 +56,7 @@ static nfc_device *pnd = NULL; static void stop_polling(int sig) { (void) sig; - if (pnd) + if (pnd != NULL) nfc_abort_command(pnd); else exit(EXIT_FAILURE); @@ -110,11 +110,14 @@ main(int argc, const char *argv[]) if (pnd == NULL) { ERR("%s", "Unable to open NFC device."); + nfc_exit(context); exit(EXIT_FAILURE); } if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); + nfc_close(pnd); + nfc_exit(context); exit(EXIT_FAILURE); } diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 224352e..7b5048c 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -89,13 +89,13 @@ main(int argc, char *argv[]) for (arg = 1; arg < argc; arg++) { if (0 == strcmp(argv[arg], "-h")) { print_usage(argv); - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } else if (0 == strcmp(argv[arg], "-q")) { quiet_output = true; } else { ERR("%s is not supported option.", argv[arg]); print_usage(argv); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } } @@ -116,14 +116,16 @@ main(int argc, char *argv[]) if (szFound < 2) { ERR("%zd device found but two opened devices are needed to relay NFC.", szFound); - return EXIT_FAILURE; + nfc_exit(context); + exit(EXIT_FAILURE); } // Try to open the NFC emulator device pndTag = nfc_open(context, connstrings[0]); if (pndTag == NULL) { printf("Error opening NFC emulator device\n"); - return EXIT_FAILURE; + nfc_exit(context); + exit(EXIT_FAILURE); } printf("Hint: tag <---> initiator (relay) <---> target (relay) <---> original reader\n\n"); @@ -153,30 +155,44 @@ main(int argc, char *argv[]) ERR("%s", "Initialization of NFC emulator failed"); nfc_close(pndTag); nfc_exit(context); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } printf("%s", "Configuring emulator settings..."); if ((nfc_device_set_property_bool(pndTag, NP_HANDLE_CRC, false) < 0) || (nfc_device_set_property_bool(pndTag, NP_HANDLE_PARITY, false) < 0) || (nfc_device_set_property_bool(pndTag, NP_ACCEPT_INVALID_FRAMES, true)) < 0) { nfc_perror(pndTag, "nfc_device_set_property_bool"); + nfc_close(pndTag); + nfc_exit(context); exit(EXIT_FAILURE); } printf("%s", "Done, emulated tag is initialized"); // Try to open the NFC reader pndReader = nfc_open(context, connstrings[1]); + if (pndReader == NULL) { + printf("Error opening NFC reader device\n"); + nfc_close(pndTag); + nfc_exit(context); + exit(EXIT_FAILURE); + } printf("NFC reader device: %s opened", nfc_device_get_name(pndReader)); printf("%s", "Configuring NFC reader settings..."); if (nfc_initiator_init(pndReader) < 0) { nfc_perror(pndReader, "nfc_initiator_init"); + nfc_close(pndTag); + nfc_close(pndReader); + nfc_exit(context); exit(EXIT_FAILURE); } if ((nfc_device_set_property_bool(pndReader, NP_HANDLE_CRC, false) < 0) || (nfc_device_set_property_bool(pndReader, NP_HANDLE_PARITY, false) < 0) || (nfc_device_set_property_bool(pndReader, NP_ACCEPT_INVALID_FRAMES, true)) < 0) { nfc_perror(pndReader, "nfc_device_set_property_bool"); + nfc_close(pndTag); + nfc_close(pndReader); + nfc_exit(context); exit(EXIT_FAILURE); } printf("%s", "Done, relaying frames now!"); @@ -189,12 +205,18 @@ main(int argc, char *argv[]) // Drop down field for a very short time (original tag will reboot) if (nfc_device_set_property_bool(pndReader, NP_ACTIVATE_FIELD, false) < 0) { nfc_perror(pndReader, "nfc_device_set_property_bool"); + nfc_close(pndTag); + nfc_close(pndReader); + nfc_exit(context); exit(EXIT_FAILURE); } if (!quiet_output) printf("\n"); if (nfc_device_set_property_bool(pndReader, NP_ACTIVATE_FIELD, true) < 0) { nfc_perror(pndReader, "nfc_device_set_property_bool"); + nfc_close(pndTag); + nfc_close(pndReader); + nfc_exit(context); exit(EXIT_FAILURE); } } @@ -209,6 +231,9 @@ main(int argc, char *argv[]) // Redirect the answer back to the reader if (nfc_target_send_bits(pndTag, abtTagRx, szTagRxBits, abtTagRxPar) < 0) { nfc_perror(pndTag, "nfc_target_send_bits"); + nfc_close(pndTag); + nfc_close(pndReader); + nfc_exit(context); exit(EXIT_FAILURE); } // Print the tag frame to the screen diff --git a/examples/pn53x-diagnose.c b/examples/pn53x-diagnose.c index 1457977..97f0848 100644 --- a/examples/pn53x-diagnose.c +++ b/examples/pn53x-diagnose.c @@ -51,7 +51,7 @@ int main(int argc, const char *argv[]) { size_t i; - nfc_device *pnd; + nfc_device *pnd = NULL; const char *acLibnfcVersion; bool result; int res = 0; @@ -63,7 +63,8 @@ main(int argc, const char *argv[]) const uint8_t pncmd_diagnose_ram_test[] = { Diagnose, 0x02 }; if (argc > 1) { - errx(1, "usage: %s", argv[0]); + printf("Usage: %s", argv[0]); + exit(EXIT_FAILURE); } nfc_context *context; @@ -85,7 +86,8 @@ main(int argc, const char *argv[]) if (pnd == NULL) { ERR("%s", "Unable to open NFC device."); - return EXIT_FAILURE; + nfc_exit(context); + exit(EXIT_FAILURE); } printf("NFC device [%s] opened.\n", nfc_device_get_name(pnd)); @@ -119,4 +121,7 @@ main(int argc, const char *argv[]) nfc_perror(pnd, "pn53x_transceive: cannot diagnose RAM"); } } + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_SUCCESS); } diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 05ff351..61e3924 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -75,8 +75,6 @@ main(int argc, const char *argv[]) (void) argc; (void) argv; - int ret = EXIT_FAILURE; - nfc_context *context; nfc_init(&context); @@ -90,7 +88,8 @@ main(int argc, const char *argv[]) if (pnd == NULL) { ERR("%s", "Unable to open NFC device."); - return EXIT_FAILURE; + nfc_exit(context); + exit(EXIT_FAILURE); } printf("NFC device: %s opened\n", nfc_device_get_name(pnd)); @@ -107,7 +106,9 @@ main(int argc, const char *argv[]) printf("\n"); if ((input < '1') || (input > '3')) { ERR("%s", "Invalid selection."); - goto error; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } /* @@ -125,7 +126,9 @@ main(int argc, const char *argv[]) // FIXME Its a private pn53x function if (pn532_SAMConfiguration(pnd, mode, 0) < 0) { nfc_perror(pnd, "pn53x_SAMConfiguration"); - goto error; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } printf("Now the SAM is readable for 1 minute from an external reader.\n"); wait_one_minute(); @@ -136,13 +139,17 @@ main(int argc, const char *argv[]) // Set opened NFC device to initiator mode if (nfc_initiator_init_secure_element(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init_secure_element"); - goto error; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } // Let the reader only try once to find a tag if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); - goto error; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } // Read the SAM's info const nfc_modulation nmSAM = { @@ -154,16 +161,22 @@ main(int argc, const char *argv[]) int res; if ((res = nfc_initiator_select_passive_target(pnd, nmSAM, NULL, 0, &nt)) < 0) { nfc_perror(pnd, "nfc_initiator_select_passive_target"); - goto error; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } else if (res == 0) { ERR("No SAM found."); - goto error; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } else if (res == 1) { printf("The following ISO14443A tag (SAM) was found:\n"); print_nfc_target(nt, true); } else { ERR("%s", "More than one ISO14442 tag found as SAM."); - goto error; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } } break; @@ -172,7 +185,9 @@ main(int argc, const char *argv[]) // FIXME Its a private pn53x function if (pn532_SAMConfiguration(pnd, mode, 0) < 0) { nfc_perror(pnd, "pn53x_SAMConfiguration"); - goto error; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } uint8_t abtRx[MAX_FRAME_LEN]; @@ -195,7 +210,9 @@ main(int argc, const char *argv[]) printf("Please note that NFC device (configured as target) stay in target mode until it receive RATS, ATR_REQ or proprietary command.\n"); if (nfc_target_init(pnd, &nt, abtRx, sizeof(abtRx), 0) < 0) { nfc_perror(pnd, "nfc_target_init"); - return EXIT_FAILURE; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } // wait_one_minute (); } @@ -204,15 +221,12 @@ main(int argc, const char *argv[]) // This should not happend... nothing to do. break; } - ret = EXIT_SUCCESS; -error: // Disconnect from the SAM pn532_SAMConfiguration(pnd, PSM_NORMAL, -1); // Close NFC device nfc_close(pnd); nfc_exit(context); - - exit(ret); + exit(EXIT_SUCCESS); } diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index b161347..bc59177 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -79,7 +79,7 @@ int main(int argc, const char *argv[]) if (argc >= 2) { if ((input = fopen(argv[1], "r")) == NULL) { ERR("%s", "Cannot open file."); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } } @@ -94,7 +94,8 @@ int main(int argc, const char *argv[]) if (input != NULL) { fclose(input); } - return EXIT_FAILURE; + nfc_exit(context); + exit(EXIT_FAILURE); } printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); @@ -103,6 +104,8 @@ int main(int argc, const char *argv[]) if (input != NULL) { fclose(input); } + nfc_close(pnd); + nfc_exit(context); exit(EXIT_FAILURE); } @@ -205,5 +208,5 @@ int main(int argc, const char *argv[]) } nfc_close(pnd); nfc_exit(context); - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } diff --git a/utils/nfc-emulate-forum-tag4.c b/utils/nfc-emulate-forum-tag4.c index e17f090..7ef5e10 100644 --- a/utils/nfc-emulate-forum-tag4.c +++ b/utils/nfc-emulate-forum-tag4.c @@ -237,22 +237,25 @@ nfcforum_tag4_io(struct nfc_emulator *emulator, const uint8_t *data_in, const si static void stop_emulation(int sig) { (void) sig; - if (pnd) + if (pnd != NULL) nfc_abort_command(pnd); else exit(EXIT_FAILURE); } -static size_t +static int ndef_message_load(char *filename, struct nfcforum_tag4_ndef_data *tag_data) { struct stat sb; - if (stat(filename, &sb) < 0) - return 0; + if (stat(filename, &sb) < 0) { + printf("file not found or not accessible '%s'", filename); + return -1; + } /* Check file size */ if (sb.st_size > 0xFFFF) { - errx(EXIT_FAILURE, "file size too large '%s'", filename); + printf("file size too large '%s'", filename); + return -1; } tag_data->ndef_file_len = sb.st_size + 2; @@ -261,29 +264,37 @@ ndef_message_load(char *filename, struct nfcforum_tag4_ndef_data *tag_data) tag_data->ndef_file[1] = (uint8_t)(sb.st_size); FILE *F; - if (!(F = fopen(filename, "r"))) - err(EXIT_FAILURE, "fopen (%s, \"r\")", filename); + if (!(F = fopen(filename, "r"))) { + printf("fopen (%s, \"r\")", filename); + return -1; + } - if (1 != fread(tag_data->ndef_file + 2, sb.st_size, 1, F)) - err(EXIT_FAILURE, "Can't read from %s", filename); + if (1 != fread(tag_data->ndef_file + 2, sb.st_size, 1, F)) { + printf("Can't read from %s", filename); + fclose(F); + return -1; + } fclose(F); return sb.st_size; } -static size_t +static int ndef_message_save(char *filename, struct nfcforum_tag4_ndef_data *tag_data) { FILE *F; - if (!(F = fopen(filename, "w"))) - err(EXIT_FAILURE, "fopen (%s, w)", filename); + if (!(F = fopen(filename, "w"))) { + printf("fopen (%s, w)", filename); + return -1; + } if (1 != fwrite(tag_data->ndef_file + 2, tag_data->ndef_file_len - 2, 1, F)) { - err(EXIT_FAILURE, "fwrite (%d)", (int) tag_data->ndef_file_len - 2); + printf("fwrite (%d)", (int) tag_data->ndef_file_len - 2); + fclose(F); + return -1; } fclose(F); - return tag_data->ndef_file_len - 2; } @@ -361,8 +372,9 @@ main(int argc, char *argv[]) // If some file is provided load it if (argc >= (2 + options)) { - if (!ndef_message_load(argv[1 + options], &nfcforum_tag4_data)) { - err(EXIT_FAILURE, "Can't load NDEF file '%s'", argv[1 + options]); + if (ndef_message_load(argv[1 + options], &nfcforum_tag4_data) < 0) { + printf("Can't load NDEF file '%s'", argv[1 + options]); + exit(EXIT_FAILURE); } } @@ -374,6 +386,7 @@ main(int argc, char *argv[]) if (pnd == NULL) { ERR("Unable to open NFC device"); + nfc_exit(context); exit(EXIT_FAILURE); } @@ -384,16 +397,21 @@ main(int argc, char *argv[]) if (0 != nfc_emulate_target(pnd, &emulator, 0)) { // contains already nfc_target_init() call nfc_perror(pnd, "nfc_emulate_target"); + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } - nfc_close(pnd); - if (argc == (3 + options)) { - if (!(ndef_message_save(argv[2 + options], &nfcforum_tag4_data))) { - err(EXIT_FAILURE, "Can't save NDEF file '%s'", argv[2 + options]); + if (ndef_message_save(argv[2 + options], &nfcforum_tag4_data) < 0) { + printf("Can't save NDEF file '%s'", argv[2 + options]); + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } } + nfc_close(pnd); nfc_exit(context); exit(EXIT_SUCCESS); } diff --git a/utils/nfc-list.c b/utils/nfc-list.c index 380120d..d50b469 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -122,6 +122,7 @@ main(int argc, const char *argv[]) } if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); + nfc_exit(context); exit(EXIT_FAILURE); } @@ -242,5 +243,5 @@ main(int argc, const char *argv[]) } nfc_exit(context); - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index 764b4ca..83138f2 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -224,12 +224,12 @@ unlock_card(void) // Configure the CRC if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) { nfc_perror(pnd, "nfc_configure"); - exit(EXIT_FAILURE); + return false; } // Use raw send/receive methods if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) { nfc_perror(pnd, "nfc_configure"); - exit(EXIT_FAILURE); + return false; } iso14443a_crc_append(abtHalt, 2); @@ -248,12 +248,12 @@ unlock_card(void) // Configure the CRC if (nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, true) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); - exit(EXIT_FAILURE); + return false; } // Switch off raw send/receive methods if (nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); - exit(EXIT_FAILURE); + return false; } return true; } @@ -465,134 +465,138 @@ main(int argc, const char *argv[]) bUseKeyFile = (argc > 4); } - switch (atAction) { - case ACTION_USAGE: - print_usage(argv[0]); + if (atAction == ACTION_USAGE) { + print_usage(argv[0]); + exit(EXIT_FAILURE); + } + if (bUseKeyFile) { + pfKeys = fopen(argv[4], "rb"); + if (pfKeys == NULL) { + printf("Could not open keys file: %s\n", argv[4]); exit(EXIT_FAILURE); - break; - case ACTION_READ: - case ACTION_WRITE: - if (bUseKeyFile) { - pfKeys = fopen(argv[4], "rb"); - if (pfKeys == NULL) { - printf("Could not open keys file: %s\n", argv[4]); - exit(EXIT_FAILURE); - } - if (fread(&mtKeys, 1, sizeof(mtKeys), pfKeys) != sizeof(mtKeys)) { - printf("Could not read keys file: %s\n", argv[4]); - fclose(pfKeys); - exit(EXIT_FAILURE); - } - fclose(pfKeys); - } + } + if (fread(&mtKeys, 1, sizeof(mtKeys), pfKeys) != sizeof(mtKeys)) { + printf("Could not read keys file: %s\n", argv[4]); + fclose(pfKeys); + exit(EXIT_FAILURE); + } + fclose(pfKeys); + } - if (atAction == ACTION_READ) { - memset(&mtDump, 0x00, sizeof(mtDump)); - } else { - pfDump = fopen(argv[3], "rb"); + if (atAction == ACTION_READ) { + memset(&mtDump, 0x00, sizeof(mtDump)); + } else { + pfDump = fopen(argv[3], "rb"); - if (pfDump == NULL) { - printf("Could not open dump file: %s\n", argv[3]); - exit(EXIT_FAILURE); - } + if (pfDump == NULL) { + printf("Could not open dump file: %s\n", argv[3]); + exit(EXIT_FAILURE); + } - if (fread(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) { - printf("Could not read dump file: %s\n", argv[3]); - fclose(pfDump); - exit(EXIT_FAILURE); - } - fclose(pfDump); - } - // printf("Successfully opened required files\n"); + if (fread(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) { + printf("Could not read dump file: %s\n", argv[3]); + fclose(pfDump); + exit(EXIT_FAILURE); + } + fclose(pfDump); + } +// printf("Successfully opened required files\n"); - nfc_init(&context); + nfc_init(&context); - // Try to open the NFC reader - pnd = nfc_open(context, NULL); - if (pnd == NULL) { - printf("Error opening NFC reader\n"); - exit(EXIT_FAILURE); - } +// Try to open the NFC reader + pnd = nfc_open(context, NULL); + if (pnd == NULL) { + printf("Error opening NFC reader\n"); + nfc_exit(context); + exit(EXIT_FAILURE); + } - if (nfc_initiator_init(pnd) < 0) { - nfc_perror(pnd, "nfc_initiator_init"); - exit(EXIT_FAILURE); - }; + if (nfc_initiator_init(pnd) < 0) { + nfc_perror(pnd, "nfc_initiator_init"); + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); + }; - // Let the reader only try once to find a tag - if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) { - nfc_perror(pnd, "nfc_device_set_property_bool"); - exit(EXIT_FAILURE); - } - // Disable ISO14443-4 switching in order to read devices that emulate Mifare Classic with ISO14443-4 compliance. - nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, false); +// Let the reader only try once to find a tag + if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) { + nfc_perror(pnd, "nfc_device_set_property_bool"); + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); + } +// Disable ISO14443-4 switching in order to read devices that emulate Mifare Classic with ISO14443-4 compliance. + nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, false); - printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); + printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); - // Try to find a MIFARE Classic tag - if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) { - printf("Error: no tag was found\n"); +// Try to find a MIFARE Classic tag + if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) { + printf("Error: no tag was found\n"); + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); + } +// Test if we are dealing with a MIFARE compatible tag + if ((nt.nti.nai.btSak & 0x08) == 0) { + printf("Warning: tag is probably not a MFC!\n"); + } + +// Get the info from the current tag + pbtUID = nt.nti.nai.abtUid; + + if (bUseKeyFile) { + uint8_t fileUid[4]; + memcpy(fileUid, mtKeys.amb[0].mbm.abtUID, 4); +// Compare if key dump UID is the same as the current tag UID, at least for the first 4 bytes + if (memcmp(pbtUID, fileUid, 4) != 0) { + printf("Expected MIFARE Classic card with UID starting as: %02x%02x%02x%02x\n", + fileUid[0], fileUid[1], fileUid[2], fileUid[3]); + } + } + printf("Found MIFARE Classic card:\n"); + print_nfc_target(nt, false); + +// Guessing size + if ((nt.nti.nai.abtAtqa[1] & 0x02) == 0x02) +// 4K + uiBlocks = 0xff; + else if ((nt.nti.nai.btSak & 0x01) == 0x01) +// 320b + uiBlocks = 0x13; + else +// 1K +// TODO: for MFP it is 0x7f (2K) but how to be sure it's a MFP? Try to get RATS? + uiBlocks = 0x3f; + printf("Guessing size: seems to be a %i-byte card\n", (uiBlocks + 1) * 16); + + if (atAction == ACTION_READ) { + if (read_card(unlock)) { + printf("Writing data to file: %s ...", argv[3]); + fflush(stdout); + pfDump = fopen(argv[3], "wb"); + if (pfDump == NULL) { + printf("Could not open dump file: %s\n", argv[3]); nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); } - // Test if we are dealing with a MIFARE compatible tag - if ((nt.nti.nai.btSak & 0x08) == 0) { - printf("Warning: tag is probably not a MFC!\n"); + if (fwrite(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) { + printf("\nCould not write to file: %s\n", argv[3]); + fclose(pfDump); + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } + printf("Done.\n"); + fclose(pfDump); + } + } else if (atAction == ACTION_WRITE) { + write_card(unlock); + } - // Get the info from the current tag - pbtUID = nt.nti.nai.abtUid; - - if (bUseKeyFile) { - uint8_t fileUid[4]; - memcpy(fileUid, mtKeys.amb[0].mbm.abtUID, 4); - // Compare if key dump UID is the same as the current tag UID, at least for the first 4 bytes - if (memcmp(pbtUID, fileUid, 4) != 0) { - printf("Expected MIFARE Classic card with UID starting as: %02x%02x%02x%02x\n", - fileUid[0], fileUid[1], fileUid[2], fileUid[3]); - } - } - printf("Found MIFARE Classic card:\n"); - print_nfc_target(nt, false); - - // Guessing size - if ((nt.nti.nai.abtAtqa[1] & 0x02) == 0x02) - // 4K - uiBlocks = 0xff; - else if ((nt.nti.nai.btSak & 0x01) == 0x01) - // 320b - uiBlocks = 0x13; - else - // 1K - // TODO: for MFP it is 0x7f (2K) but how to be sure it's a MFP? Try to get RATS? - uiBlocks = 0x3f; - printf("Guessing size: seems to be a %i-byte card\n", (uiBlocks + 1) * 16); - - if (atAction == ACTION_READ) { - if (read_card(unlock)) { - printf("Writing data to file: %s ...", argv[3]); - fflush(stdout); - pfDump = fopen(argv[3], "wb"); - if (pfDump == NULL) { - printf("Could not open dump file: %s\n", argv[3]); - exit(EXIT_FAILURE); - } - if (fwrite(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) { - printf("\nCould not write to file: %s\n", argv[3]); - exit(EXIT_FAILURE); - } - printf("Done.\n"); - fclose(pfDump); - } - } else if (atAction == ACTION_WRITE) { - write_card(unlock); - } - - nfc_close(pnd); - break; - }; - + nfc_close(pnd); nfc_exit(context); exit(EXIT_SUCCESS); } diff --git a/utils/nfc-mfultralight.c b/utils/nfc-mfultralight.c index 1113a5a..178e60b 100644 --- a/utils/nfc-mfultralight.c +++ b/utils/nfc-mfultralight.c @@ -178,7 +178,7 @@ main(int argc, const char *argv[]) printf("r|w - Perform read from or write to card\n"); printf(" - MiFare Dump (MFD) used to write (card to MFD) or (MFD to card)\n"); printf("\n"); - return 1; + exit(EXIT_FAILURE); } DBG("\nChecking arguments and settings\n"); @@ -192,13 +192,13 @@ main(int argc, const char *argv[]) if (pfDump == NULL) { ERR("Could not open dump file: %s\n", argv[2]); - return 1; + exit(EXIT_FAILURE); } if (fread(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) { ERR("Could not read from dump file: %s\n", argv[2]); fclose(pfDump); - return 1; + exit(EXIT_FAILURE); } fclose(pfDump); } @@ -211,17 +211,22 @@ main(int argc, const char *argv[]) pnd = nfc_open(context, NULL); if (pnd == NULL) { ERR("Error opening NFC device\n"); - return 1; + nfc_exit(context); + exit(EXIT_FAILURE); } if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); + nfc_close(pnd); + nfc_exit(context); exit(EXIT_FAILURE); } // Let the device only try once to find a tag if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) { nfc_perror(pnd, "nfc_device_set_property_bool"); + nfc_close(pnd); + nfc_exit(context); exit(EXIT_FAILURE); } @@ -232,7 +237,7 @@ main(int argc, const char *argv[]) ERR("no tag was found\n"); nfc_close(pnd); nfc_exit(context); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } // Test if we are dealing with a MIFARE compatible tag @@ -240,7 +245,7 @@ main(int argc, const char *argv[]) ERR("tag is not a MIFARE Ultralight card\n"); nfc_close(pnd); nfc_exit(context); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } // Get the info from the current tag printf("Found MIFARE Ultralight card with UID: "); @@ -257,12 +262,16 @@ main(int argc, const char *argv[]) pfDump = fopen(argv[2], "wb"); if (pfDump == NULL) { printf("Could not open file: %s\n", argv[2]); - return EXIT_FAILURE; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } if (fwrite(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) { printf("Could not write to file: %s\n", argv[2]); fclose(pfDump); - return EXIT_FAILURE; + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } fclose(pfDump); printf("Done.\n"); @@ -273,5 +282,5 @@ main(int argc, const char *argv[]) nfc_close(pnd); nfc_exit(context); - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index 5dd479e..a075627 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -72,7 +72,7 @@ print_usage(char *progname) static void stop_select(int sig) { (void) sig; - if (pnd) + if (pnd != NULL) nfc_abort_command(pnd); else exit(EXIT_FAILURE); @@ -203,6 +203,8 @@ main(int argc, char *argv[]) if (pnd == NULL) { ERR("Unable to open NFC device"); + fclose(ndef_stream); + nfc_exit(context); exit(EXIT_FAILURE); } @@ -219,17 +221,21 @@ main(int argc, char *argv[]) if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); + fclose(ndef_stream); + nfc_close(pnd); + nfc_exit(context); exit(EXIT_FAILURE); } fprintf(message_stream, "Place your NFC Forum Tag Type 3 in the field...\n"); - int error = EXIT_SUCCESS; // Polling payload (SENSF_REQ) must be present (see NFC Digital Protol) const uint8_t *pbtSensfReq = (uint8_t *)"\x00\xff\xff\x01\x00"; if (nfc_initiator_select_passive_target(pnd, nm, pbtSensfReq, 5, &nt) <= 0) { nfc_perror(pnd, "nfc_initiator_select_passive_target"); - error = EXIT_FAILURE; - goto error; + fclose(ndef_stream); + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } // Check if System Code equals 0x12fc @@ -239,14 +245,18 @@ main(int argc, char *argv[]) const uint8_t *pbtSensfReqNfcForum = (uint8_t *)"\x00\x12\xfc\x01\x00"; if (nfc_initiator_select_passive_target(pnd, nm, pbtSensfReqNfcForum, 5, &nt) <= 0) { nfc_perror(pnd, "nfc_initiator_select_passive_target"); - error = EXIT_FAILURE; - goto error; + fclose(ndef_stream); + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } // Check again if System Code equals 0x12fc if (0 != memcmp(nt.nti.nfi.abtSysCode, abtNfcForumSysCode, 2)) { fprintf(stderr, "Tag is not NFC Forum Tag Type 3 compliant.\n"); - error = EXIT_FAILURE; - goto error; + fclose(ndef_stream); + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } } @@ -254,8 +264,10 @@ main(int argc, char *argv[]) if ((nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false) < 0) || (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0)) { nfc_perror(pnd, "nfc_device_set_property_bool"); - error = EXIT_FAILURE; - goto error; + fclose(ndef_stream); + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } uint8_t data[1024]; @@ -264,8 +276,10 @@ main(int argc, char *argv[]) if (0 >= (len = nfc_forum_tag_type3_check(pnd, nt, 0, 1, data, &data_len))) { nfc_perror(pnd, "nfc_forum_tag_type3_check"); - error = EXIT_FAILURE; - goto error; + fclose(ndef_stream); + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } const int ndef_major_version = (data[0] & 0xf0) >> 4; @@ -285,14 +299,18 @@ main(int argc, char *argv[]) const uint16_t ndef_checksum = (data[14] << 8) + data[15]; if (ndef_calculated_checksum != ndef_checksum) { fprintf(stderr, "NDEF CRC does not match with calculated one\n"); - error = EXIT_FAILURE; - goto error; + fclose(ndef_stream); + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } if (!ndef_data_len) { fprintf(stderr, "Empty NFC Forum Tag Type 3\n"); - error = EXIT_FAILURE; - goto error; + fclose(ndef_stream); + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } const uint8_t block_max_per_check = data[1]; @@ -303,22 +321,23 @@ main(int argc, char *argv[]) size_t size = sizeof(data) - data_len; if (!nfc_forum_tag_type3_check(pnd, nt, 1 + b, MIN(block_max_per_check, (block_count_to_check - (b * block_max_per_check))), data + data_len, &size)) { nfc_perror(pnd, "nfc_forum_tag_type3_check"); - error = EXIT_FAILURE; - goto error; + fclose(ndef_stream); + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } data_len += size; } if (fwrite(data, 1, data_len, ndef_stream) != data_len) { fprintf(stderr, "Could not write to file.\n"); - error = EXIT_FAILURE; - goto error; + fclose(ndef_stream); + nfc_close(pnd); + nfc_exit(context); + exit(EXIT_FAILURE); } -error: fclose(ndef_stream); - if (pnd) { - nfc_close(pnd); - } + nfc_close(pnd); nfc_exit(context); - exit(error); + exit(EXIT_SUCCESS); } diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index bc7ab00..9dcafa2 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -94,29 +94,29 @@ print_usage(char *argv[]) printf("\t-n N\tAdds a waiting time of N seconds (integer) in the relay to mimic long distance.\n"); } -static bool print_hex_fd4(const uint8_t *pbtData, const size_t szBytes, const char *pchPrefix) +static int print_hex_fd4(const uint8_t *pbtData, const size_t szBytes, const char *pchPrefix) { size_t szPos; if (szBytes > MAX_FRAME_LEN) { - return EXIT_FAILURE; + return -1; } if (fprintf(fd4, "#%s %04zx: ", pchPrefix, szBytes) < 0) { - return EXIT_FAILURE; + return -1; } for (szPos = 0; szPos < szBytes; szPos++) { if (fprintf(fd4, "%02x ", pbtData[szPos]) < 0) { - return EXIT_FAILURE; + return -1; } } if (fprintf(fd4, "\n") < 0) { - return EXIT_FAILURE; + return -1; } fflush(fd4); - return EXIT_SUCCESS; + return 0; } -static bool scan_hex_fd3(uint8_t *pbtData, size_t *pszBytes, const char *pchPrefix) +static int scan_hex_fd3(uint8_t *pbtData, size_t *pszBytes, const char *pchPrefix) { size_t szPos; unsigned int uiBytes; @@ -126,25 +126,25 @@ static bool scan_hex_fd3(uint8_t *pbtData, size_t *pszBytes, const char *pchPref // Look for our next sync marker while ((c = fgetc(fd3)) != '#') { if (c == EOF) { - return EXIT_FAILURE; + return -1; } } strncpy(pchScan, pchPrefix, 250); strcat(pchScan, " %04x:"); if (fscanf(fd3, pchScan, &uiBytes) < 1) { - return EXIT_FAILURE; + return -1; } *pszBytes = uiBytes; if (*pszBytes > MAX_FRAME_LEN) { - return EXIT_FAILURE; + return -1; } for (szPos = 0; szPos < *pszBytes; szPos++) { if (fscanf(fd3, "%02x", &uiData) < 1) { - return EXIT_FAILURE; + return -1; } pbtData[szPos] = uiData; } - return EXIT_SUCCESS; + return 0; } int @@ -158,7 +158,7 @@ main(int argc, char *argv[]) for (arg = 1; arg < argc; arg++) { if (0 == strcmp(argv[arg], "-h")) { print_usage(argv); - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } else if (0 == strcmp(argv[arg], "-q")) { quiet_output = true; } else if (0 == strcmp(argv[arg], "-t")) { @@ -176,13 +176,13 @@ main(int argc, char *argv[]) if (++arg == argc || (sscanf(argv[arg], "%i", &waiting_time) < 1)) { ERR("Missing or wrong waiting time value: %s.", argv[arg]); print_usage(argv); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } printf("Waiting time: %i secs.\n", waiting_time); } else { ERR("%s is not supported option.", argv[arg]); print_usage(argv); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } } @@ -205,20 +205,24 @@ main(int argc, char *argv[]) if (initiator_only_mode || target_only_mode) { if (szFound < 1) { ERR("No device found"); - return EXIT_FAILURE; + nfc_exit(context); + exit(EXIT_FAILURE); } if ((fd3 = fdopen(3, "r")) == NULL) { ERR("Could not open file descriptor 3"); - return EXIT_FAILURE; + nfc_exit(context); + exit(EXIT_FAILURE); } if ((fd4 = fdopen(4, "r")) == NULL) { ERR("Could not open file descriptor 4"); - return EXIT_FAILURE; + nfc_exit(context); + exit(EXIT_FAILURE); } } else { if (szFound < 2) { ERR("%zd device found but two opened devices are needed to relay NFC.", szFound); - return EXIT_FAILURE; + nfc_exit(context); + exit(EXIT_FAILURE); } } @@ -234,8 +238,9 @@ main(int argc, char *argv[]) pndInitiator = nfc_open(context, connstrings[1]); } - if (!pndInitiator) { + if (pndInitiator == NULL) { printf("Error opening NFC reader\n"); + nfc_exit(context); exit(EXIT_FAILURE); } @@ -263,25 +268,25 @@ main(int argc, char *argv[]) printf("Found tag:\n"); print_nfc_target(ntRealTarget, false); if (initiator_only_mode) { - if (print_hex_fd4(ntRealTarget.nti.nai.abtUid, ntRealTarget.nti.nai.szUidLen, "UID") != EXIT_SUCCESS) { + if (print_hex_fd4(ntRealTarget.nti.nai.abtUid, ntRealTarget.nti.nai.szUidLen, "UID") < 0) { fprintf(stderr, "Error while printing UID to FD4\n"); nfc_close(pndInitiator); nfc_exit(context); exit(EXIT_FAILURE); } - if (print_hex_fd4(ntRealTarget.nti.nai.abtAtqa, 2, "ATQA") != EXIT_SUCCESS) { + if (print_hex_fd4(ntRealTarget.nti.nai.abtAtqa, 2, "ATQA") < 0) { fprintf(stderr, "Error while printing ATQA to FD4\n"); nfc_close(pndInitiator); nfc_exit(context); exit(EXIT_FAILURE); } - if (print_hex_fd4(&(ntRealTarget.nti.nai.btSak), 1, "SAK") != EXIT_SUCCESS) { + if (print_hex_fd4(&(ntRealTarget.nti.nai.btSak), 1, "SAK") < 0) { fprintf(stderr, "Error while printing SAK to FD4\n"); nfc_close(pndInitiator); nfc_exit(context); exit(EXIT_FAILURE); } - if (print_hex_fd4(ntRealTarget.nti.nai.abtAts, ntRealTarget.nti.nai.szAtsLen, "ATS") != EXIT_SUCCESS) { + if (print_hex_fd4(ntRealTarget.nti.nai.abtAts, ntRealTarget.nti.nai.szAtsLen, "ATS") < 0) { fprintf(stderr, "Error while printing ATS to FD4\n"); nfc_close(pndInitiator); nfc_exit(context); @@ -305,24 +310,25 @@ main(int argc, char *argv[]) }; if (target_only_mode) { size_t foo; - if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtUid, &(ntEmulatedTarget.nti.nai.szUidLen), "UID") != EXIT_SUCCESS) { + if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtUid, &(ntEmulatedTarget.nti.nai.szUidLen), "UID") < 0) { fprintf(stderr, "Error while scanning UID from FD3\n"); nfc_close(pndInitiator); nfc_exit(context); exit(EXIT_FAILURE); } - if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtAtqa, &foo, "ATQA") != EXIT_SUCCESS) { + if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtAtqa, &foo, "ATQA") < 0) { fprintf(stderr, "Error while scanning ATQA from FD3\n"); nfc_close(pndInitiator); + nfc_exit(context); exit(EXIT_FAILURE); } - if (scan_hex_fd3(&(ntEmulatedTarget.nti.nai.btSak), &foo, "SAK") != EXIT_SUCCESS) { + if (scan_hex_fd3(&(ntEmulatedTarget.nti.nai.btSak), &foo, "SAK") < 0) { fprintf(stderr, "Error while scanning SAK from FD3\n"); nfc_close(pndInitiator); nfc_exit(context); exit(EXIT_FAILURE); } - if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtAts, &(ntEmulatedTarget.nti.nai.szAtsLen), "ATS") != EXIT_SUCCESS) { + if (scan_hex_fd3(ntEmulatedTarget.nti.nai.abtAts, &(ntEmulatedTarget.nti.nai.szAtsLen), "ATS") < 0) { fprintf(stderr, "Error while scanning ATS from FD3\n"); nfc_close(pndInitiator); nfc_exit(context); @@ -375,7 +381,7 @@ main(int argc, char *argv[]) nfc_close(pndInitiator); } nfc_exit(context); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } printf("NFC emulator device: %s opened\n", nfc_device_get_name(pndTarget)); @@ -408,7 +414,7 @@ main(int argc, char *argv[]) } szCapduLen = (size_t) res; if (target_only_mode) { - if (print_hex_fd4(abtCapdu, szCapduLen, "C-APDU") != EXIT_SUCCESS) { + if (print_hex_fd4(abtCapdu, szCapduLen, "C-APDU") < 0) { fprintf(stderr, "Error while printing C-APDU to FD4\n"); nfc_close(pndTarget); nfc_exit(context); @@ -416,7 +422,7 @@ main(int argc, char *argv[]) } } } else { - if (scan_hex_fd3(abtCapdu, &szCapduLen, "C-APDU") != EXIT_SUCCESS) { + if (scan_hex_fd3(abtCapdu, &szCapduLen, "C-APDU") < 0) { fprintf(stderr, "Error while scanning C-APDU from FD3\n"); nfc_close(pndInitiator); nfc_exit(context); @@ -438,7 +444,7 @@ main(int argc, char *argv[]) ret = true; } } else { - if (scan_hex_fd3(abtRapdu, &szRapduLen, "R-APDU") != EXIT_SUCCESS) { + if (scan_hex_fd3(abtRapdu, &szRapduLen, "R-APDU") < 0) { fprintf(stderr, "Error while scanning R-APDU from FD3\n"); nfc_close(pndTarget); nfc_exit(context); @@ -474,7 +480,7 @@ main(int argc, char *argv[]) exit(EXIT_FAILURE); } } else { - if (print_hex_fd4(abtRapdu, szRapduLen, "R-APDU") != EXIT_SUCCESS) { + if (print_hex_fd4(abtRapdu, szRapduLen, "R-APDU") < 0) { fprintf(stderr, "Error while printing R-APDU to FD4\n"); nfc_close(pndInitiator); nfc_exit(context); diff --git a/utils/nfc-scan-device.c b/utils/nfc-scan-device.c index 04eae0d..72a28bc 100644 --- a/utils/nfc-scan-device.c +++ b/utils/nfc-scan-device.c @@ -76,7 +76,7 @@ main(int argc, const char *argv[]) for (int arg = 1; arg < argc; arg++) { if (0 == strcmp(argv[arg], "-h")) { print_usage(argv); - return EXIT_SUCCESS; + exit(EXIT_SUCCESS); } else if (0 == strcmp(argv[arg], "-v")) { verbose = true; } else if (0 == strcmp(argv[arg], "-i")) { @@ -85,7 +85,7 @@ main(int argc, const char *argv[]) } else { ERR("%s is not supported option.", argv[arg]); print_usage(argv); - return EXIT_FAILURE; + exit(EXIT_FAILURE); } } @@ -98,10 +98,10 @@ main(int argc, const char *argv[]) nfc_connstring connstrings[MAX_DEVICE_COUNT]; size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT); - int res = EXIT_FAILURE; if (szDeviceFound == 0) { printf("No NFC device found.\n"); - goto bye; + nfc_exit(context); + exit(EXIT_FAILURE); } printf("%d NFC device(s) found:\n", (int)szDeviceFound); @@ -121,9 +121,6 @@ main(int argc, const char *argv[]) printf("nfc_open failed for %s\n", connstrings[i]); } } - res = EXIT_SUCCESS; - -bye: nfc_exit(context); - return res; + exit(EXIT_SUCCESS); } From 09ef2e39277f78b9f6ac445614da1eb34939ddcb Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Tue, 5 Mar 2013 19:57:36 +0100 Subject: [PATCH 04/43] nfc_device_new(): replace err() by return Not a good idea to call exit() from a library... --- libnfc/drivers/acr122_pcsc.c | 4 ++++ libnfc/drivers/acr122_usb.c | 4 ++++ libnfc/drivers/acr122s.c | 8 ++++++++ libnfc/drivers/arygon.c | 8 ++++++++ libnfc/drivers/pn532_uart.c | 8 ++++++++ libnfc/drivers/pn53x_usb.c | 4 ++++ libnfc/nfc-device.c | 2 +- 7 files changed, 37 insertions(+), 1 deletion(-) diff --git a/libnfc/drivers/acr122_pcsc.c b/libnfc/drivers/acr122_pcsc.c index 1bb9b8e..e24e425 100644 --- a/libnfc/drivers/acr122_pcsc.c +++ b/libnfc/drivers/acr122_pcsc.c @@ -280,6 +280,10 @@ acr122_pcsc_open(const nfc_context *context, const nfc_connstring connstring) char *pcFirmware; nfc_device *pnd = nfc_device_new(context, fullconnstring); + if (!pnd) { + perror("malloc"); + goto error; + } pnd->driver_data = malloc(sizeof(struct acr122_pcsc_data)); if (!pnd->driver_data) { perror("malloc"); diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c index 91b47c0..c39cac6 100644 --- a/libnfc/drivers/acr122_usb.c +++ b/libnfc/drivers/acr122_usb.c @@ -475,6 +475,10 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) data.model = acr122_usb_get_device_model(dev->descriptor.idVendor, dev->descriptor.idProduct); // Allocate memory for the device info and specification, fill it and return the info pnd = nfc_device_new(context, connstring); + if (!pnd) { + perror("malloc"); + goto error; + } acr122_usb_get_usb_device_name(dev, data.pudh, pnd->name, sizeof(pnd->name)); pnd->driver_data = malloc(sizeof(struct acr122_usb_data)); diff --git a/libnfc/drivers/acr122s.c b/libnfc/drivers/acr122s.c index 3043268..0b19d32 100644 --- a/libnfc/drivers/acr122s.c +++ b/libnfc/drivers/acr122s.c @@ -470,6 +470,10 @@ acr122s_scan(const nfc_context *context, nfc_connstring connstrings[], const siz nfc_connstring connstring; snprintf(connstring, sizeof(nfc_connstring), "%s:%s:%"PRIu32, ACR122S_DRIVER_NAME, acPort, ACR122S_DEFAULT_SPEED); nfc_device *pnd = nfc_device_new(context, connstring); + if (!pnd) { + perror("malloc"); + return -1; + } pnd->driver = &acr122s_driver; pnd->driver_data = malloc(sizeof(struct acr122s_data)); @@ -574,6 +578,10 @@ acr122s_open(const nfc_context *context, const nfc_connstring connstring) uart_set_speed(sp, ndd.speed); pnd = nfc_device_new(context, connstring); + if (!pnd) { + perror("malloc"); + return NULL; + } pnd->driver = &acr122s_driver; strcpy(pnd->name, ACR122S_DRIVER_NAME); diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index ee0f149..a788807 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -114,6 +114,10 @@ arygon_scan(const nfc_context *context, nfc_connstring connstrings[], const size nfc_connstring connstring; snprintf(connstring, sizeof(nfc_connstring), "%s:%s:%"PRIu32, ARYGON_DRIVER_NAME, acPort, ARYGON_DEFAULT_SPEED); nfc_device *pnd = nfc_device_new(context, connstring); + if (!pnd) { + perror("malloc"); + return 0; + } pnd->driver = &arygon_driver; pnd->driver_data = malloc(sizeof(struct arygon_data)); @@ -263,6 +267,10 @@ arygon_open(const nfc_context *context, const nfc_connstring connstring) // We have a connection pnd = nfc_device_new(context, connstring); + if (!pnd) { + perror("malloc"); + return NULL; + } snprintf(pnd->name, sizeof(pnd->name), "%s:%s", ARYGON_DRIVER_NAME, ndd.port); pnd->driver_data = malloc(sizeof(struct arygon_data)); diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 48ffc4b..4cbb9a5 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -88,6 +88,10 @@ pn532_uart_scan(const nfc_context *context, nfc_connstring connstrings[], const nfc_connstring connstring; snprintf(connstring, sizeof(nfc_connstring), "%s:%s:%"PRIu32, PN532_UART_DRIVER_NAME, acPort, PN532_UART_DEFAULT_SPEED); nfc_device *pnd = nfc_device_new(context, connstring); + if (!pnd) { + perror("malloc"); + return 0; + } pnd->driver = &pn532_uart_driver; pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); if (!pnd->driver_data) { @@ -240,6 +244,10 @@ pn532_uart_open(const nfc_context *context, const nfc_connstring connstring) // We have a connection pnd = nfc_device_new(context, connstring); + if (!pnd) { + perror("malloc"); + return NULL; + } snprintf(pnd->name, sizeof(pnd->name), "%s:%s", PN532_UART_DRIVER_NAME, ndd.port); pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 747c6ec..f440b5a 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -360,6 +360,10 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring) data.model = pn53x_usb_get_device_model(dev->descriptor.idVendor, dev->descriptor.idProduct); // Allocate memory for the device info and specification, fill it and return the info pnd = nfc_device_new(context, connstring); + if (!pnd) { + perror("malloc"); + goto error; + } pn53x_usb_get_usb_device_name(dev, data.pudh, pnd->name, sizeof(pnd->name)); pnd->driver_data = malloc(sizeof(struct pn53x_usb_data)); diff --git a/libnfc/nfc-device.c b/libnfc/nfc-device.c index 76c29f1..4e5d883 100644 --- a/libnfc/nfc-device.c +++ b/libnfc/nfc-device.c @@ -38,7 +38,7 @@ nfc_device_new(const nfc_context *context, const nfc_connstring connstring) nfc_device *res = malloc(sizeof(*res)); if (!res) { - err(EXIT_FAILURE, "nfc_device_new: malloc"); + return NULL; } // Store associated context From ae6062e5ba83c213616e32ad162ed143a170667e Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Tue, 5 Mar 2013 20:03:19 +0100 Subject: [PATCH 05/43] nfc_context_new(): replace err() by return, problem still in nfc_init()!! Not a good idea to call exit() from a library... Problem is now moved to void nfc_init() calling exit() This requires a change in API to return error rather than exiting... --- libnfc/nfc-internal.c | 2 +- libnfc/nfc.c | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/libnfc/nfc-internal.c b/libnfc/nfc-internal.c index 5a40325..715c7ff 100644 --- a/libnfc/nfc-internal.c +++ b/libnfc/nfc-internal.c @@ -68,7 +68,7 @@ nfc_context_new(void) nfc_context *res = malloc(sizeof(*res)); if (!res) { - err(EXIT_FAILURE, "nfc_context_new: malloc"); + return NULL; } // Set default context values diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 20ed75d..e9a8a9c 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -172,7 +172,11 @@ void nfc_init(nfc_context **context) { *context = nfc_context_new(); - + if (!context) { + perror("malloc"); + // TODO: not a good idea to call exit() from a library, we should change API and return error + exit(EXIT_FAILURE); + } if (!nfc_drivers) nfc_drivers_init(); } From 73b5c9d0af39128d41b544fe6d8617ea944d5dc7 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Tue, 5 Mar 2013 22:24:59 +0100 Subject: [PATCH 06/43] nfc_init() return rather than exit on malloc error, examples fixed accordingly --- examples/doc/quick_start_example1.c | 4 ++++ examples/doc/quick_start_example2.c | 4 ++++ examples/nfc-anticol.c | 6 +++++- examples/nfc-dep-initiator.c | 6 +++++- examples/nfc-dep-target.c | 4 ++++ examples/nfc-emulate-forum-tag2.c | 4 ++++ examples/nfc-emulate-tag.c | 4 ++++ examples/nfc-emulate-uid.c | 6 +++++- examples/nfc-mfsetuid.c | 6 +++++- examples/nfc-poll.c | 4 ++++ examples/nfc-relay.c | 6 +++++- examples/pn53x-diagnose.c | 4 ++++ examples/pn53x-sam.c | 4 ++++ examples/pn53x-tamashell.c | 4 ++++ libnfc/nfc.c | 3 +-- utils/nfc-emulate-forum-tag4.c | 4 ++++ utils/nfc-list.c | 4 ++++ utils/nfc-mfclassic.c | 6 +++++- utils/nfc-mfultralight.c | 6 +++++- utils/nfc-read-forum-tag3.c | 4 ++++ utils/nfc-relay-picc.c | 4 ++++ utils/nfc-scan-device.c | 4 ++++ 22 files changed, 92 insertions(+), 9 deletions(-) diff --git a/examples/doc/quick_start_example1.c b/examples/doc/quick_start_example1.c index 7d26a3d..59cf06d 100644 --- a/examples/doc/quick_start_example1.c +++ b/examples/doc/quick_start_example1.c @@ -36,6 +36,10 @@ main(int argc, const char *argv[]) // Initialize libnfc and set the nfc_context nfc_init(&context); + if (context == NULL) { + warnx("Unable to init libnfc (malloc)\n"); + exit(EXIT_FAILURE); + } // Display libnfc version const char *acLibnfcVersion = nfc_version(); diff --git a/examples/doc/quick_start_example2.c b/examples/doc/quick_start_example2.c index 0532ca9..dd7bdcc 100644 --- a/examples/doc/quick_start_example2.c +++ b/examples/doc/quick_start_example2.c @@ -28,6 +28,10 @@ main(int argc, const char *argv[]) // Initialize libnfc and set the nfc_context nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Display libnfc version const char *acLibnfcVersion = nfc_version(); diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index 217b267..bbbdb1b 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -151,12 +151,16 @@ main(int argc, char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Try to open the NFC reader pnd = nfc_open(context, NULL); if (pnd == NULL) { - printf("Error opening NFC reader\n"); + ERR("Error opening NFC reader"); nfc_exit(context); exit(EXIT_FAILURE); } diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index 5d5da9e..acf6e31 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -73,10 +73,14 @@ main(int argc, const char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } pnd = nfc_open(context, NULL); if (pnd == NULL) { - printf("Unable to open NFC device.\n"); + ERR("Unable to open NFC device."); nfc_exit(context); exit(EXIT_FAILURE); } diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 0853809..d238a27 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -72,6 +72,10 @@ main(int argc, const char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } #define MAX_DEVICE_COUNT 2 nfc_connstring connstrings[MAX_DEVICE_COUNT]; size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT); diff --git a/examples/nfc-emulate-forum-tag2.c b/examples/nfc-emulate-forum-tag2.c index e569d78..f76ca44 100644 --- a/examples/nfc-emulate-forum-tag2.c +++ b/examples/nfc-emulate-forum-tag2.c @@ -187,6 +187,10 @@ main(int argc, char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } pnd = nfc_open(context, NULL); if (pnd == NULL) { diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index aa73e7e..68e8766 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -181,6 +181,10 @@ main(int argc, char *argv[]) #endif nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Display libnfc version acLibnfcVersion = nfc_version(); diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 11d861f..969848f 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -128,12 +128,16 @@ main(int argc, char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Try to open the NFC device pnd = nfc_open(context, NULL); if (pnd == NULL) { - printf("Unable to open NFC device\n"); + ERR("Unable to open NFC device"); nfc_exit(context); exit(EXIT_FAILURE); } diff --git a/examples/nfc-mfsetuid.c b/examples/nfc-mfsetuid.c index 9311873..b785a65 100644 --- a/examples/nfc-mfsetuid.c +++ b/examples/nfc-mfsetuid.c @@ -179,12 +179,16 @@ main(int argc, char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Try to open the NFC reader pnd = nfc_open(context, NULL); if (pnd == NULL) { - printf("Error opening NFC reader\n"); + ERR("Error opening NFC reader"); nfc_exit(context); exit(EXIT_FAILURE); } diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index 0bb1e8f..7c2d079 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -105,6 +105,10 @@ main(int argc, const char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } pnd = nfc_open(context, NULL); diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 7b5048c..8dbdcd9 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -110,6 +110,10 @@ main(int argc, char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } nfc_connstring connstrings[MAX_DEVICE_COUNT]; // List available devices size_t szFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT); @@ -123,7 +127,7 @@ main(int argc, char *argv[]) // Try to open the NFC emulator device pndTag = nfc_open(context, connstrings[0]); if (pndTag == NULL) { - printf("Error opening NFC emulator device\n"); + ERR("Error opening NFC emulator device"); nfc_exit(context); exit(EXIT_FAILURE); } diff --git a/examples/pn53x-diagnose.c b/examples/pn53x-diagnose.c index 97f0848..f747b39 100644 --- a/examples/pn53x-diagnose.c +++ b/examples/pn53x-diagnose.c @@ -69,6 +69,10 @@ main(int argc, const char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Display libnfc version acLibnfcVersion = nfc_version(); diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 61e3924..f4baabe 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -77,6 +77,10 @@ main(int argc, const char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Display libnfc version const char *acLibnfcVersion = nfc_version(); diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index bc59177..eac9993 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -85,6 +85,10 @@ int main(int argc, const char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Try to open the NFC reader pnd = nfc_open(context, NULL); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index e9a8a9c..50d6272 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -174,8 +174,7 @@ nfc_init(nfc_context **context) *context = nfc_context_new(); if (!context) { perror("malloc"); - // TODO: not a good idea to call exit() from a library, we should change API and return error - exit(EXIT_FAILURE); + return; } if (!nfc_drivers) nfc_drivers_init(); diff --git a/utils/nfc-emulate-forum-tag4.c b/utils/nfc-emulate-forum-tag4.c index 7ef5e10..cfd94e7 100644 --- a/utils/nfc-emulate-forum-tag4.c +++ b/utils/nfc-emulate-forum-tag4.c @@ -380,6 +380,10 @@ main(int argc, char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)\n"); + exit(EXIT_FAILURE); + } // Try to open the NFC reader pnd = nfc_open(context, NULL); diff --git a/utils/nfc-list.c b/utils/nfc-list.c index d50b469..47d485c 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -71,6 +71,10 @@ main(int argc, const char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Display libnfc version acLibnfcVersion = nfc_version(); diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index 83138f2..081be3b 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -503,11 +503,15 @@ main(int argc, const char *argv[]) // printf("Successfully opened required files\n"); nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Try to open the NFC reader pnd = nfc_open(context, NULL); if (pnd == NULL) { - printf("Error opening NFC reader\n"); + ERR("Error opening NFC reader"); nfc_exit(context); exit(EXIT_FAILURE); } diff --git a/utils/nfc-mfultralight.c b/utils/nfc-mfultralight.c index 178e60b..b1e3e79 100644 --- a/utils/nfc-mfultralight.c +++ b/utils/nfc-mfultralight.c @@ -206,11 +206,15 @@ main(int argc, const char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Try to open the NFC device pnd = nfc_open(context, NULL); if (pnd == NULL) { - ERR("Error opening NFC device\n"); + ERR("Error opening NFC device"); nfc_exit(context); exit(EXIT_FAILURE); } diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index a075627..27ce150 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -198,6 +198,10 @@ main(int argc, char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)\n"); + exit(EXIT_FAILURE); + } pnd = nfc_open(context, NULL); diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 9dcafa2..90221c8 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -197,6 +197,10 @@ main(int argc, char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } nfc_connstring connstrings[MAX_DEVICE_COUNT]; // List available devices diff --git a/utils/nfc-scan-device.c b/utils/nfc-scan-device.c index 72a28bc..9bfebee 100644 --- a/utils/nfc-scan-device.c +++ b/utils/nfc-scan-device.c @@ -90,6 +90,10 @@ main(int argc, const char *argv[]) } nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)\n"); + exit(EXIT_FAILURE); + } // Display libnfc version acLibnfcVersion = nfc_version(); From e55efd6db0a16e2fa40fdcc74cd6b8a21f45a772 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Tue, 5 Mar 2013 22:50:37 +0100 Subject: [PATCH 07/43] examples/utils: add nfc_exit() to signal traps and one missing nfc_abort_command() --- examples/nfc-dep-initiator.c | 6 ++++-- examples/nfc-dep-target.c | 6 ++++-- examples/nfc-emulate-forum-tag2.c | 1 + examples/nfc-emulate-tag.c | 3 +++ examples/nfc-poll.c | 4 +++- utils/nfc-emulate-forum-tag4.c | 6 ++++-- utils/nfc-read-forum-tag3.c | 6 ++++-- 7 files changed, 23 insertions(+), 9 deletions(-) diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index acf6e31..c34ea6c 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -53,10 +53,12 @@ static nfc_device *pnd; static void stop_dep_communication(int sig) { (void) sig; - if (pnd != NULL) + if (pnd != NULL) { nfc_abort_command(pnd); - else + } else { + nfc_exit(context); exit(EXIT_FAILURE); + } } int diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index d238a27..b91ec18 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -52,10 +52,12 @@ static nfc_device *pnd; static void stop_dep_communication(int sig) { (void) sig; - if (pnd != NULL) + if (pnd != NULL) { nfc_abort_command(pnd); - else + } else { + nfc_exit(context); exit(EXIT_FAILURE); + } } int diff --git a/examples/nfc-emulate-forum-tag2.c b/examples/nfc-emulate-forum-tag2.c index f76ca44..cdd430e 100644 --- a/examples/nfc-emulate-forum-tag2.c +++ b/examples/nfc-emulate-forum-tag2.c @@ -82,6 +82,7 @@ stop_emulation(int sig) if (pnd != NULL) { nfc_abort_command(pnd); } else { + nfc_exit(context); exit(EXIT_FAILURE); } } diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 68e8766..42c9a10 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -66,6 +66,9 @@ intr_hdlr(int sig) { (void) sig; printf("\nQuitting...\n"); + if (pnd != NULL) { + nfc_abort_command(pnd); + } nfc_close(pnd); nfc_exit(context); exit(EXIT_FAILURE); diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index 7c2d079..0f885bb 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -58,8 +58,10 @@ static void stop_polling(int sig) (void) sig; if (pnd != NULL) nfc_abort_command(pnd); - else + else { + nfc_exit(context); exit(EXIT_FAILURE); + } } static void diff --git a/utils/nfc-emulate-forum-tag4.c b/utils/nfc-emulate-forum-tag4.c index cfd94e7..f604388 100644 --- a/utils/nfc-emulate-forum-tag4.c +++ b/utils/nfc-emulate-forum-tag4.c @@ -237,10 +237,12 @@ nfcforum_tag4_io(struct nfc_emulator *emulator, const uint8_t *data_in, const si static void stop_emulation(int sig) { (void) sig; - if (pnd != NULL) + if (pnd != NULL) { nfc_abort_command(pnd); - else + } else { + nfc_exit(context); exit(EXIT_FAILURE); + } } static int diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index 27ce150..720ff6a 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -72,10 +72,12 @@ print_usage(char *progname) static void stop_select(int sig) { (void) sig; - if (pnd != NULL) + if (pnd != NULL) { nfc_abort_command(pnd); - else + } else { + nfc_exit(context); exit(EXIT_FAILURE); + } } static void From 03bc30f1162de2e547a77d601a8bc9ec0027e1a4 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Tue, 5 Mar 2013 23:56:38 +0100 Subject: [PATCH 08/43] Fix "Possible null pointer dereference" error --- libnfc/nfc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 50d6272..6fabdf9 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -172,7 +172,7 @@ void nfc_init(nfc_context **context) { *context = nfc_context_new(); - if (!context) { + if (!*context) { perror("malloc"); return; } From 4822bb3e4202cfc6824d91a1cacffc06ecff0bfe Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Tue, 5 Mar 2013 23:58:35 +0100 Subject: [PATCH 09/43] Fix bug introduced in some signal traps where context was not declared --- examples/nfc-dep-initiator.c | 2 +- examples/nfc-dep-target.c | 2 +- examples/nfc-emulate-forum-tag2.c | 2 +- examples/nfc-emulate-uid.c | 2 +- examples/nfc-poll.c | 2 +- utils/nfc-emulate-forum-tag4.c | 2 +- utils/nfc-read-forum-tag3.c | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index c34ea6c..c095a4b 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -49,6 +49,7 @@ #define MAX_FRAME_LEN 264 static nfc_device *pnd; +static nfc_context *context; static void stop_dep_communication(int sig) { @@ -73,7 +74,6 @@ main(int argc, const char *argv[]) exit(EXIT_FAILURE); } - nfc_context *context; nfc_init(&context); if (context == NULL) { ERR("Unable to init libnfc (malloc)"); diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index b91ec18..1aef22d 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -48,6 +48,7 @@ #define MAX_FRAME_LEN 264 static nfc_device *pnd; +static nfc_context *context; static void stop_dep_communication(int sig) { @@ -72,7 +73,6 @@ main(int argc, const char *argv[]) exit(EXIT_FAILURE); } - nfc_context *context; nfc_init(&context); if (context == NULL) { ERR("Unable to init libnfc (malloc)"); diff --git a/examples/nfc-emulate-forum-tag2.c b/examples/nfc-emulate-forum-tag2.c index cdd430e..e008379 100644 --- a/examples/nfc-emulate-forum-tag2.c +++ b/examples/nfc-emulate-forum-tag2.c @@ -74,6 +74,7 @@ #include "utils/nfc-utils.h" static nfc_device *pnd; +static nfc_context *context; static void stop_emulation(int sig) @@ -186,7 +187,6 @@ main(int argc, char *argv[]) signal(SIGINT, stop_emulation); - nfc_context *context; nfc_init(&context); if (context == NULL) { ERR("Unable to init libnfc (malloc)"); diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 969848f..408ee3a 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -59,6 +59,7 @@ static uint8_t abtRecv[MAX_FRAME_LEN]; static int szRecvBits; static nfc_device *pnd; +static nfc_context *context; // ISO14443A Anti-Collision response uint8_t abtAtqa[2] = { 0x04, 0x00 }; @@ -126,7 +127,6 @@ main(int argc, char *argv[]) signal(SIGINT, intr_hdlr); #endif - nfc_context *context; nfc_init(&context); if (context == NULL) { ERR("Unable to init libnfc (malloc)"); diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index 0f885bb..b5f29ee 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -52,6 +52,7 @@ #define MAX_DEVICE_COUNT 16 static nfc_device *pnd = NULL; +static nfc_context *context; static void stop_polling(int sig) { @@ -105,7 +106,6 @@ main(int argc, const char *argv[]) nfc_target nt; int res = 0; - nfc_context *context; nfc_init(&context); if (context == NULL) { ERR("Unable to init libnfc (malloc)"); diff --git a/utils/nfc-emulate-forum-tag4.c b/utils/nfc-emulate-forum-tag4.c index f604388..47335ba 100644 --- a/utils/nfc-emulate-forum-tag4.c +++ b/utils/nfc-emulate-forum-tag4.c @@ -71,6 +71,7 @@ #include "nfc-utils.h" static nfc_device *pnd; +static nfc_context *context; static bool quiet_output = false; // Version of the emulated type4 tag: static int type4v = 2; @@ -380,7 +381,6 @@ main(int argc, char *argv[]) } } - nfc_context *context; nfc_init(&context); if (context == NULL) { ERR("Unable to init libnfc (malloc)\n"); diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index 720ff6a..df4a543 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -60,6 +60,7 @@ #endif static nfc_device *pnd; +static nfc_context *context; static void print_usage(char *progname) @@ -198,7 +199,6 @@ main(int argc, char *argv[]) } } - nfc_context *context; nfc_init(&context); if (context == NULL) { ERR("Unable to init libnfc (malloc)\n"); From 3f5a3fd58ff9ef2e477a3c3203ac882fb3a4518a Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 00:30:38 +0100 Subject: [PATCH 10/43] nfc-read-forum-tag3 & nfc-relay-picc: remove unused variable Fix cppcheck warnings [utils/nfc-read-forum-tag3.c:281]: (style) Variable 'len' is assigned a value that is never used [utils/nfc-relay-picc.c:392]: (style) Variable 'res' is assigned a value that is never used --- utils/nfc-read-forum-tag3.c | 3 +-- utils/nfc-relay-picc.c | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index df4a543..7b60f8d 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -278,9 +278,8 @@ main(int argc, char *argv[]) uint8_t data[1024]; size_t data_len = sizeof(data); - int len; - if (0 >= (len = nfc_forum_tag_type3_check(pnd, nt, 0, 1, data, &data_len))) { + if (nfc_forum_tag_type3_check(pnd, nt, 0, 1, data, &data_len) <= 0) { nfc_perror(pnd, "nfc_forum_tag_type3_check"); fclose(ndef_stream); nfc_close(pnd); diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 90221c8..1659c4d 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -389,8 +389,7 @@ main(int argc, char *argv[]) } printf("NFC emulator device: %s opened\n", nfc_device_get_name(pndTarget)); - int res; - if ((res = nfc_target_init(pndTarget, &ntEmulatedTarget, abtCapdu, sizeof(abtCapdu), 0)) < 0) { + if (nfc_target_init(pndTarget, &ntEmulatedTarget, abtCapdu, sizeof(abtCapdu), 0) < 0) { ERR("%s", "Initialization of NFC emulator failed"); if (!target_only_mode) { nfc_close(pndInitiator); From 47693921576e8ec88255ca131ebd89b520b9cb5c Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 00:35:36 +0100 Subject: [PATCH 11/43] Fix cppcheck style: scope of variable can be reduced Fix the following cppcheck warnings: [libnfc/drivers/pn532_uart.c:492]: (style) The scope of the variable 'res' can be reduced [libnfc/chips/pn53x.c:320]: (style) The scope of the variable 'res' can be reduced [libnfc/chips/pn53x.c:616]: (style) The scope of the variable 'res' can be reduced [libnfc/nfc.c:356]: (style) The scope of the variable '_device_found' can be reduced [utils/nfc-mfclassic.c:178]: (style) The scope of the variable 'key_index' can be reduced --- libnfc/chips/pn53x.c | 4 ++-- libnfc/drivers/pn532_uart.c | 2 +- libnfc/nfc.c | 3 +-- utils/nfc-mfclassic.c | 3 +-- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 362cb4a..c961b46 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -317,9 +317,9 @@ pn53x_set_parameters(struct nfc_device *pnd, const uint8_t ui8Parameter, const b int pn53x_set_tx_bits(struct nfc_device *pnd, const uint8_t ui8Bits) { - int res = 0; // Test if we need to update the transmission bits register setting if (CHIP_DATA(pnd)->ui8TxBits != ui8Bits) { + int res = 0; // Set the amount of transmission bits in the PN53X chip register if ((res = pn53x_write_register(pnd, PN53X_REG_CIU_BitFraming, SYMBOL_TX_LAST_BITS, ui8Bits)) < 0) return res; @@ -613,8 +613,8 @@ pn53x_WriteRegister(struct nfc_device *pnd, const uint16_t ui16RegisterAddress, int pn53x_write_register(struct nfc_device *pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8SymbolMask, const uint8_t ui8Value) { - int res = 0; if ((ui16RegisterAddress < PN53X_CACHE_REGISTER_MIN_ADDRESS) || (ui16RegisterAddress > PN53X_CACHE_REGISTER_MAX_ADDRESS)) { + int res = 0; // Direct write if (ui8SymbolMask != 0xff) { uint8_t ui8CurrentValue; diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 4cbb9a5..f7b8867 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -489,8 +489,8 @@ error: int pn532_uart_ack(nfc_device *pnd) { - int res = 0; if (POWERDOWN == CHIP_DATA(pnd)->power_mode) { + int res = 0; if ((res = pn532_uart_wakeup(pnd)) < 0) { return res; } diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 6fabdf9..dd511a1 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -353,9 +353,8 @@ nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], const size_ const struct nfc_driver_list *pndl = nfc_drivers; while (pndl) { const struct nfc_driver *ndr = pndl->driver; - size_t _device_found = 0; if ((ndr->scan_type == NOT_INTRUSIVE) || ((context->allow_intrusive_scan) && (ndr->scan_type == INTRUSIVE))) { - _device_found = ndr->scan(context, connstrings + (device_found), connstrings_len - (device_found)); + size_t _device_found = ndr->scan(context, connstrings + (device_found), connstrings_len - (device_found)); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%ld device(s) found using %s driver", (unsigned long) _device_found, ndr->name); if (_device_found > 0) { device_found += _device_found; diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index 081be3b..a5ebfe4 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -175,7 +175,6 @@ authenticate(uint32_t uiBlock) { mifare_cmd mc; uint32_t uiTrailerBlock; - size_t key_index; // Set the authentication information (uid) memcpy(mp.mpa.abtAuthUid, nt.nti.nai.abtUid + nt.nti.nai.szUidLen - 4, 4); @@ -200,7 +199,7 @@ authenticate(uint32_t uiBlock) return true; } else { // Try to guess the right key - for (key_index = 0; key_index < num_keys; key_index++) { + for (size_t key_index = 0; key_index < num_keys; key_index++) { memcpy(mp.mpa.abtKey, keys + (key_index * 6), 6); if (nfc_initiator_mifare_cmd(pnd, mc, uiBlock, &mp)) { if (bUseKeyA) From d577fda412bc8419191b464ffaefe57285e908e2 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 00:52:08 +0100 Subject: [PATCH 12/43] Fix cppcheck warning "scanf without field width limits can crash with huge input data" Fix following warnings: [examples/pn53x-tamashell.c:162]: (warning) scanf without field width limits can crash with huge input data [libnfc/drivers/acr122_pcsc.c:261]: (warning) scanf without field width limits can crash with huge input data [libnfc/drivers/acr122s.c:441]: (warning) scanf without field width limits can crash with huge input data [libnfc/drivers/arygon.c:210]: (warning) scanf without field width limits can crash with huge input data [libnfc/drivers/arygon.c:522]: (warning) scanf without field width limits can crash with huge input data [libnfc/drivers/pn532_uart.c:187]: (warning) scanf without field width limits can crash with huge input data [utils/nfc-relay-picc.c:176]: (warning) scanf without field width limits can crash with huge input data --- examples/pn53x-tamashell.c | 2 +- libnfc/drivers/acr122_pcsc.c | 2 +- libnfc/drivers/acr122s.c | 2 +- libnfc/drivers/arygon.c | 6 ++++-- libnfc/drivers/pn532_uart.c | 2 +- utils/nfc-relay-picc.c | 2 +- 6 files changed, 9 insertions(+), 7 deletions(-) diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index eac9993..42a06ff 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -159,7 +159,7 @@ int main(int argc, const char *argv[]) while (isspace(cmd[offset])) { offset++; } - sscanf(cmd + offset, "%d", &s); + sscanf(cmd + offset, "%10d", &s); printf("Pause for %i msecs\n", s); if (s > 0) { sleep(s * SUSP_TIME); diff --git a/libnfc/drivers/acr122_pcsc.c b/libnfc/drivers/acr122_pcsc.c index e24e425..6a9151a 100644 --- a/libnfc/drivers/acr122_pcsc.c +++ b/libnfc/drivers/acr122_pcsc.c @@ -258,7 +258,7 @@ acr122_pcsc_open(const nfc_context *context, const nfc_connstring connstring) if (strlen(ndd.pcsc_device_name) < 5) { // We can assume it's a reader ID as pcsc_name always ends with "NN NN" // Device was not specified, only ID, retrieve it size_t index; - if (sscanf(ndd.pcsc_device_name, "%lu", &index) != 1) + if (sscanf(ndd.pcsc_device_name, "%4lu", &index) != 1) return NULL; nfc_connstring *ncs = malloc(sizeof(nfc_connstring) * (index + 1)); if (!ncs) { diff --git a/libnfc/drivers/acr122s.c b/libnfc/drivers/acr122s.c index 0b19d32..c84a8da 100644 --- a/libnfc/drivers/acr122s.c +++ b/libnfc/drivers/acr122s.c @@ -438,7 +438,7 @@ acr122s_connstring_decode(const nfc_connstring connstring, struct acr122s_descri return 2; } unsigned long speed; - if (sscanf(speed_s, "%lu", &speed) != 1) { + if (sscanf(speed_s, "%10lu", &speed) != 1) { // speed_s is not a number free(cs); return 2; diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index a788807..315fc0b 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -207,7 +207,7 @@ arygon_connstring_decode(const nfc_connstring connstring, struct arygon_descript return 2; } unsigned long speed; - if (sscanf(speed_s, "%lu", &speed) != 1) { + if (sscanf(speed_s, "%10lu", &speed) != 1) { // speed_s is not a number free(cs); return 2; @@ -519,7 +519,9 @@ arygon_firmware(nfc_device *pnd, char *str) if (0 == memcmp(abtRx, arygon_error_none, 6)) { uint8_t *p = abtRx + 6; unsigned int szData; - sscanf((const char *)p, "%02x%s", &szData, p); + sscanf((const char *)p, "%02x%9s", &szData, p); + if (szData > 9) + szData = 9; memcpy(str, p, szData); *(str + szData) = '\0'; } diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index f7b8867..4ae4380 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -184,7 +184,7 @@ pn532_connstring_decode(const nfc_connstring connstring, struct pn532_uart_descr return 2; } unsigned long speed; - if (sscanf(speed_s, "%lu", &speed) != 1) { + if (sscanf(speed_s, "%10lu", &speed) != 1) { // speed_s is not a number free(cs); return 2; diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 1659c4d..43988e5 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -173,7 +173,7 @@ main(int argc, char *argv[]) printf("INFO: %s\n", "Swapping devices."); swap_devices = true; } else if (0 == strcmp(argv[arg], "-n")) { - if (++arg == argc || (sscanf(argv[arg], "%i", &waiting_time) < 1)) { + if (++arg == argc || (sscanf(argv[arg], "%10i", &waiting_time) < 1)) { ERR("Missing or wrong waiting time value: %s.", argv[arg]); print_usage(argv); exit(EXIT_FAILURE); From c37d1742c3839e08b2595e21855cccf7090003e3 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 00:54:49 +0100 Subject: [PATCH 13/43] Fix cppcheck style: scope of variable can be reduced Fix the following cppcheck warnings: [libnfc/chips/pn53x.c:617]: (style) The scope of the variable 'res' can be reduced --- libnfc/chips/pn53x.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index c961b46..9b5946c 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -614,9 +614,9 @@ int pn53x_write_register(struct nfc_device *pnd, const uint16_t ui16RegisterAddress, const uint8_t ui8SymbolMask, const uint8_t ui8Value) { if ((ui16RegisterAddress < PN53X_CACHE_REGISTER_MIN_ADDRESS) || (ui16RegisterAddress > PN53X_CACHE_REGISTER_MAX_ADDRESS)) { - int res = 0; // Direct write if (ui8SymbolMask != 0xff) { + int res = 0; uint8_t ui8CurrentValue; if ((res = pn53x_read_register(pnd, ui16RegisterAddress, &ui8CurrentValue)) < 0) return res; From 9632f3ef3585ab50217ff8107d50a6971318a7af Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 01:09:57 +0100 Subject: [PATCH 14/43] Edit HACKING & introduce cppcheck Still a few todo according to cppcheck: [examples/pn53x-tamashell.c:165]: (style) Found obsolete function 'usleep'. It is recommended that new applications use the 'nanosleep' or 'setitimer' function [libnfc/buses/uart_posix.c:343]: (portability) Found non reentrant function 'readdir'. For threadsafe applications it is recommended to use the reentrant replacement function 'readdir_r' [libnfc/drivers/acr122_pcsc.c:206]: [libnfc/drivers/acr122_pcsc.c:219]: [libnfc/drivers/acr122s.c:412]: [libnfc/drivers/acr122s.c:425]: [libnfc/drivers/acr122s.c:434]: [libnfc/drivers/arygon.c:181]: [libnfc/drivers/arygon.c:194]: [libnfc/drivers/arygon.c:203]: [libnfc/drivers/pn532_uart.c:158]: [libnfc/drivers/pn532_uart.c:171]: [libnfc/drivers/pn532_uart.c:180]: (portability) Found non reentrant function 'strtok'. For threadsafe applications it is recommended to use the reentrant replacement function 'strtok_r' --- HACKING | 3 +++ 1 file changed, 3 insertions(+) diff --git a/HACKING b/HACKING index b6b8968..1ee667d 100644 --- a/HACKING +++ b/HACKING @@ -36,6 +36,9 @@ Here are some directions to get you started: $ scan-build ./configure $ make clean $ scan-build make + And by using cppcheck: + $ cppcheck --quiet --enable=all --std=c99 --std=posix \ + -I include -I libnfc -I libnfc/buses . 2.3 When Debianizing $ lintian --info --display-info --display-experimental *deb or (shorter version) From f152aaffa35bbea27f7b01e555d8006db9788d19 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 10:54:54 +0100 Subject: [PATCH 15/43] Edit HACKING: cppcheck v1.58 --- HACKING | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/HACKING b/HACKING index 1ee667d..e457137 100644 --- a/HACKING +++ b/HACKING @@ -23,22 +23,31 @@ Here are some directions to get you started: 2.1 When using autotools $ autoreconf -Wall -vis 2.2 When compiling - $ export CFLAGS="-Wall -g -O2 -Wextra -pipe -funsigned-char -fstrict-aliasing \ - -Wchar-subscripts -Wundef -Wshadow -Wcast-align -Wwrite-strings -Wunused \ - -Wuninitialized -Wpointer-arith -Wredundant-decls -Winline -Wformat \ - -Wformat-security -Wswitch-enum -Winit-self -Wmissing-include-dirs \ - -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition \ - -Wbad-function-cast -Wnested-externs -Wmissing-declarations" - $ ./configure - $ make clean - $ make - You can chase even more issues by using clang: - $ scan-build ./configure - $ make clean - $ scan-build make - And by using cppcheck: - $ cppcheck --quiet --enable=all --std=c99 --std=posix \ - -I include -I libnfc -I libnfc/buses . + 2.2.1 Using extra flags: + $ export CFLAGS="-Wall -g -O2 -Wextra -pipe -funsigned-char -fstrict-aliasing \ + -Wchar-subscripts -Wundef -Wshadow -Wcast-align -Wwrite-strings -Wunused \ + -Wuninitialized -Wpointer-arith -Wredundant-decls -Winline -Wformat \ + -Wformat-security -Wswitch-enum -Winit-self -Wmissing-include-dirs \ + -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition \ + -Wbad-function-cast -Wnested-externs -Wmissing-declarations" + $ ./configure + $ make clean + $ make + 2.2.2 Using clang: + $ scan-build ./configure + $ make clean + $ scan-build make + 2.2.3 Using cppcheck (v1.58 or higher): + $ cppcheck --quiet \ + -I include -I libnfc -I libnfc/buses -I libnfc/chips -I libnfc/drivers \ + --check-config . + $ cppcheck --quiet --enable=all --std=posix --std=c99 \ + -I include -I libnfc -I libnfc/buses -I libnfc/chips -I libnfc/drivers \ + -DLOG -D__linux__ \ + -DDRIVER_PN53X_USB_ENABLED -DDRIVER_ACR122_PCSC_ENABLED \ + -DDRIVER_ACR122_USB_ENABLED -DDRIVER_ACR122S_ENABLED \ + -DDRIVER_PN532_UART_ENABLED -DDRIVER_ARYGON_ENABLED \ + --force . 2.3 When Debianizing $ lintian --info --display-info --display-experimental *deb or (shorter version) From 0aa1ff309ea8f25927515422af34c1ab9c229b3d Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 10:56:12 +0100 Subject: [PATCH 16/43] Fix cppcheck warning "Variable is assigned a value that is never used" --- libnfc/buses/uart_posix.c | 2 +- test/test_dep_passive.c | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index 7b687c1..ced1bc6 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -127,7 +127,7 @@ uart_flush_input(serial_port sp) } char *rx = malloc(available_bytes_count); // There is something available, read the data - res = read(UART_DATA(sp)->fd, rx, available_bytes_count); + read(UART_DATA(sp)->fd, rx, available_bytes_count); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%d bytes have eatten.", available_bytes_count); free(rx); } diff --git a/test/test_dep_passive.c b/test/test_dep_passive.c index 6f093e9..71d7e3a 100644 --- a/test/test_dep_passive.c +++ b/test/test_dep_passive.c @@ -105,7 +105,6 @@ target_thread(void *arg) // Second pass res = nfc_target_receive_bytes(device, abtRx, sizeof(abtRx), 500); cut_assert_operator_int(res, > , 0, cut_message("Can't receive bytes from initiator: %s", nfc_strerror(device))); - szRx = (size_t) res; cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, res, cut_message("Invalid received data")); if (res <= 0) { thread_res = -1; return (void *) thread_res; } @@ -117,7 +116,6 @@ target_thread(void *arg) // Third pass res = nfc_target_receive_bytes(device, abtRx, sizeof(abtRx), 500); cut_assert_operator_int(res, > , 0, cut_message("Can't receive bytes from initiator: %s", nfc_strerror(device))); - szRx = (size_t) res; cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, res, cut_message("Invalid received data")); if (res <= 0) { thread_res = -1; return (void *) thread_res; } @@ -129,7 +127,6 @@ target_thread(void *arg) // Fourth pass res = nfc_target_receive_bytes(device, abtRx, sizeof(abtRx), 500); cut_assert_operator_int(res, > , 0, cut_message("Can't receive bytes from initiator: %s", nfc_strerror(device))); - szRx = (size_t) res; cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, res, cut_message("Invalid received data")); if (res <= 0) { thread_res = -1; return (void *) thread_res; } From f0d589614034326309aa6795b19191960bb4a3a5 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 11:02:47 +0100 Subject: [PATCH 17/43] Fix cppcheck style: "The scope of the variable can be reduced" --- libnfc/target-subr.c | 2 +- utils/nfc-mfclassic.c | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/libnfc/target-subr.c b/libnfc/target-subr.c index 9b3640f..3765ecb 100644 --- a/libnfc/target-subr.c +++ b/libnfc/target-subr.c @@ -496,7 +496,6 @@ sprint_nfc_jewel_info(char *dst, const nfc_jewel_info nji, bool verbose) void sprint_nfc_iso14443b_info(char *dst, const nfc_iso14443b_info nbi, bool verbose) { - const int iMaxFrameSizes[] = { 16, 24, 32, 40, 48, 64, 96, 128, 256 }; dst += sprintf(dst, " PUPI: "); dst += sprint_hex(dst, nbi.abtPupi, 4); dst += sprintf(dst, " Application Data: "); @@ -533,6 +532,7 @@ sprint_nfc_iso14443b_info(char *dst, const nfc_iso14443b_info nbi, bool verbose) dst += sprintf(dst, " * ERROR unknown value\n"); } if ((nbi.abtProtocolInfo[1] & 0xf0) <= 0x80) { + const int iMaxFrameSizes[] = { 16, 24, 32, 40, 48, 64, 96, 128, 256 }; dst += sprintf(dst, "* Maximum frame sizes: %d bytes\n", iMaxFrameSizes[((nbi.abtProtocolInfo[1] & 0xf0) >> 4)]); } if ((nbi.abtProtocolInfo[1] & 0x0f) == PI_ISO14443_4_SUPPORTED) { diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index a5ebfe4..938aef7 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -430,8 +430,6 @@ main(int argc, const char *argv[]) { action_t atAction = ACTION_USAGE; uint8_t *pbtUID; - FILE *pfKeys = NULL; - FILE *pfDump = NULL; int unlock = 0; if (argc < 2) { @@ -469,7 +467,7 @@ main(int argc, const char *argv[]) exit(EXIT_FAILURE); } if (bUseKeyFile) { - pfKeys = fopen(argv[4], "rb"); + FILE *pfKeys = fopen(argv[4], "rb"); if (pfKeys == NULL) { printf("Could not open keys file: %s\n", argv[4]); exit(EXIT_FAILURE); @@ -485,11 +483,12 @@ main(int argc, const char *argv[]) if (atAction == ACTION_READ) { memset(&mtDump, 0x00, sizeof(mtDump)); } else { - pfDump = fopen(argv[3], "rb"); + FILE *pfDump = fopen(argv[3], "rb"); if (pfDump == NULL) { printf("Could not open dump file: %s\n", argv[3]); exit(EXIT_FAILURE); + } if (fread(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) { @@ -578,7 +577,7 @@ main(int argc, const char *argv[]) if (read_card(unlock)) { printf("Writing data to file: %s ...", argv[3]); fflush(stdout); - pfDump = fopen(argv[3], "wb"); + FILE *pfDump = fopen(argv[3], "wb"); if (pfDump == NULL) { printf("Could not open dump file: %s\n", argv[3]); nfc_close(pnd); From 658ec4585a8d197481860f050e1a3035078c9e85 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 11:42:35 +0100 Subject: [PATCH 18/43] Fix cppcheck warning "scanf without field width limits can crash with huge input data" --- libnfc/drivers/acr122_usb.c | 4 +++- libnfc/drivers/pn53x_usb.c | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c index c39cac6..db57d76 100644 --- a/libnfc/drivers/acr122_usb.c +++ b/libnfc/drivers/acr122_usb.c @@ -372,7 +372,9 @@ acr122_usb_connstring_decode(const nfc_connstring connstring, struct acr122_usb_ driver_name[0] = '\0'; - int res = sscanf(connstring, "%[^:]:%[^:]:%[^:]", driver_name, dirname, filename); + char format[32]; + snprintf(format, sizeof(format), "%%%i[^:]:%%%i[^:]:%%%i[^:]", n - 1, n - 1, n - 1); + int res = sscanf(connstring, format, driver_name, dirname, filename); if (!res || ((0 != strcmp(driver_name, ACR122_USB_DRIVER_NAME)) && (0 != strcmp(driver_name, "usb")))) { // Driver name does not match. diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index f440b5a..9f6efc0 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -257,7 +257,9 @@ pn53x_usb_connstring_decode(const nfc_connstring connstring, struct pn53x_usb_de driver_name[0] = '\0'; - int res = sscanf(connstring, "%[^:]:%[^:]:%[^:]", driver_name, dirname, filename); + char format[32]; + snprintf(format, sizeof(format), "%%%i[^:]:%%%i[^:]:%%%i[^:]", n - 1, n - 1, n - 1); + int res = sscanf(connstring, format, driver_name, dirname, filename); if (!res || ((0 != strcmp(driver_name, PN53X_USB_DRIVER_NAME)) && (0 != strcmp(driver_name, "usb")))) { // Driver name does not match. From a3facbdc7d37e626c0feebab9bb5428cd47403e1 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 12:17:33 +0100 Subject: [PATCH 19/43] sprint_nfc_target() => snprint_nfc_target() And all sub-functions in target_subr.c --- libnfc/nfc.c | 2 +- libnfc/target-subr.c | 364 ++++++++++++++++++++++--------------------- libnfc/target-subr.h | 20 +-- 3 files changed, 197 insertions(+), 189 deletions(-) diff --git a/libnfc/nfc.c b/libnfc/nfc.c index dd511a1..fc1ed9f 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -1292,6 +1292,6 @@ str_nfc_target(char **buf, const nfc_target nt, bool verbose) if (! *buf) return NFC_ESOFT; (*buf)[0] = '\0'; - sprint_nfc_target(*buf, nt, verbose); + snprint_nfc_target(*buf, 4096, nt, verbose); return strlen(*buf); } diff --git a/libnfc/target-subr.c b/libnfc/target-subr.c index 3765ecb..cdc9028 100644 --- a/libnfc/target-subr.c +++ b/libnfc/target-subr.c @@ -117,15 +117,14 @@ struct card_sak const_cs[] = { }; int -sprint_hex(char *dst, const uint8_t *pbtData, const size_t szBytes) +snprint_hex(char *dst, size_t size, const uint8_t *pbtData, const size_t szBytes) { size_t szPos; - - int res = 0; + size_t res = 0; for (szPos = 0; szPos < szBytes; szPos++) { - res += sprintf(dst + res, "%02x ", pbtData[szPos]); + res += snprintf(dst + res, size - res, "%02x ", pbtData[szPos]); } - res += sprintf(dst + res, "\n"); + res += snprintf(dst + res, size - res, "\n"); return res; } @@ -134,267 +133,268 @@ sprint_hex(char *dst, const uint8_t *pbtData, const size_t szBytes) #define SAK_ISO18092_COMPLIANT 0x40 void -sprint_nfc_iso14443a_info(char *dst, const nfc_iso14443a_info nai, bool verbose) +snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, bool verbose) { - dst += sprintf(dst, " ATQA (SENS_RES): "); - dst += sprint_hex(dst, nai.abtAtqa, 2); + int off = 0; + off += snprintf(dst + off, size - off, " ATQA (SENS_RES): "); + off += snprint_hex(dst + off, size - off, nai.abtAtqa, 2); if (verbose) { - dst += sprintf(dst, "* UID size: "); + off += snprintf(dst + off, size - off, "* UID size: "); switch ((nai.abtAtqa[1] & 0xc0) >> 6) { case 0: - dst += sprintf(dst, "single\n"); + off += snprintf(dst + off, size - off, "single\n"); break; case 1: - dst += sprintf(dst, "double\n"); + off += snprintf(dst + off, size - off, "double\n"); break; case 2: - dst += sprintf(dst, "triple\n"); + off += snprintf(dst + off, size - off, "triple\n"); break; case 3: - dst += sprintf(dst, "RFU\n"); + off += snprintf(dst + off, size - off, "RFU\n"); break; } - dst += sprintf(dst, "* bit frame anticollision "); + off += snprintf(dst + off, size - off, "* bit frame anticollision "); switch (nai.abtAtqa[1] & 0x1f) { case 0x01: case 0x02: case 0x04: case 0x08: case 0x10: - dst += sprintf(dst, "supported\n"); + off += snprintf(dst + off, size - off, "supported\n"); break; default: - dst += sprintf(dst, "not supported\n"); + off += snprintf(dst + off, size - off, "not supported\n"); break; } } - dst += sprintf(dst, " UID (NFCID%c): ", (nai.abtUid[0] == 0x08 ? '3' : '1')); - dst += sprint_hex(dst, nai.abtUid, nai.szUidLen); + off += snprintf(dst + off, size - off, " UID (NFCID%c): ", (nai.abtUid[0] == 0x08 ? '3' : '1')); + off += snprint_hex(dst + off, size - off, nai.abtUid, nai.szUidLen); if (verbose) { if (nai.abtUid[0] == 0x08) { - dst += sprintf(dst, "* Random UID\n"); + off += snprintf(dst + off, size - off, "* Random UID\n"); } } - dst += sprintf(dst, " SAK (SEL_RES): "); - dst += sprint_hex(dst, &nai.btSak, 1); + off += snprintf(dst + off, size - off, " SAK (SEL_RES): "); + off += snprint_hex(dst + off, size - off, &nai.btSak, 1); if (verbose) { if (nai.btSak & SAK_UID_NOT_COMPLETE) { - dst += sprintf(dst, "* Warning! Cascade bit set: UID not complete\n"); + off += snprintf(dst + off, size - off, "* Warning! Cascade bit set: UID not complete\n"); } if (nai.btSak & SAK_ISO14443_4_COMPLIANT) { - dst += sprintf(dst, "* Compliant with ISO/IEC 14443-4\n"); + off += snprintf(dst + off, size - off, "* Compliant with ISO/IEC 14443-4\n"); } else { - dst += sprintf(dst, "* Not compliant with ISO/IEC 14443-4\n"); + off += snprintf(dst + off, size - off, "* Not compliant with ISO/IEC 14443-4\n"); } if (nai.btSak & SAK_ISO18092_COMPLIANT) { - dst += sprintf(dst, "* Compliant with ISO/IEC 18092\n"); + off += snprintf(dst + off, size - off, "* Compliant with ISO/IEC 18092\n"); } else { - dst += sprintf(dst, "* Not compliant with ISO/IEC 18092\n"); + off += snprintf(dst + off, size - off, "* Not compliant with ISO/IEC 18092\n"); } } if (nai.szAtsLen) { - dst += sprintf(dst, " ATS: "); - dst += sprint_hex(dst, nai.abtAts, nai.szAtsLen); + off += snprintf(dst + off, size - off, " ATS: "); + off += snprint_hex(dst + off, size - off, nai.abtAts, nai.szAtsLen); } if (nai.szAtsLen && verbose) { // Decode ATS according to ISO/IEC 14443-4 (5.2 Answer to select) const int iMaxFrameSizes[] = { 16, 24, 32, 40, 48, 64, 96, 128, 256 }; - dst += sprintf(dst, "* Max Frame Size accepted by PICC: %d bytes\n", iMaxFrameSizes[nai.abtAts[0] & 0x0F]); + off += snprintf(dst + off, size - off, "* Max Frame Size accepted by PICC: %d bytes\n", iMaxFrameSizes[nai.abtAts[0] & 0x0F]); size_t offset = 1; if (nai.abtAts[0] & 0x10) { // TA(1) present uint8_t TA = nai.abtAts[offset]; offset++; - dst += sprintf(dst, "* Bit Rate Capability:\n"); + off += snprintf(dst + off, size - off, "* Bit Rate Capability:\n"); if (TA == 0) { - dst += sprintf(dst, " * PICC supports only 106 kbits/s in both directions\n"); + off += snprintf(dst + off, size - off, " * PICC supports only 106 kbits/s in both directions\n"); } if (TA & 1 << 7) { - dst += sprintf(dst, " * Same bitrate in both directions mandatory\n"); + off += snprintf(dst + off, size - off, " * Same bitrate in both directions mandatory\n"); } if (TA & 1 << 4) { - dst += sprintf(dst, " * PICC to PCD, DS=2, bitrate 212 kbits/s supported\n"); + off += snprintf(dst + off, size - off, " * PICC to PCD, DS=2, bitrate 212 kbits/s supported\n"); } if (TA & 1 << 5) { - dst += sprintf(dst, " * PICC to PCD, DS=4, bitrate 424 kbits/s supported\n"); + off += snprintf(dst + off, size - off, " * PICC to PCD, DS=4, bitrate 424 kbits/s supported\n"); } if (TA & 1 << 6) { - dst += sprintf(dst, " * PICC to PCD, DS=8, bitrate 847 kbits/s supported\n"); + off += snprintf(dst + off, size - off, " * PICC to PCD, DS=8, bitrate 847 kbits/s supported\n"); } if (TA & 1 << 0) { - dst += sprintf(dst, " * PCD to PICC, DR=2, bitrate 212 kbits/s supported\n"); + off += snprintf(dst + off, size - off, " * PCD to PICC, DR=2, bitrate 212 kbits/s supported\n"); } if (TA & 1 << 1) { - dst += sprintf(dst, " * PCD to PICC, DR=4, bitrate 424 kbits/s supported\n"); + off += snprintf(dst + off, size - off, " * PCD to PICC, DR=4, bitrate 424 kbits/s supported\n"); } if (TA & 1 << 2) { - dst += sprintf(dst, " * PCD to PICC, DR=8, bitrate 847 kbits/s supported\n"); + off += snprintf(dst + off, size - off, " * PCD to PICC, DR=8, bitrate 847 kbits/s supported\n"); } if (TA & 1 << 3) { - dst += sprintf(dst, " * ERROR unknown value\n"); + off += snprintf(dst + off, size - off, " * ERROR unknown value\n"); } } if (nai.abtAts[0] & 0x20) { // TB(1) present uint8_t TB = nai.abtAts[offset]; offset++; - dst += sprintf(dst, "* Frame Waiting Time: %.4g ms\n", 256.0 * 16.0 * (1 << ((TB & 0xf0) >> 4)) / 13560.0); + off += snprintf(dst + off, size - off, "* Frame Waiting Time: %.4g ms\n", 256.0 * 16.0 * (1 << ((TB & 0xf0) >> 4)) / 13560.0); if ((TB & 0x0f) == 0) { - dst += sprintf(dst, "* No Start-up Frame Guard Time required\n"); + off += snprintf(dst + off, size - off, "* No Start-up Frame Guard Time required\n"); } else { - dst += sprintf(dst, "* Start-up Frame Guard Time: %.4g ms\n", 256.0 * 16.0 * (1 << (TB & 0x0f)) / 13560.0); + off += snprintf(dst + off, size - off, "* Start-up Frame Guard Time: %.4g ms\n", 256.0 * 16.0 * (1 << (TB & 0x0f)) / 13560.0); } } if (nai.abtAts[0] & 0x40) { // TC(1) present uint8_t TC = nai.abtAts[offset]; offset++; if (TC & 0x1) { - dst += sprintf(dst, "* Node ADdress supported\n"); + off += snprintf(dst + off, size - off, "* Node ADdress supported\n"); } else { - dst += sprintf(dst, "* Node ADdress not supported\n"); + off += snprintf(dst + off, size - off, "* Node ADdress not supported\n"); } if (TC & 0x2) { - dst += sprintf(dst, "* Card IDentifier supported\n"); + off += snprintf(dst + off, size - off, "* Card IDentifier supported\n"); } else { - dst += sprintf(dst, "* Card IDentifier not supported\n"); + off += snprintf(dst + off, size - off, "* Card IDentifier not supported\n"); } } if (nai.szAtsLen > offset) { - dst += sprintf(dst, "* Historical bytes Tk: "); - dst += sprint_hex(dst, nai.abtAts + offset, (nai.szAtsLen - offset)); + off += snprintf(dst + off, size - off, "* Historical bytes Tk: "); + off += snprint_hex(dst + off, size - off, nai.abtAts + offset, (nai.szAtsLen - offset)); uint8_t CIB = nai.abtAts[offset]; offset++; if (CIB != 0x00 && CIB != 0x10 && (CIB & 0xf0) != 0x80) { - dst += sprintf(dst, " * Proprietary format\n"); + off += snprintf(dst + off, size - off, " * Proprietary format\n"); if (CIB == 0xc1) { - dst += sprintf(dst, " * Tag byte: Mifare or virtual cards of various types\n"); + off += snprintf(dst + off, size - off, " * Tag byte: Mifare or virtual cards of various types\n"); uint8_t L = nai.abtAts[offset]; offset++; if (L != (nai.szAtsLen - offset)) { - dst += sprintf(dst, " * Warning: Type Identification Coding length (%i)", L); - dst += sprintf(dst, " not matching Tk length (%zi)\n", (nai.szAtsLen - offset)); + off += snprintf(dst + off, size - off, " * Warning: Type Identification Coding length (%i)", L); + off += snprintf(dst + off, size - off, " not matching Tk length (%zi)\n", (nai.szAtsLen - offset)); } if ((nai.szAtsLen - offset - 2) > 0) { // Omit 2 CRC bytes uint8_t CTC = nai.abtAts[offset]; offset++; - dst += sprintf(dst, " * Chip Type: "); + off += snprintf(dst + off, size - off, " * Chip Type: "); switch (CTC & 0xf0) { case 0x00: - dst += sprintf(dst, "(Multiple) Virtual Cards\n"); + off += snprintf(dst + off, size - off, "(Multiple) Virtual Cards\n"); break; case 0x10: - dst += sprintf(dst, "Mifare DESFire\n"); + off += snprintf(dst + off, size - off, "Mifare DESFire\n"); break; case 0x20: - dst += sprintf(dst, "Mifare Plus\n"); + off += snprintf(dst + off, size - off, "Mifare Plus\n"); break; default: - dst += sprintf(dst, "RFU\n"); + off += snprintf(dst + off, size - off, "RFU\n"); break; } - dst += sprintf(dst, " * Memory size: "); + off += snprintf(dst + off, size - off, " * Memory size: "); switch (CTC & 0x0f) { case 0x00: - dst += sprintf(dst, "<1 kbyte\n"); + off += snprintf(dst + off, size - off, "<1 kbyte\n"); break; case 0x01: - dst += sprintf(dst, "1 kbyte\n"); + off += snprintf(dst + off, size - off, "1 kbyte\n"); break; case 0x02: - dst += sprintf(dst, "2 kbyte\n"); + off += snprintf(dst + off, size - off, "2 kbyte\n"); break; case 0x03: - dst += sprintf(dst, "4 kbyte\n"); + off += snprintf(dst + off, size - off, "4 kbyte\n"); break; case 0x04: - dst += sprintf(dst, "8 kbyte\n"); + off += snprintf(dst + off, size - off, "8 kbyte\n"); break; case 0x0f: - dst += sprintf(dst, "Unspecified\n"); + off += snprintf(dst + off, size - off, "Unspecified\n"); break; default: - dst += sprintf(dst, "RFU\n"); + off += snprintf(dst + off, size - off, "RFU\n"); break; } } if ((nai.szAtsLen - offset) > 0) { // Omit 2 CRC bytes uint8_t CVC = nai.abtAts[offset]; offset++; - dst += sprintf(dst, " * Chip Status: "); + off += snprintf(dst + off, size - off, " * Chip Status: "); switch (CVC & 0xf0) { case 0x00: - dst += sprintf(dst, "Engineering sample\n"); + off += snprintf(dst + off, size - off, "Engineering sample\n"); break; case 0x20: - dst += sprintf(dst, "Released\n"); + off += snprintf(dst + off, size - off, "Released\n"); break; default: - dst += sprintf(dst, "RFU\n"); + off += snprintf(dst + off, size - off, "RFU\n"); break; } - dst += sprintf(dst, " * Chip Generation: "); + off += snprintf(dst + off, size - off, " * Chip Generation: "); switch (CVC & 0x0f) { case 0x00: - dst += sprintf(dst, "Generation 1\n"); + off += snprintf(dst + off, size - off, "Generation 1\n"); break; case 0x01: - dst += sprintf(dst, "Generation 2\n"); + off += snprintf(dst + off, size - off, "Generation 2\n"); break; case 0x02: - dst += sprintf(dst, "Generation 3\n"); + off += snprintf(dst + off, size - off, "Generation 3\n"); break; case 0x0f: - dst += sprintf(dst, "Unspecified\n"); + off += snprintf(dst + off, size - off, "Unspecified\n"); break; default: - dst += sprintf(dst, "RFU\n"); + off += snprintf(dst + off, size - off, "RFU\n"); break; } } if ((nai.szAtsLen - offset) > 0) { // Omit 2 CRC bytes uint8_t VCS = nai.abtAts[offset]; offset++; - dst += sprintf(dst, " * Specifics (Virtual Card Selection):\n"); + off += snprintf(dst + off, size - off, " * Specifics (Virtual Card Selection):\n"); if ((VCS & 0x09) == 0x00) { - dst += sprintf(dst, " * Only VCSL supported\n"); + off += snprintf(dst + off, size - off, " * Only VCSL supported\n"); } else if ((VCS & 0x09) == 0x01) { - dst += sprintf(dst, " * VCS, VCSL and SVC supported\n"); + off += snprintf(dst + off, size - off, " * VCS, VCSL and SVC supported\n"); } if ((VCS & 0x0e) == 0x00) { - dst += sprintf(dst, " * SL1, SL2(?), SL3 supported\n"); + off += snprintf(dst + off, size - off, " * SL1, SL2(?), SL3 supported\n"); } else if ((VCS & 0x0e) == 0x02) { - dst += sprintf(dst, " * SL3 only card\n"); + off += snprintf(dst + off, size - off, " * SL3 only card\n"); } else if ((VCS & 0x0f) == 0x0e) { - dst += sprintf(dst, " * No VCS command supported\n"); + off += snprintf(dst + off, size - off, " * No VCS command supported\n"); } else if ((VCS & 0x0f) == 0x0f) { - dst += sprintf(dst, " * Unspecified\n"); + off += snprintf(dst + off, size - off, " * Unspecified\n"); } else { - dst += sprintf(dst, " * RFU\n"); + off += snprintf(dst + off, size - off, " * RFU\n"); } } } } else { if (CIB == 0x00) { - dst += sprintf(dst, " * Tk after 0x00 consist of optional consecutive COMPACT-TLV data objects\n"); - dst += sprintf(dst, " followed by a mandatory status indicator (the last three bytes, not in TLV)\n"); - dst += sprintf(dst, " See ISO/IEC 7816-4 8.1.1.3 for more info\n"); + off += snprintf(dst + off, size - off, " * Tk after 0x00 consist of optional consecutive COMPACT-TLV data objects\n"); + off += snprintf(dst + off, size - off, " followed by a mandatory status indicator (the last three bytes, not in TLV)\n"); + off += snprintf(dst + off, size - off, " See ISO/IEC 7816-4 8.1.1.3 for more info\n"); } if (CIB == 0x10) { - dst += sprintf(dst, " * DIR data reference: %02x\n", nai.abtAts[offset]); + off += snprintf(dst + off, size - off, " * DIR data reference: %02x\n", nai.abtAts[offset]); } if (CIB == 0x80) { if (nai.szAtsLen == offset) { - dst += sprintf(dst, " * No COMPACT-TLV objects found, no status found\n"); + off += snprintf(dst + off, size - off, " * No COMPACT-TLV objects found, no status found\n"); } else { - dst += sprintf(dst, " * Tk after 0x80 consist of optional consecutive COMPACT-TLV data objects;\n"); - dst += sprintf(dst, " the last data object may carry a status indicator of one, two or three bytes.\n"); - dst += sprintf(dst, " See ISO/IEC 7816-4 8.1.1.3 for more info\n"); + off += snprintf(dst + off, size - off, " * Tk after 0x80 consist of optional consecutive COMPACT-TLV data objects;\n"); + off += snprintf(dst + off, size - off, " the last data object may carry a status indicator of one, two or three bytes.\n"); + off += snprintf(dst + off, size - off, " See ISO/IEC 7816-4 8.1.1.3 for more info\n"); } } } } } if (verbose) { - dst += sprintf(dst, "\nFingerprinting based on MIFARE type Identification Procedure:\n"); // AN10833 + off += snprintf(dst + off, size - off, "\nFingerprinting based on MIFARE type Identification Procedure:\n"); // AN10833 uint16_t atqa = 0; uint8_t sak = 0; uint8_t i, j; @@ -409,7 +409,7 @@ sprint_nfc_iso14443a_info(char *dst, const nfc_iso14443a_info nai, bool verbose) for (j = 0; (j < sizeof(const_ca[i].saklist)) && (const_ca[i].saklist[j] >= 0); j++) { int sakindex = const_ca[i].saklist[j]; if ((sak & const_cs[sakindex].mask) == const_cs[sakindex].sak) { - dst += sprintf(dst, "* %s%s\n", const_ca[i].type, const_cs[sakindex].type); + off += snprintf(dst + off, size - off, "* %s%s\n", const_ca[i].type, const_cs[sakindex].type); found_possible_match = true; } } @@ -418,225 +418,233 @@ sprint_nfc_iso14443a_info(char *dst, const nfc_iso14443a_info nai, bool verbose) // Other matches not described in // AN10833 MIFARE Type Identification Procedure // but seen in the field: - dst += sprintf(dst, "Other possible matches based on ATQA & SAK values:\n"); + off += snprintf(dst + off, size - off, "Other possible matches based on ATQA & SAK values:\n"); uint32_t atqasak = 0; atqasak += (((uint32_t)nai.abtAtqa[0] & 0xff) << 16); atqasak += (((uint32_t)nai.abtAtqa[1] & 0xff) << 8); atqasak += ((uint32_t)nai.btSak & 0xff); switch (atqasak) { case 0x000488: - dst += sprintf(dst, "* Mifare Classic 1K Infineon\n"); + off += snprintf(dst + off, size - off, "* Mifare Classic 1K Infineon\n"); found_possible_match = true; break; case 0x000298: - dst += sprintf(dst, "* Gemplus MPCOS\n"); + off += snprintf(dst + off, size - off, "* Gemplus MPCOS\n"); found_possible_match = true; break; case 0x030428: - dst += sprintf(dst, "* JCOP31\n"); + off += snprintf(dst + off, size - off, "* JCOP31\n"); found_possible_match = true; break; case 0x004820: - dst += sprintf(dst, "* JCOP31 v2.4.1\n"); - dst += sprintf(dst, "* JCOP31 v2.2\n"); + off += snprintf(dst + off, size - off, "* JCOP31 v2.4.1\n"); + off += snprintf(dst + off, size - off, "* JCOP31 v2.2\n"); found_possible_match = true; break; case 0x000428: - dst += sprintf(dst, "* JCOP31 v2.3.1\n"); + off += snprintf(dst + off, size - off, "* JCOP31 v2.3.1\n"); found_possible_match = true; break; case 0x000453: - dst += sprintf(dst, "* Fudan FM1208SH01\n"); + off += snprintf(dst + off, size - off, "* Fudan FM1208SH01\n"); found_possible_match = true; break; case 0x000820: - dst += sprintf(dst, "* Fudan FM1208\n"); + off += snprintf(dst + off, size - off, "* Fudan FM1208\n"); found_possible_match = true; break; case 0x000238: - dst += sprintf(dst, "* MFC 4K emulated by Nokia 6212 Classic\n"); + off += snprintf(dst + off, size - off, "* MFC 4K emulated by Nokia 6212 Classic\n"); found_possible_match = true; break; case 0x000838: - dst += sprintf(dst, "* MFC 4K emulated by Nokia 6131 NFC\n"); + off += snprintf(dst + off, size - off, "* MFC 4K emulated by Nokia 6131 NFC\n"); found_possible_match = true; break; } if (! found_possible_match) { - sprintf(dst, "* Unknown card, sorry\n"); + snprintf(dst + off, size - off, "* Unknown card, sorry\n"); } } } void -sprint_nfc_felica_info(char *dst, const nfc_felica_info nfi, bool verbose) +snprint_nfc_felica_info(char *dst, size_t size, const nfc_felica_info nfi, bool verbose) { (void) verbose; - dst += sprintf(dst, " ID (NFCID2): "); - dst += sprint_hex(dst, nfi.abtId, 8); - dst += sprintf(dst, " Parameter (PAD): "); - dst += sprint_hex(dst, nfi.abtPad, 8); - dst += sprintf(dst, " System Code (SC): "); - sprint_hex(dst, nfi.abtSysCode, 2); + int off = 0; + off += snprintf(dst + off, size - off, " ID (NFCID2): "); + off += snprint_hex(dst + off, size - off, nfi.abtId, 8); + off += snprintf(dst + off, size - off, " Parameter (PAD): "); + off += snprint_hex(dst + off, size - off, nfi.abtPad, 8); + off += snprintf(dst + off, size - off, " System Code (SC): "); + snprint_hex(dst + off, size - off, nfi.abtSysCode, 2); } void -sprint_nfc_jewel_info(char *dst, const nfc_jewel_info nji, bool verbose) +snprint_nfc_jewel_info(char *dst, size_t size, const nfc_jewel_info nji, bool verbose) { (void) verbose; - dst += sprintf(dst, " ATQA (SENS_RES): "); - dst += sprint_hex(dst, nji.btSensRes, 2); - dst += sprintf(dst, " 4-LSB JEWELID: "); - sprint_hex(dst, nji.btId, 4); + int off = 0; + off += snprintf(dst + off, size - off, " ATQA (SENS_RES): "); + off += snprint_hex(dst + off, size - off, nji.btSensRes, 2); + off += snprintf(dst + off, size - off, " 4-LSB JEWELID: "); + snprint_hex(dst + off, size - off, nji.btId, 4); } #define PI_ISO14443_4_SUPPORTED 0x01 #define PI_NAD_SUPPORTED 0x01 #define PI_CID_SUPPORTED 0x02 void -sprint_nfc_iso14443b_info(char *dst, const nfc_iso14443b_info nbi, bool verbose) +snprint_nfc_iso14443b_info(char *dst, size_t size, const nfc_iso14443b_info nbi, bool verbose) { - dst += sprintf(dst, " PUPI: "); - dst += sprint_hex(dst, nbi.abtPupi, 4); - dst += sprintf(dst, " Application Data: "); - dst += sprint_hex(dst, nbi.abtApplicationData, 4); - dst += sprintf(dst, " Protocol Info: "); - dst += sprint_hex(dst, nbi.abtProtocolInfo, 3); + int off = 0; + off += snprintf(dst + off, size - off, " PUPI: "); + off += snprint_hex(dst + off, size - off, nbi.abtPupi, 4); + off += snprintf(dst + off, size - off, " Application Data: "); + off += snprint_hex(dst + off, size - off, nbi.abtApplicationData, 4); + off += snprintf(dst + off, size - off, " Protocol Info: "); + off += snprint_hex(dst + off, size - off, nbi.abtProtocolInfo, 3); if (verbose) { - dst += sprintf(dst, "* Bit Rate Capability:\n"); + off += snprintf(dst + off, size - off, "* Bit Rate Capability:\n"); if (nbi.abtProtocolInfo[0] == 0) { - dst += sprintf(dst, " * PICC supports only 106 kbits/s in both directions\n"); + off += snprintf(dst + off, size - off, " * PICC supports only 106 kbits/s in both directions\n"); } if (nbi.abtProtocolInfo[0] & 1 << 7) { - dst += sprintf(dst, " * Same bitrate in both directions mandatory\n"); + off += snprintf(dst + off, size - off, " * Same bitrate in both directions mandatory\n"); } if (nbi.abtProtocolInfo[0] & 1 << 4) { - dst += sprintf(dst, " * PICC to PCD, 1etu=64/fc, bitrate 212 kbits/s supported\n"); + off += snprintf(dst + off, size - off, " * PICC to PCD, 1etu=64/fc, bitrate 212 kbits/s supported\n"); } if (nbi.abtProtocolInfo[0] & 1 << 5) { - dst += sprintf(dst, " * PICC to PCD, 1etu=32/fc, bitrate 424 kbits/s supported\n"); + off += snprintf(dst + off, size - off, " * PICC to PCD, 1etu=32/fc, bitrate 424 kbits/s supported\n"); } if (nbi.abtProtocolInfo[0] & 1 << 6) { - dst += sprintf(dst, " * PICC to PCD, 1etu=16/fc, bitrate 847 kbits/s supported\n"); + off += snprintf(dst + off, size - off, " * PICC to PCD, 1etu=16/fc, bitrate 847 kbits/s supported\n"); } if (nbi.abtProtocolInfo[0] & 1 << 0) { - dst += sprintf(dst, " * PCD to PICC, 1etu=64/fc, bitrate 212 kbits/s supported\n"); + off += snprintf(dst + off, size - off, " * PCD to PICC, 1etu=64/fc, bitrate 212 kbits/s supported\n"); } if (nbi.abtProtocolInfo[0] & 1 << 1) { - dst += sprintf(dst, " * PCD to PICC, 1etu=32/fc, bitrate 424 kbits/s supported\n"); + off += snprintf(dst + off, size - off, " * PCD to PICC, 1etu=32/fc, bitrate 424 kbits/s supported\n"); } if (nbi.abtProtocolInfo[0] & 1 << 2) { - dst += sprintf(dst, " * PCD to PICC, 1etu=16/fc, bitrate 847 kbits/s supported\n"); + off += snprintf(dst + off, size - off, " * PCD to PICC, 1etu=16/fc, bitrate 847 kbits/s supported\n"); } if (nbi.abtProtocolInfo[0] & 1 << 3) { - dst += sprintf(dst, " * ERROR unknown value\n"); + off += snprintf(dst + off, size - off, " * ERROR unknown value\n"); } if ((nbi.abtProtocolInfo[1] & 0xf0) <= 0x80) { const int iMaxFrameSizes[] = { 16, 24, 32, 40, 48, 64, 96, 128, 256 }; - dst += sprintf(dst, "* Maximum frame sizes: %d bytes\n", iMaxFrameSizes[((nbi.abtProtocolInfo[1] & 0xf0) >> 4)]); + off += snprintf(dst + off, size - off, "* Maximum frame sizes: %d bytes\n", iMaxFrameSizes[((nbi.abtProtocolInfo[1] & 0xf0) >> 4)]); } if ((nbi.abtProtocolInfo[1] & 0x0f) == PI_ISO14443_4_SUPPORTED) { - dst += sprintf(dst, "* Protocol types supported: ISO/IEC 14443-4\n"); + off += snprintf(dst + off, size - off, "* Protocol types supported: ISO/IEC 14443-4\n"); } - dst += sprintf(dst, "* Frame Waiting Time: %.4g ms\n", 256.0 * 16.0 * (1 << ((nbi.abtProtocolInfo[2] & 0xf0) >> 4)) / 13560.0); + off += snprintf(dst + off, size - off, "* Frame Waiting Time: %.4g ms\n", 256.0 * 16.0 * (1 << ((nbi.abtProtocolInfo[2] & 0xf0) >> 4)) / 13560.0); if ((nbi.abtProtocolInfo[2] & (PI_NAD_SUPPORTED | PI_CID_SUPPORTED)) != 0) { - dst += sprintf(dst, "* Frame options supported: "); - if ((nbi.abtProtocolInfo[2] & PI_NAD_SUPPORTED) != 0) dst += sprintf(dst, "NAD "); - if ((nbi.abtProtocolInfo[2] & PI_CID_SUPPORTED) != 0) dst += sprintf(dst, "CID "); - sprintf(dst, "\n"); + off += snprintf(dst + off, size - off, "* Frame options supported: "); + if ((nbi.abtProtocolInfo[2] & PI_NAD_SUPPORTED) != 0) off += snprintf(dst + off, size - off, "NAD "); + if ((nbi.abtProtocolInfo[2] & PI_CID_SUPPORTED) != 0) off += snprintf(dst + off, size - off, "CID "); + snprintf(dst + off, size - off, "\n"); } } } void -sprint_nfc_iso14443bi_info(char *dst, const nfc_iso14443bi_info nii, bool verbose) +snprint_nfc_iso14443bi_info(char *dst, size_t size, const nfc_iso14443bi_info nii, bool verbose) { - dst += sprintf(dst, " DIV: "); - dst += sprint_hex(dst, nii.abtDIV, 4); + int off = 0; + off += snprintf(dst + off, size - off, " DIV: "); + off += snprint_hex(dst + off, size - off, nii.abtDIV, 4); if (verbose) { int version = (nii.btVerLog & 0x1e) >> 1; - dst += sprintf(dst, " Software Version: "); + off += snprintf(dst + off, size - off, " Software Version: "); if (version == 15) { - dst += sprintf(dst, "Undefined\n"); + off += snprintf(dst + off, size - off, "Undefined\n"); } else { - dst += sprintf(dst, "%i\n", version); + off += snprintf(dst + off, size - off, "%i\n", version); } if ((nii.btVerLog & 0x80) && (nii.btConfig & 0x80)) { - dst += sprintf(dst, " Wait Enable: yes"); + off += snprintf(dst + off, size - off, " Wait Enable: yes"); } } if ((nii.btVerLog & 0x80) && (nii.btConfig & 0x40)) { - dst += sprintf(dst, " ATS: "); - sprint_hex(dst, nii.abtAtr, nii.szAtrLen); + off += snprintf(dst + off, size - off, " ATS: "); + snprint_hex(dst + off, size - off, nii.abtAtr, nii.szAtrLen); } } void -sprint_nfc_iso14443b2sr_info(char *dst, const nfc_iso14443b2sr_info nsi, bool verbose) +snprint_nfc_iso14443b2sr_info(char *dst, size_t size, const nfc_iso14443b2sr_info nsi, bool verbose) { (void) verbose; - dst += sprintf(dst, " UID: "); - sprint_hex(dst, nsi.abtUID, 8); + int off = 0; + off += snprintf(dst + off, size - off, " UID: "); + snprint_hex(dst + off, size - off, nsi.abtUID, 8); } void -sprint_nfc_iso14443b2ct_info(char *dst, const nfc_iso14443b2ct_info nci, bool verbose) +snprint_nfc_iso14443b2ct_info(char *dst, size_t size, const nfc_iso14443b2ct_info nci, bool verbose) { (void) verbose; + int off = 0; uint32_t uid; uid = (nci.abtUID[3] << 24) + (nci.abtUID[2] << 16) + (nci.abtUID[1] << 8) + nci.abtUID[0]; - dst += sprintf(dst, " UID: "); - dst += sprint_hex(dst, nci.abtUID, sizeof(nci.abtUID)); - dst += sprintf(dst, " UID (decimal): %010u\n", uid); - dst += sprintf(dst, " Product Code: %02X\n", nci.btProdCode); - sprintf(dst, " Fab Code: %02X\n", nci.btFabCode); + off += snprintf(dst + off, size - off, " UID: "); + off += snprint_hex(dst + off, size - off, nci.abtUID, sizeof(nci.abtUID)); + off += snprintf(dst + off, size - off, " UID (decimal): %010u\n", uid); + off += snprintf(dst + off, size - off, " Product Code: %02X\n", nci.btProdCode); + snprintf(dst + off, size - off, " Fab Code: %02X\n", nci.btFabCode); } void -sprint_nfc_dep_info(char *dst, const nfc_dep_info ndi, bool verbose) +snprint_nfc_dep_info(char *dst, size_t size, const nfc_dep_info ndi, bool verbose) { (void) verbose; - dst += sprintf(dst, " NFCID3: "); - dst += sprint_hex(dst, ndi.abtNFCID3, 10); - dst += sprintf(dst, " BS: %02x\n", ndi.btBS); - dst += sprintf(dst, " BR: %02x\n", ndi.btBR); - dst += sprintf(dst, " TO: %02x\n", ndi.btTO); - dst += sprintf(dst, " PP: %02x\n", ndi.btPP); + int off = 0; + off += snprintf(dst + off, size - off, " NFCID3: "); + off += snprint_hex(dst + off, size - off, ndi.abtNFCID3, 10); + off += snprintf(dst + off, size - off, " BS: %02x\n", ndi.btBS); + off += snprintf(dst + off, size - off, " BR: %02x\n", ndi.btBR); + off += snprintf(dst + off, size - off, " TO: %02x\n", ndi.btTO); + off += snprintf(dst + off, size - off, " PP: %02x\n", ndi.btPP); if (ndi.szGB) { - dst += sprintf(dst, "General Bytes: "); - sprint_hex(dst, ndi.abtGB, ndi.szGB); + off += snprintf(dst + off, size - off, "General Bytes: "); + snprint_hex(dst + off, size - off, ndi.abtGB, ndi.szGB); } } void -sprint_nfc_target(char *dst, const nfc_target nt, bool verbose) +snprint_nfc_target(char *dst, size_t size, const nfc_target nt, bool verbose) { - dst += sprintf(dst, "%s (%s%s) target:\n", str_nfc_modulation_type(nt.nm.nmt), str_nfc_baud_rate(nt.nm.nbr), (nt.nm.nmt != NMT_DEP) ? "" : (nt.nti.ndi.ndm == NDM_ACTIVE) ? "active mode" : "passive mode"); + int off = 0; + off += snprintf(dst + off, size - off, "%s (%s%s) target:\n", str_nfc_modulation_type(nt.nm.nmt), str_nfc_baud_rate(nt.nm.nbr), (nt.nm.nmt != NMT_DEP) ? "" : (nt.nti.ndi.ndm == NDM_ACTIVE) ? "active mode" : "passive mode"); switch (nt.nm.nmt) { case NMT_ISO14443A: - sprint_nfc_iso14443a_info(dst, nt.nti.nai, verbose); + snprint_nfc_iso14443a_info(dst + off, size - off, nt.nti.nai, verbose); break; case NMT_JEWEL: - sprint_nfc_jewel_info(dst, nt.nti.nji, verbose); + snprint_nfc_jewel_info(dst + off, size - off, nt.nti.nji, verbose); break; case NMT_FELICA: - sprint_nfc_felica_info(dst, nt.nti.nfi, verbose); + snprint_nfc_felica_info(dst + off, size - off, nt.nti.nfi, verbose); break; case NMT_ISO14443B: - sprint_nfc_iso14443b_info(dst, nt.nti.nbi, verbose); + snprint_nfc_iso14443b_info(dst + off, size - off, nt.nti.nbi, verbose); break; case NMT_ISO14443BI: - sprint_nfc_iso14443bi_info(dst, nt.nti.nii, verbose); + snprint_nfc_iso14443bi_info(dst + off, size - off, nt.nti.nii, verbose); break; case NMT_ISO14443B2SR: - sprint_nfc_iso14443b2sr_info(dst, nt.nti.nsi, verbose); + snprint_nfc_iso14443b2sr_info(dst + off, size - off, nt.nti.nsi, verbose); break; case NMT_ISO14443B2CT: - sprint_nfc_iso14443b2ct_info(dst, nt.nti.nci, verbose); + snprint_nfc_iso14443b2ct_info(dst + off, size - off, nt.nti.nci, verbose); break; case NMT_DEP: - sprint_nfc_dep_info(dst, nt.nti.ndi, verbose); + snprint_nfc_dep_info(dst + off, size - off, nt.nti.ndi, verbose); break; } } diff --git a/libnfc/target-subr.h b/libnfc/target-subr.h index 85a292e..89710b6 100644 --- a/libnfc/target-subr.h +++ b/libnfc/target-subr.h @@ -27,15 +27,15 @@ #ifndef _TARGET_SUBR_H_ #define _TARGET_SUBR_H_ -int sprint_hex(char *dst, const uint8_t *pbtData, const size_t szLen); -void sprint_nfc_iso14443a_info(char *dst, const nfc_iso14443a_info nai, bool verbose); -void sprint_nfc_iso14443b_info(char *dst, const nfc_iso14443b_info nbi, bool verbose); -void sprint_nfc_iso14443bi_info(char *dst, const nfc_iso14443bi_info nii, bool verbose); -void sprint_nfc_iso14443b2sr_info(char *dst, const nfc_iso14443b2sr_info nsi, bool verbose); -void sprint_nfc_iso14443b2ct_info(char *dst, const nfc_iso14443b2ct_info nci, bool verbose); -void sprint_nfc_felica_info(char *dst, const nfc_felica_info nfi, bool verbose); -void sprint_nfc_jewel_info(char *dst, const nfc_jewel_info nji, bool verbose); -void sprint_nfc_dep_info(char *dst, const nfc_dep_info ndi, bool verbose); -void sprint_nfc_target(char *dst, const nfc_target nt, bool verbose); +int snprint_hex(char *dst, size_t size, const uint8_t *pbtData, const size_t szLen); +void snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, bool verbose); +void snprint_nfc_iso14443b_info(char *dst, size_t size, const nfc_iso14443b_info nbi, bool verbose); +void snprint_nfc_iso14443bi_info(char *dst, size_t size, const nfc_iso14443bi_info nii, bool verbose); +void snprint_nfc_iso14443b2sr_info(char *dst, size_t size, const nfc_iso14443b2sr_info nsi, bool verbose); +void snprint_nfc_iso14443b2ct_info(char *dst, size_t size, const nfc_iso14443b2ct_info nci, bool verbose); +void snprint_nfc_felica_info(char *dst, size_t size, const nfc_felica_info nfi, bool verbose); +void snprint_nfc_jewel_info(char *dst, size_t size, const nfc_jewel_info nji, bool verbose); +void snprint_nfc_dep_info(char *dst, size_t size, const nfc_dep_info ndi, bool verbose); +void snprint_nfc_target(char *dst, size_t size, const nfc_target nt, bool verbose); #endif From 3e773ab494568d915f80aebc4ce798cdaa7f0aaf Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 12:30:10 +0100 Subject: [PATCH 20/43] Edit HACKING: cppcheck: add --inconclusive --- HACKING | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HACKING b/HACKING index e457137..1e39d6c 100644 --- a/HACKING +++ b/HACKING @@ -47,7 +47,7 @@ Here are some directions to get you started: -DDRIVER_PN53X_USB_ENABLED -DDRIVER_ACR122_PCSC_ENABLED \ -DDRIVER_ACR122_USB_ENABLED -DDRIVER_ACR122S_ENABLED \ -DDRIVER_PN532_UART_ENABLED -DDRIVER_ARYGON_ENABLED \ - --force . + --force --inconclusive . 2.3 When Debianizing $ lintian --info --display-info --display-experimental *deb or (shorter version) From 1d5f9956fb19de410aaf51ec5440289db402224c Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 12:38:59 +0100 Subject: [PATCH 21/43] Fix cppcheck warning "buffer may not be null-terminated after call to strncpy()" --- libnfc/drivers/acr122_pcsc.c | 1 + libnfc/drivers/acr122_usb.c | 1 + libnfc/drivers/pn53x_usb.c | 1 + libnfc/nfc.c | 1 + utils/nfc-relay-picc.c | 1 + 5 files changed, 5 insertions(+) diff --git a/libnfc/drivers/acr122_pcsc.c b/libnfc/drivers/acr122_pcsc.c index 6a9151a..f8dc9d2 100644 --- a/libnfc/drivers/acr122_pcsc.c +++ b/libnfc/drivers/acr122_pcsc.c @@ -271,6 +271,7 @@ acr122_pcsc_open(const nfc_context *context, const nfc_connstring connstring) return NULL; } strncpy(fullconnstring, ncs[index], sizeof(nfc_connstring)); + fullconnstring[sizeof(nfc_connstring) - 1] = '\0'; free(ncs); connstring_decode_level = acr122_pcsc_connstring_decode(fullconnstring, &ndd); if (connstring_decode_level < 2) { diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c index db57d76..3608c5b 100644 --- a/libnfc/drivers/acr122_usb.c +++ b/libnfc/drivers/acr122_usb.c @@ -410,6 +410,7 @@ acr122_usb_get_usb_device_name(struct usb_device *dev, usb_dev_handle *udev, cha if ((acr122_usb_supported_devices[n].vendor_id == dev->descriptor.idVendor) && (acr122_usb_supported_devices[n].product_id == dev->descriptor.idProduct)) { strncpy(buffer, acr122_usb_supported_devices[n].name, len); + buffer[len - 1] = '\0'; return true; } } diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 9f6efc0..1d229c5 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -295,6 +295,7 @@ pn53x_usb_get_usb_device_name(struct usb_device *dev, usb_dev_handle *udev, char if ((pn53x_usb_supported_devices[n].vendor_id == dev->descriptor.idVendor) && (pn53x_usb_supported_devices[n].product_id == dev->descriptor.idProduct)) { strncpy(buffer, pn53x_usb_supported_devices[n].name, len); + buffer[len - 1] = '\0'; return true; } } diff --git a/libnfc/nfc.c b/libnfc/nfc.c index fc1ed9f..53f68be 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -226,6 +226,7 @@ nfc_open(nfc_context *context, const nfc_connstring connstring) } } else { strncpy(ncs, connstring, sizeof(nfc_connstring)); + ncs[sizeof(nfc_connstring) - 1] = '\0'; } // Search through the device list for an available device diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 43988e5..18ec0fa 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -130,6 +130,7 @@ static int scan_hex_fd3(uint8_t *pbtData, size_t *pszBytes, const char *pchPrefi } } strncpy(pchScan, pchPrefix, 250); + pchScan[sizeof(pchScan) - 1] = '\0'; strcat(pchScan, " %04x:"); if (fscanf(fd3, pchScan, &uiBytes) < 1) { return -1; From 4dafd70b64761c8cbf5cac9822ca50bca7266f68 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 12:42:39 +0100 Subject: [PATCH 22/43] Fix cppcheck warning "Statements following return will never be executed" --- libnfc/drivers/acr122_pcsc.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/libnfc/drivers/acr122_pcsc.c b/libnfc/drivers/acr122_pcsc.c index f8dc9d2..b8337ea 100644 --- a/libnfc/drivers/acr122_pcsc.c +++ b/libnfc/drivers/acr122_pcsc.c @@ -227,9 +227,6 @@ acr122_pcsc_connstring_decode(const nfc_connstring connstring, struct acr122_pcs free(cs); return 2; - - free(cs); - return 3; } static nfc_device * From a0b859b2a10019708cfb0f886b5f268ca450b618 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 15:20:15 +0100 Subject: [PATCH 23/43] Fix cppcheck warning "Non reentrant function 'readdir' called" For threadsafe applications it is recommended to use the reentrant replacement function 'readdir_r'. --- libnfc/buses/uart_posix.c | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index ced1bc6..54c8e62 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -39,6 +39,7 @@ #include #include #include +#include #include "nfc-internal.h" @@ -337,26 +338,32 @@ uart_list_ports(void) size_t szRes = 1; res[0] = NULL; - - DIR *pdDir = opendir("/dev"); - struct dirent *pdDirEnt; - while ((pdDirEnt = readdir(pdDir)) != NULL) { + DIR *dir; + if ((dir = opendir("/dev")) == NULL) { + perror("opendir error: /dev"); + return res; + } + struct dirent entry; + struct dirent *result; + while ((readdir_r(dir, &entry, &result) == 0) && (result != NULL)) { #if !defined(__APPLE__) - if (!isdigit(pdDirEnt->d_name[strlen(pdDirEnt->d_name) - 1])) + if (!isdigit(entry.d_name[strlen(entry.d_name) - 1])) continue; #endif const char **p = serial_ports_device_radix; while (*p) { - if (!strncmp(pdDirEnt->d_name, *p, strlen(*p))) { + if (!strncmp(entry.d_name, *p, strlen(*p))) { char **res2 = realloc(res, (szRes + 1) * sizeof(char *)); - if (!res2) + if (!res2) { + perror("malloc"); goto oom; - + } res = res2; - if (!(res[szRes - 1] = malloc(6 + strlen(pdDirEnt->d_name)))) + if (!(res[szRes - 1] = malloc(6 + strlen(entry.d_name)))) { + perror("malloc"); goto oom; - - sprintf(res[szRes - 1], "/dev/%s", pdDirEnt->d_name); + } + sprintf(res[szRes - 1], "/dev/%s", entry.d_name); szRes++; res[szRes - 1] = NULL; @@ -365,7 +372,7 @@ uart_list_ports(void) } } oom: - closedir(pdDir); + closedir(dir); return res; } From b81519c6a9b2ac28a09b3854da4636f8f2601321 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 19:34:57 +0100 Subject: [PATCH 24/43] uart_posix.c: remove redundant include --- libnfc/buses/uart_posix.c | 1 - 1 file changed, 1 deletion(-) diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index 54c8e62..d6dd3a1 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -30,7 +30,6 @@ #include #include #include -#include #include #include #include From 90622f52b759da91d6eb8b0eeb4e401a8a9dcd50 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 19:36:32 +0100 Subject: [PATCH 25/43] conf.h: make it more standard --- libnfc/conf.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libnfc/conf.h b/libnfc/conf.h index 7ba1c72..ce7b8e2 100644 --- a/libnfc/conf.h +++ b/libnfc/conf.h @@ -14,7 +14,12 @@ * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see */ -#include "nfc-internal.h" +#ifndef __NFC_CONF_H__ +#define __NFC_CONF_H__ + +#include void conf_load(nfc_context *context); +#endif // __NFC_CONF_H__ + From 7fb538737d4a5d8e086785339fb2933f8a5c18b7 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 20:59:41 +0100 Subject: [PATCH 26/43] Fix cppcheck warning "Non reentrant function 'readdir' called" For threadsafe applications it is recommended to use the reentrant replacement function 'readdir_r'. --- libnfc/conf.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libnfc/conf.c b/libnfc/conf.c index 7647a52..2ec348b 100644 --- a/libnfc/conf.c +++ b/libnfc/conf.c @@ -1,3 +1,4 @@ + /*- * Copyright (C) 2012, 2013 Romuald Conty * @@ -15,12 +16,12 @@ * along with this program. If not, see */ -#include "conf.h" - #ifdef HAVE_CONFIG_H # include "config.h" #endif // HAVE_CONFIG_H +#include "conf.h" + #ifdef CONFFILES #include #include @@ -163,7 +164,10 @@ conf_devices_load(const char *dirname, nfc_context *context) log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Unable to open directory: %s", dirname); } else { struct dirent *de; - while ((de = readdir(d))) { + struct dirent entry; + struct dirent *result; + while ((readdir_r(d, &entry, &result) == 0) && (result != NULL)) { + de = &entry; if (de->d_name[0] != '.') { const size_t filename_len = strlen(de->d_name); const size_t extension_len = strlen(".conf"); From fdd5bd3523697412369aaee88453c3e49a081f5e Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 21:13:39 +0100 Subject: [PATCH 27/43] Avoid warning about ignoring return value of 'read' --- libnfc/buses/uart_posix.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index d6dd3a1..fc32c12 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -127,7 +127,8 @@ uart_flush_input(serial_port sp) } char *rx = malloc(available_bytes_count); // There is something available, read the data - read(UART_DATA(sp)->fd, rx, available_bytes_count); + res = read(UART_DATA(sp)->fd, rx, available_bytes_count); + (void) res; log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%d bytes have eatten.", available_bytes_count); free(rx); } From 048ed88e164247252e8299c963a7de8beab4caa7 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 21:43:36 +0100 Subject: [PATCH 28/43] Fix cppcheck warning "Obsolete function 'usleep' called" It is recommended to use the 'nanosleep' or 'setitimer' function instead. --- examples/pn53x-tamashell.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index 42a06ff..e817145 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -48,15 +48,16 @@ #include #ifndef _WIN32 -// Needed by sleep() under Unix -# include -# define sleep usleep -# define SUSP_TIME 1000 // usecs. +# include +# define msleep(x) do { \ + struct timespec xsleep; \ + xsleep.tv_sec = x / 1000; \ + xsleep.tv_nsec = (x - xsleep.tv_sec * 1000) * 1000 * 1000; \ + nanosleep(&xsleep, NULL); \ + } while (0) #else -// Needed by Sleep() under Windows # include -# define sleep Sleep -# define SUSP_TIME 1 // msecs. +# define msleep Sleep #endif @@ -154,15 +155,15 @@ int main(int argc, const char *argv[]) break; } if (cmd[0] == 'p') { - int s = 0; + int ms = 0; offset++; while (isspace(cmd[offset])) { offset++; } - sscanf(cmd + offset, "%10d", &s); - printf("Pause for %i msecs\n", s); - if (s > 0) { - sleep(s * SUSP_TIME); + sscanf(cmd + offset, "%10d", &ms); + printf("Pause for %i msecs\n", ms); + if (ms > 0) { + msleep(ms); } free(cmd); continue; From 1c67c3f92a4399d5a6e914e4b27be3552d4f77ef Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 6 Mar 2013 23:22:02 +0100 Subject: [PATCH 29/43] Fix scan-build warning: cast increases required alignment from 1 to 4 acr122_usb.c:562:20: warning: cast from 'uint8_t *' (aka 'unsigned char *') to 'uint32_t *' (aka 'unsigned int *') increases required alignment from 1 to 4 [-Wcast-align] uint32_t *pu32 = (uint32_t *)u8; ^~~~~~~~~~~~~~ --- libnfc/drivers/acr122_usb.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c index 3608c5b..d2c439f 100644 --- a/libnfc/drivers/acr122_usb.c +++ b/libnfc/drivers/acr122_usb.c @@ -554,13 +554,7 @@ uint32_t htole32(uint32_t u32); uint32_t htole32(uint32_t u32) { - uint8_t u8[4]; - for (int i = 0; i < 4; i++) { - u8[i] = (u32 & 0xff); - u32 >>= 8; - } - uint32_t *pu32 = (uint32_t *)u8; - return *pu32; + return (((u32 & 0xff) << 24) + ((u32 & 0xff00) << 8) + ((u32 & 0xff0000) >> 8) + (u32 >> 24)); } static int From 008158090c1a4c785fa305b2f230c1562c2c81e4 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Thu, 7 Mar 2013 00:43:35 +0100 Subject: [PATCH 30/43] Fix bug when compiling without libusb: skip usbbus.c --- libnfc/buses/Makefile.am | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/libnfc/buses/Makefile.am b/libnfc/buses/Makefile.am index 23cf46f..58f193c 100644 --- a/libnfc/buses/Makefile.am +++ b/libnfc/buses/Makefile.am @@ -1,10 +1,21 @@ - # set the include path found by configure AM_CPPFLAGS = $(all_includes) $(LIBNFC_CFLAGS) noinst_LTLIBRARIES = libnfcbuses.la -libnfcbuses_la_SOURCES = uart.c uart.h usbbus.c usbbus.h -libnfcbuses_la_CFLAGS = -I$(top_srcdir)/libnfc +libnfcbuses_la_SOURCES = +libnfcbuses_la_CFLAGS = -I$(top_srcdir)/libnfc +libnfcbuses_la_LIBADD = + +# UART_ENABLED +libnfcbuses_la_SOURCES += uart.c uart.h +libnfcbuses_la_CFLAGS += +libnfcbuses_la_LIBADD += EXTRA_DIST = uart_posix.c uart_win32.c +if LIBUSB_ENABLED + libnfcbuses_la_SOURCES += usbbus.c usbbus.h + libnfcbuses_la_CFLAGS += @libusb_CFLAGS@ + libnfcbuses_la_LIBADD += @libusb_LIBS@ +endif + From f8d80528666b3351775c5ee3727fd162bcab7066 Mon Sep 17 00:00:00 2001 From: Alex Lian Date: Sun, 3 Mar 2013 12:31:06 -0500 Subject: [PATCH 31/43] Windows: Fix compilation due to new usbbus file --- libnfc/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnfc/CMakeLists.txt b/libnfc/CMakeLists.txt index fdfca79..0ad34da 100644 --- a/libnfc/CMakeLists.txt +++ b/libnfc/CMakeLists.txt @@ -12,7 +12,7 @@ SET(CHIPS_SOURCES chips/pn53x) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/chips) # Library's buses -SET(BUSES_SOURCES buses/uart) +SET(BUSES_SOURCES buses/uart buses/usbbus) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/buses) INCLUDE(LibnfcDrivers) From a262be563374f92d29e3b976a51da91762d08307 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Thu, 7 Mar 2013 08:07:42 +0100 Subject: [PATCH 32/43] Windows: Fix bug when compiling without libusb: skip usbbus.c --- libnfc/CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libnfc/CMakeLists.txt b/libnfc/CMakeLists.txt index 0ad34da..81fb023 100644 --- a/libnfc/CMakeLists.txt +++ b/libnfc/CMakeLists.txt @@ -12,7 +12,11 @@ SET(CHIPS_SOURCES chips/pn53x) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/chips) # Library's buses -SET(BUSES_SOURCES buses/uart buses/usbbus) +IF(LIBUSB_FOUND) + SET(BUSES_SOURCES buses/uart buses/usbbus) +ELSE(LIBUSB_FOUND) + SET(BUSES_SOURCES buses/uart) +ENDIF(LIBUSB_FOUND) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/buses) INCLUDE(LibnfcDrivers) From c72846e3c62e320ab2d8b98ac1cb8f23739c776b Mon Sep 17 00:00:00 2001 From: Alex Lian Date: Sun, 3 Mar 2013 23:40:07 -0500 Subject: [PATCH 33/43] Convert by value passing of nfc_target to pointer for str_nfc_target and nfc_initiator_target_is_present This becomes more consistent with all other pass by pointer of most structures. Additionally, this should lessen stack memory usage, as building strings with str_nfc_target would push the target (283 bytes) plus then a copy of the info objects (up to 275) onto the stack as it dives into the sprintf functions. Lastly, this makes my attempt at a .NET wrapper easier, as I can make passing by pointer work, but passing by value seems to bomb on the interop right now. --- examples/nfc-dep-initiator.c | 2 +- examples/nfc-dep-target.c | 2 +- examples/nfc-emulate-tag.c | 2 +- examples/nfc-poll.c | 2 +- include/nfc/nfc.h | 4 +- libnfc/chips/pn53x.c | 4 +- libnfc/chips/pn53x.h | 2 +- libnfc/nfc-internal.h | 2 +- libnfc/nfc.c | 8 +- libnfc/target-subr.c | 236 ++++++++++++++++++----------------- libnfc/target-subr.h | 18 +-- utils/nfc-list.c | 16 +-- utils/nfc-mfclassic.c | 2 +- utils/nfc-relay-picc.c | 4 +- utils/nfc-utils.c | 4 +- utils/nfc-utils.h | 2 +- 16 files changed, 156 insertions(+), 154 deletions(-) diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index c095a4b..36033a8 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -103,7 +103,7 @@ main(int argc, const char *argv[]) nfc_exit(context); exit(EXIT_FAILURE); } - print_nfc_target(nt, false); + print_nfc_target(&nt, false); printf("Sending: %s\n", abtTx); int res; diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 1aef22d..fe1bd38 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -126,7 +126,7 @@ main(int argc, const char *argv[]) signal(SIGINT, stop_dep_communication); printf("NFC device will now act as: "); - print_nfc_target(nt, false); + print_nfc_target(&nt, false); printf("Waiting for initiator request...\n"); if ((szRx = nfc_target_init(pnd, &nt, abtRx, sizeof(abtRx), 0)) < 0) { diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index 42c9a10..94368c8 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -266,7 +266,7 @@ main(int argc, char *argv[]) */ printf("%s will emulate this ISO14443-A tag:\n", argv[0]); - print_nfc_target(nt, true); + print_nfc_target(&nt, true); // Switch off NP_EASY_FRAMING if target is not ISO14443-4 nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, (nt.nti.nai.btSak & SAK_ISO14443_4_COMPLIANT)); diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index b5f29ee..40cd87d 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -137,7 +137,7 @@ main(int argc, const char *argv[]) } if (res > 0) { - print_nfc_target(nt, verbose); + print_nfc_target(&nt, verbose); } else { printf("No target found.\n"); } diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 74596b5..6862db0 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -101,7 +101,7 @@ extern "C" { NFC_EXPORT int nfc_initiator_transceive_bits(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar); NFC_EXPORT int nfc_initiator_transceive_bytes_timed(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, const size_t szRx, uint32_t *cycles); NFC_EXPORT int nfc_initiator_transceive_bits_timed(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar, uint32_t *cycles); - NFC_EXPORT int nfc_initiator_target_is_present(nfc_device *pnd, const nfc_target nt); + NFC_EXPORT int nfc_initiator_target_is_present(nfc_device *pnd, const nfc_target *pnt); /* NFC target: act as tag (i.e. MIFARE Classic) or NFC target device. */ NFC_EXPORT int nfc_target_init(nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout); @@ -138,7 +138,7 @@ extern "C" { /* String converter functions */ NFC_EXPORT const char *str_nfc_modulation_type(const nfc_modulation_type nmt); NFC_EXPORT const char *str_nfc_baud_rate(const nfc_baud_rate nbr); - NFC_EXPORT int str_nfc_target(char **buf, const nfc_target nt, bool verbose); + NFC_EXPORT int str_nfc_target(char **buf, const nfc_target *pnt, bool verbose); /* Error codes */ /** @ingroup error diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 9b5946c..96c4bda 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1668,10 +1668,10 @@ pn53x_initiator_deselect_target(struct nfc_device *pnd) } int -pn53x_initiator_target_is_present(struct nfc_device *pnd, const nfc_target nt) +pn53x_initiator_target_is_present(struct nfc_device *pnd, const nfc_target *pnt) { // Check if the argument target nt is equals to current saved target - if (!pn53x_current_target_is(pnd, &nt)) { + if (!pn53x_current_target_is(pnd, pnt)) { return NFC_ETGRELEASED; } diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 8acf2ae..be67ef8 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -337,7 +337,7 @@ int pn53x_initiator_transceive_bits_timed(struct nfc_device *pnd, const uint8 int pn53x_initiator_transceive_bytes_timed(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, const size_t szRx, uint32_t *cycles); int pn53x_initiator_deselect_target(struct nfc_device *pnd); -int pn53x_initiator_target_is_present(struct nfc_device *pnd, const nfc_target nt); +int pn53x_initiator_target_is_present(struct nfc_device *pnd, const nfc_target *pnt); // NFC device as Target functions int pn53x_target_init(struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRxLen, int timeout); diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 30e4947..1a5eec4 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -131,7 +131,7 @@ struct nfc_driver { int (*initiator_transceive_bits)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, uint8_t *pbtRxPar); int (*initiator_transceive_bytes_timed)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, const size_t szRx, uint32_t *cycles); int (*initiator_transceive_bits_timed)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, uint8_t *pbtRxPar, uint32_t *cycles); - int (*initiator_target_is_present)(struct nfc_device *pnd, const nfc_target nt); + int (*initiator_target_is_present)(struct nfc_device *pnd, const nfc_target *pnt); int (*target_init)(struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout); int (*target_send_bytes)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 53f68be..473af3f 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -821,9 +821,9 @@ nfc_initiator_transceive_bytes_timed(nfc_device *pnd, * @warning To run the test, one or more commands will be sent to target */ int -nfc_initiator_target_is_present(nfc_device *pnd, const nfc_target nt) +nfc_initiator_target_is_present(nfc_device *pnd, const nfc_target *pnt) { - HAL(initiator_target_is_present, pnd, nt); + HAL(initiator_target_is_present, pnd, pnt); } /** @ingroup initiator @@ -1287,12 +1287,12 @@ str_nfc_modulation_type(const nfc_modulation_type nmt) * @warning *buf must be freed using nfc_free() */ int -str_nfc_target(char **buf, const nfc_target nt, bool verbose) +str_nfc_target(char **buf, const nfc_target *pnt, bool verbose) { *buf = malloc(4096); if (! *buf) return NFC_ESOFT; (*buf)[0] = '\0'; - snprint_nfc_target(*buf, 4096, nt, verbose); + snprint_nfc_target(*buf, 4096, pnt, verbose); return strlen(*buf); } diff --git a/libnfc/target-subr.c b/libnfc/target-subr.c index cdc9028..b46d6bc 100644 --- a/libnfc/target-subr.c +++ b/libnfc/target-subr.c @@ -133,14 +133,14 @@ snprint_hex(char *dst, size_t size, const uint8_t *pbtData, const size_t szBytes #define SAK_ISO18092_COMPLIANT 0x40 void -snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, bool verbose) +snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info *pnai, bool verbose) { int off = 0; off += snprintf(dst + off, size - off, " ATQA (SENS_RES): "); - off += snprint_hex(dst + off, size - off, nai.abtAtqa, 2); + off += snprint_hex(dst + off, size - off, pnai->abtAtqa, 2); if (verbose) { off += snprintf(dst + off, size - off, "* UID size: "); - switch ((nai.abtAtqa[1] & 0xc0) >> 6) { + switch ((pnai->abtAtqa[1] & 0xc0) >> 6) { case 0: off += snprintf(dst + off, size - off, "single\n"); break; @@ -155,7 +155,7 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, break; } off += snprintf(dst + off, size - off, "* bit frame anticollision "); - switch (nai.abtAtqa[1] & 0x1f) { + switch (pnai->abtAtqa[1] & 0x1f) { case 0x01: case 0x02: case 0x04: @@ -168,42 +168,42 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, break; } } - off += snprintf(dst + off, size - off, " UID (NFCID%c): ", (nai.abtUid[0] == 0x08 ? '3' : '1')); - off += snprint_hex(dst + off, size - off, nai.abtUid, nai.szUidLen); + off += snprintf(dst + off, size - off, " UID (NFCID%c): ", (pnai->abtUid[0] == 0x08 ? '3' : '1')); + off += snprint_hex(dst + off, size - off, pnai->abtUid, pnai->szUidLen); if (verbose) { - if (nai.abtUid[0] == 0x08) { + if (pnai->abtUid[0] == 0x08) { off += snprintf(dst + off, size - off, "* Random UID\n"); } } off += snprintf(dst + off, size - off, " SAK (SEL_RES): "); - off += snprint_hex(dst + off, size - off, &nai.btSak, 1); + off += snprint_hex(dst + off, size - off, &pnai->btSak, 1); if (verbose) { - if (nai.btSak & SAK_UID_NOT_COMPLETE) { + if (pnai->btSak & SAK_UID_NOT_COMPLETE) { off += snprintf(dst + off, size - off, "* Warning! Cascade bit set: UID not complete\n"); } - if (nai.btSak & SAK_ISO14443_4_COMPLIANT) { + if (pnai->btSak & SAK_ISO14443_4_COMPLIANT) { off += snprintf(dst + off, size - off, "* Compliant with ISO/IEC 14443-4\n"); } else { off += snprintf(dst + off, size - off, "* Not compliant with ISO/IEC 14443-4\n"); } - if (nai.btSak & SAK_ISO18092_COMPLIANT) { + if (pnai->btSak & SAK_ISO18092_COMPLIANT) { off += snprintf(dst + off, size - off, "* Compliant with ISO/IEC 18092\n"); } else { off += snprintf(dst + off, size - off, "* Not compliant with ISO/IEC 18092\n"); } } - if (nai.szAtsLen) { + if (pnai->szAtsLen) { off += snprintf(dst + off, size - off, " ATS: "); - off += snprint_hex(dst + off, size - off, nai.abtAts, nai.szAtsLen); + off += snprint_hex(dst + off, size - off, pnai->abtAts, pnai->szAtsLen); } - if (nai.szAtsLen && verbose) { + if (pnai->szAtsLen && verbose) { // Decode ATS according to ISO/IEC 14443-4 (5.2 Answer to select) const int iMaxFrameSizes[] = { 16, 24, 32, 40, 48, 64, 96, 128, 256 }; - off += snprintf(dst + off, size - off, "* Max Frame Size accepted by PICC: %d bytes\n", iMaxFrameSizes[nai.abtAts[0] & 0x0F]); + off += snprintf(dst + off, size - off, "* Max Frame Size accepted by PICC: %d bytes\n", iMaxFrameSizes[pnai->abtAts[0] & 0x0F]); size_t offset = 1; - if (nai.abtAts[0] & 0x10) { // TA(1) present - uint8_t TA = nai.abtAts[offset]; + if (pnai->abtAts[0] & 0x10) { // TA(1) present + uint8_t TA = pnai->abtAts[offset]; offset++; off += snprintf(dst + off, size - off, "* Bit Rate Capability:\n"); if (TA == 0) { @@ -234,8 +234,8 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, off += snprintf(dst + off, size - off, " * ERROR unknown value\n"); } } - if (nai.abtAts[0] & 0x20) { // TB(1) present - uint8_t TB = nai.abtAts[offset]; + if (pnai->abtAts[0] & 0x20) { // TB(1) present + uint8_t TB = pnai->abtAts[offset]; offset++; off += snprintf(dst + off, size - off, "* Frame Waiting Time: %.4g ms\n", 256.0 * 16.0 * (1 << ((TB & 0xf0) >> 4)) / 13560.0); if ((TB & 0x0f) == 0) { @@ -244,8 +244,8 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, off += snprintf(dst + off, size - off, "* Start-up Frame Guard Time: %.4g ms\n", 256.0 * 16.0 * (1 << (TB & 0x0f)) / 13560.0); } } - if (nai.abtAts[0] & 0x40) { // TC(1) present - uint8_t TC = nai.abtAts[offset]; + if (pnai->abtAts[0] & 0x40) { // TC(1) present + uint8_t TC = pnai->abtAts[offset]; offset++; if (TC & 0x1) { off += snprintf(dst + off, size - off, "* Node ADdress supported\n"); @@ -258,23 +258,23 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, off += snprintf(dst + off, size - off, "* Card IDentifier not supported\n"); } } - if (nai.szAtsLen > offset) { + if (pnai->szAtsLen > offset) { off += snprintf(dst + off, size - off, "* Historical bytes Tk: "); - off += snprint_hex(dst + off, size - off, nai.abtAts + offset, (nai.szAtsLen - offset)); - uint8_t CIB = nai.abtAts[offset]; + off += snprint_hex(dst + off, size - off, pnai->abtAts + offset, (pnai->szAtsLen - offset)); + uint8_t CIB = pnai->abtAts[offset]; offset++; if (CIB != 0x00 && CIB != 0x10 && (CIB & 0xf0) != 0x80) { off += snprintf(dst + off, size - off, " * Proprietary format\n"); if (CIB == 0xc1) { off += snprintf(dst + off, size - off, " * Tag byte: Mifare or virtual cards of various types\n"); - uint8_t L = nai.abtAts[offset]; + uint8_t L = pnai->abtAts[offset]; offset++; - if (L != (nai.szAtsLen - offset)) { + if (L != (pnai->szAtsLen - offset)) { off += snprintf(dst + off, size - off, " * Warning: Type Identification Coding length (%i)", L); - off += snprintf(dst + off, size - off, " not matching Tk length (%zi)\n", (nai.szAtsLen - offset)); + off += snprintf(dst + off, size - off, " not matching Tk length (%zi)\n", (pnai->szAtsLen - offset)); } - if ((nai.szAtsLen - offset - 2) > 0) { // Omit 2 CRC bytes - uint8_t CTC = nai.abtAts[offset]; + if ((pnai->szAtsLen - offset - 2) > 0) { // Omit 2 CRC bytes + uint8_t CTC = pnai->abtAts[offset]; offset++; off += snprintf(dst + off, size - off, " * Chip Type: "); switch (CTC & 0xf0) { @@ -316,8 +316,8 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, break; } } - if ((nai.szAtsLen - offset) > 0) { // Omit 2 CRC bytes - uint8_t CVC = nai.abtAts[offset]; + if ((pnai->szAtsLen - offset) > 0) { // Omit 2 CRC bytes + uint8_t CVC = pnai->abtAts[offset]; offset++; off += snprintf(dst + off, size - off, " * Chip Status: "); switch (CVC & 0xf0) { @@ -350,8 +350,8 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, break; } } - if ((nai.szAtsLen - offset) > 0) { // Omit 2 CRC bytes - uint8_t VCS = nai.abtAts[offset]; + if ((pnai->szAtsLen - offset) > 0) { // Omit 2 CRC bytes + uint8_t VCS = pnai->abtAts[offset]; offset++; off += snprintf(dst + off, size - off, " * Specifics (Virtual Card Selection):\n"); if ((VCS & 0x09) == 0x00) { @@ -379,10 +379,10 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, off += snprintf(dst + off, size - off, " See ISO/IEC 7816-4 8.1.1.3 for more info\n"); } if (CIB == 0x10) { - off += snprintf(dst + off, size - off, " * DIR data reference: %02x\n", nai.abtAts[offset]); + off += snprintf(dst + off, size - off, " * DIR data reference: %02x\n", pnai->abtAts[offset]); } if (CIB == 0x80) { - if (nai.szAtsLen == offset) { + if (pnai->szAtsLen == offset) { off += snprintf(dst + off, size - off, " * No COMPACT-TLV objects found, no status found\n"); } else { off += snprintf(dst + off, size - off, " * Tk after 0x80 consist of optional consecutive COMPACT-TLV data objects;\n"); @@ -400,9 +400,9 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, uint8_t i, j; bool found_possible_match = false; - atqa = (((uint16_t)nai.abtAtqa[0] & 0xff) << 8); - atqa += (((uint16_t)nai.abtAtqa[1] & 0xff)); - sak = ((uint8_t)nai.btSak & 0xff); + atqa = (((uint16_t)pnai->abtAtqa[0] & 0xff) << 8); + atqa += (((uint16_t)pnai->abtAtqa[1] & 0xff)); + sak = ((uint8_t)pnai->btSak & 0xff); for (i = 0; i < sizeof(const_ca) / sizeof(const_ca[0]); i++) { if ((atqa & const_ca[i].mask) == const_ca[i].atqa) { @@ -420,9 +420,9 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, // but seen in the field: off += snprintf(dst + off, size - off, "Other possible matches based on ATQA & SAK values:\n"); uint32_t atqasak = 0; - atqasak += (((uint32_t)nai.abtAtqa[0] & 0xff) << 16); - atqasak += (((uint32_t)nai.abtAtqa[1] & 0xff) << 8); - atqasak += ((uint32_t)nai.btSak & 0xff); + atqasak += (((uint32_t)pnai->abtAtqa[0] & 0xff) << 16); + atqasak += (((uint32_t)pnai->abtAtqa[1] & 0xff) << 8); + atqasak += ((uint32_t)pnai->btSak & 0xff); switch (atqasak) { case 0x000488: off += snprintf(dst + off, size - off, "* Mifare Classic 1K Infineon\n"); @@ -469,96 +469,96 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, } void -snprint_nfc_felica_info(char *dst, size_t size, const nfc_felica_info nfi, bool verbose) +snprint_nfc_felica_info(char *dst, size_t size, const nfc_felica_info *pnfi, bool verbose) { (void) verbose; int off = 0; off += snprintf(dst + off, size - off, " ID (NFCID2): "); - off += snprint_hex(dst + off, size - off, nfi.abtId, 8); + off += snprint_hex(dst + off, size - off, pnfi->abtId, 8); off += snprintf(dst + off, size - off, " Parameter (PAD): "); - off += snprint_hex(dst + off, size - off, nfi.abtPad, 8); + off += snprint_hex(dst + off, size - off, pnfi->abtPad, 8); off += snprintf(dst + off, size - off, " System Code (SC): "); - snprint_hex(dst + off, size - off, nfi.abtSysCode, 2); + snprint_hex(dst + off, size - off, pnfi->abtSysCode, 2); } void -snprint_nfc_jewel_info(char *dst, size_t size, const nfc_jewel_info nji, bool verbose) +snprint_nfc_jewel_info(char *dst, size_t size, const nfc_jewel_info *pnji, bool verbose) { (void) verbose; int off = 0; off += snprintf(dst + off, size - off, " ATQA (SENS_RES): "); - off += snprint_hex(dst + off, size - off, nji.btSensRes, 2); + off += snprint_hex(dst + off, size - off, pnji->btSensRes, 2); off += snprintf(dst + off, size - off, " 4-LSB JEWELID: "); - snprint_hex(dst + off, size - off, nji.btId, 4); + snprint_hex(dst + off, size - off, pnji->btId, 4); } #define PI_ISO14443_4_SUPPORTED 0x01 #define PI_NAD_SUPPORTED 0x01 #define PI_CID_SUPPORTED 0x02 void -snprint_nfc_iso14443b_info(char *dst, size_t size, const nfc_iso14443b_info nbi, bool verbose) +snprint_nfc_iso14443b_info(char *dst, size_t size, const nfc_iso14443b_info *pnbi, bool verbose) { int off = 0; off += snprintf(dst + off, size - off, " PUPI: "); - off += snprint_hex(dst + off, size - off, nbi.abtPupi, 4); + off += snprint_hex(dst + off, size - off, pnbi->abtPupi, 4); off += snprintf(dst + off, size - off, " Application Data: "); - off += snprint_hex(dst + off, size - off, nbi.abtApplicationData, 4); + off += snprint_hex(dst + off, size - off, pnbi->abtApplicationData, 4); off += snprintf(dst + off, size - off, " Protocol Info: "); - off += snprint_hex(dst + off, size - off, nbi.abtProtocolInfo, 3); + off += snprint_hex(dst + off, size - off, pnbi->abtProtocolInfo, 3); if (verbose) { off += snprintf(dst + off, size - off, "* Bit Rate Capability:\n"); - if (nbi.abtProtocolInfo[0] == 0) { + if (pnbi->abtProtocolInfo[0] == 0) { off += snprintf(dst + off, size - off, " * PICC supports only 106 kbits/s in both directions\n"); } - if (nbi.abtProtocolInfo[0] & 1 << 7) { + if (pnbi->abtProtocolInfo[0] & 1 << 7) { off += snprintf(dst + off, size - off, " * Same bitrate in both directions mandatory\n"); } - if (nbi.abtProtocolInfo[0] & 1 << 4) { + if (pnbi->abtProtocolInfo[0] & 1 << 4) { off += snprintf(dst + off, size - off, " * PICC to PCD, 1etu=64/fc, bitrate 212 kbits/s supported\n"); } - if (nbi.abtProtocolInfo[0] & 1 << 5) { + if (pnbi->abtProtocolInfo[0] & 1 << 5) { off += snprintf(dst + off, size - off, " * PICC to PCD, 1etu=32/fc, bitrate 424 kbits/s supported\n"); } - if (nbi.abtProtocolInfo[0] & 1 << 6) { + if (pnbi->abtProtocolInfo[0] & 1 << 6) { off += snprintf(dst + off, size - off, " * PICC to PCD, 1etu=16/fc, bitrate 847 kbits/s supported\n"); } - if (nbi.abtProtocolInfo[0] & 1 << 0) { + if (pnbi->abtProtocolInfo[0] & 1 << 0) { off += snprintf(dst + off, size - off, " * PCD to PICC, 1etu=64/fc, bitrate 212 kbits/s supported\n"); } - if (nbi.abtProtocolInfo[0] & 1 << 1) { + if (pnbi->abtProtocolInfo[0] & 1 << 1) { off += snprintf(dst + off, size - off, " * PCD to PICC, 1etu=32/fc, bitrate 424 kbits/s supported\n"); } - if (nbi.abtProtocolInfo[0] & 1 << 2) { + if (pnbi->abtProtocolInfo[0] & 1 << 2) { off += snprintf(dst + off, size - off, " * PCD to PICC, 1etu=16/fc, bitrate 847 kbits/s supported\n"); } - if (nbi.abtProtocolInfo[0] & 1 << 3) { + if (pnbi->abtProtocolInfo[0] & 1 << 3) { off += snprintf(dst + off, size - off, " * ERROR unknown value\n"); } - if ((nbi.abtProtocolInfo[1] & 0xf0) <= 0x80) { + if ((pnbi->abtProtocolInfo[1] & 0xf0) <= 0x80) { const int iMaxFrameSizes[] = { 16, 24, 32, 40, 48, 64, 96, 128, 256 }; - off += snprintf(dst + off, size - off, "* Maximum frame sizes: %d bytes\n", iMaxFrameSizes[((nbi.abtProtocolInfo[1] & 0xf0) >> 4)]); + off += snprintf(dst + off, size - off, "* Maximum frame sizes: %d bytes\n", iMaxFrameSizes[((pnbi->abtProtocolInfo[1] & 0xf0) >> 4)]); } - if ((nbi.abtProtocolInfo[1] & 0x0f) == PI_ISO14443_4_SUPPORTED) { + if ((pnbi->abtProtocolInfo[1] & 0x0f) == PI_ISO14443_4_SUPPORTED) { off += snprintf(dst + off, size - off, "* Protocol types supported: ISO/IEC 14443-4\n"); } - off += snprintf(dst + off, size - off, "* Frame Waiting Time: %.4g ms\n", 256.0 * 16.0 * (1 << ((nbi.abtProtocolInfo[2] & 0xf0) >> 4)) / 13560.0); - if ((nbi.abtProtocolInfo[2] & (PI_NAD_SUPPORTED | PI_CID_SUPPORTED)) != 0) { + off += snprintf(dst + off, size - off, "* Frame Waiting Time: %.4g ms\n", 256.0 * 16.0 * (1 << ((pnbi->abtProtocolInfo[2] & 0xf0) >> 4)) / 13560.0); + if ((pnbi->abtProtocolInfo[2] & (PI_NAD_SUPPORTED | PI_CID_SUPPORTED)) != 0) { off += snprintf(dst + off, size - off, "* Frame options supported: "); - if ((nbi.abtProtocolInfo[2] & PI_NAD_SUPPORTED) != 0) off += snprintf(dst + off, size - off, "NAD "); - if ((nbi.abtProtocolInfo[2] & PI_CID_SUPPORTED) != 0) off += snprintf(dst + off, size - off, "CID "); + if ((pnbi->abtProtocolInfo[2] & PI_NAD_SUPPORTED) != 0) off += snprintf(dst + off, size - off, "NAD "); + if ((pnbi->abtProtocolInfo[2] & PI_CID_SUPPORTED) != 0) off += snprintf(dst + off, size - off, "CID "); snprintf(dst + off, size - off, "\n"); } } } void -snprint_nfc_iso14443bi_info(char *dst, size_t size, const nfc_iso14443bi_info nii, bool verbose) +snprint_nfc_iso14443bi_info(char *dst, size_t size, const nfc_iso14443bi_info *pnii, bool verbose) { int off = 0; off += snprintf(dst + off, size - off, " DIV: "); - off += snprint_hex(dst + off, size - off, nii.abtDIV, 4); + off += snprint_hex(dst + off, size - off, pnii->abtDIV, 4); if (verbose) { - int version = (nii.btVerLog & 0x1e) >> 1; + int version = (pnii->btVerLog & 0x1e) >> 1; off += snprintf(dst + off, size - off, " Software Version: "); if (version == 15) { off += snprintf(dst + off, size - off, "Undefined\n"); @@ -566,86 +566,88 @@ snprint_nfc_iso14443bi_info(char *dst, size_t size, const nfc_iso14443bi_info ni off += snprintf(dst + off, size - off, "%i\n", version); } - if ((nii.btVerLog & 0x80) && (nii.btConfig & 0x80)) { + if ((pnii->btVerLog & 0x80) && (pnii->btConfig & 0x80)) { off += snprintf(dst + off, size - off, " Wait Enable: yes"); } } - if ((nii.btVerLog & 0x80) && (nii.btConfig & 0x40)) { + if ((pnii->btVerLog & 0x80) && (pnii->btConfig & 0x40)) { off += snprintf(dst + off, size - off, " ATS: "); - snprint_hex(dst + off, size - off, nii.abtAtr, nii.szAtrLen); + snprint_hex(dst + off, size - off, pnii->abtAtr, pnii->szAtrLen); } } void -snprint_nfc_iso14443b2sr_info(char *dst, size_t size, const nfc_iso14443b2sr_info nsi, bool verbose) +snprint_nfc_iso14443b2sr_info(char *dst, size_t size, const nfc_iso14443b2sr_info *pnsi, bool verbose) { (void) verbose; int off = 0; off += snprintf(dst + off, size - off, " UID: "); - snprint_hex(dst + off, size - off, nsi.abtUID, 8); + snprint_hex(dst + off, size - off, pnsi->abtUID, 8); } void -snprint_nfc_iso14443b2ct_info(char *dst, size_t size, const nfc_iso14443b2ct_info nci, bool verbose) +snprint_nfc_iso14443b2ct_info(char *dst, size_t size, const nfc_iso14443b2ct_info *pnci, bool verbose) { (void) verbose; int off = 0; uint32_t uid; - uid = (nci.abtUID[3] << 24) + (nci.abtUID[2] << 16) + (nci.abtUID[1] << 8) + nci.abtUID[0]; + uid = (pnci->abtUID[3] << 24) + (pnci->abtUID[2] << 16) + (pnci->abtUID[1] << 8) + pnci->abtUID[0]; off += snprintf(dst + off, size - off, " UID: "); - off += snprint_hex(dst + off, size - off, nci.abtUID, sizeof(nci.abtUID)); + off += snprint_hex(dst + off, size - off, pnci->abtUID, sizeof(pnci->abtUID)); off += snprintf(dst + off, size - off, " UID (decimal): %010u\n", uid); - off += snprintf(dst + off, size - off, " Product Code: %02X\n", nci.btProdCode); - snprintf(dst + off, size - off, " Fab Code: %02X\n", nci.btFabCode); + off += snprintf(dst + off, size - off, " Product Code: %02X\n", pnci->btProdCode); + snprintf(dst + off, size - off, " Fab Code: %02X\n", pnci->btFabCode); } void -snprint_nfc_dep_info(char *dst, size_t size, const nfc_dep_info ndi, bool verbose) +snprint_nfc_dep_info(char *dst, size_t size, const nfc_dep_info *pndi, bool verbose) { (void) verbose; int off = 0; off += snprintf(dst + off, size - off, " NFCID3: "); - off += snprint_hex(dst + off, size - off, ndi.abtNFCID3, 10); - off += snprintf(dst + off, size - off, " BS: %02x\n", ndi.btBS); - off += snprintf(dst + off, size - off, " BR: %02x\n", ndi.btBR); - off += snprintf(dst + off, size - off, " TO: %02x\n", ndi.btTO); - off += snprintf(dst + off, size - off, " PP: %02x\n", ndi.btPP); - if (ndi.szGB) { + off += snprint_hex(dst + off, size - off, pndi->abtNFCID3, 10); + off += snprintf(dst + off, size - off, " BS: %02x\n", pndi->btBS); + off += snprintf(dst + off, size - off, " BR: %02x\n", pndi->btBR); + off += snprintf(dst + off, size - off, " TO: %02x\n", pndi->btTO); + off += snprintf(dst + off, size - off, " PP: %02x\n", pndi->btPP); + if (pndi->szGB) { off += snprintf(dst + off, size - off, "General Bytes: "); - snprint_hex(dst + off, size - off, ndi.abtGB, ndi.szGB); + snprint_hex(dst + off, size - off, pndi->abtGB, pndi->szGB); } } void -snprint_nfc_target(char *dst, size_t size, const nfc_target nt, bool verbose) +snprint_nfc_target(char *dst, size_t size, const nfc_target *pnt, bool verbose) { - int off = 0; - off += snprintf(dst + off, size - off, "%s (%s%s) target:\n", str_nfc_modulation_type(nt.nm.nmt), str_nfc_baud_rate(nt.nm.nbr), (nt.nm.nmt != NMT_DEP) ? "" : (nt.nti.ndi.ndm == NDM_ACTIVE) ? "active mode" : "passive mode"); - switch (nt.nm.nmt) { - case NMT_ISO14443A: - snprint_nfc_iso14443a_info(dst + off, size - off, nt.nti.nai, verbose); - break; - case NMT_JEWEL: - snprint_nfc_jewel_info(dst + off, size - off, nt.nti.nji, verbose); - break; - case NMT_FELICA: - snprint_nfc_felica_info(dst + off, size - off, nt.nti.nfi, verbose); - break; - case NMT_ISO14443B: - snprint_nfc_iso14443b_info(dst + off, size - off, nt.nti.nbi, verbose); - break; - case NMT_ISO14443BI: - snprint_nfc_iso14443bi_info(dst + off, size - off, nt.nti.nii, verbose); - break; - case NMT_ISO14443B2SR: - snprint_nfc_iso14443b2sr_info(dst + off, size - off, nt.nti.nsi, verbose); - break; - case NMT_ISO14443B2CT: - snprint_nfc_iso14443b2ct_info(dst + off, size - off, nt.nti.nci, verbose); - break; - case NMT_DEP: - snprint_nfc_dep_info(dst + off, size - off, nt.nti.ndi, verbose); - break; + if (NULL != pnt) { + int off = 0; + off += snprintf(dst + off, size - off, "%s (%s%s) target:\n", str_nfc_modulation_type(pnt->nm.nmt), str_nfc_baud_rate(pnt->nm.nbr), (pnt->nm.nmt != NMT_DEP) ? "" : (pnt->nti.ndi.ndm == NDM_ACTIVE) ? "active mode" : "passive mode"); + switch (pnt->nm.nmt) { + case NMT_ISO14443A: + snprint_nfc_iso14443a_info(dst + off, size - off, &pnt->nti.nai, verbose); + break; + case NMT_JEWEL: + snprint_nfc_jewel_info(dst + off, size - off, &pnt->nti.nji, verbose); + break; + case NMT_FELICA: + snprint_nfc_felica_info(dst + off, size - off, &pnt->nti.nfi, verbose); + break; + case NMT_ISO14443B: + snprint_nfc_iso14443b_info(dst + off, size - off, &pnt->nti.nbi, verbose); + break; + case NMT_ISO14443BI: + snprint_nfc_iso14443bi_info(dst + off, size - off, &pnt->nti.nii, verbose); + break; + case NMT_ISO14443B2SR: + snprint_nfc_iso14443b2sr_info(dst + off, size - off, &pnt->nti.nsi, verbose); + break; + case NMT_ISO14443B2CT: + snprint_nfc_iso14443b2ct_info(dst + off, size - off, &pnt->nti.nci, verbose); + break; + case NMT_DEP: + snprint_nfc_dep_info(dst + off, size - off, &pnt->nti.ndi, verbose); + break; + } } } diff --git a/libnfc/target-subr.h b/libnfc/target-subr.h index 89710b6..a1b9d60 100644 --- a/libnfc/target-subr.h +++ b/libnfc/target-subr.h @@ -28,14 +28,14 @@ #define _TARGET_SUBR_H_ int snprint_hex(char *dst, size_t size, const uint8_t *pbtData, const size_t szLen); -void snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info nai, bool verbose); -void snprint_nfc_iso14443b_info(char *dst, size_t size, const nfc_iso14443b_info nbi, bool verbose); -void snprint_nfc_iso14443bi_info(char *dst, size_t size, const nfc_iso14443bi_info nii, bool verbose); -void snprint_nfc_iso14443b2sr_info(char *dst, size_t size, const nfc_iso14443b2sr_info nsi, bool verbose); -void snprint_nfc_iso14443b2ct_info(char *dst, size_t size, const nfc_iso14443b2ct_info nci, bool verbose); -void snprint_nfc_felica_info(char *dst, size_t size, const nfc_felica_info nfi, bool verbose); -void snprint_nfc_jewel_info(char *dst, size_t size, const nfc_jewel_info nji, bool verbose); -void snprint_nfc_dep_info(char *dst, size_t size, const nfc_dep_info ndi, bool verbose); -void snprint_nfc_target(char *dst, size_t size, const nfc_target nt, bool verbose); +void snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info *pnai, bool verbose); +void snprint_nfc_iso14443b_info(char *dst, size_t size, const nfc_iso14443b_info *pnbi, bool verbose); +void snprint_nfc_iso14443bi_info(char *dst, size_t size, const nfc_iso14443bi_info *pnii, bool verbose); +void snprint_nfc_iso14443b2sr_info(char *dst, size_t size, const nfc_iso14443b2sr_info *pnsi, bool verbose); +void snprint_nfc_iso14443b2ct_info(char *dst, size_t size, const nfc_iso14443b2ct_info *pnci, bool verbose); +void snprint_nfc_felica_info(char *dst, size_t size, const nfc_felica_info *pnfi, bool verbose); +void snprint_nfc_jewel_info(char *dst, size_t size, const nfc_jewel_info *pnji, bool verbose); +void snprint_nfc_dep_info(char *dst, size_t size, const nfc_dep_info *pndi, bool verbose); +void snprint_nfc_target(char *dst, size_t size, const nfc_target *pnt, bool verbose); #endif diff --git a/utils/nfc-list.c b/utils/nfc-list.c index 47d485c..251ede2 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -143,7 +143,7 @@ main(int argc, const char *argv[]) printf("%d ISO14443A passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } for (n = 0; n < res; n++) { - print_nfc_target(ant[n], verbose); + print_nfc_target(&ant[n], verbose); printf("\n"); } } @@ -157,7 +157,7 @@ main(int argc, const char *argv[]) printf("%d Felica (212 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } for (n = 0; n < res; n++) { - print_nfc_target(ant[n], verbose); + print_nfc_target(&ant[n], verbose); printf("\n"); } } @@ -169,7 +169,7 @@ main(int argc, const char *argv[]) printf("%d Felica (424 kbps) passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } for (n = 0; n < res; n++) { - print_nfc_target(ant[n], verbose); + print_nfc_target(&ant[n], verbose); printf("\n"); } } @@ -183,7 +183,7 @@ main(int argc, const char *argv[]) printf("%d ISO14443B passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } for (n = 0; n < res; n++) { - print_nfc_target(ant[n], verbose); + print_nfc_target(&ant[n], verbose); printf("\n"); } } @@ -197,7 +197,7 @@ main(int argc, const char *argv[]) printf("%d ISO14443B' passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } for (n = 0; n < res; n++) { - print_nfc_target(ant[n], verbose); + print_nfc_target(&ant[n], verbose); printf("\n"); } } @@ -211,7 +211,7 @@ main(int argc, const char *argv[]) printf("%d ISO14443B-2 ST SRx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } for (n = 0; n < res; n++) { - print_nfc_target(ant[n], verbose); + print_nfc_target(&ant[n], verbose); printf("\n"); } } @@ -225,7 +225,7 @@ main(int argc, const char *argv[]) printf("%d ISO14443B-2 ASK CTx passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } for (n = 0; n < res; n++) { - print_nfc_target(ant[n], verbose); + print_nfc_target(&ant[n], verbose); printf("\n"); } } @@ -239,7 +239,7 @@ main(int argc, const char *argv[]) printf("%d Jewel passive target(s) found%s\n", res, (res == 0) ? ".\n" : ":"); } for (n = 0; n < res; n++) { - print_nfc_target(ant[n], verbose); + print_nfc_target(&ant[n], verbose); printf("\n"); } } diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index 938aef7..964bfeb 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -558,7 +558,7 @@ main(int argc, const char *argv[]) } } printf("Found MIFARE Classic card:\n"); - print_nfc_target(nt, false); + print_nfc_target(&nt, false); // Guessing size if ((nt.nti.nai.abtAtqa[1] & 0x02) == 0x02) diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 18ec0fa..ed180ca 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -271,7 +271,7 @@ main(int argc, char *argv[]) } printf("Found tag:\n"); - print_nfc_target(ntRealTarget, false); + print_nfc_target(&ntRealTarget, false); if (initiator_only_mode) { if (print_hex_fd4(ntRealTarget.nti.nai.abtUid, ntRealTarget.nti.nai.szUidLen, "UID") < 0) { fprintf(stderr, "Error while printing UID to FD4\n"); @@ -372,7 +372,7 @@ main(int argc, char *argv[]) memcpy(&(ntEmulatedTarget.nti.nai.abtAts[4]), pbtTkt, szTk); printf("We will emulate:\n"); - print_nfc_target(ntEmulatedTarget, false); + print_nfc_target(&ntEmulatedTarget, false); // Try to open the NFC emulator device if (swap_devices) { diff --git a/utils/nfc-utils.c b/utils/nfc-utils.c index a56b0f7..4d714bf 100644 --- a/utils/nfc-utils.c +++ b/utils/nfc-utils.c @@ -115,10 +115,10 @@ print_hex_par(const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDat } void -print_nfc_target(const nfc_target nt, bool verbose) +print_nfc_target(const nfc_target *pnt, bool verbose) { char *s; - str_nfc_target(&s, nt, verbose); + str_nfc_target(&s, pnt, verbose); printf("%s", s); nfc_free(s); } diff --git a/utils/nfc-utils.h b/utils/nfc-utils.h index c92fbc2..6203d0f 100644 --- a/utils/nfc-utils.h +++ b/utils/nfc-utils.h @@ -94,6 +94,6 @@ void print_hex(const uint8_t *pbtData, const size_t szLen); void print_hex_bits(const uint8_t *pbtData, const size_t szBits); void print_hex_par(const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDataPar); -void print_nfc_target(const nfc_target nt, bool verbose); +void print_nfc_target(const nfc_target *pnt, bool verbose); #endif From 181cd914d5d52ae11d6a8348af1ba57736ba0bd3 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Thu, 7 Mar 2013 08:51:35 +0100 Subject: [PATCH 34/43] pn53x-sam: fix print_nfc_target --- examples/pn53x-sam.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index f4baabe..c6eea4f 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -175,7 +175,7 @@ main(int argc, const char *argv[]) exit(EXIT_FAILURE); } else if (res == 1) { printf("The following ISO14443A tag (SAM) was found:\n"); - print_nfc_target(nt, true); + print_nfc_target(&nt, true); } else { ERR("%s", "More than one ISO14442 tag found as SAM."); nfc_close(pnd); From d8a93217ffd7f287a9d42f89fca4a995d6527ad1 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Thu, 7 Mar 2013 09:17:27 +0100 Subject: [PATCH 35/43] make style --- examples/pn53x-tamashell.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index e817145..8a6f575 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -50,11 +50,11 @@ #ifndef _WIN32 # include # define msleep(x) do { \ - struct timespec xsleep; \ - xsleep.tv_sec = x / 1000; \ - xsleep.tv_nsec = (x - xsleep.tv_sec * 1000) * 1000 * 1000; \ - nanosleep(&xsleep, NULL); \ - } while (0) + struct timespec xsleep; \ + xsleep.tv_sec = x / 1000; \ + xsleep.tv_nsec = (x - xsleep.tv_sec * 1000) * 1000 * 1000; \ + nanosleep(&xsleep, NULL); \ + } while (0) #else # include # define msleep Sleep From bd961222a30828d7d6ec7f844e33ba957c30d3dc Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Thu, 7 Mar 2013 09:27:45 +0100 Subject: [PATCH 36/43] API change --- NEWS | 8 ++++++++ debian/libnfc4.symbols | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 70638b9..59a18a6 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,11 @@ +New in 1.7.0-***: + +API Changes: + + * Functions + - nfc_initiator_target_is_present() & str_nfc_target() + now take a pointer to nfc_target as argument + New in 1.7.0-rc5: API Changes: diff --git a/debian/libnfc4.symbols b/debian/libnfc4.symbols index df9dc8e..4db3519 100644 --- a/debian/libnfc4.symbols +++ b/debian/libnfc4.symbols @@ -30,7 +30,7 @@ libnfc.so.4 libnfc4 #MINVER# nfc_initiator_poll_target@Base 1.7.0~rc2 nfc_initiator_select_dep_target@Base 1.7.0~rc2 nfc_initiator_select_passive_target@Base 1.7.0~rc2 - nfc_initiator_target_is_present@Base 1.7.0~rc2 + nfc_initiator_target_is_present@Base 1.7.0~rc6-0 nfc_initiator_transceive_bits@Base 1.7.0~rc2 nfc_initiator_transceive_bits_timed@Base 1.7.0~rc2 nfc_initiator_transceive_bytes@Base 1.7.0~rc2 @@ -53,4 +53,4 @@ libnfc.so.4 libnfc4 #MINVER# pn53x_write_register@Base 1.7.0~rc2 str_nfc_baud_rate@Base 1.7.0~rc2 str_nfc_modulation_type@Base 1.7.0~rc2 - str_nfc_target@Base 1.7.0~rc2 + str_nfc_target@Base 1.7.0~rc6-0 From 7b917f9a8b55e26ee9a4ed9b2dfc2b9618196142 Mon Sep 17 00:00:00 2001 From: Alex Lian Date: Mon, 4 Mar 2013 01:26:23 -0500 Subject: [PATCH 37/43] Windows: Clean up all compiler warnings and link warnings - Fixed the suppression of the auto-fixup for linking against MS built libs - Fixed all the formatting warnings by shifting to inttypes.h specifiers - shifted to %lu for DWORD printf --- CMakeLists.txt | 6 +++--- examples/nfc-poll.c | 3 ++- examples/nfc-relay.c | 3 ++- libnfc/buses/uart_win32.c | 5 +++-- libnfc/chips/pn53x.c | 7 ++++--- libnfc/drivers/acr122_pcsc.c | 2 +- libnfc/drivers/acr122_usb.c | 2 +- libnfc/drivers/acr122s.c | 2 +- libnfc/drivers/arygon.c | 4 ++-- libnfc/drivers/pn532_uart.c | 2 +- libnfc/drivers/pn53x_usb.c | 2 +- libnfc/target-subr.c | 7 ++++--- utils/nfc-relay-picc.c | 5 +++-- 13 files changed, 28 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ba2dc3b..4e203c1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,9 +79,9 @@ ADD_DEFINITIONS(-Du_int8_t=uint8_t -Du_int16_t=uint16_t) IF(MINGW) # force MinGW-w64 in 32bit mode SET(CMAKE_C_FLAGS "-m32 ${CMAKE_C_FLAGS}") - SET(CMAKE_MODULE_LINKER_FLAGS "-m32 --enable-stdcall-fixup ${CMAKE_SHARED_LINKER_FLAGS}") - SET(CMAKE_SHARED_LINKER_FLAGS "-m32 --enable-stdcall-fixup ${CMAKE_SHARED_LINKER_FLAGS}") - SET(CMAKE_EXE_LINKER_FLAGS "-m32 --enable-stdcall-fixup ${CMAKE_EXE_LINKER_FLAGS}") + SET(CMAKE_MODULE_LINKER_FLAGS "-m32 -Wl,--enable-stdcall-fixup ${CMAKE_SHARED_LINKER_FLAGS}") + SET(CMAKE_SHARED_LINKER_FLAGS "-m32 -Wl,--enable-stdcall-fixup ${CMAKE_SHARED_LINKER_FLAGS}") + SET(CMAKE_EXE_LINKER_FLAGS "-m32 -Wl,--enable-stdcall-fixup ${CMAKE_EXE_LINKER_FLAGS}") SET(CMAKE_RC_FLAGS "--target=pe-i386 --output-format=coff ${CMAKE_RC_FLAGS}") ENDIF(MINGW) diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index 40cd87d..6ef1805 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -38,6 +38,7 @@ #endif // HAVE_CONFIG_H #include +#include #include #include #include @@ -128,7 +129,7 @@ main(int argc, const char *argv[]) } printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); - printf("NFC device will poll during %ld ms (%u pollings of %lu ms for %zd modulations)\n", (unsigned long) uiPollNr * szModulations * uiPeriod * 150, uiPollNr, (unsigned long) uiPeriod * 150, szModulations); + printf("NFC device will poll during %ld ms (%u pollings of %lu ms for %" PRIdPTR " modulations)\n", (unsigned long) uiPollNr * szModulations * uiPeriod * 150, uiPollNr, (unsigned long) uiPeriod * 150, szModulations); if ((res = nfc_initiator_poll_target(pnd, nmModulations, szModulations, uiPollNr, uiPeriod, &nt)) < 0) { nfc_perror(pnd, "nfc_initiator_poll_target"); nfc_close(pnd); diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 8dbdcd9..9d1061c 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -37,6 +37,7 @@ # include "config.h" #endif /* HAVE_CONFIG_H */ +#include #include #include #include @@ -119,7 +120,7 @@ main(int argc, char *argv[]) size_t szFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT); if (szFound < 2) { - ERR("%zd device found but two opened devices are needed to relay NFC.", szFound); + ERR("%" PRIdPTR " device found but two opened devices are needed to relay NFC.", szFound); nfc_exit(context); exit(EXIT_FAILURE); } diff --git a/libnfc/buses/uart_win32.c b/libnfc/buses/uart_win32.c index f14e28d..0c675ad 100644 --- a/libnfc/buses/uart_win32.c +++ b/libnfc/buses/uart_win32.c @@ -24,6 +24,7 @@ * @brief Windows UART driver */ +#include #include "log.h" #define LOG_GROUP NFC_LOG_GROUP_COM @@ -164,7 +165,7 @@ uart_receive(serial_port sp, uint8_t *pbtRx, const size_t szRx, void *abort_p, i log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to apply new timeout settings."); return NFC_EIO; } - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Timeouts are set to %u ms", timeout_ms); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Timeouts are set to %lu ms", timeout_ms); // TODO Enhance the reception method // - According to MSDN, it could be better to implement nfc_abort_command() mecanism using Cancello() @@ -179,7 +180,7 @@ uart_receive(serial_port sp, uint8_t *pbtRx, const size_t szRx, void *abort_p, i if (!res) { DWORD err = GetLastError(); - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "ReadFile error: %u", err); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "ReadFile error: %lu", err); return NFC_EIO; } else if (dwBytesReceived == 0) { return NFC_ETIMEOUT; diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 96c4bda..a4a5153 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -28,6 +28,7 @@ # include "config.h" #endif // HAVE_CONFIG_H +#include #include #include #include @@ -1354,7 +1355,7 @@ pn53x_initiator_transceive_bytes(struct nfc_device *pnd, const uint8_t *pbtTx, c const size_t szRxLen = (size_t)res - 1; if (pbtRx != NULL) { if (szRxLen > szRx) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Buffer size is too short: %zuo available(s), %zuo needed", szRx, szRxLen); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Buffer size is too short: %" PRIuPTR " available(s), %" PRIuPTR " needed", szRx, szRxLen); return NFC_EOVFLOW; } // Copy the received bytes @@ -1629,7 +1630,7 @@ pn53x_initiator_transceive_bytes_timed(struct nfc_device *pnd, const uint8_t *pb } if (pbtRx != NULL) { if ((szRxLen + sz) > szRx) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Buffer size is too short: %zuo available(s), %zuo needed", szRx, szRxLen + sz); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Buffer size is too short: %" PRIuPTR " available(s), %" PRIuPTR " needed", szRx, szRxLen + sz); return NFC_EOVFLOW; } // Copy the received bytes @@ -2711,7 +2712,7 @@ pn53x_build_frame(uint8_t *pbtFrame, size_t *pszFrame, const uint8_t *pbtData, c (*pszFrame) = szData + PN53x_EXTENDED_FRAME__OVERHEAD; } else { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "We can't send more than %d bytes in a raw (requested: %zd)", PN53x_EXTENDED_FRAME__DATA_MAX_LEN, szData); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "We can't send more than %d bytes in a raw (requested: %" PRIdPTR ")", PN53x_EXTENDED_FRAME__DATA_MAX_LEN, szData); return NFC_ECHIP; } return NFC_SUCCESS; diff --git a/libnfc/drivers/acr122_pcsc.c b/libnfc/drivers/acr122_pcsc.c index b8337ea..4341aad 100644 --- a/libnfc/drivers/acr122_pcsc.c +++ b/libnfc/drivers/acr122_pcsc.c @@ -255,7 +255,7 @@ acr122_pcsc_open(const nfc_context *context, const nfc_connstring connstring) if (strlen(ndd.pcsc_device_name) < 5) { // We can assume it's a reader ID as pcsc_name always ends with "NN NN" // Device was not specified, only ID, retrieve it size_t index; - if (sscanf(ndd.pcsc_device_name, "%4lu", &index) != 1) + if (sscanf(ndd.pcsc_device_name, "%4" SCNuPTR, &index) != 1) return NULL; nfc_connstring *ncs = malloc(sizeof(nfc_connstring) * (index + 1)); if (!ncs) { diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c index d2c439f..93e65f3 100644 --- a/libnfc/drivers/acr122_usb.c +++ b/libnfc/drivers/acr122_usb.c @@ -718,7 +718,7 @@ read: len -= 4; // We skip 2 bytes for PN532 direction byte (D5) and command byte (CMD+1), then 2 bytes for APDU status (90 00). if (len > szDataLen) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %" PRIuPTR ", len: %" PRIuPTR ")", szDataLen, len); pnd->last_error = NFC_EOVFLOW; return pnd->last_error; } diff --git a/libnfc/drivers/acr122s.c b/libnfc/drivers/acr122s.c index c84a8da..3daf30b 100644 --- a/libnfc/drivers/acr122s.c +++ b/libnfc/drivers/acr122s.c @@ -684,7 +684,7 @@ acr122s_receive(nfc_device *pnd, uint8_t *buf, size_t buf_len, int timeout) size_t data_len = FRAME_SIZE(tmp) - 17; if (data_len > buf_len) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Receive buffer too small. (buf_len: %zu, data_len: %zu)", buf_len, data_len); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Receive buffer too small. (buf_len: %" PRIuPTR ", data_len: %" PRIuPTR ")", buf_len, data_len); pnd->last_error = NFC_EIO; return pnd->last_error; } diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 315fc0b..729b0b8 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -330,7 +330,7 @@ arygon_tama_send(nfc_device *pnd, const uint8_t *pbtData, const size_t szData, i size_t szFrame = 0; if (szData > PN53x_NORMAL_FRAME__DATA_MAX_LEN) { // ARYGON Reader with PN532 equipped does not support extended frame (bug in ARYGON firmware?) - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "ARYGON device does not support more than %d bytes as payload (requested: %zd)", PN53x_NORMAL_FRAME__DATA_MAX_LEN, szData); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "ARYGON device does not support more than %d bytes as payload (requested: %" PRIdPTR ")", PN53x_NORMAL_FRAME__DATA_MAX_LEN, szData); pnd->last_error = NFC_EDEVNOTSUPP; return pnd->last_error; } @@ -438,7 +438,7 @@ arygon_tama_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, i } if (len > szDataLen) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %" PRIuPTR ", len: %" PRIuPTR ")", szDataLen, len); pnd->last_error = NFC_EIO; return pnd->last_error; } diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 4ae4380..6816558 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -424,7 +424,7 @@ pn532_uart_receive(nfc_device *pnd, uint8_t *pbtData, const size_t szDataLen, in } if (len > szDataLen) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %" PRIuPTR ", len: %" PRIuPTR ")", szDataLen, len); pnd->last_error = NFC_EIO; goto error; } diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 1d229c5..edc9eee 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -588,7 +588,7 @@ read: } if (len > szDataLen) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %" PRIuPTR ", len: %" PRIuPTR ")", szDataLen, len); pnd->last_error = NFC_EIO; return pnd->last_error; } diff --git a/libnfc/target-subr.c b/libnfc/target-subr.c index b46d6bc..9429839 100644 --- a/libnfc/target-subr.c +++ b/libnfc/target-subr.c @@ -23,6 +23,7 @@ * @file target-subr.c * @brief Target-related subroutines. (ie. determine target type, print target, etc.) */ +#include #include #include "target-subr.h" @@ -248,9 +249,9 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info *pna uint8_t TC = pnai->abtAts[offset]; offset++; if (TC & 0x1) { - off += snprintf(dst + off, size - off, "* Node ADdress supported\n"); + off += snprintf(dst + off, size - off, "* Node Address supported\n"); } else { - off += snprintf(dst + off, size - off, "* Node ADdress not supported\n"); + off += snprintf(dst + off, size - off, "* Node Address not supported\n"); } if (TC & 0x2) { off += snprintf(dst + off, size - off, "* Card IDentifier supported\n"); @@ -271,7 +272,7 @@ snprint_nfc_iso14443a_info(char *dst, size_t size, const nfc_iso14443a_info *pna offset++; if (L != (pnai->szAtsLen - offset)) { off += snprintf(dst + off, size - off, " * Warning: Type Identification Coding length (%i)", L); - off += snprintf(dst + off, size - off, " not matching Tk length (%zi)\n", (pnai->szAtsLen - offset)); + off += snprintf(dst + off, size - off, " not matching Tk length (%" PRIdPTR ")\n", (pnai->szAtsLen - offset)); } if ((pnai->szAtsLen - offset - 2) > 0) { // Omit 2 CRC bytes uint8_t CTC = pnai->abtAts[offset]; diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index ed180ca..dd92607 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -42,6 +42,7 @@ # include "config.h" #endif /* HAVE_CONFIG_H */ +#include #include #include #include @@ -100,7 +101,7 @@ static int print_hex_fd4(const uint8_t *pbtData, const size_t szBytes, const cha if (szBytes > MAX_FRAME_LEN) { return -1; } - if (fprintf(fd4, "#%s %04zx: ", pchPrefix, szBytes) < 0) { + if (fprintf(fd4, "#%s %04" PRIxPTR ": ", pchPrefix, szBytes) < 0) { return -1; } @@ -225,7 +226,7 @@ main(int argc, char *argv[]) } } else { if (szFound < 2) { - ERR("%zd device found but two opened devices are needed to relay NFC.", szFound); + ERR("%" PRIdPTR " device found but two opened devices are needed to relay NFC.", szFound); nfc_exit(context); exit(EXIT_FAILURE); } From 563054d2a29d7cdcae154f7972cb8e88e9c1e598 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Thu, 7 Mar 2013 10:34:17 +0100 Subject: [PATCH 38/43] More explicitely ignoring return value as suggested by Ludovic --- libnfc/buses/uart_posix.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libnfc/buses/uart_posix.c b/libnfc/buses/uart_posix.c index fc32c12..081d399 100644 --- a/libnfc/buses/uart_posix.c +++ b/libnfc/buses/uart_posix.c @@ -127,8 +127,7 @@ uart_flush_input(serial_port sp) } char *rx = malloc(available_bytes_count); // There is something available, read the data - res = read(UART_DATA(sp)->fd, rx, available_bytes_count); - (void) res; + (void)read(UART_DATA(sp)->fd, rx, available_bytes_count); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%d bytes have eatten.", available_bytes_count); free(rx); } From b3c6ea86ad1e032703d11a0389ee7abb26913ef9 Mon Sep 17 00:00:00 2001 From: Alex Lian Date: Sun, 3 Mar 2013 17:38:19 -0500 Subject: [PATCH 39/43] Split logging internals so that platforms may choose additional or alternate spew mechanisms In the case of windows, allow for the OutputDebugString call in addition to logging to stderr. Useful for the dll scenario and debugging in Visual Studio. --- libnfc/CMakeLists.txt | 7 +++++- libnfc/Makefile.am | 4 +++- libnfc/log-printf.c | 19 ++++++++++++--- libnfc/log_posix.c | 41 +++++++++++++++++++++++++++++++ libnfc/log_win32.c | 56 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 libnfc/log_posix.c create mode 100644 libnfc/log_win32.c diff --git a/libnfc/CMakeLists.txt b/libnfc/CMakeLists.txt index 81fb023..50fff78 100644 --- a/libnfc/CMakeLists.txt +++ b/libnfc/CMakeLists.txt @@ -46,7 +46,12 @@ SET(LIBRARY_SOURCES nfc nfc-device nfc-emulation nfc-internal conf iso14443-subr INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) IF(LIBNFC_LOG) - LIST(APPEND LIBRARY_SOURCES log-printf) + IF(WIN32) + SET(CMAKE_C_FLAGS "-fgnu89-inline ${CMAKE_C_FLAGS}") + LIST(APPEND LIBRARY_SOURCES log-printf log_win32) + ELSE(WIN32) + LIST(APPEND LIBRARY_SOURCES log-printf log_posix) + ENDIF(WIN32) ENDIF(LIBNFC_LOG) ADD_LIBRARY(nfc SHARED ${LIBRARY_SOURCES}) diff --git a/libnfc/Makefile.am b/libnfc/Makefile.am index 7fc31ed..c6098a6 100644 --- a/libnfc/Makefile.am +++ b/libnfc/Makefile.am @@ -45,5 +45,7 @@ endif EXTRA_DIST = \ CMakeLists.txt \ - log-printf.c + log-printf.c \ + log_posix.c \ + log_win32.c diff --git a/libnfc/log-printf.c b/libnfc/log-printf.c index e8ef238..a0c1513 100644 --- a/libnfc/log-printf.c +++ b/libnfc/log-printf.c @@ -1,6 +1,7 @@ /*- * Copyright (C) 2011 Romain Tartière * Copyright (C) 2011, 2012 Romuald Conty + * Copyright (C) 2013 Alex Lian * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the @@ -31,6 +32,12 @@ #error "No logging defined, but log-printf.c still compiled." #else // LOG +// Internal methods so different platforms can route the logging +// Offering both forms of the variadic function +// These are implemented in the log_ specific file +void log_put_internal(const char *format, ...); +void log_vput_internal(const char *format, va_list args); + void log_init(const nfc_context *context) { @@ -71,11 +78,17 @@ log_put(const uint8_t group, const char *category, const uint8_t priority, const if (log_level) { // If log is not disabled by log_level=none if (((log_level & 0x00000003) >= priority) || // Global log level (((log_level >> (group * 2)) & 0x00000003) >= priority)) { // Group log level + va_list va; va_start(va, format); - fprintf(stderr, "%s\t%s\t", log_priority_to_str(priority), category); - vfprintf(stderr, format, va); - fprintf(stderr, "\n"); + +// fprintf(stderr, "%s\t%s\t", log_priority_to_str(priority), category); +// vfprintf(stderr, format, va); +// fprintf(stderr, "\n"); + + log_put_internal("%s\t%s\t", log_priority_to_str(priority), category); + log_vput_internal(format, va); + log_put_internal("\n"); va_end(va); } } diff --git a/libnfc/log_posix.c b/libnfc/log_posix.c new file mode 100644 index 0000000..3e963d2 --- /dev/null +++ b/libnfc/log_posix.c @@ -0,0 +1,41 @@ +/*- + * Copyright (C) 2011 Romain Tartière + * Copyright (C) 2011, 2012 Romuald Conty + * Copyright (C) 2013 Alex Lian + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see + */ + +#include "log.h" + +#include +#include +#include +#include +#include + +void +log_vput_internal(const char *format, va_list args) +{ + vfprintf(stderr, format, args); +} + +void +log_put_internal(const char *format, ...) +{ + va_list va; + va_start(va, format); + vfprintf(stderr, format, va); + va_end(va); +} diff --git a/libnfc/log_win32.c b/libnfc/log_win32.c new file mode 100644 index 0000000..3713cfb --- /dev/null +++ b/libnfc/log_win32.c @@ -0,0 +1,56 @@ +/*- + * Copyright (C) 2011 Romain Tartière + * Copyright (C) 2011, 2012 Romuald Conty + * Copyright (C) 2013 Alex Lian + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see + */ + +#include "log.h" + +#include +#include +#include +#include +#include +#include + +void +log_output_debug(const char *format, va_list args) +{ + char buffer[1024]; + HRESULT hr = StringCbVPrintf( buffer, sizeof( buffer ), format, args ); + // Spew what we got, even if the buffer is not sized large enough + if ( (STRSAFE_E_INSUFFICIENT_BUFFER == hr) || (S_OK == hr) ) + OutputDebugString( buffer ); +} + +void +log_vput_internal(const char *format, va_list args) +{ + vfprintf(stderr, format, args); + // Additional windows output to the debug window for debugging purposes + log_output_debug(format, args); +} + +void +log_put_internal(const char *format, ...) +{ + va_list va; + va_start(va, format); + vfprintf(stderr, format, va); + // Additional windows output to the debug window for debugging purposes + log_output_debug(format, va); + va_end(va); +} From 37cf7554d95aaa68d5827352c0a735bd2adf3469 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Fri, 8 Mar 2013 00:34:14 +0100 Subject: [PATCH 40/43] Add log_posix to Makefile.am --- libnfc/Makefile.am | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libnfc/Makefile.am b/libnfc/Makefile.am index c6098a6..a5ad329 100644 --- a/libnfc/Makefile.am +++ b/libnfc/Makefile.am @@ -40,7 +40,7 @@ if LIBUSB_ENABLED endif if WITH_LOG - libnfc_la_SOURCES += log-printf.c + libnfc_la_SOURCES += log-printf.c log_posix.c endif EXTRA_DIST = \ @@ -48,4 +48,3 @@ EXTRA_DIST = \ log-printf.c \ log_posix.c \ log_win32.c - From fef6517e27e4385b0254d13c9b27095734553bbc Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Fri, 8 Mar 2013 00:35:04 +0100 Subject: [PATCH 41/43] make style --- libnfc/log-printf.c | 5 ----- libnfc/log_win32.c | 6 +++--- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/libnfc/log-printf.c b/libnfc/log-printf.c index a0c1513..3f912be 100644 --- a/libnfc/log-printf.c +++ b/libnfc/log-printf.c @@ -81,11 +81,6 @@ log_put(const uint8_t group, const char *category, const uint8_t priority, const va_list va; va_start(va, format); - -// fprintf(stderr, "%s\t%s\t", log_priority_to_str(priority), category); -// vfprintf(stderr, format, va); -// fprintf(stderr, "\n"); - log_put_internal("%s\t%s\t", log_priority_to_str(priority), category); log_vput_internal(format, va); log_put_internal("\n"); diff --git a/libnfc/log_win32.c b/libnfc/log_win32.c index 3713cfb..81196a7 100644 --- a/libnfc/log_win32.c +++ b/libnfc/log_win32.c @@ -30,10 +30,10 @@ void log_output_debug(const char *format, va_list args) { char buffer[1024]; - HRESULT hr = StringCbVPrintf( buffer, sizeof( buffer ), format, args ); + HRESULT hr = StringCbVPrintf(buffer, sizeof(buffer), format, args); // Spew what we got, even if the buffer is not sized large enough - if ( (STRSAFE_E_INSUFFICIENT_BUFFER == hr) || (S_OK == hr) ) - OutputDebugString( buffer ); + if ((STRSAFE_E_INSUFFICIENT_BUFFER == hr) || (S_OK == hr)) + OutputDebugString(buffer); } void From 5704b668b7a5fefc189777462a3b3bb707a90414 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Fri, 8 Mar 2013 00:35:36 +0100 Subject: [PATCH 42/43] Add usbbus to Makefile.am EXTRA_DIST --- libnfc/buses/Makefile.am | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnfc/buses/Makefile.am b/libnfc/buses/Makefile.am index 58f193c..2adaf9c 100644 --- a/libnfc/buses/Makefile.am +++ b/libnfc/buses/Makefile.am @@ -18,4 +18,4 @@ if LIBUSB_ENABLED libnfcbuses_la_CFLAGS += @libusb_CFLAGS@ libnfcbuses_la_LIBADD += @libusb_LIBS@ endif - +EXTRA_DIST += usbbus.c usbbus.h From 3ac1d64b4fa3d65319d807521b80d4ddcd4de1c4 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Fri, 8 Mar 2013 08:15:40 +0100 Subject: [PATCH 43/43] Windows: workaround as libusb is not automatically detected by CMake Fixes issue 238 --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e203c1..cce782f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -119,6 +119,7 @@ ENDIF(PCSC_INCLUDE_DIRS) IF(LIBUSB_INCLUDE_DIRS) INCLUDE_DIRECTORIES(${LIBUSB_INCLUDE_DIRS}) LINK_DIRECTORIES(${LIBUSB_LIBRARY_DIRS}) + SET(LIBUSB_FOUND TRUE) ENDIF(LIBUSB_INCLUDE_DIRS) # version.rc for Windows