blob: cd8e2946a870dac432c39406675ba179f1241d5e [file] [log] [blame]
rspangler@google.com49fdf182009-10-10 00:57:34 +00001// 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.comc98a7ed2009-12-04 18:54:03 +00005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_DECOMPRESSING_FILE_WRITER_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_DECOMPRESSING_FILE_WRITER_H__
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
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
17namespace chromeos_update_engine {
18
19class 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.
Andrew de los Reyes0cca4212010-04-29 14:00:58 -070039 virtual ssize_t Write(const void* bytes, size_t count);
rspangler@google.com49fdf182009-10-10 00:57:34 +000040
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.comc98a7ed2009-12-04 18:54:03 +000060#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_DECOMPRESSING_FILE_WRITER_H__