/* qendian.c (description) Copyright (C) 1996-1997 Id Software, Inc. Copyright (C) 1999,2000 contributors of the QuakeForge project Please see the file "AUTHORS" for a list of contributors 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; either version 2 of the License, or (at your option) any later version. 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: Free Software Foundation, Inc. 59 Temple Place - Suite 330 Boston, MA 02111-1307, USA $Id$ */ #ifdef HAVE_CONFIG_H # include "config.h" #endif #include #include "QF/qendian.h" #include "QF/qtypes.h" #include "QF/vfile.h" /* BYTE ORDER FUNCTIONS */ #ifndef WORDS_BIGENDIAN qboolean bigendien = false;; short (*BigShort) (short l) = ShortSwap; short (*LittleShort) (short l) = ShortNoSwap; int (*BigLong) (int l) = LongSwap; int (*LittleLong) (int l) = LongNoSwap; float (*BigFloat) (float l) = FloatSwap; float (*LittleFloat) (float l) = FloatNoSwap; #else qboolean bigendien = true;; short (*BigShort) (short l) = ShortNoSwap; short (*LittleShort) (short l) = ShortSwap; int (*BigLong) (int l) = LongNoSwap; int (*LittleLong) (int l) = LongSwap; float (*BigFloat) (float l) = FloatNoSwap; float (*LittleFloat) (float l) = FloatSwap; #endif short ShortSwap (short l) { byte b1, b2; b1 = l & 255; b2 = (l >> 8) & 255; return (b1 << 8) + b2; } short ShortNoSwap (short l) { return l; } int LongSwap (int l) { byte b1, b2, b3, b4; 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; } int LongNoSwap (int 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; } void WriteFloat (VFile *file, float f) { // a float in C is /defined/ to be 32 bits. byte order, can, of course // still make a mess. union { float f; byte b[4]; } dat; dat.f = LittleFloat (f); Qwrite (file, dat.b, sizeof (dat.b)); } void WriteByte (VFile *file, int b) { byte dat = b & 0xff; Qwrite (file, &dat, 1); } void WriteShort (VFile *file, unsigned int s) { byte dat[2]; dat[0] = s & 0xff; dat[1] = (s >> 8) & 0xff; Qwrite (file, dat, sizeof (dat)); } void WriteLong (VFile *file, unsigned int l) { byte dat[4]; dat[0] = l & 0xff; dat[1] = (l >> 8) & 0xff; dat[2] = (l >> 16) & 0xff; dat[3] = (l >> 24) & 0xff; Qwrite (file, dat, sizeof (dat)); } float ReadFloat (VFile *file) { // a float in C is /defined/ to be 32 bits. byte order, can, of course // still make a mess. union { float f; byte b[4]; } dat; Qread (file, dat.b, sizeof (dat.b)); return LittleFloat (dat.f); } byte ReadByte (VFile *file) { byte dat; Qread (file, &dat, 1); return dat; } unsigned short ReadShort (VFile *file) { byte dat[2]; Qread (file, dat, sizeof (dat)); return (dat[1] << 8) | dat[0]; } unsigned long ReadLong (VFile *file) { byte dat[4]; Qread (file, dat, sizeof (dat)); return (dat[3] << 24) | (dat[2] << 16) | (dat[1] << 8) | dat[0]; }