github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/compress/libdeflate/gzip_compress.c (about) 1 /* 2 * gzip_compress.c - compress with a gzip 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 "gzip_constants.h" 32 #include "unaligned.h" 33 34 #include "libdeflate.h" 35 36 LIBDEFLATEAPI size_t 37 libdeflate_gzip_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 unsigned compression_level; 43 u8 xfl; 44 size_t deflate_size; 45 46 if (out_nbytes_avail <= GZIP_MIN_OVERHEAD) 47 return 0; 48 49 /* ID1 */ 50 *out_next++ = GZIP_ID1; 51 /* ID2 */ 52 *out_next++ = GZIP_ID2; 53 /* CM */ 54 *out_next++ = GZIP_CM_DEFLATE; 55 /* FLG */ 56 *out_next++ = 0; 57 /* MTIME */ 58 put_unaligned_le32(GZIP_MTIME_UNAVAILABLE, out_next); 59 out_next += 4; 60 /* XFL */ 61 xfl = 0; 62 compression_level = deflate_get_compression_level(c); 63 if (compression_level < 2) 64 xfl |= GZIP_XFL_FASTEST_COMRESSION; 65 else if (compression_level >= 8) 66 xfl |= GZIP_XFL_SLOWEST_COMRESSION; 67 *out_next++ = xfl; 68 /* OS */ 69 *out_next++ = GZIP_OS_UNKNOWN; /* OS */ 70 71 /* Compressed data */ 72 deflate_size = libdeflate_deflate_compress(c, in, in_size, out_next, 73 out_nbytes_avail - GZIP_MIN_OVERHEAD); 74 if (deflate_size == 0) 75 return 0; 76 out_next += deflate_size; 77 78 /* CRC32 */ 79 put_unaligned_le32(libdeflate_crc32(0, in, in_size), out_next); 80 out_next += 4; 81 82 /* ISIZE */ 83 put_unaligned_le32((u32)in_size, out_next); 84 out_next += 4; 85 86 return out_next - (u8 *)out; 87 } 88 89 LIBDEFLATEAPI size_t 90 libdeflate_gzip_compress_bound(struct libdeflate_compressor *c, 91 size_t in_nbytes) 92 { 93 return GZIP_MIN_OVERHEAD + 94 libdeflate_deflate_compress_bound(c, in_nbytes); 95 }