From c3a5fba0283b59352537aa71947a4481009a54bb Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Mon, 19 Aug 2013 14:55:27 +0200 Subject: [PATCH] Adding iso14443b_crc_append() --- include/nfc/nfc.h | 2 ++ libnfc/iso14443-subr.c | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/include/nfc/nfc.h b/include/nfc/nfc.h index b34a005..44f92d9 100644 --- a/include/nfc/nfc.h +++ b/include/nfc/nfc.h @@ -135,6 +135,8 @@ NFC_EXPORT int nfc_device_set_property_bool(nfc_device *pnd, const nfc_property /* Misc. functions */ NFC_EXPORT void iso14443a_crc(uint8_t *pbtData, size_t szLen, uint8_t *pbtCrc); NFC_EXPORT void iso14443a_crc_append(uint8_t *pbtData, size_t szLen); +NFC_EXPORT void iso14443b_crc(uint8_t *pbtData, size_t szLen, uint8_t *pbtCrc); +NFC_EXPORT void iso14443b_crc_append(uint8_t *pbtData, size_t szLen); NFC_EXPORT uint8_t *iso14443a_locate_historical_bytes(uint8_t *pbtAts, size_t szAts, size_t *pszTk); NFC_EXPORT void nfc_free(void *p); diff --git a/libnfc/iso14443-subr.c b/libnfc/iso14443-subr.c index 3f29bdf..27a1ba7 100644 --- a/libnfc/iso14443-subr.c +++ b/libnfc/iso14443-subr.c @@ -41,7 +41,7 @@ /** - * @brief CRC + * @brief CRC_A * */ void @@ -62,7 +62,7 @@ iso14443a_crc(uint8_t *pbtData, size_t szLen, uint8_t *pbtCrc) } /** - * @brief Append CRC + * @brief Append CRC_A * */ void @@ -71,6 +71,37 @@ iso14443a_crc_append(uint8_t *pbtData, size_t szLen) iso14443a_crc(pbtData, szLen, pbtData + szLen); } +/** + * @brief CRC_B + * + */ +void +iso14443b_crc(uint8_t *pbtData, size_t szLen, uint8_t *pbtCrc) +{ + uint8_t bt; + uint32_t wCrc = 0xFFFF; + + do { + bt = *pbtData++; + bt = (bt ^ (uint8_t)(wCrc & 0x00FF)); + bt = (bt ^ (bt << 4)); + wCrc = (wCrc >> 8) ^ ((uint32_t) bt << 8) ^ ((uint32_t) bt << 3) ^ ((uint32_t) bt >> 4); + } while (--szLen); + wCrc = ~wCrc; + *pbtCrc++ = (uint8_t)(wCrc & 0xFF); + *pbtCrc = (uint8_t)((wCrc >> 8) & 0xFF); +} + +/** + * @brief Append CRC_B + * + */ +void +iso14443b_crc_append(uint8_t *pbtData, size_t szLen) +{ + iso14443b_crc(pbtData, szLen, pbtData + szLen); +} + /** * @brief Locate historical bytes * @see ISO/IEC 14443-4 (5.2.7 Historical bytes)