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 | |
| 28 | Care is taken to use unique temporary files so that this function can be |
| 29 | called from multiple threads. |
| 30 | |
| 31 | Args: |
Simon Glass | dd5c14ec | 2022-01-09 20:14:04 -0700 | [diff] [blame] | 32 | indata (bytes): Input data to compress |
| 33 | algo (str): Algorithm to use ('none', 'gzip', 'lz4' or 'lzma') |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 34 | |
| 35 | Returns: |
Simon Glass | dd5c14ec | 2022-01-09 20:14:04 -0700 | [diff] [blame] | 36 | bytes: Compressed data |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 37 | """ |
| 38 | if algo == 'none': |
| 39 | return indata |
| 40 | fname = tempfile.NamedTemporaryFile(prefix='%s.comp.tmp' % algo, |
| 41 | dir=tools.GetOutputDir()).name |
| 42 | tools.WriteFile(fname, indata) |
| 43 | if algo == 'lz4': |
Simon Glass | 9203c16 | 2022-01-09 20:14:06 -0700 | [diff] [blame] | 44 | data = LZ4.compress(indata) |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 45 | # cbfstool uses a very old version of lzma |
| 46 | elif algo == 'lzma': |
Simon Glass | 84b0c22 | 2022-01-09 20:14:08 -0700 | [diff] [blame] | 47 | data = LZMA_ALONE.compress(indata) |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 48 | elif algo == 'gzip': |
| 49 | data = tools.Run('gzip', '-c', fname, binary=True) |
| 50 | else: |
| 51 | raise ValueError("Unknown algorithm '%s'" % algo) |
| 52 | if with_header: |
| 53 | hdr = struct.pack('<I', len(data)) |
| 54 | data = hdr + data |
| 55 | return data |
| 56 | |
Simon Glass | dd5c14ec | 2022-01-09 20:14:04 -0700 | [diff] [blame] | 57 | def decompress(indata, algo, with_header=True): |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 58 | """Decompress some data using a given algorithm |
| 59 | |
| 60 | Note that for lzma this uses an old version of the algorithm, not that |
| 61 | provided by xz. |
| 62 | |
| 63 | This requires 'lz4' and 'lzma_alone' tools. It also requires an output |
| 64 | directory to be previously set up, by calling PrepareOutputDir(). |
| 65 | |
| 66 | Args: |
Simon Glass | dd5c14ec | 2022-01-09 20:14:04 -0700 | [diff] [blame] | 67 | indata (bytes): Input data to decompress |
| 68 | algo (str): Algorithm to use ('none', 'gzip', 'lz4' or 'lzma') |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 69 | |
| 70 | Returns: |
Simon Glass | dd5c14ec | 2022-01-09 20:14:04 -0700 | [diff] [blame] | 71 | (bytes) Compressed data |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 72 | """ |
| 73 | if algo == 'none': |
| 74 | return indata |
| 75 | if with_header: |
| 76 | data_len = struct.unpack('<I', indata[:4])[0] |
| 77 | indata = indata[4:4 + data_len] |
| 78 | fname = tools.GetOutputFilename('%s.decomp.tmp' % algo) |
Simon Glass | dd5c14ec | 2022-01-09 20:14:04 -0700 | [diff] [blame] | 79 | tools.WriteFile(fname, indata) |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 80 | if algo == 'lz4': |
Simon Glass | 9203c16 | 2022-01-09 20:14:06 -0700 | [diff] [blame] | 81 | data = LZ4.decompress(indata) |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 82 | elif algo == 'lzma': |
Simon Glass | 84b0c22 | 2022-01-09 20:14:08 -0700 | [diff] [blame] | 83 | data = LZMA_ALONE.decompress(indata) |
Simon Glass | 3ac7d83 | 2022-01-09 20:14:03 -0700 | [diff] [blame] | 84 | elif algo == 'gzip': |
| 85 | data = tools.Run('gzip', '-cd', fname, binary=True) |
| 86 | else: |
| 87 | raise ValueError("Unknown algorithm '%s'" % algo) |
| 88 | return data |