Loading...
  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
/*
 * Copyright (c) 2020 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@
 */

#include <iterator>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <charconv>
#include <TargetConditionals.h>
#include <mach-o/loader.h>

#include "Platform.h"
#include "Architecture.h"

#ifndef PLATFORM_VISIONOS
  #define PLATFORM_VISIONOS 11
#endif

#ifndef PLATFORM_VISIONOSSIMULATOR
  #define PLATFORM_VISIONOSSIMULATOR 12
#endif

#ifndef PLATFORM_MACOS_EXCLAVECORE
  #define PLATFORM_MACOS_EXCLAVECORE 15
#endif

#ifndef PLATFORM_MACOS_EXCLAVEKIT
  #define PLATFORM_MACOS_EXCLAVEKIT 16
#endif

#ifndef PLATFORM_IOS_EXCLAVECORE
  #define PLATFORM_IOS_EXCLAVECORE 17
#endif

#ifndef PLATFORM_IOS_EXCLAVEKIT
  #define PLATFORM_IOS_EXCLAVEKIT 18
#endif

#ifndef PLATFORM_TVOS_EXCLAVECORE
  #define PLATFORM_TVOS_EXCLAVECORE 19
#endif

#ifndef PLATFORM_TVOS_EXCLAVEKIT
  #define PLATFORM_TVOS_EXCLAVEKIT 20
#endif

#ifndef PLATFORM_WATCHOS_EXCLAVECORE
  #define PLATFORM_WATCHOS_EXCLAVECORE 21
#endif

#ifndef PLATFORM_WATCHOS_EXCLAVEKIT
  #define PLATFORM_WATCHOS_EXCLAVEKIT 22
#endif

#ifndef PLATFORM_VISIONOS_EXCLAVECORE
  #define PLATFORM_VISIONOS_EXCLAVECORE 23
#endif

#ifndef PLATFORM_VISIONOS_EXCLAVEKIT
  #define PLATFORM_VISIONOS_EXCLAVEKIT 24
#endif



namespace mach_o {


//
// MARK: --- Epoch values ---
//

constinit const Platform::Epoch Platform::Epoch::invalid(0);
constinit const Platform::Epoch Platform::Epoch::fall2012(2012);
constinit const Platform::Epoch Platform::Epoch::fall2013(2013);
constinit const Platform::Epoch Platform::Epoch::fall2015(2015);
constinit const Platform::Epoch Platform::Epoch::fall2016(2016);
constinit const Platform::Epoch Platform::Epoch::fall2017(2017);
constinit const Platform::Epoch Platform::Epoch::fall2018(2018);
constinit const Platform::Epoch Platform::Epoch::fall2019(2019);
constinit const Platform::Epoch Platform::Epoch::spring2020(2020, true);
constinit const Platform::Epoch Platform::Epoch::fall2020(2020);
constinit const Platform::Epoch Platform::Epoch::fall2021(2021);
constinit const Platform::Epoch Platform::Epoch::spring2021(2021, true);
constinit const Platform::Epoch Platform::Epoch::fall2022(2022);
constinit const Platform::Epoch Platform::Epoch::fall2023(2023);
constinit const Platform::Epoch Platform::Epoch::spring2024(2024, true);
constinit const Platform::Epoch Platform::Epoch::fall2024(2024);
constinit const Platform::Epoch Platform::Epoch::spring2025(2025, true);
constinit const Platform::Epoch Platform::Epoch::fall2025(2025);



//
// MARK: --- PlatformInfo class ---
//

/*!
 * @class PlatformInfo
 *
 * @abstract
 *      Implementation details for Platform.
 */
class VIS_HIDDEN PlatformInfo
{
public:
    consteval PlatformInfo(uint32_t v, CString nm, bool isSim, bool fp, bool alignedIn2025, uint16_t year, const PlatformInfo* info, CString altName = {})
        : basePlatformInfo(info), name(nm), altName(altName), value(v), baseYear(year),
          isSimulator(isSim), supportsFairPlayEncryption(fp), wasAlignedIn2025(alignedIn2025) { }

    const PlatformInfo* basePlatformInfo;
    CString             name;
    CString             altName;
    uint32_t            value;
    uint16_t            baseYear;    // year 1.0 shipped
    bool                isSimulator;
    bool                isExclaveCore=false;
    bool                isExclaveKit=false;
    bool                supportsFairPlayEncryption;
    bool                wasAlignedIn2025;           // was this platform bumped to version 26.0 in fall 2025?

    // Epoch is private to Platform and PlatformInfo, so convert to year/spring of use by subclasses
    Platform::Epoch epochForVersion(Version32 vers) const {
        uint16_t year;
        bool     spring;
        this->yearForVersion(vers, year, spring);
        return Platform::Epoch(year, spring);
    }

