BaseConverter.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * A Utility class for converting between raw binary strings and a given
  4. * list of characters
  5. *
  6. * PHP version 5.3
  7. *
  8. * @category PHPSecurityLib
  9. * @package Core
  10. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  11. * @copyright 2011 The Authors
  12. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  13. * @version Build @@version@@
  14. */
  15. namespace SecurityLib;
  16. /**
  17. * A Utility class for converting between raw binary strings and a given
  18. * list of characters
  19. *
  20. * @category PHPSecurityLib
  21. * @package Core
  22. * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
  23. */
  24. class BaseConverter {
  25. /**
  26. * Convert from a raw binary string to a string of characters
  27. *
  28. * @param string $string The string to convert from
  29. * @param string $characters The list of characters to convert to
  30. *
  31. * @return string The converted string
  32. */
  33. public static function convertFromBinary($string, $characters) {
  34. if ($string === '' || empty($characters)) {
  35. return '';
  36. }
  37. $string = str_split($string);
  38. $callback = function($str) {
  39. return ord($str);
  40. };
  41. $string = array_map($callback, $string);
  42. $converted = static::baseConvert($string, 256, strlen($characters));
  43. $callback = function ($num) use ($characters) {
  44. return $characters[$num];
  45. };
  46. $ret = implode('', array_map($callback, $converted));
  47. return $ret;
  48. }
  49. /**
  50. * Convert to a raw binary string from a string of characters
  51. *
  52. * @param string $string The string to convert from
  53. * @param string $characters The list of characters to convert to
  54. *
  55. * @return string The converted string
  56. */
  57. public static function convertToBinary($string, $characters) {
  58. if (empty($string) || empty($characters)) {
  59. return '';
  60. }
  61. $string = str_split($string);
  62. $callback = function($str) use ($characters) {
  63. return strpos($characters, $str);
  64. };
  65. $string = array_map($callback, $string);
  66. $converted = static::baseConvert($string, strlen($characters), 256);
  67. $callback = function ($num) {
  68. return chr($num);
  69. };
  70. return implode('', array_map($callback, $converted));
  71. }
  72. /**
  73. * Convert an array of input blocks to another numeric base
  74. *
  75. * This function was modified from an implementation found on StackOverflow.
  76. * Special Thanks to @KeithRandall for supplying the implementation.
  77. *
  78. * @param int[] $source The source number, as an array
  79. * @param int $srcBase The source base as an integer
  80. * @param int $dstBase The destination base as an integer
  81. *
  82. * @see http://codegolf.stackexchange.com/questions/1620/arb/1626#1626
  83. * @return int[] An array of integers in the encoded base
  84. */
  85. public static function baseConvert(array $source, $srcBase, $dstBase) {
  86. if ($dstBase < 2) {
  87. $message = sprintf('Invalid Destination Base: %d', $dstBase);
  88. throw new \InvalidArgumentException($message);
  89. }
  90. $result = array();
  91. $count = count($source);
  92. while ($count) {
  93. $itMax = $count;
  94. $remainder = $count = $loop = 0;
  95. while($loop < $itMax) {
  96. $dividend = $source[$loop++] + $remainder * $srcBase;
  97. $remainder = $dividend % $dstBase;
  98. $res = ($dividend - $remainder) / $dstBase;
  99. if ($count || $res) {
  100. $source[$count++] = $res;
  101. }
  102. }
  103. $result[] = $remainder;
  104. }
  105. return array_reverse($result);
  106. }
  107. }