quake: fix unaligned accesses in model.c

Allows playing registered episodes.

Change-Id: I20e1ca2000776a19767a9c3caef790d4f62ca188
diff --git a/apps/plugins/sdl/progs/quake/common.c b/apps/plugins/sdl/progs/quake/common.c
index 11be234..5e842b0 100644
--- a/apps/plugins/sdl/progs/quake/common.c
+++ b/apps/plugins/sdl/progs/quake/common.c
@@ -446,58 +446,111 @@
 
 short   ShortSwap (short l)
 {
-	byte    b1,b2;
+    byte    b1,b2;
 
-	b1 = l&255;
-	b2 = (l>>8)&255;
+    b1 = l&255;
+    b2 = (l>>8)&255;
 
-	return (b1<<8) + b2;
+    return (b1<<8) + b2;
 }
 
 short   ShortNoSwap (short l)
 {
-	return l;
+    return l;
 }
 
 int    LongSwap (int l)
 {
-	byte    b1,b2,b3,b4;
+    byte    b1,b2,b3,b4;
 
-	b1 = l&255;
-	b2 = (l>>8)&255;
-	b3 = (l>>16)&255;
-	b4 = (l>>24)&255;
+    b1 = l&255;
+    b2 = (l>>8)&255;
+    b3 = (l>>16)&255;
+    b4 = (l>>24)&255;
 
-	return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
+    return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
 }
 
 int     LongNoSwap (int l)
 {
-	return l;
+    return l;
 }
 
 float FloatSwap (float f)
 {
+    union
+    {
+        float   f;
+        byte    b[4];
+    } dat1, dat2;
+        
+        
+    dat1.f = f;
+    dat2.b[0] = dat1.b[3];
+    dat2.b[1] = dat1.b[2];
+    dat2.b[2] = dat1.b[1];
+    dat2.b[3] = dat1.b[0];
+    return dat2.f;
+}
+
+float FloatNoSwap (float f)
+{
+        return f;
+}
+
+// safe for unaligned accesses
+short   ReadLittleShort (char *l)
+{
+    return *(l + 0) | (*(l + 1) << 8);
+}
+
+short   ReadBigShort (char *l)
+{
+    return *(l + 1) | (*(l + 0) << 8);
+}
+
+int    ReadLittleLong (char *l)
+{
+    return *(l + 0) | (*(l + 1) << 8) | (*(l + 2) << 16) | (*(l + 3) << 24);
+}
+
+int ReadBigLong (char *l)
+{
+    return *(l + 3) | (*(l + 2) << 8) | (*(l + 1) << 16) | (*(l + 0) << 24);
+}
+
+// same
+float ReadLittleFloat (char *f)
+{
 	union
 	{
 		float   f;
 		byte    b[4];
-	} dat1, dat2;
+	} dat2;
 	
 	
-	dat1.f = f;
-	dat2.b[0] = dat1.b[3];
-	dat2.b[1] = dat1.b[2];
-	dat2.b[2] = dat1.b[1];
-	dat2.b[3] = dat1.b[0];
+	dat2.b[0] = f[0];
+	dat2.b[1] = f[1];
+	dat2.b[2] = f[2];
+	dat2.b[3] = f[3];
 	return dat2.f;
 }
 
-float FloatNoSwap (float f)
+float ReadBigFloat (char *f)
 {
-	return f;
+	union
+	{
+		float   f;
+		byte    b[4];
+	} dat2;
+	
+	
+	dat2.b[0] = f[0];
+	dat2.b[1] = f[1];
+	dat2.b[2] = f[2];
+	dat2.b[3] = f[3];
+	return dat2.f;
 }
