Subversion Repositories uuid_mac_utils

Rev

Rev 75 | Rev 78 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 75 Rev 77
Line 1... Line 1...
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 * UUID utils for PHP
4
 * UUID utils for PHP
5
 * Copyright 2011 - 2023 Daniel Marschall, ViaThinkSoft
5
 * Copyright 2011 - 2023 Daniel Marschall, ViaThinkSoft
6
 * Version 2023-10-06
6
 * Version 2023-11-11
7
 *
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
10
 * You may obtain a copy of the License at
11
 *
11
 *
Line 85... Line 85...
85
        echo sprintf("%-32s %s\n", "URN:", 'urn:uuid:' . strtolower(oid_to_uuid(uuid_to_oid($uuid))));
85
        echo sprintf("%-32s %s\n", "URN:", 'urn:uuid:' . strtolower(oid_to_uuid(uuid_to_oid($uuid))));
86
        echo sprintf("%-32s %s\n", "URI:", 'uuid:' . strtolower(oid_to_uuid(uuid_to_oid($uuid))));
86
        echo sprintf("%-32s %s\n", "URI:", 'uuid:' . strtolower(oid_to_uuid(uuid_to_oid($uuid))));
87
        echo sprintf("%-32s %s\n", "Microsoft GUID syntax:", '{' . strtoupper(oid_to_uuid(uuid_to_oid($uuid))) . '}');
87
        echo sprintf("%-32s %s\n", "Microsoft GUID syntax:", '{' . strtoupper(oid_to_uuid(uuid_to_oid($uuid))) . '}');
88
        echo sprintf("%-32s %s\n", "C++ struct syntax:", uuid_c_syntax($uuid));
88
        echo sprintf("%-32s %s\n", "C++ struct syntax:", uuid_c_syntax($uuid));
89
        echo "\n";
89
        echo "\n";
-
 
90
 
90
        echo sprintf("%-32s %s\n", "As OID:", $oid);
91
        echo sprintf("%-32s %s\n", "As OID (ISO/ITU-T 128 bits):", $oid=uuid_to_oid($uuid, '2.25'));
-
 
92
        # Removed because it is too much information (we would also need to add this to the other OIDs too)
91
        if (class_exists('OidDerConverter')) {
93
        #if (class_exists('OidDerConverter')) {
92
                echo sprintf("%-32s %s\n", "DER encoding of OID:", OidDerConverter::hexarrayToStr(OidDerConverter::oidToDER($oid)));
94
        #       echo sprintf("%-32s %s\n", "DER encoding of OID:", OidDerConverter::hexarrayToStr(OidDerConverter::oidToDER($oid)));
93
        }
95
        #}
-
 
96
        echo sprintf("%-32s %s\n", "As OID (Microsoft):", $oid=uuid_to_oid($uuid, '1.2.840.113556.1.8000.2554'));
-
 
97
        echo sprintf("%-32s %s\n", "As OID (Waterjuice 2x64 bits):", $oid=uuid_to_oid($uuid, '1.3.6.1.4.1.54392.1'));
-
 
98
        echo sprintf("%-32s %s\n", "As OID (Waterjuice 4x32 bits):", $oid=uuid_to_oid($uuid, '1.3.6.1.4.1.54392.2'));
-
 
99
        echo sprintf("%-32s %s\n", "As OID (Waterjuice 8x16 bits):", $oid=uuid_to_oid($uuid, '1.3.6.1.4.1.54392.3'));
-
 
100
 
94
        echo "\n";
101
        echo "\n";
-
 
102
 
95
        echo "<u>Interpretation of the UUID:</u>\n\n";
103
        echo "<u>Interpretation of the UUID:</u>\n\n";
96
 
104
 
97
        if (!$echo) ob_start();
105
        if (!$echo) ob_start();
98
 
106
 
99
        #$uuid = trim($uuid);
107
        #$uuid = trim($uuid);
Line 838... Line 846...
838
        if (!uuid_valid($uuid)) return false;
846
        if (!uuid_valid($uuid)) return false;
839
        return oid_to_uuid(uuid_to_oid($uuid));
847
        return oid_to_uuid(uuid_to_oid($uuid));
840
}
848
}
841
 
849
 
842
function oid_to_uuid($oid) {
850
function oid_to_uuid($oid) {
-
 
851
        // TODO: Also support Non-2.25 base UUID-to-OID
843
        if (!is_uuid_oid($oid,true)) return false;
852
        if (!is_uuid_oid($oid,true)) return false;
844
 
853
 
845
        if (substr($oid,0,1) == '.') {
854
        if (substr($oid,0,1) == '.') {
846
                $oid = substr($oid, 1);
855
                $oid = substr($oid, 1);
847
        }
856
        }
Line 860... Line 869...
860
               substr($y, 16, 4).'-'.
869
               substr($y, 16, 4).'-'.
861
               substr($y, 20, 12);
870
               substr($y, 20, 12);
862
}
871
}
863
 
