Skip to content

Commit

Permalink
use offset for all binary input functions instead of slicing
Browse files Browse the repository at this point in the history
  • Loading branch information
homm authored and radarhere committed Dec 30, 2020
1 parent 3757b8c commit 1ff61bc
Show file tree
Hide file tree
Showing 18 changed files with 82 additions and 82 deletions.
34 changes: 17 additions & 17 deletions src/PIL/BmpImagePlugin.py
Expand Up @@ -51,7 +51,7 @@ def _accept(prefix):


def _dib_accept(prefix):
return i32(prefix[:4]) in [12, 40, 64, 108, 124]
return i32(prefix) in [12, 40, 64, 108, 124]


# =============================================================================
Expand Down Expand Up @@ -86,10 +86,10 @@ def _bitmap(self, header=0, offset=0):
# -------------------------------------------------- IBM OS/2 Bitmap v1
# ----- This format has different offsets because of width/height types
if file_info["header_size"] == 12:
file_info["width"] = i16(header_data[0:2])
file_info["height"] = i16(header_data[2:4])
file_info["planes"] = i16(header_data[4:6])
file_info["bits"] = i16(header_data[6:8])
file_info["width"] = i16(header_data, 0)
file_info["height"] = i16(header_data, 2)
file_info["planes"] = i16(header_data, 4)
file_info["bits"] = i16(header_data, 6)
file_info["compression"] = self.RAW
file_info["palette_padding"] = 3

