Subversion Repositories oidinfo_api

Compare Revisions

Regard whitespace Rev 11 → Rev 12

/trunk/oid_utils.inc.phps
2,8 → 2,8
 
/*
* OID-Utilities for PHP
* Copyright 2011-2019 Daniel Marschall, ViaThinkSoft
* Version 2019-03-25
* Copyright 2011-2020 Daniel Marschall, ViaThinkSoft
* Version 2020-06-11
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
20,8 → 20,8
 
// All functions in this library are compatible with leading zeroes (not recommended) and leading dots
 
// TODO: change the function names, so that they have a uniform naming schema, and rename "oid identifier" into "asn.1 alphanumeric identifier"
// TODO: Function for finding a shared ancestor, e.g. oid_shared_ancestor('2.999.1.2.3', '2.999.4.5') == '2.999'
// TODO: change some function names, so that they have a uniform naming schema, and rename "oid identifier" into "asn.1 alphanumeric identifier"
// oid_id_is_valid() => asn1_alpha_id_valid()
 
define('OID_DOT_FORBIDDEN', 0);
define('OID_DOT_OPTIONAL', 1);
305,6 → 305,26
}
 
/**
* Checks if two OIDs in dot-notation are equal
* @author Daniel Marschall, ViaThinkSoft
* @version 2020-05-27
* @param $oidA (string)<br />
* First OID
* @param $oidB (string)<br />
* Second OID
* @return (bool) True if the OIDs are equal
**/
function oid_dotnotation_equal($oidA, $oidB) {
$oidA = sanitizeOID($oidA, false);
if ($oidA === false) return null;
 
$oidB = sanitizeOID($oidB, false);
if ($oidB === false) return null;
 
return $oidA === $oidB;
}
 
/**
* Removes leading zeroes from an OID in dot notation.
* @author Daniel Marschall, ViaThinkSoft
* @version 2015-08-17
417,7 → 437,6
 
return count($ary) - count($bry);
}
 
/*
assert(oid_distance('2.999.1.2.3', '2.999.4.5') === false);
assert(oid_distance('2.999.1.2', '2.999') === 2);
458,7 → 477,50
return $oid;
}
 
/**
* Find the common ancestor of two or more OIDs
* @author Daniel Marschall, ViaThinkSoft
* @version 2020-05-27
* @param $oids (array)<br />
* An array of multiple OIDs, e.g. 2.999.1 and 2.999.2.3.4
* @return (mixed) The common ancestor, e.g. 2.999, or false if there is no common ancestor.
**/
function oid_common_ancestor(array $oids) {
$shared = array();
 
if (!is_array($oids)) return false;
if (count($oids) === 0) return false;
 
foreach ($oids as &$oid) {
$oid = sanitizeOID($oid, false);
if ($oid === false) return false;
$oid = explode('.', $oid);
}
 
$max_ok = count($oids[0]);
for ($i=1; $i<count($oids); $i++) {
for ($j=0; $j<min(count($oids[$i]),count($oids[0])); $j++) {
if ($oids[$i][$j] != $oids[0][$j]) {
if ($j < $max_ok) $max_ok = $j;
break;
}
}
if ($j < $max_ok) $max_ok = $j;
}
 
$out = array();
for ($i=0; $i<$max_ok; $i++) {
$out[] = $oids[0][$i];
}
return implode('.', $out);
}
/*
assert(oid_shared_ancestor(array('2.999.4.5.3', '2.999.4.5')) === "2.999.4.5");
assert(oid_shared_ancestor(array('2.999.4.5', '2.999.4.5.3')) === "2.999.4.5");
assert(oid_shared_ancestor(array('2.999.1.2.3', '2.999.4.5')) === "2.999");
*/
 
 
# === OID-IRI NOTATION FUNCTIONS ===
 
