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 //
16
17 #include "update_engine/payload_consumer/bzip_extent_writer.h"
18
19 using google::protobuf::RepeatedPtrField;
20
21 namespace chromeos_update_engine {
22
23 namespace {
24 const brillo::Blob::size_type kOutputBufferLength = 16 * 1024;
25 }
26
~BzipExtentWriter()27 BzipExtentWriter::~BzipExtentWriter() {
28 TEST_AND_RETURN(BZ2_bzDecompressEnd(&stream_) == BZ_OK);
29 TEST_AND_RETURN(input_buffer_.empty());
30 }
31
Init(FileDescriptorPtr fd,const RepeatedPtrField<Extent> & extents,uint32_t block_size)32 bool BzipExtentWriter::Init(FileDescriptorPtr fd,
33 const RepeatedPtrField<Extent>& extents,
34 uint32_t block_size) {
35 // Init bzip2 stream
36 int rc = BZ2_bzDecompressInit(&stream_,
37 0, // verbosity. (0 == silent)
38 0); // 0 = faster algo, more memory
39
40 TEST_AND_RETURN_FALSE(rc == BZ_OK);
41
42 return next_->Init(fd, extents, block_size);
43 }
44
Write(const void * bytes,size_t count)45 bool BzipExtentWriter::Write(const void* bytes, size_t count) {
46 brillo::Blob output_buffer(kOutputBufferLength);
47
48 // Copy the input data into |input_buffer_| only if |input_buffer_| already
49 // contains unconsumed data. Otherwise, process the data directly from the
50 // source.
51 const uint8_t* input = reinterpret_cast<const uint8_t*>(bytes);
52 const uint8_t* input_end = input + count;
53 if (!input_buffer_.empty()) {
54 input_buffer_.insert(input_buffer_.end(), input, input_end);
55 input = input_buffer_.data();
56 input_end = input + input_buffer_.size();
57 }
58 stream_.next_in = reinterpret_cast<char*>(const_cast<uint8_t*>(input));
59 stream_.avail_in = input_end - input;
60
61 for (;;) {
62 stream_.next_out = reinterpret_cast<char*>(output_buffer.data());
63 stream_.avail_out = output_buffer.size();
64
65 int rc = BZ2_bzDecompress(&stream_);
66 TEST_AND_RETURN_FALSE(rc == BZ_OK || rc == BZ_STREAM_END);
67
68 if (stream_.avail_out == output_buffer.size())
69 break; // got no new bytes
70
71 TEST_AND_RETURN_FALSE(next_->Write(
72 output_buffer.data(), output_buffer.size() - stream_.avail_out));
73
74 if (rc == BZ_STREAM_END)
75 CHECK_EQ(stream_.avail_in, 0u);
76 if (stream_.avail_in == 0)
77 break; // no more input to process
78 }
79
80 // Store unconsumed data (if any) in |input_buffer_|.
81 if (stream_.avail_in || !input_buffer_.empty()) {
82 brillo::Blob new_input_buffer(input_end - stream_.avail_in, input_end);
83 new_input_buffer.swap(input_buffer_);
84 }
85
86 return true;
87 }
88
89 } // namespace chromeos_update_engine
90