home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Networking / SambaManager / byteorder.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-10  |  9.6 KB  |  247 lines

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