github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/compress/libdeflate/zlib_decompress.c (about) 1 /* 2 * zlib_decompress.c - decompress 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 "unaligned.h" 31 #include "zlib_constants.h" 32 33 #include "libdeflate.h" 34 35 LIBDEFLATEAPI enum libdeflate_result 36 libdeflate_zlib_decompress(struct libdeflate_decompressor *d, 37 const void *in, size_t in_nbytes, 38 void *out, size_t out_nbytes_avail, 39 size_t *actual_out_nbytes_ret) 40 { 41 const u8 *in_next = in; 42 const u8 * const in_end = in_next + in_nbytes; 43 u16 hdr; 44 size_t actual_out_nbytes; 45 enum libdeflate_result result; 46 47 if (in_nbytes < ZLIB_MIN_OVERHEAD) 48 return LIBDEFLATE_BAD_DATA; 49 50 /* 2 byte header: CMF and FLG */ 51 hdr = get_unaligned_be16(in_next); 52 in_next += 2; 53 54 /* FCHECK */ 55 if ((hdr % 31) != 0) 56 return LIBDEFLATE_BAD_DATA; 57 58 /* CM */ 59 if (((hdr >> 8) & 0xF) != ZLIB_CM_DEFLATE) 60 return LIBDEFLATE_BAD_DATA; 61 62 /* CINFO */ 63 if ((hdr >> 12) > ZLIB_CINFO_32K_WINDOW) 64 return LIBDEFLATE_BAD_DATA; 65 66 /* FDICT */ 67 if ((hdr >> 5) & 1) 68 return LIBDEFLATE_BAD_DATA; 69 70 /* Compressed data */ 71 result = libdeflate_deflate_decompress(d, in_next, 72 in_end - ZLIB_FOOTER_SIZE - in_next, 73 out, out_nbytes_avail, 74 actual_out_nbytes_ret); 75 if (result != LIBDEFLATE_SUCCESS) 76 return result; 77 78 if (actual_out_nbytes_ret) 79 actual_out_nbytes = *actual_out_nbytes_ret; 80 else 81 actual_out_nbytes = out_nbytes_avail; 82 83 in_next = in_end - ZLIB_FOOTER_SIZE; 84 85 /* ADLER32 */ 86 if (libdeflate_adler32(1, out, actual_out_nbytes) != 87 get_unaligned_be32(in_next)) 88 return LIBDEFLATE_BAD_DATA; 89 90 return LIBDEFLATE_SUCCESS; 91 }