blob: 835dcf7fa844e55867d2e381cb2cb6ca6b120b06 [file] [log] [blame]
Alex Deymo2e71f902015-09-30 01:25:48 -07001//
2// Copyright (C) 2015 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
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/xz_extent_writer.h"
Alex Deymo2e71f902015-09-30 01:25:48 -070018
Amin Hassanicd7edbe2017-09-18 17:05:02 -070019using google::protobuf::RepeatedPtrField;
Alex Deymo2e71f902015-09-30 01:25:48 -070020
21namespace chromeos_update_engine {
22
23namespace {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070024const brillo::Blob::size_type kOutputBufferLength = 16 * 1024;
Alex Deymo2e71f902015-09-30 01:25:48 -070025
26// xz uses a variable dictionary size which impacts on the compression ratio
27// and is required to be reconstructed in RAM during decompression. While we
28// control the required memory from the compressor side, the decompressor allows
29// to set a limit on this dictionary size, rejecting compressed streams that
30// require more than that. "xz -9" requires up to 64 MiB, so a 64 MiB limit
31// will allow compressed streams up to -9, the maximum compression setting.
32const uint32_t kXzMaxDictSize = 64 * 1024 * 1024;
33
34const char* XzErrorString(enum xz_ret error) {
35 #define __XZ_ERROR_STRING_CASE(code) case code: return #code;
36 switch (error) {
37 __XZ_ERROR_STRING_CASE(XZ_OK)
38 __XZ_ERROR_STRING_CASE(XZ_STREAM_END)
39 __XZ_ERROR_STRING_CASE(XZ_UNSUPPORTED_CHECK)
40 __XZ_ERROR_STRING_CASE(XZ_MEM_ERROR)
41 __XZ_ERROR_STRING_CASE(XZ_MEMLIMIT_ERROR)
42 __XZ_ERROR_STRING_CASE(XZ_FORMAT_ERROR)
43 __XZ_ERROR_STRING_CASE(XZ_OPTIONS_ERROR)
44 __XZ_ERROR_STRING_CASE(XZ_DATA_ERROR)
45 __XZ_ERROR_STRING_CASE(XZ_BUF_ERROR)
46 default:
47 return "<unknown xz error>";
48 }
49 #undef __XZ_ERROR_STRING_CASE
Amin Hassanicd7edbe2017-09-18 17:05:02 -070050}
Alex Deymo2e71f902015-09-30 01:25:48 -070051} // namespace
52
53XzExtentWriter::~XzExtentWriter() {
54 xz_dec_end(stream_);
Sen Jiang5e1af982018-11-01 15:01:45 -070055 TEST_AND_RETURN(input_buffer_.empty());
Alex Deymo2e71f902015-09-30 01:25:48 -070056}
57
58bool XzExtentWriter::Init(FileDescriptorPtr fd,
Amin Hassanicd7edbe2017-09-18 17:05:02 -070059 const RepeatedPtrField<Extent>& extents,
Alex Deymo2e71f902015-09-30 01:25:48 -070060 uint32_t block_size) {
61 stream_ = xz_dec_init(XZ_DYNALLOC, kXzMaxDictSize);
62 TEST_AND_RETURN_FALSE(stream_ != nullptr);
63 return underlying_writer_->Init(fd, extents, block_size);
64}
65
66bool XzExtentWriter::Write(const void* bytes, size_t count) {
67 // Copy the input data into |input_buffer_| only if |input_buffer_| already
68 // contains unconsumed data. Otherwise, process the data directly from the
69 // source.
70 const uint8_t* input = reinterpret_cast<const uint8_t*>(bytes);
71 if (!input_buffer_.empty()) {
72 input_buffer_.insert(input_buffer_.end(), input, input + count);
73 input = input_buffer_.data();
74 count = input_buffer_.size();
75 }
76
77 xz_buf request;
78 request.in = input;
79 request.in_pos = 0;
80 request.in_size = count;
81
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070082 brillo::Blob output_buffer(kOutputBufferLength);
Alex Deymo2e71f902015-09-30 01:25:48 -070083 request.out = output_buffer.data();
84 request.out_size = output_buffer.size();
85 for (;;) {
86 request.out_pos = 0;
87
88 xz_ret ret = xz_dec_run(stream_, &request);
89 if (ret != XZ_OK && ret != XZ_STREAM_END) {
90 LOG(ERROR) << "xz_dec_run returned " << XzErrorString(ret);
91 return false;
92 }
93
94 if (request.out_pos == 0)
95 break;
96
97 TEST_AND_RETURN_FALSE(
98 underlying_writer_->Write(output_buffer.data(), request.out_pos));
99 if (ret == XZ_STREAM_END)
100 CHECK_EQ(request.in_size, request.in_pos);
101 if (request.in_size == request.in_pos)
102 break; // No more input to process.
103 }
104 output_buffer.clear();
105
106 // Store unconsumed data (if any) in |input_buffer_|. Since |input| can point
107 // to the existing |input_buffer_| we create a new one before assigning it.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700108 brillo::Blob new_input_buffer(request.in + request.in_pos,
109 request.in + request.in_size);
Alex Deymo2e71f902015-09-30 01:25:48 -0700110 input_buffer_ = std::move(new_input_buffer);
111 return true;
112}
113
Alex Deymo2e71f902015-09-30 01:25:48 -0700114} // namespace chromeos_update_engine