From aff3ddea6c1555b1d99c166037b0325a98e2907b Mon Sep 17 00:00:00 2001 From: Daniel Lenski Date: Wed, 25 Aug 2021 10:17:55 -0700 Subject: [PATCH] mangled 'binary' barcode (see #17) --- test/barcodes/QR_CODE-binary-80.png | Bin 0 -> 478 bytes .../QR_CODE-binary-8081fe01008200.png | Bin 0 -> 295 bytes test/test_all.py | 7 +++++- zxing/__init__.py | 20 ++++++++++++------ 4 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 test/barcodes/QR_CODE-binary-80.png create mode 100644 test/barcodes/QR_CODE-binary-8081fe01008200.png diff --git a/test/barcodes/QR_CODE-binary-80.png b/test/barcodes/QR_CODE-binary-80.png new file mode 100644 index 0000000000000000000000000000000000000000..5f378aebdd2f33c4b72665cdbef244a6123c397b GIT binary patch literal 478 zcmeAS@N?(olH%oJU|`_&^l%AcU|@0qVMd?;!~NN8-x(Mf7kIijhE&{odp9FdAWR}>(@E)ay8yjJl=j`$+^lChub64qu%LDt)1YZBGl=kG!cm^ab3A) z@wxd||4xa285U;>lKIkaShlCH^Zi4)`^Pk`{D8`5TG_4J`#Sk(+`jl<>4{x^lRZ>| zZuNbi@!A4tTI}Y>{yH~H=K#(9RAOWDdxv6PJ&1Fuv_|ssv#!rtKqIH@eY|{fXXXXjmB@W1Ef1&Dp? zYN!6L_;mmqsnUscSmw|=|fi}oCO^@$!!uDt$u*Eww3Y^&7}D_1{$82$)A0#WhLS5;uW;FsbkR(ux)3w{`BV1zTBu{P5vT)9kBXfqj)^ zD;GZ}ToGvS99jPS>CR7fM_YnDIjN*Z^5eO4bw`WVf{eNJ+xp%AKp+DxWM3vP(f_Gn Xa9;FT^FvUqdAj6$pV)f;IFQF#;1OBOz`!j8!i<;h*8KqrZu4|;46*1vdwC=8VFdx!3*8zoY#1k9 zUy!UCaiCA!BWzJJuXiMaRhRH|Z#c_YO{c maJ}yF9LG=V|3%&R-&wqtnaOvH9MA_0p00i_>zoprc)0*`EO7Mz literal 0 HcmV?d00001 diff --git a/test/test_all.py b/test/test_all.py index f508efd..77a4129 100644 --- a/test/test_all.py +++ b/test/test_all.py @@ -16,6 +16,8 @@ ( 'QR CODE (¡filenáme törture test! 😉).png', 'QR_CODE', 'This should be QR_CODE' ), ( 'QR_CODE-png-but-wrong-extension.bmp', 'QR_CODE', 'This should be QR_CODE' ), ( 'QR_CODE-fun-with-whitespace.png', 'QR_CODE', '\n\r\t\r\r\r\n ' ), + ( 'QR_CODE-binary-8081fe01008200.png', 'QR_CODE', b'\x80\x81\xfe\x01\x00\x82\x00' ), + ( 'QR_CODE-binary-80.png', 'QR_CODE', b'\x80' ), ( 'QR_CODE-screen_scraping_torture_test.png', 'QR_CODE', '\n\\n¡Atención ☹! UTF-8 characters,\n\r embedded newlines,\r &&am&p;& trailing whitespace\t \r ' ), ] @@ -44,8 +46,11 @@ def _check_decoding(filename, expected_format, expected_raw, extra={}): if dec is not None: raise AssertionError('Expected failure, but got result in {} format'.format(expected_format, dec.format)) else: - if dec.raw != expected_raw: + if isinstance(expected_raw, bytes) and dec.raw_bytes != expected_raw: + raise AssertionError('Expected {!r} but got {!r}'.format(expected_raw, dec.raw_bytes)) + elif dec.raw != expected_raw: raise AssertionError('Expected {!r} but got {!r}'.format(expected_raw, dec.raw)) + if dec.format != expected_format: raise AssertionError('Expected {!r} but got {!r}'.format(expected_format, dec.format)) diff --git a/zxing/__init__.py b/zxing/__init__.py index 9a95e12..e6256fa 100644 --- a/zxing/__init__.py +++ b/zxing/__init__.py @@ -140,11 +140,19 @@ def parse(cls, zxing_output): if m: points.append((float(m.group(1)), float(m.group(2)))) - raw = raw[:-1].decode() - parsed = parsed[:-1].decode() - return cls(uri, format, type, raw, parsed, points) + raw_bytes = raw[:-1] + try: + raw = raw[:-1].decode() + except UnicodeDecodeError: + raw = None + try: + parsed = parsed[:-1].decode() + except UnicodeDecodeError: + parsed = None + return cls(uri, format, type, raw_bytes, raw, parsed, points) - def __init__(self, uri, format, type, raw, parsed, points): + def __init__(self, uri, format, type, raw_bytes, raw, parsed, points): + self.raw_bytes = raw_bytes self.raw = raw self.parsed = parsed self.uri = uri @@ -153,5 +161,5 @@ def __init__(self, uri, format, type, raw, parsed, points): self.points = points def __repr__(self): - return '{}(raw={!r}, parsed={!r}, uri={!r}, format={!r}, type={!r}, points={!r})'.format( - self.__class__.__name__, self.raw, self.parsed, self.uri, self.format, self.type, self.points) + return '{}(raw={!r}, parsed={!r}, uri={!r}, format={!r}, type={!r}, points={!r}, raw_bytes={!r})'.format( + self.__class__.__name__, self.raw, self.parsed, self.uri, self.format, self.type, self.points, self.raw_bytes)