src/java.desktop/share/native/libawt/awt/image/gif/gifdecoder.c
author ihse
Sat, 03 Mar 2018 08:21:47 +0100
branchihse-warnings-cflags-branch
changeset 56230 489867818774
parent 47216 71c04702a3d5
permissions -rw-r--r--
No longer disable E_OLD_STYLE_FUNC_DEF.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 5506
diff changeset
     2
 * Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2610
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2610
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2610
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2610
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2610
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#define OUTCODELENGTH 4097
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/* We use Get/ReleasePrimitiveArrayCritical functions to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * the need to copy buffer elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * MAKE SURE TO:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * - carefully insert pairs of RELEASE_ARRAYS and GET_ARRAYS around
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *   callbacks to Java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * - call RELEASE_ARRAYS before returning to Java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Otherwise things will go horribly wrong. There may be memory leaks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * excessive pinning, or even VM crashes!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Note that GetPrimitiveArrayCritical may fail!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#define GET_ARRAYS() \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    prefix  = (short *) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        (*env)->GetPrimitiveArrayCritical(env, prefixh, 0); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    if (prefix == 0) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        goto out_of_memory; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    suffix  = (unsigned char *) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        (*env)->GetPrimitiveArrayCritical(env, suffixh, 0); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    if (suffix == 0) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        goto out_of_memory; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    outCode = (unsigned char *) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        (*env)->GetPrimitiveArrayCritical(env, outCodeh, 0); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    if (outCode == 0) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        goto out_of_memory; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    rasline = (unsigned char *) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        (*env)->GetPrimitiveArrayCritical(env, raslineh, 0); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    if (rasline == 0) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        goto out_of_memory; \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    block = (unsigned char *) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        (*env)->GetPrimitiveArrayCritical(env, blockh, 0); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    if (block == 0) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        goto out_of_memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Note that it is important to check whether the arrays are NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * because GetPrimitiveArrayCritical might have failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
#define RELEASE_ARRAYS() \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
if (prefix) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    (*env)->ReleasePrimitiveArrayCritical(env, prefixh, prefix, 0); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
if (suffix) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    (*env)->ReleasePrimitiveArrayCritical(env, suffixh, suffix, 0); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
if (outCode) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    (*env)->ReleasePrimitiveArrayCritical(env, outCodeh, outCode, 0); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
if (rasline) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    (*env)->ReleasePrimitiveArrayCritical(env, raslineh, rasline, 0); \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
if (block) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    (*env)->ReleasePrimitiveArrayCritical(env, blockh, block, 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
/* Place holders for the old native interface. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
long
56230
489867818774 No longer disable E_OLD_STYLE_FUNC_DEF.
ihse
parents: 47216
diff changeset
    88
sun_awt_image_GifImageDecoder_parseImage(void)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
  return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
void
56230
489867818774 No longer disable E_OLD_STYLE_FUNC_DEF.
ihse
parents: 47216
diff changeset
    94
sun_awt_image_GifImageDecoder_initIDs(void)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
static jmethodID readID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
static jmethodID sendID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
static jfieldID prefixID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
static jfieldID suffixID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
static jfieldID outCodeID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
Java_sun_awt_image_GifImageDecoder_initIDs(JNIEnv *env, jclass this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
{
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 5506
diff changeset
   107
    CHECK_NULL(readID = (*env)->GetMethodID(env, this, "readBytes", "([BII)I"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 5506
diff changeset
   108
    CHECK_NULL(sendID = (*env)->GetMethodID(env, this, "sendPixels",
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 5506
diff changeset
   109
                                 "(IIII[BLjava/awt/image/ColorModel;)I"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 5506
diff changeset
   110
    CHECK_NULL(prefixID = (*env)->GetFieldID(env, this, "prefix", "[S"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 5506
diff changeset
   111
    CHECK_NULL(suffixID = (*env)->GetFieldID(env, this, "suffix", "[B"));
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 5506
diff changeset
   112
    CHECK_NULL(outCodeID = (*env)->GetFieldID(env, this, "outCode", "[B"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
Java_sun_awt_image_GifImageDecoder_parseImage(JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                              jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                              jint relx, jint rely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                              jint width, jint height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                              jint interlace,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                                              jint initCodeSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                                              jbyteArray blockh,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                                              jbyteArray raslineh,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                              jobject cmh)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /* Patrick Naughton:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Note that I ignore the possible existence of a local color map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * I'm told there aren't many files around that use them, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * spec says it's defined for future use.  This could lead to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * error reading some files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Start reading the image data. First we get the intial code size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * and compute decompressor constant values, based on this code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * The GIF spec has it that the code size is the code size used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * compute the above values is the code size given in the file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * but the code size used in compression/decompression is the code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * size given in the file plus one. (thus the ++).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Arthur van Hoff:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * The following narly code reads LZW compressed data blocks and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * dumps it into the image data. The input stream is broken up into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * blocks of 1-255 characters, each preceded by a length byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * 3-12 bit codes are read from these blocks. The codes correspond to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * entry is the hashtable (the prefix, suffix stuff), and the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * pixels are written to the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    static int verbose = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    int clearCode = (1 << initCodeSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    int eofCode = clearCode + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    int bitMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    int curCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    int outCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /* Variables used to form reading data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    int blockEnd = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    int remain = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    int byteoff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    int accumbits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    int accumdata = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /* Variables used to decompress the data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    int codeSize = initCodeSize + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    int maxCode = 1 << codeSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    int codeMask = maxCode - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    int freeCode = clearCode + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    int code = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    int oldCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    unsigned char prevChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /* Temproray storage for decompression */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    short *prefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    unsigned char *suffix = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    unsigned char *outCode = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    unsigned char *rasline = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    unsigned char *block = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    jshortArray prefixh = (*env)->GetObjectField(env, this, prefixID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    jbyteArray suffixh = (*env)->GetObjectField(env, this, suffixID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    jbyteArray outCodeh = (*env)->GetObjectField(env, this, outCodeID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    int blockLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /* Variables used for writing pixels */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    int x = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    int y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    int off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    int passinc = interlace ? 8 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    int passht = passinc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
2610
9856ea68e32b 6804998: JRE GIF Decoding Heap Corruption [V-y6g5jlm8e1]
bae
parents: 2
diff changeset
   194
    /* We have verified the initial code size on the java layer.
9856ea68e32b 6804998: JRE GIF Decoding Heap Corruption [V-y6g5jlm8e1]
bae
parents: 2
diff changeset
   195
     * Here we just check bounds for particular indexes. */
9856ea68e32b 6804998: JRE GIF Decoding Heap Corruption [V-y6g5jlm8e1]
bae
parents: 2
diff changeset
   196
    if (freeCode >= 4096 || maxCode >= 4096) {
9856ea68e32b 6804998: JRE GIF Decoding Heap Corruption [V-y6g5jlm8e1]
bae
parents: 2
diff changeset
   197
        return 0;
9856ea68e32b 6804998: JRE GIF Decoding Heap Corruption [V-y6g5jlm8e1]
bae
parents: 2
diff changeset
   198
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    if (blockh == 0 || raslineh == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        || prefixh == 0 || suffixh == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        || outCodeh == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        JNU_ThrowNullPointerException(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    if (((*env)->GetArrayLength(env, prefixh) != 4096) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        ((*env)->GetArrayLength(env, suffixh) != 4096) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        ((*env)->GetArrayLength(env, outCodeh) != OUTCODELENGTH))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        JNU_ThrowArrayIndexOutOfBoundsException(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        fprintf(stdout, "Decompressing...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /* Fix for bugid 4216605 Some animated GIFs display corrupted. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    bitMask = clearCode - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    GET_ARRAYS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /* Read codes until the eofCode is encountered */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (accumbits < codeSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            /* fill the buffer if needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            while (remain < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                if (blockEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    /* Sometimes we have one last byte to process... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    if (remain == 1 && accumbits + 8 >= codeSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                        remain--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                        goto last_byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    RELEASE_ARRAYS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    if (off > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        (*env)->CallIntMethod(env, this, sendID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                              relx, rely + y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                              width, passht,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                              raslineh, cmh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    /* quietly accept truncated GIF images */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                /* move remaining bytes to the beginning of the buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                block[0] = block[byteoff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                byteoff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                RELEASE_ARRAYS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                /* fill the block */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                len = (*env)->CallIntMethod(env, this, readID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                            blockh, remain, blockLength + 1);
23892
9959956918be 8029854: Enhance JPEG decodings
prr
parents: 23644
diff changeset
   252
                if (len > blockLength + 1) len = blockLength + 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                if ((*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                GET_ARRAYS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                remain += blockLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    remain -= (len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    blockLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    blockLength = block[remain];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                if (blockLength == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    blockEnd = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            remain -= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            /* 2 bytes at a time saves checking for accumbits < codeSize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
             * We know we'll get enough and also that we can't overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
             * since codeSize <= 12.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            accumdata += (block[byteoff++] & 0xff) << accumbits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            accumbits += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        last_byte:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            accumdata += (block[byteoff++] & 0xff) << accumbits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            accumbits += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        /* Compute the code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        code = accumdata & codeMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        accumdata >>= codeSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        accumbits -= codeSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         * Interpret the code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (code == clearCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            /* Clear code sets everything back to its initial value, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
             * reads the immediately subsequent code as uncompressed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            if (verbose) {
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 5506
diff changeset
   295
                RELEASE_ARRAYS();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                fprintf(stdout, ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                fflush(stdout);
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 5506
diff changeset
   298
                GET_ARRAYS();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            /* Note that freeCode is one less than it is supposed to be,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
             * this is because it will be incremented next time round the loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            freeCode = clearCode + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            codeSize = initCodeSize + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            maxCode = 1 << codeSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            codeMask = maxCode - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            /* Continue if we've NOT reached the end, some Gif images
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
             * contain bogus codes after the last clear code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if (y < height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            /* pretend we've reached the end of the data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            code = eofCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (code == eofCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            /* make sure we read the whole block of pixels. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        flushit:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            while (!blockEnd) {
23644
0cdb97daeef5 8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents: 5506
diff changeset
   324
                RELEASE_ARRAYS();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    fprintf(stdout, "flushing %d bytes\n", blockLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                if ((*env)->CallIntMethod(env, this, readID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                          blockh, 0, blockLength + 1) != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    || (*env)->ExceptionOccurred(env))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    /* quietly accept truncated GIF images */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    return (!(*env)->ExceptionOccurred(env));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                GET_ARRAYS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                blockLength = block[blockLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                blockEnd = (blockLength == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            RELEASE_ARRAYS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        /* It must be data: save code in CurCode */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        curCode = code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        outCount = OUTCODELENGTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        /* If greater or equal to freeCode, not in the hash table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
         * yet; repeat the last character decoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        if (curCode >= freeCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            if (curCode > freeCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                 * if we get a code too far outside our range, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                 * could case the parser to start traversing parts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                 * of our data structure that are out of range...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                goto flushit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            curCode = oldCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            outCode[--outCount] = prevChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        /* Unless this code is raw data, pursue the chain pointed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
         * to by curCode through the hash table to its end; each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
         * code in the chain puts its associated output code on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
         * the output queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
         while (curCode > bitMask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
             outCode[--outCount] = suffix[curCode];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
             if (outCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                 /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                  * In theory this should never happen since our
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                  * prefix and suffix arrays are monotonically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                  * decreasing and so outCode will only be filled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                  * as much as those arrays, but I don't want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                  * take that chance and the test is probably
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                  * cheap compared to the read and write operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                  * If we ever do overflow the array, we will just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                  * flush the rest of the data and quietly accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                  * the GIF as truncated here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                 goto flushit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
             curCode = prefix[curCode];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        /* The last code in the chain is treated as raw data. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        prevChar = (unsigned char)curCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        outCode[--outCount] = prevChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        /* Now we put the data out to the Output routine. It's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
         * been stacked LIFO, so deal with it that way...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
         * Note that for some malformed images we have to skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
         * current frame and continue with rest of data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
         * because we may have not enough info to interpret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
         * corrupted frame correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
         * However, we can not skip frame without decoding it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         * and therefore we have to continue looping through data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         * but skip internal output loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         *
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 26751
diff changeset
   402
         * In particular this is possible when
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
         * width of the frame is set to zero. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
         * global width (i.e. width of the logical screen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
         * is zero too then zero-length scanline buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         * is allocated in java code and we have no buffer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         * store decoded data in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        len = OUTCODELENGTH - outCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        while ((width > 0) && (--len >= 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            rasline[off++] = outCode[outCount++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            /* Update the X-coordinate, and if it overflows, update the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
             * Y-coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            if (--x == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                /* If a non-interlaced picture, just increment y to the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                 * scan line.  If it's interlaced, deal with the interlace as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                 * described in the GIF spec.  Put the decoded scan line out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                 * to the screen if we haven't gone past the bottom of it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                RELEASE_ARRAYS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                count = (*env)->CallIntMethod(env, this, sendID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                                              relx, rely + y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                                              width, passht,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                                              raslineh, cmh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                if (count <= 0 || (*env)->ExceptionOccurred(env)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    /* Nobody is listening any more. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                        fprintf(stdout, "Orphan gif decoder quitting\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                GET_ARRAYS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                x = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                /*  pass        inc     ht      ystart */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                /*   0           8      8          0   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                /*   1           8      4          4   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                /*   2           4      2          2   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                /*   3           2      1          1   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                y += passinc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                while (y >= height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    passinc = passht;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    passht >>= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    y = passht;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    if (passht == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        goto flushit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        /* Build the hash table on-the-fly. No table is stored in the file. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        prefix[freeCode] = (short)oldCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        suffix[freeCode] = prevChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        oldCode = code;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        /* Point to the next slot in the table.  If we exceed the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
         * maxCode, increment the code size unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
         * it's already 12.  If it is, do nothing: the next code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
         * decompressed better be CLEAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (++freeCode >= maxCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            if (codeSize < 12) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                codeSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                maxCode <<= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                codeMask = maxCode - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                /* Just in case */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                freeCode = maxCode - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
out_of_memory:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    RELEASE_ARRAYS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
}