blob: 5f3afe96a9da36ed53632d1d535644b0f8641314 [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
17#include "update_engine/bzip_extent_writer.h"
18
19using std::vector;
20
21namespace chromeos_update_engine {
22
23namespace {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080024const chromeos::Blob::size_type kOutputBufferLength = 16 * 1024;
Andrew de los Reyes80061062010-02-04 14:25:00 -080025}
26
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080027bool BzipExtentWriter::Init(FileDescriptorPtr fd,
Andrew de los Reyes80061062010-02-04 14:25:00 -080028 const vector<Extent>& extents,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070029 uint32_t block_size) {
Andrew de los Reyes80061062010-02-04 14:25:00 -080030 // Init bzip2 stream
31 int rc = BZ2_bzDecompressInit(&stream_,
Alex Vakulenkod2779df2014-06-16 13:19:00 -070032 0, // verbosity. (0 == silent)
33 0); // 0 = faster algo, more memory
34
Andrew de los Reyes80061062010-02-04 14:25:00 -080035 TEST_AND_RETURN_FALSE(rc == BZ_OK);
36
37 return next_->Init(fd, extents, block_size);
38}
39
40bool BzipExtentWriter::Write(const void* bytes, size_t count) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080041 chromeos::Blob output_buffer(kOutputBufferLength);
Andrew de los Reyes80061062010-02-04 14:25:00 -080042
Darin Petkove0622392013-04-24 12:56:19 +020043 // Copy the input data into |input_buffer_| only if |input_buffer_| already
44 // contains unconsumed data. Otherwise, process the data directly from the
45 // source.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080046 const uint8_t* input = reinterpret_cast<const uint8_t*>(bytes);
47 const uint8_t* input_end = input + count;
Darin Petkove0622392013-04-24 12:56:19 +020048 if (!input_buffer_.empty()) {
49 input_buffer_.insert(input_buffer_.end(), input, input_end);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080050 input = input_buffer_.data();
Darin Petkove0622392013-04-24 12:56:19 +020051 input_end = input + input_buffer_.size();
52 }
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080053 stream_.next_in = reinterpret_cast<char*>(const_cast<uint8_t*>(input));
Darin Petkove0622392013-04-24 12:56:19 +020054 stream_.avail_in = input_end - input;
Andrew de los Reyes80061062010-02-04 14:25:00 -080055
Andrew de los Reyes80061062010-02-04 14:25:00 -080056 for (;;) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080057 stream_.next_out = reinterpret_cast<char*>(output_buffer.data());
Andrew de los Reyes80061062010-02-04 14:25:00 -080058 stream_.avail_out = output_buffer.size();
59
60 int rc = BZ2_bzDecompress(&stream_);
61 TEST_AND_RETURN_FALSE(rc == BZ_OK || rc == BZ_STREAM_END);
Darin Petkove0622392013-04-24 12:56:19 +020062
Andrew de los Reyes80061062010-02-04 14:25:00 -080063 if (stream_.avail_out == output_buffer.size())
64 break; // got no new bytes
Darin Petkove0622392013-04-24 12:56:19 +020065
Andrew de los Reyes80061062010-02-04 14:25:00 -080066 TEST_AND_RETURN_FALSE(
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080067 next_->Write(output_buffer.data(),
Andrew de los Reyes80061062010-02-04 14:25:00 -080068 output_buffer.size() - stream_.avail_out));
Darin Petkove0622392013-04-24 12:56:19 +020069
Andrew de los Reyes80061062010-02-04 14:25:00 -080070 if (rc == BZ_STREAM_END)
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080071 CHECK_EQ(stream_.avail_in, 0u);
Andrew de los Reyes80061062010-02-04 14:25:00 -080072 if (stream_.avail_in == 0)
73 break; // no more input to process
74 }
75
Darin Petkove0622392013-04-24 12:56:19 +020076 // Store unconsumed data (if any) in |input_buffer_|.
77 if (stream_.avail_in || !input_buffer_.empty()) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080078 chromeos::Blob new_input_buffer(input_end - stream_.avail_in, input_end);
Darin Petkove0622392013-04-24 12:56:19 +020079 new_input_buffer.swap(input_buffer_);
80 }
81
Andrew de los Reyes80061062010-02-04 14:25:00 -080082 return true;
83}
84
85bool BzipExtentWriter::EndImpl() {
86 TEST_AND_RETURN_FALSE(input_buffer_.empty());
87 TEST_AND_RETURN_FALSE(BZ2_bzDecompressEnd(&stream_) == BZ_OK);
88 return next_->End();
89}
90
91} // namespace chromeos_update_engine