-
Notifications
You must be signed in to change notification settings - Fork 109
/
CFBundle.c
2623 lines (2357 loc) · 117 KB
/
CFBundle.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) 2015 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@
*/
/* CFBundle.c
Copyright (c) 1999-2014, Apple Inc. All rights reserved.
Responsibility: Tony Parker
*/
#include "CFBundle_Internal.h"
#include <CoreFoundation/CFPropertyList.h>
#include <CoreFoundation/CFNumber.h>
#include <CoreFoundation/CFSet.h>
#include <CoreFoundation/CFURLAccess.h>
#include <CoreFoundation/CFError.h>
#include <string.h>
#include <CoreFoundation/CFPriv.h>
#include "CFInternal.h"
#include <CoreFoundation/CFByteOrder.h>
#include "CFBundle_BinaryTypes.h"
#include <ctype.h>
#include <sys/stat.h>
#include <stdlib.h>
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_WINDOWS
#else
#error Unknown deployment target
#endif
#define AVOID_WEAK_COLLECTIONS 1
#if !AVOID_WEAK_COLLECTIONS
#include "CFHashTable.h"
#include "CFMapTable.h"
#include "CFPointerArray.h"
#endif /* !AVOID_WEAK_COLLECTIONS */
#if defined(BINARY_SUPPORT_DYLD)
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <crt_externs.h>
#endif /* BINARY_SUPPORT_DYLD */
#if defined(BINARY_SUPPORT_DLFCN)
#include <dlfcn.h>
#endif /* BINARY_SUPPORT_DLFCN */
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
#include <fcntl.h>
#elif DEPLOYMENT_TARGET_WINDOWS
#include <fcntl.h>
#include <io.h>
#endif
static void _CFBundleFlushBundleCachesAlreadyLocked(CFBundleRef bundle, Boolean alreadyLocked);
#define LOG_BUNDLE_LOAD 0
// Public CFBundle Info plist keys
CONST_STRING_DECL(kCFBundleInfoDictionaryVersionKey, "CFBundleInfoDictionaryVersion")
CONST_STRING_DECL(kCFBundleExecutableKey, "CFBundleExecutable")
CONST_STRING_DECL(kCFBundleIdentifierKey, "CFBundleIdentifier")
CONST_STRING_DECL(kCFBundleVersionKey, "CFBundleVersion")
CONST_STRING_DECL(kCFBundleDevelopmentRegionKey, "CFBundleDevelopmentRegion")
CONST_STRING_DECL(kCFBundleLocalizationsKey, "CFBundleLocalizations")
// Private CFBundle Info plist keys, possible candidates for public constants
CONST_STRING_DECL(_kCFBundleAllowMixedLocalizationsKey, "CFBundleAllowMixedLocalizations")
CONST_STRING_DECL(_kCFBundleSupportedPlatformsKey, "CFBundleSupportedPlatforms")
CONST_STRING_DECL(_kCFBundleResourceSpecificationKey, "CFBundleResourceSpecification")
// Finder stuff
CONST_STRING_DECL(_kCFBundlePackageTypeKey, "CFBundlePackageType")
CONST_STRING_DECL(_kCFBundleSignatureKey, "CFBundleSignature")
CONST_STRING_DECL(_kCFBundleIconFileKey, "CFBundleIconFile")
CONST_STRING_DECL(_kCFBundleDocumentTypesKey, "CFBundleDocumentTypes")
CONST_STRING_DECL(_kCFBundleURLTypesKey, "CFBundleURLTypes")
// Keys that are usually localized in InfoPlist.strings
CONST_STRING_DECL(kCFBundleNameKey, "CFBundleName")
CONST_STRING_DECL(_kCFBundleDisplayNameKey, "CFBundleDisplayName")
CONST_STRING_DECL(_kCFBundleShortVersionStringKey, "CFBundleShortVersionString")
CONST_STRING_DECL(_kCFBundleGetInfoStringKey, "CFBundleGetInfoString")
CONST_STRING_DECL(_kCFBundleGetInfoHTMLKey, "CFBundleGetInfoHTML")
// Sub-keys for CFBundleDocumentTypes dictionaries
CONST_STRING_DECL(_kCFBundleTypeNameKey, "CFBundleTypeName")
CONST_STRING_DECL(_kCFBundleTypeRoleKey, "CFBundleTypeRole")
CONST_STRING_DECL(_kCFBundleTypeIconFileKey, "CFBundleTypeIconFile")
CONST_STRING_DECL(_kCFBundleTypeOSTypesKey, "CFBundleTypeOSTypes")
CONST_STRING_DECL(_kCFBundleTypeExtensionsKey, "CFBundleTypeExtensions")
CONST_STRING_DECL(_kCFBundleTypeMIMETypesKey, "CFBundleTypeMIMETypes")
// Sub-keys for CFBundleURLTypes dictionaries
CONST_STRING_DECL(_kCFBundleURLNameKey, "CFBundleURLName")
CONST_STRING_DECL(_kCFBundleURLIconFileKey, "CFBundleURLIconFile")
CONST_STRING_DECL(_kCFBundleURLSchemesKey, "CFBundleURLSchemes")
// Compatibility key names
CONST_STRING_DECL(_kCFBundleOldExecutableKey, "NSExecutable")
CONST_STRING_DECL(_kCFBundleOldInfoDictionaryVersionKey, "NSInfoPlistVersion")
CONST_STRING_DECL(_kCFBundleOldNameKey, "NSHumanReadableName")
CONST_STRING_DECL(_kCFBundleOldIconFileKey, "NSIcon")
CONST_STRING_DECL(_kCFBundleOldDocumentTypesKey, "NSTypes")
CONST_STRING_DECL(_kCFBundleOldShortVersionStringKey, "NSAppVersion")
// Compatibility CFBundleDocumentTypes key names
CONST_STRING_DECL(_kCFBundleOldTypeNameKey, "NSName")
CONST_STRING_DECL(_kCFBundleOldTypeRoleKey, "NSRole")
CONST_STRING_DECL(_kCFBundleOldTypeIconFileKey, "NSIcon")
CONST_STRING_DECL(_kCFBundleOldTypeExtensions1Key, "NSUnixExtensions")
CONST_STRING_DECL(_kCFBundleOldTypeExtensions2Key, "NSDOSExtensions")
CONST_STRING_DECL(_kCFBundleOldTypeOSTypesKey, "NSMacOSType")
// Internally used keys for loaded Info plists.
CONST_STRING_DECL(_kCFBundleInfoPlistURLKey, "CFBundleInfoPlistURL")
CONST_STRING_DECL(_kCFBundleRawInfoPlistURLKey, "CFBundleRawInfoPlistURL")
CONST_STRING_DECL(_kCFBundleNumericVersionKey, "CFBundleNumericVersion")
CONST_STRING_DECL(_kCFBundleExecutablePathKey, "CFBundleExecutablePath")
CONST_STRING_DECL(_kCFBundleResourcesFileMappedKey, "CSResourcesFileMapped")
CONST_STRING_DECL(_kCFBundleCFMLoadAsBundleKey, "CFBundleCFMLoadAsBundle")
// Keys used by NSBundle for loaded Info plists.
CONST_STRING_DECL(_kCFBundlePrincipalClassKey, "NSPrincipalClass")
static char __CFBundleMainID__[1026] = {0};
CF_PRIVATE char *__CFBundleMainID = __CFBundleMainID__;
static CFTypeID __kCFBundleTypeID = _kCFRuntimeNotATypeID;
static pthread_mutex_t CFBundleGlobalDataLock = PTHREAD_MUTEX_INITIALIZER;
static CFMutableDictionaryRef _bundlesByIdentifier = NULL;
#if AVOID_WEAK_COLLECTIONS
static CFMutableDictionaryRef _bundlesByURL = NULL;
static CFMutableArrayRef _allBundles = NULL;
static CFMutableSetRef _bundlesToUnload = NULL;
#else /* AVOID_WEAK_COLLECTIONS */
static __CFHashTable *_allBundles = nil;
static __CFHashTable *_bundlesToUnload = nil;
#endif /* AVOID_WEAK_COLLECTIONS */
static Boolean _scheduledBundlesAreUnloading = false;
static Boolean _initedMainBundle = false;
static CFBundleRef _mainBundle = NULL;
// Forward declares functions.
static CFBundleRef _CFBundleCreate(CFAllocatorRef allocator, CFURLRef bundleURL, Boolean alreadyLocked, Boolean doFinalProcessing);
static CFURLRef _CFBundleCopyExecutableURLIgnoringCache(CFBundleRef bundle);
static void _CFBundleEnsureBundlesUpToDateWithHintAlreadyLocked(CFStringRef hint);
static void _CFBundleEnsureAllBundlesUpToDateAlreadyLocked(void);
static void _CFBundleEnsureBundleExistsForImagePath(CFStringRef imagePath);
static void _CFBundleEnsureBundlesExistForImagePaths(CFArrayRef imagePaths);
#pragma mark -
#if AVOID_WEAK_COLLECTIONS
static void _CFBundleAddToTables(CFBundleRef bundle, Boolean alreadyLocked) {
CFStringRef bundleID = CFBundleGetIdentifier(bundle);
if (!alreadyLocked) pthread_mutex_lock(&CFBundleGlobalDataLock);
// Add to the _allBundles list
if (!_allBundles) {
CFArrayCallBacks nonRetainingArrayCallbacks = kCFTypeArrayCallBacks;
nonRetainingArrayCallbacks.retain = NULL;
nonRetainingArrayCallbacks.release = NULL;
_allBundles = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &nonRetainingArrayCallbacks);
}
CFArrayAppendValue(_allBundles, bundle);
// Add to the table that maps urls to bundles
if (!_bundlesByURL) {
CFDictionaryValueCallBacks nonRetainingDictionaryValueCallbacks = kCFTypeDictionaryValueCallBacks;
nonRetainingDictionaryValueCallbacks.retain = NULL;
nonRetainingDictionaryValueCallbacks.release = NULL;
_bundlesByURL = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeDictionaryKeyCallBacks, &nonRetainingDictionaryValueCallbacks);
}
CFDictionarySetValue(_bundlesByURL, bundle->_url, bundle);
// Add to the table that maps identifiers to bundles
if (bundleID) {
CFMutableArrayRef bundlesWithThisID = NULL;
CFBundleRef existingBundle = NULL;
if (!_bundlesByIdentifier) {
_bundlesByIdentifier = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
}
bundlesWithThisID = (CFMutableArrayRef)CFDictionaryGetValue(_bundlesByIdentifier, bundleID);
if (bundlesWithThisID) {
CFIndex i, count = CFArrayGetCount(bundlesWithThisID);
UInt32 existingVersion, newVersion = CFBundleGetVersionNumber(bundle);
for (i = 0; i < count; i++) {
existingBundle = (CFBundleRef)CFArrayGetValueAtIndex(bundlesWithThisID, i);
existingVersion = CFBundleGetVersionNumber(existingBundle);
// If you load two bundles with the same identifier and the same version, the last one wins.
if (newVersion >= existingVersion) break;
}
CFArrayInsertValueAtIndex(bundlesWithThisID, i, bundle);
} else {
CFArrayCallBacks nonRetainingArrayCallbacks = kCFTypeArrayCallBacks;
nonRetainingArrayCallbacks.retain = NULL;
nonRetainingArrayCallbacks.release = NULL;
bundlesWithThisID = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &nonRetainingArrayCallbacks);
CFArrayAppendValue(bundlesWithThisID, bundle);
CFDictionarySetValue(_bundlesByIdentifier, bundleID, bundlesWithThisID);
CFRelease(bundlesWithThisID);
}
}
if (!alreadyLocked) pthread_mutex_unlock(&CFBundleGlobalDataLock);
}
static void _CFBundleRemoveFromTables(CFBundleRef bundle, CFURLRef bundleURL, CFStringRef bundleID) {
pthread_mutex_lock(&CFBundleGlobalDataLock);
// Remove from the various lists
if (_allBundles) {
CFIndex i = CFArrayGetFirstIndexOfValue(_allBundles, CFRangeMake(0, CFArrayGetCount(_allBundles)), bundle);
if (i >= 0) CFArrayRemoveValueAtIndex(_allBundles, i);
}
// Remove from the table that maps urls to bundles
if (bundleURL && _bundlesByURL) {
CFBundleRef bundleForURL = (CFBundleRef)CFDictionaryGetValue(_bundlesByURL, bundleURL);
if (bundleForURL == bundle) CFDictionaryRemoveValue(_bundlesByURL, bundleURL);
}
// Remove from the table that maps identifiers to bundles
if (bundleID && _bundlesByIdentifier) {
CFMutableArrayRef bundlesWithThisID = (CFMutableArrayRef)CFDictionaryGetValue(_bundlesByIdentifier, bundleID);
if (bundlesWithThisID) {
CFIndex count = CFArrayGetCount(bundlesWithThisID);
while (count-- > 0) if (bundle == (CFBundleRef)CFArrayGetValueAtIndex(bundlesWithThisID, count)) CFArrayRemoveValueAtIndex(bundlesWithThisID, count);
if (0 == CFArrayGetCount(bundlesWithThisID)) CFDictionaryRemoveValue(_bundlesByIdentifier, bundleID);
}
}
pthread_mutex_unlock(&CFBundleGlobalDataLock);
}
static CFBundleRef _CFBundleCopyBundleForURL(CFURLRef url, Boolean alreadyLocked) {
CFBundleRef result = NULL;
if (!alreadyLocked) pthread_mutex_lock(&CFBundleGlobalDataLock);
if (_bundlesByURL) result = (CFBundleRef)CFDictionaryGetValue(_bundlesByURL, url);
if (result && !result->_url) {
result = NULL;
CFDictionaryRemoveValue(_bundlesByURL, url);
}
if (result) CFRetain(result);
if (!alreadyLocked) pthread_mutex_unlock(&CFBundleGlobalDataLock);
return result;
}
static CFBundleRef _CFBundlePrimitiveGetBundleWithIdentifierAlreadyLocked(CFStringRef bundleID) {
CFBundleRef result = NULL, bundle;
if (_bundlesByIdentifier && bundleID) {
// Note that this array is maintained in descending order by version number
CFArrayRef bundlesWithThisID = (CFArrayRef)CFDictionaryGetValue(_bundlesByIdentifier, bundleID);
if (bundlesWithThisID) {
CFIndex i, count = CFArrayGetCount(bundlesWithThisID);
if (count > 0) {
// First check for loaded bundles so we will always prefer a loaded to an unloaded bundle
for (i = 0; !result && i < count; i++) {
bundle = (CFBundleRef)CFArrayGetValueAtIndex(bundlesWithThisID, i);
if (CFBundleIsExecutableLoaded(bundle)) result = bundle;
}
// If no loaded bundle, simply take the first item in the array, i.e. the one with the latest version number
if (!result) result = (CFBundleRef)CFArrayGetValueAtIndex(bundlesWithThisID, 0);
}
}
}
return result;
}
#else /* AVOID_WEAK_COLLECTIONS */
/*
An explanation of what I'm doing here is probably in order.
8029300 has cast suspicion on the correctness of __CFMapTable with strong keys and weak values, at least under non-GC.
An early attempt to work around it by inserting dummy values instead of removing things succeeded, as did turning on the AVOID_WEAK_COLLECTIONS #ifdef
This indicates that it's not an overrelease in securityd, since AVOID_WEAK_COLLECTIONS wouldn't help in that case.
Therefore, these functions following this comment allow us to have _bundlesByURL be a CFDictionary on non-GC and keep __CFMapTable to GC where it's needed.
*/
static inline id _getBundlesByURL() {
static id _bundles = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (CF_USING_COLLECTABLE_MEMORY) {
_bundles = [[__CFMapTable alloc] initWithKeyOptions:CFPointerFunctionsStrongMemory valueOptions:CFPointerFunctionsZeroingWeakMemory capacity:0];
} else {
CFDictionaryValueCallBacks nonRetainingDictionaryValueCallbacks = kCFTypeDictionaryValueCallBacks;
nonRetainingDictionaryValueCallbacks.retain = NULL;
nonRetainingDictionaryValueCallbacks.release = NULL;
_bundles = (id)CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeDictionaryKeyCallBacks, &nonRetainingDictionaryValueCallbacks);
}
});
return _bundles;
}
#define _bundlesByURL _getBundlesByURL()
static void _setInBundlesByURL(CFURLRef key, CFBundleRef bundle) {
if (CF_USING_COLLECTABLE_MEMORY) {
[(__CFMapTable *)_bundlesByURL setObject:(id)bundle forKey:(id)key];
} else {
CFDictionarySetValue((CFMutableDictionaryRef)_bundlesByURL, key, bundle);
}
}
static void _removeFromBundlesByURL(CFURLRef key) {
if (CF_USING_COLLECTABLE_MEMORY) {
[(__CFMapTable *)_bundlesByURL removeObjectForKey:(id)key];
} else {
CFDictionaryRemoveValue((CFMutableDictionaryRef)_bundlesByURL, key);
}
}
static CFBundleRef _getFromBundlesByURL(CFURLRef key) {
if (CF_USING_COLLECTABLE_MEMORY) {
return (CFBundleRef)[(__CFMapTable *)_bundlesByURL objectForKey:(id)key];
} else {
return (CFBundleRef)CFDictionaryGetValue((CFMutableDictionaryRef)_bundlesByURL, key);
}
}
static void _CFBundleAddToTables(CFBundleRef bundle, Boolean alreadyLocked) {
CFStringRef bundleID = CFBundleGetIdentifier(bundle);
if (!alreadyLocked) pthread_mutex_lock(&CFBundleGlobalDataLock);
// Add to the _allBundles list
if (!_allBundles) _allBundles = [[__CFHashTable alloc] initWithOptions:CFPointerFunctionsZeroingWeakMemory capacity:0];
[_allBundles addObject:(id)bundle];
// Add to the table that maps urls to bundles
_setInBundlesByURL(bundle->_url, bundle);
// Add to the table that maps identifiers to bundles
if (bundleID) {
__CFPointerArray *bundlesWithThisID = nil;
CFBundleRef existingBundle = NULL;
if (!_bundlesByIdentifier) {
_bundlesByIdentifier = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
}
bundlesWithThisID = (__CFPointerArray *)CFDictionaryGetValue(_bundlesByIdentifier, bundleID);
if (bundlesWithThisID) {
CFIndex i, count = (CFIndex)[bundlesWithThisID count];
UInt32 existingVersion, newVersion = CFBundleGetVersionNumber(bundle);
for (i = 0; i < count; i++) {
existingBundle = (CFBundleRef)[bundlesWithThisID pointerAtIndex:i];
if (!existingBundle) continue;
existingVersion = CFBundleGetVersionNumber(existingBundle);
// If you load two bundles with the same identifier and the same version, the last one wins.
if (newVersion >= existingVersion) break;
}
if (i < count) {
[bundlesWithThisID insertPointer:bundle atIndex:i];
} else {
[bundlesWithThisID addPointer:bundle];
}
} else {
bundlesWithThisID = [[__CFPointerArray alloc] initWithOptions:CFPointerFunctionsZeroingWeakMemory];
[bundlesWithThisID addPointer:bundle];
CFDictionarySetValue(_bundlesByIdentifier, bundleID, bundlesWithThisID);
[bundlesWithThisID release];
}
}
if (!alreadyLocked) pthread_mutex_unlock(&CFBundleGlobalDataLock);
}
static void _CFBundleRemoveFromTables(CFBundleRef bundle, CFURLRef bundleURL, CFStringRef bundleID) {
pthread_mutex_lock(&CFBundleGlobalDataLock);
// Remove from the various lists
if (_allBundles && [_allBundles member:(id)bundle]) [_allBundles removeObject:(id)bundle];
// Remove from the table that maps urls to bundles
if (bundleURL) {
_removeFromBundlesByURL(bundleURL);
}
// Remove from the table that maps identifiers to bundles
if (bundleID && _bundlesByIdentifier) {
__CFPointerArray *bundlesWithThisID = (__CFPointerArray *)CFDictionaryGetValue(_bundlesByIdentifier, bundleID);
if (bundlesWithThisID) {
CFIndex count = (CFIndex)[bundlesWithThisID count];
while (count-- > 0) if (bundle == (CFBundleRef)[bundlesWithThisID pointerAtIndex:count]) [bundlesWithThisID removePointerAtIndex:count];
[bundlesWithThisID compact];
if (0 == [bundlesWithThisID count]) CFDictionaryRemoveValue(_bundlesByIdentifier, bundleID);
}
}
pthread_mutex_unlock(&CFBundleGlobalDataLock);
}
static CFBundleRef _CFBundleCopyBundleForURL(CFURLRef url, Boolean alreadyLocked) {
CFBundleRef result = NULL;
if (!alreadyLocked) pthread_mutex_lock(&CFBundleGlobalDataLock);
result = _getFromBundlesByURL(url);
if (result && !result->_url) {
result = NULL;
_removeFromBundlesByURL(url);
}
if (result) CFRetain(result);
if (!alreadyLocked) pthread_mutex_unlock(&CFBundleGlobalDataLock);
return result;
}
static CFBundleRef _CFBundlePrimitiveGetBundleWithIdentifierAlreadyLocked(CFStringRef bundleID) {
CFBundleRef result = NULL;
if (_bundlesByIdentifier && bundleID) {
// Note that this array is maintained in descending order by version number
__CFPointerArray *bundlesWithThisID = (__CFPointerArray *)CFDictionaryGetValue(_bundlesByIdentifier, bundleID);
if (bundlesWithThisID && [bundlesWithThisID count] > 0) {
// First check for loaded bundles so we will always prefer a loaded to an unloaded bundle
for (id bundle in bundlesWithThisID) {
if (bundle && CFBundleIsExecutableLoaded((CFBundleRef)bundle)) {
result = (CFBundleRef)bundle;
break;
}
}
// If no loaded bundle, simply take the first item in the array, i.e. the one with the latest version number
if (!result) {
for (id bundle in bundlesWithThisID) {
if (bundle) {
result = (CFBundleRef)bundle;
break;
}
}
}
}
}
return result;
}
#endif /* AVOID_WEAK_COLLECTIONS */
static CFURLRef _CFBundleCopyBundleURLForExecutablePath(CFStringRef str) {
//!!! need to handle frameworks, NT; need to integrate with NSBundle - drd
UniChar buff[CFMaxPathSize];
CFIndex buffLen;
CFURLRef url = NULL;
CFStringRef outstr;
buffLen = CFStringGetLength(str);
if (buffLen > CFMaxPathSize) buffLen = CFMaxPathSize;
CFStringGetCharacters(str, CFRangeMake(0, buffLen), buff);
#if DEPLOYMENT_TARGET_WINDOWS
// Is this a .dll or .exe?
if (buffLen >= 5 && (_wcsnicmp((wchar_t *)&(buff[buffLen-4]), L".dll", 4) == 0 || _wcsnicmp((wchar_t *)&(buff[buffLen-4]), L".exe", 4) == 0)) {
CFIndex extensionLength = CFStringGetLength(_CFBundleWindowsResourceDirectoryExtension);
buffLen -= 4;
// If this is an _debug, we should strip that before looking for the bundle
if (buffLen >= 7 && (_wcsnicmp((wchar_t *)&buff[buffLen-6], L"_debug", 6) == 0)) buffLen -= 6;
if (buffLen + 1 + extensionLength < CFMaxPathSize) {
buff[buffLen] = '.';
buffLen ++;
CFStringGetCharacters(_CFBundleWindowsResourceDirectoryExtension, CFRangeMake(0, extensionLength), buff + buffLen);
buffLen += extensionLength;
outstr = CFStringCreateWithCharactersNoCopy(kCFAllocatorSystemDefault, buff, buffLen, kCFAllocatorNull);
url = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, outstr, PLATFORM_PATH_STYLE, true);
CFRelease(outstr);
}
}
#endif
if (!url) {
buffLen = _CFLengthAfterDeletingLastPathComponent(buff, buffLen); // Remove exe name
if (buffLen > 0) {
// See if this is a new bundle. If it is, we have to remove more path components.
CFIndex startOfLastDir = _CFStartOfLastPathComponent(buff, buffLen);
if (startOfLastDir > 0 && startOfLastDir < buffLen) {
CFStringRef lastDirName = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, &(buff[startOfLastDir]), buffLen - startOfLastDir);
if (CFEqual(lastDirName, _CFBundleGetPlatformExecutablesSubdirectoryName()) || CFEqual(lastDirName, _CFBundleGetAlternatePlatformExecutablesSubdirectoryName()) || CFEqual(lastDirName, _CFBundleGetOtherPlatformExecutablesSubdirectoryName()) || CFEqual(lastDirName, _CFBundleGetOtherAlternatePlatformExecutablesSubdirectoryName())) {
// This is a new bundle. Back off a few more levels
if (buffLen > 0) {
// Remove platform folder
buffLen = _CFLengthAfterDeletingLastPathComponent(buff, buffLen);
}
if (buffLen > 0) {
// Remove executables folder (if present)
CFIndex startOfNextDir = _CFStartOfLastPathComponent(buff, buffLen);
if (startOfNextDir > 0 && startOfNextDir < buffLen) {
CFStringRef nextDirName = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, &(buff[startOfNextDir]), buffLen - startOfNextDir);
if (CFEqual(nextDirName, _CFBundleExecutablesDirectoryName)) buffLen = _CFLengthAfterDeletingLastPathComponent(buff, buffLen);
CFRelease(nextDirName);
}
}
if (buffLen > 0) {
// Remove support files folder
buffLen = _CFLengthAfterDeletingLastPathComponent(buff, buffLen);
}
}
CFRelease(lastDirName);
}
}
if (buffLen > 0) {
outstr = CFStringCreateWithCharactersNoCopy(kCFAllocatorSystemDefault, buff, buffLen, kCFAllocatorNull);
url = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, outstr, PLATFORM_PATH_STYLE, true);
CFRelease(outstr);
}
}
return url;
}
static CFURLRef _CFBundleCopyResolvedURLForExecutableURL(CFURLRef url) {
// this is necessary so that we match any sanitization CFURL may perform on the result of _CFBundleCopyBundleURLForExecutableURL()
CFURLRef absoluteURL, url1, url2, outURL = NULL;
CFStringRef str, str1, str2;
absoluteURL = CFURLCopyAbsoluteURL(url);
str = CFURLCopyFileSystemPath(absoluteURL, PLATFORM_PATH_STYLE);
if (str) {
UniChar buff[CFMaxPathSize];
CFIndex buffLen = CFStringGetLength(str), len1;
if (buffLen > CFMaxPathSize) buffLen = CFMaxPathSize;
CFStringGetCharacters(str, CFRangeMake(0, buffLen), buff);
len1 = _CFLengthAfterDeletingLastPathComponent(buff, buffLen);
if (len1 > 0 && len1 + 1 < buffLen) {
str1 = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, buff, len1);
CFIndex skipSlashCount = 1;
#if DEPLOYMENT_TARGET_WINDOWS
// On Windows, _CFLengthAfterDeletingLastPathComponent will return a value of 3 if the path is at the root (e.g. C:\). This includes the \, which is not the case for URLs with subdirectories
if (len1 == 3 && buff[1] == ':' && buff[2] == '\\') {
skipSlashCount = 0;
}
#endif
str2 = CFStringCreateWithCharacters(kCFAllocatorSystemDefault, buff + len1 + skipSlashCount, buffLen - len1 - skipSlashCount);
if (str1 && str2) {
url1 = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, str1, PLATFORM_PATH_STYLE, true);
if (url1) {
url2 = CFURLCreateWithFileSystemPathRelativeToBase(kCFAllocatorSystemDefault, str2, PLATFORM_PATH_STYLE, false, url1);
if (url2) {
outURL = CFURLCopyAbsoluteURL(url2);
CFRelease(url2);
}
CFRelease(url1);
}
}
if (str1) CFRelease(str1);
if (str2) CFRelease(str2);
}
CFRelease(str);
}
if (!outURL) {
outURL = absoluteURL;
} else {
CFRelease(absoluteURL);
}
return outURL;
}
CFURLRef _CFBundleCopyBundleURLForExecutableURL(CFURLRef url) {
CFURLRef resolvedURL, outurl = NULL;
CFStringRef str;
resolvedURL = _CFBundleCopyResolvedURLForExecutableURL(url);
str = CFURLCopyFileSystemPath(resolvedURL, PLATFORM_PATH_STYLE);
if (str) {
outurl = _CFBundleCopyBundleURLForExecutablePath(str);
CFRelease(str);
}
CFRelease(resolvedURL);
return outurl;
}
static uint8_t _CFBundleEffectiveLayoutVersion(CFBundleRef bundle) {
uint8_t localVersion = bundle->_version;
// exclude type 0 bundles with no binary (or CFM binary) and no Info.plist, since they give too many false positives
if (0 == localVersion) {
CFDictionaryRef infoDict = CFBundleGetInfoDictionary(bundle);
if (!infoDict || 0 == CFDictionaryGetCount(infoDict)) {
#if defined(BINARY_SUPPORT_DYLD)
CFURLRef executableURL = CFBundleCopyExecutableURL(bundle);
if (executableURL) {
if (bundle->_binaryType == __CFBundleUnknownBinary) bundle->_binaryType = _CFBundleGrokBinaryType(executableURL);
if (bundle->_binaryType == __CFBundleCFMBinary || bundle->_binaryType == __CFBundleUnreadableBinary) {
localVersion = 4;
} else {
bundle->_resourceData._executableLacksResourceFork = true;
}
CFRelease(executableURL);
} else {
localVersion = 4;
}
#else
CFURLRef executableURL = CFBundleCopyExecutableURL(bundle);
if (executableURL) {
CFRelease(executableURL);
} else {
localVersion = 4;
}
#endif /* BINARY_SUPPORT_DYLD */
}
}
return localVersion;
}
CFBundleRef _CFBundleCreateIfLooksLikeBundle(CFAllocatorRef allocator, CFURLRef url) {
CFBundleRef bundle = CFBundleCreate(allocator, url);
if (bundle) {
uint8_t localVersion = _CFBundleEffectiveLayoutVersion(bundle);
if (3 == localVersion || 4 == localVersion) {
CFRelease(bundle);
bundle = NULL;
}
}
return bundle;
}
CF_EXPORT Boolean _CFBundleURLLooksLikeBundle(CFURLRef url) {
Boolean result = false;
CFBundleRef bundle = _CFBundleCreateIfLooksLikeBundle(kCFAllocatorSystemDefault, url);
if (bundle) {
result = true;
CFRelease(bundle);
}
return result;
}
CFBundleRef _CFBundleGetMainBundleIfLooksLikeBundle(void) {
CFBundleRef mainBundle = CFBundleGetMainBundle();
if (mainBundle && (3 == mainBundle->_version || 4 == mainBundle->_version)) mainBundle = NULL;
return mainBundle;
}
Boolean _CFBundleMainBundleInfoDictionaryComesFromResourceFork(void) {
CFBundleRef mainBundle = CFBundleGetMainBundle();
return (mainBundle && mainBundle->_resourceData._infoDictionaryFromResourceFork);
}
CFBundleRef _CFBundleCreateWithExecutableURLIfLooksLikeBundle(CFAllocatorRef allocator, CFURLRef url) {
CFBundleRef bundle = NULL;
CFURLRef bundleURL = _CFBundleCopyBundleURLForExecutableURL(url), resolvedURL = _CFBundleCopyResolvedURLForExecutableURL(url);
if (bundleURL && resolvedURL) {
// We used to call _CFBundleCreateIfLooksLikeBundle here, but switched to the regular CFBundleCreate because we want this to return a result for certain flat bundles as well.
bundle = CFBundleCreate(allocator, bundleURL);
if (bundle) {
CFURLRef executableURL = _CFBundleCopyExecutableURLIgnoringCache(bundle);
char buff1[CFMaxPathSize], buff2[CFMaxPathSize];
if (!executableURL || !CFURLGetFileSystemRepresentation(resolvedURL, true, (uint8_t *)buff1, CFMaxPathSize) || !CFURLGetFileSystemRepresentation(executableURL, true, (uint8_t *)buff2, CFMaxPathSize) || 0 != strcmp(buff1, buff2)) {
CFRelease(bundle);
bundle = NULL;
}
if (executableURL) CFRelease(executableURL);
}
}
if (bundleURL) CFRelease(bundleURL);
if (resolvedURL) CFRelease(resolvedURL);
return bundle;
}
CFBundleRef _CFBundleCreateIfMightBeBundle(CFAllocatorRef allocator, CFURLRef url) {
// This function is obsolete
CFBundleRef bundle = CFBundleCreate(allocator, url);
return bundle;
}
CFBundleRef _CFBundleCreateWithExecutableURLIfMightBeBundle(CFAllocatorRef allocator, CFURLRef url) {
CFBundleRef result = _CFBundleCreateWithExecutableURLIfLooksLikeBundle(allocator, url);
// This function applies additional requirements on a bundle to return a result
// The above makes sure that:
// 0. CFBundleCreate must succeed using a URL derived from the executable URL
// 1. The bundle must have an executableURL, and it must match the passed in executable URL
// This function additionally requires that
// 2. If flat, the bundle must have a non-empty Info.plist. (15663535)
if (result) {
uint8_t localVersion = _CFBundleEffectiveLayoutVersion(result);
if (3 == localVersion || 4 == localVersion) {
CFDictionaryRef infoPlist = CFBundleGetInfoDictionary(result);
if (!infoPlist || (infoPlist && CFDictionaryGetCount(infoPlist) == 0)) {
CFRelease(result);
result = NULL;
}
}
}
return result;
}
CFURLRef _CFBundleCopyMainBundleExecutableURL(Boolean *looksLikeBundle) {
// This function is for internal use only; _mainBundle is deliberately accessed outside of the lock to get around a reentrancy issue
const char *processPath;
CFStringRef str = NULL;
CFURLRef executableURL = NULL;
processPath = _CFProcessPath();
if (processPath) {
str = CFStringCreateWithFileSystemRepresentation(kCFAllocatorSystemDefault, processPath);
if (str) {
executableURL = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, str, PLATFORM_PATH_STYLE, false);
CFRelease(str);
}
}
if (looksLikeBundle) {
CFBundleRef mainBundle = _mainBundle;
if (mainBundle && (3 == mainBundle->_version || 4 == mainBundle->_version)) mainBundle = NULL;
*looksLikeBundle = (mainBundle ? true : false);
}
return executableURL;
}
static void _CFBundleInitializeMainBundleInfoDictionaryAlreadyLocked(CFStringRef executablePath) {
CFBundleGetInfoDictionary(_mainBundle);
if (!_mainBundle->_infoDict || CFDictionaryGetCount(_mainBundle->_infoDict) == 0) {
// if type 3 bundle and no Info.plist, treat as unbundled, since this gives too many false positives
if (_mainBundle->_version == 3) _mainBundle->_version = 4;
if (_mainBundle->_version == 0) {
// if type 0 bundle and no Info.plist and not main executable for bundle, treat as unbundled, since this gives too many false positives
CFStringRef executableName = _CFBundleCopyExecutableName(_mainBundle, NULL, NULL);
if (!executableName || !executablePath || !CFStringHasSuffix(executablePath, executableName)) _mainBundle->_version = 4;
if (executableName) CFRelease(executableName);
}
#if defined(BINARY_SUPPORT_DYLD)
if (_mainBundle->_binaryType == __CFBundleDYLDExecutableBinary) {
if (_mainBundle->_infoDict && !(0)) CFRelease(_mainBundle->_infoDict);
_mainBundle->_infoDict = (CFDictionaryRef)_CFBundleCreateInfoDictFromMainExecutable();
}
#endif /* BINARY_SUPPORT_DYLD */
} else {
#if defined(BINARY_SUPPORT_DYLD)
if (_mainBundle->_binaryType == __CFBundleDYLDExecutableBinary) {
// if dyld and not main executable for bundle, prefer info dictionary from executable
CFStringRef executableName = _CFBundleCopyExecutableName(_mainBundle, NULL, NULL);
if (!executableName || !executablePath || !CFStringHasSuffix(executablePath, executableName)) {
CFDictionaryRef infoDictFromExecutable = (CFDictionaryRef)_CFBundleCreateInfoDictFromMainExecutable();
if (infoDictFromExecutable && CFDictionaryGetCount(infoDictFromExecutable) > 0) {
if (_mainBundle->_infoDict) CFRelease(_mainBundle->_infoDict);
_mainBundle->_infoDict = infoDictFromExecutable;
} else if (infoDictFromExecutable) {
CFRelease(infoDictFromExecutable);
}
}
if (executableName) CFRelease(executableName);
}
#endif /* BINARY_SUPPORT_DYLD */
}
if (!_mainBundle->_infoDict) _mainBundle->_infoDict = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
if (!_mainBundle->_executablePath && executablePath) _mainBundle->_executablePath = (CFStringRef)CFRetain(executablePath);
CFStringRef bundleID = (CFStringRef)CFDictionaryGetValue(_mainBundle->_infoDict, kCFBundleIdentifierKey);
if (bundleID) {
if (!CFStringGetCString(bundleID, __CFBundleMainID__, sizeof(__CFBundleMainID__) - 2, kCFStringEncodingUTF8)) {
__CFBundleMainID__[0] = '\0';
}
}
}
static void _CFBundleFlushBundleCachesAlreadyLocked(CFBundleRef bundle, Boolean alreadyLocked) {
CFDictionaryRef oldInfoDict = bundle->_infoDict;
CFTypeRef val;
bundle->_infoDict = NULL;
if (bundle->_localInfoDict) {
CFRelease(bundle->_localInfoDict);
bundle->_localInfoDict = NULL;
}
if (bundle->_developmentRegion) {
CFRelease(bundle->_developmentRegion);
bundle->_developmentRegion = NULL;
}
if (bundle->_executablePath) {
CFRelease(bundle->_executablePath);
bundle->_executablePath = NULL;
}
if (bundle->_searchLanguages) {
CFRelease(bundle->_searchLanguages);
bundle->_searchLanguages = NULL;
}
if (bundle->_stringTable) {
CFRelease(bundle->_stringTable);
bundle->_stringTable = NULL;
}
if (bundle == _mainBundle) {
CFStringRef executablePath = bundle->_executablePath;
if (!alreadyLocked) pthread_mutex_lock(&CFBundleGlobalDataLock);
_CFBundleInitializeMainBundleInfoDictionaryAlreadyLocked(executablePath);
if (!alreadyLocked) pthread_mutex_unlock(&CFBundleGlobalDataLock);
} else {
CFBundleGetInfoDictionary(bundle);
}
if (oldInfoDict) {
if (!bundle->_infoDict) bundle->_infoDict = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
val = CFDictionaryGetValue(oldInfoDict, _kCFBundlePrincipalClassKey);
if (val) CFDictionarySetValue((CFMutableDictionaryRef)bundle->_infoDict, _kCFBundlePrincipalClassKey, val);
CFRelease(oldInfoDict);
}
_CFBundleFlushQueryTableCache(bundle);
}
CF_EXPORT void _CFBundleFlushBundleCaches(CFBundleRef bundle) {
_CFBundleFlushBundleCachesAlreadyLocked(bundle, false);
}
static CFBundleRef _CFBundleGetMainBundleAlreadyLocked(void) {
if (!_initedMainBundle) {
const char *processPath;
CFStringRef str = NULL;
CFURLRef executableURL = NULL, bundleURL = NULL;
_initedMainBundle = true;
processPath = _CFProcessPath();
if (processPath) {
str = CFStringCreateWithFileSystemRepresentation(kCFAllocatorSystemDefault, processPath);
if (!executableURL) executableURL = CFURLCreateWithFileSystemPath(kCFAllocatorSystemDefault, str, PLATFORM_PATH_STYLE, false);
}
if (executableURL) bundleURL = _CFBundleCopyBundleURLForExecutableURL(executableURL);
if (bundleURL) {
// make sure that main bundle has executable path
//??? what if we are not the main executable in the bundle?
// NB doFinalProcessing must be false here, see below
_mainBundle = _CFBundleCreate(kCFAllocatorSystemDefault, bundleURL, true, false);
if (_mainBundle) {
// make sure that the main bundle is listed as loaded, and mark it as executable
_mainBundle->_isLoaded = true;
#if defined(BINARY_SUPPORT_DYLD)
if (_mainBundle->_binaryType == __CFBundleUnknownBinary) {
if (!executableURL) {
_mainBundle->_binaryType = __CFBundleNoBinary;
} else {
_mainBundle->_binaryType = _CFBundleGrokBinaryType(executableURL);
if (_mainBundle->_binaryType != __CFBundleCFMBinary && _mainBundle->_binaryType != __CFBundleUnreadableBinary) _mainBundle->_resourceData._executableLacksResourceFork = true;
}
}
#endif /* BINARY_SUPPORT_DYLD */
// get cookie for already-loaded main bundle
#if defined(BINARY_SUPPORT_DLFCN)
if (!_mainBundle->_handleCookie) {
_mainBundle->_handleCookie = dlopen(NULL, RTLD_NOLOAD | RTLD_FIRST);
#if LOG_BUNDLE_LOAD
printf("main bundle %p getting handle %p\n", _mainBundle, _mainBundle->_handleCookie);
#endif /* LOG_BUNDLE_LOAD */
}
#elif defined(BINARY_SUPPORT_DYLD)
if (_mainBundle->_binaryType == __CFBundleDYLDExecutableBinary && !_mainBundle->_imageCookie) {
_mainBundle->_imageCookie = (void *)_dyld_get_image_header(0);
#if LOG_BUNDLE_LOAD
printf("main bundle %p getting image %p\n", _mainBundle, _mainBundle->_imageCookie);
#endif /* LOG_BUNDLE_LOAD */
}
#endif /* BINARY_SUPPORT_DLFCN */
_CFBundleInitializeMainBundleInfoDictionaryAlreadyLocked(str);
// Perform delayed final processing steps.
// This must be done after _isLoaded has been set, for security reasons (3624341).
if (_CFBundleNeedsInitPlugIn(_mainBundle)) {
pthread_mutex_unlock(&CFBundleGlobalDataLock);
_CFBundleInitPlugIn(_mainBundle);
pthread_mutex_lock(&CFBundleGlobalDataLock);
}
}
}
if (bundleURL) CFRelease(bundleURL);
if (str) CFRelease(str);
if (executableURL) CFRelease(executableURL);
}
return _mainBundle;
}
CFBundleRef CFBundleGetMainBundle(void) {
CFBundleRef mainBundle;
pthread_mutex_lock(&CFBundleGlobalDataLock);
mainBundle = _CFBundleGetMainBundleAlreadyLocked();
pthread_mutex_unlock(&CFBundleGlobalDataLock);
return mainBundle;
}
CFBundleRef CFBundleGetBundleWithIdentifier(CFStringRef bundleID) {
CFBundleRef result = NULL;
if (bundleID) {
pthread_mutex_lock(&CFBundleGlobalDataLock);
(void)_CFBundleGetMainBundleAlreadyLocked();
result = _CFBundlePrimitiveGetBundleWithIdentifierAlreadyLocked(bundleID);
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
if (!result) {
// Try to create the bundle for the caller and try again
void *p = __builtin_return_address(0);
if (p) {
CFStringRef imagePath = _CFBundleCopyLoadedImagePathForPointer(p);
if (imagePath) {
_CFBundleEnsureBundleExistsForImagePath(imagePath);
CFRelease(imagePath);
}
result = _CFBundlePrimitiveGetBundleWithIdentifierAlreadyLocked(bundleID);
}
}
#endif
if (!result) {
// Try to guess the bundle from the identifier and try again
_CFBundleEnsureBundlesUpToDateWithHintAlreadyLocked(bundleID);
result = _CFBundlePrimitiveGetBundleWithIdentifierAlreadyLocked(bundleID);
}
if (!result) {
// Make sure all bundles have been created and try again.
_CFBundleEnsureAllBundlesUpToDateAlreadyLocked();
result = _CFBundlePrimitiveGetBundleWithIdentifierAlreadyLocked(bundleID);
}
pthread_mutex_unlock(&CFBundleGlobalDataLock);
}
return result;
}
static CFStringRef __CFBundleCopyDescription(CFTypeRef cf) {
char buff[CFMaxPathSize];
CFStringRef path = NULL, binaryType = NULL, retval = NULL;
if (((CFBundleRef)cf)->_url && CFURLGetFileSystemRepresentation(((CFBundleRef)cf)->_url, true, (uint8_t *)buff, CFMaxPathSize)) path = CFStringCreateWithFileSystemRepresentation(kCFAllocatorSystemDefault, buff);
switch (((CFBundleRef)cf)->_binaryType) {
case __CFBundleCFMBinary:
binaryType = CFSTR("");
break;
case __CFBundleDYLDExecutableBinary:
binaryType = CFSTR("executable, ");
break;
case __CFBundleDYLDBundleBinary:
binaryType = CFSTR("bundle, ");
break;
case __CFBundleDYLDFrameworkBinary:
binaryType = CFSTR("framework, ");
break;
case __CFBundleDLLBinary:
binaryType = CFSTR("DLL, ");
break;
case __CFBundleUnreadableBinary:
binaryType = CFSTR("");
break;
default:
binaryType = CFSTR("");
break;
}
if (((CFBundleRef)cf)->_plugInData._isPlugIn) {
retval = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("CFBundle/CFPlugIn %p <%@> (%@%@loaded)"), cf, path, binaryType, ((CFBundleRef)cf)->_isLoaded ? CFSTR("") : CFSTR("not "));
} else {
retval = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("CFBundle %p <%@> (%@%@loaded)"), cf, path, binaryType, ((CFBundleRef)cf)->_isLoaded ? CFSTR("") : CFSTR("not "));
}
if (path) CFRelease(path);
return retval;
}
static void _CFBundleDeallocateGlue(const void *key, const void *value, void *context) {
CFAllocatorRef allocator = (CFAllocatorRef)context;
if (value) CFAllocatorDeallocate(allocator, (void *)value);
}
static void __CFBundleDeallocate(CFTypeRef cf) {
CFBundleRef bundle = (CFBundleRef)cf;
CFURLRef bundleURL;
CFStringRef bundleID = NULL;
__CFGenericValidateType(cf, CFBundleGetTypeID());
bundleURL = bundle->_url;
bundle->_url = NULL;
if (bundle->_infoDict) bundleID = (CFStringRef)CFDictionaryGetValue(bundle->_infoDict, kCFBundleIdentifierKey);
_CFBundleRemoveFromTables(bundle, bundleURL, bundleID);
CFBundleUnloadExecutable(bundle);
_CFBundleDeallocatePlugIn(bundle);
if (bundleURL) {
CFRelease(bundleURL);
}
if (bundle->_infoDict && !(0)) CFRelease(bundle->_infoDict);
if (bundle->_modDate) CFRelease(bundle->_modDate);
if (bundle->_localInfoDict && !(0)) CFRelease(bundle->_localInfoDict);
if (bundle->_searchLanguages) CFRelease(bundle->_searchLanguages);
if (bundle->_executablePath) CFRelease(bundle->_executablePath);
if (bundle->_developmentRegion) CFRelease(bundle->_developmentRegion);
if (bundle->_glueDict) {
CFDictionaryApplyFunction(bundle->_glueDict, _CFBundleDeallocateGlue, (void *)CFGetAllocator(bundle));
CFRelease(bundle->_glueDict);
}
if (bundle->_stringTable) CFRelease(bundle->_stringTable);
if (bundle->_bundleBasePath) CFRelease(bundle->_bundleBasePath);
if (bundle->_queryTable) CFRelease(bundle->_queryTable);
if (bundle->_localizations) CFRelease(bundle->_localizations);
if (bundle->_resourceDirectoryContents) CFRelease(bundle->_resourceDirectoryContents);
pthread_mutex_destroy(&(bundle->_bundleLoadingLock));
}