byteorder.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Simple byteorder handling.
  3. *
  4. * Copyright (C) 1992-1995 Andrew Tridgell
  5. * Copyright (C) 2007-2008 Wayne Davison
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, visit the http://fsf.org website.
  19. */
  20. #undef CAREFUL_ALIGNMENT
  21. #undef AVOID_BYTEORDER_INLINE
  22. /* We know that the x86 can handle misalignment and has the same
  23. * byte order (LSB-first) as the 32-bit numbers we transmit. */
  24. #ifdef __i386__
  25. #define CAREFUL_ALIGNMENT 0
  26. #endif
  27. #ifndef CAREFUL_ALIGNMENT
  28. #define CAREFUL_ALIGNMENT 1
  29. #endif
  30. #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
  31. #define UVAL(buf,pos) ((uint32)CVAL(buf,pos))
  32. #if CAREFUL_ALIGNMENT
  33. #define PVAL(buf,pos) (UVAL(buf,pos)|UVAL(buf,(pos)+1)<<8)
  34. #define IVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+2)<<16)
  35. #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
  36. #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
  37. #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val)))
  38. #define IVALu(buf,pos) IVAL(buf,pos)
  39. #define SIVALu(buf,pos,val) SIVAL(buf,pos,val)
  40. #else /* !CAREFUL_ALIGNMENT */
  41. /* This handles things for architectures like the 386 that can handle alignment errors.
  42. * WARNING: This section is dependent on the length of an int32 (and thus a uint32)
  43. * being correct (4 bytes)! Set CAREFUL_ALIGNMENT if it is not. */
  44. # ifdef AVOID_BYTEORDER_INLINE
  45. #define IVAL(buf,pos) (*(uint32 *)((char *)(buf) + (pos)))
  46. #define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32)(val))
  47. #define IVALu(buf,pos) IVAL(buf,pos)
  48. #define SIVALu(buf,pos,val) SIVAL(buf,pos,val)
  49. # else /* !AVOID_BYTEORDER_INLINE */
  50. static inline uint32
  51. IVALu(const uchar *buf, int pos)
  52. {
  53. union {
  54. const uchar *b;
  55. const uint32 *num;
  56. } u;
  57. u.b = buf + pos;
  58. return *u.num;
  59. }
  60. static inline void
  61. SIVALu(uchar *buf, int pos, uint32 val)
  62. {
  63. union {
  64. uchar *b;
  65. uint32 *num;
  66. } u;
  67. u.b = buf + pos;
  68. *u.num = val;
  69. }
  70. static inline uint32
  71. IVAL(const char *buf, int pos)
  72. {
  73. return IVALu((uchar*)buf, pos);
  74. }
  75. static inline void
  76. SIVAL(char *buf, int pos, uint32 val)
  77. {
  78. SIVALu((uchar*)buf, pos, val);
  79. }
  80. # endif /* !AVOID_BYTEORDER_INLINE */
  81. #endif /* !CAREFUL_ALIGNMENT */