blob: 7828589e6ca5fc5dffd156972d1fe5314acedd79 [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);
29}
30
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080031bool BzipExtentWriter::Init(FileDescriptorPtr fd,
Amin Hassanicd7edbe2017-09-18 17:05:02 -070032 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
41 return next_->Init(fd, extents, block_size);
42}
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
Andrew de los Reyes80061062010-02-04 14:25:00 -080070 TEST_AND_RETURN_FALSE(
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080071 next_->Write(output_buffer.data(),
Andrew de los Reyes80061062010-02-04 14:25:00 -080072 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()) {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070082 brillo::Blob new_input_buffer(input_end - stream_.avail_in, input_end);
Darin Petkove0622392013-04-24 12:56:19 +020083 new_input_buffer.swap(input_buffer_);
84 }
85
Andrew de los Reyes80061062010-02-04 14:25:00 -080086 return true;
87}
88
89bool BzipExtentWriter::EndImpl() {
90 TEST_AND_RETURN_FALSE(input_buffer_.empty());
Andrew de los Reyes80061062010-02-04 14:25:00 -080091 return next_->End();
92}
93
94} // namespace chromeos_update_engine