2023-04-13 14:27:37 +03:00
/*
2003-08-13 05:53:07 +04:00
Unix SMB / CIFS implementation .
SMB Byte handling
Copyright ( C ) Andrew Tridgell 1992 - 1998
2023-04-13 14:27:37 +03:00
2003-08-13 05:53:07 +04:00
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
2007-07-10 06:07:03 +04:00
the Free Software Foundation ; either version 3 of the License , or
2003-08-13 05:53:07 +04:00
( at your option ) any later version .
2023-04-13 14:27:37 +03:00
2003-08-13 05:53:07 +04:00
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
2023-04-13 14:27:37 +03:00
2003-08-13 05:53:07 +04:00
You should have received a copy of the GNU General Public License
2007-07-10 06:07:03 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2003-08-13 05:53:07 +04:00
*/
# ifndef _BYTEORDER_H
# define _BYTEORDER_H
2018-12-20 13:21:00 +03:00
# include "bytearray.h"
2003-08-13 05:53:07 +04:00
/*
2023-04-13 14:27:37 +03:00
This file implements macros for machine independent short and
2003-08-13 05:53:07 +04:00
int manipulation
Here is a description of this file that I emailed to the samba list once :
> I am confused about the way that byteorder . h works in Samba . I have
> looked at it , and I would have thought that you might make a distinction
> between LE and BE machines , but you only seem to distinguish between 386
> and all other architectures .
2023-04-13 14:27:37 +03:00
>
2003-08-13 05:53:07 +04:00
> Can you give me a clue ?
sure .
Ok , now to the macros themselves . I ' ll take a simple example , say we
want to extract a 2 byte integer from a SMB packet and put it into a
2004-05-25 21:24:24 +04:00
type called uint16_t that is in the local machines byte order , and you
want to do it with only the assumption that uint16_t is _at_least_ 16
2003-08-13 05:53:07 +04:00
bits long ( this last condition is very important for architectures
that don ' t have any int types that are 2 bytes long )
You do this :
2004-05-29 12:11:46 +04:00
# define CVAL(buf,pos) (((uint8_t *)(buf))[pos])
2010-01-05 20:40:54 +03:00
# define PVAL(buf,pos) ((unsigned int)CVAL(buf,pos))
2003-08-13 05:53:07 +04:00
# define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
2004-05-25 21:24:24 +04:00
then to extract a uint16_t value at offset 25 in a buffer you do this :
2003-08-13 05:53:07 +04:00
char * buffer = foo_bar ( ) ;
2004-05-25 21:24:24 +04:00
uint16_t xx = SVAL ( buffer , 25 ) ;
2003-08-13 05:53:07 +04:00
2023-04-13 14:29:32 +03:00
We are using the byteorder independence of the ANSI C bitshifts to do
2003-08-13 05:53:07 +04:00
the work . A good optimising compiler should turn this into efficient
code , especially if it happens to have the right byteorder : - )
I know these macros can be made a bit tidier by removing some of the
casts , but you need to look at byteorder . h as a whole to see the
reasoning behind them . byteorder . h defines the following macros :
SVAL ( buf , pos ) - extract a 2 byte SMB value
IVAL ( buf , pos ) - extract a 4 byte SMB value
2006-12-31 15:28:16 +03:00
BVAL ( buf , pos ) - extract a 8 byte SMB value
SVALS ( buf , pos ) - signed version of SVAL ( )
IVALS ( buf , pos ) - signed version of IVAL ( )
BVALS ( buf , pos ) - signed version of BVAL ( )
2003-08-13 05:53:07 +04:00
SSVAL ( buf , pos , val ) - put a 2 byte SMB value into a buffer
SIVAL ( buf , pos , val ) - put a 4 byte SMB value into a buffer
2006-12-31 15:28:16 +03:00
SBVAL ( buf , pos , val ) - put a 8 byte SMB value into a buffer
2003-08-13 05:53:07 +04:00
SSVALS ( buf , pos , val ) - signed version of SSVAL ( )
SIVALS ( buf , pos , val ) - signed version of SIVAL ( )
2006-12-31 15:28:16 +03:00
SBVALS ( buf , pos , val ) - signed version of SBVAL ( )
2003-08-13 05:53:07 +04:00
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 : - )
*/
2004-10-14 09:26:35 +04:00
2019-01-17 13:00:23 +03:00
/****************************************************************************
*
* ATTENTION : Do not use those macros anymore , use the ones from bytearray . h
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2018-12-20 13:21:00 +03:00
# define CVAL(buf,pos) ((uint32_t)_DATA_BYTE_CONST(buf, pos))
# define CVAL_NC(buf,pos) _DATA_BYTE(buf, pos) /* Non-const version of CVAL */
2003-08-13 05:53:07 +04:00
# define PVAL(buf,pos) (CVAL(buf,pos))
# define SCVAL(buf,pos,val) (CVAL_NC(buf,pos) = (val))
2019-01-17 13:00:23 +03:00
/****************************************************************************
*
* ATTENTION : Do not use those macros anymore , use the ones from bytearray . h
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2018-12-21 11:48:29 +03:00
# define SVAL(buf,pos) (uint32_t)PULL_LE_U16(buf, pos)
# define IVAL(buf,pos) PULL_LE_U32(buf, pos)
2023-11-30 05:06:15 +03:00
# define SSVALX(buf,pos,val) (CVAL_NC(buf,pos)=(uint8_t)((val)&0xFF),CVAL_NC(buf,(pos)+1)=(uint8_t)((val)>>8))
# define SIVALX(buf,pos,val) (SSVALX(buf,pos,(val)&0xFFFF),SSVALX(buf,(pos)+2,(val)>>16))
2004-05-25 21:24:24 +04:00
# define SVALS(buf,pos) ((int16_t)SVAL(buf,pos))
2004-05-25 20:24:13 +04:00
# define IVALS(buf,pos) ((int32_t)IVAL(buf,pos))
2018-12-20 13:23:46 +03:00
# define SSVAL(buf,pos,val) PUSH_LE_U16(buf, pos, val)
# define SIVAL(buf,pos,val) PUSH_LE_U32(buf, pos, val)
2018-12-20 13:57:08 +03:00
# define SSVALS(buf,pos,val) PUSH_LE_U16(buf, pos, val)
# define SIVALS(buf,pos,val) PUSH_LE_U32(buf, pos, val)
2003-08-13 05:53:07 +04:00
2019-01-17 13:00:23 +03:00
/****************************************************************************
*
* ATTENTION : Do not use those macros anymore , use the ones from bytearray . h
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2011-04-23 13:01:34 +04:00
/* 64 bit macros */
2018-12-20 13:24:29 +03:00
# define BVAL(p, ofs) PULL_LE_U64(p, ofs)
2011-04-23 13:01:34 +04:00
# define BVALS(p, ofs) ((int64_t)BVAL(p,ofs))
2018-12-20 13:24:29 +03:00
# define SBVAL(p, ofs, v) PUSH_LE_U64(p, ofs, v)
2023-11-30 05:06:15 +03:00
# define SBVALS(p, ofs, v) (SBVAL(p,ofs,(uint64_t)(v)))
2011-04-23 13:01:34 +04:00
2019-01-17 13:00:23 +03:00
/****************************************************************************
*
* ATTENTION : Do not use those macros anymore , use the ones from bytearray . h
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-13 05:53:07 +04:00
/* now the reverse routines - these are used in nmb packets (mostly) */
# define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
2023-11-30 05:06:15 +03:00
# define IREV(x) ((SREV((uint32_t)(x))<<16) | (SREV(((uint32_t)(x))>>16)))
# define BREV(x) ((IREV((uint64_t)(x))<<32) | (IREV(((uint64_t)(x))>>32)))
2003-08-13 05:53:07 +04:00
2019-01-17 13:00:23 +03:00
/****************************************************************************
*
* ATTENTION : Do not use those macros anymore , use the ones from bytearray . h
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2018-12-20 13:24:29 +03:00
# define RSVAL(buf,pos) (uint32_t)PULL_BE_U16(buf, pos)
# define RSVALS(buf,pos) PULL_BE_U16(buf, pos)
# define RIVAL(buf,pos) PULL_BE_U32(buf, pos)
# define RIVALS(buf,pos) PULL_BE_U32(buf, pos)
# define RBVAL(buf,pos) PULL_BE_U64(buf, pos)
# define RBVALS(buf,pos) PULL_BE_U64(buf, pos)
# define RSSVAL(buf,pos,val) PUSH_BE_U16(buf, pos, val)
# define RSSVALS(buf,pos,val) PUSH_BE_U16(buf, pos, val)
# define RSIVAL(buf,pos,val) PUSH_BE_U32(buf, pos, val)
# define RSIVALS(buf,pos,val) PUSH_BE_U32(buf, pos, val)
# define RSBVAL(buf,pos,val) PUSH_BE_U64(buf, pos, val)
# define RSBVALS(buf,pos,val) PUSH_BE_U64(buf, pos, val)
2003-08-13 05:53:07 +04:00
2019-01-17 13:00:23 +03:00
/****************************************************************************
*
* ATTENTION : Do not use those macros anymore , use the ones from bytearray . h
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2003-08-13 05:53:07 +04:00
# endif /* _BYTEORDER_H */