Expand All @@ -98,22 +98,22 @@ def _bitmap(self, header=0, offset=0):
elif file_info["header_size"] in (40, 64, 108, 124):
file_info["y_flip"] = header_data[7] == 0xFF
file_info["direction"] = 1 if file_info["y_flip"] else -1
file_info["width"] = i32(header_data[0:4])
file_info["width"] = i32(header_data, 0)
file_info["height"] = (
i32(header_data[4:8])
i32(header_data, 4)
if not file_info["y_flip"]
else 2 ** 32 - i32(header_data[4:8])
else 2 ** 32 - i32(header_data, 4)
)
file_info["planes"] = i16(header_data[8:10])
file_info["bits"] = i16(header_data[10:12])
file_info["compression"] = i32(header_data[12:16])
file_info["planes"] = i16(header_data, 8)
file_info["bits"] = i16(header_data, 10)
file_info["compression"] = i32(header_data, 12)
# byte size of pixel data
file_info["data_size"] = i32(header_data[16:20])
file_info["data_size"] = i32(header_data, 16)
file_info["pixels_per_meter"] = (
i32(header_data[20:24]),
i32(header_data[24:28]),
i32(header_data, 20),
i32(header_data, 24),
)
file_info["colors"] = i32(header_data[28:32])
file_info["colors"] = i32(header_data, 28)
file_info["palette_padding"] = 4
self.info["dpi"] = tuple(
int(x / 39.3701 + 0.5) for x in file_info["pixels_per_meter"]
Expand All @@ -123,7 +123,7 @@ def _bitmap(self, header=0, offset=0):
for idx, mask in enumerate(
["r_mask", "g_mask", "b_mask", "a_mask"]
):
file_info[mask] = i32(header_data[36 + idx * 4 : 40 + idx * 4])
file_info[mask] = i32(header_data, 36 + idx * 4)
else:
# 40 byte headers only have the three components in the
# bitfields masks, ref:
Expand Down Expand Up @@ -266,7 +266,7 @@ def _open(self):
if not _accept(head_data):
raise SyntaxError("Not a BMP file")
# read the start position of the BMP image data (u32)
offset = i32(head_data[10:14])
offset = i32(head_data, 10)
# load bitmap information (offset=raster info)
self._bitmap(offset=offset)

Expand Down
4 changes: 2 additions & 2 deletions src/PIL/CurImagePlugin.py
Expand Up @@ -47,7 +47,7 @@ def _open(self):

# pick the largest cursor in the file
m = b""
for i in range(i16(s[4:])):
for i in range(i16(s, 4)):
s = self.fp.read(16)
if not m:
m = s
Expand All @@ -57,7 +57,7 @@ def _open(self):
raise TypeError("No cursors were found")

# load as bitmap
self._bitmap(i32(m[12:]) + offset)
self._bitmap(i32(m, 12) + offset)

# patch up the bitmap height
self._size = self.size[0], self.size[1] // 2
Expand Down
6 changes: 3 additions & 3 deletions src/PIL/EpsImagePlugin.py
Expand Up @@ -312,14 +312,14 @@ def _find_offset(self, fp):
fp.seek(0, io.SEEK_END)
length = fp.tell()
offset = 0
elif i32(s[0:4]) == 0xC6D3D0C5:
elif i32(s, 0) == 0xC6D3D0C5:
# FIX for: Some EPS file not handled correctly / issue #302
# EPS can contain binary data
# or start directly with latin coding
# more info see:
# https://web.archive.org/web/20160528181353/http://partners.adobe.com/public/developer/en/ps/5002.EPSF_Spec.pdf
offset = i32(s[4:8])
length = i32(s[8:12])
offset = i32(s, 4)
length = i32(s, 8)
else:
raise SyntaxError("not an EPS file")

Expand Down
20 changes: 10 additions & 10 deletions src/PIL/FliImagePlugin.py
Expand Up @@ -26,7 +26,7 @@


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


##
Expand All @@ -46,22 +46,22 @@ def _open(self):
s = self.fp.read(128)
if not (
_accept(s)
and i16(s[14:16]) in [0, 3] # flags
and i16(s, 14) in [0, 3] # flags
and s[20:22] == b"\x00\x00" # reserved
):
raise SyntaxError("not an FLI/FLC file")

# frames
self.n_frames = i16(s[6:8])
self.n_frames = i16(s, 6)
self.is_animated = self.n_frames > 1

# image characteristics
self.mode = "P"
self._size = i16(s[8:10]), i16(s[10:12])
self._size = i16(s, 8), i16(s, 10)

# animation speed
duration = i32(s[16:20])
magic = i16(s[4:6])
duration = i32(s, 16)
magic = i16(s, 4)
if magic == 0xAF11:
duration = (duration * 1000) // 70
self.info["duration"] = duration
Expand All @@ -73,17 +73,17 @@ def _open(self):

self.__offset = 128

if i16(s[4:6]) == 0xF100:
if i16(s, 4) == 0xF100:
# prefix chunk; ignore it
self.__offset = self.__offset + i32(s)
s = self.fp.read(16)

if i16(s[4:6]) == 0xF1FA:
if i16(s, 4) == 0xF1FA:
# look for palette chunk
s = self.fp.read(6)
if i16(s[4:6]) == 11:
if i16(s, 4) == 11:
self._palette(palette, 2)
elif i16(s[4:6]) == 4:
elif i16(s, 4) == 4:
self._palette(palette, 0)

palette = [o8(r) + o8(g) + o8(b) for (r, g, b) in palette]
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/GbrImagePlugin.py
Expand Up @@ -29,7 +29,7 @@


def _accept(prefix):
return len(prefix) >= 8 and i32(prefix[:4]) >= 20 and i32(prefix[4:8]) in (1, 2)
return len(prefix) >= 8 and i32(prefix, 0) >= 20 and i32(prefix, 4) in (1, 2)


##
Expand Down
6 changes: 3 additions & 3 deletions src/PIL/GdImageFile.py
Expand Up @@ -48,17 +48,17 @@ def _open(self):
# Header
s = self.fp.read(1037)

if not i16(s[:2]) in [65534, 65535]:
if not i16(s) in [65534, 65535]:
raise SyntaxError("Not a valid GD 2.x .gd file")

self.mode = "L" # FIXME: "P"
self._size = i16(s[2:4]), i16(s[4:6])
self._size = i16(s, 2), i16(s, 4)

trueColor = s[6]
trueColorOffset = 2 if trueColor else 0

# transparency index
tindex = i32(s[7 + trueColorOffset : 7 + trueColorOffset + 4])
tindex = i32(s, 7 + trueColorOffset)
if tindex < 256:
self.info["transparency"] = tindex

Expand Down
10 changes: 5 additions & 5 deletions src/PIL/GifImagePlugin.py
Expand Up @@ -69,7 +69,7 @@ def _open(self):
raise SyntaxError("not a GIF file")

self.info["version"] = s[:6]
self._size = i16(s[6:]), i16(s[8:])
self._size = i16(s, 6), i16(s, 8)
self.tile = []
flags = s[10]
bits = (flags & 7) + 1
Expand Down Expand Up @@ -193,7 +193,7 @@ def _seek(self, frame):
flags = block[0]
if flags & 1:
info["transparency"] = block[3]
info["duration"] = i16(block[1:3]) * 10
info["duration"] = i16(block, 1) * 10

# disposal method - find the value of bits 4 - 6
dispose_bits = 0b00011100 & flags
Expand Down Expand Up @@ -223,7 +223,7 @@ def _seek(self, frame):
if block[:11] == b"NETSCAPE2.0":
block = self.data()
if len(block) >= 3 and block[0] == 1:
info["loop"] = i16(block[1:3])
info["loop"] = i16(block, 1)
while self.data():
pass

Expand All @@ -234,8 +234,8 @@ def _seek(self, frame):
s = self.fp.read(9)

# extent
x0, y0 = i16(s[0:]), i16(s[2:])
x1, y1 = x0 + i16(s[4:]), y0 + i16(s[6:])
x0, y0 = i16(s, 0), i16(s, 2)
x1, y1 = x0 + i16(s, 4), y0 + i16(s, 6)
if x1 > self.size[0] or y1 > self.size[1]:
self._size = max(x1, self.size[0]), max(y1, self.size[1])
self.dispose_extent = x0, y0, x1, y1
Expand Down
10 changes: 5 additions & 5 deletions src/PIL/IcoImagePlugin.py
Expand Up @@ -102,7 +102,7 @@ def __init__(self, buf):
self.entry = []

# Number of items in file
self.nb_items = i16(s[4:])
self.nb_items = i16(s, 4)

# Get headers for each item
for i in range(self.nb_items):
Expand All @@ -113,10 +113,10 @@ def __init__(self, buf):
"height": s[1],
"nb_color": s[2], # No. of colors in image (0 if >=8bpp)
"reserved": s[3],
"planes": i16(s[4:]),
"bpp": i16(s[6:]),
"size": i32(s[8:]),
"offset": i32(s[12:]),
"planes": i16(s, 4),
"bpp": i16(s, 6),
"size": i32(s, 8),
"offset": i32(s, 12),
}

