From 28b6ab7196f7b8f2fead57f7bd864d9d05214653 Mon Sep 17 00:00:00 2001 From: Romain Tartiere Date: Thu, 29 Jul 2010 07:58:25 +0000 Subject: [PATCH] Move buffer macros to freefare_internal.h. While here, tweak include. --- libfreefare/freefare_internal.h | 73 ++++++++++++++++++++++++++++++++ libfreefare/mifare_desfire.c | 75 +-------------------------------- 2 files changed, 74 insertions(+), 74 deletions(-) diff --git a/libfreefare/freefare_internal.h b/libfreefare/freefare_internal.h index e3ee48c..0b66fcf 100644 --- a/libfreefare/freefare_internal.h +++ b/libfreefare/freefare_internal.h @@ -206,4 +206,77 @@ struct mifare_ultralight_tag { #define DEBUG_XFER(data, nbytes, hint) do {} while (0) #endif + +/* + * Buffer management macros. + * + * The following macros ease setting-up and using buffers: + * BUFFER_INIT (data, 5); // data -> [ xx, xx, xx, xx, xx ] + * BUFFER_SIZE (data); // size -> 0 + * BUFFER_APPEND (data, 0x12); // data -> [ 12, xx, xx, xx, xx ] + * BUFFER_SIZE (data); // size -> 1 + * uint16_t x = 0x3456; // We suppose we are little endian + * BUFFER_APPEND_BYTES (data, x, 2); + * // data -> [ 12, 56, 34, xx, xx ] + * BUFFER_SIZE (data); // size -> 3 + * BUFFER_APPEND_LE (data, x, 2, sizeof (x)); + * // data -> [ 12, 56, 34, 34, 56 ] + * BUFFER_SIZE (data); // size -> 5 + */ + +/* + * Initialise a buffer named buffer_name of size bytes. + */ +#define BUFFER_INIT(buffer_name, size) \ + uint8_t buffer_name[size]; \ + size_t __##buffer_name##_n = 0 + +#define BUFFER_SIZE(buffer_name) (__##buffer_name##_n) + +#define BUFFER_CLEAR(buffer_name) (__##buffer_name##_n = 0) +/* + * Append one byte of data to the buffer buffer_name. + */ +#define BUFFER_APPEND(buffer_name, data) \ + do { \ + buffer_name[__##buffer_name##_n++] = data; \ + } while (0) + +/* + * Append size bytes of data to the buffer buffer_name. + */ +#define BUFFER_APPEND_BYTES(buffer_name, data, size) \ + do { \ + size_t __n = 0; \ + while (__n < size) { \ + buffer_name[__##buffer_name##_n++] = ((uint8_t *)data)[__n++]; \ + } \ + } while (0) + +/* + * Append data_size bytes of data at the end of the buffer. Since data is + * copied as a little endian value, the storage size of the value has to be + * passed as the field_size parameter. + * + * Example: to copy 24 bits of data from a 32 bits value: + * BUFFER_APPEND_LE (buffer, data, 3, 4); + */ + +#if _BYTE_ORDER != _LITTLE_ENDIAN +#define BUFFER_APPEND_LE(buffer, data, data_size, field_size) \ + do { \ + size_t __data_size = data_size; \ + size_t __field_size = field_size; \ + while (__field_size--, __data_size--) { \ + buffer[__##buffer##_n++] = ((uint8_t *)&data)[__field_size]; \ + } \ + } while (0) +#else +#define BUFFER_APPEND_LE(buffer, data, data_size, field_size) \ + do { \ + memcpy (buffer + __##buffer##_n, &data, data_size); \ + __##buffer##_n += data_size; \ + } while (0) +#endif + #endif /* !__FREEFARE_INTERNAL_H__ */ diff --git a/libfreefare/mifare_desfire.c b/libfreefare/mifare_desfire.c index de038a9..0c47f3c 100644 --- a/libfreefare/mifare_desfire.c +++ b/libfreefare/mifare_desfire.c @@ -45,7 +45,7 @@ #include #ifdef WITH_DEBUG -# include "libutil.h" +# include #endif #include @@ -97,79 +97,6 @@ static ssize_t read_data (MifareTag tag, uint8_t command, uint8_t file_no, off_ return errno = EINVAL, -1; \ } while (0); - -/* - * Buffer management macros. - * - * The following macros ease setting-up and using buffers: - * BUFFER_INIT (data, 5); // data -> [ xx, xx, xx, xx, xx ] - * BUFFER_SIZE (data); // size -> 0 - * BUFFER_APPEND (data, 0x12); // data -> [ 12, xx, xx, xx, xx ] - * BUFFER_SIZE (data); // size -> 1 - * uint16_t x = 0x3456; // We suppose we are little endian - * BUFFER_APPEND_BYTES (data, x, 2); - * // data -> [ 12, 56, 34, xx, xx ] - * BUFFER_SIZE (data); // size -> 3 - * BUFFER_APPEND_LE (data, x, 2, sizeof (x)); - * // data -> [ 12, 56, 34, 34, 56 ] - * BUFFER_SIZE (data); // size -> 5 - */ - -/* - * Initialise a buffer named buffer_name of size bytes. - */ -#define BUFFER_INIT(buffer_name, size) \ - uint8_t buffer_name[size]; \ - size_t __##buffer_name##_n = 0 - -#define BUFFER_SIZE(buffer_name) (__##buffer_name##_n) - -#define BUFFER_CLEAR(buffer_name) (__##buffer_name##_n = 0) -/* - * Append one byte of data to the buffer buffer_name. - */ -#define BUFFER_APPEND(buffer_name, data) \ - do { \ - buffer_name[__##buffer_name##_n++] = data; \ - } while (0) - -/* - * Append size bytes of data to the buffer buffer_name. - */ -#define BUFFER_APPEND_BYTES(buffer_name, data, size) \ - do { \ - size_t __n = 0; \ - while (__n < size) { \ - buffer_name[__##buffer_name##_n++] = ((uint8_t *)data)[__n++]; \ - } \ - } while (0) - -/* - * Append data_size bytes of data at the end of the buffer. Since data is - * copied as a little endian value, the storage size of the value has to be - * passed as the field_size parameter. - * - * Example: to copy 24 bits of data from a 32 bits value: - * BUFFER_APPEND_LE (buffer, data, 3, 4); - */ - -#if _BYTE_ORDER != _LITTLE_ENDIAN -#define BUFFER_APPEND_LE(buffer, data, data_size, field_size) \ - do { \ - size_t __data_size = data_size; \ - size_t __field_size = field_size; \ - while (__field_size--, __data_size--) { \ - buffer[__##buffer##_n++] = ((uint8_t *)&data)[__field_size]; \ - } \ - } while (0) -#else -#define BUFFER_APPEND_LE(buffer, data, data_size, field_size) \ - do { \ - memcpy (buffer + __##buffer##_n, &data, data_size); \ - __##buffer##_n += data_size; \ - } while (0) -#endif - /* * Convenience macros.