Skip to content

Commit 266ffce

Browse files
committed
🔧 QRData::estimateTotalBitLength(): do not substract from the estimated bit length
1 parent e81ed39 commit 266ffce

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/Data/QRData.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ public function estimateTotalBitLength():int{
165165

166166
// it seems that in some cases the estimated total length is not 100% accurate,
167167
// so we substract 4 bits from the total when not in mixed mode
168-
if(count($this->dataSegments) <= 1){
169-
$length -= 4;
170-
}
168+
# if(count($this->dataSegments) <= 1){
169+
# $length -= 4;
170+
# }
171171

172172
// we've got a match!
173173
// or let's see if there's a higher version number available

tests/Data/QRDataTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
namespace chillerlan\QRCodeTest\Data;
1212

1313
use chillerlan\QRCode\Common\BitBuffer;
14+
use chillerlan\QRCode\Common\EccLevel;
1415
use chillerlan\QRCode\Common\MaskPattern;
16+
use chillerlan\QRCode\Data\Byte;
1517
use chillerlan\QRCode\Data\QRData;
1618
use chillerlan\QRCode\Output\QRGdImagePNG;
1719
use chillerlan\QRCode\QRCode;
@@ -63,4 +65,28 @@ public function testSetBitBuffer():void{
6365
$this::assertSame($decodeResult->data, 'https://www.youtube.com/watch?v=DLzxrzFCyOs&t=43s');
6466
}
6567

68+
public function testEstimateTotalBitLength():void{
69+
70+
$options = new QROptions([
71+
'versionMin' => 10,
72+
'quietzoneSize' => 2,
73+
'eccLevel' => EccLevel::H,
74+
# 'outputType' => QROutputInterface::CUSTOM,
75+
# 'outputInterface' => PmaQrCodeSVG::class,
76+
'outputBase64' => false,
77+
'cssClass' => 'pma-2fa-qrcode',
78+
'drawCircularModules' => true,
79+
]);
80+
81+
// version 10H has a maximum of 976 bits, which is the exact length of the string below
82+
// QRData::estimateTotalBitLength() used to substract 4 bits for a hypothetical data mode indicator
83+
// we're now going the safe route and do not do that anymore...
84+
$str = 'otpauth://totp/user?secret=P2SXMJFJ7DJGHLVEQYBNH2EYM4FH66CR'.
85+
'&issuer=phpMyAdmin%20%28%29&digits=6&algorithm=SHA1&period=30';
86+
87+
$qrData = new QRData($options, [new Byte($str)]);
88+
89+
$this::assertSame(980, $qrData->estimateTotalBitLength());
90+
}
91+
6692
}

0 commit comments

Comments
 (0)