# See Wikipedia
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/Image.py
Expand Up @@ -3374,7 +3374,7 @@ def get_ifd(self, tag):

if self[0x927C][:8] == b"FUJIFILM":
exif_data = self[0x927C]
ifd_offset = i32le(exif_data[8:12])
ifd_offset = i32le(exif_data, 8)
ifd_data = exif_data[ifd_offset:]

makernote = {}
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/IptcImagePlugin.py
Expand Up @@ -77,7 +77,7 @@ def field(self):
elif size > 128:
size = i(self.fp.read(size - 128))
else:
size = i16(s[3:])
size = i16(s, 3)

return tag, size

Expand Down
10 changes: 5 additions & 5 deletions src/PIL/JpegImagePlugin.py
Expand Up @@ -124,10 +124,10 @@ def APP(self, marker):
data = s[offset : offset + size]
if code == 0x03ED: # ResolutionInfo
data = {
"XResolution": i32(data[:4]) / 65536,
"DisplayedUnitsX": i16(data[4:8]),
"YResolution": i32(data[8:12]) / 65536,
"DisplayedUnitsY": i16(data[12:]),
"XResolution": i32(data, 0) / 65536,
"DisplayedUnitsX": i16(data, 4),
"YResolution": i32(data, 8) / 65536,
"DisplayedUnitsY": i16(data, 12),
}
photoshop[code] = data
offset += size
Expand Down Expand Up @@ -194,7 +194,7 @@ def SOF(self, marker):

