xref: /netbsd-src/external/gpl3/binutils.old/dist/zlib/compress.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
175fd0b74Schristos /* compress.c -- compress a memory buffer
2ede78133Schristos  * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
375fd0b74Schristos  * For conditions of distribution and use, see copyright notice in zlib.h
475fd0b74Schristos  */
575fd0b74Schristos 
6*e992f068Schristos /* @(#) Id */
775fd0b74Schristos 
875fd0b74Schristos #define ZLIB_INTERNAL
975fd0b74Schristos #include "zlib.h"
1075fd0b74Schristos 
1175fd0b74Schristos /* ===========================================================================
1275fd0b74Schristos      Compresses the source buffer into the destination buffer. The level
1375fd0b74Schristos    parameter has the same meaning as in deflateInit.  sourceLen is the byte
1475fd0b74Schristos    length of the source buffer. Upon entry, destLen is the total size of the
1575fd0b74Schristos    destination buffer, which must be at least 0.1% larger than sourceLen plus
1675fd0b74Schristos    12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
1775fd0b74Schristos 
1875fd0b74Schristos      compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
1975fd0b74Schristos    memory, Z_BUF_ERROR if there was not enough room in the output buffer,
2075fd0b74Schristos    Z_STREAM_ERROR if the level parameter is invalid.
2175fd0b74Schristos */
compress2(dest,destLen,source,sourceLen,level)2275fd0b74Schristos int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
2375fd0b74Schristos     Bytef *dest;
2475fd0b74Schristos     uLongf *destLen;
2575fd0b74Schristos     const Bytef *source;
2675fd0b74Schristos     uLong sourceLen;
2775fd0b74Schristos     int level;
2875fd0b74Schristos {
2975fd0b74Schristos     z_stream stream;
3075fd0b74Schristos     int err;
31ede78133Schristos     const uInt max = (uInt)-1;
32ede78133Schristos     uLong left;
3375fd0b74Schristos 
34ede78133Schristos     left = *destLen;
35ede78133Schristos     *destLen = 0;
3675fd0b74Schristos 
3775fd0b74Schristos     stream.zalloc = (alloc_func)0;
3875fd0b74Schristos     stream.zfree = (free_func)0;
3975fd0b74Schristos     stream.opaque = (voidpf)0;
4075fd0b74Schristos 
4175fd0b74Schristos     err = deflateInit(&stream, level);
4275fd0b74Schristos     if (err != Z_OK) return err;
4375fd0b74Schristos 
44ede78133Schristos     stream.next_out = dest;
45ede78133Schristos     stream.avail_out = 0;
46ede78133Schristos     stream.next_in = (z_const Bytef *)source;
47ede78133Schristos     stream.avail_in = 0;
4875fd0b74Schristos 
49ede78133Schristos     do {
50ede78133Schristos         if (stream.avail_out == 0) {
51ede78133Schristos             stream.avail_out = left > (uLong)max ? max : (uInt)left;
52ede78133Schristos             left -= stream.avail_out;
53ede78133Schristos         }
54ede78133Schristos         if (stream.avail_in == 0) {
55ede78133Schristos             stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;
56ede78133Schristos             sourceLen -= stream.avail_in;
57ede78133Schristos         }
58ede78133Schristos         err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
59ede78133Schristos     } while (err == Z_OK);
60ede78133Schristos 
61ede78133Schristos     *destLen = stream.total_out;
62ede78133Schristos     deflateEnd(&stream);
63ede78133Schristos     return err == Z_STREAM_END ? Z_OK : err;
6475fd0b74Schristos }
6575fd0b74Schristos 
6675fd0b74Schristos /* ===========================================================================
6775fd0b74Schristos  */
compress(dest,destLen,source,sourceLen)6875fd0b74Schristos int ZEXPORT compress (dest, destLen, source, sourceLen)
6975fd0b74Schristos     Bytef *dest;
7075fd0b74Schristos     uLongf *destLen;
7175fd0b74Schristos     const Bytef *source;
7275fd0b74Schristos     uLong sourceLen;
7375fd0b74Schristos {
7475fd0b74Schristos     return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
7575fd0b74Schristos }
7675fd0b74Schristos 
7775fd0b74Schristos /* ===========================================================================
7875fd0b74Schristos      If the default memLevel or windowBits for deflateInit() is changed, then
7975fd0b74Schristos    this function needs to be updated.
8075fd0b74Schristos  */
compressBound(sourceLen)8175fd0b74Schristos uLong ZEXPORT compressBound (sourceLen)
8275fd0b74Schristos     uLong sourceLen;
8375fd0b74Schristos {
8475fd0b74Schristos     return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
8575fd0b74Schristos            (sourceLen >> 25) + 13;
8675fd0b74Schristos }
87