-
 /*
 ==============================================================================
 
@@ -1136,6 +1189,7 @@
 {
 	byte    swaptest[2] = {1,0};
 
+#if 1
 // set the byte swapping variables in a portable manner 
 #ifdef SDL
 	// This is necessary because egcs 1.1.1 mis-compiles swaptest with -O2
@@ -1162,6 +1216,7 @@
 		BigFloat = FloatNoSwap;
 		LittleFloat = FloatSwap;
 	}
+#endif
 
 	Cvar_RegisterVariable (&registered);
 	Cvar_RegisterVariable (&cmdline);
diff --git a/apps/plugins/sdl/progs/quake/common.h b/apps/plugins/sdl/progs/quake/common.h
index a1ac227..8c6ab3a 100644
--- a/apps/plugins/sdl/progs/quake/common.h
+++ b/apps/plugins/sdl/progs/quake/common.h
@@ -88,12 +88,29 @@
 
 extern	qboolean		bigendien;
 
-extern	short	(*BigShort) (short l);
-extern	short	(*LittleShort) (short l);
-extern	int	(*BigLong) (int l);
-extern	int	(*LittleLong) (int l);
-extern	float	(*BigFloat) (float l);
-extern	float	(*LittleFloat) (float l);
+// not safe on unaligned data 
+extern  short   (*BigShort) (short l);
+extern  short   (*LittleShort) (short l);
+extern  int     (*BigLong) (int l);
+extern  int     (*LittleLong) (int l);
+extern  float   (*BigFloat) (float l);
+extern  float   (*LittleFloat) (float l);
+
+#define LittleShortUnaligned(x) ReadLittleShort(((char*)(&(x))))
+#define BigShortUnaligned(x) ReadBigShort(((char*)&(x)))
+#define LittleLongUnaligned(x) ReadLittleLong(((char*)&(x)))
+#define BigLongUnaligned(x) ReadBigLong(((char*)&(x)))
+#define LittleFloatUnaligned(x) ReadLittleFloat(((char*)&(x)))
+#define BigFloatUnaligned(x) ReadBigFloat(((char*)&(x))
+
+
+// for unaligned
+short	ReadBigShort (char *l);
+short	ReadLittleShort (char *l);
+int	ReadBigLong (char *l);
+int	ReadLittleLong (char *l);
+float	ReadBigFloat (char *l);
+float	ReadLittleFloat (char *l);
 
 //============================================================================
 
diff --git a/apps/plugins/sdl/progs/quake/model.c b/apps/plugins/sdl/progs/quake/model.c
index 57fdfec..4c16c7f 100644
--- a/apps/plugins/sdl/progs/quake/model.c
+++ b/apps/plugins/sdl/progs/quake/model.c
@@ -255,6 +255,7 @@
 */
 model_t *Mod_LoadModel (model_t *mod, qboolean crash)
 {
+    //printf("loadmodel 1");
 	unsigned *buf;
 	byte	stackbuf[1024];		// avoid dirtying the cache heap
 
@@ -279,6 +280,8 @@
 //
 // load the file
 //
+        
+        //printf("loadmodel 2");
 	buf = (unsigned *)COM_LoadStackFile (mod->name, stackbuf, sizeof(stackbuf));
 	if (!buf)
 	{
@@ -294,6 +297,7 @@
 	
 	loadmodel = mod;
 
+        //printf("loadmodel 3");
 //
 // fill it in
 //
@@ -301,7 +305,7 @@
 // call the apropriate loader
 	mod->needload = NL_PRESENT;
 
-	switch (LittleLong(*(unsigned *)buf))
+	switch (LittleLongUnaligned(buf[0]))
 	{
 	case IDPOLYHEADER:
 		Mod_LoadAliasModel (mod, buf);
@@ -330,7 +334,10 @@
 {
 	model_t	*mod;
 
+        //printf("modfor1");
+        
 	mod = Mod_FindName (name);
+        //printf("modfor2");
 
 	return Mod_LoadModel (mod, crash);
 }
@@ -368,21 +375,21 @@
 	}
 	m = (dmiptexlump_t *)(mod_base + l->fileofs);
 	
-	m->nummiptex = LittleLong (m->nummiptex);
+	m->nummiptex = LittleLongUnaligned (m->nummiptex);
 	
 	loadmodel->numtextures = m->nummiptex;
 	loadmodel->textures = Hunk_AllocName (m->nummiptex * sizeof(*loadmodel->textures) , loadname);
 
 	for (i=0 ; i<m->nummiptex ; i++)
 	{
-		m->dataofs[i] = LittleLong(m->dataofs[i]);
+		m->dataofs[i] = LittleLongUnaligned(m->dataofs[i]);
 		if (m->dataofs[i] == -1)
 			continue;
 		mt = (miptex_t *)((byte *)m + m->dataofs[i]);
-		mt->width = LittleLong (mt->width);
-		mt->height = LittleLong (mt->height);
+		mt->width = LittleLongUnaligned (mt->width);
+		mt->height = LittleLongUnaligned (mt->height);
 		for (j=0 ; j<MIPLEVELS ; j++)
-			mt->offsets[j] = LittleLong (mt->offsets[j]);
+			mt->offsets[j] = LittleLongUnaligned (mt->offsets[j]);
 		
 		if ( (mt->width & 15) || (mt->height & 15) )
 			Sys_Error ("Texture %s is not 16 aligned", mt->name);
@@ -569,9 +576,9 @@
 
 	for ( i=0 ; i<count ; i++, in++, out++)
 	{
-		out->position[0] = LittleFloat (in->point[0]);
-		out->position[1] = LittleFloat (in->point[1]);
-		out->position[2] = LittleFloat (in->point[2]);
+		out->position[0] = LittleFloatUnaligned (in->point[0]);
+		out->position[1] = LittleFloatUnaligned (in->point[1]);
+		out->position[2] = LittleFloatUnaligned (in->point[2]);
 	}
 }
 
@@ -599,15 +606,15 @@
 	{
 		for (j=0 ; j<3 ; j++)
 		{	// spread the mins / maxs by a pixel
-			out->mins[j] = LittleFloat (in->mins[j]) - 1;
-			out->maxs[j] = LittleFloat (in->maxs[j]) + 1;
-			out->origin[j] = LittleFloat (in->origin[j]);
+			out->mins[j] = LittleFloatUnaligned (in->mins[j]) - 1;
+			out->maxs[j] = LittleFloatUnaligned (in->maxs[j]) + 1;
+			out->origin[j] = LittleFloatUnaligned (in->origin[j]);
 		}
 		for (j=0 ; j<MAX_MAP_HULLS ; j++)
-			out->headnode[j] = LittleLong (in->headnode[j]);
-		out->visleafs = LittleLong (in->visleafs);
-		out->firstface = LittleLong (in->firstface);
-		out->numfaces = LittleLong (in->numfaces);
+			out->headnode[j] = LittleLongUnaligned (in->headnode[j]);
+		out->visleafs = LittleLongUnaligned (in->visleafs);
+		out->firstface = LittleLongUnaligned (in->firstface);
+		out->numfaces = LittleLongUnaligned (in->numfaces);
 	}
 }
 
@@ -633,8 +640,8 @@
 
 	for ( i=0 ; i<count ; i++, in++, out++)
 	{
-		out->v[0] = (unsigned short)LittleShort(in->v[0]);
-		out->v[1] = (unsigned short)LittleShort(in->v[1]);
+		out->v[0] = (unsigned short)LittleShortUnaligned(in->v[0]);
+		out->v[1] = (unsigned short)LittleShortUnaligned(in->v[1]);
 	}
 }
 
