2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
28 /* Don't compile this if we don't have zlib. */
31 __FBSDID("$FreeBSD: src/lib/libarchive/archive_write_set_compression_gzip.c,v 1.16 2008/02/21 03:21:50 kientzle Exp $");
48 #include "archive_private.h"
49 #include "archive_write_private.h"
54 unsigned char *compressed
;
55 size_t compressed_buffer_size
;
61 * Yuck. zlib.h is not const-correct, so I need this one bit
62 * of ugly hackery to convert a const * pointer to a non-const pointer.
64 #define SET_NEXT_IN(st,src) \
65 (st)->stream.next_in = (Bytef *)(uintptr_t)(const void *)(src)
67 static int archive_compressor_gzip_finish(struct archive_write
*);
68 static int archive_compressor_gzip_init(struct archive_write
*);
69 static int archive_compressor_gzip_write(struct archive_write
*,
70 const void *, size_t);
71 static int drive_compressor(struct archive_write
*, struct private_data
*,
76 * Allocate, initialize and return a archive object.
79 archive_write_set_compression_gzip(struct archive
*_a
)
81 struct archive_write
*a
= (struct archive_write
*)_a
;
82 __archive_check_magic(&a
->archive
, ARCHIVE_WRITE_MAGIC
,
83 ARCHIVE_STATE_NEW
, "archive_write_set_compression_gzip");
84 a
->compressor
.init
= &archive_compressor_gzip_init
;
85 a
->archive
.compression_code
= ARCHIVE_COMPRESSION_GZIP
;
86 a
->archive
.compression_name
= "gzip";
94 archive_compressor_gzip_init(struct archive_write
*a
)
97 struct private_data
*state
;
100 a
->archive
.compression_code
= ARCHIVE_COMPRESSION_GZIP
;
101 a
->archive
.compression_name
= "gzip";
103 if (a
->client_opener
!= NULL
) {
104 ret
= (a
->client_opener
)(&a
->archive
, a
->client_data
);
105 if (ret
!= ARCHIVE_OK
)
110 * The next check is a temporary workaround until the gzip
111 * code can be overhauled some. The code should not require
112 * that compressed_buffer_size == bytes_per_block. Removing
113 * this assumption will allow us to compress larger chunks at
114 * a time, which should improve overall performance
115 * marginally. As a minor side-effect, such a cleanup would
116 * allow us to support truly arbitrary block sizes.
118 if (a
->bytes_per_block
< 10) {
119 archive_set_error(&a
->archive
, EINVAL
,
120 "GZip compressor requires a minimum 10 byte block size");
121 return (ARCHIVE_FATAL
);
124 state
= (struct private_data
*)malloc(sizeof(*state
));
126 archive_set_error(&a
->archive
, ENOMEM
,
127 "Can't allocate data for compression");
128 return (ARCHIVE_FATAL
);
130 memset(state
, 0, sizeof(*state
));
133 * See comment above. We should set compressed_buffer_size to
134 * max(bytes_per_block, 65536), but the code can't handle that yet.
136 state
->compressed_buffer_size
= a
->bytes_per_block
;
137 state
->compressed
= (unsigned char *)malloc(state
->compressed_buffer_size
);
138 state
->crc
= crc32(0L, NULL
, 0);
140 if (state
->compressed
== NULL
) {
141 archive_set_error(&a
->archive
, ENOMEM
,
142 "Can't allocate data for compression buffer");
144 return (ARCHIVE_FATAL
);
147 state
->stream
.next_out
= state
->compressed
;
148 state
->stream
.avail_out
= state
->compressed_buffer_size
;
150 /* Prime output buffer with a gzip header. */
152 state
->compressed
[0] = 0x1f; /* GZip signature bytes */
153 state
->compressed
[1] = 0x8b;
154 state
->compressed
[2] = 0x08; /* "Deflate" compression */
155 state
->compressed
[3] = 0; /* No options */
156 state
->compressed
[4] = (t
)&0xff; /* Timestamp */
157 state
->compressed
[5] = (t
>>8)&0xff;
158 state
->compressed
[6] = (t
>>16)&0xff;
159 state
->compressed
[7] = (t
>>24)&0xff;
160 state
->compressed
[8] = 0; /* No deflate options */
161 state
->compressed
[9] = 3; /* OS=Unix */
162 state
->stream
.next_out
+= 10;
163 state
->stream
.avail_out
-= 10;
165 a
->compressor
.write
= archive_compressor_gzip_write
;
166 a
->compressor
.finish
= archive_compressor_gzip_finish
;
168 /* Initialize compression library. */
169 ret
= deflateInit2(&(state
->stream
),
170 Z_DEFAULT_COMPRESSION
,
172 -15 /* < 0 to suppress zlib header */,
177 a
->compressor
.data
= state
;
181 /* Library setup failed: clean up. */
182 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
, "Internal error "
183 "initializing compression library");
184 free(state
->compressed
);
187 /* Override the error message if we know what really went wrong. */
190 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
191 "Internal error initializing "
192 "compression library: invalid setup parameter");
195 archive_set_error(&a
->archive
, ENOMEM
, "Internal error initializing "
196 "compression library");
198 case Z_VERSION_ERROR
:
199 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
200 "Internal error initializing "
201 "compression library: invalid library version");
205 return (ARCHIVE_FATAL
);
209 * Write data to the compressed stream.
212 archive_compressor_gzip_write(struct archive_write
*a
, const void *buff
,
215 struct private_data
*state
;
218 state
= (struct private_data
*)a
->compressor
.data
;
219 if (a
->client_writer
== NULL
) {
220 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_PROGRAMMER
,
221 "No write callback is registered? "
222 "This is probably an internal programming error.");
223 return (ARCHIVE_FATAL
);
226 /* Update statistics */
227 state
->crc
= crc32(state
->crc
, (const Bytef
*)buff
, length
);
228 state
->total_in
+= length
;
230 /* Compress input data to output buffer */
231 SET_NEXT_IN(state
, buff
);
232 state
->stream
.avail_in
= length
;
233 if ((ret
= drive_compressor(a
, state
, 0)) != ARCHIVE_OK
)
236 a
->archive
.file_position
+= length
;
242 * Finish the compression...
245 archive_compressor_gzip_finish(struct archive_write
*a
)
247 ssize_t block_length
, target_block_length
, bytes_written
;
249 struct private_data
*state
;
251 unsigned char trailer
[8];
253 state
= (struct private_data
*)a
->compressor
.data
;
255 if (a
->client_writer
== NULL
) {
256 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_PROGRAMMER
,
257 "No write callback is registered? "
258 "This is probably an internal programming error.");
263 /* By default, always pad the uncompressed data. */
264 if (a
->pad_uncompressed
) {
265 tocopy
= a
->bytes_per_block
-
266 (state
->total_in
% a
->bytes_per_block
);
267 while (tocopy
> 0 && tocopy
< (unsigned)a
->bytes_per_block
) {
268 SET_NEXT_IN(state
, a
->nulls
);
269 state
->stream
.avail_in
= tocopy
< a
->null_length
?
270 tocopy
: a
->null_length
;
271 state
->crc
= crc32(state
->crc
, a
->nulls
,
272 state
->stream
.avail_in
);
273 state
->total_in
+= state
->stream
.avail_in
;
274 tocopy
-= state
->stream
.avail_in
;
275 ret
= drive_compressor(a
, state
, 0);
276 if (ret
!= ARCHIVE_OK
)
281 /* Finish compression cycle */
282 if (((ret
= drive_compressor(a
, state
, 1))) != ARCHIVE_OK
)
285 /* Build trailer: 4-byte CRC and 4-byte length. */
286 trailer
[0] = (state
->crc
)&0xff;
287 trailer
[1] = (state
->crc
>> 8)&0xff;
288 trailer
[2] = (state
->crc
>> 16)&0xff;
289 trailer
[3] = (state
->crc
>> 24)&0xff;
290 trailer
[4] = (state
->total_in
)&0xff;
291 trailer
[5] = (state
->total_in
>> 8)&0xff;
292 trailer
[6] = (state
->total_in
>> 16)&0xff;
293 trailer
[7] = (state
->total_in
>> 24)&0xff;
295 /* Add trailer to current block. */
297 if (tocopy
> state
->stream
.avail_out
)
298 tocopy
= state
->stream
.avail_out
;
299 memcpy(state
->stream
.next_out
, trailer
, tocopy
);
300 state
->stream
.next_out
+= tocopy
;
301 state
->stream
.avail_out
-= tocopy
;
303 /* If it overflowed, flush and start a new block. */
305 bytes_written
= (a
->client_writer
)(&a
->archive
, a
->client_data
,
306 state
->compressed
, state
->compressed_buffer_size
);
307 if (bytes_written
<= 0) {
311 a
->archive
.raw_position
+= bytes_written
;
312 state
->stream
.next_out
= state
->compressed
;
313 state
->stream
.avail_out
= state
->compressed_buffer_size
;
314 memcpy(state
->stream
.next_out
, trailer
+ tocopy
, 8-tocopy
);
315 state
->stream
.next_out
+= 8-tocopy
;
316 state
->stream
.avail_out
-= 8-tocopy
;
319 /* Optionally, pad the final compressed block. */
320 block_length
= state
->stream
.next_out
- state
->compressed
;
323 /* Tricky calculation to determine size of last block. */
324 target_block_length
= block_length
;
325 if (a
->bytes_in_last_block
<= 0)
326 /* Default or Zero: pad to full block */
327 target_block_length
= a
->bytes_per_block
;
329 /* Round length to next multiple of bytes_in_last_block. */
330 target_block_length
= a
->bytes_in_last_block
*
331 ( (block_length
+ a
->bytes_in_last_block
- 1) /
332 a
->bytes_in_last_block
);
333 if (target_block_length
> a
->bytes_per_block
)
334 target_block_length
= a
->bytes_per_block
;
335 if (block_length
< target_block_length
) {
336 memset(state
->stream
.next_out
, 0,
337 target_block_length
- block_length
);
338 block_length
= target_block_length
;
341 /* Write the last block */
342 bytes_written
= (a
->client_writer
)(&a
->archive
, a
->client_data
,
343 state
->compressed
, block_length
);
344 if (bytes_written
<= 0) {
348 a
->archive
.raw_position
+= bytes_written
;
350 /* Cleanup: shut down compressor, release memory, etc. */
352 switch (deflateEnd(&(state
->stream
))) {
356 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
357 "Failed to clean up compressor");
360 free(state
->compressed
);
366 * Utility function to push input data through compressor,
367 * writing full output blocks as necessary.
369 * Note that this handles both the regular write case (finishing ==
370 * false) and the end-of-archive case (finishing == true).
373 drive_compressor(struct archive_write
*a
, struct private_data
*state
, int finishing
)
375 ssize_t bytes_written
;
379 if (state
->stream
.avail_out
== 0) {
380 bytes_written
= (a
->client_writer
)(&a
->archive
,
381 a
->client_data
, state
->compressed
,
382 state
->compressed_buffer_size
);
383 if (bytes_written
<= 0) {
384 /* TODO: Handle this write failure */
385 return (ARCHIVE_FATAL
);
386 } else if ((size_t)bytes_written
< state
->compressed_buffer_size
) {
387 /* Short write: Move remaining to
388 * front of block and keep filling */
389 memmove(state
->compressed
,
390 state
->compressed
+ bytes_written
,
391 state
->compressed_buffer_size
- bytes_written
);
393 a
->archive
.raw_position
+= bytes_written
;
394 state
->stream
.next_out
395 = state
->compressed
+
396 state
->compressed_buffer_size
- bytes_written
;
397 state
->stream
.avail_out
= bytes_written
;
400 /* If there's nothing to do, we're done. */
401 if (!finishing
&& state
->stream
.avail_in
== 0)
404 ret
= deflate(&(state
->stream
),
405 finishing
? Z_FINISH
: Z_NO_FLUSH
);
409 /* In non-finishing case, check if compressor
410 * consumed everything */
411 if (!finishing
&& state
->stream
.avail_in
== 0)
413 /* In finishing case, this return always means
414 * there's more work */
417 /* This return can only occur in finishing case. */
420 /* Any other return value indicates an error. */
421 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
422 "GZip compression failed:"
423 " deflate() call returned status %d",
425 return (ARCHIVE_FATAL
);
430 #endif /* HAVE_ZLIB_H */