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
19 #include <algorithm>
20
21 #include <android-base/logging.h>
22 #include <7zCrc.h>
23 #include <Xz.h>
24 #include <XzEnc.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 {
BlobReaderStream__anonbbc57b750111::BlobReaderStream32 explicit BlobReaderStream(const brillo::Blob& data) : data_(data) {
33 Read = &BlobReaderStream::ReadStatic;
34 }
35
ReadStatic__anonbbc57b750111::BlobReaderStream36 static SRes ReadStatic(const ISeqInStream* p, void* buf, size_t* size) {
37 auto* self = static_cast<BlobReaderStream*>(const_cast<ISeqInStream*>(p));
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 {
BlobWriterStream__anonbbc57b750111::BlobWriterStream52 explicit BlobWriterStream(brillo::Blob* data) : data_(data) {
53 Write = &BlobWriterStream::WriteStatic;
54 }
55
WriteStatic__anonbbc57b750111::BlobWriterStream56 static size_t WriteStatic(const ISeqOutStream* p,
57 const void* buf,
58 size_t size) {
59 auto* self = static_cast<const BlobWriterStream*>(p);
60 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(buf);
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
XzCompressInit()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
XzCompress(const brillo::Blob & in,brillo::Blob * out)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;
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);
106 props.lzma2Props = lzma2Props;
107
108 // We do not use xz's BCJ filters (http://b/329112384).
109 props.filterProps.id = 0;
110
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
118