Commit b1155820 authored by Rob Swindell's avatar Rob Swindell 💬
Browse files

Fix LE/BE_INT() and BYTE_SWAP_INT() for 64-bit long targets (Linux-x64)

long is 64-bits (8 bytes), not 32-bit on some target platforms (e.g. Linux-x64), so don't assume sizeof(long)==4.

It appears we weren't using these "automatic" integer-size macros anywhere that I can see, so no harm, but might as well fix them.

Also remove BYTE_SWAP_SHORT and BYTE_SWAP_LONG since they're terribly named. Retain the LE/BE_SHORT/LONG macros since we're using them currently in SBBS (e.g. js_file.c).
parent 958dac64
/* xpendian.h */
/* Macros to convert integer "endianness" */
/* $Id: xpendian.h,v 1.6 2018/07/24 01:13:10 rswindell Exp $ */
/****************************************************************************
* @format.tab-size 4 (Plain Text/Source Code File Header) *
* @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
......@@ -15,41 +11,27 @@
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* See the GNU Lesser General Public License for more details: lgpl.txt or *
* http://www.fsf.org/copyleft/lesser.html *
* *
* Anonymous FTP access to the most recent released source is available at *
* ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net *
* *
* Anonymous CVS access to the development source and modification history *
* is available at cvs.synchro.net:/cvsroot/sbbs, example: *
* cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login *
* (just hit return, no password is necessary) *
* cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src *
* https://www.gnu.org/licenses/old-licenses/lgpl-2.0.html *
* *
* For Synchronet coding style and modification guidelines, see *
* http://www.synchro.net/source.html *
* *
* You are encouraged to submit any modifications (preferably in Unix diff *
* format) via e-mail to mods@synchro.net *
* *
* Note: If this box doesn't appear square, then you need to fix your tabs. *
****************************************************************************/
#ifndef _XPENDIAN_H
#define _XPENDIAN_H
#include "gen_defs.h"
/************************/
/* byte-swapping macros */
/************************/
#define BYTE_SWAP_16(x) ((((short)(x)&0xff00)>>8) | (((short)(x)&0x00ff)<<8))
#define BYTE_SWAP_32(x) ((((long)(x)&0xff000000)>>24) | (((long)(x)&0x00ff0000)>>8) | (((long)(x)&0x0000ff00)<<8) | (((long)(x)&0x000000ff)<<24))
/* these may need to be updated for > 32-bit platforms */
#define BYTE_SWAP_SHORT(x) BYTE_SWAP_16(x)
#define BYTE_SWAP_LONG(x) BYTE_SWAP_32(x)
#define BYTE_SWAP_16(x) ((((WORD)(x)&0xff00)>>8) | (((WORD)(x)&0x00ff)<<8))
#define BYTE_SWAP_32(x) ((((DWORD)(x)&0xff000000)>>24) | (((DWORD)(x)&0x00ff0000)>>8) | (((DWORD)(x)&0x0000ff00)<<8) | (((DWORD)(x)&0x000000ff)<<24))
/* auto-detect integer size */
#define BYTE_SWAP_INT(x) (sizeof(x)==sizeof(short) ? BYTE_SWAP_SHORT(x) : sizeof(x)==sizeof(long) ? BYTE_SWAP_LONG(x) : (x))
#define BYTE_SWAP_INT(x) (sizeof(x)==2 ? BYTE_SWAP_16(x) : sizeof(x)==4 ? BYTE_SWAP_32(x) : (x))
/********************************/
/* Architecture-specific macros */
......@@ -61,8 +43,8 @@
#define BE_INT16(x) (x)
#define BE_INT32(x) (x)
#define BE_INT(x) (x)
#define LE_SHORT(x) BYTE_SWAP_SHORT(x)
#define LE_LONG(x) BYTE_SWAP_LONG(x)
#define LE_SHORT(x) BYTE_SWAP_16(x)
#define LE_LONG(x) BYTE_SWAP_32(x)
#define LE_INT16(x) BYTE_SWAP_16(x)
#define LE_INT32(x) BYTE_SWAP_32(x)
#define LE_INT(x) BYTE_SWAP_INT(x)
......@@ -74,8 +56,8 @@
#define LE_INT16(x) (x)
#define LE_INT32(x) (x)
#define LE_INT(x) (x)
#define BE_SHORT(x) BYTE_SWAP_SHORT(x)
#define BE_LONG(x) BYTE_SWAP_LONG(x)
#define BE_SHORT(x) BYTE_SWAP_16(x)
#define BE_LONG(x) BYTE_SWAP_32(x)
#define BE_INT16(x) BYTE_SWAP_16(x)
#define BE_INT32(x) BYTE_SWAP_32(x)
#define BE_INT(x) BYTE_SWAP_INT(x)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment