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

fix (input/output files): use utf-8 encoding #64

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 5 additions & 3 deletions suit_generator/cmd_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())


Expand Down
10 changes: 6 additions & 4 deletions suit_generator/input_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions suit_generator/suit/types/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
Loading