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

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;