    virtual void yearForVersion(Version32 vers, uint16_t& year, bool& spring) const {
        yearForMajorVersion(vers, year, spring);
    }
    virtual uint16_t minorVersionForSpring(uint16_t major) const {
        // most spring releses are X.4
        return 4;
    }


protected:
    friend class Platform;
    static constinit const PlatformInfo* knownPlatformInfos[];

    void yearForMajorVersion(Version32 vers, uint16_t& year, bool& spring) const {
        // version is >= 11.0

        // In 2025, many platform aligned on version 26
        if ( wasAlignedIn2025 ) {
            if ( vers.major() >= 26 ) {
                year = 2000 + (vers.major() - 1);
            } else {
                // clamp anything in the gap to 26
                year = baseYear + vers.major();
                if ( year > 2026 )
                    year = 2026;
            }
        } else {
            year = baseYear + vers.major();
        }

        spring = (vers.minor() >= minorVersionForSpring(vers.major()));
        // Say the 2023 fall release has the year 2023, then we want the following spring release,
        // what availability calls 2023(e) to actually be in calendar year 2024
        if ( spring )
            ++year;
    }

    void yearForTenMinorVersion(Version32 vers, uint16_t tenBaseYear, uint16_t& year, bool& spring) const {
        // version is 10.x
        year = tenBaseYear + ((vers.value() - 0x000A0000) >> 8);
        spring = ((vers.value() & 0x000000FF) >= 0x04);
        if ( spring )
            --year;
    }
};


class VIS_HIDDEN PlatformInfo_macOS : public PlatformInfo
{
public:
    consteval PlatformInfo_macOS() : PlatformInfo(PLATFORM_MACOS, "macOS", false, false, true, 2009, &PlatformInfo_macOS::singleton, "macOSX") { }

    void yearForVersion(Version32 vers, uint16_t& year, bool& spring) const override {
        if ( vers >= Version32(11,0) )
            yearForMajorVersion(vers, year, spring);             // 11.0 -> 2020
        else
            yearForTenMinorVersion(vers, 2004, year, spring);    // 10.15 -> 2019
    }

    uint16_t minorVersionForSpring(uint16_t major) const override {
        // The past releases have been 11.3, 12.3, 13.3, so assume that pattern for those releases.
        if ( major <= 13 )
            return 3;

        // 14.4 needs a 4
        // also assume that later releases are .4, just to be conservative
        return 4;
    }

    static const PlatformInfo_macOS singleton;
protected:
    consteval PlatformInfo_macOS(uint32_t v, CString nm, bool isSim, const PlatformInfo* info = &PlatformInfo_macOS::singleton) 
                                    : PlatformInfo(v, nm, isSim, false, true, 2009, info) { }
};
const PlatformInfo_macOS        PlatformInfo_macOS::singleton;


class VIS_HIDDEN PlatformInfo_iOS : public PlatformInfo
{
public:
    consteval PlatformInfo_iOS() : PlatformInfo(PLATFORM_IOS, "iOS", false, true, true, 2006, &PlatformInfo_iOS::singleton) { }

    static const PlatformInfo_iOS singleton;
protected:
    consteval PlatformInfo_iOS(uint32_t v, CString nm, bool isSim, const PlatformInfo* info = &PlatformInfo_iOS::singleton, CString altName = {})
                                : PlatformInfo(v, nm, isSim, !isSim, true, 2006, info, altName) { }
};
const PlatformInfo_iOS      PlatformInfo_iOS::singleton;


// iOS_simulator uses same versioning as iOS
class VIS_HIDDEN PlatformInfo_iOS_simulator : public PlatformInfo_iOS
{
public:
    consteval PlatformInfo_iOS_simulator() : PlatformInfo_iOS(PLATFORM_IOSSIMULATOR, "iOS-simulator", true) { }

    static const PlatformInfo_iOS_simulator singleton;
};
const PlatformInfo_iOS_simulator         PlatformInfo_iOS_simulator::singleton;


// tvOS uses same versioning as iOS
class VIS_HIDDEN PlatformInfo_tvOS : public PlatformInfo_iOS
{
public:
    consteval PlatformInfo_tvOS() : PlatformInfo_iOS(PLATFORM_TVOS, "tvOS", false, &PlatformInfo_tvOS::singleton) { }

    static const PlatformInfo_tvOS  singleton;
protected:
    consteval PlatformInfo_tvOS(uint32_t v, CString nm, bool isSim, const PlatformInfo* info = &PlatformInfo_tvOS::singleton)
                                : PlatformInfo_iOS(v, nm, isSim, info) { }
};
const PlatformInfo_tvOS      PlatformInfo_tvOS::singleton;


// tvOS_simulator uses same versioning as tvOS
class VIS_HIDDEN PlatformInfo_tvOS_simulator : public PlatformInfo_tvOS
{
public:
    consteval PlatformInfo_tvOS_simulator() : PlatformInfo_tvOS(PLATFORM_TVOSSIMULATOR, "tvOS-simulator", true) { }

