r23779: Change from v2 or later to v3 or later.
[samba.git] / source3 / include / byteorder.h
index 113de607fb80998d4f5f573d2c165f97d6c10b32..9b0ef98ce40d480fe14081f551b8ccdaaf6a2a81 100644 (file)
@@ -1,12 +1,11 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 1.9.
+   Unix SMB/CIFS implementation.
    SMB Byte handling
    Copyright (C) Andrew Tridgell 1992-1998
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -19,6 +18,9 @@
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
+#ifndef _BYTEORDER_H
+#define _BYTEORDER_H
+
 /*
    This file implements macros for machine independent short and 
    int manipulation
@@ -80,40 +82,17 @@ SSVALS(buf,pos,val) - signed version of SSVAL()
 SIVALS(buf,pos,val) - signed version of SIVAL()
 
 RSVAL(buf,pos) - like SVAL() but for NMB byte ordering
+RSVALS(buf,pos) - like SVALS() but for NMB byte ordering
 RIVAL(buf,pos) - like IVAL() but for NMB byte ordering
+RIVALS(buf,pos) - like IVALS() but for NMB byte ordering
 RSSVAL(buf,pos,val) - like SSVAL() but for NMB ordering
 RSIVAL(buf,pos,val) - like SIVAL() but for NMB ordering
+RSIVALS(buf,pos,val) - like SIVALS() but for NMB ordering
 
 it also defines lots of intermediate macros, just ignore those :-)
 
 */
 
-/* some switch macros that do both store and read to and from SMB buffers */
-
-#define RW_PCVAL(read,inbuf,outbuf,len) \
-                if (read) { PCVAL (inbuf,0,outbuf,len) } \
-                else      { PSCVAL(inbuf,0,outbuf,len) }
-
-#define RW_PIVAL(read,inbuf,outbuf,len) \
-                if (read) { PIVAL (inbuf,0,outbuf,len) } \
-                else      { PSIVAL(inbuf,0,outbuf,len) }
-
-#define RW_PSVAL(read,inbuf,outbuf,len) \
-                if (read) { PSVAL (inbuf,0,outbuf,len) } \
-                else      { PSSVAL(inbuf,0,outbuf,len) }
-
-#define RW_CVAL(read, inbuf, outbuf, offset) \
-                if (read) (outbuf) = CVAL (inbuf,offset); \
-                else                 SCVAL(inbuf,offset,outbuf);
-
-#define RW_IVAL(read, inbuf, outbuf, offset) \
-                if (read) (outbuf)= IVAL (inbuf,offset); \
-                else                SIVAL(inbuf,offset,outbuf);
-
-#define RW_SVAL(read, inbuf, outbuf, offset) \
-                if (read) (outbuf)= SVAL (inbuf,offset); \
-                else                SSVAL(inbuf,offset,outbuf);
-
 #undef CAREFUL_ALIGNMENT
 
 /* we know that the 386 can handle misalignment and has the "right" 
@@ -126,16 +105,17 @@ it also defines lots of intermediate macros, just ignore those :-)
 #define CAREFUL_ALIGNMENT 1
 #endif
 
-#define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
-#define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
-#define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val))
+#define CVAL(buf,pos) ((unsigned)(((const unsigned char *)(buf))[pos]))
+#define CVAL_NC(buf,pos) (((unsigned char *)(buf))[pos]) /* Non-const version of CVAL */
+#define PVAL(buf,pos) (CVAL(buf,pos))
+#define SCVAL(buf,pos,val) (CVAL_NC(buf,pos) = (val))
 
 
 #if CAREFUL_ALIGNMENT
 
 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
 #define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
-#define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
+#define SSVALX(buf,pos,val) (CVAL_NC(buf,pos)=(unsigned char)((val)&0xFF),CVAL_NC(buf,pos+1)=(unsigned char)((val)>>8))
 #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
 #define SVALS(buf,pos) ((int16)SVAL(buf,pos))
 #define IVALS(buf,pos) ((int32)IVAL(buf,pos))
