1717
1818namespace Zxing \Qrcode \Decoder ;
1919
20+ use ValueError ;
2021use Zxing \Common \BitSource ;
2122use Zxing \Common \CharacterSetECI ;
2223use Zxing \Common \DecoderResult ;
@@ -53,7 +54,7 @@ public static function decode(
5354 array |null $ hints
5455 ): \Zxing \Common \DecoderResult {
5556 $ bits = new BitSource ($ bytes );
56- $ result = '' ;//new StringBuilder(50);
57+ $ result = '' ; //new StringBuilder(50);
5758 $ byteSegments = [];
5859 $ symbolSequence = -1 ;
5960 $ parityData = -1 ;
@@ -76,7 +77,7 @@ public static function decode(
7677 $ fc1InEffect = true ;
7778 } elseif ($ mode == Mode::$ STRUCTURED_APPEND ) {
7879 if ($ bits ->available () < 16 ) {
79- throw FormatException::getFormatInstance ();
80+ throw FormatException::getFormatInstance (" Bits available < 16 " );
8081 }
8182 // sequence number and parity is added later to the result metadata
8283 // Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue
@@ -87,7 +88,7 @@ public static function decode(
8788 $ value = self ::parseECIValue ($ bits );
8889 $ currentCharacterSetECI = CharacterSetECI::getCharacterSetECIByValue ($ value );
8990 if ($ currentCharacterSetECI == null ) {
90- throw FormatException::getFormatInstance ();
91+ throw FormatException::getFormatInstance (" Current character set ECI is null " );
9192 }
9293 } else {
9394 // First handle Hanzi mode which does not start with character count
@@ -111,22 +112,22 @@ public static function decode(
111112 } elseif ($ mode == Mode::$ KANJI ) {
112113 self ::decodeKanjiSegment ($ bits , $ result , $ count );
113114 } else {
114- throw FormatException::getFormatInstance ();
115+ throw FormatException::getFormatInstance (" Unknown mode $ mode to decode " );
115116 }
116117 }
117118 }
118119 }
119120 } while ($ mode != Mode::$ TERMINATOR );
120- } catch (\InvalidArgumentException ) {
121+ } catch (\InvalidArgumentException $ e ) {
121122 // from readBits() calls
122- throw FormatException::getFormatInstance ();
123+ throw FormatException::getFormatInstance (" Invalid argument exception when formatting: " . $ e -> getMessage () );
123124 }
124125
125126 return new DecoderResult (
126127 $ bytes ,
127128 $ result ,
128129 empty ($ byteSegments ) ? null : $ byteSegments ,
129- $ ecLevel == null ? null : 'L ' ,//ErrorCorrectionLevel::toString($ecLevel),
130+ $ ecLevel == null ? null : 'L ' , //ErrorCorrectionLevel::toString($ecLevel),
130131 $ symbolSequence ,
131132 $ parityData
132133 );
@@ -151,7 +152,7 @@ private static function parseECIValue(BitSource $bits): int
151152
152153 return (($ firstByte & 0x1F ) << 16 ) | $ secondThirdBytes ;
153154 }
154- throw FormatException::getFormatInstance ();
155+ throw FormatException::getFormatInstance (" ECI Value parsing failed. " );
155156 }
156157
157158 /**
@@ -166,7 +167,7 @@ private static function decodeHanziSegment(
166167 ): void {
167168 // Don't crash trying to read more bits than we have available.
168169 if ($ count * 13 > $ bits ->available ()) {
169- throw FormatException::getFormatInstance ();
170+ throw FormatException::getFormatInstance (" Trying to read more bits than we have available " );
170171 }
171172
172173 // Each character will require 2 bytes. Read the characters as 2-byte pairs
@@ -184,8 +185,8 @@ private static function decodeHanziSegment(
184185 // In the 0xB0A1 to 0xFAFE range
185186 $ assembledTwoBytes += 0x0A6A1 ;
186187 }
187- $ buffer [$ offset ] = (($ assembledTwoBytes >> 8 ) & 0xFF );//(byte)
188- $ buffer [$ offset + 1 ] = ($ assembledTwoBytes & 0xFF );//(byte)
188+ $ buffer [$ offset ] = (($ assembledTwoBytes >> 8 ) & 0xFF ); //(byte)
189+ $ buffer [$ offset + 1 ] = ($ assembledTwoBytes & 0xFF ); //(byte)
189190 $ offset += 2 ;
190191 $ count --;
191192 }
@@ -201,11 +202,11 @@ private static function decodeNumericSegment(
201202 while ($ count >= 3 ) {
202203 // Each 10 bits encodes three digits
203204 if ($ bits ->available () < 10 ) {
204- throw FormatException::getFormatInstance ();
205+ throw FormatException::getFormatInstance (" Not enough bits available " );
205206 }
206207 $ threeDigitsBits = $ bits ->readBits (10 );
207208 if ($ threeDigitsBits >= 1000 ) {
208- throw FormatException::getFormatInstance ();
209+ throw FormatException::getFormatInstance (" Too many three digit bits " );
209210 }
210211 $ result .= (self ::toAlphaNumericChar ($ threeDigitsBits / 100 ));
211212 $ result .= (self ::toAlphaNumericChar (($ threeDigitsBits / 10 ) % 10 ));
@@ -215,22 +216,22 @@ private static function decodeNumericSegment(
215216 if ($ count == 2 ) {
216217 // Two digits left over to read, encoded in 7 bits
217218 if ($ bits ->available () < 7 ) {
218- throw FormatException::getFormatInstance ();
219+ throw FormatException::getFormatInstance (" Two digits left over to read, encoded in 7 bits, but only " . $ bits -> available () . ' bits available ' );
219220 }
220221 $ twoDigitsBits = $ bits ->readBits (7 );
221222 if ($ twoDigitsBits >= 100 ) {
222- throw FormatException::getFormatInstance ();
223+ throw FormatException::getFormatInstance (" Too many bits: $ twoDigitsBits expected < 100 " );
223224 }
224225 $ result .= (self ::toAlphaNumericChar ($ twoDigitsBits / 10 ));
225226 $ result .= (self ::toAlphaNumericChar ($ twoDigitsBits % 10 ));
226227 } elseif ($ count == 1 ) {
227228 // One digit left over to read
228229 if ($ bits ->available () < 4 ) {
229- throw FormatException::getFormatInstance ();
230+ throw FormatException::getFormatInstance (" One digit left to read, but < 4 bits available " );
230231 }
231232 $ digitBits = $ bits ->readBits (4 );
232233 if ($ digitBits >= 10 ) {
233- throw FormatException::getFormatInstance ();
234+ throw FormatException::getFormatInstance (" Too many bits: $ digitBits expected < 10 " );
234235 }
235236 $ result .= (self ::toAlphaNumericChar ($ digitBits ));
236237 }
@@ -242,7 +243,7 @@ private static function decodeNumericSegment(
242243 private static function toAlphaNumericChar (int |float $ value )
243244 {
244245 if ($ value >= count (self ::$ ALPHANUMERIC_CHARS )) {
245- throw FormatException::getFormatInstance ();
246+ throw FormatException::getFormatInstance (" $ value has too many alphanumeric chars " );
246247 }
247248
248249 return self ::$ ALPHANUMERIC_CHARS [$ value ];
@@ -258,7 +259,7 @@ private static function decodeAlphanumericSegment(
258259 $ start = strlen ((string ) $ result );
259260 while ($ count > 1 ) {
260261 if ($ bits ->available () < 11 ) {
261- throw FormatException::getFormatInstance ();
262+ throw FormatException::getFormatInstance (" Not enough bits available to read two expected characters " );
262263 }
263264 $ nextTwoCharsBits = $ bits ->readBits (11 );
264265 $ result .= (self ::toAlphaNumericChar ($ nextTwoCharsBits / 45 ));
@@ -268,7 +269,7 @@ private static function decodeAlphanumericSegment(
268269 if ($ count == 1 ) {
269270 // special case: one character left
270271 if ($ bits ->available () < 6 ) {
271- throw FormatException::getFormatInstance ();
272+ throw FormatException::getFormatInstance (" Not enough bits available to read one expected character " );
272273 }
273274 $ result .= self ::toAlphaNumericChar ($ bits ->readBits (6 ));
274275 }
@@ -279,7 +280,7 @@ private static function decodeAlphanumericSegment(
279280 if ($ result [$ i ] == '% ' ) {
280281 if ($ i < strlen ((string ) $ result ) - 1 && $ result [$ i + 1 ] == '% ' ) {
281282 // %% is rendered as %
282- $ result = substr_replace ($ result , '' , $ i + 1 , 1 );//deleteCharAt(i + 1);
283+ $ result = substr_replace ($ result , '' , $ i + 1 , 1 ); //deleteCharAt(i + 1);
283284 } else {
284285 // In alpha mode, % should be converted to FNC1 separator 0x1D
285286 $ result [$ i ] = chr (0x1D );
@@ -299,12 +300,12 @@ private static function decodeByteSegment(
299300 ): void {
300301 // Don't crash trying to read more bits than we have available.
301302 if (8 * $ count > $ bits ->available ()) {
302- throw FormatException::getFormatInstance ();
303+ throw FormatException::getFormatInstance (" Trying to read more bits than we have available " );
303304 }
304305
305306 $ readBytes = fill_array (0 , $ count , 0 );
306307 for ($ i = 0 ; $ i < $ count ; $ i ++) {
307- $ readBytes [$ i ] = $ bits ->readBits (8 );//(byte)
308+ $ readBytes [$ i ] = $ bits ->readBits (8 ); //(byte)
308309 }
309310 $ text = implode (array_map ('chr ' , $ readBytes ));
310311 $ encoding = '' ;
@@ -315,12 +316,16 @@ private static function decodeByteSegment(
315316 // Shift_JIS -- without anything like an ECI designator to
316317 // give a hint.
317318
318- $ encoding = mb_detect_encoding ($ text , $ hints );
319+ try {
320+ $ encoding = mb_detect_encoding ($ text , $ hints );
321+ } catch (ValueError $ e ) {
322+ $ encoding = mb_detect_encoding ($ text , mb_detect_order (), false );
323+ }
319324 } else {
320325 $ encoding = $ currentCharacterSetECI ->name ();
321326 }
322- // $result.= mb_convert_encoding($text , $encoding);//(new String(readBytes, encoding));
323- $ result .= $ text ;//(new String(readBytes, encoding));
327+ $ result .= mb_convert_encoding ($ text, $ encoding ); //(new String(readBytes, encoding));
328+ // $result .= $text; //(new String(readBytes, encoding));
324329
325330 $ byteSegments = array_merge ($ byteSegments , $ readBytes );
326331 }
@@ -332,7 +337,7 @@ private static function decodeKanjiSegment(
332337 ): void {
333338 // Don't crash trying to read more bits than we have available.
334339 if ($ count * 13 > $ bits ->available ()) {
335- throw FormatException::getFormatInstance ();
340+ throw FormatException::getFormatInstance (" Trying to read more bits than we have available " );
336341 }
337342
338343 // Each character will require 2 bytes. Read the characters as 2-byte pairs
@@ -350,7 +355,7 @@ private static function decodeKanjiSegment(
350355 // In the 0xE040 to 0xEBBF range
351356 $ assembledTwoBytes += 0x0C140 ;
352357 }
353- $ buffer [$ offset ] = ($ assembledTwoBytes >> 8 );//(byte)
358+ $ buffer [$ offset ] = ($ assembledTwoBytes >> 8 ); //(byte)
354359 $ buffer [$ offset + 1 ] = $ assembledTwoBytes ; //(byte)
355360 $ offset += 2 ;
356361 $ count --;
0 commit comments