Changeset 168 for trunk/include/byteorder.h
- Timestamp:
- 03/02/10 19:08:42 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/byteorder.h
r111 r168 1 /* 1 /* 2 2 * Branched from Samba project Subversion repository, version #2: 3 3 * http://websvn.samba.org/cgi-bin/viewcvs.cgi/trunk/source/include/byteorder.h … … 28 28 #define _BYTEORDER_H 29 29 30 /* 31 This file implements macros for machine independent short and 32 int manipulation 30 /** 31 * @file 32 * 33 * This file implements macros for machine independent short and 34 * int manipulation 35 36 @verbatim 33 37 34 38 Here is a description of this file that I emailed to the samba list once: … … 54 58 Ok, now to the macros themselves. I'll take a simple example, say we 55 59 want to extract a 2 byte integer from a SMB packet and put it into a 56 type called uint16 that is in the local machines byte order, and you57 want to do it with only the assumption that uint16 is _at_least_ 1660 type called uint16_t that is in the local machines byte order, and you 61 want to do it with only the assumption that uint16_t is _at_least_ 16 58 62 bits long (this last condition is very important for architectures 59 63 that don't have any int types that are 2 bytes long) … … 65 69 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8) 66 70 67 then to extract a uint16 value at offset 25 in a buffer you do this:71 then to extract a uint16_t value at offset 25 in a buffer you do this: 68 72 69 73 char *buffer = foo_bar(); 70 uint16 xx = SVAL(buffer,25);74 uint16_t xx = SVAL(buffer,25); 71 75 72 76 We are using the byteoder independence of the ANSI C bitshifts to do … … 98 102 it also defines lots of intermediate macros, just ignore those :-) 99 103 104 @endverbatim 105 100 106 */ 101 107 … … 124 130 #define SSVALX(buf,pos,val) (CVAL_NC(buf,pos)=(unsigned char)((val)&0xFF),CVAL_NC(buf,pos+1)=(unsigned char)((val)>>8)) 125 131 #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16)) 126 #define SVALS(buf,pos) ((int16 )SVAL(buf,pos))127 #define IVALS(buf,pos) ((int32 )IVAL(buf,pos))128 #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16 )(val)))129 #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32 )(val)))130 #define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16 )(val)))131 #define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32 )(val)))132 #define SVALS(buf,pos) ((int16_t)SVAL(buf,pos)) 133 #define IVALS(buf,pos) ((int32_t)IVAL(buf,pos)) 134 #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16_t)(val))) 135 #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32_t)(val))) 136 #define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16_t)(val))) 137 #define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32_t)(val))) 132 138 133 139 #else /* CAREFUL_ALIGNMENT */ … … 136 142 alignment errors */ 137 143 /* 138 WARNING: This section is dependent on the length of int16 and int32144 WARNING: This section is dependent on the length of int16_t and int32_t 139 145 being correct 140 146 */ 141 147 142 148 /* get single value from an SMB buffer */ 143 #define SVAL(buf,pos) (*(const uint16 *)((const char *)(buf) + (pos)))144 #define SVAL_NC(buf,pos) (*(uint16 *)((char *)(buf) + (pos))) /* Non const version of above. */145 #define IVAL(buf,pos) (*(const uint32 *)((const char *)(buf) + (pos)))146 #define IVAL_NC(buf,pos) (*(uint32 *)((char *)(buf) + (pos))) /* Non const version of above. */147 #define SVALS(buf,pos) (*(const int16 *)((const char *)(buf) + (pos)))148 #define SVALS_NC(buf,pos) (*(int16 *)((char *)(buf) + (pos))) /* Non const version of above. */149 #define IVALS(buf,pos) (*(const int32 *)((const char *)(buf) + (pos)))150 #define IVALS_NC(buf,pos) (*(int32 *)((char *)(buf) + (pos))) /* Non const version of above. */149 #define SVAL(buf,pos) (*(const uint16_t *)((const char *)(buf) + (pos))) 150 #define SVAL_NC(buf,pos) (*(uint16_t *)((char *)(buf) + (pos))) /* Non const version of above. */ 151 #define IVAL(buf,pos) (*(const uint32_t *)((const char *)(buf) + (pos))) 152 #define IVAL_NC(buf,pos) (*(uint32_t *)((char *)(buf) + (pos))) /* Non const version of above. */ 153 #define SVALS(buf,pos) (*(const int16_t *)((const char *)(buf) + (pos))) 154 #define SVALS_NC(buf,pos) (*(int16_t *)((char *)(buf) + (pos))) /* Non const version of above. */ 155 #define IVALS(buf,pos) (*(const int32_t *)((const char *)(buf) + (pos))) 156 #define IVALS_NC(buf,pos) (*(int32_t *)((char *)(buf) + (pos))) /* Non const version of above. */ 151 157 152 158 /* store single value in an SMB buffer */ 153 #define SSVAL(buf,pos,val) SVAL_NC(buf,pos)=((uint16 )(val))154 #define SIVAL(buf,pos,val) IVAL_NC(buf,pos)=((uint32 )(val))155 #define SSVALS(buf,pos,val) SVALS_NC(buf,pos)=((int16 )(val))156 #define SIVALS(buf,pos,val) IVALS_NC(buf,pos)=((int32 )(val))159 #define SSVAL(buf,pos,val) SVAL_NC(buf,pos)=((uint16_t)(val)) 160 #define SIVAL(buf,pos,val) IVAL_NC(buf,pos)=((uint32_t)(val)) 161 #define SSVALS(buf,pos,val) SVALS_NC(buf,pos)=((int16_t)(val)) 162 #define SIVALS(buf,pos,val) IVALS_NC(buf,pos)=((int32_t)(val)) 157 163 158 164 #endif /* CAREFUL_ALIGNMENT */
Note: See TracChangeset
for help on using the changeset viewer.