From bece73faaf62d10b66023eb446c4beac1c520f92 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Tue, 5 Mar 2013 19:44:59 +0100 Subject: [PATCH] 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); }