| 1 |
/* |
| 2 |
* zlib_compress.c - compress with a zlib 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 "zlib_constants.h" |
| 30 |
|
| 31 |
LIBDEFLATEAPI size_t |
| 32 |
libdeflate_zlib_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 |
u16 hdr; |
| 38 |
unsigned compression_level; |
| 39 |
unsigned level_hint; |
| 40 |
size_t deflate_size; |
| 41 |
|
| 42 |
if (out_nbytes_avail <= ZLIB_MIN_OVERHEAD) |
| 43 |
return 0; |
| 44 |
|
| 45 |
/* 2 byte header: CMF and FLG */ |
| 46 |
hdr = (ZLIB_CM_DEFLATE << 8) | (ZLIB_CINFO_32K_WINDOW << 12); |
| 47 |
compression_level = libdeflate_get_compression_level(c); |
| 48 |
if (compression_level < 2) |
| 49 |
level_hint = ZLIB_FASTEST_COMPRESSION; |
| 50 |
else if (compression_level < 6) |
| 51 |
level_hint = ZLIB_FAST_COMPRESSION; |
| 52 |
else if (compression_level < 8) |
| 53 |
level_hint = ZLIB_DEFAULT_COMPRESSION; |
| 54 |
else |
| 55 |
level_hint = ZLIB_SLOWEST_COMPRESSION; |
| 56 |
hdr |= level_hint << 6; |
| 57 |
hdr |= 31 - (hdr % 31); |
| 58 |
|
| 59 |
put_unaligned_be16(hdr, out_next); |
| 60 |
out_next += 2; |
| 61 |
|
| 62 |
/* Compressed data */ |
| 63 |
deflate_size = libdeflate_deflate_compress(c, in, in_nbytes, out_next, |
| 64 |
out_nbytes_avail - ZLIB_MIN_OVERHEAD); |
| 65 |
if (deflate_size == 0) |
| 66 |
return 0; |
| 67 |
out_next += deflate_size; |
| 68 |
|
| 69 |
/* ADLER32 */ |
| 70 |
put_unaligned_be32(libdeflate_adler32(1, in, in_nbytes), out_next); |
| 71 |
out_next += 4; |
| 72 |
|
| 73 |
return out_next - (u8 *)out; |
| 74 |
} |
| 75 |
|
| 76 |
LIBDEFLATEAPI size_t |
| 77 |
libdeflate_zlib_compress_bound(struct libdeflate_compressor *c, |
| 78 |
size_t in_nbytes) |
| 79 |
{ |
| 80 |
return ZLIB_MIN_OVERHEAD + |
| 81 |
libdeflate_deflate_compress_bound(c, in_nbytes); |
| 82 |
} |