Skip to content

Commit

Permalink
Compatibility with cstruct v4
Browse files Browse the repository at this point in the history
(DIS-2990)
  • Loading branch information
Nariman committed May 28, 2024
1 parent 9f2d7a5 commit 75c26e1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
5 changes: 2 additions & 3 deletions dissect/thumbcache/c_thumbcache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dissect.cstruct import cstruct

c_thumbcache_index_def = """
thumbcache_index_def = """
struct INDEX_HEADER_V1 {
char Signature[4]; // 0x00
uint32 Version; // 0x04
Expand Down Expand Up @@ -75,5 +75,4 @@
uint32 Unknown; // 0x28
}; // 0x2C
"""
c_thumbcache_index = cstruct()
c_thumbcache_index.load(c_thumbcache_index_def)
c_thumbcache_index = cstruct().load(thumbcache_index_def)
13 changes: 8 additions & 5 deletions dissect/thumbcache/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from datetime import datetime
from typing import BinaryIO, Iterator

from dissect.cstruct import Structure
from dissect.util import ts

from dissect.thumbcache.c_thumbcache import c_thumbcache_index
Expand All @@ -30,12 +29,12 @@ def __init__(self, fh: BinaryIO) -> None:
self._header = None

@property
def header(self) -> Structure:
def header(self) -> c_thumbcache_index.INDEX_HEADER_V1 | c_thumbcache_index.INDEX_HEADER_V2:
if self._header is None:
self._header = self._find_header(self.fh)
return self._header

def _find_header(self, fh: BinaryIO) -> Structure:
def _find_header(self, fh: BinaryIO) -> c_thumbcache_index.INDEX_HEADER_V1 | c_thumbcache_index.INDEX_HEADER_V2:
"""Searches for the header signature, and puts ``fh`` at the correct position.
From Windows 8.1 onward, the two fields seem to use a 64-bit format field
Expand Down Expand Up @@ -118,12 +117,16 @@ def __init__(self, fh: BinaryIO, type: ThumbnailType) -> None:
self._data = None

@property
def header(self) -> Structure:
def header(
self,
) -> c_thumbcache_index.VISTA_ENTRY | c_thumbcache_index.WINDOWS7_ENTRY | c_thumbcache_index.WINDOWS8_ENTRY:
if not self._header:
self._header = self._select_header()
return self._header

def _select_header(self) -> Structure:
def _select_header(
self,
) -> c_thumbcache_index.VISTA_ENTRY | c_thumbcache_index.WINDOWS7_ENTRY | c_thumbcache_index.WINDOWS8_ENTRY:
"""Selects header version according to the thumbnailtype."""
if self.type == ThumbnailType.WINDOWS_VISTA:
return c_thumbcache_index.VISTA_ENTRY(self.fh)
Expand Down
10 changes: 5 additions & 5 deletions dissect/thumbcache/thumbcache_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from typing import Any, BinaryIO

from dissect.cstruct import Structure

from dissect.thumbcache.c_thumbcache import c_thumbcache_index
from dissect.thumbcache.exceptions import (
InvalidSignatureError,
Expand Down Expand Up @@ -41,7 +39,7 @@ def __init__(self, fh: BinaryIO) -> None:
self._header = self._get_header_type(self.fh)
self._cached_entries: dict[int, ThumbcacheEntry] = {}

def _get_header_type(self, fh: BinaryIO) -> Structure:
def _get_header_type(self, fh: BinaryIO) -> c_thumbcache_index.CACHE_HEADER_VISTA | c_thumbcache_index.CACHE_HEADER:
tmp_header = c_thumbcache_index.CACHE_HEADER(fh)

if self._signature != tmp_header.Signature:
Expand All @@ -55,7 +53,7 @@ def _get_header_type(self, fh: BinaryIO) -> Structure:
return tmp_header

@property
def header(self) -> Structure:
def header(self) -> c_thumbcache_index.CACHE_HEADER_VISTA | c_thumbcache_index.CACHE_HEADER:
return self._header

@property
Expand Down Expand Up @@ -134,7 +132,9 @@ def __init__(self, fh: BinaryIO, type: ThumbnailType) -> None:

self._data = fh.read(self._header.Size - header_size)

def _get_header(self, thumbnail_type: ThumbnailType) -> type[Structure]:
def _get_header(
self, thumbnail_type: ThumbnailType
) -> type[c_thumbcache_index.CACHE_ENTRY_VISTA | c_thumbcache_index.CACHE_ENTRY]:
if thumbnail_type == ThumbnailType.WINDOWS_VISTA:
return c_thumbcache_index.CACHE_ENTRY_VISTA
else:
Expand Down
10 changes: 8 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ classifiers = [
"Topic :: Utilities",
]
dependencies = [
"dissect.cstruct>=3.0.dev,<4.0.dev",
"dissect.util>=3.0.dev,<4.0.dev",
"dissect.cstruct>=4.0.dev,<5",
"dissect.util>=3,<4",
]
dynamic = ["version"]

Expand All @@ -35,6 +35,12 @@ homepage = "https://dissect.tools"
documentation = "https://docs.dissect.tools/en/latest/projects/dissect.thumbcache"
repository = "https://github.com/fox-it/dissect.thumbcache"

[project.optional-dependencies]
dev = [
"dissect.cstruct>=4.0.dev,<5.0.dev",
"dissect.util>=3.0.dev,<4.0.dev",
]

[project.scripts]
thumbcache-extract = "dissect.thumbcache.tools.extract_images:main"
thumbcache-extract-indexed = "dissect.thumbcache.tools.extract_with_index:main"
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ minversion = 4.4.3
requires = virtualenv>=20.16.6

[testenv]
extras = dev
deps =
pytest
pytest-cov
Expand Down

0 comments on commit 75c26e1

Please sign in to comment.