Index: src/OFColor.m ================================================================== --- src/OFColor.m +++ src/OFColor.m @@ -84,32 +84,29 @@ } - (uint32_t)hash { uint32_t hash; - union { - float f; - unsigned char b[sizeof(float)]; - } f; + float tmp; OF_HASH_INIT(hash); - f.f = OF_BSWAP_FLOAT_IF_LE(_red); - for (uint_fast8_t i = 0; i < sizeof(float); i++) - OF_HASH_ADD(hash, f.b[i]); - - f.f = OF_BSWAP_FLOAT_IF_LE(_green); - for (uint_fast8_t i = 0; i < sizeof(float); i++) - OF_HASH_ADD(hash, f.b[i]); - - f.f = OF_BSWAP_FLOAT_IF_LE(_blue); - for (uint_fast8_t i = 0; i < sizeof(float); i++) - OF_HASH_ADD(hash, f.b[i]); - - f.f = OF_BSWAP_FLOAT_IF_LE(_alpha); - for (uint_fast8_t i = 0; i < sizeof(float); i++) - OF_HASH_ADD(hash, f.b[i]); + tmp = OF_BSWAP_FLOAT_IF_LE(_red); + for (uint_fast8_t i = 0; i < sizeof(float); i++) + OF_HASH_ADD(hash, ((char *)&tmp)[i]); + + tmp = OF_BSWAP_FLOAT_IF_LE(_green); + for (uint_fast8_t i = 0; i < sizeof(float); i++) + OF_HASH_ADD(hash, ((char *)&tmp)[i]); + + tmp = OF_BSWAP_FLOAT_IF_LE(_blue); + for (uint_fast8_t i = 0; i < sizeof(float); i++) + OF_HASH_ADD(hash, ((char *)&tmp)[i]); + + tmp = OF_BSWAP_FLOAT_IF_LE(_alpha); + for (uint_fast8_t i = 0; i < sizeof(float); i++) + OF_HASH_ADD(hash, ((char *)&tmp)[i]); OF_HASH_FINALIZE(hash); return hash; } Index: src/OFData+MessagePackValue.m ================================================================== --- src/OFData+MessagePackValue.m +++ src/OFData+MessagePackValue.m @@ -278,35 +278,28 @@ *object = [OFNumber numberWithInt64: readUInt64(buffer + 1)]; return 9; /* Floating point */ case 0xCA:; /* float 32 */ - union { - unsigned char u8[4]; - float f; - } f; + float f; if (length < 5) @throw [OFTruncatedDataException exception]; - memcpy(&f.u8, buffer + 1, 4); + memcpy(&f, buffer + 1, 4); - *object = [OFNumber numberWithFloat: OF_BSWAP_FLOAT_IF_LE(f.f)]; + *object = [OFNumber numberWithFloat: OF_BSWAP_FLOAT_IF_LE(f)]; return 5; case 0xCB:; /* float 64 */ - union { - unsigned char u8[8]; - double d; - } d; + double d; if (length < 9) @throw [OFTruncatedDataException exception]; - memcpy(&d.u8, buffer + 1, 8); + memcpy(&d, buffer + 1, 8); - *object = [OFNumber numberWithDouble: - OF_BSWAP_DOUBLE_IF_LE(d.d)]; + *object = [OFNumber numberWithDouble: OF_BSWAP_DOUBLE_IF_LE(d)]; return 9; /* nil */ case 0xC0: *object = [OFNull null]; return 1; Index: src/OFDate.m ================================================================== --- src/OFDate.m +++ src/OFDate.m @@ -338,22 +338,17 @@ { self = [super init]; @try { void *pool = objc_autoreleasePoolPush(); - union { - double d; - uint64_t u; - } d; if (![element.name isEqual: self.className] || ![element.namespace isEqual: OF_SERIALIZATION_NS]) @throw [OFInvalidArgumentException exception]; - d.u = (uint64_t)element.hexadecimalValue; - d.u = OF_BSWAP64_IF_LE(d.u); - _seconds = OF_BSWAP_DOUBLE_IF_LE(d.d); + _seconds = OF_BSWAP_DOUBLE_IF_LE(OF_INT_TO_DOUBLE_RAW( + OF_BSWAP64_IF_LE(element.hexadecimalValue))); objc_autoreleasePoolPop(pool); } @catch (id e) { [self release]; @throw e; @@ -381,21 +376,18 @@ } - (uint32_t)hash { uint32_t hash; - union { - double d; - uint8_t b[sizeof(double)]; - } d; - - d.d = OF_BSWAP_DOUBLE_IF_BE(_seconds); + double tmp; OF_HASH_INIT(hash); + + tmp = OF_BSWAP_DOUBLE_IF_BE(_seconds); for (size_t i = 0; i < sizeof(double); i++) - OF_HASH_ADD(hash, d.b[i]); + OF_HASH_ADD(hash, ((char *)&tmp)[i]); OF_HASH_FINALIZE(hash); return hash; } @@ -429,21 +421,17 @@ - (OFXMLElement *)XMLElementBySerializing { void *pool = objc_autoreleasePoolPush(); OFXMLElement *element; - union { - double d; - uint64_t u; - } d; element = [OFXMLElement elementWithName: self.className namespace: OF_SERIALIZATION_NS]; - d.d = OF_BSWAP_DOUBLE_IF_LE(_seconds); - element.stringValue = - [OFString stringWithFormat: @"%016" PRIx64, OF_BSWAP64_IF_LE(d.u)]; + element.stringValue = [OFString stringWithFormat: @"%016" PRIx64, + OF_BSWAP64_IF_LE(OF_DOUBLE_TO_INT_RAW(OF_BSWAP_DOUBLE_IF_LE( + _seconds)))]; [element retain]; objc_autoreleasePoolPop(pool); Index: src/OFLocale.m ================================================================== --- src/OFLocale.m +++ src/OFLocale.m @@ -453,21 +453,18 @@ initWithCString: buffer encoding: _encoding]; if ((locale = OpenLocale(NULL)) != NULL) { @try { - union { - uint32_t u32; - char c[4]; - } territory; + uint32_t territory; size_t length; - territory.u32 = + territory = OF_BSWAP32_IF_LE(locale->loc_CountryCode); for (length = 0; length < 4; length++) - if (territory.c[length] == 0) + if (((char *)territory)[length] == 0) break; _territory = [[OFString alloc] initWithCString: territory.c encoding: _encoding Index: src/OFMD5Hash.h ================================================================== --- src/OFMD5Hash.h +++ src/OFMD5Hash.h @@ -32,11 +32,11 @@ OFSecureData *_iVarsData; struct of_md5_hash_ivars { uint32_t state[4]; uint64_t bits; union of_md5_hash_buffer { - uint8_t bytes[64]; + unsigned char bytes[64]; uint32_t words[16]; } buffer; size_t bufferLength; } *_iVars; bool _allowsSwappableMemory; Index: src/OFNumber.m ================================================================== --- src/OFNumber.m +++ src/OFNumber.m @@ -556,31 +556,19 @@ _value.uIntMax = element.decimalValue; } else if ([typeString isEqual: @"signed"]) { _type = OF_NUMBER_TYPE_INTMAX; _value.intMax = element.decimalValue; } else if ([typeString isEqual: @"float"]) { - union { - float f; - uint32_t u; - } f; - - f.u = OF_BSWAP32_IF_LE( - (uint32_t)element.hexadecimalValue); - _type = OF_NUMBER_TYPE_FLOAT; - _value.float_ = OF_BSWAP_FLOAT_IF_LE(f.f); + _value.float_ = OF_BSWAP_FLOAT_IF_LE( + OF_INT_TO_FLOAT_RAW(OF_BSWAP32_IF_LE( + (uint32_t)element.hexadecimalValue))); } else if ([typeString isEqual: @"double"]) { - union { - double d; - uint64_t u; - } d; - - d.u = OF_BSWAP64_IF_LE( - (uint64_t)element.hexadecimalValue); - _type = OF_NUMBER_TYPE_DOUBLE; - _value.double_ = OF_BSWAP_DOUBLE_IF_LE(d.d); + _value.double_ = OF_BSWAP_DOUBLE_IF_LE( + OF_INT_TO_DOUBLE_RAW(OF_BSWAP64_IF_LE( + (uint64_t)element.hexadecimalValue))); } else @throw [OFInvalidArgumentException exception]; objc_autoreleasePoolPop(pool); } @catch (id e) { @@ -1073,22 +1061,19 @@ } OF_HASH_INIT(hash); if (type & OF_NUMBER_TYPE_FLOAT) { - union { - double d; - uint8_t b[sizeof(double)]; - } d; + double d; if (isnan(self.doubleValue)) return 0; - d.d = OF_BSWAP_DOUBLE_IF_BE(self.doubleValue); + d = OF_BSWAP_DOUBLE_IF_BE(self.doubleValue); for (uint_fast8_t i = 0; i < sizeof(double); i++) - OF_HASH_ADD(hash, d.b[i]); + OF_HASH_ADD(hash, ((char *)&d)[i]); } else if (type & OF_NUMBER_TYPE_SIGNED) { intmax_t v = self.intMaxValue * -1; while (v != 0) { OF_HASH_ADD(hash, v & 0xFF); @@ -1216,42 +1201,29 @@ case OF_NUMBER_TYPE_INT32: case OF_NUMBER_TYPE_INT64: case OF_NUMBER_TYPE_SSIZE: case OF_NUMBER_TYPE_INTMAX: case OF_NUMBER_TYPE_PTRDIFF: - case OF_NUMBER_TYPE_INTPTR:; + case OF_NUMBER_TYPE_INTPTR: [element addAttributeWithName: @"type" stringValue: @"signed"]; break; - case OF_NUMBER_TYPE_FLOAT:; - union { - float f; - uint32_t u; - } f; - - f.f = OF_BSWAP_FLOAT_IF_LE(_value.float_); - + case OF_NUMBER_TYPE_FLOAT: [element addAttributeWithName: @"type" stringValue: @"float"]; - element.stringValue = - [OFString stringWithFormat: @"%08" PRIx32, - OF_BSWAP32_IF_LE(f.u)]; + element.stringValue = [OFString stringWithFormat: @"%08" PRIx32, + OF_BSWAP32_IF_LE(OF_FLOAT_TO_INT_RAW(OF_BSWAP_FLOAT_IF_LE( + _value.float_)))]; break; - case OF_NUMBER_TYPE_DOUBLE:; - union { - double d; - uint64_t u; - } d; - - d.d = OF_BSWAP_DOUBLE_IF_LE(_value.double_); - + case OF_NUMBER_TYPE_DOUBLE: [element addAttributeWithName: @"type" stringValue: @"double"]; - element.stringValue = - [OFString stringWithFormat: @"%016" PRIx64, - OF_BSWAP64_IF_LE(d.u)]; + element.stringValue = [OFString + stringWithFormat: @"%016" PRIx64, + OF_BSWAP64_IF_LE(OF_DOUBLE_TO_INT_RAW(OF_BSWAP_DOUBLE_IF_LE( + _value.double_)))]; break; default: @throw [OFInvalidFormatException exception]; } Index: src/OFRIPEMD160Hash.h ================================================================== --- src/OFRIPEMD160Hash.h +++ src/OFRIPEMD160Hash.h @@ -32,11 +32,11 @@ OFSecureData *_iVarsData; struct of_ripemd160_hash_ivars { uint32_t state[5]; uint64_t bits; union of_ripemd160_hash_buffer { - uint8_t bytes[64]; + unsigned char bytes[64]; uint32_t words[16]; } buffer; size_t bufferLength; } *_iVars; bool _allowsSwappableMemory; Index: src/OFSHA1Hash.h ================================================================== --- src/OFSHA1Hash.h +++ src/OFSHA1Hash.h @@ -32,11 +32,11 @@ OFSecureData *_iVarsData; struct of_sha1_hash_ivars { uint32_t state[5]; uint64_t bits; union of_sha1_hash_buffer { - uint8_t bytes[64]; + unsigned char bytes[64]; uint32_t words[80]; } buffer; size_t bufferLength; } *_iVars; bool _allowsSwappableMemory; Index: src/OFSHA224Or256Hash.h ================================================================== --- src/OFSHA224Or256Hash.h +++ src/OFSHA224Or256Hash.h @@ -33,11 +33,11 @@ @protected struct of_sha224_or_256_hash_ivars { uint32_t state[8]; uint64_t bits; union of_sha224_or_256_hash_buffer { - uint8_t bytes[64]; + unsigned char bytes[64]; uint32_t words[64]; } buffer; size_t bufferLength; } *_iVars; @private Index: src/OFSHA384Or512Hash.h ================================================================== --- src/OFSHA384Or512Hash.h +++ src/OFSHA384Or512Hash.h @@ -33,11 +33,11 @@ @protected struct of_sha384_or_512_hash_ivars { uint64_t state[8]; uint64_t bits[2]; union of_sha384_or_512_hash_buffer { - uint8_t bytes[128]; + unsigned char bytes[128]; uint64_t words[80]; } buffer; size_t bufferLength; } *_iVars; @private Index: src/OFTarArchive.m ================================================================== --- src/OFTarArchive.m +++ src/OFTarArchive.m @@ -98,26 +98,23 @@ _mode = OF_TAR_ARCHIVE_MODE_APPEND; else @throw [OFInvalidArgumentException exception]; if (_mode == OF_TAR_ARCHIVE_MODE_APPEND) { - union { - char c[1024]; - uint32_t u32[1024 / sizeof(uint32_t)]; - } buffer; + uint32_t buffer[1024 / sizeof(uint32_t)]; bool empty = true; if (![_stream isKindOfClass: [OFSeekableStream class]]) @throw [OFInvalidArgumentException exception]; [(OFSeekableStream *)_stream seekToOffset: -1024 whence: SEEK_END]; - [_stream readIntoBuffer: buffer.c + [_stream readIntoBuffer: buffer exactLength: 1024]; for (size_t i = 0; i < 1024 / sizeof(uint32_t); i++) - if (buffer.u32[i] != 0) + if (buffer[i] != 0) empty = false; if (!empty) @throw [OFInvalidFormatException exception]; @@ -166,14 +163,11 @@ } - (OFTarArchiveEntry *)nextEntry { OFTarArchiveEntry *entry; - union { - unsigned char c[512]; - uint32_t u32[512 / sizeof(uint32_t)]; - } buffer; + uint32_t buffer[512 / sizeof(uint32_t)]; bool empty = true; if (_mode != OF_TAR_ARCHIVE_MODE_READ) @throw [OFInvalidArgumentException exception]; @@ -187,30 +181,30 @@ _lastReturnedStream = nil; if (_stream.atEndOfStream) return nil; - [_stream readIntoBuffer: buffer.c + [_stream readIntoBuffer: buffer exactLength: 512]; for (size_t i = 0; i < 512 / sizeof(uint32_t); i++) - if (buffer.u32[i] != 0) + if (buffer[i] != 0) empty = false; if (empty) { - [_stream readIntoBuffer: buffer.c + [_stream readIntoBuffer: buffer exactLength: 512]; for (size_t i = 0; i < 512 / sizeof(uint32_t); i++) - if (buffer.u32[i] != 0) + if (buffer[i] != 0) @throw [OFInvalidFormatException exception]; return nil; } entry = [[[OFTarArchiveEntry alloc] - of_initWithHeader: buffer.c + of_initWithHeader: (unsigned char *)buffer encoding: _encoding] autorelease]; _lastReturnedStream = [[OFTarArchiveFileReadStream alloc] of_initWithStream: _stream entry: entry]; Index: src/macros.h ================================================================== --- src/macros.h +++ src/macros.h @@ -550,37 +550,53 @@ #else # define OF_BSWAP16(i) OF_BSWAP16_CONST(i) # define OF_BSWAP32(i) OF_BSWAP32_CONST(i) # define OF_BSWAP64(i) OF_BSWAP64_CONST(i) #endif + +static OF_INLINE uint32_t +OF_FLOAT_TO_INT_RAW(float f) +{ + uint32_t ret; + memcpy(&ret, &f, 4); + return ret; +} + +static OF_INLINE float +OF_INT_TO_FLOAT_RAW(uint32_t u32) +{ + float ret; + memcpy(&ret, &u32, 4); + return ret; +} + +static OF_INLINE uint64_t +OF_DOUBLE_TO_INT_RAW(double d) +{ + uint64_t ret; + memcpy(&ret, &d, 8); + return ret; +} + +static OF_INLINE double +OF_INT_TO_DOUBLE_RAW(uint64_t u64) +{ + double ret; + memcpy(&ret, &u64, 8); + return ret; +} static OF_INLINE float OF_CONST_FUNC OF_BSWAP_FLOAT(float f) { - union { - float f; - uint32_t i; - } u; - - u.f = f; - u.i = OF_BSWAP32(u.i); - - return u.f; + return OF_INT_TO_FLOAT_RAW(OF_BSWAP32(OF_FLOAT_TO_INT_RAW(f))); } static OF_INLINE double OF_CONST_FUNC OF_BSWAP_DOUBLE(double d) { - union { - double d; - uint64_t i; - } u; - - u.d = d; - u.i = OF_BSWAP64(u.i); - - return u.d; + return OF_INT_TO_DOUBLE_RAW(OF_BSWAP64(OF_DOUBLE_TO_INT_RAW(d))); } #ifdef OF_BIG_ENDIAN # define OF_BSWAP16_IF_BE(i) OF_BSWAP16(i) # define OF_BSWAP32_IF_BE(i) OF_BSWAP32(i)