PageRenderTime 65ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/trustly/phpseclib/File/ASN1.php

https://github.com/DaveBenNoah/PrestaShop-modules
PHP | 1273 lines | 790 code | 93 blank | 390 comment | 183 complexity | 92a5f946b266316f46576a930312d436 MD5 | raw file
Possible License(s): Apache-2.0, CC-BY-SA-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Pure-PHP ASN.1 Parser
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * ASN.1 provides the semantics for data encoded using various schemes. The most commonly
  9. * utilized scheme is DER or the "Distinguished Encoding Rules". PEM's are base64 encoded
  10. * DER blobs.
  11. *
  12. * File_ASN1 decodes and encodes DER formatted messages and places them in a semantic context.
  13. *
  14. * Uses the 1988 ASN.1 syntax.
  15. *
  16. * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
  17. * of this software and associated documentation files (the "Software"), to deal
  18. * in the Software without restriction, including without limitation the rights
  19. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  20. * copies of the Software, and to permit persons to whom the Software is
  21. * furnished to do so, subject to the following conditions:
  22. *
  23. * The above copyright notice and this permission notice shall be included in
  24. * all copies or substantial portions of the Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  31. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  32. * THE SOFTWARE.
  33. *
  34. * @category File
  35. * @package File_ASN1
  36. * @author Jim Wigginton <terrafrost@php.net>
  37. * @copyright MMXII Jim Wigginton
  38. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  39. * @version $Id$
  40. * @link http://phpseclib.sourceforge.net
  41. */
  42. /**
  43. * Include Math_BigInteger
  44. */
  45. if (!class_exists('Math_BigInteger')) {
  46. require_once(dirname(__FILE__).'/../Math/BigInteger.php');
  47. }
  48. /**#@+
  49. * Tag Classes
  50. *
  51. * @access private
  52. * @link http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#page=12
  53. */
  54. define('FILE_ASN1_CLASS_UNIVERSAL', 0);
  55. define('FILE_ASN1_CLASS_APPLICATION', 1);
  56. define('FILE_ASN1_CLASS_CONTEXT_SPECIFIC', 2);
  57. define('FILE_ASN1_CLASS_PRIVATE', 3);
  58. /**#@-*/
  59. /**#@+
  60. * Tag Classes
  61. *
  62. * @access private
  63. * @link http://www.obj-sys.com/asn1tutorial/node124.html
  64. */
  65. define('FILE_ASN1_TYPE_BOOLEAN', 1);
  66. define('FILE_ASN1_TYPE_INTEGER', 2);
  67. define('FILE_ASN1_TYPE_BIT_STRING', 3);
  68. define('FILE_ASN1_TYPE_OCTET_STRING', 4);
  69. define('FILE_ASN1_TYPE_NULL', 5);
  70. define('FILE_ASN1_TYPE_OBJECT_IDENTIFIER',6);
  71. //define('FILE_ASN1_TYPE_OBJECT_DESCRIPTOR',7);
  72. //define('FILE_ASN1_TYPE_INSTANCE_OF', 8); // EXTERNAL
  73. define('FILE_ASN1_TYPE_REAL', 9);
  74. define('FILE_ASN1_TYPE_ENUMERATED', 10);
  75. //define('FILE_ASN1_TYPE_EMBEDDED', 11);
  76. define('FILE_ASN1_TYPE_UTF8_STRING', 12);
  77. //define('FILE_ASN1_TYPE_RELATIVE_OID', 13);
  78. define('FILE_ASN1_TYPE_SEQUENCE', 16); // SEQUENCE OF
  79. define('FILE_ASN1_TYPE_SET', 17); // SET OF
  80. /**#@-*/
  81. /**#@+
  82. * More Tag Classes
  83. *
  84. * @access private
  85. * @link http://www.obj-sys.com/asn1tutorial/node10.html
  86. */
  87. define('FILE_ASN1_TYPE_NUMERIC_STRING', 18);
  88. define('FILE_ASN1_TYPE_PRINTABLE_STRING',19);
  89. define('FILE_ASN1_TYPE_TELETEX_STRING', 20); // T61String
  90. define('FILE_ASN1_TYPE_VIDEOTEX_STRING', 21);
  91. define('FILE_ASN1_TYPE_IA5_STRING', 22);
  92. define('FILE_ASN1_TYPE_UTC_TIME', 23);
  93. define('FILE_ASN1_TYPE_GENERALIZED_TIME',24);
  94. define('FILE_ASN1_TYPE_GRAPHIC_STRING', 25);
  95. define('FILE_ASN1_TYPE_VISIBLE_STRING', 26); // ISO646String
  96. define('FILE_ASN1_TYPE_GENERAL_STRING', 27);
  97. define('FILE_ASN1_TYPE_UNIVERSAL_STRING',28);
  98. //define('FILE_ASN1_TYPE_CHARACTER_STRING',29);
  99. define('FILE_ASN1_TYPE_BMP_STRING', 30);
  100. /**#@-*/
  101. /**#@+
  102. * Tag Aliases
  103. *
  104. * These tags are kinda place holders for other tags.
  105. *
  106. * @access private
  107. */
  108. define('FILE_ASN1_TYPE_CHOICE', -1);
  109. define('FILE_ASN1_TYPE_ANY', -2);
  110. /**#@-*/
  111. /**
  112. * ASN.1 Element
  113. *
  114. * Bypass normal encoding rules in File_ASN1::encodeDER()
  115. *
  116. * @author Jim Wigginton <terrafrost@php.net>
  117. * @version 0.3.0
  118. * @access public
  119. * @package File_ASN1
  120. */
  121. class File_ASN1_Element {
  122. /**
  123. * Raw element value
  124. *
  125. * @var String
  126. * @access private
  127. */
  128. var $element;
  129. /**
  130. * Constructor
  131. *
  132. * @param String $encoded
  133. * @return File_ASN1_Element
  134. * @access public
  135. */
  136. function File_ASN1_Element($encoded)
  137. {
  138. $this->element = $encoded;
  139. }
  140. }
  141. /**
  142. * Pure-PHP ASN.1 Parser
  143. *
  144. * @author Jim Wigginton <terrafrost@php.net>
  145. * @version 0.3.0
  146. * @access public
  147. * @package File_ASN1
  148. */
  149. class File_ASN1 {
  150. /**
  151. * ASN.1 object identifier
  152. *
  153. * @var Array
  154. * @access private
  155. * @link http://en.wikipedia.org/wiki/Object_identifier
  156. */
  157. var $oids = array();
  158. /**
  159. * Default date format
  160. *
  161. * @var String
  162. * @access private
  163. * @link http://php.net/class.datetime
  164. */
  165. var $format = 'D, d M y H:i:s O';
  166. /**
  167. * Default date format
  168. *
  169. * @var Array
  170. * @access private
  171. * @see File_ASN1::setTimeFormat()
  172. * @see File_ASN1::asn1map()
  173. * @link http://php.net/class.datetime
  174. */
  175. var $encoded;
  176. /**
  177. * Filters
  178. *
  179. * If the mapping type is FILE_ASN1_TYPE_ANY what do we actually encode it as?
  180. *
  181. * @var Array
  182. * @access private
  183. * @see File_ASN1::_encode_der()
  184. */
  185. var $filters;
  186. /**
  187. * Type mapping table for the ANY type.
  188. *
  189. * Structured or unknown types are mapped to a FILE_ASN1_Element.
  190. * Unambiguous types get the direct mapping (int/real/bool).
  191. * Others are mapped as a choice, with an extra indexing level.
  192. *
  193. * @var Array
  194. * @access public
  195. */
  196. var $ANYmap = array(
  197. FILE_ASN1_TYPE_BOOLEAN => true,
  198. FILE_ASN1_TYPE_INTEGER => true,
  199. FILE_ASN1_TYPE_BIT_STRING => 'bitString',
  200. FILE_ASN1_TYPE_OCTET_STRING => 'octetString',
  201. FILE_ASN1_TYPE_NULL => 'null',
  202. FILE_ASN1_TYPE_OBJECT_IDENTIFIER => 'objectIdentifier',
  203. FILE_ASN1_TYPE_REAL => true,
  204. FILE_ASN1_TYPE_ENUMERATED => 'enumerated',
  205. FILE_ASN1_TYPE_UTF8_STRING => 'utf8String',
  206. FILE_ASN1_TYPE_NUMERIC_STRING => 'numericString',
  207. FILE_ASN1_TYPE_PRINTABLE_STRING => 'printableString',
  208. FILE_ASN1_TYPE_TELETEX_STRING => 'teletexString',
  209. FILE_ASN1_TYPE_VIDEOTEX_STRING => 'videotexString',
  210. FILE_ASN1_TYPE_IA5_STRING => 'ia5String',
  211. FILE_ASN1_TYPE_UTC_TIME => 'utcTime',
  212. FILE_ASN1_TYPE_GENERALIZED_TIME => 'generalTime',
  213. FILE_ASN1_TYPE_GRAPHIC_STRING => 'graphicString',
  214. FILE_ASN1_TYPE_VISIBLE_STRING => 'visibleString',
  215. FILE_ASN1_TYPE_GENERAL_STRING => 'generalString',
  216. FILE_ASN1_TYPE_UNIVERSAL_STRING => 'universalString',
  217. //FILE_ASN1_TYPE_CHARACTER_STRING => 'characterString',
  218. FILE_ASN1_TYPE_BMP_STRING => 'bmpString'
  219. );
  220. /**
  221. * String type to character size mapping table.
  222. *
  223. * Non-convertable types are absent from this table.
  224. * size == 0 indicates variable length encoding.
  225. *
  226. * @var Array
  227. * @access public
  228. */
  229. var $stringTypeSize = array(
  230. FILE_ASN1_TYPE_UTF8_STRING => 0,
  231. FILE_ASN1_TYPE_BMP_STRING => 2,
  232. FILE_ASN1_TYPE_UNIVERSAL_STRING => 4,
  233. FILE_ASN1_TYPE_PRINTABLE_STRING => 1,
  234. FILE_ASN1_TYPE_TELETEX_STRING => 1,
  235. FILE_ASN1_TYPE_IA5_STRING => 1,
  236. FILE_ASN1_TYPE_VISIBLE_STRING => 1,
  237. );
  238. /**
  239. * Parse BER-encoding
  240. *
  241. * Serves a similar purpose to openssl's asn1parse
  242. *
  243. * @param String $encoded
  244. * @return Array
  245. * @access public
  246. */
  247. function decodeBER($encoded)
  248. {
  249. if (is_object($encoded) && strtolower(get_class($encoded)) == 'file_asn1_element') {
  250. $encoded = $encoded->element;
  251. }
  252. $this->encoded = $encoded;
  253. return $this->_decode_ber($encoded);
  254. }
  255. /**
  256. * Parse BER-encoding (Helper function)
  257. *
  258. * Sometimes we want to get the BER encoding of a particular tag. $start lets us do that without having to reencode.
  259. * $encoded is passed by reference for the recursive calls done for FILE_ASN1_TYPE_BIT_STRING and
  260. * FILE_ASN1_TYPE_OCTET_STRING. In those cases, the indefinite length is used.
  261. *
  262. * @param String $encoded
  263. * @param Integer $start
  264. * @return Array
  265. * @access private
  266. */
  267. function _decode_ber(&$encoded, $start = 0)
  268. {
  269. $decoded = array();
  270. while ( strlen($encoded) ) {
  271. $current = array('start' => $start);
  272. $type = ord($this->_string_shift($encoded));
  273. $start++;
  274. $constructed = ($type >> 5) & 1;
  275. $tag = $type & 0x1F;
  276. if ($tag == 0x1F) {
  277. $tag = 0;
  278. // process septets (since the eighth bit is ignored, it's not an octet)
  279. do {
  280. $loop = ord($encoded[0]) >> 7;
  281. $tag <<= 7;
  282. $tag |= ord($this->_string_shift($encoded)) & 0x7F;
  283. $start++;
  284. } while ( $loop );
  285. }
  286. // Length, as discussed in § 8.1.3 of X.690-0207.pdf#page=13
  287. $length = ord($this->_string_shift($encoded));
  288. $start++;
  289. if ( $length == 0x80 ) { // indefinite length
  290. // "[A sender shall] use the indefinite form (see 8.1.3.6) if the encoding is constructed and is not all
  291. // immediately available." -- § 8.1.3.2.c
  292. //if ( !$constructed ) {
  293. // return false;
  294. //}
  295. $length = strlen($encoded);
  296. } elseif ( $length & 0x80 ) { // definite length, long form
  297. // technically, the long form of the length can be represented by up to 126 octets (bytes), but we'll only
  298. // support it up to four.
  299. $length&= 0x7F;
  300. $temp = $this->_string_shift($encoded, $length);
  301. $start+= $length;
  302. extract(unpack('Nlength', substr(str_pad($temp, 4, chr(0), STR_PAD_LEFT), -4)));
  303. }
  304. // End-of-content, see §§ 8.1.1.3, 8.1.3.2, 8.1.3.6, 8.1.5, and (for an example) 8.6.4.2
  305. if (!$type && !$length) {
  306. return $decoded;
  307. }
  308. $content = $this->_string_shift($encoded, $length);
  309. /* Class is UNIVERSAL, APPLICATION, PRIVATE, or CONTEXT-SPECIFIC. The UNIVERSAL class is restricted to the ASN.1
  310. built-in types. It defines an application-independent data type that must be distinguishable from all other
  311. data types. The other three classes are user defined. The APPLICATION class distinguishes data types that
  312. have a wide, scattered use within a particular presentation context. PRIVATE distinguishes data types within
  313. a particular organization or country. CONTEXT-SPECIFIC distinguishes members of a sequence or set, the
  314. alternatives of a CHOICE, or universally tagged set members. Only the class number appears in braces for this
  315. data type; the term CONTEXT-SPECIFIC does not appear.
  316. -- http://www.obj-sys.com/asn1tutorial/node12.html */
  317. $class = ($type >> 6) & 3;
  318. switch ($class) {
  319. case FILE_ASN1_CLASS_APPLICATION:
  320. case FILE_ASN1_CLASS_PRIVATE:
  321. case FILE_ASN1_CLASS_CONTEXT_SPECIFIC:
  322. $decoded[] = array(
  323. 'type' => $class,
  324. 'constant' => $tag,
  325. 'content' => $constructed ? $this->_decode_ber($content, $start) : $content,
  326. 'length' => $length + $start - $current['start']
  327. ) + $current;
  328. continue 2;
  329. }
  330. $current+= array('type' => $tag);
  331. // decode UNIVERSAL tags
  332. switch ($tag) {
  333. case FILE_ASN1_TYPE_BOOLEAN:
  334. // "The contents octets shall consist of a single octet." -- § 8.2.1
  335. //if (strlen($content) != 1) {
  336. // return false;
  337. //}
  338. $current['content'] = (bool) ord($content[0]);
  339. break;
  340. case FILE_ASN1_TYPE_INTEGER:
  341. case FILE_ASN1_TYPE_ENUMERATED:
  342. $current['content'] = new Math_BigInteger($content, -256);
  343. break;
  344. case FILE_ASN1_TYPE_REAL: // not currently supported
  345. return false;
  346. case FILE_ASN1_TYPE_BIT_STRING:
  347. // The initial octet shall encode, as an unsigned binary integer with bit 1 as the least significant bit,
  348. // the number of unused bits in the final subsequent octet. The number shall be in the range zero to
  349. // seven.
  350. if (!$constructed) {
  351. $current['content'] = $content;
  352. } else {
  353. $temp = $this->_decode_ber($content, $start);
  354. $length-= strlen($content);
  355. $last = count($temp) - 1;
  356. for ($i = 0; $i < $last; $i++) {
  357. // all subtags should be bit strings
  358. //if ($temp[$i]['type'] != FILE_ASN1_TYPE_BIT_STRING) {
  359. // return false;
  360. //}
  361. $current['content'].= substr($temp[$i]['content'], 1);
  362. }
  363. // all subtags should be bit strings
  364. //if ($temp[$last]['type'] != FILE_ASN1_TYPE_BIT_STRING) {
  365. // return false;
  366. //}
  367. $current['content'] = $temp[$last]['content'][0] . $current['content'] . substr($temp[$i]['content'], 1);
  368. }
  369. break;
  370. case FILE_ASN1_TYPE_OCTET_STRING:
  371. if (!$constructed) {
  372. $current['content'] = $content;
  373. } else {
  374. $temp = $this->_decode_ber($content, $start);
  375. $length-= strlen($content);
  376. for ($i = 0, $size = count($temp); $i < $size; $i++) {
  377. // all subtags should be octet strings
  378. //if ($temp[$i]['type'] != FILE_ASN1_TYPE_OCTET_STRING) {
  379. // return false;
  380. //}
  381. $current['content'].= $temp[$i]['content'];
  382. }
  383. // $length =
  384. }
  385. break;
  386. case FILE_ASN1_TYPE_NULL:
  387. // "The contents octets shall not contain any octets." -- § 8.8.2
  388. //if (strlen($content)) {
  389. // return false;
  390. //}
  391. break;
  392. case FILE_ASN1_TYPE_SEQUENCE:
  393. case FILE_ASN1_TYPE_SET:
  394. $current['content'] = $this->_decode_ber($content, $start);
  395. break;
  396. case FILE_ASN1_TYPE_OBJECT_IDENTIFIER:
  397. $temp = ord($this->_string_shift($content));
  398. $current['content'] = sprintf('%d.%d', floor($temp / 40), $temp % 40);
  399. $valuen = 0;
  400. // process septets
  401. while (strlen($content)) {
  402. $temp = ord($this->_string_shift($content));
  403. $valuen <<= 7;
  404. $valuen |= $temp & 0x7F;
  405. if (~$temp & 0x80) {
  406. $current['content'].= ".$valuen";
  407. $valuen = 0;
  408. }
  409. }
  410. // the eighth bit of the last byte should not be 1
  411. //if ($temp >> 7) {
  412. // return false;
  413. //}
  414. break;
  415. /* Each character string type shall be encoded as if it had been declared:
  416. [UNIVERSAL x] IMPLICIT OCTET STRING
  417. -- X.690-0207.pdf#page=23 (§ 8.21.3)
  418. Per that, we're not going to do any validation. If there are any illegal characters in the string,
  419. we don't really care */
  420. case FILE_ASN1_TYPE_NUMERIC_STRING:
  421. // 0,1,2,3,4,5,6,7,8,9, and space
  422. case FILE_ASN1_TYPE_PRINTABLE_STRING:
  423. // Upper and lower case letters, digits, space, apostrophe, left/right parenthesis, plus sign, comma,
  424. // hyphen, full stop, solidus, colon, equal sign, question mark
  425. case FILE_ASN1_TYPE_TELETEX_STRING:
  426. // The Teletex character set in CCITT's T61, space, and delete
  427. // see http://en.wikipedia.org/wiki/Teletex#Character_sets
  428. case FILE_ASN1_TYPE_VIDEOTEX_STRING:
  429. // The Videotex character set in CCITT's T.100 and T.101, space, and delete
  430. case FILE_ASN1_TYPE_VISIBLE_STRING:
  431. // Printing character sets of international ASCII, and space
  432. case FILE_ASN1_TYPE_IA5_STRING:
  433. // International Alphabet 5 (International ASCII)
  434. case FILE_ASN1_TYPE_GRAPHIC_STRING:
  435. // All registered G sets, and space
  436. case FILE_ASN1_TYPE_GENERAL_STRING:
  437. // All registered C and G sets, space and delete
  438. case FILE_ASN1_TYPE_UTF8_STRING:
  439. // ????
  440. case FILE_ASN1_TYPE_BMP_STRING:
  441. $current['content'] = $content;
  442. break;
  443. case FILE_ASN1_TYPE_UTC_TIME:
  444. case FILE_ASN1_TYPE_GENERALIZED_TIME:
  445. $current['content'] = $this->_decodeTime($content, $tag);
  446. default:
  447. }
  448. $start+= $length;
  449. $decoded[] = $current + array('length' => $start - $current['start']);
  450. }
  451. return $decoded;
  452. }
  453. /**
  454. * ASN.1 Decode
  455. *
  456. * Provides an ASN.1 semantic mapping ($mapping) from a parsed BER-encoding to a human readable format.
  457. *
  458. * @param Array $decoded
  459. * @param Array $mapping
  460. * @return Array
  461. * @access public
  462. */
  463. function asn1map($decoded, $mapping)
  464. {
  465. if (isset($mapping['explicit'])) {
  466. $decoded = $decoded['content'][0];
  467. }
  468. switch (true) {
  469. case $mapping['type'] == FILE_ASN1_TYPE_ANY:
  470. $intype = $decoded['type'];
  471. if (isset($decoded['constant']) || !isset($this->ANYmap[$intype]) || ($this->encoded[$decoded['start']] & 0x20)) {
  472. return new File_ASN1_Element(substr($this->encoded, $decoded['start'], $decoded['length']));
  473. }
  474. $inmap = $this->ANYmap[$intype];
  475. if (is_string($inmap)) {
  476. return array($inmap => $this->asn1map($decoded, array('type' => $intype) + $mapping));
  477. }
  478. break;
  479. case $mapping['type'] == FILE_ASN1_TYPE_CHOICE:
  480. foreach ($mapping['children'] as $key => $option) {
  481. switch (true) {
  482. case isset($option['constant']) && $option['constant'] == $decoded['constant']:
  483. case !isset($option['constant']) && $option['type'] == $decoded['type']:
  484. $value = $this->asn1map($decoded, $option);
  485. }
  486. if (isset($value)) {
  487. return array($key => $value);
  488. }
  489. }
  490. return NULL;
  491. case isset($mapping['implicit']):
  492. case isset($mapping['explicit']):
  493. case $decoded['type'] == $mapping['type']:
  494. break;
  495. default:
  496. return NULL;
  497. }
  498. if (isset($mapping['implicit'])) {
  499. $decoded['type'] = $mapping['type'];
  500. }
  501. switch ($decoded['type']) {
  502. case FILE_ASN1_TYPE_SEQUENCE:
  503. $map = array();
  504. if (empty($decoded['content'])) {
  505. return $map;
  506. }
  507. // ignore the min and max
  508. if (isset($mapping['min']) && isset($mapping['max'])) {
  509. $child = $mapping['children'];
  510. foreach ($decoded['content'] as $content) {
  511. $map[] = $this->asn1map($content, $child);
  512. }
  513. return $map;
  514. }
  515. $temp = $decoded['content'][$i = 0];
  516. foreach ($mapping['children'] as $key => $child) {
  517. if (!isset($child['optional']) && $child['type'] == FILE_ASN1_TYPE_CHOICE) {
  518. $map[$key] = $this->asn1map($temp, $child);
  519. $i++;
  520. if (count($decoded['content']) == $i) {
  521. break;
  522. }
  523. $temp = $decoded['content'][$i];
  524. continue;
  525. }
  526. $childClass = $tempClass = FILE_ASN1_CLASS_UNIVERSAL;
  527. $constant = NULL;
  528. if (isset($temp['constant'])) {
  529. $tempClass = isset($temp['class']) ? $temp['class'] : FILE_ASN1_CLASS_CONTEXT_SPECIFIC;
  530. }
  531. if (isset($child['class'])) {
  532. $childClass = $child['class'];
  533. $constant = $child['cast'];
  534. }
  535. elseif (isset($child['constant'])) {
  536. $childClass = FILE_ASN1_CLASS_CONTEXT_SPECIFIC;
  537. $constant = $child['constant'];
  538. }
  539. if (isset($child['optional'])) {
  540. if (isset($constant) && isset($temp['constant'])) {
  541. if (($constant == $temp['constant']) && ($childClass == $tempClass)) {
  542. $map[$key] = $this->asn1map($temp, $child);
  543. $i++;
  544. if (count($decoded['content']) == $i) {
  545. break;
  546. }
  547. $temp = $decoded['content'][$i];
  548. }
  549. } elseif (!isset($child['constant'])) {
  550. // we could do this, as well:
  551. // $buffer = $this->asn1map($temp, $child); if (isset($buffer)) { $map[$key] = $buffer; }
  552. if ($child['type'] == $temp['type'] || $child['type'] == FILE_ASN1_TYPE_ANY) {
  553. $map[$key] = $this->asn1map($temp, $child);
  554. $i++;
  555. if (count($decoded['content']) == $i) {
  556. break;
  557. }
  558. $temp = $decoded['content'][$i];
  559. } elseif ($child['type'] == FILE_ASN1_TYPE_CHOICE) {
  560. $candidate = $this->asn1map($temp, $child);
  561. if (!empty($candidate)) {
  562. $map[$key] = $candidate;
  563. $i++;
  564. if (count($decoded['content']) == $i) {
  565. break;
  566. }
  567. $temp = $decoded['content'][$i];
  568. }
  569. }
  570. }
  571. if (!isset($map[$key]) && isset($child['default'])) {
  572. $map[$key] = $child['default'];
  573. }
  574. } else {
  575. $map[$key] = $this->asn1map($temp, $child);
  576. $i++;
  577. if (count($decoded['content']) == $i) {
  578. break;
  579. }
  580. $temp = $decoded['content'][$i];
  581. }
  582. }
  583. return $map;
  584. // the main diff between sets and sequences is the encapsulation of the foreach in another for loop
  585. case FILE_ASN1_TYPE_SET:
  586. $map = array();
  587. // ignore the min and max
  588. if (isset($mapping['min']) && isset($mapping['max'])) {
  589. $child = $mapping['children'];
  590. foreach ($decoded['content'] as $content) {
  591. $map[] = $this->asn1map($content, $child);
  592. }
  593. return $map;
  594. }
  595. for ($i = 0; $i < count($decoded['content']); $i++) {
  596. foreach ($mapping['children'] as $key => $child) {
  597. $temp = $decoded['content'][$i];
  598. if (!isset($child['optional']) && $child['type'] == FILE_ASN1_TYPE_CHOICE) {
  599. $map[$key] = $this->asn1map($temp, $child);
  600. continue;
  601. }
  602. $childClass = $tempClass = FILE_ASN1_CLASS_UNIVERSAL;
  603. $constant = NULL;
  604. if (isset($temp['constant'])) {
  605. $tempClass = isset($temp['class']) ? $temp['class'] : FILE_ASN1_CLASS_CONTEXT_SPECIFIC;
  606. }
  607. if (isset($child['class'])) {
  608. $childClass = $child['class'];
  609. $constant = $child['cast'];
  610. }
  611. elseif (isset($child['constant'])) {
  612. $childClass = FILE_ASN1_CLASS_CONTEXT_SPECIFIC;
  613. $constant = $child['constant'];
  614. }
  615. if (isset($constant) && isset($temp['constant'])) {
  616. if (($constant == $temp['constant']) && ($childClass == $tempClass)) {
  617. $map[$key] = $this->asn1map($temp['content'], $child);
  618. }
  619. } elseif (!isset($child['constant'])) {
  620. // we could do this, as well:
  621. // $buffer = $this->asn1map($temp['content'], $child); if (isset($buffer)) { $map[$key] = $buffer; }
  622. if ($child['type'] == $temp['type']) {
  623. $map[$key] = $this->asn1map($temp, $child);
  624. }
  625. }
  626. }
  627. }
  628. foreach ($mapping['children'] as $key => $child) {
  629. if (!isset($map[$key]) && isset($child['default'])) {
  630. $map[$key] = $child['default'];
  631. }
  632. }
  633. return $map;
  634. case FILE_ASN1_TYPE_OBJECT_IDENTIFIER:
  635. return isset($this->oids[$decoded['content']]) ? $this->oids[$decoded['content']] : $decoded['content'];
  636. case FILE_ASN1_TYPE_UTC_TIME:
  637. case FILE_ASN1_TYPE_GENERALIZED_TIME:
  638. if (isset($mapping['implicit'])) {
  639. $decoded['content'] = $this->_decodeTime($decoded['content'], $decoded['type']);
  640. }
  641. return @date($this->format, $decoded['content']);
  642. case FILE_ASN1_TYPE_BIT_STRING:
  643. if (isset($mapping['mapping'])) {
  644. $offset = ord($decoded['content'][0]);
  645. $size = (strlen($decoded['content']) - 1) * 8 - $offset;
  646. /*
  647. From X.680-0207.pdf#page=46 (21.7):
  648. "When a "NamedBitList" is used in defining a bitstring type ASN.1 encoding rules are free to add (or remove)
  649. arbitrarily any trailing 0 bits to (or from) values that are being encoded or decoded. Application designers should
  650. therefore ensure that different semantics are not associated with such values which differ only in the number of trailing
  651. 0 bits."
  652. */
  653. $bits = count($mapping['mapping']) == $size ? array() : array_fill(0, count($mapping['mapping']) - $size, false);
  654. for ($i = strlen($decoded['content']) - 1; $i > 0; $i--) {
  655. $current = ord($decoded['content'][$i]);
  656. for ($j = $offset; $j < 8; $j++) {
  657. $bits[] = (bool) ($current & (1 << $j));
  658. }
  659. $offset = 0;
  660. }
  661. $values = array();
  662. $map = array_reverse($mapping['mapping']);
  663. foreach ($map as $i => $value) {
  664. if ($bits[$i]) {
  665. $values[] = $value;
  666. }
  667. }
  668. return $values;
  669. }
  670. case FILE_ASN1_TYPE_OCTET_STRING:
  671. return base64_encode($decoded['content']);
  672. case FILE_ASN1_TYPE_NULL:
  673. return '';
  674. case FILE_ASN1_TYPE_BOOLEAN:
  675. return $decoded['content'];
  676. case FILE_ASN1_TYPE_NUMERIC_STRING:
  677. case FILE_ASN1_TYPE_PRINTABLE_STRING:
  678. case FILE_ASN1_TYPE_TELETEX_STRING:
  679. case FILE_ASN1_TYPE_VIDEOTEX_STRING:
  680. case FILE_ASN1_TYPE_IA5_STRING:
  681. case FILE_ASN1_TYPE_GRAPHIC_STRING:
  682. case FILE_ASN1_TYPE_VISIBLE_STRING:
  683. case FILE_ASN1_TYPE_GENERAL_STRING:
  684. case FILE_ASN1_TYPE_UNIVERSAL_STRING:
  685. case FILE_ASN1_TYPE_UTF8_STRING:
  686. case FILE_ASN1_TYPE_BMP_STRING:
  687. return $decoded['content'];
  688. case FILE_ASN1_TYPE_INTEGER:
  689. case FILE_ASN1_TYPE_ENUMERATED:
  690. $temp = $decoded['content'];
  691. if (isset($mapping['implicit'])) {
  692. $temp = new Math_BigInteger($decoded['content'], -256);
  693. }
  694. if (isset($mapping['mapping'])) {
  695. $temp = (int) $temp->toString();
  696. return isset($mapping['mapping'][$temp]) ?
  697. $mapping['mapping'][$temp] :
  698. false;
  699. }
  700. return $temp;
  701. }
  702. }
  703. /**
  704. * ASN.1 Encode
  705. *
  706. * DER-encodes an ASN.1 semantic mapping ($mapping). Some libraries would probably call this function
  707. * an ASN.1 compiler.
  708. *
  709. * @param String $source
  710. * @param String $mapping
  711. * @param Integer $idx
  712. * @return String
  713. * @access public
  714. */
  715. function encodeDER($source, $mapping)
  716. {
  717. $this->location = array();
  718. return $this->_encode_der($source, $mapping);
  719. }
  720. /**
  721. * ASN.1 Encode (Helper function)
  722. *
  723. * @param String $source
  724. * @param String $mapping
  725. * @param Integer $idx
  726. * @return String
  727. * @access private
  728. */
  729. function _encode_der($source, $mapping, $idx = NULL)
  730. {
  731. if (is_object($source) && strtolower(get_class($source)) == 'file_asn1_element') {
  732. return $source->element;
  733. }
  734. // do not encode (implicitly optional) fields with value set to default
  735. if (isset($mapping['default']) && $source === $mapping['default']) {
  736. return '';
  737. }
  738. if (isset($idx)) {
  739. $this->location[] = $idx;
  740. }
  741. $tag = $mapping['type'];
  742. switch ($tag) {
  743. case FILE_ASN1_TYPE_SET: // Children order is not important, thus process in sequence.
  744. case FILE_ASN1_TYPE_SEQUENCE:
  745. $tag|= 0x20; // set the constructed bit
  746. $value = '';
  747. // ignore the min and max
  748. if (isset($mapping['min']) && isset($mapping['max'])) {
  749. $child = $mapping['children'];
  750. foreach ($source as $content) {
  751. $temp = $this->_encode_der($content, $child);
  752. if ($temp === false) {
  753. return false;
  754. }
  755. $value.= $temp;
  756. }
  757. break;
  758. }
  759. foreach ($mapping['children'] as $key => $child) {
  760. if (!isset($source[$key])) {
  761. if (!isset($child['optional'])) {
  762. return false;
  763. }
  764. continue;
  765. }
  766. $temp = $this->_encode_der($source[$key], $child, $key);
  767. if ($temp === false) {
  768. return false;
  769. }
  770. // An empty child encoding means it has been optimized out.
  771. // Else we should have at least one tag byte.
  772. if ($temp === '') {
  773. continue;
  774. }
  775. // if isset($child['constant']) is true then isset($child['optional']) should be true as well
  776. if (isset($child['constant'])) {
  777. /*
  778. From X.680-0207.pdf#page=58 (30.6):
  779. "The tagging construction specifies explicit tagging if any of the following holds:
  780. ...
  781. c) the "Tag Type" alternative is used and the value of "TagDefault" for the module is IMPLICIT TAGS or
  782. AUTOMATIC TAGS, but the type defined by "Type" is an untagged choice type, an untagged open type, or
  783. an untagged "DummyReference" (see ITU-T Rec. X.683 | ISO/IEC 8824-4, 8.3)."
  784. */
  785. if (isset($child['explicit']) || $child['type'] == FILE_ASN1_TYPE_CHOICE) {
  786. $subtag = chr((FILE_ASN1_CLASS_CONTEXT_SPECIFIC << 6) | 0x20 | $child['constant']);
  787. $temp = $subtag . $this->_encodeLength(strlen($temp)) . $temp;
  788. } else {
  789. $subtag = chr((FILE_ASN1_CLASS_CONTEXT_SPECIFIC << 6) | (ord($temp[0]) & 0x20) | $child['constant']);
  790. $temp = $subtag . substr($temp, 1);
  791. }
  792. }
  793. $value.= $temp;
  794. }
  795. break;
  796. case FILE_ASN1_TYPE_CHOICE:
  797. $temp = false;
  798. foreach ($mapping['children'] as $key => $child) {
  799. if (!isset($source[$key])) {
  800. continue;
  801. }
  802. $temp = $this->_encode_der($source[$key], $child, $key);
  803. if ($temp === false) {
  804. return false;
  805. }
  806. // An empty child encoding means it has been optimized out.
  807. // Else we should have at least one tag byte.
  808. if ($temp === '') {
  809. continue;
  810. }
  811. $tag = ord($temp[0]);
  812. // if isset($child['constant']) is true then isset($child['optional']) should be true as well
  813. if (isset($child['constant'])) {
  814. if (isset($child['explicit']) || $child['type'] == FILE_ASN1_TYPE_CHOICE) {
  815. $subtag = chr((FILE_ASN1_CLASS_CONTEXT_SPECIFIC << 6) | 0x20 | $child['constant']);
  816. $temp = $subtag . $this->_encodeLength(strlen($temp)) . $temp;
  817. } else {
  818. $subtag = chr((FILE_ASN1_CLASS_CONTEXT_SPECIFIC << 6) | (ord($temp[0]) & 0x20) | $child['constant']);
  819. $temp = $subtag . substr($temp, 1);
  820. }
  821. }
  822. }
  823. if (isset($idx)) {
  824. array_pop($this->location);
  825. }
  826. if ($temp && isset($mapping['cast'])) {
  827. $temp[0] = chr(($mapping['class'] << 6) | ($tag & 0x20) | $mapping['cast']);
  828. }
  829. return $temp;
  830. case FILE_ASN1_TYPE_INTEGER:
  831. case FILE_ASN1_TYPE_ENUMERATED:
  832. if (!isset($mapping['mapping'])) {
  833. $value = $source->toBytes(true);
  834. } else {
  835. $value = array_search($source, $mapping['mapping']);
  836. if ($value === false) {
  837. return false;
  838. }
  839. $value = new Math_BigInteger($value);
  840. $value = $value->toBytes(true);
  841. }
  842. break;
  843. case FILE_ASN1_TYPE_UTC_TIME:
  844. case FILE_ASN1_TYPE_GENERALIZED_TIME:
  845. $format = $mapping['type'] == FILE_ASN1_TYPE_UTC_TIME ? 'y' : 'Y';
  846. $format.= 'mdHis';
  847. $value = @gmdate($format, strtotime($source)) . 'Z';
  848. break;
  849. case FILE_ASN1_TYPE_BIT_STRING:
  850. if (isset($mapping['mapping'])) {
  851. $bits = array_fill(0, count($mapping['mapping']), 0);
  852. $size = 0;
  853. for ($i = 0; $i < count($mapping['mapping']); $i++) {
  854. if (in_array($mapping['mapping'][$i], $source)) {
  855. $bits[$i] = 1;
  856. $size = $i;
  857. }
  858. }
  859. $offset = 8 - (($size + 1) & 7);
  860. $offset = $offset !== 8 ? $offset : 0;
  861. $value = chr($offset);
  862. for ($i = $size + 1; $i < count($mapping['mapping']); $i++) {
  863. unset($bits[$i]);
  864. }
  865. $bits = implode('', array_pad($bits, $size + $offset + 1, 0));
  866. $bytes = explode(' ', rtrim(chunk_split($bits, 8, ' ')));
  867. foreach ($bytes as $byte) {
  868. $value.= chr(bindec($byte));
  869. }
  870. break;
  871. }
  872. case FILE_ASN1_TYPE_OCTET_STRING:
  873. /* The initial octet shall encode, as an unsigned binary integer with bit 1 as the least significant bit,
  874. the number of unused bits in the final subsequent octet. The number shall be in the range zero to seven.
  875. -- http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#page=16 */
  876. $value = base64_decode($source);
  877. break;
  878. case FILE_ASN1_TYPE_OBJECT_IDENTIFIER:
  879. $oid = preg_match('#(?:\d+\.)+#', $source) ? $source : array_search($source, $this->oids);
  880. if ($oid === false) {
  881. user_error('Invalid OID', E_USER_NOTICE);
  882. return false;
  883. }
  884. $value = '';
  885. $parts = explode('.', $oid);
  886. $value = chr(40 * $parts[0] + $parts[1]);
  887. for ($i = 2; $i < count($parts); $i++) {
  888. $temp = '';
  889. if (!$parts[$i]) {
  890. $temp = "\0";
  891. } else {
  892. while ($parts[$i]) {
  893. $temp = chr(0x80 | ($parts[$i] & 0x7F)) . $temp;
  894. $parts[$i] >>= 7;
  895. }
  896. $temp[strlen($temp) - 1] = $temp[strlen($temp) - 1] & chr(0x7F);
  897. }
  898. $value.= $temp;
  899. }
  900. break;
  901. case FILE_ASN1_TYPE_ANY:
  902. $loc = $this->location;
  903. if (isset($idx)) {
  904. array_pop($this->location);
  905. }
  906. switch (true) {
  907. case !isset($source):
  908. return $this->_encode_der(NULL, array('type' => FILE_ASN1_TYPE_NULL) + $mapping);
  909. case is_int($source):
  910. case is_object($source) && strtolower(get_class($source)) == 'math_biginteger':
  911. return $this->_encode_der($source, array('type' => FILE_ASN1_TYPE_INTEGER) + $mapping);
  912. case is_float($source):
  913. return $this->_encode_der($source, array('type' => FILE_ASN1_TYPE_REAL) + $mapping);
  914. case is_bool($source):
  915. return $this->_encode_der($source, array('type' => FILE_ASN1_TYPE_BOOLEAN) + $mapping);
  916. case is_array($source) && count($source) == 1:
  917. $typename = implode('', array_keys($source));
  918. $outtype = array_search($typename, $this->ANYmap, true);
  919. if ($outtype !== false) {
  920. return $this->_encode_der($source[$typename], array('type' => $outtype) + $mapping);
  921. }
  922. }
  923. $filters = $this->filters;
  924. foreach ($loc as $part) {
  925. if (!isset($filters[$part])) {
  926. $filters = false;
  927. break;
  928. }
  929. $filters = $filters[$part];
  930. }
  931. if ($filters === false) {
  932. user_error('No filters defined for ' . implode('/', $loc), E_USER_NOTICE);
  933. return false;
  934. }
  935. return $this->_encode_der($source, $filters + $mapping);
  936. case FILE_ASN1_TYPE_NULL:
  937. $value = '';
  938. break;
  939. case FILE_ASN1_TYPE_NUMERIC_STRING:
  940. case FILE_ASN1_TYPE_TELETEX_STRING:
  941. case FILE_ASN1_TYPE_PRINTABLE_STRING:
  942. case FILE_ASN1_TYPE_UNIVERSAL_STRING:
  943. case FILE_ASN1_TYPE_UTF8_STRING:
  944. case FILE_ASN1_TYPE_BMP_STRING:
  945. case FILE_ASN1_TYPE_IA5_STRING:
  946. case FILE_ASN1_TYPE_VISIBLE_STRING:
  947. case FILE_ASN1_TYPE_VIDEOTEX_STRING:
  948. case FILE_ASN1_TYPE_GRAPHIC_STRING:
  949. case FILE_ASN1_TYPE_GENERAL_STRING:
  950. $value = $source;
  951. break;
  952. case FILE_ASN1_TYPE_BOOLEAN:
  953. $value = $source ? "\xFF" : "\x00";
  954. break;
  955. default:
  956. user_error('Mapping provides no type definition for ' . implode('/', $this->location), E_USER_NOTICE);
  957. return false;
  958. }
  959. if (isset($idx)) {
  960. array_pop($this->location);
  961. }
  962. if (isset($mapping['cast'])) {
  963. $tag = ($mapping['class'] << 6) | ($tag & 0x20) | $mapping['cast'];
  964. }
  965. return chr($tag) . $this->_encodeLength(strlen($value)) . $value;
  966. }
  967. /**
  968. * DER-encode the length
  969. *
  970. * DER supports lengths up to (2**8)**127, however, we'll only support lengths up to (2**8)**4. See
  971. * {@link http://itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#p=13 X.690 § 8.1.3} for more information.
  972. *
  973. * @access private
  974. * @param Integer $length
  975. * @return String
  976. */
  977. function _encodeLength($length)
  978. {
  979. if ($length <= 0x7F) {
  980. return chr($length);
  981. }
  982. $temp = ltrim(pack('N', $length), chr(0));
  983. return pack('Ca*', 0x80 | strlen($temp), $temp);
  984. }
  985. /**
  986. * BER-decode the time
  987. *
  988. * Called by _decode_ber() and in the case of implicit tags asn1map().
  989. *
  990. * @access private
  991. * @param String $content
  992. * @param Integer $tag
  993. * @return String
  994. */
  995. function _decodeTime($content, $tag)
  996. {
  997. /* UTCTime:
  998. http://tools.ietf.org/html/rfc5280#section-4.1.2.5.1
  999. http://www.obj-sys.com/asn1tutorial/node15.html
  1000. GeneralizedTime:
  1001. http://tools.ietf.org/html/rfc5280#section-4.1.2.5.2
  1002. http://www.obj-sys.com/asn1tutorial/node14.html */
  1003. $pattern = $tag == FILE_ASN1_TYPE_UTC_TIME ?
  1004. '#(..)(..)(..)(..)(..)(..)(.*)#' :
  1005. '#(....)(..)(..)(..)(..)(..).*([Z+-].*)$#';
  1006. preg_match($pattern, $content, $matches);
  1007. list(, $year, $month, $day, $hour, $minute, $second, $timezone) = $matches;
  1008. if ($tag == FILE_ASN1_TYPE_UTC_TIME) {
  1009. $year = $year >= 50 ? "19$year" : "20$year";
  1010. }
  1011. if ($timezone == 'Z') {
  1012. $mktime = 'gmmktime';
  1013. $timezone = 0;
  1014. } elseif (preg_match('#([+-])(\d\d)(\d\d)#', $timezone, $matches)) {
  1015. $mktime = 'gmmktime';
  1016. $timezone = 60 * $matches[3] + 3600 * $matches[2];
  1017. if ($matches[1] == '-') {
  1018. $timezone = -$timezone;
  1019. }
  1020. } else {
  1021. $mktime = 'mktime';
  1022. $timezone = 0;
  1023. }
  1024. return @$mktime($hour, $minute, $second, $month, $day, $year) + $timezone;
  1025. }
  1026. /**
  1027. * Set the time format
  1028. *
  1029. * Sets the time / date format for asn1map().
  1030. *
  1031. * @access public
  1032. * @param String $format
  1033. */
  1034. function setTimeFormat($format)
  1035. {
  1036. $this->format = $format;
  1037. }
  1038. /**
  1039. * Load OIDs
  1040. *
  1041. * Load the relevant OIDs for a particular ASN.1 semantic mapping.
  1042. *
  1043. * @access public
  1044. * @param Array $oids
  1045. */
  1046. function loadOIDs($oids)
  1047. {
  1048. $this->oids = $oids;
  1049. }
  1050. /**
  1051. * Load filters
  1052. *
  1053. * See File_X509, etc, for an example.
  1054. *
  1055. * @access public
  1056. * @param Array $filters
  1057. */
  1058. function loadFilters($filters)
  1059. {
  1060. $this->filters = $filters;
  1061. }
  1062. /**
  1063. * String Shift
  1064. *
  1065. * Inspired by array_shift
  1066. *
  1067. * @param String $string
  1068. * @param optional Integer $index
  1069. * @return String
  1070. * @access private
  1071. */
  1072. function _string_shift(&$string, $index = 1)
  1073. {
  1074. $substr = substr($string, 0, $index);
  1075. $string = substr($string, $index);
  1076. return $substr;
  1077. }
  1078. /**
  1079. * String type conversion
  1080. *
  1081. * This is a lazy conversion, dealing only with character size.
  1082. * No real conversion table is used.
  1083. *
  1084. * @param String $in
  1085. * @param optional Integer $from
  1086. * @param optional Integer $to
  1087. * @return String
  1088. * @access public
  1089. */
  1090. function convert($in, $from = FILE_ASN1_TYPE_UTF8_STRING, $to = FILE_ASN1_TYPE_UTF8_STRING)
  1091. {
  1092. if (!isset($this->stringTypeSize[$from]) || !isset($this->stringTypeSize[$to])) {
  1093. return false;
  1094. }
  1095. $insize = $this->stringTypeSize[$from];
  1096. $outsize = $this->stringTypeSize[$to];
  1097. $inlength = strlen($in);
  1098. $out = '';
  1099. for ($i = 0; $i < $inlength;) {
  1100. if ($inlength - $i < $insize) {
  1101. return false;
  1102. }
  1103. // Get an input character as a 32-bit value.
  1104. $c = ord($in[$i++]);
  1105. switch (true) {
  1106. case $insize == 4:
  1107. $c = ($c << 8) | ord($in[$i++]);
  1108. $c = ($c << 8) | ord($in[$i++]);
  1109. case $insize == 2:
  1110. $c = ($c << 8) | ord($in[$i++]);
  1111. case $insize == 1:
  1112. break;
  1113. case ($c & 0x80) == 0x00:
  1114. break;
  1115. case ($c & 0x40) == 0x00:
  1116. return false;
  1117. default:
  1118. $bit = 6;
  1119. do {
  1120. if ($bit > 25 || $i >= $inlength || (ord($in[$i]) & 0xC0) != 0x80) {
  1121. return false;
  1122. }
  1123. $c = ($c << 6) | (ord($in[$i++]) & 0x3F);
  1124. $bit += 5;
  1125. $mask = 1 << $bit;
  1126. } while ($c & $bit);
  1127. $c &= $mask - 1;
  1128. break;
  1129. }
  1130. // Convert and append the character to output string.
  1131. $v = '';
  1132. switch (true) {
  1133. case $outsize == 4:
  1134. $v .= chr($c & 0xFF);
  1135. $c >>= 8;
  1136. $v .= chr($c & 0xFF);
  1137. $c >>= 8;
  1138. case $outsize == 2:
  1139. $v .= chr($c & 0xFF);
  1140. $c >>= 8;
  1141. case $outsize == 1:
  1142. $v .= chr($c & 0xFF);
  1143. $c >>= 8;
  1144. if ($c) {
  1145. return false;
  1146. }
  1147. break;
  1148. case ($c & 0x80000000) != 0:
  1149. return false;
  1150. case $c >= 0x04000000:
  1151. $v .= chr(0x80 | ($c & 0x3F));
  1152. $c = ($c >> 6) | 0x04000000;
  1153. case $c >= 0x00200000:
  1154. $v .= chr(0x80 | ($c & 0x3F));
  1155. $c = ($c >> 6) | 0x00200000;
  1156. case $c >= 0x00010000:
  1157. $v .= chr(0x80 | ($c & 0x3F));

Large files files are truncated, but you can click here to view the full file