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
This commit is contained in:
Philippe Teuwen 2013-03-05 19:44:59 +01:00
parent 232930c3d5
commit bece73faaf
21 changed files with 433 additions and 298 deletions

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -178,7 +178,7 @@ main(int argc, const char *argv[])
printf("r|w - Perform read from or write to card\n");
printf("<dump.mfd> - 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);
}

View file

@ -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);
}

View file

@ -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);

View file

@ -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);
}