Index: src/OFObject.h ================================================================== --- src/OFObject.h +++ src/OFObject.h @@ -56,10 +56,18 @@ size_t start; /// The length of the range size_t length; } of_range_t; +/** + * \brief An enum for storing endianess. + */ +typedef enum of_endianess_t { + OF_ENDIANESS_BIG_ENDIAN, + OF_ENDIANESS_LITTLE_ENDIAN +} of_endianess_t; + @class OFString; /** * \brief The protocol which all root classes implement. */ Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -123,10 +123,21 @@ * \param string The unicode string * \return A new autoreleased OFString */ + stringWithUnicodeString: (of_unichar_t*)string; +/** + * Creates a new OFString from a unicode string, assuming the specified byte + * order if no BOM is found. + * + * \param string The unicode string + * \param byteOrder The byte order to assume if there is no BOM + * \return A new autoreleased OFString + */ ++ stringWithUnicodeString: (of_unichar_t*)string + byteOrder: (of_endianess_t)byteOrder; + /** * Creates a new OFString from a unicode string with the specified length. * * \param string The unicode string * \param length The length of the unicode string @@ -133,27 +144,64 @@ * \return A new autoreleased OFString */ + stringWithUnicodeString: (of_unichar_t*)string length: (size_t)length; +/** + * Creates a new OFString from a unicode string with the specified length, + * assuming the specified byte order if no BOM is found. + * + * \param string The unicode string + * \param byteOrder The byte order to assume if there is no BOM + * \param length The length of the unicode string + * \return A new autoreleased OFString + */ ++ stringWithUnicodeString: (of_unichar_t*)string + byteOrder: (of_endianess_t)byteOrder + length: (size_t)length; + /** * Creates a new OFString from a UTF-16 encoded string. * * \param string The UTF-16 string * \return A new autoreleased OFString */ + stringWithUTF16String: (uint16_t*)string; +/** + * Creates a new OFString from a UTF-16 encoded string, assuming the specified + * byte order if no BOM is found. + * + * \param string The UTF-16 string + * \param byteOrder The byte order to assume if there is no BOM + * \return A new autoreleased OFString + */ ++ stringWithUTF16String: (uint16_t*)string + byteOrder: (of_endianess_t)byteOrder; + +/** + * Creates a new OFString from a UTF-16 encoded string with the specified + * length. + * + * \param string The UTF-16 string + * \param length The length of the unicode string + * \return A new autoreleased OFString + */ ++ stringWithUTF16String: (uint16_t*)string + length: (size_t)length; + /** * Creates a new OFString from a UTF-16 encoded string with the specified - * length. + * length, assuming the specified byte order if no BOM is found. * * \param string The UTF-16 string + * \param byteOrder The byte order to assume if there is no BOM * \param length The length of the unicode string * \return A new autoreleased OFString */ + stringWithUTF16String: (uint16_t*)string + byteOrder: (of_endianess_t)byteOrder length: (size_t)length; /** * Creates a new OFString from a format string. * See printf for the format syntax. @@ -272,10 +320,21 @@ * \param string The unicode string * \return An initialized OFString */ - initWithUnicodeString: (of_unichar_t*)string; +/** + * Initializes an already allocated OFString with a unicode string, assuming the + * specified byte order if no BOM is found. + * + * \param string The unicode string + * \param byteOrder The byte order to assume if there is no BOM + * \return An initialized OFString + */ +- initWithUnicodeString: (of_unichar_t*)string + byteOrder: (of_endianess_t)byteOrder; + /** * Initializes an already allocated OFString with a unicode string with the * specified length. * * \param string The unicode string @@ -283,18 +342,42 @@ * \return An initialized OFString */ - initWithUnicodeString: (of_unichar_t*)string length: (size_t)length; +/** + * Initializes an already allocated OFString with a unicode string with the + * specified length, assuming the specified byte order if no BOM is found. + * + * \param string The unicode string + * \param byteOrder The byte order to assume if there is no BOM + * \param length The length of the unicode string + * \return An initialized OFString + */ +- initWithUnicodeString: (of_unichar_t*)string + byteOrder: (of_endianess_t)byteOrder + length: (size_t)length; + /** * Initializes an already allocated OFString with a UTF-16 string. * * \param string The UTF-16 string * \return An initialized OFString */ - initWithUTF16String: (uint16_t*)string; +/** + * Initializes an already allocated OFString with a UTF-16 string, assuming the + * specified byte order if no BOM is found. + * + * \param string The UTF-16 string + * \param byteOrder The byte order to assume if there is no BOM + * \return An initialized OFString + */ +- initWithUTF16String: (uint16_t*)string + byteOrder: (of_endianess_t)byteOrder; + /** * Initializes an already allocated OFString with a UTF-16 string with the * specified length. * * \param string The UTF-16 string @@ -302,10 +385,23 @@ * \return An initialized OFString */ - initWithUTF16String: (uint16_t*)string length: (size_t)length; +/** + * Initializes an already allocated OFString with a UTF-16 string with the + * specified length, assuming the specified byte order if no BOM is found. + * + * \param string The UTF-16 string + * \param byteOrder The byte order to assume if there is no BOM + * \param length The length of the UTF-16 string + * \return An initialized OFString + */ +- initWithUTF16String: (uint16_t*)string + byteOrder: (of_endianess_t)byteOrder + length: (size_t)length; + /** * Initializes an already allocated OFString with a format string. * See printf for the format syntax. * * \param format A string used as format to initialize the OFString Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -310,25 +310,57 @@ { return [[[self alloc] initWithUnicodeString: string] autorelease]; } + stringWithUnicodeString: (of_unichar_t*)string + byteOrder: (of_endianess_t)byteOrder +{ + return [[[self alloc] initWithUnicodeString: string + byteOrder: byteOrder] autorelease]; +} + ++ stringWithUnicodeString: (of_unichar_t*)string + length: (size_t)length +{ + return [[[self alloc] initWithUnicodeString: string + length: length] autorelease]; +} + ++ stringWithUnicodeString: (of_unichar_t*)string + byteOrder: (of_endianess_t)byteOrder length: (size_t)length { return [[[self alloc] initWithUnicodeString: string + byteOrder: byteOrder length: length] autorelease]; } + stringWithUTF16String: (uint16_t*)string { return [[[self alloc] initWithUTF16String: string] autorelease]; } + stringWithUTF16String: (uint16_t*)string + byteOrder: (of_endianess_t)byteOrder +{ + return [[[self alloc] initWithUTF16String: string + byteOrder: byteOrder] autorelease]; +} + ++ stringWithUTF16String: (uint16_t*)string + length: (size_t)length +{ + return [[[self alloc] initWithUTF16String: string + length: length] autorelease]; +} + ++ stringWithUTF16String: (uint16_t*)string + byteOrder: (of_endianess_t)byteOrder length: (size_t)length { return [[[self alloc] initWithUTF16String: string + byteOrder: byteOrder length: length] autorelease]; } + stringWithFormat: (OFString*)format, ... { @@ -573,33 +605,50 @@ } - initWithUnicodeString: (of_unichar_t*)string_ { return [self initWithUnicodeString: string_ + byteOrder: OF_ENDIANESS_NATIVE + length: of_unicode_string_length(string_)]; +} + +- initWithUnicodeString: (of_unichar_t*)string_ + byteOrder: (of_endianess_t)byteOrder +{ + return [self initWithUnicodeString: string_ + byteOrder: byteOrder length: of_unicode_string_length(string_)]; } - initWithUnicodeString: (of_unichar_t*)string_ length: (size_t)length_ +{ + return [self initWithUnicodeString: string_ + byteOrder: OF_ENDIANESS_NATIVE + length: length_]; +} + +- initWithUnicodeString: (of_unichar_t*)string_ + byteOrder: (of_endianess_t)byteOrder + length: (size_t)length_ { self = [super init]; @try { char buffer[4]; size_t i, j = 0; BOOL swap = NO; - if (*string_ == 0xFEFF) { + if (length_ > 0 && *string_ == 0xFEFF) { string_++; length_--; - } - - if (*string_ == 0xFFFE0000) { + } else if (length_ > 0 && *string_ == 0xFFFE0000) { swap = YES; string_++; length_--; - } + } else if (byteOrder != OF_ENDIANESS_NATIVE) + swap = YES; length = length_; string = [self allocMemoryWithSize: (length * 4) + 1]; for (i = 0; i < length_; i++) { @@ -659,33 +708,50 @@ } - initWithUTF16String: (uint16_t*)string_ { return [self initWithUTF16String: string_ + byteOrder: OF_ENDIANESS_NATIVE + length: of_utf16_string_length(string_)]; +} + +- initWithUTF16String: (uint16_t*)string_ + byteOrder: (of_endianess_t)byteOrder +{ + return [self initWithUTF16String: string_ + byteOrder: byteOrder length: of_utf16_string_length(string_)]; } - initWithUTF16String: (uint16_t*)string_ length: (size_t)length_ +{ + return [self initWithUTF16String: string_ + byteOrder: OF_ENDIANESS_NATIVE + length: length_]; +} + +- initWithUTF16String: (uint16_t*)string_ + byteOrder: (of_endianess_t)byteOrder + length: (size_t)length_ { self = [super init]; @try { char buffer[4]; size_t i, j = 0; BOOL swap = NO; - if (*string_ == 0xFEFF) { + if (length_ > 0 && *string_ == 0xFEFF) { string_++; length_--; - } - - if (*string_ == 0xFFFE) { + } else if (length_ > 0 && *string_ == 0xFFFE) { swap = YES; string_++; length_--; - } + } else if (byteOrder != OF_ENDIANESS_NATIVE) + swap = YES; length = length_; string = [self allocMemoryWithSize: (length * 4) + 1]; for (i = 0; i < length_; i++) { Index: src/macros.h ================================================================== --- src/macros.h +++ src/macros.h @@ -48,10 +48,13 @@ # error __BIG_ENDIAN__ and __LITTLE_ENDIAN__ defined! # endif # undef OF_BIG_ENDIAN # if __BIG_ENDIAN__ # define OF_BIG_ENDIAN +# define OF_ENDIANESS_NATIVE OF_ENDIANESS_BIG_ENDIAN +# else +# define OF_ENDIANESS_NATIVE OF_ENDIANESS_LITTLE_ENDIAN # endif #endif #ifdef __GNUC__ # if defined(__amd64__) || defined(__x86_64__)