-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathNSArchiver.m
1109 lines (990 loc) · 25.4 KB
/
NSArchiver.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** Implementation of NSArchiver for GNUstep
Copyright (C) 1998,1999 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <rfm@gnu.org>
Created: October 1998
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
<title>NSArchiver class reference</title>
*/
#import "common.h"
#if !defined (__GNU_LIBOBJC__)
# include <objc/encoding.h>
#endif
#define EXPOSE_NSArchiver_IVARS 1
#define EXPOSE_NSUnarchiver_IVARS 1
/*
* Setup for inline operation of pointer map tables.
*/
#define GSI_MAP_KTYPES GSUNION_NSINT | GSUNION_PTR | GSUNION_OBJ | GSUNION_CLS
#define GSI_MAP_VTYPES GSUNION_NSINT | GSUNION_PTR | GSUNION_OBJ
#define GSI_MAP_RETAIN_KEY(M, X)
#define GSI_MAP_RELEASE_KEY(M, X)
#define GSI_MAP_RETAIN_VAL(M, X)
#define GSI_MAP_RELEASE_VAL(M, X)
#define GSI_MAP_HASH(M, X) ((X).nsu)
#define GSI_MAP_EQUAL(M, X,Y) ((X).ptr == (Y).ptr)
#define GSI_MAP_NOCLEAN 1
#include "GNUstepBase/GSIMap.h"
#define _IN_NSARCHIVER_M
#import "Foundation/NSArchiver.h"
#undef _IN_NSARCHIVER_M
#import "Foundation/NSCoder.h"
#import "Foundation/NSData.h"
#import "Foundation/NSException.h"
typedef unsigned char uchar;
#define PREFIX "GNUstep archive"
static SEL serSel;
static SEL tagSel;
static SEL xRefSel;
static SEL eObjSel;
static SEL eValSel;
@class NSMutableDataMalloc;
@interface NSMutableDataMalloc : NSObject // Help the compiler
@end
static Class NSMutableDataMallocClass;
/**
* <p>Implementation of [NSCoder] capable of creating sequential archives which
* must be read in the same order they were written. This class implements
* methods for saving to and restoring from a serial archive (usually a file
* on disk, but can be an [NSData] object) as well as methods that can be
* used by objects that need to write/restore themselves.</p>
*
* <p>Note, the sibling class [NSKeyedArchiver] supports a form of archive
* that is more robust to class changes, and is recommended over this one.</p>
*/
@implementation NSArchiver
+ (void) initialize
{
if (self == [NSArchiver class])
{
serSel = @selector(serializeDataAt:ofObjCType:context:);
tagSel = @selector(serializeTypeTag:);
xRefSel = @selector(serializeTypeTag:andCrossRef:);
eObjSel = @selector(encodeObject:);
eValSel = @selector(encodeValueOfObjCType:at:);
NSMutableDataMallocClass = [NSMutableDataMalloc class];
}
}
/**
* Creates an NSMutableData instance and calls
* [initForWritingWithMutableData:].
*/
- (id) init
{
NSMutableData *d;
d = [[NSMutableDataMallocClass allocWithZone: [self zone]] init];
self = [self initForWritingWithMutableData: d];
RELEASE(d);
return self;
}
/**
* Init instance that will archive its data to mdata. (Even if
* [archiveRootObject:toFile:] is called, this still gets written to.)
*/
- (id) initForWritingWithMutableData: (NSMutableData*)mdata
{
self = [super init];
if (self)
{
NSZone *zone = [self zone];
_data = RETAIN(mdata);
if ([self directDataAccess] == YES)
{
_dst = _data;
}
else
{
_dst = self;
}
_serImp = [_dst methodForSelector: serSel];
_tagImp = [_dst methodForSelector: tagSel];
_xRefImp = [_dst methodForSelector: xRefSel];
_eObjImp = [self methodForSelector: eObjSel];
_eValImp = [self methodForSelector: eValSel];
[self resetArchiver];
/*
* Set up map tables.
*/
_clsMap = (GSIMapTable)NSZoneMalloc(zone, sizeof(GSIMapTable_t)*6);
_cIdMap = &_clsMap[1];
_uIdMap = &_clsMap[2];
_ptrMap = &_clsMap[3];
_namMap = &_clsMap[4];
_repMap = &_clsMap[5];
GSIMapInitWithZoneAndCapacity(_clsMap, zone, 100);
GSIMapInitWithZoneAndCapacity(_cIdMap, zone, 10);
GSIMapInitWithZoneAndCapacity(_uIdMap, zone, 200);
GSIMapInitWithZoneAndCapacity(_ptrMap, zone, 100);
GSIMapInitWithZoneAndCapacity(_namMap, zone, 1);
GSIMapInitWithZoneAndCapacity(_repMap, zone, 1);
}
return self;
}
- (void) dealloc
{
RELEASE(_data);
if (_clsMap)
{
GSIMapEmptyMap(_clsMap);
if (_cIdMap)
{
GSIMapEmptyMap(_cIdMap);
}
if (_uIdMap)
{
GSIMapEmptyMap(_uIdMap);
}
if (_ptrMap)
{
GSIMapEmptyMap(_ptrMap);
}
if (_namMap)
{
GSIMapEmptyMap(_namMap);
}
if (_repMap)
{
GSIMapEmptyMap(_repMap);
}
NSZoneFree(_clsMap->zone, (void*)_clsMap);
}
[super dealloc];
}
/**
* Writes serialized representation of object and, recursively, any
* other objects it holds references to, to byte array.
*/
+ (NSData*) archivedDataWithRootObject: (id)rootObject
{
NSArchiver *archiver;
id d;
NSZone *z = NSDefaultMallocZone();
d = [[NSMutableDataMallocClass allocWithZone: z] initWithCapacity: 0];
if (d == nil)
{
return nil;
}
archiver = [[self allocWithZone: z] initForWritingWithMutableData: d];
RELEASE(d);
d = nil;
if (archiver)
{
NS_DURING
{
[archiver encodeRootObject: rootObject];
d = AUTORELEASE([archiver->_data copy]);
}
NS_HANDLER
{
RELEASE(archiver);
[localException raise];
}
NS_ENDHANDLER
RELEASE(archiver);
}
return d;
}
/**
* Writes out serialized representation of object and, recursively, any
* other objects it holds references to.
*/
+ (BOOL) archiveRootObject: (id)rootObject
toFile: (NSString*)path
{
id d = [self archivedDataWithRootObject: rootObject];
return [d writeToFile: path atomically: YES];
}
- (void) encodeArrayOfObjCType: (const char*)type
count: (NSUInteger)count
at: (const void*)buf
{
uint32_t c;
uint8_t bytes[20];
uint8_t *bytePtr = 0;
uint8_t byteCount = 0;
NSUInteger i;
NSUInteger offset = 0;
uint32_t size;
uint32_t version;
uchar info;
type = GSSkipTypeQualifierAndLayoutInfo(type);
size = objc_sizeof_type(type);
version = [self systemVersion];
if (12402 == version)
{
NSUInteger tmp = count;
bytes[sizeof(bytes) - ++byteCount] = (uint8_t)(tmp % 128);
tmp /= 128;
while (tmp > 0)
{
bytes[sizeof(bytes) - ++byteCount] = (uint8_t)(128 | (tmp % 128));
tmp /= 128;
}
bytePtr = &bytes[sizeof(bytes) - byteCount];
}
/* We normally store the count as a 32bit integer ... but if it's
* very big, we store 0xffffffff and then an additional 64bit value
* containing the actual count.
*/
if (count >= 0xffffffff)
{
c = 0xffffffff;
}
else
{
c = count;
}
switch (*type)
{
case _C_ID: info = _GSC_NONE; break;
case _C_CHR: info = _GSC_CHR; break;
case _C_UCHR: info = _GSC_UCHR; break;
case _C_SHT: info = _GSC_SHT | _GSC_S_SHT; break;
case _C_USHT: info = _GSC_USHT | _GSC_S_SHT; break;
case _C_INT: info = _GSC_INT | _GSC_S_INT; break;
case _C_UINT: info = _GSC_UINT | _GSC_S_INT; break;
case _C_LNG: info = _GSC_LNG | _GSC_S_LNG; break;
case _C_ULNG: info = _GSC_ULNG | _GSC_S_LNG; break;
case _C_LNG_LNG: info = _GSC_LNG_LNG | _GSC_S_LNG_LNG; break;
case _C_ULNG_LNG: info = _GSC_ULNG_LNG | _GSC_S_LNG_LNG; break;
case _C_FLT: info = _GSC_FLT; break;
case _C_DBL: info = _GSC_DBL; break;
#if defined(_C_BOOL) && (!defined(__GNUC__) || __GNUC__ > 2)
case _C_BOOL: info = _GSC_BOOL; break;
#endif
default: info = _GSC_NONE; break;
}
/*
* Simple types can be serialized immediately, more complex ones
* are dealt with by our [encodeValueOfObjCType:at:] method.
*/
if (info == _GSC_NONE)
{
if (_initialPass == NO)
{
(*_tagImp)(_dst, tagSel, _GSC_ARY_B);
if (12402 == version)
{
for (i = 0; i < byteCount; i++)
{
(*_serImp)(_dst, serSel, bytePtr + i, @encode(uint8_t), nil);
}
}
else
{
(*_serImp)(_dst, serSel, &c, @encode(uint32_t), nil);
if (0xffffffff == c)
{
(*_serImp)(_dst, serSel, &count, @encode(NSUInteger), nil);
}
}
}
for (i = 0; i < c; i++)
{
(*_eValImp)(self, eValSel, type, (char*)buf + offset);
offset += size;
}
}
else if (_initialPass == NO)
{
(*_tagImp)(_dst, tagSel, _GSC_ARY_B);
if (12402 == version)
{
for (i = 0; i < byteCount; i++)
{
(*_serImp)(_dst, serSel, bytePtr + i, @encode(uint8_t), nil);
}
}
else
{
(*_serImp)(_dst, serSel, &c, @encode(uint32_t), nil);
if (0xffffffff == c)
{
(*_serImp)(_dst, serSel, &count, @encode(NSUInteger), nil);
}
}
(*_tagImp)(_dst, tagSel, info);
for (i = 0; i < count; i++)
{
(*_serImp)(_dst, serSel, (char*)buf + offset, type, nil);
offset += size;
}
}
}
- (void) encodeValueOfObjCType: (const char*)type
at: (const void*)buf
{
type = GSSkipTypeQualifierAndLayoutInfo(type);
switch (*type)
{
case _C_ID:
(*_eObjImp)(self, eObjSel, *(void**)buf);
return;
case _C_ARY_B:
{
unsigned count = atoi(++type);
while (isdigit(*type))
{
type++;
}
if (_initialPass == NO)
{
(*_tagImp)(_dst, tagSel, _GSC_ARY_B);
}
[self encodeArrayOfObjCType: type count: count at: buf];
}
return;
case _C_STRUCT_B:
{
struct objc_struct_layout layout;
if (_initialPass == NO)
{
(*_tagImp)(_dst, tagSel, _GSC_STRUCT_B);
}
objc_layout_structure (type, &layout);
while (objc_layout_structure_next_member (&layout))
{
unsigned offset;
unsigned align;
const char *ftype;
objc_layout_structure_get_info (&layout, &offset, &align, &ftype);
(*_eValImp)(self, eValSel, ftype, (char*)buf + offset);
}
}
return;
case _C_PTR:
if (*(void**)buf == 0)
{
if (_initialPass == NO)
{
/*
* Special case - a null pointer gets an xref of zero
*/
(*_tagImp)(_dst, tagSel, _GSC_PTR | _GSC_XREF | _GSC_X_0);
}
}
else
{
GSIMapNode node;
node = GSIMapNodeForKey(_ptrMap, (GSIMapKey)*(void**)buf);
if (_initialPass == YES)
{
/*
* First pass - add pointer to map and encode item pointed
* to in case it is a conditionally encoded object.
*/
if (node == 0)
{
GSIMapAddPair(_ptrMap,
(GSIMapKey)*(void**)buf, (GSIMapVal)(NSUInteger)0);
type++;
buf = *(char**)buf;
(*_eValImp)(self, eValSel, type, buf);
}
}
else if (node == 0 || node->value.nsu == 0)
{
/*
* Second pass, unwritten pointer - write it.
*/
if (node == 0)
{
node = GSIMapAddPair(_ptrMap,
(GSIMapKey)*(void**)buf, (GSIMapVal)(NSUInteger)++_xRefP);
}
else
{
node->value.nsu = ++_xRefP;
}
(*_xRefImp)(_dst, xRefSel, _GSC_PTR, node->value.nsu);
type++;
buf = *(char**)buf;
(*_eValImp)(self, eValSel, type, buf);
}
else
{
/*
* Second pass, write a cross-reference number.
*/
(*_xRefImp)(_dst, xRefSel, _GSC_PTR|_GSC_XREF,
node->value.nsu);
}
}
return;
default: /* Types that can be ignored in first pass. */
if (_initialPass)
{
return;
}
break;
}
switch (*type)
{
case _C_CLASS:
if (*(Class*)buf == 0)
{
/*
* Special case - a null pointer gets an xref of zero
*/
(*_tagImp)(_dst, tagSel, _GSC_CLASS | _GSC_XREF | _GSC_X_0);
}
else
{
Class c = *(Class*)buf;
GSIMapNode node;
BOOL done = NO;
node = GSIMapNodeForKey(_clsMap, (GSIMapKey)(void*)c);
if (node != 0)
{
(*_xRefImp)(_dst, xRefSel, _GSC_CLASS | _GSC_XREF,
node->value.nsu);
return;
}
while (done == NO)
{
int tmp = class_getVersion(c);
unsigned version = tmp;
Class s = class_getSuperclass(c);
if (tmp < 0)
{
[NSException raise: NSInternalInconsistencyException
format: @"negative class version"];
}
node = GSIMapAddPair(_clsMap,
(GSIMapKey)(void*)c, (GSIMapVal)(NSUInteger)++_xRefC);
/*
* Encode tag and crossref number.
*/
(*_xRefImp)(_dst, xRefSel, _GSC_CLASS, node->value.nsu);
/*
* Encode class, and version.
*/
(*_serImp)(_dst, serSel, &c, @encode(Class), nil);
(*_serImp)(_dst, serSel, &version, @encode(unsigned), nil);
/*
* If we have a super class that has not been encoded,
* we must loop round to encode it here so that its
* version information will be available when objects
* of its subclasses are decoded and call
* [super initWithCoder:ccc]
*/
if (s == c || s == 0
|| GSIMapNodeForKey(_clsMap, (GSIMapKey)(void*)s) != 0)
{
done = YES;
}
else
{
c = s;
}
}
/*
* Encode an empty tag to terminate the list of classes.
*/
(*_tagImp)(_dst, tagSel, _GSC_NONE);
}
return;
case _C_SEL:
if (*(SEL*)buf == 0)
{
/*
* Special case - a null pointer gets an xref of zero
*/
(*_tagImp)(_dst, tagSel, _GSC_SEL | _GSC_XREF | _GSC_X_0);
}
else
{
SEL s = *(SEL*)buf;
GSIMapNode node = GSIMapNodeForKey(_ptrMap, (GSIMapKey)(void*)s);
if (node == 0)
{
node = GSIMapAddPair(_ptrMap,
(GSIMapKey)(void*)s, (GSIMapVal)(NSUInteger)++_xRefP);
(*_xRefImp)(_dst, xRefSel, _GSC_SEL, node->value.nsu);
/*
* Encode selector.
*/
(*_serImp)(_dst, serSel, buf, @encode(SEL), nil);
}
else
{
(*_xRefImp)(_dst, xRefSel, _GSC_SEL|_GSC_XREF,
node->value.nsu);
}
}
return;
case _C_CHARPTR:
if (*(char**)buf == 0)
{
/*
* Special case - a null pointer gets an xref of zero
*/
(*_tagImp)(_dst, tagSel, _GSC_CHARPTR | _GSC_XREF | _GSC_X_0);
}
else
{
GSIMapNode node;
node = GSIMapNodeForKey(_ptrMap, (GSIMapKey)*(char**)buf);
if (node == 0)
{
node = GSIMapAddPair(_ptrMap,
(GSIMapKey)*(char**)buf, (GSIMapVal)(NSUInteger)++_xRefP);
(*_xRefImp)(_dst, xRefSel, _GSC_CHARPTR, node->value.nsu);
(*_serImp)(_dst, serSel, buf, type, nil);
}
else
{
(*_xRefImp)(_dst, xRefSel, _GSC_CHARPTR|_GSC_XREF,
node->value.nsu);
}
}
return;
case _C_CHR:
(*_tagImp)(_dst, tagSel, _GSC_CHR);
(*_serImp)(_dst, serSel, (void*)buf, @encode(signed char), nil);
return;
case _C_UCHR:
(*_tagImp)(_dst, tagSel, _GSC_UCHR);
(*_serImp)(_dst, serSel, (void*)buf, @encode(unsigned char), nil);
return;
case _C_SHT:
(*_tagImp)(_dst, tagSel, _GSC_SHT | _GSC_S_SHT);
(*_serImp)(_dst, serSel, (void*)buf, @encode(short), nil);
return;
case _C_USHT:
(*_tagImp)(_dst, tagSel, _GSC_USHT | _GSC_S_SHT);
(*_serImp)(_dst, serSel, (void*)buf, @encode(unsigned short), nil);
return;
case _C_INT:
(*_tagImp)(_dst, tagSel, _GSC_INT | _GSC_S_INT);
(*_serImp)(_dst, serSel, (void*)buf, @encode(int), nil);
return;
case _C_UINT:
(*_tagImp)(_dst, tagSel, _GSC_UINT | _GSC_S_INT);
(*_serImp)(_dst, serSel, (void*)buf, @encode(unsigned int), nil);
return;
case _C_LNG:
(*_tagImp)(_dst, tagSel, _GSC_LNG | _GSC_S_LNG);
(*_serImp)(_dst, serSel, (void*)buf, @encode(long), nil);
return;
case _C_ULNG:
(*_tagImp)(_dst, tagSel, _GSC_ULNG | _GSC_S_LNG);
(*_serImp)(_dst, serSel, (void*)buf, @encode(unsigned long), nil);
return;
case _C_LNG_LNG:
(*_tagImp)(_dst, tagSel, _GSC_LNG_LNG | _GSC_S_LNG_LNG);
(*_serImp)(_dst, serSel, (void*)buf, @encode(long long), nil);
return;
case _C_ULNG_LNG:
(*_tagImp)(_dst, tagSel, _GSC_ULNG_LNG | _GSC_S_LNG_LNG);
(*_serImp)(_dst, serSel, (void*)buf, @encode(unsigned long long), nil);
return;
case _C_FLT:
(*_tagImp)(_dst, tagSel, _GSC_FLT);
(*_serImp)(_dst, serSel, (void*)buf, @encode(float), nil);
return;
case _C_DBL:
(*_tagImp)(_dst, tagSel, _GSC_DBL);
(*_serImp)(_dst, serSel, (void*)buf, @encode(double), nil);
return;
#if defined(_C_BOOL) && (!defined(__GNUC__) || __GNUC__ > 2)
case _C_BOOL:
(*_tagImp)(_dst, tagSel, _GSC_BOOL);
(*_serImp)(_dst, serSel, (void*)buf, @encode(_Bool), nil);
return;
#endif
case _C_VOID:
[NSException raise: NSInvalidArgumentException
format: @"can't encode void item"];
default:
[NSException raise: NSInvalidArgumentException
format: @"item with unknown type - %s", type];
}
}
- (void) encodeRootObject: (id)rootObject
{
if (_encodingRoot)
{
[NSException raise: NSInvalidArgumentException
format: @"encoding root object more than once"];
}
_encodingRoot = YES;
/*
* First pass - find conditional objects.
*/
_initialPass = YES;
(*_eObjImp)(self, eObjSel, rootObject);
/*
* Second pass - write archive.
*/
_initialPass = NO;
(*_eObjImp)(self, eObjSel, rootObject);
/*
* Write sizes of crossref arrays to head of archive.
*/
[self serializeHeaderAt: _startPos
version: [self systemVersion]
classes: _clsMap->nodeCount
objects: _uIdMap->nodeCount
pointers: _ptrMap->nodeCount];
_encodingRoot = NO;
}
- (void) encodeConditionalObject: (id)anObject
{
if (_encodingRoot == NO)
{
[NSException raise: NSInvalidArgumentException
format: @"conditionally encoding without root object"];
return;
}
if (_initialPass)
{
GSIMapNode node;
/*
* Conditionally encoding 'nil' is a no-op.
*/
if (anObject == nil)
{
return;
}
/*
* If we have already conditionally encoded this object, we can
* ignore it this time.
*/
node = GSIMapNodeForKey(_cIdMap, (GSIMapKey)anObject);
if (node != 0)
{
return;
}
/*
* If we have unconditionally encoded this object, we can ignore
* it now.
*/
node = GSIMapNodeForKey(_uIdMap, (GSIMapKey)anObject);
if (node != 0)
{
return;
}
GSIMapAddPair(_cIdMap, (GSIMapKey)anObject, (GSIMapVal)(NSUInteger)0);
}
else if (anObject == nil)
{
(*_eObjImp)(self, eObjSel, nil);
}
else
{
GSIMapNode node;
if (_repMap->nodeCount)
{
node = GSIMapNodeForKey(_repMap, (GSIMapKey)anObject);
if (node)
{
anObject = (id)node->value.ptr;
}
}
node = GSIMapNodeForKey(_cIdMap, (GSIMapKey)anObject);
if (node != 0)
{
(*_eObjImp)(self, eObjSel, nil);
}
else
{
(*_eObjImp)(self, eObjSel, anObject);
}
}
}
- (void) encodeDataObject: (NSData*)anObject
{
unsigned l = [anObject length];
(*_eValImp)(self, eValSel, @encode(unsigned int), &l);
if (l)
{
const void *b = [anObject bytes];
unsigned char c = 0; /* Type tag */
/*
* The type tag 'c' is used to specify an encoding scheme for the
* actual data - at present we have '0' meaning raw data. In the
* future we might want zipped data for instance.
*/
(*_eValImp)(self, eValSel, @encode(unsigned char), &c);
[self encodeArrayOfObjCType: @encode(unsigned char)
count: l
at: b];
}
}
- (void) encodeObject: (id)anObject
{
if (anObject == nil)
{
if (_initialPass == NO)
{
/*
* Special case - encode a nil pointer as a crossref of zero.
*/
(*_tagImp)(_dst, tagSel, _GSC_ID | _GSC_XREF, _GSC_X_0);
}
}
else
{
GSIMapNode node;
/*
* Substitute replacement object if required.
*/
node = GSIMapNodeForKey(_repMap, (GSIMapKey)anObject);
if (node)
{
anObject = (id)node->value.ptr;
}
/*
* See if the object has already been encoded.
*/
node = GSIMapNodeForKey(_uIdMap, (GSIMapKey)anObject);
if (_initialPass)
{
if (node == 0)
{
/*
* Remove object from map of conditionally encoded objects
* and add it to the map of unconditionay encoded ones.
*/
GSIMapRemoveKey(_cIdMap, (GSIMapKey)anObject);
GSIMapAddPair(_uIdMap,
(GSIMapKey)anObject, (GSIMapVal)(NSUInteger)0);
[anObject encodeWithCoder: self];
}
return;
}
if (node == 0 || node->value.nsu == 0)
{
Class cls;
id obj;
if (node == 0)
{
node = GSIMapAddPair(_uIdMap,
(GSIMapKey)anObject, (GSIMapVal)(NSUInteger)++_xRefO);
}
else
{
node->value.nsu = ++_xRefO;
}
obj = [anObject replacementObjectForArchiver: self];
if (GSObjCIsInstance(obj) == NO)
{
/*
* If the object we have been given is actually a class,
* we encode it as a special case.
*/
(*_xRefImp)(_dst, xRefSel, _GSC_CID, node->value.nsu);
(*_eValImp)(self, eValSel, @encode(Class), &obj);
}
else
{
cls = [obj classForArchiver];
if (_namMap->nodeCount)
{
GSIMapNode n;
n = GSIMapNodeForKey(_namMap, (GSIMapKey)cls);
if (n)
{
cls = (Class)n->value.ptr;
}
}
(*_xRefImp)(_dst, xRefSel, _GSC_ID, node->value.nsu);
(*_eValImp)(self, eValSel, @encode(Class), &cls);
[obj encodeWithCoder: self];
}
}
else
{
(*_xRefImp)(_dst, xRefSel, _GSC_ID | _GSC_XREF, node->value.nsu);
}
}
}
/**
* Returns whatever data has been encoded thus far.
*/
- (NSMutableData*) archiverData
{
return _data;
}
/**
* Returns substitute class used to encode objects of given class. This
* would have been set through an earlier call to
* [NSArchiver -encodeClassName:intoClassName:].
*/
- (NSString*) classNameEncodedForTrueClassName: (NSString*)trueName
{
if (_namMap->nodeCount)
{
GSIMapNode node;
Class c;
c = objc_lookUpClass([trueName cString]);
node = GSIMapNodeForKey(_namMap, (GSIMapKey)c);
if (node)
{
c = (Class)node->value.ptr;
return [NSString stringWithUTF8String: class_getName(c)];
}
}
return trueName;
}
/**
* Specify substitute class used in archiving objects of given class. This
* class is written to the archive as the class to use for restoring the
* object, instead of what is returned from [NSObject -classForArchiver].
* This can be used to provide backward compatibility across class name
* changes. The object is still encoded by calling
* <code>encodeWithCoder:</code> as normal.
*/
- (void) encodeClassName: (NSString*)trueName
intoClassName: (NSString*)inArchiveName
{
GSIMapNode node;
Class tc;
Class ic;
tc = objc_lookUpClass([trueName cString]);
if (tc == 0)
{
[NSException raise: NSInternalInconsistencyException
format: @"Can't find class '%@'.", trueName];
}
ic = objc_lookUpClass([inArchiveName cString]);
if (ic == 0)
{
[NSException raise: NSInternalInconsistencyException
format: @"Can't find class '%@'.", inArchiveName];
}
node = GSIMapNodeForKey(_namMap, (GSIMapKey)tc);
if (node == 0)
{
GSIMapAddPair(_namMap, (GSIMapKey)(void*)tc, (GSIMapVal)(void*)ic);
}
else
{
node->value.ptr = (void*)ic;
}
}
/**
* Set encoder to write out newObject in place of object.
*/
- (void) replaceObject: (id)object
withObject: (id)newObject
{
GSIMapNode node;
if (object == 0)
{
[NSException raise: NSInternalInconsistencyException
format: @"attempt to remap nil"];
}
if (newObject == 0)
{
[NSException raise: NSInternalInconsistencyException
format: @"attempt to remap object to nil"];