00001 /*- 00002 * See the file LICENSE for redistribution information. 00003 * 00004 * Copyright (c) 1996, 1997, 1998, 1999, 2000 00005 * Sleepycat Software. All rights reserved. 00006 */ 00007 /* 00008 * Copyright (c) 1990, 1993, 1994 00009 * The Regents of the University of California. All rights reserved. 00010 * 00011 * Redistribution and use in source and binary forms, with or without 00012 * modification, are permitted provided that the following conditions 00013 * are met: 00014 * 1. Redistributions of source code must retain the above copyright 00015 * notice, this list of conditions and the following disclaimer. 00016 * 2. Redistributions in binary form must reproduce the above copyright 00017 * notice, this list of conditions and the following disclaimer in the 00018 * documentation and/or other materials provided with the distribution. 00019 * 3. Neither the name of the University nor the names of its contributors 00020 * may be used to endorse or promote products derived from this software 00021 * without specific prior written permission. 00022 * 00023 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 00024 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00025 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00026 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00027 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00028 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00029 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00030 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00032 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00033 * SUCH DAMAGE. 00034 * 00035 * $Id: db__swap_8h-source.html,v 1.1 2008/06/08 10:18:19 sebdiaz Exp $ 00036 */ 00037 00038 #ifndef _DB_SWAP_H_ 00039 #define _DB_SWAP_H_ 00040 00041 /* 00042 * Little endian <==> big endian 32-bit swap macros. 00043 * M_32_SWAP swap a memory location 00044 * P_32_COPY copy potentially unaligned 4 byte quantities 00045 * P_32_SWAP swap a referenced memory location 00046 */ 00047 #define M_32_SWAP(a) { \ 00048 u_int32_t _tmp; \ 00049 _tmp = a; \ 00050 ((u_int8_t *)&a)[0] = ((u_int8_t *)&_tmp)[3]; \ 00051 ((u_int8_t *)&a)[1] = ((u_int8_t *)&_tmp)[2]; \ 00052 ((u_int8_t *)&a)[2] = ((u_int8_t *)&_tmp)[1]; \ 00053 ((u_int8_t *)&a)[3] = ((u_int8_t *)&_tmp)[0]; \ 00054 } 00055 #define P_32_COPY(a, b) { \ 00056 ((u_int8_t *)b)[0] = ((u_int8_t *)a)[0]; \ 00057 ((u_int8_t *)b)[1] = ((u_int8_t *)a)[1]; \ 00058 ((u_int8_t *)b)[2] = ((u_int8_t *)a)[2]; \ 00059 ((u_int8_t *)b)[3] = ((u_int8_t *)a)[3]; \ 00060 } 00061 #define P_32_SWAP(a) { \ 00062 u_int32_t _tmp; \ 00063 P_32_COPY(a, &_tmp); \ 00064 ((u_int8_t *)a)[0] = ((u_int8_t *)&_tmp)[3]; \ 00065 ((u_int8_t *)a)[1] = ((u_int8_t *)&_tmp)[2]; \ 00066 ((u_int8_t *)a)[2] = ((u_int8_t *)&_tmp)[1]; \ 00067 ((u_int8_t *)a)[3] = ((u_int8_t *)&_tmp)[0]; \ 00068 } 00069 00070 /* 00071 * Little endian <==> big endian 16-bit swap macros. 00072 * M_16_SWAP swap a memory location 00073 * P_16_COPY copy potentially unaligned 2 byte quantities 00074 * P_16_SWAP swap a referenced memory location 00075 */ 00076 #define M_16_SWAP(a) { \ 00077 u_int16_t _tmp; \ 00078 _tmp = (u_int16_t)a; \ 00079 ((u_int8_t *)&a)[0] = ((u_int8_t *)&_tmp)[1]; \ 00080 ((u_int8_t *)&a)[1] = ((u_int8_t *)&_tmp)[0]; \ 00081 } 00082 #define P_16_COPY(a, b) { \ 00083 ((u_int8_t *)b)[0] = ((u_int8_t *)a)[0]; \ 00084 ((u_int8_t *)b)[1] = ((u_int8_t *)a)[1]; \ 00085 } 00086 #define P_16_SWAP(a) { \ 00087 u_int16_t _tmp; \ 00088 P_16_COPY(a, &_tmp); \ 00089 ((u_int8_t *)a)[0] = ((u_int8_t *)&_tmp)[1]; \ 00090 ((u_int8_t *)a)[1] = ((u_int8_t *)&_tmp)[0]; \ 00091 } 00092 00093 #define SWAP32(p) { \ 00094 P_32_SWAP(p); \ 00095 (p) += sizeof(u_int32_t); \ 00096 } 00097 #define SWAP16(p) { \ 00098 P_16_SWAP(p); \ 00099 (p) += sizeof(u_int16_t); \ 00100 } 00101 00102 /* 00103 * DB has local versions of htonl() and ntohl() that only operate on pointers 00104 * to the right size memory locations, the portability magic for finding the 00105 * real ones isn't worth the effort. 00106 */ 00107 #if defined(WORDS_BIGENDIAN) 00108 #define DB_HTONL(p) 00109 #define DB_NTOHL(p) 00110 #else 00111 #define DB_HTONL(p) P_32_SWAP(p) 00112 #define DB_NTOHL(p) P_32_SWAP(p) 00113 #endif 00114 00115 #endif /* !_DB_SWAP_H_ */