Line data Source code
1 : /*
2 : Zipios -- a small C++ library that provides easy access to .zip files.
3 :
4 : Copyright (C) 2000-2007 Thomas Sondergaard
5 : Copyright (C) 2015-2019 Made to Order Software Corporation
6 :
7 : This library is free software; you can redistribute it and/or
8 : modify it under the terms of the GNU Lesser General Public
9 : License as published by the Free Software Foundation; either
10 : version 2.1 of the License, or (at your option) any later version.
11 :
12 : This library is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : Lesser General Public License for more details.
16 :
17 : You should have received a copy of the GNU Lesser General Public
18 : License along with this library; if not, write to the Free Software
19 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 : */
21 :
22 : /** \file
23 : * \brief This file is the implementation of zipios::GZIPOutputStreambuf class.
24 : *
25 : * This class is an output stream filter which knows how to creates a .gz
26 : * file from the data you write to it.
27 : *
28 : * The compression makes use of the zlib library.
29 : */
30 :
31 : #include "gzipoutputstreambuf.hpp"
32 :
33 : #include "zipios/zipiosexceptions.hpp"
34 :
35 :
36 : namespace zipios
37 : {
38 :
39 : /** \class GZIPOutputStreambuf
40 : * \brief Save the output stream buffer.
41 : *
42 : * This class is used to output the data of a file in a gzip stream
43 : * including the necessary header and footer.
44 : */
45 :
46 :
47 : /** \brief Initialize a GZIPOutputStreambuf object.
48 : *
49 : * A newly constructed GZIPOutputStreambuf is ready to accept data.
50 : *
51 : * \param[in,out] outbuf The streambuf to use for output.
52 : * \param[in] compression_level The compression level to use to compress.
53 : */
54 0 : GZIPOutputStreambuf::GZIPOutputStreambuf(std::streambuf *outbuf, FileEntry::CompressionLevel compression_level)
55 0 : : DeflateOutputStreambuf(outbuf)
56 : //, m_open(false) -- auto-init
57 : {
58 0 : if(!init(compression_level))
59 : {
60 0 : throw InvalidStateException("GZIPOutputStreambuf::GZIPOutputStreambuf() failed initializing zlib.");
61 : }
62 0 : }
63 :
64 :
65 : /** \brief Ensures that the stream gets closed properly.
66 : *
67 : * This function makes sure that the stream gets closed properly
68 : * which means that the compress terminates by calling finish()
69 : * and the streams get closed.
70 : */
71 0 : GZIPOutputStreambuf::~GZIPOutputStreambuf()
72 : {
73 0 : finish();
74 0 : }
75 :
76 :
77 0 : void GZIPOutputStreambuf::setFilename(std::string const& filename)
78 : {
79 0 : m_filename = filename;
80 0 : }
81 :
82 :
83 0 : void GZIPOutputStreambuf::setComment(std::string const& comment)
84 : {
85 0 : m_comment = comment;
86 0 : }
87 :
88 :
89 : /** \brief Close the stream.
90 : *
91 : * This function ensures that the streams get closed.
92 : */
93 0 : void GZIPOutputStreambuf::close()
94 : {
95 0 : finish();
96 0 : }
97 :
98 :
99 : /** \brief Finishes the compression.
100 : *
101 : * Write whatever is still necessary and close the streams.
102 : */
103 0 : void GZIPOutputStreambuf::finish()
104 : {
105 0 : if(!m_open)
106 : {
107 0 : return;
108 : }
109 0 : m_open = false;
110 :
111 0 : closeStream();
112 0 : writeTrailer();
113 : }
114 :
115 :
116 0 : int GZIPOutputStreambuf::overflow(int c)
117 : {
118 0 : if(!m_open)
119 : {
120 0 : writeHeader();
121 0 : m_open = true;
122 : }
123 :
124 0 : return DeflateOutputStreambuf::overflow(c);
125 : }
126 :
127 :
128 0 : int GZIPOutputStreambuf::sync()
129 : {
130 0 : return DeflateOutputStreambuf::sync();
131 : }
132 :
133 :
134 0 : void GZIPOutputStreambuf::writeHeader()
135 : {
136 : unsigned char const flg(
137 0 : (m_filename.empty() ? 0x00 : 0x08)
138 0 : | (m_comment.empty() ? 0x00 : 0x10)
139 0 : );
140 :
141 : /** \TODO
142 : * We need to know of the last modification time instead of
143 : * saving all zeros for MTIME values.
144 : */
145 :
146 : /** \todo:
147 : * I am thinking that the OS should be 3 under Unices.
148 : */
149 :
150 0 : std::ostream os(m_outbuf) ;
151 0 : os << static_cast<unsigned char>(0x1f); // Magic #
152 0 : os << static_cast<unsigned char>(0x8b); // Magic #
153 0 : os << static_cast<unsigned char>(0x08); // Deflater.DEFLATED
154 0 : os << flg; // FLG
155 0 : os << static_cast<unsigned char>(0x00); // MTIME
156 0 : os << static_cast<unsigned char>(0x00); // MTIME
157 0 : os << static_cast<unsigned char>(0x00); // MTIME
158 0 : os << static_cast<unsigned char>(0x00); // MTIME
159 0 : os << static_cast<unsigned char>(0x00); // XFLG
160 0 : os << static_cast<unsigned char>(0x00); // OS
161 :
162 0 : if(!m_filename.empty())
163 : {
164 0 : os << m_filename.c_str(); // Filename
165 0 : os << static_cast<unsigned char>(0x00);
166 : }
167 :
168 0 : if(!m_comment.empty())
169 : {
170 0 : os << m_comment.c_str(); // Comment
171 0 : os << static_cast<unsigned char>(0x00);
172 : }
173 0 : }
174 :
175 :
176 0 : void GZIPOutputStreambuf::writeTrailer()
177 : {
178 : // write the CRC32 and Size at the end of the file
179 0 : writeInt(getCrc32());
180 0 : writeInt(getSize());
181 0 : }
182 :
183 :
184 0 : void GZIPOutputStreambuf::writeInt(uint32_t i)
185 : {
186 : /** \todo: add support for 64 bit files if it exists? */
187 0 : std::ostream os(m_outbuf);
188 0 : os << static_cast<unsigned char>( i & 0xFF);
189 0 : os << static_cast<unsigned char>((i >> 8) & 0xFF);
190 0 : os << static_cast<unsigned char>((i >> 16) & 0xFF);
191 0 : os << static_cast<unsigned char>((i >> 24) & 0xFF);
192 0 : }
193 :
194 :
195 3 : } // zipios namespace
196 :
197 : // Local Variables:
198 : // mode: cpp
199 : // indent-tabs-mode: nil
200 : // c-basic-offset: 4
201 : // tab-width: 4
202 : // End:
203 :
204 : // vim: ts=4 sw=4 et
|