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 |
|
---|
32 | // A couple of helper functions used to interface with zlib's
|
---|
33 | // allocation.
|
---|
34 |
|
---|
35 | void *
|
---|
36 | _Jv_ZMalloc (void *, uInt nitems, uInt size)
|
---|
37 | {
|
---|
38 | return _Jv_Malloc (nitems * size);
|
---|
39 | }
|
---|
40 |
|
---|
41 | void
|
---|
42 | _Jv_ZFree (void *, void *addr)
|
---|
43 | {
|
---|
44 | _Jv_Free (addr);
|
---|
45 | }
|
---|
46 |
|
---|
47 | |
---|
48 |
|
---|
49 |
|
---|
50 | void
|
---|
51 | java::util::zip::Inflater::end ()
|
---|
52 | {
|
---|
53 | JvSynchronize sync (this);
|
---|
54 | // Just ignore errors.
|
---|
55 | inflateEnd ((z_streamp) zstream);
|
---|
56 | _Jv_Free (zstream);
|
---|
57 | zstream = NULL;
|
---|
58 | }
|
---|
59 |
|
---|
60 | jint
|
---|
61 | java::util::zip::Inflater::getAdler ()
|
---|
62 | {
|
---|
63 | JvSynchronize sync (this);
|
---|
64 | z_streamp s = (z_streamp) zstream;
|
---|
65 | return s->adler;
|
---|
66 | }
|
---|
67 |
|
---|
68 | jint
|
---|
69 | java::util::zip::Inflater::getRemaining ()
|
---|
70 | {
|
---|
71 | JvSynchronize sync (this);
|
---|
72 | z_streamp s = (z_streamp) zstream;
|
---|
73 | return s->avail_in;
|
---|
74 | }
|
---|
75 |
|
---|
76 | jint
|
---|
77 | java::util::zip::Inflater::getTotalIn ()
|
---|
78 | {
|
---|
79 | JvSynchronize sync (this);
|
---|
80 | z_streamp s = (z_streamp) zstream;
|
---|
81 | return s->total_in;
|
---|
82 | }
|
---|
83 |
|
---|
84 | jint
|
---|
85 | java::util::zip::Inflater::getTotalOut ()
|
---|
86 | {
|
---|
87 | JvSynchronize sync (this);
|
---|
88 | z_streamp s = (z_streamp) zstream;
|
---|
89 | return s->total_out;
|
---|
90 | }
|
---|
91 |
|
---|
92 | jint
|
---|
93 | java::util::zip::Inflater::inflate (jbyteArray buf, jint off, jint len)
|
---|
94 | {
|
---|
95 | JvSynchronize sync (this);
|
---|
96 | z_streamp s = (z_streamp) zstream;
|
---|
97 |
|
---|
98 | if (! buf)
|
---|
99 | throw new java::lang::NullPointerException;
|
---|
100 | if (off < 0 || len < 0 || off + len > buf->length)
|
---|
101 | throw new java::lang::ArrayIndexOutOfBoundsException;
|
---|
102 |
|
---|
103 | if (len == 0)
|
---|
104 | return 0;
|
---|
105 |
|
---|
106 | s->next_out = (Bytef *) (elements (buf) + off);
|
---|
107 | s->avail_out = len;
|
---|
108 |
|
---|
109 | switch (::inflate (s, Z_SYNC_FLUSH))
|
---|
110 | {
|
---|
111 | case Z_BUF_ERROR:
|
---|
112 | /* Using the no_header option, zlib requires an extra padding byte at the
|
---|
113 | end of the stream in order to successfully complete decompression (see
|
---|
114 | zlib/contrib/minizip/unzip.c). We don't do this, so can end up with a
|
---|
115 | Z_BUF_ERROR at the end of a stream when zlib has completed inflation
|
---|
116 | and there's no more input. Thats not a problem. */
|
---|
117 | if (s->avail_in != 0)
|
---|
118 | throw new java::lang::InternalError;
|
---|
119 | // Fall through.
|
---|
120 |
|
---|
121 | case Z_STREAM_END:
|
---|
122 | is_finished = true;
|
---|
123 | if (s->avail_out == (unsigned int) len)
|
---|
124 | return -1;
|
---|
125 | break;
|
---|
126 |
|
---|
127 | case Z_NEED_DICT:
|
---|
128 | dict_needed = true;
|
---|
129 | break;
|
---|
130 |
|
---|
131 | case Z_DATA_ERROR:
|
---|
132 | throw new java::util::zip::DataFormatException
|
---|
133 | (s->msg == NULL ? NULL : JvNewStringLatin1 (s->msg));
|
---|
134 | break;
|
---|
135 |
|
---|
136 | case Z_MEM_ERROR:
|
---|
137 | throw new java::lang::OutOfMemoryError;
|
---|
138 | break;
|
---|
139 |
|
---|
140 | case Z_OK:
|
---|
141 | break;
|
---|
142 | }
|
---|
143 |
|
---|
144 | return len - s->avail_out;
|
---|
145 | }
|
---|
146 |
|
---|
147 | void
|
---|
148 | java::util::zip::Inflater::reset ()
|
---|
149 | {
|
---|
150 | JvSynchronize sync (this);
|
---|
151 | z_streamp s = (z_streamp) zstream;
|
---|
152 | // Just ignore errors.
|
---|
153 | inflateReset (s);
|
---|
154 | s->avail_in = 0;
|
---|
155 | is_finished = false;
|
---|
156 | dict_needed = false;
|
---|
157 | }
|
---|
158 |
|
---|
159 | void
|
---|
160 | java::util::zip::Inflater::setDictionary (jbyteArray buf, jint off, jint len)
|
---|
161 | {
|
---|
162 | JvSynchronize sync (this);
|
---|
163 | z_streamp s = (z_streamp) zstream;
|
---|
164 |
|
---|
165 | if (! buf)
|
---|
166 | throw new java::lang::NullPointerException;
|
---|
167 | if (off < 0 || len < 0 || off + len > buf->length)
|
---|
168 | throw new java::lang::ArrayIndexOutOfBoundsException;
|
---|
169 |
|
---|
170 | // Ignore errors.
|
---|
171 | inflateSetDictionary (s, (Bytef *) (elements (buf) + off), len);
|
---|
172 | dict_needed = false;
|
---|
173 | }
|
---|
174 |
|
---|
175 | void
|
---|
176 | java::util::zip::Inflater::setInput (jbyteArray buf, jint off, jint len)
|
---|
177 | {
|
---|
178 | JvSynchronize sync (this);
|
---|
179 | z_streamp s = (z_streamp) zstream;
|
---|
180 |
|
---|
181 | if (! buf)
|
---|
182 | throw new java::lang::NullPointerException;
|
---|
183 | if (off < 0 || len < 0 || off + len > buf->length)
|
---|
184 | throw new java::lang::ArrayIndexOutOfBoundsException;
|
---|
185 |
|
---|
186 | s->next_in = (Bytef *) (elements (buf) + off);
|
---|
187 | s->avail_in = len;
|
---|
188 | }
|
---|
189 |
|
---|
190 | void
|
---|
191 | java::util::zip::Inflater::init (jboolean no_header)
|
---|
192 | {
|
---|
193 | z_stream_s *stream = (z_stream_s *) _Jv_Malloc (sizeof (z_stream_s));
|
---|
194 | stream->next_in = Z_NULL;
|
---|
195 | stream->avail_in = 0;
|
---|
196 | stream->zalloc = _Jv_ZMalloc;
|
---|
197 | stream->zfree = _Jv_ZFree;
|
---|
198 | stream->opaque = NULL;
|
---|
199 |
|
---|
200 | // Handle NO_HEADER using undocumented zlib feature.
|
---|
201 | int wbits = MAX_WBITS;
|
---|
202 | if (no_header)
|
---|
203 | wbits = - wbits;
|
---|
204 |
|
---|
205 | if (inflateInit2 (stream, wbits) != Z_OK)
|
---|
206 | {
|
---|
207 | jstring msg = NULL;
|
---|
208 | if (stream->msg != NULL)
|
---|
209 | msg = JvNewStringLatin1 (stream->msg);
|
---|
210 | throw new java::lang::InternalError (msg);
|
---|
211 | }
|
---|
212 |
|
---|
213 | zstream = reinterpret_cast<gnu::gcj::RawData *> (stream);
|
---|
214 | is_finished = false;
|
---|
215 | dict_needed = false;
|
---|
216 | }
|
---|