@@ -144,7 +124,7 @@ it also defines lots of intermediate macros, just ignore those :-)
 #define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val)))
 #define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32)(val)))
 
-#else
+#else /* CAREFUL_ALIGNMENT */
 
 /* this handles things for architectures like the 386 that can handle
    alignment errors */
@@ -155,94 +135,37 @@ it also defines lots of intermediate macros, just ignore those :-)
 
 /* get single value from an SMB buffer */
 #define SVAL(buf,pos) (*(const uint16 *)((const char *)(buf) + (pos)))
+#define SVAL_NC(buf,pos) (*(uint16 *)((char *)(buf) + (pos))) /* Non const version of above. */
 #define IVAL(buf,pos) (*(const uint32 *)((const char *)(buf) + (pos)))
+#define IVAL_NC(buf,pos) (*(uint32 *)((char *)(buf) + (pos))) /* Non const version of above. */
 #define SVALS(buf,pos) (*(const int16 *)((const char *)(buf) + (pos)))
+#define SVALS_NC(buf,pos) (*(int16 *)((char *)(buf) + (pos))) /* Non const version of above. */
 #define IVALS(buf,pos) (*(const int32 *)((const char *)(buf) + (pos)))
-
-#define SVALMOD(buf,pos) (*(uint16 *)((char *)(buf) + (pos)))
-#define IVALMOD(buf,pos) (*(uint32 *)((char *)(buf) + (pos)))
-#define SVALMODS(buf,pos) (*(int16 *)((char *)(buf) + (pos)))
-#define IVALMODS(buf,pos) (*(int32 *)((char *)(buf) + (pos)))
+#define IVALS_NC(buf,pos) (*(int32 *)((char *)(buf) + (pos))) /* Non const version of above. */
 
 /* store single value in an SMB buffer */
-#define SSVAL(buf,pos,val) SVALMOD(buf,pos)=((uint16)(val))
-#define SIVAL(buf,pos,val) IVALMOD(buf,pos)=((uint32)(val))
-#define SSVALS(buf,pos,val) SVALMODS(buf,pos)=((int16)(val))
-#define SIVALS(buf,pos,val) IVALMODS(buf,pos)=((int32)(val))
-
-#endif
-
-
-/* macros for reading / writing arrays */
-
-#define SMBMACRO(macro,buf,pos,val,len,size) \
-{ int l; for (l = 0; l < (len); l++) (val)[l] = macro((buf), (pos) + (size)*l); }
-
-#define SSMBMACRO(macro,buf,pos,val,len,size) \
-{ int l; for (l = 0; l < (len); l++) macro((buf), (pos) + (size)*l, (val)[l]); }
-
-/* reads multiple data from an SMB buffer */
-#define PCVAL(buf,pos,val,len) SMBMACRO(CVAL,buf,pos,val,len,1)
-#define PSVAL(buf,pos,val,len) SMBMACRO(SVAL,buf,pos,val,len,2)
-#define PIVAL(buf,pos,val,len) SMBMACRO(IVAL,buf,pos,val,len,4)
-#define PCVALS(buf,pos,val,len) SMBMACRO(CVALS,buf,pos,val,len,1)
-#define PSVALS(buf,pos,val,len) SMBMACRO(SVALS,buf,pos,val,len,2)
-#define PIVALS(buf,pos,val,len) SMBMACRO(IVALS,buf,pos,val,len,4)
-
-/* stores multiple data in an SMB buffer */
-#define PSCVAL(buf,pos,val,len) SSMBMACRO(SCVAL,buf,pos,val,len,1)
-#define PSSVAL(buf,pos,val,len) SSMBMACRO(SSVAL,buf,pos,val,len,2)
-#define PSIVAL(buf,pos,val,len) SSMBMACRO(SIVAL,buf,pos,val,len,4)
-#define PSCVALS(buf,pos,val,len) SSMBMACRO(SCVALS,buf,pos,val,len,1)
-#define PSSVALS(buf,pos,val,len) SSMBMACRO(SSVALS,buf,pos,val,len,2)
-#define PSIVALS(buf,pos,val,len) SSMBMACRO(SIVALS,buf,pos,val,len,4)
+#define SSVAL(buf,pos,val) SVAL_NC(buf,pos)=((uint16)(val))
+#define SIVAL(buf,pos,val) IVAL_NC(buf,pos)=((uint32)(val))
+#define SSVALS(buf,pos,val) SVALS_NC(buf,pos)=((int16)(val))
+#define SIVALS(buf,pos,val) IVALS_NC(buf,pos)=((int32)(val))
 
