Math.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. *
  4. * @filesource Math.php
  5. * @created 25.11.2015
  6. * @package codemasher\QRCode
  7. * @author Smiley <smiley@chillerlan.net>
  8. * @copyright 2015 Smiley
  9. * @license MIT
  10. */
  11. namespace codemasher\QRCode;
  12. use codemasher\QRCode\QRCodeException;
  13. /**
  14. * Class Math
  15. */
  16. class Math{
  17. /**
  18. * @var array
  19. */
  20. protected $QR_MATH_EXP_TABLE;
  21. /**
  22. * @var array
  23. */
  24. protected $QR_MATH_LOG_TABLE;
  25. /**
  26. * Math constructor.
  27. */
  28. public function __construct(){
  29. $this->QR_MATH_EXP_TABLE = $this->createNumArray(256);
  30. for($i = 0; $i < 8; $i++){
  31. $this->QR_MATH_EXP_TABLE[$i] = 1 << $i;
  32. }
  33. for($i = 8; $i < 256; $i++){
  34. $this->QR_MATH_EXP_TABLE[$i] =
  35. $this->QR_MATH_EXP_TABLE[$i - 4]
  36. ^$this->QR_MATH_EXP_TABLE[$i - 5]
  37. ^$this->QR_MATH_EXP_TABLE[$i - 6]
  38. ^$this->QR_MATH_EXP_TABLE[$i - 8];
  39. }
  40. $this->QR_MATH_LOG_TABLE = $this->createNumArray(256);
  41. for($i = 0; $i < 255; $i++){
  42. $this->QR_MATH_LOG_TABLE[$this->QR_MATH_EXP_TABLE[$i]] = $i;
  43. }
  44. }
  45. /**
  46. * @param int $length
  47. *
  48. * @return array
  49. */
  50. public function createNumArray($length){
  51. $num_array = [];
  52. for($i = 0; $i < $length; $i++){
  53. $num_array[] = 0;
  54. }
  55. return $num_array;
  56. }
  57. /**
  58. * @param int $n
  59. *
  60. * @return mixed
  61. * @throws \codemasher\QRCode\QRCodeException
  62. */
  63. public function glog($n){
  64. if($n < 1){
  65. throw new QRCodeException('log('.$n.')');
  66. }
  67. return $this->QR_MATH_LOG_TABLE[$n];
  68. }
  69. /**
  70. * @param int $n
  71. *
  72. * @return mixed
  73. */
  74. public function gexp($n){
  75. while($n < 0){
  76. $n += 255;
  77. }
  78. while($n >= 256){
  79. $n -= 255;
  80. }
  81. return $this->QR_MATH_EXP_TABLE[$n];
  82. }
  83. }