1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #ifndef _OSL_ENDIAN_H_ 29 #define _OSL_ENDIAN_H_ 30 31 #include <sal/types.h> 32 33 #ifdef __cplusplus 34 extern "C" { 35 #endif 36 37 /** Determine the platform byte order as _BIG_ENDIAN, _LITTLE_ENDIAN, ... 38 */ 39 #ifdef _WIN32 40 # if defined(_M_IX86) 41 # define _LITTLE_ENDIAN 42 # elif defined(_M_MRX000) 43 # define _LITTLE_ENDIAN 44 # elif defined(_M_ALPHA) 45 # define _LITTLE_ENDIAN 46 # elif defined(_M_PPC) 47 # define _LITTLE_ENDIAN 48 # endif 49 #endif 50 51 #ifdef LINUX 52 # include <endian.h> 53 # if __BYTE_ORDER == __LITTLE_ENDIAN 54 # ifndef _LITTLE_ENDIAN 55 # define _LITTLE_ENDIAN 56 # endif 57 # elif __BYTE_ORDER == __BIG_ENDIAN 58 # ifndef _BIG_ENDIAN 59 # define _BIG_ENDIAN 60 # endif 61 # elif __BYTE_ORDER == __PDP_ENDIAN 62 # define _PDP_ENDIAN 63 # endif 64 #endif 65 66 #ifdef NETBSD 67 # include <machine/endian.h> 68 # if BYTE_ORDER == LITTLE_ENDIAN 69 # define _LITTLE_ENDIAN 70 # elif BYTE_ORDER == BIG_ENDIAN 71 # define _BIG_ENDIAN 72 # elif BYTE_ORDER == PDP_ENDIAN 73 # define _PDP_ENDIAN 74 # endif 75 #endif 76 77 #ifdef FREEBSD 78 # include <sys/param.h> 79 # include <machine/endian.h> 80 #if __FreeBSD_version < 500000 81 # if BYTE_ORDER == LITTLE_ENDIAN 82 # define _LITTLE_ENDIAN 83 # elif BYTE_ORDER == BIG_ENDIAN 84 # define _BIG_ENDIAN 85 # elif BYTE_ORDER == PDP_ENDIAN 86 # define _PDP_ENDIAN 87 # endif 88 #endif 89 #endif 90 91 #ifdef SCO 92 # include <sys/types.h> 93 # include <sys/byteorder.h> 94 # if BYTE_ORDER == LITTLE_ENDIAN 95 # define _LITTLE_ENDIAN 96 # elif BYTE_ORDER == BIG_ENDIAN 97 # define _BIG_ENDIAN 98 # elif BYTE_ORDER == PDP_ENDIAN 99 # define _PDP_ENDIAN 100 # endif 101 #endif 102 103 #ifdef AIX 104 # include <sys/machine.h> 105 # if BYTE_ORDER == LITTLE_ENDIAN 106 # define _LITTLE_ENDIAN 107 # elif BYTE_ORDER == BIG_ENDIAN 108 # define _BIG_ENDIAN 109 # elif BYTE_ORDER == PDP_ENDIAN 110 # define _PDP_ENDIAN 111 # endif 112 #endif 113 114 #ifdef HPUX 115 # include <machine/param.h> 116 #endif 117 118 #ifdef _WIN16 119 # define _LITTLE_ENDIAN 120 #endif 121 122 #ifdef OS2 123 # include <machine/endian.h> 124 #endif 125 126 #ifdef SOLARIS 127 # include <sys/isa_defs.h> 128 #endif 129 130 #ifdef MACOSX 131 # include <machine/endian.h> 132 # if BYTE_ORDER == LITTLE_ENDIAN 133 # ifndef _LITTLE_ENDIAN 134 # define _LITTLE_ENDIAN 135 # endif 136 # elif BYTE_ORDER == BIG_ENDIAN 137 # ifndef _BIG_ENDIAN 138 # define _BIG_ENDIAN 139 # endif 140 # elif BYTE_ORDER == PDP_ENDIAN 141 # ifndef _PDP_ENDIAN 142 # define _PDP_ENDIAN 143 # endif 144 # endif 145 #endif 146 147 /** Check supported platform. 148 */ 149 #if !defined(_WIN32) && !defined(_WIN16) && !defined(OS2) && \ 150 !defined(LINUX) && !defined(NETBSD) && !defined(SCO) && \ 151 !defined(AIX) && !defined(HPUX) && \ 152 !defined(SOLARIS) && !defined(MACOSX) && !defined(FREEBSD) 153 # error "Target platform not specified !" 154 #endif 155 156 157 /** Define the determined byte order as OSL_BIGENDIAN or OSL_LITENDIAN. 158 */ 159 #if defined _LITTLE_ENDIAN 160 # define OSL_LITENDIAN 161 #elif defined _BIG_ENDIAN 162 # define OSL_BIGENDIAN 163 #else 164 # error undetermined endianess 165 #endif 166 167 168 /** Define macros for byte order manipulation. 169 */ 170 #ifndef OSL_MAKEBYTE 171 # define OSL_MAKEBYTE(nl, nh) ((sal_uInt8)(((nl) & 0x0F) | (((nh) & 0x0F) << 4))) 172 #endif 173 #ifndef OSL_LONIBBLE 174 # define OSL_LONIBBLE(b) ((sal_uInt8)((b) & 0x0F)) 175 #endif 176 #ifndef OSL_HINIBBLE 177 # define OSL_HINIBBLE(b) ((sal_uInt8)(((b) >> 4) & 0x0F)) 178 #endif 179 180 #ifndef OSL_MAKEWORD 181 # define OSL_MAKEWORD(bl, bh) ((sal_uInt16)((bl) & 0xFF) | (((sal_uInt16)(bh) & 0xFF) << 8)) 182 #endif 183 #ifndef OSL_LOBYTE 184 # define OSL_LOBYTE(w) ((sal_uInt8)((sal_uInt16)(w) & 0xFF)) 185 #endif 186 #ifndef OSL_HIBYTE 187 # define OSL_HIBYTE(w) ((sal_uInt8)(((sal_uInt16)(w) >> 8) & 0xFF)) 188 #endif 189 190 #ifndef OSL_MAKEDWORD 191 # define OSL_MAKEDWORD(wl, wh) ((sal_uInt32)((wl) & 0xFFFF) | (((sal_uInt32)(wh) & 0xFFFF) << 16)) 192 #endif 193 #ifndef OSL_LOWORD 194 # define OSL_LOWORD(d) ((sal_uInt16)((sal_uInt32)(d) & 0xFFFF)) 195 #endif 196 #ifndef OSL_HIWORD 197 # define OSL_HIWORD(d) ((sal_uInt16)(((sal_uInt32)(d) >> 16) & 0xFFFF)) 198 #endif 199 200 201 /** Define macros for swapping between host and network byte order. 202 */ 203 #ifdef OSL_BIGENDIAN 204 #ifndef OSL_NETWORD 205 # define OSL_NETWORD(w) (sal_uInt16)(w) 206 #endif 207 #ifndef OSL_NETDWORD 208 # define OSL_NETDWORD(d) (sal_uInt32)(d) 209 #endif 210 #else /* OSL_LITENDIAN */ 211 #ifndef OSL_NETWORD 212 # define OSL_NETWORD(w) OSL_MAKEWORD(OSL_HIBYTE(w),OSL_LOBYTE(w)) 213 #endif 214 #ifndef OSL_NETDWORD 215 # define OSL_NETDWORD(d) OSL_MAKEDWORD(OSL_NETWORD(OSL_HIWORD(d)),OSL_NETWORD(OSL_LOWORD(d))) 216 #endif 217 #endif /* OSL_BIGENDIAN */ 218 219 220 /** Define macros for swapping between byte orders. 221 */ 222 #ifndef OSL_SWAPWORD 223 # define OSL_SWAPWORD(w) OSL_MAKEWORD(OSL_HIBYTE(w),OSL_LOBYTE(w)) 224 #endif 225 #ifndef OSL_SWAPDWORD 226 # define OSL_SWAPDWORD(d) OSL_MAKEDWORD(OSL_SWAPWORD(OSL_HIWORD(d)),OSL_SWAPWORD(OSL_LOWORD(d))) 227 #endif 228 229 230 #ifdef __cplusplus 231 } 232 #endif 233 234 #endif /*_OSL_ENDIAN_H_ */ 235 236