forked from jayfuerstenberg/JFCommon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JFBoxEncoder.m
executable file
·625 lines (477 loc) · 14.9 KB
/
JFBoxEncoder.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
//
// JFBoxEncoder.m
// JFCommon
//
// Created by Jason Fuerstenberg on 12/03/19.
// Copyright (c) 2012 Jason Fuerstenberg. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "JFBoxEncoder.h"
@interface JFBoxEncoder (PrivateMethods) // TODO: try to get rid of this now that Xcode 4.3.2 doesn't require it.
- (void) appendHeader;
+ (JFBoxType) boxTypeForNumber: (NSNumber *) number;
@end
@implementation JFBoxEncoder
#pragma mark - Properties
@synthesize data = _data;
#pragma mark - Object lifecycle methods
+ (id) boxEncoderWithData: (NSMutableData *) data {
id boxEncoder = [[JFBoxEncoder alloc] initWithData: data];
#if __has_feature(objc_arc)
// Using ARC so do nothing
#else
// Autorelease the instance
[boxEncoder autorelease];
#endif
return boxEncoder;
}
/*
* Instantiates a box encoder with a mutale data to populate.
*/
- (id) initWithData: (NSMutableData *) data {
self = [super init];
if (self != nil) {
#if __has_feature(objc_arc)
_data = data;
#else
[data retain];
[_data release];
_data = data;
#endif
[self appendHeader];
}
return self;
}
#if __has_feature(objc_arc)
// Using ARC so do nothing
- (void) dealloc {
_data = nil;
}
#else
- (void) dealloc {
[_data release];
[super dealloc];
}
#endif
#pragma mark - Encoding methods
- (void) encodeNil {
JFBoxType type = JFBoxTypeNil;
[_data appendBytes: &type length: 1];
}
- (void) encodeBool: (BOOL) value {
JFBoxType type = JFBoxTypeBool;
SInt8 internalValue = value;
[_data appendBytes: &type length: 1];
[_data appendBytes: &internalValue length: sizeof(SInt8)];
}
- (void) encodeSInt8: (SInt8) value {
JFBoxType type = JFBoxTypeSInt8;
SInt8 internalValue = value;
[_data appendBytes: &type length: 1];
[_data appendBytes: &internalValue length: sizeof(SInt8)];
}
- (void) encodeSInt16: (SInt16) value {
JFBoxType type = JFBoxTypeSInt16;
SInt16 internalValue = value;
[_data appendBytes: &type length: 1];
[_data appendBytes: &internalValue length: sizeof(SInt16)];
}
- (void) encodeSInt32: (SInt32) value {
JFBoxType type = JFBoxTypeSInt32;
SInt32 internalValue = value;
[_data appendBytes: &type length: 1];
[_data appendBytes: &internalValue length: sizeof(SInt32)];
}
- (void) encodeSInt64: (SInt64) value {
JFBoxType type = JFBoxTypeSInt64;
SInt64 internalValue = value;
[_data appendBytes: &type length: 1];
[_data appendBytes: &internalValue length: sizeof(SInt64)];
}
- (void) encodeUInt8: (UInt64) value {
JFBoxType type = JFBoxTypeUInt8;
UInt8 internalValue = value;
[_data appendBytes: &type length: 1];
[_data appendBytes: &internalValue length: sizeof(UInt8)];
}
- (void) encodeUInt16: (UInt16) value {
JFBoxType type = JFBoxTypeUInt16;
UInt16 internalValue = value;
[_data appendBytes: &type length: 1];
[_data appendBytes: &internalValue length: sizeof(UInt16)];
}
- (void) encodeUInt32: (UInt32) value {
JFBoxType type = JFBoxTypeUInt32;
UInt32 internalValue = value;
[_data appendBytes: &type length: 1];
[_data appendBytes: &internalValue length: sizeof(UInt32)];
}
- (void) encodeUInt64: (UInt64) value {
JFBoxType type = JFBoxTypeUInt64;
UInt64 internalValue = value;
[_data appendBytes: &type length: 1];
[_data appendBytes: &internalValue length: sizeof(UInt64)];
}
- (void) encodeFloat: (float) value {
JFBoxType type = JFBoxTypeFloat;
float internalValue = value;
[_data appendBytes: &type length: 1];
[_data appendBytes: &internalValue length: sizeof(float)];
}
- (void) encodeDouble: (double) value {
JFBoxType type = JFBoxTypeDouble;
double internalValue = value;
[_data appendBytes: &type length: 1];
[_data appendBytes: &internalValue length: sizeof(double)];
}
/*
* If nil, nothing is encoded.
*/
- (void) encodeString: (NSString *) value {
JFBoxType type = JFBoxTypeString;
[_data appendBytes: &type length: 1];
[self appendNilOrNotNilForObject: value];
if (value == nil) {
return;
}
const char *internalValue = [value UTF8String];
UInt64 valueLength = [value lengthOfBytesUsingEncoding: NSUTF8StringEncoding];
// Write the length in bytes first...
[_data appendBytes: &valueLength length: sizeof(UInt64)];
// Write the string value...
[_data appendBytes: internalValue length: valueLength];
}
/*
* If nil, nothing is encoded.
*/
- (void) encodeNumber: (NSNumber *) value {
JFBoxType type = JFBoxTypeNumber;
[_data appendBytes: &type length: 1];
[self appendNilOrNotNilForObject: value];
if (value == nil) {
return;
}
JFBoxType numberType = [JFBoxEncoder boxTypeForNumber: value];
if (numberType == JFBoxTypeUnknown) {
return;
}
switch (numberType) {
case JFBoxTypeBool:
[self encodeBool: [value boolValue]];
break;
case JFBoxTypeSInt8:
[self encodeSInt8: [value integerValue]];
break;
case JFBoxTypeSInt16:
[self encodeSInt16: [value integerValue]];
break;
case JFBoxTypeSInt32:
[self encodeSInt32: [value longValue]]; // TODO: is this right? Unit test has some encoded data size issues to resolve.
break;
case JFBoxTypeSInt64:
[self encodeSInt64: [value longLongValue]];
break;
case JFBoxTypeUInt8:
[self encodeUInt8: [value integerValue]];
break;
case JFBoxTypeUInt16:
[self encodeUInt16: [value integerValue]];
break;
case JFBoxTypeUInt32:
[self encodeUInt32: [value longValue]];
break;
case JFBoxTypeUInt64:
[self encodeUInt64: [value longLongValue]];
break;
case JFBoxTypeFloat:
[self encodeFloat: [value floatValue]];
break;
case JFBoxTypeDouble:
[self encodeDouble: [value doubleValue]];
break;
default:
break;
}
}
/*
* Encodes the provided date.
* NOTE: This method encodes the date since Jan 1st 1970 as an NSTimeInterval (double).
* If this does not agree with your application's date logic you are advised to convert your
* NSDate instances manually using a different reference date.
* If nil, nothing is encoded.
*/
- (void) encodeDate: (NSDate *) value {
JFBoxType type = JFBoxTypeDate;
[_data appendBytes: &type length: 1];
[self appendNilOrNotNilForObject: value];
if (value == nil) {
return;
}
if (![value isKindOfClass: [NSDate class]]) {
// Append nil.
[self appendNilOrNotNilForObject: nil];
return;
}
NSTimeInterval internalValue = [value timeIntervalSince1970];
[_data appendBytes: &internalValue length: sizeof(NSTimeInterval)];
}
- (void) encodeData: (NSData *) value {
JFBoxType type = JFBoxTypeData;
[_data appendBytes: &type length: 1];
[self appendNilOrNotNilForObject: value];
if (value == nil) {
return;
}
if (![value isKindOfClass: [NSData class]]) {
// Append nil.
[self appendNilOrNotNilForObject: nil];
return;
}
UInt64 valueLength = [value length];
// Write the length in bytes first...
[_data appendBytes: &valueLength length: sizeof(UInt64)];
// Write the data value...
if (valueLength > 0) {
[_data appendData: value];
}
}
/*
* NOTE: If any element of the Array is not a supported type (not scalar, date, array, dictionary or Box encodable) it will be omitted from the encoding.
*
* If nil, nothing is encoded.
*/
- (void) encodeArray: (NSArray *) value {
UInt8 type = JFBoxTypeArray;
[_data appendBytes: &type length: 1];
[self appendNilOrNotNilForObject: value];
if (value == nil) {
return;
}
UInt64 elementCount = [value count];
// Write the element count first...
[_data appendBytes: &elementCount length: sizeof(UInt64)];
NSMutableData *_masterData = _data;
_data = [NSMutableData dataWithCapacity: elementCount * 100];
@synchronized (value) {
for (id <NSObject> element in value) {
if (element == nil) {
[self encodeNil];
continue;
}
Protocol *protocol = @protocol(JFBoxEncodable);
if ([element conformsToProtocol: protocol]) {
// BOX encodable object...
id <JFBoxEncodable> boxEncodable = (id <JFBoxEncodable>) element;
[self encodeBoxEncodable: boxEncodable];
continue;
}
if ([element isKindOfClass: [NSNumber class]]) {
// NSNumber...
NSNumber *number = (NSNumber *) element;
[self encodeNumber: number];
continue;
}
if ([element isKindOfClass: [NSString class]]) {
// NSString...
NSString *string = (NSString *) element;
[self encodeString: string];
continue;
}
if ([element isKindOfClass: [NSDate class]]) {
// NSDate...
NSDate *date = (NSDate *) element;
[self encodeDate: date];
continue;
}
if ([element isKindOfClass: [NSData class]]) {
// NSData...
NSData *data = (NSData *) element;
[self encodeData: data];
continue;
}
if ([element isKindOfClass: [NSArray class]]) {
// NSArray...
NSArray *array = (NSArray *) element;
[self encodeArray: array];
continue;
}
if ([element isKindOfClass: [NSDictionary class]]) {
// NSDictionary...
NSDictionary *dictionary = (NSDictionary *) element;
[self encodeDictionary: dictionary];
continue;
}
}
UInt64 encodedArrayLength = [_data length];
NSMutableData *encodedArray = _data;
_data = _masterData;
// Write the array length in bytes...
[_data appendBytes: &encodedArrayLength length: sizeof(UInt64)];
[_data appendData: encodedArray];
}
}
/*
* Encodes the provided dictionary to this box encoder.
* If nil, nothing is encoded.
*/
- (void) encodeDictionary: (NSDictionary *) value {
UInt8 type = JFBoxTypeDictionary;
[_data appendBytes: &type length: 1];
[self appendNilOrNotNilForObject: value];
if (value == nil) {
return;
}
UInt64 elementCount = [value count];
// Write the element count first...
[_data appendBytes: &elementCount length: sizeof(UInt64)];
NSMutableData *_masterData = _data;
_data = [NSMutableData dataWithCapacity: elementCount * 100];
@synchronized (value) {
for (id <NSObject> key in value) {
if (![key isKindOfClass: [NSString class]]) {
// All keys must be strings in BOX so continue to the next element...
continue;
}
id <NSObject> element = [value objectForKey: key];
NSString *keyAsString = (NSString *) key;
const char *internalKey = [keyAsString UTF8String];
UInt64 keyLength = [keyAsString lengthOfBytesUsingEncoding: NSUTF8StringEncoding];
// Write the key length and value...
[_data appendBytes: &keyLength length: sizeof(UInt64)];
[_data appendBytes: internalKey length: keyLength];
if (element == nil) {
[self encodeNil];
continue;
}
Protocol *protocol = @protocol(JFBoxEncodable);
if ([element conformsToProtocol: protocol]) {
// BOX encodable object...
id <JFBoxEncodable> boxEncodable = (id <JFBoxEncodable>) element;
[self encodeBoxEncodable: boxEncodable];
continue;
}
if ([element isKindOfClass: [NSNumber class]]) {
// NSNumber...
NSNumber *number = (NSNumber *) element;
[self encodeNumber: number];
continue;
}
if ([element isKindOfClass: [NSString class]]) {
// NSString...
NSString *string = (NSString *) element;
[self encodeString: string];
continue;
}
if ([element isKindOfClass: [NSDate class]]) {
// NSDate...
NSDate *date = (NSDate *) element;
[self encodeDate: date];
continue;
}
if ([element isKindOfClass: [NSData class]]) {
// NSData...
NSData *data = (NSData *) element;
[self encodeData: data];
continue;
}
if ([element isKindOfClass: [NSArray class]]) {
// NSArray...
NSArray *array = (NSArray *) element;
[self encodeArray: array];
continue;
}
if ([element isKindOfClass: [NSDictionary class]]) {
// NSDictionary...
NSDictionary *dictionary = (NSDictionary *) element;
[self encodeDictionary: dictionary];
continue;
}
}
UInt64 encodedDictionaryLength = [_data length];
NSMutableData *encodedDictionary = _data;
_data = _masterData;
// Write the array length in bytes...
[_data appendBytes: &encodedDictionaryLength length: sizeof(UInt64)];
[_data appendData: encodedDictionary];
}
}
/*
* Encodes a JFBoxEncodable implementing class instance to this encoder.
* If nil, nothing is encoded.
*/
- (void) encodeBoxEncodable: (id <JFBoxEncodable>) value {
JFBoxType type = JFBoxTypeBoxEncodable;
[_data appendBytes: &type length: 1];
[self appendNilOrNotNilForObject: value];
if (value == nil) {
return;
}
NSString *className = NSStringFromClass([value class]);
const char *internalClassName = [className UTF8String];
UInt64 classNameLength = [className lengthOfBytesUsingEncoding: NSUTF8StringEncoding];
// Write the class name...
[_data appendBytes: &classNameLength length: sizeof(UInt64)];
[_data appendBytes: internalClassName length: classNameLength];
NSMutableData *masterData = _data;
_data = [NSMutableData dataWithCapacity: 1000];
/*
* Only the object knows how to encode itself so
* let it go to work...
*/
UInt16 formatVersion = [value encodeWithBoxEncoder: self];
UInt64 boxEncodableObjectLength = [_data length];
NSData *boxEncodedObjectData = _data;
_data = masterData;
[_data appendBytes: &formatVersion length: sizeof(UInt16)];
[_data appendBytes: &boxEncodableObjectLength length: sizeof(UInt64)];
[_data appendData: boxEncodedObjectData];
}
- (void) appendNilOrNotNilForObject: (id) object {
UInt8 nilOrNot = JFBoxNotNilObjectValue;
if (object == nil) {
nilOrNot = JFBoxNilObjectValue;
}
[_data appendBytes: &nilOrNot length: sizeof(UInt8)];
}
#pragma mark - Private methods
- (void) appendHeader {
UInt16 boxFormatVersion = JFBoxFormatVersion;
// Write the length in bytes first...
[_data appendBytes: "BOX" length: 3];
// Write the string value...
[_data appendBytes: &boxFormatVersion length: sizeof(UInt16)];
}
+ (JFBoxType) boxTypeForNumber: (NSNumber *) number {
CFNumberType numberType = CFNumberGetType((CFNumberRef) number);
switch (numberType) {
case kCFNumberSInt8Type: return JFBoxTypeSInt8;
case kCFNumberSInt16Type: return JFBoxTypeSInt16;
case kCFNumberSInt32Type: return JFBoxTypeSInt32;
case kCFNumberSInt64Type: return JFBoxTypeSInt64;
case kCFNumberCharType: return JFBoxTypeSInt8;
case kCFNumberShortType: return JFBoxTypeSInt16;
case kCFNumberIntType:
case kCFNumberLongType: return JFBoxTypeSInt32;
case kCFNumberLongLongType: return JFBoxTypeSInt64;
case kCFNumberFloatType:
case kCFNumberCGFloatType:
case kCFNumberFloat32Type: return JFBoxTypeFloat;
case kCFNumberFloat64Type:
case kCFNumberDoubleType: return JFBoxTypeDouble;
default: break;
// This is for handling types the compilers seems to warn about, and that I don't much care about.
}
return JFBoxTypeUnknown;
}
@end