2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / util / zip / natInflater.cc
blob69de6c33560491f3eba8e0f30aff43b86cceaac4
1 // natInflater.cc - Implementation of Inflater native methods.
3 /* Copyright (C) 1999, 2002 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 // Written by Tom Tromey <tromey@cygnus.com>
13 #include <config.h>
15 #include <zlib.h>
16 #include <stdlib.h>
18 #include <gcj/cni.h>
19 #include <jvm.h>
21 #include <java/util/zip/Inflater.h>
22 #include <java/util/zip/DataFormatException.h>
24 #include <java/lang/InternalError.h>
25 #include <java/lang/NullPointerException.h>
26 #include <java/lang/ArrayIndexOutOfBoundsException.h>
27 #include <java/lang/OutOfMemoryError.h>
31 // A couple of helper functions used to interface with zlib's
32 // allocation.
34 void *
35 _Jv_ZMalloc (void *, uInt nitems, uInt size)
37 return _Jv_Malloc (nitems * size);
40 void
41 _Jv_ZFree (void *, void *addr)
43 _Jv_Free (addr);
48 void
49 java::util::zip::Inflater::end ()
51 JvSynchronize sync (this);
52 // Just ignore errors.
53 inflateEnd ((z_streamp) zstream);
54 _Jv_Free (zstream);
55 zstream = NULL;
58 jint
59 java::util::zip::Inflater::getAdler ()
61 JvSynchronize sync (this);
62 z_streamp s = (z_streamp) zstream;
63 return s->adler;
66 jint
67 java::util::zip::Inflater::getRemaining ()
69 JvSynchronize sync (this);
70 z_streamp s = (z_streamp) zstream;
71 return s->avail_in;
74 jint
75 java::util::zip::Inflater::getTotalIn ()
77 JvSynchronize sync (this);
78 z_streamp s = (z_streamp) zstream;
79 return s->total_in;
82 jint
83 java::util::zip::Inflater::getTotalOut ()
85 JvSynchronize sync (this);
86 z_streamp s = (z_streamp) zstream;
87 return s->total_out;
90 jint
91 java::util::zip::Inflater::inflate (jbyteArray buf, jint off, jint len)
93 JvSynchronize sync (this);
94 z_streamp s = (z_streamp) zstream;
96 if (! buf)
97 throw new java::lang::NullPointerException;
98 if (off < 0 || len < 0 || off + len > buf->length)
99 throw new java::lang::ArrayIndexOutOfBoundsException;
101 if (len == 0)
102 return 0;
104 s->next_out = (Bytef *) (elements (buf) + off);
105 s->avail_out = len;
107 switch (::inflate (s, Z_SYNC_FLUSH))
109 case Z_BUF_ERROR:
110 /* Using the no_header option, zlib requires an extra padding byte at the
111 end of the stream in order to successfully complete decompression (see
112 zlib/contrib/minizip/unzip.c). We don't do this, so can end up with a
113 Z_BUF_ERROR at the end of a stream when zlib has completed inflation
114 and there's no more input. Thats not a problem. */
115 if (s->avail_in != 0)
116 throw new java::lang::InternalError;
117 // Fall through.
119 case Z_STREAM_END:
120 is_finished = true;
121 if (s->avail_out == (unsigned int) len)
122 return -1;
123 break;
125 case Z_NEED_DICT:
126 dict_needed = true;
127 break;
129 case Z_DATA_ERROR:
130 throw new java::util::zip::DataFormatException
131 (s->msg == NULL ? NULL : JvNewStringLatin1 (s->msg));
132 break;
134 case Z_MEM_ERROR:
135 throw new java::lang::OutOfMemoryError;
136 break;
138 case Z_OK:
139 break;
142 return len - s->avail_out;
145 void
146 java::util::zip::Inflater::reset ()
148 JvSynchronize sync (this);
149 z_streamp s = (z_streamp) zstream;
150 // Just ignore errors.
151 inflateReset (s);
152 s->avail_in = 0;
153 is_finished = false;
154 dict_needed = false;
157 void
158 java::util::zip::Inflater::setDictionary (jbyteArray buf, jint off, jint len)
160 JvSynchronize sync (this);
161 z_streamp s = (z_streamp) zstream;
163 if (! buf)
164 throw new java::lang::NullPointerException;
165 if (off < 0 || len < 0 || off + len > buf->length)
166 throw new java::lang::ArrayIndexOutOfBoundsException;
168 // Ignore errors.
169 inflateSetDictionary (s, (Bytef *) (elements (buf) + off), len);
170 dict_needed = false;
173 void
174 java::util::zip::Inflater::setInput (jbyteArray buf, jint off, jint len)
176 JvSynchronize sync (this);
177 z_streamp s = (z_streamp) zstream;
179 if (! buf)
180 throw new java::lang::NullPointerException;
181 if (off < 0 || len < 0 || off + len > buf->length)
182 throw new java::lang::ArrayIndexOutOfBoundsException;
184 s->next_in = (Bytef *) (elements (buf) + off);
185 s->avail_in = len;
188 void
189 java::util::zip::Inflater::init (jboolean no_header)
191 z_stream_s *stream = (z_stream_s *) _Jv_Malloc (sizeof (z_stream_s));
192 stream->next_in = Z_NULL;
193 stream->avail_in = 0;
194 stream->zalloc = _Jv_ZMalloc;
195 stream->zfree = _Jv_ZFree;
196 stream->opaque = NULL;
198 // Handle NO_HEADER using undocumented zlib feature.
199 int wbits = MAX_WBITS;
200 if (no_header)
201 wbits = - wbits;
203 if (inflateInit2 (stream, wbits) != Z_OK)
205 jstring msg = NULL;
206 if (stream->msg != NULL)
207 msg = JvNewStringLatin1 (stream->msg);
208 throw new java::lang::InternalError (msg);
211 zstream = reinterpret_cast<gnu::gcj::RawData *> (stream);
212 is_finished = false;
213 dict_needed = false;