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 27, 2024
1 parent 9f2d7a5 commit 7735e59
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
3 changes: 1 addition & 2 deletions dissect/thumbcache/c_thumbcache.py
Original file line number Diff line number Diff line change
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(c_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
4 changes: 2 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>3,<5",
"dissect.util>2,<4",
]
dynamic = ["version"]

Expand Down
7 changes: 7 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ deps =
pytest
pytest-cov
coverage
# Unfortunately, tox does not allow separate installation flags for the project
# dependencies and the test dependencies. When running tox, we want to install the
# project dependencies with the --pre flag, so that we get the latest version of all
# dependencies. We do the installation step ourselves for this reason.
skip_install = true
commands_pre =
pip install --pre -e .
commands =
pytest --basetemp="{envtmpdir}" {posargs:--color=yes --cov=dissect --cov-report=term-missing -v tests}
coverage report
Expand Down

0 comments on commit 7735e59

Please sign in to comment.