add endian functions

This commit is contained in:
Nishi 2026-01-10 18:17:41 +09:00
commit 929df6a38c
Signed by: nishi
GPG key ID: 27EF69B208EB9343
6 changed files with 148 additions and 18 deletions

View file

@ -0,0 +1,39 @@
#ifndef __GEARBOX_ENDIAN_H__
#define __GEARBOX_ENDIAN_H__
#include <GearBox/MachDep.h>
#include <GearBox/TypeDefs.h>
#ifdef __cplusplus
extern "C" {
#endif
#if defined(_M_IX86) || defined(__x86_64__) || defined(__i386__)
#define GBEndianHostLittle
#else
#define GBEndianHostBig
#endif
#ifdef GBEndianHostLittle
GBDECL GBI16 GBEndianSwapI16BE(GBI16 in);
GBDECL GBI32 GBEndianSwapI32BE(GBI32 in);
GBDECL GBI64 GBEndianSwapI64BE(GBI64 in);
GBDECL GBU16 GBEndianSwapU16BE(GBU16 in);
GBDECL GBU32 GBEndianSwapU32BE(GBU32 in);
GBDECL GBU64 GBEndianSwapU64BE(GBU64 in);
#else
#define GBEndianSwapI16BE(in) (in)
#define GBEndianSwapI32BE(in) (in)
#define GBEndianSwapI64BE(in) (in)
#define GBEndianSwapU16BE(in) (in)
#define GBEndianSwapU32BE(in) (in)
#define GBEndianSwapU64BE(in) (in)
#endif
#ifdef __cplusplus
}
#endif
#endif

View file

@ -13,5 +13,6 @@
#include <GearBox/GL.h>
#include <GearBox/File.h>
#include <GearBox/Resource.h>
#include <GearBox/Endian.h>
#endif

View file

@ -3,6 +3,40 @@
#include <GearBox/MachDep.h>
#if defined(_MSC_VER) || defined(__WATCOMC__)
typedef __int8_t GBI8;
typedef __int16_t GBI16;
typedef __int32_t GBI32;
typedef __int64_t GBI64;
typedef unsigned __int8_t GBU8;
typedef unsigned __int16_t GBU16;
typedef unsigned __int32_t GBU32;
typedef unsigned __int64_t GBU64;
#elif __STDC_VERSION__ >= 199901L
#include <stdint.h>
typedef int8_t GBI8;
typedef int16_t GBI16;
typedef int32_t GBI32;
typedef int64_t GBI64;
typedef uint8_t GBU8;
typedef uint16_t GBU16;
typedef uint32_t GBU32;
typedef uint64_t GBU64;
#else
typedef signed char GBI8;
typedef short GBI16;
typedef int GBI32;
typedef long GBI64;
typedef unsigned char GBU8;
typedef unsigned short GBU16;
typedef unsigned int GBU32;
typedef unsigned long GBU64;
#endif
typedef struct _GBVersion GBVersion;
typedef struct _GBEngineParam GBEngineParam;
typedef struct _GBResourceKV GBResourceKV;

64
engine/src/endian.c Normal file
View file

@ -0,0 +1,64 @@
#include <GearBox/Endian.h>
#ifdef GBEndianHostLittle
static void swap(void* from, void* to, int size) {
unsigned char* f = from;
unsigned char* t = to;
int i;
for(i = 0; i < size; i++) {
t[size - 1 - i] = f[i];
}
}
#define SWAP(in, out) swap(&in, &out, sizeof(in))
GBI16 GBEndianSwapI16BE(GBI16 in) {
GBI16 out;
SWAP(in, out);
return out;
}
GBI32 GBEndianSwapI32BE(GBI32 in) {
GBI32 out;
SWAP(in, out);
return out;
}
GBI64 GBEndianSwapI64BE(GBI64 in) {
GBI64 out;
SWAP(in, out);
return out;
}
GBU16 GBEndianSwapU16BE(GBU16 in) {
GBU16 out;
SWAP(in, out);
return out;
}
GBU32 GBEndianSwapU32BE(GBU32 in) {
GBU32 out;
SWAP(in, out);
return out;
}
GBU64 GBEndianSwapU64BE(GBU64 in) {
GBU64 out;
SWAP(in, out);
return out;
}
#endif

View file

@ -2,6 +2,7 @@
#include <GearBox/File.h>
#include <GearBox/Log.h>
#include <GearBox/Endian.h>
#include <lz4.h>
@ -44,25 +45,16 @@ void* GBResourceGet(GBResource resource, const char* name, unsigned int* size) {
GBFileSeek(resource->file, 4);
while(!(GBFileRead(resource->file, fn, 128) < 128 || fn[0] == 0)) {
unsigned int actsz = 0;
unsigned int cmpsz = 0;
unsigned char c;
int i;
void* d;
GBU32 actsz = 0;
GBU32 cmpsz = 0;
int i;
void* d;
for(i = 0; i < 4; i++) {
if(GBFileRead(resource->file, &c, 1) < 1) return NULL;
if(GBFileRead(resource->file, &actsz, 4) < 4) return NULL;
if(GBFileRead(resource->file, &cmpsz, 4) < 4) return NULL;
actsz = actsz << 8;
actsz = actsz | c;
}
for(i = 0; i < 4; i++) {
if(GBFileRead(resource->file, &c, 1) < 1) return NULL;
cmpsz = cmpsz << 8;
cmpsz = cmpsz | c;
}
actsz = GBEndianSwapU32BE(actsz);
cmpsz = GBEndianSwapU32BE(cmpsz);
*size = actsz;

View file

@ -73,7 +73,7 @@ static void pack(const char* fullpath, const char* path) {
fwrite(output, 1, outsz, out);
printf("done (%6d -> %6d)\n", insz, outsz);
printf("done (%8d -> %8d)\n", insz, outsz);
total++;
totalact += insz;