diff options
-rw-r--r-- | mitmproxy/contentviews/image/image_parser.py | 4 | ||||
-rw-r--r-- | mitmproxy/contentviews/image/png.py | 34 | ||||
-rw-r--r-- | test/mitmproxy/contentviews/test_image_parser.py | 68 | ||||
-rw-r--r-- | test/mitmproxy/data/png_parser/ctzn0g04.png | bin | 0 -> 753 bytes |
4 files changed, 87 insertions, 19 deletions
diff --git a/mitmproxy/contentviews/image/image_parser.py b/mitmproxy/contentviews/image/image_parser.py index e523def5..3a375006 100644 --- a/mitmproxy/contentviews/image/image_parser.py +++ b/mitmproxy/contentviews/image/image_parser.py @@ -19,4 +19,8 @@ def parse_png(data: bytes) -> Metadata: parts.append(tuple(['aspect', str(aspectx) + " x " + str(aspecty)])) elif chunk.type == 'tEXt': parts.append(tuple([chunk.body.keyword, chunk.body.text])) + elif chunk.type == 'iTXt': + parts.append(tuple([chunk.body.keyword, chunk.body.text])) + elif chunk.type == 'zTXt': + parts.append(tuple([chunk.body.keyword, chunk.body.text_datastream.decode('iso8859-1')])) return parts diff --git a/mitmproxy/contentviews/image/png.py b/mitmproxy/contentviews/image/png.py index 5996e3ce..53d39799 100644 --- a/mitmproxy/contentviews/image/png.py +++ b/mitmproxy/contentviews/image/png.py @@ -53,7 +53,11 @@ class Png(KaitaiStruct): self.len = self._io.read_u4be() self.type = self._io.read_str_byte_limit(4, "UTF-8") _on = self.type - if _on == u"gAMA": + if _on == u"iTXt": + self._raw_body = self._io.read_bytes(self.len) + io = KaitaiStream(BytesIO(self._raw_body)) + self.body = self._root.InternationalTextChunk(io, self, self._root) + elif _on == u"gAMA": self._raw_body = self._io.read_bytes(self.len) io = KaitaiStream(BytesIO(self._raw_body)) self.body = self._root.GamaChunk(io, self, self._root) @@ -85,6 +89,10 @@ class Png(KaitaiStruct): self._raw_body = self._io.read_bytes(self.len) io = KaitaiStream(BytesIO(self._raw_body)) self.body = self._root.SrgbChunk(io, self, self._root) + elif _on == u"zTXt": + self._raw_body = self._io.read_bytes(self.len) + io = KaitaiStream(BytesIO(self._raw_body)) + self.body = self._root.CompressedTextChunk(io, self, self._root) else: self.body = self._io.read_bytes(self.len) self.crc = self._io.read_bytes(4) @@ -181,6 +189,17 @@ class Png(KaitaiStruct): self.render_intent = self._root.Intent(self._io.read_u1()) + class CompressedTextChunk(KaitaiStruct): + def __init__(self, _io, _parent=None, _root=None): + self._io = _io + self._parent = _parent + self._root = _root if _root else self + self.keyword = self._io.read_strz("UTF-8", 0, False, True, True) + self.compression_method = self._io.read_u1() + self._raw_text_datastream = self._io.read_bytes_full() + self.text_datastream = zlib.decompress(self._raw_text_datastream) + + class BkgdTruecolor(KaitaiStruct): def __init__(self, _io, _parent=None, _root=None): self._io = _io @@ -235,6 +254,19 @@ class Png(KaitaiStruct): self.unit = self._root.PhysUnit(self._io.read_u1()) + class InternationalTextChunk(KaitaiStruct): + def __init__(self, _io, _parent=None, _root=None): + self._io = _io + self._parent = _parent + self._root = _root if _root else self + self.keyword = self._io.read_strz("UTF-8", 0, False, True, True) + self.compression_flag = self._io.read_u1() + self.compression_method = self._io.read_u1() + self.language_tag = self._io.read_strz("iso8859-1", 0, False, True, True) + self.translated_keyword = self._io.read_strz("UTF-8", 0, False, True, True) + self.text = self._io.read_str_eos("UTF-8") + + class TextChunk(KaitaiStruct): def __init__(self, _io, _parent=None, _root=None): self._io = _io diff --git a/test/mitmproxy/contentviews/test_image_parser.py b/test/mitmproxy/contentviews/test_image_parser.py index 2544e3f1..3d987b9d 100644 --- a/test/mitmproxy/contentviews/test_image_parser.py +++ b/test/mitmproxy/contentviews/test_image_parser.py @@ -4,7 +4,7 @@ from mitmproxy.contentviews.image import image_parser from mitmproxy.test import tutils class TestPngParser: - def test_png_parser(self): + def test_png_size(self): img = "mitmproxy/data/image.png" with open(tutils.test_data.path(img), "rb") as f: parts = image_parser.parse_png(io.BytesIO(f.read())) @@ -12,22 +12,6 @@ class TestPngParser: assert tuple(['Size', '174 x 174 px']) in parts assert tuple(["Format", "Portable network graphics"]) in parts - def test_textual_data(self): - img = "mitmproxy/data/png_parser/ct1n0g04.png" - with open(tutils.test_data.path(img), "rb") as f: - parts = image_parser.parse_png(io.BytesIO(f.read())) - assert parts - expected = [ - ('Title', 'PngSuite'), - ('Author', 'Willem A.J. van Schaik\n(willem@schaik.com)'), - ('Copyright', 'Copyright Willem van Schaik, Singapore 1995-96'), - ('Description', 'A compilation of a set of images created to test the\nvarious color-types of the PNG format. Included are\nblack&white, color, paletted, with alpha channel, with\ntransparency formats. All bit-depths allowed according\nto the spec are present.'), - ('Software', 'Created on a NeXTstation color using "pnmtopng".'), - ('Disclaimer', 'Freeware.') - ] - for data in expected: - assert data in parts - def test_no_textual_data(self): img = "mitmproxy/data/png_parser/ct0n0g04.png" with open(tutils.test_data.path(img), "rb") as f: @@ -41,6 +25,54 @@ class TestPngParser: parts = [data for data in parts if data not in metadata] assert not parts + def test_textual_data(self): + img = "mitmproxy/data/png_parser/ct1n0g04.png" + expected = [ + ('Title', 'PngSuite'), + ('Author', 'Willem A.J. van Schaik\n(willem@schaik.com)'), + ('Copyright', 'Copyright Willem van Schaik, Singapore 1995-96'), + ('Description', 'A compilation of a set of images created to test the\nvarious color-types of the PNG format. Included are\nblack&white, color, paletted, with alpha channel, with\ntransparency formats. All bit-depths allowed according\nto the spec are present.'), + ('Software', 'Created on a NeXTstation color using "pnmtopng".'), + ('Disclaimer', 'Freeware.') + ] + with open(tutils.test_data.path(img), "rb") as f: + parts = image_parser.parse_png(io.BytesIO(f.read())) + assert parts + for data in expected: + assert data in parts + + def test_compressed_textual_data(self): + img = "mitmproxy/data/png_parser/ctzn0g04.png" + expected = [ + ('Title', 'PngSuite'), + ('Author', 'Willem A.J. van Schaik\n(willem@schaik.com)'), + ('Copyright', 'Copyright Willem van Schaik, Singapore 1995-96'), + ('Description', 'A compilation of a set of images created to test the\nvarious color-types of the PNG format. Included are\nblack&white, color, paletted, with alpha channel, with\ntransparency formats. All bit-depths allowed according\nto the spec are present.'), + ('Software', 'Created on a NeXTstation color using "pnmtopng".'), + ('Disclaimer', 'Freeware.') + ] + with open(tutils.test_data.path(img), "rb") as f: + parts = image_parser.parse_png(io.BytesIO(f.read())) + assert parts + for data in expected: + assert data in parts + + def test_international_textual_data(self): + img = "mitmproxy/data/png_parser/cten0g04.png" + expected = [ + ('Author', 'Willem van Schaik (willem@schaik.com)'), + ('Copyright', 'Copyright Willem van Schaik, Canada 2011'), + ('Description', 'A compilation of a set of images created to test the various color-types of the PNG format. Included are black&white, color, paletted, with alpha channel, with transparency formats. All bit-depths allowed according to the spec are present.'), + ('Software', 'Created on a NeXTstation color using "pnmtopng".'), + ('Disclaimer', 'Freeware.') + ] + with open(tutils.test_data.path(img), "rb") as f: + parts = image_parser.parse_png(io.BytesIO(f.read())) + print(parts) + assert parts + for data in expected: + assert data in parts + def test_gamma(self): img = "mitmproxy/data/png_parser/g07n0g16.png" with open(tutils.test_data.path(img), "rb") as f: @@ -48,7 +80,7 @@ class TestPngParser: assert parts assert ('gamma', '0.7') in parts - def test_gamma(self): + def test_aspect(self): img = "mitmproxy/data/png_parser/aspect.png" with open(tutils.test_data.path(img), "rb") as f: parts = image_parser.parse_png(io.BytesIO(f.read())) diff --git a/test/mitmproxy/data/png_parser/ctzn0g04.png b/test/mitmproxy/data/png_parser/ctzn0g04.png Binary files differnew file mode 100644 index 00000000..b4401c9c --- /dev/null +++ b/test/mitmproxy/data/png_parser/ctzn0g04.png |