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 { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 12 | const chromeos::Blob::size_type kOutputBufferLength = 16 * 1024; |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 13 | } |
| 14 | |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 15 | bool BzipExtentWriter::Init(FileDescriptorPtr fd, |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 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_, |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 20 | 0, // verbosity. (0 == silent) |
| 21 | 0); // 0 = faster algo, more memory |
| 22 | |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 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) { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 29 | chromeos::Blob output_buffer(kOutputBufferLength); |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 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. |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 34 | const uint8_t* input = reinterpret_cast<const uint8_t*>(bytes); |
| 35 | const uint8_t* input_end = input + count; |
Darin Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 36 | if (!input_buffer_.empty()) { |
| 37 | input_buffer_.insert(input_buffer_.end(), input, input_end); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 38 | input = input_buffer_.data(); |
Darin Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 39 | input_end = input + input_buffer_.size(); |
| 40 | } |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 41 | stream_.next_in = reinterpret_cast<char*>(const_cast<uint8_t*>(input)); |
Darin Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 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 (;;) { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 45 | stream_.next_out = reinterpret_cast<char*>(output_buffer.data()); |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 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( |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 55 | next_->Write(output_buffer.data(), |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 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) |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 59 | CHECK_EQ(stream_.avail_in, 0u); |
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()) { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 66 | chromeos::Blob new_input_buffer(input_end - stream_.avail_in, input_end); |
Darin Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 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 |