Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 1 | // 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 | |
| 7 | using std::vector; |
| 8 | |
| 9 | namespace chromeos_update_engine { |
| 10 | |
| 11 | namespace { |
Darin Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 12 | const vector<char>::size_type kOutputBufferLength = 16 * 1024; |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | bool BzipExtentWriter::Init(int fd, |
| 16 | const vector<Extent>& extents, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 17 | uint32_t block_size) { |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 18 | // Init bzip2 stream |
| 19 | int rc = BZ2_bzDecompressInit(&stream_, |
| 20 | 0, // verbosity. (0 == silent) |
| 21 | 0 // 0 = faster algo, more memory |
| 22 | ); |
| 23 | TEST_AND_RETURN_FALSE(rc == BZ_OK); |
| 24 | |
| 25 | return next_->Init(fd, extents, block_size); |
| 26 | } |
| 27 | |
| 28 | bool BzipExtentWriter::Write(const void* bytes, size_t count) { |
| 29 | vector<char> output_buffer(kOutputBufferLength); |
| 30 | |
Darin Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 31 | // 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. |
| 34 | const char* input = reinterpret_cast<const char*>(bytes); |
| 35 | const char* input_end = input + count; |
| 36 | if (!input_buffer_.empty()) { |
| 37 | input_buffer_.insert(input_buffer_.end(), input, input_end); |
| 38 | input = &input_buffer_[0]; |
| 39 | input_end = input + input_buffer_.size(); |
| 40 | } |
| 41 | stream_.next_in = const_cast<char*>(input); |
| 42 | stream_.avail_in = input_end - input; |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 43 | |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 44 | for (;;) { |
| 45 | stream_.next_out = &output_buffer[0]; |
| 46 | 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 Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 50 | |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 51 | if (stream_.avail_out == output_buffer.size()) |
| 52 | break; // got no new bytes |
Darin Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 53 | |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 54 | TEST_AND_RETURN_FALSE( |
| 55 | next_->Write(&output_buffer[0], |
| 56 | output_buffer.size() - stream_.avail_out)); |
Darin Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 57 | |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 58 | if (rc == BZ_STREAM_END) |
Mike Frysinger | 0f9547d | 2012-02-16 12:11:37 -0500 | [diff] [blame] | 59 | CHECK_EQ(stream_.avail_in, static_cast<unsigned int>(0)); |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 60 | if (stream_.avail_in == 0) |
| 61 | break; // no more input to process |
| 62 | } |
| 63 | |
Darin Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 64 | // Store unconsumed data (if any) in |input_buffer_|. |
| 65 | if (stream_.avail_in || !input_buffer_.empty()) { |
| 66 | vector<char> new_input_buffer(input_end - stream_.avail_in, input_end); |
| 67 | new_input_buffer.swap(input_buffer_); |
| 68 | } |
| 69 | |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 70 | return true; |
| 71 | } |
| 72 | |
| 73 | bool 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 |