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

Compatibility with cstruct v4 #21

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions dissect/ole/c_ole.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dissect import cstruct
from dissect.cstruct import cstruct

ole_def = """
typedef short OFFSET;
Expand Down Expand Up @@ -81,8 +81,7 @@
DFPROPTYPE _dptPropType; // [07CH,02] Reserved for future use. Must be zero.
};
"""
c_ole = cstruct.cstruct()
c_ole.load(ole_def)
c_ole = cstruct().load(ole_def)

SIGNATURE = b"\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1"
SIGNATURE_BETA = b"\x0e\x11\xfc\x0d\xd0\xcf\x11\xe0"
Expand Down
46 changes: 27 additions & 19 deletions dissect/ole/ole.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def __init__(self, fh):
self._difatcache = {}
self._chaincache = {}
self._minichaincache = {}
self._dirlist = {}

minifat_buf = self.chain(self.header._sectMiniFatStart).open().read()
self._minifat = c_ole.uint32[len(minifat_buf) // 4](minifat_buf)
Expand All @@ -38,21 +37,24 @@ def __init__(self, fh):
self.root = self.directory(0)
self.ministream = self.root.open()

def get(self, name):
dirlist = self.listdir()
try:
return dirlist[name]
except KeyError:
return NotFoundError(name)
def get(self, path, root=None):
root = root or self.root

def listdir(self):
if not self._dirlist:
for entry in self.root.walk():
self._dirlist[entry.name] = entry
search_path = path.replace("\\", "/")
node = root

return self._dirlist
for part in search_path.split("/"):
if not part:
continue

for child in node.walk():
if child.name == part:
node = child
break
else:
raise NotFoundError(path)

dirlist = listdir
return node

def directory(self, sid):
try:
Expand Down Expand Up @@ -148,29 +150,35 @@ def __init__(self, ole, sid):
else:
self.chain = ole.chain(self.start, self.size)

self._dirlist = {}

def __repr__(self):
return "<DirectoryEntry sid={} name={} type={} size=0x{:x}>".format(self.sid, self.name, self.type, self.size)

def open(self):
return self.chain.open()

def listdir(self):
if not self._dirlist:
for entry in self.walk():
self._dirlist[entry.name] = entry

return self._dirlist

def walk(self):
if self.has_left_sibling:
yield self.left_sibling
for child in self.left_sibling.walk():
yield child
yield from self.left_sibling.walk()

if self.has_child:
yield self.child

if self.has_right_sibling:
yield self.right_sibling
for child in self.right_sibling.walk():
yield child
yield from self.right_sibling.walk()

if self.has_child:
for child in self.child.walk():
yield child
yield from self.child.walk()

@property
def child(self):
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",
pyrco marked this conversation as resolved.
Show resolved Hide resolved
"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