Vadim Bendebury | 5679752 | 2015-05-20 10:32:25 -0700 | [diff] [blame] | 1 | // This file was extracted from the TCG Published |
| 2 | // Trusted Platform Module Library |
| 3 | // Part 4: Supporting Routines |
| 4 | // Family "2.0" |
| 5 | // Level 00 Revision 01.16 |
| 6 | // October 30, 2014 |
| 7 | |
| 8 | #ifndef _SWAP_H |
| 9 | #define _SWAP_H |
| 10 | #include "Implementation.h" |
| 11 | #if NO_AUTO_ALIGN == YES || LITTLE_ENDIAN_TPM == YES |
| 12 | // |
| 13 | // The aggregation macros for machines that do not allow unaligned access or for little-endian machines. |
| 14 | // Aggregate bytes into an UINT |
| 15 | // |
| 16 | #define BYTE_ARRAY_TO_UINT8(b) (UINT8)((b)[0]) |
| 17 | #define BYTE_ARRAY_TO_UINT16(b) (UINT16)( ((b)[0] << 8) \ |
| 18 | + (b)[1]) |
| 19 | #define BYTE_ARRAY_TO_UINT32(b) (UINT32)( ((b)[0] << 24) \ |
| 20 | + ((b)[1] << 16) \ |
| 21 | + ((b)[2] << 8 ) \ |
| 22 | + (b)[3]) |
| 23 | #define BYTE_ARRAY_TO_UINT64(b) (UINT64)( ((UINT64)(b)[0] << 56) \ |
| 24 | + ((UINT64)(b)[1] << 48) \ |
| 25 | + ((UINT64)(b)[2] << 40) \ |
| 26 | + ((UINT64)(b)[3] << 32) \ |
| 27 | + ((UINT64)(b)[4] << 24) \ |
| 28 | + ((UINT64)(b)[5] << 16) \ |
| 29 | + ((UINT64)(b)[6] << 8) \ |
| 30 | + (UINT64)(b)[7]) |
| 31 | // |
| 32 | // Disaggregate a UINT into a byte array |
| 33 | // |
| 34 | #define UINT8_TO_BYTE_ARRAY(i, b) ((b)[0] = (BYTE)(i), i) |
| 35 | #define UINT16_TO_BYTE_ARRAY(i, b) ((b)[0] = (BYTE)((i) >> 8), \ |
| 36 | (b)[1] = (BYTE) (i), \ |
| 37 | (i)) |
| 38 | #define UINT32_TO_BYTE_ARRAY(i, b) ((b)[0] = (BYTE)((i) >> 24), \ |
| 39 | (b)[1] = (BYTE)((i) >> 16), \ |
| 40 | (b)[2] = (BYTE)((i) >> 8), \ |
| 41 | (b)[3] = (BYTE) (i), \ |
| 42 | (i)) |
| 43 | #define UINT64_TO_BYTE_ARRAY(i, b) ((b)[0] = (BYTE)((i) >> 56), \ |
| 44 | (b)[1] = (BYTE)((i) >> 48), \ |
| 45 | (b)[2] = (BYTE)((i) >> 40), \ |
| 46 | (b)[3] = (BYTE)((i) >> 32), \ |
| 47 | (b)[4] = (BYTE)((i) >> 24), \ |
| 48 | (b)[5] = (BYTE)((i) >> 16), \ |
| 49 | (b)[6] = (BYTE)((i) >> 8), \ |
| 50 | (b)[7] = (BYTE) (i), \ |
| 51 | (i)) |
| 52 | #else |
| 53 | // |
| 54 | // the big-endian macros for machines that allow unaligned memory access Aggregate a byte array into a |
| 55 | // UINT |
| 56 | // |
| 57 | #define BYTE_ARRAY_TO_UINT8(b) *((UINT8 *)(b)) |
| 58 | #define BYTE_ARRAY_TO_UINT16(b) *((UINT16 *)(b)) |
| 59 | #define BYTE_ARRAY_TO_UINT32(b) *((UINT32 *)(b)) |
| 60 | #define BYTE_ARRAY_TO_UINT64(b) *((UINT64 *)(b)) |
| 61 | // |
| 62 | // Disaggregate a UINT into a byte array |
| 63 | #define UINT8_TO_BYTE_ARRAY(i, b) (*((UINT8 *)(b)) = (i)) |
| 64 | #define UINT16_TO_BYTE_ARRAY(i, b) (*((UINT16 *)(b)) = (i)) |
| 65 | #define UINT32_TO_BYTE_ARRAY(i, b) (*((UINT32 *)(b)) = (i)) |
| 66 | #define UINT64_TO_BYTE_ARRAY(i, b) (*((UINT64 *)(b)) = (i)) |
| 67 | #endif // NO_AUTO_ALIGN == YES |
| 68 | #endif // _SWAP_H |