Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrected ICNS file length in header #5845

Merged
merged 1 commit into from Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion Tests/test_file_icns.py
@@ -1,8 +1,9 @@
import io
import os

import pytest

from PIL import IcnsImagePlugin, Image, features
from PIL import IcnsImagePlugin, Image, _binary, features

from .helper import assert_image_equal, assert_image_similar_tofile

Expand Down Expand Up @@ -38,6 +39,11 @@ def test_save(tmp_path):
assert reread.size == (1024, 1024)
assert reread.format == "ICNS"

file_length = os.path.getsize(temp_file)
with open(temp_file, "rb") as fp:
fp.seek(4)
assert _binary.i32be(fp.read(4)) == file_length


def test_save_append_images(tmp_path):
temp_file = str(tmp_path / "temp.icns")
Expand Down
13 changes: 9 additions & 4 deletions src/PIL/IcnsImagePlugin.py
Expand Up @@ -337,23 +337,28 @@ def _save(im, fp, filename):
entries = []
for type, size in sizes.items():
stream = size_streams[size]
entries.append({"type": type, "size": len(stream), "stream": stream})
entries.append(
{"type": type, "size": HEADERSIZE + len(stream), "stream": stream}
)

# Header
fp.write(MAGIC)
fp.write(struct.pack(">i", sum(entry["size"] for entry in entries)))
file_length = HEADERSIZE # Header
file_length += HEADERSIZE + 8 * len(entries) # TOC
file_length += sum(entry["size"] for entry in entries)
fp.write(struct.pack(">i", file_length))

# TOC
fp.write(b"TOC ")
fp.write(struct.pack(">i", HEADERSIZE + len(entries) * HEADERSIZE))
for entry in entries:
fp.write(entry["type"])
fp.write(struct.pack(">i", HEADERSIZE + entry["size"]))
fp.write(struct.pack(">i", entry["size"]))

# Data
for entry in entries:
fp.write(entry["type"])
fp.write(struct.pack(">i", HEADERSIZE + entry["size"]))
fp.write(struct.pack(">i", entry["size"]))
fp.write(entry["stream"])

if hasattr(fp, "flush"):
Expand Down