1995-04-09 02:35:28 +00:00
|
|
|
/* Interface for NSData for GNUStep
|
1995-07-01 19:01:11 +00:00
|
|
|
Copyright (C) 1995 Free Software Foundation, Inc.
|
|
|
|
|
1996-04-17 20:17:45 +00:00
|
|
|
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
1995-07-01 19:01:11 +00:00
|
|
|
Date: 1995
|
1995-04-09 02:35:28 +00:00
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
1995-04-09 02:35:28 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
2007-09-14 11:36:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
1995-04-09 02:35:28 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2007-09-14 11:36:11 +00:00
|
|
|
version 3 of the License, or (at your option) any later version.
|
1995-04-09 02:35:28 +00:00
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
1995-04-09 02:35:28 +00:00
|
|
|
License along with this library; if not, write to the Free
|
2006-10-31 07:05:46 +00:00
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02111 USA.
|
1995-04-09 02:35:28 +00:00
|
|
|
*/
|
|
|
|
|
1996-04-17 19:36:35 +00:00
|
|
|
#ifndef __NSData_h_GNUSTEP_BASE_INCLUDE
|
|
|
|
#define __NSData_h_GNUSTEP_BASE_INCLUDE
|
2006-10-31 07:05:46 +00:00
|
|
|
#import <GNUstepBase/GSVersionMacros.h>
|
1995-04-09 02:35:28 +00:00
|
|
|
|
2006-10-31 07:05:46 +00:00
|
|
|
#import <Foundation/NSObject.h>
|
|
|
|
#import <Foundation/NSRange.h>
|
|
|
|
#import <Foundation/NSSerialization.h>
|
1995-04-09 02:35:28 +00:00
|
|
|
|
2006-09-13 10:20:49 +00:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2006-10-31 07:05:46 +00:00
|
|
|
#if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST)
|
2000-09-22 13:45:58 +00:00
|
|
|
@class NSURL;
|
|
|
|
#endif
|
|
|
|
|
1997-10-28 14:34:49 +00:00
|
|
|
@interface NSData : NSObject <NSCoding, NSCopying, NSMutableCopying>
|
1995-04-09 02:35:28 +00:00
|
|
|
|
|
|
|
// Allocating and Initializing a Data Object
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
+ (id) data;
|
|
|
|
+ (id) dataWithBytes: (const void*)bytes
|
|
|
|
length: (unsigned int)length;
|
|
|
|
+ (id) dataWithBytesNoCopy: (void*)bytes
|
|
|
|
length: (unsigned int)length;
|
2006-10-31 07:05:46 +00:00
|
|
|
#if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST)
|
2002-08-27 12:26:17 +00:00
|
|
|
+ (id) dataWithBytesNoCopy: (void*)aBuffer
|
|
|
|
length: (unsigned int)bufferSize
|
|
|
|
freeWhenDone: (BOOL)shouldFree;
|
|
|
|
#endif
|
1999-09-16 07:21:34 +00:00
|
|
|
+ (id) dataWithContentsOfFile: (NSString*)path;
|
|
|
|
+ (id) dataWithContentsOfMappedFile: (NSString*)path;
|
2006-10-31 07:05:46 +00:00
|
|
|
#if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST)
|
2000-09-22 13:45:58 +00:00
|
|
|
+ (id) dataWithContentsOfURL: (NSURL*)url;
|
|
|
|
#endif
|
1999-09-16 07:21:34 +00:00
|
|
|
+ (id) dataWithData: (NSData*)data;
|
2001-12-17 14:31:42 +00:00
|
|
|
- (id) initWithBytes: (const void*)aBuffer
|
|
|
|
length: (unsigned int)bufferSize;
|
|
|
|
- (id) initWithBytesNoCopy: (void*)aBuffer
|
|
|
|
length: (unsigned int)bufferSize;
|
2006-10-31 07:05:46 +00:00
|
|
|
#if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST)
|
2002-08-27 12:26:17 +00:00
|
|
|
- (id) initWithBytesNoCopy: (void*)aBuffer
|
|
|
|
length: (unsigned int)bufferSize
|
|
|
|
freeWhenDone: (BOOL)shouldFree;
|
|
|
|
#endif
|
1999-09-16 07:21:34 +00:00
|
|
|
- (id) initWithContentsOfFile: (NSString*)path;
|
|
|
|
- (id) initWithContentsOfMappedFile: (NSString*)path;
|
2006-10-31 07:05:46 +00:00
|
|
|
#if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST)
|
2000-09-22 13:45:58 +00:00
|
|
|
- (id) initWithContentsOfURL: (NSURL*)url;
|
|
|
|
#endif
|
1999-09-16 07:21:34 +00:00
|
|
|
- (id) initWithData: (NSData*)data;
|
1995-04-09 02:35:28 +00:00
|
|
|
|
|
|
|
// Accessing Data
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
- (const void*) bytes;
|
|
|
|
- (NSString*) description;
|
|
|
|
- (void) getBytes: (void*)buffer;
|
|
|
|
- (void) getBytes: (void*)buffer
|
|
|
|
length: (unsigned int)length;
|
|
|
|
- (void) getBytes: (void*)buffer
|
|
|
|
range: (NSRange)aRange;
|
|
|
|
- (NSData*) subdataWithRange: (NSRange)aRange;
|
1995-04-09 02:35:28 +00:00
|
|
|
|
|
|
|
// Querying a Data Object
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
- (BOOL) isEqualToData: (NSData*)other;
|
|
|
|
- (unsigned int) length;
|
1995-04-09 02:35:28 +00:00
|
|
|
|
|
|
|
// Storing Data
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
- (BOOL) writeToFile: (NSString*)path
|
|
|
|
atomically: (BOOL)useAuxiliaryFile;
|
1995-04-09 02:35:28 +00:00
|
|
|
|
2006-10-31 07:05:46 +00:00
|
|
|
#if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST)
|
2002-01-02 10:27:26 +00:00
|
|
|
- (BOOL) writeToURL: (NSURL*)anURL atomically: (BOOL)flag;
|
|
|
|
#endif
|
|
|
|
|
1995-04-09 02:35:28 +00:00
|
|
|
// Deserializing Data
|
|
|
|
|
1999-09-16 07:21:34 +00:00
|
|
|
- (unsigned int) deserializeAlignedBytesLengthAtCursor: (unsigned int*)cursor;
|
|
|
|
- (void) deserializeBytes: (void*)buffer
|
|
|
|
length: (unsigned int)bytes
|
|
|
|
atCursor: (unsigned int*)cursor;
|
|
|
|
- (void) deserializeDataAt: (void*)data
|
|
|
|
ofObjCType: (const char*)type
|
|
|
|
atCursor: (unsigned int*)cursor
|
|
|
|
context: (id <NSObjCTypeSerializationCallBack>)callback;
|
|
|
|
- (int) deserializeIntAtCursor: (unsigned int*)cursor;
|
2001-12-17 14:31:42 +00:00
|
|
|
- (int) deserializeIntAtIndex: (unsigned int)index;
|
1999-09-16 07:21:34 +00:00
|
|
|
- (void) deserializeInts: (int*)intBuffer
|
|
|
|
count: (unsigned int)numInts
|
|
|
|
atCursor: (unsigned int*)cursor;
|
|
|
|
- (void) deserializeInts: (int*)intBuffer
|
|
|
|
count: (unsigned int)numInts
|
|
|
|
atIndex: (unsigned int)index;
|
1995-04-09 02:35:28 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2006-10-31 07:05:46 +00:00
|
|
|
#if OS_API_VERSION(GS_API_NONE, GS_API_NONE)
|
1999-01-27 12:49:49 +00:00
|
|
|
|
2004-06-22 22:27:39 +00:00
|
|
|
/**
|
|
|
|
* Extension methods for the NSData class- mainly conversion utilities.
|
|
|
|
*/
|
2003-04-17 06:20:17 +00:00
|
|
|
@interface NSData (GSCategories)
|
2004-06-22 22:27:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an NSString object containing an ASCII hexadecimal representation
|
|
|
|
* of the receiver. This means that the returned object will contain
|
|
|
|
* exactly twice as many characters as there are bytes as the receiver,
|
|
|
|
* as each byte in the receiver is represented by two hexadecimal digits.<br />
|
|
|
|
* The high order four bits of each byte is encoded before the low
|
|
|
|
* order four bits. Capital letters 'A' to 'F' are used to represent
|
|
|
|
* values from 10 to 15.<br />
|
|
|
|
* If you need the hexadecimal representation as raw byte data, use code
|
|
|
|
* like -
|
|
|
|
* <example>
|
|
|
|
* hexData = [[sourceData hexadecimalRepresentation]
|
|
|
|
* dataUsingEncoding: NSASCIIStringEncoding];
|
|
|
|
* </example>
|
|
|
|
*/
|
2003-04-17 06:20:17 +00:00
|
|
|
- (NSString*) hexadecimalRepresentation;
|
2004-06-22 22:27:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialises the receiver with the supplied string data which contains
|
|
|
|
* a hexadecimal coding of the bytes. The parsing of the string is
|
|
|
|
* fairly tolerant, ignoring whitespace and permitting both upper and
|
|
|
|
* lower case hexadecimal digits (the -hexadecimalRepresentation method
|
2005-11-06 13:53:40 +00:00
|
|
|
* produces a string using only uppercase digits with no white space).<br />
|
2004-06-22 22:27:39 +00:00
|
|
|
* If the string does not contain one or more pairs of hexadecimal digits
|
|
|
|
* then an exception is raised.
|
|
|
|
*/
|
2003-04-17 06:20:17 +00:00
|
|
|
- (id) initWithHexadecimalRepresentation: (NSString*)string;
|
2004-06-22 22:27:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an MD5 digest of the information stored in the receiver and
|
|
|
|
* returns it as an autoreleased 16 byte NSData object.<br />
|
|
|
|
* If you need to produce a digest of string information, you need to
|
|
|
|
* decide what character encoding is to be used and convert your string
|
|
|
|
* to a data object of that encoding type first using the
|
|
|
|
* [NSString-dataUsingEncoding:] method -
|
|
|
|
* <example>
|
|
|
|
* myDigest = [[myString dataUsingEncoding: NSUTF8StringEncoding] md5Digest];
|
|
|
|
* </example>
|
|
|
|
* If you need to use the digest in a human readable form, you will
|
|
|
|
* probably want it to be seen as 32 hexadecimal digits, and can do that
|
|
|
|
* using the -hexadecimalRepresentation method.
|
|
|
|
*/
|
2003-04-17 06:20:17 +00:00
|
|
|
- (NSData*) md5Digest;
|
2004-06-22 22:27:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Decodes the source data from uuencoded and return the result.<br />
|
|
|
|
* Returns the encoded file name in namePtr if it is not null.
|
|
|
|
* Returns the encoded file mode in modePtr if it is not null.
|
|
|
|
*/
|
2004-03-18 16:37:47 +00:00
|
|
|
- (BOOL) uudecodeInto: (NSMutableData*)decoded
|
|
|
|
name: (NSString**)namePtr
|
|
|
|
mode: (int*)modePtr;
|
2004-06-22 22:27:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode the source data to uuencoded.<br />
|
|
|
|
* Uses the supplied name as the filename in the encoded data,
|
|
|
|
* and says that the file mode is as specified.<br />
|
|
|
|
* If no name is supplied, uses <code>untitled</code> as the name.
|
|
|
|
*/
|
2004-03-18 16:37:47 +00:00
|
|
|
- (BOOL) uuencodeInto: (NSMutableData*)encoded
|
|
|
|
name: (NSString*)name
|
|
|
|
mode: (int)mode;
|
2003-04-17 06:20:17 +00:00
|
|
|
@end
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
/*
|
|
|
|
* We include special support for coding/decoding - adding methods for
|
|
|
|
* serializing/deserializing type-tags and cross-references.
|
|
|
|
*
|
|
|
|
* A type-tag is a byte containing -
|
|
|
|
* Bit7 Set to indicate that the tag is for a cross-reference.
|
|
|
|
* Bit5-6 A value for the size of the type or cross-reference.
|
|
|
|
* Bit0-4 A value representing an Objective-C type.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GSC_NONE 0x00 /* No type information. */
|
|
|
|
#define _GSC_XREF 0x80 /* Cross reference to an item. */
|
|
|
|
#define _GSC_SIZE 0x60 /* Type-size info mask. */
|
|
|
|
#define _GSC_MASK 0x1f /* Basic type info mask. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the tag is for a cross-reference, the size field defines the
|
|
|
|
* size of the cross-reference value -
|
|
|
|
* _GSC_X_0 (no crossref), _GSC_X_1, _GSC_X_2, _GSC_X_4
|
|
|
|
*/
|
|
|
|
#define _GSC_X_0 0x00 /* nil or null pointer */
|
|
|
|
#define _GSC_X_1 0x20 /* 8-bit cross-ref */
|
|
|
|
#define _GSC_X_2 0x40 /* 16-bit cross-ref */
|
|
|
|
#define _GSC_X_4 0x60 /* 32-bit cross-ref */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the tag is for an integer value, the size field defines the
|
|
|
|
* size of the the encoded integer -
|
|
|
|
* _GSC_I16, _GSC_I32, _GSC_I64, _GSC_I128
|
|
|
|
* The file GSConfig.h (produced by the configure script) defines the
|
|
|
|
* size codes for this machines 'natural' integers -
|
|
|
|
* _GSC_S_SHT, _GSC_S_INT, _GSC_S_LNG, _GSC_S_LNG_LNG
|
|
|
|
*/
|
|
|
|
#define _GSC_I16 0x00
|
|
|
|
#define _GSC_I32 0x20
|
|
|
|
#define _GSC_I64 0x40
|
|
|
|
#define _GSC_I128 0x60
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For the first sixteen types, the size information applies to the
|
|
|
|
* size of the type, for the second sixteen it applies to the
|
|
|
|
* following cross-reference number (or is zero if no crossref follows).
|
|
|
|
*/
|
|
|
|
#define _GSC_MAYX 0x10 /* Item may have crossref. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These are the types that can be archived -
|
|
|
|
*/
|
|
|
|
#define _GSC_CHR 0x01
|
|
|
|
#define _GSC_UCHR 0x02
|
|
|
|
#define _GSC_SHT 0x03
|
|
|
|
#define _GSC_USHT 0x04
|
|
|
|
#define _GSC_INT 0x05
|
|
|
|
#define _GSC_UINT 0x06
|
|
|
|
#define _GSC_LNG 0x07
|
|
|
|
#define _GSC_ULNG 0x08
|
|
|
|
#define _GSC_LNG_LNG 0x09
|
|
|
|
#define _GSC_ULNG_LNG 0x0a
|
|
|
|
#define _GSC_FLT 0x0b
|
|
|
|
#define _GSC_DBL 0x0c
|
|
|
|
|
|
|
|
#define _GSC_ID 0x10
|
|
|
|
#define _GSC_CLASS 0x11
|
|
|
|
#define _GSC_SEL 0x12
|
|
|
|
#define _GSC_PTR 0x13
|
|
|
|
#define _GSC_CHARPTR 0x14
|
|
|
|
#define _GSC_ARY_B 0x15
|
|
|
|
#define _GSC_STRUCT_B 0x16
|
2000-08-07 22:00:31 +00:00
|
|
|
#define _GSC_CID 0x17 /* Class encoded as id */
|
1999-01-27 12:49:49 +00:00
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
@interface NSData (GNUstepExtensions)
|
2001-12-17 14:31:42 +00:00
|
|
|
+ (id) dataWithShmID: (int)anID length: (unsigned int) length;
|
|
|
|
+ (id) dataWithSharedBytes: (const void*)bytes length: (unsigned int) length;
|
1998-10-23 15:48:21 +00:00
|
|
|
|
|
|
|
/*
|
1999-01-27 12:49:49 +00:00
|
|
|
* -deserializeTypeTag:andCrossRef:atCursor:
|
|
|
|
* This method is provided in order to give the GNUstep version of
|
1998-10-23 15:48:21 +00:00
|
|
|
* NSUnarchiver maximum possible performance.
|
|
|
|
*/
|
1999-01-27 12:49:49 +00:00
|
|
|
- (void) deserializeTypeTag: (unsigned char*)tag
|
2001-12-17 14:31:42 +00:00
|
|
|
andCrossRef: (unsigned int*)ref
|
|
|
|
atCursor: (unsigned int*)cursor;
|
1997-09-29 14:39:53 +00:00
|
|
|
@end
|
1998-11-10 20:23:39 +00:00
|
|
|
#endif
|
1995-04-17 20:42:02 +00:00
|
|
|
|
|
|
|
@interface NSMutableData : NSData
|
|
|
|
|
|
|
|
+ (id) dataWithCapacity: (unsigned int)numBytes;
|
|
|
|
+ (id) dataWithLength: (unsigned int)length;
|
|
|
|
- (id) initWithCapacity: (unsigned int)capacity;
|
|
|
|
- (id) initWithLength: (unsigned int)length;
|
|
|
|
|
|
|
|
// Adjusting Capacity
|
|
|
|
|
|
|
|
- (void) increaseLengthBy: (unsigned int)extraLength;
|
2001-12-17 14:31:42 +00:00
|
|
|
- (void) setLength: (unsigned int)size;
|
1995-04-17 20:42:02 +00:00
|
|
|
- (void*) mutableBytes;
|
|
|
|
|
|
|
|
// Appending Data
|
|
|
|
|
2001-12-17 14:31:42 +00:00
|
|
|
- (void) appendBytes: (const void*)aBuffer
|
|
|
|
length: (unsigned int)bufferSize;
|
1995-04-17 20:42:02 +00:00
|
|
|
- (void) appendData: (NSData*)other;
|
|
|
|
|
|
|
|
// Modifying Data
|
|
|
|
|
|
|
|
- (void) replaceBytesInRange: (NSRange)aRange
|
|
|
|
withBytes: (const void*)bytes;
|
2006-10-31 07:05:46 +00:00
|
|
|
#if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST)
|
2002-08-27 12:26:17 +00:00
|
|
|
- (void) replaceBytesInRange: (NSRange)aRange
|
|
|
|
withBytes: (const void*)bytes
|
|
|
|
length: (unsigned int)length;
|
|
|
|
#endif
|
1995-04-17 20:42:02 +00:00
|
|
|
- (void) resetBytesInRange: (NSRange)aRange;
|
1998-01-19 15:20:15 +00:00
|
|
|
- (void) setData: (NSData*)data;
|
1995-04-17 20:42:02 +00:00
|
|
|
|
|
|
|
// Serializing Data
|
|
|
|
|
|
|
|
- (void) serializeAlignedBytesLength: (unsigned int)length;
|
|
|
|
- (void) serializeDataAt: (const void*)data
|
|
|
|
ofObjCType: (const char*)type
|
|
|
|
context: (id <NSObjCTypeSerializationCallBack>)callback;
|
|
|
|
- (void) serializeInt: (int)value;
|
|
|
|
- (void) serializeInt: (int)value
|
2001-12-17 14:31:42 +00:00
|
|
|
atIndex: (unsigned int)index;
|
1995-04-17 20:42:02 +00:00
|
|
|
- (void) serializeInts: (int*)intBuffer
|
|
|
|
count: (unsigned int)numInts;
|
|
|
|
- (void) serializeInts: (int*)intBuffer
|
|
|
|
count: (unsigned int)numInts
|
2001-12-17 14:31:42 +00:00
|
|
|
atIndex: (unsigned int)index;
|
1995-04-17 20:42:02 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2006-10-31 07:05:46 +00:00
|
|
|
#if OS_API_VERSION(GS_API_NONE, GS_API_NONE)
|
2003-04-17 06:20:17 +00:00
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
@interface NSMutableData (GNUstepExtensions)
|
1998-10-23 15:48:21 +00:00
|
|
|
/*
|
|
|
|
* Capacity management - GNUstep gives you control over the size of
|
|
|
|
* the data buffer as well as the 'length' of valid data in it.
|
|
|
|
*/
|
1997-09-29 14:39:53 +00:00
|
|
|
- (unsigned int) capacity;
|
|
|
|
- (id) setCapacity: (unsigned int)newCapacity;
|
1998-10-23 15:48:21 +00:00
|
|
|
|
|
|
|
- (int) shmID; /* Shared memory ID for data buffer (if any) */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* -serializeTypeTag:
|
1999-01-27 12:49:49 +00:00
|
|
|
* -serializeTypeTag:andCrossRef:
|
1998-10-23 15:48:21 +00:00
|
|
|
* These methods are provided in order to give the GNUstep version of
|
|
|
|
* NSArchiver maximum possible performance.
|
|
|
|
*/
|
|
|
|
- (void) serializeTypeTag: (unsigned char)tag;
|
1999-01-27 12:49:49 +00:00
|
|
|
- (void) serializeTypeTag: (unsigned char)tag
|
2001-12-17 14:31:42 +00:00
|
|
|
andCrossRef: (unsigned int)xref;
|
1998-10-23 15:48:21 +00:00
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
@end
|
1998-11-10 20:23:39 +00:00
|
|
|
#endif
|
1997-09-29 14:39:53 +00:00
|
|
|
|
2006-09-13 10:20:49 +00:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
1995-04-17 20:42:02 +00:00
|
|
|
|
1996-04-17 19:36:35 +00:00
|
|
|
#endif /* __NSData_h_GNUSTEP_BASE_INCLUDE */
|