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
11 // Written by Tom Tromey <tromey@cygnus.com>
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
35 _Jv_ZMalloc (void *, uInt nitems
, uInt size
)
37 return _Jv_Malloc (nitems
* size
);
41 _Jv_ZFree (void *, void *addr
)
49 java::util::zip::Inflater::end ()
51 JvSynchronize
sync (this);
52 // Just ignore errors.
53 inflateEnd ((z_streamp
) zstream
);
59 java::util::zip::Inflater::getAdler ()
61 JvSynchronize
sync (this);
62 z_streamp s
= (z_streamp
) zstream
;
67 java::util::zip::Inflater::getRemaining ()
69 JvSynchronize
sync (this);
70 z_streamp s
= (z_streamp
) zstream
;
75 java::util::zip::Inflater::getTotalIn ()
77 JvSynchronize
sync (this);
78 z_streamp s
= (z_streamp
) zstream
;
83 java::util::zip::Inflater::getTotalOut ()
85 JvSynchronize
sync (this);
86 z_streamp s
= (z_streamp
) zstream
;
91 java::util::zip::Inflater::inflate (jbyteArray buf
, jint off
, jint len
)
93 JvSynchronize
sync (this);
94 z_streamp s
= (z_streamp
) zstream
;
97 throw new java::lang::NullPointerException
;
98 if (off
< 0 || len
< 0 || off
+ len
> buf
->length
)
99 throw new java::lang::ArrayIndexOutOfBoundsException
;
104 s
->next_out
= (Bytef
*) (elements (buf
) + off
);
107 switch (::inflate (s
, Z_SYNC_FLUSH
))
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
;
121 if (s
->avail_out
== (unsigned int) len
)
130 throw new java::util::zip::DataFormatException
131 (s
->msg
== NULL
? NULL
: JvNewStringLatin1 (s
->msg
));
135 throw new java::lang::OutOfMemoryError
;
142 return len
- s
->avail_out
;
146 java::util::zip::Inflater::reset ()
148 JvSynchronize
sync (this);
149 z_streamp s
= (z_streamp
) zstream
;
150 // Just ignore errors.
158 java::util::zip::Inflater::setDictionary (jbyteArray buf
, jint off
, jint len
)
160 JvSynchronize
sync (this);
161 z_streamp s
= (z_streamp
) zstream
;
164 throw new java::lang::NullPointerException
;
165 if (off
< 0 || len
< 0 || off
+ len
> buf
->length
)
166 throw new java::lang::ArrayIndexOutOfBoundsException
;
169 inflateSetDictionary (s
, (Bytef
*) (elements (buf
) + off
), len
);
174 java::util::zip::Inflater::setInput (jbyteArray buf
, jint off
, jint len
)
176 JvSynchronize
sync (this);
177 z_streamp s
= (z_streamp
) zstream
;
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
);
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
;
203 if (inflateInit2 (stream
, wbits
) != Z_OK
)
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
);