blob: b4f864fa079adcfb8ef64648562084efa1005def [file] [log] [blame]
Andrew de los Reyes80061062010-02-04 14:25:00 -08001// 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
7using std::vector;
8
9namespace chromeos_update_engine {
10
11namespace {
Darin Petkove0622392013-04-24 12:56:19 +020012const vector<char>::size_type kOutputBufferLength = 16 * 1024;
Andrew de los Reyes80061062010-02-04 14:25:00 -080013}
14
15bool BzipExtentWriter::Init(int fd,
16 const vector<Extent>& extents,
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070017 uint32_t block_size) {
Andrew de los Reyes80061062010-02-04 14:25:00 -080018 // 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
28bool BzipExtentWriter::Write(const void* bytes, size_t count) {
29 vector<char> output_buffer(kOutputBufferLength);
30
Darin Petkove0622392013-04-24 12:56:19 +020031 // 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 Reyes80061062010-02-04 14:25:00 -080043
Andrew de los Reyes80061062010-02-04 14:25:00 -080044 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 Petkove0622392013-04-24 12:56:19 +020050
Andrew de los Reyes80061062010-02-04 14:25:00 -080051 if (stream_.avail_out == output_buffer.size())
52 break; // got no new bytes
Darin Petkove0622392013-04-24 12:56:19 +020053
Andrew de los Reyes80061062010-02-04 14:25:00 -080054 TEST_AND_RETURN_FALSE(
55 next_->Write(&output_buffer[0],
56 output_buffer.size() - stream_.avail_out));
Darin Petkove0622392013-04-24 12:56:19 +020057
Andrew de los Reyes80061062010-02-04 14:25:00 -080058 if (rc == BZ_STREAM_END)
Mike Frysinger0f9547d2012-02-16 12:11:37 -050059 CHECK_EQ(stream_.avail_in, static_cast<unsigned int>(0));
Andrew de los Reyes80061062010-02-04 14:25:00 -080060 if (stream_.avail_in == 0)
61 break; // no more input to process
62 }
63
Darin Petkove0622392013-04-24 12:56:19 +020064 // 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 Reyes80061062010-02-04 14:25:00 -080070 return true;
71}
72
73bool 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