1 // natInflater.cc - Implementation of Inflater native methods.
2 
3 /* Copyright (C) 1999, 2002  Free Software Foundation
4 
5    This file is part of libgcj.
6 
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10 
11 // Written by Tom Tromey <tromey@cygnus.com>
12 
13 #include <config.h>
14 
15 #include <zlib.h>
16 #include <stdlib.h>
17 
18 #include <gcj/cni.h>
19 #include <jvm.h>
20 
21 #include <java/util/zip/Inflater.h>
22 #include <java/util/zip/DataFormatException.h>
23 
24 #include <java/lang/InternalError.h>
25 #include <java/lang/NullPointerException.h>
26 #include <java/lang/ArrayIndexOutOfBoundsException.h>
27 #include <java/lang/OutOfMemoryError.h>
28 
29 
30 
31 // A couple of helper functions used to interface with zlib's
32 // allocation.
33 
34 void *
_Jv_ZMalloc(void *,uInt nitems,uInt size)35 _Jv_ZMalloc (void *, uInt nitems, uInt size)
36 {
37   return _Jv_Malloc (nitems * size);
38 }
39 
40 void
_Jv_ZFree(void *,void * addr)41 _Jv_ZFree (void *, void *addr)
42 {
43   _Jv_Free (addr);
44 }
45 
46 
47 
48 void
end()49 java::util::zip::Inflater::end ()
50 {
51   JvSynchronize sync (this);
52   // Just ignore errors.
53   inflateEnd ((z_streamp) zstream);
54   _Jv_Free (zstream);
55   zstream = NULL;
56 }
57 
58 jint
getAdler()59 java::util::zip::Inflater::getAdler ()
60 {
61   JvSynchronize sync (this);
62   z_streamp s = (z_streamp) zstream;
63   return s->adler;
64 }
65 
66 jint
getRemaining()67 java::util::zip::Inflater::getRemaining ()
68 {
69   JvSynchronize sync (this);
70   z_streamp s = (z_streamp) zstream;
71   return s->avail_in;
72 }
73 
74 jint
getTotalIn()75 java::util::zip::Inflater::getTotalIn ()
76 {
77   JvSynchronize sync (this);
78   z_streamp s = (z_streamp) zstream;
79   return s->total_in;
80 }
81 
82 jint
getTotalOut()83 java::util::zip::Inflater::getTotalOut ()
84 {
85   JvSynchronize sync (this);
86   z_streamp s = (z_streamp) zstream;
87   return s->total_out;
88 }
89 
90 jint
inflate(jbyteArray buf,jint off,jint len)91 java::util::zip::Inflater::inflate (jbyteArray buf, jint off, jint len)
92 {
93   JvSynchronize sync (this);
94   z_streamp s = (z_streamp) zstream;
95 
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;
100 
101   if (len == 0)
102     return 0;
103 
104   s->next_out = (Bytef *) (elements (buf) + off);
105   s->avail_out = len;
106 
107   switch (::inflate (s, Z_SYNC_FLUSH))
108     {
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.
118 
119     case Z_STREAM_END:
120       is_finished = true;
121       if (s->avail_out == (unsigned int) len)
122 	return -1;
123       break;
124 
125     case Z_NEED_DICT:
126       dict_needed = true;
127       break;
128 
129     case Z_DATA_ERROR:
130       throw new java::util::zip::DataFormatException
131 	(s->msg == NULL ? NULL : JvNewStringLatin1 (s->msg));
132       break;
133 
134     case Z_MEM_ERROR:
135       throw new java::lang::OutOfMemoryError;
136       break;
137 
138     case Z_OK:
139       break;
140     }
141 
142   return len - s->avail_out;
143 }
144 
145 void
reset()146 java::util::zip::Inflater::reset ()
147 {
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;
155 }
156 
157 void
setDictionary(jbyteArray buf,jint off,jint len)158 java::util::zip::Inflater::setDictionary (jbyteArray buf, jint off, jint len)
159 {
160   JvSynchronize sync (this);
161   z_streamp s = (z_streamp) zstream;
162 
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;
167 
168   // Ignore errors.
169   inflateSetDictionary (s, (Bytef *) (elements (buf) + off), len);
170   dict_needed = false;
171 }
172 
173 void
setInput(jbyteArray buf,jint off,jint len)174 java::util::zip::Inflater::setInput (jbyteArray buf, jint off, jint len)
175 {
176   JvSynchronize sync (this);
177   z_streamp s = (z_streamp) zstream;
178 
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;
183 
184   s->next_in = (Bytef *) (elements (buf) + off);
185   s->avail_in = len;
186 }
187 
188 void
init(jboolean no_header)189 java::util::zip::Inflater::init (jboolean no_header)
190 {
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;
197 
198   // Handle NO_HEADER using undocumented zlib feature.
199   int wbits = MAX_WBITS;
200   if (no_header)
201     wbits = - wbits;
202 
203   if (inflateInit2 (stream, wbits) != Z_OK)
204     {
205       jstring msg = NULL;
206       if (stream->msg != NULL)
207 	msg = JvNewStringLatin1 (stream->msg);
208       throw new java::lang::InternalError (msg);
209     }
210 
211   zstream = reinterpret_cast<gnu::gcj::RawData *> (stream);
212   is_finished = false;
213   dict_needed = false;
214 }
215