    static const PlatformInfo_tvOS_simulator    singleton;
};
const PlatformInfo_tvOS_simulator  PlatformInfo_tvOS_simulator::singleton;




// catalyst uses same versioning as iOS
class VIS_HIDDEN PlatformInfo_macCatalyst : public PlatformInfo_iOS
{
public:
    consteval PlatformInfo_macCatalyst() : PlatformInfo_iOS(PLATFORM_MACCATALYST, "macCatalyst", false, &PlatformInfo_iOS::singleton, "Mac Catalyst") {
        supportsFairPlayEncryption = false;
    }

    static const PlatformInfo_macCatalyst   singleton;
};
const PlatformInfo_macCatalyst PlatformInfo_macCatalyst::singleton;

class VIS_HIDDEN PlatformInfo_zippered : public PlatformInfo_macOS
{
public:
    consteval PlatformInfo_zippered() : PlatformInfo_macOS(PLATFORM_ZIPPERED, "zippered(macOS/Catalyst)", false) { }

    static const PlatformInfo_zippered   singleton;
};
const PlatformInfo_zippered PlatformInfo_zippered::singleton;


class VIS_HIDDEN PlatformInfo_watchOS : public PlatformInfo
{
public:
    consteval PlatformInfo_watchOS() : PlatformInfo(PLATFORM_WATCHOS, "watchOS", false, true, true, 2013, &PlatformInfo_watchOS::singleton) { }

    static const PlatformInfo_watchOS   singleton;
protected:
    consteval PlatformInfo_watchOS(uint32_t v, CString nm, bool isSim, const PlatformInfo* info = &PlatformInfo_watchOS::singleton)
                                    : PlatformInfo(v, nm, isSim, !isSim, true, 2013, info) { }
};
const PlatformInfo_watchOS    PlatformInfo_watchOS::singleton;


// watchOS_simulator uses same versioning as watchOS
class VIS_HIDDEN PlatformInfo_watchOS_simulator: public PlatformInfo_watchOS
{
public:
    consteval PlatformInfo_watchOS_simulator() : PlatformInfo_watchOS(PLATFORM_WATCHOSSIMULATOR, "watchOS-simulator", true) { }

    static const PlatformInfo_watchOS_simulator     singleton;
};
const PlatformInfo_watchOS_simulator PlatformInfo_watchOS_simulator::singleton;


class VIS_HIDDEN PlatformInfo_bridgeOS : public PlatformInfo
{
public:
    consteval PlatformInfo_bridgeOS() : PlatformInfo(PLATFORM_BRIDGEOS, "bridgeOS", false, false, false, 2015, &PlatformInfo_bridgeOS::singleton) { }

    uint16_t minorVersionForSpring(uint16_t major) const override {
        // The past 2 releases have been 7.3 and 8.3, so assume that pattern for those releases.
        if ( major <= 8 )
            return 3;

        // use .4 for future just in case it changes.  We'd rather be conservative for future
        // than accidentally opt in something we shouldn't
        return 4;
    }

    static const PlatformInfo_bridgeOS singleton;
protected:
    consteval PlatformInfo_bridgeOS(uint32_t v, CString nm) : PlatformInfo(v, nm, false, false, false, 2015, &PlatformInfo_bridgeOS::singleton) { }
};
const PlatformInfo_bridgeOS         PlatformInfo_bridgeOS::singleton;



class VIS_HIDDEN PlatformInfo_driverKit : public PlatformInfo
{
public:
    consteval PlatformInfo_driverKit() : PlatformInfo(PLATFORM_DRIVERKIT, "driverKit", false, true, false, 2000, &PlatformInfo_driverKit::singleton) { }

    static const PlatformInfo_driverKit singleton;
};
const PlatformInfo_driverKit         PlatformInfo_driverKit::singleton;


// firmware does not using versioning or epochs
class VIS_HIDDEN PlatformInfo_firmware : public PlatformInfo
{
public:
    consteval PlatformInfo_firmware() : PlatformInfo(PLATFORM_FIRMWARE, "firmware", false, false, false, 0, &PlatformInfo_firmware::singleton, "free standing") { }

    void yearForVersion(Version32 vers, uint16_t& year, bool& spring) const override {
        year = 2020;
        spring = false;
    }