n = i16(self.fp.read(2)) - 2
s = ImageFile._safe_read(self.fp, n)
self._size = i16(s[3:]), i16(s[1:])
self._size = i16(s, 3), i16(s, 1)

self.bits = s[0]
if self.bits != 8:
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/MspImagePlugin.py
Expand Up @@ -58,12 +58,12 @@ def _open(self):
# Header checksum
checksum = 0
for i in range(0, 32, 2):
checksum = checksum ^ i16(s[i : i + 2])
checksum = checksum ^ i16(s, i)
if checksum != 0:
raise SyntaxError("bad MSP checksum")

self.mode = "1"
self._size = i16(s[4:]), i16(s[6:])
self._size = i16(s, 4), i16(s, 6)

if s[:4] == b"DanM":
self.tile = [("raw", (0, 0) + self.size, 32, ("1", 0, 1))]
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/PixarImagePlugin.py
Expand Up @@ -49,10 +49,10 @@ def _open(self):
# read rest of header
s = s + self.fp.read(508)

self._size = i16(s[418:420]), i16(s[416:418])
self._size = i16(s, 418), i16(s, 416)

# get channel/depth descriptions
mode = i16(s[424:426]), i16(s[426:428])
mode = i16(s, 424), i16(s, 426)

if mode == (14, 2):
self.mode = "RGB"
Expand Down
14 changes: 7 additions & 7 deletions src/PIL/PngImagePlugin.py
Expand Up @@ -409,7 +409,7 @@ def chunk_IHDR(self, pos, length):

# image header
s = ImageFile._safe_read(self.fp, length)
self.im_size = i32(s), i32(s[4:])
self.im_size = i32(s, 0), i32(s, 4)
try:
self.im_mode, self.im_rawmode = _MODES[(s[8], s[9])]
except Exception:
Expand Down Expand Up @@ -464,7 +464,7 @@ def chunk_tRNS(self, pos, length):
elif self.im_mode in ("1", "L", "I"):
self.im_info["transparency"] = i16(s)
elif self.im_mode == "RGB":
self.im_info["transparency"] = i16(s), i16(s[2:]), i16(s[4:])
self.im_info["transparency"] = i16(s), i16(s, 2), i16(s, 4)
return s

def chunk_gAMA(self, pos, length):
Expand Down Expand Up @@ -497,7 +497,7 @@ def chunk_pHYs(self, pos, length):

# pixels per unit
s = ImageFile._safe_read(self.fp, length)
px, py = i32(s), i32(s[4:])
px, py = i32(s, 0), i32(s, 4)
unit = s[8]
if unit == 1: # meter
dpi = int(px * 0.0254 + 0.5), int(py * 0.0254 + 0.5)
Expand Down Expand Up @@ -618,7 +618,7 @@ def chunk_acTL(self, pos, length):
warnings.warn("Invalid APNG, will use default PNG image if possible")
return s
self.im_n_frames = n_frames
self.im_info["loop"] = i32(s[4:])
self.im_info["loop"] = i32(s, 4)
self.im_custom_mimetype = "image/apng"
return s

Expand All @@ -630,13 +630,13 @@ def chunk_fcTL(self, pos, length):
):
raise SyntaxError("APNG contains frame sequence errors")
self._seq_num = seq
width, height = i32(s[4:]), i32(s[8:])
px, py = i32(s[12:]), i32(s[16:])
width, height = i32(s, 4), i32(s, 8)
px, py = i32(s, 12), i32(s, 16)
im_w, im_h = self.im_size
if px + width > im_w or py + height > im_h:
raise SyntaxError("APNG contains invalid frames")
self.im_info["bbox"] = (px, py, px + width, py + height)
delay_num, delay_den = i16(s[20:]), i16(s[22:])
delay_num, delay_den = i16(s, 20), i16(s, 22)
if delay_den == 0:
delay_den = 100
self.im_info["duration"] = float(delay_num) / float(delay_den) * 1000
Expand Down

0 comments on commit 1ff61bc

Please sign in to comment.