@@ -663,7 +670,7 @@
 	for ( i=0 ; i<count ; i++, in++, out++)
 	{
 		for (j=0 ; j<8 ; j++)
-			out->vecs[0][j] = LittleFloat (in->vecs[0][j]);
+			out->vecs[0][j] = LittleFloatUnaligned (in->vecs[0][j]);
 		len1 = Length (out->vecs[0]);
 		len2 = Length (out->vecs[1]);
 		len1 = (len1 + len2)/2;
@@ -682,8 +689,8 @@
 			out->mipadjust = 1 / floor( (len1+len2)/2 + 0.1 );
 #endif
 
-		miptex = LittleLong (in->miptex);
-		out->flags = LittleLong (in->flags);
+		miptex = LittleLongUnaligned (in->miptex);
+		out->flags = LittleLongUnaligned (in->flags);
 	
 		if (!loadmodel->textures)
 		{
@@ -781,18 +788,18 @@
 
 	for ( surfnum=0 ; surfnum<count ; surfnum++, in++, out++)
 	{
-		out->firstedge = LittleLong(in->firstedge);
-		out->numedges = LittleShort(in->numedges);		
+		out->firstedge = LittleLongUnaligned(in->firstedge);
+		out->numedges = LittleShortUnaligned(in->numedges);		
 		out->flags = 0;
 
-		planenum = LittleShort(in->planenum);
-		side = LittleShort(in->side);
+		planenum = LittleShortUnaligned(in->planenum);
+		side = LittleShortUnaligned(in->side);
 		if (side)
 			out->flags |= SURF_PLANEBACK;			
 
 		out->plane = loadmodel->planes + planenum;
 
-		out->texinfo = loadmodel->texinfo + LittleShort (in->texinfo);
+		out->texinfo = loadmodel->texinfo + LittleShortUnaligned (in->texinfo);
 
 		CalcSurfaceExtents (out);
 				
@@ -800,7 +807,7 @@
 
 		for (i=0 ; i<MAXLIGHTMAPS ; i++)
 			out->styles[i] = in->styles[i];
-		i = LittleLong(in->lightofs);
+		i = LittleLongUnaligned(in->lightofs);
 		if (i == -1)
 			out->samples = NULL;
 		else
@@ -866,19 +873,19 @@
 	{
 		for (j=0 ; j<3 ; j++)
 		{
-			out->minmaxs[j] = LittleShort (in->mins[j]);
-			out->minmaxs[3+j] = LittleShort (in->maxs[j]);
+			out->minmaxs[j] = LittleShortUnaligned (in->mins[j]);
+			out->minmaxs[3+j] = LittleShortUnaligned (in->maxs[j]);
 		}
 	
-		p = LittleLong(in->planenum);
+		p = LittleLongUnaligned(in->planenum);
 		out->plane = loadmodel->planes + p;
 
-		out->firstsurface = LittleShort (in->firstface);
-		out->numsurfaces = LittleShort (in->numfaces);
+		out->firstsurface = LittleShortUnaligned (in->firstface);
+		out->numsurfaces = LittleShortUnaligned (in->numfaces);
 		
 		for (j=0 ; j<2 ; j++)
 		{
-			p = LittleShort (in->children[j]);
+			p = LittleShortUnaligned (in->children[j]);
 			if (p >= 0)
 				out->children[j] = loadmodel->nodes + p;
 			else
@@ -913,18 +920,18 @@
 	{
 		for (j=0 ; j<3 ; j++)
 		{
-			out->minmaxs[j] = LittleShort (in->mins[j]);
-			out->minmaxs[3+j] = LittleShort (in->maxs[j]);
+			out->minmaxs[j] = LittleShortUnaligned (in->mins[j]);
+			out->minmaxs[3+j] = LittleShortUnaligned (in->maxs[j]);
 		}
 
-		p = LittleLong(in->contents);
+		p = LittleLongUnaligned(in->contents);
 		out->contents = p;
 
 		out->firstmarksurface = loadmodel->marksurfaces +
-			LittleShort(in->firstmarksurface);
-		out->nummarksurfaces = LittleShort(in->nummarksurfaces);
+			LittleShortUnaligned(in->firstmarksurface);
+		out->nummarksurfaces = LittleShortUnaligned(in->nummarksurfaces);
 		
-		p = LittleLong(in->visofs);
+		p = LittleLongUnaligned(in->visofs);
 		if (p == -1)
 			out->compressed_vis = NULL;
 		else
@@ -982,9 +989,9 @@
 
 	for (i=0 ; i<count ; i++, out++, in++)
 	{
-		out->planenum = LittleLong(in->planenum);
-		out->children[0] = LittleShort(in->children[0]);
-		out->children[1] = LittleShort(in->children[1]);
+		out->planenum = LittleLongUnaligned(in->planenum);
+		out->children[0] = LittleShortUnaligned(in->children[0]);
+		out->children[1] = LittleShortUnaligned(in->children[1]);
 	}
 }
 
@@ -1049,7 +1056,7 @@
 
 	for ( i=0 ; i<count ; i++)
 	{
-		j = LittleShort(in[i]);
+		j = LittleShortUnaligned(in[i]);
 		if (j >= loadmodel->numsurfaces)
 			Sys_Error ("Mod_ParseMarksurfaces: bad surface number");
 		out[i] = loadmodel->surfaces + j;
@@ -1076,7 +1083,7 @@
 	loadmodel->numsurfedges = count;
 
 	for ( i=0 ; i<count ; i++)
-		out[i] = LittleLong (in[i]);
+		out[i] = LittleLongUnaligned (in[i]);
 }
 
 /*
@@ -1106,13 +1113,13 @@
 		bits = 0;
 		for (j=0 ; j<3 ; j++)
 		{
-			out->normal[j] = LittleFloat (in->normal[j]);
+			out->normal[j] = LittleFloatUnaligned (in->normal[j]);
 			if (out->normal[j] < 0)
 				bits |= 1<<j;
 		}
 
-		out->dist = LittleFloat (in->dist);
-		out->type = LittleLong (in->type);
+		out->dist = LittleFloatUnaligned (in->dist);
+		out->type = LittleLongUnaligned (in->type);
 		out->signbits = bits;
 	}
 }
@@ -1150,7 +1157,7 @@
 	
 	header = (dheader_t *)buffer;
 
-	i = LittleLong (header->version);
+	i = LittleLongUnaligned (header->version);
 	if (i != BSPVERSION)
             rb->splashf (HZ, "Mod_LoadBrushModel: %s has wrong version number (%i should be %i)", mod->name, i, BSPVERSION);
 
@@ -1158,7 +1165,7 @@
 	mod_base = (byte *)header;
 
 	for (i=0 ; i<sizeof(dheader_t)/4 ; i++)
-		((int *)header)[i] = LittleLong ( ((int *)header)[i]);
+		((int *)header)[i] = LittleLongUnaligned ( ((int *)header)[i]);
 
 // load into heap
 	
@@ -1292,7 +1299,7 @@
 	
 	pingroup = (daliasgroup_t *)pin;
 
-	numframes = LittleLong (pingroup->numframes);
+	numframes = LittleLongUnaligned (pingroup->numframes);
 
 	paliasgroup = Hunk_AllocName (sizeof (maliasgroup_t) +
 			(numframes - 1) * sizeof (paliasgroup->frames[0]), loadname);
@@ -1316,7 +1323,7 @@
 
 	for (i=0 ; i<numframes ; i++)
 	{
-		*poutintervals = LittleFloat (pin_intervals->interval);
+		*poutintervals = LittleFloatUnaligned (pin_intervals->interval);
 		if (*poutintervals <= 0.0)
 			Sys_Error ("Mod_LoadAliasGroup: interval<=0");
 
@@ -1396,7 +1403,7 @@
 
 	pinskingroup = (daliasskingroup_t *)pin;
 
-	numskins = LittleLong (pinskingroup->numskins);
+	numskins = LittleLongUnaligned (pinskingroup->numskins);
 
 	paliasskingroup = Hunk_AllocName (sizeof (maliasskingroup_t) +
 			(numskins - 1) * sizeof (paliasskingroup->skindescs[0]),
@@ -1414,7 +1421,7 @@
 
 	for (i=0 ; i<numskins ; i++)
 	{
-		*poutskinintervals = LittleFloat (pinskinintervals->interval);
+		*poutskinintervals = LittleFloatUnaligned (pinskinintervals->interval);
 		if (*poutskinintervals <= 0)
 			Sys_Error ("Mod_LoadAliasSkinGroup: interval<=0");
 
@@ -1459,7 +1466,7 @@
 
 	pinmodel = (mdl_t *)buffer;
 
-	version = LittleLong (pinmodel->version);
+	version = LittleLongUnaligned (pinmodel->version);
 	if (version != ALIAS_VERSION)
 		Sys_Error ("%s has wrong version number (%i should be %i)",
 				 mod->name, version, ALIAS_VERSION);
@@ -1468,33 +1475,33 @@
 // allocate space for a working header, plus all the data except the frames,
 // skin and group info
 //
-	size = 	sizeof (aliashdr_t) + (LittleLong (pinmodel->numframes) - 1) *
+	size = 	sizeof (aliashdr_t) + (LittleLongUnaligned (pinmodel->numframes) - 1) *
 			 sizeof (pheader->frames[0]) +
 			sizeof (mdl_t) +
-			LittleLong (pinmodel->numverts) * sizeof (stvert_t) +
-			LittleLong (pinmodel->numtris) * sizeof (mtriangle_t);
+			LittleLongUnaligned (pinmodel->numverts) * sizeof (stvert_t) +
+			LittleLongUnaligned (pinmodel->numtris) * sizeof (mtriangle_t);
 
 	pheader = Hunk_AllocName (size, loadname);
 	pmodel = (mdl_t *) ((byte *)&pheader[1] +
-			(LittleLong (pinmodel->numframes) - 1) *
+			(LittleLongUnaligned (pinmodel->numframes) - 1) *
 			 sizeof (pheader->frames[0]));
 	
 //	mod->cache.data = pheader;
-	mod->flags = LittleLong (pinmodel->flags);
+	mod->flags = LittleLongUnaligned (pinmodel->flags);
 
 //
 // endian-adjust and copy the data, starting with the alias model header
 //
-	pmodel->boundingradius = LittleFloat (pinmodel->boundingradius);
-	pmodel->numskins = LittleLong (pinmodel->numskins);
-	pmodel->skinwidth = LittleLong (pinmodel->skinwidth);
-	pmodel->skinheight = LittleLong (pinmodel->skinheight);
+	pmodel->boundingradius = LittleFloatUnaligned (pinmodel->boundingradius);
+	pmodel->numskins = LittleLongUnaligned (pinmodel->numskins);
+	pmodel->skinwidth = LittleLongUnaligned (pinmodel->skinwidth);
+	pmodel->skinheight = LittleLongUnaligned (pinmodel->skinheight);
 
 	if (pmodel->skinheight > MAX_LBM_HEIGHT)
 		Sys_Error ("model %s has a skin taller than %d", mod->name,
 				   MAX_LBM_HEIGHT);
 
-	pmodel->numverts = LittleLong (pinmodel->numverts);
+	pmodel->numverts = LittleLongUnaligned (pinmodel->numverts);
 
 	if (pmodel->numverts <= 0)
 		Sys_Error ("model %s has no vertices", mod->name);
@@ -1502,21 +1509,21 @@
 	if (pmodel->numverts > MAXALIASVERTS)
 		Sys_Error ("model %s has too many vertices", mod->name);
 
-	pmodel->numtris = LittleLong (pinmodel->numtris);
+	pmodel->numtris = LittleLongUnaligned (pinmodel->numtris);
 
 	if (pmodel->numtris <= 0)
 		Sys_Error ("model %s has no triangles", mod->name);
 
-	pmodel->numframes = LittleLong (pinmodel->numframes);
-	pmodel->size = LittleFloat (pinmodel->size) * ALIAS_BASE_SIZE_RATIO;
-	mod->synctype = LittleLong (pinmodel->synctype);
+	pmodel->numframes = LittleLongUnaligned (pinmodel->numframes);
+	pmodel->size = LittleFloatUnaligned (pinmodel->size) * ALIAS_BASE_SIZE_RATIO;
+	mod->synctype = LittleLongUnaligned (pinmodel->synctype);
 	mod->numframes = pmodel->numframes;
 
 	for (i=0 ; i<3 ; i++)
 	{
-		pmodel->scale[i] = LittleFloat (pinmodel->scale[i]);
-		pmodel->scale_origin[i] = LittleFloat (pinmodel->scale_origin[i]);
-		pmodel->eyeposition[i] = LittleFloat (pinmodel->eyeposition[i]);
+		pmodel->scale[i] = LittleFloatUnaligned (pinmodel->scale[i]);
+		pmodel->scale_origin[i] = LittleFloatUnaligned (pinmodel->scale_origin[i]);
+		pmodel->eyeposition[i] = LittleFloatUnaligned (pinmodel->eyeposition[i]);
 	}
 
 	numskins = pmodel->numskins;
@@ -1546,7 +1553,7 @@
 	{
 		aliasskintype_t	skintype;
 
-		skintype = LittleLong (pskintype->type);
+		skintype = LittleLongUnaligned (pskintype->type);
 		pskindesc[i].type = skintype;
 
 		if (skintype == ALIAS_SKIN_SINGLE)
@@ -1575,10 +1582,10 @@
 
 	for (i=0 ; i<pmodel->numverts ; i++)
 	{
-		pstverts[i].onseam = LittleLong (pinstverts[i].onseam);
+		pstverts[i].onseam = LittleLongUnaligned (pinstverts[i].onseam);
 	// put s and t in 16.16 format
-		pstverts[i].s = LittleLong (pinstverts[i].s) << 16;
-		pstverts[i].t = LittleLong (pinstverts[i].t) << 16;
+		pstverts[i].s = LittleLongUnaligned (pinstverts[i].s) << 16;
+		pstverts[i].t = LittleLongUnaligned (pinstverts[i].t) << 16;
 	}
 
 //
@@ -1593,12 +1600,12 @@
 	{
 		int		j;
 
-		ptri[i].facesfront = LittleLong (pintriangles[i].facesfront);
+		ptri[i].facesfront = LittleLongUnaligned (pintriangles[i].facesfront);
 
 		for (j=0 ; j<3 ; j++)
 		{
 			ptri[i].vertindex[j] =
-					LittleLong (pintriangles[i].vertindex[j]);
+					LittleLongUnaligned (pintriangles[i].vertindex[j]);
 		}
 	}
 
@@ -1614,7 +1621,7 @@
 	{
 		aliasframetype_t	frametype;
 
-		frametype = LittleLong (pframetype->type);
+		frametype = LittleLongUnaligned (pframetype->type);
 		pheader->frames[i].type = frametype;
 
 		if (frametype == ALIAS_SINGLE)
@@ -1676,8 +1683,8 @@
 
 	pinframe = (dspriteframe_t *)pin;
 
-	width = LittleLong (pinframe->width);
-	height = LittleLong (pinframe->height);
+	width = LittleLongUnaligned (pinframe->width);
+	height = LittleLongUnaligned (pinframe->height);
 	size = width * height;
 
 	pspriteframe = Hunk_AllocName (sizeof (mspriteframe_t) + size*r_pixbytes,
@@ -1688,8 +1695,8 @@
 
 	pspriteframe->width = width;
 	pspriteframe->height = height;
-	origin[0] = LittleLong (pinframe->origin[0]);
-	origin[1] = LittleLong (pinframe->origin[1]);
+	origin[0] = LittleLongUnaligned (pinframe->origin[0]);
+	origin[1] = LittleLongUnaligned (pinframe->origin[1]);
 
 	pspriteframe->up = origin[1];
 	pspriteframe->down = origin[1] - height;
@@ -1734,7 +1741,7 @@
 
 	pingroup = (dspritegroup_t *)pin;
 
-	numframes = LittleLong (pingroup->numframes);
+	numframes = LittleLongUnaligned (pingroup->numframes);
 
 	pspritegroup = Hunk_AllocName (sizeof (mspritegroup_t) +
 				(numframes - 1) * sizeof (pspritegroup->frames[0]), loadname);
@@ -1751,7 +1758,7 @@
 
 	for (i=0 ; i<numframes ; i++)
 	{
-		*poutintervals = LittleFloat (pin_intervals->interval);
+		*poutintervals = LittleFloatUnaligned (pin_intervals->interval);
 		if (*poutintervals <= 0.0)
 			Sys_Error ("Mod_LoadSpriteGroup: interval<=0");
 
@@ -1787,12 +1794,12 @@
 	
 	pin = (dsprite_t *)buffer;
 
-	version = LittleLong (pin->version);
+	version = LittleLongUnaligned (pin->version);
 	if (version != SPRITE_VERSION)
 		Sys_Error ("%s has wrong version number "
 				 "(%i should be %i)", mod->name, version, SPRITE_VERSION);
 
-	numframes = LittleLong (pin->numframes);
+	numframes = LittleLongUnaligned (pin->numframes);
 
 	size = sizeof (msprite_t) +	(numframes - 1) * sizeof (psprite->frames);
 
@@ -1800,11 +1807,11 @@
 
 	mod->cache.data = psprite;
 
-	psprite->type = LittleLong (pin->type);
-	psprite->maxwidth = LittleLong (pin->width);
-	psprite->maxheight = LittleLong (pin->height);
-	psprite->beamlength = LittleFloat (pin->beamlength);
-	mod->synctype = LittleLong (pin->synctype);
+	psprite->type = LittleLongUnaligned (pin->type);
+	psprite->maxwidth = LittleLongUnaligned (pin->width);
+	psprite->maxheight = LittleLongUnaligned (pin->height);
+	psprite->beamlength = LittleFloatUnaligned (pin->beamlength);
+	mod->synctype = LittleLongUnaligned (pin->synctype);
 	psprite->numframes = numframes;
 
 	mod->mins[0] = mod->mins[1] = -psprite->maxwidth/2;
@@ -1827,7 +1834,7 @@
 	{
 		spriteframetype_t	frametype;
 
-		frametype = LittleLong (pframetype->type);
+		frametype = LittleLongUnaligned (pframetype->type);
 		psprite->frames[i].type = frametype;
 
 		if (frametype == SPR_SINGLE)