diff --git a/suit_generator/cmd_convert.py b/suit_generator/cmd_convert.py index 144a5ff4..27415e2f 100644 --- a/suit_generator/cmd_convert.py +++ b/suit_generator/cmd_convert.py @@ -93,6 +93,8 @@ def add_arguments(parser): class KeyConverter: """Creates a C file with public key data in form of an array from a private key stored in PEM format.""" + DEFAULT_ENCODING = "utf-8" + default_array_type = "uint8_t" default_array_name = "key_buf" default_length_type = "size_t" @@ -187,7 +189,7 @@ def _validate_input_file(self): def _prepare_header(self) -> str: if self._header_file: - with open(self._header_file, "r") as fd: + with open(self._header_file, "r", encoding=self.DEFAULT_ENCODING) as fd: contents = fd.read() if len(contents) > 0: contents += KeyConverter.newline * 2 @@ -236,7 +238,7 @@ def _prepare_length_variable(self) -> str: def _prepare_footer(self) -> str: if self._footer_file: - with open(self._footer_file, "r") as fd: + with open(self._footer_file, "r", encoding=self.DEFAULT_ENCODING) as fd: contents = fd.read() if len(contents) > 0: contents = KeyConverter.newline + contents @@ -300,7 +302,7 @@ def prepare_file_contents(self): def generate_c_file(self): """Create a C file containing public key data stored as an array.""" - with open(self._output_file, "w") as fd: + with open(self._output_file, "w", encoding=self.DEFAULT_ENCODING) as fd: fd.write(self.prepare_file_contents()) diff --git a/suit_generator/input_output.py b/suit_generator/input_output.py index c6a2373a..93117271 100644 --- a/suit_generator/input_output.py +++ b/suit_generator/input_output.py @@ -40,10 +40,12 @@ class InputOutputMixin: "suit_simplified": "from_suit_file_simplified", } + DEFAULT_ENCODING = "utf-8" + @classmethod def from_json_file(cls, file_name: str) -> dict: """Read json file and return dict.""" - with open(file_name, "r") as fh: + with open(file_name, "r", encoding=cls.DEFAULT_ENCODING) as fh: data = json.load(fh) return data @@ -61,13 +63,13 @@ def parse_json_submanifests(cls, data): @classmethod def to_json_file(cls, file_name: str, data: dict, parse_hierarchy: bool, *args) -> None: """Write dict content into json file.""" - with open(file_name, "w") as fh: + with open(file_name, "w", encoding=cls.DEFAULT_ENCODING) as fh: json.dump(cls.parse_json_submanifests(data) if parse_hierarchy is True else data, fh, sort_keys=False) @classmethod def from_yaml_file(cls, file_name) -> dict: """Read yaml file and return dict.""" - with open(file_name, "r") as fh: + with open(file_name, "r", encoding=cls.DEFAULT_ENCODING) as fh: data = yaml.load(fh, Loader=yaml.SafeLoader) return data @@ -92,7 +94,7 @@ def parse_yaml_submanifests(cls, data): @classmethod def to_yaml_file(cls, file_name, data, parse_hierarchy: bool, *args) -> None: """Write dict content into yaml file.""" - with open(file_name, "w") as fh: + with open(file_name, "w", encoding=cls.DEFAULT_ENCODING) as fh: yaml.dump(cls.parse_yaml_submanifests(data) if parse_hierarchy is True else data, fh, sort_keys=False) @classmethod diff --git a/suit_generator/suit/types/common.py b/suit_generator/suit/types/common.py index 47c8f008..8a4d0188 100644 --- a/suit_generator/suit/types/common.py +++ b/suit_generator/suit/types/common.py @@ -93,7 +93,7 @@ def __init__(self, value): setattr(self, self.__class__.__name__, value) @staticmethod - def decode_cbor_length(subtype: int, data, allow_indefinite: bool = False) -> int | None: + def decode_cbor_length(subtype: int, data: bytes, allow_indefinite: bool = False) -> int | None: """Decode cbor object length. https://github.com/agronholm/cbor2/blob/master/cbor2/_decoder.py#L258-L272 @@ -102,7 +102,7 @@ def decode_cbor_length(subtype: int, data, allow_indefinite: bool = False) -> in if subtype < 24: return subtype elif subtype == 24: - return data[0][0] + return data[0] elif subtype == 25: return cast(int, struct.unpack(">H", data[:2])[0]) elif subtype == 26: