| 1 |
/* |
| 2 |
* gzip_compress.c - compress with a gzip wrapper |
| 3 |
* |
| 4 |
* Copyright 2016 Eric Biggers |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person |
| 7 |
* obtaining a copy of this software and associated documentation |
| 8 |
* files (the "Software"), to deal in the Software without |
| 9 |
* restriction, including without limitation the rights to use, |
| 10 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 |
* copies of the Software, and to permit persons to whom the |
| 12 |
* Software is furnished to do so, subject to the following |
| 13 |
* conditions: |
| 14 |
* |
| 15 |
* The above copyright notice and this permission notice shall be |
| 16 |
* included in all copies or substantial portions of the Software. |
| 17 |
* |
| 18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 20 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 22 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 23 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 24 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 25 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "deflate_compress.h" |
| 29 |
#include "gzip_constants.h" |
| 30 |
|
| 31 |
LIBDEFLATEAPI size_t |
| 32 |
libdeflate_gzip_compress(struct libdeflate_compressor *c, |
| 33 |
const void *in, size_t in_nbytes, |
| 34 |
void *out, size_t out_nbytes_avail) |
| 35 |
{ |
| 36 |
u8 *out_next = out; |
| 37 |
unsigned compression_level; |
| 38 |
u8 xfl; |
| 39 |
size_t deflate_size; |
| 40 |
|
| 41 |
if (out_nbytes_avail <= GZIP_MIN_OVERHEAD) |
| 42 |
return 0; |
| 43 |
|
| 44 |
/* ID1 */ |
| 45 |
*out_next++ = GZIP_ID1; |
| 46 |
/* ID2 */ |
| 47 |
*out_next++ = GZIP_ID2; |
| 48 |
/* CM */ |
| 49 |
*out_next++ = GZIP_CM_DEFLATE; |
| 50 |
/* FLG */ |
| 51 |
*out_next++ = 0; |
| 52 |
/* MTIME */ |
| 53 |
put_unaligned_le32(GZIP_MTIME_UNAVAILABLE, out_next); |
| 54 |
out_next += 4; |
| 55 |
/* XFL */ |
| 56 |
xfl = 0; |
| 57 |
compression_level = libdeflate_get_compression_level(c); |
| 58 |
if (compression_level < 2) |
| 59 |
xfl |= GZIP_XFL_FASTEST_COMPRESSION; |
| 60 |
else if (compression_level >= 8) |
| 61 |
xfl |= GZIP_XFL_SLOWEST_COMPRESSION; |
| 62 |
*out_next++ = xfl; |
| 63 |
/* OS */ |
| 64 |
*out_next++ = GZIP_OS_UNKNOWN; /* OS */ |
| 65 |
|
| 66 |
/* Compressed data */ |
| 67 |
deflate_size = libdeflate_deflate_compress(c, in, in_nbytes, out_next, |
| 68 |
out_nbytes_avail - GZIP_MIN_OVERHEAD); |
| 69 |
if (deflate_size == 0) |
| 70 |
return 0; |
| 71 |
out_next += deflate_size; |
| 72 |
|
| 73 |
/* CRC32 */ |
| 74 |
put_unaligned_le32(libdeflate_crc32(0, in, in_nbytes), out_next); |
| 75 |
out_next += 4; |
| 76 |
|
| 77 |
/* ISIZE */ |
| 78 |
put_unaligned_le32((u32)in_nbytes, out_next); |
| 79 |
out_next += 4; |
| 80 |
|
| 81 |
return out_next - (u8 *)out; |
| 82 |
} |
| 83 |
|
| 84 |
LIBDEFLATEAPI size_t |
| 85 |
libdeflate_gzip_compress_bound(struct libdeflate_compressor *c, |
| 86 |
size_t in_nbytes) |
| 87 |
{ |
| 88 |
return GZIP_MIN_OVERHEAD + |
| 89 |
libdeflate_deflate_compress_bound(c, in_nbytes); |
| 90 |
} |