Skip to content

Commit

Permalink
Merge pull request #6092 from radarhere/accept
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Mar 5, 2022
2 parents 6b9f854 + 633abcb commit 180c89f
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 23 deletions.
7 changes: 7 additions & 0 deletions Tests/test_file_dds.py
Expand Up @@ -196,6 +196,13 @@ def test__accept_false():
assert not output


def test_invalid_file():
invalid_file = "Tests/images/flower.jpg"

with pytest.raises(SyntaxError):
DdsImagePlugin.DdsImageFile(invalid_file)


def test_short_header():
"""Check a short header"""
with open(TEST_FILE_DXT5, "rb") as f:
Expand Down
7 changes: 7 additions & 0 deletions Tests/test_file_ftex.py
Expand Up @@ -16,6 +16,13 @@ def test_load_dxt1():
assert_image_similar(im, target.convert("RGBA"), 15)


def test_invalid_file():
invalid_file = "Tests/images/flower.jpg"

with pytest.raises(SyntaxError):
FtexImagePlugin.FtexImageFile(invalid_file)


def test_constants_deprecation():
for enum, prefix in {
FtexImagePlugin.Format: "FORMAT_",
Expand Down
9 changes: 8 additions & 1 deletion Tests/test_file_xbm.py
Expand Up @@ -2,7 +2,7 @@

import pytest

from PIL import Image
from PIL import Image, XbmImagePlugin

from .helper import hopper

Expand Down Expand Up @@ -63,6 +63,13 @@ def test_open_filename_with_underscore():
assert im.size == (128, 128)


def test_invalid_file():
invalid_file = "Tests/images/flower.jpg"

with pytest.raises(SyntaxError):
XbmImagePlugin.XbmImageFile(invalid_file)


def test_save_wrong_mode(tmp_path):
im = hopper()
out = str(tmp_path / "temp.xbm")
Expand Down
4 changes: 3 additions & 1 deletion docs/example/DdsImagePlugin.py
Expand Up @@ -210,7 +210,9 @@ class DdsImageFile(ImageFile.ImageFile):
format_description = "DirectDraw Surface"

def _open(self):
magic, header_size = struct.unpack("<II", self.fp.read(8))
if not _accept(self.fp.read(4)):
raise SyntaxError("not a DDS file")
(header_size,) = struct.unpack("<I", self.fp.read(4))
if header_size != 124:
raise OSError(f"Unsupported header size {repr(header_size)}")
header_bytes = self.fp.read(header_size - 4)
Expand Down
4 changes: 3 additions & 1 deletion src/PIL/DdsImagePlugin.py
Expand Up @@ -111,7 +111,9 @@ class DdsImageFile(ImageFile.ImageFile):
format_description = "DirectDraw Surface"

def _open(self):
magic, header_size = struct.unpack("<II", self.fp.read(8))
if not _accept(self.fp.read(4)):
raise SyntaxError("not a DDS file")
(header_size,) = struct.unpack("<I", self.fp.read(4))
if header_size != 124:
raise OSError(f"Unsupported header size {repr(header_size)}")
header_bytes = self.fp.read(header_size - 4)
Expand Down
12 changes: 6 additions & 6 deletions src/PIL/FliImagePlugin.py
Expand Up @@ -26,7 +26,11 @@


def _accept(prefix):
return len(prefix) >= 6 and i16(prefix, 4) in [0xAF11, 0xAF12]
return (
len(prefix) >= 6
and i16(prefix, 4) in [0xAF11, 0xAF12]
and i16(prefix, 14) in [0, 3] # flags
)


##
Expand All @@ -44,11 +48,7 @@ def _open(self):

# HEAD
s = self.fp.read(128)
if not (
_accept(s)
and i16(s, 14) in [0, 3] # flags
and s[20:22] == b"\x00\x00" # reserved
):
if not (_accept(s) and s[20:22] == b"\x00\x00"):
raise SyntaxError("not an FLI/FLC file")

# frames
Expand Down
3 changes: 2 additions & 1 deletion src/PIL/FtexImagePlugin.py
Expand Up @@ -94,7 +94,8 @@ class FtexImageFile(ImageFile.ImageFile):
format_description = "Texture File Format (IW2:EOC)"

def _open(self):
struct.unpack("<I", self.fp.read(4)) # magic
if not _accept(self.fp.read(4)):
raise SyntaxError("not an FTEX file")
struct.unpack("<i", self.fp.read(4)) # version
self._size = struct.unpack("<2i", self.fp.read(8))
mipmap_count, format_count = struct.unpack("<2i", self.fp.read(8))
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/GbrImagePlugin.py
Expand Up @@ -43,9 +43,9 @@ class GbrImageFile(ImageFile.ImageFile):

def _open(self):
header_size = i32(self.fp.read(4))
version = i32(self.fp.read(4))
if header_size < 20:
raise SyntaxError("not a GIMP brush")
version = i32(self.fp.read(4))
if version not in (1, 2):
raise SyntaxError(f"Unsupported GIMP brush version: {version}")

Expand Down
2 changes: 1 addition & 1 deletion src/PIL/IcnsImagePlugin.py
Expand Up @@ -167,7 +167,7 @@ def __init__(self, fobj):
self.dct = dct = {}
self.fobj = fobj
sig, filesize = nextheader(fobj)
if sig != MAGIC:
if not _accept(sig):
raise SyntaxError("not an icns file")
i = HEADERSIZE
while i < filesize:
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/TiffImagePlugin.py
Expand Up @@ -493,7 +493,7 @@ def __init__(self, ifh=b"II\052\0\0\0\0\0", prefix=None, group=None):
endianness.
:param prefix: Override the endianness of the file.
"""
if ifh[:4] not in PREFIXES:
if not _accept(ifh):
raise SyntaxError(f"not a TIFF file (header {repr(ifh)} not valid)")
self._prefix = prefix if prefix is not None else ifh[:2]
if self._prefix == MM:
Expand Down
3 changes: 1 addition & 2 deletions src/PIL/WmfImagePlugin.py
Expand Up @@ -21,7 +21,6 @@

from . import Image, ImageFile
from ._binary import i16le as word
from ._binary import i32le as dword
from ._binary import si16le as short
from ._binary import si32le as _long

Expand Down Expand Up @@ -112,7 +111,7 @@ def _open(self):
if s[22:26] != b"\x01\x00\t\x00":
raise SyntaxError("Unsupported WMF file format")

elif dword(s) == 1 and s[40:44] == b" EMF":
elif s[:4] == b"\x01\x00\x00\x00" and s[40:44] == b" EMF":
# enhanced metafile

# get bounding box
Expand Down
17 changes: 9 additions & 8 deletions src/PIL/XbmImagePlugin.py
Expand Up @@ -52,18 +52,19 @@ def _open(self):

m = xbm_head.match(self.fp.read(512))

if m:
if not m:
raise SyntaxError("not a XBM file")

xsize = int(m.group("width"))
ysize = int(m.group("height"))
xsize = int(m.group("width"))
ysize = int(m.group("height"))

if m.group("hotspot"):
self.info["hotspot"] = (int(m.group("xhot")), int(m.group("yhot")))
if m.group("hotspot"):
self.info["hotspot"] = (int(m.group("xhot")), int(m.group("yhot")))

self.mode = "1"
self._size = xsize, ysize
self.mode = "1"
self._size = xsize, ysize

self.tile = [("xbm", (0, 0) + self.size, m.end(), None)]
self.tile = [("xbm", (0, 0) + self.size, m.end(), None)]


def _save(im, fp, filename):
Expand Down

0 comments on commit 180c89f

Please sign in to comment.