diff --git a/NEWS b/NEWS index 35ae8d1..bba1b79 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,8 @@ str_nfc_baud_rate() - New nfc_device_get_information_about() function to retreive some device's information + - No more in/out function parameter: nfc_initiator_transceive_bytes() now + take a constant size for Rx buffer New in 1.6.0-rc1: diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index 87aa1c4..9a947c6 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -104,14 +104,15 @@ printf ("Sent bits: "); print_hex (pbtTx, szTx); } + int res; // Transmit the command bytes - if (nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, 0) < 0) + if ((res = nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, sizeof(abtRx), 0)) < 0) return false; // Show received answer if (!quiet_output) { printf ("Received bits: "); - print_hex (abtRx, szRx); + print_hex (abtRx, res); } // Succesful transfer return true; diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index 0d74915..0bc622f 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -64,7 +64,6 @@ { nfc_target nt; uint8_t abtRx[MAX_FRAME_LEN]; - size_t szRx = sizeof(abtRx); uint8_t abtTx[] = "Hello World!"; if (argc > 1) { @@ -95,12 +94,13 @@ print_nfc_target (nt, false); printf ("Sending: %s\n", abtTx); - if (nfc_initiator_transceive_bytes (pnd, abtTx, sizeof(abtTx), abtRx, &szRx, 0) < 0) { + 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; } - abtRx[szRx] = 0; + abtRx[res] = 0; printf ("Received: %s\n", abtRx); if (nfc_initiator_deselect_target (pnd) < 0) { diff --git a/examples/nfc-mfsetuid.c b/examples/nfc-mfsetuid.c index 2ad89ed..137445e 100644 --- a/examples/nfc-mfsetuid.c +++ b/examples/nfc-mfsetuid.c @@ -58,7 +58,6 @@ static uint8_t abtRx[MAX_FRAME_LEN]; static int szRxBits; -static size_t szRx = sizeof(abtRx); static uint8_t abtRawUid[12]; static uint8_t abtAtqa[2]; static uint8_t abtSak; @@ -117,14 +116,15 @@ printf ("Sent bits: "); print_hex (pbtTx, szTx); } + int res; // Transmit the command bytes - if (nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, 0) < 0) + if ((res = nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, sizeof(abtRx), 0)) < 0) return false; // Show received answer if (!quiet_output) { printf ("Received bits: "); - print_hex (abtRx, szRx); + print_hex (abtRx, res); } // Succesful transfer return true; diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index 75dbc3c..06d77fc 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -82,7 +82,7 @@ NFC_EXPORT int nfc_initiator_select_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout); NFC_EXPORT int nfc_initiator_poll_dep_target (nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout); NFC_EXPORT int nfc_initiator_deselect_target (nfc_device *pnd); - NFC_EXPORT int nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, size_t *pszRx, int timeout); + NFC_EXPORT int nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, const size_t szRx, int timeout); 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, 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, 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, uint8_t *pbtRxPar, uint32_t *cycles); diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index dbf800e..6a29417 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -1003,7 +1003,7 @@ int timeout) { uint8_t abtTargetsData[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; - size_t szTargetsData = sizeof(abtTargetsData); + size_t szTargetsData; int res = 0; if (nm.nmt == NMT_ISO14443BI || nm.nmt == NMT_ISO14443B2SR || nm.nmt == NMT_ISO14443B2CT) { @@ -1027,37 +1027,35 @@ // Some work to do before getting the UID... uint8_t abtInitiate[]="\x06\x00"; size_t szInitiateLen = 2; - uint8_t abtSelect[]="\x0e\x00"; - size_t szSelectLen = 2; + uint8_t abtSelect[] = { 0x0e, 0x00 }; uint8_t abtRx[1]; - size_t szRxLen = 1; // Getting random Chip_ID - if ((res = pn53x_initiator_transceive_bytes (pnd, abtInitiate, szInitiateLen, abtRx, &szRxLen, timeout)) < 0) { + if ((res = pn53x_initiator_transceive_bytes (pnd, abtInitiate, szInitiateLen, abtRx, sizeof(abtRx), timeout)) < 0) { return res; } abtSelect[1] = abtRx[0]; - if ((res = pn53x_initiator_transceive_bytes (pnd, abtSelect, szSelectLen, abtRx, &szRxLen, timeout)) < 0) { + if ((res = pn53x_initiator_transceive_bytes (pnd, abtSelect, sizeof(abtSelect), abtRx, sizeof(abtRx), timeout)) < 0) { return res; } + szTargetsData = (size_t)res; } else if (nm.nmt == NMT_ISO14443B2CT) { // Some work to do before getting the UID... - uint8_t abtReqt[]="\x10"; - size_t szReqtLen = 1; + const uint8_t abtReqt[]= { 0x10 }; // Getting product code / fab code & store it in output buffer after the serial nr we'll obtain later - if ((res = pn53x_initiator_transceive_bytes (pnd, abtReqt, szReqtLen, abtTargetsData+2, &szTargetsData, timeout)) < 0) { + if ((res = pn53x_initiator_transceive_bytes (pnd, abtReqt, sizeof(abtReqt), abtTargetsData+2, sizeof(abtTargetsData)-2, timeout)) < 0) { return res; } - } - if ((res = pn53x_initiator_transceive_bytes (pnd, pbtInitData, szInitData, abtTargetsData, &szTargetsData, timeout)) < 0) { + szTargetsData = (size_t)res; + } + if ((res = pn53x_initiator_transceive_bytes (pnd, pbtInitData, szInitData, abtTargetsData, sizeof(abtTargetsData), timeout)) < 0) { return res; } if (nm.nmt == NMT_ISO14443B2CT) { if (szTargetsData != 2) - return NFC_ECHIP; - uint8_t abtRead[]="\xC4"; // Reading UID_MSB (Read address 4) - size_t szReadLen = 1; - if ((res = pn53x_initiator_transceive_bytes (pnd, abtRead, szReadLen, abtTargetsData+4, &szTargetsData, timeout) < 0)) { + return NFC_ECHIP; // FIXME: It should not return a NFC_ECHIP here! + uint8_t abtRead[]= { 0xC4 }; // Reading UID_MSB (Read address 4) + if ((res = pn53x_initiator_transceive_bytes (pnd, abtRead, sizeof(abtRead), abtTargetsData+4, sizeof(abtTargetsData)-4, timeout) < 0)) { return res; } szTargetsData = 6; // u16 UID_LSB, u8 prod code, u8 fab code, u16 UID_MSB @@ -1072,12 +1070,12 @@ if (nm.nmt == NMT_ISO14443BI) { // Select tag uint8_t abtAttrib[6]; - size_t szAttribLen = sizeof(abtAttrib); - memcpy(abtAttrib, abtTargetsData, szAttribLen); + memcpy(abtAttrib, abtTargetsData, sizeof(abtAttrib)); abtAttrib[1] = 0x0f; // ATTRIB - if ((res = pn53x_initiator_transceive_bytes (pnd, abtAttrib, szAttribLen, NULL, NULL, timeout)) < 0) { + if ((res = pn53x_initiator_transceive_bytes (pnd, abtAttrib, sizeof(abtAttrib), NULL, 0, timeout)) < 0) { return res; } + szTargetsData = (size_t)res; } return abtTargetsData[0]; } // else: @@ -1284,7 +1282,7 @@ int pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, - size_t *pszRx, int timeout) + const size_t szRx, int timeout) { size_t szExtraTxLen; uint8_t abtCmd[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; @@ -1317,22 +1315,20 @@ // Send the frame to the PN53X chip and get the answer // We have to give the amount of bytes + (the two command bytes 0xD4, 0x42) uint8_t abtRx[PN53x_EXTENDED_FRAME__DATA_MAX_LEN]; - size_t szRx = sizeof(abtRx); - - if ((res = pn53x_transceive (pnd, abtCmd, szTx + szExtraTxLen, abtRx, szRx, timeout)) < 0) { + if ((res = pn53x_transceive (pnd, abtCmd, szTx + szExtraTxLen, abtRx, sizeof(abtRx), timeout)) < 0) { pnd->last_error = res; return pnd->last_error; } - szRx = (size_t) res; + const size_t szRxLen = (size_t)res - 1; if (pbtRx != NULL) { - // Save the received byte count - *pszRx = szRx - 1; - + if (szRxLen > szRx) { + return NFC_EOVFLOW; + } // Copy the received bytes - memcpy (pbtRx, abtRx + 1, *pszRx); + memcpy (pbtRx, abtRx + 1, szRxLen); } // Everything went successful, we return received bytes count - return (szRx - 1); + return szRxLen; } static void __pn53x_init_timer(struct nfc_device *pnd, const uint32_t max_cycles) diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 35dc388..c141ebf 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -328,7 +328,7 @@ int pn53x_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 pn53x_initiator_transceive_bytes (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, - uint8_t *pbtRx, size_t *pszRx, int timeout); + uint8_t *pbtRx, const size_t szRx, int timeout); int pn53x_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 pn53x_initiator_transceive_bytes_timed (struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, diff --git a/libnfc/nfc-internal.h b/libnfc/nfc-internal.h index 1815902..375694b 100644 --- a/libnfc/nfc-internal.h +++ b/libnfc/nfc-internal.h @@ -145,7 +145,7 @@ int (*initiator_poll_target) (struct nfc_device *pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt); int (*initiator_select_dep_target) (struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout); int (*initiator_deselect_target) (struct nfc_device *pnd); - int (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, size_t * pszRx, int timeout); + int (*initiator_transceive_bytes) (struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, const size_t szRx, int timeout); 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, 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); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 7a6fa7c..d90ea5c 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -597,7 +597,7 @@ * @param pbtTx contains a byte array of the frame that needs to be transmitted. * @param szTx contains the length in bytes. * @param[out] pbtRx response from the tags - * @param pszRx size of \a pbtRx + * @param szRx size of \a pbtRx (Will return NFC_EOVFLOW if RX exceeds this size) * @param timeout in milliseconds * * The NFC device (configured as initiator) will transmit the supplied bytes (\a pbtTx) to the target. @@ -617,9 +617,9 @@ */ int nfc_initiator_transceive_bytes (nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, - size_t *pszRx, int timeout) -{ - HAL (initiator_transceive_bytes, pnd, pbtTx, szTx, pbtRx, pszRx, timeout) + const size_t szRx, int timeout) +{ + HAL (initiator_transceive_bytes, pnd, pbtTx, szTx, pbtRx, szRx, timeout) } /** @ingroup initiator diff --git a/utils/mifare.c b/utils/mifare.c index be46e28..e51e9ee 100644 --- a/utils/mifare.c +++ b/utils/mifare.c @@ -54,7 +54,6 @@ nfc_initiator_mifare_cmd (nfc_device *pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param *pmp) { uint8_t abtRx[265]; - size_t szRx = sizeof(abtRx); size_t szParamLen; uint8_t abtCmd[265]; //bool bEasyFraming; @@ -105,7 +104,7 @@ } // Fire the mifare command int res; - if ((res = nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, &szRx, -1)) < 0) { + if ((res = nfc_initiator_transceive_bytes (pnd, abtCmd, 2 + szParamLen, abtRx, sizeof(abtRx), -1)) < 0) { if (res == NFC_ERFTRANS) { // "Invalid received frame", usual means we are // authenticated on a sector but the requested MIFARE cmd (read, write) @@ -126,7 +125,7 @@ // When we have executed a read command, copy the received bytes into the param if (mc == MC_READ) { - if (szRx == 16) { + if (res == 16) { memcpy (pmp->mpd.abtData, abtRx, 16); } else { return false; diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index a08d5cd..acfaf2a 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -83,7 +83,6 @@ static uint8_t abtRx[MAX_FRAME_LEN]; static int szRxBits; -static size_t szRx = sizeof(abtRx); uint8_t abtHalt[4] = { 0x50, 0x00, 0x00, 0x00 }; @@ -116,12 +115,13 @@ printf ("Sent bits: "); print_hex (pbtTx, szTx); // Transmit the command bytes - if (nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, &szRx, 0) < 0) + int res; + if ((res = nfc_initiator_transceive_bytes (pnd, pbtTx, szTx, abtRx, sizeof(abtRx), 0)) < 0) return false; // Show received answer printf ("Received bits: "); - print_hex (abtRx, szRx); + print_hex (abtRx, res); // Succesful transfer return true; } diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index 18d485d..922bf21 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -111,40 +111,39 @@ size_t frame_len = sizeof(frame); build_felica_frame (nt.nti.nfi, CHECK, payload, payload_len, frame, &frame_len); - uint8_t res[1024]; - - size_t res_len; - if (nfc_initiator_transceive_bytes (dev, frame, frame_len, res, &res_len, 0) < 0) { - return -1; - } - const size_t res_overhead = 1 + 1 + 8 + 2; // 1+1+8+2: LEN + CMD + NFCID2 + STATUS - if (res_len < res_overhead) { + uint8_t rx[1024]; + int res; + if ((res = nfc_initiator_transceive_bytes (dev, frame, frame_len, rx, sizeof(rx), 0)) < 0) { + return res; + } + const int res_overhead = 1 + 1 + 8 + 2; // 1+1+8+2: LEN + CMD + NFCID2 + STATUS + if (res < res_overhead) { // Not enough data return -1; } - uint8_t felica_res_len = res[0]; - if (res_len != felica_res_len) { + uint8_t felica_res_len = rx[0]; + if (res != felica_res_len) { // Error while receiving felica frame return -1; } - if ((CHECK + 1) != res[1]) { + if ((CHECK + 1) != rx[1]) { // Command return does not match return -1; } - if (0 != memcmp (&res[2], nt.nti.nfi.abtId, 8)) { + if (0 != memcmp (&rx[2], nt.nti.nfi.abtId, 8)) { // NFCID2 does not match return -1; } - const uint8_t status_flag1 = res[10]; - const uint8_t status_flag2 = res[11]; + const uint8_t status_flag1 = rx[10]; + const uint8_t status_flag2 = rx[11]; if ((status_flag1) || (status_flag2)) { // Felica card's error fprintf (stderr, "Status bytes: %02x, %02x\n", status_flag1, status_flag2); return -1; } // const uint8_t res_block_count = res[12]; - *data_len = res_len - res_overhead + 1; // +1 => block count is stored on 1 byte - memcpy (data, &res[res_overhead+1], *data_len); + *data_len = res - res_overhead + 1; // +1 => block count is stored on 1 byte + memcpy (data, &rx[res_overhead+1], *data_len); return *data_len; } diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index c3cadfa..288ab1d 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -365,9 +365,8 @@ } printf ("NFC emulator device: %s opened\n", nfc_device_get_name (pndTarget)); - - szCapduLen = sizeof (abtCapdu); - if (nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, szCapduLen, 0) < 0) { + int res; + if ((res = nfc_target_init (pndTarget, &ntEmulatedTarget, abtCapdu, sizeof(abtCapdu), 0)) < 0) { ERR ("%s", "Initialization of NFC emulator failed"); if (!target_only_mode) { nfc_close (pndInitiator); @@ -378,7 +377,6 @@ } printf ("%s\n", "Done, relaying frames now!"); } - while (!quitting) { bool ret; @@ -419,8 +417,12 @@ if (!target_only_mode) { // Forward the frame to the original tag - ret = (nfc_initiator_transceive_bytes - (pndInitiator, abtCapdu, szCapduLen, abtRapdu, &szRapduLen, 0) < 0) ? 0 : 1; + if ((res = nfc_initiator_transceive_bytes (pndInitiator, abtCapdu, szCapduLen, abtRapdu, sizeof(abtRapdu), -1) < 0)) { + ret = false; + } else { + szCapduLen = (size_t) res; + ret = true; + } } else { if (scan_hex_fd3(abtRapdu, &szRapduLen, "R-APDU") != EXIT_SUCCESS) { fprintf (stderr, "Error while scanning R-APDU from FD3\n");