Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2016 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | #include "update_engine/payload_generator/xz.h" |
| 18 | |
Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | |
Sen Jiang | 5288bd1 | 2018-05-03 11:39:04 -0700 | [diff] [blame] | 21 | #include <7zCrc.h> |
| 22 | #include <Xz.h> |
| 23 | #include <XzEnc.h> |
Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 24 | #include <base/logging.h> |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | bool xz_initialized = false; |
| 29 | |
| 30 | // An ISeqInStream implementation that reads all the data from the passed Blob. |
| 31 | struct BlobReaderStream : public ISeqInStream { |
| 32 | explicit BlobReaderStream(const brillo::Blob& data) : data_(data) { |
| 33 | Read = &BlobReaderStream::ReadStatic; |
| 34 | } |
| 35 | |
Sen Jiang | 654ce96 | 2018-05-03 13:42:06 -0700 | [diff] [blame] | 36 | static SRes ReadStatic(const ISeqInStream* p, void* buf, size_t* size) { |
| 37 | auto* self = static_cast<BlobReaderStream*>(const_cast<ISeqInStream*>(p)); |
Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 38 | *size = std::min(*size, self->data_.size() - self->pos_); |
| 39 | memcpy(buf, self->data_.data() + self->pos_, *size); |
| 40 | self->pos_ += *size; |
| 41 | return SZ_OK; |
| 42 | } |
| 43 | |
| 44 | const brillo::Blob& data_; |
| 45 | |
| 46 | // The current reader position. |
| 47 | size_t pos_ = 0; |
| 48 | }; |
| 49 | |
| 50 | // An ISeqOutStream implementation that writes all the data to the passed Blob. |
| 51 | struct BlobWriterStream : public ISeqOutStream { |
| 52 | explicit BlobWriterStream(brillo::Blob* data) : data_(data) { |
| 53 | Write = &BlobWriterStream::WriteStatic; |
| 54 | } |
| 55 | |
Sen Jiang | 654ce96 | 2018-05-03 13:42:06 -0700 | [diff] [blame] | 56 | static size_t WriteStatic(const ISeqOutStream* p, |
| 57 | const void* buf, |
| 58 | size_t size) { |
| 59 | auto* self = static_cast<const BlobWriterStream*>(p); |
Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 60 | const uint8_t* buffer = reinterpret_cast<const uint8_t*>(buf); |
Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 61 | self->data_->insert(self->data_->end(), buffer, buffer + size); |
| 62 | return size; |
| 63 | } |
| 64 | |
| 65 | brillo::Blob* data_; |
| 66 | }; |
| 67 | |
| 68 | } // namespace |
| 69 | |
| 70 | namespace chromeos_update_engine { |
| 71 | |
| 72 | void XzCompressInit() { |
| 73 | if (xz_initialized) |
| 74 | return; |
| 75 | xz_initialized = true; |
| 76 | // Although we don't include a CRC32 for the stream, the xz file header has |
| 77 | // a CRC32 of the header itself, which required the CRC table to be |
| 78 | // initialized. |
| 79 | CrcGenerateTable(); |
| 80 | } |
| 81 | |
| 82 | bool XzCompress(const brillo::Blob& in, brillo::Blob* out) { |
| 83 | CHECK(xz_initialized) << "Initialize XzCompress first"; |
| 84 | out->clear(); |
| 85 | if (in.empty()) |
| 86 | return true; |
| 87 | |
| 88 | // Xz compression properties. |
| 89 | CXzProps props; |
| 90 | XzProps_Init(&props); |
| 91 | // No checksum in the xz stream. xz-embedded (used by the decompressor) only |
| 92 | // supports CRC32, but we already check the sha-1 of the whole blob during |
| 93 | // payload application. |
| 94 | props.checkId = XZ_CHECK_NO; |
| 95 | |
| 96 | // LZMA2 compression properties. |
| 97 | CLzma2EncProps lzma2Props; |
Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 98 | Lzma2EncProps_Init(&lzma2Props); |
| 99 | // LZMA compression "level 6" requires 9 MB of RAM to decompress in the worst |
| 100 | // case. |
| 101 | lzma2Props.lzmaProps.level = 6; |
| 102 | lzma2Props.lzmaProps.numThreads = 1; |
| 103 | // The input size data is used to reduce the dictionary size if possible. |
| 104 | lzma2Props.lzmaProps.reduceSize = in.size(); |
| 105 | Lzma2EncProps_Normalize(&lzma2Props); |
Sen Jiang | 654ce96 | 2018-05-03 13:42:06 -0700 | [diff] [blame] | 106 | props.lzma2Props = lzma2Props; |
Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 107 | |
Elliott Hughes | ecbd6b0 | 2024-04-12 17:49:01 +0000 | [diff] [blame] | 108 | // We do not use xz's BCJ filters (http://b/329112384). |
| 109 | props.filterProps.id = 0; |
Sen Jiang | 5288bd1 | 2018-05-03 11:39:04 -0700 | [diff] [blame] | 110 | |
Alex Deymo | 246bf21 | 2016-03-22 19:27:33 -0700 | [diff] [blame] | 111 | BlobWriterStream out_writer(out); |
| 112 | BlobReaderStream in_reader(in); |
| 113 | SRes res = Xz_Encode(&out_writer, &in_reader, &props, nullptr /* progress */); |
| 114 | return res == SZ_OK; |
| 115 | } |
| 116 | |
| 117 | } // namespace chromeos_update_engine |