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

/vendor/phpseclib/phpseclib/phpseclib/File/ASN1.php

https://bitbucket.org/devthiagolino/gepro-sistema
PHP | 1337 lines | 826 code | 99 blank | 412 comment | 188 complexity | fc7a0c7640f2554e47aac2c768def9b5 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, CC-BY-3.0

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

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

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