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