PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/AmaniYunge/greenWeb
PHP | 1319 lines | 809 code | 98 blank | 412 comment | 178 complexity | f4848fb359067f2ad1a6a33cc9f41939 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause

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

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