    static const PlatformInfo_firmware singleton;
};
const PlatformInfo_firmware         PlatformInfo_firmware::singleton;


// sepOS does not using versioning or epochs
class VIS_HIDDEN PlatformInfo_sepOS : public PlatformInfo
{
public:
    consteval PlatformInfo_sepOS() : PlatformInfo(PLATFORM_SEPOS, "sepOS", false, false, false, 0, &PlatformInfo_sepOS::singleton) { }

    void yearForVersion(Version32 vers, uint16_t& year, bool& spring) const override {
        year = 2020;
        spring = false;
    }
    static const PlatformInfo_sepOS singleton;
};
const PlatformInfo_sepOS         PlatformInfo_sepOS::singleton;


// macOS_exclaveCore OS versioning follows macOS versioning
class VIS_HIDDEN PlatformInfo_macOS_exclaveCore : public PlatformInfo_macOS
{
public:
    consteval PlatformInfo_macOS_exclaveCore() : PlatformInfo_macOS(PLATFORM_MACOS_EXCLAVECORE, "macOS-exclaveCore", false, &PlatformInfo_macOS_exclaveCore::singleton)
    {
        isExclaveCore = true;
    }

    static const PlatformInfo_macOS_exclaveCore     singleton;
};
const PlatformInfo_macOS_exclaveCore PlatformInfo_macOS_exclaveCore::singleton;

// macOS_exclaveKit OS versioning follows macOS versioning
class VIS_HIDDEN PlatformInfo_macOS_exclaveKit : public PlatformInfo_macOS
{
public:
    consteval PlatformInfo_macOS_exclaveKit() : PlatformInfo_macOS(PLATFORM_MACOS_EXCLAVEKIT, "macOS-exclaveKit", false, &PlatformInfo_macOS_exclaveKit::singleton)
    {
        isExclaveKit = true;
    }

    static const PlatformInfo_macOS_exclaveKit     singleton;
};
const PlatformInfo_macOS_exclaveKit PlatformInfo_macOS_exclaveKit::singleton;


// iOS_exclaveCore OS versioning follows iOS versioning
class VIS_HIDDEN PlatformInfo_iOS_exclaveCore : public PlatformInfo_iOS
{
public:
    consteval PlatformInfo_iOS_exclaveCore() : PlatformInfo_iOS(PLATFORM_IOS_EXCLAVECORE, "iOS-exclaveCore", false, &PlatformInfo_iOS_exclaveCore::singleton)
    {
        supportsFairPlayEncryption = false;
        isExclaveCore = true;
    }

    static const PlatformInfo_iOS_exclaveCore     singleton;
};
const PlatformInfo_iOS_exclaveCore PlatformInfo_iOS_exclaveCore::singleton;

// iOS_exclaveKit OS versioning follows iOS versioning
class VIS_HIDDEN PlatformInfo_iOS_exclaveKit : public PlatformInfo_iOS
{
public:
    consteval PlatformInfo_iOS_exclaveKit() : PlatformInfo_iOS(PLATFORM_IOS_EXCLAVEKIT, "iOS-exclaveKit", false, &PlatformInfo_iOS_exclaveKit::singleton)
    {
        supportsFairPlayEncryption = false;
        isExclaveKit = true;
    }

    static const PlatformInfo_iOS_exclaveKit     singleton;
};
const PlatformInfo_iOS_exclaveKit PlatformInfo_iOS_exclaveKit::singleton;


// tvOS_exclaveCore OS versioning follows tvOS versioning
class VIS_HIDDEN PlatformInfo_tvOS_exclaveCore : public PlatformInfo_tvOS
{
public:
    consteval PlatformInfo_tvOS_exclaveCore() : PlatformInfo_tvOS(PLATFORM_TVOS_EXCLAVECORE, "tvOS-exclaveCore", false, &PlatformInfo_tvOS_exclaveCore::singleton)
    {
        supportsFairPlayEncryption = false;
        isExclaveCore = true;
    }

    static const PlatformInfo_tvOS_exclaveCore     singleton;
};
const PlatformInfo_tvOS_exclaveCore PlatformInfo_tvOS_exclaveCore::singleton;

// tvOS_exclaveKit OS versioning follows tvOS versioning
class VIS_HIDDEN PlatformInfo_tvOS_exclaveKit : public PlatformInfo_tvOS
{
public:
    consteval PlatformInfo_tvOS_exclaveKit() : PlatformInfo_tvOS(PLATFORM_TVOS_EXCLAVEKIT, "tvOS-exclaveKit", false, &PlatformInfo_tvOS_exclaveKit::singleton)
    {
        supportsFairPlayEncryption = false;
        isExclaveKit = true;
    }

