@@ -50,7 +50,7 @@ class TestCbfs(unittest.TestCase):
cls.cbfstool = bintool.Bintool.create('cbfstool')
cls.have_cbfstool = cls.cbfstool.is_present()
- cls.have_lz4 = comp_util.HAVE_LZ4
+ cls.have_lz4 = comp_util.is_present('lz4')
@classmethod
def tearDownClass(cls):
@@ -1,69 +1,108 @@
# SPDX-License-Identifier: GPL-2.0+
# Copyright 2022 Google LLC
+# Copyright (C) 2022 Weidmüller Interface GmbH & Co. KG
+# Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
#
-"""Utilities to compress and decompress data"""
+"""Utilities to compress and decompress data
+
+This supports the following compression algorithm:
+ none
+ lz4
+ lzma
+
+Note that for lzma this uses an old version of the algorithm, not that
+provided by xz.
+
+This requires the following tools:
+ lz4
+ lzma_alone
+
+It also requires an output directory to be previously set up, by calling
+PrepareOutputDir().
+"""
import tempfile
from binman import bintool
from patman import tools
-LZ4 = bintool.Bintool.create('lz4')
-HAVE_LZ4 = LZ4.is_present()
+# Supported compression algorithms
+ALGORITHMS = ['lz4', 'lzma']
-LZMA_ALONE = bintool.Bintool.create('lzma_alone')
-HAVE_LZMA_ALONE = LZMA_ALONE.is_present()
+bintools = {}
+def _get_tool_name(algo):
+ """Get the tool name of a compression algorithm
-def compress(indata, algo):
- """Compress some data using a given algorithm
+ Args:
+ algo (str): Algorithm to use
- Note that for lzma this uses an old version of the algorithm, not that
- provided by xz.
+ Returns:
+ str: Tool name
+ """
+ names = {'lzma': 'lzma_alone'}
+ return names.get(algo, algo)
+
+def _get_tool(algo):
+ """Get the bintool object of a compression algorithm
- This requires 'lz4' and 'lzma_alone' tools. It also requires an output
- directory to be previously set up, by calling PrepareOutputDir().
+ The function creates new bintool object on demand per compression algorithm
+ and save it in a global bintools dictionary.
+
+ Args:
+ algo (str): Algorithm to use
+
+ Returns:
+ A bintool object for the compression algorithm
+ """
+ global bintools
+ name = _get_tool_name(algo)
+ tool = bintools.get(name)
+ if not tool:
+ tool = bintool.Bintool.create(name)
+ bintools[name] = tool
+ return tool
+
+def compress(indata, algo):
+ """Compress some data using a given algorithm
Args:
indata (bytes): Input data to compress
- algo (str): Algorithm to use ('none', 'lz4' or 'lzma')
+ algo (str): Algorithm to use
Returns:
bytes: Compressed data
"""
if algo == 'none':
return indata
- if algo == 'lz4':
- data = LZ4.compress(indata)
- # cbfstool uses a very old version of lzma
- elif algo == 'lzma':
- data = LZMA_ALONE.compress(indata)
- else:
+ if algo not in ALGORITHMS:
raise ValueError("Unknown algorithm '%s'" % algo)
+
+ tool = _get_tool(algo)
+ data = tool.compress(indata)
+
return data
def decompress(indata, algo):
"""Decompress some data using a given algorithm
- Note that for lzma this uses an old version of the algorithm, not that
- provided by xz.
-
- This requires 'lz4' and 'lzma_alone' tools. It also requires an output
- directory to be previously set up, by calling PrepareOutputDir().
-
Args:
indata (bytes): Input data to decompress
- algo (str): Algorithm to use ('none', 'lz4' or 'lzma')
+ algo (str): Algorithm to use
Returns:
- (bytes) Compressed data
+ bytes: Decompressed data
"""
if algo == 'none':
return indata
- if algo == 'lz4':
- data = LZ4.decompress(indata)
- elif algo == 'lzma':
- data = LZMA_ALONE.decompress(indata)
- else:
+ if algo not in ALGORITHMS:
raise ValueError("Unknown algorithm '%s'" % algo)
+
+ tool = _get_tool(algo)
+ data = tool.decompress(indata)
+
return data
+
+def is_present(algo):
+ tool = _get_tool(algo)
+ return tool.is_present()
@@ -212,7 +212,7 @@ class TestFunctional(unittest.TestCase):
TestFunctional._MakeInputFile('tee.elf',
tools.read_file(cls.ElfTestFile('elf_sections')))
- cls.have_lz4 = comp_util.HAVE_LZ4
+ cls.have_lz4 = comp_util.is_present('lz4')
@classmethod
def tearDownClass(cls):