diff --git a/ChangeLog b/ChangeLog index fb3d598d7..3fc2baabd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -14,6 +14,8 @@ * Source/UdpPort.m: Likewise. * Tools/gdomap.c: Likewise. + * Source/NSBinaryCStream: Use NSByteOrder functions. + * Source/UdpPort.m: Likewise. * Source/Invocation.m (initWithTarget:selector:): Use proper cast. * Source/NSFileManager.m (-isExecutableFileAtPath): Typo. * Source/NSPage.m (getpagesize): New function for WIN32 diff --git a/Source/BinaryCStream.m b/Source/BinaryCStream.m index b0534d5b8..f4d746459 100644 --- a/Source/BinaryCStream.m +++ b/Source/BinaryCStream.m @@ -29,16 +29,11 @@ #include #include #include +#include #include -#ifdef __WIN32__ -#include -#else /* __WIN32__ */ #if HAVE_VALUES_H #include // This gets BITSPERBYTE on Solaris #endif -#include -#include // for byte-conversion -#endif /* !__WIN32__ */ #define PRE_SIZEOF_PREFIX_FORMAT_VERSION 0 #define CURRENT_FORMAT_VERSION ((((GNUSTEP_BASE_MAJOR_VERSION * 100) + \ @@ -315,7 +310,7 @@ static int debug_binary_coder = 0; { unsigned length = strlen (*(char**)d); unsigned nlength; - nlength = htonl (length); + nlength = NSSwapHostIntToBig (length); [stream writeBytes: &nlength length: NUM_BYTES_STRING_LENGTH]; [stream writeBytes: *(char**)d @@ -331,24 +326,24 @@ static int debug_binary_coder = 0; /* Reading and writing signed scalar types. */ case _C_SHT: - WRITE_SIGNED_TYPE (d, short, htons); + WRITE_SIGNED_TYPE (d, short, NSSwapHostShortToBig); break; case _C_USHT: - WRITE_UNSIGNED_TYPE (d, short, htons); + WRITE_UNSIGNED_TYPE (d, short, NSSwapHostShortToBig); break; case _C_INT: - WRITE_SIGNED_TYPE (d, int, htonl); + WRITE_SIGNED_TYPE (d, int, NSSwapHostIntToBig); break; case _C_UINT: - WRITE_UNSIGNED_TYPE (d, int, htonl); + WRITE_UNSIGNED_TYPE (d, int, NSSwapHostIntToBig); break; case _C_LNG: - WRITE_SIGNED_TYPE (d, long, htonl); + WRITE_SIGNED_TYPE (d, long, NSSwapHostLongToBig); break; case _C_ULNG: - WRITE_UNSIGNED_TYPE (d, long, htonl); + WRITE_UNSIGNED_TYPE (d, long, NSSwapHostLongToBig); break; /* xxx The handling of floats and doubles could be improved. @@ -373,8 +368,8 @@ static int debug_binary_coder = 0; NSAssert (value - mantissa == 0, @"mantissa and value should be the same"); /* Encode the value as its two integer components. */ - WRITE_SIGNED_TYPE (&exponent_encoded, short, htons); - WRITE_SIGNED_TYPE (&mantissa, int, htonl); + WRITE_SIGNED_TYPE (&exponent_encoded, short, NSSwapHostShortToBig); + WRITE_SIGNED_TYPE (&mantissa, int, NSSwapHostIntToBig); break; } @@ -398,9 +393,9 @@ static int debug_binary_coder = 0; NSAssert (value - mantissa2 == 0, @"mantissa2 and value should be the same"); /* Encode the value as its three integer components. */ - WRITE_SIGNED_TYPE (&exponent_encoded, short, htons); - WRITE_SIGNED_TYPE (&mantissa1, int, htonl); - WRITE_SIGNED_TYPE (&mantissa2, int, htonl); + WRITE_SIGNED_TYPE (&exponent_encoded, short, NSSwapHostShortToBig); + WRITE_SIGNED_TYPE (&mantissa1, int, NSSwapHostIntToBig); + WRITE_SIGNED_TYPE (&mantissa2, int, NSSwapHostIntToBig); break; } @@ -485,7 +480,7 @@ static int debug_binary_coder = 0; NSAssert2 (read_count == NUM_BYTES_STRING_LENGTH, @"expected %d bytes of input, got %d", NUM_BYTES_STRING_LENGTH,read_count); - length = ntohl (length); + length = NSSwapBigIntToHost (length); OBJC_MALLOC (*(char**)d, char, length+1); read_count = [stream readBytes: *(char**)d length: length]; @@ -504,24 +499,24 @@ static int debug_binary_coder = 0; break; case _C_SHT: - READ_SIGNED_TYPE (d, short, ntohs); + READ_SIGNED_TYPE (d, short, NSSwapBigShortToHost); break; case _C_USHT: - READ_UNSIGNED_TYPE (d, short, ntohs); + READ_UNSIGNED_TYPE (d, short, NSSwapBigShortToHost); break; case _C_INT: - READ_SIGNED_TYPE (d, int, ntohl); + READ_SIGNED_TYPE (d, int, NSSwapBigIntToHost); break; case _C_UINT: - READ_UNSIGNED_TYPE (d, int, ntohl); + READ_UNSIGNED_TYPE (d, int, NSSwapBigIntToHost); break; case _C_LNG: - READ_SIGNED_TYPE (d, long, ntohl); + READ_SIGNED_TYPE (d, long, NSSwapBigLongToHost); break; case _C_ULNG: - READ_UNSIGNED_TYPE (d, long, ntohl); + READ_UNSIGNED_TYPE (d, long, NSSwapBigLongToHost); break; case _C_FLT: @@ -532,8 +527,8 @@ static int debug_binary_coder = 0; float fvalue; /* Decode the exponent and mantissa. */ - READ_SIGNED_TYPE (&exponent, short, ntohs); - READ_SIGNED_TYPE (&mantissa, int, ntohl); + READ_SIGNED_TYPE (&exponent, short, NSSwapBigShortToHost); + READ_SIGNED_TYPE (&mantissa, int, NSSwapBigIntToHost); /* Assemble them into a double */ value = mantissa / FLOAT_FACTOR; value = ldexp (value, exponent); @@ -550,9 +545,9 @@ static int debug_binary_coder = 0; double value; /* Decode the exponent and the two pieces of the mantissa. */ - READ_SIGNED_TYPE (&exponent, short, ntohs); - READ_SIGNED_TYPE (&mantissa1, int, ntohl); - READ_SIGNED_TYPE (&mantissa2, int, ntohl); + READ_SIGNED_TYPE (&exponent, short, NSSwapBigShortToHost); + READ_SIGNED_TYPE (&mantissa1, int, NSSwapBigIntToHost); + READ_SIGNED_TYPE (&mantissa2, int, NSSwapBigIntToHost); /* Assemble them into a double */ value = ((mantissa2 / FLOAT_FACTOR) + mantissa1) / FLOAT_FACTOR; value = ldexp (value, exponent); diff --git a/Source/UdpPort.m b/Source/UdpPort.m index b38fe4dde..afa308369 100644 --- a/Source/UdpPort.m +++ b/Source/UdpPort.m @@ -31,6 +31,7 @@ #include #include #include +#include #if _AIX #include @@ -141,7 +142,7 @@ static NSMapTable *port_number_2_in_port = NULL; our unique host address that can identify us across the network. */ memcpy (&(p->_address.sin_addr), hp->h_addr, hp->h_length); p->_address.sin_family = AF_INET; - p->_address.sin_port = htons (n); + p->_address.sin_port = NSSwapHostShortToBig (n); /* N may be zero, in which case bind() will choose a port number for us. */ if (bind (p->_port_socket, @@ -177,7 +178,7 @@ static NSMapTable *port_number_2_in_port = NULL; if (udp_port_debug) fprintf(stderr, "created new UdpInPort 0x%x, fd=%d port_number=%d\n", - (unsigned)p, p->_port_socket, htons(p->_address.sin_port)); + (unsigned)p, p->_port_socket, NSSwapHostShortToBig(p->_address.sin_port)); return p; } @@ -261,6 +262,11 @@ static NSMapTable *port_number_2_in_port = NULL; { if (_is_valid) { +#if defined(__WIN32__) + closesocket (_port_socket); +#else + close (_port_socket); +#endif /* __WIN32__ */ close (_port_socket); [super invalidate]; } @@ -279,7 +285,7 @@ static NSMapTable *port_number_2_in_port = NULL; - (int) portNumber { - return (int) ntohs (_address.sin_port); + return (int) NSSwapBigShortToHost (_address.sin_port); } - (Class) packetClass @@ -401,7 +407,7 @@ static Array *udp_out_port_array; /* Get the sockaddr_in address. */ memcpy (&addr.sin_addr, hp->h_addr, hp->h_length); addr.sin_family = AF_INET; - addr.sin_port = htons (n); + addr.sin_port = NSSwapHostShortToBig (n); return [self newForSendingToSockaddr: &addr]; } @@ -419,7 +425,7 @@ static Array *udp_out_port_array; if ( ! [reply_port isKindOfClass: [UdpInPort class]]) [self error:"Trying to send to a port that is not a UdpInPort"]; if (udp_port_debug) - fprintf (stderr, "sending to %d\n", (int) ntohs (_address.sin_port)); + fprintf (stderr, "sending to %d\n", (int) NSSwapBigShortToHost (_address.sin_port)); if (sendto ([reply_port socket], [packet streamBuffer], len, 0, (struct sockaddr*)&_address, sizeof (_address)) @@ -433,7 +439,7 @@ static Array *udp_out_port_array; - (int) portNumber { - return (int) ntohs (_address.sin_port); + return (int) NSSwapBigShortToHost (_address.sin_port); } - (NSString*) hostname