blob: 0c25c71ff2fdab4296ffd57f373e258544656e81 [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
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080032bool BzipExtentWriter::Init(FileDescriptorPtr fd,
Amin Hassanicd7edbe2017-09-18 17:05:02 -070033 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
42 return next_->Init(fd, extents, block_size);
43}
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()) {
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
Andrew de los Reyes80061062010-02-04 14:25:00 -080089} // namespace chromeos_update_engine