    static const PlatformInfo_tvOS_exclaveKit     singleton;
};
const PlatformInfo_tvOS_exclaveKit PlatformInfo_tvOS_exclaveKit::singleton;


// watchOS_exclaveCore OS versioning follows watchOS versioning
class VIS_HIDDEN PlatformInfo_watchOS_exclaveCore : public PlatformInfo_watchOS
{
public:
    consteval PlatformInfo_watchOS_exclaveCore() : PlatformInfo_watchOS(PLATFORM_WATCHOS_EXCLAVECORE, "watchOS-exclaveCore", false, &PlatformInfo_watchOS_exclaveCore::singleton)
    {
        supportsFairPlayEncryption = false;
        isExclaveCore = true;
    }

    static const PlatformInfo_watchOS_exclaveCore  singleton;
};
const PlatformInfo_watchOS_exclaveCore PlatformInfo_watchOS_exclaveCore::singleton;

// watchOS_exclaveKit OS versioning follows watchOS versioning
class VIS_HIDDEN PlatformInfo_watchOS_exclaveKit : public PlatformInfo_watchOS
{
public:
    consteval PlatformInfo_watchOS_exclaveKit() : PlatformInfo_watchOS(PLATFORM_WATCHOS_EXCLAVEKIT, "watchOS-exclaveKit", false, &PlatformInfo_watchOS_exclaveKit::singleton)
    {
        supportsFairPlayEncryption = false;
        isExclaveKit = true;
    }

    static const PlatformInfo_watchOS_exclaveKit  singleton;
};
const PlatformInfo_watchOS_exclaveKit PlatformInfo_watchOS_exclaveKit::singleton;

class VIS_HIDDEN PlatformInfo_visionOS : public PlatformInfo
{
public:
    consteval PlatformInfo_visionOS() : PlatformInfo(PLATFORM_VISIONOS, "visionOS", false, true, true, 2022, &PlatformInfo_visionOS::singleton, "xrOS") {}

    uint16_t minorVersionForSpring(uint16_t major) const override {
        // The first spring release is 1.1
        if ( major == 1 )
            return 1;

        // use .4 for future just in case it changes from the above.  We'd rather be conservative
        // for future than accidentally opt in something we shouldn't
        return 4;
    }

    static const PlatformInfo_visionOS singleton;
protected:
    consteval PlatformInfo_visionOS(uint32_t v, CString nm, bool isSim, const PlatformInfo* info = &PlatformInfo_visionOS::singleton, CString altName = {}) : PlatformInfo(v, nm, isSim, !isSim, true, 2022, info, altName) { }
};
const PlatformInfo_visionOS          PlatformInfo_visionOS::singleton;

class VIS_HIDDEN PlatformInfo_visionOS_simulator : public PlatformInfo_visionOS
{
public:
    consteval PlatformInfo_visionOS_simulator() : PlatformInfo_visionOS(PLATFORM_VISIONOSSIMULATOR, "visionOS-simulator", true, &PlatformInfo_visionOS::singleton, "xrOS-simulator") { }

    static const PlatformInfo_visionOS_simulator    singleton;
};
const PlatformInfo_visionOS_simulator          PlatformInfo_visionOS_simulator::singleton;

class VIS_HIDDEN PlatformInfo_visionOS_exclaveCore : public PlatformInfo_visionOS
{
public:
    consteval PlatformInfo_visionOS_exclaveCore() : PlatformInfo_visionOS(PLATFORM_VISIONOS_EXCLAVECORE, "visionOS-exclaveCore", false, &PlatformInfo_visionOS_exclaveCore::singleton, "xrOS-exclaveCore")
    {
        supportsFairPlayEncryption = false;
        isExclaveCore = true;
    }

    static const PlatformInfo_visionOS_exclaveCore  singleton;
};
const PlatformInfo_visionOS_exclaveCore          PlatformInfo_visionOS_exclaveCore::singleton;

class VIS_HIDDEN PlatformInfo_visionOS_exclaveKit : public PlatformInfo_visionOS
{
public:
    consteval PlatformInfo_visionOS_exclaveKit() : PlatformInfo_visionOS(PLATFORM_VISIONOS_EXCLAVEKIT, "visionOS-exclaveKit", false, &PlatformInfo_visionOS_exclaveKit::singleton, "xrOS-exclaveKit")
    {
        supportsFairPlayEncryption = false;
        isExclaveKit = true;
    }

