/
CFBundle_Resources.c
2351 lines (2098 loc) · 117 KB
/
CFBundle_Resources.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) 2014 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_Resources.c
Copyright (c) 1999-2013, Apple Inc. All rights reserved.
Responsibility: Tony Parker
*/
#include "CFBundle_Internal.h"
#include <CoreFoundation/CFURLAccess.h>
#include <CoreFoundation/CFPropertyList.h>
#include <CoreFoundation/CFByteOrder.h>
#include <CoreFoundation/CFNumber.h>
#include <CoreFoundation/CFLocale.h>
#include <CoreFoundation/CFPreferences.h>
#include <string.h>
#include "CFInternal.h"
#include <CoreFoundation/CFPriv.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX
#include <unistd.h>
#include <sys/sysctl.h>
#include <sys/stat.h>
#include <dirent.h>
#endif
#if DEPLOYMENT_TARGET_MACOSX
#endif
#if DEPLOYMENT_TARGET_WINDOWS
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#define close _close
#define write _write
#define read _read
#define open _NS_open
#define stat _NS_stat
#define fstat _fstat
#define mkdir(a,b) _NS_mkdir(a)
#define rmdir _NS_rmdir
#define unlink _NS_unlink
#endif
#pragma mark -
#pragma mark Directory Contents and Caches
// These are here for compatibility, but they do nothing anymore
CF_EXPORT void _CFBundleFlushCachesForURL(CFURLRef url) { }
CF_EXPORT void _CFBundleFlushCaches(void) { }
#pragma mark -
#pragma mark Resource URL Lookup
static inline Boolean _CFIsResourceCommon(char *path, Boolean *isDir) {
Boolean exists;
SInt32 mode;
if (_CFGetPathProperties(kCFAllocatorSystemDefault, path, &exists, &mode, NULL, NULL, NULL, NULL) == 0) {
if (isDir) *isDir = ((exists && ((mode & S_IFMT) == S_IFDIR)) ? true : false);
return (exists && (mode & 0444));
}
return false;
}
CF_PRIVATE Boolean _CFIsResourceAtURL(CFURLRef url, Boolean *isDir) {
char path[CFMaxPathSize];
if (!CFURLGetFileSystemRepresentation(url, true, (uint8_t *)path, CFMaxPathLength)) return false;
return _CFIsResourceCommon(path, isDir);
}
CF_PRIVATE Boolean _CFIsResourceAtPath(CFStringRef path, Boolean *isDir) {
char pathBuf[CFMaxPathSize];
if (!CFStringGetFileSystemRepresentation(path, pathBuf, CFMaxPathSize)) return false;
return _CFIsResourceCommon(pathBuf, isDir);
}
static CFStringRef _CFBundleGetResourceDirForVersion(uint8_t version) {
if (1 == version) {
return _CFBundleSupportFilesDirectoryName1WithResources;
} else if (2 == version) {
return _CFBundleSupportFilesDirectoryName2WithResources;
} else if (0 == version) {
return _CFBundleResourcesDirectoryName;
}
return CFSTR("");
}
CF_PRIVATE void _CFBundleAppendResourceDir(CFMutableStringRef path, uint8_t version) {
if (1 == version) {
// /path/to/bundle/Support Files/
CFStringAppend(path, _CFBundleSupportFilesDirectoryName1);
_CFAppendTrailingPathSlash2(path);
} else if (2 == version) {
// /path/to/bundle/Contents/
CFStringAppend(path, _CFBundleSupportFilesDirectoryName2);
_CFAppendTrailingPathSlash2(path);
}
if (0 == version || 1 == version || 2 == version) {
// /path/to/bundle/<above>/Resources
CFStringAppend(path, _CFBundleResourcesDirectoryName);
}
}
CF_EXPORT CFURLRef CFBundleCopyResourceURL(CFBundleRef bundle, CFStringRef resourceName, CFStringRef resourceType, CFStringRef subDirName) {
if (!bundle) return NULL;
CFURLRef result = (CFURLRef) _CFBundleCopyFindResources(bundle, NULL, NULL, resourceName, resourceType, subDirName, NULL, NO, NO, NULL);
return result;
}
CF_EXPORT CFArrayRef CFBundleCopyResourceURLsOfType(CFBundleRef bundle, CFStringRef resourceType, CFStringRef subDirName) {
if (!bundle) return CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks);
CFArrayRef result = (CFArrayRef) _CFBundleCopyFindResources(bundle, NULL, NULL, NULL, resourceType, subDirName, NULL, YES, NO, NULL);
return result;
}
CF_EXPORT CFURLRef _CFBundleCopyResourceURLForLanguage(CFBundleRef bundle, CFStringRef resourceName, CFStringRef resourceType, CFStringRef subDirName, CFStringRef language) {
return CFBundleCopyResourceURLForLocalization(bundle, resourceName, resourceType, subDirName, language);
}
CF_EXPORT CFURLRef CFBundleCopyResourceURLForLocalization(CFBundleRef bundle, CFStringRef resourceName, CFStringRef resourceType, CFStringRef subDirName, CFStringRef localizationName) {
if (!bundle) return NULL;
CFURLRef result = (CFURLRef) _CFBundleCopyFindResources(bundle, NULL, NULL, resourceName, resourceType, subDirName, localizationName, NO, YES, NULL);
return result;
}
CF_EXPORT CFArrayRef _CFBundleCopyResourceURLsOfTypeForLanguage(CFBundleRef bundle, CFStringRef resourceType, CFStringRef subDirName, CFStringRef language) {
return CFBundleCopyResourceURLsOfTypeForLocalization(bundle, resourceType, subDirName, language);
}
CF_EXPORT CFArrayRef CFBundleCopyResourceURLsOfTypeForLocalization(CFBundleRef bundle, CFStringRef resourceType, CFStringRef subDirName, CFStringRef localizationName) {
if (!bundle) return CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks);
CFArrayRef result = (CFArrayRef) _CFBundleCopyFindResources(bundle, NULL, NULL, NULL, resourceType, subDirName, localizationName, YES, YES, NULL);
return result;
}
CF_EXPORT CFURLRef CFBundleCopyResourceURLInDirectory(CFURLRef bundleURL, CFStringRef resourceName, CFStringRef resourceType, CFStringRef subDirName) {
CFURLRef result = NULL;
unsigned char buff[CFMaxPathSize];
CFURLRef newURL = NULL;
if (!CFURLGetFileSystemRepresentation(bundleURL, true, buff, CFMaxPathSize)) return NULL;
newURL = CFURLCreateFromFileSystemRepresentation(kCFAllocatorSystemDefault, buff, strlen((char *)buff), true);
if (!newURL) newURL = (CFURLRef)CFRetain(bundleURL);
if (_CFBundleCouldBeBundle(newURL)) {
result = (CFURLRef) _CFBundleCopyFindResources(NULL, bundleURL, NULL, resourceName, resourceType, subDirName, NULL, NO, NO, NULL);
}
if (newURL) CFRelease(newURL);
return result;
}
CF_EXPORT CFArrayRef CFBundleCopyResourceURLsOfTypeInDirectory(CFURLRef bundleURL, CFStringRef resourceType, CFStringRef subDirName) {
CFArrayRef array = NULL;
unsigned char buff[CFMaxPathSize];
CFURLRef newURL = NULL;
if (!CFURLGetFileSystemRepresentation(bundleURL, true, buff, CFMaxPathSize)) return NULL;
newURL = CFURLCreateFromFileSystemRepresentation(kCFAllocatorSystemDefault, buff, strlen((char *)buff), true);
if (!newURL) newURL = (CFURLRef)CFRetain(bundleURL);
if (_CFBundleCouldBeBundle(newURL)) {
array = (CFArrayRef) _CFBundleCopyFindResources(NULL, bundleURL, NULL, NULL, resourceType, subDirName, NULL, YES, NO, NULL);
}
if (newURL) CFRelease(newURL);
return array;
}
#pragma mark -
#pragma mark Lanaguages and Locales
// string, with groups of 6 characters being 1 element in the array of locale abbreviations
const char * __CFBundleLocaleAbbreviationsArray =
"en_US\0" "fr_FR\0" "en_GB\0" "de_DE\0" "it_IT\0" "nl_NL\0" "nl_BE\0" "sv_SE\0"
"es_ES\0" "da_DK\0" "pt_PT\0" "fr_CA\0" "nb_NO\0" "he_IL\0" "ja_JP\0" "en_AU\0"
"ar\0\0\0\0" "fi_FI\0" "fr_CH\0" "de_CH\0" "el_GR\0" "is_IS\0" "mt_MT\0" "el_CY\0"
"tr_TR\0" "hr_HR\0" "nl_NL\0" "nl_BE\0" "en_CA\0" "en_CA\0" "pt_PT\0" "nb_NO\0"
"da_DK\0" "hi_IN\0" "ur_PK\0" "tr_TR\0" "it_CH\0" "en\0\0\0\0" "\0\0\0\0\0\0" "ro_RO\0"
"grc\0\0\0" "lt_LT\0" "pl_PL\0" "hu_HU\0" "et_EE\0" "lv_LV\0" "se\0\0\0\0" "fo_FO\0"
"fa_IR\0" "ru_RU\0" "ga_IE\0" "ko_KR\0" "zh_CN\0" "zh_TW\0" "th_TH\0" "\0\0\0\0\0\0"
"cs_CZ\0" "sk_SK\0" "\0\0\0\0\0\0" "hu_HU\0" "bn\0\0\0\0" "be_BY\0" "uk_UA\0" "\0\0\0\0\0\0"
"el_GR\0" "sr_CS\0" "sl_SI\0" "mk_MK\0" "hr_HR\0" "\0\0\0\0\0\0" "de_DE\0" "pt_BR\0"
"bg_BG\0" "ca_ES\0" "\0\0\0\0\0\0" "gd\0\0\0\0" "gv\0\0\0\0" "br\0\0\0\0" "iu_CA\0" "cy\0\0\0\0"
"en_CA\0" "ga_IE\0" "en_CA\0" "dz_BT\0" "hy_AM\0" "ka_GE\0" "es_XL\0" "es_ES\0"
"to_TO\0" "pl_PL\0" "ca_ES\0" "fr\0\0\0\0" "de_AT\0" "es_XL\0" "gu_IN\0" "pa\0\0\0\0"
"ur_IN\0" "vi_VN\0" "fr_BE\0" "uz_UZ\0" "en_SG\0" "nn_NO\0" "af_ZA\0" "eo\0\0\0\0"
"mr_IN\0" "bo\0\0\0\0" "ne_NP\0" "kl\0\0\0\0" "en_IE\0";
#define NUM_LOCALE_ABBREVIATIONS 109
#define LOCALE_ABBREVIATION_LENGTH 6
static const char * const __CFBundleLanguageNamesArray[] = {
"English", "French", "German", "Italian", "Dutch", "Swedish", "Spanish", "Danish",
"Portuguese", "Norwegian", "Hebrew", "Japanese", "Arabic", "Finnish", "Greek", "Icelandic",
"Maltese", "Turkish", "Croatian", "Chinese", "Urdu", "Hindi", "Thai", "Korean",
"Lithuanian", "Polish", "Hungarian", "Estonian", "Latvian", "Sami", "Faroese", "Farsi",
"Russian", "Chinese", "Dutch", "Irish", "Albanian", "Romanian", "Czech", "Slovak",
"Slovenian", "Yiddish", "Serbian", "Macedonian", "Bulgarian", "Ukrainian", "Byelorussian", "Uzbek",
"Kazakh", "Azerbaijani", "Azerbaijani", "Armenian", "Georgian", "Moldavian", "Kirghiz", "Tajiki",
"Turkmen", "Mongolian", "Mongolian", "Pashto", "Kurdish", "Kashmiri", "Sindhi", "Tibetan",
"Nepali", "Sanskrit", "Marathi", "Bengali", "Assamese", "Gujarati", "Punjabi", "Oriya",
"Malayalam", "Kannada", "Tamil", "Telugu", "Sinhalese", "Burmese", "Khmer", "Lao",
"Vietnamese", "Indonesian", "Tagalog", "Malay", "Malay", "Amharic", "Tigrinya", "Oromo",
"Somali", "Swahili", "Kinyarwanda", "Rundi", "Nyanja", "Malagasy", "Esperanto", "",
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", "",
"Welsh", "Basque", "Catalan", "Latin", "Quechua", "Guarani", "Aymara", "Tatar",
"Uighur", "Dzongkha", "Javanese", "Sundanese", "Galician", "Afrikaans", "Breton", "Inuktitut",
"Scottish", "Manx", "Irish", "Tongan", "Greek", "Greenlandic", "Azerbaijani", "Nynorsk"
};
#define NUM_LANGUAGE_NAMES 152
#define LANGUAGE_NAME_LENGTH 13
// string, with groups of 3 characters being 1 element in the array of abbreviations
const char * __CFBundleLanguageAbbreviationsArray =
"en\0" "fr\0" "de\0" "it\0" "nl\0" "sv\0" "es\0" "da\0"
"pt\0" "nb\0" "he\0" "ja\0" "ar\0" "fi\0" "el\0" "is\0"
"mt\0" "tr\0" "hr\0" "zh\0" "ur\0" "hi\0" "th\0" "ko\0"
"lt\0" "pl\0" "hu\0" "et\0" "lv\0" "se\0" "fo\0" "fa\0"
"ru\0" "zh\0" "nl\0" "ga\0" "sq\0" "ro\0" "cs\0" "sk\0"
"sl\0" "yi\0" "sr\0" "mk\0" "bg\0" "uk\0" "be\0" "uz\0"
"kk\0" "az\0" "az\0" "hy\0" "ka\0" "mo\0" "ky\0" "tg\0"
"tk\0" "mn\0" "mn\0" "ps\0" "ku\0" "ks\0" "sd\0" "bo\0"
"ne\0" "sa\0" "mr\0" "bn\0" "as\0" "gu\0" "pa\0" "or\0"
"ml\0" "kn\0" "ta\0" "te\0" "si\0" "my\0" "km\0" "lo\0"
"vi\0" "id\0" "tl\0" "ms\0" "ms\0" "am\0" "ti\0" "om\0"
"so\0" "sw\0" "rw\0" "rn\0" "\0\0\0" "mg\0" "eo\0" "\0\0\0"
"\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0"
"\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0"
"\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0"
"\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0" "\0\0\0"
"cy\0" "eu\0" "ca\0" "la\0" "qu\0" "gn\0" "ay\0" "tt\0"
"ug\0" "dz\0" "jv\0" "su\0" "gl\0" "af\0" "br\0" "iu\0"
"gd\0" "gv\0" "ga\0" "to\0" "el\0" "kl\0" "az\0" "nn\0";
#define NUM_LANGUAGE_ABBREVIATIONS 152
#define LANGUAGE_ABBREVIATION_LENGTH 3
#if defined(__CONSTANT_CFSTRINGS__)
// These are not necessarily common localizations per se, but localizations for which the full language name is still in common use.
// These are used to provide a fast path for it (other localizations usually use the abbreviation, which is even faster).
static CFStringRef const __CFBundleCommonLanguageNamesArray[] = {CFSTR("English"), CFSTR("French"), CFSTR("German"), CFSTR("Italian"), CFSTR("Dutch"), CFSTR("Spanish"), CFSTR("Japanese")};
static CFStringRef const __CFBundleCommonLanguageAbbreviationsArray[] = {CFSTR("en"), CFSTR("fr"), CFSTR("de"), CFSTR("it"), CFSTR("nl"), CFSTR("es"), CFSTR("ja")};
#define NUM_COMMON_LANGUAGE_NAMES 7
#endif /* __CONSTANT_CFSTRINGS__ */
static const SInt32 __CFBundleScriptCodesArray[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 1, 4, 0, 6, 0,
0, 0, 0, 2, 4, 9, 21, 3, 29, 29, 29, 29, 29, 0, 0, 4,
7, 25, 0, 0, 0, 0, 29, 29, 0, 5, 7, 7, 7, 7, 7, 7,
7, 7, 4, 24, 23, 7, 7, 7, 7, 27, 7, 4, 4, 4, 4, 26,
9, 9, 9, 13, 13, 11, 10, 12, 17, 16, 14, 15, 18, 19, 20, 22,
30, 0, 0, 0, 4, 28, 28, 28, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 7, 4, 26, 0, 0, 0, 0, 0, 28,
0, 0, 0, 0, 6, 0, 0, 0
};
static const CFStringEncoding __CFBundleStringEncodingsArray[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 1, 4, 0, 6, 37,
0, 35, 36, 2, 4, 9, 21, 3, 29, 29, 29, 29, 29, 0, 37, 0x8C,
7, 25, 0, 39, 0, 38, 29, 29, 36, 5, 7, 7, 7, 0x98, 7, 7,
7, 7, 4, 24, 23, 7, 7, 7, 7, 27, 7, 4, 4, 4, 4, 26,
9, 9, 9, 13, 13, 11, 10, 12, 17, 16, 14, 15, 18, 19, 20, 22,
30, 0, 0, 0, 4, 28, 28, 28, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
39, 0, 0, 0, 0, 0, 0, 7, 4, 26, 0, 0, 0, 0, 39, 0xEC,
39, 39, 40, 0, 6, 0, 0, 0
};
static SInt32 _CFBundleGetLanguageCodeForLocalization(CFStringRef localizationName) {
SInt32 result = -1, i;
char buff[256];
CFIndex length = CFStringGetLength(localizationName);
if (length >= LANGUAGE_ABBREVIATION_LENGTH - 1 && length <= 255 && CFStringGetCString(localizationName, buff, 255, kCFStringEncodingASCII)) {
buff[255] = '\0';
for (i = 0; -1 == result && i < NUM_LANGUAGE_NAMES; i++) {
if (0 == strcmp(buff, __CFBundleLanguageNamesArray[i])) result = i;
}
if (0 == strcmp(buff, "zh_TW") || 0 == strcmp(buff, "zh-Hant")) result = 19; else if (0 == strcmp(buff, "zh_CN") || 0 == strcmp(buff, "zh-Hans")) result = 33; // hack for mixed-up Chinese language codes
if (-1 == result && (length == LANGUAGE_ABBREVIATION_LENGTH - 1 || !isalpha(buff[LANGUAGE_ABBREVIATION_LENGTH - 1]))) {
buff[LANGUAGE_ABBREVIATION_LENGTH - 1] = '\0';
if ('n' == buff[0] && 'o' == buff[1]) result = 9; // hack for Norwegian
for (i = 0; -1 == result && i < NUM_LANGUAGE_ABBREVIATIONS * LANGUAGE_ABBREVIATION_LENGTH; i += LANGUAGE_ABBREVIATION_LENGTH) {
if (buff[0] == *(__CFBundleLanguageAbbreviationsArray + i + 0) && buff[1] == *(__CFBundleLanguageAbbreviationsArray + i + 1)) result = i / LANGUAGE_ABBREVIATION_LENGTH;
}
}
}
return result;
}
static CFStringRef _CFBundleCopyLanguageAbbreviationForLanguageCode(SInt32 languageCode) {
CFStringRef result = NULL;
if (0 <= languageCode && languageCode < NUM_LANGUAGE_ABBREVIATIONS) {
const char *languageAbbreviation = __CFBundleLanguageAbbreviationsArray + languageCode * LANGUAGE_ABBREVIATION_LENGTH;
if (languageAbbreviation && *languageAbbreviation != '\0') result = CFStringCreateWithCStringNoCopy(kCFAllocatorSystemDefault, languageAbbreviation, kCFStringEncodingASCII, kCFAllocatorNull);
}
return result;
}
CF_INLINE CFStringRef _CFBundleCopyLanguageNameForLanguageCode(SInt32 languageCode) {
CFStringRef result = NULL;
if (0 <= languageCode && languageCode < NUM_LANGUAGE_NAMES) {
const char *languageName = __CFBundleLanguageNamesArray[languageCode];
if (languageName && *languageName != '\0') result = CFStringCreateWithCStringNoCopy(kCFAllocatorSystemDefault, languageName, kCFStringEncodingASCII, kCFAllocatorNull);
}
return result;
}
CF_INLINE CFStringRef _CFBundleCopyLanguageAbbreviationForLocalization(CFStringRef localizationName) {
CFStringRef result = NULL;
SInt32 languageCode = _CFBundleGetLanguageCodeForLocalization(localizationName);
if (languageCode >= 0) {
result = _CFBundleCopyLanguageAbbreviationForLanguageCode(languageCode);
} else {
CFIndex length = CFStringGetLength(localizationName);
if (length == LANGUAGE_ABBREVIATION_LENGTH - 1 || (length > LANGUAGE_ABBREVIATION_LENGTH - 1 && CFStringGetCharacterAtIndex(localizationName, LANGUAGE_ABBREVIATION_LENGTH - 1) == '_')) {
result = CFStringCreateWithSubstring(kCFAllocatorSystemDefault, localizationName, CFRangeMake(0, LANGUAGE_ABBREVIATION_LENGTH - 1));
}
}
return result;
}
CF_INLINE CFStringRef _CFBundleCopyModifiedLocalization(CFStringRef localizationName) {
CFMutableStringRef result = NULL;
CFIndex length = CFStringGetLength(localizationName);
if (length >= 4) {
UniChar c = CFStringGetCharacterAtIndex(localizationName, 2);
if ('-' == c || '_' == c) {
result = CFStringCreateMutableCopy(kCFAllocatorSystemDefault, length, localizationName);
CFStringReplace(result, CFRangeMake(2, 1), ('-' == c) ? CFSTR("_") : CFSTR("-"));
}
}
return result;
}
CF_INLINE CFStringRef _CFBundleCopyLanguageNameForLocalization(CFStringRef localizationName) {
CFStringRef result = NULL;
SInt32 languageCode = _CFBundleGetLanguageCodeForLocalization(localizationName);
if (languageCode >= 0) {
result = _CFBundleCopyLanguageNameForLanguageCode(languageCode);
} else {
result = (CFStringRef)CFStringCreateCopy(kCFAllocatorSystemDefault, localizationName);
}
return result;
}
static SInt32 _CFBundleGetLanguageCodeForRegionCode(SInt32 regionCode) {
SInt32 result = -1, i;
if (52 == regionCode) { // hack for mixed-up Chinese language codes
result = 33;
} else if (0 <= regionCode && regionCode < NUM_LOCALE_ABBREVIATIONS) {
const char *localeAbbreviation = __CFBundleLocaleAbbreviationsArray + regionCode * LOCALE_ABBREVIATION_LENGTH;
if (localeAbbreviation && *localeAbbreviation != '\0') {
for (i = 0; -1 == result && i < NUM_LANGUAGE_ABBREVIATIONS * LANGUAGE_ABBREVIATION_LENGTH; i += LANGUAGE_ABBREVIATION_LENGTH) {
if (localeAbbreviation[0] == *(__CFBundleLanguageAbbreviationsArray + i + 0) && localeAbbreviation[1] == *(__CFBundleLanguageAbbreviationsArray + i + 1)) result = i / LANGUAGE_ABBREVIATION_LENGTH;
}
}
}
return result;
}
static SInt32 _CFBundleGetRegionCodeForLanguageCode(SInt32 languageCode) {
SInt32 result = -1, i;
if (19 == languageCode) { // hack for mixed-up Chinese language codes
result = 53;
} else if (0 <= languageCode && languageCode < NUM_LANGUAGE_ABBREVIATIONS) {
const char *languageAbbreviation = __CFBundleLanguageAbbreviationsArray + languageCode * LANGUAGE_ABBREVIATION_LENGTH;
if (languageAbbreviation && *languageAbbreviation != '\0') {
for (i = 0; -1 == result && i < NUM_LOCALE_ABBREVIATIONS * LOCALE_ABBREVIATION_LENGTH; i += LOCALE_ABBREVIATION_LENGTH) {
if (*(__CFBundleLocaleAbbreviationsArray + i + 0) == languageAbbreviation[0] && *(__CFBundleLocaleAbbreviationsArray + i + 1) == languageAbbreviation[1]) result = i / LOCALE_ABBREVIATION_LENGTH;
}
}
}
if (25 == result) result = 68;
if (28 == result) result = 82;
return result;
}
static SInt32 _CFBundleGetRegionCodeForLocalization(CFStringRef localizationName) {
SInt32 result = -1, i;
char buff[LOCALE_ABBREVIATION_LENGTH];
CFIndex length = CFStringGetLength(localizationName);
if (length >= LANGUAGE_ABBREVIATION_LENGTH - 1 && length <= LOCALE_ABBREVIATION_LENGTH - 1 && CFStringGetCString(localizationName, buff, LOCALE_ABBREVIATION_LENGTH, kCFStringEncodingASCII)) {
buff[LOCALE_ABBREVIATION_LENGTH - 1] = '\0';
for (i = 0; -1 == result && i < NUM_LOCALE_ABBREVIATIONS * LOCALE_ABBREVIATION_LENGTH; i += LOCALE_ABBREVIATION_LENGTH) {
if (0 == strcmp(buff, __CFBundleLocaleAbbreviationsArray + i)) result = i / LOCALE_ABBREVIATION_LENGTH;
}
}
if (25 == result) result = 68;
if (28 == result) result = 82;
if (37 == result) result = 0;
if (-1 == result) {
SInt32 languageCode = _CFBundleGetLanguageCodeForLocalization(localizationName);
result = _CFBundleGetRegionCodeForLanguageCode(languageCode);
}
return result;
}
CF_PRIVATE CFStringRef _CFBundleCopyLocaleAbbreviationForRegionCode(SInt32 regionCode) {
CFStringRef result = NULL;
if (0 <= regionCode && regionCode < NUM_LOCALE_ABBREVIATIONS) {
const char *localeAbbreviation = __CFBundleLocaleAbbreviationsArray + regionCode * LOCALE_ABBREVIATION_LENGTH;
if (localeAbbreviation && *localeAbbreviation != '\0') {
result = CFStringCreateWithCStringNoCopy(kCFAllocatorSystemDefault, localeAbbreviation, kCFStringEncodingASCII, kCFAllocatorNull);
}
}
return result;
}
CF_EXPORT Boolean CFBundleGetLocalizationInfoForLocalization(CFStringRef localizationName, SInt32 *languageCode, SInt32 *regionCode, SInt32 *scriptCode, CFStringEncoding *stringEncoding) {
Boolean retval = false;
SInt32 language = -1, region = -1, script = 0;
CFStringEncoding encoding = kCFStringEncodingMacRoman;
if (!localizationName) {
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFArrayRef languages = NULL;
if (mainBundle) {
languages = _CFBundleGetLanguageSearchList(mainBundle);
if (languages) CFRetain(languages);
}
if (!languages) languages = _CFBundleCopyUserLanguages();
if (languages && CFArrayGetCount(languages) > 0) localizationName = (CFStringRef)CFArrayGetValueAtIndex(languages, 0);
}
if (localizationName) {
LangCode langCode = -1;
RegionCode regCode = -1;
ScriptCode scrCode = 0;
CFStringEncoding enc = kCFStringEncodingMacRoman;
retval = CFLocaleGetLanguageRegionEncodingForLocaleIdentifier(localizationName, &langCode, ®Code, &scrCode, &enc);
if (retval) {
language = langCode;
region = regCode;
script = scrCode;
encoding = enc;
}
}
if (!retval) {
if (localizationName) {
language = _CFBundleGetLanguageCodeForLocalization(localizationName);
region = _CFBundleGetRegionCodeForLocalization(localizationName);
} else {
_CFBundleGetLanguageAndRegionCodes(&language, ®ion);
}
if ((language < 0 || language > (int)(sizeof(__CFBundleScriptCodesArray)/sizeof(SInt32))) && region != -1) language = _CFBundleGetLanguageCodeForRegionCode(region);
if (region == -1 && language != -1) region = _CFBundleGetRegionCodeForLanguageCode(language);
if (language >= 0 && language < (int)(sizeof(__CFBundleScriptCodesArray)/sizeof(SInt32))) {
script = __CFBundleScriptCodesArray[language];
}
if (language >= 0 && language < (int)(sizeof(__CFBundleStringEncodingsArray)/sizeof(CFStringEncoding))) {
encoding = __CFBundleStringEncodingsArray[language];
}
retval = (language != -1 || region != -1);
}
if (languageCode) *languageCode = language;
if (regionCode) *regionCode = region;
if (scriptCode) *scriptCode = script;
if (stringEncoding) *stringEncoding = encoding;
return retval;
}
CFStringRef CFBundleCopyLocalizationForLocalizationInfo(SInt32 languageCode, SInt32 regionCode, SInt32 scriptCode, CFStringEncoding stringEncoding) {
CFStringRef localizationName = NULL;
if (!localizationName) localizationName = _CFBundleCopyLocaleAbbreviationForRegionCode(regionCode);
#if DEPLOYMENT_TARGET_MACOSX
if (!localizationName && 0 <= languageCode && languageCode < SHRT_MAX) localizationName = CFLocaleCreateCanonicalLocaleIdentifierFromScriptManagerCodes(kCFAllocatorSystemDefault, (LangCode)languageCode, (RegionCode)-1);
#endif
if (!localizationName) localizationName = _CFBundleCopyLanguageAbbreviationForLanguageCode(languageCode);
if (!localizationName) {
SInt32 language = -1, scriptLanguage = -1, encodingLanguage = -1;
unsigned int i;
for (i = 0; language == -1 && i < (sizeof(__CFBundleScriptCodesArray)/sizeof(SInt32)); i++) {
if (__CFBundleScriptCodesArray[i] == scriptCode && __CFBundleStringEncodingsArray[i] == stringEncoding) language = i;
}
for (i = 0; scriptLanguage == -1 && i < (sizeof(__CFBundleScriptCodesArray)/sizeof(SInt32)); i++) {
if (__CFBundleScriptCodesArray[i] == scriptCode) scriptLanguage = i;
}
for (i = 0; encodingLanguage == -1 && i < (sizeof(__CFBundleStringEncodingsArray)/sizeof(CFStringEncoding)); i++) {
if (__CFBundleStringEncodingsArray[i] == stringEncoding) encodingLanguage = i;
}
localizationName = _CFBundleCopyLanguageAbbreviationForLanguageCode(language);
if (!localizationName) localizationName = _CFBundleCopyLanguageAbbreviationForLanguageCode(encodingLanguage);
if (!localizationName) localizationName = _CFBundleCopyLanguageAbbreviationForLanguageCode(scriptLanguage);
}
return localizationName;
}
static Boolean CFBundleAllowMixedLocalizations(void) {
static Boolean allowMixed = false;
static dispatch_once_t once = 0;
dispatch_once(&once, ^{
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFDictionaryRef infoDict = mainBundle ? CFBundleGetInfoDictionary(mainBundle) : NULL;
CFTypeRef allowMixedValue = infoDict ? CFDictionaryGetValue(infoDict, _kCFBundleAllowMixedLocalizationsKey) : NULL;
if (allowMixedValue) {
CFTypeID typeID = CFGetTypeID(allowMixedValue);
if (typeID == CFBooleanGetTypeID()) {
allowMixed = CFBooleanGetValue((CFBooleanRef)allowMixedValue);
} else if (typeID == CFStringGetTypeID()) {
allowMixed = (CFStringCompare((CFStringRef)allowMixedValue, CFSTR("true"), kCFCompareCaseInsensitive) == kCFCompareEqualTo || CFStringCompare((CFStringRef)allowMixedValue, CFSTR("YES"), kCFCompareCaseInsensitive) == kCFCompareEqualTo);
} else if (typeID == CFNumberGetTypeID()) {
SInt32 val = 0;
if (CFNumberGetValue((CFNumberRef)allowMixedValue, kCFNumberSInt32Type, &val)) allowMixed = (val != 0);
}
}
});
return allowMixed;
}
// Get a list of localizations for a particular resource directory URL. Uncached. Does not include any predefined localizations from an Info.plist.
static CFArrayRef _CFBundleCopyURLLocalizations(CFAllocatorRef allocator, CFURLRef url) {
__block CFMutableArrayRef result = NULL;
CFURLRef absoluteURL = CFURLCopyAbsoluteURL(url);
CFStringRef directoryPath = CFURLCopyFileSystemPath(absoluteURL, PLATFORM_PATH_STYLE);
CFRelease(absoluteURL);
CFStringRef lproj = _CFBundleLprojExtensionWithDot;
CFIndex lprojLen = CFStringGetLength(lproj);
_CFIterateDirectory(directoryPath, ^Boolean(CFStringRef fileName, uint8_t fileType) {
// See if the fileName ends in .lproj
// The comparison starts at the end of the fileName, backed up by the length of .lproj
CFIndex fileNameLen = CFStringGetLength(fileName);
if (fileNameLen > lprojLen && CFStringCompareWithOptions(fileName, lproj, CFRangeMake(fileNameLen - lprojLen, lprojLen), 0) == kCFCompareEqualTo) {
// Chop off the .lproj part before creating a string
CFStringRef lprojDirectoryName = CFStringCreateWithSubstring(kCFAllocatorSystemDefault, fileName, CFRangeMake(0, fileNameLen - lprojLen));
if (!result) result = CFArrayCreateMutable(allocator, 0, &kCFTypeArrayCallBacks);
CFArrayAppendValue(result, lprojDirectoryName);
CFRelease(lprojDirectoryName);
}
return true;
});
CFRelease(directoryPath);
return (CFArrayRef)result;
}
static Boolean _CFBundleTryOnePreferredLprojNameInURL(CFAllocatorRef alloc, CFArrayRef localizations, CFStringRef curLangStr, CFMutableArrayRef lprojNames, Boolean fallBackToLanguage);
CF_EXPORT CFArrayRef CFBundleCopyBundleLocalizations(CFBundleRef bundle) {
CFArrayRef result = NULL;
__CFSpinLock(&bundle->_lock);
if (bundle->_lookedForLocalizations) {
result = (CFArrayRef)CFRetain(bundle->_localizations);
__CFSpinUnlock(&bundle->_lock);
return result;
}
__CFSpinUnlock(&bundle->_lock);
CFDictionaryRef infoDict = CFBundleGetInfoDictionary(bundle);
if (infoDict) {
CFArrayRef predefinedLocalizations = (CFArrayRef)CFDictionaryGetValue(infoDict, kCFBundleLocalizationsKey);
if (predefinedLocalizations && CFGetTypeID(predefinedLocalizations) != CFArrayGetTypeID()) {
CFDictionaryRemoveValue((CFMutableDictionaryRef)infoDict, kCFBundleLocalizationsKey);
} else if (predefinedLocalizations) {
// <rdar://problem/14255685> Some people put bad things inside this array =(
CFMutableArrayRef realPredefinedLocalizations = CFArrayCreateMutable(CFGetAllocator(bundle), CFArrayGetCount(predefinedLocalizations), &kCFTypeArrayCallBacks);
for (CFIndex i = 0; i < CFArrayGetCount(predefinedLocalizations); i++) {
CFStringRef oneEntry = CFArrayGetValueAtIndex(predefinedLocalizations, i);
if (CFGetTypeID(oneEntry) == CFStringGetTypeID() && CFStringGetLength(oneEntry) > 0) {
CFArrayAppendValue(realPredefinedLocalizations, oneEntry);
}
}
result = CFArrayCreateCopy(CFGetAllocator(bundle), realPredefinedLocalizations);
CFRelease(realPredefinedLocalizations);
}
}
CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(bundle);
if (resourcesURL) {
CFArrayRef lprojDirectoriesInResources = _CFBundleCopyURLLocalizations(CFGetAllocator(bundle), resourcesURL);
if (lprojDirectoriesInResources) {
if (result) {
// Append the lproj result to the predefined localization array
CFMutableArrayRef newResult = CFArrayCreateMutableCopy(kCFAllocatorDefault, 0, result);
CFRelease(result);
CFArrayAppendArray(newResult, lprojDirectoriesInResources, CFRangeMake(0, CFArrayGetCount(lprojDirectoriesInResources)));
CFRelease(lprojDirectoriesInResources);
result = newResult;
} else {
result = lprojDirectoriesInResources;
}
}
CFRelease(resourcesURL);
}
CFStringRef developmentLocalization = CFBundleGetDevelopmentRegion(bundle);
if (result) {
if (developmentLocalization) {
CFRange entireRange = CFRangeMake(0, CFArrayGetCount(result));
if (CFArrayContainsValue(result, entireRange, _CFBundleBaseDirectory)) {
// Base.lproj contains localizations for the development region. Insert the devleopment region into the existing array if there isn't already a match so that resource lookup doesn't default to another language.
CFMutableArrayRef tempArray = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeArrayCallBacks);
if (tempArray) {
if (!_CFBundleTryOnePreferredLprojNameInURL(kCFAllocatorSystemDefault, result, developmentLocalization, tempArray, false) && CFArrayGetCount(tempArray) == 0) {
CFMutableArrayRef newResult = CFArrayCreateMutableCopy(kCFAllocatorDefault, 0, result);
CFRelease(result);
CFArrayAppendValue(newResult, developmentLocalization);
result = newResult;
}
CFRelease(tempArray);
}
}
}
} else {
if (developmentLocalization) {
result = CFArrayCreate(CFGetAllocator(bundle), (const void **)&developmentLocalization, 1, &kCFTypeArrayCallBacks);
} else {
result = CFArrayCreate(CFGetAllocator(bundle), NULL, 0, &kCFTypeArrayCallBacks);
}
}
// Cache the result.
__CFSpinLock(&bundle->_lock);
if (bundle->_localizations) CFRelease(bundle->_localizations);
bundle->_localizations = (CFArrayRef)CFRetain(result);
bundle->_lookedForLocalizations = true;
__CFSpinUnlock(&bundle->_lock);
return result;
}
CFArrayRef CFBundleCopyLocalizationsForURL(CFURLRef url) {
CFArrayRef result = NULL;
CFBundleRef bundle = CFBundleCreate(kCFAllocatorSystemDefault, url);
CFStringRef devLang = NULL;
if (bundle) {
result = CFBundleCopyBundleLocalizations(bundle);
CFRelease(bundle);
} else {
CFDictionaryRef infoDict = _CFBundleCopyInfoDictionaryInExecutable(url);
if (infoDict) {
CFArrayRef predefinedLocalizations = (CFArrayRef)CFDictionaryGetValue(infoDict, kCFBundleLocalizationsKey);
if (predefinedLocalizations && CFGetTypeID(predefinedLocalizations) == CFArrayGetTypeID()) result = (CFArrayRef)CFRetain(predefinedLocalizations);
if (!result) {
devLang = (CFStringRef)CFDictionaryGetValue(infoDict, kCFBundleDevelopmentRegionKey);
if (devLang && (CFGetTypeID(devLang) == CFStringGetTypeID() && CFStringGetLength(devLang) > 0)) result = CFArrayCreate(kCFAllocatorSystemDefault, (const void **)&devLang, 1, &kCFTypeArrayCallBacks);
}
CFRelease(infoDict);
}
}
return result;
}
extern void *__CFAppleLanguages;
CF_PRIVATE CFArrayRef _CFBundleCopyUserLanguages() {
static CFArrayRef _CFBundleUserLanguages = NULL;
static dispatch_once_t once = 0;
dispatch_once(&once, ^{
CFArrayRef preferencesArray = NULL;
if (__CFAppleLanguages) {
CFDataRef data;
CFIndex length = strlen((const char *)__CFAppleLanguages);
if (length > 0) {
data = CFDataCreateWithBytesNoCopy(kCFAllocatorSystemDefault, (const UInt8 *)__CFAppleLanguages, length, kCFAllocatorNull);
if (data) {
_CFBundleUserLanguages = (CFArrayRef)CFPropertyListCreateFromXMLData(kCFAllocatorSystemDefault, data, kCFPropertyListImmutable, NULL);
CFRelease(data);
}
}
}
if (!_CFBundleUserLanguages && preferencesArray) _CFBundleUserLanguages = (CFArrayRef)CFRetain(preferencesArray);
Boolean useEnglishAsBackstop = true;
// could perhaps read out of LANG environment variable
if (useEnglishAsBackstop && !_CFBundleUserLanguages) {
CFStringRef english = CFSTR("en");
_CFBundleUserLanguages = CFArrayCreate(kCFAllocatorSystemDefault, (const void **)&english, 1, &kCFTypeArrayCallBacks);
}
if (_CFBundleUserLanguages && CFGetTypeID(_CFBundleUserLanguages) != CFArrayGetTypeID()) {
CFRelease(_CFBundleUserLanguages);
_CFBundleUserLanguages = NULL;
}
if (preferencesArray) CFRelease(preferencesArray);
});
if (_CFBundleUserLanguages) {
CFRetain(_CFBundleUserLanguages);
return _CFBundleUserLanguages;
} else {
return NULL;
}
}
CF_EXPORT void _CFBundleGetLanguageAndRegionCodes(SInt32 *languageCode, SInt32 *regionCode) {
// an attempt to answer the question, "what language are we running in?"
// note that the question cannot be answered fully since it may depend on the bundle
SInt32 language = -1, region = -1;
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFArrayRef languages = NULL;
if (mainBundle) {
languages = _CFBundleGetLanguageSearchList(mainBundle);
if (languages) CFRetain(languages);
}
if (!languages) languages = _CFBundleCopyUserLanguages();
if (languages && CFArrayGetCount(languages) > 0) {
CFStringRef localizationName = (CFStringRef)CFArrayGetValueAtIndex(languages, 0);
Boolean retval = false;
LangCode langCode = -1;
RegionCode regCode = -1;
retval = CFLocaleGetLanguageRegionEncodingForLocaleIdentifier(localizationName, &langCode, ®Code, NULL, NULL);
if (retval) {
language = langCode;
region = regCode;
}
if (!retval) {
language = _CFBundleGetLanguageCodeForLocalization(localizationName);
region = _CFBundleGetRegionCodeForLocalization(localizationName);
}
} else {
language = 0;
region = 0;
}
if (language == -1 && region != -1) language = _CFBundleGetLanguageCodeForRegionCode(region);
if (region == -1 && language != -1) region = _CFBundleGetRegionCodeForLanguageCode(language);
if (languages) CFRelease(languages);
if (languageCode) *languageCode = language;
if (regionCode) *regionCode = region;
}
static Boolean _CFBundleTryOnePreferredLprojNameInArray(CFArrayRef array, CFStringRef curLangStr, CFMutableArrayRef lprojNames, Boolean fallBackToLanguage) {
CFRange range = CFRangeMake(0, CFArrayGetCount(array));
if (range.length == 0) return false;
Boolean foundOne = false, specifiesScript = false;
CFStringRef altLangStr = NULL, modifiedLangStr = NULL, languageAbbreviation = NULL, languageName = NULL, canonicalLanguageIdentifier = NULL, canonicalLanguageAbbreviation = NULL;
CFMutableDictionaryRef canonicalLanguageIdentifiers = NULL;
if (CFArrayContainsValue(array, range, curLangStr)) {
if (!CFArrayContainsValue(lprojNames, CFRangeMake(0, CFArrayGetCount(lprojNames)), curLangStr)) CFArrayAppendValue(lprojNames, curLangStr);
foundOne = true;
if (range.length == 1 || CFStringGetLength(curLangStr) <= 2) return foundOne;
}
if (range.length == 1 && CFArrayContainsValue(array, range, CFSTR("default"))) return foundOne;
#if defined(__CONSTANT_CFSTRINGS__)
if (!altLangStr) {
CFIndex idx;
for (idx = 0; !altLangStr && idx < NUM_COMMON_LANGUAGE_NAMES; idx++) {
if (CFEqual(curLangStr, __CFBundleCommonLanguageAbbreviationsArray[idx])) altLangStr = __CFBundleCommonLanguageNamesArray[idx];
else if (CFEqual(curLangStr, __CFBundleCommonLanguageNamesArray[idx])) altLangStr = __CFBundleCommonLanguageAbbreviationsArray[idx];
}
}
if (foundOne && altLangStr) return foundOne;
#endif /* __CONSTANT_CFSTRINGS__ */
if (altLangStr && CFArrayContainsValue(array, range, altLangStr)) {
if (!CFArrayContainsValue(lprojNames, CFRangeMake(0, CFArrayGetCount(lprojNames)), altLangStr)) CFArrayAppendValue(lprojNames, altLangStr);
foundOne = true;
return foundOne;
}
if (!altLangStr && (modifiedLangStr = _CFBundleCopyModifiedLocalization(curLangStr))) {
if (CFArrayContainsValue(array, range, modifiedLangStr)) {
if (!CFArrayContainsValue(lprojNames, CFRangeMake(0, CFArrayGetCount(lprojNames)), modifiedLangStr)) CFArrayAppendValue(lprojNames, modifiedLangStr);
foundOne = true;
}
}
if (!specifiesScript && (foundOne || fallBackToLanguage) && !altLangStr && (languageAbbreviation = _CFBundleCopyLanguageAbbreviationForLocalization(curLangStr)) && !CFEqual(curLangStr, languageAbbreviation)) {
if (CFArrayContainsValue(array, range, languageAbbreviation)) {
if (!CFArrayContainsValue(lprojNames, CFRangeMake(0, CFArrayGetCount(lprojNames)), languageAbbreviation)) CFArrayAppendValue(lprojNames, languageAbbreviation);
foundOne = true;
}
}
if (!specifiesScript && (foundOne || fallBackToLanguage) && !altLangStr && (languageName = _CFBundleCopyLanguageNameForLocalization(curLangStr)) && !CFEqual(curLangStr, languageName)) {
if (CFArrayContainsValue(array, range, languageName)) {
if (!CFArrayContainsValue(lprojNames, CFRangeMake(0, CFArrayGetCount(lprojNames)), languageName)) CFArrayAppendValue(lprojNames, languageName);
foundOne = true;
}
}
if (modifiedLangStr) CFRelease(modifiedLangStr);
if (languageAbbreviation) CFRelease(languageAbbreviation);
if (languageName) CFRelease(languageName);
if (canonicalLanguageIdentifier) CFRelease(canonicalLanguageIdentifier);
if (canonicalLanguageIdentifiers) CFRelease(canonicalLanguageIdentifiers);
if (canonicalLanguageAbbreviation) CFRelease(canonicalLanguageAbbreviation);
return foundOne;
}
// localizations array must include both predefined and actual lproj localizations
static Boolean _CFBundleTryOnePreferredLprojNameInURL(CFAllocatorRef alloc, CFArrayRef localizations, CFStringRef curLangStr, CFMutableArrayRef lprojNames, Boolean fallBackToLanguage) {
CFStringRef altLangStr = NULL, modifiedLangStr = NULL, languageAbbreviation = NULL, languageName = NULL, canonicalLanguageIdentifier = NULL, canonicalLanguageAbbreviation = NULL;
CFMutableDictionaryRef canonicalLanguageIdentifiers = NULL;
Boolean foundOne = false, specifiesScript = false;
if (!localizations) return false;
CFRange localizationsRange = CFRangeMake(0, CFArrayGetCount(localizations));
// this use of contents is only checking for language strings - it could get the list of existing localizations and use that instead
if (CFArrayContainsValue(localizations, localizationsRange, curLangStr)) {
if (!CFArrayContainsValue(lprojNames, CFRangeMake(0, CFArrayGetCount(lprojNames)), curLangStr)) CFArrayAppendValue(lprojNames, curLangStr);
foundOne = true;
if (CFStringGetLength(curLangStr) <= 2) {
return foundOne;
}
}
#if defined(__CONSTANT_CFSTRINGS__)
if (!altLangStr) {
CFIndex idx;
for (idx = 0; !altLangStr && idx < NUM_COMMON_LANGUAGE_NAMES; idx++) {
if (CFEqual(curLangStr, __CFBundleCommonLanguageAbbreviationsArray[idx])) altLangStr = __CFBundleCommonLanguageNamesArray[idx];
else if (CFEqual(curLangStr, __CFBundleCommonLanguageNamesArray[idx])) altLangStr = __CFBundleCommonLanguageAbbreviationsArray[idx];
}
}
#endif /* __CONSTANT_CFSTRINGS__ */
if (foundOne && altLangStr) {
return foundOne;
}
if (altLangStr) {
if (CFArrayContainsValue(localizations, localizationsRange, altLangStr)) {
if (!CFArrayContainsValue(lprojNames, CFRangeMake(0, CFArrayGetCount(lprojNames)), altLangStr)) CFArrayAppendValue(lprojNames, altLangStr);
foundOne = true;
return foundOne;
}
}
if (!altLangStr && (modifiedLangStr = _CFBundleCopyModifiedLocalization(curLangStr))) {
if (CFArrayContainsValue(localizations, localizationsRange, modifiedLangStr)) {
if (!CFArrayContainsValue(lprojNames, CFRangeMake(0, CFArrayGetCount(lprojNames)), modifiedLangStr)) CFArrayAppendValue(lprojNames, modifiedLangStr);
foundOne = true;
}
}
if (!specifiesScript && (foundOne || fallBackToLanguage) && !altLangStr && (languageAbbreviation = _CFBundleCopyLanguageAbbreviationForLocalization(curLangStr)) && !CFEqual(curLangStr, languageAbbreviation)) {
if (CFArrayContainsValue(localizations, localizationsRange, languageAbbreviation)) {
if (!CFArrayContainsValue(lprojNames, CFRangeMake(0, CFArrayGetCount(lprojNames)), languageAbbreviation)) CFArrayAppendValue(lprojNames, languageAbbreviation);
foundOne = true;
}
}
if (!specifiesScript && (foundOne || fallBackToLanguage) && !altLangStr && (languageName = _CFBundleCopyLanguageNameForLocalization(curLangStr)) && !CFEqual(curLangStr, languageName)) {
if (CFArrayContainsValue(localizations, localizationsRange, languageName)) {
if (!CFArrayContainsValue(lprojNames, CFRangeMake(0, CFArrayGetCount(lprojNames)), languageName)) CFArrayAppendValue(lprojNames, languageName);
foundOne = true;
}
}
if (modifiedLangStr) CFRelease(modifiedLangStr);
if (languageAbbreviation) CFRelease(languageAbbreviation);
if (languageName) CFRelease(languageName);
if (canonicalLanguageIdentifier) CFRelease(canonicalLanguageIdentifier);
if (canonicalLanguageIdentifiers) CFRelease(canonicalLanguageIdentifiers);
if (canonicalLanguageAbbreviation) CFRelease(canonicalLanguageAbbreviation);
return foundOne;
}
static Boolean _CFBundleLocalizationsHaveCommonPrefix(CFStringRef loc1, CFStringRef loc2) {
Boolean result = false;
CFIndex length1 = CFStringGetLength(loc1), length2 = CFStringGetLength(loc2), idx;
if (length1 > 3 && length2 > 3) {
for (idx = 0; idx < length1 && idx < length2; idx++) {
UniChar c1 = CFStringGetCharacterAtIndex(loc1, idx), c2 = CFStringGetCharacterAtIndex(loc2, idx);
if (idx >= 2 && (c1 == '-' || c1 == '_') && (c2 == '-' || c2 == '_')) {
result = true;
break;
} else if (c1 != c2) {
break;
}
}
}
return result;
}
static void _CFBundleAddPreferredLprojNamesInDirectory(CFAllocatorRef alloc, CFURLRef bundleURL, CFArrayRef localizations, CFMutableArrayRef lprojNames, CFStringRef devLang) {
// This function will add zero, one or two elements to the lprojNames array.
// It examines the users preferred language list and the lproj directories inside the bundle directory. It picks the lproj directory that is highest on the users list.
// The users list can contain region names (like "en_US" for US English). In this case, if the region lproj exists, it will be added, and, if the region's associated language lproj exists that will be added.
Boolean foundOne = false;
// First check the main bundle.
if (!CFBundleAllowMixedLocalizations()) {
CFBundleRef mainBundle = CFBundleGetMainBundle();
if (mainBundle) {
CFURLRef mainBundleURL = CFBundleCopyBundleURL(mainBundle);
if (mainBundleURL) {
if (!CFEqual(bundleURL, mainBundleURL)) {
// If there is a main bundle, and it isn't this one, try to use the language it prefers.
CFArrayRef mainBundleLangs = _CFBundleGetLanguageSearchList(mainBundle);
if (mainBundleLangs && (CFArrayGetCount(mainBundleLangs) > 0)) {
CFStringRef curLangStr = (CFStringRef)CFArrayGetValueAtIndex(mainBundleLangs, 0);
foundOne = _CFBundleTryOnePreferredLprojNameInURL(kCFAllocatorSystemDefault, localizations, curLangStr, lprojNames, true);
}
}
CFRelease(mainBundleURL);
}
}
}
if (!foundOne) {
// If we didn't find the main bundle's preferred language, look at the users' prefs again and find the best one.
CFArrayRef userLanguages = _CFBundleCopyUserLanguages();
if (userLanguages) {
CFIndex count = CFArrayGetCount(userLanguages);
CFIndex idx, startIdx;
for (idx = 0, startIdx = -1; !foundOne && idx < count; idx++) {
CFStringRef curLangStr = (CFStringRef)CFArrayGetValueAtIndex(userLanguages, idx);
CFStringRef nextLangStr = (idx + 1 < count) ? (CFStringRef)CFArrayGetValueAtIndex(userLanguages, idx + 1) : NULL;
if (nextLangStr && _CFBundleLocalizationsHaveCommonPrefix(curLangStr, nextLangStr)) {
foundOne = _CFBundleTryOnePreferredLprojNameInURL(kCFAllocatorSystemDefault, localizations, curLangStr, lprojNames, false);
if (startIdx < 0) startIdx = idx;
} else if (startIdx >= 0 && startIdx <= idx) {
foundOne = _CFBundleTryOnePreferredLprojNameInURL(kCFAllocatorSystemDefault, localizations, curLangStr, lprojNames, false);
for (; !foundOne && startIdx <= idx; startIdx++) {
curLangStr = (CFStringRef)CFArrayGetValueAtIndex(userLanguages, startIdx);
foundOne = _CFBundleTryOnePreferredLprojNameInURL(kCFAllocatorSystemDefault, localizations, curLangStr, lprojNames, true);
}
startIdx = -1;
} else {
foundOne = _CFBundleTryOnePreferredLprojNameInURL(kCFAllocatorSystemDefault, localizations, curLangStr, lprojNames, true);
startIdx = -1;
}
}
}
// use development region and U.S. English as backstops
if (!foundOne && devLang) foundOne = _CFBundleTryOnePreferredLprojNameInURL(kCFAllocatorSystemDefault, localizations, devLang, lprojNames, true);
if (!foundOne) foundOne = _CFBundleTryOnePreferredLprojNameInURL(kCFAllocatorSystemDefault, localizations, CFSTR("en_US"), lprojNames, true);
if (userLanguages) CFRelease(userLanguages);
}
}
static CFArrayRef _CFBundleCopyLanguageSearchListInDirectory(CFAllocatorRef alloc, CFURLRef url, uint8_t *version) {
uint8_t localVersion = 0;
CFDictionaryRef infoDict = _CFBundleCopyInfoDictionaryInDirectory(alloc, url, &localVersion);
CFArrayRef predefinedLocalizations = NULL;
CFStringRef devLang = NULL;
if (infoDict) {
devLang = (CFStringRef)CFDictionaryGetValue(infoDict, kCFBundleDevelopmentRegionKey);
if (devLang && (CFGetTypeID(devLang) != CFStringGetTypeID() || CFStringGetLength(devLang) == 0)) devLang = NULL;
predefinedLocalizations = (CFArrayRef)CFDictionaryGetValue(infoDict, kCFBundleLocalizationsKey);
if (predefinedLocalizations && CFGetTypeID(predefinedLocalizations) != CFArrayGetTypeID()) {
predefinedLocalizations = NULL;
CFDictionaryRemoveValue((CFMutableDictionaryRef)infoDict, kCFBundleLocalizationsKey);
}
}
CFURLRef resourcesURL = _CFBundleCopyResourcesDirectoryURLInDirectory(url, localVersion);
CFArrayRef localizations = _CFBundleCopyURLLocalizations(alloc, resourcesURL);
CFRelease(resourcesURL);
if (predefinedLocalizations && localizations) {
CFMutableArrayRef newLocalizations = CFArrayCreateMutableCopy(alloc, 0, predefinedLocalizations);
CFArrayAppendArray(newLocalizations, localizations, CFRangeMake(0, CFArrayGetCount(localizations)));
CFRelease(localizations);
localizations = (CFArrayRef)newLocalizations;
} else if (predefinedLocalizations) {
localizations = (CFArrayRef)CFRetain(predefinedLocalizations);
} else if (!localizations) {
localizations = CFArrayCreate(alloc, NULL, 0, &kCFTypeArrayCallBacks);
}