1994-11-04 16:29:24 +00:00
|
|
|
|
/* Implementation of GNU Objective-C coder object for use serializing
|
1996-01-23 16:55:56 +00:00
|
|
|
|
Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
1996-04-17 20:17:45 +00:00
|
|
|
|
Written by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
1994-11-04 16:29:24 +00:00
|
|
|
|
Date: July 1994
|
|
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
|
This file is part of the GNUstep Base Library.
|
1994-11-04 16:29:24 +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
|
|
|
|
|
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
1996-04-17 15:34:35 +00:00
|
|
|
|
#include <gnustep/base/preface.h>
|
1996-04-17 15:23:00 +00:00
|
|
|
|
#include <gnustep/base/Coder.h>
|
|
|
|
|
#include <gnustep/base/CoderPrivate.h>
|
|
|
|
|
#include <gnustep/base/MemoryStream.h>
|
|
|
|
|
#include <gnustep/base/Coding.h>
|
|
|
|
|
#include <gnustep/base/Dictionary.h>
|
|
|
|
|
#include <gnustep/base/Stack.h>
|
|
|
|
|
#include <gnustep/base/Set.h>
|
|
|
|
|
#include <gnustep/base/NSString.h>
|
|
|
|
|
#include <gnustep/base/Streaming.h>
|
|
|
|
|
#include <gnustep/base/Stream.h>
|
|
|
|
|
#include <gnustep/base/CStreaming.h>
|
|
|
|
|
#include <gnustep/base/CStream.h>
|
|
|
|
|
#include <gnustep/base/TextCStream.h>
|
|
|
|
|
#include <gnustep/base/BinaryCStream.h>
|
|
|
|
|
#include <gnustep/base/StdioStream.h>
|
1996-04-19 22:59:11 +00:00
|
|
|
|
#include <gnustep/base/Archiver.h>
|
1996-01-23 22:51:58 +00:00
|
|
|
|
#include <Foundation/NSException.h>
|
1996-01-26 03:06:35 +00:00
|
|
|
|
#include <Foundation/NSGeometry.h>
|
|
|
|
|
#include <Foundation/NSData.h>
|
1996-01-26 20:02:33 +00:00
|
|
|
|
#include <Foundation/NSArchiver.h>
|
1996-02-22 16:09:23 +00:00
|
|
|
|
#include <Foundation/NSMapTable.h>
|
|
|
|
|
#include <Foundation/NSHashTable.h>
|
1996-05-12 23:57:50 +00:00
|
|
|
|
#include <Foundation/NSCoder.h>
|
1994-11-04 16:29:24 +00:00
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
1996-01-23 22:51:58 +00:00
|
|
|
|
|
|
|
|
|
/* Exception strings */
|
|
|
|
|
id CoderSignatureMalformedException = @"CoderSignatureMalformedException";
|
|
|
|
|
|
|
|
|
|
#define DEFAULT_FORMAT_VERSION 0
|
1994-11-04 16:29:24 +00:00
|
|
|
|
|
|
|
|
|
#define ROUND(V, A) \
|
|
|
|
|
({ typeof(V) __v=(V); typeof(A) __a=(A); \
|
|
|
|
|
__a*((__v+__a-1)/__a); })
|
|
|
|
|
|
1996-02-22 16:09:23 +00:00
|
|
|
|
#define DOING_ROOT_OBJECT (interconnect_stack_height != 0)
|
1996-01-23 16:55:56 +00:00
|
|
|
|
|
1996-01-23 22:51:58 +00:00
|
|
|
|
static BOOL debug_coder = NO;
|
1996-01-26 20:02:33 +00:00
|
|
|
|
|
1996-01-23 22:51:58 +00:00
|
|
|
|
|
1994-11-04 16:29:24 +00:00
|
|
|
|
@implementation Coder
|
|
|
|
|
|
1996-05-12 23:57:50 +00:00
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
|
|
|
|
if (self == [Coder class])
|
|
|
|
|
behavior_class_add_class (self, [NSCoderNonCore class]);
|
|
|
|
|
}
|
|
|
|
|
|
1994-11-04 16:29:24 +00:00
|
|
|
|
+ setDebugging: (BOOL)f
|
|
|
|
|
{
|
|
|
|
|
debug_coder = f;
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
1996-01-23 22:51:58 +00:00
|
|
|
|
|
|
|
|
|
/* Initialization. */
|
|
|
|
|
|
1996-02-22 16:09:23 +00:00
|
|
|
|
/* This is the designated initializer. But, don't call it yourself;
|
|
|
|
|
override it and call [super...] in subclasses. */
|
1996-01-23 22:51:58 +00:00
|
|
|
|
- _initWithCStream: (id <CStreaming>) cs
|
|
|
|
|
formatVersion: (int) version
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
1996-01-23 22:51:58 +00:00
|
|
|
|
format_version = version;
|
|
|
|
|
cstream = [cs retain];
|
1996-02-22 16:09:23 +00:00
|
|
|
|
classname_2_classname = NULL;
|
|
|
|
|
interconnect_stack_height = 0;
|
1996-01-25 16:58:36 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- init
|
|
|
|
|
{
|
|
|
|
|
/* Or should we provide some kind of default? */
|
|
|
|
|
[self shouldNotImplement:_cmd];
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
1996-01-25 15:27:30 +00:00
|
|
|
|
/* We must separate the idea of "closing" a coder and "deallocating"
|
|
|
|
|
a coder because of delays in deallocation due to -autorelease. */
|
1996-02-24 16:35:03 +00:00
|
|
|
|
- (void) close
|
1996-01-25 15:27:30 +00:00
|
|
|
|
{
|
1996-02-24 16:35:03 +00:00
|
|
|
|
[[cstream stream] close];
|
1996-01-25 15:27:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (BOOL) isClosed
|
|
|
|
|
{
|
|
|
|
|
return [[cstream stream] isClosed];
|
|
|
|
|
}
|
|
|
|
|
|
1995-03-12 19:59:51 +00:00
|
|
|
|
- (void) dealloc
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
|
|
|
|
/* xxx No. [self _finishDecodeRootObject]; */
|
1996-01-23 22:51:58 +00:00
|
|
|
|
[cstream release];
|
1995-03-12 19:59:51 +00:00
|
|
|
|
[super dealloc];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-01-23 22:51:58 +00:00
|
|
|
|
|
|
|
|
|
/* Access to instance variables. */
|
|
|
|
|
|
1996-01-26 03:06:35 +00:00
|
|
|
|
- cStream
|
|
|
|
|
{
|
|
|
|
|
return cstream;
|
|
|
|
|
}
|
|
|
|
|
|
1996-01-23 22:51:58 +00:00
|
|
|
|
- (int) formatVersion
|
1994-11-04 16:29:24 +00:00
|
|
|
|
{
|
|
|
|
|
return format_version;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) resetCoder
|
|
|
|
|
{
|
|
|
|
|
/* xxx Finish this */
|
1996-01-23 22:51:58 +00:00
|
|
|
|
[self notImplemented:_cmd];
|
1994-11-04 16:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
1996-02-22 16:09:23 +00:00
|
|
|
|
|
|
|
|
|
/* To fool ourselves into thinking we can call all these
|
|
|
|
|
Encoding and Decoding methods. */
|
|
|
|
|
@interface Coder (Coding) <Encoding, Decoding>
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
1996-01-26 20:02:33 +00:00
|
|
|
|
|
1996-01-26 03:06:35 +00:00
|
|
|
|
@implementation Coder (NSCoderCompatibility)
|
|
|
|
|
|
|
|
|
|
|
1996-05-12 23:57:50 +00:00
|
|
|
|
/* The core methods */
|
1996-01-26 03:06:35 +00:00
|
|
|
|
|
|
|
|
|
- (void) encodeValueOfObjCType: (const char*)type
|
|
|
|
|
at: (const void*)address;
|
|
|
|
|
{
|
|
|
|
|
[self encodeValueOfObjCType: type at: address withName: NULL];
|
|
|
|
|
}
|
|
|
|
|
|
1996-05-12 23:57:50 +00:00
|
|
|
|
- (void) decodeValueOfObjCType: (const char*)type
|
|
|
|
|
at: (void*)address
|
|
|
|
|
{
|
|
|
|
|
[self decodeValueOfObjCType: type at: address withName: NULL];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) encodeDataObject: (NSData*)data
|
1996-01-26 03:06:35 +00:00
|
|
|
|
{
|
1996-05-12 23:57:50 +00:00
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSData*) decodeDataObject
|
|
|
|
|
{
|
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned int) versionForClassName: (NSString*)className
|
|
|
|
|
{
|
|
|
|
|
[self notImplemented:_cmd];
|
|
|
|
|
return 0;
|
1996-01-26 03:06:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
1996-05-12 23:57:50 +00:00
|
|
|
|
/* Override some methods in NSCoderNonCore */
|
|
|
|
|
|
1996-01-26 20:02:33 +00:00
|
|
|
|
- (void) encodeObject: (id)anObject
|
|
|
|
|
{
|
|
|
|
|
[self encodeObject: anObject withName: NULL];
|
|
|
|
|
}
|
|
|
|
|
|
1996-01-26 03:06:35 +00:00
|
|
|
|
- (void) encodeBycopyObject: (id)anObject
|
|
|
|
|
{
|
|
|
|
|
[self encodeBycopyObject: anObject withName: NULL];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) encodeConditionalObject: (id)anObject
|
|
|
|
|
{
|
1996-05-13 00:14:51 +00:00
|
|
|
|
/* NeXT's implementation handles *forward* references by running
|
|
|
|
|
through the entire encoding process twice! GNU Coding can handle
|
|
|
|
|
forward references with only one pass. Therefore, however, GNU
|
|
|
|
|
Coding cannot return a *forward* reference from -decodeObject, so
|
|
|
|
|
here, assuming this call to -encodeConditionalObject: is mirrored
|
|
|
|
|
by a -decodeObject, we don't try to encode *forward*
|
|
|
|
|
references.
|
|
|
|
|
|
|
|
|
|
Note that this means objects that use -encodeConditionalObject:
|
|
|
|
|
that are encoded in the GNU style might decode a nil where
|
|
|
|
|
NeXT-style encoded would not. I don't see this a huge problem;
|
|
|
|
|
at least not as bad as NeXT coding mechanism that actually causes
|
|
|
|
|
crashes in situations where GNU's does fine. Still, if we wanted
|
|
|
|
|
to fix this, we might be able to build a kludgy fix based on
|
|
|
|
|
detecting when this would happen, rewinding the stream to the
|
|
|
|
|
"conditional" point, and encoding again. Yuck. */
|
|
|
|
|
|
1996-04-14 15:58:03 +00:00
|
|
|
|
if ([self _coderReferenceForObject: anObject])
|
1996-01-26 20:02:33 +00:00
|
|
|
|
[self encodeObject: anObject];
|
|
|
|
|
else
|
|
|
|
|
[self encodeObject: nil];
|
1996-01-26 03:06:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) encodeRootObject: (id)rootObject
|
|
|
|
|
{
|
|
|
|
|
[self encodeRootObject: rootObject withName: NULL];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (id) decodeObject
|
|
|
|
|
{
|
1996-05-13 00:14:51 +00:00
|
|
|
|
/* This won't work for decoding GNU-style forward references because
|
|
|
|
|
once the GNU decoder finds the object later in the decoding, it
|
|
|
|
|
will back-patch by storing the id in &o... &o will point to some
|
|
|
|
|
weird location on the stack! This is why we make the GNU
|
|
|
|
|
implementation of -encodeConditionalObject: not encode forward
|
|
|
|
|
references. */
|
1996-01-26 03:06:35 +00:00
|
|
|
|
id o;
|
|
|
|
|
[self decodeObjectAt: &o withName: NULL];
|
1996-02-13 01:50:03 +00:00
|
|
|
|
return o;
|
1996-01-26 03:06:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (unsigned int) systemVersion
|
|
|
|
|
{
|
|
|
|
|
return format_version; /* xxx Is this right? */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end /* of (NSCoderCompatibility) */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation Coder (NSArchiverCompatibility)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Initializing an archiver */
|
|
|
|
|
|
|
|
|
|
@interface NSData (Streaming) <Streaming>
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
- (id) initForWritingWithMutableData: (NSMutableData*)mdata
|
|
|
|
|
{
|
|
|
|
|
/* This relies on the fact that GNU extentions to NSMutableData
|
|
|
|
|
cause it to conform to <Streaming>. */
|
1996-02-22 16:09:23 +00:00
|
|
|
|
[(id)self initForWritingToStream: mdata];
|
1996-01-26 03:06:35 +00:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
1996-02-13 01:50:03 +00:00
|
|
|
|
- (id) initForReadingWithData: (NSData*)data
|
|
|
|
|
{
|
|
|
|
|
id ret = [[self class] newReadingFromStream: data];
|
|
|
|
|
[self release];
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
1996-01-26 03:06:35 +00:00
|
|
|
|
/* Archiving Data */
|
|
|
|
|
|
|
|
|
|
+ (NSData*) archivedDataWithRootObject: (id)rootObject
|
|
|
|
|
{
|
|
|
|
|
id d = [[NSMutableData alloc] init];
|
1996-04-19 22:59:11 +00:00
|
|
|
|
id a = [[Archiver alloc] initForWritingWithMutableData:d];
|
1996-01-26 03:06:35 +00:00
|
|
|
|
[a encodeRootObject:rootObject];
|
|
|
|
|
return [d autorelease];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (BOOL) archiveRootObject: (id)rootObject toFile: (NSString*)path
|
|
|
|
|
{
|
|
|
|
|
/* xxx fix this return value */
|
|
|
|
|
id d = [self archivedDataWithRootObject:rootObject];
|
|
|
|
|
[d writeToFile:path atomically:NO];
|
|
|
|
|
return YES;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Getting data from the archiver */
|
|
|
|
|
|
|
|
|
|
+ unarchiveObjectWithData: (NSData*) data
|
|
|
|
|
{
|
|
|
|
|
return [self decodeObjectWithName: NULL fromStream: data];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ unarchiveObjectWithFile: (NSString*) path
|
|
|
|
|
{
|
|
|
|
|
return [self decodeObjectWithName: NULL fromFile: path];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSMutableData*) archiverData
|
|
|
|
|
{
|
|
|
|
|
[self notImplemented:_cmd];
|
1996-03-18 13:42:32 +00:00
|
|
|
|
return nil;
|
1996-01-26 03:06:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|