From 5d62cee7d7c3a9bc02163b166ad3d41ee5c3a0d3 Mon Sep 17 00:00:00 2001 From: Romain Tartiere Date: Mon, 26 Jul 2010 14:12:32 +0000 Subject: [PATCH] Merge r489 from the libfreefare-desfire branch. --- libfreefare/freefare_internal.h | 52 ++++++++++++++++++++++++++------- libfreefare/mifare_classic.c | 5 ++++ libfreefare/tlv.c | 5 ++++ 3 files changed, 52 insertions(+), 10 deletions(-) diff --git a/libfreefare/freefare_internal.h b/libfreefare/freefare_internal.h index c80d059..2741bab 100644 --- a/libfreefare/freefare_internal.h +++ b/libfreefare/freefare_internal.h @@ -22,21 +22,53 @@ #include "config.h" +/* + * Endienness macros + * + * POSIX does not describe any API for endianness problems, and solutions are + * mostly vendor-dependant. Some operating systems provide a complete + * framework for this (FreeBSD, OpenBSD), some provide nothing in the base + * system (Mac OS), GNU/Linux systems may or may not provide macros to do the + * conversion depending on the version of the libc. + * + * This is a PITA but unfortunately we have no other solution than doing all + * this gymnastic. Depending of what is defined if one or more of endian.h, + * sys/endian.h and byteswap.h was included, define a set of macros to stick to + * the set of macros provided by FreeBSD (it's a historic choice: development + * was done on this operating system when endianness problems first had to be + * dealt with). + */ + #if !defined(le32toh) && defined(letoh32) # define le32toh(x) letoh32(x) #endif -#if defined(HAVE_BYTESWAP_H) -#include -#if !defined(le32toh) || !defined(htole32) - #if BYTE_ORDER == LITTLE_ENDIAN - #define le32toh(x) (x) - #define htole32(x) bswap_32(x) - #else - #define le32toh(x) bswap_32(x) - #define htole32(x) (x) - #endif +#if !defined(le32toh) && defined(bswap_32) +# if BYTE_ORDER == LITTLE_ENDIAN +# define be32toh(x) bswap_32(x) +# define htobe32(x) bswap_32(x) +# define le32toh(x) (x) +# define htole32(x) (x) +# else +# define be32toh(x) (x) +# define htobe32(x) (x) +# define le32toh(x) bswap_32(x) +# define htole32(x) bswap_32(x) +# endif #endif + +#if !defined(htole16) && defined(bswap_16) +# if BYTE_ORDER == LITTLE_ENDIAN +# define be16toh(x) (bswap_16(x)) +# define htobe16(x) (bswap_16(x)) +# define htole16(x) (x) +# define le16toh(x) (x) +# else +# define be16toh(x) (x) +# define htobe16(x) (x) +# define htole16(x) (bswap_16(x)) +# define le16toh(x) (bswap_16(x)) +# endif #endif diff --git a/libfreefare/mifare_classic.c b/libfreefare/mifare_classic.c index a4c1db4..43b8cfa 100644 --- a/libfreefare/mifare_classic.c +++ b/libfreefare/mifare_classic.c @@ -47,6 +47,11 @@ #if defined(HAVE_ENDIAN_H) # include #endif + +#if defined(HAVE_BYTESWAP_H) +# include +#endif + #include #include #include diff --git a/libfreefare/tlv.c b/libfreefare/tlv.c index 80884a2..305530a 100644 --- a/libfreefare/tlv.c +++ b/libfreefare/tlv.c @@ -36,11 +36,16 @@ # include #endif +#if defined(HAVE_BYTESWAP_H) +# include +#endif + #include #include #include #include +#include "freefare_internal.h" #define TLV_TERMINATOR 0xFE