author | prr |
Fri, 20 Dec 2013 09:58:29 -0800 | |
changeset 23892 | 9959956918be |
parent 23644 | 0cdb97daeef5 |
child 23917 | d247e52b5262 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2 |
* Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
/* |
|
27 |
* This file contains the code to link the Java Image I/O JPEG plug-in |
|
28 |
* to the IJG library used to read and write JPEG files. Much of it has |
|
29 |
* been copied, updated, and annotated from the jpegdecoder.c AWT JPEG |
|
30 |
* decoder. Where that code was unclear, the present author has either |
|
31 |
* rewritten the relevant section or commented it for the sake of future |
|
32 |
* maintainers. |
|
33 |
* |
|
34 |
* In particular, the way the AWT code handled progressive JPEGs seems |
|
35 |
* to me to be only accidentally correct and somewhat inefficient. The |
|
36 |
* scheme used here represents the way I think it should work. (REV 11/00) |
|
37 |
*/ |
|
38 |
||
39 |
#include <stdlib.h> |
|
40 |
#include <setjmp.h> |
|
41 |
#include <assert.h> |
|
42 |
#include <string.h> |
|
8749 | 43 |
#include <limits.h> |
2 | 44 |
|
45 |
/* java native interface headers */ |
|
46 |
#include "jni.h" |
|
47 |
#include "jni_util.h" |
|
48 |
||
49 |
#include "com_sun_imageio_plugins_jpeg_JPEGImageReader.h" |
|
50 |
#include "com_sun_imageio_plugins_jpeg_JPEGImageWriter.h" |
|
51 |
||
52 |
/* headers from the JPEG library */ |
|
53 |
#include <jpeglib.h> |
|
54 |
#include "jerror.h" |
|
55 |
||
56 |
#undef MAX |
|
57 |
#define MAX(a,b) ((a) > (b) ? (a) : (b)) |
|
58 |
||
59 |
/* Cached Java method ids */ |
|
16881 | 60 |
static jmethodID JPEGImageReader_readInputDataID; |
61 |
static jmethodID JPEGImageReader_skipInputBytesID; |
|
2 | 62 |
static jmethodID JPEGImageReader_warningOccurredID; |
63 |
static jmethodID JPEGImageReader_warningWithMessageID; |
|
64 |
static jmethodID JPEGImageReader_setImageDataID; |
|
65 |
static jmethodID JPEGImageReader_acceptPixelsID; |
|
66 |
static jmethodID JPEGImageReader_pushBackID; |
|
67 |
static jmethodID JPEGImageReader_passStartedID; |
|
68 |
static jmethodID JPEGImageReader_passCompleteID; |
|
16882 | 69 |
static jmethodID JPEGImageWriter_writeOutputDataID; |
2 | 70 |
static jmethodID JPEGImageWriter_warningOccurredID; |
71 |
static jmethodID JPEGImageWriter_warningWithMessageID; |
|
72 |
static jmethodID JPEGImageWriter_writeMetadataID; |
|
73 |
static jmethodID JPEGImageWriter_grabPixelsID; |
|
74 |
static jfieldID JPEGQTable_tableID; |
|
75 |
static jfieldID JPEGHuffmanTable_lengthsID; |
|
76 |
static jfieldID JPEGHuffmanTable_valuesID; |
|
77 |
||
78 |
/* |
|
79 |
* Defined in jpegdecoder.c. Copy code from there if and |
|
80 |
* when that disappears. */ |
|
81 |
extern JavaVM *jvm; |
|
82 |
||
83 |
/* |
|
84 |
* The following sets of defines must match the warning messages in the |
|
85 |
* Java code. |
|
86 |
*/ |
|
87 |
||
88 |
/* Reader warnings */ |
|
89 |
#define READ_NO_EOI 0 |
|
90 |
||
91 |
/* Writer warnings */ |
|
92 |
||
93 |
/* Return codes for various ops */ |
|
94 |
#define OK 1 |
|
95 |
#define NOT_OK 0 |
|
96 |
||
97 |
/* |
|
98 |
* First we define two objects, one for the stream and buffer and one |
|
99 |
* for pixels. Both contain references to Java objects and pointers to |
|
100 |
* pinned arrays. These objects can be used for either input or |
|
101 |
* output. Pixels can be accessed as either INT32s or bytes. |
|
102 |
* Every I/O operation will have one of each these objects, one for |
|
103 |
* the stream and the other to hold pixels, regardless of the I/O direction. |
|
104 |
*/ |
|
105 |
||
106 |
/******************** StreamBuffer definition ************************/ |
|
107 |
||
108 |
typedef struct streamBufferStruct { |
|
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
109 |
jweak ioRef; // weak reference to a provider of I/O routines |
2 | 110 |
jbyteArray hstreamBuffer; // Handle to a Java buffer for the stream |
111 |
JOCTET *buf; // Pinned buffer pointer */ |
|
8355 | 112 |
size_t bufferOffset; // holds offset between unpin and the next pin |
113 |
size_t bufferLength; // Allocated, nut just used |
|
2 | 114 |
int suspendable; // Set to true to suspend input |
115 |
long remaining_skip; // Used only on input |
|
116 |
} streamBuffer, *streamBufferPtr; |
|
117 |
||
118 |
/* |
|
119 |
* This buffer size was set to 64K in the old classes, 4K by default in the |
|
120 |
* IJG library, with the comment "an efficiently freadable size", and 1K |
|
121 |
* in AWT. |
|
122 |
* Unlike in the other Java designs, these objects will persist, so 64K |
|
123 |
* seems too big and 1K seems too small. If 4K was good enough for the |
|
124 |
* IJG folks, it's good enough for me. |
|
125 |
*/ |
|
126 |
#define STREAMBUF_SIZE 4096 |
|
127 |
||
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
128 |
#define GET_IO_REF(io_name) \ |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
129 |
do { \ |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
130 |
if ((*env)->IsSameObject(env, sb->ioRef, NULL) || \ |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
131 |
((io_name) = (*env)->NewLocalRef(env, sb->ioRef)) == NULL) \ |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
132 |
{ \ |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
133 |
cinfo->err->error_exit((j_common_ptr) cinfo); \ |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
134 |
} \ |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
135 |
} while (0) \ |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
136 |
|
2 | 137 |
/* |
138 |
* Used to signal that no data need be restored from an unpin to a pin. |
|
139 |
* I.e. the buffer is empty. |
|
140 |
*/ |
|
8355 | 141 |
#define NO_DATA ((size_t)-1) |
2 | 142 |
|
143 |
// Forward reference |
|
144 |
static void resetStreamBuffer(JNIEnv *env, streamBufferPtr sb); |
|
145 |
||
146 |
/* |
|
147 |
* Initialize a freshly allocated StreamBuffer object. The stream is left |
|
148 |
* null, as it will be set from Java by setSource, but the buffer object |
|
149 |
* is created and a global reference kept. Returns OK on success, NOT_OK |
|
150 |
* if allocating the buffer or getting a global reference for it failed. |
|
151 |
*/ |
|
152 |
static int initStreamBuffer(JNIEnv *env, streamBufferPtr sb) { |
|
153 |
/* Initialize a new buffer */ |
|
154 |
jbyteArray hInputBuffer = (*env)->NewByteArray(env, STREAMBUF_SIZE); |
|
155 |
if (hInputBuffer == NULL) { |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
156 |
(*env)->ExceptionClear(env); |
2 | 157 |
JNU_ThrowByName( env, |
158 |
"java/lang/OutOfMemoryError", |
|
159 |
"Initializing Reader"); |
|
160 |
return NOT_OK; |
|
161 |
} |
|
162 |
sb->bufferLength = (*env)->GetArrayLength(env, hInputBuffer); |
|
163 |
sb->hstreamBuffer = (*env)->NewGlobalRef(env, hInputBuffer); |
|
164 |
if (sb->hstreamBuffer == NULL) { |
|
165 |
JNU_ThrowByName( env, |
|
166 |
"java/lang/OutOfMemoryError", |
|
167 |
"Initializing Reader"); |
|
168 |
return NOT_OK; |
|
169 |
} |
|
170 |
||
171 |
||
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
172 |
sb->ioRef = NULL; |
2 | 173 |
|
174 |
sb->buf = NULL; |
|
175 |
||
176 |
resetStreamBuffer(env, sb); |
|
177 |
||
178 |
return OK; |
|
179 |
} |
|
180 |
||
181 |
/* |
|
182 |
* Free all resources associated with this streamBuffer. This must |
|
183 |
* be called to dispose the object to avoid leaking global references, as |
|
184 |
* resetStreamBuffer does not release the buffer reference. |
|
185 |
*/ |
|
186 |
static void destroyStreamBuffer(JNIEnv *env, streamBufferPtr sb) { |
|
187 |
resetStreamBuffer(env, sb); |
|
188 |
if (sb->hstreamBuffer != NULL) { |
|
189 |
(*env)->DeleteGlobalRef(env, sb->hstreamBuffer); |
|
190 |
} |
|
191 |
} |
|
192 |
||
193 |
// Forward reference |
|
194 |
static void unpinStreamBuffer(JNIEnv *env, |
|
195 |
streamBufferPtr sb, |
|
196 |
const JOCTET *next_byte); |
|
197 |
/* |
|
198 |
* Resets the state of a streamBuffer object that has been in use. |
|
199 |
* The global reference to the stream is released, but the reference |
|
200 |
* to the buffer is retained. The buffer is unpinned if it was pinned. |
|
201 |
* All other state is reset. |
|
202 |
*/ |
|
203 |
static void resetStreamBuffer(JNIEnv *env, streamBufferPtr sb) { |
|
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
204 |
if (sb->ioRef != NULL) { |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
205 |
(*env)->DeleteWeakGlobalRef(env, sb->ioRef); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
206 |
sb->ioRef = NULL; |
2 | 207 |
} |
208 |
unpinStreamBuffer(env, sb, NULL); |
|
209 |
sb->bufferOffset = NO_DATA; |
|
210 |
sb->suspendable = FALSE; |
|
211 |
sb->remaining_skip = 0; |
|
212 |
} |
|
213 |
||
214 |
/* |
|
215 |
* Pins the data buffer associated with this stream. Returns OK on |
|
216 |
* success, NOT_OK on failure, as GetPrimitiveArrayCritical may fail. |
|
217 |
*/ |
|
218 |
static int pinStreamBuffer(JNIEnv *env, |
|
219 |
streamBufferPtr sb, |
|
220 |
const JOCTET **next_byte) { |
|
221 |
if (sb->hstreamBuffer != NULL) { |
|
222 |
assert(sb->buf == NULL); |
|
223 |
sb->buf = |
|
224 |
(JOCTET *)(*env)->GetPrimitiveArrayCritical(env, |
|
225 |
sb->hstreamBuffer, |
|
226 |
NULL); |
|
227 |
if (sb->buf == NULL) { |
|
228 |
return NOT_OK; |
|
229 |
} |
|
230 |
if (sb->bufferOffset != NO_DATA) { |
|
231 |
*next_byte = sb->buf + sb->bufferOffset; |
|
232 |
} |
|
233 |
} |
|
234 |
return OK; |
|
235 |
} |
|
236 |
||
237 |
/* |
|
238 |
* Unpins the data buffer associated with this stream. |
|
239 |
*/ |
|
240 |
static void unpinStreamBuffer(JNIEnv *env, |
|
241 |
streamBufferPtr sb, |
|
242 |
const JOCTET *next_byte) { |
|
243 |
if (sb->buf != NULL) { |
|
244 |
assert(sb->hstreamBuffer != NULL); |
|
245 |
if (next_byte == NULL) { |
|
246 |
sb->bufferOffset = NO_DATA; |
|
247 |
} else { |
|
248 |
sb->bufferOffset = next_byte - sb->buf; |
|
249 |
} |
|
250 |
(*env)->ReleasePrimitiveArrayCritical(env, |
|
251 |
sb->hstreamBuffer, |
|
252 |
sb->buf, |
|
253 |
0); |
|
254 |
sb->buf = NULL; |
|
255 |
} |
|
256 |
} |
|
257 |
||
258 |
/* |
|
259 |
* Clear out the streamBuffer. This just invalidates the data in the buffer. |
|
260 |
*/ |
|
261 |
static void clearStreamBuffer(streamBufferPtr sb) { |
|
262 |
sb->bufferOffset = NO_DATA; |
|
263 |
} |
|
264 |
||
265 |
/*************************** end StreamBuffer definition *************/ |
|
266 |
||
267 |
/*************************** Pixel Buffer definition ******************/ |
|
268 |
||
269 |
typedef struct pixelBufferStruct { |
|
270 |
jobject hpixelObject; // Usually a DataBuffer bank as a byte array |
|
5189
4909c522e208
6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents:
4285
diff
changeset
|
271 |
unsigned int byteBufferLength; |
2 | 272 |
union pixptr { |
273 |
INT32 *ip; // Pinned buffer pointer, as 32-bit ints |
|
274 |
unsigned char *bp; // Pinned buffer pointer, as bytes |
|
275 |
} buf; |
|
276 |
} pixelBuffer, *pixelBufferPtr; |
|
277 |
||
278 |
/* |
|
279 |
* Initialize a freshly allocated PixelBuffer. All fields are simply |
|
280 |
* set to NULL, as we have no idea what size buffer we will need. |
|
281 |
*/ |
|
282 |
static void initPixelBuffer(pixelBufferPtr pb) { |
|
283 |
pb->hpixelObject = NULL; |
|
5189
4909c522e208
6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents:
4285
diff
changeset
|
284 |
pb->byteBufferLength = 0; |
2 | 285 |
pb->buf.ip = NULL; |
286 |
} |
|
287 |
||
288 |
/* |
|
289 |
* Set the pixelBuffer to use the given buffer, acquiring a new global |
|
290 |
* reference for it. Returns OK on success, NOT_OK on failure. |
|
291 |
*/ |
|
292 |
static int setPixelBuffer(JNIEnv *env, pixelBufferPtr pb, jobject obj) { |
|
293 |
pb->hpixelObject = (*env)->NewGlobalRef(env, obj); |
|
294 |
if (pb->hpixelObject == NULL) { |
|
295 |
JNU_ThrowByName( env, |
|
296 |
"java/lang/OutOfMemoryError", |
|
297 |
"Setting Pixel Buffer"); |
|
298 |
return NOT_OK; |
|
299 |
} |
|
5189
4909c522e208
6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents:
4285
diff
changeset
|
300 |
pb->byteBufferLength = (*env)->GetArrayLength(env, pb->hpixelObject); |
2 | 301 |
return OK; |
302 |
} |
|
303 |
||
304 |
// Forward reference |
|
305 |
static void unpinPixelBuffer(JNIEnv *env, pixelBufferPtr pb); |
|
306 |
||
307 |
/* |
|
308 |
* Resets a pixel buffer to its initial state. Unpins any pixel buffer, |
|
309 |
* releases the global reference, and resets fields to NULL. Use this |
|
310 |
* method to dispose the object as well (there is no destroyPixelBuffer). |
|
311 |
*/ |
|
312 |
static void resetPixelBuffer(JNIEnv *env, pixelBufferPtr pb) { |
|
313 |
if (pb->hpixelObject != NULL) { |
|
314 |
unpinPixelBuffer(env, pb); |
|
315 |
(*env)->DeleteGlobalRef(env, pb->hpixelObject); |
|
316 |
pb->hpixelObject = NULL; |
|
5189
4909c522e208
6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents:
4285
diff
changeset
|
317 |
pb->byteBufferLength = 0; |
2 | 318 |
} |
319 |
} |
|
320 |
||
321 |
/* |
|
322 |
* Pins the data buffer. Returns OK on success, NOT_OK on failure. |
|
323 |
*/ |
|
324 |
static int pinPixelBuffer(JNIEnv *env, pixelBufferPtr pb) { |
|
325 |
if (pb->hpixelObject != NULL) { |
|
326 |
assert(pb->buf.ip == NULL); |
|
327 |
pb->buf.bp = (unsigned char *)(*env)->GetPrimitiveArrayCritical |
|
328 |
(env, pb->hpixelObject, NULL); |
|
329 |
if (pb->buf.bp == NULL) { |
|
330 |
return NOT_OK; |
|
331 |
} |
|
332 |
} |
|
333 |
return OK; |
|
334 |
} |
|
335 |
||
336 |
/* |
|
337 |
* Unpins the data buffer. |
|
338 |
*/ |
|
339 |
static void unpinPixelBuffer(JNIEnv *env, pixelBufferPtr pb) { |
|
340 |
||
341 |
if (pb->buf.ip != NULL) { |
|
342 |
assert(pb->hpixelObject != NULL); |
|
343 |
(*env)->ReleasePrimitiveArrayCritical(env, |
|
344 |
pb->hpixelObject, |
|
345 |
pb->buf.ip, |
|
346 |
0); |
|
347 |
pb->buf.ip = NULL; |
|
348 |
} |
|
349 |
} |
|
350 |
||
351 |
/********************* end PixelBuffer definition *******************/ |
|
352 |
||
353 |
/********************* ImageIOData definition ***********************/ |
|
354 |
||
355 |
#define MAX_BANDS 4 |
|
356 |
#define JPEG_BAND_SIZE 8 |
|
357 |
#define NUM_BAND_VALUES (1<<JPEG_BAND_SIZE) |
|
358 |
#define MAX_JPEG_BAND_VALUE (NUM_BAND_VALUES-1) |
|
359 |
#define HALF_MAX_JPEG_BAND_VALUE (MAX_JPEG_BAND_VALUE>>1) |
|
360 |
||
361 |
/* The number of possible incoming values to be scaled. */ |
|
362 |
#define NUM_INPUT_VALUES (1 << 16) |
|
363 |
||
364 |
/* |
|
365 |
* The principal imageioData object, opaque to I/O direction. |
|
366 |
* Each JPEGImageReader will have associated with it a |
|
367 |
* jpeg_decompress_struct, and similarly each JPEGImageWriter will |
|
368 |
* have associated with it a jpeg_compress_struct. In order to |
|
369 |
* ensure that these associations persist from one native call to |
|
370 |
* the next, and to provide a central locus of imageio-specific |
|
371 |
* data, we define an imageioData struct containing references |
|
372 |
* to the Java object and the IJG structs. The functions |
|
373 |
* that manipulate these objects know whether input or output is being |
|
374 |
* performed and therefore know how to manipulate the contents correctly. |
|
375 |
* If for some reason they don't, the direction can be determined by |
|
376 |
* checking the is_decompressor field of the jpegObj. |
|
377 |
* In order for lower level code to determine a |
|
378 |
* Java object given an IJG struct, such as for dispatching warnings, |
|
379 |
* we use the client_data field of the jpeg object to store a pointer |
|
380 |
* to the imageIOData object. Maintenance of this pointer is performed |
|
381 |
* exclusively within the following access functions. If you |
|
382 |
* change that, you run the risk of dangling pointers. |
|
383 |
*/ |
|
384 |
typedef struct imageIODataStruct { |
|
385 |
j_common_ptr jpegObj; // Either struct is fine |
|
386 |
jobject imageIOobj; // A JPEGImageReader or a JPEGImageWriter |
|
387 |
||
388 |
streamBuffer streamBuf; // Buffer for the stream |
|
389 |
pixelBuffer pixelBuf; // Buffer for pixels |
|
390 |
||
391 |
jboolean abortFlag; // Passed down from Java abort method |
|
392 |
} imageIOData, *imageIODataPtr; |
|
393 |
||
394 |
/* |
|
395 |
* Allocate and initialize a new imageIOData object to associate the |
|
396 |
* jpeg object and the Java object. Returns a pointer to the new object |
|
397 |
* on success, NULL on failure. |
|
398 |
*/ |
|
399 |
static imageIODataPtr initImageioData (JNIEnv *env, |
|
400 |
j_common_ptr cinfo, |
|
401 |
jobject obj) { |
|
402 |
||
403 |
imageIODataPtr data = (imageIODataPtr) malloc (sizeof(imageIOData)); |
|
404 |
if (data == NULL) { |
|
405 |
return NULL; |
|
406 |
} |
|
407 |
||
408 |
data->jpegObj = cinfo; |
|
409 |
cinfo->client_data = data; |
|
410 |
||
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
411 |
#ifdef DEBUG_IIO_JPEG |
2 | 412 |
printf("new structures: data is %p, cinfo is %p\n", data, cinfo); |
413 |
#endif |
|
414 |
||
415 |
data->imageIOobj = (*env)->NewWeakGlobalRef(env, obj); |
|
416 |
if (data->imageIOobj == NULL) { |
|
417 |
free (data); |
|
418 |
return NULL; |
|
419 |
} |
|
420 |
if (initStreamBuffer(env, &data->streamBuf) == NOT_OK) { |
|
421 |
(*env)->DeleteWeakGlobalRef(env, data->imageIOobj); |
|
422 |
free (data); |
|
423 |
return NULL; |
|
424 |
} |
|
425 |
initPixelBuffer(&data->pixelBuf); |
|
426 |
||
427 |
data->abortFlag = JNI_FALSE; |
|
428 |
||
429 |
return data; |
|
430 |
} |
|
431 |
||
432 |
/* |
|
433 |
* Resets the imageIOData object to its initial state, as though |
|
434 |
* it had just been allocated and initialized. |
|
435 |
*/ |
|
436 |
static void resetImageIOData(JNIEnv *env, imageIODataPtr data) { |
|
437 |
resetStreamBuffer(env, &data->streamBuf); |
|
438 |
resetPixelBuffer(env, &data->pixelBuf); |
|
439 |
data->abortFlag = JNI_FALSE; |
|
440 |
} |
|
441 |
||
442 |
/* |
|
443 |
* Releases all resources held by this object and its subobjects, |
|
444 |
* frees the object, and returns the jpeg object. This method must |
|
445 |
* be called to avoid leaking global references. |
|
446 |
* Note that the jpeg object is not freed or destroyed, as that is |
|
447 |
* the client's responsibility, although the client_data field is |
|
448 |
* cleared. |
|
449 |
*/ |
|
450 |
static j_common_ptr destroyImageioData(JNIEnv *env, imageIODataPtr data) { |
|
451 |
j_common_ptr ret = data->jpegObj; |
|
452 |
(*env)->DeleteWeakGlobalRef(env, data->imageIOobj); |
|
453 |
destroyStreamBuffer(env, &data->streamBuf); |
|
454 |
resetPixelBuffer(env, &data->pixelBuf); |
|
455 |
ret->client_data = NULL; |
|
456 |
free(data); |
|
457 |
return ret; |
|
458 |
} |
|
459 |
||
460 |
/******************** end ImageIOData definition ***********************/ |
|
461 |
||
462 |
/******************** Java array pinning and unpinning *****************/ |
|
463 |
||
464 |
/* We use Get/ReleasePrimitiveArrayCritical functions to avoid |
|
465 |
* the need to copy array elements for the above two objects. |
|
466 |
* |
|
467 |
* MAKE SURE TO: |
|
468 |
* |
|
469 |
* - carefully insert pairs of RELEASE_ARRAYS and GET_ARRAYS around |
|
470 |
* callbacks to Java. |
|
471 |
* - call RELEASE_ARRAYS before returning to Java. |
|
472 |
* |
|
473 |
* Otherwise things will go horribly wrong. There may be memory leaks, |
|
474 |
* excessive pinning, or even VM crashes! |
|
475 |
* |
|
476 |
* Note that GetPrimitiveArrayCritical may fail! |
|
477 |
*/ |
|
478 |
||
479 |
/* |
|
480 |
* Release (unpin) all the arrays in use during a read. |
|
481 |
*/ |
|
482 |
static void RELEASE_ARRAYS(JNIEnv *env, imageIODataPtr data, const JOCTET *next_byte) |
|
483 |
{ |
|
484 |
unpinStreamBuffer(env, &data->streamBuf, next_byte); |
|
485 |
||
486 |
unpinPixelBuffer(env, &data->pixelBuf); |
|
487 |
||
488 |
} |
|
489 |
||
490 |
/* |
|
491 |
* Get (pin) all the arrays in use during a read. |
|
492 |
*/ |
|
493 |
static int GET_ARRAYS(JNIEnv *env, imageIODataPtr data, const JOCTET **next_byte) { |
|
494 |
if (pinStreamBuffer(env, &data->streamBuf, next_byte) == NOT_OK) { |
|
495 |
return NOT_OK; |
|
496 |
} |
|
497 |
||
498 |
if (pinPixelBuffer(env, &data->pixelBuf) == NOT_OK) { |
|
499 |
RELEASE_ARRAYS(env, data, *next_byte); |
|
500 |
return NOT_OK; |
|
501 |
} |
|
502 |
return OK; |
|
503 |
} |
|
504 |
||
505 |
/****** end of Java array pinning and unpinning ***********/ |
|
506 |
||
507 |
/****** Error Handling *******/ |
|
508 |
||
509 |
/* |
|
510 |
* Set up error handling to use setjmp/longjmp. This is the third such |
|
511 |
* setup, as both the AWT jpeg decoder and the com.sun... JPEG classes |
|
512 |
* setup thier own. Ultimately these should be integrated, as they all |
|
513 |
* do pretty much the same thing. |
|
514 |
*/ |
|
515 |
||
516 |
struct sun_jpeg_error_mgr { |
|
517 |
struct jpeg_error_mgr pub; /* "public" fields */ |
|
518 |
||
519 |
jmp_buf setjmp_buffer; /* for return to caller */ |
|
520 |
}; |
|
521 |
||
522 |
typedef struct sun_jpeg_error_mgr * sun_jpeg_error_ptr; |
|
523 |
||
524 |
/* |
|
525 |
* Here's the routine that will replace the standard error_exit method: |
|
526 |
*/ |
|
527 |
||
528 |
METHODDEF(void) |
|
529 |
sun_jpeg_error_exit (j_common_ptr cinfo) |
|
530 |
{ |
|
531 |
/* cinfo->err really points to a sun_jpeg_error_mgr struct */ |
|
532 |
sun_jpeg_error_ptr myerr = (sun_jpeg_error_ptr) cinfo->err; |
|
533 |
||
534 |
/* For Java, we will format the message and put it in the error we throw. */ |
|
535 |
||
536 |
/* Return control to the setjmp point */ |
|
537 |
longjmp(myerr->setjmp_buffer, 1); |
|
538 |
} |
|
539 |
||
540 |
/* |
|
541 |
* Error Message handling |
|
542 |
* |
|
543 |
* This overrides the output_message method to send JPEG messages |
|
544 |
* |
|
545 |
*/ |
|
546 |
||
547 |
METHODDEF(void) |
|
548 |
sun_jpeg_output_message (j_common_ptr cinfo) |
|
549 |
{ |
|
550 |
char buffer[JMSG_LENGTH_MAX]; |
|
551 |
jstring string; |
|
552 |
imageIODataPtr data = (imageIODataPtr) cinfo->client_data; |
|
553 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
554 |
jobject theObject; |
|
555 |
||
556 |
/* Create the message */ |
|
557 |
(*cinfo->err->format_message) (cinfo, buffer); |
|
558 |
||
559 |
// Create a new java string from the message |
|
560 |
string = (*env)->NewStringUTF(env, buffer); |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
561 |
CHECK_NULL(string); |
2 | 562 |
|
563 |
theObject = data->imageIOobj; |
|
564 |
||
565 |
if (cinfo->is_decompressor) { |
|
566 |
(*env)->CallVoidMethod(env, theObject, |
|
567 |
JPEGImageReader_warningWithMessageID, |
|
568 |
string); |
|
569 |
} else { |
|
570 |
(*env)->CallVoidMethod(env, theObject, |
|
571 |
JPEGImageWriter_warningWithMessageID, |
|
572 |
string); |
|
573 |
} |
|
574 |
} |
|
575 |
||
576 |
/* End of verbatim copy from jpegdecoder.c */ |
|
577 |
||
578 |
/*************** end of error handling *********************/ |
|
579 |
||
580 |
/*************** Shared utility code ***********************/ |
|
581 |
||
582 |
static void imageio_set_stream(JNIEnv *env, |
|
583 |
j_common_ptr cinfo, |
|
584 |
imageIODataPtr data, |
|
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
585 |
jobject io){ |
2 | 586 |
streamBufferPtr sb; |
587 |
sun_jpeg_error_ptr jerr; |
|
588 |
||
589 |
sb = &data->streamBuf; |
|
590 |
||
591 |
resetStreamBuffer(env, sb); // Removes any old stream |
|
592 |
||
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
593 |
/* Now we need a new weak global reference for the I/O provider */ |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
594 |
if (io != NULL) { // Fix for 4411955 |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
595 |
sb->ioRef = (*env)->NewWeakGlobalRef(env, io); |
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
596 |
CHECK_NULL(sb->ioRef); |
2 | 597 |
} |
598 |
||
599 |
/* And finally reset state */ |
|
600 |
data->abortFlag = JNI_FALSE; |
|
601 |
||
602 |
/* Establish the setjmp return context for sun_jpeg_error_exit to use. */ |
|
603 |
jerr = (sun_jpeg_error_ptr) cinfo->err; |
|
604 |
||
605 |
if (setjmp(jerr->setjmp_buffer)) { |
|
606 |
/* If we get here, the JPEG code has signaled an error |
|
607 |
while aborting. */ |
|
608 |
if (!(*env)->ExceptionOccurred(env)) { |
|
609 |
char buffer[JMSG_LENGTH_MAX]; |
|
610 |
(*cinfo->err->format_message) (cinfo, |
|
611 |
buffer); |
|
612 |
JNU_ThrowByName(env, "javax/imageio/IIOException", buffer); |
|
613 |
} |
|
614 |
return; |
|
615 |
} |
|
616 |
||
617 |
jpeg_abort(cinfo); // Frees any markers, but not tables |
|
618 |
||
619 |
} |
|
620 |
||
621 |
static void imageio_reset(JNIEnv *env, |
|
622 |
j_common_ptr cinfo, |
|
623 |
imageIODataPtr data) { |
|
624 |
sun_jpeg_error_ptr jerr; |
|
625 |
||
626 |
resetImageIOData(env, data); // Mapping to jpeg object is retained. |
|
627 |
||
628 |
/* Establish the setjmp return context for sun_jpeg_error_exit to use. */ |
|
629 |
jerr = (sun_jpeg_error_ptr) cinfo->err; |
|
630 |
||
631 |
if (setjmp(jerr->setjmp_buffer)) { |
|
632 |
/* If we get here, the JPEG code has signaled an error |
|
633 |
while aborting. */ |
|
634 |
if (!(*env)->ExceptionOccurred(env)) { |
|
635 |
char buffer[JMSG_LENGTH_MAX]; |
|
636 |
(*cinfo->err->format_message) (cinfo, buffer); |
|
637 |
JNU_ThrowByName(env, "javax/imageio/IIOException", buffer); |
|
638 |
} |
|
639 |
return; |
|
640 |
} |
|
641 |
||
642 |
jpeg_abort(cinfo); // Does not reset tables |
|
643 |
||
644 |
} |
|
645 |
||
646 |
static void imageio_dispose(j_common_ptr info) { |
|
647 |
||
648 |
if (info != NULL) { |
|
649 |
free(info->err); |
|
650 |
info->err = NULL; |
|
651 |
if (info->is_decompressor) { |
|
652 |
j_decompress_ptr dinfo = (j_decompress_ptr) info; |
|
653 |
free(dinfo->src); |
|
654 |
dinfo->src = NULL; |
|
655 |
} else { |
|
656 |
j_compress_ptr cinfo = (j_compress_ptr) info; |
|
657 |
free(cinfo->dest); |
|
658 |
cinfo->dest = NULL; |
|
659 |
} |
|
660 |
jpeg_destroy(info); |
|
661 |
free(info); |
|
662 |
} |
|
663 |
} |
|
664 |
||
665 |
static void imageio_abort(JNIEnv *env, jobject this, |
|
666 |
imageIODataPtr data) { |
|
667 |
data->abortFlag = JNI_TRUE; |
|
668 |
} |
|
669 |
||
670 |
static int setQTables(JNIEnv *env, |
|
671 |
j_common_ptr cinfo, |
|
672 |
jobjectArray qtables, |
|
673 |
boolean write) { |
|
674 |
jsize qlen; |
|
675 |
jobject table; |
|
676 |
jintArray qdata; |
|
677 |
jint *qdataBody; |
|
678 |
JQUANT_TBL *quant_ptr; |
|
679 |
int i, j; |
|
680 |
j_compress_ptr comp; |
|
681 |
j_decompress_ptr decomp; |
|
682 |
||
683 |
qlen = (*env)->GetArrayLength(env, qtables); |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
684 |
#ifdef DEBUG_IIO_JPEG |
2 | 685 |
printf("in setQTables, qlen = %d, write is %d\n", qlen, write); |
686 |
#endif |
|
4203 | 687 |
if (qlen > NUM_QUANT_TBLS) { |
688 |
/* Ignore extra qunterization tables. */ |
|
689 |
qlen = NUM_QUANT_TBLS; |
|
690 |
} |
|
2 | 691 |
for (i = 0; i < qlen; i++) { |
692 |
table = (*env)->GetObjectArrayElement(env, qtables, i); |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
693 |
CHECK_NULL_RETURN(table, 0); |
2 | 694 |
qdata = (*env)->GetObjectField(env, table, JPEGQTable_tableID); |
695 |
qdataBody = (*env)->GetPrimitiveArrayCritical(env, qdata, NULL); |
|
696 |
||
697 |
if (cinfo->is_decompressor) { |
|
698 |
decomp = (j_decompress_ptr) cinfo; |
|
699 |
if (decomp->quant_tbl_ptrs[i] == NULL) { |
|
700 |
decomp->quant_tbl_ptrs[i] = |
|
701 |
jpeg_alloc_quant_table(cinfo); |
|
702 |
} |
|
703 |
quant_ptr = decomp->quant_tbl_ptrs[i]; |
|
704 |
} else { |
|
705 |
comp = (j_compress_ptr) cinfo; |
|
706 |
if (comp->quant_tbl_ptrs[i] == NULL) { |
|
707 |
comp->quant_tbl_ptrs[i] = |
|
708 |
jpeg_alloc_quant_table(cinfo); |
|
709 |
} |
|
710 |
quant_ptr = comp->quant_tbl_ptrs[i]; |
|
711 |
} |
|
712 |
||
713 |
for (j = 0; j < 64; j++) { |
|
714 |
quant_ptr->quantval[j] = (UINT16)qdataBody[j]; |
|
715 |
} |
|
716 |
quant_ptr->sent_table = !write; |
|
717 |
(*env)->ReleasePrimitiveArrayCritical(env, |
|
718 |
qdata, |
|
719 |
qdataBody, |
|
720 |
0); |
|
721 |
} |
|
722 |
return qlen; |
|
723 |
} |
|
724 |
||
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
725 |
static boolean setHuffTable(JNIEnv *env, |
2 | 726 |
JHUFF_TBL *huff_ptr, |
727 |
jobject table) { |
|
728 |
||
729 |
jshortArray huffLens; |
|
730 |
jshortArray huffValues; |
|
731 |
jshort *hlensBody, *hvalsBody; |
|
732 |
jsize hlensLen, hvalsLen; |
|
733 |
int i; |
|
734 |
||
735 |
// lengths |
|
736 |
huffLens = (*env)->GetObjectField(env, |
|
737 |
table, |
|
738 |
JPEGHuffmanTable_lengthsID); |
|
739 |
hlensLen = (*env)->GetArrayLength(env, huffLens); |
|
740 |
hlensBody = (*env)->GetShortArrayElements(env, |
|
741 |
huffLens, |
|
742 |
NULL); |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
743 |
CHECK_NULL_RETURN(hlensBody, FALSE); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
744 |
|
4203 | 745 |
if (hlensLen > 16) { |
746 |
/* Ignore extra elements of bits array. Only 16 elements can be |
|
747 |
stored. 0-th element is not used. (see jpeglib.h, line 107) */ |
|
748 |
hlensLen = 16; |
|
749 |
} |
|
2 | 750 |
for (i = 1; i <= hlensLen; i++) { |
751 |
huff_ptr->bits[i] = (UINT8)hlensBody[i-1]; |
|
752 |
} |
|
753 |
(*env)->ReleaseShortArrayElements(env, |
|
754 |
huffLens, |
|
755 |
hlensBody, |
|
756 |
JNI_ABORT); |
|
757 |
// values |
|
758 |
huffValues = (*env)->GetObjectField(env, |
|
759 |
table, |
|
760 |
JPEGHuffmanTable_valuesID); |
|
761 |
hvalsLen = (*env)->GetArrayLength(env, huffValues); |
|
762 |
hvalsBody = (*env)->GetShortArrayElements(env, |
|
763 |
huffValues, |
|
764 |
NULL); |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
765 |
CHECK_NULL_RETURN(hvalsBody, FALSE); |
2 | 766 |
|
4203 | 767 |
if (hvalsLen > 256) { |
768 |
/* Ignore extra elements of hufval array. Only 256 elements |
|
769 |
can be stored. (see jpeglib.h, line 109) */ |
|
770 |
hlensLen = 256; |
|
771 |
} |
|
2 | 772 |
for (i = 0; i < hvalsLen; i++) { |
773 |
huff_ptr->huffval[i] = (UINT8)hvalsBody[i]; |
|
774 |
} |
|
775 |
(*env)->ReleaseShortArrayElements(env, |
|
776 |
huffValues, |
|
777 |
hvalsBody, |
|
778 |
JNI_ABORT); |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
779 |
return TRUE; |
2 | 780 |
} |
781 |
||
782 |
static int setHTables(JNIEnv *env, |
|
783 |
j_common_ptr cinfo, |
|
784 |
jobjectArray DCHuffmanTables, |
|
785 |
jobjectArray ACHuffmanTables, |
|
786 |
boolean write) { |
|
787 |
int i; |
|
788 |
jobject table; |
|
789 |
JHUFF_TBL *huff_ptr; |
|
790 |
j_compress_ptr comp; |
|
791 |
j_decompress_ptr decomp; |
|
792 |
jsize hlen = (*env)->GetArrayLength(env, DCHuffmanTables); |
|
4203 | 793 |
|
794 |
if (hlen > NUM_HUFF_TBLS) { |
|
795 |
/* Ignore extra DC huffman tables. */ |
|
796 |
hlen = NUM_HUFF_TBLS; |
|
797 |
} |
|
2 | 798 |
for (i = 0; i < hlen; i++) { |
799 |
if (cinfo->is_decompressor) { |
|
800 |
decomp = (j_decompress_ptr) cinfo; |
|
801 |
if (decomp->dc_huff_tbl_ptrs[i] == NULL) { |
|
802 |
decomp->dc_huff_tbl_ptrs[i] = |
|
803 |
jpeg_alloc_huff_table(cinfo); |
|
804 |
} |
|
805 |
huff_ptr = decomp->dc_huff_tbl_ptrs[i]; |
|
806 |
} else { |
|
807 |
comp = (j_compress_ptr) cinfo; |
|
808 |
if (comp->dc_huff_tbl_ptrs[i] == NULL) { |
|
809 |
comp->dc_huff_tbl_ptrs[i] = |
|
810 |
jpeg_alloc_huff_table(cinfo); |
|
811 |
} |
|
812 |
huff_ptr = comp->dc_huff_tbl_ptrs[i]; |
|
813 |
} |
|
814 |
table = (*env)->GetObjectArrayElement(env, DCHuffmanTables, i); |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
815 |
if (table == NULL || !setHuffTable(env, huff_ptr, table)) { |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
816 |
return 0; |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
817 |
} |
2 | 818 |
huff_ptr->sent_table = !write; |
819 |
} |
|
820 |
hlen = (*env)->GetArrayLength(env, ACHuffmanTables); |
|
4203 | 821 |
if (hlen > NUM_HUFF_TBLS) { |
822 |
/* Ignore extra AC huffman tables. */ |
|
823 |
hlen = NUM_HUFF_TBLS; |
|
824 |
} |
|
2 | 825 |
for (i = 0; i < hlen; i++) { |
826 |
if (cinfo->is_decompressor) { |
|
827 |
decomp = (j_decompress_ptr) cinfo; |
|
828 |
if (decomp->ac_huff_tbl_ptrs[i] == NULL) { |
|
829 |
decomp->ac_huff_tbl_ptrs[i] = |
|
830 |
jpeg_alloc_huff_table(cinfo); |
|
831 |
} |
|
832 |
huff_ptr = decomp->ac_huff_tbl_ptrs[i]; |
|
833 |
} else { |
|
834 |
comp = (j_compress_ptr) cinfo; |
|
835 |
if (comp->ac_huff_tbl_ptrs[i] == NULL) { |
|
836 |
comp->ac_huff_tbl_ptrs[i] = |
|
837 |
jpeg_alloc_huff_table(cinfo); |
|
838 |
} |
|
839 |
huff_ptr = comp->ac_huff_tbl_ptrs[i]; |
|
840 |
} |
|
841 |
table = (*env)->GetObjectArrayElement(env, ACHuffmanTables, i); |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
842 |
if(table == NULL || !setHuffTable(env, huff_ptr, table)) { |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
843 |
return 0; |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
844 |
} |
2 | 845 |
huff_ptr->sent_table = !write; |
846 |
} |
|
847 |
return hlen; |
|
848 |
} |
|
849 |
||
850 |
||
851 |
/*************** end of shared utility code ****************/ |
|
852 |
||
853 |
/********************** Reader Support **************************/ |
|
854 |
||
855 |
/********************** Source Management ***********************/ |
|
856 |
||
857 |
/* |
|
858 |
* INPUT HANDLING: |
|
859 |
* |
|
860 |
* The JPEG library's input management is defined by the jpeg_source_mgr |
|
861 |
* structure which contains two fields to convey the information in the |
|
862 |
* buffer and 5 methods which perform all buffer management. The library |
|
863 |
* defines a standard input manager that uses stdio for obtaining compressed |
|
864 |
* jpeg data, but here we need to use Java to get our data. |
|
865 |
* |
|
866 |
* We use the library jpeg_source_mgr but our own routines that access |
|
867 |
* imageio-specific information in the imageIOData structure. |
|
868 |
*/ |
|
869 |
||
870 |
/* |
|
871 |
* Initialize source. This is called by jpeg_read_header() before any |
|
872 |
* data is actually read. Unlike init_destination(), it may leave |
|
873 |
* bytes_in_buffer set to 0 (in which case a fill_input_buffer() call |
|
874 |
* will occur immediately). |
|
875 |
*/ |
|
876 |
||
877 |
GLOBAL(void) |
|
878 |
imageio_init_source(j_decompress_ptr cinfo) |
|
879 |
{ |
|
880 |
struct jpeg_source_mgr *src = cinfo->src; |
|
881 |
src->next_input_byte = NULL; |
|
882 |
src->bytes_in_buffer = 0; |
|
883 |
} |
|
884 |
||
885 |
/* |
|
886 |
* This is called whenever bytes_in_buffer has reached zero and more |
|
887 |
* data is wanted. In typical applications, it should read fresh data |
|
888 |
* into the buffer (ignoring the current state of next_input_byte and |
|
889 |
* bytes_in_buffer), reset the pointer & count to the start of the |
|
890 |
* buffer, and return TRUE indicating that the buffer has been reloaded. |
|
891 |
* It is not necessary to fill the buffer entirely, only to obtain at |
|
892 |
* least one more byte. bytes_in_buffer MUST be set to a positive value |
|
893 |
* if TRUE is returned. A FALSE return should only be used when I/O |
|
894 |
* suspension is desired (this mode is discussed in the next section). |
|
895 |
*/ |
|
896 |
/* |
|
897 |
* Note that with I/O suspension turned on, this procedure should not |
|
898 |
* do any work since the JPEG library has a very simple backtracking |
|
899 |
* mechanism which relies on the fact that the buffer will be filled |
|
900 |
* only when it has backed out to the top application level. When |
|
901 |
* suspendable is turned on, imageio_fill_suspended_buffer will |
|
902 |
* do the actual work of filling the buffer. |
|
903 |
*/ |
|
904 |
||
905 |
GLOBAL(boolean) |
|
906 |
imageio_fill_input_buffer(j_decompress_ptr cinfo) |
|
907 |
{ |
|
908 |
struct jpeg_source_mgr *src = cinfo->src; |
|
909 |
imageIODataPtr data = (imageIODataPtr) cinfo->client_data; |
|
910 |
streamBufferPtr sb = &data->streamBuf; |
|
911 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
912 |
int ret; |
|
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
913 |
jobject input = NULL; |
2 | 914 |
|
915 |
/* This is where input suspends */ |
|
916 |
if (sb->suspendable) { |
|
917 |
return FALSE; |
|
918 |
} |
|
919 |
||
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
920 |
#ifdef DEBUG_IIO_JPEG |
2 | 921 |
printf("Filling input buffer, remaining skip is %ld, ", |
922 |
sb->remaining_skip); |
|
923 |
printf("Buffer length is %d\n", sb->bufferLength); |
|
924 |
#endif |
|
925 |
||
926 |
/* |
|
927 |
* Definitively skips. Could be left over if we tried to skip |
|
928 |
* more than a buffer's worth but suspended when getting the next |
|
929 |
* buffer. Now we aren't suspended, so we can catch up. |
|
930 |
*/ |
|
931 |
if (sb->remaining_skip) { |
|
932 |
src->skip_input_data(cinfo, 0); |
|
933 |
} |
|
934 |
||
935 |
/* |
|
936 |
* Now fill a complete buffer, or as much of one as the stream |
|
937 |
* will give us if we are near the end. |
|
938 |
*/ |
|
19788 | 939 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
940 |
||
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
941 |
GET_IO_REF(input); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
942 |
|
2 | 943 |
ret = (*env)->CallIntMethod(env, |
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
944 |
input, |
16881 | 945 |
JPEGImageReader_readInputDataID, |
2 | 946 |
sb->hstreamBuffer, 0, |
947 |
sb->bufferLength); |
|
23892 | 948 |
if (ret > sb->bufferLength) ret = sb->bufferLength; |
2 | 949 |
if ((*env)->ExceptionOccurred(env) |
950 |
|| !GET_ARRAYS(env, data, &(src->next_input_byte))) { |
|
951 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
952 |
} |
|
953 |
||
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
954 |
#ifdef DEBUG_IIO_JPEG |
2 | 955 |
printf("Buffer filled. ret = %d\n", ret); |
956 |
#endif |
|
957 |
/* |
|
958 |
* If we have reached the end of the stream, then the EOI marker |
|
959 |
* is missing. We accept such streams but generate a warning. |
|
960 |
* The image is likely to be corrupted, though everything through |
|
961 |
* the end of the last complete MCU should be usable. |
|
962 |
*/ |
|
963 |
if (ret <= 0) { |
|
964 |
jobject reader = data->imageIOobj; |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
965 |
#ifdef DEBUG_IIO_JPEG |
2 | 966 |
printf("YO! Early EOI! ret = %d\n", ret); |
967 |
#endif |
|
968 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
969 |
(*env)->CallVoidMethod(env, reader, |
|
970 |
JPEGImageReader_warningOccurredID, |
|
971 |
READ_NO_EOI); |
|
972 |
if ((*env)->ExceptionOccurred(env) |
|
973 |
|| !GET_ARRAYS(env, data, &(src->next_input_byte))) { |
|
974 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
975 |
} |
|
976 |
||
977 |
sb->buf[0] = (JOCTET) 0xFF; |
|
978 |
sb->buf[1] = (JOCTET) JPEG_EOI; |
|
979 |
ret = 2; |
|
980 |
} |
|
981 |
||
982 |
src->next_input_byte = sb->buf; |
|
983 |
src->bytes_in_buffer = ret; |
|
984 |
||
985 |
return TRUE; |
|
986 |
} |
|
987 |
||
988 |
/* |
|
989 |
* With I/O suspension turned on, the JPEG library requires that all |
|
990 |
* buffer filling be done at the top application level, using this |
|
991 |
* function. Due to the way that backtracking works, this procedure |
|
992 |
* saves all of the data that was left in the buffer when suspension |
|
21278 | 993 |
* occurred and read new data only at the end. |
2 | 994 |
*/ |
995 |
||
996 |
GLOBAL(void) |
|
997 |
imageio_fill_suspended_buffer(j_decompress_ptr cinfo) |
|
998 |
{ |
|
999 |
struct jpeg_source_mgr *src = cinfo->src; |
|
1000 |
imageIODataPtr data = (imageIODataPtr) cinfo->client_data; |
|
1001 |
streamBufferPtr sb = &data->streamBuf; |
|
1002 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
1003 |
jint ret; |
|
8355 | 1004 |
size_t offset, buflen; |
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
1005 |
jobject input = NULL; |
2 | 1006 |
|
1007 |
/* |
|
1008 |
* The original (jpegdecoder.c) had code here that called |
|
1009 |
* InputStream.available and just returned if the number of bytes |
|
1010 |
* available was less than any remaining skip. Presumably this was |
|
1011 |
* to avoid blocking, although the benefit was unclear, as no more |
|
1012 |
* decompression can take place until more data is available, so |
|
1013 |
* the code would block on input a little further along anyway. |
|
1014 |
* ImageInputStreams don't have an available method, so we'll just |
|
1015 |
* block in the skip if we have to. |
|
1016 |
*/ |
|
1017 |
||
1018 |
if (sb->remaining_skip) { |
|
1019 |
src->skip_input_data(cinfo, 0); |
|
1020 |
} |
|
1021 |
||
1022 |
/* Save the data currently in the buffer */ |
|
1023 |
offset = src->bytes_in_buffer; |
|
1024 |
if (src->next_input_byte > sb->buf) { |
|
1025 |
memcpy(sb->buf, src->next_input_byte, offset); |
|
1026 |
} |
|
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
1027 |
|
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
1028 |
|
2 | 1029 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
19788 | 1030 |
|
1031 |
GET_IO_REF(input); |
|
1032 |
||
2 | 1033 |
buflen = sb->bufferLength - offset; |
1034 |
if (buflen <= 0) { |
|
1035 |
if (!GET_ARRAYS(env, data, &(src->next_input_byte))) { |
|
1036 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
1037 |
} |
|
1038 |
return; |
|
1039 |
} |
|
1040 |
||
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
1041 |
ret = (*env)->CallIntMethod(env, input, |
16881 | 1042 |
JPEGImageReader_readInputDataID, |
2 | 1043 |
sb->hstreamBuffer, |
1044 |
offset, buflen); |
|
23892 | 1045 |
if (ret > buflen) ret = buflen; |
2 | 1046 |
if ((*env)->ExceptionOccurred(env) |
1047 |
|| !GET_ARRAYS(env, data, &(src->next_input_byte))) { |
|
1048 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
1049 |
} |
|
1050 |
/* |
|
1051 |
* If we have reached the end of the stream, then the EOI marker |
|
1052 |
* is missing. We accept such streams but generate a warning. |
|
1053 |
* The image is likely to be corrupted, though everything through |
|
1054 |
* the end of the last complete MCU should be usable. |
|
1055 |
*/ |
|
1056 |
if (ret <= 0) { |
|
1057 |
jobject reader = data->imageIOobj; |
|
1058 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
1059 |
(*env)->CallVoidMethod(env, reader, |
|
1060 |
JPEGImageReader_warningOccurredID, |
|
1061 |
READ_NO_EOI); |
|
1062 |
if ((*env)->ExceptionOccurred(env) |
|
1063 |
|| !GET_ARRAYS(env, data, &(src->next_input_byte))) { |
|
1064 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
1065 |
} |
|
1066 |
||
1067 |
sb->buf[offset] = (JOCTET) 0xFF; |
|
1068 |
sb->buf[offset + 1] = (JOCTET) JPEG_EOI; |
|
1069 |
ret = 2; |
|
1070 |
} |
|
1071 |
||
1072 |
src->next_input_byte = sb->buf; |
|
1073 |
src->bytes_in_buffer = ret + offset; |
|
1074 |
||
1075 |
return; |
|
1076 |
} |
|
1077 |
||
1078 |
/* |
|
1079 |
* Skip num_bytes worth of data. The buffer pointer and count are |
|
1080 |
* advanced over num_bytes input bytes, using the input stream |
|
1081 |
* skipBytes method if the skip is greater than the number of bytes |
|
1082 |
* in the buffer. This is used to skip over a potentially large amount of |
|
1083 |
* uninteresting data (such as an APPn marker). bytes_in_buffer will be |
|
1084 |
* zero on return if the skip is larger than the current contents of the |
|
1085 |
* buffer. |
|
1086 |
* |
|
1087 |
* A negative skip count is treated as a no-op. A zero skip count |
|
1088 |
* skips any remaining skip from a previous skip while suspended. |
|
1089 |
* |
|
1090 |
* Note that with I/O suspension turned on, this procedure does not |
|
1091 |
* call skipBytes since the JPEG library has a very simple backtracking |
|
1092 |
* mechanism which relies on the fact that the application level has |
|
1093 |
* exclusive control over actual I/O. |
|
1094 |
*/ |
|
1095 |
||
1096 |
GLOBAL(void) |
|
1097 |
imageio_skip_input_data(j_decompress_ptr cinfo, long num_bytes) |
|
1098 |
{ |
|
1099 |
struct jpeg_source_mgr *src = cinfo->src; |
|
1100 |
imageIODataPtr data = (imageIODataPtr) cinfo->client_data; |
|
1101 |
streamBufferPtr sb = &data->streamBuf; |
|
1102 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
1103 |
jlong ret; |
|
1104 |
jobject reader; |
|
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
1105 |
jobject input = NULL; |
2 | 1106 |
|
1107 |
if (num_bytes < 0) { |
|
1108 |
return; |
|
1109 |
} |
|
1110 |
num_bytes += sb->remaining_skip; |
|
1111 |
sb->remaining_skip = 0; |
|
1112 |
||
1113 |
/* First the easy case where we are skipping <= the current contents. */ |
|
1114 |
ret = src->bytes_in_buffer; |
|
1115 |
if (ret >= num_bytes) { |
|
1116 |
src->next_input_byte += num_bytes; |
|
1117 |
src->bytes_in_buffer -= num_bytes; |
|
1118 |
return; |
|
1119 |
} |
|
1120 |
||
1121 |
/* |
|
1122 |
* We are skipping more than is in the buffer. We empty the buffer and, |
|
1123 |
* if we aren't suspended, call the Java skipBytes method. We always |
|
1124 |
* leave the buffer empty, to be filled by either fill method above. |
|
1125 |
*/ |
|
1126 |
src->bytes_in_buffer = 0; |
|
1127 |
src->next_input_byte = sb->buf; |
|
1128 |
||
1129 |
num_bytes -= (long)ret; |
|
1130 |
if (sb->suspendable) { |
|
1131 |
sb->remaining_skip = num_bytes; |
|
1132 |
return; |
|
1133 |
} |
|
1134 |
||
19788 | 1135 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
1136 |
||
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
1137 |
GET_IO_REF(input); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
1138 |
|
2 | 1139 |
ret = (*env)->CallLongMethod(env, |
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
1140 |
input, |
16881 | 1141 |
JPEGImageReader_skipInputBytesID, |
2 | 1142 |
(jlong) num_bytes); |
1143 |
if ((*env)->ExceptionOccurred(env) |
|
1144 |
|| !GET_ARRAYS(env, data, &(src->next_input_byte))) { |
|
1145 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
1146 |
} |
|
1147 |
||
1148 |
/* |
|
1149 |
* If we have reached the end of the stream, then the EOI marker |
|
1150 |
* is missing. We accept such streams but generate a warning. |
|
1151 |
* The image is likely to be corrupted, though everything through |
|
1152 |
* the end of the last complete MCU should be usable. |
|
1153 |
*/ |
|
1154 |
if (ret <= 0) { |
|
1155 |
reader = data->imageIOobj; |
|
1156 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
1157 |
(*env)->CallVoidMethod(env, |
|
1158 |
reader, |
|
1159 |
JPEGImageReader_warningOccurredID, |
|
1160 |
READ_NO_EOI); |
|
1161 |
||
1162 |
if ((*env)->ExceptionOccurred(env) |
|
1163 |
|| !GET_ARRAYS(env, data, &(src->next_input_byte))) { |
|
1164 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
1165 |
} |
|
1166 |
sb->buf[0] = (JOCTET) 0xFF; |
|
1167 |
sb->buf[1] = (JOCTET) JPEG_EOI; |
|
1168 |
src->bytes_in_buffer = 2; |
|
1169 |
src->next_input_byte = sb->buf; |
|
1170 |
} |
|
1171 |
} |
|
1172 |
||
1173 |
/* |
|
1174 |
* Terminate source --- called by jpeg_finish_decompress() after all |
|
1175 |
* data for an image has been read. In our case pushes back any |
|
1176 |
* remaining data, as it will be for another image and must be available |
|
1177 |
* for java to find out that there is another image. Also called if |
|
1178 |
* reseting state after reading a tables-only image. |
|
1179 |
*/ |
|
1180 |
||
1181 |
GLOBAL(void) |
|
1182 |
imageio_term_source(j_decompress_ptr cinfo) |
|
1183 |
{ |
|
1184 |
// To pushback, just seek back by src->bytes_in_buffer |
|
1185 |
struct jpeg_source_mgr *src = cinfo->src; |
|
1186 |
imageIODataPtr data = (imageIODataPtr) cinfo->client_data; |
|
1187 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
1188 |
jobject reader = data->imageIOobj; |
|
1189 |
if (src->bytes_in_buffer > 0) { |
|
1190 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
1191 |
(*env)->CallVoidMethod(env, |
|
1192 |
reader, |
|
1193 |
JPEGImageReader_pushBackID, |
|
1194 |
src->bytes_in_buffer); |
|
1195 |
||
1196 |
if ((*env)->ExceptionOccurred(env) |
|
1197 |
|| !GET_ARRAYS(env, data, &(src->next_input_byte))) { |
|
1198 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
1199 |
} |
|
1200 |
src->bytes_in_buffer = 0; |
|
1201 |
//src->next_input_byte = sb->buf; |
|
1202 |
} |
|
1203 |
} |
|
1204 |
||
1205 |
/********************* end of source manager ******************/ |
|
1206 |
||
1207 |
/********************* ICC profile support ********************/ |
|
1208 |
/* |
|
1209 |
* The following routines are modified versions of the ICC |
|
1210 |
* profile support routines available from the IJG website. |
|
1211 |
* The originals were written by Todd Newman |
|
1212 |
* <tdn@eccentric.esd.sgi.com> and modified by Tom Lane for |
|
1213 |
* the IJG. They are further modified to fit in the context |
|
1214 |
* of the imageio JPEG plug-in. |
|
1215 |
*/ |
|
1216 |
||
1217 |
/* |
|
1218 |
* Since an ICC profile can be larger than the maximum size of a JPEG marker |
|
1219 |
* (64K), we need provisions to split it into multiple markers. The format |
|
1220 |
* defined by the ICC specifies one or more APP2 markers containing the |
|
1221 |
* following data: |
|
1222 |
* Identifying string ASCII "ICC_PROFILE\0" (12 bytes) |
|
1223 |
* Marker sequence number 1 for first APP2, 2 for next, etc (1 byte) |
|
1224 |
* Number of markers Total number of APP2's used (1 byte) |
|
1225 |
* Profile data (remainder of APP2 data) |
|
1226 |
* Decoders should use the marker sequence numbers to reassemble the profile, |
|
1227 |
* rather than assuming that the APP2 markers appear in the correct sequence. |
|
1228 |
*/ |
|
1229 |
||
1230 |
#define ICC_MARKER (JPEG_APP0 + 2) /* JPEG marker code for ICC */ |
|
1231 |
#define ICC_OVERHEAD_LEN 14 /* size of non-profile data in APP2 */ |
|
1232 |
#define MAX_BYTES_IN_MARKER 65533 /* maximum data len of a JPEG marker */ |
|
1233 |
#define MAX_DATA_BYTES_IN_ICC_MARKER (MAX_BYTES_IN_MARKER - ICC_OVERHEAD_LEN) |
|
1234 |
||
1235 |
||
1236 |
/* |
|
1237 |
* Handy subroutine to test whether a saved marker is an ICC profile marker. |
|
1238 |
*/ |
|
1239 |
||
1240 |
static boolean |
|
1241 |
marker_is_icc (jpeg_saved_marker_ptr marker) |
|
1242 |
{ |
|
1243 |
return |
|
1244 |
marker->marker == ICC_MARKER && |
|
1245 |
marker->data_length >= ICC_OVERHEAD_LEN && |
|
1246 |
/* verify the identifying string */ |
|
1247 |
GETJOCTET(marker->data[0]) == 0x49 && |
|
1248 |
GETJOCTET(marker->data[1]) == 0x43 && |
|
1249 |
GETJOCTET(marker->data[2]) == 0x43 && |
|
1250 |
GETJOCTET(marker->data[3]) == 0x5F && |
|
1251 |
GETJOCTET(marker->data[4]) == 0x50 && |
|
1252 |
GETJOCTET(marker->data[5]) == 0x52 && |
|
1253 |
GETJOCTET(marker->data[6]) == 0x4F && |
|
1254 |
GETJOCTET(marker->data[7]) == 0x46 && |
|
1255 |
GETJOCTET(marker->data[8]) == 0x49 && |
|
1256 |
GETJOCTET(marker->data[9]) == 0x4C && |
|
1257 |
GETJOCTET(marker->data[10]) == 0x45 && |
|
1258 |
GETJOCTET(marker->data[11]) == 0x0; |
|
1259 |
} |
|
1260 |
||
1261 |
/* |
|
1262 |
* See if there was an ICC profile in the JPEG file being read; |
|
1263 |
* if so, reassemble and return the profile data as a new Java byte array. |
|
1264 |
* If there was no ICC profile, return NULL. |
|
1265 |
* |
|
1266 |
* If the file contains invalid ICC APP2 markers, we throw an IIOException |
|
1267 |
* with an appropriate message. |
|
1268 |
*/ |
|
1269 |
||
1270 |
jbyteArray |
|
1271 |
read_icc_profile (JNIEnv *env, j_decompress_ptr cinfo) |
|
1272 |
{ |
|
1273 |
jpeg_saved_marker_ptr marker; |
|
1274 |
int num_markers = 0; |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1275 |
int num_found_markers = 0; |
2 | 1276 |
int seq_no; |
1277 |
JOCTET *icc_data; |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1278 |
JOCTET *dst_ptr; |
2 | 1279 |
unsigned int total_length; |
1280 |
#define MAX_SEQ_NO 255 // sufficient since marker numbers are bytes |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1281 |
jpeg_saved_marker_ptr icc_markers[MAX_SEQ_NO + 1]; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1282 |
int first; // index of the first marker in the icc_markers array |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1283 |
int last; // index of the last marker in the icc_markers array |
2 | 1284 |
jbyteArray data = NULL; |
1285 |
||
1286 |
/* This first pass over the saved markers discovers whether there are |
|
1287 |
* any ICC markers and verifies the consistency of the marker numbering. |
|
1288 |
*/ |
|
1289 |
||
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1290 |
for (seq_no = 0; seq_no <= MAX_SEQ_NO; seq_no++) |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1291 |
icc_markers[seq_no] = NULL; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1292 |
|
2 | 1293 |
|
1294 |
for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) { |
|
1295 |
if (marker_is_icc(marker)) { |
|
1296 |
if (num_markers == 0) |
|
1297 |
num_markers = GETJOCTET(marker->data[13]); |
|
1298 |
else if (num_markers != GETJOCTET(marker->data[13])) { |
|
1299 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
|
1300 |
"Invalid icc profile: inconsistent num_markers fields"); |
|
1301 |
return NULL; |
|
1302 |
} |
|
1303 |
seq_no = GETJOCTET(marker->data[12]); |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1304 |
|
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1305 |
/* Some third-party tools produce images with profile chunk |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1306 |
* numeration started from zero. It is inconsistent with ICC |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1307 |
* spec, but seems to be recognized by majority of image |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1308 |
* processing tools, so we should be more tolerant to this |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1309 |
* departure from the spec. |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1310 |
*/ |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1311 |
if (seq_no < 0 || seq_no > num_markers) { |
2 | 1312 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
1313 |
"Invalid icc profile: bad sequence number"); |
|
1314 |
return NULL; |
|
1315 |
} |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1316 |
if (icc_markers[seq_no] != NULL) { |
2 | 1317 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
1318 |
"Invalid icc profile: duplicate sequence numbers"); |
|
1319 |
return NULL; |
|
1320 |
} |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1321 |
icc_markers[seq_no] = marker; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1322 |
num_found_markers ++; |
2 | 1323 |
} |
1324 |
} |
|
1325 |
||
1326 |
if (num_markers == 0) |
|
1327 |
return NULL; // There is no profile |
|
1328 |
||
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1329 |
if (num_markers != num_found_markers) { |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1330 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1331 |
"Invalid icc profile: invalid number of icc markers"); |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1332 |
return NULL; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1333 |
} |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1334 |
|
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1335 |
first = icc_markers[0] ? 0 : 1; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1336 |
last = num_found_markers + first; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1337 |
|
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1338 |
/* Check for missing markers, count total space needed. |
2 | 1339 |
*/ |
1340 |
total_length = 0; |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1341 |
for (seq_no = first; seq_no < last; seq_no++) { |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1342 |
unsigned int length; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1343 |
if (icc_markers[seq_no] == NULL) { |
2 | 1344 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
1345 |
"Invalid icc profile: missing sequence number"); |
|
1346 |
return NULL; |
|
1347 |
} |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1348 |
/* check the data length correctness */ |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1349 |
length = icc_markers[seq_no]->data_length; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1350 |
if (ICC_OVERHEAD_LEN > length || length > MAX_BYTES_IN_MARKER) { |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1351 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1352 |
"Invalid icc profile: invalid data length"); |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1353 |
return NULL; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1354 |
} |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1355 |
total_length += (length - ICC_OVERHEAD_LEN); |
2 | 1356 |
} |
1357 |
||
1358 |
if (total_length <= 0) { |
|
1359 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
|
1360 |
"Invalid icc profile: found only empty markers"); |
|
1361 |
return NULL; |
|
1362 |
} |
|
1363 |
||
1364 |
/* Allocate a Java byte array for assembled data */ |
|
1365 |
||
1366 |
data = (*env)->NewByteArray(env, total_length); |
|
1367 |
if (data == NULL) { |
|
1368 |
JNU_ThrowByName(env, |
|
1369 |
"java/lang/OutOfMemoryError", |
|
1370 |
"Reading ICC profile"); |
|
1371 |
return NULL; |
|
1372 |
} |
|
1373 |
||
1374 |
icc_data = (JOCTET *)(*env)->GetPrimitiveArrayCritical(env, |
|
1375 |
data, |
|
1376 |
NULL); |
|
1377 |
if (icc_data == NULL) { |
|
1378 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
|
1379 |
"Unable to pin icc profile data array"); |
|
1380 |
return NULL; |
|
1381 |
} |
|
1382 |
||
1383 |
/* and fill it in */ |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1384 |
dst_ptr = icc_data; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1385 |
for (seq_no = first; seq_no < last; seq_no++) { |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1386 |
JOCTET FAR *src_ptr = icc_markers[seq_no]->data + ICC_OVERHEAD_LEN; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1387 |
unsigned int length = |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1388 |
icc_markers[seq_no]->data_length - ICC_OVERHEAD_LEN; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1389 |
|
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1390 |
memcpy(dst_ptr, src_ptr, length); |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1391 |
dst_ptr += length; |
2 | 1392 |
} |
1393 |
||
1394 |
/* finally, unpin the array */ |
|
1395 |
(*env)->ReleasePrimitiveArrayCritical(env, |
|
1396 |
data, |
|
1397 |
icc_data, |
|
1398 |
0); |
|
1399 |
||
1400 |
||
1401 |
return data; |
|
1402 |
} |
|
1403 |
||
1404 |
/********************* end of ICC profile support *************/ |
|
1405 |
||
1406 |
/********************* Reader JNI calls ***********************/ |
|
1407 |
||
1408 |
JNIEXPORT void JNICALL |
|
1409 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_initReaderIDs |
|
1410 |
(JNIEnv *env, |
|
1411 |
jclass cls, |
|
1412 |
jclass ImageInputStreamClass, |
|
1413 |
jclass qTableClass, |
|
1414 |
jclass huffClass) { |
|
1415 |
||
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1416 |
CHECK_NULL(JPEGImageReader_readInputDataID = (*env)->GetMethodID(env, |
16881 | 1417 |
cls, |
1418 |
"readInputData", |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1419 |
"([BII)I")); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1420 |
CHECK_NULL(JPEGImageReader_skipInputBytesID = (*env)->GetMethodID(env, |
16881 | 1421 |
cls, |
1422 |
"skipInputBytes", |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1423 |
"(J)J")); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1424 |
CHECK_NULL(JPEGImageReader_warningOccurredID = (*env)->GetMethodID(env, |
2 | 1425 |
cls, |
1426 |
"warningOccurred", |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1427 |
"(I)V")); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1428 |
CHECK_NULL(JPEGImageReader_warningWithMessageID = |
2 | 1429 |
(*env)->GetMethodID(env, |
1430 |
cls, |
|
1431 |
"warningWithMessage", |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1432 |
"(Ljava/lang/String;)V")); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1433 |
CHECK_NULL(JPEGImageReader_setImageDataID = (*env)->GetMethodID(env, |
2 | 1434 |
cls, |
1435 |
"setImageData", |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1436 |
"(IIIII[B)V")); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1437 |
CHECK_NULL(JPEGImageReader_acceptPixelsID = (*env)->GetMethodID(env, |
2 | 1438 |
cls, |
1439 |
"acceptPixels", |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1440 |
"(IZ)V")); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1441 |
CHECK_NULL(JPEGImageReader_passStartedID = (*env)->GetMethodID(env, |
2 | 1442 |
cls, |
1443 |
"passStarted", |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1444 |
"(I)V")); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1445 |
CHECK_NULL(JPEGImageReader_passCompleteID = (*env)->GetMethodID(env, |
2 | 1446 |
cls, |
1447 |
"passComplete", |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1448 |
"()V")); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1449 |
CHECK_NULL(JPEGImageReader_pushBackID = (*env)->GetMethodID(env, |
2 | 1450 |
cls, |
1451 |
"pushBack", |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1452 |
"(I)V")); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1453 |
CHECK_NULL(JPEGQTable_tableID = (*env)->GetFieldID(env, |
2 | 1454 |
qTableClass, |
1455 |
"qTable", |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1456 |
"[I")); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1457 |
|
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1458 |
CHECK_NULL(JPEGHuffmanTable_lengthsID = (*env)->GetFieldID(env, |
2 | 1459 |
huffClass, |
1460 |
"lengths", |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1461 |
"[S")); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1462 |
|
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1463 |
CHECK_NULL(JPEGHuffmanTable_valuesID = (*env)->GetFieldID(env, |
2 | 1464 |
huffClass, |
1465 |
"values", |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1466 |
"[S")); |
2 | 1467 |
} |
1468 |
||
1469 |
JNIEXPORT jlong JNICALL |
|
1470 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_initJPEGImageReader |
|
1471 |
(JNIEnv *env, |
|
1472 |
jobject this) { |
|
1473 |
||
1474 |
imageIODataPtr ret; |
|
1475 |
struct sun_jpeg_error_mgr *jerr; |
|
1476 |
||
1477 |
/* This struct contains the JPEG decompression parameters and pointers to |
|
1478 |
* working space (which is allocated as needed by the JPEG library). |
|
1479 |
*/ |
|
1480 |
struct jpeg_decompress_struct *cinfo = |
|
1481 |
malloc(sizeof(struct jpeg_decompress_struct)); |
|
1482 |
if (cinfo == NULL) { |
|
1483 |
JNU_ThrowByName( env, |
|
1484 |
"java/lang/OutOfMemoryError", |
|
1485 |
"Initializing Reader"); |
|
1486 |
return 0; |
|
1487 |
} |
|
1488 |
||
1489 |
/* We use our private extension JPEG error handler. |
|
1490 |
*/ |
|
1491 |
jerr = malloc (sizeof(struct sun_jpeg_error_mgr)); |
|
1492 |
if (jerr == NULL) { |
|
1493 |
JNU_ThrowByName( env, |
|
1494 |
"java/lang/OutOfMemoryError", |
|
1495 |
"Initializing Reader"); |
|
4248 | 1496 |
free(cinfo); |
2 | 1497 |
return 0; |
1498 |
} |
|
1499 |
||
1500 |
/* We set up the normal JPEG error routines, then override error_exit. */ |
|
1501 |
cinfo->err = jpeg_std_error(&(jerr->pub)); |
|
1502 |
jerr->pub.error_exit = sun_jpeg_error_exit; |
|
1503 |
/* We need to setup our own print routines */ |
|
1504 |
jerr->pub.output_message = sun_jpeg_output_message; |
|
1505 |
/* Now we can setjmp before every call to the library */ |
|
1506 |
||
1507 |
/* Establish the setjmp return context for sun_jpeg_error_exit to use. */ |
|
1508 |
if (setjmp(jerr->setjmp_buffer)) { |
|
1509 |
/* If we get here, the JPEG code has signaled an error. */ |
|
1510 |
char buffer[JMSG_LENGTH_MAX]; |
|
1511 |
(*cinfo->err->format_message) ((struct jpeg_common_struct *) cinfo, |
|
1512 |
buffer); |
|
1513 |
JNU_ThrowByName(env, "javax/imageio/IIOException", buffer); |
|
1514 |
return 0; |
|
1515 |
} |
|
1516 |
||
1517 |
/* Perform library initialization */ |
|
1518 |
jpeg_create_decompress(cinfo); |
|
1519 |
||
1520 |
// Set up to keep any APP2 markers, as these might contain ICC profile |
|
1521 |
// data |
|
1522 |
jpeg_save_markers(cinfo, ICC_MARKER, 0xFFFF); |
|
1523 |
||
1524 |
/* |
|
1525 |
* Now set up our source. |
|
1526 |
*/ |
|
1527 |
cinfo->src = |
|
1528 |
(struct jpeg_source_mgr *) malloc (sizeof(struct jpeg_source_mgr)); |
|
1529 |
if (cinfo->src == NULL) { |
|
1530 |
JNU_ThrowByName(env, |
|
1531 |
"java/lang/OutOfMemoryError", |
|
1532 |
"Initializing Reader"); |
|
4248 | 1533 |
imageio_dispose((j_common_ptr)cinfo); |
2 | 1534 |
return 0; |
1535 |
} |
|
1536 |
cinfo->src->bytes_in_buffer = 0; |
|
1537 |
cinfo->src->next_input_byte = NULL; |
|
1538 |
cinfo->src->init_source = imageio_init_source; |
|
1539 |
cinfo->src->fill_input_buffer = imageio_fill_input_buffer; |
|
1540 |
cinfo->src->skip_input_data = imageio_skip_input_data; |
|
1541 |
cinfo->src->resync_to_restart = jpeg_resync_to_restart; // use default |
|
1542 |
cinfo->src->term_source = imageio_term_source; |
|
1543 |
||
1544 |
/* set up the association to persist for future calls */ |
|
1545 |
ret = initImageioData(env, (j_common_ptr) cinfo, this); |
|
1546 |
if (ret == NULL) { |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1547 |
(*env)->ExceptionClear(env); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1548 |
JNU_ThrowByName(env, "java/lang/OutOfMemoryError", |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1549 |
"Initializing Reader"); |
4248 | 1550 |
imageio_dispose((j_common_ptr)cinfo); |
2 | 1551 |
return 0; |
1552 |
} |
|
8355 | 1553 |
return ptr_to_jlong(ret); |
2 | 1554 |
} |
1555 |
||
1556 |
/* |
|
1557 |
* When we set a source from Java, we set up the stream in the streamBuf |
|
1558 |
* object. If there was an old one, it is released first. |
|
1559 |
*/ |
|
1560 |
||
1561 |
JNIEXPORT void JNICALL |
|
1562 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_setSource |
|
1563 |
(JNIEnv *env, |
|
1564 |
jobject this, |
|
16881 | 1565 |
jlong ptr) { |
2 | 1566 |
|
8355 | 1567 |
imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr); |
2 | 1568 |
j_common_ptr cinfo; |
1569 |
||
1570 |
if (data == NULL) { |
|
1571 |
JNU_ThrowByName(env, |
|
1572 |
"java/lang/IllegalStateException", |
|
1573 |
"Attempting to use reader after dispose()"); |
|
1574 |
return; |
|
1575 |
} |
|
1576 |
||
1577 |
cinfo = data->jpegObj; |
|
1578 |
||
16881 | 1579 |
imageio_set_stream(env, cinfo, data, this); |
2 | 1580 |
|
1581 |
imageio_init_source((j_decompress_ptr) cinfo); |
|
1582 |
} |
|
1583 |
||
1584 |
#define JPEG_APP1 (JPEG_APP0 + 1) /* EXIF APP1 marker code */ |
|
1585 |
||
1586 |
/* |
|
1587 |
* For EXIF images, the APP1 will appear immediately after the SOI, |
|
1588 |
* so it's safe to only look at the first marker in the list. |
|
1589 |
* (see http://www.exif.org/Exif2-2.PDF, section 4.7, page 58) |
|
1590 |
*/ |
|
1591 |
#define IS_EXIF(c) \ |
|
1592 |
(((c)->marker_list != NULL) && ((c)->marker_list->marker == JPEG_APP1)) |
|
1593 |
||
1594 |
JNIEXPORT jboolean JNICALL |
|
1595 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_readImageHeader |
|
1596 |
(JNIEnv *env, |
|
1597 |
jobject this, |
|
1598 |
jlong ptr, |
|
1599 |
jboolean clearFirst, |
|
1600 |
jboolean reset) { |
|
1601 |
||
1602 |
int ret; |
|
1603 |
int h_samp0, h_samp1, h_samp2; |
|
1604 |
int v_samp0, v_samp1, v_samp2; |
|
1605 |
jboolean retval = JNI_FALSE; |
|
8355 | 1606 |
imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr); |
2 | 1607 |
j_decompress_ptr cinfo; |
1608 |
struct jpeg_source_mgr *src; |
|
1609 |
sun_jpeg_error_ptr jerr; |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1610 |
jbyteArray profileData = NULL; |
2 | 1611 |
|
1612 |
if (data == NULL) { |
|
1613 |
JNU_ThrowByName(env, |
|
1614 |
"java/lang/IllegalStateException", |
|
1615 |
"Attempting to use reader after dispose()"); |
|
1616 |
return JNI_FALSE; |
|
1617 |
} |
|
1618 |
||
1619 |
cinfo = (j_decompress_ptr) data->jpegObj; |
|
1620 |
src = cinfo->src; |
|
1621 |
||
1622 |
/* Establish the setjmp return context for sun_jpeg_error_exit to use. */ |
|
1623 |
jerr = (sun_jpeg_error_ptr) cinfo->err; |
|
1624 |
||
1625 |
if (setjmp(jerr->setjmp_buffer)) { |
|
1626 |
/* If we get here, the JPEG code has signaled an error |
|
1627 |
while reading the header. */ |
|
1628 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
1629 |
if (!(*env)->ExceptionOccurred(env)) { |
|
1630 |
char buffer[JMSG_LENGTH_MAX]; |
|
1631 |
(*cinfo->err->format_message) ((struct jpeg_common_struct *) cinfo, |
|
1632 |
buffer); |
|
1633 |
JNU_ThrowByName(env, "javax/imageio/IIOException", buffer); |
|
1634 |
} |
|
1635 |
return retval; |
|
1636 |
} |
|
1637 |
||
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1638 |
#ifdef DEBUG_IIO_JPEG |
2 | 1639 |
printf("In readImageHeader, data is %p cinfo is %p\n", data, cinfo); |
1640 |
printf("clearFirst is %d\n", clearFirst); |
|
1641 |
#endif |
|
1642 |
||
1643 |
if (GET_ARRAYS(env, data, &src->next_input_byte) == NOT_OK) { |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1644 |
(*env)->ExceptionClear(env); |
2 | 1645 |
JNU_ThrowByName(env, |
1646 |
"javax/imageio/IIOException", |
|
1647 |
"Array pin failed"); |
|
1648 |
return retval; |
|
1649 |
} |
|
1650 |
||
1651 |
/* |
|
1652 |
* Now clear the input buffer if the Java code has done a seek |
|
1653 |
* on the stream since the last call, invalidating any buffer contents. |
|
1654 |
*/ |
|
1655 |
if (clearFirst) { |
|
1656 |
clearStreamBuffer(&data->streamBuf); |
|
1657 |
src->next_input_byte = NULL; |
|
1658 |
src->bytes_in_buffer = 0; |
|
1659 |
} |
|
1660 |
||
1661 |
ret = jpeg_read_header(cinfo, FALSE); |
|
1662 |
||
1663 |
if (ret == JPEG_HEADER_TABLES_ONLY) { |
|
1664 |
retval = JNI_TRUE; |
|
1665 |
imageio_term_source(cinfo); // Pushback remaining buffer contents |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1666 |
#ifdef DEBUG_IIO_JPEG |
2 | 1667 |
printf("just read tables-only image; q table 0 at %p\n", |
1668 |
cinfo->quant_tbl_ptrs[0]); |
|
1669 |
#endif |
|
1670 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
1671 |
} else { |
|
1672 |
/* |
|
1673 |
* Now adjust the jpeg_color_space variable, which was set in |
|
1674 |
* default_decompress_parms, to reflect our differences from IJG |
|
1675 |
*/ |
|
1676 |
||
1677 |
switch (cinfo->jpeg_color_space) { |
|
1678 |
default : |
|
1679 |
break; |
|
1680 |
case JCS_YCbCr: |
|
1681 |
||
1682 |
/* |
|
1683 |
* There are several possibilities: |
|
1684 |
* - we got image with embeded colorspace |
|
1685 |
* Use it. User knows what he is doing. |
|
1686 |
* - we got JFIF image |
|
1687 |
* Must be YCbCr (see http://www.w3.org/Graphics/JPEG/jfif3.pdf, page 2) |
|
1688 |
* - we got EXIF image |
|
1689 |
* Must be YCbCr (see http://www.exif.org/Exif2-2.PDF, section 4.7, page 63) |
|
1690 |
* - something else |
|
1691 |
* Apply heuristical rules to identify actual colorspace. |
|
1692 |
*/ |
|
1693 |
||
1694 |
if (cinfo->saw_Adobe_marker) { |
|
1695 |
if (cinfo->Adobe_transform != 1) { |
|
1696 |
/* |
|
1697 |
* IJG guesses this is YCbCr and emits a warning |
|
1698 |
* We would rather not guess. Then the user knows |
|
1699 |
* To read this as a Raster if at all |
|
1700 |
*/ |
|
1701 |
cinfo->jpeg_color_space = JCS_UNKNOWN; |
|
1702 |
cinfo->out_color_space = JCS_UNKNOWN; |
|
1703 |
} |
|
1704 |
} else if (!cinfo->saw_JFIF_marker && !IS_EXIF(cinfo)) { |
|
1705 |
/* |
|
1706 |
* IJG assumes all unidentified 3-channels are YCbCr. |
|
1707 |
* We assume that only if the second two channels are |
|
1708 |
* subsampled (either horizontally or vertically). If not, |
|
1709 |
* we assume RGB. |
|
1710 |
* |
|
1711 |
* 4776576: Some digital cameras output YCbCr JPEG images |
|
1712 |
* that do not contain a JFIF APP0 marker but are only |
|
1713 |
* vertically subsampled (no horizontal subsampling). |
|
1714 |
* We should only assume this is RGB data if the subsampling |
|
1715 |
* factors for the second two channels are the same as the |
|
1716 |
* first (check both horizontal and vertical factors). |
|
1717 |
*/ |
|
1718 |
h_samp0 = cinfo->comp_info[0].h_samp_factor; |
|
1719 |
h_samp1 = cinfo->comp_info[1].h_samp_factor; |
|
1720 |
h_samp2 = cinfo->comp_info[2].h_samp_factor; |
|
1721 |
||
1722 |
v_samp0 = cinfo->comp_info[0].v_samp_factor; |
|
1723 |
v_samp1 = cinfo->comp_info[1].v_samp_factor; |
|
1724 |
v_samp2 = cinfo->comp_info[2].v_samp_factor; |
|
1725 |
||
1726 |
if ((h_samp1 == h_samp0) && (h_samp2 == h_samp0) && |
|
1727 |
(v_samp1 == v_samp0) && (v_samp2 == v_samp0)) |
|
1728 |
{ |
|
1729 |
cinfo->jpeg_color_space = JCS_RGB; |
|
1730 |
/* output is already RGB, so it stays the same */ |
|
1731 |
} |
|
1732 |
} |
|
1733 |
break; |
|
1734 |
#ifdef YCCALPHA |
|
1735 |
case JCS_YCC: |
|
1736 |
cinfo->out_color_space = JCS_YCC; |
|
1737 |
break; |
|
1738 |
#endif |
|
1739 |
case JCS_YCCK: |
|
1740 |
if ((cinfo->saw_Adobe_marker) && (cinfo->Adobe_transform != 2)) { |
|
1741 |
/* |
|
1742 |
* IJG guesses this is YCCK and emits a warning |
|
1743 |
* We would rather not guess. Then the user knows |
|
1744 |
* To read this as a Raster if at all |
|
1745 |
*/ |
|
1746 |
cinfo->jpeg_color_space = JCS_UNKNOWN; |
|
1747 |
cinfo->out_color_space = JCS_UNKNOWN; |
|
1748 |
} |
|
1749 |
break; |
|
1750 |
case JCS_CMYK: |
|
1751 |
/* |
|
1752 |
* IJG assumes all unidentified 4-channels are CMYK. |
|
1753 |
* We assume that only if the second two channels are |
|
1754 |
* not subsampled (either horizontally or vertically). |
|
1755 |
* If they are, we assume YCCK. |
|
1756 |
*/ |
|
1757 |
h_samp0 = cinfo->comp_info[0].h_samp_factor; |
|
1758 |
h_samp1 = cinfo->comp_info[1].h_samp_factor; |
|
1759 |
h_samp2 = cinfo->comp_info[2].h_samp_factor; |
|
1760 |
||
1761 |
v_samp0 = cinfo->comp_info[0].v_samp_factor; |
|
1762 |
v_samp1 = cinfo->comp_info[1].v_samp_factor; |
|
1763 |
v_samp2 = cinfo->comp_info[2].v_samp_factor; |
|
1764 |
||
1765 |
if ((h_samp1 > h_samp0) && (h_samp2 > h_samp0) || |
|
1766 |
(v_samp1 > v_samp0) && (v_samp2 > v_samp0)) |
|
1767 |
{ |
|
1768 |
cinfo->jpeg_color_space = JCS_YCCK; |
|
1769 |
/* Leave the output space as CMYK */ |
|
1770 |
} |
|
1771 |
} |
|
1772 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1773 |
|
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1774 |
/* read icc profile data */ |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1775 |
profileData = read_icc_profile(env, cinfo); |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1776 |
|
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1777 |
if ((*env)->ExceptionCheck(env)) { |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1778 |
return retval; |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1779 |
} |
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1780 |
|
2 | 1781 |
(*env)->CallVoidMethod(env, this, |
1782 |
JPEGImageReader_setImageDataID, |
|
1783 |
cinfo->image_width, |
|
1784 |
cinfo->image_height, |
|
1785 |
cinfo->jpeg_color_space, |
|
1786 |
cinfo->out_color_space, |
|
1787 |
cinfo->num_components, |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1788 |
profileData); |
2 | 1789 |
if (reset) { |
1790 |
jpeg_abort_decompress(cinfo); |
|
1791 |
} |
|
1792 |
} |
|
1793 |
||
1794 |
return retval; |
|
1795 |
} |
|
1796 |
||
1797 |
||
1798 |
JNIEXPORT void JNICALL |
|
1799 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_setOutColorSpace |
|
1800 |
(JNIEnv *env, |
|
1801 |
jobject this, |
|
1802 |
jlong ptr, |
|
1803 |
jint code) { |
|
1804 |
||
8355 | 1805 |
imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr); |
2 | 1806 |
j_decompress_ptr cinfo; |
1807 |
||
1808 |
if (data == NULL) { |
|
1809 |
JNU_ThrowByName(env, |
|
1810 |
"java/lang/IllegalStateException", |
|
1811 |
"Attempting to use reader after dispose()"); |
|
1812 |
return; |
|
1813 |
} |
|
1814 |
||
1815 |
cinfo = (j_decompress_ptr) data->jpegObj; |
|
1816 |
||
1817 |
cinfo->out_color_space = code; |
|
1818 |
||
1819 |
} |
|
1820 |
||
1821 |
JNIEXPORT jboolean JNICALL |
|
1822 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_readImage |
|
1823 |
(JNIEnv *env, |
|
1824 |
jobject this, |
|
1825 |
jlong ptr, |
|
1826 |
jbyteArray buffer, |
|
1827 |
jint numBands, |
|
1828 |
jintArray srcBands, |
|
1829 |
jintArray bandSizes, |
|
1830 |
jint sourceXStart, |
|
1831 |
jint sourceYStart, |
|
1832 |
jint sourceWidth, |
|
1833 |
jint sourceHeight, |
|
1834 |
jint stepX, |
|
1835 |
jint stepY, |
|
1836 |
jobjectArray qtables, |
|
1837 |
jobjectArray DCHuffmanTables, |
|
1838 |
jobjectArray ACHuffmanTables, |
|
1839 |
jint minProgressivePass, // Counts from 0 |
|
1840 |
jint maxProgressivePass, |
|
1841 |
jboolean wantUpdates) { |
|
1842 |
||
1843 |
||
1844 |
struct jpeg_source_mgr *src; |
|
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
1845 |
JSAMPROW scanLinePtr = NULL; |
2 | 1846 |
jint bands[MAX_BANDS]; |
8355 | 1847 |
int i; |
2 | 1848 |
jint *body; |
1849 |
int scanlineLimit; |
|
1850 |
int pixelStride; |
|
1851 |
unsigned char *in, *out, *pixelLimit; |
|
1852 |
int targetLine; |
|
1853 |
int skipLines, linesLeft; |
|
1854 |
pixelBufferPtr pb; |
|
1855 |
sun_jpeg_error_ptr jerr; |
|
1856 |
boolean done; |
|
1857 |
boolean mustScale = FALSE; |
|
1858 |
boolean progressive = FALSE; |
|
1859 |
boolean orderedBands = TRUE; |
|
8355 | 1860 |
imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr); |
2 | 1861 |
j_decompress_ptr cinfo; |
8355 | 1862 |
size_t numBytes; |
2 | 1863 |
|
1864 |
/* verify the inputs */ |
|
1865 |
||
1866 |
if (data == NULL) { |
|
1867 |
JNU_ThrowByName(env, |
|
1868 |
"java/lang/IllegalStateException", |
|
1869 |
"Attempting to use reader after dispose()"); |
|
1870 |
return JNI_FALSE; |
|
1871 |
} |
|
1872 |
||
1873 |
if ((buffer == NULL) || (srcBands == NULL)) { |
|
1874 |
JNU_ThrowNullPointerException(env, 0); |
|
1875 |
return JNI_FALSE; |
|
1876 |
} |
|
1877 |
||
1878 |
cinfo = (j_decompress_ptr) data->jpegObj; |
|
1879 |
||
8358
39c22ace748d
7018912: [parfait] potential buffer overruns in imageio jpeg
bae
parents:
8355
diff
changeset
|
1880 |
if ((numBands < 1) || (numBands > MAX_BANDS) || |
2 | 1881 |
(sourceXStart < 0) || (sourceXStart >= (jint)cinfo->image_width) || |
1882 |
(sourceYStart < 0) || (sourceYStart >= (jint)cinfo->image_height) || |
|
1883 |
(sourceWidth < 1) || (sourceWidth > (jint)cinfo->image_width) || |
|
1884 |
(sourceHeight < 1) || (sourceHeight > (jint)cinfo->image_height) || |
|
1885 |
(stepX < 1) || (stepY < 1) || |
|
1886 |
(minProgressivePass < 0) || |
|
1887 |
(maxProgressivePass < minProgressivePass)) |
|
1888 |
{ |
|
1889 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
|
1890 |
"Invalid argument to native readImage"); |
|
1891 |
return JNI_FALSE; |
|
1892 |
} |
|
1893 |
||
8355 | 1894 |
if (stepX > (jint)cinfo->image_width) { |
4201 | 1895 |
stepX = cinfo->image_width; |
1896 |
} |
|
8355 | 1897 |
if (stepY > (jint)cinfo->image_height) { |
4201 | 1898 |
stepY = cinfo->image_height; |
1899 |
} |
|
1900 |
||
2 | 1901 |
/* |
1902 |
* First get the source bands array and copy it to our local array |
|
1903 |
* so we don't have to worry about pinning and unpinning it again. |
|
1904 |
*/ |
|
1905 |
||
1906 |
body = (*env)->GetIntArrayElements(env, srcBands, NULL); |
|
1907 |
if (body == NULL) { |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1908 |
(*env)->ExceptionClear(env); |
2 | 1909 |
JNU_ThrowByName( env, |
1910 |
"java/lang/OutOfMemoryError", |
|
1911 |
"Initializing Read"); |
|
1912 |
return JNI_FALSE; |
|
1913 |
} |
|
1914 |
||
1915 |
for (i = 0; i < numBands; i++) { |
|
1916 |
bands[i] = body[i]; |
|
1917 |
if (orderedBands && (bands[i] != i)) { |
|
1918 |
orderedBands = FALSE; |
|
1919 |
} |
|
1920 |
} |
|
1921 |
||
1922 |
(*env)->ReleaseIntArrayElements(env, srcBands, body, JNI_ABORT); |
|
1923 |
||
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
1924 |
#ifdef DEBUG_IIO_JPEG |
2 | 1925 |
printf("---- in reader.read ----\n"); |
1926 |
printf("numBands is %d\n", numBands); |
|
1927 |
printf("bands array: "); |
|
1928 |
for (i = 0; i < numBands; i++) { |
|
1929 |
printf("%d ", bands[i]); |
|
1930 |
} |
|
1931 |
printf("\n"); |
|
1932 |
printf("jq table 0 at %p\n", |
|
1933 |
cinfo->quant_tbl_ptrs[0]); |
|
1934 |
#endif |
|
1935 |
||
1936 |
data = (imageIODataPtr) cinfo->client_data; |
|
1937 |
src = cinfo->src; |
|
1938 |
||
1939 |
/* Set the buffer as our PixelBuffer */ |
|
1940 |
pb = &data->pixelBuf; |
|
1941 |
||
1942 |
if (setPixelBuffer(env, pb, buffer) == NOT_OK) { |
|
1943 |
return data->abortFlag; // We already threw an out of memory exception |
|
1944 |
} |
|
1945 |
||
1946 |
/* Establish the setjmp return context for sun_jpeg_error_exit to use. */ |
|
1947 |
jerr = (sun_jpeg_error_ptr) cinfo->err; |
|
1948 |
||
1949 |
if (setjmp(jerr->setjmp_buffer)) { |
|
1950 |
/* If we get here, the JPEG code has signaled an error |
|
1951 |
while reading. */ |
|
1952 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
1953 |
if (!(*env)->ExceptionOccurred(env)) { |
|
1954 |
char buffer[JMSG_LENGTH_MAX]; |
|
1955 |
(*cinfo->err->format_message) ((struct jpeg_common_struct *) cinfo, |
|
1956 |
buffer); |
|
1957 |
JNU_ThrowByName(env, "javax/imageio/IIOException", buffer); |
|
1958 |
} |
|
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
1959 |
if (scanLinePtr != NULL) { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
1960 |
free(scanLinePtr); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
1961 |
scanLinePtr = NULL; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
1962 |
} |
2 | 1963 |
return data->abortFlag; |
1964 |
} |
|
1965 |
||
1966 |
if (GET_ARRAYS(env, data, &src->next_input_byte) == NOT_OK) { |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
1967 |
(*env)->ExceptionClear(env); |
2 | 1968 |
JNU_ThrowByName(env, |
1969 |
"javax/imageio/IIOException", |
|
1970 |
"Array pin failed"); |
|
1971 |
return data->abortFlag; |
|
1972 |
} |
|
1973 |
||
1974 |
// If there are no tables in our structure and table arguments aren't |
|
1975 |
// NULL, use the table arguments. |
|
1976 |
if ((qtables != NULL) && (cinfo->quant_tbl_ptrs[0] == NULL)) { |
|
1977 |
(void) setQTables(env, (j_common_ptr) cinfo, qtables, TRUE); |
|
1978 |
} |
|
1979 |
||
1980 |
if ((DCHuffmanTables != NULL) && (cinfo->dc_huff_tbl_ptrs[0] == NULL)) { |
|
1981 |
setHTables(env, (j_common_ptr) cinfo, |
|
1982 |
DCHuffmanTables, |
|
1983 |
ACHuffmanTables, |
|
1984 |
TRUE); |
|
1985 |
} |
|
1986 |
||
1987 |
progressive = jpeg_has_multiple_scans(cinfo); |
|
1988 |
if (progressive) { |
|
1989 |
cinfo->buffered_image = TRUE; |
|
1990 |
cinfo->input_scan_number = minProgressivePass+1; // Java count from 0 |
|
1991 |
#define MAX_JAVA_INT 2147483647 // XXX Is this defined in JNI somewhere? |
|
1992 |
if (maxProgressivePass < MAX_JAVA_INT) { |
|
1993 |
maxProgressivePass++; // For testing |
|
1994 |
} |
|
1995 |
} |
|
1996 |
||
1997 |
data->streamBuf.suspendable = FALSE; |
|
1998 |
||
1999 |
jpeg_start_decompress(cinfo); |
|
2000 |
||
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2001 |
if (numBands != cinfo->output_components) { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2002 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2003 |
"Invalid argument to native readImage"); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2004 |
return data->abortFlag; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2005 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2006 |
|
9827 | 2007 |
if (cinfo->output_components <= 0 || |
2008 |
cinfo->image_width > (0xffffffffu / (unsigned int)cinfo->output_components)) |
|
2009 |
{ |
|
2010 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
|
2011 |
"Invalid number of output components"); |
|
2012 |
return data->abortFlag; |
|
2013 |
} |
|
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2014 |
|
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2015 |
// Allocate a 1-scanline buffer |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2016 |
scanLinePtr = (JSAMPROW)malloc(cinfo->image_width*cinfo->output_components); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2017 |
if (scanLinePtr == NULL) { |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2018 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2019 |
JNU_ThrowByName( env, |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2020 |
"java/lang/OutOfMemoryError", |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2021 |
"Reading JPEG Stream"); |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2022 |
return data->abortFlag; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2023 |
} |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2024 |
|
2 | 2025 |
// loop over progressive passes |
2026 |
done = FALSE; |
|
2027 |
while (!done) { |
|
2028 |
if (progressive) { |
|
2029 |
// initialize the next pass. Note that this skips up to |
|
2030 |
// the first interesting pass. |
|
2031 |
jpeg_start_output(cinfo, cinfo->input_scan_number); |
|
2032 |
if (wantUpdates) { |
|
2033 |
(*env)->CallVoidMethod(env, this, |
|
2034 |
JPEGImageReader_passStartedID, |
|
2035 |
cinfo->input_scan_number-1); |
|
2036 |
} |
|
2037 |
} else if (wantUpdates) { |
|
2038 |
(*env)->CallVoidMethod(env, this, |
|
2039 |
JPEGImageReader_passStartedID, |
|
2040 |
0); |
|
2041 |
||
2042 |
} |
|
2043 |
||
2044 |
// Skip until the first interesting line |
|
2045 |
while ((data->abortFlag == JNI_FALSE) |
|
2046 |
&& ((jint)cinfo->output_scanline < sourceYStart)) { |
|
2047 |
jpeg_read_scanlines(cinfo, &scanLinePtr, 1); |
|
2048 |
} |
|
2049 |
||
2050 |
scanlineLimit = sourceYStart+sourceHeight; |
|
2051 |
pixelLimit = scanLinePtr |
|
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2052 |
+(sourceXStart+sourceWidth)*cinfo->output_components; |
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2053 |
|
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2054 |
pixelStride = stepX*cinfo->output_components; |
2 | 2055 |
targetLine = 0; |
2056 |
||
2057 |
while ((data->abortFlag == JNI_FALSE) |
|
2058 |
&& ((jint)cinfo->output_scanline < scanlineLimit)) { |
|
2059 |
||
2060 |
jpeg_read_scanlines(cinfo, &scanLinePtr, 1); |
|
2061 |
||
2062 |
// Now mangle it into our buffer |
|
2063 |
out = data->pixelBuf.buf.bp; |
|
2064 |
||
2065 |
if (orderedBands && (pixelStride == numBands)) { |
|
2066 |
// Optimization: The component bands are ordered sequentially, |
|
2067 |
// so we can simply use memcpy() to copy the intermediate |
|
2068 |
// scanline buffer into the raster. |
|
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2069 |
in = scanLinePtr + (sourceXStart * cinfo->output_components); |
2 | 2070 |
if (pixelLimit > in) { |
5189
4909c522e208
6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents:
4285
diff
changeset
|
2071 |
numBytes = pixelLimit - in; |
4909c522e208
6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents:
4285
diff
changeset
|
2072 |
if (numBytes > data->pixelBuf.byteBufferLength) { |
4909c522e208
6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents:
4285
diff
changeset
|
2073 |
numBytes = data->pixelBuf.byteBufferLength; |
4909c522e208
6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents:
4285
diff
changeset
|
2074 |
} |
4909c522e208
6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents:
4285
diff
changeset
|
2075 |
memcpy(out, in, numBytes); |
2 | 2076 |
} |
2077 |
} else { |
|
5189
4909c522e208
6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents:
4285
diff
changeset
|
2078 |
numBytes = numBands; |
3009
de653b2cab31
4893408: JPEGReader throws IllegalArgException when setting the destination to BYTE_GRAY
bae
parents:
2383
diff
changeset
|
2079 |
for (in = scanLinePtr+sourceXStart*cinfo->output_components; |
5189
4909c522e208
6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents:
4285
diff
changeset
|
2080 |
in < pixelLimit && |
4909c522e208
6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents:
4285
diff
changeset
|
2081 |
numBytes <= data->pixelBuf.byteBufferLength; |
2 | 2082 |
in += pixelStride) { |
2083 |
for (i = 0; i < numBands; i++) { |
|
2084 |
*out++ = *(in+bands[i]); |
|
2085 |
} |
|
5189
4909c522e208
6909597: Sun Java Runtime Environment JPEGImageReader stepX Integer Overflow Vulnerability
bae
parents:
4285
diff
changeset
|
2086 |
numBytes += numBands; |
2 | 2087 |
} |
2088 |
} |
|
2089 |
||
2090 |
// And call it back to Java |
|
2091 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
2092 |
(*env)->CallVoidMethod(env, |
|
2093 |
this, |
|
2094 |
JPEGImageReader_acceptPixelsID, |
|
2095 |
targetLine++, |
|
2096 |
progressive); |
|
2097 |
||
2098 |
if ((*env)->ExceptionOccurred(env) |
|
2099 |
|| !GET_ARRAYS(env, data, &(src->next_input_byte))) { |
|
2100 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
2101 |
} |
|
2102 |
||
2103 |
// And skip over uninteresting lines to the next subsampled line |
|
2104 |
// Ensure we don't go past the end of the image |
|
2105 |
||
2106 |
// Lines to skip based on subsampling |
|
2107 |
skipLines = stepY - 1; |
|
2108 |
// Lines left in the image |
|
2109 |
linesLeft = scanlineLimit - cinfo->output_scanline; |
|
2110 |
// Take the minimum |
|
2111 |
if (skipLines > linesLeft) { |
|
2112 |
skipLines = linesLeft; |
|
2113 |
} |
|
2114 |
for(i = 0; i < skipLines; i++) { |
|
2115 |
jpeg_read_scanlines(cinfo, &scanLinePtr, 1); |
|
2116 |
} |
|
2117 |
} |
|
2118 |
if (progressive) { |
|
2119 |
jpeg_finish_output(cinfo); // Increments pass counter |
|
2120 |
// Call Java to notify pass complete |
|
2121 |
if (jpeg_input_complete(cinfo) |
|
2122 |
|| (cinfo->input_scan_number > maxProgressivePass)) { |
|
2123 |
done = TRUE; |
|
2124 |
} |
|
2125 |
} else { |
|
2126 |
done = TRUE; |
|
2127 |
} |
|
2128 |
if (wantUpdates) { |
|
2129 |
(*env)->CallVoidMethod(env, this, |
|
2130 |
JPEGImageReader_passCompleteID); |
|
2131 |
} |
|
2132 |
||
2133 |
} |
|
2134 |
/* |
|
2135 |
* We are done, but we might not have read all the lines, or all |
|
2136 |
* the passes, so use jpeg_abort instead of jpeg_finish_decompress. |
|
2137 |
*/ |
|
2138 |
if (cinfo->output_scanline == cinfo->output_height) { |
|
2139 |
// if ((cinfo->output_scanline == cinfo->output_height) && |
|
2140 |
//(jpeg_input_complete(cinfo))) { // We read the whole file |
|
2141 |
jpeg_finish_decompress(cinfo); |
|
2142 |
} else { |
|
2143 |
jpeg_abort_decompress(cinfo); |
|
2144 |
} |
|
2145 |
||
2146 |
free(scanLinePtr); |
|
2147 |
||
2148 |
RELEASE_ARRAYS(env, data, src->next_input_byte); |
|
2149 |
||
2150 |
return data->abortFlag; |
|
2151 |
} |
|
2152 |
||
2153 |
JNIEXPORT void JNICALL |
|
2154 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_abortRead |
|
2155 |
(JNIEnv *env, |
|
2156 |
jobject this, |
|
2157 |
jlong ptr) { |
|
2158 |
||
8355 | 2159 |
imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr); |
2 | 2160 |
|
2161 |
if (data == NULL) { |
|
2162 |
JNU_ThrowByName(env, |
|
2163 |
"java/lang/IllegalStateException", |
|
2164 |
"Attempting to use reader after dispose()"); |
|
2165 |
return; |
|
2166 |
} |
|
2167 |
||
2168 |
imageio_abort(env, this, data); |
|
2169 |
||
2170 |
} |
|
2171 |
||
2172 |
JNIEXPORT void JNICALL |
|
2173 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_resetLibraryState |
|
2174 |
(JNIEnv *env, |
|
2175 |
jobject this, |
|
2176 |
jlong ptr) { |
|
8355 | 2177 |
imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr); |
2 | 2178 |
j_decompress_ptr cinfo; |
2179 |
||
2180 |
if (data == NULL) { |
|
2181 |
JNU_ThrowByName(env, |
|
2182 |
"java/lang/IllegalStateException", |
|
2183 |
"Attempting to use reader after dispose()"); |
|
2184 |
return; |
|
2185 |
} |
|
2186 |
||
2187 |
cinfo = (j_decompress_ptr) data->jpegObj; |
|
2188 |
||
2189 |
jpeg_abort_decompress(cinfo); |
|
2190 |
} |
|
2191 |
||
2192 |
||
2193 |
JNIEXPORT void JNICALL |
|
2194 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_resetReader |
|
2195 |
(JNIEnv *env, |
|
2196 |
jobject this, |
|
2197 |
jlong ptr) { |
|
2198 |
||
8355 | 2199 |
imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr); |
2 | 2200 |
j_decompress_ptr cinfo; |
2201 |
sun_jpeg_error_ptr jerr; |
|
2202 |
||
2203 |
if (data == NULL) { |
|
2204 |
JNU_ThrowByName(env, |
|
2205 |
"java/lang/IllegalStateException", |
|
2206 |
"Attempting to use reader after dispose()"); |
|
2207 |
return; |
|
2208 |
} |
|
2209 |
||
2210 |
cinfo = (j_decompress_ptr) data->jpegObj; |
|
2211 |
||
2212 |
jerr = (sun_jpeg_error_ptr) cinfo->err; |
|
2213 |
||
2214 |
imageio_reset(env, (j_common_ptr) cinfo, data); |
|
2215 |
||
2216 |
/* |
|
2217 |
* The tables have not been reset, and there is no way to do so |
|
2218 |
* in IJG without leaking memory. The only situation in which |
|
2219 |
* this will cause a problem is if an image-only stream is read |
|
2220 |
* with this object without initializing the correct tables first. |
|
2221 |
* This situation, which should cause an error, might work but |
|
2222 |
* produce garbage instead. If the huffman tables are wrong, |
|
2223 |
* it will fail during the decode. If the q tables are wrong, it |
|
2224 |
* will look strange. This is very unlikely, so don't worry about |
|
2225 |
* it. To be really robust, we would keep a flag for table state |
|
2226 |
* and consult it to catch exceptional situations. |
|
2227 |
*/ |
|
2228 |
||
2229 |
/* above does not clean up the source, so we have to */ |
|
2230 |
||
2231 |
/* |
|
2232 |
We need to explicitly initialize exception handler or we may |
|
2233 |
longjump to random address from the term_source() |
|
2234 |
*/ |
|
2235 |
||
2236 |
if (setjmp(jerr->setjmp_buffer)) { |
|
2237 |
||
2238 |
/* |
|
2239 |
We may get IOException from pushBack() here. |
|
2240 |
||
2241 |
However it could be legal if original input stream was closed |
|
2242 |
earlier (for example because network connection was closed). |
|
2243 |
Unfortunately, image inputstream API has no way to check whether |
|
2244 |
stream is already closed or IOException was thrown because of some |
|
2245 |
other IO problem, |
|
2246 |
And we can not avoid call to pushBack() on closed stream for the |
|
2247 |
same reason. |
|
2248 |
||
2249 |
So, for now we will silently eat this exception. |
|
2250 |
||
2251 |
NB: this may be changed in future when ImageInputStream API will |
|
2252 |
become more flexible. |
|
2253 |
*/ |
|
2254 |
||
2255 |
if ((*env)->ExceptionOccurred(env)) { |
|
2256 |
(*env)->ExceptionClear(env); |
|
2257 |
} |
|
2258 |
} else { |
|
2259 |
cinfo->src->term_source(cinfo); |
|
2260 |
} |
|
2261 |
||
2262 |
cinfo->src->bytes_in_buffer = 0; |
|
2263 |
cinfo->src->next_input_byte = NULL; |
|
2264 |
} |
|
2265 |
||
2266 |
JNIEXPORT void JNICALL |
|
2267 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageReader_disposeReader |
|
2268 |
(JNIEnv *env, |
|
2269 |
jclass reader, |
|
2270 |
jlong ptr) { |
|
2271 |
||
8355 | 2272 |
imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr); |
2 | 2273 |
j_common_ptr info = destroyImageioData(env, data); |
2274 |
||
2275 |
imageio_dispose(info); |
|
2276 |
} |
|
2277 |
||
2278 |
/********************** end of Reader *************************/ |
|
2279 |
||
2280 |
/********************** Writer Support ************************/ |
|
2281 |
||
2282 |
/********************** Destination Manager *******************/ |
|
2283 |
||
2284 |
METHODDEF(void) |
|
2285 |
/* |
|
2286 |
* Initialize destination --- called by jpeg_start_compress |
|
2287 |
* before any data is actually written. The data arrays |
|
2288 |
* must be pinned before this is called. |
|
2289 |
*/ |
|
2290 |
imageio_init_destination (j_compress_ptr cinfo) |
|
2291 |
{ |
|
2292 |
struct jpeg_destination_mgr *dest = cinfo->dest; |
|
2293 |
imageIODataPtr data = (imageIODataPtr) cinfo->client_data; |
|
2294 |
streamBufferPtr sb = &data->streamBuf; |
|
2295 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
2296 |
||
2297 |
if (sb->buf == NULL) { |
|
2298 |
// We forgot to pin the array |
|
2299 |
(*env)->FatalError(env, "Output buffer not pinned!"); |
|
2300 |
} |
|
2301 |
||
2302 |
dest->next_output_byte = sb->buf; |
|
2303 |
dest->free_in_buffer = sb->bufferLength; |
|
2304 |
} |
|
2305 |
||
2306 |
/* |
|
2307 |
* Empty the output buffer --- called whenever buffer fills up. |
|
2308 |
* |
|
2309 |
* This routine writes the entire output buffer |
|
2310 |
* (ignoring the current state of next_output_byte & free_in_buffer), |
|
2311 |
* resets the pointer & count to the start of the buffer, and returns TRUE |
|
2312 |
* indicating that the buffer has been dumped. |
|
2313 |
*/ |
|
2314 |
||
2315 |
METHODDEF(boolean) |
|
2316 |
imageio_empty_output_buffer (j_compress_ptr cinfo) |
|
2317 |
{ |
|
2318 |
struct jpeg_destination_mgr *dest = cinfo->dest; |
|
2319 |
imageIODataPtr data = (imageIODataPtr) cinfo->client_data; |
|
2320 |
streamBufferPtr sb = &data->streamBuf; |
|
2321 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
2322 |
jobject output = NULL; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
2323 |
|
19788 | 2324 |
RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte)); |
2325 |
||
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
2326 |
GET_IO_REF(output); |
2 | 2327 |
|
2328 |
(*env)->CallVoidMethod(env, |
|
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
2329 |
output, |
16882 | 2330 |
JPEGImageWriter_writeOutputDataID, |
2 | 2331 |
sb->hstreamBuffer, |
2332 |
0, |
|
2333 |
sb->bufferLength); |
|
2334 |
if ((*env)->ExceptionOccurred(env) |
|
2335 |
|| !GET_ARRAYS(env, data, |
|
2336 |
(const JOCTET **)(&dest->next_output_byte))) { |
|
2337 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
2338 |
} |
|
2339 |
||
2340 |
dest->next_output_byte = sb->buf; |
|
2341 |
dest->free_in_buffer = sb->bufferLength; |
|
2342 |
||
2343 |
return TRUE; |
|
2344 |
} |
|
2345 |
||
2346 |
/* |
|
2347 |
* After all of the data has been encoded there may still be some |
|
2348 |
* more left over in some of the working buffers. Now is the |
|
2349 |
* time to clear them out. |
|
2350 |
*/ |
|
2351 |
METHODDEF(void) |
|
2352 |
imageio_term_destination (j_compress_ptr cinfo) |
|
2353 |
{ |
|
2354 |
struct jpeg_destination_mgr *dest = cinfo->dest; |
|
2355 |
imageIODataPtr data = (imageIODataPtr) cinfo->client_data; |
|
2356 |
streamBufferPtr sb = &data->streamBuf; |
|
2357 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
2358 |
||
2359 |
/* find out how much needs to be written */ |
|
8355 | 2360 |
/* this conversion from size_t to jint is safe, because the lenght of the buffer is limited by jint */ |
2361 |
jint datacount = (jint)(sb->bufferLength - dest->free_in_buffer); |
|
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
2362 |
|
2 | 2363 |
if (datacount != 0) { |
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
2364 |
jobject output = NULL; |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
2365 |
|
19788 | 2366 |
RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte)); |
2367 |
||
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
2368 |
GET_IO_REF(output); |
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
2369 |
|
2 | 2370 |
(*env)->CallVoidMethod(env, |
19017
0528383260ef
8020983: OutOfMemoryError caused by non garbage collected JPEGImageWriter Instances
bae
parents:
16882
diff
changeset
|
2371 |
output, |
16882 | 2372 |
JPEGImageWriter_writeOutputDataID, |
2 | 2373 |
sb->hstreamBuffer, |
2374 |
0, |
|
2375 |
datacount); |
|
2376 |
||
2377 |
if ((*env)->ExceptionOccurred(env) |
|
2378 |
|| !GET_ARRAYS(env, data, |
|
2379 |
(const JOCTET **)(&dest->next_output_byte))) { |
|
2380 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
2381 |
} |
|
2382 |
} |
|
2383 |
||
2384 |
dest->next_output_byte = NULL; |
|
2385 |
dest->free_in_buffer = 0; |
|
2386 |
||
2387 |
} |
|
2388 |
||
2389 |
/* |
|
2390 |
* Flush the destination buffer. This is not called by the library, |
|
2391 |
* but by our code below. This is the simplest implementation, though |
|
2392 |
* certainly not the most efficient. |
|
2393 |
*/ |
|
2394 |
METHODDEF(void) |
|
2395 |
imageio_flush_destination(j_compress_ptr cinfo) |
|
2396 |
{ |
|
2397 |
imageio_term_destination(cinfo); |
|
2398 |
imageio_init_destination(cinfo); |
|
2399 |
} |
|
2400 |
||
2401 |
/********************** end of destination manager ************/ |
|
2402 |
||
2403 |
/********************** Writer JNI calls **********************/ |
|
2404 |
||
2405 |
||
2406 |
JNIEXPORT void JNICALL |
|
2407 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_initWriterIDs |
|
2408 |
(JNIEnv *env, |
|
2409 |
jclass cls, |
|
2410 |
jclass qTableClass, |
|
2411 |
jclass huffClass) { |
|
2412 |
||
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2413 |
CHECK_NULL(JPEGImageWriter_writeOutputDataID = (*env)->GetMethodID(env, |
16882 | 2414 |
cls, |
2415 |
"writeOutputData", |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2416 |
"([BII)V")); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2417 |
CHECK_NULL(JPEGImageWriter_warningOccurredID = (*env)->GetMethodID(env, |
2 | 2418 |
cls, |
2419 |
"warningOccurred", |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2420 |
"(I)V")); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2421 |
CHECK_NULL(JPEGImageWriter_warningWithMessageID = |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2422 |
(*env)->GetMethodID(env, |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2423 |
cls, |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2424 |
"warningWithMessage", |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2425 |
"(Ljava/lang/String;)V")); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2426 |
CHECK_NULL(JPEGImageWriter_writeMetadataID = (*env)->GetMethodID(env, |
2 | 2427 |
cls, |
2428 |
"writeMetadata", |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2429 |
"()V")); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2430 |
CHECK_NULL(JPEGImageWriter_grabPixelsID = (*env)->GetMethodID(env, |
2 | 2431 |
cls, |
2432 |
"grabPixels", |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2433 |
"(I)V")); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2434 |
CHECK_NULL(JPEGQTable_tableID = (*env)->GetFieldID(env, |
2 | 2435 |
qTableClass, |
2436 |
"qTable", |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2437 |
"[I")); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2438 |
CHECK_NULL(JPEGHuffmanTable_lengthsID = (*env)->GetFieldID(env, |
2 | 2439 |
huffClass, |
2440 |
"lengths", |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2441 |
"[S")); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2442 |
CHECK_NULL(JPEGHuffmanTable_valuesID = (*env)->GetFieldID(env, |
2 | 2443 |
huffClass, |
2444 |
"values", |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2445 |
"[S")); |
2 | 2446 |
} |
2447 |
||
2448 |
JNIEXPORT jlong JNICALL |
|
2449 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_initJPEGImageWriter |
|
2450 |
(JNIEnv *env, |
|
2451 |
jobject this) { |
|
2452 |
||
2453 |
imageIODataPtr ret; |
|
2454 |
struct sun_jpeg_error_mgr *jerr; |
|
2455 |
struct jpeg_destination_mgr *dest; |
|
2456 |
||
2457 |
/* This struct contains the JPEG compression parameters and pointers to |
|
2458 |
* working space (which is allocated as needed by the JPEG library). |
|
2459 |
*/ |
|
2460 |
struct jpeg_compress_struct *cinfo = |
|
2461 |
malloc(sizeof(struct jpeg_compress_struct)); |
|
2462 |
if (cinfo == NULL) { |
|
2463 |
JNU_ThrowByName( env, |
|
2464 |
"java/lang/OutOfMemoryError", |
|
2465 |
"Initializing Writer"); |
|
2466 |
return 0; |
|
2467 |
} |
|
2468 |
||
2469 |
/* We use our private extension JPEG error handler. |
|
2470 |
*/ |
|
2471 |
jerr = malloc (sizeof(struct sun_jpeg_error_mgr)); |
|
2472 |
if (jerr == NULL) { |
|
2473 |
JNU_ThrowByName( env, |
|
2474 |
"java/lang/OutOfMemoryError", |
|
2475 |
"Initializing Writer"); |
|
2476 |
free(cinfo); |
|
2477 |
return 0; |
|
2478 |
} |
|
2479 |
||
2480 |
/* We set up the normal JPEG error routines, then override error_exit. */ |
|
2481 |
cinfo->err = jpeg_std_error(&(jerr->pub)); |
|
2482 |
jerr->pub.error_exit = sun_jpeg_error_exit; |
|
2483 |
/* We need to setup our own print routines */ |
|
2484 |
jerr->pub.output_message = sun_jpeg_output_message; |
|
2485 |
/* Now we can setjmp before every call to the library */ |
|
2486 |
||
2487 |
/* Establish the setjmp return context for sun_jpeg_error_exit to use. */ |
|
2488 |
if (setjmp(jerr->setjmp_buffer)) { |
|
2489 |
/* If we get here, the JPEG code has signaled an error. */ |
|
2490 |
char buffer[JMSG_LENGTH_MAX]; |
|
2491 |
(*cinfo->err->format_message) ((struct jpeg_common_struct *) cinfo, |
|
2492 |
buffer); |
|
2493 |
JNU_ThrowByName(env, "javax/imageio/IIOException", buffer); |
|
2494 |
return 0; |
|
2495 |
} |
|
2496 |
||
2497 |
/* Perform library initialization */ |
|
2498 |
jpeg_create_compress(cinfo); |
|
2499 |
||
2500 |
/* Now set up the destination */ |
|
2501 |
dest = malloc(sizeof(struct jpeg_destination_mgr)); |
|
2502 |
if (dest == NULL) { |
|
2503 |
JNU_ThrowByName( env, |
|
2504 |
"java/lang/OutOfMemoryError", |
|
2505 |
"Initializing Writer"); |
|
4248 | 2506 |
imageio_dispose((j_common_ptr)cinfo); |
2 | 2507 |
return 0; |
2508 |
} |
|
2509 |
||
2510 |
dest->init_destination = imageio_init_destination; |
|
2511 |
dest->empty_output_buffer = imageio_empty_output_buffer; |
|
2512 |
dest->term_destination = imageio_term_destination; |
|
2513 |
dest->next_output_byte = NULL; |
|
2514 |
dest->free_in_buffer = 0; |
|
2515 |
||
2516 |
cinfo->dest = dest; |
|
2517 |
||
2518 |
/* set up the association to persist for future calls */ |
|
2519 |
ret = initImageioData(env, (j_common_ptr) cinfo, this); |
|
2520 |
if (ret == NULL) { |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2521 |
(*env)->ExceptionClear(env); |
2 | 2522 |
JNU_ThrowByName( env, |
2523 |
"java/lang/OutOfMemoryError", |
|
2524 |
"Initializing Writer"); |
|
4248 | 2525 |
imageio_dispose((j_common_ptr)cinfo); |
2 | 2526 |
return 0; |
2527 |
} |
|
8355 | 2528 |
return ptr_to_jlong(ret); |
2 | 2529 |
} |
2530 |
||
2531 |
JNIEXPORT void JNICALL |
|
2532 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_setDest |
|
2533 |
(JNIEnv *env, |
|
2534 |
jobject this, |
|
16882 | 2535 |
jlong ptr) { |
2 | 2536 |
|
8355 | 2537 |
imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr); |
2 | 2538 |
j_compress_ptr cinfo; |
2539 |
||
2540 |
if (data == NULL) { |
|
2541 |
JNU_ThrowByName(env, |
|
2542 |
"java/lang/IllegalStateException", |
|
2543 |
"Attempting to use writer after dispose()"); |
|
2544 |
return; |
|
2545 |
} |
|
2546 |
||
2547 |
cinfo = (j_compress_ptr) data->jpegObj; |
|
2548 |
||
16882 | 2549 |
imageio_set_stream(env, data->jpegObj, data, this); |
2 | 2550 |
|
2551 |
||
2552 |
// Don't call the init method, as that depends on pinned arrays |
|
2553 |
cinfo->dest->next_output_byte = NULL; |
|
2554 |
cinfo->dest->free_in_buffer = 0; |
|
2555 |
} |
|
2556 |
||
2557 |
JNIEXPORT void JNICALL |
|
2558 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_writeTables |
|
2559 |
(JNIEnv *env, |
|
2560 |
jobject this, |
|
2561 |
jlong ptr, |
|
2562 |
jobjectArray qtables, |
|
2563 |
jobjectArray DCHuffmanTables, |
|
2564 |
jobjectArray ACHuffmanTables) { |
|
2565 |
||
2566 |
struct jpeg_destination_mgr *dest; |
|
2567 |
sun_jpeg_error_ptr jerr; |
|
8355 | 2568 |
imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr); |
2 | 2569 |
j_compress_ptr cinfo; |
2570 |
||
2571 |
if (data == NULL) { |
|
2572 |
JNU_ThrowByName(env, |
|
2573 |
"java/lang/IllegalStateException", |
|
2574 |
"Attempting to use writer after dispose()"); |
|
2575 |
return; |
|
2576 |
} |
|
2577 |
||
2578 |
cinfo = (j_compress_ptr) data->jpegObj; |
|
2579 |
dest = cinfo->dest; |
|
2580 |
||
2581 |
/* Establish the setjmp return context for sun_jpeg_error_exit to use. */ |
|
2582 |
jerr = (sun_jpeg_error_ptr) cinfo->err; |
|
2583 |
||
2584 |
if (setjmp(jerr->setjmp_buffer)) { |
|
2585 |
/* If we get here, the JPEG code has signaled an error |
|
2586 |
while writing. */ |
|
2587 |
RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte)); |
|
2588 |
if (!(*env)->ExceptionOccurred(env)) { |
|
2589 |
char buffer[JMSG_LENGTH_MAX]; |
|
2590 |
(*cinfo->err->format_message) ((j_common_ptr) cinfo, |
|
2591 |
buffer); |
|
2592 |
JNU_ThrowByName(env, "javax/imageio/IIOException", buffer); |
|
2593 |
} |
|
2594 |
return; |
|
2595 |
} |
|
2596 |
||
2597 |
if (GET_ARRAYS(env, data, |
|
2598 |
(const JOCTET **)(&dest->next_output_byte)) == NOT_OK) { |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2599 |
(*env)->ExceptionClear(env); |
2 | 2600 |
JNU_ThrowByName(env, |
2601 |
"javax/imageio/IIOException", |
|
2602 |
"Array pin failed"); |
|
2603 |
return; |
|
2604 |
} |
|
2605 |
||
2606 |
jpeg_suppress_tables(cinfo, TRUE); // Suppress writing of any current |
|
2607 |
||
2608 |
data->streamBuf.suspendable = FALSE; |
|
2609 |
if (qtables != NULL) { |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
2610 |
#ifdef DEBUG_IIO_JPEG |
2 | 2611 |
printf("in writeTables: qtables not NULL\n"); |
2612 |
#endif |
|
2613 |
setQTables(env, (j_common_ptr) cinfo, qtables, TRUE); |
|
2614 |
} |
|
2615 |
||
2616 |
if (DCHuffmanTables != NULL) { |
|
2617 |
setHTables(env, (j_common_ptr) cinfo, |
|
2618 |
DCHuffmanTables, ACHuffmanTables, TRUE); |
|
2619 |
} |
|
2620 |
||
2621 |
jpeg_write_tables(cinfo); // Flushes the buffer for you |
|
2622 |
RELEASE_ARRAYS(env, data, NULL); |
|
2623 |
} |
|
2624 |
||
2625 |
JNIEXPORT jboolean JNICALL |
|
2626 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_writeImage |
|
2627 |
(JNIEnv *env, |
|
2628 |
jobject this, |
|
2629 |
jlong ptr, |
|
2630 |
jbyteArray buffer, |
|
2631 |
jint inCs, jint outCs, |
|
2632 |
jint numBands, |
|
2633 |
jintArray bandSizes, |
|
2634 |
jint srcWidth, |
|
2635 |
jint destWidth, jint destHeight, |
|
2636 |
jint stepX, jint stepY, |
|
2637 |
jobjectArray qtables, |
|
2638 |
jboolean writeDQT, |
|
2639 |
jobjectArray DCHuffmanTables, |
|
2640 |
jobjectArray ACHuffmanTables, |
|
2641 |
jboolean writeDHT, |
|
2642 |
jboolean optimize, |
|
2643 |
jboolean progressive, |
|
2644 |
jint numScans, |
|
2645 |
jintArray scanInfo, |
|
2646 |
jintArray componentIds, |
|
2647 |
jintArray HsamplingFactors, |
|
2648 |
jintArray VsamplingFactors, |
|
2649 |
jintArray QtableSelectors, |
|
2650 |
jboolean haveMetadata, |
|
2651 |
jint restartInterval) { |
|
2652 |
||
2653 |
struct jpeg_destination_mgr *dest; |
|
2654 |
JSAMPROW scanLinePtr; |
|
2655 |
int i, j; |
|
2656 |
int pixelStride; |
|
6865
e7987a9205f4
6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents:
5506
diff
changeset
|
2657 |
unsigned char *in, *out, *pixelLimit, *scanLineLimit; |
e7987a9205f4
6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents:
5506
diff
changeset
|
2658 |
unsigned int scanLineSize, pixelBufferSize; |
2 | 2659 |
int targetLine; |
2660 |
pixelBufferPtr pb; |
|
2661 |
sun_jpeg_error_ptr jerr; |
|
2662 |
jint *ids, *hfactors, *vfactors, *qsels; |
|
2663 |
jsize qlen, hlen; |
|
2664 |
int *scanptr; |
|
2665 |
jint *scanData; |
|
2666 |
jint *bandSize; |
|
2667 |
int maxBandValue, halfMaxBandValue; |
|
8355 | 2668 |
imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr); |
2 | 2669 |
j_compress_ptr cinfo; |
2670 |
UINT8** scale = NULL; |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2671 |
boolean success = TRUE; |
2 | 2672 |
|
8355 | 2673 |
|
2 | 2674 |
/* verify the inputs */ |
2675 |
||
2676 |
if (data == NULL) { |
|
2677 |
JNU_ThrowByName(env, |
|
2678 |
"java/lang/IllegalStateException", |
|
2679 |
"Attempting to use writer after dispose()"); |
|
2680 |
return JNI_FALSE; |
|
2681 |
} |
|
2682 |
||
2683 |
if ((buffer == NULL) || |
|
2684 |
(qtables == NULL) || |
|
2685 |
// H tables can be null if optimizing |
|
2686 |
(componentIds == NULL) || |
|
2687 |
(HsamplingFactors == NULL) || (VsamplingFactors == NULL) || |
|
2688 |
(QtableSelectors == NULL) || |
|
2689 |
((numScans != 0) && (scanInfo != NULL))) { |
|
2690 |
||
2691 |
JNU_ThrowNullPointerException(env, 0); |
|
2692 |
return JNI_FALSE; |
|
2693 |
||
2694 |
} |
|
2695 |
||
6865
e7987a9205f4
6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents:
5506
diff
changeset
|
2696 |
scanLineSize = destWidth * numBands; |
2 | 2697 |
if ((inCs < 0) || (inCs > JCS_YCCK) || |
2698 |
(outCs < 0) || (outCs > JCS_YCCK) || |
|
2699 |
(numBands < 1) || (numBands > MAX_BANDS) || |
|
2700 |
(srcWidth < 0) || |
|
2701 |
(destWidth < 0) || (destWidth > srcWidth) || |
|
2702 |
(destHeight < 0) || |
|
6865
e7987a9205f4
6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents:
5506
diff
changeset
|
2703 |
(stepX < 0) || (stepY < 0) || |
8749 | 2704 |
((INT_MAX / numBands) < destWidth)) /* destWidth causes an integer overflow */ |
2 | 2705 |
{ |
2706 |
JNU_ThrowByName(env, "javax/imageio/IIOException", |
|
2707 |
"Invalid argument to native writeImage"); |
|
2708 |
return JNI_FALSE; |
|
2709 |
} |
|
2710 |
||
6865
e7987a9205f4
6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents:
5506
diff
changeset
|
2711 |
if (stepX > srcWidth) { |
e7987a9205f4
6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents:
5506
diff
changeset
|
2712 |
stepX = srcWidth; |
e7987a9205f4
6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents:
5506
diff
changeset
|
2713 |
} |
e7987a9205f4
6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents:
5506
diff
changeset
|
2714 |
|
2 | 2715 |
bandSize = (*env)->GetIntArrayElements(env, bandSizes, NULL); |
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2716 |
CHECK_NULL_RETURN(bandSize, JNI_FALSE); |
2 | 2717 |
|
2718 |
for (i = 0; i < numBands; i++) { |
|
20798 | 2719 |
if (bandSize[i] <= 0 || bandSize[i] > JPEG_BAND_SIZE) { |
2720 |
(*env)->ReleaseIntArrayElements(env, bandSizes, |
|
2721 |
bandSize, JNI_ABORT); |
|
2722 |
JNU_ThrowByName(env, "javax/imageio/IIOException", "Invalid Image"); |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2723 |
return JNI_FALSE; |
20798 | 2724 |
} |
2725 |
} |
|
2726 |
||
2727 |
for (i = 0; i < numBands; i++) { |
|
2 | 2728 |
if (bandSize[i] != JPEG_BAND_SIZE) { |
2729 |
if (scale == NULL) { |
|
2730 |
scale = (UINT8**) calloc(numBands, sizeof(UINT8*)); |
|
2731 |
||
2732 |
if (scale == NULL) { |
|
2733 |
JNU_ThrowByName( env, "java/lang/OutOfMemoryError", |
|
2734 |
"Writing JPEG Stream"); |
|
2735 |
return JNI_FALSE; |
|
2736 |
} |
|
2737 |
} |
|
2738 |
||
2739 |
maxBandValue = (1 << bandSize[i]) - 1; |
|
2740 |
||
2741 |
scale[i] = (UINT8*) malloc((maxBandValue + 1) * sizeof(UINT8)); |
|
2742 |
||
2743 |
if (scale[i] == NULL) { |
|
15627
46054ae31dfc
8005194: [parfait] #353 sun/awt/image/jpeg/imageioJPEG.c Memory leak of pointer 'scale' allocated with calloc()
jgodinez
parents:
9842
diff
changeset
|
2744 |
// Cleanup before throwing an out of memory exception |
46054ae31dfc
8005194: [parfait] #353 sun/awt/image/jpeg/imageioJPEG.c Memory leak of pointer 'scale' allocated with calloc()
jgodinez
parents:
9842
diff
changeset
|
2745 |
for (j = 0; j < i; j++) { |
46054ae31dfc
8005194: [parfait] #353 sun/awt/image/jpeg/imageioJPEG.c Memory leak of pointer 'scale' allocated with calloc()
jgodinez
parents:
9842
diff
changeset
|
2746 |
free(scale[j]); |
46054ae31dfc
8005194: [parfait] #353 sun/awt/image/jpeg/imageioJPEG.c Memory leak of pointer 'scale' allocated with calloc()
jgodinez
parents:
9842
diff
changeset
|
2747 |
} |
46054ae31dfc
8005194: [parfait] #353 sun/awt/image/jpeg/imageioJPEG.c Memory leak of pointer 'scale' allocated with calloc()
jgodinez
parents:
9842
diff
changeset
|
2748 |
free(scale); |
2 | 2749 |
JNU_ThrowByName( env, "java/lang/OutOfMemoryError", |
2750 |
"Writing JPEG Stream"); |
|
2751 |
return JNI_FALSE; |
|
2752 |
} |
|
2753 |
||
2754 |
halfMaxBandValue = maxBandValue >> 1; |
|
2755 |
||
2756 |
for (j = 0; j <= maxBandValue; j++) { |
|
2757 |
scale[i][j] = (UINT8) |
|
2758 |
((j*MAX_JPEG_BAND_VALUE + halfMaxBandValue)/maxBandValue); |
|
2759 |
} |
|
2760 |
} |
|
2761 |
} |
|
2762 |
||
2763 |
(*env)->ReleaseIntArrayElements(env, bandSizes, |
|
2764 |
bandSize, JNI_ABORT); |
|
2765 |
||
2766 |
cinfo = (j_compress_ptr) data->jpegObj; |
|
2767 |
dest = cinfo->dest; |
|
2768 |
||
2769 |
/* Set the buffer as our PixelBuffer */ |
|
2770 |
pb = &data->pixelBuf; |
|
2771 |
||
2772 |
if (setPixelBuffer(env, pb, buffer) == NOT_OK) { |
|
2773 |
return data->abortFlag; // We already threw an out of memory exception |
|
2774 |
} |
|
2775 |
||
2776 |
// Allocate a 1-scanline buffer |
|
6865
e7987a9205f4
6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents:
5506
diff
changeset
|
2777 |
scanLinePtr = (JSAMPROW)malloc(scanLineSize); |
2 | 2778 |
if (scanLinePtr == NULL) { |
2779 |
RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte)); |
|
2780 |
JNU_ThrowByName( env, |
|
2781 |
"java/lang/OutOfMemoryError", |
|
2782 |
"Writing JPEG Stream"); |
|
2783 |
return data->abortFlag; |
|
2784 |
} |
|
6865
e7987a9205f4
6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents:
5506
diff
changeset
|
2785 |
scanLineLimit = scanLinePtr + scanLineSize; |
2 | 2786 |
|
2787 |
/* Establish the setjmp return context for sun_jpeg_error_exit to use. */ |
|
2788 |
jerr = (sun_jpeg_error_ptr) cinfo->err; |
|
2789 |
||
2790 |
if (setjmp(jerr->setjmp_buffer)) { |
|
2791 |
/* If we get here, the JPEG code has signaled an error |
|
2792 |
while writing. */ |
|
2793 |
RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte)); |
|
2794 |
if (!(*env)->ExceptionOccurred(env)) { |
|
2795 |
char buffer[JMSG_LENGTH_MAX]; |
|
2796 |
(*cinfo->err->format_message) ((j_common_ptr) cinfo, |
|
2797 |
buffer); |
|
2798 |
JNU_ThrowByName(env, "javax/imageio/IIOException", buffer); |
|
2799 |
} |
|
8355 | 2800 |
|
2801 |
if (scale != NULL) { |
|
2802 |
for (i = 0; i < numBands; i++) { |
|
2803 |
if (scale[i] != NULL) { |
|
2804 |
free(scale[i]); |
|
2805 |
} |
|
2806 |
} |
|
2807 |
free(scale); |
|
2808 |
} |
|
2809 |
||
2 | 2810 |
free(scanLinePtr); |
2811 |
return data->abortFlag; |
|
2812 |
} |
|
2813 |
||
2814 |
// set up parameters |
|
2815 |
cinfo->image_width = destWidth; |
|
2816 |
cinfo->image_height = destHeight; |
|
2817 |
cinfo->input_components = numBands; |
|
2818 |
cinfo->in_color_space = inCs; |
|
2819 |
||
2820 |
jpeg_set_defaults(cinfo); |
|
2821 |
||
2822 |
jpeg_set_colorspace(cinfo, outCs); |
|
2823 |
||
2824 |
cinfo->optimize_coding = optimize; |
|
2825 |
||
2826 |
cinfo->write_JFIF_header = FALSE; |
|
2827 |
cinfo->write_Adobe_marker = FALSE; |
|
2828 |
// copy componentIds |
|
2829 |
ids = (*env)->GetIntArrayElements(env, componentIds, NULL); |
|
2830 |
hfactors = (*env)->GetIntArrayElements(env, HsamplingFactors, NULL); |
|
2831 |
vfactors = (*env)->GetIntArrayElements(env, VsamplingFactors, NULL); |
|
2832 |
qsels = (*env)->GetIntArrayElements(env, QtableSelectors, NULL); |
|
2833 |
||
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2834 |
if (ids && hfactors && vfactors && qsels) { |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2835 |
for (i = 0; i < numBands; i++) { |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2836 |
cinfo->comp_info[i].component_id = ids[i]; |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2837 |
cinfo->comp_info[i].h_samp_factor = hfactors[i]; |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2838 |
cinfo->comp_info[i].v_samp_factor = vfactors[i]; |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2839 |
cinfo->comp_info[i].quant_tbl_no = qsels[i]; |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2840 |
} |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2841 |
} else { |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2842 |
success = FALSE; |
2 | 2843 |
} |
2844 |
||
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2845 |
if (ids) { |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2846 |
(*env)->ReleaseIntArrayElements(env, componentIds, ids, JNI_ABORT); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2847 |
} |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2848 |
if (hfactors) { |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2849 |
(*env)->ReleaseIntArrayElements(env, HsamplingFactors, hfactors, JNI_ABORT); |
2 | 2850 |
} |
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2851 |
if (vfactors) { |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2852 |
(*env)->ReleaseIntArrayElements(env, VsamplingFactors, vfactors, JNI_ABORT); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2853 |
} |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2854 |
if (qsels) { |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2855 |
(*env)->ReleaseIntArrayElements(env, QtableSelectors, qsels, JNI_ABORT); |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2856 |
} |
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2857 |
if (!success) return data->abortFlag; |
2 | 2858 |
|
2859 |
jpeg_suppress_tables(cinfo, TRUE); // Disable writing any current |
|
2860 |
||
2861 |
qlen = setQTables(env, (j_common_ptr) cinfo, qtables, writeDQT); |
|
2862 |
||
2863 |
if (!optimize) { |
|
2864 |
// Set the h tables |
|
2865 |
hlen = setHTables(env, |
|
2866 |
(j_common_ptr) cinfo, |
|
2867 |
DCHuffmanTables, |
|
2868 |
ACHuffmanTables, |
|
2869 |
writeDHT); |
|
2870 |
} |
|
2871 |
||
2872 |
if (GET_ARRAYS(env, data, |
|
2873 |
(const JOCTET **)(&dest->next_output_byte)) == NOT_OK) { |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2874 |
(*env)->ExceptionClear(env); |
2 | 2875 |
JNU_ThrowByName(env, |
2876 |
"javax/imageio/IIOException", |
|
2877 |
"Array pin failed"); |
|
2878 |
return data->abortFlag; |
|
2879 |
} |
|
2880 |
||
2881 |
data->streamBuf.suspendable = FALSE; |
|
2882 |
||
2883 |
if (progressive) { |
|
2884 |
if (numScans == 0) { // then use default scans |
|
2885 |
jpeg_simple_progression(cinfo); |
|
2886 |
} else { |
|
2887 |
cinfo->num_scans = numScans; |
|
2888 |
// Copy the scanInfo to a local array |
|
2889 |
// The following is copied from jpeg_simple_progression: |
|
2890 |
/* Allocate space for script. |
|
2891 |
* We need to put it in the permanent pool in case the application performs |
|
2892 |
* multiple compressions without changing the settings. To avoid a memory |
|
2893 |
* leak if jpeg_simple_progression is called repeatedly for the same JPEG |
|
2894 |
* object, we try to re-use previously allocated space, and we allocate |
|
2895 |
* enough space to handle YCbCr even if initially asked for grayscale. |
|
2896 |
*/ |
|
2897 |
if (cinfo->script_space == NULL |
|
2898 |
|| cinfo->script_space_size < numScans) { |
|
2899 |
cinfo->script_space_size = MAX(numScans, 10); |
|
2900 |
cinfo->script_space = (jpeg_scan_info *) |
|
2901 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, |
|
2902 |
JPOOL_PERMANENT, |
|
2903 |
cinfo->script_space_size |
|
2904 |
* sizeof(jpeg_scan_info)); |
|
2905 |
} |
|
2906 |
cinfo->scan_info = cinfo->script_space; |
|
2907 |
scanptr = (int *) cinfo->script_space; |
|
2908 |
scanData = (*env)->GetIntArrayElements(env, scanInfo, NULL); |
|
23644
0cdb97daeef5
8030787: [Parfait] JNI-related warnings from b119 for jdk/src/share/native/sun/awt/image
pchelko
parents:
23010
diff
changeset
|
2909 |
CHECK_NULL_RETURN(scanData, data->abortFlag); |
2 | 2910 |
// number of jints per scan is 9 |
2911 |
// We avoid a memcpy to handle different size ints |
|
2912 |
for (i = 0; i < numScans*9; i++) { |
|
2913 |
scanptr[i] = scanData[i]; |
|
2914 |
} |
|
2915 |
(*env)->ReleaseIntArrayElements(env, scanInfo, |
|
2916 |
scanData, JNI_ABORT); |
|
2917 |
||
2918 |
} |
|
2919 |
} |
|
2920 |
||
2921 |
cinfo->restart_interval = restartInterval; |
|
2922 |
||
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
2923 |
#ifdef DEBUG_IIO_JPEG |
2 | 2924 |
printf("writer setup complete, starting compressor\n"); |
2925 |
#endif |
|
2926 |
||
2927 |
// start the compressor; tables must already be set |
|
2928 |
jpeg_start_compress(cinfo, FALSE); // Leaves sent_table alone |
|
2929 |
||
2930 |
if (haveMetadata) { |
|
2931 |
// Flush the buffer |
|
2932 |
imageio_flush_destination(cinfo); |
|
2933 |
// Call Java to write the metadata |
|
2934 |
RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte)); |
|
2935 |
(*env)->CallVoidMethod(env, |
|
2936 |
this, |
|
2937 |
JPEGImageWriter_writeMetadataID); |
|
2938 |
if ((*env)->ExceptionOccurred(env) |
|
2939 |
|| !GET_ARRAYS(env, data, |
|
2940 |
(const JOCTET **)(&dest->next_output_byte))) { |
|
2941 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
2942 |
} |
|
2943 |
} |
|
2944 |
||
2945 |
targetLine = 0; |
|
6865
e7987a9205f4
6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents:
5506
diff
changeset
|
2946 |
pixelBufferSize = srcWidth * numBands; |
e7987a9205f4
6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents:
5506
diff
changeset
|
2947 |
pixelStride = numBands * stepX; |
2 | 2948 |
|
2949 |
// for each line in destHeight |
|
2950 |
while ((data->abortFlag == JNI_FALSE) |
|
2951 |
&& (cinfo->next_scanline < cinfo->image_height)) { |
|
2952 |
// get the line from Java |
|
2953 |
RELEASE_ARRAYS(env, data, (const JOCTET *)(dest->next_output_byte)); |
|
2954 |
(*env)->CallVoidMethod(env, |
|
2955 |
this, |
|
2956 |
JPEGImageWriter_grabPixelsID, |
|
2957 |
targetLine); |
|
2958 |
if ((*env)->ExceptionOccurred(env) |
|
2959 |
|| !GET_ARRAYS(env, data, |
|
2960 |
(const JOCTET **)(&dest->next_output_byte))) { |
|
2961 |
cinfo->err->error_exit((j_common_ptr) cinfo); |
|
2962 |
} |
|
2963 |
||
2964 |
// subsample it into our buffer |
|
2965 |
||
2966 |
in = data->pixelBuf.buf.bp; |
|
2967 |
out = scanLinePtr; |
|
6865
e7987a9205f4
6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents:
5506
diff
changeset
|
2968 |
pixelLimit = in + ((pixelBufferSize > data->pixelBuf.byteBufferLength) ? |
e7987a9205f4
6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents:
5506
diff
changeset
|
2969 |
data->pixelBuf.byteBufferLength : pixelBufferSize); |
e7987a9205f4
6963023: ZDI-CAN-809: Sun JRE JPEGImageWriter.writeImage Remote Code Execution Vulnerability
bae
parents:
5506
diff
changeset
|
2970 |
for (; (in < pixelLimit) && (out < scanLineLimit); in += pixelStride) { |
2 | 2971 |
for (i = 0; i < numBands; i++) { |
2972 |
if (scale !=NULL && scale[i] != NULL) { |
|
2973 |
*out++ = scale[i][*(in+i)]; |
|
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
2974 |
#ifdef DEBUG_IIO_JPEG |
2 | 2975 |
if (in == data->pixelBuf.buf.bp){ // Just the first pixel |
2976 |
printf("in %d -> out %d, ", *(in+i), *(out-i-1)); |
|
2977 |
} |
|
2978 |
#endif |
|
2979 |
||
2383
c6a2226cc4de
6791502: IIOException "Invalid icc profile" on jpeg after update from JDK5 to JDK6
bae
parents:
2
diff
changeset
|
2980 |
#ifdef DEBUG_IIO_JPEG |
2 | 2981 |
if (in == data->pixelBuf.buf.bp){ // Just the first pixel |
2982 |
printf("\n"); |
|
2983 |
} |
|
2984 |
#endif |
|
2985 |
} else { |
|
2986 |
*out++ = *(in+i); |
|
2987 |
} |
|
2988 |
} |
|
2989 |
} |
|
2990 |
// write it out |
|
2991 |
jpeg_write_scanlines(cinfo, (JSAMPARRAY)&scanLinePtr, 1); |
|
2992 |
targetLine += stepY; |
|
2993 |
} |
|
2994 |
||
2995 |
/* |
|
2996 |
* We are done, but we might not have done all the lines, |
|
2997 |
* so use jpeg_abort instead of jpeg_finish_compress. |
|
2998 |
*/ |
|
2999 |
if (cinfo->next_scanline == cinfo->image_height) { |
|
3000 |
jpeg_finish_compress(cinfo); // Flushes buffer with term_dest |
|
3001 |
} else { |
|
3002 |
jpeg_abort((j_common_ptr)cinfo); |
|
3003 |
} |
|
3004 |
||
3005 |
if (scale != NULL) { |
|
3006 |
for (i = 0; i < numBands; i++) { |
|
3007 |
if (scale[i] != NULL) { |
|
3008 |
free(scale[i]); |
|
3009 |
} |
|
3010 |
} |
|
3011 |
free(scale); |
|
3012 |
} |
|
3013 |
||
3014 |
free(scanLinePtr); |
|
3015 |
RELEASE_ARRAYS(env, data, NULL); |
|
3016 |
return data->abortFlag; |
|
3017 |
} |
|
3018 |
||
3019 |
JNIEXPORT void JNICALL |
|
3020 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_abortWrite |
|
3021 |
(JNIEnv *env, |
|
3022 |
jobject this, |
|
3023 |
jlong ptr) { |
|
3024 |
||
8355 | 3025 |
imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr); |
2 | 3026 |
|
3027 |
if (data == NULL) { |
|
3028 |
JNU_ThrowByName(env, |
|
3029 |
"java/lang/IllegalStateException", |
|
3030 |
"Attempting to use writer after dispose()"); |
|
3031 |
return; |
|
3032 |
} |
|
3033 |
||
3034 |
imageio_abort(env, this, data); |
|
3035 |
} |
|
3036 |
||
3037 |
JNIEXPORT void JNICALL |
|
3038 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_resetWriter |
|
3039 |
(JNIEnv *env, |
|
3040 |
jobject this, |
|
3041 |
jlong ptr) { |
|
8355 | 3042 |
imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr); |
2 | 3043 |
j_compress_ptr cinfo; |
3044 |
||
3045 |
if (data == NULL) { |
|
3046 |
JNU_ThrowByName(env, |
|
3047 |
"java/lang/IllegalStateException", |
|
3048 |
"Attempting to use writer after dispose()"); |
|
3049 |
return; |
|
3050 |
} |
|
3051 |
||
3052 |
cinfo = (j_compress_ptr) data->jpegObj; |
|
3053 |
||
3054 |
imageio_reset(env, (j_common_ptr) cinfo, data); |
|
3055 |
||
3056 |
/* |
|
3057 |
* The tables have not been reset, and there is no way to do so |
|
3058 |
* in IJG without leaking memory. The only situation in which |
|
3059 |
* this will cause a problem is if an image-only stream is written |
|
3060 |
* with this object without initializing the correct tables first, |
|
3061 |
* which should not be possible. |
|
3062 |
*/ |
|
3063 |
||
3064 |
cinfo->dest->next_output_byte = NULL; |
|
3065 |
cinfo->dest->free_in_buffer = 0; |
|
3066 |
} |
|
3067 |
||
3068 |
JNIEXPORT void JNICALL |
|
3069 |
Java_com_sun_imageio_plugins_jpeg_JPEGImageWriter_disposeWriter |
|
3070 |
(JNIEnv *env, |
|
3071 |
jclass writer, |
|
3072 |
jlong ptr) { |
|
3073 |
||
8355 | 3074 |
imageIODataPtr data = (imageIODataPtr)jlong_to_ptr(ptr); |
2 | 3075 |
j_common_ptr info = destroyImageioData(env, data); |
3076 |
||
3077 |
imageio_dispose(info); |
|
3078 |
} |