From d46086fbc39e59081cbb7f9ff5730c8215194122 Mon Sep 17 00:00:00 2001 From: Richard Frith-MacDonald Date: Mon, 8 Feb 1999 10:46:32 +0000 Subject: [PATCH] Improved byte swapping stuff for 64-bit clean operation. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@3671 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 10 ++ Headers/gnustep/base/NSByteOrder.h | 202 +++++++++++++++++++++++++++++ Source/NSData.m | 132 +++++-------------- Source/NSUnarchiver.m | 9 +- Source/UnixFileHandle.m | 19 ++- 5 files changed, 258 insertions(+), 114 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2c5a5d7ef..3ac4bcbf5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Mon Feb 8 10:05:00 1999 Richard Frith-Macdonald + + * Source/include/NSByteOrder.h: Implemented a full set of byte-swapping + inline functions for integers with known sizes. + * Source/NSData.m: Corrected serialization/deserialization routines to + do byte-swapping using new functions. + * Source/UnixFileHandle.m: Use new byte-swapping functions rather than + htonl(), htons(), ntohl(), ntohs(). + * Source/NSUnarchiver.m: Use new byte-swapping functions. + 1999-02-04 Adam Fedor * config/config.printf.c: New file. diff --git a/Headers/gnustep/base/NSByteOrder.h b/Headers/gnustep/base/NSByteOrder.h index 35092d23f..bfc3d6e77 100644 --- a/Headers/gnustep/base/NSByteOrder.h +++ b/Headers/gnustep/base/NSByteOrder.h @@ -57,6 +57,42 @@ GSSwapI64(gsu64 in) __attribute__((unused)); static inline gsu128 GSSwapI128(gsu128 in) __attribute__((unused)); +static inline gsu16 +GSSwapBigI16ToHost(gsu16 in) __attribute__((unused)); +static inline gsu32 +GSSwapBigI32ToHost(gsu32 in) __attribute__((unused)); +static inline gsu64 +GSSwapBigI64ToHost(gsu64 in) __attribute__((unused)); +static inline gsu128 +GSSwapBigI128ToHost(gsu128 in) __attribute__((unused)); + +static inline gsu16 +GSSwapHostI16ToBig(gsu16 in) __attribute__((unused)); +static inline gsu32 +GSSwapHostI32ToBig(gsu32 in) __attribute__((unused)); +static inline gsu64 +GSSwapHostI64ToBig(gsu64 in) __attribute__((unused)); +static inline gsu128 +GSSwapHostI128ToBig(gsu128 in) __attribute__((unused)); + +static inline gsu16 +GSSwapLittleI16ToHost(gsu16 in) __attribute__((unused)); +static inline gsu32 +GSSwapLittleI32ToHost(gsu32 in) __attribute__((unused)); +static inline gsu64 +GSSwapLittleI64ToHost(gsu64 in) __attribute__((unused)); +static inline gsu128 +GSSwapLittleI128ToHost(gsu128 in) __attribute__((unused)); + +static inline gsu16 +GSSwapHostI16ToLittle(gsu16 in) __attribute__((unused)); +static inline gsu32 +GSSwapHostI32ToLittle(gsu32 in) __attribute__((unused)); +static inline gsu64 +GSSwapHostI64ToLittle(gsu64 in) __attribute__((unused)); +static inline gsu128 +GSSwapHostI128ToLittle(gsu128 in) __attribute__((unused)); + static inline gsu16 GSSwapI16(gsu16 in) @@ -395,6 +431,27 @@ NSHostByteOrder(void) /* * Swap Big endian to host */ +static inline gsu16 +GSSwapBigI16ToHost(gsu16 in) +{ + return in; +} +static inline gsu32 +GSSwapBigI32ToHost(gsu32 in) +{ + return in; +} +static inline gsu64 +GSSwapBigI64ToHost(gsu64 in) +{ + return in; +} +static inline gsu128 +GSSwapBigI128ToHost(gsu128 in) +{ + return in; +} + static inline double NSSwapBigDoubleToHost(NSSwappedDouble num) { @@ -434,6 +491,27 @@ NSSwapBigShortToHost(unsigned short num) /* * Swap Host to Big endian */ +static inline gsu16 +GSSwapHostI16ToBig(gsu16 in) +{ + return in; +} +static inline gsu32 +GSSwapHostI32ToBig(gsu32 in) +{ + return in; +} +static inline gsu64 +GSSwapHostI64ToBig(gsu64 in) +{ + return in; +} +static inline gsu128 +GSSwapHostI128ToBig(gsu128 in) +{ + return in; +} + static inline NSSwappedDouble NSSwapHostDoubleToBig(double num) { @@ -473,6 +551,27 @@ NSSwapHostShortToBig(unsigned short num) /* * Swap Little endian to Host */ +static inline gsu16 +GSSwapLittleI16ToHost(gsu16 in) +{ + return GSSwapI16(in); +} +static inline gsu32 +GSSwapLittleI32ToHost(gsu32 in) +{ + return GSSwapI32(in); +} +static inline gsu64 +GSSwapLittleI64ToHost(gsu64 in) +{ + return GSSwapI64(in); +} +static inline gsu128 +GSSwapLittleI128ToHost(gsu128 in) +{ + return GSSwapI128(in); +} + static inline double NSSwapLittleDoubleToHost(NSSwappedDouble num) { @@ -512,6 +611,27 @@ NSSwapLittleShortToHost(unsigned short num) /* * Swap Host to Little endian */ +static inline gsu16 +GSSwapHostI16ToLittle(gsu16 in) +{ + return GSSwapI16(in); +} +static inline gsu32 +GSSwapHostI32ToLittle(gsu32 in) +{ + return GSSwapI32(in); +} +static inline gsu64 +GSSwapHostI64ToLittle(gsu64 in) +{ + return GSSwapI64(in); +} +static inline gsu128 +GSSwapHostI128ToLittle(gsu128 in) +{ + return GSSwapI128(in); +} + static inline NSSwappedDouble NSSwapHostDoubleToLittle(double num) { @@ -561,6 +681,26 @@ NSHostByteOrder(void) /* * Swap Big endian to host */ +static inline gsu16 +GSSwapBigI16ToHost(gsu16 in) +{ + return GSSwapI16(in); +} +static inline gsu32 +GSSwapBigI32ToHost(gsu32 in) +{ + return GSSwapI32(in); +} +static inline gsu64 +GSSwapBigI64ToHost(gsu64 in) +{ + return GSSwapI64(in); +} +static inline gsu128 +GSSwapBigI128ToHost(gsu128 in) +{ + return GSSwapI128(in); +} static inline double NSSwapBigDoubleToHost(NSSwappedDouble num) { @@ -600,6 +740,26 @@ NSSwapBigShortToHost(unsigned short num) /* * Swap Host to Big endian */ +static inline gsu16 +GSSwapHostI16ToBig(gsu16 in) +{ + return GSSwapI16(in); +} +static inline gsu32 +GSSwapHostI32ToBig(gsu32 in) +{ + return GSSwapI32(in); +} +static inline gsu64 +GSSwapHostI64ToBig(gsu64 in) +{ + return GSSwapI64(in); +} +static inline gsu128 +GSSwapHostI128ToBig(gsu128 in) +{ + return GSSwapI128(in); +} static inline NSSwappedDouble NSSwapHostDoubleToBig(double num) { @@ -639,6 +799,27 @@ NSSwapHostShortToBig(unsigned short num) /* * Swap Little endian to Host */ +static inline gsu16 +GSSwapLittleI16ToHost(gsu16 in) +{ + return in; +} +static inline gsu32 +GSSwapLittleI32ToHost(gsu32 in) +{ + return in; +} +static inline gsu64 +GSSwapLittleI64ToHost(gsu64 in) +{ + return in; +} +static inline gsu128 +GSSwapLittleI128ToHost(gsu128 in) +{ + return in; +} + static inline double NSSwapLittleDoubleToHost(NSSwappedDouble num) { @@ -678,6 +859,27 @@ NSSwapLittleShortToHost(unsigned short num) /* * Swap Host to Little endian */ +static inline gsu16 +GSSwapHostI16ToLittle(gsu16 in) +{ + return in; +} +static inline gsu32 +GSSwapHostI32ToLittle(gsu32 in) +{ + return in; +} +static inline gsu64 +GSSwapHostI64ToLittle(gsu64 in) +{ + return in; +} +static inline gsu128 +GSSwapHostI128ToLittle(gsu128 in) +{ + return in; +} + static inline NSSwappedDouble NSSwapHostDoubleToLittle(double num) { diff --git a/Source/NSData.m b/Source/NSData.m index 6a217b9e8..649387c07 100644 --- a/Source/NSData.m +++ b/Source/NSData.m @@ -640,9 +640,7 @@ failure: [self deserializeBytes: &length length: sizeof(length) atCursor: cursor]; -#ifndef GS_WORDS_BIGENDIAN - length = GSSwapI32(length); -#endif + length = GSSwapBigI32ToHost(length); if (length == -1) { *(const char**)data = NULL; @@ -813,9 +811,7 @@ failure: [self deserializeBytes: &ni length: sizeof(ni) atCursor: cursor]; -#ifndef GS_WORDS_BIGENDIAN - ni = GSSwapI16(ni); -#endif + ni = GSSwapBigI16ToHost(ni); if (ni == 0) { *(Class*)data = 0; @@ -847,15 +843,11 @@ failure: [self deserializeBytes: &ln length: sizeof(ln) atCursor: cursor]; -#ifndef GS_WORDS_BIGENDIAN - ln = GSSwapI16(ln); -#endif + ln = GSSwapBigI16ToHost(ln); [self deserializeBytes: < length: sizeof(lt) atCursor: cursor]; -#ifndef GS_WORDS_BIGENDIAN - lt = GSSwapI16(lt); -#endif + lt = GSSwapBigI16ToHost(lt); if (ln == 0) { *(SEL*)data = 0; @@ -1268,24 +1260,18 @@ failure: case _C_CHARPTR: { - unsigned len; - gss32 ni; + gsu32 len; + gsu32 ni; if (!*(void**)data) { - ni = -1; -#ifndef GS_WORDS_BIGENDIAN - ni = GSSwapI32(ni); -#endif + ni = (gsu32)-1; + ni = GSSwapHostI32ToBig(ni); [self appendBytes: (void*)&ni length: sizeof(ni)]; return; } - len = strlen(*(void**)data); -#ifndef GS_WORDS_BIGENDIAN - ni = GSSwapI32(len); -#else - ni = len; -#endif + len = (gsu32)strlen(*(void**)data); + ni = GSSwapHostI32ToBig(len); [self appendBytes: (void*)&ni length: sizeof(ni)]; [self appendBytes: *(void**)data length: len]; return; @@ -1396,11 +1382,7 @@ failure: gsu16 ln = (gsu16)strlen(name); gsu16 ni; -#ifndef GS_WORDS_BIGENDIAN - ni = GSSwapI16(ln); -#else - ni = ln; -#endif + ni = GSSwapHostI16ToBig(ln); [self appendBytes: &ni length: sizeof(ni)]; if (ln) { @@ -1416,17 +1398,9 @@ failure: gsu16 lt = (gsu16)strlen(types); gsu16 ni; -#ifndef GS_WORDS_BIGENDIAN - ni = GSSwapI16(ln); -#else - ni = ln; -#endif + ni = GSSwapHostI16ToBig(ln); [self appendBytes: &ni length: sizeof(ni)]; -#ifndef GS_WORDS_BIGENDIAN - ni = GSSwapI16(lt); -#else - ni = lt; -#endif + ni = GSSwapHostI16ToBig(lt); [self appendBytes: &ni length: sizeof(ni)]; if (ln) { @@ -1444,13 +1418,13 @@ failure: } } -- (void)serializeInt: (int)value +- (void) serializeInt: (int)value { unsigned ni = NSSwapHostIntToBig(value); [self appendBytes: &ni length: sizeof(unsigned)]; } -- (void)serializeInt: (int)value atIndex: (unsigned)index +- (void) serializeInt: (int)value atIndex: (unsigned)index { unsigned ni = NSSwapHostIntToBig(value); NSRange range = { index, sizeof(int) }; @@ -1705,10 +1679,10 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos) *pos += len; } -- (void)deserializeDataAt: (void*)data - ofObjCType: (const char*)type - atCursor: (unsigned*)cursor - context: (id )callback +- (void) deserializeDataAt: (void*)data + ofObjCType: (const char*)type + atCursor: (unsigned*)cursor + context: (id )callback { if (data == 0 || type == 0) { @@ -1740,9 +1714,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos) [self deserializeBytes: &len length: sizeof(len) atCursor: cursor]; -#ifndef GS_WORDS_BIGENDIAN - len = GSSwapI32(len); -#endif + len = GSSwapBigI32ToHost(len); if (len == -1) { *(const char**)data = NULL; @@ -1893,9 +1865,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos) gsu16 ni; getBytes((void*)&ni, bytes, sizeof(ni), length, cursor); -#ifndef GS_WORDS_BIGENDIAN - ni = GSSwapI16(ni); -#endif + ni = GSSwapBigI16ToHost(ni); if (ni == 0) { *(Class*)data = 0; @@ -1923,13 +1893,9 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos) gsu16 lt; getBytes((void*)&ln, bytes, sizeof(ln), length, cursor); -#ifndef GS_WORDS_BIGENDIAN - ln = GSSwapI16(ln); -#endif + ln = GSSwapBigI16ToHost(ln); getBytes((void*)<, bytes, sizeof(lt), length, cursor); -#ifndef GS_WORDS_BIGENDIAN - lt = GSSwapI16(lt); -#endif + lt = GSSwapBigI16ToHost(lt); if (ln == 0) { *(SEL*)data = 0; @@ -2010,11 +1976,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos) } x = *(gsu16*)(bytes + *cursor); *cursor += 2; -#if GS_WORDS_BIGENDIAN - *ref = (unsigned int)x; -#else - *ref = (unsigned int)GSSwapI16(x); -#endif + *ref = (unsigned int)GSSwapBigI16ToHost(x); return; } default: @@ -2029,11 +1991,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos) } x = *(gsu32*)(bytes + *cursor); *cursor += 4; -#if GS_WORDS_BIGENDIAN - *ref = (unsigned int)x; -#else - *ref = (unsigned int)GSSwapI32(x); -#endif + *ref = (unsigned int)GSSwapBigI32ToHost(x); return; } } @@ -2662,18 +2620,12 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos) if (!*(void**)data) { ni = -1; -#ifndef GS_WORDS_BIGENDIAN - ni = GSSwapI32(ni); -#endif + ni = GSSwapHostI32ToBig(ni); [self appendBytes: (void*)&len length: sizeof(len)]; return; } len = strlen(*(void**)data); -#ifndef GS_WORDS_BIGENDIAN - ni = GSSwapI32(len); -#else - ni = len; -#endif + ni = GSSwapHostI32ToBig(len); minimum = length + len + sizeof(ni); if (minimum > capacity) { @@ -2812,11 +2764,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos) { [self _grow: minimum]; } -#ifndef GS_WORDS_BIGENDIAN - ni = GSSwapI16(ln); -#else - ni = ln; -#endif + ni = GSSwapHostI16ToBig(ln); memcpy(bytes+length, &ni, sizeof(ni)); length += sizeof(ni); if (ln) @@ -2839,18 +2787,10 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos) { [self _grow: minimum]; } -#ifndef GS_WORDS_BIGENDIAN - ni = GSSwapI16(ln); -#else - ni = ln; -#endif + ni = GSSwapHostI16ToBig(ln); memcpy(bytes+length, &ni, sizeof(ni)); length += sizeof(ni); -#ifndef GS_WORDS_BIGENDIAN - ni = GSSwapI16(lt); -#else - ni = lt; -#endif + ni = GSSwapHostI16ToBig(lt); memcpy(bytes+length, &ni, sizeof(ni)); length += sizeof(ni); if (ln) @@ -2903,11 +2843,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos) [self _grow: length + 3]; } *(gsu8*)(bytes + length++) = tag; -#if GS_WORDS_BIGENDIAN - *(gsu16*)(bytes + length) = (gsu16)x; -#else - *(gsu16*)(bytes + length) = GSSwapI16(x); -#endif + *(gsu16*)(bytes + length) = GSSwapHostI16ToBig(x); length += 2; } else @@ -2920,11 +2856,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos) [self _grow: length + 5]; } *(gsu8*)(bytes + length++) = tag; -#if GS_WORDS_BIGENDIAN - *(gsu32*)(bytes + length) = (gsu32)x; -#else - *(gsu32*)(bytes + length) = GSSwapI32(x); -#endif + *(gsu32*)(bytes + length) = GSSwapHostI32ToBig(x); length += 4; } } diff --git a/Source/NSUnarchiver.m b/Source/NSUnarchiver.m index b56637a5e..c84db93f7 100644 --- a/Source/NSUnarchiver.m +++ b/Source/NSUnarchiver.m @@ -884,10 +884,7 @@ mapClassName(NSUnarchiverObjectInfo *info) #if GS_HAVE_I64 bigval = val; #else -#if GS_WORDS_BIGENDIAN == 0 - val = GSSwapI64(val); -#endif - bigval = *(gsu32*)&val; + val = GSSwapBigI64ToHost(val); #endif break; } @@ -900,9 +897,7 @@ mapClassName(NSUnarchiverObjectInfo *info) #if GS_HAVE_I128 bigval = val; #else -#if GS_WORDS_BIGENDIAN == 0 - val = GSSwapI128(val); -#endif + val = GSSwapBigI128ToHost(val); #if GS_HAVE_I64 bigval = *(gsu64*)&val; #else diff --git a/Source/UnixFileHandle.m b/Source/UnixFileHandle.m index 38b8804dd..88822b87b 100644 --- a/Source/UnixFileHandle.m +++ b/Source/UnixFileHandle.m @@ -34,6 +34,7 @@ #include #include #include +#include #ifdef WIN32 #include @@ -116,7 +117,7 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin) #endif } else - sin->sin_addr.s_addr = htonl(INADDR_ANY); + sin->sin_addr.s_addr = GSSwapHostI32ToBig(INADDR_ANY); if (svc == nil) { @@ -133,9 +134,9 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin) if (*ptr == '\0' && val <= 0xffff) { - short v = val; + gsu16 v = val; - sin->sin_port = htons(v); + sin->sin_port = GSSwapHostI16ToBig(v); return YES; } else @@ -237,7 +238,8 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin) if (connect(net, (struct sockaddr*)&sin, sizeof(sin)) < 0) { NSLog(@"unable to make connection to %s:%d - %s", - inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), strerror(errno)); + inet_ntoa(sin.sin_addr), + GSSwapBigI16ToHost(sin.sin_port), strerror(errno)); [self release]; return nil; } @@ -290,7 +292,8 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin) if (errno != EINPROGRESS) { NSLog(@"unable to make connection to %s:%d - %s", - inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), strerror(errno)); + inet_ntoa(sin.sin_addr), + GSSwapBigI16ToHost(sin.sin_port), strerror(errno)); [self release]; return nil; } @@ -339,7 +342,8 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin) if (bind(net, (struct sockaddr *)&sin, sizeof(sin)) < 0) { NSLog(@"unable to bind to port %s:%d - %s", - inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), strerror(errno)); + inet_ntoa(sin.sin_addr), + GSSwapBigI16ToHost(sin.sin_port), strerror(errno)); (void) close(net); [self release]; return nil; @@ -1330,7 +1334,8 @@ getAddr(NSString* name, NSString* svc, NSString* pcl, struct sockaddr_in *sin) { address = [NSString stringWithCString: (char*)inet_ntoa(sin->sin_addr)]; [address retain]; - service = [NSString stringWithFormat: @"%d", (int)ntohs(sin->sin_port)]; + service = [NSString stringWithFormat: @"%d", + (int)GSSwapBigI16ToHost(sin->sin_port)]; [service retain]; protocol = @"tcp"; }