| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 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 Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 16 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_consumer/bzip_extent_writer.h" | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 18 |  | 
|  | 19 | using std::vector; | 
|  | 20 |  | 
|  | 21 | namespace chromeos_update_engine { | 
|  | 22 |  | 
|  | 23 | namespace { | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 24 | const brillo::Blob::size_type kOutputBufferLength = 16 * 1024; | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 25 | } | 
|  | 26 |  | 
| Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 27 | bool BzipExtentWriter::Init(FileDescriptorPtr fd, | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 28 | const vector<Extent>& extents, | 
| Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 29 | uint32_t block_size) { | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 30 | // Init bzip2 stream | 
|  | 31 | int rc = BZ2_bzDecompressInit(&stream_, | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 32 | 0,   // verbosity. (0 == silent) | 
|  | 33 | 0);  // 0 = faster algo, more memory | 
|  | 34 |  | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 35 | TEST_AND_RETURN_FALSE(rc == BZ_OK); | 
|  | 36 |  | 
|  | 37 | return next_->Init(fd, extents, block_size); | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | bool BzipExtentWriter::Write(const void* bytes, size_t count) { | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 41 | brillo::Blob output_buffer(kOutputBufferLength); | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 42 |  | 
| Darin Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 43 | // 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 Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 46 | const uint8_t* input = reinterpret_cast<const uint8_t*>(bytes); | 
|  | 47 | const uint8_t* input_end = input + count; | 
| Darin Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 48 | if (!input_buffer_.empty()) { | 
|  | 49 | input_buffer_.insert(input_buffer_.end(), input, input_end); | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 50 | input = input_buffer_.data(); | 
| Darin Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 51 | input_end = input + input_buffer_.size(); | 
|  | 52 | } | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 53 | stream_.next_in = reinterpret_cast<char*>(const_cast<uint8_t*>(input)); | 
| Darin Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 54 | stream_.avail_in = input_end - input; | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 55 |  | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 56 | for (;;) { | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 57 | stream_.next_out = reinterpret_cast<char*>(output_buffer.data()); | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 58 | 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 Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 62 |  | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 63 | if (stream_.avail_out == output_buffer.size()) | 
|  | 64 | break;  // got no new bytes | 
| Darin Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 65 |  | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 66 | TEST_AND_RETURN_FALSE( | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 67 | next_->Write(output_buffer.data(), | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 68 | output_buffer.size() - stream_.avail_out)); | 
| Darin Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 69 |  | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 70 | if (rc == BZ_STREAM_END) | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 71 | CHECK_EQ(stream_.avail_in, 0u); | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 72 | if (stream_.avail_in == 0) | 
|  | 73 | break;  // no more input to process | 
|  | 74 | } | 
|  | 75 |  | 
| Darin Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 76 | // Store unconsumed data (if any) in |input_buffer_|. | 
|  | 77 | if (stream_.avail_in || !input_buffer_.empty()) { | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 78 | brillo::Blob new_input_buffer(input_end - stream_.avail_in, input_end); | 
| Darin Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 79 | new_input_buffer.swap(input_buffer_); | 
|  | 80 | } | 
|  | 81 |  | 
| Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 82 | return true; | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | bool 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 |