xref: /titanic_41/usr/src/cmd/sendmail/db/include/db_swap.h (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 1996, 1997, 1998
5  *	Sleepycat Software.  All rights reserved.
6  */
7 /*
8  * Copyright (c) 1990, 1993, 1994
9  *	The Regents of the University of California.  All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *	@(#)db_swap.h	10.5 (Sleepycat) 4/10/98
40  */
41 
42 #ifndef _DB_SWAP_H_
43 #define	_DB_SWAP_H_
44 
45 /*
46  * Little endian <==> big endian 32-bit swap macros.
47  *	M_32_SWAP	swap a memory location
48  *	P_32_COPY	copy potentially unaligned 4 byte quantities
49  *	P_32_SWAP	swap a referenced memory location
50  */
51 #define	M_32_SWAP(a) {							\
52 	u_int32_t _tmp;							\
53 	_tmp = a;							\
54 	((u_int8_t *)&a)[0] = ((u_int8_t *)&_tmp)[3];			\
55 	((u_int8_t *)&a)[1] = ((u_int8_t *)&_tmp)[2];			\
56 	((u_int8_t *)&a)[2] = ((u_int8_t *)&_tmp)[1];			\
57 	((u_int8_t *)&a)[3] = ((u_int8_t *)&_tmp)[0];			\
58 }
59 #define	P_32_COPY(a, b) {						\
60 	((u_int8_t *)b)[0] = ((u_int8_t *)a)[0];			\
61 	((u_int8_t *)b)[1] = ((u_int8_t *)a)[1];			\
62 	((u_int8_t *)b)[2] = ((u_int8_t *)a)[2];			\
63 	((u_int8_t *)b)[3] = ((u_int8_t *)a)[3];			\
64 }
65 #define	P_32_SWAP(a) {							\
66 	u_int32_t _tmp;							\
67 	P_32_COPY(a, &_tmp);						\
68 	((u_int8_t *)a)[0] = ((u_int8_t *)&_tmp)[3];			\
69 	((u_int8_t *)a)[1] = ((u_int8_t *)&_tmp)[2];			\
70 	((u_int8_t *)a)[2] = ((u_int8_t *)&_tmp)[1];			\
71 	((u_int8_t *)a)[3] = ((u_int8_t *)&_tmp)[0];			\
72 }
73 
74 /*
75  * Little endian <==> big endian 16-bit swap macros.
76  *	M_16_SWAP	swap a memory location
77  *	P_16_COPY	copy potentially unaligned 2 byte quantities
78  *	P_16_SWAP	swap a referenced memory location
79  */
80 #define	M_16_SWAP(a) {							\
81 	u_int16_t _tmp;							\
82 	_tmp = (u_int16_t)a;						\
83 	((u_int8_t *)&a)[0] = ((u_int8_t *)&_tmp)[1];			\
84 	((u_int8_t *)&a)[1] = ((u_int8_t *)&_tmp)[0];			\
85 }
86 #define	P_16_COPY(a, b) {						\
87 	((u_int8_t *)b)[0] = ((u_int8_t *)a)[0];			\
88 	((u_int8_t *)b)[1] = ((u_int8_t *)a)[1];			\
89 }
90 #define	P_16_SWAP(a) {							\
91 	u_int16_t _tmp;							\
92 	P_16_COPY(a, &_tmp);						\
93 	((u_int8_t *)a)[0] = ((u_int8_t *)&_tmp)[1];			\
94 	((u_int8_t *)a)[1] = ((u_int8_t *)&_tmp)[0];			\
95 }
96 
97 #define	SWAP32(p) {							\
98 	P_32_SWAP(p);							\
99 	(p) += sizeof(u_int32_t);					\
100 }
101 #define	SWAP16(p) {							\
102 	P_16_SWAP(p);							\
103 	(p) += sizeof(u_int16_t);					\
104 }
105 #endif /* !_DB_SWAP_H_ */
106