872
 
864
function is_uuid_oid($oid, $only_allow_root=false) {
873
function is_uuid_oid($oid, $only_allow_root=false) {
-
 
874
        // TODO: Also support Non-2.25 base UUID-to-OID
865
        if (substr($oid,0,1) == '.') $oid = substr($oid, 1); // remove leading dot
875
        if (substr($oid,0,1) == '.') $oid = substr($oid, 1); // remove leading dot
866
 
876
 
867
        $ary = explode('.', $oid);
877
        $ary = explode('.', $oid);
868
 
878
 
869
        if ($only_allow_root) {
879
        if ($only_allow_root) {
Line 886... Line 896...
886
        }
896
        }
887
 
897
 
888
        return true;
898
        return true;
889
}
899
}
890
 
900
 
891
function uuid_to_oid($uuid) {
901
function uuid_to_oid($uuid, $base='2.25') {
892
        if (!uuid_valid($uuid)) return false;
902
        if (!uuid_valid($uuid)) return false;
-
 
903
        #$base = oid_sanitize($base);
893
 
904
 
894
        $uuid = str_replace(array('-', '{', '}'), '', $uuid);
905
        $uuid = str_replace(array('-', '{', '}'), '', $uuid);
-
 
906
 
-
 
907
        // Information about Microsoft and Waterjuice UUID-OID: https://waterjuiceweb.wordpress.com/2019/09/24/guids-to-oids/
-
 
908
 
-
 
909
        if ($base == '2.25') {
895
        $x = gmp_init($uuid, 16);
910
                $x = gmp_init($uuid, 16);
896
        return '2.25.'.gmp_strval($x, 10);
911
                return $base.'.'.gmp_strval($x, 10);
-
 
912
        } else if ($base == '1.2.840.113556.1.8000.2554') {
-
 
913
                return $base.'.'.
-
 
914
                        gmp_strval(gmp_init(substr($uuid,0,4),16),10).'.'.
-
 
915
                        gmp_strval(gmp_init(substr($uuid,4,4),16),10).'.'.
-
 
916
                        gmp_strval(gmp_init(substr($uuid,8,4),16),10).'.'.
-
 
917
                        gmp_strval(gmp_init(substr($uuid,12,4),16),10).'.'.
-
 
918
                        gmp_strval(gmp_init(substr($uuid,16,4),16),10).'.'.
-
 
919
                        gmp_strval(gmp_init(substr($uuid,20,6),16),10).'.'.
-
 
920
                        gmp_strval(gmp_init(substr($uuid,26,6),16),10);
-
 
921
        } else if ($base == '1.3.6.1.4.1.54392.1') {
-
 
922
                return $base.'.'.
-
 
923
                        gmp_strval(gmp_init(substr($uuid,0,16),16),10).'.'.
-
 
924
                        gmp_strval(gmp_init(substr($uuid,16,16),16),10);
-
 
925
        } else if ($base == '1.3.6.1.4.1.54392.2') {
-
 
926
                return $base.'.'.
-
 
927
                        gmp_strval(gmp_init(substr($uuid,0,8),16),10).'.'.
-
 
928
                        gmp_strval(gmp_init(substr($uuid,8,8),16),10).'.'.
-
 
929
                        gmp_strval(gmp_init(substr($uuid,16,8),16),10).'.'.
-
 
930
                        gmp_strval(gmp_init(substr($uuid,24,8),16),10);
-
 
931
        } else if ($base == '1.3.6.1.4.1.54392.3') {
-
 
932
                return $base.'.'.
-
 
933
                        gmp_strval(gmp_init(substr($uuid,0,4),16),10).'.'.
-
 
934
                        gmp_strval(gmp_init(substr($uuid,4,4),16),10).'.'.
-
 
935
                        gmp_strval(gmp_init(substr($uuid,8,4),16),10).'.'.
-
 
936
                        gmp_strval(gmp_init(substr($uuid,12,4),16),10).'.'.
-
 
937
                        gmp_strval(gmp_init(substr($uuid,16,4),16),10).'.'.
-
 
938
                        gmp_strval(gmp_init(substr($uuid,20,4),16),10).'.'.
-
 
939
                        gmp_strval(gmp_init(substr($uuid,24,4),16),10).'.'.
-
 
940
                        gmp_strval(gmp_init(substr($uuid,28,4),16),10);
-
 
941
        } else {
-
 
942
                throw new Exception("Unsupported UUID-to-OID base");
-
 
943
        }
897
}
944
}
898
 
945
 
899
function uuid_numeric_value($uuid) {
946
function uuid_numeric_value($uuid) {
900
        $oid = uuid_to_oid($uuid);
947
        $oid = uuid_to_oid($uuid);
901
        if (!$oid) return false;
948
        if (!$oid) return false;