Procházet zdrojové kódy

:wrench: fix mask pattern 2 & 3 according to ISO/IEC 18004:2000 Section 8.8.1

codemasher před 5 roky
rodič
revize
f58c42ebbf
3 změnil soubory, kde provedl 20 přidání a 21 odebrání
  1. 20 21
      src/Data/QRMatrix.php
  2. 0 0
      tests/QRCodeTest.php
  3. 0 0
      tests/Traits/QRAuthenticatorTest.php

+ 20 - 21
src/Data/QRMatrix.php

@@ -13,6 +13,7 @@
 namespace chillerlan\QRCode\Data;
 
 use chillerlan\QRCode\QRCode;
+use Closure;
 
 /**
  * @link http://www.thonky.com/qr-code-tutorial/format-version-information
@@ -479,6 +480,7 @@ class QRMatrix{
 		$this->maskPattern = $maskPattern;
 		$byteCount         = count($data);
 		$size              = $this->moduleCount - 1;
+		$mask              = $this->getMask($this->maskPattern);
 
 		for($i = $size, $y = $size, $inc = -1, $byteIndex = 0, $bitIndex  = 7; $i > 0; $i -= 2){
 
@@ -497,7 +499,7 @@ class QRMatrix{
 							$v = (($data[$byteIndex] >> $bitIndex) & 1) === 1;
 						}
 
-						if($this->getMask($x, $y, $maskPattern) === 0){
+						if($mask($x, $y) === 0){
 							$v = !$v;
 						}
 
@@ -528,36 +530,33 @@ class QRMatrix{
 	}
 
 	/**
+	 *  ISO/IEC 18004:2000 Section 8.8.1
+	 *
 	 * @see \chillerlan\QRCode\QRMatrix::mapData()
 	 *
 	 * @internal
 	 *
-	 * @param int $x
-	 * @param int $y
 	 * @param int $maskPattern
 	 *
-	 * @return int
+	 * @return \Closure
 	 * @throws \chillerlan\QRCode\Data\QRCodeDataException
 	 */
-	protected function getMask(int $x, int $y, int $maskPattern):int {
-		$a = $y + $x;
-		$m = $y * $x;
-
-		if($maskPattern >= 0 && $maskPattern < 8){
-			// this is literally the same as the stupid switch...
-			return [
-				$a % 2,
-				$y % 2,
-				$x % 3,
-				$a % 3,
-				(floor($y / 2) + floor($x / 3)) % 2,
-				$m % 2 + $m % 3,
-				($m % 2 + $m % 3) % 2,
-				($m % 3 + $a % 2) % 2
-			][$maskPattern];
+	protected function getMask(int $maskPattern):Closure{
+
+		if((0b111 & $maskPattern) !== $maskPattern){
+			throw new QRCodeDataException('invalid mask pattern'); // @codeCoverageIgnore
 		}
 
-		throw new QRCodeDataException('invalid mask pattern'); // @codeCoverageIgnore
+		return [
+			0b000 => function($x, $y):int{ return ($x + $y) % 2; },
+			0b001 => function($x, $y):int{ return $x % 2; },
+			0b010 => function($x, $y):int{ return $y % 3; },
+			0b011 => function($x, $y):int{ return ($x + $y) % 3; },
+			0b100 => function($x, $y):int{ return ((int)($x / 2) + (int)($y / 3)) % 2; },
+			0b101 => function($x, $y):int{ return (($x * $y) % 2) + (($x * $y) % 3); },
+			0b110 => function($x, $y):int{ return ((($x * $y) % 2) + (($x * $y) % 3)) % 2; },
+			0b111 => function($x, $y):int{ return ((($x * $y) % 3) + (($x + $y) % 2)) % 2; },
+		][$maskPattern];
 	}
 
 }

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 0 - 0
tests/QRCodeTest.php


Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 0 - 0
tests/Traits/QRAuthenticatorTest.php


Některé soubory nejsou zobrazeny, neboť je v těchto rozdílových datech změněno mnoho souborů