QRPolynominal.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * This file is part of the phpQr package
  4. *
  5. * See @see QRCode class for description of package and license.
  6. */
  7. /**
  8. * Import necessary dependencies
  9. */
  10. require_once 'QRUtil.php';
  11. require_once 'QRMath.php';
  12. require_once 'QRCodeException.php';
  13. /**
  14. * Derived exception
  15. *
  16. * @author Maik Greubel <[email protected]>
  17. * @package phpQr
  18. */
  19. class QRPolynominalException extends QRCodeException{};
  20. /**
  21. * The purpose of this class is to provide a polynominal implementation for the QRCode package
  22. *
  23. * @author Maik Greubel <[email protected]>
  24. * @package phpQr
  25. */
  26. class QRPolynominal
  27. {
  28. /**
  29. * Bitmap
  30. *
  31. * @var array
  32. */
  33. private $num;
  34. /**
  35. * Create a new QRPolynominal instance
  36. *
  37. * @param array $num
  38. * @param int $shift
  39. *
  40. * @throws QRPolynominalException
  41. */
  42. public function __construct($num, $shift)
  43. {
  44. if(sizeof($num) == 0)
  45. {
  46. throw new QRPolynominalException("Invalid num size");
  47. }
  48. $offset = 0;
  49. while($offset < sizeof($num) && $num[$offset] == 0)
  50. {
  51. $offset++;
  52. }
  53. $this->num = QRUtil::getInstance()->createEmptyArray(sizeof($num) - $offset + $shift);
  54. for($i = 0; $i < sizeof($num) - $offset; $i++)
  55. {
  56. $this->num[$i] = $num[$i + $offset];
  57. }
  58. }
  59. /**
  60. * Get a particular bitmap index
  61. *
  62. * @param int $index
  63. * @return multitype:
  64. */
  65. public function get($index)
  66. {
  67. return $this->num[$index];
  68. }
  69. /**
  70. * Get the length of bitmap
  71. */
  72. public function getLength()
  73. {
  74. return sizeof($this->num);
  75. }
  76. /**
  77. * Multiply another polynom against this
  78. *
  79. * @param QRPolynominal $e The other
  80. * @return QRPolynominal The multiplied result
  81. */
  82. public function multiply(QRPolynominal $e)
  83. {
  84. $num = QRUtil::getInstance()->createEmptyArray($this->getLength() + $e->getLength() - 1);
  85. for($i = 0; $i < $this->getLength(); $i++)
  86. {
  87. for($j = 0; $j < $e->getLength(); $j++)
  88. {
  89. $a = QRMath::getInstance()->glog($this->get($i));
  90. $b = QRMath::getInstance()->glog($e->get($j));
  91. $base = 0;
  92. if(isset($num[$i + $j]))
  93. $base = $num[$i + $j];
  94. $num[$i + $j] = $base ^ QRMath::getInstance()->gexp( $a + $b );
  95. }
  96. }
  97. return new QRPolynominal($num, 0);
  98. }
  99. /**
  100. * Perform modulus against another polynom
  101. *
  102. * @param QRPolynominal $e
  103. *
  104. * @return QRPolynominal
  105. */
  106. public function mod(QRPolynominal $e)
  107. {
  108. if($this->getLength() - $e->getLength() < 0)
  109. {
  110. return $this;
  111. }
  112. $ratio = QRMath::getInstance()->glog($this->get(0)) - QRMath::getInstance()->glog($e->get(0));
  113. $num = QRUtil::getInstance()->createEmptyArray($this->getLength());
  114. for($i = 0; $i < $this->getLength(); $i++)
  115. {
  116. $num[$i] = $this->get($i);
  117. }
  118. for($i = 0; $i < $e->getLength(); $i++)
  119. {
  120. $num[$i] ^= QRMath::getInstance()->gexp(QRMath::getInstance()->glog($e->get($i)) + $ratio);
  121. }
  122. $result = new QRPolynominal($num, 0);
  123. $result = $result->mod($e);
  124. return $result;
  125. }
  126. }