aboutsummaryrefslogtreecommitdiffhomepage
path: root/pyecsca/sca/trace_set/pickle.py
blob: 05bc5d7e30884dfdda1e8cb3989b144e425d863c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
Provides a traceset implementation based on Python's pickle format.

This implementation of the traceset is thus very generic.
"""

import pickle
from io import BufferedIOBase, RawIOBase
from pathlib import Path
from typing import Union, BinaryIO

from public import public

from pyecsca.sca.trace_set.base import TraceSet


@public
class PickleTraceSet(TraceSet):
    """Pickle-based traceset format."""

    @classmethod
    def read(
        cls, input: Union[str, Path, bytes, BinaryIO], **kwargs
    ) -> "PickleTraceSet":
        if isinstance(input, bytes):
            return pickle.loads(input)  # pickle is OK here, skipcq: BAN-B301
        elif isinstance(input, (str, Path)):
            with open(input, "rb") as f:
                return pickle.load(f)  # pickle is OK here, skipcq: BAN-B301
        elif isinstance(input, (RawIOBase, BufferedIOBase, BinaryIO)):
            return pickle.load(input)  # pickle is OK here, skipcq: BAN-B301
        raise TypeError

    @classmethod
    def inplace(
        cls, input: Union[str, Path, bytes, BinaryIO], **kwargs
    ) -> "PickleTraceSet":
        raise NotImplementedError

    def write(self, output: Union[str, Path, BinaryIO]):
        if isinstance(output, (str, Path)):
            with open(output, "wb") as f:
                pickle.dump(self, f)
        elif isinstance(output, (RawIOBase, BufferedIOBase, BinaryIO)):
            pickle.dump(self, output)
        else:
            raise TypeError

    def __repr__(self):
        args = ", ".join([f"{key}={getattr(self, key)!r}" for key in self._keys])
        return f"PickleTraceSet(num_traces={len(self)}, {args})"