netatalk  4.5.0
Free and Open Source Apple Filing Protocol (AFP) Server
Loading...
Searching...
No Matches
byteorder.h
Go to the documentation of this file.
1/*
2 Unix SMB/CIFS implementation.
3 SMB Byte handling
4 Copyright (C) Andrew Tridgell 1992-1998
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19*/
20
21#ifndef _BYTEORDER_H
22#define _BYTEORDER_H
23#include <arpa/inet.h>
24
39
40#define CVAL(buf,pos) ((unsigned)(((const unsigned char *)(buf))[pos]))
41#define CVAL_NC(buf,pos) (((unsigned char *)(buf))[pos])
42#define PVAL(buf,pos) (CVAL(buf,pos))
43#define SCVAL(buf,pos,val) (CVAL_NC(buf,pos) = (val))
44
45#ifdef WORDS_BIGENDIAN
46
47#define SVAL(buf,pos) (PVAL(buf,(pos)+1)|PVAL(buf,pos)<<8)
48#define SVALS(buf,pos) ((int16_t)SVAL(buf,pos))
49#define IVAL(buf,pos) (SVAL(buf,(pos)+2)|SVAL(buf,pos)<<16)
50#define IVALS(buf,pos) ((int32_t)IVAL(buf,pos))
51#define LVAL(buf,pos) (IVAL(buf,(pos)+4)|((uint64_t)IVAL(buf,pos))<<32)
52#define LVALS(buf,pos) ((int64_t)LVAL(buf,pos))
53
54#define SSVALX(buf,pos,val) (CVAL_NC(buf,pos+1)=(unsigned char)((val)&0xFF),CVAL_NC(buf,pos)=(unsigned char)((val)>>8))
55#define SIVALX(buf,pos,val) (SSVALX(buf,pos,(val)>>16),SSVALX(buf,pos+2,((val)&0xFFFF)))
56#define SLVALX(buf,pos,val) (SIVALX(buf,pos,(val)>>32),SIVALX(buf,pos+4,((val)&0xFFFFFFFF)))
57
58#else
59
60#define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
61#define SVALS(buf,pos) ((int16_t)SVAL(buf,pos))
62#define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
63#define IVALS(buf,pos) ((int32_t)IVAL(buf,pos))
64#define LVAL(buf,pos) (IVAL(buf,pos)|((uint64_t)IVAL(buf,(pos)+4))<<32)
65#define LVALS(buf,pos) ((int64_t)LVAL(buf,pos))
66
67#define SSVALX(buf,pos,val) (CVAL_NC(buf,pos)=(unsigned char)((val)&0xFF),CVAL_NC(buf,pos+1)=(unsigned char)((val)>>8))
68#define SIVALX(buf,pos,val) (SSVALX(buf,pos,((val)&0xFFFF)),SSVALX(buf,pos+2,(val)>>16))
69#define SLVALX(buf,pos,val) (SIVALX(buf,pos,((val)&0xFFFFFFFF)),SIVALX(buf,pos+4,(val)>>32))
70
71#endif /* WORDS_BIGENDIAN */
72
73#define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16_t)(val)))
74#define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16_t)(val)))
75#define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32_t)(val)))
76#define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32_t)(val)))
77#define SLVAL(buf,pos,val) SLVALX((buf),(pos),((uint64_t)(val)))
78#define SLVALS(buf,pos,val) SLVALX((buf),(pos),((int64_t)(val)))
79
80/* now the reverse routines */
81#define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
82#define IREV(x) ((SREV((uint32_t)(x))<<16) | (SREV(((uint32_t)(x))>>16)))
83#define LREV(x) (((uint64_t)IREV(x)<<32) | (IREV((x)>>32)))
84
85#define RSVAL(buf,pos) SREV(SVAL(buf,pos))
86#define RSVALS(buf,pos) SREV(SVALS(buf,pos))
87#define RIVAL(buf,pos) IREV(IVAL(buf,pos))
88#define RIVALS(buf,pos) IREV(IVALS(buf,pos))
89#define RLVAL(buf,pos) LREV(LVAL(buf,pos))
90#define RLVALS(buf,pos) LREV(LVALS(buf,pos))
91
92#define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
93#define RSSVALS(buf,pos,val) SSVALS(buf,pos,SREV(val))
94#define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
95#define RSIVALS(buf,pos,val) SIVALS(buf,pos,IREV(val))
96
97#define RSLVAL(buf,pos,val) SLVAL(buf,pos,LREV(val))
98#define RSLVALS(buf,pos,val) SLVALS(buf,pos,LREV(val))
99
100/* Alignment macros. */
101#define ALIGN4(p,base) ((p) + ((4 - (PTR_DIFF((p), (base)) & 3)) & 3))
102#define ALIGN2(p,base) ((p) + ((2 - (PTR_DIFF((p), (base)) & 1)) & 1))
103
104#endif /* _BYTEORDER_H */