if (!function_exists('mb_ord')) {
605,7 → 667,7
/**
* Tries to shorten/simplify an IRI by applying "long arcs", e.g. /2/999/123 -> /Example/123 .
* @author Daniel Marschall, ViaThinkSoft
* @version 2014-12-28
* @version 2020-05-22
* @param $iri (string)<br />
* An OID in OID-IRI notation, e.g. /Example/test
* @return (string) The modified IRI.
613,25 → 675,17
function iri_add_longarcs($iri) {
$iri_long_arcs = iri_get_long_arcs();
 
// TODO: $iri valid?
if (!iri_valid($iri)) return false;
 
$ary = explode('/', $iri);
 
$ary_number_iri = $ary;
if ($ary_number_iri[1] == 'Joint-ISO-ITU-T') $ary_number_iri[1] = '2';
/*
if ($ary_number_iri[1] == '2') {
// TODO: /2/Example -> /2/999 -> /Example
} else {
// Currently, only long arcs inside .2 are defined
// return $iri;
}
*/
 
$number_iri = implode('/', $ary_number_iri);
 
foreach ($iri_long_arcs as $cur_longarc => $cur_iri) {
// TODO: $cur_iri valid?
 
assert(iri_valid($cur_iri));
if (strpos($number_iri.'/', $cur_iri.'/') === 0) {
$cnt = substr_count($cur_iri, '/');
for ($i=1; $i<$cnt; $i++) {
646,6 → 700,9
 
return $iri;
}
/*
assert(iri_add_longarcs('/2/999/123') === '/Example/123');
*/
 
# === FUNCTIONS FOR OIDS IN ASN.1 NOTATION ===
 
652,15 → 709,18
/**
* Checks if an ASN.1 identifier is valid.
* @author Daniel Marschall, ViaThinkSoft
* @version 2014-12-09
* @version 2020-05-22
* @param $id (string)<br />
* An ASN.1 identifier, e.g. "example". Not "example(99)" or "99" and not a path like "{ 2 999 }"
* Note: Use asn1_path_valid() for validating a whole ASN.1 notation path.
* @return (bool) true, if the identifier is valid: It begins with an lowercase letter and contains only 0-9, a-z, A-Z and "-"
**/
# TODO: umbenennen in asn1_alpha_id_valid
function oid_id_is_valid($id) {
return preg_match('/^([a-z][a-zA-Z0-9-]*)$/', $id);
// see Rec. ITU-T X.660 | ISO/IEC 9834-1, clause 7.7
// and Rec. ITU-T X.680 | ISO/IEC 8824-1, clause 12.3
if (substr($id,-1,1) == '-') return false;
if (strstr($id,'--')) return false;
return preg_match('/^([a-z][a-zA-Z0-9-]*)$/', $id) != 0;
}
 
/**
793,10 → 853,26
*/
 
/**
* Gets the last numeric identifier of an ASN.1 notation OID.
* @author Daniel Marschall, ViaThinkSoft
* @version 2020-06-11
* @param $asn1id (string)<br />
* An ASN.1 identifier string, e.g. { 2 example(999) test(1) }
* @return (int) The last numeric identifier arc, e.g. "1"
**/
function asn1_last_identifier($asn1id) {
$asn1id = preg_replace('@\(\s*\d+\s*\)@', '', $asn1id);
$asn1id = trim(str_replace(array('{', '}', "\t"), ' ', $asn1id));
$ary = explode(' ', $asn1id);
$asn1id = $ary[count($ary)-1];
return preg_match('#[^0-9]#',$asn1id) ? $asn1id : false;
}
 
/**
* "Soft corrects" an invalid ASN.1 identifier.<br />
* Attention, by "soft correcting" the ID, it is not authoritative anymore, and might not be able to be resolved by ORS.
* @author Daniel Marschall, ViaThinkSoft
* @version 2014-12-09
* @version 2020-05-22
* @param $id (string)<br />
* An ASN.1 identifier.
* @param $append_id_prefix (bool)<br />
811,6 → 887,9
// Convert "_" to "-"
$id = str_replace('_', '-', $id);
 
// Convert "--" to "-"
$id = str_replace('--', '-', $id);
 
// Remove invalid characters
$id = preg_replace('/[^a-zA-Z0-9-]+/', '', $id);
 
828,4 → 907,3
 
return $id;
}
 
/trunk/uuid_utils.inc.phps
2,8 → 2,8
 
/*
* UUID utils for PHP
* Copyright 2011-2019 Daniel Marschall, ViaThinkSoft
* Version 2019-11-04
* Copyright 2011-2020 Daniel Marschall, ViaThinkSoft
* Version 2020-02-28
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
18,7 → 18,10
* limitations under the License.
*/
 
# This library requires either the GMP extension (or BCMath if gmp_supplement.inc.php is present)
 
if (file_exists(__DIR__ . '/mac_utils.inc.phps')) include_once __DIR__ . '/mac_utils.inc.phps';
if (file_exists(__DIR__ . '/gmp_supplement.inc.php')) include_once __DIR__ . '/gmp_supplement.inc.php';
 
define('UUID_NAMEBASED_NS_DNS', '6ba7b810-9dad-11d1-80b4-00c04fd430c8');
define('UUID_NAMEBASED_NS_URL', '6ba7b811-9dad-11d1-80b4-00c04fd430c8');
/trunk/xml_utils.inc.phps
2,8 → 2,8
 
/*
* XML Encoding Utilities
* Copyright 2011-2019 Daniel Marschall, ViaThinkSoft
* Version 1.7
* Copyright 2011-2020 Daniel Marschall, ViaThinkSoft
* Version 1.7.1
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
83,7 → 83,7
return false;
}
 
$h = ord($c{$index});
$h = ord($c[$index]);
 
if ($h <= 0x7F) {
$bytes = 1;
92,16 → 92,16
return false;
} else if ($h <= 0xDF && $index < $len - 1) {
$bytes = 2;
return ($h & 0x1F) << 6 | (ord($c{$index + 1}) & 0x3F);
return ($h & 0x1F) << 6 | (ord($c[$index + 1]) & 0x3F);
} else if ($h <= 0xEF && $index < $len - 2) {
$bytes = 3;
return ($h & 0x0F) << 12 | (ord($c{$index + 1}) & 0x3F) << 6
| (ord($c{$index + 2}) & 0x3F);
return ($h & 0x0F) << 12 | (ord($c[$index + 1]) & 0x3F) << 6
| (ord($c[$index + 2]) & 0x3F);
} else if ($h <= 0xF4 && $index < $len - 3) {
$bytes = 4;
return ($h & 0x0F) << 18 | (ord($c{$index + 1}) & 0x3F) << 12
| (ord($c{$index + 2}) & 0x3F) << 6
| (ord($c{$index + 3}) & 0x3F);
return ($h & 0x0F) << 18 | (ord($c[$index + 1]) & 0x3F) << 12
| (ord($c[$index + 2]) & 0x3F) << 6
| (ord($c[$index + 3]) & 0x3F);
} else {
return false;
}