From c9eb79e2c69c3accceab052b7718f6010a0ecff7 Mon Sep 17 00:00:00 2001 From: Alison Watson Date: Tue, 3 Dec 2019 14:44:52 -0500 Subject: [PATCH] add BytesX functions --- source/common.c | 10 +---- source/common.h | 84 +++++++++++++++++------------------------- source/gl_model.c | 2 +- source/image.c | 25 ++++--------- source/net_loop.c | 2 +- source/pr_comp.h | 4 +- source/sdl/gl_vidsdl.c | 3 +- source/snd_mp3.c | 6 +-- source/snd_umx.c | 4 +- 9 files changed, 51 insertions(+), 89 deletions(-) diff --git a/source/common.c b/source/common.c index 4bc2db2..19f0f1b 100644 --- a/source/common.c +++ b/source/common.c @@ -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; diff --git a/source/common.h b/source/common.h index aada6f1..d94f8ed 100644 --- a/source/common.h +++ b/source/common.h @@ -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 //============================================================================ diff --git a/source/gl_model.c b/source/gl_model.c index 20f2ecf..1160be9 100644 --- a/source/gl_model.c +++ b/source/gl_model.c @@ -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: diff --git a/source/image.c b/source/image.c index bebe510..5d99f32 100644 --- a/source/image.c +++ b/source/image.c @@ -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)); } /* diff --git a/source/net_loop.c b/source/net_loop.c index 556c30e..3174525 100644 --- a/source/net_loop.c +++ b/source/net_loop.c @@ -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); diff --git a/source/pr_comp.h b/source/pr_comp.h index c467d9c..7cd8fff 100644 --- a/source/pr_comp.h +++ b/source/pr_comp.h @@ -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 diff --git a/source/sdl/gl_vidsdl.c b/source/sdl/gl_vidsdl.c index 1188648..b3c7798 100644 --- a/source/sdl/gl_vidsdl.c +++ b/source/sdl/gl_vidsdl.c @@ -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]; } diff --git a/source/snd_mp3.c b/source/snd_mp3.c index 2181c6e..3ca52cf 100644 --- a/source/snd_mp3.c +++ b/source/snd_mp3.c @@ -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; } diff --git a/source/snd_umx.c b/source/snd_umx.c index 7ae8d1c..afcded9 100644 --- a/source/snd_umx.c +++ b/source/snd_umx.c @@ -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;