17
17
18
18
namespace Zxing \Qrcode \Decoder ;
19
19
20
+ use ValueError ;
20
21
use Zxing \Common \BitSource ;
21
22
use Zxing \Common \CharacterSetECI ;
22
23
use Zxing \Common \DecoderResult ;
@@ -53,7 +54,7 @@ public static function decode(
53
54
array |null $ hints
54
55
): \Zxing \Common \DecoderResult {
55
56
$ bits = new BitSource ($ bytes );
56
- $ result = '' ;//new StringBuilder(50);
57
+ $ result = '' ; //new StringBuilder(50);
57
58
$ byteSegments = [];
58
59
$ symbolSequence = -1 ;
59
60
$ parityData = -1 ;
@@ -76,7 +77,7 @@ public static function decode(
76
77
$ fc1InEffect = true ;
77
78
} elseif ($ mode == Mode::$ STRUCTURED_APPEND ) {
78
79
if ($ bits ->available () < 16 ) {
79
- throw FormatException::getFormatInstance ();
80
+ throw FormatException::getFormatInstance (" Bits available < 16 " );
80
81
}
81
82
// sequence number and parity is added later to the result metadata
82
83
// Read next 8 bits (symbol sequence #) and 8 bits (parity data), then continue
@@ -87,7 +88,7 @@ public static function decode(
87
88
$ value = self ::parseECIValue ($ bits );
88
89
$ currentCharacterSetECI = CharacterSetECI::getCharacterSetECIByValue ($ value );
89
90
if ($ currentCharacterSetECI == null ) {
90
- throw FormatException::getFormatInstance ();
91
+ throw FormatException::getFormatInstance (" Current character set ECI is null " );
91
92
}
92
93
} else {
93
94
// First handle Hanzi mode which does not start with character count
@@ -111,22 +112,22 @@ public static function decode(
111
112
} elseif ($ mode == Mode::$ KANJI ) {
112
113
self ::decodeKanjiSegment ($ bits , $ result , $ count );
113
114
} else {
114
- throw FormatException::getFormatInstance ();
115
+ throw FormatException::getFormatInstance (" Unknown mode $ mode to decode " );
115
116
}
116
117
}
117
118
}
118
119
}
119
120
} while ($ mode != Mode::$ TERMINATOR );
120
- } catch (\InvalidArgumentException ) {
121
+ } catch (\InvalidArgumentException $ e ) {
121
122
// from readBits() calls
122
- throw FormatException::getFormatInstance ();
123
+ throw FormatException::getFormatInstance (" Invalid argument exception when formatting: " . $ e -> getMessage () );
123
124
}
124
125
125
126
return new DecoderResult (
126
127
$ bytes ,
127
128
$ result ,
128
129
empty ($ byteSegments ) ? null : $ byteSegments ,
129
- $ ecLevel == null ? null : 'L ' ,//ErrorCorrectionLevel::toString($ecLevel),
130
+ $ ecLevel == null ? null : 'L ' , //ErrorCorrectionLevel::toString($ecLevel),
130
131
$ symbolSequence ,
131
132
$ parityData
132
133
);
@@ -151,7 +152,7 @@ private static function parseECIValue(BitSource $bits): int
151
152
152
153
return (($ firstByte & 0x1F ) << 16 ) | $ secondThirdBytes ;
153
154
}
154
- throw FormatException::getFormatInstance ();
155
+ throw FormatException::getFormatInstance (" ECI Value parsing failed. " );
155
156
}
156
157
157
158
/**
@@ -166,7 +167,7 @@ private static function decodeHanziSegment(
166
167
): void {
167
168
// Don't crash trying to read more bits than we have available.
168
169
if ($ count * 13 > $ bits ->available ()) {
169
- throw FormatException::getFormatInstance ();
170
+ throw FormatException::getFormatInstance (" Trying to read more bits than we have available " );
170
171
}
171
172
172
173
// Each character will require 2 bytes. Read the characters as 2-byte pairs
@@ -184,8 +185,8 @@ private static function decodeHanziSegment(
184
185
// In the 0xB0A1 to 0xFAFE range
185
186
$ assembledTwoBytes += 0x0A6A1 ;
186
187
}
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)
189
190
$ offset += 2 ;
190
191
$ count --;
191
192
}
@@ -201,11 +202,11 @@ private static function decodeNumericSegment(
201
202
while ($ count >= 3 ) {
202
203
// Each 10 bits encodes three digits
203
204
if ($ bits ->available () < 10 ) {
204
- throw FormatException::getFormatInstance ();
205
+ throw FormatException::getFormatInstance (" Not enough bits available " );
205
206
}
206
207
$ threeDigitsBits = $ bits ->readBits (10 );
207
208
if ($ threeDigitsBits >= 1000 ) {
208
- throw FormatException::getFormatInstance ();
209
+ throw FormatException::getFormatInstance (" Too many three digit bits " );
209
210
}
210
211
$ result .= (self ::toAlphaNumericChar ($ threeDigitsBits / 100 ));
211
212
$ result .= (self ::toAlphaNumericChar (($ threeDigitsBits / 10 ) % 10 ));
@@ -215,22 +216,22 @@ private static function decodeNumericSegment(
215
216
if ($ count == 2 ) {
216
217
// Two digits left over to read, encoded in 7 bits
217
218
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 ' );
219
220
}
220
221
$ twoDigitsBits = $ bits ->readBits (7 );
221
222
if ($ twoDigitsBits >= 100 ) {
222
- throw FormatException::getFormatInstance ();
223
+ throw FormatException::getFormatInstance (" Too many bits: $ twoDigitsBits expected < 100 " );
223
224
}
224
225
$ result .= (self ::toAlphaNumericChar ($ twoDigitsBits / 10 ));
225
226
$ result .= (self ::toAlphaNumericChar ($ twoDigitsBits % 10 ));
226
227
} elseif ($ count == 1 ) {
227
228
// One digit left over to read
228
229
if ($ bits ->available () < 4 ) {
229
- throw FormatException::getFormatInstance ();
230
+ throw FormatException::getFormatInstance (" One digit left to read, but < 4 bits available " );
230
231
}
231
232
$ digitBits = $ bits ->readBits (4 );
232
233
if ($ digitBits >= 10 ) {
233
- throw FormatException::getFormatInstance ();
234
+ throw FormatException::getFormatInstance (" Too many bits: $ digitBits expected < 10 " );
234
235
}
235
236
$ result .= (self ::toAlphaNumericChar ($ digitBits ));
236
237
}
@@ -242,7 +243,7 @@ private static function decodeNumericSegment(
242
243
private static function toAlphaNumericChar (int |float $ value )
243
244
{
244
245
if ($ value >= count (self ::$ ALPHANUMERIC_CHARS )) {
245
- throw FormatException::getFormatInstance ();
246
+ throw FormatException::getFormatInstance (" $ value has too many alphanumeric chars " );
246
247
}
247
248
248
249
return self ::$ ALPHANUMERIC_CHARS [$ value ];
@@ -258,7 +259,7 @@ private static function decodeAlphanumericSegment(
258
259
$ start = strlen ((string ) $ result );
259
260
while ($ count > 1 ) {
260
261
if ($ bits ->available () < 11 ) {
261
- throw FormatException::getFormatInstance ();
262
+ throw FormatException::getFormatInstance (" Not enough bits available to read two expected characters " );
262
263
}
263
264
$ nextTwoCharsBits = $ bits ->readBits (11 );
264
265
$ result .= (self ::toAlphaNumericChar ($ nextTwoCharsBits / 45 ));
@@ -268,7 +269,7 @@ private static function decodeAlphanumericSegment(
268
269
if ($ count == 1 ) {
269
270
// special case: one character left
270
271
if ($ bits ->available () < 6 ) {
271
- throw FormatException::getFormatInstance ();
272
+ throw FormatException::getFormatInstance (" Not enough bits available to read one expected character " );
272
273
}
273
274
$ result .= self ::toAlphaNumericChar ($ bits ->readBits (6 ));
274
275
}
@@ -279,7 +280,7 @@ private static function decodeAlphanumericSegment(
279
280
if ($ result [$ i ] == '% ' ) {
280
281
if ($ i < strlen ((string ) $ result ) - 1 && $ result [$ i + 1 ] == '% ' ) {
281
282
// %% 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);
283
284
} else {
284
285
// In alpha mode, % should be converted to FNC1 separator 0x1D
285
286
$ result [$ i ] = chr (0x1D );
@@ -299,12 +300,12 @@ private static function decodeByteSegment(
299
300
): void {
300
301
// Don't crash trying to read more bits than we have available.
301
302
if (8 * $ count > $ bits ->available ()) {
302
- throw FormatException::getFormatInstance ();
303
+ throw FormatException::getFormatInstance (" Trying to read more bits than we have available " );
303
304
}
304
305
305
306
$ readBytes = fill_array (0 , $ count , 0 );
306
307
for ($ i = 0 ; $ i < $ count ; $ i ++) {
307
- $ readBytes [$ i ] = $ bits ->readBits (8 );//(byte)
308
+ $ readBytes [$ i ] = $ bits ->readBits (8 ); //(byte)
308
309
}
309
310
$ text = implode (array_map ('chr ' , $ readBytes ));
310
311
$ encoding = '' ;
@@ -315,12 +316,16 @@ private static function decodeByteSegment(
315
316
// Shift_JIS -- without anything like an ECI designator to
316
317
// give a hint.
317
318
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
+ }
319
324
} else {
320
325
$ encoding = $ currentCharacterSetECI ->name ();
321
326
}
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));
324
329
325
330
$ byteSegments = array_merge ($ byteSegments , $ readBytes );
326
331
}
@@ -332,7 +337,7 @@ private static function decodeKanjiSegment(
332
337
): void {
333
338
// Don't crash trying to read more bits than we have available.
334
339
if ($ count * 13 > $ bits ->available ()) {
335
- throw FormatException::getFormatInstance ();
340
+ throw FormatException::getFormatInstance (" Trying to read more bits than we have available " );
336
341
}
337
342
338
343
// Each character will require 2 bytes. Read the characters as 2-byte pairs
@@ -350,7 +355,7 @@ private static function decodeKanjiSegment(
350
355
// In the 0xE040 to 0xEBBF range
351
356
$ assembledTwoBytes += 0x0C140 ;
352
357
}
353
- $ buffer [$ offset ] = ($ assembledTwoBytes >> 8 );//(byte)
358
+ $ buffer [$ offset ] = ($ assembledTwoBytes >> 8 ); //(byte)
354
359
$ buffer [$ offset + 1 ] = $ assembledTwoBytes ; //(byte)
355
360
$ offset += 2 ;
356
361
$ count --;
0 commit comments