blob: 97e2c32aff94cd24c23ba4be60466380e720895a [file] [log] [blame]
Alex Deymo246bf212016-03-22 19:27:33 -07001//
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 Deymo246bf212016-03-22 19:27:33 -070019#include <algorithm>
20
Sen Jiang5288bd12018-05-03 11:39:04 -070021#include <7zCrc.h>
22#include <Xz.h>
23#include <XzEnc.h>
Alex Deymo246bf212016-03-22 19:27:33 -070024#include <base/logging.h>
25
26namespace {
27
28bool xz_initialized = false;
29
30// An ISeqInStream implementation that reads all the data from the passed Blob.
31struct BlobReaderStream : public ISeqInStream {
32 explicit BlobReaderStream(const brillo::Blob& data) : data_(data) {
33 Read = &BlobReaderStream::ReadStatic;
34 }
35
Sen Jiang654ce962018-05-03 13:42:06 -070036 static SRes ReadStatic(const ISeqInStream* p, void* buf, size_t* size) {
37 auto* self = static_cast<BlobReaderStream*>(const_cast<ISeqInStream*>(p));
Alex Deymo246bf212016-03-22 19:27:33 -070038 *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.
51struct BlobWriterStream : public ISeqOutStream {
52 explicit BlobWriterStream(brillo::Blob* data) : data_(data) {
53 Write = &BlobWriterStream::WriteStatic;
54 }
55
Sen Jiang654ce962018-05-03 13:42:06 -070056 static size_t WriteStatic(const ISeqOutStream* p,
57 const void* buf,
58 size_t size) {
59 auto* self = static_cast<const BlobWriterStream*>(p);
Alex Deymo246bf212016-03-22 19:27:33 -070060 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(buf);
Alex Deymo246bf212016-03-22 19:27:33 -070061 self->data_->insert(self->data_->end(), buffer, buffer + size);
62 return size;
63 }
64
65 brillo::Blob* data_;
66};
67
68} // namespace
69
70namespace chromeos_update_engine {
71
72void 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
82bool 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 Deymo246bf212016-03-22 19:27:33 -070098 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 Jiang654ce962018-05-03 13:42:06 -0700106 props.lzma2Props = lzma2Props;
Alex Deymo246bf212016-03-22 19:27:33 -0700107
Elliott Hughesecbd6b02024-04-12 17:49:01 +0000108 // We do not use xz's BCJ filters (http://b/329112384).
109 props.filterProps.id = 0;
Sen Jiang5288bd12018-05-03 11:39:04 -0700110
Alex Deymo246bf212016-03-22 19:27:33 -0700111 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