jdk/src/share/native/java/util/zip/zlib-1.2.5/compress.c
author sherman
Tue, 29 Nov 2011 11:39:59 -0800
changeset 11112 6d340c7b6a32
child 13409 fe3ab27ef1a6
permissions -rw-r--r--
7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5 Summary: updated to zlib-1.2.5 Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11112
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
     1
/*
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
     3
 *
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
     9
 *
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    14
 * accompanied this code).
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    15
 *
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    19
 *
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    22
 * questions.
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    23
 */
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    24
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    25
/* compress.c -- compress a memory buffer
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    26
 * Copyright (C) 1995-2005 Jean-loup Gailly.
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    27
 * For conditions of distribution and use, see copyright notice in zlib.h
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    28
 */
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    29
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    30
/* @(#) $Id$ */
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    31
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    32
#define ZLIB_INTERNAL
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    33
#include "zlib.h"
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    34
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    35
/* ===========================================================================
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    36
     Compresses the source buffer into the destination buffer. The level
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    37
   parameter has the same meaning as in deflateInit.  sourceLen is the byte
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    38
   length of the source buffer. Upon entry, destLen is the total size of the
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    39
   destination buffer, which must be at least 0.1% larger than sourceLen plus
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    40
   12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    41
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    42
     compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    43
   memory, Z_BUF_ERROR if there was not enough room in the output buffer,
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    44
   Z_STREAM_ERROR if the level parameter is invalid.
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    45
*/
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    46
int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    47
    Bytef *dest;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    48
    uLongf *destLen;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    49
    const Bytef *source;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    50
    uLong sourceLen;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    51
    int level;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    52
{
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    53
    z_stream stream;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    54
    int err;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    55
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    56
    stream.next_in = (Bytef*)source;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    57
    stream.avail_in = (uInt)sourceLen;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    58
#ifdef MAXSEG_64K
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    59
    /* Check for source > 64K on 16-bit machine: */
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    60
    if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    61
#endif
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    62
    stream.next_out = dest;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    63
    stream.avail_out = (uInt)*destLen;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    64
    if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    65
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    66
    stream.zalloc = (alloc_func)0;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    67
    stream.zfree = (free_func)0;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    68
    stream.opaque = (voidpf)0;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    69
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    70
    err = deflateInit(&stream, level);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    71
    if (err != Z_OK) return err;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    72
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    73
    err = deflate(&stream, Z_FINISH);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    74
    if (err != Z_STREAM_END) {
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    75
        deflateEnd(&stream);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    76
        return err == Z_OK ? Z_BUF_ERROR : err;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    77
    }
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    78
    *destLen = (uLong)stream.total_out;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    79
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    80
    err = deflateEnd(&stream);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    81
    return err;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    82
}
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    83
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    84
/* ===========================================================================
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    85
 */
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    86
int ZEXPORT compress (dest, destLen, source, sourceLen)
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    87
    Bytef *dest;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    88
    uLongf *destLen;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    89
    const Bytef *source;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    90
    uLong sourceLen;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    91
{
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    92
    return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    93
}
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    94
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    95
/* ===========================================================================
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    96
     If the default memLevel or windowBits for deflateInit() is changed, then
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    97
   this function needs to be updated.
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    98
 */
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
    99
uLong ZEXPORT compressBound (sourceLen)
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
   100
    uLong sourceLen;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
   101
{
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
   102
    return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
   103
           (sourceLen >> 25) + 13;
6d340c7b6a32 7110149: Update the JDK8 bundled zlib library to the latest version 1.2.5
sherman
parents:
diff changeset
   104
}