blob: bee9fcdde9b329e149eb8968ba9758bf35d0e129 [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
Kelvin Zhange47767a2023-05-16 13:00:58 -070017#include "update_engine/common/utils.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080018#include "update_engine/payload_consumer/bzip_extent_writer.h"
Andrew de los Reyes80061062010-02-04 14:25:00 -080019
Amin Hassanicd7edbe2017-09-18 17:05:02 -070020using google::protobuf::RepeatedPtrField;
Andrew de los Reyes80061062010-02-04 14:25:00 -080021
22namespace chromeos_update_engine {
23
24namespace {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070025const brillo::Blob::size_type kOutputBufferLength = 16 * 1024;
Andrew de los Reyes80061062010-02-04 14:25:00 -080026}
27
Sen Jiangac773de2018-04-26 15:50:13 -070028BzipExtentWriter::~BzipExtentWriter() {
29 TEST_AND_RETURN(BZ2_bzDecompressEnd(&stream_) == BZ_OK);
Sen Jiang5e1af982018-11-01 15:01:45 -070030 TEST_AND_RETURN(input_buffer_.empty());
Sen Jiangac773de2018-04-26 15:50:13 -070031}
32
Kelvin Zhang4d22ca22021-02-09 14:06:25 -050033bool BzipExtentWriter::Init(const RepeatedPtrField<Extent>& extents,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070034 uint32_t block_size) {
Andrew de los Reyes80061062010-02-04 14:25:00 -080035 // Init bzip2 stream
36 int rc = BZ2_bzDecompressInit(&stream_,
Alex Vakulenkod2779df2014-06-16 13:19:00 -070037 0, // verbosity. (0 == silent)
38 0); // 0 = faster algo, more memory
39
Andrew de los Reyes80061062010-02-04 14:25:00 -080040 TEST_AND_RETURN_FALSE(rc == BZ_OK);
41
Kelvin Zhang4d22ca22021-02-09 14:06:25 -050042 return next_->Init(extents, block_size);
Andrew de los Reyes80061062010-02-04 14:25:00 -080043}
44
45bool BzipExtentWriter::Write(const void* bytes, size_t count) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070046 brillo::Blob output_buffer(kOutputBufferLength);
Andrew de los Reyes80061062010-02-04 14:25:00 -080047
Darin Petkove0622392013-04-24 12:56:19 +020048 // Copy the input data into |input_buffer_| only if |input_buffer_| already
49 // contains unconsumed data. Otherwise, process the data directly from the
50 // source.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080051 const uint8_t* input = reinterpret_cast<const uint8_t*>(bytes);
52 const uint8_t* input_end = input + count;
Darin Petkove0622392013-04-24 12:56:19 +020053 if (!input_buffer_.empty()) {
54 input_buffer_.insert(input_buffer_.end(), input, input_end);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080055 input = input_buffer_.data();
Darin Petkove0622392013-04-24 12:56:19 +020056 input_end = input + input_buffer_.size();
57 }
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080058 stream_.next_in = reinterpret_cast<char*>(const_cast<uint8_t*>(input));
Darin Petkove0622392013-04-24 12:56:19 +020059 stream_.avail_in = input_end - input;
Andrew de los Reyes80061062010-02-04 14:25:00 -080060
Andrew de los Reyes80061062010-02-04 14:25:00 -080061 for (;;) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080062 stream_.next_out = reinterpret_cast<char*>(output_buffer.data());
Andrew de los Reyes80061062010-02-04 14:25:00 -080063 stream_.avail_out = output_buffer.size();
64
65 int rc = BZ2_bzDecompress(&stream_);
66 TEST_AND_RETURN_FALSE(rc == BZ_OK || rc == BZ_STREAM_END);
Darin Petkove0622392013-04-24 12:56:19 +020067
Andrew de los Reyes80061062010-02-04 14:25:00 -080068 if (stream_.avail_out == output_buffer.size())
69 break; // got no new bytes
Darin Petkove0622392013-04-24 12:56:19 +020070
Amin Hassani008c4582019-01-13 16:22:47 -080071 TEST_AND_RETURN_FALSE(next_->Write(
72 output_buffer.data(), output_buffer.size() - stream_.avail_out));
Darin Petkove0622392013-04-24 12:56:19 +020073
Andrew de los Reyes80061062010-02-04 14:25:00 -080074 if (rc == BZ_STREAM_END)
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080075 CHECK_EQ(stream_.avail_in, 0u);
Andrew de los Reyes80061062010-02-04 14:25:00 -080076 if (stream_.avail_in == 0)
77 break; // no more input to process
78 }
79
Darin Petkove0622392013-04-24 12:56:19 +020080 // Store unconsumed data (if any) in |input_buffer_|.
81 if (stream_.avail_in || !input_buffer_.empty()) {
Kelvin Zhangc5803b72021-09-02 09:06:16 -070082 input_buffer_.assign(input_end - stream_.avail_in, input_end);
Darin Petkove0622392013-04-24 12:56:19 +020083 }
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