Skip to content

Commit

Permalink
copy endianess header from wargus, so we share the code
Browse files Browse the repository at this point in the history
  • Loading branch information
timfel committed Jul 16, 2021
1 parent 1ecd622 commit eb45140
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 63 deletions.
86 changes: 86 additions & 0 deletions endian.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// _________ __ __
// / _____// |_____________ _/ |______ ____ __ __ ______
// \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
// / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
// /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
// \/ \/ \//_____/ \/
// ______________________ ______________________
// T H E W A R B E G I N S
// Stratagus - A free fantasy real time strategy game engine
//
//
// (c) Copyright 1998-2004 by Lutz Sammer
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; only version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//

#ifndef __WENDIAN_H__
#define __WENDIAN_H__

#ifdef __aarch64__
#ifdef __AARCH64EB__
# define __BYTE_ORDER __BIG_ENDIAN
#define __BIG_ENDIAN__ 4321
#else
# define __BYTE_ORDER __LITTLE_ENDIAN
#define __LITTLE_ENDIAN__ 1234
#endif
#endif

// From SDL_byteorder.h
#if defined(__i386__) || defined(__ia64__) || defined(WIN32) || \
(defined(__alpha__) || defined(__alpha)) || \
defined(__arm__) || \
(defined(__mips__) && defined(__MIPSEL__)) || \
defined(__SYMBIAN32__) || \
defined(__x86_64__) || \
defined(__LITTLE_ENDIAN__)
#ifdef __cplusplus
static inline unsigned short FetchLE16(unsigned char*& p) {
unsigned short s = *(unsigned short*)p;
p += 2;
return s;
}
static inline unsigned int FetchLE32(unsigned char*& p) {
unsigned int s = *(unsigned int*)p;
p += 4;
return s;
}
#else
#define FetchLE16(p) (*((unsigned short*)(p))); p += 2
#define FetchLE32(p) (*((unsigned int*)(p))); p += 4
#endif
#define AccessLE16(p) (*((unsigned short*)(p)))
#define AccessLE32(p) (*((unsigned int*)(p)))
#define ConvertLE16(v) (v)
#define ConvertLE32(v) (v)
#else
static inline unsigned short Swap16(unsigned short D) {
return ((D << 8) | (D >> 8));
}
static inline unsigned int Swap32(unsigned int D) {
return ((D << 24) | ((D << 8) & 0x00FF0000) | ((D >> 8) & 0x0000FF00) | (D >> 24));
}
#define FetchLE16(p) Swap16(*((unsigned short*)(p))); p += 2
#define FetchLE32(p) Swap32(*((unsigned int*)(p))); p += 4
#define AccessLE16(p) Swap16((*((unsigned short*)(p))))
#define AccessLE32(p) Swap32(*((unsigned int*)(p)))
#define ConvertLE16(v) Swap16(v)
#define ConvertLE32(v) Swap32(v)
#endif

#define FetchByte(p) (*((unsigned char*)(p))); ++p

#endif /* __WENDIAN_H__ */
77 changes: 14 additions & 63 deletions war1tool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@

#include <stratagus-gameutils.h>

#include "endian.h"
#include "xmi2mid.h"
#include "scale2x.h"

Expand All @@ -98,57 +99,7 @@ typedef unsigned long u_int32_t;
#define O_BINARY 0
#endif

