Subversion Repositories oidplus

Rev

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

Rev 1424 Rev 1438
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-11-11
6
 * Version 2023-11-18
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 845... Line 845...
845
function uuid_canonize($uuid) {
845
function uuid_canonize($uuid) {
846
        if (!uuid_valid($uuid)) return false;
846
        if (!uuid_valid($uuid)) return false;
847
        return oid_to_uuid(uuid_to_oid($uuid));
847
        return oid_to_uuid(uuid_to_oid($uuid));
848
}
848
}
849
 
849
 
-
 
850
/*
-
 
851
assert(oid_to_uuid('2.25.111325678376819997685911819737516232943')=='53c08bb6-b2eb-5038-bf28-ad41a08c50ef');
-
 
852
assert(oid_to_uuid('1.2.840.113556.1.8000.2554.21440.35766.45803.20536.48936.11354528.9195759')=='53c08bb6-b2eb-5038-bf28-ad41a08c50ef');
-
 
853
assert(oid_to_uuid('1.3.6.1.4.1.54392.1.6034977117478539320.13774449957690691823')=='53c08bb6-b2eb-5038-bf28-ad41a08c50ef');
-
 
854
assert(oid_to_uuid('1.3.6.1.4.1.54392.2.1405127606.3001765944.3207114049.2693550319')=='53c08bb6-b2eb-5038-bf28-ad41a08c50ef');
-
 
855
assert(oid_to_uuid('1.3.6.1.4.1.54392.3.21440.35766.45803.20536.48936.44353.41100.20719')=='53c08bb6-b2eb-5038-bf28-ad41a08c50ef');
-
 
856
*/
850
function oid_to_uuid($oid) {
857
function oid_to_uuid($oid) {
851
        // TODO: Also support Non-2.25 base UUID-to-OID
-
 
852
        if (!is_uuid_oid($oid,true)) return false;
-
 
853
 
-
 
854
        if (substr($oid,0,1) == '.') {
858
        if (substr($oid,0,1) == '.') $oid = substr($oid, 1); // remove leading dot
855
                $oid = substr($oid, 1);
-
 
856
        }
-
 
857
        $ary = explode('.', $oid);
-
 
858
 
859
 
859
        if (!isset($ary[2])) return false;
860
        // Information about Microsoft and Waterjuice UUID-OID: https://waterjuiceweb.wordpress.com/2019/09/24/guids-to-oids/
860
 
861
 
-
 
862
        $ary = explode('.', $oid);
-
 
863
        if ((count($ary) == 3) && (strpos($oid, '2.25.') === 0)) {
-
 
864
                // ISO/ITU-T UUID-to-OID
-
 
865
                // Example: {53c08bb6-b2eb-5038-bf28-ad41a08c50ef} = 2.25.111325678376819997685911819737516232943
861
        $val = $ary[2];
866
                $val = $ary[2];
862
 
-
 
863
        $x = gmp_init($val, 10);
867
                $dec = gmp_init($val, 10);
864
        $y = gmp_strval($x, 16);
868
                $hex = gmp_strval($dec, 16);
865
        $y = str_pad($y, 32, "0", STR_PAD_LEFT);
869
                $hex = str_pad($hex, 32, "0", STR_PAD_LEFT);
-
 
870
                return substr($hex,0,8).'-'.substr($hex,8,4).'-'.substr($hex,12,4).'-'.substr($hex,16,4).'-'.substr($hex,20,12);
-
 
871
        } else if ((count($ary) == 14) && (strpos($oid, '1.2.840.113556.1.8000.2554.') === 0)) {
-
 
872
                // Microsoft UUID-to-OID
-
 
873
                // Example: {53c08bb6-b2eb-5038-bf28-ad41a08c50ef} = 1.2.840.113556.1.8000.2554.21440.35766.45803.20536.48936.11354528.9195759
-
 
874
                $a = $ary[7];
-
 
875
                $b = $ary[8];
-
 
876
                $c = $ary[9];
-
 
877
                $d = $ary[10];
-
 
878
                $e = $ary[11];
-
 
879
                $f = $ary[12];
-
 
880
                $g = $ary[13];
-
 
881
                return dechex($a).dechex($b).'-'.dechex($c).'-'.dechex($d).'-'.dechex($e).'-'.dechex($f).dechex($g);
-
 
882
        } else if ((count($ary) == 10) && (strpos($oid, '1.3.6.1.4.1.54392.1.') === 0)) {
-
 
883
                // Waterjuice UUID-to-OID 2x64 Bits
-
 
884
                // Example: {53c08bb6-b2eb-5038-bf28-ad41a08c50ef} = 1.3.6.1.4.1.54392.1.6034977117478539320.13774449957690691823
-
 
885
                $a1 = gmp_strval(gmp_init($ary[8],10),16); if (strlen($a1)>16) return false;
-
 
886
                $a2 = gmp_strval(gmp_init($ary[9],10),16); if (strlen($a2)>16) return false;
-
 
887
                $hex =
-
 
888
                        str_pad($a1, 16, "0", STR_PAD_LEFT).
-
 
889
                        str_pad($a2, 16, "0", STR_PAD_LEFT);
-
 
890
                return substr($hex,0,8).'-'.substr($hex,8,4).'-'.substr($hex,12,4).'-'.substr($hex,16,4).'-'.substr($hex,20,12);
-
 
891
        } else if ((count($ary) == 12) && (strpos($oid, '1.3.6.1.4.1.54392.2.') === 0)) {
-
 
892
                // Waterjuice UUID-to-OID 4x32 Bits
-
 
893
                // Example: {53c08bb6-b2eb-5038-bf28-ad41a08c50ef} = 1.3.6.1.4.1.54392.2.1405127606.3001765944.3207114049.2693550319
-
 
894
                $a1 = gmp_strval(gmp_init($ary[8],10),16); if (strlen($a1)>8) return false;
-
 
895
                $a2 = gmp_strval(gmp_init($ary[9],10),16); if (strlen($a2)>8) return false;
-
 
896
                $a3 = gmp_strval(gmp_init($ary[10],10),16); if (strlen($a3)>8) return false;
-
 
897
                $a4 = gmp_strval(gmp_init($ary[11],10),16); if (strlen($a4)>8) return false;
-
 
898
                $hex =
-
 
899
                        str_pad($a1, 8, "0", STR_PAD_LEFT).
-
 
900
                        str_pad($a2, 8, "0", STR_PAD_LEFT).
-
 
901
                        str_pad($a3, 8, "0", STR_PAD_LEFT).
-
 
902
                        str_pad($a4, 8, "0", STR_PAD_LEFT);
-
 
903
                return substr($hex,0,8).'-'.substr($hex,8,4).'-'.substr($hex,12,4).'-'.substr($hex,16,4).'-'.substr($hex,20,12);
-
 
904
        } else if ((count($ary) == 16) && (strpos($oid, '1.3.6.1.4.1.54392.3.') === 0)) {
-
 
905
                // Waterjuice UUID-to-OID 8x16 Bits
-
 
906
                // Example: {53c08bb6-b2eb-5038-bf28-ad41a08c50ef} = 1.3.6.1.4.1.54392.3.21440.35766.45803.20536.48936.44353.41100.20719
-
 
907
                $a1 = gmp_strval(gmp_init($ary[8],10),16); if (strlen($a1)>4) return false;
-
 
908
                $a2 = gmp_strval(gmp_init($ary[9],10),16); if (strlen($a2)>4) return false;
-
 
909
                $a3 = gmp_strval(gmp_init($ary[10],10),16); if (strlen($a3)>4) return false;
-
 
910
                $a4 = gmp_strval(gmp_init($ary[11],10),16); if (strlen($a4)>4) return false;
-
 
911
                $a5 = gmp_strval(gmp_init($ary[12],10),16); if (strlen($a5)>4) return false;
-
 
912
                $a6 = gmp_strval(gmp_init($ary[13],10),16); if (strlen($a6)>4) return false;
-
 
913
                $a7 = gmp_strval(gmp_init($ary[14],10),16); if (strlen($a7)>4) return false;
-
 
914
                $a8 = gmp_strval(gmp_init($ary[15],10),16); if (strlen($a8)>4) return false;
-
 
915
                $hex =
-
 
916
                        str_pad($a1, 4, "0", STR_PAD_LEFT).
-
 
917
                        str_pad($a2, 4, "0", STR_PAD_LEFT).
-
 
918
                        str_pad($a3, 4, "0", STR_PAD_LEFT).
866
        return substr($y,  0, 8).'-'.
919
                        str_pad($a4, 4, "0", STR_PAD_LEFT).
867
               substr($y,  8, 4).'-'.
920
                        str_pad($a5, 4, "0", STR_PAD_LEFT).
868
               substr($y, 12, 4).'-'.
921
                        str_pad($a6, 4, "0", STR_PAD_LEFT).
869
               substr($y, 16, 4).'-'.
922
                        str_pad($a7, 4, "0", STR_PAD_LEFT).
870
               substr($y, 20, 12);
923
                        str_pad($a8, 4, "0", STR_PAD_LEFT);
-
 
924
                return substr($hex,0,8).'-'.substr($hex,8,4).'-'.substr($hex,12,4).'-'.substr($hex,16,4).'-'.substr($hex,20,12);
-
 
925
        } else {
-
 
926
                return false;
-
 
927
        }
871
}
928
}
872
 
