Skip to content

Commit 7d52913

Browse files
committed
Make endianess detection more flexible
And fix it on FreeBSD, where _BIG_ENDIAN is not a flag indicating big endian machine, but a constant to compare _BYTE_ORDER to.
1 parent 5b5fbe9 commit 7d52913

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

src/endian.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SONIC ROBO BLAST 2
2+
//-----------------------------------------------------------------------------
3+
// Copyright (C) 2014 by Sonic Team Junior.
4+
//
5+
// This program is free software distributed under the
6+
// terms of the GNU General Public License, version 2.
7+
// See the 'LICENSE' file for more details.
8+
//-----------------------------------------------------------------------------
9+
/// \file endian.h
10+
/// \brief Endian detection
11+
12+
#ifndef __ENDIAN__
13+
#define __ENDIAN__
14+
15+
#if defined(SRB2_BIG_ENDIAN) || defined(SRB2_LITTLE_ENDIAN)
16+
// defined externally
17+
#else
18+
#if defined(__FreeBSD__)
19+
// on FreeBSD, _BIG_ENDIAN is a constant to compare
20+
// _BYTE_ORDER to, not a big-endianess flag
21+
#include <sys/endian.h>
22+
#if _BYTE_ORDER == _BIG_ENDIAN
23+
#define SRB2_BIG_ENDIAN
24+
#else
25+
#define SRB2_LITTLE_ENDIAN
26+
#endif
27+
#elif defined(__BYTE_ORDER__)
28+
// defined by at least gcc and clang
29+
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
30+
#define SRB2_BIG_ENDIAN
31+
#else
32+
#define SRB2_LITTLE_ENDIAN
33+
#endif
34+
#else
35+
// check used in vanilla SRB2 (may work incorrectly if
36+
// _BIG_ENDIAN is used as on FreeBSD)
37+
#if defined(_BIG_ENDIAN)
38+
#define SRB2_BIG_ENDIAN
39+
#else
40+
#define SRB2_LITTLE_ENDIAN
41+
#endif
42+
#endif
43+
#endif
44+
45+
#endif //__ENDIAN__

src/m_swap.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
#ifndef __M_SWAP__
1515
#define __M_SWAP__
1616

17+
#include "endian.h"
18+
1719
// Endianess handling.
1820
// WAD files are stored little endian.
19-
#ifdef _BIG_ENDIAN
21+
#ifdef SRB2_BIG_ENDIAN
2022

2123
#define SHORT(x) ((INT16)(\
2224
(((UINT16)(x) & (UINT16)0x00ffU) << 8) \

src/md5.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,9 @@
4444

4545
#include "md5.h"
4646

47-
#ifdef _LIBC
48-
#include <endian.h>
49-
#if __BYTE_ORDER == __BIG_ENDIAN
50-
#define WORDS_BIGENDIAN 1
51-
#endif
52-
#endif
47+
#include "endian.h"
5348

54-
#if defined (WORDS_BIGENDIAN) || defined (_BIG_ENDIAN)
49+
#if defined (SRB2_BIG_ENDIAN)
5550
#define SWAP(n) \
5651
(((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
5752
#else

0 commit comments

Comments
 (0)