-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJAValueToString.m
1183 lines (956 loc) · 28.8 KB
/
JAValueToString.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
/*
JAValueToString.m
Copyright © 2010–2013 Jens Ayton
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 "JAValueToString.h"
#import <objc/runtime.h>
// If nonzero, each value is preceeded by a type name in parentheses.
#define INCLUDE_TYPE_NAMES ( 0 )
// Like INCLUDE_TYPE_NAMES, but for structs and unions (which have embedded names).
// Even if 0, names will be included for pointers to named structs (because the pointee content structure isn't encoded).
#define INCLUDE_STRUCT_NAMES ( 0 || INCLUDE_TYPE_NAMES)
// If nonzero, chars are printed as text (if printable).
#define INCLUDE_CHARS ( 1 )
// If nonzero, pointer values are printed before the value of the pointee, in brackets.
#define INCLUDE_POINTERS ( 0 )
// If nonzero, the number of elements is printed before an array.
#define INCLUDE_ARRAY_COUNTS ( 1 )
// If nonzero, each interpretation of a union is printed, instead of just the first one.
#define INCLUDE_ALL_UNION_INFO ( 0 )
// If nonzero, the encoding string is included along with the decoded data.
#define INCLUDE_RAW_ENCODING ( 0 )
// If nonzero, -debugDescription is preferred over -description for objects.
#define USE_DEBUG_DESCRIPTION ( 1 )
/* Instead of actually printing decoded data, print the sorted order for the
dispatch table so that bsearch will work.
*/
#define SORT_DISPATCH_TABLE ( 0 )
#if JAENCODE_DEBUG
#define NAMES_IN_DISPATCH_TABLE 1
#else
#define NAMES_IN_DISPATCH_TABLE SORT_DISPATCH_TABLE
#endif
#if defined(__has_feature) && __has_feature(objc_arc)
#define AUTORELEASE(x) (x)
#define AUTORELEASE_ENTER @autoreleasepool {
#define AUTORELEASE_EXIT }
#define ARC_UNSAFE_UNRETAINED __unsafe_unretained
#else
#define AUTORELEASE(x) [x autorelease]
#define AUTORELEASE_ENTER { NSAutoreleasePool *autoreleasePool__ = [NSAutoreleasePool new];
#define AUTORELEASE_EXIT [autoreleasePool__ drain]; }
#define ARC_UNSAFE_UNRETAINED
#endif
static NSString * const kJAValueToStringParseError = @"se.ayton.jens.JAValueToString parse error";
enum
{
kSignaturePointer = _C_PTR,
kSignatureBool = _C_BOOL,
kSignatureChar = _C_CHR, // NOTE: @encode() assumes char is signed and doesn't differentiate it from signed char.
kSignatureUnsignedChar = _C_UCHR,
kSignatureShort = _C_SHT,
kSignatureUnsignedShort = _C_USHT,
kSignatureInt = _C_INT,
kSignatureUnsignedInt = _C_UINT,
kSignatureLong = _C_LNG,
kSignatureUnsignedLong = _C_ULNG,
kSignatureLongLong = _C_LNG_LNG,
kSignatureUnsignedLongLong = _C_ULNG_LNG,
kSignatureFloat = _C_FLT,
kSignatureDouble = _C_DBL,
kSignatureSelector = _C_SEL,
kSignatureCharPointer = _C_CHARPTR,
kSignatureObject = _C_ID,
kSignatureClass = _C_CLASS,
kSignatureStruct = _C_STRUCT_B,
kSignatureArray = _C_ARY_B,
kSignatureUnion = _C_UNION_B,
kSignatureVoid = _C_VOID,
kSignatureUndef = _C_UNDEF,
kSignatureAtom = _C_ATOM,
kSignatureBitfield = _C_BFLD,
kSignatureConst = _C_CONST,
kSignatureComplex = 'j',
// Unused.
// kSignatureVector = _C_VECTOR,
};
enum
{
kTokenAnonymousAggregate = '\?',
kTokenAggregateNameSeparator= '=',
kTokenEndOfStruct = _C_STRUCT_E,
kTokenEndOfArray = _C_ARY_E,
kTokenEndOfUnion = _C_UNION_E,
};
enum
{
/* NOTE: you might think the __alignof__ operator could do this for us.
You'd be wrong.
*/
#if __x86_64__
kAlignPointer = 8,
kAlignBool = 1,
kAlignChar = 1,
kAlignUnsignedChar = 1,
kAlignShort = 2,
kAlignUnsignedShort = 2,
kAlignInt = 4,
kAlignUnsignedInt = 4,
kAlignLong = 8,
kAlignUnsignedLong = 8,
kAlignLongLong = 8,
kAlignUnsignedLongLong = 8,
kAlignFloat = 4,
kAlignDouble = 8,
kAlignSelector = 8,
#elif __i386__
kAlignPointer = 4,
kAlignBool = 1,
kAlignChar = 1,
kAlignUnsignedChar = 1,
kAlignShort = 2,
kAlignUnsignedShort = 2,
kAlignInt = 4,
kAlignUnsignedInt = 4,
kAlignLong = 4,
kAlignUnsignedLong = 4,
kAlignLongLong = 4,
kAlignUnsignedLongLong = 4,
kAlignFloat = 4,
kAlignDouble = 4,
kAlignSelector = 4,
#elif __ppc__
kAlignPointer = 4,
kAlignBool = 4, // Sneaky!
kAlignChar = 1,
kAlignUnsignedChar = 1,
kAlignShort = 2,
kAlignUnsignedShort = 2,
kAlignInt = 4,
kAlignUnsignedInt = 4,
kAlignLong = 4,
kAlignUnsignedLong = 4,
kAlignLongLong = 4,
kAlignUnsignedLongLong = 4,
kAlignFloat = 4,
kAlignDouble = 4,
kAlignSelector = 4,
#elif __arm__
kAlignPointer = 4,
kAlignBool = 1,
kAlignChar = 1,
kAlignUnsignedChar = 1,
kAlignShort = 2,
kAlignUnsignedShort = 2,
kAlignInt = 4,
kAlignUnsignedInt = 4,
kAlignLong = 4,
kAlignUnsignedLong = 4,
kAlignLongLong = 4,
kAlignUnsignedLongLong = 4,
kAlignFloat = 4,
kAlignDouble = 4,
kAlignSelector = 4,
#elif __arm64__
kAlignPointer = 8,
kAlignBool = 1,
kAlignChar = 1,
kAlignUnsignedChar = 1,
kAlignShort = 2,
kAlignUnsignedShort = 2,
kAlignInt = 4,
kAlignUnsignedInt = 4,
kAlignLong = 8,
kAlignUnsignedLong = 8,
kAlignLongLong = 8,
kAlignUnsignedLongLong = 8,
kAlignFloat = 4,
kAlignDouble = 8,
kAlignSelector = 8,
#else
#error Unknown architecture
#endif
kAlignCharPointer = kAlignPointer,
kAlignObject = kAlignPointer,
kAlignClass = kAlignObject,
// Zero-length things and qualifiers don't need aligment. No alignment == 1-byte alignment.
kAlignVoid = 1,
kAlignUndef = 1,
kAlignConst = 1,
kAlignComplex = 1,
/* Aggregates have special alignment needs: their alignment is the highest
alignment of any member. This is signaled with a 0, and handled by
doing a "dry run" with NULL data and using the resulting maxAlign.
This might not work on all platforms, but then, that applies to our
handling of alignment in general.
*/
kAlignStruct = 0,
kAlignArray = 0,
kAlignUnion = 0,
/* The only information I can find about _C_ATOM is that it is int-sized
in Apple's runtimes.
*/
kAlignAtom = kAlignInt,
// Unsupported.
kAlignBitfield = 1
};
static inline NSString *StringWithBytesLengthEncoding(const void *bytes, NSUInteger length, NSStringEncoding encoding)
{
NSString *result = [[NSString alloc] initWithBytes:bytes
length:length
encoding:encoding];
return AUTORELEASE(result);
}
#define DO_NOTHING do {} while (0)
#define ASSERT_NOT_END_OF_ENCODING(enc, curs) do { if (enc[*curs] == '\0') [NSException raise:kJAValueToStringParseError format:@"format string ended unexpectedly: \"%s\"", enc]; } while (0)
#if INCLUDE_TYPE_NAMES
#define TYPE_NAME(string, name) [string appendFormat:@"(%@)", name]
#else
#define TYPE_NAME(string, name) DO_NOTHING
#endif
#define DECODER_PARAMS const char * const encoding, size_t * const encOffset, const uint8_t * const buffer, size_t * const bufOffset, NSMutableString *string, size_t * const maxAlign, BOOL align
#define DECODER_CALL_THROUGH_ALIGN(subAlign) encoding, encOffset, buffer, bufOffset, string, maxAlign, subAlign
#define DECODER_CALL_THROUGH DECODER_CALL_THROUGH_ALIGN(align)
static void DecodeValue(DECODER_PARAMS);
typedef void (*Decoder)(DECODER_PARAMS);
typedef struct DispatchEntry
{
#if NAMES_IN_DISPATCH_TABLE
const char *name;
#endif
Decoder decoder;
uint8_t alignment;
char signature;
} DispatchEntry;
#if NAMES_IN_DISPATCH_TABLE
#define DECLARE_DISPATCH(nm) \
static DispatchEntry sDispatch##nm = { .decoder = Decode##nm, .alignment = kAlign##nm, .signature = kSignature##nm, .name = #nm }
#else
#define DECLARE_DISPATCH(nm) \
static DispatchEntry sDispatch##nm = { .decoder = Decode##nm, .alignment = kAlign##nm, .signature = kSignature##nm }
#endif
#define DECODE_SCALAR(TYPE, name, formatString, EXTRA) \
static void Decode##name(DECODER_PARAMS) \
{ \
NSCParameterAssert(bufOffset != NULL); \
TYPE_NAME(string, @#TYPE); \
TYPE value; \
if (buffer != NULL) \
{ \
value = *((const TYPE *)(buffer + *bufOffset)); \
[string appendFormat:formatString, value]; \
EXTRA; \
} \
else \
{ \
[string appendString:@"null"]; \
} \
*bufOffset += sizeof value; \
} \
DECLARE_DISPATCH(name);
#if INCLUDE_CHARS
// Print ASCII printable chars or names.
static void AppendIfPrintableChar(unsigned char c, NSMutableString *string)
{
if (c > 127 || c == 0) return;
const char *lowNames[] =
{
"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL", "BS", "tab",
"LF", "VT", "FF", "CR", "SO", "SI", "DLE", "DC1", "DC2", "DC3", "DC4",
"NAK", "SYN", "ETB", "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
"space"
};
if (c < (sizeof(lowNames) / sizeof(*lowNames)))
{
[string appendFormat:@" (=%s)", lowNames[c]];
return;
}
if (c == 127)
{
[string appendString:@" (=DEL)"];
return;
}
[string appendFormat:@" (='%c')", c];
}
static BOOL IsPrintableMacRoman(unsigned char c)
{
// Tab, CR, LF and NBSP deliberately excluded, along with various printing glyphs not likely to occur in FCCs.
if (c < 32) return NO;
if (c == 127) return NO;
if (c >= 160 && c <= 173) return NO;
if (c >= 194 && c <= 202) return NO;
if (c >= 208 && c <= 215) return NO;
if (c >= 218 && c <= 221) return NO;
if (c >= 246) return NO;
return YES;
}
static void AppendIfPrintableFCC(unsigned val, NSMutableString *string)
{
/* Print MacRoman fourcharcodes, if and only if all four chars pass
IsPrintableMacRoman() and at least one passes isalnum().
*/
unsigned char bytes[4] = { (val >> 24) & 0xFF, (val >> 16) & 0xFF, (val >> 8) & 0xFF, val & 0xFF };
BOOL haveAlnum = NO;
for (unsigned i = 0; i < 4; i++)
{
if (!IsPrintableMacRoman(bytes[i])) return;
if (isalnum(bytes[i])) haveAlnum = YES;
}
if (!haveAlnum) return;
NSString *fcc = StringWithBytesLengthEncoding(bytes, 4, NSMacOSRomanStringEncoding);
[string appendFormat:@" (='%@')", fcc];
}
#if LONG_MAX == INT_MAX
#define AppendIfPrintableFCCLong AppendIfPrintableFCC
#else
#define AppendIfPrintableFCCLong(v, string) DO_NOTHING
#endif
#else
#define AppendIfPrintableChar(c, string) DO_NOTHING
#define AppendIfPrintableFCC(v, string) DO_NOTHING
#endif
DECODE_SCALAR(char, Char, @"%i", AppendIfPrintableChar(value, string))
DECODE_SCALAR(unsigned char, UnsignedChar, @"%u", AppendIfPrintableChar(value, string))
DECODE_SCALAR(short, Short, @"%i", DO_NOTHING)
DECODE_SCALAR(unsigned short, UnsignedShort, @"%u", DO_NOTHING)
DECODE_SCALAR(int, Int, @"%i", AppendIfPrintableFCC(value, string))
DECODE_SCALAR(unsigned int, UnsignedInt, @"%i", AppendIfPrintableFCC(value, string))
DECODE_SCALAR(long, Long, @"%li", AppendIfPrintableFCCLong(value, string))
DECODE_SCALAR(unsigned long, UnsignedLong, @"%lu", AppendIfPrintableFCCLong(value, string))
DECODE_SCALAR(long long, LongLong, @"%lli", DO_NOTHING)
DECODE_SCALAR(unsigned long long, UnsignedLongLong, @"%llu", DO_NOTHING)
DECODE_SCALAR(float, Float, @"%g", DO_NOTHING)
DECODE_SCALAR(double, Double, @"%g", DO_NOTHING)
static void DecodeBool(DECODER_PARAMS)
{
NSCParameterAssert(bufOffset != NULL);
// C _Bool/bool; unfortunately not BOOL.
TYPE_NAME(string, @"bool");
_Bool value;
if (buffer != NULL)
{
value = *((const _Bool *)(buffer + *bufOffset));
[string appendString:value ? @"true" : @"false"];
}
else
{
[string appendString:@"null"];
}
*bufOffset += sizeof(value);
}
DECLARE_DISPATCH(Bool);
static void DecodeVoid(DECODER_PARAMS)
{
// Can only occur as the target of a pointer.
[string appendString:@"void"];
}
DECLARE_DISPATCH(Void);
static void DecodeConst(DECODER_PARAMS)
{
/* Ignore the const, and move on to the actual value.
In theory it would be nice to attach it to the type of the next object
if INCLUDE_TYPE_NAMES, but it's not worth the trouble.
*/
DecodeValue(DECODER_CALL_THROUGH);
}
DECLARE_DISPATCH(Const);
static void DecodeComplex(DECODER_PARAMS)
{
// A _Complex x, encoded as "jx", is simply two xes in a row.
const size_t origEncOffset = *encOffset;
[string appendString:@"("];
DecodeValue(DECODER_CALL_THROUGH);
*encOffset = origEncOffset;
[string appendString:@" + "];
DecodeValue(DECODER_CALL_THROUGH);
[string appendString:@"i)"];
}
DECLARE_DISPATCH(Complex);
static void DecodeUndef(DECODER_PARAMS)
{
/* Used for function pointers and C++ vtables, reused in a silly way to
identify blocks (see DecodeObject()), and possibly other cases.
*/
[string appendString:@"<unknown>"];
}
DECLARE_DISPATCH(Undef);
static void DecodeObject(DECODER_PARAMS)
{
NSCParameterAssert(encoding != NULL && encOffset != NULL);
id value;
/* Special case: block references are encoded as "@?" (_C_ID, _C_UNDEF).
Although blocks are indeed objects, their descriptions aren't useful.
FIXME: extract block type signatures for stuff compiled with clang?
*/
if (encoding[*encOffset] == kSignatureUndef)
{
(*encOffset)++;
[string appendString:@"^block"];
}
else
{
TYPE_NAME(string, @"id");
if (buffer != NULL)
{
const void *valuePointer = ((const void *)(buffer + *bufOffset));
value = *(ARC_UNSAFE_UNRETAINED const id *)valuePointer;
if ([value isKindOfClass:[NSString class]])
{
// Quote strings.
[string appendFormat:@"\"%@\"", value];
}
else
{
NSString *description = nil;
if (value != nil)
{
#if USE_DEBUG_DESCRIPTION
if ([value respondsToSelector:@selector(debugDescription)]) description = [value performSelector:@selector(debugDescription)];
#endif
if (description == nil) description = [value description];
}
else
{
description = @"nil";
}
[string appendString:description];
}
}
else
{
// Not nil - here null refers to the value of a pointer to an object pointer.
[string appendString:@"null"];
}
}
*bufOffset += sizeof value;
}
DECLARE_DISPATCH(Object);
static void DecodeClass(DECODER_PARAMS)
{
NSCParameterAssert(bufOffset != NULL);
TYPE_NAME(string, @"Class");
Class value;
if (buffer != NULL)
{
const void *valuePointer = ((const void *)(buffer + *bufOffset));
value = *(ARC_UNSAFE_UNRETAINED const Class *)valuePointer;
[string appendFormat:@"class %@", value ? NSStringFromClass(value) : @"Nil"];
}
else
{
// Not Nil - here null refers to the value of a pointer to a class pointer.
[string appendString:@"null"];
}
*bufOffset += sizeof value;
}
DECLARE_DISPATCH(Class);
static void DecodeSelector(DECODER_PARAMS)
{
NSCParameterAssert(bufOffset != NULL);
TYPE_NAME(string, @"SEL");
SEL value;
if (buffer != NULL)
{
value = *((const SEL *)(buffer + *bufOffset));
[string appendString:NSStringFromSelector(value)];
}
else
{
// Not Nil - here null refers to the value of a pointer to a class pointer.
[string appendString:@"null"];
}
*bufOffset += sizeof value;
}
DECLARE_DISPATCH(Selector);
static void DecodeAtom(DECODER_PARAMS)
{
NSCParameterAssert(bufOffset != NULL);
// What's an atom? I have no idea, except that it's apparently int-sized.
TYPE_NAME(string, @"atom");
unsigned int value;
if (buffer != NULL)
{
value = *((const unsigned int *)(buffer + *bufOffset));
#if !INCLUDE_TYPE_NAMES
[string appendString:@"atom "];
#endif
[string appendFormat:@"%#x", value];
}
else
{
[string appendString:@"null"];
}
*bufOffset += sizeof(value);
}
DECLARE_DISPATCH(Atom);
static void DecodeVTable(DECODER_PARAMS)
{
NSCParameterAssert(bufOffset != NULL);
TYPE_NAME(string, @"vtable");
const void *value = NULL;
if (buffer != NULL)
{
value = *((const void **)(buffer + *bufOffset));
[string appendFormat:@"<vtable %p>", value];
}
else
{
[string appendString:@"<vtable>"];
}
*bufOffset += sizeof(value);
if (align)
{
*maxAlign = MAX(*maxAlign, kAlignPointer);
}
}
// No DECLARE_DISPATCH(VTable), since it isn't dispatched normally.
static void DecodeStruct(DECODER_PARAMS)
{
NSCParameterAssert(encoding != NULL && encOffset != NULL && bufOffset != NULL);
/* A struct with known contents is encoded as {Name} or {Name=<encoding>},
where Name is a string (or ? if no name is known). <encoding> is a
series of type encodings. For pointers to structs, the first form with
no content encoding is used.
Note that the name may not be an identifier. For C++ template classes,
the template parameters are included, as in {complex<double>=jd}.
*/
size_t nameStart = *encOffset;
while (encoding[*encOffset] != kTokenAggregateNameSeparator && encoding[*encOffset] != kTokenEndOfStruct)
{
(*encOffset)++;
ASSERT_NOT_END_OF_ENCODING(encoding, encOffset);
}
// Pointers to structs don't encode struct layout.
BOOL unknownStruct = encoding[*encOffset] == kTokenEndOfStruct;
NSString *name = nil;
if (INCLUDE_STRUCT_NAMES || unknownStruct)
{
size_t nameLength = *encOffset - nameStart;
if (nameLength != 1 || encoding[nameStart] != kTokenAnonymousAggregate)
{
name = StringWithBytesLengthEncoding(encoding + nameStart, nameLength, NSUTF8StringEncoding);
}
if (name == nil) name = @"<anonymous struct>";
}
#if INCLUDE_STRUCT_NAMES
[string appendFormat:@"(%@)", name];
#endif
if (unknownStruct)
{
#if INCLUDE_STRUCT_NAMES
[string appendString:@"{...}"];
#else
[string appendFormat:@"{%@...}", name];
#endif
}
else
{
// Skip kTokenAggregateNameSeparator.
(*encOffset)++;
ASSERT_NOT_END_OF_ENCODING(encoding, encOffset);
[string appendString:@"{ "];
BOOL first = YES;
// Check for C++ vtable (^^?).
if (encoding[*encOffset] == kSignaturePointer &&
encoding[*encOffset + 1] == kSignaturePointer &&
encoding[*encOffset + 2] == kSignatureUndef)
{
DecodeVTable(DECODER_CALL_THROUGH_ALIGN(YES));
*encOffset += 3;
first = NO;
}
while (encoding[*encOffset] != kTokenEndOfStruct)
{
if (!first)
{
[string appendString:@", "];
}
else
{
first = NO;
}
DecodeValue(DECODER_CALL_THROUGH_ALIGN(YES));
}
[string appendString:@" }"];
}
// Skip kTokenEndOfStruct.
(*encOffset)++;
}
DECLARE_DISPATCH(Struct);
static inline NSString *ExtractString(const char *strPtr, size_t maxLength)
{
if (strPtr == NULL || maxLength == 0) return @"";
size_t length = 0;
while (length < maxLength && strPtr[length] != '\0') length++;
NSString *str = StringWithBytesLengthEncoding(strPtr, length, NSUTF8StringEncoding);
if (str == nil)
{
str = StringWithBytesLengthEncoding(strPtr, length, [NSString defaultCStringEncoding]);
}
if (str == nil)
{
str = StringWithBytesLengthEncoding(strPtr, length, NSISOLatin1StringEncoding);
}
return str;
}
static void DecodeArray(DECODER_PARAMS)
{
/* An array is encoded as [<count><itemType>], where <count> is an
unsigned decimal integer and <itemType> is a type encoding.
*/
NSCParameterAssert(encoding != NULL && encOffset != NULL && bufOffset != NULL);
size_t count = 0;
while (isdigit(encoding[*encOffset]))
{
count = count * 10 + encoding[*encOffset] - '0';
(*encOffset)++;
ASSERT_NOT_END_OF_ENCODING(encoding, encOffset);
}
// Find kTokenEndOfArray.
size_t contentEncStart = *encOffset;
while (encoding[*encOffset] != kTokenEndOfArray)
{
(*encOffset)++;
ASSERT_NOT_END_OF_ENCODING(encoding, encOffset);
}
if (*encOffset - contentEncStart == 1 && encoding[contentEncStart] == kSignatureChar)
{
// Special case to show char arrays as strings.
if (buffer != NULL)
{
[string appendFormat:@"\"%@\"", ExtractString((const char *)buffer + *bufOffset, count)];
}
else
{
[string appendFormat:@"[null]"];
}
*bufOffset += count;
}
else
{
#if INCLUDE_ARRAY_COUNTS
[string appendFormat:@"(%zu)", count];
#endif
[string appendString:@"[ "];
// Iterate over array contents.
BOOL first = YES;
for (size_t iter = 0; iter < count; iter++)
{
if (!first)
{
[string appendString:@", "];
}
else
{
first = NO;
}
size_t subEncoding = contentEncStart;
DecodeValue(encoding, &subEncoding, buffer, bufOffset, string, maxAlign, YES);
}
[string appendString:@" ]"];
}
// Skip kTokenEndOfArray.
(*encOffset)++;
}
DECLARE_DISPATCH(Array);
static void DecodeUnion(DECODER_PARAMS)
{
/* Not very surprisingly, a union is laid out much like a struct, except
that each type declaration refers to the same block of memory. In
order to avoid misalignment in aggregates, we must advance by the
length of the longest encoded type. This requires us to decode each
element of the union.
If INCLUDE_ALL_UNION_INFO is nonzero, we also print the content of
each element. Otherwise, we just print the first "branch".
*/
NSCParameterAssert(encoding != NULL && encOffset != NULL && bufOffset != NULL);
size_t nameStart = *encOffset;
while (encoding[*encOffset] !=kTokenAggregateNameSeparator && encoding[*encOffset] != kTokenEndOfUnion)
{
(*encOffset)++;
ASSERT_NOT_END_OF_ENCODING(encoding, encOffset);
}
// Pointers to unions don't encode struct layout.
BOOL unknownStruct = encoding[*encOffset] == kTokenEndOfUnion;
NSString *name = nil;
if (INCLUDE_STRUCT_NAMES || unknownStruct)
{
size_t nameLength = *encOffset - nameStart;
if (nameLength != 1 || encoding[nameStart] != kTokenAnonymousAggregate)
{
name = StringWithBytesLengthEncoding(encoding + nameStart, nameLength, NSUTF8StringEncoding);
name = [@"union " stringByAppendingString:name];
}
if (name == nil) name = @"<anonymous union>";
}
#if INCLUDE_STRUCT_NAMES
[string appendFormat:@"(%@)", name];
#endif
if (unknownStruct)
{
#if INCLUDE_STRUCT_NAMES
[string appendString:@"{...}"];
#else
[string appendFormat:@"{%@...}", name];
#endif
}
else
{
// Skip kTokenAggregateNameSeparator.
(*encOffset)++;
ASSERT_NOT_END_OF_ENCODING(encoding, encOffset);
[string appendString:@"{ "];
BOOL first = YES;
size_t start = *bufOffset;
size_t end = start;
NSMutableString *subString = string;
while (encoding[*encOffset] != kTokenEndOfUnion)
{
if (!first)
{
[subString appendString:@" == "];
}
else
{
first = NO;
}
size_t offset = start;
DecodeValue(encoding, encOffset, buffer, &offset, subString, maxAlign, YES);
if (offset > end) end = offset;
#if !INCLUDE_ALL_UNION_INFO
subString = nil;
#endif
}
*bufOffset = end;
[string appendString:@" }"];
}
// Skip kTokenEndOfUnion.
(*encOffset)++;
}
DECLARE_DISPATCH(Union);
static void DecodeCharPointer(DECODER_PARAMS)
{
NSCParameterAssert(encoding != NULL && encOffset != NULL && bufOffset != NULL);
const char *value = NULL;
TYPE_NAME(string, @"char *");
if (buffer != NULL)
{
value = *((const char **)(buffer + *bufOffset));
[string appendFormat:@"\"%s\"", value];
}
else
{
[string appendString:@"null"];
}
*bufOffset += sizeof value;
}
DECLARE_DISPATCH(CharPointer);
static void DecodePointer(DECODER_PARAMS)
{
NSCParameterAssert(encoding != NULL && encOffset != NULL && bufOffset != NULL);
if (encoding[*encOffset] == kSignatureChar)
{
(*encOffset)++;
DecodeCharPointer(DECODER_CALL_THROUGH);
return;
}
const void *value = NULL;
if (buffer != NULL)
{
value = *((const void **)(buffer + *bufOffset));
}
*bufOffset += sizeof value;
[string appendString:@"&"];
#if INCLUDE_POINTERS
[string appendFormat:@"[%p]", value];
#endif
size_t subBufOffset = 0;
// Even if pointer is NULL, we need to continue parsing the encoding string if we're in an aggregate.
DecodeValue(encoding, encOffset, (const uint8_t *)value, &subBufOffset, string, maxAlign, NO);
}
DECLARE_DISPATCH(Pointer);
static void DecodeBitfield(DECODER_PARAMS)
{
[NSException raise:kJAValueToStringParseError format:@"bitfields are not supported"];
}
DECLARE_DISPATCH(Bitfield);
static const DispatchEntry *sDispatchTable[] =
{
/* NOTE: these must be listed in lexicographic order by signature. To
generate the proper order, run with SORT_DISPATCH_TABLE defined.
*/
&sDispatchClass,
&sDispatchAtom,
&sDispatchUnion,
&sDispatchCharPointer,
&sDispatchSelector,
&sDispatchUndef,
&sDispatchObject,
&sDispatchBool,