    static const PlatformInfo_visionOS_exclaveKit  singleton;
};
const PlatformInfo_visionOS_exclaveKit          PlatformInfo_visionOS_exclaveKit::singleton;




// for constructing a Platform() by value
constinit const PlatformInfo* PlatformInfo::knownPlatformInfos[] = {
    &PlatformInfo_macOS::singleton,
    &PlatformInfo_iOS::singleton,
    &PlatformInfo_tvOS::singleton,
    &PlatformInfo_watchOS::singleton,
    &PlatformInfo_bridgeOS::singleton,
    &PlatformInfo_macCatalyst::singleton,
    &PlatformInfo_zippered::singleton,
    &PlatformInfo_iOS_simulator::singleton,
    &PlatformInfo_tvOS_simulator::singleton,
    &PlatformInfo_watchOS_simulator::singleton,
    &PlatformInfo_driverKit::singleton,
    &PlatformInfo_firmware::singleton,
    &PlatformInfo_sepOS::singleton,
    &PlatformInfo_visionOS::singleton,
    &PlatformInfo_visionOS_simulator::singleton,
    &PlatformInfo_macOS_exclaveCore::singleton,
    &PlatformInfo_macOS_exclaveKit::singleton,
    &PlatformInfo_iOS_exclaveCore::singleton,
    &PlatformInfo_iOS_exclaveKit::singleton,
    &PlatformInfo_tvOS_exclaveCore::singleton,
    &PlatformInfo_tvOS_exclaveKit::singleton,
    &PlatformInfo_watchOS_exclaveCore::singleton,
    &PlatformInfo_watchOS_exclaveKit::singleton,
    &PlatformInfo_visionOS_exclaveCore::singleton,
    &PlatformInfo_visionOS_exclaveKit::singleton,

};



//
// MARK: --- Platform methods ---
//


Platform::Platform(uint32_t platformNumber) : _info(nullptr), _value(0)
{
    for (const PlatformInfo* p : PlatformInfo::knownPlatformInfos) {
        assert(p->value != 0 && "PlatformInfo value uninitialized, this might be a problem with C++ static initializers order");
        if ( p->value == platformNumber ) {
            _info = p;
            return;
        }
    }
    _value = platformNumber;
}

Platform Platform::byName(std::string_view name) {
    auto normalizedStringComp = [](char c1, char c2) {
        return (c1 == c2 || toupper(c1) == toupper(c2)
                || (c1 == ' ' && c2 == '-') || (c1 == '-' && c2 == ' '));
    };
    // check if this is a platform name in all platforms
    for ( const PlatformInfo* p : PlatformInfo::knownPlatformInfos ) {
        std::string_view pname = p->name;
        if ( std::equal(name.begin(), name.end(), pname.begin(), pname.end(), normalizedStringComp) )
            return Platform(*p);

        pname = p->altName;
        if ( std::equal(name.begin(), name.end(), pname.begin(), pname.end(), normalizedStringComp) )
            return Platform(*p);
    }

    // check if this is a raw platform number
    uint32_t num = 0;
    const char* end = name.data() + name.size();
    if ( std::from_chars_result res = std::from_chars(name.data(), end, num);
            res.ec == std::errc() && res.ptr == end
            && Platform(num).valid().noError() )
        return Platform(num);

    // Hack for -macabi
    if ( name == "ios-macabi" )
        return Platform::macCatalyst;

    return Platform(0);
}

Error Platform::valid() const
{
    if ( _info == nullptr )
        return Error("unknown platform");
    return Error();
}

bool Platform::empty() const
{
    return (_info == nullptr) && _value == 0;
}

CString Platform::name() const
{
    if ( _info == nullptr ) {
        if ( _value != 0 )
            return "future";
        return "unknown";
    }
    return _info->name;
}

Platform Platform::basePlatform() const
{
    if ( _info == nullptr ) {
        if ( _value != 0 )
            return *this;
        return Platform();
    }
    return Platform(*(_info->basePlatformInfo));
}

bool Platform::isSimulator() const
{
    if ( _info == nullptr )
        return false;
    return _info->isSimulator;
}

bool Platform::isExclaveCore() const
{
    if ( _info == nullptr )
        return false;
    return _info->isExclaveCore;
}

bool Platform::isExclaveKit() const
{
    if ( _info == nullptr )
        return false;
    return _info->isExclaveKit;
}


bool Platform::maybeFairPlayEncrypted() const
{
    return (_info != nullptr) && _info->supportsFairPlayEncryption;
}

bool Platform::canLoad(Platform other) const
{
    // can always link/load something built for same platform
    if ( _info == other._info )
        return true;

    // macOS and catalyst can link against zippered dylibs
    if ( other == Platform::zippered ) {
        if ( *this == Platform::macOS )
            return true;
        if ( *this == Platform::macCatalyst )
            return true;
    }

    return false;
}

CString Platform::libSystemDir() const
{
    if ( *this == Platform::sepOS )
        return "";
    if ( *this == Platform::firmware )
        return "";
    if ( isExclaveCore() )
        return "";

    if ( *this == Platform::driverKit )
        return "/System/DriverKit/usr/lib/system/";
    if ( isExclaveKit() )
        return "/System/ExclaveKit/usr/lib/system/";
    return "/usr/lib/system/";
}

uint32_t Platform::value() const
{
    return _info ? _info->value : _value;
}

Platform::Epoch Platform::epoch(Version32 v) const
{
    if ( _info != nullptr )
        return _info->epochForVersion(v);
    else
        return Epoch::invalid;
}


Platform Platform::current()
{
#if TARGET_OS_SIMULATOR
  #if TARGET_OS_WATCH
    return watchOS_simulator;
  #elif TARGET_OS_TV
    return tvOS_simulator;
  #elif TARGET_OS_VISION
    return visionOS_simulator;  // before TARGET_OS_IOS because TARGET_OS_IOS is set on visionOS internal SDK
  #elif TARGET_OS_IOS
    return iOS_simulator;
  #endif
#elif TARGET_OS_EXCLAVEKIT
  #if TARGET_OS_WATCH
    return watchOS_exclaveKit;
  #elif TARGET_OS_TV
    return tvOS_exclaveKit;
  #elif TARGET_OS_IOS
    return iOS_exclaveKit;
  #elif TARGET_OS_VISION
    return visionOS_exclaveKit;
  #else
    return macOS_exclaveKit;
  #endif
#elif TARGET_OS_BRIDGE
    return bridgeOS;
#elif TARGET_OS_WATCH
    return watchOS;
#elif TARGET_OS_TV
    return tvOS;
#elif TARGET_OS_VISION
    return visionOS;  // before TARGET_OS_IOS because TARGET_OS_IOS is set on visionOS internal SDK
#elif TARGET_OS_IOS
    return iOS;
#elif TARGET_OS_OSX
    return macOS;
#elif TARGET_OS_DRIVERKIT
    return driverKit;
#else
  #error unknown platform
#endif
}

constinit const Platform Platform::macOS(             PlatformInfo_macOS::singleton);
constinit const Platform Platform::iOS(               PlatformInfo_iOS::singleton);
constinit const Platform Platform::tvOS(              PlatformInfo_tvOS::singleton);
constinit const Platform Platform::watchOS(           PlatformInfo_watchOS::singleton);
constinit const Platform Platform::bridgeOS(          PlatformInfo_bridgeOS::singleton);
constinit const Platform Platform::macCatalyst(       PlatformInfo_macCatalyst::singleton);
constinit const Platform Platform::zippered(          PlatformInfo_zippered::singleton);
constinit const Platform Platform::iOS_simulator(     PlatformInfo_iOS_simulator::singleton);
constinit const Platform Platform::tvOS_simulator(    PlatformInfo_tvOS_simulator::singleton);
constinit const Platform Platform::watchOS_simulator( PlatformInfo_watchOS_simulator::singleton);
constinit const Platform Platform::driverKit(         PlatformInfo_driverKit::singleton);
constinit const Platform Platform::firmware(          PlatformInfo_firmware::singleton);
constinit const Platform Platform::sepOS(             PlatformInfo_sepOS::singleton);
constinit const Platform Platform::visionOS(          PlatformInfo_visionOS::singleton);
constinit const Platform Platform::visionOS_simulator(PlatformInfo_visionOS_simulator::singleton);
constinit const Platform Platform::macOS_exclaveCore(     PlatformInfo_macOS_exclaveCore::singleton);
constinit const Platform Platform::macOS_exclaveKit(      PlatformInfo_macOS_exclaveKit::singleton);
constinit const Platform Platform::iOS_exclaveCore(       PlatformInfo_iOS_exclaveCore::singleton);
constinit const Platform Platform::iOS_exclaveKit(        PlatformInfo_iOS_exclaveKit::singleton);
constinit const Platform Platform::tvOS_exclaveCore(      PlatformInfo_tvOS_exclaveCore::singleton);
constinit const Platform Platform::tvOS_exclaveKit(       PlatformInfo_tvOS_exclaveKit::singleton);
constinit const Platform Platform::watchOS_exclaveCore(   PlatformInfo_watchOS_exclaveCore::singleton);
constinit const Platform Platform::watchOS_exclaveKit(    PlatformInfo_watchOS_exclaveKit::singleton);
constinit const Platform Platform::visionOS_exclaveCore(  PlatformInfo_visionOS_exclaveCore::singleton);
constinit const Platform Platform::visionOS_exclaveKit(   PlatformInfo_visionOS_exclaveKit::singleton);


Error PlatformAndVersions::zip(const PlatformAndVersions& other)
{
    // Use other platform to initialize an empty platform.
    if ( platform.empty() ) {
        *this = other;
        return Error::none();
    }

    if ( Error err = other.platform.valid() ) {
        return Error("can't zip with invalid platform");
    }

    if ( platform == other.platform ) {
        *this = other;
        return Error::none();
    }

    if ( platform == Platform::macOS && other.platform == Platform::macCatalyst ) {
        *this = { Platform::zippered, minOS, sdk, other.minOS, other.sdk };
        return Error::none();
    }

    if ( other.platform == Platform::macOS && platform == Platform::macCatalyst ) {
        *this = { Platform::zippered, other.minOS, other.sdk, minOS, sdk };
        return Error::none();
    }

    // Handle additional -macos_version_min/-maccatalyst_version_min when we are already zippered
    if ( platform == Platform::zippered ) {
        if ( other.platform == Platform::macCatalyst ) {
            *this = { Platform::zippered, minOS, sdk, other.minOS, other.sdk };
            return Error::none();
        }
        if ( other.platform == Platform::macOS ) {
            *this = { Platform::zippered, other.minOS, other.sdk, zipMinOS, zipSdk };
            return Error::none();
        }
    }

    return Error("incompatible platforms: %s - %s", platform.name().c_str(), other.platform.name().c_str());
}

void PlatformAndVersions::unzip(void(^ callback)(PlatformAndVersions)) const
{
    if ( platform != Platform::zippered ) {
        callback(*this);
        return;
    }

    callback({Platform::macOS, minOS, sdk});
    callback({Platform::macCatalyst, zipMinOS, zipSdk});
}

uint32_t PlatformAndVersions::loadCommandsCount() const
{
    return (platform == Platform::zippered ? 2 : 1);
}


Error PlatformAndVersions::setFromTargetTriple(CString triple, Architecture& outArch)
{
    char splitBuf[triple.size()+1];
    strlcpy(splitBuf, triple.c_str(), sizeof(splitBuf));

    // replace dash with nul so there are 3 or 4 strings: arch, vendor, osVersion, env
    const char* arch   = splitBuf;
    const char* vendor = nullptr;
    const char* osVers = nullptr;
    const char* env    = nullptr;
    for (int i=0; i < sizeof(splitBuf); i++) {
        if ( splitBuf[i] == '-' ) {
            splitBuf[i] = '\0';
            if ( vendor == nullptr )
                vendor = &splitBuf[i+1];
            else if ( osVers == nullptr )
                osVers = &splitBuf[i+1];
            else if ( env == nullptr )
                env = &splitBuf[i+1];
            else
                return Error("more than three dashes in target triple '%s'", triple.c_str());
        }
    }
    if ( (vendor == nullptr) || (osVers == nullptr) )
        return Error("missing dashes in target triple '%s'", triple.c_str());


    // also return architecture from triple
    outArch = Architecture::byName(arch);
    if ( outArch == Architecture() )
        return Error("unknown architecture in target triple '%s'", triple.c_str());

    // firmware uses triples like "thumbv7m-apple-unknown-macho"
    if ( strcmp(osVers, "unknown") == 0 ) {
        this->platform = Platform::firmware;
        this->minOS    = Version32(0,0);
        this->sdk      = Version32(0,0);
        return Error::none();
    }

    // split osVersion into osName and minOS version
    const size_t osVersLen = strlen(osVers);
    char osName[triple.size()+1];
    char minOSVers[osVersLen+1];
    strlcpy(osName, osVers, sizeof(osName));
    minOSVers[0] = '\0';
    for (int i=0; i < osVersLen; i++) {
        if ( isdigit(osName[i]) ) {
            strlcpy(minOSVers, &osName[i], sizeof(minOSVers));
            osName[i] = '\0';
            break;
        }
    }

    //  macosx is historical name
    if ( strcmp(osName, "macosx") == 0 )
        strlcpy(osName, "macos", sizeof(osName));

    // Apple sub-platforms are fourth part of triple, but need to be added to OS name to make platform name
    // e.g. "arm64-apple-tvos16.0-simulator" --> "tvos-simulator"
    if ( env != nullptr ) {
        strlcat(osName, "-", sizeof(osName));
        strlcat(osName, env, sizeof(osName));
    }
    this->platform = Platform::byName(osName);

    // EFIShell
    if ( this->platform.empty() && CString(osName) == "windows-macho" )
        this->platform = Platform::firmware;

    if ( this->platform.empty() )
        return Error("unknown OS in target triple '%s'", triple.c_str());

    if ( (strlen(minOSVers) == 0) && (this->platform != Platform::firmware) )
        return Error("missing OS version in target triple '%s'", triple.c_str());

    // get minOS from version trailing OS name in triple
    if (Error err = Version32::fromString(minOSVers, this->minOS))
        return err;
    
    // SDK version is not encoded in triple
    this->sdk = Version32(0,0);

    return Error::none();
}




} // namespace mach_o