Alex Deymo | 2e71f90 | 2015-09-30 01:25:48 -0700 | [diff] [blame] | 1 | // |
| 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 Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_consumer/xz_extent_writer.h" |
Alex Deymo | 2e71f90 | 2015-09-30 01:25:48 -0700 | [diff] [blame] | 18 | |
Amin Hassani | cd7edbe | 2017-09-18 17:05:02 -0700 | [diff] [blame] | 19 | using google::protobuf::RepeatedPtrField; |
Alex Deymo | 2e71f90 | 2015-09-30 01:25:48 -0700 | [diff] [blame] | 20 | |
| 21 | namespace chromeos_update_engine { |
| 22 | |
| 23 | namespace { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 24 | const brillo::Blob::size_type kOutputBufferLength = 16 * 1024; |
Alex Deymo | 2e71f90 | 2015-09-30 01:25:48 -0700 | [diff] [blame] | 25 | |
| 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. |
| 32 | const uint32_t kXzMaxDictSize = 64 * 1024 * 1024; |
| 33 | |
| 34 | const char* XzErrorString(enum xz_ret error) { |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 35 | #define __XZ_ERROR_STRING_CASE(code) \ |
| 36 | case code: \ |
| 37 | return #code; |
Alex Deymo | 2e71f90 | 2015-09-30 01:25:48 -0700 | [diff] [blame] | 38 | switch (error) { |
| 39 | __XZ_ERROR_STRING_CASE(XZ_OK) |
| 40 | __XZ_ERROR_STRING_CASE(XZ_STREAM_END) |
| 41 | __XZ_ERROR_STRING_CASE(XZ_UNSUPPORTED_CHECK) |
| 42 | __XZ_ERROR_STRING_CASE(XZ_MEM_ERROR) |
| 43 | __XZ_ERROR_STRING_CASE(XZ_MEMLIMIT_ERROR) |
| 44 | __XZ_ERROR_STRING_CASE(XZ_FORMAT_ERROR) |
| 45 | __XZ_ERROR_STRING_CASE(XZ_OPTIONS_ERROR) |
| 46 | __XZ_ERROR_STRING_CASE(XZ_DATA_ERROR) |
| 47 | __XZ_ERROR_STRING_CASE(XZ_BUF_ERROR) |
| 48 | default: |
| 49 | return "<unknown xz error>"; |
| 50 | } |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame] | 51 | #undef __XZ_ERROR_STRING_CASE |
Amin Hassani | cd7edbe | 2017-09-18 17:05:02 -0700 | [diff] [blame] | 52 | } |
Alex Deymo | 2e71f90 | 2015-09-30 01:25:48 -0700 | [diff] [blame] | 53 | } // namespace |
| 54 | |
| 55 | XzExtentWriter::~XzExtentWriter() { |
Kelvin Zhang | cc04de7 | 2021-10-05 14:43:02 -0700 | [diff] [blame^] | 56 | stream_.reset(); |
Sen Jiang | 5e1af98 | 2018-11-01 15:01:45 -0700 | [diff] [blame] | 57 | TEST_AND_RETURN(input_buffer_.empty()); |
Alex Deymo | 2e71f90 | 2015-09-30 01:25:48 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Kelvin Zhang | 4d22ca2 | 2021-02-09 14:06:25 -0500 | [diff] [blame] | 60 | bool XzExtentWriter::Init(const RepeatedPtrField<Extent>& extents, |
Alex Deymo | 2e71f90 | 2015-09-30 01:25:48 -0700 | [diff] [blame] | 61 | uint32_t block_size) { |
Kelvin Zhang | cc04de7 | 2021-10-05 14:43:02 -0700 | [diff] [blame^] | 62 | stream_.reset(xz_dec_init(XZ_DYNALLOC, kXzMaxDictSize)); |
Alex Deymo | 2e71f90 | 2015-09-30 01:25:48 -0700 | [diff] [blame] | 63 | TEST_AND_RETURN_FALSE(stream_ != nullptr); |
Kelvin Zhang | 4d22ca2 | 2021-02-09 14:06:25 -0500 | [diff] [blame] | 64 | return underlying_writer_->Init(extents, block_size); |
Alex Deymo | 2e71f90 | 2015-09-30 01:25:48 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | bool XzExtentWriter::Write(const void* bytes, size_t count) { |
| 68 | // Copy the input data into |input_buffer_| only if |input_buffer_| already |
| 69 | // contains unconsumed data. Otherwise, process the data directly from the |
| 70 | // source. |
| 71 | const uint8_t* input = reinterpret_cast<const uint8_t*>(bytes); |
| 72 | if (!input_buffer_.empty()) { |
| 73 | input_buffer_.insert(input_buffer_.end(), input, input + count); |
| 74 | input = input_buffer_.data(); |
| 75 | count = input_buffer_.size(); |
| 76 | } |
| 77 | |
| 78 | xz_buf request; |
| 79 | request.in = input; |
| 80 | request.in_pos = 0; |
| 81 | request.in_size = count; |
| 82 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 83 | brillo::Blob output_buffer(kOutputBufferLength); |
Alex Deymo | 2e71f90 | 2015-09-30 01:25:48 -0700 | [diff] [blame] | 84 | request.out = output_buffer.data(); |
| 85 | request.out_size = output_buffer.size(); |
| 86 | for (;;) { |
| 87 | request.out_pos = 0; |
| 88 | |
Kelvin Zhang | cc04de7 | 2021-10-05 14:43:02 -0700 | [diff] [blame^] | 89 | xz_ret ret = xz_dec_run(stream_.get(), &request); |
Alex Deymo | 2e71f90 | 2015-09-30 01:25:48 -0700 | [diff] [blame] | 90 | if (ret != XZ_OK && ret != XZ_STREAM_END) { |
| 91 | LOG(ERROR) << "xz_dec_run returned " << XzErrorString(ret); |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | if (request.out_pos == 0) |
| 96 | break; |
| 97 | |
| 98 | TEST_AND_RETURN_FALSE( |
| 99 | underlying_writer_->Write(output_buffer.data(), request.out_pos)); |
| 100 | if (ret == XZ_STREAM_END) |
| 101 | CHECK_EQ(request.in_size, request.in_pos); |
| 102 | if (request.in_size == request.in_pos) |
| 103 | break; // No more input to process. |
| 104 | } |
| 105 | output_buffer.clear(); |
| 106 | |
| 107 | // Store unconsumed data (if any) in |input_buffer_|. Since |input| can point |
| 108 | // to the existing |input_buffer_| we create a new one before assigning it. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 109 | brillo::Blob new_input_buffer(request.in + request.in_pos, |
| 110 | request.in + request.in_size); |
Alex Deymo | 2e71f90 | 2015-09-30 01:25:48 -0700 | [diff] [blame] | 111 | input_buffer_ = std::move(new_input_buffer); |
| 112 | return true; |
| 113 | } |
| 114 | |
Alex Deymo | 2e71f90 | 2015-09-30 01:25:48 -0700 | [diff] [blame] | 115 | } // namespace chromeos_update_engine |