github.com/grailbio/base@v0.0.11/compress/libdeflate/zlib_compress.c (about) 1 /* 2 * zlib_compress.c - compress with a zlib wrapper 3 * 4 * Originally public domain; changes after 2016-09-07 are copyrighted. 5 * 6 * Copyright 2016 Eric Biggers 7 * 8 * Permission is hereby granted, free of charge, to any person 9 * obtaining a copy of this software and associated documentation 10 * files (the "Software"), to deal in the Software without 11 * restriction, including without limitation the rights to use, 12 * copy, modify, merge, publish, distribute, sublicense, and/or sell 13 * copies of the Software, and to permit persons to whom the 14 * Software is furnished to do so, subject to the following 15 * conditions: 16 * 17 * The above copyright notice and this permission notice shall be 18 * included in all copies or substantial portions of the Software. 19 * 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 22 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 24 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 27 * OTHER DEALINGS IN THE SOFTWARE. 28 */ 29 30 #include "deflate_compress.h" 31 #include "unaligned.h" 32 #include "zlib_constants.h" 33 34 #include "libdeflate.h" 35 36 LIBDEFLATEAPI size_t 37 libdeflate_zlib_compress(struct libdeflate_compressor *c, 38 const void *in, size_t in_size, 39 void *out, size_t out_nbytes_avail) 40 { 41 u8 *out_next = out; 42 u16 hdr; 43 unsigned compression_level; 44 unsigned level_hint; 45 size_t deflate_size; 46 47 if (out_nbytes_avail <= ZLIB_MIN_OVERHEAD) 48 return 0; 49 50 /* 2 byte header: CMF and FLG */ 51 hdr = (ZLIB_CM_DEFLATE << 8) | (ZLIB_CINFO_32K_WINDOW << 12); 52 compression_level = deflate_get_compression_level(c); 53 if (compression_level < 2) 54 level_hint = ZLIB_FASTEST_COMPRESSION; 55 else if (compression_level < 6) 56 level_hint = ZLIB_FAST_COMPRESSION; 57 else if (compression_level < 8) 58 level_hint = ZLIB_DEFAULT_COMPRESSION; 59 else 60 level_hint = ZLIB_SLOWEST_COMPRESSION; 61 hdr |= level_hint << 6; 62 hdr |= 31 - (hdr % 31); 63 64 put_unaligned_be16(hdr, out_next); 65 out_next += 2; 66 67 /* Compressed data */ 68 deflate_size = libdeflate_deflate_compress(c, in, in_size, out_next, 69 out_nbytes_avail - ZLIB_MIN_OVERHEAD); 70 if (deflate_size == 0) 71 return 0; 72 out_next += deflate_size; 73 74 /* ADLER32 */ 75 put_unaligned_be32(libdeflate_adler32(1, in, in_size), out_next); 76 out_next += 4; 77 78 return out_next - (u8 *)out; 79 } 80 81 LIBDEFLATEAPI size_t 82 libdeflate_zlib_compress_bound(struct libdeflate_compressor *c, 83 size_t in_nbytes) 84 { 85 return ZLIB_MIN_OVERHEAD + 86 libdeflate_deflate_compress_bound(c, in_nbytes); 87 }