blob: 14a0f6344f16dc5e6621c28fdb30b5ee56b6e772 [file] [log] [blame]
Andrew de los Reyes80061062010-02-04 14:25:00 -08001// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/bzip_extent_writer.h"
6
7using std::vector;
8
9namespace chromeos_update_engine {
10
11namespace {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080012const chromeos::Blob::size_type kOutputBufferLength = 16 * 1024;
Andrew de los Reyes80061062010-02-04 14:25:00 -080013}
14
Nam T. Nguyenf1d582e2014-12-08 15:07:17 -080015bool BzipExtentWriter::Init(FileDescriptorPtr fd,
Andrew de los Reyes80061062010-02-04 14:25:00 -080016 const vector<Extent>& extents,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070017 uint32_t block_size) {
Andrew de los Reyes80061062010-02-04 14:25:00 -080018 // Init bzip2 stream
19 int rc = BZ2_bzDecompressInit(&stream_,
Alex Vakulenkod2779df2014-06-16 13:19:00 -070020 0, // verbosity. (0 == silent)
21 0); // 0 = faster algo, more memory
22
Andrew de los Reyes80061062010-02-04 14:25:00 -080023 TEST_AND_RETURN_FALSE(rc == BZ_OK);
24
25 return next_->Init(fd, extents, block_size);
26}
27
28bool BzipExtentWriter::Write(const void* bytes, size_t count) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080029 chromeos::Blob output_buffer(kOutputBufferLength);
Andrew de los Reyes80061062010-02-04 14:25:00 -080030
Darin Petkove0622392013-04-24 12:56:19 +020031 // Copy the input data into |input_buffer_| only if |input_buffer_| already
32 // contains unconsumed data. Otherwise, process the data directly from the
33 // source.
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080034 const uint8_t* input = reinterpret_cast<const uint8_t*>(bytes);
35 const uint8_t* input_end = input + count;
Darin Petkove0622392013-04-24 12:56:19 +020036 if (!input_buffer_.empty()) {
37 input_buffer_.insert(input_buffer_.end(), input, input_end);
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080038 input = input_buffer_.data();
Darin Petkove0622392013-04-24 12:56:19 +020039 input_end = input + input_buffer_.size();
40 }
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080041 stream_.next_in = reinterpret_cast<char*>(const_cast<uint8_t*>(input));
Darin Petkove0622392013-04-24 12:56:19 +020042 stream_.avail_in = input_end - input;
Andrew de los Reyes80061062010-02-04 14:25:00 -080043
Andrew de los Reyes80061062010-02-04 14:25:00 -080044 for (;;) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080045 stream_.next_out = reinterpret_cast<char*>(output_buffer.data());
Andrew de los Reyes80061062010-02-04 14:25:00 -080046 stream_.avail_out = output_buffer.size();
47
48 int rc = BZ2_bzDecompress(&stream_);
49 TEST_AND_RETURN_FALSE(rc == BZ_OK || rc == BZ_STREAM_END);
Darin Petkove0622392013-04-24 12:56:19 +020050
Andrew de los Reyes80061062010-02-04 14:25:00 -080051 if (stream_.avail_out == output_buffer.size())
52 break; // got no new bytes
Darin Petkove0622392013-04-24 12:56:19 +020053
Andrew de los Reyes80061062010-02-04 14:25:00 -080054 TEST_AND_RETURN_FALSE(
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080055 next_->Write(output_buffer.data(),
Andrew de los Reyes80061062010-02-04 14:25:00 -080056 output_buffer.size() - stream_.avail_out));
Darin Petkove0622392013-04-24 12:56:19 +020057
Andrew de los Reyes80061062010-02-04 14:25:00 -080058 if (rc == BZ_STREAM_END)
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080059 CHECK_EQ(stream_.avail_in, 0u);
Andrew de los Reyes80061062010-02-04 14:25:00 -080060 if (stream_.avail_in == 0)
61 break; // no more input to process
62 }
63
Darin Petkove0622392013-04-24 12:56:19 +020064 // Store unconsumed data (if any) in |input_buffer_|.
65 if (stream_.avail_in || !input_buffer_.empty()) {
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -080066 chromeos::Blob new_input_buffer(input_end - stream_.avail_in, input_end);
Darin Petkove0622392013-04-24 12:56:19 +020067 new_input_buffer.swap(input_buffer_);
68 }
69
Andrew de los Reyes80061062010-02-04 14:25:00 -080070 return true;
71}
72
73bool BzipExtentWriter::EndImpl() {
74 TEST_AND_RETURN_FALSE(input_buffer_.empty());
75 TEST_AND_RETURN_FALSE(BZ2_bzDecompressEnd(&stream_) == BZ_OK);
76 return next_->End();
77}
78
79} // namespace chromeos_update_engine