// From SDL_byteorder.h
#if defined(__i386__) || defined(__ia64__) || defined(WIN32) || \
(defined(__alpha__) || defined(__alpha)) || \
defined(__arm__) || \
(defined(__mips__) && defined(__MIPSEL__)) || \
defined(__SYMBIAN32__) || \
defined(__x86_64__) || \
defined(__LITTLE_ENDIAN__)
#ifdef __cplusplus
static inline void SkipLE16(unsigned char*& p) {
p += 2;
}
static inline unsigned short FetchLE16(unsigned char*& p) {
unsigned short s = *(unsigned short*)p;
SkipLE16(p);
return s;
}
static inline void SkipLE32(unsigned char*& p) {
p += 4;
}
static inline unsigned int FetchLE32(unsigned char*& p) {
unsigned int s = *(unsigned int*)p;
SkipLE32(p);
return s;
}
#else
#define SkipLE16(p) p += 2
#define FetchLE16(p) (*((unsigned short*)(p))); SkipLE16(p)
#define SkipLE32(p) p += 4
#define FetchLE32(p) (*((unsigned int*)(p))); SkipLE32(p)
#endif
#define AccessLE16(p) (*((unsigned short*)(p)))
#define AccessLE32(p) (*((unsigned int*)(p)))
#define ConvertLE16(v) (v)
#else
static inline unsigned short Swap16(unsigned short D) {
return ((D << 8) | (D >> 8));
}
static inline unsigned int Swap32(unsigned int D) {
return ((D << 24) | ((D << 8) & 0x00FF0000) | ((D >> 8) & 0x0000FF00) | (D >> 24));
}
#define FetchLE16(p) Swap16(*((unsigned short*)(p))); p += 2
#define FetchLE32(p) Swap32(*((unsigned int*)(p))) p += 4
#define AccessLE16(p) Swap16((*((unsigned short*)(p))))
#define AccessLE32(p) Swap32(*((unsigned int*)(p)))
#define ConvertLE16(v) Swap16(v)
#endif

#define SkipByte(p) ++p
#define AccessByte(p) (*((unsigned char*)(p)))
#define FetchByte(p) (*((unsigned char*)(p))); SkipByte(p)

//----------------------------------------------------------------------------
// Config
Expand Down Expand Up @@ -1507,7 +1458,7 @@ void ConvertFLC_PSTAMP(unsigned char* buf)
p = buf;
height = FetchLE16(p);
width = FetchLE16(p);
SkipLE16(p);
FetchLE16(p);

image = (unsigned char*)malloc(height * width);
if (!image) {
Expand All @@ -1520,7 +1471,7 @@ void ConvertFLC_PSTAMP(unsigned char* buf)
//
// PSTAMP header
//
SkipLE32(p);
FetchLE32(p);
pstamp_type = FetchLE16(p);

switch (pstamp_type) {
Expand Down Expand Up @@ -2736,8 +2687,8 @@ int ConvertCursor(const char* file, int pale, int cure)
return 0;
}

SkipLE16(p); // hoty
SkipLE16(p); // hotx
FetchLE16(p); // hoty
FetchLE16(p); // hotx
w = FetchLE16(p);
h = FetchLE16(p);
image = (unsigned char*)calloc(sizeof(unsigned char*), w * h);
Expand Down Expand Up @@ -2980,7 +2931,7 @@ int ConvertVoc(const char* file,int voce)
}
p += 19;
++p; // 0x1A
SkipLE16(p);
FetchLE16(p);
i = FetchLE16(p); // Version
i = FetchLE16(p); // 1's comp of version

Expand All @@ -2999,8 +2950,8 @@ int ConvertVoc(const char* file,int voce)
size = (c << 16) | (b << 8) | a;
switch (type) {
case 1:
SkipByte(p);
SkipByte(p);
FetchByte(p);
FetchByte(p);
wavlen += size - 2;
wavp = (unsigned char*)realloc(wavp, wavlen);
for (i = size - 2; i; --i) {
Expand Down Expand Up @@ -3481,14 +3432,14 @@ static void SmsSaveUnits(gzFile f, unsigned char* txtp)
numunits = 0;
p2 = p;
while (p2[0] != 0xFF || p2[1] != 0xFF) {
SkipByte(p2);
SkipByte(p2);
FetchByte(p2);
FetchByte(p2);
type = FetchByte(p2);
SkipByte(p2);
FetchByte(p2);
if (type == 0x32) {
// gold mine
SkipByte(p2);
SkipByte(p2);
FetchByte(p2);
FetchByte(p2);
}
++numunits;
}
Expand All @@ -3507,7 +3458,7 @@ static void SmsSaveUnits(gzFile f, unsigned char* txtp)
player = FetchByte(p);
if (type == 0x32) {
// gold mine
SkipByte(p); // skip -0xfe
FetchByte(p); // skip -0xfe
value = (int)FetchByte(p);
value *= 250;
} else {
Expand Down

0 comments on commit eb45140

Please sign in to comment.