- Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCFBinaryPList.c
1442 lines (1313 loc) · 59.8 KB
/
CFBinaryPList.c
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
/*
* Copyright (c) 2008-2012 Brent Fulgham <bfulgham@gmail.org>. All rights reserved.
* Copyright (c) 2009 Stuart Crook <stuart@echus.demon.co.uk>. All rights reserved.
*
* This source code is a modified version of the CoreFoundation sources released by Apple Inc. under
* the terms of the APSL version 2.0 (see below).
*
* For information about changes from the original Apple source release can be found by reviewing the
* source control system for the project at https://sourceforge.net/svn/?group_id=246198.
*
* The original license information is as follows:
*
* Copyright (c) 2012 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/* CFBinaryPList.c
Copyright (c) 2000-2011, Apple Inc. All rights reserved.
Responsibility: Tony Parker
*/
#include<CoreFoundation/CFString.h>
#include<CoreFoundation/CFNumber.h>
#include<CoreFoundation/CFDate.h>
#include<CoreFoundation/CFData.h>
#include<CoreFoundation/CFError.h>
#include<CoreFoundation/CFArray.h>
#include<CoreFoundation/CFDictionary.h>
#include<CoreFoundation/CFSet.h>
#include<CoreFoundation/CFPropertyList.h>
#include<CoreFoundation/CFByteOrder.h>
#include<CoreFoundation/CFRuntime.h>
#include<stdio.h>
#include<limits.h>
#include<string.h>
#include<CoreFoundation/CoreFoundation_Prefix.h>
#include"CFInternal.h"
#ifDEPLOYMENT_TARGET_MACOSX||DEPLOYMENT_TARGET_EMBEDDED||DEPLOYMENT_TARGET_WINDOWS||DEPLOYMENT_TARGET_LINUX||DEPLOYMENT_TARGET_FREEBSD
#include<CoreFoundation/CFStream.h>
#endif
typedefstruct {
int64_thigh;
uint64_tlow;
} CFSInt128Struct;
enum {
kCFNumberSInt128Type=17
};
CF_EXPORTCFNumberType_CFNumberGetType2(CFNumberRefnumber);
__private_extern__CFErrorRef__CFPropertyListCreateError(CFIndexcode, CFStringRefdebugString, ...);
enum {
CF_NO_ERROR=0,
CF_OVERFLOW_ERROR= (1 << 0),
};
CF_INLINEuint32_t__check_uint32_add_unsigned_unsigned(uint32_tx, uint32_ty, int32_t*err) {
if((UINT_MAX-y) <x)
*err=*err | CF_OVERFLOW_ERROR;
returnx+y;
};
CF_INLINEuint64_t__check_uint64_add_unsigned_unsigned(uint64_tx, uint64_ty, int32_t*err) {
if((ULLONG_MAX-y) <x)
*err=*err | CF_OVERFLOW_ERROR;
returnx+y;
};
CF_INLINEuint32_t__check_uint32_mul_unsigned_unsigned(uint32_tx, uint32_ty, int32_t*err) {
uint64_ttmp= (uint64_t) x* (uint64_t) y;
/* If any of the upper 32 bits touched, overflow */
if(tmp&0xffffffff00000000ULL)
*err=*err | CF_OVERFLOW_ERROR;
return (uint32_t) tmp;
};
CF_INLINEuint64_t__check_uint64_mul_unsigned_unsigned(uint64_tx, uint64_ty, int32_t*err) {
if(x==0) return0;
if(ULLONG_MAX/x<y)
*err=*err | CF_OVERFLOW_ERROR;
returnx*y;
};
#if__LP64__
#definecheck_ptr_add(p, a, err) (const uint8_t *)__check_uint64_add_unsigned_unsigned((uintptr_t)p, (uintptr_t)a, err)
#definecheck_size_t_mul(b, a, err) (size_t)__check_uint64_mul_unsigned_unsigned((size_t)b, (size_t)a, err)
#else
#definecheck_ptr_add(p, a, err) (const uint8_t *)__check_uint32_add_unsigned_unsigned((uintptr_t)p, (uintptr_t)a, err)
#definecheck_size_t_mul(b, a, err) (size_t)__check_uint32_mul_unsigned_unsigned((size_t)b, (size_t)a, err)
#endif
struct__CFKeyedArchiverUID {
CFRuntimeBase_base;
uint32_t_value;
};
staticCFStringRef__CFKeyedArchiverUIDCopyDescription(CFTypeRefcf) {
CFKeyedArchiverUIDRefuid= (CFKeyedArchiverUIDRef)cf;
returnCFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("<CFKeyedArchiverUID %p [%p]>{value = %u}"), cf, CFGetAllocator(cf), uid->_value);
}
staticCFStringRef__CFKeyedArchiverUIDCopyFormattingDescription(CFTypeRefcf, CFDictionaryRefformatOptions) {
CFKeyedArchiverUIDRefuid= (CFKeyedArchiverUIDRef)cf;
returnCFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("@%u@"), uid->_value);
}
staticCFTypeID__kCFKeyedArchiverUIDTypeID=_kCFRuntimeNotATypeID;
staticconstCFRuntimeClass__CFKeyedArchiverUIDClass= {
0,
"CFKeyedArchiverUID",
NULL, // init
NULL, // copy
NULL, // finalize
NULL, // equal -- pointer equality only
NULL, // hash -- pointer hashing only
__CFKeyedArchiverUIDCopyFormattingDescription,
__CFKeyedArchiverUIDCopyDescription
};
__private_extern__void__CFKeyedArchiverUIDInitialize(void) {
__kCFKeyedArchiverUIDTypeID=_CFRuntimeRegisterClass(&__CFKeyedArchiverUIDClass);
}
CFTypeID_CFKeyedArchiverUIDGetTypeID(void) {
return__kCFKeyedArchiverUIDTypeID;
}
CFKeyedArchiverUIDRef_CFKeyedArchiverUIDCreate(CFAllocatorRefallocator, uint32_tvalue) {
CFKeyedArchiverUIDRefuid;
uid= (CFKeyedArchiverUIDRef)_CFRuntimeCreateInstance(allocator, __kCFKeyedArchiverUIDTypeID, sizeof(struct__CFKeyedArchiverUID) -sizeof(CFRuntimeBase), NULL);
if (NULL==uid) {
returnNULL;
}
((struct__CFKeyedArchiverUID*)uid)->_value=value;
returnuid;
}
uint32_t_CFKeyedArchiverUIDGetValue(CFKeyedArchiverUIDRefuid) {
returnuid->_value;
}
typedefstruct {
CFTypeRefstream;
CFErrorReferror;
uint64_twritten;
int32_tused;
boolstreamIsData;
uint8_tbuffer[8192-32];
} __CFBinaryPlistWriteBuffer;
staticvoidwriteBytes(__CFBinaryPlistWriteBuffer*buf, constUInt8*bytes, CFIndexlength) {
if (0==length) return;
if (buf->error) return;
if (buf->streamIsData) {
CFDataAppendBytes((CFMutableDataRef)buf->stream, bytes, length);
buf->written+=length;
} else {
#ifDEPLOYMENT_TARGET_MACOSX||DEPLOYMENT_TARGET_EMBEDDED||DEPLOYMENT_TARGET_WINDOWS||DEPLOYMENT_TARGET_LINUX||DEPLOYMENT_TARGET_FREEBSD
while (0<length) {
CFIndexret=CFWriteStreamWrite((CFWriteStreamRef)buf->stream, bytes, length);
if (ret==0) {
buf->error=__CFPropertyListCreateError(kCFPropertyListWriteStreamError, CFSTR("Binary property list writing could not be completed because stream is full."));
return;
}
if (ret<0) {
CFErrorReferr=CFWriteStreamCopyError((CFWriteStreamRef)buf->stream);
buf->error=err ? err : __CFPropertyListCreateError(kCFPropertyListWriteStreamError, CFSTR("Binary property list writing could not be completed because the stream had an unknown error."));
return;
}
buf->written+=ret;
length-=ret;
bytes+=ret;
}
#else
CFAssert(false, __kCFLogAssertion, "Streams are not supported on this platform");
#endif
}
}
staticvoidbufferWrite(__CFBinaryPlistWriteBuffer*buf, constuint8_t*buffer, CFIndexcount) {
if (0==count) return;
if ((CFIndex)sizeof(buf->buffer) <= count) {
writeBytes(buf, buf->buffer, buf->used);
buf->used=0;
writeBytes(buf, buffer, count);
return;
}
CFIndexcopyLen=__CFMin(count, (CFIndex)sizeof(buf->buffer) -buf->used);
memmove(buf->buffer+buf->used, buffer, copyLen);
buf->used+=copyLen;
if (sizeof(buf->buffer) ==buf->used) {
writeBytes(buf, buf->buffer, sizeof(buf->buffer));
memmove(buf->buffer, buffer+copyLen, count-copyLen);
buf->used=count-copyLen;
}
}
staticvoidbufferFlush(__CFBinaryPlistWriteBuffer*buf) {
writeBytes(buf, buf->buffer, buf->used);
buf->used=0;
}
/*
HEADER
magic number ("bplist")
file format version
OBJECT TABLE
variable-sized objects
Object Formats (marker byte followed by additional info in some cases)
null 0000 0000
bool 0000 1000 // false
bool 0000 1001 // true
fill 0000 1111 // fill byte
int 0001 nnnn ... // # of bytes is 2^nnnn, big-endian bytes
real 0010 nnnn ... // # of bytes is 2^nnnn, big-endian bytes
date 0011 0011 ... // 8 byte float follows, big-endian bytes
data 0100 nnnn [int] ... // nnnn is number of bytes unless 1111 then int count follows, followed by bytes
string 0101 nnnn [int] ... // ASCII string, nnnn is # of chars, else 1111 then int count, then bytes
string 0110 nnnn [int] ... // Unicode string, nnnn is # of chars, else 1111 then int count, then big-endian 2-byte uint16_t
0111 xxxx // unused
uid 1000 nnnn ... // nnnn+1 is # of bytes
1001 xxxx // unused
array 1010 nnnn [int] objref* // nnnn is count, unless '1111', then int count follows
1011 xxxx // unused
set 1100 nnnn [int] objref* // nnnn is count, unless '1111', then int count follows
dict 1101 nnnn [int] keyref* objref* // nnnn is count, unless '1111', then int count follows
1110 xxxx // unused
1111 xxxx // unused
OFFSET TABLE
list of ints, byte size of which is given in trailer
-- these are the byte offsets into the file
-- number of these is in the trailer
TRAILER
byte size of offset ints in offset table
byte size of object refs in arrays and dicts
number of offsets in offset table (also is number of objects)
element # in offset table which is top level object
offset table offset
*/
staticCFTypeIDstringtype=-1, datatype=-1, numbertype=-1, datetype=-1;
staticCFTypeIDbooltype=-1, nulltype=-1, dicttype=-1, arraytype=-1, settype=-1;
staticvoidinitStatics() {
if ((CFTypeID)-1==stringtype) {
stringtype=CFStringGetTypeID();
}
if ((CFTypeID)-1==datatype) {
datatype=CFDataGetTypeID();
}
if ((CFTypeID)-1==numbertype) {
numbertype=CFNumberGetTypeID();
}
if ((CFTypeID)-1==booltype) {
booltype=CFBooleanGetTypeID();
}
if ((CFTypeID)-1==datetype) {
datetype=CFDateGetTypeID();
}
if ((CFTypeID)-1==dicttype) {
dicttype=CFDictionaryGetTypeID();
}
if ((CFTypeID)-1==arraytype) {
arraytype=CFArrayGetTypeID();
}
if ((CFTypeID)-1==settype) {
settype=CFSetGetTypeID();
}
if ((CFTypeID)-1==nulltype) {
nulltype=CFNullGetTypeID();
}
}
staticvoid_appendInt(__CFBinaryPlistWriteBuffer*buf, uint64_tbigint) {
uint8_tmarker;
uint8_t*bytes;
CFIndexnbytes;
if (bigint <= (uint64_t)0xff) {
nbytes=1;
marker=kCFBinaryPlistMarkerInt | 0;
} elseif (bigint <= (uint64_t)0xffff) {
nbytes=2;
marker=kCFBinaryPlistMarkerInt | 1;
} elseif (bigint <= (uint64_t)0xffffffff) {
nbytes=4;
marker=kCFBinaryPlistMarkerInt | 2;
} else {
nbytes=8;
marker=kCFBinaryPlistMarkerInt | 3;
}
bigint=CFSwapInt64HostToBig(bigint);
bytes= (uint8_t*)&bigint+sizeof(bigint) -nbytes;
bufferWrite(buf, &marker, 1);
bufferWrite(buf, bytes, nbytes);
}
staticvoid_appendUID(__CFBinaryPlistWriteBuffer*buf, CFKeyedArchiverUIDRefuid) {
uint8_tmarker;
uint8_t*bytes;
CFIndexnbytes;
uint64_tbigint=_CFKeyedArchiverUIDGetValue(uid);
if (bigint <= (uint64_t)0xff) {
nbytes=1;
} elseif (bigint <= (uint64_t)0xffff) {
nbytes=2;
} elseif (bigint <= (uint64_t)0xffffffff) {
nbytes=4;
} else {
nbytes=8;
}
marker=kCFBinaryPlistMarkerUID | (uint8_t)(nbytes-1);
bigint=CFSwapInt64HostToBig(bigint);
bytes= (uint8_t*)&bigint+sizeof(bigint) -nbytes;
bufferWrite(buf, &marker, 1);
bufferWrite(buf, bytes, nbytes);
}
staticvoid_flattenPlist(CFPropertyListRefplist, CFMutableArrayRefobjlist, CFMutableDictionaryRefobjtable, CFMutableSetRefuniquingset) {
CFPropertyListRefunique;
uint32_trefnum;
CFTypeIDtype=CFGetTypeID(plist);
CFIndexidx;
CFPropertyListRef*list, buffer[256];
// Do not unique dictionaries or arrays, because: they
// are slow to compare, and have poor hash codes.
// Uniquing bools is unnecessary.
if (stringtype==type||numbertype==type||datetype==type||datatype==type) {
CFIndexbefore=CFSetGetCount(uniquingset);
CFSetAddValue(uniquingset, plist);
CFIndexafter=CFSetGetCount(uniquingset);
if (after==before) { // already in set
unique=CFSetGetValue(uniquingset, plist);
if (unique!=plist) {
refnum= (uint32_t)(uintptr_t)CFDictionaryGetValue(objtable, unique);
CFDictionaryAddValue(objtable, plist, (constvoid*)(uintptr_t)refnum);
}
return;
}
}
refnum=CFArrayGetCount(objlist);
CFArrayAppendValue(objlist, plist);
CFDictionaryAddValue(objtable, plist, (constvoid*)(uintptr_t)refnum);
if (dicttype==type) {
CFIndexcount=CFDictionaryGetCount((CFDictionaryRef)plist);
list= (count <= 128) ? buffer : (CFPropertyListRef*)CFAllocatorAllocate(kCFAllocatorSystemDefaultGCRefZero, 2*count*sizeof(CFTypeRef), __kCFAllocatorGCScannedMemory);
CFDictionaryGetKeysAndValues((CFDictionaryRef)plist, list, list+count);
for (idx=0; idx<2*count; idx++) {
_flattenPlist(list[idx], objlist, objtable, uniquingset);
}
if (list!=buffer) CFAllocatorDeallocate(kCFAllocatorSystemDefaultGCRefZero, list);
} elseif (arraytype==type) {
CFIndexcount=CFArrayGetCount((CFArrayRef)plist);
list= (count <= 256) ? buffer : (CFPropertyListRef*)CFAllocatorAllocate(kCFAllocatorSystemDefaultGCRefZero, count*sizeof(CFTypeRef), __kCFAllocatorGCScannedMemory);
CFArrayGetValues((CFArrayRef)plist, CFRangeMake(0, count), list);
for (idx=0; idx<count; idx++) {
_flattenPlist(list[idx], objlist, objtable, uniquingset);
}
if (list!=buffer) CFAllocatorDeallocate(kCFAllocatorSystemDefaultGCRefZero, list);
}
}
/* Get the number of bytes required to hold the value in 'count'. Will return a power of 2 value big enough to hold 'count'.
*/
CF_INLINEuint8_t_byteCount(uint64_tcount) {
uint64_tmask= ~(uint64_t)0;
uint8_tsize=0;
// Find something big enough to hold 'count'
while (count&mask) {
size++;
mask=mask << 8;
}
// Ensure that 'count' is a power of 2
// For sizes bigger than 8, just use the required count
while ((size!=1&&size!=2&&size!=4&&size!=8) &&size <= 8) {
size++;
}
returnsize;
}
// stream can be a CFWriteStreamRef (on supported platforms) or a CFMutableDataRef
/* Write a property list to a stream, in binary format. plist is the property list to write (one of the basic property list types), stream is the destination of the property list, and estimate is a best-guess at the total number of objects in the property list. The estimate parameter is for efficiency in pre-allocating memory for the uniquing step. Pass in a 0 if no estimate is available. The options flag specifies sort options. If the error parameter is non-NULL and an error occurs, it will be used to return a CFError explaining the problem. It is the callers responsibility to release the error. */
CFIndex__CFBinaryPlistWrite(CFPropertyListRefplist, CFTypeRefstream, uint64_testimate, CFOptionFlagsoptions, CFErrorRef*error) {
CFMutableDictionaryRefobjtable;
CFMutableArrayRefobjlist;
CFMutableSetRefuniquingset;
CFBinaryPlistTrailertrailer;
uint64_t*offsets, length_so_far;
uint64_trefnum;
int64_tidx, idx2, cnt;
__CFBinaryPlistWriteBuffer*buf;
initStatics();
constCFDictionaryKeyCallBacksdictKeyCallbacks= {0, __CFTypeCollectionRetain, __CFTypeCollectionRelease, 0, 0, 0};
objtable=CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &dictKeyCallbacks, NULL);
constCFArrayCallBacksarrayCallbacks= {0, __CFTypeCollectionRetain, __CFTypeCollectionRelease, 0, 0};
objlist=CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &arrayCallbacks);
constCFSetCallBackssetCallbacks= {0, __CFTypeCollectionRetain, __CFTypeCollectionRelease, 0, 0, 0};
uniquingset=CFSetCreateMutable(kCFAllocatorSystemDefault, 0, &setCallbacks);
#ifDEPLOYMENT_TARGET_MACOSX
_CFDictionarySetCapacity(objtable, estimate ? estimate : 650);
_CFArraySetCapacity(objlist, estimate ? estimate : 650);
_CFSetSetCapacity(uniquingset, estimate ? estimate : 1000);
#endif
_flattenPlist(plist, objlist, objtable, uniquingset);
CFRelease(uniquingset);
cnt=CFArrayGetCount(objlist);
offsets= (uint64_t*)CFAllocatorAllocate(kCFAllocatorSystemDefaultGCRefZero, (CFIndex)(cnt*sizeof(*offsets)), 0);
buf= (__CFBinaryPlistWriteBuffer*)CFAllocatorAllocate(kCFAllocatorSystemDefaultGCRefZero, sizeof(__CFBinaryPlistWriteBuffer), 0);
buf->stream=stream;
buf->error=NULL;
buf->streamIsData= (CFGetTypeID(stream) ==CFDataGetTypeID());
buf->written=0;
buf->used=0;
bufferWrite(buf, (uint8_t*)"bplist00", 8); // header
memset(&trailer, 0, sizeof(trailer));
trailer._numObjects=CFSwapInt64HostToBig(cnt);
trailer._topObject=0; // true for this implementation
trailer._objectRefSize=_byteCount(cnt);
for (idx=0; idx<cnt; idx++) {
CFPropertyListRefobj=CFArrayGetValueAtIndex(objlist, (CFIndex)idx);
CFTypeIDtype=CFGetTypeID(obj);
offsets[idx] =buf->written+buf->used;
if (stringtype==type) {
CFIndexret, count=CFStringGetLength((CFStringRef)obj);
CFIndexneeded;
uint8_t*bytes, buffer[1024];
bytes= (count <= 1024) ? buffer : (uint8_t*)CFAllocatorAllocate(kCFAllocatorSystemDefaultGCRefZero, count, 0);
// presumption, believed to be true, is that ASCII encoding may need
// less bytes, but will not need greater, than the # of unichars
ret=CFStringGetBytes((CFStringRef)obj, CFRangeMake(0, count), kCFStringEncodingASCII, 0, false, bytes, count, &needed);
if (ret==count) {
uint8_tmarker= (uint8_t)(kCFBinaryPlistMarkerASCIIString | (needed<15 ? needed : 0xf));
bufferWrite(buf, &marker, 1);
if (15 <= needed) {
_appendInt(buf, (uint64_t)needed);
}
bufferWrite(buf, bytes, needed);
} else {
UniChar*chars;
uint8_tmarker= (uint8_t)(kCFBinaryPlistMarkerUnicode16String | (count<15 ? count : 0xf));
bufferWrite(buf, &marker, 1);
if (15 <= count) {
_appendInt(buf, (uint64_t)count);
}
chars= (UniChar*)CFAllocatorAllocate(kCFAllocatorSystemDefaultGCRefZero, count*sizeof(UniChar), 0);
CFStringGetCharacters((CFStringRef)obj, CFRangeMake(0, count), chars);
for (idx2=0; idx2<count; idx2++) {
chars[idx2] =CFSwapInt16HostToBig(chars[idx2]);
}
bufferWrite(buf, (uint8_t*)chars, count*sizeof(UniChar));
CFAllocatorDeallocate(kCFAllocatorSystemDefaultGCRefZero, chars);
}
if (bytes!=buffer) CFAllocatorDeallocate(kCFAllocatorSystemDefaultGCRefZero, bytes);
} elseif (numbertype==type) {
uint8_tmarker;
uint64_tbigint;
uint8_t*bytes;
CFIndexnbytes;
if (CFNumberIsFloatType((CFNumberRef)obj)) {
CFSwappedFloat64swapped64;
CFSwappedFloat32swapped32;
if (CFNumberGetByteSize((CFNumberRef)obj) <= (CFIndex)sizeof(float)) {
floatv;
CFNumberGetValue((CFNumberRef)obj, kCFNumberFloat32Type, &v);
swapped32=CFConvertFloat32HostToSwapped(v);
bytes= (uint8_t*)&swapped32;
nbytes=sizeof(float);
marker=kCFBinaryPlistMarkerReal | 2;
} else {
doublev;
CFNumberGetValue((CFNumberRef)obj, kCFNumberFloat64Type, &v);
swapped64=CFConvertFloat64HostToSwapped(v);
bytes= (uint8_t*)&swapped64;
nbytes=sizeof(double);
marker=kCFBinaryPlistMarkerReal | 3;
}
bufferWrite(buf, &marker, 1);
bufferWrite(buf, bytes, nbytes);
} else {
CFNumberTypetype=_CFNumberGetType2((CFNumberRef)obj);
if (kCFNumberSInt128Type==type) {
CFSInt128Structs;
CFNumberGetValue((CFNumberRef)obj, kCFNumberSInt128Type, &s);
struct {
int64_thigh;
uint64_tlow;
} storage;
storage.high=CFSwapInt64HostToBig(s.high);
storage.low=CFSwapInt64HostToBig(s.low);
uint8_t*bytes= (uint8_t*)&storage;
uint8_tmarker=kCFBinaryPlistMarkerInt | 4;
CFIndexnbytes=16;
bufferWrite(buf, &marker, 1);
bufferWrite(buf, bytes, nbytes);
} else {
CFNumberGetValue((CFNumberRef)obj, kCFNumberSInt64Type, &bigint);
_appendInt(buf, bigint);
}
}
} elseif (_CFKeyedArchiverUIDGetTypeID() ==type) {
_appendUID(buf, (CFKeyedArchiverUIDRef)obj);
} elseif (booltype==type) {
uint8_tmarker=CFBooleanGetValue((CFBooleanRef)obj) ? kCFBinaryPlistMarkerTrue : kCFBinaryPlistMarkerFalse;
bufferWrite(buf, &marker, 1);
} elseif (datatype==type) {
CFIndexcount=CFDataGetLength((CFDataRef)obj);
uint8_tmarker= (uint8_t)(kCFBinaryPlistMarkerData | (count<15 ? count : 0xf));
bufferWrite(buf, &marker, 1);
if (15 <= count) {
_appendInt(buf, (uint64_t)count);
}
bufferWrite(buf, CFDataGetBytePtr((CFDataRef)obj), count);
} elseif (datetype==type) {
CFSwappedFloat64swapped;
uint8_tmarker=kCFBinaryPlistMarkerDate;
bufferWrite(buf, &marker, 1);
swapped=CFConvertFloat64HostToSwapped(CFDateGetAbsoluteTime((CFDateRef)obj));
bufferWrite(buf, (uint8_t*)&swapped, sizeof(swapped));
} elseif (dicttype==type) {
CFIndexcount=CFDictionaryGetCount((CFDictionaryRef)obj);
uint8_tmarker= (uint8_t)(kCFBinaryPlistMarkerDict | (count<15 ? count : 0xf));
bufferWrite(buf, &marker, 1);
if (15 <= count) {
_appendInt(buf, (uint64_t)count);
}
CFPropertyListRef*list, buffer[512];
list= (count <= 256) ? buffer : (CFPropertyListRef*)CFAllocatorAllocate(kCFAllocatorSystemDefaultGCRefZero, 2*count*sizeof(CFTypeRef), __kCFAllocatorGCScannedMemory);
CFDictionaryGetKeysAndValues((CFDictionaryRef)obj, list, list+count);
for (idx2=0; idx2<2*count; idx2++) {
CFPropertyListRefvalue=list[idx2];
uint32_tswapped=0;
uint8_t*source= (uint8_t*)&swapped;
refnum= (uint32_t)(uintptr_t)CFDictionaryGetValue(objtable, value);
swapped=CFSwapInt32HostToBig((uint32_t)refnum);
bufferWrite(buf, source+sizeof(swapped) -trailer._objectRefSize, trailer._objectRefSize);
}
if (list!=buffer) CFAllocatorDeallocate(kCFAllocatorSystemDefaultGCRefZero, list);
} elseif (arraytype==type) {
CFIndexcount=CFArrayGetCount((CFArrayRef)obj);
CFPropertyListRef*list, buffer[256];
uint8_tmarker= (uint8_t)(kCFBinaryPlistMarkerArray | (count<15 ? count : 0xf));
bufferWrite(buf, &marker, 1);
if (15 <= count) {
_appendInt(buf, (uint64_t)count);
}
list= (count <= 256) ? buffer : (CFPropertyListRef*)CFAllocatorAllocate(kCFAllocatorSystemDefaultGCRefZero, count*sizeof(CFTypeRef), __kCFAllocatorGCScannedMemory);
CFArrayGetValues((CFArrayRef)obj, CFRangeMake(0, count), list);
for (idx2=0; idx2<count; idx2++) {
CFPropertyListRefvalue=list[idx2];
uint32_tswapped=0;
uint8_t*source= (uint8_t*)&swapped;
refnum= (uint32_t)(uintptr_t)CFDictionaryGetValue(objtable, value);
swapped=CFSwapInt32HostToBig((uint32_t)refnum);
bufferWrite(buf, source+sizeof(swapped) -trailer._objectRefSize, trailer._objectRefSize);
}
if (list!=buffer) CFAllocatorDeallocate(kCFAllocatorSystemDefaultGCRefZero, list);
} else {
CFRelease(objtable);
CFRelease(objlist);
if (error&&buf->error) {
// caller will release error
*error=buf->error;
} elseif (buf->error) {
// caller is not interested in error, release it here
CFRelease(buf->error);
}
CFAllocatorDeallocate(kCFAllocatorSystemDefaultGCRefZero, buf);
CFAllocatorDeallocate(kCFAllocatorSystemDefaultGCRefZero, offsets);
return0;
}
}
CFRelease(objtable);
CFRelease(objlist);
length_so_far=buf->written+buf->used;
trailer._offsetTableOffset=CFSwapInt64HostToBig(length_so_far);
trailer._offsetIntSize=_byteCount(length_so_far);
for (idx=0; idx<cnt; idx++) {
uint64_tswapped=CFSwapInt64HostToBig(offsets[idx]);
uint8_t*source= (uint8_t*)&swapped;
bufferWrite(buf, source+sizeof(*offsets) -trailer._offsetIntSize, trailer._offsetIntSize);
}
length_so_far+=cnt*trailer._offsetIntSize;
CFAllocatorDeallocate(kCFAllocatorSystemDefaultGCRefZero, offsets);
bufferWrite(buf, (uint8_t*)&trailer, sizeof(trailer));
bufferFlush(buf);
length_so_far+=sizeof(trailer);
if (buf->error) {
if (error) {
// caller will release error
*error=buf->error;
} else {
CFRelease(buf->error);
}
CFAllocatorDeallocate(kCFAllocatorSystemDefaultGCRefZero, buf);
return0;
}
CFAllocatorDeallocate(kCFAllocatorSystemDefaultGCRefZero, buf);
return (CFIndex)length_so_far;
}
CFIndex__CFBinaryPlistWriteToStream(CFPropertyListRefplist, CFTypeRefstream) {
return__CFBinaryPlistWrite(plist, stream, 0, 0, NULL);
}
// to be removed soon
CFIndex__CFBinaryPlistWriteToStreamWithEstimate(CFPropertyListRefplist, CFTypeRefstream, uint64_testimate) {
return__CFBinaryPlistWrite(plist, stream, estimate, 0, NULL);
}
// to be removed soon
CFIndex__CFBinaryPlistWriteToStreamWithOptions(CFPropertyListRefplist, CFTypeRefstream, uint64_testimate, CFOptionFlagsoptions) {
return__CFBinaryPlistWrite(plist, stream, estimate, options, NULL);
}
#defineFAIL_FALSE do { return false; } while (0)
#defineFAIL_MAXOFFSET do { return UINT64_MAX; } while (0)
/* Grab a valSize-bytes integer out of the buffer pointed at by data and return it.
*/
CF_INLINEuint64_t_getSizedInt(constuint8_t*data, uint8_tvalSize) {
#if defined(__i386__) || defined(__x86_64__)
if (valSize==1) {
return (uint64_t)*data;
} elseif (valSize==2) {
uint16_tval=*(uint16_t*)data;
return (uint64_t)CFSwapInt16BigToHost(val);
} elseif (valSize==4) {
uint32_tval=*(uint32_t*)data;
return (uint64_t)CFSwapInt32BigToHost(val);
} elseif (valSize==8) {
uint64_tval=*(uint64_t*)data;
returnCFSwapInt64BigToHost(val);
}
#endif
// Compatability with existing archives, including anything with a non-power-of-2
// size and 16-byte values, and architectures that don't support unaligned access
uint64_tres=0;
for (CFIndexidx=0; idx<valSize; idx++) {
res= (res << 8) +data[idx];
}
returnres;
}
bool__CFBinaryPlistGetTopLevelInfo(constuint8_t*databytes, uint64_tdatalen, uint8_t*marker, uint64_t*offset, CFBinaryPlistTrailer*trailer) {
CFBinaryPlistTrailertrail;
initStatics();
if (!databytes||datalen<sizeof(trail) +8+1) FAIL_FALSE;
// Tiger and earlier will parse "bplist00"
// Leopard will parse "bplist00" or "bplist01"
// SnowLeopard will parse "bplist0?" where ? is any one character
if (0!=memcmp("bplist0", databytes, 7)) {
return false;
}
memmove(&trail, databytes+datalen-sizeof(trail), sizeof(trail));
// In Leopard, the unused bytes in the trailer must be 0 or the parse will fail
// This check is not present in Tiger and earlier or after Leopard
trail._numObjects=CFSwapInt64BigToHost(trail._numObjects);
trail._topObject=CFSwapInt64BigToHost(trail._topObject);
trail._offsetTableOffset=CFSwapInt64BigToHost(trail._offsetTableOffset);
// Don't overflow on the number of objects or offset of the table
if (LONG_MAX<trail._numObjects) FAIL_FALSE;
if (LONG_MAX<trail._offsetTableOffset) FAIL_FALSE;
// Must be a minimum of 1 object
if (trail._numObjects<1) FAIL_FALSE;
// The ref to the top object must be a value in the range of 1 to the total number of objects
if (trail._numObjects <= trail._topObject) FAIL_FALSE;
// The offset table must be after at least 9 bytes of other data ('bplist??' + 1 byte of object table data).
if (trail._offsetTableOffset<9) FAIL_FALSE;
// The trailer must point to a value before itself in the data.
if (datalen-sizeof(trail) <= trail._offsetTableOffset) FAIL_FALSE;
// Minimum of 1 byte for the size of integers and references in the data
if (trail._offsetIntSize<1) FAIL_FALSE;
if (trail._objectRefSize<1) FAIL_FALSE;
int32_terr=CF_NO_ERROR;
// The total size of the offset table (number of objects * size of each int in the table) must not overflow
uint64_toffsetIntSize=trail._offsetIntSize;
uint64_toffsetTableSize=__check_uint64_mul_unsigned_unsigned(trail._numObjects, offsetIntSize, &err);
if (CF_NO_ERROR!=err) FAIL_FALSE;
// The offset table must have at least 1 entry
if (offsetTableSize<1) FAIL_FALSE;
// Make sure the size of the offset table and data sections do not overflow
uint64_tobjectDataSize=trail._offsetTableOffset-8;
uint64_ttmpSum=__check_uint64_add_unsigned_unsigned(8, objectDataSize, &err);
tmpSum=__check_uint64_add_unsigned_unsigned(tmpSum, offsetTableSize, &err);
tmpSum=__check_uint64_add_unsigned_unsigned(tmpSum, sizeof(trail), &err);
if (CF_NO_ERROR!=err) FAIL_FALSE;
// The total size of the data should be equal to the sum of offsetTableOffset + sizeof(trailer)
if (datalen!=tmpSum) FAIL_FALSE;
// The object refs must be the right size to point into the offset table. That is, if the count of objects is 260, but only 1 byte is used to store references (max value 255), something is wrong.
if (trail._objectRefSize<8&& (1ULL << (8*trail._objectRefSize)) <= trail._numObjects) FAIL_FALSE;
// The integers used for pointers in the offset table must be able to reach as far as the start of the offset table.
if (trail._offsetIntSize<8&& (1ULL << (8*trail._offsetIntSize)) <= trail._offsetTableOffset) FAIL_FALSE;
constuint8_t*objectsFirstByte;
objectsFirstByte=check_ptr_add(databytes, 8, &err);
if (CF_NO_ERROR!=err) FAIL_FALSE;
constuint8_t*offsetsFirstByte=check_ptr_add(databytes, trail._offsetTableOffset, &err);
if (CF_NO_ERROR!=err) FAIL_FALSE;
constuint8_t*offsetsLastByte;
offsetsLastByte=check_ptr_add(offsetsFirstByte, offsetTableSize-1, &err);
if (CF_NO_ERROR!=err) FAIL_FALSE;
constuint8_t*bytesptr=databytes+trail._offsetTableOffset;
uint64_tmaxOffset=trail._offsetTableOffset-1;
for (CFIndexidx=0; idx<trail._numObjects; idx++) {
uint64_toff=_getSizedInt(bytesptr, trail._offsetIntSize);
if (maxOffset<off) FAIL_FALSE;
bytesptr+=trail._offsetIntSize;
}
bytesptr=databytes+trail._offsetTableOffset+trail._topObject*trail._offsetIntSize;
uint64_toff=_getSizedInt(bytesptr, trail._offsetIntSize);
if (off<8||trail._offsetTableOffset <= off) FAIL_FALSE;
if (trailer) *trailer=trail;
if (offset) *offset=off;
if (marker) *marker=*(databytes+off);
return true;
}
CF_INLINEBoolean_plistIsPrimitive(CFPropertyListRefpl) {
CFTypeIDtype=CFGetTypeID(pl);
if (dicttype==type||arraytype==type||settype==type) FAIL_FALSE;
return true;
}
CF_INLINEbool_readInt(constuint8_t*ptr, constuint8_t*end_byte_ptr, uint64_t*bigint, constuint8_t**newptr) {
if (end_byte_ptr<ptr) FAIL_FALSE;
uint8_tmarker=*ptr++;
if ((marker&0xf0) !=kCFBinaryPlistMarkerInt) FAIL_FALSE;
uint64_tcnt=1 << (marker&0x0f);
int32_terr=CF_NO_ERROR;
constuint8_t*extent=check_ptr_add(ptr, cnt, &err) -1;
if (CF_NO_ERROR!=err) FAIL_FALSE;
if (end_byte_ptr<extent) FAIL_FALSE;
// integers are not required to be in the most compact possible representation, but only the last 64 bits are significant currently
*bigint=_getSizedInt(ptr, cnt);
ptr+=cnt;
if (newptr) *newptr=ptr;
return true;
}
// bytesptr points at a ref
CF_INLINEuint64_t_getOffsetOfRefAt(constuint8_t*databytes, constuint8_t*bytesptr, constCFBinaryPlistTrailer*trailer) {
// *trailer contents are trusted, even for overflows -- was checked when the trailer was parsed;
// this pointer arithmetic and the multiplication was also already done once and checked,
// and the offsetTable was already validated.
constuint8_t*objectsFirstByte=databytes+8;
constuint8_t*offsetsFirstByte=databytes+trailer->_offsetTableOffset;
if (bytesptr<objectsFirstByte||offsetsFirstByte-trailer->_objectRefSize<bytesptr) FAIL_MAXOFFSET;
uint64_tref=_getSizedInt(bytesptr, trailer->_objectRefSize);
if (trailer->_numObjects <= ref) FAIL_MAXOFFSET;
bytesptr=databytes+trailer->_offsetTableOffset+ref*trailer->_offsetIntSize;
uint64_toff=_getSizedInt(bytesptr, trailer->_offsetIntSize);
returnoff;
}
bool__CFBinaryPlistGetOffsetForValueFromArray2(constuint8_t*databytes, uint64_tdatalen, uint64_tstartOffset, constCFBinaryPlistTrailer*trailer, CFIndexidx, uint64_t*offset, CFMutableDictionaryRefobjects) {
uint64_tobjectsRangeStart=8, objectsRangeEnd=trailer->_offsetTableOffset-1;
if (startOffset<objectsRangeStart||objectsRangeEnd<startOffset) FAIL_FALSE;
constuint8_t*ptr=databytes+startOffset;
uint8_tmarker=*ptr;
if ((marker&0xf0) !=kCFBinaryPlistMarkerArray) FAIL_FALSE;
int32_terr=CF_NO_ERROR;
ptr=check_ptr_add(ptr, 1, &err);
if (CF_NO_ERROR!=err) FAIL_FALSE;
uint64_tcnt= (marker&0x0f);
if (0xf==cnt) {
uint64_tbigint;
if (!_readInt(ptr, databytes+objectsRangeEnd, &bigint, &ptr)) FAIL_FALSE;
if (LONG_MAX<bigint) FAIL_FALSE;
cnt=bigint;
}
if (cnt <= idx) FAIL_FALSE;
size_tbyte_cnt=check_size_t_mul(cnt, trailer->_objectRefSize, &err);
if (CF_NO_ERROR!=err) FAIL_FALSE;
constuint8_t*extent=check_ptr_add(ptr, byte_cnt, &err) -1;
if (CF_NO_ERROR!=err) FAIL_FALSE;
if (databytes+objectsRangeEnd<extent) FAIL_FALSE;
uint64_toff=_getOffsetOfRefAt(databytes, ptr+idx*trailer->_objectRefSize, trailer);
if (offset) *offset=off;
return true;
}
// Compatibility method, to be removed soon
CF_EXPORTbool__CFBinaryPlistGetOffsetForValueFromDictionary2(constuint8_t*databytes, uint64_tdatalen, uint64_tstartOffset, constCFBinaryPlistTrailer*trailer, CFTypeRefkey, uint64_t*koffset, uint64_t*voffset, CFMutableDictionaryRefobjects) {
return__CFBinaryPlistGetOffsetForValueFromDictionary3(databytes, datalen, startOffset, trailer, key, koffset, voffset, false, objects);
}
/* Get the offset for a value in a dictionary in a binary property list.
@param databytes A pointer to the start of the binary property list data.
@param datalen The length of the data.
@param startOffset The offset at which the dictionary starts.
@param trailer A pointer to a filled out trailer structure (use __CFBinaryPlistGetTopLevelInfo).
@param key A string key in the dictionary that should be searched for.
@param koffset Will be filled out with the offset to the key in the data bytes.
@param voffset Will be filled out with the offset to the value in the data bytes.
@param unused Unused parameter.
@param objects Used for caching objects. Should be a valid CFMutableDictionaryRef.
@return True if the key was found, false otherwise.
*/
bool__CFBinaryPlistGetOffsetForValueFromDictionary3(constuint8_t*databytes, uint64_tdatalen, uint64_tstartOffset, constCFBinaryPlistTrailer*trailer, CFTypeRefkey, uint64_t*koffset, uint64_t*voffset, Booleanunused, CFMutableDictionaryRefobjects) {
// Require a key that is a plist primitive
if (!key|| !_plistIsPrimitive(key)) FAIL_FALSE;
// Require that startOffset is in the range of the object table
uint64_tobjectsRangeStart=8, objectsRangeEnd=trailer->_offsetTableOffset-1;
if (startOffset<objectsRangeStart||objectsRangeEnd<startOffset) FAIL_FALSE;
// ptr is the start of the dictionary we are reading
constuint8_t*ptr=databytes+startOffset;
// Check that the data pointer actually points to a dictionary
uint8_tmarker=*ptr;
if ((marker&0xf0) !=kCFBinaryPlistMarkerDict) FAIL_FALSE;
// Get the number of objects in this dictionary
int32_terr=CF_NO_ERROR;
ptr=check_ptr_add(ptr, 1, &err);
if (CF_NO_ERROR!=err) FAIL_FALSE;
uint64_tcnt= (marker&0x0f);
if (0xf==cnt) {
uint64_tbigint=0;
if (!_readInt(ptr, databytes+objectsRangeEnd, &bigint, &ptr)) FAIL_FALSE;
if (LONG_MAX<bigint) FAIL_FALSE;
cnt=bigint;
}
// Total number of objects (keys + values) is cnt * 2
cnt=check_size_t_mul(cnt, 2, &err);
if (CF_NO_ERROR!=err) FAIL_FALSE;
size_tbyte_cnt=check_size_t_mul(cnt, trailer->_objectRefSize, &err);
if (CF_NO_ERROR!=err) FAIL_FALSE;
// Find the end of the dictionary
constuint8_t*extent=check_ptr_add(ptr, byte_cnt, &err) -1;
if (CF_NO_ERROR!=err) FAIL_FALSE;
// Check that we didn't overflow the size of the dictionary
if (databytes+objectsRangeEnd<extent) FAIL_FALSE;
// For short keys (15 bytes or less) in ASCII form, we can do a quick comparison check
// We get the pointer or copy the buffer here, outside of the loop
CFIndexstringKeyLen=-1;
if (CFGetTypeID(key) ==stringtype) {
stringKeyLen=CFStringGetLength((CFStringRef)key);
}
// Find the object in the dictionary with this key
cnt=cnt / 2;
uint64_ttotalKeySize=cnt*trailer->_objectRefSize;
uint64_toff;
Booleanmatch= false;
CFPropertyListRefkeyInData=NULL;
#defineKEY_BUFF_SIZE 16
charkeyBuffer[KEY_BUFF_SIZE];
constchar*keyBufferPtr=NULL;
// If we have a string for the key, then we will grab the ASCII encoded version of it, if possible, and do a memcmp on it
if (stringKeyLen!=-1) {
// Since we will only be comparing ASCII strings, we can attempt to get a pointer using MacRoman encoding
// (this is cheaper than a copy)
if (!(keyBufferPtr=CFStringGetCStringPtr((CFStringRef)key, kCFStringEncodingMacRoman)) &&stringKeyLen<KEY_BUFF_SIZE) {
CFStringGetCString((CFStringRef)key, keyBuffer, KEY_BUFF_SIZE, kCFStringEncodingMacRoman);
// The pointer should now point to our keyBuffer instead of the original string buffer, since we've copied it
keyBufferPtr=keyBuffer;
}
}
// Perform linear search of the keys
for (CFIndexidx=0; idx<cnt; idx++) {
off=_getOffsetOfRefAt(databytes, ptr, trailer);
marker=*(databytes+off);
// if it is an ASCII string in the data, then we do a memcmp. If the key isn't ASCII, then it won't pass the compare, unless it hits some odd edge case of the ASCII string actually containing the unicode escape sequence.
if (keyBufferPtr&& (marker&0xf0) ==kCFBinaryPlistMarkerASCIIString) {
CFIndexlen=marker&0x0f;
// move past the marker
constuint8_t*ptr2=databytes+off;
err=CF_NO_ERROR;
ptr2=check_ptr_add(ptr2, 1, &err);
if (CF_NO_ERROR!=err) FAIL_FALSE;
// If the key's length is large, and the length we are querying is also large, then we have to read it in. If stringKeyLen is less than 0xf, then len will never be equal to it if it was encoded as large.
if (0xf==len&&stringKeyLen >= 0xf) {
uint64_tbigint=0;
if (!_readInt(ptr2, databytes+objectsRangeEnd, &bigint, &ptr2)) FAIL_FALSE;
if (LONG_MAX<bigint) FAIL_FALSE;
len= (CFIndex)bigint;
}
if (len==stringKeyLen) {
err=CF_NO_ERROR;
extent=check_ptr_add(ptr2, len, &err);
if (CF_NO_ERROR!=err) FAIL_FALSE;
if (databytes+trailer->_offsetTableOffset <= extent) FAIL_FALSE;
// Compare the key to this potential match
if (memcmp(ptr2, keyBufferPtr, stringKeyLen) ==0) {
match= true;
}
}
} else {
// temp object not saved in 'objects', because we don't know what allocator to use
// (what allocator __CFBinaryPlistCreateObject2() or __CFBinaryPlistCreateObject()
// will eventually be called with which results in that object)
keyInData=NULL;
if (!__CFBinaryPlistCreateObject2(databytes, datalen, off, trailer, kCFAllocatorSystemDefault, kCFPropertyListImmutable, NULL/*objects*/, NULL, 0, &keyInData) || !_plistIsPrimitive(keyInData)) {
if (keyInData) CFRelease(keyInData);
return false;
}
match=CFEqual(key, keyInData);
CFRelease(keyInData);
}
if (match) {
if (koffset) *koffset=off;
if (voffset) *voffset=_getOffsetOfRefAt(databytes, ptr+totalKeySize, trailer);
return true;
}
ptr+=trailer->_objectRefSize;
}
return false;
}