Skip to content

Commit cfa2a8b

Browse files
committed
[ZH] Add endian compat for StdBIGFileSystem
1 parent ce5d683 commit cfa2a8b

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
** Command & Conquer Generals Zero Hour(tm)
3+
** Copyright 2025 TheSuperHackers
4+
**
5+
** This program is free software: you can redistribute it and/or modify
6+
** it under the terms of the GNU General Public License as published by
7+
** the Free Software Foundation, either version 3 of the License, or
8+
** (at your option) any later version.
9+
**
10+
** This program is distributed in the hope that it will be useful,
11+
** but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
** GNU General Public License for more details.
14+
**
15+
** You should have received a copy of the GNU General Public License
16+
** along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
// This file contains macros to help with endian conversions between different endian systems.
20+
#pragma once
21+
22+
#if defined(__linux__) || defined(__CYGWIN__)
23+
#include <endian.h>
24+
25+
#elif defined(__APPLE__)
26+
#include <libkern/OSByteOrder.h>
27+
28+
#define htobe16(x) OSSwapHostToBigInt16(x)
29+
#define htole16(x) OSSwapHostToLittleInt16(x)
30+
#define be16toh(x) OSSwapBigToHostInt16(x)
31+
#define le16toh(x) OSSwapLittleToHostInt16(x)
32+
33+
#define htobe32(x) OSSwapHostToBigInt32(x)
34+
#define htole32(x) OSSwapHostToLittleInt32(x)
35+
#define be32toh(x) OSSwapBigToHostInt32(x)
36+
#define le32toh(x) OSSwapLittleToHostInt32(x)
37+
38+
#define htobe64(x) OSSwapHostToBigInt64(x)
39+
#define htole64(x) OSSwapHostToLittleInt64(x)
40+
#define be64toh(x) OSSwapBigToHostInt64(x)
41+
#define le64toh(x) OSSwapLittleToHostInt64(x)
42+
43+
#elif defined(__OpenBSD__)
44+
#include <sys/endian.h>
45+
46+
#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
47+
#include <sys/endian.h>
48+
49+
#define be16toh(x) betoh16(x)
50+
#define le16toh(x) letoh16(x)
51+
52+
#define be32toh(x) betoh32(x)
53+
#define le32toh(x) letoh32(x)
54+
55+
#define be64toh(x) betoh64(x)
56+
#define le64toh(x) letoh64(x)
57+
58+
#elif defined(_WIN32) || defined(_WIN64))
59+
#if defined (_MSC_VER)
60+
#include <intrin.h>
61+
#else
62+
#include <stdlib.h>
63+
#endif // _MSC_VER
64+
65+
#define htobe16(x) _byteswap_ushort(x)
66+
#define htole16(x) (x)
67+
#define be16toh(x) _byteswap_ushort(x)
68+
#define le16toh(x) (x)
69+
70+
#define htobe32(x) _byteswap_ulong(x)
71+
#define htole32(x) (x)
72+
#define be32toh(x) _byteswap_ulong(x)
73+
#define le32toh(x) (x)
74+
75+
#define htobe64(x) _byteswap_uint64(x)
76+
#define htole64(x) (x)
77+
#define be64toh(x) _byteswap_uint64(x)
78+
#define le64toh(x) (x)
79+
80+
#else
81+
#error platform not supported
82+
#endif

GeneralsMD/Code/GameEngineDevice/Source/StdDevice/Common/StdBIGFileSystem.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "StdDevice/Common/StdBIGFile.h"
3737
#include "StdDevice/Common/StdBIGFileSystem.h"
3838
#include "Common/Registry.h"
39+
#include "Utility/endian_compat.h"
3940

4041
static const char *BIGFileIdentifier = "BIGF";
4142

@@ -113,7 +114,7 @@ ArchiveFile * StdBIGFileSystem::openArchiveFile(const Char *filename) {
113114
// read in the number of files contained in this BIG file.
114115
// change the order of the bytes cause the file size is in reverse byte order for some reason.
115116
fp->read(&numLittleFiles, 4);
116-
numLittleFiles = ntohl(numLittleFiles);
117+
numLittleFiles = be32toh(numLittleFiles);
117118

118119
DEBUG_LOG(("StdBIGFileSystem::openArchiveFile - %d are contained in archive\n", numLittleFiles));
119120
// for (Int i = 0; i < 2; ++i) {
@@ -133,8 +134,8 @@ ArchiveFile * StdBIGFileSystem::openArchiveFile(const Char *filename) {
133134
fp->read(&fileOffset, 4);
134135
fp->read(&filesize, 4);
135136

136-
filesize = ntohl(filesize);
137-
fileOffset = ntohl(fileOffset);
137+
filesize = be32toh(filesize);
138+
fileOffset = be32toh(fileOffset);
138139

139140
fileInfo->m_archiveFilename = archiveFileName;
140141
fileInfo->m_offset = fileOffset;

0 commit comments

Comments
 (0)