endianlove.h: don't use stdint definitions outside the stdint ifdef
[openc2e.git] / endianlove.h
blob01a0eb851fa628dc1187803ec698e59d1fed9228
1 /*
2 * endianlove.h
3 * openc2e
5 * Created by Alyssa Milburn on Sat May 22 2004.
6 * Copyright (c) 2004 Alyssa Milburn. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
20 #ifndef _ENDIANLOVE_H
21 #define _ENDIANLOVE_H
23 #include <stdlib.h> // load the standard libraries for these defines
25 #if HAVE_STDINT_H
27 #include <stdint.h>
29 typedef uint8_t uint8;
30 typedef uint16_t uint16;
31 typedef uint32_t uint32;
33 #else
35 typedef unsigned char uint8;
36 typedef unsigned short uint16;
37 typedef unsigned int uint32;
39 #endif
41 #ifndef OC2E_BIG_ENDIAN
42 # ifdef __GNU__
43 # include <endian.h>
44 # endif
45 # if __BYTE_ORDER == __LITTLE_ENDIAN || defined(_MSC_VER) || defined(__i386__)
46 # define OC2E_BIG_ENDIAN 0
47 # else
48 # define OC2E_BIG_ENDIAN 1
49 # endif
50 #endif
52 #if OC2E_BIG_ENDIAN
54 # if HAVE_BYTESWAP_H
56 #include <byteswap.h>
57 static inline uint16 swapEndianShort(uint16 a) {
58 return bswap_16(a);
61 static inline uint32 swapEndianLong(uint32 a) {
62 return bswap_32(a);
65 # else // HAVE_BYTESWAP_H
67 static inline uint16 swapEndianShort(uint16 a) {
68 return ((((uint16)(a) & 0xff00) >> 8) |
69 (((uint16)(a) & 0x00ff) << 8));
72 static inline uint32 swapEndianLong(uint32 a) {
73 return ((((uint32)(a) & 0xff000000) >> 24) |
74 (((uint32)(a) & 0x00ff0000) >> 8) |
75 (((uint32)(a) & 0x0000ff00) << 8) |
76 (((uint32)(a) & 0x000000ff) << 24));
79 # endif
81 #else // OC2E_BIG_ENDIAN
83 static inline uint16 swapEndianShort(uint16 a) {
84 return a;
87 static inline uint32 swapEndianLong(uint32 a) {
88 return a;
91 #endif
93 #endif // _ENDIANLOVE_H
95 /* vim: set noet: */