byteorder.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. Unix SMB/Netbios implementation.
  3. Version 1.9.
  4. SMB Byte handling
  5. Copyright (C) Andrew Tridgell 1992-1998
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #ifndef _BYTEORDER_H
  19. #define _BYTEORDER_H
  20. /*
  21. This file implements macros for machine independent short and
  22. int manipulation
  23. Here is a description of this file that I emailed to the samba list once:
  24. > I am confused about the way that byteorder.h works in Samba. I have
  25. > looked at it, and I would have thought that you might make a distinction
  26. > between LE and BE machines, but you only seem to distinguish between 386
  27. > and all other architectures.
  28. >
  29. > Can you give me a clue?
  30. sure.
  31. The distinction between 386 and other architectures is only there as
  32. an optimisation. You can take it out completely and it will make no
  33. difference. The routines (macros) in byteorder.h are totally byteorder
  34. independent. The 386 optimsation just takes advantage of the fact that
  35. the x86 processors don't care about alignment, so we don't have to
  36. align ints on int boundaries etc. If there are other processors out
  37. there that aren't alignment sensitive then you could also define
  38. CAREFUL_ALIGNMENT=0 on those processors as well.
  39. Ok, now to the macros themselves. I'll take a simple example, say we
  40. want to extract a 2 byte integer from a SMB packet and put it into a
  41. type called uint16 that is in the local machines byte order, and you
  42. want to do it with only the assumption that uint16 is _at_least_ 16
  43. bits long (this last condition is very important for architectures
  44. that don't have any int types that are 2 bytes long)
  45. You do this:
  46. #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
  47. #define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
  48. #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
  49. then to extract a uint16 value at offset 25 in a buffer you do this:
  50. char *buffer = foo_bar();
  51. uint16 xx = SVAL(buffer,25);
  52. We are using the byteoder independence of the ANSI C bitshifts to do
  53. the work. A good optimising compiler should turn this into efficient
  54. code, especially if it happens to have the right byteorder :-)
  55. I know these macros can be made a bit tidier by removing some of the
  56. casts, but you need to look at byteorder.h as a whole to see the
  57. reasoning behind them. byteorder.h defines the following macros:
  58. SVAL(buf,pos) - extract a 2 byte SMB value
  59. IVAL(buf,pos) - extract a 4 byte SMB value
  60. SVALS(buf,pos) signed version of SVAL()
  61. IVALS(buf,pos) signed version of IVAL()
  62. SSVAL(buf,pos,val) - put a 2 byte SMB value into a buffer
  63. SIVAL(buf,pos,val) - put a 4 byte SMB value into a buffer
  64. SSVALS(buf,pos,val) - signed version of SSVAL()
  65. SIVALS(buf,pos,val) - signed version of SIVAL()
  66. RSVAL(buf,pos) - like SVAL() but for NMB byte ordering
  67. RSVALS(buf,pos) - like SVALS() but for NMB byte ordering
  68. RIVAL(buf,pos) - like IVAL() but for NMB byte ordering
  69. RIVALS(buf,pos) - like IVALS() but for NMB byte ordering
  70. RSSVAL(buf,pos,val) - like SSVAL() but for NMB ordering
  71. RSIVAL(buf,pos,val) - like SIVAL() but for NMB ordering
  72. RSIVALS(buf,pos,val) - like SIVALS() but for NMB ordering
  73. it also defines lots of intermediate macros, just ignore those :-)
  74. */
  75. /* some switch macros that do both store and read to and from SMB buffers */
  76. #define RW_PCVAL(read,inbuf,outbuf,len) \
  77. { if (read) { PCVAL (inbuf,0,outbuf,len); } \
  78. else { PSCVAL(inbuf,0,outbuf,len); } }
  79. #define RW_PIVAL(read,big_endian,inbuf,outbuf,len) \
  80. { if (read) { if (big_endian) { RPIVAL(inbuf,0,outbuf,len); } else { PIVAL(inbuf,0,outbuf,len); } } \
  81. else { if (big_endian) { RPSIVAL(inbuf,0,outbuf,len); } else { PSIVAL(inbuf,0,outbuf,len); } } }
  82. #define RW_PSVAL(read,big_endian,inbuf,outbuf,len) \
  83. { if (read) { if (big_endian) { RPSVAL(inbuf,0,outbuf,len); } else { PSVAL(inbuf,0,outbuf,len); } } \
  84. else { if (big_endian) { RPSSVAL(inbuf,0,outbuf,len); } else { PSSVAL(inbuf,0,outbuf,len); } } }
  85. #define RW_CVAL(read, inbuf, outbuf, offset) \
  86. { if (read) { (outbuf) = CVAL (inbuf,offset); } \
  87. else { SCVAL(inbuf,offset,outbuf); } }
  88. #define RW_IVAL(read, big_endian, inbuf, outbuf, offset) \
  89. { if (read) { (outbuf) = ((big_endian) ? RIVAL(inbuf,offset) : IVAL (inbuf,offset)); } \
  90. else { if (big_endian) { RSIVAL(inbuf,offset,outbuf); } else { SIVAL(inbuf,offset,outbuf); } } }
  91. #define RW_SVAL(read, big_endian, inbuf, outbuf, offset) \
  92. { if (read) { (outbuf) = ((big_endian) ? RSVAL(inbuf,offset) : SVAL (inbuf,offset)); } \
  93. else { if (big_endian) { RSSVAL(inbuf,offset,outbuf); } else { SSVAL(inbuf,offset,outbuf); } } }
  94. #undef CAREFUL_ALIGNMENT
  95. /* we know that the 386 can handle misalignment and has the "right"
  96. byteorder */
  97. #ifdef __i386__
  98. #define CAREFUL_ALIGNMENT 0
  99. #endif
  100. #ifndef CAREFUL_ALIGNMENT
  101. #define CAREFUL_ALIGNMENT 1
  102. #endif
  103. #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
  104. #define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
  105. #define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val))
  106. #if CAREFUL_ALIGNMENT
  107. #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
  108. #define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
  109. #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
  110. #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
  111. #define SVALS(buf,pos) ((int16)SVAL(buf,pos))
  112. #define IVALS(buf,pos) ((int32)IVAL(buf,pos))
  113. #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16)(val)))
  114. #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val)))
  115. #define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val)))
  116. #define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32)(val)))
  117. #else /* CAREFUL_ALIGNMENT */
  118. /* this handles things for architectures like the 386 that can handle
  119. alignment errors */
  120. /*
  121. WARNING: This section is dependent on the length of int16 and int32
  122. being correct
  123. */
  124. /* get single value from an SMB buffer */
  125. #define SVAL(buf,pos) (*(uint16 *)((char *)(buf) + (pos)))
  126. #define IVAL(buf,pos) (*(uint32 *)((char *)(buf) + (pos)))
  127. #define SVALS(buf,pos) (*(int16 *)((char *)(buf) + (pos)))
  128. #define IVALS(buf,pos) (*(int32 *)((char *)(buf) + (pos)))
  129. /* store single value in an SMB buffer */
  130. #define SSVAL(buf,pos,val) SVAL(buf,pos)=((uint16)(val))
  131. #define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32)(val))
  132. #define SSVALS(buf,pos,val) SVALS(buf,pos)=((int16)(val))
  133. #define SIVALS(buf,pos,val) IVALS(buf,pos)=((int32)(val))
  134. #endif /* CAREFUL_ALIGNMENT */
  135. /* macros for reading / writing arrays */
  136. #define SMBMACRO(macro,buf,pos,val,len,size) \
  137. { int l; for (l = 0; l < (len); l++) (val)[l] = macro((buf), (pos) + (size)*l); }
  138. #define SSMBMACRO(macro,buf,pos,val,len,size) \
  139. { int l; for (l = 0; l < (len); l++) macro((buf), (pos) + (size)*l, (val)[l]); }
  140. /* reads multiple data from an SMB buffer */
  141. #define PCVAL(buf,pos,val,len) SMBMACRO(CVAL,buf,pos,val,len,1)
  142. #define PSVAL(buf,pos,val,len) SMBMACRO(SVAL,buf,pos,val,len,2)
  143. #define PIVAL(buf,pos,val,len) SMBMACRO(IVAL,buf,pos,val,len,4)
  144. #define PCVALS(buf,pos,val,len) SMBMACRO(CVALS,buf,pos,val,len,1)
  145. #define PSVALS(buf,pos,val,len) SMBMACRO(SVALS,buf,pos,val,len,2)
  146. #define PIVALS(buf,pos,val,len) SMBMACRO(IVALS,buf,pos,val,len,4)
  147. /* stores multiple data in an SMB buffer */
  148. #define PSCVAL(buf,pos,val,len) SSMBMACRO(SCVAL,buf,pos,val,len,1)
  149. #define PSSVAL(buf,pos,val,len) SSMBMACRO(SSVAL,buf,pos,val,len,2)
  150. #define PSIVAL(buf,pos,val,len) SSMBMACRO(SIVAL,buf,pos,val,len,4)
  151. #define PSCVALS(buf,pos,val,len) SSMBMACRO(SCVALS,buf,pos,val,len,1)
  152. #define PSSVALS(buf,pos,val,len) SSMBMACRO(SSVALS,buf,pos,val,len,2)
  153. #define PSIVALS(buf,pos,val,len) SSMBMACRO(SIVALS,buf,pos,val,len,4)
  154. /* now the reverse routines - these are used in nmb packets (mostly) */
  155. #define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
  156. #define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
  157. #define RSVAL(buf,pos) SREV(SVAL(buf,pos))
  158. #define RSVALS(buf,pos) SREV(SVALS(buf,pos))
  159. #define RIVAL(buf,pos) IREV(IVAL(buf,pos))
  160. #define RIVALS(buf,pos) IREV(IVALS(buf,pos))
  161. #define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
  162. #define RSSVALS(buf,pos,val) SSVALS(buf,pos,SREV(val))
  163. #define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
  164. #define RSIVALS(buf,pos,val) SIVALS(buf,pos,IREV(val))
  165. /* reads multiple data from an SMB buffer (big-endian) */
  166. #define RPSVAL(buf,pos,val,len) SMBMACRO(RSVAL,buf,pos,val,len,2)
  167. #define RPIVAL(buf,pos,val,len) SMBMACRO(RIVAL,buf,pos,val,len,4)
  168. #define RPSVALS(buf,pos,val,len) SMBMACRO(RSVALS,buf,pos,val,len,2)
  169. #define RPIVALS(buf,pos,val,len) SMBMACRO(RIVALS,buf,pos,val,len,4)
  170. /* stores multiple data in an SMB buffer (big-endian) */
  171. #define RPSSVAL(buf,pos,val,len) SSMBMACRO(RSSVAL,buf,pos,val,len,2)
  172. #define RPSIVAL(buf,pos,val,len) SSMBMACRO(RSIVAL,buf,pos,val,len,4)
  173. #define RPSSVALS(buf,pos,val,len) SSMBMACRO(RSSVALS,buf,pos,val,len,2)
  174. #define RPSIVALS(buf,pos,val,len) SSMBMACRO(RSIVALS,buf,pos,val,len,4)
  175. #endif /* _BYTEORDER_H */