Home | History | Annotate | Download | only in tpm2
      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);}
     35 #define UINT16_TO_BYTE_ARRAY(i, b)          {(b)[0]   = (BYTE)((i) >>   8); \
     36                                              (b)[1]   = (BYTE) (i);}
     37 #define UINT32_TO_BYTE_ARRAY(i, b)          {(b)[0]   = (BYTE)((i) >> 24);  \
     38                                              (b)[1]   = (BYTE)((i) >> 16);  \
     39                                              (b)[2]   = (BYTE)((i) >> 8);   \
     40                                              (b)[3]   = (BYTE) (i);}
     41 #define UINT64_TO_BYTE_ARRAY(i, b)          {(b)[0]   = (BYTE)((i) >>  56); \
     42                                              (b)[1]   = (BYTE)((i) >>  48); \
     43                                              (b)[2]   = (BYTE)((i) >>  40); \
     44                                              (b)[3]   = (BYTE)((i) >>  32); \
     45                                              (b)[4]   = (BYTE)((i) >>  24); \
     46                                              (b)[5]   = (BYTE)((i) >>  16); \
     47                                              (b)[6]   = (BYTE)((i) >>   8); \
     48                                              (b)[7]   = (BYTE) (i);}
     49 #else
     50 //
     51 //     the big-endian macros for machines that allow unaligned memory access Aggregate a byte array into a
     52 //     UINT
     53 //
     54 #define   BYTE_ARRAY_TO_UINT8(b)            *((UINT8      *)(b))
     55 #define   BYTE_ARRAY_TO_UINT16(b)           *((UINT16     *)(b))
     56 #define   BYTE_ARRAY_TO_UINT32(b)           *((UINT32     *)(b))
     57 #define   BYTE_ARRAY_TO_UINT64(b)           *((UINT64     *)(b))
     58 //
     59 //     Disaggregate a UINT into a byte array
     60 #define   UINT8_TO_BYTE_ARRAY(i, b)      (*((UINT8    *)(b))   =   (i))
     61 #define   UINT16_TO_BYTE_ARRAY(i, b)     (*((UINT16   *)(b))   =   (i))
     62 #define   UINT32_TO_BYTE_ARRAY(i, b)     (*((UINT32   *)(b))   =   (i))
     63 #define   UINT64_TO_BYTE_ARRAY(i, b)     (*((UINT64   *)(b))   =   (i))
     64 #endif    // NO_AUTO_ALIGN == YES
     65 #endif    // _SWAP_H
     66