astyle --formatted --mode=c --indent=spaces=2 --indent-switches --indent-preprocessor --keep-one-line-blocks --max-instatement-indent=60 --brackets=linux --pad-oper --unpad-paren --pad-header

This commit is contained in:
Philippe Teuwen 2012-05-29 15:54:36 +00:00
parent 562205cc14
commit 01303fab0d
59 changed files with 3178 additions and 3178 deletions

View file

@ -63,45 +63,45 @@ static const nfc_modulation nmMifare = {
};
static void
print_success_or_failure (bool bFailure, uint32_t * uiCounter)
print_success_or_failure(bool bFailure, uint32_t * uiCounter)
{
printf ("%c", (bFailure) ? 'x' : '.');
printf("%c", (bFailure) ? 'x' : '.');
if (uiCounter)
*uiCounter += (bFailure) ? 0 : 1;
}
static bool
read_card (void)
read_card(void)
{
uint32_t page;
bool bFailure = false;
uint32_t uiReadedPages = 0;
printf ("Reading %d pages |", uiBlocks + 1);
printf("Reading %d pages |", uiBlocks + 1);
for (page = 0; page <= uiBlocks; page += 4) {
// Try to read out the data block
if (nfc_initiator_mifare_cmd (pnd, MC_READ, page, &mp)) {
memcpy (mtDump.amb[page / 4].mbd.abtData, mp.mpd.abtData, 16);
if (nfc_initiator_mifare_cmd(pnd, MC_READ, page, &mp)) {
memcpy(mtDump.amb[page / 4].mbd.abtData, mp.mpd.abtData, 16);
} else {
bFailure = true;
break;
}
print_success_or_failure (bFailure, &uiReadedPages);
print_success_or_failure (bFailure, &uiReadedPages);
print_success_or_failure (bFailure, &uiReadedPages);
print_success_or_failure (bFailure, &uiReadedPages);
print_success_or_failure(bFailure, &uiReadedPages);
print_success_or_failure(bFailure, &uiReadedPages);
print_success_or_failure(bFailure, &uiReadedPages);
print_success_or_failure(bFailure, &uiReadedPages);
}
printf ("|\n");
printf ("Done, %d of %d pages readed.\n", uiReadedPages, uiBlocks + 1);
fflush (stdout);
printf("|\n");
printf("Done, %d of %d pages readed.\n", uiReadedPages, uiBlocks + 1);
fflush(stdout);
return (!bFailure);
}
static bool
write_card (void)
write_card(void)
{
uint32_t uiBlock = 0;
bool bFailure = false;
@ -112,38 +112,38 @@ write_card (void)
bool write_otp;
bool write_lock;
printf ("Write OTP bytes ? [yN] ");
if (!fgets (buffer, BUFSIZ, stdin)) {
ERR ("Unable to read standard input.");
printf("Write OTP bytes ? [yN] ");
if (!fgets(buffer, BUFSIZ, stdin)) {
ERR("Unable to read standard input.");
}
write_otp = ((buffer[0] == 'y') || (buffer[0] == 'Y'));
printf ("Write Lock bytes ? [yN] ");
if (!fgets (buffer, BUFSIZ, stdin)) {
ERR ("Unable to read standard input.");
printf("Write Lock bytes ? [yN] ");
if (!fgets(buffer, BUFSIZ, stdin)) {
ERR("Unable to read standard input.");
}
write_lock = ((buffer[0] == 'y') || (buffer[0] == 'Y'));
printf ("Writing %d pages |", uiBlocks + 1);
printf("Writing %d pages |", uiBlocks + 1);
/* We need to skip 2 first pages. */
printf ("ss");
printf("ss");
uiSkippedPages = 2;
for (int page = 0x2; page <= 0xF; page++) {
if ((page == 0x2) && (!write_lock)) {
printf ("s");
printf("s");
uiSkippedPages++;
continue;
}
if ((page == 0x3) && (!write_otp)) {
printf ("s");
printf("s");
uiSkippedPages++;
continue;
}
// Show if the readout went well
if (bFailure) {
// When a failure occured we need to redo the anti-collision
if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) {
ERR ("tag was removed");
if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) < 0) {
ERR("tag was removed");
return false;
}
bFailure = false;
@ -153,123 +153,123 @@ write_card (void)
// page (4 bytes). The Ultralight-specific Write command only
// writes one page at a time.
uiBlock = page / 4;
memcpy (mp.mpd.abtData, mtDump.amb[uiBlock].mbd.abtData + ((page % 4) * 4), 16);
if (!nfc_initiator_mifare_cmd (pnd, MC_WRITE, page, &mp))
memcpy(mp.mpd.abtData, mtDump.amb[uiBlock].mbd.abtData + ((page % 4) * 4), 16);
if (!nfc_initiator_mifare_cmd(pnd, MC_WRITE, page, &mp))
bFailure = true;
print_success_or_failure (bFailure, &uiWritenPages);
print_success_or_failure(bFailure, &uiWritenPages);
}
printf ("|\n");
printf ("Done, %d of %d pages written (%d pages skipped).\n", uiWritenPages, uiBlocks + 1, uiSkippedPages);
printf("|\n");
printf("Done, %d of %d pages written (%d pages skipped).\n", uiWritenPages, uiBlocks + 1, uiSkippedPages);
return true;
}
int
main (int argc, const char *argv[])
main(int argc, const char *argv[])
{
bool bReadAction;
FILE *pfDump;
if (argc < 3) {
printf ("\n");
printf ("%s r|w <dump.mfd>\n", argv[0]);
printf ("\n");
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");
printf("\n");
printf("%s r|w <dump.mfd>\n", argv[0]);
printf("\n");
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;
}
DBG ("\nChecking arguments and settings\n");
DBG("\nChecking arguments and settings\n");
bReadAction = tolower ((int) ((unsigned char) * (argv[1])) == 'r');
bReadAction = tolower((int)((unsigned char) * (argv[1])) == 'r');
if (bReadAction) {
memset (&mtDump, 0x00, sizeof (mtDump));
memset(&mtDump, 0x00, sizeof(mtDump));
} else {
pfDump = fopen (argv[2], "rb");
pfDump = fopen(argv[2], "rb");
if (pfDump == NULL) {
ERR ("Could not open dump file: %s\n", argv[2]);
ERR("Could not open dump file: %s\n", argv[2]);
return 1;
}
if (fread (&mtDump, 1, sizeof (mtDump), pfDump) != sizeof (mtDump)) {
ERR ("Could not read from dump file: %s\n", argv[2]);
fclose (pfDump);
if (fread(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) {
ERR("Could not read from dump file: %s\n", argv[2]);
fclose(pfDump);
return 1;
}
fclose (pfDump);
fclose(pfDump);
}
DBG ("Successfully opened the dump file\n");
DBG("Successfully opened the dump file\n");
nfc_init (NULL);
nfc_init(NULL);
// Try to open the NFC device
pnd = nfc_open (NULL, NULL);
pnd = nfc_open(NULL, NULL);
if (pnd == NULL) {
ERR ("Error opening NFC device\n");
ERR("Error opening NFC device\n");
return 1;
}
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");
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");
exit (EXIT_FAILURE);
if (nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false) < 0) {
nfc_perror(pnd, "nfc_device_set_property_bool");
exit(EXIT_FAILURE);
}
printf ("NFC device: %s opened\n", nfc_device_get_name (pnd));
printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
// Try to find a MIFARE Ultralight tag
if (nfc_initiator_select_passive_target (pnd, nmMifare, NULL, 0, &nt) < 0) {
ERR ("no tag was found\n");
nfc_close (pnd);
nfc_exit (NULL);
if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) < 0) {
ERR("no tag was found\n");
nfc_close(pnd);
nfc_exit(NULL);
return 1;
}
// Test if we are dealing with a MIFARE compatible tag
if (nt.nti.nai.abtAtqa[1] != 0x44) {
ERR ("tag is not a MIFARE Ultralight card\n");
nfc_close (pnd);
nfc_exit (NULL);
ERR("tag is not a MIFARE Ultralight card\n");
nfc_close(pnd);
nfc_exit(NULL);
return EXIT_FAILURE;
}
// Get the info from the current tag
printf ("Found MIFARE Ultralight card with UID: ");
printf("Found MIFARE Ultralight card with UID: ");
size_t szPos;
for (szPos = 0; szPos < nt.nti.nai.szUidLen; szPos++) {
printf ("%02x", nt.nti.nai.abtUid[szPos]);
printf("%02x", nt.nti.nai.abtUid[szPos]);
}
printf("\n");
if (bReadAction) {
if (read_card ()) {
printf ("Writing data to file: %s ... ", argv[2]);
fflush (stdout);
pfDump = fopen (argv[2], "wb");
if (read_card()) {
printf("Writing data to file: %s ... ", argv[2]);
fflush(stdout);
pfDump = fopen(argv[2], "wb");
if (pfDump == NULL) {
printf ("Could not open file: %s\n", argv[2]);
printf("Could not open file: %s\n", argv[2]);
return EXIT_FAILURE;
}
if (fwrite (&mtDump, 1, sizeof (mtDump), pfDump) != sizeof (mtDump)) {
printf ("Could not write to file: %s\n", argv[2]);
if (fwrite(&mtDump, 1, sizeof(mtDump), pfDump) != sizeof(mtDump)) {
printf("Could not write to file: %s\n", argv[2]);
return EXIT_FAILURE;
}
fclose (pfDump);
printf ("Done.\n");
fclose(pfDump);
printf("Done.\n");
}
} else {
write_card ();
write_card();
}
nfc_close (pnd);
nfc_exit (NULL);
nfc_close(pnd);
nfc_exit(NULL);
return EXIT_SUCCESS;
}