byteorder.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 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 #ifndef SQUID_LIB_RFCNB_BYTEORDER_H
38 #define SQUID_LIB_RFCNB_BYTEORDER_H
39 
40 #undef CAREFUL_ALIGNMENT
41 
42 /* we know that the 386 can handle misalignment and has the "right"
43  * byteorder */
44 #ifdef __i386__
45 #define CAREFUL_ALIGNMENT 0
46 #endif
47 
48 #ifndef CAREFUL_ALIGNMENT
49 #define CAREFUL_ALIGNMENT 1
50 #endif
51 
52 #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
53 #define PVAL(buf,pos) ((unsigned)CVAL(buf,pos))
54 #define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val))
55 
56 #if CAREFUL_ALIGNMENT
57 #define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
58 #define IVAL(buf,pos) (SVAL(buf,pos)|SVAL(buf,(pos)+2)<<16)
59 #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
60 #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
61 #define SVALS(buf,pos) ((int16)SVAL(buf,pos))
62 #define IVALS(buf,pos) ((int32)IVAL(buf,pos))
63 #define SSVAL(buf,pos,val) SSVALX((buf),(pos),((uint16)(val)))
64 #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val)))
65 #define SSVALS(buf,pos,val) SSVALX((buf),(pos),((int16)(val)))
66 #define SIVALS(buf,pos,val) SIVALX((buf),(pos),((int32)(val)))
67 #else
68 /* this handles things for architectures like the 386 that can handle
69  * alignment errors */
70 /*
71  * WARNING: This section is dependent on the length of int16 and int32
72  * being correct
73  */
74 #define SVAL(buf,pos) (*(uint16 *)((char *)(buf) + (pos)))
75 #define IVAL(buf,pos) (*(uint32 *)((char *)(buf) + (pos)))
76 #define SVALS(buf,pos) (*(int16 *)((char *)(buf) + (pos)))
77 #define IVALS(buf,pos) (*(int32 *)((char *)(buf) + (pos)))
78 #define SSVAL(buf,pos,val) SVAL(buf,pos)=((uint16)(val))
79 #define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32)(val))
80 #define SSVALS(buf,pos,val) SVALS(buf,pos)=((int16)(val))
81 #define SIVALS(buf,pos,val) IVALS(buf,pos)=((int32)(val))
82 #endif
83 
84 /* now the reverse routines - these are used in nmb packets (mostly) */
85 #define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
86 #define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
87 
88 #define RSVAL(buf,pos) SREV(SVAL(buf,pos))
89 #define RIVAL(buf,pos) IREV(IVAL(buf,pos))
90 #define RSSVAL(buf,pos,val) SSVAL(buf,pos,SREV(val))
91 #define RSIVAL(buf,pos,val) SIVAL(buf,pos,IREV(val))
92 
93 #endif /* SQUID_LIB_RFCNB_BYTEORDER_H */
94 

 

Introduction

Documentation

Support

Miscellaneous