blob: 26fdc5f464c300eebed41f7107dbbaa9626fda82 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2009 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//
Andrew de los Reyes80061062010-02-04 14:25:00 -080016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/bzip_extent_writer.h"
Andrew de los Reyes80061062010-02-04 14:25:00 -080018
Amin Hassanicd7edbe2017-09-18 17:05:02 -070019using google::protobuf::RepeatedPtrField;
Andrew de los Reyes80061062010-02-04 14:25:00 -080020
21namespace chromeos_update_engine {
22
23namespace {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070024const brillo::Blob::size_type kOutputBufferLength = 16 * 1024;
Andrew de los Reyes80061062010-02-04 14:25:00 -080025}
26
Sen Jiangac773de2018-04-26 15:50:13 -070027BzipExtentWriter::~BzipExtentWriter() {
28 TEST_AND_RETURN(BZ2_bzDecompressEnd(&stream_) == BZ_OK);
Sen Jiang5e1af982018-11-01 15:01:45 -070029 TEST_AND_RETURN(input_buffer_.empty());
Sen Jiangac773de2018-04-26 15:50:13 -070030}
31
Kelvin Zhang4d22ca22021-02-09 14:06:25 -050032bool BzipExtentWriter::Init(const RepeatedPtrField<Extent>& extents,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070033 uint32_t block_size) {
Andrew de los Reyes80061062010-02-04 14:25:00 -080034 // Init bzip2 stream
35 int rc = BZ2_bzDecompressInit(&stream_,
Alex Vakulenkod2779df2014-06-16 13:19:00 -070036 0, // verbosity. (0 == silent)
37 0); // 0 = faster algo, more memory
38
Andrew de los Reyes80061062010-02-04 14:25:00 -080039 TEST_AND_RETURN_FALSE(rc == BZ_OK);
40
Kelvin Zhang4d22ca22021-02-09 14:06:25 -050041 return next_->Init(extents, block_size);
Andrew de los Reyes80061062010-02-04 14:25:00 -080042}
43
44bool BzipExtentWriter::Write(const void* bytes, size_t count) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070045 brillo::Blob output_buffer(kOutputBufferLength);
Andrew de los Reyes80061062010-02-04 14:25:00 -080046
Darin Petkove0622392013-04-24 12:56:19 +020047 // Copy the input data into |input_buffer_| only if |input_buffer_| already
48 // contains unconsumed data. Otherwise, process the data directly from the
49 // source.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080050 const uint8_t* input = reinterpret_cast<const uint8_t*>(bytes);
51 const uint8_t* input_end = input + count;
Darin Petkove0622392013-04-24 12:56:19 +020052 if (!input_buffer_.empty()) {
53 input_buffer_.insert(input_buffer_.end(), input, input_end);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080054 input = input_buffer_.data();
Darin Petkove0622392013-04-24 12:56:19 +020055 input_end = input + input_buffer_.size();
56 }
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080057 stream_.next_in = reinterpret_cast<char*>(const_cast<uint8_t*>(input));
Darin Petkove0622392013-04-24 12:56:19 +020058 stream_.avail_in = input_end - input;
Andrew de los Reyes80061062010-02-04 14:25:00 -080059
Andrew de los Reyes80061062010-02-04 14:25:00 -080060 for (;;) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080061 stream_.next_out = reinterpret_cast<char*>(output_buffer.data());
Andrew de los Reyes80061062010-02-04 14:25:00 -080062 stream_.avail_out = output_buffer.size();
63
64 int rc = BZ2_bzDecompress(&stream_);
65 TEST_AND_RETURN_FALSE(rc == BZ_OK || rc == BZ_STREAM_END);
Darin Petkove0622392013-04-24 12:56:19 +020066
Andrew de los Reyes80061062010-02-04 14:25:00 -080067 if (stream_.avail_out == output_buffer.size())
68 break; // got no new bytes
Darin Petkove0622392013-04-24 12:56:19 +020069
Amin Hassani008c4582019-01-13 16:22:47 -080070 TEST_AND_RETURN_FALSE(next_->Write(
71 output_buffer.data(), output_buffer.size() - stream_.avail_out));
Darin Petkove0622392013-04-24 12:56:19 +020072
Andrew de los Reyes80061062010-02-04 14:25:00 -080073 if (rc == BZ_STREAM_END)
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080074 CHECK_EQ(stream_.avail_in, 0u);
Andrew de los Reyes80061062010-02-04 14:25:00 -080075 if (stream_.avail_in == 0)
76 break; // no more input to process
77 }
78
Darin Petkove0622392013-04-24 12:56:19 +020079 // Store unconsumed data (if any) in |input_buffer_|.
80 if (stream_.avail_in || !input_buffer_.empty()) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070081 brillo::Blob new_input_buffer(input_end - stream_.avail_in, input_end);
Darin Petkove0622392013-04-24 12:56:19 +020082 new_input_buffer.swap(input_buffer_);
83 }
84
Andrew de los Reyes80061062010-02-04 14:25:00 -080085 return true;
86}
87
Andrew de los Reyes80061062010-02-04 14:25:00 -080088} // namespace chromeos_update_engine