Strings.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * Common String Functions
  4. *
  5. * PHP version 5
  6. *
  7. * @category Common
  8. * @package Functions\Strings
  9. * @author Jim Wigginton <terrafrost@php.net>
  10. * @copyright 2016 Jim Wigginton
  11. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  12. * @link http://phpseclib.sourceforge.net
  13. */
  14. // namespace phpseclib\Common\Functions;
  15. // use phpseclib\Math\BigInteger;
  16. /**
  17. * Common String Functions
  18. *
  19. * @package Functions\Strings
  20. * @author Jim Wigginton <terrafrost@php.net>
  21. */
  22. abstract class Strings
  23. {
  24. /**
  25. * String Shift
  26. *
  27. * Inspired by array_shift
  28. *
  29. * @param string $string
  30. * @param int $index
  31. * @access public
  32. * @return string
  33. */
  34. public static function shift(&$string, $index = 1)
  35. {
  36. $substr = substr($string, 0, $index);
  37. $string = substr($string, $index);
  38. return $substr;
  39. }
  40. /**
  41. * String Pop
  42. *
  43. * Inspired by array_pop
  44. *
  45. * @param string $string
  46. * @param int $index
  47. * @access public
  48. * @return string
  49. */
  50. public static function pop(&$string, $index = 1)
  51. {
  52. $substr = substr($string, -$index);
  53. $string = substr($string, 0, -$index);
  54. return $substr;
  55. }
  56. /**
  57. * Performs blinded equality testing on strings
  58. *
  59. * Protects against a particular type of timing attack described.
  60. *
  61. * See {@link http://codahale.com/a-lesson-in-timing-attacks/ A Lesson In Timing Attacks (or, Don't use MessageDigest.isEquals)}
  62. *
  63. * Thanks for the heads up singpolyma!
  64. *
  65. * @access public
  66. * @param string $x
  67. * @param string $y
  68. * @return bool
  69. */
  70. public static function equals($x, $y)
  71. {
  72. if (strlen($x) != strlen($y)) {
  73. return false;
  74. }
  75. $result = 0;
  76. for ($i = 0; $i < strlen($x); $i++) {
  77. $result |= ord($x[$i]) ^ ord($y[$i]);
  78. }
  79. return $result == 0;
  80. }
  81. /**
  82. * Parse SSH2-style string
  83. *
  84. * Returns either an array or a boolean if $data is malformed.
  85. *
  86. * Valid characters for $format are as follows:
  87. *
  88. * C = byte
  89. * b = boolean (true/false)
  90. * N = uint32
  91. * s = string
  92. * i = mpint
  93. * l = name-list
  94. *
  95. * uint64 is not supported.
  96. *
  97. * @param string $string
  98. * @param int $index
  99. * @access public
  100. * @return mixed
  101. */
  102. public static function unpackSSH2($format, $data)
  103. {
  104. $result = array();
  105. for ($i = 0; $i < strlen($format); $i++) {
  106. switch ($format[$i]) {
  107. case 'C':
  108. case 'b':
  109. if (!strlen($data)) {
  110. return false;
  111. }
  112. break;
  113. case 'N':
  114. case 'i':
  115. case 's':
  116. case 'l':
  117. if (strlen($data) < 4) {
  118. return false;
  119. }
  120. break;
  121. default:
  122. throw new \InvalidArgumentException('$format contains an invalid character');
  123. }
  124. switch ($format[$i]) {
  125. case 'C':
  126. $result[] = ord(self::shift($data));
  127. continue 2;
  128. case 'b':
  129. $result[] = ord(self::shift($data)) != 0;
  130. continue 2;
  131. case 'N':
  132. list(, $temp) = unpack('N', self::shift($data, 4));
  133. $result[] = $temp;
  134. continue 2;
  135. }
  136. list(, $length) = unpack('N', self::shift($data, 4));
  137. if (strlen($data) < $length) {
  138. return false;
  139. }
  140. $temp = self::shift($data, $length);
  141. switch ($format[$i]) {
  142. case 'i':
  143. $result[] = new BigInteger($temp, -256);
  144. break;
  145. case 's':
  146. $result[] = $temp;
  147. break;
  148. case 'l':
  149. $result[] = explode(',', $temp);
  150. }
  151. }
  152. return $result;
  153. }
  154. /**
  155. * Create SSH2-style string
  156. *
  157. * @param mixed $input..
  158. * @access public
  159. * @return mixed
  160. */
  161. public static function packSSH2()
  162. {
  163. $elements = func_get_args();
  164. $format = $elements[0];
  165. array_shift($elements);
  166. if (strlen($format) != count($elements)) {
  167. throw new \InvalidArgumentException('There must be as many arguments as there are characters in the $format string');
  168. }
  169. $result = '';
  170. for ($i = 0; $i < strlen($format); $i++) {
  171. $element = $elements[$i];
  172. switch ($format[$i]) {
  173. case 'C':
  174. if (!is_int($element)) {
  175. throw new \InvalidArgumentException('Bytes must be represented as an integer between 0 and 255, inclusive.');
  176. }
  177. $result.= pack('C', $element);
  178. break;
  179. case 'b':
  180. if (!is_bool($element)) {
  181. throw new \InvalidArgumentException('A boolean parameter was expected.');
  182. }
  183. $result.= $element ? "\1" : "\0";
  184. break;
  185. case 'N':
  186. if (!is_int($element)) {
  187. throw new \InvalidArgumentException('An integer was expected.');
  188. }
  189. $result.= pack('N', $element);
  190. break;
  191. case 's':
  192. if (!is_string($element)) {
  193. throw new \InvalidArgumentException('A string was expected.');
  194. }
  195. $result.= pack('Na*', strlen($element), $element);
  196. break;
  197. case 'i':
  198. if (!$element instanceof BigInteger) {
  199. throw new \InvalidArgumentException('A phpseclib\Math\BigInteger object was expected.');
  200. }
  201. $element = $element->toBytes(true);
  202. $result.= pack('Na*', strlen($element), $element);
  203. break;
  204. case 'l':
  205. if (!is_array($element)) {
  206. throw new \InvalidArgumentException('An array was expected.');
  207. }
  208. $element = implode(',', $element);
  209. $result.= pack('Na*', strlen($element), $element);
  210. break;
  211. default:
  212. throw new \InvalidArgumentException('$format contains an invalid character');
  213. }
  214. }
  215. return $result;
  216. }
  217. }