endian.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*############################################################################
  2. # Copyright 2017 Intel Corporation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################*/
  16. /// Convert values between host and big-/little-endian byte order.
  17. /*! \file */
  18. #ifndef EPID_MEMBER_TINY_STDLIB_ENDIAN_H_
  19. #define EPID_MEMBER_TINY_STDLIB_ENDIAN_H_
  20. #include <stdint.h>
  21. /// Transform big endian uint32_t to host uint32_t
  22. #define be32toh(big_endian_32bits) \
  23. ((uint32_t)(((((unsigned char*)&(big_endian_32bits))[0]) << 24) + \
  24. ((((unsigned char*)&(big_endian_32bits))[1]) << 16) + \
  25. ((((unsigned char*)&(big_endian_32bits))[2]) << 8) + \
  26. (((unsigned char*)&(big_endian_32bits))[3])))
  27. /// Transform host uint32_t to big endian uint32_t
  28. #define htobe32(host_32bits) \
  29. (uint32_t)(((((uint32_t)(host_32bits)) & 0xFF) << 24) | \
  30. ((((uint32_t)(host_32bits)) & 0xFF00) << 8) | \
  31. ((((uint32_t)(host_32bits)) & 0xFF0000) >> 8) | \
  32. ((((uint32_t)(host_32bits)) & 0xFF000000) >> 24))
  33. #endif // EPID_MEMBER_TINY_STDLIB_ENDIAN_H_