From cd22b9aee3187236447787ebb181840df7f3b0c0 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sat, 17 Aug 2024 10:15:25 +0200 Subject: [PATCH] pySim.esim.saip.File: move away from stream for file content Let's linearize the file content in a bytes member variable self.body. Change-Id: I6cb23a3a644854abd3dfd3b50b586ce80da21353 --- pySim/esim/saip/__init__.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/pySim/esim/saip/__init__.py b/pySim/esim/saip/__init__.py index 20e2cb4e..27327b0d 100644 --- a/pySim/esim/saip/__init__.py +++ b/pySim/esim/saip/__init__.py @@ -97,7 +97,7 @@ class File: self.pe_name = pename self.template = template self.fileDescriptor = {} - self.stream = None + self.body = None # apply some defaults from profile if self.template: self.from_template(self.template) @@ -173,7 +173,7 @@ class File: raise ValueError("No fileDescriptor found in tuple, and none set by template before") if fd: self.fileDescriptor.update(dict(fd)) - self.stream = self.linearize_file_content(l) + self.body = self.linearize_file_content(l) def from_gfm(self, d: Dict): print(d) @@ -184,7 +184,7 @@ class File: raise NotImplementedError @staticmethod - def linearize_file_content(l: List[Tuple]) -> Optional[io.BytesIO]: + def linearize_file_content(l: List[Tuple]) -> Optional[bytes]: """linearize a list of fillFileContent / fillFileOffset tuples into a stream of bytes.""" stream = io.BytesIO() for k, v in l: @@ -199,7 +199,14 @@ class File: stream.write(v) else: return ValueError("Unknown key '%s' in tuple list" % k) - return stream + return stream.getvalue() + + def file_content_to_tuples(self) -> List[Tuple]: + # FIXME: simplistic approach. needs optimization. We should first check if the content + # matches the expanded default value from the template. If it does, return empty list. + # Next, we should compute the diff between the default value and self.body, and encode + # that as a sequence of fillFileOffset and fillFileContent tuples. + return [('fillFileContent', self.body)] def __str__(self) -> str: return "File(%s)" % self.pe_name @@ -819,7 +826,7 @@ class ProfileElementUSIM(FsProfileElement): @property def imsi(self) -> Optional[str]: f = File('ef-imsi', self.decoded['ef-imsi']) - return dec_imsi(b2h(f.stream.getvalue())) + return dec_imsi(b2h(f.body)) class ProfileElementOptUSIM(FsProfileElement): type = 'opt-usim'