add BytesX functions

master
an 2019-12-03 14:44:52 -05:00
parent 8fd7e7ba10
commit c9eb79e2c6
9 changed files with 51 additions and 89 deletions

View File

@ -697,9 +697,7 @@ int32_t MSG_ReadShort(void)
return -1;
}
c = (int16_t)(net_message.data[msg_readcount]
+ (net_message.data[msg_readcount + 1] << 8));
c = LittleShort(BytesShort(&net_message.data[msg_readcount]));
msg_readcount += 2;
return c;
@ -715,11 +713,7 @@ int32_t MSG_ReadLong(void)
return -1;
}
c = net_message.data[msg_readcount]
+ (net_message.data[msg_readcount + 1] << 8)
+ (net_message.data[msg_readcount + 2] << 16)
+ (net_message.data[msg_readcount + 3] << 24);
c = LittleLong(BytesLong(&net_message.data[msg_readcount]));
msg_readcount += 4;
return c;

View File

@ -96,63 +96,45 @@ void InsertLinkAfter(link_t *l, link_t *after);
//============================================================================
static inline int16_t ReadBigShort(byte const *bytes)
#define BytesX(typ, name) \
static inline typ Bytes##name(byte const *bytes) \
{ \
typ v; \
memcpy(&v, bytes, sizeof(typ)); \
return v; \
}
#define ReadXX(endian, typ, name) \
static inline typ Read##endian##name(byte const **bytes) \
{ \
typ v = endian##name(Bytes##name(*bytes)); \
*bytes += sizeof(typ); \
return v; \
}
#define Reader(typ, name) \
BytesX(typ, name) \
ReadXX(Big, typ, name) \
ReadXX(Little, typ, name)
Reader(int16_t, Short)
Reader(int32_t, Long)
Reader(float, Float)
static inline void ReadSkip(byte const **bytes, size_t n)
{
int16_t v;
v = bytes[1];
v |= bytes[0] << 8;
return v;
*bytes += n;
}
static inline int32_t ReadBigLong(byte const *bytes)
static inline void ReadCopy(void *out, byte const **bytes, size_t n)
{
int32_t v;
v = bytes[3];
v |= bytes[2] << 8;
v |= bytes[1] << 16;
v |= bytes[0] << 24;
return v;
memcpy(out, *bytes, n);
*bytes += n;
}
static inline float ReadBigFloat(byte const *bytes)
{
union
{
float f;
uint32_t i;
} data;
data.i = ReadBigLong(bytes);
return data.f;
}
static inline int16_t ReadLittleShort(byte const *bytes)
{
int16_t v;
v = bytes[0];
v |= bytes[1] << 8;
return v;
}
static inline int32_t ReadLittleLong(byte const *bytes)
{
int32_t v;
v = bytes[0];
v |= bytes[1] << 8;
v |= bytes[2] << 16;
v |= bytes[3] << 24;
return v;
}
static inline float ReadLittleFloat(byte const *bytes)
{
union
{
float f;
uint32_t i;
} data;
data.i = ReadLittleLong(bytes);
return data.f;
}
#undef BytesX
#undef ReadXX
#undef Reader
//============================================================================

View File

@ -355,7 +355,7 @@ qmodel_t *Mod_LoadModel(qmodel_t *mod, bool crash)
// call the apropriate loader
mod->needload = false;
mod_type = (buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24));
mod_type = LittleLong(BytesLong(buf));
switch(mod_type)
{
case IDPOLYHEADER:

View File

@ -113,28 +113,17 @@ typedef struct targaheader_s
#define TARGAHEADERSIZE 18 //size on disk
targaheader_t targa_header;
static targaheader_t targa_header;
int32_t fgetLittleShort(FILE *f)
static int32_t fgetLittleShort(FILE *f)
{
byte b1, b2;
size_t i;
byte b[2];
b1 = fgetc(f);
b2 = fgetc(f);
for(i = 0; i < sizeof(b); i++)
b[i] = fgetc(f);
return (int16_t)(b1 + b2 * 256);
}
int32_t fgetLittleLong(FILE *f)
{
byte b1, b2, b3, b4;
b1 = fgetc(f);
b2 = fgetc(f);
b3 = fgetc(f);
b4 = fgetc(f);
return b1 + (b2 << 8) + (b3 << 16) + (b4 << 24);
return LittleShort(BytesShort(b));
}
/*

View File

@ -139,7 +139,7 @@ int32_t Loop_GetMessage(qsocket_t *sock)
return 0;
ret = sock->receiveMessage[0];
length = sock->receiveMessage[1] + (sock->receiveMessage[2] << 8);
length = LittleShort(BytesShort(&sock->receiveMessage[1]));
// alignment byte skipped here
SZ_Clear(&net_message);
SZ_Write(&net_message, &sock->receiveMessage[4], length);

View File

@ -35,8 +35,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define DEF_SAVEGLOBAL (1 << 15)
#define MAX_PARMS 8
#define PROG_VERSION 6
typedef int32_t func_t;
@ -159,7 +157,7 @@ typedef struct
int32_t s_file; // source file defined in
int32_t numparms;
byte parm_size[MAX_PARMS];
byte parm_size[8];
} dfunction_t;
typedef struct

View File

@ -246,8 +246,7 @@ static void VID_Gamma_f(cvar_t *var)
for(i = 0; i < 256; i++)
{
vid_gamma_red[i] =
CLAMP(0, (int32_t)((255 * pow((i + 0.5) / 255.5, vid_gamma.value) + 0.5) * vid_contrast.value), 255) << 8;
vid_gamma_red[i] = CLAMP(0, (int32_t)((255 * pow((i + 0.5) / 255.5, vid_gamma.value) + 0.5) * vid_contrast.value), 255) << 8;
vid_gamma_green[i] = vid_gamma_red[i];
vid_gamma_blue[i] = vid_gamma_red[i];
}

View File

@ -100,8 +100,8 @@ static inline bool tag_is_apetag(const uint8_t *data, size_t length)
if(length < 32) return false;
if(memcmp(data, "APETAGEX", 8) != 0)
return false;
v = (data[11] << 24) | (data[10] << 16) | (data[9] << 8) | data[8];
if(v != 2000U/* && v != 1000U*/)
v = LittleLong(BytesLong(&data[8]));
if(v != 2000)
return false;
v = 0;
if(memcmp(&data[24], &v, 4) != 0 || memcmp(&data[28], &v, 4) != 0)
@ -129,7 +129,7 @@ static size_t mp3_tagsize(const uint8_t *data, size_t length)
if(tag_is_apetag(data, length))
{
size = (data[15] << 24) | (data[14] << 16) | (data[13] << 8) | data[12];
size = LittleLong(BytesLong(&data[12]));
size += 32;
return size;
}

View File

@ -153,7 +153,7 @@ _retry:
if(type == UMUSIC_MP2)
{
uint8_t *p = (uint8_t *)sig;
uint16_t u = ((p[0] << 8) | p[1]) & 0xFFFE;
uint16_t u = BigShort(BytesShort(p)) & 0xFFFE;
if(u == 0xFFFC || u == 0xFFF4)
return UMUSIC_MP2;
return -1;
@ -304,7 +304,7 @@ static int32_t probe_header(void *header)
swp = (uint32_t *) header;
for(i = 0; i < UPKG_HDR_SIZE / 4; i++, p += 4)
{
swp[i] = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
swp[i] = LittleLong(BytesLong(p));
}
hdr = (struct upkg_hdr *) header;