]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/rfcnb/byteorder.h
SourceFormat Enforcement
[thirdparty/squid.git] / lib / rfcnb / byteorder.h
1 /*
2 * Copyright (C) 1996-2019 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /*
10 * Unix SMB/Netbios implementation.
11 * Version 1.9.
12 * SMB Byte handling
13 * Copyright (C) Andrew Tridgell 1992-1995
14 */
15
16 /*
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 */
31
32 /*
33 * This file implements macros for machine independent short and
34 * int manipulation
35 */
36
37 #undef CAREFUL_ALIGNMENT
38
39 /* we know that the 386 can handle misalignment and has the "right"
40 * byteorder */
41 #ifdef __i386__
42 #define CAREFUL_ALIGNMENT 0
43 #endif
44
45 #ifndef CAREFUL_ALIGNMENT
46 #define CAREFUL_ALIGNMENT 1
47 #endif
48
49 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
50 #define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
51 #define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val))
52
53 #if CAREFUL_ALIGNMENT
54 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
55 #define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
56 #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
57 #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
58 #define SVALS(buf,pos) ((int16)SVAL(buf,pos))
59 #define IVALS(buf,pos) ((int32)IVAL(buf,pos))
60 #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16)(val)))
61 #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val)))
62 #define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val)))
63 #define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32)(val)))
64 #else
65 /* this handles things for architectures like the 386 that can handle
66 * alignment errors */
67 /*
68 * WARNING: This section is dependent on the length of int16 and int32
69 * being correct
70 */
71 #define SVAL(buf,pos) (*(uint16 *)((char *)(buf) + (pos)))
72 #define IVAL(buf,pos) (*(uint32 *)((char *)(buf) + (pos)))
73 #define SVALS(buf,pos) (*(int16 *)((char *)(buf) + (pos)))
74 #define IVALS(buf,pos) (*(int32 *)((char *)(buf) + (pos)))
75 #define SSVAL(buf,pos,val) SVAL(buf,pos)=((uint16)(val))
76 #define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32)(val))
77 #define SSVALS(buf,pos,val) SVALS(buf,pos)=((int16)(val))
78 #define SIVALS(buf,pos,val) IVALS(buf,pos)=((int32)(val))
79 #endif
80
81 /* now the reverse routines - these are used in nmb packets (mostly) */
82 #define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
83 #define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
84
85 #define RSVAL(buf,pos) SREV(SVAL(buf,pos))
86 #define RIVAL(buf,pos) IREV(IVAL(buf,pos))
87 #define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
88 #define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
89