@@ -18,10 +18,11 @@ #include #include #include #import "OFData.h" +#import "OFBase64.h" #import "OFDictionary.h" #ifdef OF_HAVE_FILES # import "OFFile.h" # import "OFFileManager.h" #endif @@ -38,29 +39,24 @@ #import "OFOutOfMemoryException.h" #import "OFOutOfRangeException.h" #import "OFTruncatedDataException.h" #import "OFUnsupportedProtocolException.h" -#import "base64.h" - /* References for static linking */ void _references_to_categories_of_OFData(void) { - _OFData_ASN1DERParsing_reference = 1; - _OFData_CryptoHashing_reference = 1; + _OFData_CryptographicHashing_reference = 1; _OFData_MessagePackParsing_reference = 1; } @implementation OFData @synthesize itemSize = _itemSize; -+ (instancetype)dataWithItems: (const void *)items - count: (size_t)count ++ (instancetype)dataWithItems: (const void *)items count: (size_t)count { - return [[[self alloc] initWithItems: items - count: count] autorelease]; + return [[[self alloc] initWithItems: items count: count] autorelease]; } + (instancetype)dataWithItems: (const void *)items count: (size_t)count itemSize: (size_t)itemSize @@ -111,16 +107,13 @@ + (instancetype)dataWithBase64EncodedString: (OFString *)string { return [[[self alloc] initWithBase64EncodedString: string] autorelease]; } -- (instancetype)initWithItems: (const void *)items - count: (size_t)count +- (instancetype)initWithItems: (const void *)items count: (size_t)count { - return [self initWithItems: items - count: count - itemSize: 1]; + return [self initWithItems: items count: count itemSize: 1]; } - (instancetype)initWithItems: (const void *)items count: (size_t)count itemSize: (size_t)itemSize @@ -129,11 +122,11 @@ @try { if (itemSize == 0) @throw [OFInvalidArgumentException exception]; - _items = of_alloc(count, itemSize); + _items = OFAllocMemory(count, itemSize); _count = count; _itemSize = itemSize; _freeWhenDone = true; memcpy(_items, items, count * itemSize); @@ -193,21 +186,19 @@ # if ULLONG_MAX > SIZE_MAX if (size > SIZE_MAX) @throw [OFOutOfRangeException exception]; # endif - buffer = of_alloc((size_t)size, 1); - file = [[OFFile alloc] initWithPath: path - mode: @"r"]; + buffer = OFAllocMemory((size_t)size, 1); + file = [[OFFile alloc] initWithPath: path mode: @"r"]; @try { - [file readIntoBuffer: buffer - exactLength: (size_t)size]; + [file readIntoBuffer: buffer exactLength: (size_t)size]; } @finally { [file release]; } } @catch (id e) { - free(buffer); + OFFreeMemory(buffer); [self release]; @throw e; } @@ -214,11 +205,11 @@ @try { self = [self initWithItemsNoCopy: buffer count: (size_t)size freeWhenDone: true]; } @catch (id e) { - free(buffer); + OFFreeMemory(buffer); @throw e; } return self; } @@ -237,19 +228,18 @@ if ((URLHandler = [OFURLHandler handlerForURL: URL]) == nil) @throw [OFUnsupportedProtocolException exceptionWithURL: URL]; - stream = [URLHandler openItemAtURL: URL - mode: @"r"]; + stream = [URLHandler openItemAtURL: URL mode: @"r"]; _count = 0; _itemSize = 1; _freeWhenDone = true; pageSize = [OFSystemInfo pageSize]; - buffer = of_alloc(1, pageSize); + buffer = OFAllocMemory(1, pageSize); @try { while (!stream.atEndOfStream) { size_t length = [stream readIntoBuffer: buffer @@ -257,16 +247,17 @@ if (SIZE_MAX - _count < length) @throw [OFOutOfRangeException exception]; - _items = of_realloc(_items, _count + length, 1); + _items = OFResizeMemory(_items, + _count + length, 1); memcpy(_items + _count, buffer, length); _count += length; } } @finally { - free(buffer); + OFFreeMemory(buffer); } objc_autoreleasePoolPop(pool); } @catch (id e) { [self release]; @@ -280,25 +271,24 @@ { self = [super init]; @try { size_t count = [string - cStringLengthWithEncoding: OF_STRING_ENCODING_ASCII]; + cStringLengthWithEncoding: OFStringEncodingASCII]; const char *cString; if (count % 2 != 0) @throw [OFInvalidFormatException exception]; count /= 2; - _items = of_alloc(count, 1); + _items = OFAllocMemory(count, 1); _count = count; _itemSize = 1; _freeWhenDone = true; - cString = [string - cStringWithEncoding: OF_STRING_ENCODING_ASCII]; + cString = [string cStringWithEncoding: OFStringEncodingASCII]; for (size_t i = 0; i < count; i++) { uint8_t c1 = cString[2 * i]; uint8_t c2 = cString[2 * i + 1]; uint8_t byte; @@ -341,14 +331,13 @@ } self = [(OFMutableData *)self initWithCapacity: string.length / 3]; @try { - if (!of_base64_decode((OFMutableData *)self, - [string cStringWithEncoding: OF_STRING_ENCODING_ASCII], - [string cStringLengthWithEncoding: - OF_STRING_ENCODING_ASCII])) + if (!OFBase64Decode((OFMutableData *)self, + [string cStringWithEncoding: OFStringEncodingASCII], + [string cStringLengthWithEncoding: OFStringEncodingASCII])) @throw [OFInvalidFormatException exception]; } @catch (id e) { [self release]; @throw e; } @@ -364,11 +353,11 @@ void *pool = objc_autoreleasePoolPush(); OFString *stringValue; @try { if (![element.name isEqual: self.className] || - ![element.namespace isEqual: OF_SERIALIZATION_NS]) + ![element.namespace isEqual: OFSerializationNS]) @throw [OFInvalidArgumentException exception]; stringValue = element.stringValue; } @catch (id e) { [self release]; @@ -383,11 +372,11 @@ } - (void)dealloc { if (_freeWhenDone) - free(_items); + OFFreeMemory(_items); [_parentData release]; [super dealloc]; } @@ -456,58 +445,55 @@ return false; return true; } -- (of_comparison_result_t)compare: (id )object +- (OFComparisonResult)compare: (OFData *)data { - OFData *data; int comparison; size_t count, minCount; - if (![(id)object isKindOfClass: [OFData class]]) + if (![data isKindOfClass: [OFData class]]) @throw [OFInvalidArgumentException exception]; - data = (OFData *)object; - if (data.itemSize != _itemSize) @throw [OFInvalidArgumentException exception]; count = data.count; minCount = (_count > count ? count : _count); if ((comparison = memcmp(_items, data.items, minCount * _itemSize)) == 0) { if (_count > count) - return OF_ORDERED_DESCENDING; + return OFOrderedDescending; if (_count < count) - return OF_ORDERED_ASCENDING; + return OFOrderedAscending; - return OF_ORDERED_SAME; + return OFOrderedSame; } if (comparison > 0) - return OF_ORDERED_DESCENDING; + return OFOrderedDescending; else - return OF_ORDERED_ASCENDING; + return OFOrderedAscending; } - (unsigned long)hash { - uint32_t hash; + unsigned long hash; - OF_HASH_INIT(hash); + OFHashInit(&hash); for (size_t i = 0; i < _count * _itemSize; i++) - OF_HASH_ADD(hash, ((uint8_t *)_items)[i]); + OFHashAdd(&hash, ((uint8_t *)_items)[i]); - OF_HASH_FINALIZE(hash); + OFHashFinalize(&hash); return hash; } -- (OFData *)subdataWithRange: (of_range_t)range +- (OFData *)subdataWithRange: (OFRange)range { OFData *ret; if (range.length > SIZE_MAX - range.location || range.location + range.length > _count) @@ -552,16 +538,16 @@ return ret; } - (OFString *)stringByBase64Encoding { - return of_base64_encode(_items, _count * _itemSize); + return OFBase64Encode(_items, _count * _itemSize); } -- (of_range_t)rangeOfData: (OFData *)data - options: (int)options - range: (of_range_t)range +- (OFRange)rangeOfData: (OFData *)data + options: (OFDataSearchOptions)options + range: (OFRange)range { const char *search; size_t searchLength; if (range.length > SIZE_MAX - range.location || @@ -570,22 +556,22 @@ if (data == nil || data.itemSize != _itemSize) @throw [OFInvalidArgumentException exception]; if ((searchLength = data.count) == 0) - return of_range(0, 0); + return OFRangeMake(0, 0); if (searchLength > range.length) - return of_range(OF_NOT_FOUND, 0); + return OFRangeMake(OFNotFound, 0); search = data.items; - if (options & OF_DATA_SEARCH_BACKWARDS) { + if (options & OFDataSearchBackwards) { for (size_t i = range.length - searchLength;; i--) { if (memcmp(_items + i * _itemSize, search, searchLength * _itemSize) == 0) - return of_range(i, searchLength); + return OFRangeMake(i, searchLength); /* No match and we're at the last item */ if (i == 0) break; } @@ -592,25 +578,22 @@ } else { for (size_t i = range.location; i <= range.length - searchLength; i++) if (memcmp(_items + i * _itemSize, search, searchLength * _itemSize) == 0) - return of_range(i, searchLength); + return OFRangeMake(i, searchLength); } - return of_range(OF_NOT_FOUND, 0); + return OFRangeMake(OFNotFound, 0); } #ifdef OF_HAVE_FILES - (void)writeToFile: (OFString *)path { - OFFile *file = [[OFFile alloc] initWithPath: path - mode: @"w"]; - + OFFile *file = [[OFFile alloc] initWithPath: path mode: @"w"]; @try { - [file writeBuffer: _items - length: _count * _itemSize]; + [file writeBuffer: _items length: _count * _itemSize]; } @finally { [file release]; } } #endif @@ -617,18 +600,15 @@ - (void)writeToURL: (OFURL *)URL { void *pool = objc_autoreleasePoolPush(); OFURLHandler *URLHandler; - OFStream *stream; if ((URLHandler = [OFURLHandler handlerForURL: URL]) == nil) @throw [OFUnsupportedProtocolException exceptionWithURL: URL]; - stream = [URLHandler openItemAtURL: URL - mode: @"w"]; - [stream writeData: self]; + [[URLHandler openItemAtURL: URL mode: @"w"] writeData: self]; objc_autoreleasePoolPop(pool); } - (OFXMLElement *)XMLElementBySerializing @@ -640,12 +620,12 @@ @throw [OFInvalidArgumentException exception]; pool = objc_autoreleasePoolPush(); element = [OFXMLElement elementWithName: self.className - namespace: OF_SERIALIZATION_NS - stringValue: of_base64_encode(_items, _count * _itemSize)]; + namespace: OFSerializationNS + stringValue: OFBase64Encode(_items, _count * _itemSize)]; [element retain]; objc_autoreleasePoolPop(pool); @@ -661,41 +641,31 @@ if (_count <= UINT8_MAX) { uint8_t type = 0xC4; uint8_t tmp = (uint8_t)_count; - data = [OFMutableData dataWithItemSize: 1 - capacity: _count + 2]; - + data = [OFMutableData dataWithCapacity: _count + 2]; [data addItem: &type]; [data addItem: &tmp]; } else if (_count <= UINT16_MAX) { uint8_t type = 0xC5; - uint16_t tmp = OF_BSWAP16_IF_LE((uint16_t)_count); - - data = [OFMutableData dataWithItemSize: 1 - capacity: _count + 3]; - - [data addItem: &type]; - [data addItems: &tmp - count: sizeof(tmp)]; + uint16_t tmp = OFToBigEndian16((uint16_t)_count); + + data = [OFMutableData dataWithCapacity: _count + 3]; + [data addItem: &type]; + [data addItems: &tmp count: sizeof(tmp)]; } else if (_count <= UINT32_MAX) { uint8_t type = 0xC6; - uint32_t tmp = OF_BSWAP32_IF_LE((uint32_t)_count); - - data = [OFMutableData dataWithItemSize: 1 - capacity: _count + 5]; - - [data addItem: &type]; - [data addItems: &tmp - count: sizeof(tmp)]; + uint32_t tmp = OFToBigEndian32((uint32_t)_count); + + data = [OFMutableData dataWithCapacity: _count + 5]; + [data addItem: &type]; + [data addItems: &tmp count: sizeof(tmp)]; } else @throw [OFOutOfRangeException exception]; - [data addItems: _items - count: _count]; - + [data addItems: _items count: _count]; [data makeImmutable]; return data; } @end