home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / samba-1.9.18p7.tar.gz / samba-1.9.18p7.tar / samba-1.9.18p7 / source / byteorder.h < prev    next >
C/C++ Source or Header  |  1998-01-26  |  10KB  |  244 lines

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.9.
  4.    SMB Byte handling
  5.    Copyright (C) Andrew Tridgell 1992-1998
  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 2 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
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. /*
  23.    This file implements macros for machine independent short and 
  24.    int manipulation
  25.  
  26. Here is a description of this file that I emailed to the samba list once:
  27.  
  28. > I am confused about the way that byteorder.h works in Samba. I have
  29. > looked at it, and I would have thought that you might make a distinction
  30. > between LE and BE machines, but you only seem to distinguish between 386
  31. > and all other architectures.
  32. > Can you give me a clue?
  33.  
  34. sure.
  35.  
  36. The distinction between 386 and other architectures is only there as
  37. an optimisation. You can take it out completely and it will make no
  38. difference. The routines (macros) in byteorder.h are totally byteorder
  39. independent. The 386 optimsation just takes advantage of the fact that
  40. the x86 processors don't care about alignment, so we don't have to
  41. align ints on int boundaries etc. If there are other processors out
  42. there that aren't alignment sensitive then you could also define
  43. CAREFUL_ALIGNMENT=0 on those processors as well.
  44.  
  45. Ok, now to the macros themselves. I'll take a simple example, say we
  46. want to extract a 2 byte integer from a SMB packet and put it into a
  47. type called uint16 that is in the local machines byte order, and you
  48. want to do it with only the assumption that uint16 is _at_least_ 16
  49. bits long (this last condition is very important for architectures
  50. that don't have any int types that are 2 bytes long)
  51.  
  52. You do this:
  53.  
  54. #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
  55. #define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
  56. #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
  57.  
  58. then to extract a uint16 value at offset 25 in a buffer you do this:
  59.  
  60. char *buffer = foo_bar();
  61. uint16 xx = SVAL(buffer,25);
  62.  
  63. We are using the byteoder independence of the ANSI C bitshifts to do
  64. the work. A good optimising compiler should turn this into efficient
  65. code, especially if it happens to have the right byteorder :-)
  66.  
  67. I know these macros can be made a bit tidier by removing some of the
  68. casts, but you need to look at byteorder.h as a whole to see the
  69. reasoning behind them. byteorder.h defines the following macros:
  70.  
  71. SVAL(buf,pos) - extract a 2 byte SMB value
  72. IVAL(buf,pos) - extract a 4 byte SMB value
  73. SVALS(buf,pos) signed version of SVAL()
  74. IVALS(buf,pos) signed version of IVAL()
  75.  
  76. SSVAL(buf,pos,val) - put a 2 byte SMB value into a buffer
  77. SIVAL(buf,pos,val) - put a 4 byte SMB value into a buffer
  78. SSVALS(buf,pos,val) - signed version of SSVAL()
  79. SIVALS(buf,pos,val) - signed version of SIVAL()
  80.  
  81. RSVAL(buf,pos) - like SVAL() but for NMB byte ordering
  82. RIVAL(buf,pos) - like IVAL() but for NMB byte ordering
  83. RSSVAL(buf,pos,val) - like SSVAL() but for NMB ordering
  84. RSIVAL(buf,pos,val) - like SIVAL() but for NMB ordering
  85.  
  86. it also defines lots of intermediate macros, just ignore those :-)
  87.  
  88. */
  89.  
  90. /* some switch macros that do both store and read to and from SMB buffers */
  91.  
  92. #define RW_PCVAL(read,inbuf,outbuf,len) \
  93.                 if (read) { PCVAL (inbuf,0,outbuf,len) } \
  94.                 else      { PSCVAL(inbuf,0,outbuf,len) }
  95.  
  96. #define RW_PIVAL(read,inbuf,outbuf,len) \
  97.                 if (read) { PIVAL (inbuf,0,outbuf,len) } \
  98.                 else      { PSIVAL(inbuf,0,outbuf,len) }
  99.  
  100. #define RW_PSVAL(read,inbuf,outbuf,len) \
  101.                 if (read) { PSVAL (inbuf,0,outbuf,len) } \
  102.                 else      { PSSVAL(inbuf,0,outbuf,len) }
  103.  
  104. #define RW_CVAL(read, inbuf, outbuf, offset) \
  105.                 if (read) (outbuf) = CVAL (inbuf,offset); \
  106.                 else                 SCVAL(inbuf,offset,outbuf);
  107.  
  108. #define RW_IVAL(read, inbuf, outbuf, offset) \
  109.                 if (read) (outbuf)= IVAL (inbuf,offset); \
  110.                 else                SIVAL(inbuf,offset,outbuf);
  111.  
  112. #define RW_SVAL(read, inbuf, outbuf, offset) \
  113.                 if (read) (outbuf)= SVAL (inbuf,offset); \
  114.                 else                SSVAL(inbuf,offset,outbuf);
  115.  
  116. #undef CAREFUL_ALIGNMENT
  117.  
  118. /* we know that the 386 can handle misalignment and has the "right" 
  119.    byteorder */
  120. #ifdef __i386__
  121. #define CAREFUL_ALIGNMENT 0
  122. #endif
  123.  
  124. #ifndef CAREFUL_ALIGNMENT
  125. #define CAREFUL_ALIGNMENT 1
  126. #endif
  127.  
  128. #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
  129. #define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
  130. #define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val))
  131.  
  132.  
  133. #if CAREFUL_ALIGNMENT
  134.  
  135. #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
  136. #define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
  137. #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
  138. #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
  139. #define SVALS(buf,pos) ((int16)SVAL(buf,pos))
  140. #define IVALS(buf,pos) ((int32)IVAL(buf,pos))
  141. #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16)(val)))
  142. #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val)))
  143. #define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val)))
  144. #define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32)(val)))
  145.  
  146. #else
  147.  
  148. /* this handles things for architectures like the 386 that can handle
  149.    alignment errors */
  150. /*
  151.    WARNING: This section is dependent on the length of int16 and int32
  152.    being correct 
  153. */
  154.  
  155. /* get single value from an SMB buffer */
  156. #define SVAL(buf,pos) (*(uint16 *)((char *)(buf) + (pos)))
  157. #define IVAL(buf,pos) (*(uint32 *)((char *)(buf) + (pos)))
  158. #define SVALS(buf,pos) (*(int16 *)((char *)(buf) + (pos)))
  159. #define IVALS(buf,pos) (*(int32 *)((char *)(buf) + (pos)))
  160.  
  161. /* store single value in an SMB buffer */
  162. #define SSVAL(buf,pos,val) SVAL(buf,pos)=((uint16)(val))
  163. #define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32)(val))
  164. #define SSVALS(buf,pos,val) SVALS(buf,pos)=((int16)(val))
  165. #define SIVALS(buf,pos,val) IVALS(buf,pos)=((int32)(val))
  166.  
  167. #endif
  168.  
  169.  
  170. /* macros for reading / writing arrays */
  171.  
  172. #define SMBMACRO(macro,buf,pos,val,len,size) \
  173. { int l; for (l = 0; l < (len); l++) (val)[l] = macro((buf), (pos) + (size)*l); }
  174.  
  175. #define SSMBMACRO(macro,buf,pos,val,len,size) \
  176. { int l; for (l = 0; l < (len); l++) macro((buf), (pos) + (size)*l, (val)[l]); }
  177.  
  178. /* reads multiple data from an SMB buffer */
  179. #define PCVAL(buf,pos,val,len) SMBMACRO(CVAL,buf,pos,val,len,1)
  180. #define PSVAL(buf,pos,val,len) SMBMACRO(SVAL,buf,pos,val,len,2)
  181. #define PIVAL(buf,pos,val,len) SMBMACRO(IVAL,buf,pos,val,len,4)
  182. #define PCVALS(buf,pos,val,len) SMBMACRO(CVALS,buf,pos,val,len,1)
  183. #define PSVALS(buf,pos,val,len) SMBMACRO(SVALS,buf,pos,val,len,2)
  184. #define PIVALS(buf,pos,val,len) SMBMACRO(IVALS,buf,pos,val,len,4)
  185.  
  186. /* stores multiple data in an SMB buffer */
  187. #define PSCVAL(buf,pos,val,len) SSMBMACRO(SCVAL,buf,pos,val,len,1)
  188. #define PSSVAL(buf,pos,val,len) SSMBMACRO(SSVAL,buf,pos,val,len,2)
  189. #define PSIVAL(buf,pos,val,len) SSMBMACRO(SIVAL,buf,pos,val,len,4)
  190. #define PSCVALS(buf,pos,val,len) SSMBMACRO(SCVALS,buf,pos,val,len,1)
  191. #define PSSVALS(buf,pos,val,len) SSMBMACRO(SSVALS,buf,pos,val,len,2)
  192. #define PSIVALS(buf,pos,val,len) SSMBMACRO(SIVALS,buf,pos,val,len,4)
  193.  
  194.  
  195. /* now the reverse routines - these are used in nmb packets (mostly) */
  196. #define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
  197. #define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
  198.  
  199. #define RSVAL(buf,pos) SREV(SVAL(buf,pos))
  200. #define RIVAL(buf,pos) IREV(IVAL(buf,pos))
  201. #define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
  202. #define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
  203.  
  204. #define DBG_RW_PCVAL(charmode,string,depth,base,read,inbuf,outbuf,len) \
  205.     RW_PCVAL(read,inbuf,outbuf,len) \
  206.     DEBUG(5,("%s%04x %s: ", \
  207.              tab_depth(depth), PTR_DIFF(inbuf,base),string)); \
  208.     if (charmode) print_asc(5, (unsigned char*)(outbuf), (len)); else \
  209.     { int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%02x ", (outbuf)[idx])); } } \
  210.     DEBUG(5,("\n"));
  211.  
  212. #define DBG_RW_PSVAL(charmode,string,depth,base,read,inbuf,outbuf,len) \
  213.     RW_PSVAL(read,inbuf,outbuf,len) \
  214.     DEBUG(5,("%s%04x %s: ", \
  215.              tab_depth(depth), PTR_DIFF(inbuf,base),string)); \
  216.     if (charmode) print_asc(5, (unsigned char*)(outbuf), 2*(len)); else \
  217.     { int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%04x ", (outbuf)[idx])); } } \
  218.     DEBUG(5,("\n"));
  219.  
  220. #define DBG_RW_PIVAL(charmode,string,depth,base,read,inbuf,outbuf,len) \
  221.     RW_PIVAL(read,inbuf,outbuf,len) \
  222.     DEBUG(5,("%s%04x %s: ", \
  223.              tab_depth(depth), PTR_DIFF(inbuf,base),string)); \
  224.     if (charmode) print_asc(5, (unsigned char*)(outbuf), 4*(len)); else \
  225.     { int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%08x ", (outbuf)[idx])); } } \
  226.     DEBUG(5,("\n"));
  227.  
  228. #define DBG_RW_CVAL(string,depth,base,read,inbuf,outbuf) \
  229.     RW_CVAL(read,inbuf,outbuf,0) \
  230.     DEBUG(5,("%s%04x %s: %02x\n", \
  231.              tab_depth(depth), PTR_DIFF(inbuf,base), string, outbuf));
  232.  
  233. #define DBG_RW_SVAL(string,depth,base,read,inbuf,outbuf) \
  234.     RW_SVAL(read,inbuf,outbuf,0) \
  235.     DEBUG(5,("%s%04x %s: %04x\n", \
  236.              tab_depth(depth), PTR_DIFF(inbuf,base), string, outbuf));
  237.  
  238. #define DBG_RW_IVAL(string,depth,base,read,inbuf,outbuf) \
  239.     RW_IVAL(read,inbuf,outbuf,0) \
  240.     DEBUG(5,("%s%04x %s: %08x\n", \
  241.              tab_depth(depth), PTR_DIFF(inbuf,base), string, outbuf));
  242.  
  243.