byteorder.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. /* We know that the x86 can handle misalignment and has the same
  22. * byte order (LSB-first) as the 32-bit numbers we transmit. */
  23. #ifdef __i386__
  24. #define CAREFUL_ALIGNMENT 0
  25. #endif
  26. #ifndef CAREFUL_ALIGNMENT
  27. #define CAREFUL_ALIGNMENT 1
  28. #endif
  29. #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
  30. #define UVAL(buf,pos) ((uint32)CVAL(buf,pos))
  31. #define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val))
  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. #else
  39. /* this handles things for architectures like the 386 that can handle
  40. alignment errors */
  41. /*
  42. WARNING: This section is dependent on the length of int32
  43. being correct. set CAREFUL_ALIGNMENT if it is not.
  44. */
  45. #define IVAL(buf,pos) (*(uint32 *)((char *)(buf) + (pos)))
  46. #define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32)(val))
  47. #endif