1995-04-09 02:20:37 +00:00
|
|
|
|
/* Stream of bytes class for serialization and persistance in GNUStep
|
2000-06-27 16:18:02 +00:00
|
|
|
|
Copyright (C) 1995, 1996, 1997, 2000 Free Software Foundation, Inc.
|
1995-04-09 02:20:37 +00:00
|
|
|
|
|
1996-04-17 20:17:45 +00:00
|
|
|
|
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
1995-04-09 02:20:37 +00:00
|
|
|
|
Date: March 1995
|
1997-09-29 14:39:53 +00:00
|
|
|
|
Rewritten by: Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
|
|
|
|
Date: September 1997
|
1995-04-09 02:20:37 +00:00
|
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
|
This file is part of the GNUstep Base Library.
|
1995-04-09 02:20:37 +00:00
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
|
License along with this library; if not, write to the Free
|
1999-09-09 02:56:20 +00:00
|
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
1995-04-09 02:20:37 +00:00
|
|
|
|
*/
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
/* NOTES - Richard Frith-Macdonald 1997
|
|
|
|
|
*
|
|
|
|
|
* Rewritten to use the class cluster architecture as in OPENSTEP.
|
|
|
|
|
*
|
|
|
|
|
* NB. In our implementaion we require an extra primitive for the
|
|
|
|
|
* NSMutableData subclasses. This new primitive method is the
|
|
|
|
|
* [-setCapacity:] method, and it differs from [-setLength:]
|
|
|
|
|
* as follows -
|
|
|
|
|
*
|
|
|
|
|
* [-setLength:]
|
|
|
|
|
* clears bytes when the allocated buffer grows
|
1998-10-21 11:56:58 +00:00
|
|
|
|
* never shrinks the allocated buffer capacity
|
1997-09-29 14:39:53 +00:00
|
|
|
|
* [-setCapacity:]
|
|
|
|
|
* doesn't clear newly allocated bytes
|
|
|
|
|
* sets the size of the allocated buffer.
|
|
|
|
|
*
|
|
|
|
|
* The actual class hierarchy is as follows -
|
|
|
|
|
*
|
|
|
|
|
* NSData Abstract base class.
|
1998-10-21 11:56:58 +00:00
|
|
|
|
* NSDataStatic Concrete class static buffers.
|
|
|
|
|
* NSDataMalloc Concrete class.
|
|
|
|
|
* NSDataMappedFile Memory mapped files.
|
|
|
|
|
* NSDataShared Extension for shared memory.
|
1997-09-29 14:39:53 +00:00
|
|
|
|
* NSMutableData Abstract base class.
|
|
|
|
|
* NSMutableDataMalloc Concrete class.
|
|
|
|
|
* NSMutableDataShared Extension for shared memory.
|
|
|
|
|
*
|
1998-10-21 11:56:58 +00:00
|
|
|
|
* NSMutableDataMalloc MUST share it's initial instance variable layout
|
|
|
|
|
* with NSDataMalloc so that it can use the 'behavior' code to inherit
|
|
|
|
|
* methods from NSDataMalloc.
|
|
|
|
|
*
|
1997-09-29 14:39:53 +00:00
|
|
|
|
* Since all the other subclasses are based on NSDataMalloc or
|
|
|
|
|
* NSMutableDataMalloc, we can put most methods in here and not
|
|
|
|
|
* bother with duplicating them in the other classes.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
1997-11-06 00:51:23 +00:00
|
|
|
|
#include <config.h>
|
1997-12-11 19:09:56 +00:00
|
|
|
|
#include <objc/objc-api.h>
|
1998-12-20 21:27:47 +00:00
|
|
|
|
#include <base/preface.h>
|
|
|
|
|
#include <base/fast.x>
|
|
|
|
|
#include <base/behavior.h>
|
1998-03-06 18:13:53 +00:00
|
|
|
|
#include <Foundation/NSByteOrder.h>
|
1997-09-29 14:39:53 +00:00
|
|
|
|
#include <Foundation/NSCoder.h>
|
1995-04-17 21:13:20 +00:00
|
|
|
|
#include <Foundation/NSData.h>
|
|
|
|
|
#include <Foundation/NSString.h>
|
1995-04-21 00:11:21 +00:00
|
|
|
|
#include <Foundation/NSException.h>
|
1997-09-29 14:39:53 +00:00
|
|
|
|
#include <Foundation/NSDebug.h>
|
1999-02-20 21:19:15 +00:00
|
|
|
|
#include <Foundation/NSFileManager.h>
|
|
|
|
|
#include <Foundation/NSPathUtilities.h>
|
1999-06-21 08:30:26 +00:00
|
|
|
|
#include <Foundation/NSRange.h>
|
2000-04-14 10:38:22 +00:00
|
|
|
|
#include <Foundation/NSZone.h>
|
1996-03-18 13:55:26 +00:00
|
|
|
|
#include <string.h> /* for memset() */
|
1997-09-01 21:59:51 +00:00
|
|
|
|
#include <unistd.h> /* SEEK_* on SunOS 4 */
|
1995-04-09 02:20:37 +00:00
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
#if HAVE_MMAP
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#ifndef MAP_FAILED
|
|
|
|
|
#define MAP_FAILED ((void*)-1) /* Failure address. */
|
|
|
|
|
#endif
|
|
|
|
|
@class NSDataMappedFile;
|
|
|
|
|
#endif
|
1995-04-09 02:20:37 +00:00
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
#if HAVE_SHMCTL
|
|
|
|
|
#include <sys/ipc.h>
|
|
|
|
|
#include <sys/shm.h>
|
1995-04-09 02:20:37 +00:00
|
|
|
|
|
1997-10-28 14:34:49 +00:00
|
|
|
|
#define VM_RDONLY 0644 /* self read/write - other readonly */
|
|
|
|
|
#define VM_ACCESS 0666 /* read/write access for all */
|
1997-09-29 14:39:53 +00:00
|
|
|
|
@class NSDataShared;
|
|
|
|
|
@class NSMutableDataShared;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
@class NSDataMalloc;
|
|
|
|
|
@class NSDataStatic;
|
|
|
|
|
@class NSMutableDataMalloc;
|
1995-07-03 22:10:49 +00:00
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
/*
|
|
|
|
|
* Some static variables to cache classes and methods for quick access -
|
|
|
|
|
* these are set up at process startup or in [NSData +initialize]
|
|
|
|
|
*/
|
|
|
|
|
static SEL appendSel = @selector(appendBytes:length:);
|
2000-07-03 11:47:17 +00:00
|
|
|
|
static Class dataStatic;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
static Class dataMalloc;
|
|
|
|
|
static Class mutableDataMalloc;
|
|
|
|
|
static IMP appendImp;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
|
|
|
|
|
static BOOL
|
1998-10-15 05:03:16 +00:00
|
|
|
|
readContentsOfFile(NSString* path, void** buf, unsigned* len, NSZone* zone)
|
1995-07-03 22:10:49 +00:00
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
char thePath[BUFSIZ*2];
|
|
|
|
|
FILE *theFile = 0;
|
|
|
|
|
unsigned fileLength;
|
|
|
|
|
void *tmp = 0;
|
|
|
|
|
int c;
|
2000-06-12 05:17:41 +00:00
|
|
|
|
#if defined(__MINGW__)
|
2000-06-30 15:44:30 +00:00
|
|
|
|
HANDLE fh;
|
|
|
|
|
DWORD fileLength
|
|
|
|
|
DWORD high;
|
|
|
|
|
DWORD got;
|
2000-06-12 05:17:41 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
if ([path getFileSystemRepresentation: thePath
|
|
|
|
|
maxLength: sizeof(thePath)-1] == NO)
|
|
|
|
|
{
|
1999-11-28 18:56:27 +00:00
|
|
|
|
NSDebugLog(@"Open (%s) attempt failed - bad path", thePath);
|
1999-01-27 12:49:49 +00:00
|
|
|
|
return NO;
|
|
|
|
|
}
|
2000-06-30 15:44:30 +00:00
|
|
|
|
|
|
|
|
|
#if defined(__MINGW__)
|
|
|
|
|
fh = CreateFile(thePath, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING,
|
|
|
|
|
FILE_ATTRIBUTE_NORMAL, 0);
|
|
|
|
|
if (fh == INVALID_HANDLE_VALUE)
|
|
|
|
|
{
|
|
|
|
|
NSDebugLog(@"Open (%s) attempt failed", thePath);
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileLength = GetFileSize(fh, &high);
|
|
|
|
|
if ((fileLength == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
|
|
|
|
|
{
|
|
|
|
|
CloseHandle(fh);
|
|
|
|
|
NSLog(@"Failed to determine size of - %s", thePath);
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
if (high != 0)
|
|
|
|
|
{
|
|
|
|
|
CloseHandle(fh);
|
|
|
|
|
NSLog(@"File too big to handle - %s", thePath);
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if GS_WITH_GC == 1
|
|
|
|
|
tmp = NSZoneMalloc(GSAtomicMallocZone(), fileLength);
|
|
|
|
|
#else
|
|
|
|
|
tmp = NSZoneMalloc(zone, fileLength);
|
|
|
|
|
#endif
|
|
|
|
|
if (tmp == 0)
|
|
|
|
|
{
|
|
|
|
|
CloseHandle(fh);
|
|
|
|
|
NSLog(@"Malloc failed for file (%s) of length %d - %s",
|
|
|
|
|
thePath, fileLength, strerror(errno));
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
if (!ReadFile(fh, tmp, fileSize, &got, 0))
|
|
|
|
|
{
|
|
|
|
|
if (tmp != 0)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(zone, tmp);
|
|
|
|
|
CloseHandle(fh);
|
|
|
|
|
NSLog(@"File read operation failed for %s", thePath);
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (got != fileSize)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(zone, tmp);
|
|
|
|
|
CloseHandle(fh);
|
|
|
|
|
NSLog(@"File read operation short for %s", thePath);
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
CloseHandle(fh);
|
|
|
|
|
*buf = tmp;
|
|
|
|
|
*len = fileLength;
|
|
|
|
|
return YES;
|
|
|
|
|
#endif
|
|
|
|
|
|
1999-05-05 18:47:44 +00:00
|
|
|
|
theFile = fopen(thePath, "rb");
|
1999-01-27 12:49:49 +00:00
|
|
|
|
|
|
|
|
|
if (theFile == NULL) /* We failed to open the file. */
|
|
|
|
|
{
|
1999-11-28 18:56:27 +00:00
|
|
|
|
NSDebugLog(@"Open (%s) attempt failed - %s", thePath, strerror(errno));
|
1999-01-27 12:49:49 +00:00
|
|
|
|
goto failure;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Seek to the end of the file.
|
|
|
|
|
*/
|
|
|
|
|
c = fseek(theFile, 0L, SEEK_END);
|
|
|
|
|
if (c != 0)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Seek to end of file failed - %s", strerror(errno));
|
|
|
|
|
goto failure;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Determine the length of the file (having seeked to the end of the
|
|
|
|
|
* file) by calling ftell().
|
|
|
|
|
*/
|
|
|
|
|
fileLength = ftell(theFile);
|
|
|
|
|
if (fileLength == -1)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Ftell failed - %s", strerror(errno));
|
|
|
|
|
goto failure;
|
|
|
|
|
}
|
|
|
|
|
|
1999-09-29 20:15:17 +00:00
|
|
|
|
#if GS_WITH_GC == 1
|
|
|
|
|
tmp = NSZoneMalloc(GSAtomicMallocZone(), fileLength);
|
|
|
|
|
#else
|
1999-01-27 12:49:49 +00:00
|
|
|
|
tmp = NSZoneMalloc(zone, fileLength);
|
1999-09-29 20:15:17 +00:00
|
|
|
|
#endif
|
1999-01-27 12:49:49 +00:00
|
|
|
|
if (tmp == 0)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Malloc failed for file of length %d- %s",
|
1997-09-29 14:39:53 +00:00
|
|
|
|
fileLength, strerror(errno));
|
1999-01-27 12:49:49 +00:00
|
|
|
|
goto failure;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
/*
|
|
|
|
|
* Rewind the file pointer to the beginning, preparing to read in
|
|
|
|
|
* the file.
|
|
|
|
|
*/
|
|
|
|
|
c = fseek(theFile, 0L, SEEK_SET);
|
|
|
|
|
if (c != 0)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Fseek to start of file failed - %s", strerror(errno));
|
|
|
|
|
goto failure;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
c = fread(tmp, 1, fileLength, theFile);
|
|
|
|
|
if (c != fileLength)
|
|
|
|
|
{
|
1999-05-05 18:47:44 +00:00
|
|
|
|
NSLog(@"read of file contents failed - %s", strerror(errno));
|
1999-01-27 12:49:49 +00:00
|
|
|
|
goto failure;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
*buf = tmp;
|
|
|
|
|
*len = fileLength;
|
|
|
|
|
fclose(theFile);
|
|
|
|
|
return YES;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
/*
|
|
|
|
|
* Just in case the failure action needs to be changed.
|
|
|
|
|
*/
|
1998-10-21 11:56:58 +00:00
|
|
|
|
failure:
|
2000-06-30 15:44:30 +00:00
|
|
|
|
if (tmp != 0)
|
1999-01-27 12:49:49 +00:00
|
|
|
|
NSZoneFree(zone, tmp);
|
2000-06-30 15:44:30 +00:00
|
|
|
|
if (theFile != 0)
|
1999-01-27 12:49:49 +00:00
|
|
|
|
fclose(theFile);
|
|
|
|
|
return NO;
|
1995-07-03 22:10:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* NB, The start of the NSMutableDataMalloc instance variables must be
|
|
|
|
|
* identical to that of NSDataMalloc in order to inherit its methods.
|
|
|
|
|
*/
|
|
|
|
|
@interface NSDataStatic : NSData
|
1995-07-03 22:10:49 +00:00
|
|
|
|
{
|
1998-10-21 11:56:58 +00:00
|
|
|
|
unsigned length;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
void *bytes;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@interface NSDataMalloc : NSDataStatic
|
|
|
|
|
{
|
1998-10-15 05:03:16 +00:00
|
|
|
|
NSZone *zone;
|
1995-07-03 22:10:49 +00:00
|
|
|
|
}
|
1997-09-29 14:39:53 +00:00
|
|
|
|
@end
|
1995-07-03 22:10:49 +00:00
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
@interface NSMutableDataMalloc : NSMutableData
|
1995-07-03 22:10:49 +00:00
|
|
|
|
{
|
1998-10-21 11:56:58 +00:00
|
|
|
|
unsigned length;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
void *bytes;
|
1998-10-15 05:03:16 +00:00
|
|
|
|
NSZone *zone;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
unsigned capacity;
|
|
|
|
|
unsigned growth;
|
1995-07-03 22:10:49 +00:00
|
|
|
|
}
|
1998-10-21 11:56:58 +00:00
|
|
|
|
/* Increase capacity to at least the specified minimum value. */
|
|
|
|
|
- (void) _grow: (unsigned)minimum;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
#if HAVE_MMAP
|
|
|
|
|
@interface NSDataMappedFile : NSDataMalloc
|
|
|
|
|
@end
|
|
|
|
|
#endif
|
1995-07-03 22:10:49 +00:00
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
#if HAVE_SHMCTL
|
|
|
|
|
@interface NSDataShared : NSDataMalloc
|
1995-07-03 22:10:49 +00:00
|
|
|
|
{
|
1997-09-29 14:39:53 +00:00
|
|
|
|
int shmid;
|
1995-07-03 22:10:49 +00:00
|
|
|
|
}
|
1997-09-29 14:39:53 +00:00
|
|
|
|
- (id) initWithShmID: (int)anId length: (unsigned)bufferSize;
|
|
|
|
|
@end
|
1995-07-03 22:10:49 +00:00
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
@interface NSMutableDataShared : NSMutableDataMalloc
|
1995-07-03 22:10:49 +00:00
|
|
|
|
{
|
1997-09-29 14:39:53 +00:00
|
|
|
|
int shmid;
|
1995-07-03 22:10:49 +00:00
|
|
|
|
}
|
1997-09-29 14:39:53 +00:00
|
|
|
|
- (id) initWithShmID: (int)anId length: (unsigned)bufferSize;
|
|
|
|
|
@end
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation NSData
|
1998-10-20 14:40:05 +00:00
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
if (self == [NSData class])
|
|
|
|
|
{
|
|
|
|
|
dataMalloc = [NSDataMalloc class];
|
2000-07-03 11:47:17 +00:00
|
|
|
|
dataStatic = [NSDataStatic class];
|
1999-01-27 12:49:49 +00:00
|
|
|
|
mutableDataMalloc = [NSMutableDataMalloc class];
|
2000-06-27 16:18:02 +00:00
|
|
|
|
appendImp = [mutableDataMalloc instanceMethodForSelector: appendSel];
|
1998-10-20 14:40:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-04-14 10:38:22 +00:00
|
|
|
|
+ (id) allocWithZone: (NSZone*)z
|
1995-08-23 15:36:59 +00:00
|
|
|
|
{
|
1998-10-21 11:56:58 +00:00
|
|
|
|
return (NSData*)NSAllocateObject(dataMalloc, 0, z);
|
1995-08-23 15:36:59 +00:00
|
|
|
|
}
|
1995-04-09 02:20:37 +00:00
|
|
|
|
|
|
|
|
|
+ (id) data
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSData *d;
|
|
|
|
|
|
|
|
|
|
d = [NSDataStatic allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithBytesNoCopy: 0 length: 0];
|
|
|
|
|
return AUTORELEASE(d);
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (id) dataWithBytes: (const void*)bytes
|
1998-10-21 11:56:58 +00:00
|
|
|
|
length: (unsigned)length
|
1995-04-09 02:20:37 +00:00
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSData *d;
|
|
|
|
|
|
|
|
|
|
d = [dataMalloc allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithBytes: bytes length: length];
|
|
|
|
|
return AUTORELEASE(d);
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (id) dataWithBytesNoCopy: (void*)bytes
|
1998-10-21 11:56:58 +00:00
|
|
|
|
length: (unsigned)length
|
1995-04-09 02:20:37 +00:00
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSData *d;
|
|
|
|
|
|
|
|
|
|
d = [dataMalloc allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithBytesNoCopy: bytes length: length];
|
|
|
|
|
return AUTORELEASE(d);
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
+ (id) dataWithContentsOfFile: (NSString*)path
|
1995-04-09 02:20:37 +00:00
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSData *d;
|
|
|
|
|
|
|
|
|
|
d = [dataMalloc allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithContentsOfFile: path];
|
|
|
|
|
return AUTORELEASE(d);
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (id) dataWithContentsOfMappedFile: (NSString*)path
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSData *d;
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
#if HAVE_MMAP
|
1999-07-03 19:59:44 +00:00
|
|
|
|
d = [NSDataMappedFile allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithContentsOfMappedFile: path];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
#else
|
1999-07-03 19:59:44 +00:00
|
|
|
|
d = [dataMalloc allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithContentsOfMappedFile: path];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
#endif
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return AUTORELEASE(d);
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-01-19 15:20:15 +00:00
|
|
|
|
+ (id) dataWithData: (NSData*)data
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSData *d;
|
|
|
|
|
|
|
|
|
|
d = [dataMalloc allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithBytes: [data bytes] length: [data length]];
|
|
|
|
|
return AUTORELEASE(d);
|
1998-01-19 15:20:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-07-03 11:47:17 +00:00
|
|
|
|
+ (id) new
|
|
|
|
|
{
|
|
|
|
|
NSData *d;
|
|
|
|
|
|
|
|
|
|
d = [dataMalloc allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithBytesNoCopy: 0 length: 0 fromZone: NSDefaultMallocZone()];
|
|
|
|
|
return d;
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-15 05:03:16 +00:00
|
|
|
|
- (id) init
|
1995-08-02 14:51:26 +00:00
|
|
|
|
{
|
2000-06-27 16:18:02 +00:00
|
|
|
|
return [self initWithBytesNoCopy: 0 length: 0];
|
1995-08-02 14:51:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-15 05:03:16 +00:00
|
|
|
|
- (id) initWithBytes: (const void*)aBuffer
|
1998-10-21 11:56:58 +00:00
|
|
|
|
length: (unsigned)bufferSize
|
1995-04-09 02:20:37 +00:00
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
|
return nil;
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-15 05:03:16 +00:00
|
|
|
|
- (id) initWithBytesNoCopy: (void*)aBuffer
|
1998-10-21 11:56:58 +00:00
|
|
|
|
length: (unsigned)bufferSize
|
1998-10-15 05:03:16 +00:00
|
|
|
|
{
|
|
|
|
|
if (aBuffer)
|
|
|
|
|
return [self initWithBytesNoCopy: aBuffer
|
|
|
|
|
length: bufferSize
|
|
|
|
|
fromZone: NSZoneFromPointer(aBuffer)];
|
|
|
|
|
else
|
|
|
|
|
return [self initWithBytesNoCopy: aBuffer
|
|
|
|
|
length: bufferSize
|
|
|
|
|
fromZone: [self zone]];
|
|
|
|
|
}
|
|
|
|
|
|
1995-04-20 16:02:26 +00:00
|
|
|
|
- (id) initWithContentsOfFile: (NSString *)path
|
1995-04-09 02:20:37 +00:00
|
|
|
|
{
|
1998-10-20 14:40:05 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1995-04-20 16:02:26 +00:00
|
|
|
|
return nil;
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-20 16:02:26 +00:00
|
|
|
|
- (id) initWithContentsOfMappedFile: (NSString *)path;
|
1995-04-09 02:20:37 +00:00
|
|
|
|
{
|
1998-10-20 14:40:05 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
return nil;
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithData: (NSData*)data
|
|
|
|
|
{
|
2000-06-27 16:18:02 +00:00
|
|
|
|
return [self initWithBytes: [data bytes] length: [data length]];
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Accessing Data
|
|
|
|
|
|
|
|
|
|
- (const void*) bytes
|
|
|
|
|
{
|
1998-10-20 14:40:05 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1995-04-09 02:20:37 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSString*) description
|
|
|
|
|
{
|
1997-09-29 14:39:53 +00:00
|
|
|
|
NSString *str;
|
|
|
|
|
const char *src = [self bytes];
|
|
|
|
|
char *dest;
|
|
|
|
|
int length = [self length];
|
|
|
|
|
int i,j;
|
1998-10-15 05:03:16 +00:00
|
|
|
|
NSZone *z = [self zone];
|
1996-05-13 16:43:29 +00:00
|
|
|
|
|
|
|
|
|
#define num2char(num) ((num) < 0xa ? ((num)+'0') : ((num)+0x57))
|
|
|
|
|
|
|
|
|
|
/* we can just build a cString and convert it to an NSString */
|
1999-09-29 20:15:17 +00:00
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
dest = (char*) NSZoneMalloc(GSAtomicMallocZone(), 2*length+length/4+3);
|
|
|
|
|
#else
|
1998-10-15 05:03:16 +00:00
|
|
|
|
dest = (char*) NSZoneMalloc(z, 2*length+length/4+3);
|
1999-09-29 20:15:17 +00:00
|
|
|
|
#endif
|
1997-09-29 14:39:53 +00:00
|
|
|
|
if (dest == 0)
|
1998-10-20 14:40:05 +00:00
|
|
|
|
[NSException raise: NSMallocException
|
|
|
|
|
format: @"No memory for description of NSData object"];
|
1996-05-13 16:43:29 +00:00
|
|
|
|
dest[0] = '<';
|
|
|
|
|
for (i=0,j=1; i<length; i++,j++)
|
|
|
|
|
{
|
|
|
|
|
dest[j++] = num2char((src[i]>>4) & 0x0f);
|
|
|
|
|
dest[j] = num2char(src[i] & 0x0f);
|
1998-10-20 14:40:05 +00:00
|
|
|
|
if ((i&0x3) == 3 && i != length-1)
|
1996-05-13 16:43:29 +00:00
|
|
|
|
/* if we've just finished a 32-bit int, print a space */
|
|
|
|
|
dest[++j] = ' ';
|
|
|
|
|
}
|
|
|
|
|
dest[j++] = '>';
|
|
|
|
|
dest[j] = '\0';
|
1999-09-29 20:15:17 +00:00
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
str = [[NSString allocWithZone: z]
|
|
|
|
|
initWithCStringNoCopy: dest length: j fromZone: GSAtomicMallocZone()];
|
|
|
|
|
#else
|
1999-07-03 19:59:44 +00:00
|
|
|
|
str = [[NSString allocWithZone: z] initWithCStringNoCopy: dest
|
|
|
|
|
length: j
|
|
|
|
|
fromZone: z];
|
1999-09-29 20:15:17 +00:00
|
|
|
|
#endif
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return AUTORELEASE(str);
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-06-27 16:18:02 +00:00
|
|
|
|
- (void) getBytes: (void*)buffer
|
1995-04-09 02:20:37 +00:00
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
[self getBytes: buffer range: NSMakeRange(0, [self length])];
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-06-27 16:18:02 +00:00
|
|
|
|
- (void) getBytes: (void*)buffer length: (unsigned)length
|
1995-04-09 02:20:37 +00:00
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
[self getBytes: buffer range: NSMakeRange(0, length)];
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-06-27 16:18:02 +00:00
|
|
|
|
- (void) getBytes: (void*)buffer range: (NSRange)aRange
|
1995-04-09 02:20:37 +00:00
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
|
unsigned size = [self length];
|
|
|
|
|
|
|
|
|
|
GS_RANGE_CHECK(aRange, size);
|
|
|
|
|
memcpy(buffer, [self bytes] + aRange.location, aRange.length);
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-10-28 14:37:53 +00:00
|
|
|
|
- (id) replacementObjectForPortCoder: (NSPortCoder*)aCoder
|
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
return self;
|
1997-10-28 14:37:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-09 02:20:37 +00:00
|
|
|
|
- (NSData*) subdataWithRange: (NSRange)aRange
|
|
|
|
|
{
|
1997-09-29 14:39:53 +00:00
|
|
|
|
void *buffer;
|
|
|
|
|
unsigned l = [self length];
|
|
|
|
|
|
1999-06-21 08:30:26 +00:00
|
|
|
|
GS_RANGE_CHECK(aRange, l);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
|
1999-09-29 20:15:17 +00:00
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
buffer = NSZoneMalloc(GSAtomicMallocZone(), aRange.length);
|
|
|
|
|
#else
|
1998-10-15 05:03:16 +00:00
|
|
|
|
buffer = NSZoneMalloc([self zone], aRange.length);
|
1999-09-29 20:15:17 +00:00
|
|
|
|
#endif
|
1997-09-29 14:39:53 +00:00
|
|
|
|
if (buffer == 0)
|
1998-10-20 14:40:05 +00:00
|
|
|
|
[NSException raise: NSMallocException
|
|
|
|
|
format: @"No memory for subdata of NSData object"];
|
|
|
|
|
[self getBytes: buffer range: aRange];
|
1995-04-20 16:02:26 +00:00
|
|
|
|
|
1998-10-20 14:40:05 +00:00
|
|
|
|
return [NSData dataWithBytesNoCopy: buffer length: aRange.length];
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (unsigned) hash
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
2000-03-16 12:41:01 +00:00
|
|
|
|
unsigned char buf[64];
|
|
|
|
|
unsigned l = [self length];
|
|
|
|
|
unsigned ret =0;
|
|
|
|
|
|
|
|
|
|
l = MIN(l,64);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* hash for empty data matches hash for empty string
|
|
|
|
|
*/
|
|
|
|
|
if (l == 0)
|
|
|
|
|
{
|
|
|
|
|
return 0xfffffffe;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[self getBytes: &buf range: NSMakeRange(0, l)];
|
|
|
|
|
|
|
|
|
|
while (l-- > 0)
|
|
|
|
|
{
|
|
|
|
|
ret = (ret << 5 ) + ret + buf[l];
|
|
|
|
|
}
|
|
|
|
|
// Again, match NSString
|
|
|
|
|
if (ret == 0)
|
|
|
|
|
{
|
|
|
|
|
ret = 0xffffffff;
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-13 15:03:10 +00:00
|
|
|
|
- (BOOL) isEqual: anObject
|
|
|
|
|
{
|
1998-10-20 14:40:05 +00:00
|
|
|
|
if ([anObject isKindOfClass: [NSData class]])
|
|
|
|
|
return [self isEqualToData: anObject];
|
1995-04-13 15:03:10 +00:00
|
|
|
|
return NO;
|
|
|
|
|
}
|
1995-04-09 02:20:37 +00:00
|
|
|
|
|
|
|
|
|
// Querying a Data Object
|
|
|
|
|
- (BOOL) isEqualToData: (NSData*)other;
|
|
|
|
|
{
|
|
|
|
|
int len;
|
|
|
|
|
if ((len = [self length]) != [other length])
|
|
|
|
|
return NO;
|
|
|
|
|
return (memcmp([self bytes], [other bytes], len) ? NO : YES);
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-27 16:18:02 +00:00
|
|
|
|
- (unsigned) length;
|
1995-04-09 02:20:37 +00:00
|
|
|
|
{
|
1995-04-20 16:02:26 +00:00
|
|
|
|
/* This is left to concrete subclasses to implement. */
|
1998-10-20 14:40:05 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1995-04-09 02:20:37 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Storing Data
|
|
|
|
|
|
2000-06-27 16:18:02 +00:00
|
|
|
|
- (BOOL) writeToFile: (NSString*)path atomically: (BOOL)useAuxiliaryFile
|
1995-04-09 02:20:37 +00:00
|
|
|
|
{
|
2000-06-27 16:18:02 +00:00
|
|
|
|
char thePath[BUFSIZ*2+8];
|
|
|
|
|
char theRealPath[BUFSIZ*2];
|
|
|
|
|
FILE *theFile;
|
|
|
|
|
int c;
|
1995-04-20 16:02:26 +00:00
|
|
|
|
|
1998-02-03 14:20:00 +00:00
|
|
|
|
if ([path getFileSystemRepresentation: theRealPath
|
|
|
|
|
maxLength: sizeof(theRealPath)-1] == NO)
|
|
|
|
|
{
|
1999-11-28 18:56:27 +00:00
|
|
|
|
NSDebugLog(@"Open (%s) attempt failed - bad path", theRealPath);
|
1998-02-03 14:20:00 +00:00
|
|
|
|
return NO;
|
|
|
|
|
}
|
1999-02-20 21:19:15 +00:00
|
|
|
|
|
2000-06-30 15:44:30 +00:00
|
|
|
|
#if defined(__MINGW__)
|
|
|
|
|
HANDLE fh;
|
|
|
|
|
DWORD wroteBytes;
|
|
|
|
|
|
|
|
|
|
if (useAuxiliaryFile)
|
|
|
|
|
{
|
|
|
|
|
path = [path stringByAppendingPathExtension: @"tmp"];
|
|
|
|
|
}
|
|
|
|
|
if ([path getFileSystemRepresentation: thePath
|
|
|
|
|
maxLength: sizeof(thePath)-1] == NO)
|
|
|
|
|
{
|
|
|
|
|
NSDebugLog(@"Open (%s) attempt failed - bad path", thePath);
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fh = CreateFile(thePath, GENERIC_WRITE, 0, 0, CREATE_ALWAYS,
|
|
|
|
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
|
if (fh == INVALID_HANDLE_VALUE)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Create (%s) attempt failed", thePath);
|
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!WriteFile(fh, [self bytes], [self length], &wroteBytes, 0))
|
|
|
|
|
{
|
|
|
|
|
CloseHandle(fh);
|
|
|
|
|
NSLog(@"Write (%s) attempt failed", thePath);
|
|
|
|
|
goto failure;
|
|
|
|
|
}
|
|
|
|
|
CloseHandle(fh);
|
|
|
|
|
#else
|
|
|
|
|
|
1999-02-01 14:00:17 +00:00
|
|
|
|
#ifdef HAVE_MKSTEMP
|
|
|
|
|
if (useAuxiliaryFile)
|
|
|
|
|
{
|
|
|
|
|
int desc;
|
1995-04-20 16:02:26 +00:00
|
|
|
|
|
1999-02-01 14:00:17 +00:00
|
|
|
|
strcpy(thePath, theRealPath);
|
|
|
|
|
strcat(thePath, "XXXXXX");
|
1999-02-01 15:24:53 +00:00
|
|
|
|
if ((desc = mkstemp(thePath)) < 0)
|
1999-02-01 14:00:17 +00:00
|
|
|
|
{
|
|
|
|
|
NSLog(@"mkstemp (%s) failed - %s", thePath, strerror(errno));
|
|
|
|
|
goto failure;
|
|
|
|
|
}
|
|
|
|
|
if ((theFile = fdopen(desc, "w")) == 0)
|
|
|
|
|
{
|
|
|
|
|
close(desc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
strcpy(thePath, theRealPath);
|
1999-05-05 18:47:44 +00:00
|
|
|
|
theFile = fopen(thePath, "wb");
|
1999-02-01 14:00:17 +00:00
|
|
|
|
}
|
|
|
|
|
#else
|
1995-04-20 16:02:26 +00:00
|
|
|
|
if (useAuxiliaryFile)
|
|
|
|
|
{
|
1997-10-31 16:26:44 +00:00
|
|
|
|
/* Use the path name of the destination file as a prefix for the
|
|
|
|
|
* mktemp() call so that we can be sure that both files are on
|
|
|
|
|
* the same filesystem and the subsequent rename() will work. */
|
1998-02-03 14:20:00 +00:00
|
|
|
|
strcpy(thePath, theRealPath);
|
|
|
|
|
strcat(thePath, "XXXXXX");
|
|
|
|
|
if (mktemp(thePath) == 0)
|
1997-10-31 16:26:44 +00:00
|
|
|
|
{
|
1998-02-03 14:20:00 +00:00
|
|
|
|
NSLog(@"mktemp (%s) failed - %s", thePath, strerror(errno));
|
1997-10-31 16:26:44 +00:00
|
|
|
|
goto failure;
|
|
|
|
|
}
|
1995-04-20 16:02:26 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
1998-02-03 14:20:00 +00:00
|
|
|
|
strcpy(thePath, theRealPath);
|
1995-04-20 16:02:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Open the file (whether temp or real) for writing. */
|
1999-05-05 18:47:44 +00:00
|
|
|
|
theFile = fopen(thePath, "wb");
|
1999-02-01 14:00:17 +00:00
|
|
|
|
#endif
|
1995-04-20 16:02:26 +00:00
|
|
|
|
|
|
|
|
|
if (theFile == NULL) /* Something went wrong; we weren't
|
|
|
|
|
* even able to open the file. */
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1998-02-03 14:20:00 +00:00
|
|
|
|
NSLog(@"Open (%s) failed - %s", thePath, strerror(errno));
|
1997-09-29 14:39:53 +00:00
|
|
|
|
goto failure;
|
|
|
|
|
}
|
1995-04-20 16:02:26 +00:00
|
|
|
|
|
|
|
|
|
/* Now we try and write the NSData's bytes to the file. Here `c' is
|
|
|
|
|
* the number of bytes which were successfully written to the file
|
|
|
|
|
* in the fwrite() call. */
|
|
|
|
|
c = fwrite([self bytes], sizeof(char), [self length], theFile);
|
|
|
|
|
|
|
|
|
|
if (c < [self length]) /* We failed to write everything for
|
|
|
|
|
* some reason. */
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1998-02-03 14:20:00 +00:00
|
|
|
|
NSLog(@"Fwrite (%s) failed - %s", thePath, strerror(errno));
|
1997-09-29 14:39:53 +00:00
|
|
|
|
goto failure;
|
|
|
|
|
}
|
1995-04-20 16:02:26 +00:00
|
|
|
|
|
|
|
|
|
/* We're done, so close everything up. */
|
|
|
|
|
c = fclose(theFile);
|
|
|
|
|
|
|
|
|
|
if (c != 0) /* I can't imagine what went wrong
|
|
|
|
|
* closing the file, but we got here,
|
|
|
|
|
* so we need to deal with it. */
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1998-02-03 14:20:00 +00:00
|
|
|
|
NSLog(@"Fclose (%s) failed - %s", thePath, strerror(errno));
|
1997-09-29 14:39:53 +00:00
|
|
|
|
goto failure;
|
|
|
|
|
}
|
2000-06-30 15:44:30 +00:00
|
|
|
|
#endif
|
1995-04-20 16:02:26 +00:00
|
|
|
|
|
|
|
|
|
/* If we used a temporary file, we still need to rename() it be the
|
1999-02-20 21:19:15 +00:00
|
|
|
|
* real file. Also, we need to try to retain the file attributes of
|
|
|
|
|
* the original file we are overwriting (if we are) */
|
1995-04-20 16:02:26 +00:00
|
|
|
|
if (useAuxiliaryFile)
|
|
|
|
|
{
|
1999-02-20 21:19:15 +00:00
|
|
|
|
NSFileManager *mgr = [NSFileManager defaultManager];
|
|
|
|
|
NSMutableDictionary *att = nil;
|
|
|
|
|
|
|
|
|
|
if ([mgr fileExistsAtPath: path])
|
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
att = [[mgr fileAttributesAtPath: path
|
|
|
|
|
traverseLink: YES] mutableCopy];
|
1999-09-28 19:35:09 +00:00
|
|
|
|
IF_NO_GC(TEST_AUTORELEASE(att));
|
1999-02-20 21:19:15 +00:00
|
|
|
|
}
|
1995-04-20 16:02:26 +00:00
|
|
|
|
|
1999-02-20 21:19:15 +00:00
|
|
|
|
c = rename(thePath, theRealPath);
|
|
|
|
|
if (c != 0) /* Many things could go wrong, I guess. */
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
2000-02-19 00:40:47 +00:00
|
|
|
|
NSLog(@"Rename ('%s' to '%s') failed - %s",
|
|
|
|
|
thePath, theRealPath, strerror(errno));
|
1997-09-29 14:39:53 +00:00
|
|
|
|
goto failure;
|
|
|
|
|
}
|
1999-02-20 21:19:15 +00:00
|
|
|
|
|
|
|
|
|
if (att)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* We have created a new file - so we attempt to make it's
|
|
|
|
|
* attributes match that of the original.
|
|
|
|
|
*/
|
|
|
|
|
[att removeObjectForKey: NSFileSize];
|
|
|
|
|
[att removeObjectForKey: NSFileModificationDate];
|
|
|
|
|
[att removeObjectForKey: NSFileReferenceCount];
|
|
|
|
|
[att removeObjectForKey: NSFileSystemNumber];
|
|
|
|
|
[att removeObjectForKey: NSFileSystemFileNumber];
|
|
|
|
|
[att removeObjectForKey: NSFileDeviceIdentifier];
|
|
|
|
|
[att removeObjectForKey: NSFileType];
|
|
|
|
|
if ([mgr changeFileAttributes: att atPath: path] == NO)
|
|
|
|
|
NSLog(@"Unable to correctly set all attributes for '%@'", path);
|
|
|
|
|
}
|
1999-02-21 20:28:19 +00:00
|
|
|
|
else if (geteuid() == 0 && [@"root" isEqualToString: NSUserName()] == NO)
|
|
|
|
|
{
|
|
|
|
|
att = [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
|
NSFileOwnerAccountName, NSUserName(), nil];
|
|
|
|
|
if ([mgr changeFileAttributes: att atPath: path] == NO)
|
|
|
|
|
NSLog(@"Unable to correctly set ownership for '%@'", path);
|
|
|
|
|
}
|
1995-04-20 16:02:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* success: */
|
|
|
|
|
return YES;
|
|
|
|
|
|
|
|
|
|
/* Just in case the failure action needs to be changed. */
|
2000-06-30 15:44:30 +00:00
|
|
|
|
failure:
|
2000-02-19 00:40:47 +00:00
|
|
|
|
/*
|
|
|
|
|
* Attempt to tidy up by removing temporary file on failure.
|
|
|
|
|
*/
|
|
|
|
|
if (useAuxiliaryFile)
|
|
|
|
|
{
|
|
|
|
|
unlink(thePath);
|
|
|
|
|
}
|
1995-04-09 02:20:37 +00:00
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
|
1995-04-09 02:20:37 +00:00
|
|
|
|
// Deserializing Data
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (unsigned) deserializeAlignedBytesLengthAtCursor: (unsigned int*)cursor
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
return (unsigned)[self deserializeIntAtCursor: cursor];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-20 14:40:05 +00:00
|
|
|
|
- (void) deserializeBytes: (void*)buffer
|
1998-10-21 11:56:58 +00:00
|
|
|
|
length: (unsigned)bytes
|
|
|
|
|
atCursor: (unsigned*)cursor
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
NSRange range = { *cursor, bytes };
|
1998-10-20 14:40:05 +00:00
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
[self getBytes: buffer range: range];
|
|
|
|
|
*cursor += bytes;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
- (void) deserializeDataAt: (void*)data
|
|
|
|
|
ofObjCType: (const char*)type
|
|
|
|
|
atCursor: (unsigned*)cursor
|
|
|
|
|
context: (id <NSObjCTypeSerializationCallBack>)callback
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
if (!type || !data)
|
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
switch (*type)
|
|
|
|
|
{
|
|
|
|
|
case _C_ID:
|
|
|
|
|
{
|
|
|
|
|
[callback deserializeObjectAt: data
|
|
|
|
|
ofObjCType: type
|
|
|
|
|
fromData: self
|
|
|
|
|
atCursor: cursor];
|
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
case _C_CHARPTR:
|
|
|
|
|
{
|
1999-02-01 12:05:15 +00:00
|
|
|
|
gss32 length;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
[self deserializeBytes: &length
|
|
|
|
|
length: sizeof(length)
|
|
|
|
|
atCursor: cursor];
|
1999-02-08 10:46:32 +00:00
|
|
|
|
length = GSSwapBigI32ToHost(length);
|
1999-01-27 12:49:49 +00:00
|
|
|
|
if (length == -1)
|
|
|
|
|
{
|
|
|
|
|
*(const char**)data = NULL;
|
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
unsigned len = (length+1)*sizeof(char);
|
2000-07-02 18:57:05 +00:00
|
|
|
|
|
|
|
|
|
#if GS_WITH_GC == 0
|
|
|
|
|
*(char**)data = (char*)NSZoneMalloc(NSDefaultMallocZone(), len);
|
|
|
|
|
#else
|
|
|
|
|
*(char**)data = (char*)NSZoneMalloc(NSAtomicMallocZone(), len);
|
|
|
|
|
#endif
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
[self deserializeBytes: *(char**)data
|
|
|
|
|
length: length
|
|
|
|
|
atCursor: cursor];
|
|
|
|
|
(*(char**)data)[length] = '\0';
|
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
case _C_ARY_B:
|
|
|
|
|
{
|
|
|
|
|
unsigned offset = 0;
|
|
|
|
|
unsigned size;
|
|
|
|
|
unsigned count = atoi(++type);
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
while (isdigit(*type))
|
|
|
|
|
{
|
|
|
|
|
type++;
|
1998-10-20 14:40:05 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
size = objc_sizeof_type(type);
|
1998-10-20 14:40:05 +00:00
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
|
{
|
|
|
|
|
[self deserializeDataAt: (char*)data + offset
|
|
|
|
|
ofObjCType: type
|
|
|
|
|
atCursor: cursor
|
|
|
|
|
context: callback];
|
|
|
|
|
offset += size;
|
1998-10-20 14:40:05 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
case _C_STRUCT_B:
|
|
|
|
|
{
|
|
|
|
|
int offset = 0;
|
1998-10-20 14:40:05 +00:00
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
while (*type != _C_STRUCT_E && *type++ != '='); /* skip "<name>=" */
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
[self deserializeDataAt: ((char*)data) + offset
|
|
|
|
|
ofObjCType: type
|
|
|
|
|
atCursor: cursor
|
|
|
|
|
context: callback];
|
|
|
|
|
offset += objc_sizeof_type(type);
|
|
|
|
|
type = objc_skip_typespec(type);
|
|
|
|
|
if (*type != _C_STRUCT_E)
|
|
|
|
|
{
|
|
|
|
|
int align = objc_alignof_type(type);
|
|
|
|
|
int rem = offset % align;
|
|
|
|
|
|
|
|
|
|
if (rem != 0)
|
|
|
|
|
{
|
|
|
|
|
offset += align - rem;
|
1998-10-20 14:40:05 +00:00
|
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
else break;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
case _C_PTR:
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
unsigned len = objc_sizeof_type(++type);
|
2000-07-02 18:57:05 +00:00
|
|
|
|
|
|
|
|
|
#if GS_WITH_GC == 0
|
|
|
|
|
*(char**)data = (char*)NSZoneMalloc(NSDefaultMallocZone(), len);
|
|
|
|
|
#else
|
|
|
|
|
*(char**)data = (char*)NSZoneMalloc(NSAtomicMallocZone(), len);
|
|
|
|
|
#endif
|
1999-01-27 12:49:49 +00:00
|
|
|
|
[self deserializeDataAt: *(char**)data
|
|
|
|
|
ofObjCType: type
|
|
|
|
|
atCursor: cursor
|
|
|
|
|
context: callback];
|
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
case _C_CHR:
|
|
|
|
|
case _C_UCHR:
|
|
|
|
|
{
|
|
|
|
|
[self deserializeBytes: data
|
|
|
|
|
length: sizeof(unsigned char)
|
|
|
|
|
atCursor: cursor];
|
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
case _C_SHT:
|
|
|
|
|
case _C_USHT:
|
|
|
|
|
{
|
|
|
|
|
unsigned short ns;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
[self deserializeBytes: &ns
|
|
|
|
|
length: sizeof(unsigned short)
|
|
|
|
|
atCursor: cursor];
|
|
|
|
|
*(unsigned short*)data = NSSwapBigShortToHost(ns);
|
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
case _C_INT:
|
|
|
|
|
case _C_UINT:
|
|
|
|
|
{
|
|
|
|
|
unsigned ni;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
[self deserializeBytes: &ni
|
|
|
|
|
length: sizeof(unsigned)
|
|
|
|
|
atCursor: cursor];
|
|
|
|
|
*(unsigned*)data = NSSwapBigIntToHost(ni);
|
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
case _C_LNG:
|
|
|
|
|
case _C_ULNG:
|
|
|
|
|
{
|
|
|
|
|
unsigned long nl;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
[self deserializeBytes: &nl
|
|
|
|
|
length: sizeof(unsigned long)
|
|
|
|
|
atCursor: cursor];
|
|
|
|
|
*(unsigned long*)data = NSSwapBigLongToHost(nl);
|
|
|
|
|
return;
|
1998-10-20 14:40:05 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
case _C_LNG_LNG:
|
|
|
|
|
case _C_ULNG_LNG:
|
|
|
|
|
{
|
|
|
|
|
unsigned long long nl;
|
1998-10-20 14:40:05 +00:00
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
[self deserializeBytes: &nl
|
|
|
|
|
length: sizeof(unsigned long long)
|
|
|
|
|
atCursor: cursor];
|
|
|
|
|
*(unsigned long long*)data = NSSwapBigLongLongToHost(nl);
|
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
case _C_FLT:
|
|
|
|
|
{
|
|
|
|
|
NSSwappedFloat nf;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
[self deserializeBytes: &nf
|
|
|
|
|
length: sizeof(NSSwappedFloat)
|
|
|
|
|
atCursor: cursor];
|
|
|
|
|
*(float*)data = NSSwapBigFloatToHost(nf);
|
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
case _C_DBL:
|
|
|
|
|
{
|
|
|
|
|
NSSwappedDouble nd;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
[self deserializeBytes: &nd
|
|
|
|
|
length: sizeof(NSSwappedDouble)
|
|
|
|
|
atCursor: cursor];
|
|
|
|
|
*(double*)data = NSSwapBigDoubleToHost(nd);
|
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
case _C_CLASS:
|
|
|
|
|
{
|
1999-02-01 12:05:15 +00:00
|
|
|
|
gsu16 ni;
|
1999-01-27 12:49:49 +00:00
|
|
|
|
|
|
|
|
|
[self deserializeBytes: &ni
|
1999-02-01 12:05:15 +00:00
|
|
|
|
length: sizeof(ni)
|
1999-01-27 12:49:49 +00:00
|
|
|
|
atCursor: cursor];
|
1999-02-08 10:46:32 +00:00
|
|
|
|
ni = GSSwapBigI16ToHost(ni);
|
1999-01-27 12:49:49 +00:00
|
|
|
|
if (ni == 0)
|
|
|
|
|
{
|
|
|
|
|
*(Class*)data = 0;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
char name[ni+1];
|
|
|
|
|
Class c;
|
|
|
|
|
|
|
|
|
|
[self deserializeBytes: name
|
|
|
|
|
length: ni
|
|
|
|
|
atCursor: cursor];
|
|
|
|
|
name[ni] = '\0';
|
|
|
|
|
c = objc_get_class(name);
|
|
|
|
|
if (c == 0)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"can't find class - %s", name];
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
*(Class*)data = c;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
case _C_SEL:
|
|
|
|
|
{
|
1999-02-01 12:05:15 +00:00
|
|
|
|
gsu16 ln;
|
|
|
|
|
gsu16 lt;
|
1999-01-27 12:49:49 +00:00
|
|
|
|
|
|
|
|
|
[self deserializeBytes: &ln
|
1999-02-01 12:05:15 +00:00
|
|
|
|
length: sizeof(ln)
|
1999-01-27 12:49:49 +00:00
|
|
|
|
atCursor: cursor];
|
1999-02-08 10:46:32 +00:00
|
|
|
|
ln = GSSwapBigI16ToHost(ln);
|
1999-01-27 12:49:49 +00:00
|
|
|
|
[self deserializeBytes: <
|
1999-02-01 12:05:15 +00:00
|
|
|
|
length: sizeof(lt)
|
1999-01-27 12:49:49 +00:00
|
|
|
|
atCursor: cursor];
|
1999-02-08 10:46:32 +00:00
|
|
|
|
lt = GSSwapBigI16ToHost(lt);
|
1999-01-27 12:49:49 +00:00
|
|
|
|
if (ln == 0)
|
|
|
|
|
{
|
|
|
|
|
*(SEL*)data = 0;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
char name[ln+1];
|
|
|
|
|
char types[lt+1];
|
|
|
|
|
SEL sel;
|
|
|
|
|
|
|
|
|
|
[self deserializeBytes: name
|
|
|
|
|
length: ln
|
|
|
|
|
atCursor: cursor];
|
|
|
|
|
name[ln] = '\0';
|
|
|
|
|
[self deserializeBytes: types
|
|
|
|
|
length: lt
|
|
|
|
|
atCursor: cursor];
|
|
|
|
|
types[lt] = '\0';
|
|
|
|
|
|
|
|
|
|
if (lt)
|
|
|
|
|
{
|
|
|
|
|
sel = sel_get_typed_uid(name, types);
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sel = sel_get_any_typed_uid(name);
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
if (sel == 0)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"can't find sel with name '%s' "
|
|
|
|
|
@"and types '%s'", name, types];
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
*(SEL*)data = sel;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
default:
|
|
|
|
|
[NSException raise: NSGenericException
|
|
|
|
|
format: @"Unknown type to deserialize - '%s'", type];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
- (int) deserializeIntAtCursor: (unsigned*)cursor
|
1995-04-09 02:20:37 +00:00
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
unsigned ni, result;
|
1995-04-09 02:20:37 +00:00
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
[self deserializeBytes: &ni length: sizeof(unsigned) atCursor: cursor];
|
|
|
|
|
result = NSSwapBigIntToHost(ni);
|
|
|
|
|
return result;
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
- (int) deserializeIntAtIndex: (unsigned)index
|
1995-04-09 02:20:37 +00:00
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
unsigned ni, result;
|
1995-04-09 02:20:37 +00:00
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
[self deserializeBytes: &ni length: sizeof(unsigned) atCursor: &index];
|
|
|
|
|
result = NSSwapBigIntToHost(ni);
|
|
|
|
|
return result;
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
- (void) deserializeInts: (int*)intBuffer
|
|
|
|
|
count: (unsigned)numInts
|
|
|
|
|
atCursor: (unsigned*)cursor
|
1995-04-09 02:20:37 +00:00
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
unsigned i;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
[self deserializeBytes: &intBuffer
|
|
|
|
|
length: numInts * sizeof(unsigned)
|
|
|
|
|
atCursor: cursor];
|
|
|
|
|
for (i = 0; i < numInts; i++)
|
|
|
|
|
intBuffer[i] = NSSwapBigIntToHost(intBuffer[i]);
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
- (void) deserializeInts: (int*)intBuffer
|
|
|
|
|
count: (unsigned)numInts
|
|
|
|
|
atIndex: (unsigned)index
|
1995-04-09 02:20:37 +00:00
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
unsigned i;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
[self deserializeBytes: &intBuffer
|
|
|
|
|
length: numInts * sizeof(int)
|
|
|
|
|
atCursor: &index];
|
|
|
|
|
for (i = 0; i < numInts; i++)
|
|
|
|
|
{
|
|
|
|
|
intBuffer[i] = NSSwapBigIntToHost(intBuffer[i]);
|
1998-10-20 14:40:05 +00:00
|
|
|
|
}
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-11-12 10:58:17 +00:00
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
1995-04-09 02:20:37 +00:00
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
if (NSShouldRetainWithZone(self, z) &&
|
1998-03-12 14:21:20 +00:00
|
|
|
|
[self isKindOfClass: [NSMutableData class]] == NO)
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return RETAIN(self);
|
1999-01-27 12:49:49 +00:00
|
|
|
|
else
|
|
|
|
|
return [[dataMalloc allocWithZone: z]
|
|
|
|
|
initWithBytes: [self bytes] length: [self length]];
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) mutableCopyWithZone: (NSZone*)zone
|
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
return [[mutableDataMalloc allocWithZone: zone]
|
1998-03-12 14:21:20 +00:00
|
|
|
|
initWithBytes: [self bytes] length: [self length]];
|
1995-04-09 02:20:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-20 14:40:05 +00:00
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)coder
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
2000-06-27 16:18:02 +00:00
|
|
|
|
[coder encodeDataObject: self];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-20 14:40:05 +00:00
|
|
|
|
- (id) initWithCoder: (NSCoder*)coder
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
2000-06-27 16:18:02 +00:00
|
|
|
|
id obj = [coder decodeDataObject];
|
|
|
|
|
|
|
|
|
|
if (obj != self)
|
|
|
|
|
{
|
|
|
|
|
ASSIGN(self, obj);
|
|
|
|
|
}
|
|
|
|
|
return self;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1995-04-09 02:20:37 +00:00
|
|
|
|
@end
|
1995-04-17 20:40:59 +00:00
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
@implementation NSData (GNUstepExtensions)
|
|
|
|
|
+ (id) dataWithShmID: (int)anID length: (unsigned)length
|
|
|
|
|
{
|
|
|
|
|
#if HAVE_SHMCTL
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSDataShared *d;
|
|
|
|
|
|
|
|
|
|
d = [NSDataShared allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithShmID: anID length: length];
|
|
|
|
|
return AUTORELEASE(d);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
#else
|
|
|
|
|
NSLog(@"[NSData -dataWithSmdID:length:] no shared memory support");
|
|
|
|
|
return nil;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (id) dataWithSharedBytes: (const void*)bytes length: (unsigned)length
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSData *d;
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
#if HAVE_SHMCTL
|
1999-07-03 19:59:44 +00:00
|
|
|
|
d = [NSDataShared allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithBytes: bytes length: length];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
#else
|
1999-07-03 19:59:44 +00:00
|
|
|
|
d = [dataMalloc allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithBytes: bytes length: length];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
#endif
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return AUTORELEASE(d);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
1995-04-17 20:40:59 +00:00
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
+ (id) dataWithStaticBytes: (const void*)bytes length: (unsigned)length
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSDataStatic *d;
|
|
|
|
|
|
|
|
|
|
d = [NSDataStatic allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithBytesNoCopy: (void*)bytes length: length];
|
|
|
|
|
return AUTORELEASE(d);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
1998-02-05 22:06:20 +00:00
|
|
|
|
|
1998-10-15 05:03:16 +00:00
|
|
|
|
- (id) initWithBytesNoCopy: (void*)bytes
|
|
|
|
|
length: (unsigned)length
|
|
|
|
|
fromZone: (NSZone*)zone
|
|
|
|
|
{
|
1998-10-20 14:40:05 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1999-06-24 19:30:29 +00:00
|
|
|
|
return nil;
|
1998-10-15 05:03:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
- (void) deserializeTypeTag: (unsigned char*)tag
|
|
|
|
|
andCrossRef: (unsigned int*)ref
|
|
|
|
|
atCursor: (unsigned*)cursor
|
1998-10-23 15:48:21 +00:00
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
[self deserializeDataAt: (void*)tag
|
|
|
|
|
ofObjCType: @encode(gsu8)
|
|
|
|
|
atCursor: cursor
|
|
|
|
|
context: nil];
|
|
|
|
|
if (*tag & _GSC_MAYX)
|
|
|
|
|
{
|
|
|
|
|
switch (*tag & _GSC_SIZE)
|
|
|
|
|
{
|
|
|
|
|
case _GSC_X_0:
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case _GSC_X_1:
|
|
|
|
|
{
|
|
|
|
|
gsu8 x;
|
|
|
|
|
|
|
|
|
|
[self deserializeDataAt: (void*)&x
|
|
|
|
|
ofObjCType: @encode(gsu8)
|
|
|
|
|
atCursor: cursor
|
|
|
|
|
context: nil];
|
|
|
|
|
*ref = (unsigned int)x;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case _GSC_X_2:
|
|
|
|
|
{
|
|
|
|
|
gsu16 x;
|
|
|
|
|
|
|
|
|
|
[self deserializeDataAt: (void*)&x
|
|
|
|
|
ofObjCType: @encode(gsu16)
|
|
|
|
|
atCursor: cursor
|
|
|
|
|
context: nil];
|
|
|
|
|
*ref = (unsigned int)x;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
gsu32 x;
|
|
|
|
|
|
|
|
|
|
[self deserializeDataAt: (void*)&x
|
|
|
|
|
ofObjCType: @encode(gsu32)
|
|
|
|
|
atCursor: cursor
|
|
|
|
|
context: nil];
|
|
|
|
|
*ref = (unsigned int)x;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
1998-10-23 15:48:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-02-05 22:06:20 +00:00
|
|
|
|
- (void*) relinquishAllocatedBytes
|
1998-10-21 11:56:58 +00:00
|
|
|
|
{
|
|
|
|
|
return [self relinquishAllocatedBytesFromZone: 0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void*) relinquishAllocatedBytesFromZone: (NSZone*)aZone;
|
1998-02-05 22:06:20 +00:00
|
|
|
|
{
|
1998-10-15 05:03:16 +00:00
|
|
|
|
return 0; /* No data from NSZoneMalloc - return nul pointer */
|
1998-02-05 22:06:20 +00:00
|
|
|
|
}
|
1997-09-29 14:39:53 +00:00
|
|
|
|
@end
|
1995-04-17 20:40:59 +00:00
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
|
1995-04-17 20:40:59 +00:00
|
|
|
|
@implementation NSMutableData
|
2000-04-14 10:38:22 +00:00
|
|
|
|
+ (id) allocWithZone: (NSZone*)z
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1998-10-21 11:56:58 +00:00
|
|
|
|
return (NSData*)NSAllocateObject(mutableDataMalloc, 0, z);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-06-24 21:53:49 +00:00
|
|
|
|
+ (id) data
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSMutableData *d;
|
|
|
|
|
|
|
|
|
|
d = [mutableDataMalloc allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithCapacity: 0];
|
|
|
|
|
return AUTORELEASE(d);
|
1998-06-24 21:53:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
+ (id) dataWithBytes: (const void*)bytes
|
1998-10-21 11:56:58 +00:00
|
|
|
|
length: (unsigned)length
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSData *d;
|
|
|
|
|
|
|
|
|
|
d = [mutableDataMalloc allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithBytes: bytes length: length];
|
|
|
|
|
return AUTORELEASE(d);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
1995-04-17 20:40:59 +00:00
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
+ (id) dataWithBytesNoCopy: (void*)bytes
|
1998-10-21 11:56:58 +00:00
|
|
|
|
length: (unsigned)length
|
1995-08-23 15:36:59 +00:00
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSData *d;
|
|
|
|
|
|
|
|
|
|
d = [mutableDataMalloc allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithBytesNoCopy: bytes length: length];
|
|
|
|
|
return AUTORELEASE(d);
|
1995-08-23 15:36:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
+ (id) dataWithCapacity: (unsigned)numBytes
|
1995-04-17 20:40:59 +00:00
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSMutableData *d;
|
|
|
|
|
|
|
|
|
|
d = [mutableDataMalloc allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithCapacity: numBytes];
|
|
|
|
|
return AUTORELEASE(d);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (id) dataWithContentsOfFile: (NSString*)path
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSData *d;
|
|
|
|
|
|
|
|
|
|
d = [mutableDataMalloc allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithContentsOfFile: path];
|
|
|
|
|
return AUTORELEASE(d);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (id) dataWithContentsOfMappedFile: (NSString*)path
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSData *d;
|
|
|
|
|
|
|
|
|
|
d = [mutableDataMalloc allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithContentsOfMappedFile: path];
|
|
|
|
|
return AUTORELEASE(d);
|
1995-04-17 20:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-01-19 15:20:15 +00:00
|
|
|
|
+ (id) dataWithData: (NSData*)data
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSData *d;
|
|
|
|
|
|
|
|
|
|
d = [mutableDataMalloc allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithBytes: [data bytes] length: [data length]];
|
|
|
|
|
return AUTORELEASE(d);
|
1998-01-19 15:20:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
+ (id) dataWithLength: (unsigned)length
|
1995-04-17 20:40:59 +00:00
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSMutableData *d;
|
|
|
|
|
|
|
|
|
|
d = [mutableDataMalloc allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithLength: length];
|
|
|
|
|
return AUTORELEASE(d);
|
1995-04-17 20:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-07-03 11:47:17 +00:00
|
|
|
|
+ (id) new
|
|
|
|
|
{
|
|
|
|
|
NSMutableData *d;
|
|
|
|
|
|
|
|
|
|
d = [mutableDataMalloc allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithCapacity: 0];
|
|
|
|
|
return d;
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
- (const void*) bytes
|
1995-04-17 20:40:59 +00:00
|
|
|
|
{
|
1997-09-29 14:39:53 +00:00
|
|
|
|
return [self mutableBytes];
|
1995-04-17 20:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-06-27 16:18:02 +00:00
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
unsigned length = [self length];
|
|
|
|
|
void *bytes = [self mutableBytes];
|
|
|
|
|
|
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(unsigned long)
|
|
|
|
|
at: &length];
|
|
|
|
|
if (length)
|
|
|
|
|
{
|
|
|
|
|
[aCoder encodeArrayOfObjCType: @encode(unsigned char)
|
|
|
|
|
count: length
|
|
|
|
|
at: bytes];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (id) initWithCapacity: (unsigned)capacity
|
1995-04-17 20:40:59 +00:00
|
|
|
|
{
|
1998-10-20 14:40:05 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1995-04-17 20:40:59 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
2000-06-27 16:18:02 +00:00
|
|
|
|
- (id) initWithCoder: (NSCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
unsigned l;
|
|
|
|
|
void *b;
|
|
|
|
|
NSZone *zone;
|
|
|
|
|
|
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
zone = GSAtomicMallocZone();
|
|
|
|
|
#else
|
|
|
|
|
zone = [self zone];
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
[aCoder decodeValueOfObjCType: @encode(unsigned long) at: &l];
|
|
|
|
|
if (l)
|
|
|
|
|
{
|
|
|
|
|
b = NSZoneMalloc(zone, l);
|
|
|
|
|
if (b == 0)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"[NSDataMalloc -initWithCoder:] unable to get %lu bytes", l);
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
[aCoder decodeArrayOfObjCType: @encode(unsigned char) count: l at: b];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
b = 0;
|
|
|
|
|
}
|
|
|
|
|
return [self initWithBytesNoCopy: b length: l fromZone: zone];
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (id) initWithLength: (unsigned)length
|
1995-04-17 20:40:59 +00:00
|
|
|
|
{
|
1998-10-20 14:40:05 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
return nil;
|
1995-04-17 20:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Adjusting Capacity
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (void) increaseLengthBy: (unsigned)extraLength
|
1995-04-17 20:40:59 +00:00
|
|
|
|
{
|
1998-10-20 14:40:05 +00:00
|
|
|
|
[self setLength: [self length]+extraLength];
|
1995-04-17 20:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
- (void) setLength: (unsigned)size
|
1995-04-17 20:40:59 +00:00
|
|
|
|
{
|
1998-10-20 14:40:05 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1995-04-17 20:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void*) mutableBytes
|
|
|
|
|
{
|
1998-10-20 14:40:05 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
1995-04-17 20:40:59 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Appending Data
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
- (void) appendBytes: (const void*)aBuffer
|
1998-10-21 11:56:58 +00:00
|
|
|
|
length: (unsigned)bufferSize
|
1995-04-17 20:40:59 +00:00
|
|
|
|
{
|
1997-09-29 14:39:53 +00:00
|
|
|
|
unsigned oldLength = [self length];
|
|
|
|
|
void* buffer;
|
|
|
|
|
|
|
|
|
|
[self setLength: oldLength + bufferSize];
|
|
|
|
|
buffer = [self mutableBytes];
|
|
|
|
|
memcpy(buffer + oldLength, aBuffer, bufferSize);
|
1995-04-17 20:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) appendData: (NSData*)other
|
|
|
|
|
{
|
2000-06-27 16:18:02 +00:00
|
|
|
|
[self appendBytes: [other bytes] length: [other length]];
|
1995-04-17 20:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Modifying Data
|
|
|
|
|
|
|
|
|
|
- (void) replaceBytesInRange: (NSRange)aRange
|
|
|
|
|
withBytes: (const void*)bytes
|
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
|
unsigned size = [self length];
|
|
|
|
|
|
|
|
|
|
GS_RANGE_CHECK(aRange, size);
|
1995-04-17 21:31:59 +00:00
|
|
|
|
memcpy([self mutableBytes] + aRange.location, bytes, aRange.length);
|
1995-04-17 20:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) resetBytesInRange: (NSRange)aRange
|
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
|
unsigned size = [self length];
|
|
|
|
|
|
|
|
|
|
GS_RANGE_CHECK(aRange, size);
|
1996-03-26 00:28:20 +00:00
|
|
|
|
memset((char*)[self bytes] + aRange.location, 0, aRange.length);
|
1995-04-17 20:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-01-19 15:20:15 +00:00
|
|
|
|
- (void) setData: (NSData*)data
|
|
|
|
|
{
|
|
|
|
|
NSRange r = NSMakeRange(0, [data length]);
|
|
|
|
|
|
|
|
|
|
[self setCapacity: [data length]];
|
|
|
|
|
[self replaceBytesInRange: r withBytes: [data bytes]];
|
|
|
|
|
}
|
|
|
|
|
|
1995-04-17 20:40:59 +00:00
|
|
|
|
// Serializing Data
|
|
|
|
|
|
2000-06-27 16:18:02 +00:00
|
|
|
|
- (void) serializeAlignedBytesLength: (unsigned)length
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1999-02-01 12:05:15 +00:00
|
|
|
|
[self serializeInt: length];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-06-27 16:18:02 +00:00
|
|
|
|
- (void) serializeDataAt: (const void*)data
|
|
|
|
|
ofObjCType: (const char*)type
|
|
|
|
|
context: (id <NSObjCTypeSerializationCallBack>)callback
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1999-02-01 12:05:15 +00:00
|
|
|
|
if (!data || !type)
|
|
|
|
|
return;
|
1998-10-20 14:40:05 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
switch (*type)
|
|
|
|
|
{
|
|
|
|
|
case _C_ID:
|
|
|
|
|
[callback serializeObjectAt: (id*)data
|
|
|
|
|
ofObjCType: type
|
|
|
|
|
intoData: self];
|
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_CHARPTR:
|
|
|
|
|
{
|
1999-02-08 10:46:32 +00:00
|
|
|
|
gsu32 len;
|
|
|
|
|
gsu32 ni;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
if (!*(void**)data)
|
|
|
|
|
{
|
1999-02-08 10:46:32 +00:00
|
|
|
|
ni = (gsu32)-1;
|
|
|
|
|
ni = GSSwapHostI32ToBig(ni);
|
1999-02-01 12:05:15 +00:00
|
|
|
|
[self appendBytes: (void*)&ni length: sizeof(ni)];
|
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-02-08 10:46:32 +00:00
|
|
|
|
len = (gsu32)strlen(*(void**)data);
|
|
|
|
|
ni = GSSwapHostI32ToBig(len);
|
1999-02-01 12:05:15 +00:00
|
|
|
|
[self appendBytes: (void*)&ni length: sizeof(ni)];
|
|
|
|
|
[self appendBytes: *(void**)data length: len];
|
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_ARY_B:
|
|
|
|
|
{
|
|
|
|
|
unsigned offset = 0;
|
|
|
|
|
unsigned size;
|
|
|
|
|
unsigned count = atoi(++type);
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
while (isdigit(*type))
|
|
|
|
|
{
|
|
|
|
|
type++;
|
1998-10-20 14:40:05 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
size = objc_sizeof_type(type);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
|
{
|
|
|
|
|
[self serializeDataAt: (char*)data + offset
|
|
|
|
|
ofObjCType: type
|
|
|
|
|
context: callback];
|
|
|
|
|
offset += size;
|
1998-10-20 14:40:05 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_STRUCT_B:
|
|
|
|
|
{
|
|
|
|
|
int offset = 0;
|
|
|
|
|
int align, rem;
|
|
|
|
|
|
|
|
|
|
while (*type != _C_STRUCT_E && *type++ != '='); /* skip "<name>=" */
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
[self serializeDataAt: ((char*)data) + offset
|
|
|
|
|
ofObjCType: type
|
|
|
|
|
context: callback];
|
|
|
|
|
offset += objc_sizeof_type(type);
|
|
|
|
|
type = objc_skip_typespec(type);
|
|
|
|
|
if (*type != _C_STRUCT_E)
|
|
|
|
|
{
|
|
|
|
|
align = objc_alignof_type(type);
|
|
|
|
|
if ((rem = offset % align))
|
|
|
|
|
offset += align - rem;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
else break;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_PTR:
|
|
|
|
|
[self serializeDataAt: *(char**)data
|
|
|
|
|
ofObjCType: ++type
|
|
|
|
|
context: callback];
|
|
|
|
|
return;
|
|
|
|
|
case _C_CHR:
|
|
|
|
|
case _C_UCHR:
|
|
|
|
|
[self appendBytes: data length: sizeof(unsigned char)];
|
|
|
|
|
return;
|
|
|
|
|
case _C_SHT:
|
|
|
|
|
case _C_USHT:
|
|
|
|
|
{
|
|
|
|
|
unsigned short ns = NSSwapHostShortToBig(*(unsigned short*)data);
|
|
|
|
|
[self appendBytes: &ns length: sizeof(unsigned short)];
|
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_INT:
|
|
|
|
|
case _C_UINT:
|
|
|
|
|
{
|
|
|
|
|
unsigned ni = NSSwapHostIntToBig(*(unsigned int*)data);
|
|
|
|
|
[self appendBytes: &ni length: sizeof(unsigned)];
|
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_LNG:
|
|
|
|
|
case _C_ULNG:
|
|
|
|
|
{
|
|
|
|
|
unsigned long nl = NSSwapHostLongToBig(*(unsigned long*)data);
|
|
|
|
|
[self appendBytes: &nl length: sizeof(unsigned long)];
|
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_LNG_LNG:
|
|
|
|
|
case _C_ULNG_LNG:
|
|
|
|
|
{
|
|
|
|
|
unsigned long long nl;
|
1998-10-20 14:40:05 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
nl = NSSwapHostLongLongToBig(*(unsigned long long*)data);
|
|
|
|
|
[self appendBytes: &nl length: sizeof(unsigned long long)];
|
|
|
|
|
return;
|
1998-10-20 14:40:05 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_FLT:
|
|
|
|
|
{
|
|
|
|
|
NSSwappedFloat nf = NSSwapHostFloatToBig(*(float*)data);
|
|
|
|
|
|
|
|
|
|
[self appendBytes: &nf length: sizeof(NSSwappedFloat)];
|
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_DBL:
|
|
|
|
|
{
|
|
|
|
|
NSSwappedDouble nd = NSSwapHostDoubleToBig(*(double*)data);
|
|
|
|
|
|
|
|
|
|
[self appendBytes: &nd length: sizeof(NSSwappedDouble)];
|
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_CLASS:
|
|
|
|
|
{
|
|
|
|
|
const char *name = *(Class*)data?fastClassName(*(Class*)data):"";
|
|
|
|
|
gsu16 ln = (gsu16)strlen(name);
|
|
|
|
|
gsu16 ni;
|
|
|
|
|
|
1999-02-08 10:46:32 +00:00
|
|
|
|
ni = GSSwapHostI16ToBig(ln);
|
1999-02-01 12:05:15 +00:00
|
|
|
|
[self appendBytes: &ni length: sizeof(ni)];
|
|
|
|
|
if (ln)
|
|
|
|
|
{
|
|
|
|
|
[self appendBytes: name length: ln];
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_SEL:
|
|
|
|
|
{
|
|
|
|
|
const char *name = *(SEL*)data?fastSelectorName(*(SEL*)data):"";
|
2000-01-05 16:30:34 +00:00
|
|
|
|
gsu16 ln = (name == 0) ? 0 : (gsu16)strlen(name);
|
1999-02-01 12:05:15 +00:00
|
|
|
|
const char *types = *(SEL*)data?fastSelectorTypes(*(SEL*)data):"";
|
2000-01-05 16:30:34 +00:00
|
|
|
|
gsu16 lt = (types == 0) ? 0 : (gsu16)strlen(types);
|
1999-02-01 12:05:15 +00:00
|
|
|
|
gsu16 ni;
|
|
|
|
|
|
1999-02-08 10:46:32 +00:00
|
|
|
|
ni = GSSwapHostI16ToBig(ln);
|
1999-02-01 12:05:15 +00:00
|
|
|
|
[self appendBytes: &ni length: sizeof(ni)];
|
1999-02-08 10:46:32 +00:00
|
|
|
|
ni = GSSwapHostI16ToBig(lt);
|
1999-02-01 12:05:15 +00:00
|
|
|
|
[self appendBytes: &ni length: sizeof(ni)];
|
|
|
|
|
if (ln)
|
|
|
|
|
{
|
|
|
|
|
[self appendBytes: name length: ln];
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
if (lt)
|
|
|
|
|
{
|
|
|
|
|
[self appendBytes: types length: lt];
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
return;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
default:
|
|
|
|
|
[NSException raise: NSGenericException
|
|
|
|
|
format: @"Unknown type to serialize - '%s'", type];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1995-04-17 20:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-02-08 10:46:32 +00:00
|
|
|
|
- (void) serializeInt: (int)value
|
1995-04-17 20:40:59 +00:00
|
|
|
|
{
|
1999-02-01 12:05:15 +00:00
|
|
|
|
unsigned ni = NSSwapHostIntToBig(value);
|
|
|
|
|
[self appendBytes: &ni length: sizeof(unsigned)];
|
1995-04-17 20:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-02-08 10:46:32 +00:00
|
|
|
|
- (void) serializeInt: (int)value atIndex: (unsigned)index
|
1995-04-17 20:40:59 +00:00
|
|
|
|
{
|
1999-02-01 12:05:15 +00:00
|
|
|
|
unsigned ni = NSSwapHostIntToBig(value);
|
|
|
|
|
NSRange range = { index, sizeof(int) };
|
|
|
|
|
|
|
|
|
|
[self replaceBytesInRange: range withBytes: &ni];
|
1995-04-17 20:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (void) serializeInts: (int*)intBuffer
|
|
|
|
|
count: (unsigned)numInts
|
1995-04-17 20:40:59 +00:00
|
|
|
|
{
|
1999-02-01 12:05:15 +00:00
|
|
|
|
unsigned i;
|
|
|
|
|
SEL sel = @selector(serializeInt:);
|
|
|
|
|
IMP imp = [self methodForSelector: sel];
|
1995-04-17 20:40:59 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
for (i = 0; i < numInts; i++)
|
|
|
|
|
{
|
|
|
|
|
(*imp)(self, sel, intBuffer[i]);
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1995-04-17 20:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (void) serializeInts: (int*)intBuffer
|
|
|
|
|
count: (unsigned)numInts
|
|
|
|
|
atIndex: (unsigned)index
|
1995-04-17 20:40:59 +00:00
|
|
|
|
{
|
1999-02-01 12:05:15 +00:00
|
|
|
|
unsigned i;
|
|
|
|
|
SEL sel = @selector(serializeInt:atIndex:);
|
|
|
|
|
IMP imp = [self methodForSelector: sel];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
for (i = 0; i < numInts; i++)
|
|
|
|
|
{
|
|
|
|
|
(*imp)(self, sel, intBuffer[i], index++);
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1995-04-17 20:40:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
@implementation NSMutableData (GNUstepExtensions)
|
|
|
|
|
+ (id) dataWithShmID: (int)anID length: (unsigned)length
|
|
|
|
|
{
|
|
|
|
|
#if HAVE_SHMCTL
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSDataShared *d;
|
|
|
|
|
|
|
|
|
|
d = [NSMutableDataShared allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithShmID: anID length: length];
|
|
|
|
|
return AUTORELEASE(d);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
#else
|
|
|
|
|
NSLog(@"[NSMutableData -dataWithSmdID:length:] no shared memory support");
|
|
|
|
|
return nil;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (id) dataWithSharedBytes: (const void*)bytes length: (unsigned)length
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
NSData *d;
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
#if HAVE_SHMCTL
|
1999-07-03 19:59:44 +00:00
|
|
|
|
d = [NSMutableDataShared allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithBytes: bytes length: length];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
#else
|
1999-07-03 19:59:44 +00:00
|
|
|
|
d = [mutableDataMalloc allocWithZone: NSDefaultMallocZone()];
|
|
|
|
|
d = [d initWithBytes: bytes length: length];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
#endif
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return AUTORELEASE(d);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (unsigned) capacity
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (id) setCapacity: (unsigned)newCapacity
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (int) shmID
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
1998-10-23 15:48:21 +00:00
|
|
|
|
|
|
|
|
|
- (void) serializeTypeTag: (unsigned char)tag
|
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
[self serializeDataAt: (void*)&tag
|
|
|
|
|
ofObjCType: @encode(unsigned char)
|
|
|
|
|
context: nil];
|
1998-10-23 15:48:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
- (void) serializeTypeTag: (unsigned char)tag
|
|
|
|
|
andCrossRef: (unsigned)xref
|
1998-10-23 15:48:21 +00:00
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
if (xref <= 0xff)
|
|
|
|
|
{
|
|
|
|
|
gsu8 x = (gsu8)xref;
|
|
|
|
|
|
|
|
|
|
tag = (tag & ~_GSC_SIZE) | _GSC_X_1;
|
|
|
|
|
[self serializeDataAt: (void*)&tag
|
|
|
|
|
ofObjCType: @encode(unsigned char)
|
|
|
|
|
context: nil];
|
|
|
|
|
[self serializeDataAt: (void*)&x
|
|
|
|
|
ofObjCType: @encode(gsu8)
|
|
|
|
|
context: nil];
|
|
|
|
|
}
|
|
|
|
|
else if (xref <= 0xffff)
|
|
|
|
|
{
|
|
|
|
|
gsu16 x = (gsu16)xref;
|
|
|
|
|
|
|
|
|
|
tag = (tag & ~_GSC_SIZE) | _GSC_X_2;
|
|
|
|
|
[self serializeDataAt: (void*)&tag
|
|
|
|
|
ofObjCType: @encode(unsigned char)
|
|
|
|
|
context: nil];
|
|
|
|
|
[self serializeDataAt: (void*)&x
|
|
|
|
|
ofObjCType: @encode(gsu16)
|
|
|
|
|
context: nil];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
gsu32 x = (gsu32)xref;
|
|
|
|
|
|
|
|
|
|
tag = (tag & ~_GSC_SIZE) | _GSC_X_4;
|
|
|
|
|
[self serializeDataAt: (void*)&tag
|
|
|
|
|
ofObjCType: @encode(unsigned char)
|
|
|
|
|
context: nil];
|
|
|
|
|
[self serializeDataAt: (void*)&x
|
|
|
|
|
ofObjCType: @encode(gsu32)
|
|
|
|
|
context: nil];
|
|
|
|
|
}
|
1998-10-23 15:48:21 +00:00
|
|
|
|
}
|
1997-09-29 14:39:53 +00:00
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
/*
|
|
|
|
|
* This is the top of the hierarchy of concrete implementations.
|
|
|
|
|
* As such, it contains efficient implementations of most methods.
|
|
|
|
|
*/
|
|
|
|
|
@implementation NSDataStatic
|
|
|
|
|
|
2000-04-14 10:38:22 +00:00
|
|
|
|
+ (id) allocWithZone: (NSZone*)z
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1998-11-12 10:58:17 +00:00
|
|
|
|
return (NSData*)NSAllocateObject(self, 0, z);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
/* Creation and Destruction of objects. */
|
|
|
|
|
|
1998-11-12 10:58:17 +00:00
|
|
|
|
- (id) copy
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return RETAIN(self);
|
1998-11-12 10:58:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return RETAIN(self);
|
1998-11-12 10:58:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) mutableCopy
|
|
|
|
|
{
|
|
|
|
|
return [[mutableDataMalloc allocWithZone: NSDefaultMallocZone()]
|
2000-06-27 16:18:02 +00:00
|
|
|
|
initWithBytes: bytes length: length];
|
1998-11-12 10:58:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) mutableCopyWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
return [[mutableDataMalloc allocWithZone: z]
|
2000-06-27 16:18:02 +00:00
|
|
|
|
initWithBytes: bytes length: length];
|
1998-11-12 10:58:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
1998-11-12 10:58:17 +00:00
|
|
|
|
bytes = 0;
|
|
|
|
|
length = 0;
|
|
|
|
|
[super dealloc];
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) init
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1998-11-12 10:58:17 +00:00
|
|
|
|
return [self initWithBytesNoCopy: 0
|
|
|
|
|
length: 0
|
|
|
|
|
fromZone: [self zone]];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (id) initWithBytesNoCopy: (void*)aBuffer
|
|
|
|
|
length: (unsigned)bufferSize
|
|
|
|
|
fromZone: (NSZone*)aZone
|
|
|
|
|
{
|
1998-11-12 10:58:17 +00:00
|
|
|
|
bytes = aBuffer;
|
|
|
|
|
length = bufferSize;
|
|
|
|
|
return self;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* NSCoding */
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
- (Class) classForArchiver
|
|
|
|
|
{
|
1998-11-12 10:58:17 +00:00
|
|
|
|
return dataMalloc; /* Will not be static data when decoded. */
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (Class) classForCoder
|
|
|
|
|
{
|
1998-11-12 10:58:17 +00:00
|
|
|
|
return dataMalloc; /* Will not be static data when decoded. */
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (Class) classForPortCoder
|
|
|
|
|
{
|
1998-11-12 10:58:17 +00:00
|
|
|
|
return dataMalloc; /* Will not be static data when decoded. */
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
/* Basic methods */
|
|
|
|
|
|
|
|
|
|
- (const void*) bytes
|
|
|
|
|
{
|
1998-11-12 10:58:17 +00:00
|
|
|
|
return bytes;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) getBytes: (void*)buffer
|
|
|
|
|
range: (NSRange)aRange
|
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
|
GS_RANGE_CHECK(aRange, length);
|
|
|
|
|
memcpy(buffer, bytes + aRange.location, aRange.length);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (unsigned) length
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1998-11-12 10:58:17 +00:00
|
|
|
|
return length;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-22 20:20:31 +00:00
|
|
|
|
static inline void
|
|
|
|
|
getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
|
|
|
|
|
{
|
1998-11-12 10:58:17 +00:00
|
|
|
|
if (*pos > limit || len > limit || len+*pos > limit)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSRangeException
|
|
|
|
|
format: @"Range: (%u, %u) Size: %d",
|
1998-10-22 20:20:31 +00:00
|
|
|
|
*pos, len, limit];
|
|
|
|
|
}
|
1998-11-12 10:58:17 +00:00
|
|
|
|
memcpy(dst, src + *pos, len);
|
|
|
|
|
*pos += len;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-02-08 10:46:32 +00:00
|
|
|
|
- (void) deserializeDataAt: (void*)data
|
|
|
|
|
ofObjCType: (const char*)type
|
|
|
|
|
atCursor: (unsigned*)cursor
|
|
|
|
|
context: (id <NSObjCTypeSerializationCallBack>)callback
|
1998-10-22 20:20:31 +00:00
|
|
|
|
{
|
1999-02-01 12:05:15 +00:00
|
|
|
|
if (data == 0 || type == 0)
|
|
|
|
|
{
|
|
|
|
|
if (data == 0)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"attempt to deserialize to a nul pointer");
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
if (type == 0)
|
|
|
|
|
{
|
1998-10-22 20:20:31 +00:00
|
|
|
|
NSLog(@"attempt to deserialize with a nul type encoding");
|
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
switch (*type)
|
|
|
|
|
{
|
|
|
|
|
case _C_ID:
|
|
|
|
|
{
|
|
|
|
|
[callback deserializeObjectAt: data
|
|
|
|
|
ofObjCType: type
|
|
|
|
|
fromData: self
|
|
|
|
|
atCursor: cursor];
|
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_CHARPTR:
|
|
|
|
|
{
|
|
|
|
|
gss32 len;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
[self deserializeBytes: &len
|
|
|
|
|
length: sizeof(len)
|
|
|
|
|
atCursor: cursor];
|
1999-02-08 10:46:32 +00:00
|
|
|
|
len = GSSwapBigI32ToHost(len);
|
1999-02-01 12:05:15 +00:00
|
|
|
|
if (len == -1)
|
|
|
|
|
{
|
|
|
|
|
*(const char**)data = NULL;
|
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2000-07-02 18:57:05 +00:00
|
|
|
|
#if GS_WITH_GC == 0
|
|
|
|
|
*(char**)data = (char*)NSZoneMalloc(NSDefaultMallocZone(), len+1);
|
|
|
|
|
#else
|
|
|
|
|
*(char**)data = (char*)NSZoneMalloc(NSAtomicMallocZone(), len+1);
|
1999-07-03 19:59:44 +00:00
|
|
|
|
#endif
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
getBytes(*(void**)data, bytes, len, length, cursor);
|
|
|
|
|
(*(char**)data)[len] = '\0';
|
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_ARY_B:
|
|
|
|
|
{
|
|
|
|
|
unsigned offset = 0;
|
|
|
|
|
unsigned size;
|
|
|
|
|
unsigned count = atoi(++type);
|
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
|
|
while (isdigit(*type))
|
|
|
|
|
{
|
|
|
|
|
type++;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
size = objc_sizeof_type(type);
|
1998-10-22 20:20:31 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
|
{
|
|
|
|
|
[self deserializeDataAt: (char*)data + offset
|
|
|
|
|
ofObjCType: type
|
|
|
|
|
atCursor: cursor
|
|
|
|
|
context: callback];
|
|
|
|
|
offset += size;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_STRUCT_B:
|
|
|
|
|
{
|
|
|
|
|
int offset = 0;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
while (*type != _C_STRUCT_E && *type++ != '='); /* skip "<name>=" */
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
[self deserializeDataAt: ((char*)data) + offset
|
|
|
|
|
ofObjCType: type
|
|
|
|
|
atCursor: cursor
|
|
|
|
|
context: callback];
|
|
|
|
|
offset += objc_sizeof_type(type);
|
|
|
|
|
type = objc_skip_typespec(type);
|
|
|
|
|
if (*type != _C_STRUCT_E)
|
|
|
|
|
{
|
|
|
|
|
int align = objc_alignof_type(type);
|
|
|
|
|
int rem = offset % align;
|
|
|
|
|
|
|
|
|
|
if (rem != 0)
|
|
|
|
|
{
|
|
|
|
|
offset += align - rem;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
else break;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_PTR:
|
|
|
|
|
{
|
|
|
|
|
unsigned len = objc_sizeof_type(++type);
|
2000-07-02 18:57:05 +00:00
|
|
|
|
|
|
|
|
|
#if GS_WITH_GC == 0
|
|
|
|
|
*(char**)data = (char*)NSZoneMalloc(NSDefaultMallocZone(), len);
|
|
|
|
|
#else
|
|
|
|
|
*(char**)data = (char*)NSZoneMalloc(NSAtomicMallocZone(), len);
|
1999-07-03 19:59:44 +00:00
|
|
|
|
#endif
|
1999-02-01 12:05:15 +00:00
|
|
|
|
[self deserializeDataAt: *(char**)data
|
|
|
|
|
ofObjCType: type
|
|
|
|
|
atCursor: cursor
|
|
|
|
|
context: callback];
|
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_CHR:
|
|
|
|
|
case _C_UCHR:
|
|
|
|
|
{
|
|
|
|
|
getBytes(data, bytes, sizeof(unsigned char), length, cursor);
|
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_SHT:
|
|
|
|
|
case _C_USHT:
|
|
|
|
|
{
|
|
|
|
|
unsigned short ns;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
getBytes((void*)&ns, bytes, sizeof(ns), length, cursor);
|
|
|
|
|
*(unsigned short*)data = NSSwapBigShortToHost(ns);
|
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_INT:
|
|
|
|
|
case _C_UINT:
|
|
|
|
|
{
|
|
|
|
|
unsigned ni;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
getBytes((void*)&ni, bytes, sizeof(ni), length, cursor);
|
|
|
|
|
*(unsigned*)data = NSSwapBigIntToHost(ni);
|
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_LNG:
|
|
|
|
|
case _C_ULNG:
|
|
|
|
|
{
|
|
|
|
|
unsigned long nl;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
getBytes((void*)&nl, bytes, sizeof(nl), length, cursor);
|
|
|
|
|
*(unsigned long*)data = NSSwapBigLongToHost(nl);
|
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_LNG_LNG:
|
|
|
|
|
case _C_ULNG_LNG:
|
|
|
|
|
{
|
|
|
|
|
unsigned long long nl;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
getBytes((void*)&nl, bytes, sizeof(nl), length, cursor);
|
|
|
|
|
*(unsigned long long*)data = NSSwapBigLongLongToHost(nl);
|
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_FLT:
|
|
|
|
|
{
|
|
|
|
|
NSSwappedFloat nf;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
getBytes((void*)&nf, bytes, sizeof(nf), length, cursor);
|
|
|
|
|
*(float*)data = NSSwapBigFloatToHost(nf);
|
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_DBL:
|
|
|
|
|
{
|
|
|
|
|
NSSwappedDouble nd;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
getBytes((void*)&nd, bytes, sizeof(nd), length, cursor);
|
|
|
|
|
*(double*)data = NSSwapBigDoubleToHost(nd);
|
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_CLASS:
|
|
|
|
|
{
|
|
|
|
|
gsu16 ni;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
getBytes((void*)&ni, bytes, sizeof(ni), length, cursor);
|
1999-02-08 10:46:32 +00:00
|
|
|
|
ni = GSSwapBigI16ToHost(ni);
|
1999-02-01 12:05:15 +00:00
|
|
|
|
if (ni == 0)
|
|
|
|
|
{
|
|
|
|
|
*(Class*)data = 0;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
char name[ni+1];
|
|
|
|
|
Class c;
|
|
|
|
|
|
|
|
|
|
getBytes((void*)name, bytes, ni, length, cursor);
|
|
|
|
|
name[ni] = '\0';
|
|
|
|
|
c = objc_get_class(name);
|
|
|
|
|
if (c == 0)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"can't find class - %s", name];
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
*(Class*)data = c;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_SEL:
|
|
|
|
|
{
|
|
|
|
|
gsu16 ln;
|
|
|
|
|
gsu16 lt;
|
|
|
|
|
|
|
|
|
|
getBytes((void*)&ln, bytes, sizeof(ln), length, cursor);
|
1999-02-08 10:46:32 +00:00
|
|
|
|
ln = GSSwapBigI16ToHost(ln);
|
1999-02-01 12:05:15 +00:00
|
|
|
|
getBytes((void*)<, bytes, sizeof(lt), length, cursor);
|
1999-02-08 10:46:32 +00:00
|
|
|
|
lt = GSSwapBigI16ToHost(lt);
|
1999-02-01 12:05:15 +00:00
|
|
|
|
if (ln == 0)
|
|
|
|
|
{
|
|
|
|
|
*(SEL*)data = 0;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
char name[ln+1];
|
|
|
|
|
char types[lt+1];
|
|
|
|
|
SEL sel;
|
|
|
|
|
|
|
|
|
|
getBytes((void*)name, bytes, ln, length, cursor);
|
|
|
|
|
name[ln] = '\0';
|
|
|
|
|
getBytes((void*)types, bytes, lt, length, cursor);
|
|
|
|
|
types[lt] = '\0';
|
|
|
|
|
|
|
|
|
|
if (lt)
|
|
|
|
|
{
|
|
|
|
|
sel = sel_get_typed_uid(name, types);
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
sel = sel_get_any_typed_uid(name);
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
if (sel == 0)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSInternalInconsistencyException
|
|
|
|
|
format: @"can't find sel with name '%s' "
|
|
|
|
|
@"and types '%s'", name, types];
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
*(SEL*)data = sel;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
default:
|
|
|
|
|
[NSException raise: NSGenericException
|
|
|
|
|
format: @"Unknown type to deserialize - '%s'", type];
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
- (void) deserializeTypeTag: (unsigned char*)tag
|
|
|
|
|
andCrossRef: (unsigned int*)ref
|
|
|
|
|
atCursor: (unsigned*)cursor
|
1998-10-23 15:48:21 +00:00
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
if (*cursor >= length)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSRangeException
|
|
|
|
|
format: @"Range: (%u, 1) Size: %d", *cursor, length];
|
|
|
|
|
}
|
|
|
|
|
*tag = *((unsigned char*)bytes + (*cursor)++);
|
|
|
|
|
if (*tag & _GSC_MAYX)
|
|
|
|
|
{
|
|
|
|
|
switch (*tag & _GSC_SIZE)
|
|
|
|
|
{
|
|
|
|
|
case _GSC_X_0:
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case _GSC_X_1:
|
|
|
|
|
{
|
|
|
|
|
if (*cursor >= length)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSRangeException
|
|
|
|
|
format: @"Range: (%u, 1) Size: %d",
|
|
|
|
|
*cursor, length];
|
|
|
|
|
}
|
|
|
|
|
*ref = (unsigned int)*((unsigned char*)bytes + (*cursor)++);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case _GSC_X_2:
|
|
|
|
|
{
|
|
|
|
|
gsu16 x;
|
|
|
|
|
|
|
|
|
|
if (*cursor >= length-1)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSRangeException
|
|
|
|
|
format: @"Range: (%u, 1) Size: %d",
|
|
|
|
|
*cursor, length];
|
|
|
|
|
}
|
2000-05-23 16:26:40 +00:00
|
|
|
|
#if NEED_WORD_ALIGNMENT
|
2000-06-27 21:11:01 +00:00
|
|
|
|
if ((*cursor % __alignof__(gsu16)) != 0)
|
2000-05-03 03:14:14 +00:00
|
|
|
|
memcpy(&x, (bytes + *cursor), 2);
|
|
|
|
|
else
|
|
|
|
|
#endif
|
1999-01-27 12:49:49 +00:00
|
|
|
|
x = *(gsu16*)(bytes + *cursor);
|
|
|
|
|
*cursor += 2;
|
1999-02-08 10:46:32 +00:00
|
|
|
|
*ref = (unsigned int)GSSwapBigI16ToHost(x);
|
1999-01-27 12:49:49 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
gsu32 x;
|
|
|
|
|
|
|
|
|
|
if (*cursor >= length-3)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSRangeException
|
|
|
|
|
format: @"Range: (%u, 1) Size: %d",
|
|
|
|
|
*cursor, length];
|
|
|
|
|
}
|
2000-05-23 16:26:40 +00:00
|
|
|
|
#if NEED_WORD_ALIGNMENT
|
2000-06-27 21:11:01 +00:00
|
|
|
|
if ((*cursor % __alignof__(gsu32)) != 0)
|
2000-05-03 03:14:14 +00:00
|
|
|
|
memcpy(&x, (bytes + *cursor), 4);
|
|
|
|
|
else
|
|
|
|
|
#endif
|
1999-01-27 12:49:49 +00:00
|
|
|
|
x = *(gsu32*)(bytes + *cursor);
|
|
|
|
|
*cursor += 4;
|
1999-02-08 10:46:32 +00:00
|
|
|
|
*ref = (unsigned int)GSSwapBigI32ToHost(x);
|
1999-01-27 12:49:49 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
1998-10-23 15:48:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation NSDataMalloc
|
|
|
|
|
|
1998-11-12 10:58:17 +00:00
|
|
|
|
- (id) copy
|
|
|
|
|
{
|
1999-05-06 14:42:26 +00:00
|
|
|
|
if (NSShouldRetainWithZone(self, NSDefaultMallocZone()))
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return RETAIN(self);
|
1999-05-06 14:42:26 +00:00
|
|
|
|
else
|
|
|
|
|
return [[dataMalloc allocWithZone: NSDefaultMallocZone()]
|
|
|
|
|
initWithBytes: bytes length: length];
|
1998-11-12 10:58:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
|
{
|
1999-05-06 14:42:26 +00:00
|
|
|
|
if (NSShouldRetainWithZone(self, z))
|
1999-07-03 19:59:44 +00:00
|
|
|
|
return RETAIN(self);
|
1999-05-06 14:42:26 +00:00
|
|
|
|
else
|
|
|
|
|
return [[dataMalloc allocWithZone: z]
|
|
|
|
|
initWithBytes: bytes length: length];
|
1998-11-12 10:58:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (void) dealloc
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1998-11-12 10:58:17 +00:00
|
|
|
|
if (bytes)
|
|
|
|
|
{
|
|
|
|
|
NSZoneFree(zone, bytes);
|
|
|
|
|
bytes = 0;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1998-11-12 10:58:17 +00:00
|
|
|
|
[super dealloc];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (id) initWithBytes: (const void*)aBuffer length: (unsigned)bufferSize
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1998-11-12 10:58:17 +00:00
|
|
|
|
void* tmp = 0;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
|
1998-11-12 10:58:17 +00:00
|
|
|
|
if (aBuffer != 0 && bufferSize > 0)
|
|
|
|
|
{
|
1999-09-29 20:15:17 +00:00
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
zone = GSAtomicMallocZone();
|
|
|
|
|
#else
|
1998-11-12 10:58:17 +00:00
|
|
|
|
zone = [self zone];
|
1999-09-29 20:15:17 +00:00
|
|
|
|
#endif
|
1998-11-12 10:58:17 +00:00
|
|
|
|
tmp = NSZoneMalloc(zone, bufferSize);
|
|
|
|
|
if (tmp == 0)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"[NSDataMalloc -initWithBytes:length:] unable to allocate %lu bytes", bufferSize);
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(self);
|
1998-11-12 10:58:17 +00:00
|
|
|
|
return nil;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
1998-11-12 10:58:17 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
memcpy(tmp, aBuffer, bufferSize);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
1998-11-12 10:58:17 +00:00
|
|
|
|
self = [self initWithBytesNoCopy: tmp length: bufferSize fromZone: zone];
|
|
|
|
|
return self;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithBytesNoCopy: (void*)aBuffer
|
1998-10-21 11:56:58 +00:00
|
|
|
|
length: (unsigned)bufferSize
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1998-11-12 10:58:17 +00:00
|
|
|
|
NSZone *z = NSZoneFromPointer(aBuffer);
|
1998-10-15 05:03:16 +00:00
|
|
|
|
|
1998-11-12 10:58:17 +00:00
|
|
|
|
return [self initWithBytesNoCopy: aBuffer length: bufferSize fromZone: z];
|
1998-10-15 05:03:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithBytesNoCopy: (void*)aBuffer
|
|
|
|
|
length: (unsigned)bufferSize
|
|
|
|
|
fromZone: (NSZone*)aZone
|
|
|
|
|
{
|
1998-11-12 10:58:17 +00:00
|
|
|
|
/*
|
|
|
|
|
* If the zone is zero, the data we have been given does not belong
|
|
|
|
|
* to use so we must create an NSDataStatic object to contain it.
|
|
|
|
|
*/
|
|
|
|
|
if (aZone == 0)
|
|
|
|
|
{
|
|
|
|
|
NSData *data;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
|
1999-05-06 14:42:26 +00:00
|
|
|
|
data = [[NSDataStatic allocWithZone: NSDefaultMallocZone()]
|
|
|
|
|
initWithBytesNoCopy: aBuffer length: bufferSize];
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(self);
|
1998-11-12 10:58:17 +00:00
|
|
|
|
return data;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
1998-10-15 05:03:16 +00:00
|
|
|
|
|
1999-09-29 20:15:17 +00:00
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
zone = GSAtomicMallocZone();
|
|
|
|
|
#else
|
1998-11-12 10:58:17 +00:00
|
|
|
|
zone = aZone;
|
1999-09-29 20:15:17 +00:00
|
|
|
|
#endif
|
1998-11-12 10:58:17 +00:00
|
|
|
|
bytes = aBuffer;
|
|
|
|
|
if (bytes)
|
|
|
|
|
{
|
|
|
|
|
length = bufferSize;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1998-11-12 10:58:17 +00:00
|
|
|
|
return self;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithContentsOfFile: (NSString *)path
|
|
|
|
|
{
|
1999-09-29 20:15:17 +00:00
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
zone = GSAtomicMallocZone();
|
|
|
|
|
#else
|
1998-11-12 10:58:17 +00:00
|
|
|
|
zone = [self zone];
|
1999-09-29 20:15:17 +00:00
|
|
|
|
#endif
|
1998-11-12 10:58:17 +00:00
|
|
|
|
if (readContentsOfFile(path, &bytes, &length, zone) == NO)
|
|
|
|
|
{
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(self);
|
1998-11-12 10:58:17 +00:00
|
|
|
|
self = nil;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
1998-11-12 10:58:17 +00:00
|
|
|
|
return self;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithContentsOfMappedFile: (NSString *)path
|
|
|
|
|
{
|
|
|
|
|
#if HAVE_MMAP
|
1998-11-12 10:58:17 +00:00
|
|
|
|
NSZone *z = [self zone];
|
1998-10-21 11:56:58 +00:00
|
|
|
|
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(self);
|
1998-11-12 10:58:17 +00:00
|
|
|
|
self = [NSDataMappedFile allocWithZone: z];
|
|
|
|
|
return [self initWithContentsOfMappedFile: path];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
#else
|
1998-11-12 10:58:17 +00:00
|
|
|
|
return [self initWithContentsOfFile: path];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithData: (NSData*)anObject
|
|
|
|
|
{
|
1998-11-12 10:58:17 +00:00
|
|
|
|
if (anObject == nil)
|
|
|
|
|
{
|
|
|
|
|
return [self initWithBytesNoCopy: 0 length: 0 fromZone: [self zone]];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
1998-11-12 10:58:17 +00:00
|
|
|
|
if ([anObject isKindOfClass: [NSData class]] == NO)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"-initWithData: passed a non-data object");
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(self);
|
1998-11-12 10:58:17 +00:00
|
|
|
|
return nil;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
1998-11-12 10:58:17 +00:00
|
|
|
|
return [self initWithBytes: [anObject bytes] length: [anObject length]];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (void*) relinquishAllocatedBytesFromZone: (NSZone*)aZone
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1998-11-12 10:58:17 +00:00
|
|
|
|
if (aZone == zone || aZone == 0)
|
|
|
|
|
{
|
|
|
|
|
void *buf = bytes;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
|
1998-11-12 10:58:17 +00:00
|
|
|
|
bytes = 0;
|
|
|
|
|
length = 0;
|
|
|
|
|
return buf;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1998-11-12 10:58:17 +00:00
|
|
|
|
return 0;
|
1998-02-05 22:06:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
#if HAVE_MMAP
|
|
|
|
|
@implementation NSDataMappedFile
|
2000-04-14 10:38:22 +00:00
|
|
|
|
+ (id) allocWithZone: (NSZone*)z
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
|
|
|
|
return (NSData*)NSAllocateObject([NSDataMappedFile class], 0, z);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
1998-11-12 10:58:17 +00:00
|
|
|
|
if (bytes)
|
|
|
|
|
{
|
|
|
|
|
munmap(bytes, length);
|
|
|
|
|
bytes = 0;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
1998-11-12 10:58:17 +00:00
|
|
|
|
[super dealloc];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithContentsOfMappedFile: (NSString*)path
|
|
|
|
|
{
|
|
|
|
|
int fd;
|
1998-02-03 14:20:00 +00:00
|
|
|
|
char thePath[BUFSIZ*2];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
|
1998-02-03 14:20:00 +00:00
|
|
|
|
if ([path getFileSystemRepresentation: thePath
|
|
|
|
|
maxLength: sizeof(thePath)-1] == NO)
|
|
|
|
|
{
|
1999-11-28 18:56:27 +00:00
|
|
|
|
NSDebugLog(@"Open (%s) attempt failed - bad path", thePath);
|
1998-02-03 14:20:00 +00:00
|
|
|
|
return NO;
|
|
|
|
|
}
|
|
|
|
|
fd = open(thePath, O_RDONLY);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
if (fd < 0)
|
|
|
|
|
{
|
1998-02-03 14:20:00 +00:00
|
|
|
|
NSLog(@"[NSDataMappedFile -initWithContentsOfMappedFile:] unable to open %s - %s", thePath, strerror(errno));
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(self);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
/* Find size of file to be mapped. */
|
|
|
|
|
length = lseek(fd, 0, SEEK_END);
|
|
|
|
|
if (length < 0)
|
|
|
|
|
{
|
1998-02-03 14:20:00 +00:00
|
|
|
|
NSLog(@"[NSDataMappedFile -initWithContentsOfMappedFile:] unable to seek to eof %s - %s", thePath, strerror(errno));
|
1997-09-29 14:39:53 +00:00
|
|
|
|
close(fd);
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(self);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
/* Position at start of file. */
|
|
|
|
|
if (lseek(fd, 0, SEEK_SET) != 0)
|
|
|
|
|
{
|
1998-02-03 14:20:00 +00:00
|
|
|
|
NSLog(@"[NSDataMappedFile -initWithContentsOfMappedFile:] unable to seek to sof %s - %s", thePath, strerror(errno));
|
1997-09-29 14:39:53 +00:00
|
|
|
|
close(fd);
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(self);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
bytes = mmap(0, length, PROT_READ, MAP_SHARED, fd, 0);
|
|
|
|
|
if (bytes == MAP_FAILED)
|
|
|
|
|
{
|
1998-02-03 14:20:00 +00:00
|
|
|
|
NSLog(@"[NSDataMappedFile -initWithContentsOfMappedFile:] mapping failed for %s - %s", thePath, strerror(errno));
|
1997-09-29 14:39:53 +00:00
|
|
|
|
close(fd);
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(self);
|
1999-05-06 14:42:26 +00:00
|
|
|
|
self = [dataMalloc allocWithZone: NSDefaultMallocZone()];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
self = [self initWithContentsOfFile: path];
|
|
|
|
|
}
|
|
|
|
|
close(fd);
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (void*) relinquishAllocatedBytesFromZone: (NSZone*)aZone
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
1997-09-29 14:39:53 +00:00
|
|
|
|
@end
|
|
|
|
|
#endif /* HAVE_MMAP */
|
|
|
|
|
|
|
|
|
|
#if HAVE_SHMCTL
|
|
|
|
|
@implementation NSDataShared
|
2000-04-14 10:38:22 +00:00
|
|
|
|
+ (id) allocWithZone: (NSZone*)z
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
|
|
|
|
return (NSData*)NSAllocateObject([NSDataShared class], 0, z);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
if (bytes)
|
|
|
|
|
{
|
1997-10-28 14:34:49 +00:00
|
|
|
|
struct shmid_ds buf;
|
|
|
|
|
|
|
|
|
|
if (shmctl(shmid, IPC_STAT, &buf) < 0)
|
|
|
|
|
NSLog(@"[NSDataShared -dealloc] shared memory control failed - %s",
|
|
|
|
|
strerror(errno));
|
|
|
|
|
else if (buf.shm_nattch == 1)
|
|
|
|
|
if (shmctl(shmid, IPC_RMID, &buf) < 0) /* Mark for deletion. */
|
|
|
|
|
NSLog(@"[NSDataShared -dealloc] shared memory delete failed - %s",
|
|
|
|
|
strerror(errno));
|
|
|
|
|
if (shmdt(bytes) < 0)
|
|
|
|
|
NSLog(@"[NSDataShared -dealloc] shared memory detach failed - %s",
|
|
|
|
|
strerror(errno));
|
1997-09-29 14:39:53 +00:00
|
|
|
|
bytes = 0;
|
|
|
|
|
length = 0;
|
|
|
|
|
shmid = -1;
|
|
|
|
|
}
|
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithBytes: (const void*)aBuffer length: (unsigned)bufferSize
|
|
|
|
|
{
|
|
|
|
|
shmid = -1;
|
|
|
|
|
if (aBuffer && bufferSize)
|
|
|
|
|
{
|
1997-10-28 14:34:49 +00:00
|
|
|
|
shmid = shmget(IPC_PRIVATE, bufferSize, IPC_CREAT|VM_RDONLY);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
if (shmid == -1) /* Created memory? */
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"[-initWithBytes:length:] shared mem get failed for %u - %s",
|
|
|
|
|
bufferSize, strerror(errno));
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(self);
|
1999-05-06 14:42:26 +00:00
|
|
|
|
self = [dataMalloc allocWithZone: NSDefaultMallocZone()];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
return [self initWithBytes: aBuffer length: bufferSize];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bytes = shmat(shmid, 0, 0);
|
|
|
|
|
if (bytes == (void*)-1)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"[-initWithBytes:length:] shared mem attach failed for %u - %s",
|
|
|
|
|
bufferSize, strerror(errno));
|
|
|
|
|
bytes = 0;
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(self);
|
1999-05-06 14:42:26 +00:00
|
|
|
|
self = [dataMalloc allocWithZone: NSDefaultMallocZone()];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
return [self initWithBytes: aBuffer length: bufferSize];
|
|
|
|
|
}
|
|
|
|
|
length = bufferSize;
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithShmID: (int)anId length: (unsigned)bufferSize
|
|
|
|
|
{
|
|
|
|
|
struct shmid_ds buf;
|
|
|
|
|
|
|
|
|
|
shmid = anId;
|
|
|
|
|
if (shmctl(shmid, IPC_STAT, &buf) < 0)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"[NSDataShared -initWithShmID:length:] shared memory control failed - %s", strerror(errno));
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(self); /* Unable to access memory. */
|
1997-09-29 14:39:53 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
if (buf.shm_segsz < bufferSize)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"[NSDataShared -initWithShmID:length:] shared memory segment too small");
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(self); /* Memory segment too small. */
|
1997-09-29 14:39:53 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
bytes = shmat(shmid, 0, 0);
|
|
|
|
|
if (bytes == (void*)-1)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"[NSDataShared -initWithShmID:length:] shared memory attach failed - %s",
|
|
|
|
|
strerror(errno));
|
|
|
|
|
bytes = 0;
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(self); /* Unable to attach to memory. */
|
1997-09-29 14:39:53 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
length = bufferSize;
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (void*) relinquishAllocatedBytesFromZone: (NSZone*)aZone
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
- (int) shmID
|
|
|
|
|
{
|
|
|
|
|
return shmid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
#endif /* HAVE_SHMCTL */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation NSMutableDataMalloc
|
1998-10-21 11:56:58 +00:00
|
|
|
|
+ (void) initialize
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1999-09-29 20:15:17 +00:00
|
|
|
|
if (self == [NSMutableDataMalloc class])
|
|
|
|
|
{
|
|
|
|
|
behavior_class_add_class(self, [NSDataMalloc class]);
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-04-14 10:38:22 +00:00
|
|
|
|
+ (id) allocWithZone: (NSZone*)z
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1998-10-21 11:56:58 +00:00
|
|
|
|
return (NSData*)NSAllocateObject(mutableDataMalloc, 0, z);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (Class) classForArchiver
|
|
|
|
|
{
|
1999-09-29 20:15:17 +00:00
|
|
|
|
return mutableDataMalloc;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (Class) classForCoder
|
|
|
|
|
{
|
1999-09-29 20:15:17 +00:00
|
|
|
|
return mutableDataMalloc;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (Class) classForPortCoder
|
|
|
|
|
{
|
1999-09-29 20:15:17 +00:00
|
|
|
|
return mutableDataMalloc;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-11-12 10:58:17 +00:00
|
|
|
|
- (id) copy
|
|
|
|
|
{
|
|
|
|
|
return [[dataMalloc allocWithZone: NSDefaultMallocZone()]
|
1999-05-06 14:42:26 +00:00
|
|
|
|
initWithBytes: bytes length: length];
|
1998-11-12 10:58:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) copyWithZone: (NSZone*)z
|
|
|
|
|
{
|
|
|
|
|
return [[dataMalloc allocWithZone: z]
|
1999-09-29 20:15:17 +00:00
|
|
|
|
initWithBytes: bytes length: length];
|
1998-11-12 10:58:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (id) initWithBytes: (const void*)aBuffer length: (unsigned)bufferSize
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
|
|
|
|
self = [self initWithCapacity: bufferSize];
|
|
|
|
|
if (self)
|
|
|
|
|
{
|
|
|
|
|
if (aBuffer && bufferSize > 0)
|
|
|
|
|
{
|
|
|
|
|
memcpy(bytes, aBuffer, bufferSize);
|
|
|
|
|
length = bufferSize;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithBytesNoCopy: (void*)aBuffer
|
1998-10-21 11:56:58 +00:00
|
|
|
|
length: (unsigned)bufferSize
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1998-10-15 05:03:16 +00:00
|
|
|
|
NSZone *aZone = NSZoneFromPointer(aBuffer);
|
|
|
|
|
return [self initWithBytesNoCopy: aBuffer length: bufferSize fromZone: aZone];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-15 05:03:16 +00:00
|
|
|
|
- (id) initWithBytesNoCopy: (void*)aBuffer
|
|
|
|
|
length: (unsigned)bufferSize
|
|
|
|
|
fromZone: (NSZone*)aZone
|
|
|
|
|
{
|
1999-09-29 20:15:17 +00:00
|
|
|
|
if (aZone == 0)
|
|
|
|
|
{
|
|
|
|
|
self = [self initWithBytes: aBuffer length: bufferSize];
|
|
|
|
|
return self;
|
1998-10-15 05:03:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-09-29 20:15:17 +00:00
|
|
|
|
if (aBuffer == 0)
|
|
|
|
|
{
|
|
|
|
|
self = [self initWithCapacity: bufferSize];
|
|
|
|
|
if (self)
|
|
|
|
|
{
|
|
|
|
|
[self setLength: bufferSize];
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-09-29 20:15:17 +00:00
|
|
|
|
return self;
|
1998-10-15 05:03:16 +00:00
|
|
|
|
}
|
1999-09-29 20:15:17 +00:00
|
|
|
|
self = [self initWithCapacity: 0];
|
|
|
|
|
if (self)
|
|
|
|
|
{
|
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
zone = GSAtomicMallocZone();
|
|
|
|
|
#else
|
|
|
|
|
zone = aZone;
|
|
|
|
|
#endif
|
|
|
|
|
bytes = aBuffer;
|
|
|
|
|
length = bufferSize;
|
|
|
|
|
capacity = bufferSize;
|
|
|
|
|
growth = capacity/2;
|
|
|
|
|
if (growth == 0)
|
|
|
|
|
{
|
|
|
|
|
growth = 1;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
1999-09-29 20:15:17 +00:00
|
|
|
|
return self;
|
1998-10-15 05:03:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
/*
|
|
|
|
|
* THIS IS THE DESIGNATED INITIALISER
|
|
|
|
|
*/
|
1997-09-29 14:39:53 +00:00
|
|
|
|
- (id) initWithCapacity: (unsigned)size
|
|
|
|
|
{
|
1999-09-29 20:15:17 +00:00
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
zone = GSAtomicMallocZone();
|
|
|
|
|
#else
|
|
|
|
|
zone = [self zone];
|
|
|
|
|
#endif
|
|
|
|
|
if (size)
|
|
|
|
|
{
|
|
|
|
|
bytes = NSZoneMalloc(zone, size);
|
|
|
|
|
if (bytes == 0)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"[NSMutableDataMalloc -initWithCapacity:] out of memory for %u bytes - %s", size, strerror(errno));
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
return nil;
|
1998-10-15 05:03:16 +00:00
|
|
|
|
}
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
1999-09-29 20:15:17 +00:00
|
|
|
|
capacity = size;
|
|
|
|
|
growth = capacity/2;
|
|
|
|
|
if (growth == 0)
|
|
|
|
|
{
|
|
|
|
|
growth = 1;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-09-29 20:15:17 +00:00
|
|
|
|
length = 0;
|
1998-10-15 05:03:16 +00:00
|
|
|
|
|
1999-09-29 20:15:17 +00:00
|
|
|
|
return self;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithLength: (unsigned)size
|
|
|
|
|
{
|
1999-09-29 20:15:17 +00:00
|
|
|
|
self = [self initWithCapacity: size];
|
|
|
|
|
if (self)
|
|
|
|
|
{
|
|
|
|
|
memset(bytes, '\0', size);
|
|
|
|
|
length = size;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
1999-09-29 20:15:17 +00:00
|
|
|
|
return self;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithContentsOfFile: (NSString *)path
|
|
|
|
|
{
|
1999-09-29 20:15:17 +00:00
|
|
|
|
self = [self initWithCapacity: 0];
|
|
|
|
|
if (readContentsOfFile(path, &bytes, &length, zone) == NO)
|
|
|
|
|
{
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
self = nil;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
1999-09-29 20:15:17 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
capacity = length;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-09-29 20:15:17 +00:00
|
|
|
|
return self;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithContentsOfMappedFile: (NSString *)path
|
|
|
|
|
{
|
1999-09-29 20:15:17 +00:00
|
|
|
|
return [self initWithContentsOfFile: path];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithData: (NSData*)anObject
|
|
|
|
|
{
|
1999-09-29 20:15:17 +00:00
|
|
|
|
if (anObject == nil)
|
|
|
|
|
{
|
|
|
|
|
return [self initWithCapacity: 0];
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-09-29 20:15:17 +00:00
|
|
|
|
if ([anObject isKindOfClass: [NSData class]] == NO)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"-initWithData: passed a non-data object");
|
|
|
|
|
RELEASE(self);
|
|
|
|
|
return nil;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
1999-09-29 20:15:17 +00:00
|
|
|
|
return [self initWithBytes: [anObject bytes] length: [anObject length]];
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) appendBytes: (const void*)aBuffer
|
|
|
|
|
length: (unsigned)bufferSize
|
|
|
|
|
{
|
1999-09-29 20:15:17 +00:00
|
|
|
|
unsigned oldLength = length;
|
|
|
|
|
unsigned minimum = length + bufferSize;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
|
1999-09-29 20:15:17 +00:00
|
|
|
|
if (minimum > capacity)
|
|
|
|
|
{
|
|
|
|
|
[self _grow: minimum];
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-09-29 20:15:17 +00:00
|
|
|
|
memcpy(bytes + oldLength, aBuffer, bufferSize);
|
|
|
|
|
length = minimum;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned) capacity
|
|
|
|
|
{
|
|
|
|
|
return capacity;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (void) _grow: (unsigned)minimum
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1999-09-29 20:15:17 +00:00
|
|
|
|
if (minimum > capacity)
|
|
|
|
|
{
|
|
|
|
|
unsigned nextCapacity = capacity + growth;
|
|
|
|
|
unsigned nextGrowth = capacity ? capacity : 1;
|
|
|
|
|
|
|
|
|
|
while (nextCapacity < minimum)
|
|
|
|
|
{
|
|
|
|
|
unsigned tmp = nextCapacity + nextGrowth;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
|
1999-09-29 20:15:17 +00:00
|
|
|
|
nextGrowth = nextCapacity;
|
|
|
|
|
nextCapacity = tmp;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-09-29 20:15:17 +00:00
|
|
|
|
[self setCapacity: nextCapacity];
|
|
|
|
|
growth = nextGrowth;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void*) mutableBytes
|
|
|
|
|
{
|
1999-09-29 20:15:17 +00:00
|
|
|
|
return bytes;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (void*) relinquishAllocatedBytesFromZone: (NSZone*)aZone
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1999-09-29 20:15:17 +00:00
|
|
|
|
void *ptr = [super relinquishAllocatedBytesFromZone: aZone];
|
1997-10-17 13:35:52 +00:00
|
|
|
|
|
1999-09-29 20:15:17 +00:00
|
|
|
|
if (ptr != 0)
|
|
|
|
|
{
|
|
|
|
|
capacity = 0;
|
|
|
|
|
growth = 1;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-09-29 20:15:17 +00:00
|
|
|
|
return ptr;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1997-09-29 14:39:53 +00:00
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (void) replaceBytesInRange: (NSRange)aRange
|
|
|
|
|
withBytes: (const void*)moreBytes
|
|
|
|
|
{
|
1999-06-21 08:30:26 +00:00
|
|
|
|
GS_RANGE_CHECK(aRange, length);
|
|
|
|
|
memcpy(bytes + aRange.location, moreBytes, aRange.length);
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (void) serializeDataAt: (const void*)data
|
|
|
|
|
ofObjCType: (const char*)type
|
|
|
|
|
context: (id <NSObjCTypeSerializationCallBack>)callback
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1999-02-01 12:05:15 +00:00
|
|
|
|
if (data == 0 || type == 0)
|
|
|
|
|
{
|
|
|
|
|
if (data == 0)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"attempt to serialize from a nul pointer");
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
if (type == 0)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"attempt to serialize with a nul type encoding");
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
return;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
switch (*type)
|
|
|
|
|
{
|
|
|
|
|
case _C_ID:
|
|
|
|
|
[callback serializeObjectAt: (id*)data
|
|
|
|
|
ofObjCType: type
|
|
|
|
|
intoData: self];
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
case _C_CHARPTR:
|
|
|
|
|
{
|
|
|
|
|
unsigned len;
|
|
|
|
|
gss32 ni;
|
|
|
|
|
unsigned minimum;
|
|
|
|
|
|
|
|
|
|
if (!*(void**)data)
|
|
|
|
|
{
|
|
|
|
|
ni = -1;
|
1999-02-08 10:46:32 +00:00
|
|
|
|
ni = GSSwapHostI32ToBig(ni);
|
1999-02-01 12:05:15 +00:00
|
|
|
|
[self appendBytes: (void*)&len length: sizeof(len)];
|
|
|
|
|
return;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
len = strlen(*(void**)data);
|
1999-02-08 10:46:32 +00:00
|
|
|
|
ni = GSSwapHostI32ToBig(len);
|
1999-02-01 12:05:15 +00:00
|
|
|
|
minimum = length + len + sizeof(ni);
|
|
|
|
|
if (minimum > capacity)
|
|
|
|
|
{
|
|
|
|
|
[self _grow: minimum];
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
memcpy(bytes+length, &ni, sizeof(ni));
|
|
|
|
|
length += sizeof(ni);
|
|
|
|
|
if (len)
|
|
|
|
|
{
|
|
|
|
|
memcpy(bytes+length, *(void**)data, len);
|
|
|
|
|
length += len;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
return;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_ARY_B:
|
|
|
|
|
{
|
|
|
|
|
unsigned offset = 0;
|
|
|
|
|
unsigned size;
|
|
|
|
|
unsigned count = atoi(++type);
|
|
|
|
|
unsigned i;
|
|
|
|
|
unsigned minimum;
|
|
|
|
|
|
|
|
|
|
while (isdigit(*type))
|
|
|
|
|
{
|
|
|
|
|
type++;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
size = objc_sizeof_type(type);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Serialized objects are going to take up at least as much
|
|
|
|
|
* space as the originals, so we can calculate a minimum space
|
|
|
|
|
* we are going to need and make sure our buffer is big enough.
|
|
|
|
|
*/
|
|
|
|
|
minimum = length + size*count;
|
|
|
|
|
if (minimum > capacity)
|
|
|
|
|
{
|
|
|
|
|
[self _grow: minimum];
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
|
{
|
|
|
|
|
[self serializeDataAt: (char*)data + offset
|
|
|
|
|
ofObjCType: type
|
|
|
|
|
context: callback];
|
|
|
|
|
offset += size;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case _C_STRUCT_B:
|
|
|
|
|
{
|
|
|
|
|
int offset = 0;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
while (*type != _C_STRUCT_E && *type++ != '='); /* skip "<name>=" */
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
[self serializeDataAt: ((char*)data) + offset
|
|
|
|
|
ofObjCType: type
|
|
|
|
|
context: callback];
|
|
|
|
|
offset += objc_sizeof_type(type);
|
|
|
|
|
type = objc_skip_typespec(type);
|
|
|
|
|
if (*type != _C_STRUCT_E)
|
|
|
|
|
{
|
|
|
|
|
unsigned align = objc_alignof_type(type);
|
|
|
|
|
unsigned rem = offset % align;
|
|
|
|
|
|
|
|
|
|
if (rem != 0)
|
|
|
|
|
{
|
|
|
|
|
offset += align - rem;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
}
|
|
|
|
|
else break;
|
|
|
|
|
}
|
|
|
|
|
return;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_PTR:
|
|
|
|
|
[self serializeDataAt: *(char**)data
|
|
|
|
|
ofObjCType: ++type
|
|
|
|
|
context: callback];
|
|
|
|
|
return;
|
|
|
|
|
case _C_CHR:
|
|
|
|
|
case _C_UCHR:
|
|
|
|
|
(*appendImp)(self, appendSel, data, sizeof(unsigned char));
|
|
|
|
|
return;
|
|
|
|
|
case _C_SHT:
|
|
|
|
|
case _C_USHT:
|
|
|
|
|
{
|
|
|
|
|
unsigned short ns = NSSwapHostShortToBig(*(unsigned short*)data);
|
|
|
|
|
(*appendImp)(self, appendSel, &ns, sizeof(unsigned short));
|
|
|
|
|
return;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_INT:
|
|
|
|
|
case _C_UINT:
|
|
|
|
|
{
|
|
|
|
|
unsigned ni = NSSwapHostIntToBig(*(unsigned int*)data);
|
|
|
|
|
(*appendImp)(self, appendSel, &ni, sizeof(unsigned));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
case _C_LNG:
|
|
|
|
|
case _C_ULNG:
|
|
|
|
|
{
|
|
|
|
|
unsigned long nl = NSSwapHostLongToBig(*(unsigned long*)data);
|
|
|
|
|
(*appendImp)(self, appendSel, &nl, sizeof(unsigned long));
|
|
|
|
|
return;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_LNG_LNG:
|
|
|
|
|
case _C_ULNG_LNG:
|
|
|
|
|
{
|
|
|
|
|
unsigned long long nl;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
|
1999-02-01 12:05:15 +00:00
|
|
|
|
nl = NSSwapHostLongLongToBig(*(unsigned long long*)data);
|
|
|
|
|
(*appendImp)(self, appendSel, &nl, sizeof(unsigned long long));
|
|
|
|
|
return;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_FLT:
|
|
|
|
|
{
|
|
|
|
|
NSSwappedFloat nf = NSSwapHostFloatToBig(*(float*)data);
|
|
|
|
|
(*appendImp)(self, appendSel, &nf, sizeof(NSSwappedFloat));
|
|
|
|
|
return;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_DBL:
|
|
|
|
|
{
|
|
|
|
|
NSSwappedDouble nd = NSSwapHostDoubleToBig(*(double*)data);
|
|
|
|
|
(*appendImp)(self, appendSel, &nd, sizeof(NSSwappedDouble));
|
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_CLASS:
|
|
|
|
|
{
|
|
|
|
|
const char *name = *(Class*)data?fastClassName(*(Class*)data):"";
|
|
|
|
|
gsu16 ln = (gsu16)strlen(name);
|
|
|
|
|
gsu16 minimum = length + ln + sizeof(gsu16);
|
|
|
|
|
gsu16 ni;
|
|
|
|
|
|
|
|
|
|
if (minimum > capacity)
|
|
|
|
|
{
|
|
|
|
|
[self _grow: minimum];
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-08 10:46:32 +00:00
|
|
|
|
ni = GSSwapHostI16ToBig(ln);
|
1999-02-01 12:05:15 +00:00
|
|
|
|
memcpy(bytes+length, &ni, sizeof(ni));
|
|
|
|
|
length += sizeof(ni);
|
|
|
|
|
if (ln)
|
|
|
|
|
{
|
|
|
|
|
memcpy(bytes+length, name, ln);
|
|
|
|
|
length += ln;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
return;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
case _C_SEL:
|
|
|
|
|
{
|
|
|
|
|
const char *name = *(SEL*)data?fastSelectorName(*(SEL*)data):"";
|
2000-01-05 16:30:34 +00:00
|
|
|
|
gsu16 ln = (name == 0) ? 0 : (gsu16)strlen(name);
|
1999-02-01 12:05:15 +00:00
|
|
|
|
const char *types = *(SEL*)data?fastSelectorTypes(*(SEL*)data):"";
|
2000-01-05 16:30:34 +00:00
|
|
|
|
gsu16 lt = (types == 0) ? 0 : (gsu16)strlen(types);
|
1999-02-01 12:05:15 +00:00
|
|
|
|
gsu16 minimum = length + ln + lt + 2*sizeof(gsu16);
|
|
|
|
|
gsu16 ni;
|
|
|
|
|
|
|
|
|
|
if (minimum > capacity)
|
|
|
|
|
{
|
|
|
|
|
[self _grow: minimum];
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-08 10:46:32 +00:00
|
|
|
|
ni = GSSwapHostI16ToBig(ln);
|
1999-02-01 12:05:15 +00:00
|
|
|
|
memcpy(bytes+length, &ni, sizeof(ni));
|
|
|
|
|
length += sizeof(ni);
|
1999-02-08 10:46:32 +00:00
|
|
|
|
ni = GSSwapHostI16ToBig(lt);
|
1999-02-01 12:05:15 +00:00
|
|
|
|
memcpy(bytes+length, &ni, sizeof(ni));
|
|
|
|
|
length += sizeof(ni);
|
|
|
|
|
if (ln)
|
|
|
|
|
{
|
|
|
|
|
memcpy(bytes+length, name, ln);
|
|
|
|
|
length += ln;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
if (lt)
|
|
|
|
|
{
|
|
|
|
|
memcpy(bytes+length, types, lt);
|
|
|
|
|
length += lt;
|
1998-10-22 20:20:31 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
return;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-02-01 12:05:15 +00:00
|
|
|
|
default:
|
|
|
|
|
[NSException raise: NSGenericException
|
|
|
|
|
format: @"Unknown type to serialize - '%s'", type];
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
1997-09-29 14:39:53 +00:00
|
|
|
|
|
1998-10-23 15:48:21 +00:00
|
|
|
|
- (void) serializeTypeTag: (unsigned char)tag
|
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
if (length == capacity)
|
|
|
|
|
{
|
|
|
|
|
[self _grow: length + 1];
|
1998-10-23 15:48:21 +00:00
|
|
|
|
}
|
1999-01-27 12:49:49 +00:00
|
|
|
|
((unsigned char*)bytes)[length++] = tag;
|
1998-10-23 15:48:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-27 12:49:49 +00:00
|
|
|
|
- (void) serializeTypeTag: (unsigned char)tag
|
|
|
|
|
andCrossRef: (unsigned)xref
|
1998-10-23 15:48:21 +00:00
|
|
|
|
{
|
1999-01-27 12:49:49 +00:00
|
|
|
|
if (xref <= 0xff)
|
|
|
|
|
{
|
|
|
|
|
tag = (tag & ~_GSC_SIZE) | _GSC_X_1;
|
|
|
|
|
if (length + 2 >= capacity)
|
|
|
|
|
{
|
|
|
|
|
[self _grow: length + 2];
|
|
|
|
|
}
|
|
|
|
|
*(gsu8*)(bytes + length++) = tag;
|
|
|
|
|
*(gsu8*)(bytes + length++) = xref;
|
|
|
|
|
}
|
|
|
|
|
else if (xref <= 0xffff)
|
|
|
|
|
{
|
|
|
|
|
gsu16 x = (gsu16)xref;
|
|
|
|
|
|
|
|
|
|
tag = (tag & ~_GSC_SIZE) | _GSC_X_2;
|
|
|
|
|
if (length + 3 >= capacity)
|
|
|
|
|
{
|
|
|
|
|
[self _grow: length + 3];
|
|
|
|
|
}
|
|
|
|
|
*(gsu8*)(bytes + length++) = tag;
|
2000-05-23 16:26:40 +00:00
|
|
|
|
#if NEED_WORD_ALIGNMENT
|
2000-06-27 21:11:01 +00:00
|
|
|
|
if ((length % __alignof__(gsu16)) != 0)
|
2000-05-03 03:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
x = GSSwapHostI16ToBig(x);
|
|
|
|
|
memcpy((bytes + length), &x, 2);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
1999-02-08 10:46:32 +00:00
|
|
|
|
*(gsu16*)(bytes + length) = GSSwapHostI16ToBig(x);
|
1999-01-27 12:49:49 +00:00
|
|
|
|
length += 2;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
gsu32 x = (gsu32)xref;
|
|
|
|
|
|
|
|
|
|
tag = (tag & ~_GSC_SIZE) | _GSC_X_4;
|
|
|
|
|
if (length + 5 >= capacity)
|
|
|
|
|
{
|
|
|
|
|
[self _grow: length + 5];
|
|
|
|
|
}
|
|
|
|
|
*(gsu8*)(bytes + length++) = tag;
|
2000-05-23 16:26:40 +00:00
|
|
|
|
#if NEED_WORD_ALIGNMENT
|
2000-06-27 21:11:01 +00:00
|
|
|
|
if ((length % __alignof__(gsu32)) != 0)
|
2000-05-03 03:14:14 +00:00
|
|
|
|
{
|
|
|
|
|
x = GSSwapHostI32ToBig(x);
|
|
|
|
|
memcpy((bytes + length), &x, 4);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
#endif
|
1999-02-08 10:46:32 +00:00
|
|
|
|
*(gsu32*)(bytes + length) = GSSwapHostI32ToBig(x);
|
1999-01-27 12:49:49 +00:00
|
|
|
|
length += 4;
|
1998-10-23 15:48:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (id) setCapacity: (unsigned)size
|
|
|
|
|
{
|
1999-09-29 20:15:17 +00:00
|
|
|
|
if (size != capacity)
|
|
|
|
|
{
|
|
|
|
|
void* tmp;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
|
1999-09-29 20:15:17 +00:00
|
|
|
|
if (bytes)
|
|
|
|
|
{
|
|
|
|
|
tmp = NSZoneRealloc(zone, bytes, size);
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-09-29 20:15:17 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tmp = NSZoneMalloc(zone, size);
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-09-29 20:15:17 +00:00
|
|
|
|
if (tmp == 0)
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSMallocException
|
|
|
|
|
format: @"Unable to set data capacity to '%d'", size];
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-09-29 20:15:17 +00:00
|
|
|
|
bytes = tmp;
|
|
|
|
|
capacity = size;
|
|
|
|
|
growth = capacity/2;
|
|
|
|
|
if (growth == 0)
|
|
|
|
|
{
|
|
|
|
|
growth = 1;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
1999-09-29 20:15:17 +00:00
|
|
|
|
if (size < length)
|
|
|
|
|
{
|
|
|
|
|
length = size;
|
1998-10-21 11:56:58 +00:00
|
|
|
|
}
|
1999-09-29 20:15:17 +00:00
|
|
|
|
return self;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (void) setLength: (unsigned)size
|
1998-02-05 22:06:20 +00:00
|
|
|
|
{
|
1998-10-21 11:56:58 +00:00
|
|
|
|
if (size > capacity) {
|
|
|
|
|
[self setCapacity: size];
|
|
|
|
|
}
|
|
|
|
|
if (size > length) {
|
|
|
|
|
memset(bytes + length, '\0', size - length);
|
|
|
|
|
}
|
|
|
|
|
length = size;
|
1998-02-05 22:06:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if HAVE_SHMCTL
|
|
|
|
|
@implementation NSMutableDataShared
|
2000-04-14 10:38:22 +00:00
|
|
|
|
+ (id) allocWithZone: (NSZone*)z
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
|
|
|
|
return (NSData*)NSAllocateObject([NSMutableDataShared class], 0, z);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
if (bytes)
|
|
|
|
|
{
|
1997-10-28 14:34:49 +00:00
|
|
|
|
struct shmid_ds buf;
|
|
|
|
|
|
|
|
|
|
if (shmctl(shmid, IPC_STAT, &buf) < 0)
|
|
|
|
|
NSLog(@"[NSMutableDataShared -dealloc] shared memory control failed - %s", strerror(errno));
|
|
|
|
|
else if (buf.shm_nattch == 1)
|
|
|
|
|
if (shmctl(shmid, IPC_RMID, &buf) < 0) /* Mark for deletion. */
|
|
|
|
|
NSLog(@"[NSMutableDataShared -dealloc] shared memory delete failed - %s", strerror(errno));
|
|
|
|
|
if (shmdt(bytes) < 0)
|
|
|
|
|
NSLog(@"[NSMutableDataShared -dealloc] shared memory detach failed - %s", strerror(errno));
|
1997-09-29 14:39:53 +00:00
|
|
|
|
bytes = 0;
|
|
|
|
|
length = 0;
|
|
|
|
|
capacity = 0;
|
|
|
|
|
shmid = -1;
|
|
|
|
|
}
|
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithBytes: (const void*)aBuffer length: (unsigned)bufferSize
|
|
|
|
|
{
|
|
|
|
|
self = [self initWithCapacity: bufferSize];
|
|
|
|
|
if (self)
|
|
|
|
|
{
|
|
|
|
|
if (bufferSize && aBuffer)
|
|
|
|
|
memcpy(bytes, aBuffer, bufferSize);
|
|
|
|
|
length = bufferSize;
|
|
|
|
|
}
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithCapacity: (unsigned)bufferSize
|
|
|
|
|
{
|
1999-06-24 19:30:29 +00:00
|
|
|
|
int e;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
|
1998-10-15 05:03:16 +00:00
|
|
|
|
shmid = shmget(IPC_PRIVATE, bufferSize, IPC_CREAT|VM_ACCESS);
|
|
|
|
|
if (shmid == -1) /* Created memory? */
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1998-10-15 05:03:16 +00:00
|
|
|
|
NSLog(@"[NSMutableDataShared -initWithCapacity:] shared memory get failed for %u - %s", bufferSize, strerror(errno));
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(self);
|
1999-05-06 14:42:26 +00:00
|
|
|
|
self = [mutableDataMalloc allocWithZone: NSDefaultMallocZone()];
|
1998-10-15 05:03:16 +00:00
|
|
|
|
return [self initWithCapacity: bufferSize];
|
|
|
|
|
}
|
1997-09-29 14:39:53 +00:00
|
|
|
|
|
1998-10-15 05:03:16 +00:00
|
|
|
|
bytes = shmat(shmid, 0, 0);
|
|
|
|
|
e = errno;
|
|
|
|
|
if (bytes == (void*)-1)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"[NSMutableDataShared -initWithCapacity:] shared memory attach failed for %u - %s", bufferSize, strerror(e));
|
|
|
|
|
bytes = 0;
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(self);
|
1999-05-06 14:42:26 +00:00
|
|
|
|
self = [mutableDataMalloc allocWithZone: NSDefaultMallocZone()];
|
1998-10-15 05:03:16 +00:00
|
|
|
|
return [self initWithCapacity: bufferSize];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
1998-10-15 05:03:16 +00:00
|
|
|
|
length = 0;
|
|
|
|
|
capacity = bufferSize;
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) initWithShmID: (int)anId length: (unsigned)bufferSize
|
|
|
|
|
{
|
|
|
|
|
struct shmid_ds buf;
|
|
|
|
|
|
1998-10-15 05:03:16 +00:00
|
|
|
|
shmid = anId;
|
|
|
|
|
if (shmctl(shmid, IPC_STAT, &buf) < 0)
|
1997-09-29 14:39:53 +00:00
|
|
|
|
{
|
1998-10-15 05:03:16 +00:00
|
|
|
|
NSLog(@"[NSMutableDataShared -initWithShmID:length:] shared memory control failed - %s", strerror(errno));
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(self); /* Unable to access memory. */
|
1998-10-15 05:03:16 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
if (buf.shm_segsz < bufferSize)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"[NSMutableDataShared -initWithShmID:length:] shared memory segment too small");
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(self); /* Memory segment too small. */
|
1998-10-15 05:03:16 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
bytes = shmat(shmid, 0, 0);
|
|
|
|
|
if (bytes == (void*)-1)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"[NSMutableDataShared -initWithShmID:length:] shared memory attach failed - %s", strerror(errno));
|
|
|
|
|
bytes = 0;
|
1999-07-03 19:59:44 +00:00
|
|
|
|
RELEASE(self); /* Unable to attach to memory. */
|
1998-10-15 05:03:16 +00:00
|
|
|
|
return nil;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
}
|
1998-10-15 05:03:16 +00:00
|
|
|
|
length = bufferSize;
|
|
|
|
|
capacity = length;
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) setCapacity: (unsigned)size
|
|
|
|
|
{
|
|
|
|
|
if (size != capacity)
|
|
|
|
|
{
|
1999-06-24 19:30:29 +00:00
|
|
|
|
void *tmp;
|
|
|
|
|
int newid;
|
1997-09-29 14:39:53 +00:00
|
|
|
|
|
|
|
|
|
newid = shmget(IPC_PRIVATE, size, IPC_CREAT|VM_ACCESS);
|
|
|
|
|
if (newid == -1) /* Created memory? */
|
1998-10-20 14:40:05 +00:00
|
|
|
|
[NSException raise: NSMallocException
|
|
|
|
|
format: @"Unable to create shared memory segment - %s.",
|
1997-09-29 14:39:53 +00:00
|
|
|
|
strerror(errno)];
|
|
|
|
|
tmp = shmat(newid, 0, 0);
|
|
|
|
|
if ((int)tmp == -1) /* Attached memory? */
|
1998-10-20 14:40:05 +00:00
|
|
|
|
[NSException raise: NSMallocException
|
|
|
|
|
format: @"Unable to attach to shared memory segment."];
|
1997-09-29 14:39:53 +00:00
|
|
|
|
memcpy(tmp, bytes, length);
|
|
|
|
|
if (bytes)
|
1997-10-28 14:34:49 +00:00
|
|
|
|
{
|
|
|
|
|
struct shmid_ds buf;
|
|
|
|
|
|
|
|
|
|
if (shmctl(shmid, IPC_STAT, &buf) < 0)
|
|
|
|
|
NSLog(@"[NSMutableDataShared -setCapacity:] shared memory control failed - %s", strerror(errno));
|
|
|
|
|
else if (buf.shm_nattch == 1)
|
|
|
|
|
if (shmctl(shmid, IPC_RMID, &buf) < 0) /* Mark for deletion. */
|
|
|
|
|
NSLog(@"[NSMutableDataShared -setCapacity:] shared memory delete failed - %s", strerror(errno));
|
|
|
|
|
if (shmdt(bytes) < 0) /* Detach memory. */
|
|
|
|
|
NSLog(@"[NSMutableDataShared -setCapacity:] shared memory detach failed - %s", strerror(errno));
|
|
|
|
|
}
|
1997-09-29 14:39:53 +00:00
|
|
|
|
bytes = tmp;
|
|
|
|
|
shmid = newid;
|
|
|
|
|
capacity = size;
|
|
|
|
|
}
|
|
|
|
|
if (size < length)
|
|
|
|
|
length = size;
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
1998-10-21 11:56:58 +00:00
|
|
|
|
- (void*) relinquishAllocatedBytesFromZone: (NSZone*)aZone
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
- (int) shmID
|
|
|
|
|
{
|
|
|
|
|
return shmid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
#endif /* HAVE_SHMCTL */
|
|
|
|
|
|