1 /* 2 * Copyright (c) 2023 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@dragonflybsd.org> 6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 3. Neither the name of The DragonFly Project nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific, prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 #include "hammer2.h" 36 #include "lz4/hammer2_lz4.h" 37 #include "zlib/hammer2_zlib.h" 38 39 #define DEBUFSIZE HAMMER2_PBUFSIZE 40 41 static char OutBuf[DEBUFSIZE + 128]; /* allow for some overflow */ 42 43 void * 44 hammer2_decompress_LZ4(void *inbuf, size_t insize, size_t outsize, 45 int *statusp) 46 { 47 int result; 48 int cinsize; 49 50 assert(outsize <= DEBUFSIZE); 51 *statusp = 0; 52 53 cinsize = *(const int *)inbuf; 54 if (cinsize > (int)insize) { 55 printf("FAIL1\n"); 56 bzero(OutBuf, outsize); 57 return OutBuf; 58 } 59 60 result = LZ4_decompress_safe((char *)inbuf + 4, OutBuf, 61 cinsize, outsize); 62 if (result < 0) { 63 bzero(OutBuf, outsize); 64 } else { 65 *statusp = 1; 66 if (result < (int)outsize) 67 bzero(OutBuf + result, outsize - result); 68 } 69 if (result < 0) 70 printf("LZ4 decompression failure\n"); 71 72 return OutBuf; 73 } 74 75 void * 76 hammer2_decompress_ZLIB(void *inbuf, size_t insize, size_t outsize, 77 int *statusp) 78 { 79 z_stream strm_decompress; 80 int ret; 81 82 assert(outsize <= DEBUFSIZE); 83 *statusp = 0; 84 85 bzero(&strm_decompress, sizeof(strm_decompress)); 86 strm_decompress.avail_in = 0; 87 strm_decompress.next_in = Z_NULL; 88 ret = inflateInit(&strm_decompress); 89 90 if (ret != Z_OK) { 91 bzero(OutBuf, outsize); 92 printf("ZLIB1 decompression failure\n"); 93 } else { 94 strm_decompress.next_in = inbuf; 95 strm_decompress.next_out = OutBuf; 96 strm_decompress.avail_in = insize; 97 strm_decompress.avail_out = outsize; 98 ret = inflate(&strm_decompress, Z_FINISH); 99 if (ret != Z_STREAM_END) { 100 bzero(OutBuf, outsize); 101 printf("ZLIB2 decompression failure\n"); 102 } else { 103 ret = outsize - strm_decompress.avail_out; 104 if (ret < (int)outsize) 105 bzero(OutBuf + ret, strm_decompress.avail_out); 106 *statusp = 1; 107 } 108 ret = inflateEnd(&strm_decompress); 109 } 110 return OutBuf; 111 } 112