Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0+ |
| 2 | # Copyright 2022 Google LLC |
| 3 | # |
| 4 | """Utilities to compress and decompress data""" |
| 5 | |
| 6 | import struct |
| 7 | import tempfile |
| 8 | |
Simon Glass | 9203c16 | 2022-01-09 20:14:06 -0700 | [diff] [blame] | 9 | from binman import bintool |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 10 | from patman import tools |
| 11 | |
Simon Glass | 9203c16 | 2022-01-09 20:14:06 -0700 | [diff] [blame] | 12 | LZ4 = bintool.Bintool.create('lz4') |
| 13 | HAVE_LZ4 = LZ4.is_present() |
| 14 | |
Simon Glass | 84b0c22 | 2022-01-09 20:14:08 -0700 | [diff] [blame] | 15 | LZMA_ALONE = bintool.Bintool.create('lzma_alone') |
| 16 | HAVE_LZMA_ALONE = LZMA_ALONE.is_present() |
| 17 | |
Simon Glass | 9203c16 | 2022-01-09 20:14:06 -0700 | [diff] [blame] | 18 | |
Simon Glass | dd5c14ec | 2022-01-09 20:14:04 -0700 | [diff] [blame] | 19 | def compress(indata, algo, with_header=True): |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 20 | """Compress some data using a given algorithm |
| 21 | |
| 22 | Note that for lzma this uses an old version of the algorithm, not that |
| 23 | provided by xz. |
| 24 | |
| 25 | This requires 'lz4' and 'lzma_alone' tools. It also requires an output |
| 26 | directory to be previously set up, by calling PrepareOutputDir(). |
| 27 | |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 28 | Args: |
Simon Glass | dd5c14ec | 2022-01-09 20:14:04 -0700 | [diff] [blame] | 29 | indata (bytes): Input data to compress |
Simon Glass | dab7c14 | 2022-01-09 20:14:10 -0700 | [diff] [blame] | 30 | algo (str): Algorithm to use ('none', 'lz4' or 'lzma') |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 31 | |
| 32 | Returns: |
Simon Glass | dd5c14ec | 2022-01-09 20:14:04 -0700 | [diff] [blame] | 33 | bytes: Compressed data |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 34 | """ |
| 35 | if algo == 'none': |
| 36 | return indata |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 37 | if algo == 'lz4': |
Simon Glass | 9203c16 | 2022-01-09 20:14:06 -0700 | [diff] [blame] | 38 | data = LZ4.compress(indata) |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 39 | # cbfstool uses a very old version of lzma |
| 40 | elif algo == 'lzma': |
Simon Glass | 84b0c22 | 2022-01-09 20:14:08 -0700 | [diff] [blame] | 41 | data = LZMA_ALONE.compress(indata) |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 42 | else: |
| 43 | raise ValueError("Unknown algorithm '%s'" % algo) |
| 44 | if with_header: |
| 45 | hdr = struct.pack('<I', len(data)) |
| 46 | data = hdr + data |
| 47 | return data |
| 48 | |
Simon Glass | dd5c14ec | 2022-01-09 20:14:04 -0700 | [diff] [blame] | 49 | def decompress(indata, algo, with_header=True): |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 50 | """Decompress some data using a given algorithm |
| 51 | |
| 52 | Note that for lzma this uses an old version of the algorithm, not that |
| 53 | provided by xz. |
| 54 | |
| 55 | This requires 'lz4' and 'lzma_alone' tools. It also requires an output |
| 56 | directory to be previously set up, by calling PrepareOutputDir(). |
| 57 | |
| 58 | Args: |
Simon Glass | dd5c14ec | 2022-01-09 20:14:04 -0700 | [diff] [blame] | 59 | indata (bytes): Input data to decompress |
Simon Glass | dab7c14 | 2022-01-09 20:14:10 -0700 | [diff] [blame] | 60 | algo (str): Algorithm to use ('none', 'lz4' or 'lzma') |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 61 | |
| 62 | Returns: |
Simon Glass | dd5c14ec | 2022-01-09 20:14:04 -0700 | [diff] [blame] | 63 | (bytes) Compressed data |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 64 | """ |
| 65 | if algo == 'none': |
| 66 | return indata |
| 67 | if with_header: |
| 68 | data_len = struct.unpack('<I', indata[:4])[0] |
| 69 | indata = indata[4:4 + data_len] |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 70 | if algo == 'lz4': |
Simon Glass | 9203c16 | 2022-01-09 20:14:06 -0700 | [diff] [blame] | 71 | data = LZ4.decompress(indata) |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 72 | elif algo == 'lzma': |
Simon Glass | 84b0c22 | 2022-01-09 20:14:08 -0700 | [diff] [blame] | 73 | data = LZMA_ALONE.decompress(indata) |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 74 | else: |
| 75 | raise ValueError("Unknown algorithm '%s'" % algo) |
| 76 | return data |