Skip to content

Commit

Permalink
Add Bootsplash image support
Browse files Browse the repository at this point in the history
  • Loading branch information
Zedeldi committed Oct 17, 2024
1 parent 3d46d62 commit 1ea2fcc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
7 changes: 6 additions & 1 deletion igelfs/models/boot_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ def _flag_bits(self) -> tuple[str, str, str]:

@property
def _flag_values(self) -> tuple[int, int, int]:
"""Return tuple of integers for flag values."""
"""
Return tuple of integers for flag values.
Tuple consists of next block index, next block present, key length
as integers.
"""
return tuple(map(partial(int, base=2), self._flag_bits))

@property
Expand Down
41 changes: 40 additions & 1 deletion igelfs/models/bootsplash.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"""Data models for bootsplash structures."""

import io
from dataclasses import dataclass
from typing import ClassVar

from PIL import Image

from igelfs.constants import BOOTSPLASH_MAGIC
from igelfs.models.base import BaseDataModel
from igelfs.models.base import BaseDataGroup, BaseDataModel
from igelfs.models.collections import DataModelCollection


@dataclass
Expand Down Expand Up @@ -35,3 +39,38 @@ class Bootsplash(BaseDataModel):
offset: int
length: int
ident: bytes


@dataclass
class BootsplashExtent(BaseDataGroup):
"""
Dataclass to handle data of a bootsplash partition extent.
Extent is not a fixed size so cannot use mapping of attribute to sizes.
"""

header: BootsplashHeader
splashes: DataModelCollection[Bootsplash]
data: bytes

@classmethod
def from_bytes(cls: type["BootsplashExtent"], data: bytes) -> "BootsplashExtent":
"""Return bootsplash extent model from bytes."""
header, data = BootsplashHeader.from_bytes_with_remaining(data)
splashes = DataModelCollection()
for _ in range(header.num_splashs):
splash, data = Bootsplash.from_bytes_with_remaining(data)
splashes.append(splash)
return cls(header=header, splashes=splashes, data=data)

def _get_image_data(self) -> list[bytes]:
"""Return list of bytes for images."""
offset = self.header.get_actual_size() + self.splashes.get_actual_size()
return [
self.data[splash.offset - offset : splash.offset - offset + splash.length]
for splash in self.splashes
]

def get_images(self) -> list[Image.Image]:
"""Return list of image instances."""
return [Image.open(io.BytesIO(image)) for image in self._get_image_data()]
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ authors = [
maintainers = [
{name = "Zack Didcott"}
]
dependencies = ["rsa"]
dependencies = [
"pillow",
"rsa",
]
requires-python = ">= 3.10"

[project.optional-dependencies]
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
iniconfig==2.0.0
packaging==24.1
pillow==11.0.0
pluggy==1.5.0
pyasn1==0.6.1
pyparted==3.12.0
pytest==8.3.3
python-magic==0.4.27
rsa==4.9

0 comments on commit 1ea2fcc

Please sign in to comment.