Skip to content

Python development

Joachim Metz edited this page Jul 15, 2022 · 1 revision

libsmraw comes with Python-bindings named pysmraw.

Below are examples how use pysmraw. They assume you have a working version of pysmraw on your system. To build pysmraw see Building.

Import

To be able to use pysmraw in your Python scripts add the following import:

import pysmraw

Get version

The get_version() module function can be used to retrieve the version of the pysmraw.

pysmraw.get_version()

This will return a textual string (Unicode) that contains the libsmraw version. Since pysmraw is a wrapper around libsmraw it does not have a separate version.

Open handle

smraw_handle = pysmraw.handle()

smraw_handle.open(["image.raw"])

smraw_handle.close()

The explicit call to smraw_handle.close() is not required.

smraw_handle.open() requires a list of all the necessary filenames. You can use the pysmraw.glob() to obtain a list of available filenames from "image.raw", e.g.

filenames = pysmraw.glob("image.raw")

smraw_handle.open(filenames)

Open handle using a file-like object

file_object = open("image.raw", "rb")

smraw_handle = pysmraw.handle()

smraw_handle.open_file_objects([file_object])

smraw_handle.close()

The explicit call to smraw_handle.close() is not required.

smraw_handle.open_file_objects() requires a list of all the necessary file objects.

Examples

Combining pysmraw with pytsk3

The following additional import is required:

import pytsk3
class smraw_Img_Info(pytsk3.Img_Info):
  def __init__(self, smraw_handle):
    self._smraw_handle = smraw_handle
    super(smraw_Img_Info, self).__init__(
        url="", type=pytsk3.TSK_IMG_TYPE_EXTERNAL)

  def close(self):
    self._smraw_handle.close()

  def read(self, offset, size):
    self._smraw_handle.seek(offset)
    return self._smraw_handle.read(size)

  def get_size(self):
    return self._smraw_handle.get_media_size()


filenames = pysmraw.glob("image.raw")

smraw_handle = pysmraw.handle()

smraw_handle.open(filenames)

img_info = smraw_Img_Info(smraw_handle)

fs_info = pytsk3.FS_Info(img_info, offset=63 * 512)

Also see

import pysmraw

help(pysmraw)
help(pysmraw.handle)