diff options
| author | kirigayakazima <46666711+kirigayakazima@users.noreply.github.com> | 2026-11-27 20:09:43 +0800 |
|---|---|---|
| committer | kirigayakazima <46666711+kirigayakazima@users.noreply.github.com> | 2026-11-27 20:09:43 +0800 |
| commit | 2689c7f5ba095d03a96df7db4d046d7efad40fe2 (patch) | |
| tree | e8dbc9ae673b2e98ffe700a4e2a7ee58fcbeff06 /tools/batch_glb.py | |
| parent | a0957dcd899e54ba7b4ab239b097516ecd60f95a (diff) | |
batch_glb: validate PNG structure/CRCs before preferring PNG over sibling DDS
Diffstat (limited to 'tools/batch_glb.py')
| -rw-r--r-- | tools/batch_glb.py | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/tools/batch_glb.py b/tools/batch_glb.py index 5a2407994..65bb6fa73 100644 --- a/tools/batch_glb.py +++ b/tools/batch_glb.py | |||
| @@ -22,9 +22,11 @@ from __future__ import annotations | |||
| 22 | import argparse | 22 | import argparse |
| 23 | import concurrent.futures | 23 | import concurrent.futures |
| 24 | import os | 24 | import os |
| 25 | import struct | ||
| 25 | import sys | 26 | import sys |
| 26 | import threading | 27 | import threading |
| 27 | import time | 28 | import time |
| 29 | import zlib | ||
| 28 | 30 | ||
| 29 | sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) | 31 | sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
| 30 | import glb_writer | 32 | import glb_writer |
| @@ -52,6 +54,50 @@ def _build_texture_index(texture_dir: str | None) -> dict[str, list[str]]: | |||
| 52 | return index | 54 | return index |
| 53 | 55 | ||
| 54 | 56 | ||
| 57 | _PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n" | ||
| 58 | |||
| 59 | |||
| 60 | def _validate_png_bytes(data: bytes) -> str | None: | ||
| 61 | if not data: | ||
| 62 | return "the PNG file is empty" | ||
| 63 | if len(data) < 33 or not data.startswith(_PNG_SIGNATURE): | ||
| 64 | return "not a valid PNG header" | ||
| 65 | pos = len(_PNG_SIGNATURE) | ||
| 66 | chunk_idx = 0 | ||
| 67 | saw_idat = False | ||
| 68 | while pos < len(data): | ||
| 69 | if pos + 12 > len(data): | ||
| 70 | return "truncated PNG chunk header" | ||
| 71 | length = struct.unpack_from(">I", data, pos)[0] | ||
| 72 | ctype = data[pos + 4:pos + 8] | ||
| 73 | chunk_end = pos + 12 + length | ||
| 74 | if chunk_end > len(data): | ||
| 75 | return f"truncated PNG {ctype.decode('ascii', 'replace')} chunk" | ||
| 76 | expected_crc = struct.unpack_from(">I", data, pos + 8 + length)[0] | ||
| 77 | actual_crc = zlib.crc32(data[pos + 4:pos + 8 + length]) & 0xFFFFFFFF | ||
| 78 | if actual_crc != expected_crc: | ||
| 79 | return f"bad CRC in PNG {ctype.decode('ascii', 'replace')} chunk" | ||
| 80 | if chunk_idx == 0: | ||
| 81 | if ctype != b"IHDR" or length != 13: | ||
| 82 | return "missing valid PNG IHDR chunk" | ||
| 83 | width, height = struct.unpack_from(">II", data, pos + 8) | ||
| 84 | if width <= 0 or height <= 0: | ||
| 85 | return "PNG IHDR has zero dimension" | ||
| 86 | elif ctype == b"IDAT": | ||
| 87 | saw_idat = True | ||
| 88 | elif ctype == b"IEND": | ||
| 89 | if length != 0: | ||
| 90 | return "non-empty PNG IEND chunk" | ||
| 91 | if chunk_end != len(data): | ||
| 92 | return "trailing data after PNG IEND" | ||
| 93 | if not saw_idat: | ||
| 94 | return "PNG has no IDAT chunk" | ||
| 95 | return None | ||
| 96 | pos = chunk_end | ||
| 97 | chunk_idx += 1 | ||
| 98 | return "missing PNG IEND chunk" | ||
| 99 | |||
| 100 | |||
| 55 | def _load_png_bytes(tex_path: str, max_size: int = 0) -> tuple[bytes | None, str]: | 101 | def _load_png_bytes(tex_path: str, max_size: int = 0) -> tuple[bytes | None, str]: |
| 56 | """(PNG bytes from file or DDS decode, or None and why it cannot be embedded: a texture the model | 102 | """(PNG bytes from file or DDS decode, or None and why it cannot be embedded: a texture the model |
| 57 | can live without is skipped and reported, never a silent hole in the output).""" | 103 | can live without is skipped and reported, never a silent hole in the output).""" |
| @@ -62,8 +108,9 @@ def _load_png_bytes(tex_path: str, max_size: int = 0) -> tuple[bytes | None, str | |||
| 62 | data = f.read() | 108 | data = f.read() |
| 63 | except OSError as exc: | 109 | except OSError as exc: |
| 64 | return None, str(exc) | 110 | return None, str(exc) |
| 65 | if not data: | 111 | err = _validate_png_bytes(data) |
| 66 | return None, "the PNG file is empty" | 112 | if err: |
| 113 | return None, err | ||
| 67 | return data, "" | 114 | return data, "" |
| 68 | elif lower.endswith(".dds"): | 115 | elif lower.endswith(".dds"): |
| 69 | try: | 116 | try: |