QRMath.php 2.5 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 'QRCodeException.php';
  11. /**
  12. * Derived exception
  13. *
  14. * @author Maik Greubel <[email protected]>
  15. * @package phpQr
  16. */
  17. class QRMathException extends QRCodeException
  18. {
  19. }
  20. /**
  21. * QR Code math helper class
  22. *
  23. * @author Maik Greubel <[email protected]>
  24. * @package phpQr
  25. */
  26. final class QRMath
  27. {
  28. /**
  29. * Exponent table
  30. *
  31. * @var array
  32. */
  33. private $EXP_TABLE = null;
  34. /**
  35. * Logarithm table
  36. *
  37. * @var array
  38. */
  39. private $LOG_TABLE = null;
  40. /**
  41. * Singleton pattern
  42. *
  43. * @var QRMath
  44. */
  45. private static $instance = null;
  46. /**
  47. * Singleton pattern
  48. *
  49. * @return QRMath Singleton
  50. */
  51. public static function getInstance()
  52. {
  53. if (! self::$instance)
  54. {
  55. self::$instance = new self ();
  56. }
  57. return self::$instance;
  58. }
  59. /**
  60. * Create a new instance of QRMath
  61. */
  62. private function __construct()
  63. {
  64. $this->init ();
  65. }
  66. /**
  67. * Initialize the tables
  68. */
  69. private function init()
  70. {
  71. $this->EXP_TABLE = array ();
  72. for($i = 0; $i < 8; $i ++)
  73. {
  74. $this->EXP_TABLE [$i] = 1 << $i;
  75. }
  76. for($i = 8; $i < 256; $i ++)
  77. {
  78. $this->EXP_TABLE [$i] = $this->EXP_TABLE [$i - 4] ^ $this->EXP_TABLE [$i - 5] ^ $this->EXP_TABLE [$i - 6] ^ $this->EXP_TABLE [$i - 8];
  79. }
  80. $this->LOG_TABLE = array ();
  81. for($i = 0; $i < 255; $i ++)
  82. {
  83. $this->LOG_TABLE [$this->EXP_TABLE [$i]] = $i;
  84. }
  85. }
  86. /**
  87. * Get logarithm of n
  88. *
  89. * @param int $n
  90. * @throws QRMathException
  91. * @return int
  92. */
  93. public function glog($n)
  94. {
  95. if ($n < 1)
  96. {
  97. throw new QRMathException ( "glog(" . $n . ")" );
  98. }
  99. foreach ( $this->LOG_TABLE as $key => $value )
  100. {
  101. if ($key == $n)
  102. return $value;
  103. }
  104. throw new QRMathException ( "glog($n)" );
  105. }
  106. /**
  107. * Get the exponent of n
  108. *
  109. * @param int $n
  110. * @return int
  111. */
  112. public function gexp($n)
  113. {
  114. while ( $n < 0 )
  115. {
  116. $n += 255;
  117. }
  118. while ( $n >= 256 )
  119. {
  120. $n -= 255;
  121. }
  122. foreach ( $this->EXP_TABLE as $key => $value )
  123. {
  124. if ($key == $n)
  125. return $value;
  126. }
  127. throw new QRMathException ( "gexp($n)" );
  128. }
  129. }