Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * This file is part of LibParserUtils.
  3.  * Licensed under the MIT License,
  4.  *                http://www.opensource.org/licenses/mit-license.php
  5.  * Copyright 2009 John-Mark Bell <jmb@netsurf-browser.org>
  6.  */
  7.  
  8. #ifndef parserutils_endian_h_
  9. #define parserutils_endian_h_
  10.  
  11. static inline bool endian_host_is_le(void)
  12. {
  13.         static uint32_t magic = 0x10000002;
  14.  
  15.         return (((uint8_t *) &magic)[0] == 0x02);
  16. }
  17.  
  18. static inline uint32_t endian_swap(uint32_t val)
  19. {
  20.         return ((val & 0xff000000) >> 24) | ((val & 0x00ff0000) >> 8) |
  21.                 ((val & 0x0000ff00) << 8) | ((val & 0x000000ff) << 24);
  22. }
  23.  
  24. static inline uint32_t endian_host_to_big(uint32_t host)
  25. {
  26.         if (endian_host_is_le())
  27.                 return endian_swap(host);
  28.  
  29.         return host;
  30. }
  31.  
  32. static inline uint32_t endian_big_to_host(uint32_t big)
  33. {
  34.         if (endian_host_is_le())
  35.                 return endian_swap(big);
  36.  
  37.         return big;
  38. }
  39.  
  40. #endif
  41.