From 5eb6ddc45b9d00f2594c12ca90b57ed93a9e542c Mon Sep 17 00:00:00 2001 From: Alexey Khromov Date: Thu, 1 May 2025 18:10:15 +0300 Subject: [PATCH] Endianness check --- source/bforce/nodelist.c | 9 ++++++--- source/bforce/u_misc.c | 33 +++++++++++++++++++++------------ source/include/nodelist.h | 3 ++- source/include/util.h | 2 ++ 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/source/bforce/nodelist.c b/source/bforce/nodelist.c index 582c7ea..9d6a87e 100644 --- a/source/bforce/nodelist.c +++ b/source/bforce/nodelist.c @@ -582,7 +582,7 @@ s_nodelist *nodelist_open(const char *dir, char *name, int mode) return NULL; } } - + DEB((D_NODELIST, "nodelist_open: endianness = %x", (uint32_t) tmp.endian)); return xmemcpy(&tmp, sizeof(s_nodelist)); } @@ -630,6 +630,7 @@ int nodelist_checkheader(s_nodelist *nlp) (long)nlstat.st_size, (long)nlsize); return -1; } + nlp->endian = buffer_getlong(buffer + 12); return 0; } @@ -660,7 +661,7 @@ int nodelist_createheader(s_nodelist *nlp) buffer_putlong(hdrbuf + 0, nlstat.st_mtime); buffer_putlong(hdrbuf + 4, nlstat.st_size); - + buffer_putlong(hdrbuf + 12, (long) 0xDEADBEEF); if( fseek(nlp->fp_index, 0L, SEEK_SET) == -1 ) { logerr("cannot seek to zero offset of index"); @@ -754,7 +755,9 @@ int nodelist_findindex(s_nodelist *nlp, s_bni *bni, s_faddr addr) bni->point = buffer_getint(p + 6); bni->hub = buffer_getint(p + 8); bni->offset = buffer_getlong(p + 10); - + if ((uint32_t)nlp->endian == 0xBEEFDEAD) { + swap_long((char *)&bni->offset); + } DEB((D_NODELIST, "nodelist: findindex found" )); return 0; } diff --git a/source/bforce/u_misc.c b/source/bforce/u_misc.c index b6f2fec..8a84027 100644 --- a/source/bforce/u_misc.c +++ b/source/bforce/u_misc.c @@ -253,10 +253,10 @@ int checkmasks(const char *masks, const char *str) */ char *buffer_putlong(char *buffer, long val) { - buffer[0] = ( ((unsigned long) val) ) & 0xff; - buffer[1] = ( ((unsigned long) val) >> 8 ) & 0xff; - buffer[2] = ( ((unsigned long) val) >> 16 ) & 0xff; - buffer[3] = ( ((unsigned long) val) >> 24 ) & 0xff; + buffer[0] = ( ((uint_least32_t) val) ) & 0xff; + buffer[1] = ( ((uint_least32_t) val) >> 8 ) & 0xff; + buffer[2] = ( ((uint_least32_t) val) >> 16 ) & 0xff; + buffer[3] = ( ((uint_least32_t) val) >> 24 ) & 0xff; return buffer; } @@ -273,8 +273,8 @@ char *buffer_putlong(char *buffer, long val) */ char *buffer_putint(char *buffer, int val) { - buffer[0] = ( ((unsigned int) val) ) & 0xff; - buffer[1] = ( ((unsigned int) val) >> 8 ) & 0xff; + buffer[0] = ( ((uint_least16_t) val) ) & 0xff; + buffer[1] = ( ((uint_least16_t) val) >> 8 ) & 0xff; return buffer; } @@ -290,10 +290,10 @@ char *buffer_putint(char *buffer, int val) */ long buffer_getlong(const char *buffer) { - return ( (unsigned long) ((unsigned char) buffer[0]) ) - | ( (unsigned long) ((unsigned char) buffer[1]) << 8 ) - | ( (unsigned long) ((unsigned char) buffer[2]) << 16 ) - | ( (unsigned long) ((unsigned char) buffer[3]) << 24 ); + return (long)( (uint_least32_t) ((unsigned char) buffer[0]) ) + | ( (uint_least32_t) ((unsigned char) buffer[1]) << 8 ) + | ( (uint_least32_t) ((unsigned char) buffer[2]) << 16 ) + | ( (uint_least32_t) ((unsigned char) buffer[3]) << 24 ); } /***************************************************************************** @@ -307,8 +307,17 @@ long buffer_getlong(const char *buffer) */ int buffer_getint(const char *buffer) { - return ( (unsigned int) ((unsigned char) buffer[0]) ) - | ( (unsigned int) ((unsigned char) buffer[1]) << 8 ); + return (int)( (uint_least16_t) ((unsigned char) buffer[0]) ) + | ( (uint_least16_t) ((unsigned char) buffer[1]) << 8 ); +} + +void swap_long(char *buffer){ + char *temp = xmemcpy(buffer, sizeof(uint32_t)); + temp[0] = buffer[2]; + temp[1] = buffer[3]; + temp[2] = buffer[0]; + temp[3] = buffer[1]; + memcpy(buffer,temp,sizeof(uint32_t)); } void printf_usage(const char *ident, const char *fmt, ...) diff --git a/source/include/nodelist.h b/source/include/nodelist.h index 2fcef0a..6d0bc1a 100644 --- a/source/include/nodelist.h +++ b/source/include/nodelist.h @@ -27,7 +27,7 @@ * 0x0000 4 bytes Nodelist file date * 0x0004 4 bytes Nodelist file size * 0x0008 4 bytes Total number of entries in index file - * 0x000c 4 bytes Unused + * 0x000c 4 bytes ENDIANNES MARK * * Nodelist index is an array of next entries: * @@ -123,6 +123,7 @@ typedef struct nodelist char name_index[BF_MAXPATH+1]; char name_nodelist[BF_MAXPATH+1]; long entries; + long endian; } s_nodelist; diff --git a/source/include/util.h b/source/include/util.h index cdeefd6..8c19b06 100644 --- a/source/include/util.h +++ b/source/include/util.h @@ -189,8 +189,10 @@ char *buffer_putlong(char *buffer, long val); char *buffer_putint(char *buf, int val); long buffer_getlong(const char *buf); int buffer_getint(const char *buf); +void swap_long(char *buffer); void printf_usage(const char *ident, const char *fmt, ...); + /* u_pkt.c */ int pkt_createpacket(const char *pktname, const s_packet *pkt);