Enhance Mifare DESFire AID code.

- Fix endianness problems;
- Enforce AID validity;
- Simplify code.
This commit is contained in:
Romain Tartiere 2010-09-03 10:55:57 +00:00
parent 087db1b298
commit 5b1d51bd55

View file

@ -16,6 +16,23 @@
* *
* $Id$ * $Id$
*/ */
#include "config.h"
#if defined(HAVE_SYS_TYPES_H)
# include <sys/types.h>
#endif
#if defined(HAVE_SYS_ENDIAN_H)
# include <sys/endian.h>
#endif
#if defined(HAVE_ENDIAN_H)
# include <endian.h>
#endif
#if defined(HAVE_BYTESWAP_H)
# include <byteswap.h>
#endif
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
@ -28,11 +45,14 @@
MifareDESFireAID MifareDESFireAID
mifare_desfire_aid_new (uint32_t aid) mifare_desfire_aid_new (uint32_t aid)
{ {
if (aid > 0x00ffffff)
return errno = EINVAL, NULL;
MifareDESFireAID res; MifareDESFireAID res;
uint32_t aid_le = htole32 (aid);
if ((res = malloc (sizeof (*res)))) { if ((res = malloc (sizeof (*res)))) {
// XXX We may take care of endianess memcpy(res->data, ((uint8_t*)&aid_le), 3);
memcpy(res->data, ((uint8_t*)&aid), 3);
} }
return res; return res;
@ -42,18 +62,9 @@ mifare_desfire_aid_new (uint32_t aid)
MifareDESFireAID MifareDESFireAID
mifare_desfire_aid_new_with_mad_aid (MadAid mad_aid, uint8_t n) mifare_desfire_aid_new_with_mad_aid (MadAid mad_aid, uint8_t n)
{ {
if (n > 0xf)
MifareDESFireAID res;
if (n & 0xf0)
return errno = EINVAL, NULL; return errno = EINVAL, NULL;
if ((res = malloc (sizeof (*res)))) { return mifare_desfire_aid_new (0xf00000 | (mad_aid.function_cluster_code << 12) | (mad_aid.application_code << 4) | n);
res->data[0] = 0xf0 | (mad_aid.function_cluster_code >> 4);
res->data[1] = (uint8_t) (((mad_aid.function_cluster_code & 0x0f) << 4) | ((mad_aid.application_code & 0xf0) >> 4));
res->data[2] = ((mad_aid.application_code & 0x0f) << 4) | n;
}
return res;
} }