1*96c32821Schristos /* $NetBSD: compress.c,v 1.5 2024/09/22 19:12:27 christos Exp $ */ 2aaf4ece6Schristos 3aaf4ece6Schristos /* compress.c -- compress a memory buffer 46db8c6e9Schristos * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler 5aaf4ece6Schristos * For conditions of distribution and use, see copyright notice in zlib.h 6aaf4ece6Schristos */ 7aaf4ece6Schristos 83b1253e8Schristos /* @(#) Id */ 9aaf4ece6Schristos 10aaf4ece6Schristos #define ZLIB_INTERNAL 11aaf4ece6Schristos #include "zlib.h" 12aaf4ece6Schristos 13aaf4ece6Schristos /* =========================================================================== 14aaf4ece6Schristos Compresses the source buffer into the destination buffer. The level 15aaf4ece6Schristos parameter has the same meaning as in deflateInit. sourceLen is the byte 16aaf4ece6Schristos length of the source buffer. Upon entry, destLen is the total size of the 17aaf4ece6Schristos destination buffer, which must be at least 0.1% larger than sourceLen plus 18aaf4ece6Schristos 12 bytes. Upon exit, destLen is the actual size of the compressed buffer. 19aaf4ece6Schristos 20aaf4ece6Schristos compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 21aaf4ece6Schristos memory, Z_BUF_ERROR if there was not enough room in the output buffer, 22aaf4ece6Schristos Z_STREAM_ERROR if the level parameter is invalid. 23aaf4ece6Schristos */ 24*96c32821Schristos int ZEXPORT compress2(Bytef *dest, uLongf *destLen, const Bytef *source, 25*96c32821Schristos uLong sourceLen, int level) { 26aaf4ece6Schristos z_stream stream; 27aaf4ece6Schristos int err; 286db8c6e9Schristos const uInt max = (uInt)-1; 296db8c6e9Schristos uLong left; 30aaf4ece6Schristos 316db8c6e9Schristos left = *destLen; 326db8c6e9Schristos *destLen = 0; 33aaf4ece6Schristos 34aaf4ece6Schristos stream.zalloc = (alloc_func)0; 35aaf4ece6Schristos stream.zfree = (free_func)0; 36aaf4ece6Schristos stream.opaque = (voidpf)0; 37aaf4ece6Schristos 38aaf4ece6Schristos err = deflateInit(&stream, level); 39aaf4ece6Schristos if (err != Z_OK) return err; 40aaf4ece6Schristos 416db8c6e9Schristos stream.next_out = dest; 426db8c6e9Schristos stream.avail_out = 0; 436db8c6e9Schristos stream.next_in = __UNCONST(source); 446db8c6e9Schristos stream.avail_in = 0; 45aaf4ece6Schristos 466db8c6e9Schristos do { 476db8c6e9Schristos if (stream.avail_out == 0) { 486db8c6e9Schristos stream.avail_out = left > (uLong)max ? max : (uInt)left; 496db8c6e9Schristos left -= stream.avail_out; 506db8c6e9Schristos } 516db8c6e9Schristos if (stream.avail_in == 0) { 526db8c6e9Schristos stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen; 536db8c6e9Schristos sourceLen -= stream.avail_in; 546db8c6e9Schristos } 556db8c6e9Schristos err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH); 566db8c6e9Schristos } while (err == Z_OK); 576db8c6e9Schristos 586db8c6e9Schristos *destLen = stream.total_out; 596db8c6e9Schristos deflateEnd(&stream); 606db8c6e9Schristos return err == Z_STREAM_END ? Z_OK : err; 61aaf4ece6Schristos } 62aaf4ece6Schristos 63aaf4ece6Schristos /* =========================================================================== 64aaf4ece6Schristos */ 65*96c32821Schristos int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, 66*96c32821Schristos uLong sourceLen) { 67aaf4ece6Schristos return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); 68aaf4ece6Schristos } 69aaf4ece6Schristos 70aaf4ece6Schristos /* =========================================================================== 71aaf4ece6Schristos If the default memLevel or windowBits for deflateInit() is changed, then 72aaf4ece6Schristos this function needs to be updated. 73aaf4ece6Schristos */ 74*96c32821Schristos uLong ZEXPORT compressBound(uLong sourceLen) { 756db8c6e9Schristos return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 766db8c6e9Schristos (sourceLen >> 25) + 13; 77aaf4ece6Schristos } 78