929
 
873
function is_uuid_oid($oid, $only_allow_root=false) {
930
function is_uuid_oid($oid, $only_allow_root=false) {
874
        // TODO: Also support Non-2.25 base UUID-to-OID
-
 
875
        if (substr($oid,0,1) == '.') $oid = substr($oid, 1); // remove leading dot
931
        if (substr($oid,0,1) == '.') $oid = substr($oid, 1); // remove leading dot
876
 
932
 
877
        $ary = explode('.', $oid);
-
 
878
 
-
 
879
        if ($only_allow_root) {
933
        if ($only_allow_root) {
880
                if (count($ary) != 3) return false;
934
                return oid_to_uuid($oid) !== false;
881
        } else {
935
        } else {
882
                if (count($ary) < 3) return false;
936
                // TODO: Check range of the components (e.g. max 128 bits for 2.25)
883
        }
-
 
884
 
-
 
885
        if ($ary[0] != '2') return false;
937
                if (strpos($oid,'2.25.') === 0) return true;
886
        if ($ary[1] != '25') return false;
938
                if (strpos($oid,'1.2.840.113556.1.8000.2554.') === 0) return true;
887
        for ($i=2; $i<count($ary); $i++) {
-
 
888
                $v = $ary[$i];
-
 
889
                if (!is_numeric($v)) return false;
939
                if (strpos($oid,'1.3.6.1.4.1.54392.1.') === 0) return true;
890
                if ($i == 2) {
-
 
891
                        // Must be in the range of 128 bit UUID
940
                if (strpos($oid,'1.3.6.1.4.1.54392.2.') === 0) return true;
892
                        $test = gmp_init($v, 10);
-
 
893
                        if (strlen(gmp_strval($test, 16)) > 32) return false;
941
                if (strpos($oid,'1.3.6.1.4.1.54392.3.') === 0) return true;
894
                }
-
 
895
                if ($v < 0) return false;
942
                return false;
896
        }
943
        }
897
 
-
 
898
        return true;
-
 
899
}
944
}
900
 
945
 
901
function uuid_to_oid($uuid, $base='2.25') {
946
function uuid_to_oid($uuid, $base='2.25') {
902
        if (!uuid_valid($uuid)) return false;
947
        if (!uuid_valid($uuid)) return false;
903
        #$base = oid_sanitize($base);
948
        #$base = oid_sanitize($base);