forked from mchinen/NSKeylessArchiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSKeylessArchiver.m
executable file
·453 lines (351 loc) · 11.2 KB
/
NSKeylessArchiver.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
/* Copyright (c) 2006-2007 Christopher J. W. Lloyd
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#import <CoreGraphics/CoreGraphics.h>
#import <Foundation/Foundation.h>
#include <string.h>
#import "NSKeylessArchiver.h"
#define NSUnimplementedMethod() \
NSLog(@"Method %s is not implemented!", __FUNCTION__)
@implementation NSKeylessArchiver
-init {
_data=[NSMutableData new];
_bytes=[_data mutableBytes];
_position=0;
_pass=0;
// was NSCreateHashTable(NSNonOwnedPointerHashCallBacks,0)
_conditionals = [[NSHashTable alloc] initWithOptions:NSPointerFunctionsWeakMemory|NSPointerFunctionsObjectPointerPersonality capacity:0];
_objects= [[NSHashTable alloc] initWithOptions:NSPointerFunctionsWeakMemory|NSPointerFunctionsObjectPointerPersonality capacity:0];
_classes= [[NSHashTable alloc] initWithOptions:NSPointerFunctionsWeakMemory|NSPointerFunctionsObjectPointerPersonality capacity:0];
// was NSCreateHashTable(NSObjectHashCallBacks,0);
_cStrings= [[NSHashTable alloc] initWithOptions:NSPointerFunctionsStrongMemory|NSPointerFunctionsObjectPointerPersonality capacity:0];
return self;
}
-(void)dealloc {
[_data release];
[_conditionals release];
[_objects release];
[_classes release];
[_cStrings release];
[super dealloc];
}
-(NSData *)data {
return [[_data copy] autorelease];
}
+(NSData *)archivedDataWithRootObject:(id)rootObject {
NSKeylessArchiver *archiver=[[[self allocWithZone:NULL] init] autorelease];
[archiver encodeRootObject:rootObject];
return [archiver data];
}
+(BOOL)archiveRootObject:rootObject toFile:(NSString *)path {
NSData *data=[self archivedDataWithRootObject:rootObject];
return [data writeToFile:path atomically:YES];
}
-(NSMutableData *)archiverData {
return _data;
}
-(void)cannotEncodeType:(const char *)type {
[NSException raise:@"NSKeylessArchiverCannotEncodeException"
format:@"NSKeylessArchiver cannot encode type=%s",type];
}
-(void)_ensureLength:(NSUInteger)length {
[_data setLength:[_data length]+length];
_bytes=[_data mutableBytes];
}
-(void)_appendData:(NSData *)data {
if(_pass==0)
return;
[_data appendData:data];
_bytes=[_data mutableBytes];
_position=[_data length];
}
-(void)_appendWordOne:(uint8_t)value {
if(_pass==0)
return;
[self _ensureLength:1];
_bytes[_position++]=value;
}
-(void)_appendWordTwo:(uint16_t)value {
if(_pass==0)
return;
[self _ensureLength:2];
_bytes[_position++]=(value>>8)&0xFF;
_bytes[_position++]=value&0xFF;
}
-(void)_appendWordFour:(uint32_t)value {
if(_pass==0)
return;
[self _ensureLength:4];
_bytes[_position++]=(value>>24)&0xFF;
_bytes[_position++]=(value>>16)&0xFF;
_bytes[_position++]=(value>>8)&0xFF;
_bytes[_position++]=value&0xFF;
}
-(void)_appendFloat:(float)value {
[self _appendWordFour:NSConvertHostFloatToSwapped(value).v];
}
-(void)_appendWordEight:(uint64_t)value {
if(_pass==0)
return;
[self _ensureLength:8];
_bytes[_position++]=(value>>56)&0xFF;
_bytes[_position++]=(value>>48)&0xFF;
_bytes[_position++]=(value>>40)&0xFF;
_bytes[_position++]=(value>>32)&0xFF;
_bytes[_position++]=(value>>24)&0xFF;
_bytes[_position++]=(value>>16)&0xFF;
_bytes[_position++]=(value>>8)&0xFF;
_bytes[_position++]=value&0xFF;
}
- (void)_appendReference: (void *)value {
#ifdef __LP64__
[self _appendWordEight: (uint64_t)value];
#else
[self _appendWordFour: (uint32_t)value];
#endif
}
-(void)_appendBytes:(const uint8_t *)bytes length:(NSUInteger)length {
int i;
if(_pass==0)
return;
[self _ensureLength:length];
for(i=0;i<length;i++)
_bytes[_position++]=bytes[i];
}
-(void)_appendCStringBytes:(const char *)cString {
[self _ensureLength:strlen(cString)+1];
for(;*cString!='\0';cString++)
_bytes[_position++]=*cString;
_bytes[_position++]='\0';
}
- (void)_appendCString:(const char *)cString
{
NSString *string, *lookup;
if (_pass == 0) {
return;
}
//NSLog(@"cString=%s", cString);
string = [NSString stringWithUTF8String:cString];
//NSLog(@"string=%@", string);
if ((lookup = [_cStrings member:string]) != nil) {
[self _appendReference:lookup];
} else {
[_cStrings addObject:string];
[self _appendReference:string];
[self _appendCStringBytes:[string UTF8String]];
}
}
- (void)_appendClassVersion:(Class)class
{
if (class == [NSObject class]) {
// this is a ref, it can be 8 or 4 bytes depending on arch.
[self _appendReference:(void *)0];
return;
}
[self _appendReference:class];
if ([_classes member:class] == NULL)
{
[_classes addObject:class];
[self _appendCString:[NSStringFromClass(class) UTF8String]];
[self _appendWordFour:[class version]];
[self _appendClassVersion:[class superclass]];
}
}
- (void)_appendObject:(id)object conditional:(BOOL)conditional
{
if (_pass == 0) {
if (object != nil) {
//NSLog(@"%@ conditional=%s", NSStringFromClass([object class]), conditional ? "YES" : "NO");
if (!conditional) {
if ([_conditionals member:object] == NULL) {
[_conditionals addObject:object];
[object encodeWithCoder:self];
}
}
}
} else {
if (conditional && ([_conditionals member:object] == NULL)) {
object=nil;
}
if (object == nil) {
[self _appendWordFour:0];
} else if ([_objects member:object] != NULL) {
[self _appendReference:object];
} else { // FIX do replacementForCoder ?
Class class = [object classForArchiver];
[_objects addObject:object];
[self _appendReference:object];
[self _appendClassVersion:class];
[object encodeWithCoder:self];
}
}
}
-(void)_appendArrayOfObjCType:(const char *)type length:(NSUInteger)length
at:(const void *)addr {
if(_pass==0)
return;
switch(*type){
case 'c':
case 'C':{
const unsigned char *values=addr;
NSInteger i;
for(i=0;i<length;i++)
[self _appendWordOne:values[i]];
}
break;
case 's':
case 'S':{
const unsigned short *values=addr;
NSInteger i;
for(i=0;i<length;i++)
[self _appendWordTwo:values[i]];
}
break;
default:
[self cannotEncodeType:type];
break;
}
}
-(void)encodeValueOfObjCType:(const char *)type at:(const void *)addr {
//NSLog(@"type=%s",type);
[self _appendCString:type];
switch(*type){
case 'c':
case 'C':{
unsigned char value=*(const unsigned char *)addr;
[self _appendWordOne:value];
}
break;
case 's':
case 'S':{
unsigned short value=*(const unsigned short *)addr;
[self _appendWordTwo:value];
}
break;
case 'i':
case 'I':{
unsigned int value=*(const unsigned int *)addr;
[self _appendWordFour:value];
}
break;
case 'l':
case 'L':{
unsigned long value=*(const unsigned long *)addr;
[self _appendWordFour:value];
}
break;
case 'q':
case 'Q':{
unsigned long long value=*(const unsigned long long *)addr;
[self _appendWordEight:value];
}
break;
case 'f':{
float value=*(const float *)addr;
[self _appendFloat:value];
}
break;
case 'd':{
double value=*(const double *)addr;
[self _appendWordEight:NSConvertHostDoubleToSwapped(value).v];
}
break;
case '*':{
const char * const *cString=addr;
[self _appendCString:*cString];
}
break;
case '@':{
id object=*(const id *)addr;
[self _appendObject:object conditional:NO];
}
break;
case '#':
[self cannotEncodeType:type];
break;
case ':':{
SEL selector=*(const SEL *)addr;
[self _appendCString:sel_getName(selector)];
}
break;
case '[':{
const char *tmp=type;
unsigned length=0;
tmp++; // skip [
for(;*tmp>='0' && *tmp<='9';tmp++)
length=(length*10)+(*tmp-'0');
[self _appendArrayOfObjCType:tmp length:length at:addr];
}
break;
case '{': // this is extremely lame
if(strcmp(type,@encode(NSRange))==0){
NSRange value=*(const NSRange *)addr;
[self _appendWordFour:value.location];
[self _appendWordFour:value.length];
break;
}
if(strcmp(type,@encode(CGPoint))==0){
CGPoint value=*(const CGPoint *)addr;
[self _appendFloat:value.x];
[self _appendFloat:value.y];
break;
}
if(strcmp(type,@encode(CGSize))==0){
CGSize value=*(const CGSize *)addr;
[self _appendFloat:value.width];
[self _appendFloat:value.height];
break;
}
if(strcmp(type,@encode(CGRect))==0){
CGRect value=*(const CGRect *)addr;
[self _appendFloat:value.origin.x];
[self _appendFloat:value.origin.y];
[self _appendFloat:value.size.width];
[self _appendFloat:value.size.height];
break;
}
[self cannotEncodeType:type];
break;
case '(':
case 'b':
case '^':
case '?':
default:
[self cannotEncodeType:type];
break;
}
}
-(void)encodeBytes:(const void *)byteaddr length:(NSUInteger)length {
[self _appendWordFour:length];
[self _appendBytes: byteaddr length:length];
}
-(void)encodeDataObject:(NSData *)data {
[self cannotEncodeType:"encodeDataObject"];
}
-(void)encodeRootObject:(id)rootObject {
_position=0;
_pass=0;
[self _appendObject:rootObject conditional:NO];
if(_position!=0)
NSLog(@"_position=%d",_position);
if([_conditionals member:rootObject]==NULL)
NSLog(@"rootObject not in conditionals");
_position=0;
_pass=1;
[self _appendCStringBytes:"~V1~"];
[self _appendWordFour:0]; // archive version
[self _appendCString:"@"];
[self _appendObject:rootObject conditional: NO];
}
-(void)encodeConditionalObject:(id)object {
[self _appendCString:"@"];
[self _appendObject:object conditional:YES];
}
-(void)encodeClassName:(NSString *)runtime intoClassName:(NSString *)archive {
NSUnimplementedMethod();
}
-(void)replaceObject:original withObject:replacement {
NSUnimplementedMethod();
}
@end