Control: tags -1 patch

Hi,

I had a go at fixing all the big endian issues. Things seem to work OK.
I couldn't test the sound since the machine I was using has no sound
card, but it doesn't crash!

James
Description: Fix endianness issues
 This changes the various pieces of code which relied on being on a little
 endian machine to be platform independant.
Author: James Cowgill <[email protected]>
Bug-Debian: https://bugs.debian.org/440673
---
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
--- a/src/nikwi/gfx.cpp
+++ b/src/nikwi/gfx.cpp
@@ -37,21 +37,19 @@ bool		fullscreen = false;
 
 static SDL_Joystick	*joy = NULL;
 
+static unsigned short read_ushort_le(unsigned char* data)
+{
+	return data[0] | data[1] << 8;
+}
+
 SDL_Surface *createSurface(int width, int height, bool colorKey)
 {
 	SDL_Surface	*surf;
 	Uint32		rm, gm, bm;
 
-	#if SDL_BYTEORDER == SDL_BIG_ENDIAN
-	#error makeit
-	rm = 0xFF000000;
-	gm = 0x00FF0000;
-	bm = 0x0000FF00;
-	#else
 	rm = 0x0000001F;
 	gm = 0x000007E0;
 	bm = 0x0000F800;
-	#endif
 	
 	if (screen && screen->format)
 	{
@@ -89,14 +87,8 @@ SDL_Surface *loadImage(String file)
 		return NULL;
 	}
 	pixels = (unsigned short*)&data[8];
-	#if SDL_BYTEORDER == SDL_BIG_ENDIAN
-	#error redo
-	width = (data[0] << 8)|data[1];
-	height = (data[2] << 8)|data[3];
-	#else
-	width = *((unsigned short*)(data + 4));
-	height = *((unsigned short*)(data + 6));
-	#endif
+	width = read_ushort_le(data + 4);
+	height = read_ushort_le(data + 6);
 	
 	surf = createSurface(width, height);
 	if (!surf)
@@ -118,7 +110,7 @@ SDL_Surface *loadImage(String file)
 			int	gv = pixels[index++];
 			int	bv = pixels[index++];
 			*/
-			unsigned int	pixel = pixels[index++];
+			unsigned int	pixel = read_ushort_le((unsigned char*) &pixels[index++]);
 			int		rv, gv, bv;
 			
 			bv = (pixel&31) << 3;
--- a/src/tools/bmp2ut/bmp2ut.c
+++ b/src/tools/bmp2ut/bmp2ut.c
@@ -47,6 +47,12 @@ SDL_Surface	*mask = NULL;
 int		alpha = 0;
 int		halfBits = 0;
 
+static void fwrite_ushort_le(unsigned short value, FILE *file)
+{
+    fputc(value & 0xFF, file);
+    fputc(value >> 8, file);
+}
+
 void convert(void)
 {
 	char		id[3] = "UT";
@@ -68,10 +74,8 @@ void convert(void)
 	f = fopen(output, "wb");
 	fwrite(id, 3, 1, f);
 	fwrite(&flags, 1, 1, f);
-	wv = surf->w;
-	hv = surf->h;
-	fwrite(&wv, 2, 1, f);
-	fwrite(&hv, 2, 1, f);
+	fwrite_ushort_le(surf->w, f);
+	fwrite_ushort_le(surf->h, f);
 	rgb = (unsigned char*)surf->pixels;
 	mrgb = mask?((unsigned char*)mask->pixels):0;
 	for (i=0;i<surf->w*surf->h;i++)
@@ -91,7 +95,7 @@ void convert(void)
 		{
 			unsigned short	rgb;
 			rgb = (b >> 3)|((g >> 2) << 5)|((r >> 3) << 11);
-			fwrite(&rgb, 2, 1, f);
+			fwrite_ushort_le(rgb, f);
 		}
 		else
 		{
--- a/src/tools/upack/upack.c
+++ b/src/tools/upack/upack.c
@@ -49,6 +49,20 @@ char		*input = "data";
 char		*output = "justdata.up";
 FILE		*f;
 
+static void fwrite_ushort_le(unsigned short value, FILE *file)
+{
+	fputc(value & 0xFF, file);
+	fputc(value >> 8, file);
+}
+
+static void fwrite_uint_le(unsigned int value, FILE *file)
+{
+	fputc(value & 0xFF, file);
+	fputc((value >> 8) & 0xFF, file);
+	fputc((value >> 16) & 0xFF, file);
+	fputc(value >> 24, file);
+}
+
 static void addEntry(char *name, unsigned int pos, unsigned int size)
 {
 	DirEntry	*de = malloc(sizeof(DirEntry));
@@ -79,7 +93,6 @@ static void startPackage(void)
 {
 	char		id[2] = "UP";
 	char		version = 1;
-	unsigned int	dirPlace = 0;
 	f = fopen(output, "wb");
 	if (!f)
 	{
@@ -88,9 +101,9 @@ static void startPackage(void)
 	}
 	fwrite(id, 2, 1, f);
 	fwrite(&version, 1, 1, f);
-	fwrite(&dirPlace, 4, 1, f);
-	fwrite(&entryCount, 4, 1, f);
-	fwrite(&priority, 4, 1, f);
+	fwrite_uint_le(0, f);
+	fwrite_uint_le(0, f);
+	fwrite_uint_le(priority, f);
 }
 
 static void saveDirectoryEntries()
@@ -101,14 +114,14 @@ static void saveDirectoryEntries()
 	for (;de;de = de->next)
 	{
 		unsigned short	len = strlen(de->name);
-		fwrite(&len, 2, 1, f);
+		fwrite_ushort_le(len, f);
 		fwrite(de->name, len, 1, f);
-		fwrite(&de->pos, 4, 1, f);
-		fwrite(&de->size, 4, 1, f);
+		fwrite_uint_le(de->pos, f);
+		fwrite_uint_le(de->size, f);
 	}
 	fseek(f, 3, SEEK_SET);
-	fwrite(&dirPlace, 4, 1, f);
-	fwrite(&entryCount, 4, 1, f);
+	fwrite_uint_le(dirPlace, f);
+	fwrite_uint_le(entryCount, f);
 	printf("%i entries written\n", entryCount);
 }
 
--- a/src/nikwi/nikio.cpp
+++ b/src/nikwi/nikio.cpp
@@ -43,6 +43,18 @@ static String		packFileName;
 static uint		dirPlace, entryCount;
 static PackEntry	*entry;
 
+static unsigned short fread_ushort_le(FILE *file)
+{
+	unsigned char lsb = (unsigned char) fgetc(file);
+	return lsb | ((unsigned char) fgetc(file)) << 8;
+}
+
+static unsigned int fread_uint_le(FILE *file)
+{
+	unsigned short lsw = fread_ushort_le(file);
+	return lsw | fread_ushort_le(file) << 16;
+}
+
 void initNikIO()
 {
 	FILE	*f;
@@ -56,8 +68,8 @@ void initNikIO()
 	if (f)
 	{
 		fseek(f, 3, SEEK_SET);
-		fread(&dirPlace, 4, 1, f);
-		fread(&entryCount, 4, 1, f);
+		dirPlace = fread_uint_le(f);
+		entryCount = fread_uint_le(f);
 		fseek(f, dirPlace, SEEK_SET);
 		entry = (PackEntry*)malloc(sizeof(PackEntry)*entryCount);
 		for (uint i=0;i<entryCount;i++)
@@ -65,12 +77,12 @@ void initNikIO()
 			unsigned short	len;
 			uint		start, size;
 			String		name;
-			fread(&len, 2, 1, f);
+			len = fread_ushort_le(f);
 			name = (String)malloc(len + 1);
 			fread(name, len, 1, f);
 			name[len] = 0;
-			fread(&start, 4, 1, f);
-			fread(&size, 4, 1, f);
+			start = fread_uint_le(f);
+			size = fread_uint_le(f);
 			entry[i].name = name;
 			entry[i].start = start;
 			entry[i].size = size;
--- a/src/slashfx/main.c
+++ b/src/slashfx/main.c
@@ -23,6 +23,7 @@
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <stdint.h>
 #include <string.h>
 #include <math.h>
 #include <slashfx.h>
@@ -46,8 +47,8 @@ typedef struct _Effect
 	double		squarization;
 	double		delay, delayAmp;
 	double		purify;
-	int		waveFuncCode;
-	int		length;
+	unsigned int	waveFuncCode;
+	unsigned int	length;
 	int		upHerz;
 	
 	Envelope	freqEnv;
@@ -89,18 +90,37 @@ static void readBuffer(Buffer *buffer, v
 	buffer->position += length;
 }
 
+static uint32_t readBuffer32(Buffer *buffer)
+{
+	uint8_t b1 = buffer->data[buffer->position++];
+	uint8_t b2 = buffer->data[buffer->position++];
+	uint8_t b3 = buffer->data[buffer->position++];
+	uint8_t b4 = buffer->data[buffer->position++];
+
+	return b1 | b2 << 8 | b3 << 16 | b4 << 24;
+}
+
+static double readBufferDouble(Buffer *buffer)
+{
+	union { uint64_t i; double d; } conv;
+
+	conv.i = readBuffer32(buffer);
+	conv.i |= (uint64_t) readBuffer32(buffer) << 32;
+	return conv.d;
+}
+
 static void readEnvelope(Buffer *fb, Envelope *e)
 {
 	int	i;
 	
-	readBuffer(fb, &e->size, sizeof(int));
+	e->size = readBuffer32(fb);
 	e->x = malloc(sizeof(double)*e->size);
 	e->y = malloc(sizeof(double)*e->size);
 	
 	for (i=0;i<e->size;i++)
 	{
-		readBuffer(fb, &e->x[i], sizeof(double));
-		readBuffer(fb, &e->y[i], sizeof(double));
+		e->x[i] = readBufferDouble(fb);
+		e->y[i] = readBufferDouble(fb);
 	}
 }
 
@@ -129,14 +149,14 @@ int ssfxLoadEffectFromMemory(char *ptr,
 		return 0;
 	}
 	
-	readBuffer(fb, &fx.globalVol, sizeof(double));
-	readBuffer(fb, &fx.squarization, sizeof(double));
-	readBuffer(fb, &fx.delay, sizeof(double));
-	readBuffer(fb, &fx.delayAmp, sizeof(double));
-	readBuffer(fb, &fx.purify, sizeof(double));
-	readBuffer(fb, &fx.waveFuncCode, sizeof(int));
-	readBuffer(fb, &fx.length, sizeof(int));
-	readBuffer(fb, &fx.upHerz, sizeof(int));
+	fx.globalVol = readBufferDouble(fb);
+	fx.squarization = readBufferDouble(fb);
+	fx.delay = readBufferDouble(fb);
+	fx.delayAmp = readBufferDouble(fb);
+	fx.purify = readBufferDouble(fb);
+	fx.waveFuncCode = readBuffer32(fb);
+	fx.length = readBuffer32(fb);
+	fx.upHerz = readBuffer32(fb);
 	
 	readEnvelope(fb, &fx.freqEnv);
 	readEnvelope(fb, &fx.volEnv);
--- a/src/nikwi/world.cpp
+++ b/src/nikwi/world.cpp
@@ -29,6 +29,20 @@
 
 World	*world = NULL;
 
+static uint read_uint_le(const void *data)
+{
+	unsigned char *ptr = (unsigned char *) data;
+	return ptr[0] | ptr[1] << 8 | ptr[2] << 16 | ptr[3] << 24;
+}
+
+static void fwrite_uint_le(uint value, FILE *file)
+{
+	fputc(value & 0xFF, file);
+	fputc((value >> 8) & 0xFF, file);
+	fputc((value >> 16) & 0xFF, file);
+	fputc(value >> 24, file);
+}
+
 //World
 World::World()
 {
@@ -103,15 +117,15 @@ void World::loadWorld(String file)
 	uint	*tileCode, *iv;
 	if (!data)
 		return;
-	newWorld(*((uint*)&data[0]), *((uint*)&data[4]));
+	newWorld(read_uint_le(data), read_uint_le(data + 4));
 	tileCode = (uint*)&data[8];
 	for (uint y=0;y<height;y++)
 	{
 		for (uint x=0;x<width;x++,tileCode++)
 		{
-			if (*tileCode)
+			if (read_uint_le(tileCode))
 			{
-				tile[y*width + x] = new Tile(*tileCode);
+				tile[y*width + x] = new Tile(read_uint_le(tileCode));
 				tile[y*width + x]->x = x*32;
 				tile[y*width + x]->y = y*32;
 			}
@@ -120,14 +134,14 @@ void World::loadWorld(String file)
 
 	prepareForPhysics();
 
-	oc = *tileCode;
+	oc = read_uint_le(tileCode);
 	iv = ++tileCode;
 	for (uint i=0;i<oc;i++)
 	{
-		uint	code = *(iv++);
-		uint	flags = (*(iv++))&0x1;
-		int	x = (int)*(iv++);
-		int	y = (int)*(iv++);
+		uint	code = read_uint_le(iv++);
+		uint	flags = read_uint_le(iv++) & 0x1;
+		int	x = (int) read_uint_le(iv++);
+		int	y = (int) read_uint_le(iv++);
 		Object	*obj = createObject(code, x, y);
 		obj->state->mirror = flags;
 		if (code == makeCode("hero"))
@@ -139,8 +153,8 @@ void World::loadWorld(String file)
 		}
 		obj->init();
 	}
-	bgnd = *(iv++);
-	bgndImage = *iv;
+	bgnd = read_uint_le(iv++);
+	bgndImage = read_uint_le(iv);
 	free(data);
 	
 	refreshStaticPart = true;
@@ -159,27 +173,27 @@ void World::saveWorld(String file)
 {
 	FILE	*f = fopen(file, "wb");
 	uint	oc = 0;
-	fwrite(&width, 4, 1, f);
-	fwrite(&height, 4, 1, f);
+	fwrite_uint_le(width, f);
+	fwrite_uint_le(height, f);
 	for (uint i=0;i<width*height;i++)
 	{
 		if (tile[i])
-			fwrite(&tile[i]->tclass->code, 4, 1, f);
+			fwrite_uint_le(tile[i]->tclass->code, f);
 		else
-			fwrite(&tile[i], 4, 1, f);	//zero//
+			fwrite_uint_le(0, f);
 	}
 	for (Object *obj=firstObject;obj;obj = obj->next)
 		oc++;
-	fwrite(&oc, 4, 1, f);
+	fwrite_uint_le(oc, f);
 	for (Object *obj=firstObject;obj;obj = obj->next)
 	{
-		fwrite(&obj->oclass->code, 4, 1, f);
-		fwrite(&obj->state->mirror, 4, 1, f);
-		fwrite(&obj->x, 4, 1, f);
-		fwrite(&obj->y, 4, 1, f);
+		fwrite_uint_le(obj->oclass->code, f);
+		fwrite_uint_le(obj->state->mirror, f);
+		fwrite_uint_le(obj->x, f);
+		fwrite_uint_le(obj->y, f);
 	}
-	fwrite(&bgnd, 4, 1, f);
-	fwrite(&bgndImage, 4, 1, f);
+	fwrite_uint_le(bgnd, f);
+	fwrite_uint_le(bgndImage, f);
 	fclose(f);
 }
 
--- a/src/us/bytecode.cpp
+++ b/src/us/bytecode.cpp
@@ -61,11 +61,11 @@ void UScriptByteCode::putInt(uint addr,
 
 void UScriptByteCode::putFloat(uint addr, float f)
 {
+	union { int i; float f; } conv;
+
 	makeRoom(addr + 4);
-	bc[addr++] = ((unsigned char*)(&f))[0];
-	bc[addr++] = ((unsigned char*)(&f))[1];
-	bc[addr++] = ((unsigned char*)(&f))[2];
-	bc[addr] = ((unsigned char*)(&f))[3];
+	conv.f = f;
+	putInt(addr, conv.i);
 }
 
 void UScriptByteCode::addByte(unsigned char b)
@@ -98,7 +98,9 @@ int UScriptByteCode::getInt(uint addr)
 
 float UScriptByteCode::getFloat(uint addr)
 {
-	return *((float*)&bc[addr]);
+	union { int i; float f; } conv;
+	conv.i = getInt(addr);
+	return conv.f;
 }
 
 String UScriptByteCode::getSS(uint index)

Attachment: signature.asc
Description: This is a digitally signed message part

Reply via email to