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