rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [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 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_DECOMPRESSING_FILE_WRITER_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_DECOMPRESSING_FILE_WRITER_H__ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | |
| 8 | #include <zlib.h> |
| 9 | #include "base/basictypes.h" |
| 10 | #include "update_engine/file_writer.h" |
| 11 | |
| 12 | // GzipDecompressingFileWriter is an implementation of FileWriter. It will |
| 13 | // gzip decompress all data that is passed via Write() onto another FileWriter, |
| 14 | // which is responsible for actually writing the data out. Calls to |
| 15 | // Open and Close are passed through to the other FileWriter. |
| 16 | |
| 17 | namespace chromeos_update_engine { |
| 18 | |
| 19 | class GzipDecompressingFileWriter : public FileWriter { |
| 20 | public: |
| 21 | explicit GzipDecompressingFileWriter(FileWriter* next) : next_(next) { |
| 22 | memset(&stream_, 0, sizeof(stream_)); |
| 23 | CHECK_EQ(inflateInit2(&stream_, 16 + MAX_WBITS), Z_OK); |
| 24 | } |
| 25 | virtual ~GzipDecompressingFileWriter() { |
| 26 | inflateEnd(&stream_); |
| 27 | } |
| 28 | |
| 29 | virtual int Open(const char* path, int flags, mode_t mode) { |
| 30 | return next_->Open(path, flags, mode); |
| 31 | } |
| 32 | |
| 33 | // Write() calls into zlib to decompress the bytes passed in. It will not |
| 34 | // necessarily write all the decompressed data associated with this set of |
| 35 | // passed-in compressed data during this call, however for a valid gzip |
| 36 | // stream, after the entire stream has been written to this object, |
| 37 | // the entire decompressed stream will have been written to the |
| 38 | // underlying FileWriter. |
| 39 | virtual int Write(const void* bytes, size_t count); |
| 40 | |
| 41 | virtual int Close() { |
| 42 | return next_->Close(); |
| 43 | } |
| 44 | private: |
| 45 | // The FileWriter that we write all uncompressed data to |
| 46 | FileWriter* next_; |
| 47 | |
| 48 | // The zlib state |
| 49 | z_stream stream_; |
| 50 | |
| 51 | // This is for an optimization in Write(). We can keep this buffer around |
| 52 | // in our class to avoid repeated calls to malloc(). |
| 53 | std::vector<char> buffer_; |
| 54 | |
| 55 | DISALLOW_COPY_AND_ASSIGN(GzipDecompressingFileWriter); |
| 56 | }; |
| 57 | |
| 58 | } // namespace chromeos_update_engine |
| 59 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 60 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DECOMPRESSING_FILE_WRITER_H__ |