diff --git a/libfreefare/freefare_internal.h b/libfreefare/freefare_internal.h index 0425bb5..bd1175d 100644 --- a/libfreefare/freefare_internal.h +++ b/libfreefare/freefare_internal.h @@ -315,14 +315,16 @@ struct mifare_ultralight_tag { */ #define BUFFER_INIT(buffer_name, size) \ uint8_t buffer_name[size]; \ + size_t __##buffer_name##_size = size; \ size_t __##buffer_name##_n = 0 /* * Create a wrapper for an existing buffer. * BEWARE! It eats children! */ -#define BUFFER_ALIAS(buffer_name, origin) \ +#define BUFFER_ALIAS(buffer_name, origin, origin_size) \ uint8_t *buffer_name = (void *)origin; \ + size_t __##buffer_name##_size = origin_size; \ size_t __##buffer_name##_n = 0; #define BUFFER_SIZE(buffer_name) (__##buffer_name##_n) diff --git a/libfreefare/mifare_classic.c b/libfreefare/mifare_classic.c index afcf54a..3cfb367 100644 --- a/libfreefare/mifare_classic.c +++ b/libfreefare/mifare_classic.c @@ -89,12 +89,14 @@ do { \ errno = 0; \ DEBUG_XFER (msg, __##msg##_n, "===> "); \ - if ((nfc_initiator_transceive_bytes (tag->device, msg, __##msg##_n, res, &__##res##_n, 0) < 0)) { \ + int _res; \ + if ((_res = nfc_initiator_transceive_bytes (tag->device, msg, __##msg##_n, res, __##res##_size, 0)) < 0) { \ if (disconnect) { \ tag->active = false; \ } \ return errno = EIO, -1; \ } \ + __##res##_n = _res; \ DEBUG_XFER (res, __##res##_n, "<=== "); \ } while (0) @@ -308,7 +310,7 @@ mifare_classic_read (MifareTag tag, const MifareClassicBlockNumber block, Mifare ASSERT_MIFARE_CLASSIC (tag); BUFFER_INIT (cmd, 2); - BUFFER_ALIAS (res, data); + BUFFER_ALIAS (res, data, sizeof(MifareClassicBlock)); BUFFER_APPEND (cmd, MC_READ); BUFFER_APPEND (cmd, block); diff --git a/libfreefare/mifare_desfire.c b/libfreefare/mifare_desfire.c index 119838c..cb812ea 100644 --- a/libfreefare/mifare_desfire.c +++ b/libfreefare/mifare_desfire.c @@ -172,9 +172,11 @@ static ssize_t read_data (MifareTag tag, uint8_t command, uint8_t file_no, off_ MIFARE_DESFIRE (tag)->last_picc_error = OPERATION_OK; \ MIFARE_DESFIRE (tag)->last_pcd_error = OPERATION_OK; \ DEBUG_XFER (__msg, __len, "===> "); \ - if ((nfc_initiator_transceive_bytes (tag->device, __msg, __len, __res, &__##res##_n, 0)) < 0) { \ + int _res; \ + if ((_res = nfc_initiator_transceive_bytes (tag->device, __msg, __len, __res, __##res##_size, 0)) < 0) { \ return errno = EIO, -1; \ } \ + __##res##_n = _res; \ DEBUG_XFER (__res, __##res##_n, "<=== "); \ res[__##res##_n-2] = __res[__##res##_n-1]; \ __##res##_n--; \ diff --git a/libfreefare/mifare_ultralight.c b/libfreefare/mifare_ultralight.c index 963a3f1..63304db 100644 --- a/libfreefare/mifare_ultralight.c +++ b/libfreefare/mifare_ultralight.c @@ -62,9 +62,11 @@ do { \ errno = 0; \ DEBUG_XFER (msg, __##msg##_n, "===> "); \ - if ((nfc_initiator_transceive_bytes (tag->device, msg, __##msg##_n, res, &__##res##_n, 0)) < 0) { \ + int _res; \ + if ((_res = nfc_initiator_transceive_bytes (tag->device, msg, __##msg##_n, res, __##res##_size, 0)) < 0) { \ return errno = EIO, -1; \ } \ + __##res##_n = _res; \ DEBUG_XFER (res, __##res##_n, "<=== "); \ } while (0) @@ -76,10 +78,12 @@ return -1; \ } \ DEBUG_XFER (msg, __##msg##_n, "===> "); \ - if ((nfc_initiator_transceive_bytes (tag->device, msg, __##msg##_n, res, &__##res##_n, 0)) < 0) { \ + int _res; \ + if ((_res = nfc_initiator_transceive_bytes (tag->device, msg, __##msg##_n, res, __##res##_size, 0)) < 0) { \ nfc_device_set_property_bool (tag->device, NP_EASY_FRAMING, true); \ return errno = EIO, -1; \ } \ + __##res##_n = _res; \ DEBUG_XFER (res, __##res##_n, "<=== "); \ if (nfc_device_set_property_bool (tag->device, NP_EASY_FRAMING, true) < 0) { \ errno = EIO; \ @@ -183,7 +187,7 @@ mifare_ultralight_read (MifareTag tag, MifareUltralightPageNumber page, MifareUl if (!MIFARE_ULTRALIGHT(tag)->cached_pages[page]) { BUFFER_INIT (cmd, 2); - BUFFER_ALIAS (res, MIFARE_ULTRALIGHT(tag)->cache[page]); + BUFFER_ALIAS (res, MIFARE_ULTRALIGHT(tag)->cache[page], sizeof(MifareUltralightPage)); BUFFER_APPEND (cmd, 0x30); BUFFER_APPEND (cmd, page); @@ -322,8 +326,7 @@ is_mifare_ultralightc_on_reader (nfc_device *device, nfc_iso14443a_info nai) }; nfc_initiator_select_passive_target (device, modulation, nai.abtUid, nai.szUidLen, &pnti); nfc_device_set_property_bool (device, NP_EASY_FRAMING, false); - size_t n; - ret = nfc_initiator_transceive_bytes (device, cmd_step1, sizeof (cmd_step1), res_step1, &n, 0); + ret = nfc_initiator_transceive_bytes (device, cmd_step1, sizeof (cmd_step1), res_step1, sizeof(res_step1), 0); nfc_device_set_property_bool (device, NP_EASY_FRAMING, true); nfc_initiator_deselect_target (device); return ret >= 0;