PageRenderTime 53ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/phpseclib/File/ASN1.php

https://github.com/kea/phpseclib
PHP | 1266 lines | 785 code | 94 blank | 387 comment | 182 complexity | 6615e4f403fecfa40c66d65d05fae7a1 MD5 | raw file

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

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