+#endif /* CAREFUL_ALIGNMENT */
 
 /* now the reverse routines - these are used in nmb packets (mostly) */
 #define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
 #define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
 
 #define RSVAL(buf,pos) SREV(SVAL(buf,pos))
+#define RSVALS(buf,pos) SREV(SVALS(buf,pos))
 #define RIVAL(buf,pos) IREV(IVAL(buf,pos))
+#define RIVALS(buf,pos) IREV(IVALS(buf,pos))
 #define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
+#define RSSVALS(buf,pos,val) SSVALS(buf,pos,SREV(val))
 #define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
+#define RSIVALS(buf,pos,val) SIVALS(buf,pos,IREV(val))
 
-#define DBG_RW_PCVAL(charmode,string,depth,base,read,inbuf,outbuf,len) \
-       RW_PCVAL(read,inbuf,outbuf,len) \
-       DEBUG(5,("%s%04x %s: ", \
-             tab_depth(depth), base,string)); \
-    if (charmode) print_asc(5, (unsigned char*)(outbuf), (len)); else \
-       { int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%02x ", (outbuf)[idx])); } } \
-       DEBUG(5,("\n"));
-
-#define DBG_RW_PSVAL(charmode,string,depth,base,read,inbuf,outbuf,len) \
-       RW_PSVAL(read,inbuf,outbuf,len) \
-       DEBUG(5,("%s%04x %s: ", \
-             tab_depth(depth), base,string)); \
-    if (charmode) print_asc(5, (unsigned char*)(outbuf), 2*(len)); else \
-       { int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%04x ", (outbuf)[idx])); } } \
-       DEBUG(5,("\n"));
-
-#define DBG_RW_PIVAL(charmode,string,depth,base,read,inbuf,outbuf,len) \
-       RW_PIVAL(read,inbuf,outbuf,len) \
-       DEBUG(5,("%s%04x %s: ", \
-             tab_depth(depth), base,string)); \
-    if (charmode) print_asc(5, (unsigned char*)(outbuf), 4*(len)); else \
-       { int idx; for (idx = 0; idx < len; idx++) { DEBUG(5,("%08x ", (outbuf)[idx])); } } \
-       DEBUG(5,("\n"));
-
-#define DBG_RW_CVAL(string,depth,base,read,inbuf,outbuf) \
-       RW_CVAL(read,inbuf,outbuf,0) \
-       DEBUG(5,("%s%04x %s: %02x\n", \
-             tab_depth(depth), base, string, outbuf));
-
-#define DBG_RW_SVAL(string,depth,base,read,inbuf,outbuf) \
-       RW_SVAL(read,inbuf,outbuf,0) \
-       DEBUG(5,("%s%04x %s: %04x\n", \
-             tab_depth(depth), base, string, outbuf));
-
-#define DBG_RW_IVAL(string,depth,base,read,inbuf,outbuf) \
-       RW_IVAL(read,inbuf,outbuf,0) \
-       DEBUG(5,("%s%04x %s: %08x\n", \
-             tab_depth(depth), base, string, outbuf));
+/* Alignment macros. */
+#define ALIGN4(p,base) ((p) + ((4 - (PTR_DIFF((p), (base)) & 3)) & 3))
+#define ALIGN2(p,base) ((p) + ((2 - (PTR_DIFF((p), (base)) & 1)) & 1))
 
+#endif /* _BYTEORDER_H */