natInflater.cc 4.74 KB
Newer Older
1 2
// natInflater.cc - Implementation of Inflater native methods.

3
/* Copyright (C) 1999, 2002  Free Software Foundation
4 5 6 7 8 9 10 11 12 13 14 15

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

// Written by Tom Tromey <tromey@cygnus.com>

#include <config.h>

#include <zlib.h>
16
#include <stdlib.h>
17

Tom Tromey committed
18
#include <gcj/cni.h>
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#include <jvm.h>

#include <java/util/zip/Inflater.h>
#include <java/util/zip/DataFormatException.h>

#include <java/lang/InternalError.h>
#include <java/lang/NullPointerException.h>
#include <java/lang/ArrayIndexOutOfBoundsException.h>
#include <java/lang/OutOfMemoryError.h>



// A couple of helper functions used to interface with zlib's
// allocation.

void *
_Jv_ZMalloc (void *, uInt nitems, uInt size)
{
  return _Jv_Malloc (nitems * size);
}

void
_Jv_ZFree (void *, void *addr)
{
  _Jv_Free (addr);
}



void
java::util::zip::Inflater::end ()
{
  JvSynchronize sync (this);
  // Just ignore errors.
  inflateEnd ((z_streamp) zstream);
  _Jv_Free (zstream);
  zstream = NULL;
}

jint
java::util::zip::Inflater::getAdler ()
{
  JvSynchronize sync (this);
  z_streamp s = (z_streamp) zstream;
  return s->adler;
}

jint
java::util::zip::Inflater::getRemaining ()
{
  JvSynchronize sync (this);
  z_streamp s = (z_streamp) zstream;
71
  return s->avail_in;
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
}

jint
java::util::zip::Inflater::getTotalIn ()
{
  JvSynchronize sync (this);
  z_streamp s = (z_streamp) zstream;
  return s->total_in;
}

jint
java::util::zip::Inflater::getTotalOut ()
{
  JvSynchronize sync (this);
  z_streamp s = (z_streamp) zstream;
  return s->total_out;
}

jint
java::util::zip::Inflater::inflate (jbyteArray buf, jint off, jint len)
{
  JvSynchronize sync (this);
  z_streamp s = (z_streamp) zstream;

  if (! buf)
97
    throw new java::lang::NullPointerException;
98
  if (off < 0 || len < 0 || off + len > buf->length)
99
    throw new java::lang::ArrayIndexOutOfBoundsException;
100

101 102 103
  if (len == 0)
    return 0;

104 105 106 107 108
  s->next_out = (Bytef *) (elements (buf) + off);
  s->avail_out = len;

  switch (::inflate (s, Z_SYNC_FLUSH))
    {
109 110 111 112 113 114 115 116 117 118
    case Z_BUF_ERROR:
      /* Using the no_header option, zlib requires an extra padding byte at the
      end of the stream in order to successfully complete decompression (see
      zlib/contrib/minizip/unzip.c). We don't do this, so can end up with a 
      Z_BUF_ERROR at the end of a stream when zlib has completed inflation
      and there's no more input. Thats not a problem. */
      if (s->avail_in != 0)
        throw new java::lang::InternalError;
      // Fall through.
      
119 120
    case Z_STREAM_END:
      is_finished = true;
121
      if (s->avail_out == (unsigned int) len)
122 123 124 125 126 127 128 129
	return -1;
      break;

    case Z_NEED_DICT:
      dict_needed = true;
      break;

    case Z_DATA_ERROR:
130 131
      throw new java::util::zip::DataFormatException 
	(s->msg == NULL ? NULL : JvNewStringLatin1 (s->msg));
132 133 134
      break;

    case Z_MEM_ERROR:
135
      throw new java::lang::OutOfMemoryError;
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
      break;

    case Z_OK:
      break;
    }

  return len - s->avail_out;
}

void
java::util::zip::Inflater::reset ()
{
  JvSynchronize sync (this);
  z_streamp s = (z_streamp) zstream;
  // Just ignore errors.
  inflateReset (s);
152
  s->avail_in = 0;
153 154
  is_finished = false;
  dict_needed = false;
155 156 157 158 159 160 161 162 163
}

void
java::util::zip::Inflater::setDictionary (jbyteArray buf, jint off, jint len)
{
  JvSynchronize sync (this);
  z_streamp s = (z_streamp) zstream;

  if (! buf)
164
    throw new java::lang::NullPointerException;
165
  if (off < 0 || len < 0 || off + len > buf->length)
166
    throw new java::lang::ArrayIndexOutOfBoundsException;
167 168 169 170 171 172 173 174 175 176 177 178 179

  // Ignore errors.
  inflateSetDictionary (s, (Bytef *) (elements (buf) + off), len);
  dict_needed = false;
}

void
java::util::zip::Inflater::setInput (jbyteArray buf, jint off, jint len)
{
  JvSynchronize sync (this);
  z_streamp s = (z_streamp) zstream;

  if (! buf)
180
    throw new java::lang::NullPointerException;
181
  if (off < 0 || len < 0 || off + len > buf->length)
182
    throw new java::lang::ArrayIndexOutOfBoundsException;
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

  s->next_in = (Bytef *) (elements (buf) + off);
  s->avail_in = len;
}

void
java::util::zip::Inflater::init (jboolean no_header)
{
  z_stream_s *stream = (z_stream_s *) _Jv_Malloc (sizeof (z_stream_s));
  stream->next_in = Z_NULL;
  stream->avail_in = 0;
  stream->zalloc = _Jv_ZMalloc;
  stream->zfree = _Jv_ZFree;
  stream->opaque = NULL;

  // Handle NO_HEADER using undocumented zlib feature.
  int wbits = MAX_WBITS;
  if (no_header)
    wbits = - wbits;

  if (inflateInit2 (stream, wbits) != Z_OK)
    {
      jstring msg = NULL;
      if (stream->msg != NULL)
	msg = JvNewStringLatin1 (stream->msg);
208
      throw new java::lang::InternalError (msg);
209 210 211 212 213 214
    }

  zstream = reinterpret_cast<gnu::gcj::RawData *> (stream);
  is_finished = false;
  dict_needed = false;
}