]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | |
2 | /* pngpread.c - read a png file in push mode | |
3 | * | |
4 | * libpng 1.0.1 | |
5 | * For conditions of distribution and use, see copyright notice in png.h | |
6 | * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. | |
7 | * Copyright (c) 1996, 1997 Andreas Dilger | |
8 | * Copyright (c) 1998, Glenn Randers-Pehrson | |
9 | * March 15, 1998 | |
10 | */ | |
11 | ||
12 | #define PNG_INTERNAL | |
1f0299c1 | 13 | #include "../png/png.h" |
c801d85f KB |
14 | |
15 | #ifdef PNG_PROGRESSIVE_READ_SUPPORTED | |
16 | ||
17 | void | |
18 | png_process_data(png_structp png_ptr, png_infop info_ptr, | |
19 | png_bytep buffer, png_size_t buffer_size) | |
20 | { | |
21 | png_push_restore_buffer(png_ptr, buffer, buffer_size); | |
22 | ||
23 | while (png_ptr->buffer_size) | |
24 | { | |
25 | png_process_some_data(png_ptr, info_ptr); | |
26 | } | |
27 | } | |
28 | ||
29 | /* What we do with the incoming data depends on what we were previously | |
30 | * doing before we ran out of data... | |
31 | */ | |
32 | void | |
33 | png_process_some_data(png_structp png_ptr, png_infop info_ptr) | |
34 | { | |
35 | switch (png_ptr->process_mode) | |
36 | { | |
37 | case PNG_READ_SIG_MODE: | |
38 | { | |
39 | png_push_read_sig(png_ptr, info_ptr); | |
40 | break; | |
41 | } | |
42 | case PNG_READ_CHUNK_MODE: | |
43 | { | |
44 | png_push_read_chunk(png_ptr, info_ptr); | |
45 | break; | |
46 | } | |
47 | case PNG_READ_IDAT_MODE: | |
48 | { | |
49 | png_push_read_IDAT(png_ptr); | |
50 | break; | |
51 | } | |
52 | #if defined(PNG_READ_tEXt_SUPPORTED) | |
53 | case PNG_READ_tEXt_MODE: | |
54 | { | |
55 | png_push_read_tEXt(png_ptr, info_ptr); | |
56 | break; | |
57 | } | |
58 | #endif | |
59 | #if defined(PNG_READ_zTXt_SUPPORTED) | |
60 | case PNG_READ_zTXt_MODE: | |
61 | { | |
62 | png_push_read_zTXt(png_ptr, info_ptr); | |
63 | break; | |
64 | } | |
65 | #endif | |
66 | case PNG_SKIP_MODE: | |
67 | { | |
68 | png_push_crc_finish(png_ptr); | |
69 | break; | |
70 | } | |
71 | default: | |
72 | { | |
73 | png_ptr->buffer_size = 0; | |
74 | break; | |
75 | } | |
76 | } | |
77 | } | |
78 | ||
79 | /* Read any remaining signature bytes from the stream and compare them with | |
80 | * the correct PNG signature. It is possible that this routine is called | |
81 | * with bytes already read from the signature, whether because they have been | |
82 | * checked by the calling application, or from multiple calls to this routine. | |
83 | */ | |
84 | void | |
85 | png_push_read_sig(png_structp png_ptr, png_infop info_ptr) | |
86 | { | |
87 | png_size_t num_checked = png_ptr->sig_bytes, | |
88 | num_to_check = 8 - num_checked; | |
89 | ||
90 | if (png_ptr->buffer_size < num_to_check) | |
91 | { | |
92 | num_to_check = png_ptr->buffer_size; | |
93 | } | |
94 | ||
95 | png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]), | |
96 | num_to_check); | |
97 | png_ptr->sig_bytes += num_to_check; | |
98 | ||
99 | if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check)) | |
100 | { | |
101 | if (num_checked < 4 && | |
102 | png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4)) | |
103 | png_error(png_ptr, "Not a PNG file"); | |
104 | else | |
105 | png_error(png_ptr, "PNG file corrupted by ASCII conversion"); | |
106 | } | |
107 | else | |
108 | { | |
109 | if (png_ptr->sig_bytes >= 8) | |
110 | { | |
111 | png_ptr->process_mode = PNG_READ_CHUNK_MODE; | |
112 | } | |
113 | } | |
114 | } | |
115 | ||
116 | void | |
117 | png_push_read_chunk(png_structp png_ptr, png_infop info_ptr) | |
118 | { | |
119 | /* First we make sure we have enough data for the 4 byte chunk name | |
120 | * and the 4 byte chunk length before proceeding with decoding the | |
121 | * chunk data. To fully decode each of these chunks, we also make | |
122 | * sure we have enough data in the buffer for the 4 byte CRC at the | |
123 | * end of every chunk (except IDAT, which is handled separately). | |
124 | */ | |
125 | if (!(png_ptr->flags & PNG_FLAG_HAVE_CHUNK_HEADER)) | |
126 | { | |
127 | png_byte chunk_length[4]; | |
128 | ||
129 | if (png_ptr->buffer_size < 8) | |
130 | { | |
131 | png_push_save_buffer(png_ptr); | |
132 | return; | |
133 | } | |
134 | ||
135 | png_push_fill_buffer(png_ptr, chunk_length, 4); | |
136 | png_ptr->push_length = png_get_uint_32(chunk_length); | |
137 | png_reset_crc(png_ptr); | |
138 | png_crc_read(png_ptr, png_ptr->chunk_name, 4); | |
139 | png_ptr->flags |= PNG_FLAG_HAVE_CHUNK_HEADER; | |
140 | } | |
141 | ||
142 | if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4)) | |
143 | { | |
144 | if (png_ptr->push_length + 4 > png_ptr->buffer_size) | |
145 | { | |
146 | png_push_save_buffer(png_ptr); | |
147 | return; | |
148 | } | |
149 | ||
150 | png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length); | |
151 | } | |
152 | else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4)) | |
153 | { | |
154 | if (png_ptr->push_length + 4 > png_ptr->buffer_size) | |
155 | { | |
156 | png_push_save_buffer(png_ptr); | |
157 | return; | |
158 | } | |
159 | ||
160 | png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length); | |
161 | } | |
162 | else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) | |
163 | { | |
164 | /* If we reach an IDAT chunk, this means we have read all of the | |
165 | * header chunks, and we can start reading the image (or if this | |
166 | * is called after the image has been read - we have an error). | |
167 | */ | |
168 | if (png_ptr->mode & PNG_HAVE_IDAT) | |
169 | { | |
170 | if (png_ptr->push_length == 0) | |
171 | return; | |
172 | ||
173 | if (png_ptr->mode & PNG_AFTER_IDAT) | |
174 | png_error(png_ptr, "Too many IDAT's found"); | |
175 | } | |
176 | ||
177 | png_ptr->idat_size = png_ptr->push_length; | |
178 | png_ptr->mode |= PNG_HAVE_IDAT; | |
179 | png_ptr->process_mode = PNG_READ_IDAT_MODE; | |
180 | png_push_have_info(png_ptr, info_ptr); | |
181 | png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes; | |
182 | png_ptr->zstream.next_out = png_ptr->row_buf; | |
183 | return; | |
184 | } | |
185 | else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4)) | |
186 | { | |
187 | if (png_ptr->push_length + 4 > png_ptr->buffer_size) | |
188 | { | |
189 | png_push_save_buffer(png_ptr); | |
190 | return; | |
191 | } | |
192 | ||
193 | png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length); | |
194 | png_ptr->process_mode = PNG_READ_DONE_MODE; | |
195 | png_push_have_end(png_ptr, info_ptr); | |
196 | } | |
197 | #if defined(PNG_READ_gAMA_SUPPORTED) | |
198 | else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4)) | |
199 | { | |
200 | if (png_ptr->push_length + 4 > png_ptr->buffer_size) | |
201 | { | |
202 | png_push_save_buffer(png_ptr); | |
203 | return; | |
204 | } | |
205 | ||
206 | png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length); | |
207 | } | |
208 | #endif | |
209 | #if defined(PNG_READ_sBIT_SUPPORTED) | |
210 | else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4)) | |
211 | { | |
212 | if (png_ptr->push_length + 4 > png_ptr->buffer_size) | |
213 | { | |
214 | png_push_save_buffer(png_ptr); | |
215 | return; | |
216 | } | |
217 | ||
218 | png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length); | |
219 | } | |
220 | #endif | |
221 | #if defined(PNG_READ_cHRM_SUPPORTED) | |
222 | else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4)) | |
223 | { | |
224 | if (png_ptr->push_length + 4 > png_ptr->buffer_size) | |
225 | { | |
226 | png_push_save_buffer(png_ptr); | |
227 | return; | |
228 | } | |
229 | ||
230 | png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length); | |
231 | } | |
232 | #endif | |
233 | #if defined(PNG_READ_sRGB_SUPPORTED) | |
234 | else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4)) | |
235 | { | |
236 | if (png_ptr->push_length + 4 > png_ptr->buffer_size) | |
237 | { | |
238 | png_push_save_buffer(png_ptr); | |
239 | return; | |
240 | } | |
241 | ||
242 | png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length); | |
243 | } | |
244 | #endif | |
245 | #if defined(PNG_READ_tRNS_SUPPORTED) | |
246 | else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4)) | |
247 | { | |
248 | if (png_ptr->push_length + 4 > png_ptr->buffer_size) | |
249 | { | |
250 | png_push_save_buffer(png_ptr); | |
251 | return; | |
252 | } | |
253 | ||
254 | png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length); | |
255 | } | |
256 | #endif | |
257 | #if defined(PNG_READ_bKGD_SUPPORTED) | |
258 | else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4)) | |
259 | { | |
260 | if (png_ptr->push_length + 4 > png_ptr->buffer_size) | |
261 | { | |
262 | png_push_save_buffer(png_ptr); | |
263 | return; | |
264 | } | |
265 | ||
266 | png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length); | |
267 | } | |
268 | #endif | |
269 | #if defined(PNG_READ_hIST_SUPPORTED) | |
270 | else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4)) | |
271 | { | |
272 | if (png_ptr->push_length + 4 > png_ptr->buffer_size) | |
273 | { | |
274 | png_push_save_buffer(png_ptr); | |
275 | return; | |
276 | } | |
277 | ||
278 | png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length); | |
279 | } | |
280 | #endif | |
281 | #if defined(PNG_READ_pHYs_SUPPORTED) | |
282 | else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4)) | |
283 | { | |
284 | if (png_ptr->push_length + 4 > png_ptr->buffer_size) | |
285 | { | |
286 | png_push_save_buffer(png_ptr); | |
287 | return; | |
288 | } | |
289 | ||
290 | png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length); | |
291 | } | |
292 | #endif | |
293 | #if defined(PNG_READ_oFFs_SUPPORTED) | |
294 | else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4)) | |
295 | { | |
296 | if (png_ptr->push_length + 4 > png_ptr->buffer_size) | |
297 | { | |
298 | png_push_save_buffer(png_ptr); | |
299 | return; | |
300 | } | |
301 | ||
302 | png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length); | |
303 | } | |
304 | #endif | |
305 | #if defined(PNG_READ_pCAL_SUPPORTED) | |
306 | else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4)) | |
307 | { | |
308 | if (png_ptr->push_length + 4 > png_ptr->buffer_size) | |
309 | { | |
310 | png_push_save_buffer(png_ptr); | |
311 | return; | |
312 | } | |
313 | ||
314 | png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length); | |
315 | } | |
316 | #endif | |
317 | #if defined(PNG_READ_tIME_SUPPORTED) | |
318 | else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4)) | |
319 | { | |
320 | if (png_ptr->push_length + 4 > png_ptr->buffer_size) | |
321 | { | |
322 | png_push_save_buffer(png_ptr); | |
323 | return; | |
324 | } | |
325 | ||
326 | png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length); | |
327 | } | |
328 | #endif | |
329 | #if defined(PNG_READ_tEXt_SUPPORTED) | |
330 | else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4)) | |
331 | { | |
332 | png_push_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length); | |
333 | } | |
334 | #endif | |
335 | #if defined(PNG_READ_zTXt_SUPPORTED) | |
336 | else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4)) | |
337 | { | |
338 | png_push_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length); | |
339 | } | |
340 | #endif | |
341 | else | |
342 | { | |
343 | png_push_handle_unknown(png_ptr, info_ptr, png_ptr->push_length); | |
344 | } | |
345 | ||
346 | png_ptr->flags &= ~PNG_FLAG_HAVE_CHUNK_HEADER; | |
347 | } | |
348 | ||
349 | void | |
350 | png_push_crc_skip(png_structp png_ptr, png_uint_32 skip) | |
351 | { | |
352 | png_ptr->process_mode = PNG_SKIP_MODE; | |
353 | png_ptr->skip_length = skip; | |
354 | } | |
355 | ||
356 | void | |
357 | png_push_crc_finish(png_structp png_ptr) | |
358 | { | |
359 | if (png_ptr->skip_length && png_ptr->save_buffer_size) | |
360 | { | |
361 | png_size_t save_size; | |
362 | ||
363 | if (png_ptr->skip_length < (png_uint_32)png_ptr->save_buffer_size) | |
364 | save_size = (png_size_t)png_ptr->skip_length; | |
365 | else | |
366 | save_size = png_ptr->save_buffer_size; | |
367 | ||
368 | png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size); | |
369 | ||
370 | png_ptr->skip_length -= save_size; | |
371 | png_ptr->buffer_size -= save_size; | |
372 | png_ptr->save_buffer_size -= save_size; | |
373 | png_ptr->save_buffer_ptr += save_size; | |
374 | } | |
375 | if (png_ptr->skip_length && png_ptr->current_buffer_size) | |
376 | { | |
377 | png_size_t save_size; | |
378 | ||
379 | if (png_ptr->skip_length < (png_uint_32)png_ptr->current_buffer_size) | |
380 | save_size = (png_size_t)png_ptr->skip_length; | |
381 | else | |
382 | save_size = png_ptr->current_buffer_size; | |
383 | ||
384 | png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size); | |
385 | ||
386 | png_ptr->skip_length -= save_size; | |
387 | png_ptr->buffer_size -= save_size; | |
388 | png_ptr->current_buffer_size -= save_size; | |
389 | png_ptr->current_buffer_ptr += save_size; | |
390 | } | |
391 | if (!png_ptr->skip_length) | |
392 | { | |
393 | if (png_ptr->buffer_size < 4) | |
394 | { | |
395 | png_push_save_buffer(png_ptr); | |
396 | return; | |
397 | } | |
398 | ||
399 | png_crc_finish(png_ptr, 0); | |
400 | png_ptr->process_mode = PNG_READ_CHUNK_MODE; | |
401 | } | |
402 | } | |
403 | ||
404 | void | |
405 | png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length) | |
406 | { | |
407 | png_bytep ptr; | |
408 | ||
409 | ptr = buffer; | |
410 | if (png_ptr->save_buffer_size) | |
411 | { | |
412 | png_size_t save_size; | |
413 | ||
414 | if (length < png_ptr->save_buffer_size) | |
415 | save_size = length; | |
416 | else | |
417 | save_size = png_ptr->save_buffer_size; | |
418 | ||
419 | png_memcpy(ptr, png_ptr->save_buffer_ptr, save_size); | |
420 | length -= save_size; | |
421 | ptr += save_size; | |
422 | png_ptr->buffer_size -= save_size; | |
423 | png_ptr->save_buffer_size -= save_size; | |
424 | png_ptr->save_buffer_ptr += save_size; | |
425 | } | |
426 | if (length && png_ptr->current_buffer_size) | |
427 | { | |
428 | png_size_t save_size; | |
429 | ||
430 | if (length < png_ptr->current_buffer_size) | |
431 | save_size = length; | |
432 | else | |
433 | save_size = png_ptr->current_buffer_size; | |
434 | ||
435 | png_memcpy(ptr, png_ptr->current_buffer_ptr, save_size); | |
436 | png_ptr->buffer_size -= save_size; | |
437 | png_ptr->current_buffer_size -= save_size; | |
438 | png_ptr->current_buffer_ptr += save_size; | |
439 | } | |
440 | } | |
441 | ||
442 | void | |
443 | png_push_save_buffer(png_structp png_ptr) | |
444 | { | |
445 | if (png_ptr->save_buffer_size) | |
446 | { | |
447 | if (png_ptr->save_buffer_ptr != png_ptr->save_buffer) | |
448 | { | |
449 | png_size_t i; | |
450 | png_bytep sp; | |
451 | png_bytep dp; | |
452 | ||
453 | for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer; | |
454 | i < png_ptr->save_buffer_size; | |
455 | i++, sp++, dp++) | |
456 | { | |
457 | *dp = *sp; | |
458 | } | |
459 | } | |
460 | } | |
461 | if (png_ptr->save_buffer_size + png_ptr->current_buffer_size > | |
462 | png_ptr->save_buffer_max) | |
463 | { | |
464 | png_size_t new_max; | |
465 | png_bytep old_buffer; | |
466 | ||
467 | new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256; | |
468 | old_buffer = png_ptr->save_buffer; | |
469 | png_ptr->save_buffer = (png_bytep)png_malloc(png_ptr, | |
470 | (png_uint_32)new_max); | |
471 | png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size); | |
472 | png_free(png_ptr, old_buffer); | |
473 | png_ptr->save_buffer_max = new_max; | |
474 | } | |
475 | if (png_ptr->current_buffer_size) | |
476 | { | |
477 | png_memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size, | |
478 | png_ptr->current_buffer_ptr, png_ptr->current_buffer_size); | |
479 | png_ptr->save_buffer_size += png_ptr->current_buffer_size; | |
480 | png_ptr->current_buffer_size = 0; | |
481 | } | |
482 | png_ptr->save_buffer_ptr = png_ptr->save_buffer; | |
483 | png_ptr->buffer_size = 0; | |
484 | } | |
485 | ||
486 | void | |
487 | png_push_restore_buffer(png_structp png_ptr, png_bytep buffer, | |
488 | png_size_t buffer_length) | |
489 | { | |
490 | png_ptr->current_buffer = buffer; | |
491 | png_ptr->current_buffer_size = buffer_length; | |
492 | png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size; | |
493 | png_ptr->current_buffer_ptr = png_ptr->current_buffer; | |
494 | } | |
495 | ||
496 | void | |
497 | png_push_read_IDAT(png_structp png_ptr) | |
498 | { | |
499 | if (!(png_ptr->flags & PNG_FLAG_HAVE_CHUNK_HEADER)) | |
500 | { | |
501 | png_byte chunk_length[4]; | |
502 | ||
503 | if (png_ptr->buffer_size < 8) | |
504 | { | |
505 | png_push_save_buffer(png_ptr); | |
506 | return; | |
507 | } | |
508 | ||
509 | png_push_fill_buffer(png_ptr, chunk_length, 4); | |
510 | png_ptr->push_length = png_get_uint_32(chunk_length); | |
511 | ||
512 | png_reset_crc(png_ptr); | |
513 | png_crc_read(png_ptr, png_ptr->chunk_name, 4); | |
514 | png_ptr->flags |= PNG_FLAG_HAVE_CHUNK_HEADER; | |
515 | ||
516 | if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) | |
517 | { | |
518 | png_ptr->process_mode = PNG_READ_CHUNK_MODE; | |
519 | if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED)) | |
520 | png_error(png_ptr, "Not enough compressed data"); | |
521 | return; | |
522 | } | |
523 | ||
524 | png_ptr->idat_size = png_ptr->push_length; | |
525 | } | |
526 | if (png_ptr->idat_size && png_ptr->save_buffer_size) | |
527 | { | |
528 | png_size_t save_size; | |
529 | ||
530 | if (png_ptr->idat_size < (png_uint_32)png_ptr->save_buffer_size) | |
531 | { | |
532 | save_size = (png_size_t)png_ptr->idat_size; | |
533 | /* check for overflow */ | |
534 | if((png_uint_32)save_size != png_ptr->idat_size) | |
535 | png_error(png_ptr, "save_size overflowed in pngpread"); | |
536 | } | |
537 | else | |
538 | save_size = png_ptr->save_buffer_size; | |
539 | ||
540 | png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size); | |
541 | png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size); | |
542 | ||
543 | png_ptr->idat_size -= save_size; | |
544 | png_ptr->buffer_size -= save_size; | |
545 | png_ptr->save_buffer_size -= save_size; | |
546 | png_ptr->save_buffer_ptr += save_size; | |
547 | } | |
548 | if (png_ptr->idat_size && png_ptr->current_buffer_size) | |
549 | { | |
550 | png_size_t save_size; | |
551 | ||
552 | if (png_ptr->idat_size < (png_uint_32)png_ptr->current_buffer_size) | |
553 | { | |
554 | save_size = (png_size_t)png_ptr->idat_size; | |
555 | /* check for overflow */ | |
556 | if((png_uint_32)save_size != png_ptr->idat_size) | |
557 | png_error(png_ptr, "save_size overflowed in pngpread"); | |
558 | } | |
559 | else | |
560 | save_size = png_ptr->current_buffer_size; | |
561 | ||
562 | png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size); | |
563 | png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size); | |
564 | ||
565 | png_ptr->idat_size -= save_size; | |
566 | png_ptr->buffer_size -= save_size; | |
567 | png_ptr->current_buffer_size -= save_size; | |
568 | png_ptr->current_buffer_ptr += save_size; | |
569 | } | |
570 | if (!png_ptr->idat_size) | |
571 | { | |
572 | if (png_ptr->buffer_size < 4) | |
573 | { | |
574 | png_push_save_buffer(png_ptr); | |
575 | return; | |
576 | } | |
577 | ||
578 | png_crc_finish(png_ptr, 0); | |
579 | png_ptr->flags &= ~PNG_FLAG_HAVE_CHUNK_HEADER; | |
580 | } | |
581 | } | |
582 | ||
583 | void | |
584 | png_process_IDAT_data(png_structp png_ptr, png_bytep buffer, | |
585 | png_size_t buffer_length) | |
586 | { | |
587 | int ret; | |
588 | ||
589 | if ((png_ptr->flags & PNG_FLAG_ZLIB_FINISHED) && buffer_length) | |
590 | png_error(png_ptr, "Extra compression data"); | |
591 | ||
592 | png_ptr->zstream.next_in = buffer; | |
593 | png_ptr->zstream.avail_in = (uInt)buffer_length; | |
594 | for(;;) | |
595 | { | |
596 | ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); | |
597 | if (ret == Z_STREAM_END) | |
598 | { | |
599 | if (png_ptr->zstream.avail_in) | |
600 | png_error(png_ptr, "Extra compressed data"); | |
601 | if (!(png_ptr->zstream.avail_out)) | |
602 | { | |
603 | png_push_process_row(png_ptr); | |
604 | } | |
605 | ||
606 | png_ptr->mode |= PNG_AFTER_IDAT; | |
607 | png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED; | |
608 | break; | |
609 | } | |
610 | else if (ret == Z_BUF_ERROR) | |
611 | break; | |
612 | else if (ret != Z_OK) | |
613 | png_error(png_ptr, "Decompression Error"); | |
614 | if (!(png_ptr->zstream.avail_out)) | |
615 | { | |
616 | png_push_process_row(png_ptr); | |
617 | png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes; | |
618 | png_ptr->zstream.next_out = png_ptr->row_buf; | |
619 | } | |
620 | else | |
621 | break; | |
622 | } | |
623 | } | |
624 | ||
625 | void | |
626 | png_push_process_row(png_structp png_ptr) | |
627 | { | |
628 | png_ptr->row_info.color_type = png_ptr->color_type; | |
629 | png_ptr->row_info.width = png_ptr->iwidth; | |
630 | png_ptr->row_info.channels = png_ptr->channels; | |
631 | png_ptr->row_info.bit_depth = png_ptr->bit_depth; | |
632 | png_ptr->row_info.pixel_depth = png_ptr->pixel_depth; | |
633 | ||
634 | png_ptr->row_info.rowbytes = ((png_ptr->row_info.width * | |
635 | (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3); | |
636 | ||
637 | png_read_filter_row(png_ptr, &(png_ptr->row_info), | |
638 | png_ptr->row_buf + 1, png_ptr->prev_row + 1, | |
639 | (int)(png_ptr->row_buf[0])); | |
640 | ||
641 | png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf, | |
642 | png_ptr->rowbytes + 1); | |
643 | ||
644 | if (png_ptr->transformations) | |
645 | png_do_read_transformations(png_ptr); | |
646 | ||
647 | #if defined(PNG_READ_INTERLACING_SUPPORTED) | |
648 | /* blow up interlaced rows to full size */ | |
649 | if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE)) | |
650 | { | |
651 | if (png_ptr->pass < 6) | |
652 | png_do_read_interlace(&(png_ptr->row_info), | |
653 | png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations); | |
654 | ||
655 | switch (png_ptr->pass) | |
656 | { | |
657 | case 0: | |
658 | { | |
659 | int i; | |
660 | for (i = 0; i < 8 && png_ptr->pass == 0; i++) | |
661 | { | |
662 | png_push_have_row(png_ptr, png_ptr->row_buf + 1); | |
663 | png_read_push_finish_row(png_ptr); | |
664 | } | |
665 | break; | |
666 | } | |
667 | case 1: | |
668 | { | |
669 | int i; | |
670 | for (i = 0; i < 8 && png_ptr->pass == 1; i++) | |
671 | { | |
672 | png_push_have_row(png_ptr, png_ptr->row_buf + 1); | |
673 | png_read_push_finish_row(png_ptr); | |
674 | } | |
675 | if (png_ptr->pass == 2) | |
676 | { | |
677 | for (i = 0; i < 4 && png_ptr->pass == 2; i++) | |
678 | { | |
679 | png_push_have_row(png_ptr, NULL); | |
680 | png_read_push_finish_row(png_ptr); | |
681 | } | |
682 | } | |
683 | break; | |
684 | } | |
685 | case 2: | |
686 | { | |
687 | int i; | |
688 | for (i = 0; i < 4 && png_ptr->pass == 2; i++) | |
689 | { | |
690 | png_push_have_row(png_ptr, png_ptr->row_buf + 1); | |
691 | png_read_push_finish_row(png_ptr); | |
692 | } | |
693 | for (i = 0; i < 4 && png_ptr->pass == 2; i++) | |
694 | { | |
695 | png_push_have_row(png_ptr, NULL); | |
696 | png_read_push_finish_row(png_ptr); | |
697 | } | |
698 | break; | |
699 | } | |
700 | case 3: | |
701 | { | |
702 | int i; | |
703 | for (i = 0; i < 4 && png_ptr->pass == 3; i++) | |
704 | { | |
705 | png_push_have_row(png_ptr, png_ptr->row_buf + 1); | |
706 | png_read_push_finish_row(png_ptr); | |
707 | } | |
708 | if (png_ptr->pass == 4) | |
709 | { | |
710 | for (i = 0; i < 2 && png_ptr->pass == 4; i++) | |
711 | { | |
712 | png_push_have_row(png_ptr, NULL); | |
713 | png_read_push_finish_row(png_ptr); | |
714 | } | |
715 | } | |
716 | break; | |
717 | } | |
718 | case 4: | |
719 | { | |
720 | int i; | |
721 | for (i = 0; i < 2 && png_ptr->pass == 4; i++) | |
722 | { | |
723 | png_push_have_row(png_ptr, png_ptr->row_buf + 1); | |
724 | png_read_push_finish_row(png_ptr); | |
725 | } | |
726 | for (i = 0; i < 2 && png_ptr->pass == 4; i++) | |
727 | { | |
728 | png_push_have_row(png_ptr, NULL); | |
729 | png_read_push_finish_row(png_ptr); | |
730 | } | |
731 | break; | |
732 | } | |
733 | case 5: | |
734 | { | |
735 | int i; | |
736 | for (i = 0; i < 2 && png_ptr->pass == 5; i++) | |
737 | { | |
738 | png_push_have_row(png_ptr, png_ptr->row_buf + 1); | |
739 | png_read_push_finish_row(png_ptr); | |
740 | } | |
741 | if (png_ptr->pass == 6) | |
742 | { | |
743 | png_push_have_row(png_ptr, NULL); | |
744 | png_read_push_finish_row(png_ptr); | |
745 | } | |
746 | break; | |
747 | } | |
748 | case 6: | |
749 | { | |
750 | png_push_have_row(png_ptr, png_ptr->row_buf + 1); | |
751 | png_read_push_finish_row(png_ptr); | |
752 | if (png_ptr->pass != 6) | |
753 | break; | |
754 | png_push_have_row(png_ptr, NULL); | |
755 | png_read_push_finish_row(png_ptr); | |
756 | } | |
757 | } | |
758 | } | |
759 | else | |
760 | #endif | |
761 | { | |
762 | png_push_have_row(png_ptr, png_ptr->row_buf + 1); | |
763 | png_read_push_finish_row(png_ptr); | |
764 | } | |
765 | } | |
766 | ||
767 | void | |
768 | png_read_push_finish_row(png_structp png_ptr) | |
769 | { | |
770 | png_ptr->row_number++; | |
771 | if (png_ptr->row_number < png_ptr->num_rows) | |
772 | return; | |
773 | ||
774 | if (png_ptr->interlaced) | |
775 | { | |
776 | png_ptr->row_number = 0; | |
777 | png_memset_check(png_ptr, png_ptr->prev_row, 0, | |
778 | png_ptr->rowbytes + 1); | |
779 | do | |
780 | { | |
781 | png_ptr->pass++; | |
782 | if (png_ptr->pass >= 7) | |
783 | break; | |
784 | png_ptr->iwidth = (png_ptr->width + | |
785 | png_pass_inc[png_ptr->pass] - 1 - | |
786 | png_pass_start[png_ptr->pass]) / | |
787 | png_pass_inc[png_ptr->pass]; | |
788 | ||
789 | png_ptr->irowbytes = ((png_ptr->iwidth * | |
790 | png_ptr->pixel_depth + 7) >> 3) + 1; | |
791 | ||
792 | if (!(png_ptr->transformations & PNG_INTERLACE)) | |
793 | { | |
794 | png_ptr->num_rows = (png_ptr->height + | |
795 | png_pass_yinc[png_ptr->pass] - 1 - | |
796 | png_pass_ystart[png_ptr->pass]) / | |
797 | png_pass_yinc[png_ptr->pass]; | |
798 | if (!(png_ptr->num_rows)) | |
799 | continue; | |
800 | } | |
801 | if (png_ptr->transformations & PNG_INTERLACE) | |
802 | break; | |
803 | } while (png_ptr->iwidth == 0); | |
804 | } | |
805 | } | |
806 | ||
807 | #if defined(PNG_READ_tEXt_SUPPORTED) | |
808 | void | |
809 | png_push_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) | |
810 | { | |
811 | if (png_ptr->mode == PNG_BEFORE_IHDR || png_ptr->mode & PNG_HAVE_IEND) | |
812 | { | |
813 | png_error(png_ptr, "Out of place tEXt"); | |
814 | /* to quiet some compiler warnings */ | |
815 | if(info_ptr == NULL) return; | |
816 | } | |
817 | ||
818 | #ifdef PNG_MAX_MALLOC_64K | |
819 | png_ptr->skip_length = 0; /* This may not be necessary */ | |
820 | ||
821 | if (length > (png_uint_32)65535L) /* Can't hold the entire string in memory */ | |
822 | { | |
823 | png_warning(png_ptr, "tEXt chunk too large to fit in memory"); | |
824 | png_ptr->skip_length = length - (png_uint_32)65535L; | |
825 | length = (png_uint_32)65535L; | |
826 | } | |
827 | #endif | |
828 | ||
829 | png_ptr->current_text = (png_charp)png_malloc(png_ptr, | |
830 | (png_uint_32)(length+1)); | |
831 | png_ptr->current_text[length] = '\0'; | |
832 | png_ptr->current_text_ptr = png_ptr->current_text; | |
833 | png_ptr->current_text_size = (png_size_t)length; | |
834 | png_ptr->current_text_left = (png_size_t)length; | |
835 | png_ptr->process_mode = PNG_READ_tEXt_MODE; | |
836 | } | |
837 | ||
838 | void | |
839 | png_push_read_tEXt(png_structp png_ptr, png_infop info_ptr) | |
840 | { | |
841 | if (png_ptr->buffer_size && png_ptr->current_text_left) | |
842 | { | |
843 | png_size_t text_size; | |
844 | ||
845 | if (png_ptr->buffer_size < png_ptr->current_text_left) | |
846 | text_size = png_ptr->buffer_size; | |
847 | else | |
848 | text_size = png_ptr->current_text_left; | |
849 | png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size); | |
850 | png_ptr->current_text_left -= text_size; | |
851 | png_ptr->current_text_ptr += text_size; | |
852 | } | |
853 | if (!(png_ptr->current_text_left)) | |
854 | { | |
855 | png_textp text_ptr; | |
856 | png_charp text; | |
857 | png_charp key; | |
858 | ||
859 | if (png_ptr->buffer_size < 4) | |
860 | { | |
861 | png_push_save_buffer(png_ptr); | |
862 | return; | |
863 | } | |
864 | ||
865 | png_push_crc_finish(png_ptr); | |
866 | ||
867 | #if defined(PNG_MAX_MALLOC_64K) | |
868 | if (png_ptr->skip_length) | |
869 | return; | |
870 | #endif | |
871 | ||
872 | key = png_ptr->current_text; | |
873 | png_ptr->current_text = 0; | |
874 | ||
875 | for (text = key; *text; text++) | |
876 | /* empty loop */ ; | |
877 | ||
878 | if (text != key + png_ptr->current_text_size) | |
879 | text++; | |
880 | ||
881 | text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text)); | |
882 | text_ptr->compression = PNG_TEXT_COMPRESSION_NONE; | |
883 | text_ptr->key = key; | |
884 | text_ptr->text = text; | |
885 | ||
886 | png_set_text(png_ptr, info_ptr, text_ptr, 1); | |
887 | ||
888 | png_free(png_ptr, text_ptr); | |
889 | } | |
890 | } | |
891 | #endif | |
892 | ||
893 | #if defined(PNG_READ_zTXt_SUPPORTED) | |
894 | void | |
895 | png_push_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) | |
896 | { | |
897 | if (png_ptr->mode == PNG_BEFORE_IHDR || png_ptr->mode & PNG_HAVE_IEND) | |
898 | { | |
899 | png_error(png_ptr, "Out of place zTXt"); | |
900 | /* to quiet some compiler warnings */ | |
901 | if(info_ptr == NULL) return; | |
902 | } | |
903 | ||
904 | #ifdef PNG_MAX_MALLOC_64K | |
905 | /* We can't handle zTXt chunks > 64K, since we don't have enough space | |
906 | * to be able to store the uncompressed data. Actually, the threshold | |
907 | * is probably around 32K, but it isn't as definite as 64K is. | |
908 | */ | |
909 | if (length > (png_uint_32)65535L) | |
910 | { | |
911 | png_warning(png_ptr, "zTXt chunk too large to fit in memory"); | |
912 | png_push_crc_skip(png_ptr, length); | |
913 | return; | |
914 | } | |
915 | #endif | |
916 | ||
917 | png_ptr->current_text = (png_charp)png_malloc(png_ptr, | |
918 | (png_uint_32)(length+1)); | |
919 | png_ptr->current_text[length] = '\0'; | |
920 | png_ptr->current_text_ptr = png_ptr->current_text; | |
921 | png_ptr->current_text_size = (png_size_t)length; | |
922 | png_ptr->current_text_left = (png_size_t)length; | |
923 | png_ptr->process_mode = PNG_READ_zTXt_MODE; | |
924 | } | |
925 | ||
926 | void | |
927 | png_push_read_zTXt(png_structp png_ptr, png_infop info_ptr) | |
928 | { | |
929 | if (png_ptr->buffer_size && png_ptr->current_text_left) | |
930 | { | |
931 | png_size_t text_size; | |
932 | ||
933 | if (png_ptr->buffer_size < (png_uint_32)png_ptr->current_text_left) | |
934 | text_size = png_ptr->buffer_size; | |
935 | else | |
936 | text_size = png_ptr->current_text_left; | |
937 | png_crc_read(png_ptr, (png_bytep)png_ptr->current_text_ptr, text_size); | |
938 | png_ptr->current_text_left -= text_size; | |
939 | png_ptr->current_text_ptr += text_size; | |
940 | } | |
941 | if (!(png_ptr->current_text_left)) | |
942 | { | |
943 | png_textp text_ptr; | |
944 | png_charp text; | |
945 | png_charp key; | |
946 | int ret; | |
947 | png_size_t text_size, key_size; | |
948 | ||
949 | if (png_ptr->buffer_size < 4) | |
950 | { | |
951 | png_push_save_buffer(png_ptr); | |
952 | return; | |
953 | } | |
954 | ||
955 | png_push_crc_finish(png_ptr); | |
956 | ||
957 | key = png_ptr->current_text; | |
958 | png_ptr->current_text = 0; | |
959 | ||
960 | for (text = key; *text; text++) | |
961 | /* empty loop */ ; | |
962 | ||
963 | /* zTXt can't have zero text */ | |
964 | if (text == key + png_ptr->current_text_size) | |
965 | { | |
966 | png_free(png_ptr, key); | |
967 | return; | |
968 | } | |
969 | ||
970 | text++; | |
971 | ||
972 | if (*text != PNG_TEXT_COMPRESSION_zTXt) /* check compression byte */ | |
973 | { | |
974 | png_free(png_ptr, key); | |
975 | return; | |
976 | } | |
977 | ||
978 | text++; | |
979 | ||
980 | png_ptr->zstream.next_in = (png_bytep )text; | |
981 | png_ptr->zstream.avail_in = (uInt)(png_ptr->current_text_size - | |
982 | (text - key)); | |
983 | png_ptr->zstream.next_out = png_ptr->zbuf; | |
984 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; | |
985 | ||
986 | key_size = text - key; | |
987 | text_size = 0; | |
988 | text = NULL; | |
989 | ret = Z_STREAM_END; | |
990 | ||
991 | while (png_ptr->zstream.avail_in) | |
992 | { | |
993 | ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH); | |
994 | if (ret != Z_OK && ret != Z_STREAM_END) | |
995 | { | |
996 | inflateReset(&png_ptr->zstream); | |
997 | png_ptr->zstream.avail_in = 0; | |
998 | png_free(png_ptr, key); | |
999 | png_free(png_ptr, text); | |
1000 | return; | |
1001 | } | |
1002 | if (!(png_ptr->zstream.avail_out) || ret == Z_STREAM_END) | |
1003 | { | |
1004 | if (text == NULL) | |
1005 | { | |
1006 | text = (png_charp)png_malloc(png_ptr, | |
1007 | (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out + | |
1008 | key_size + 1)); | |
1009 | png_memcpy(text + key_size, png_ptr->zbuf, | |
1010 | png_ptr->zbuf_size - png_ptr->zstream.avail_out); | |
1011 | png_memcpy(text, key, key_size); | |
1012 | text_size = key_size + png_ptr->zbuf_size - | |
1013 | png_ptr->zstream.avail_out; | |
1014 | *(text + text_size) = '\0'; | |
1015 | } | |
1016 | else | |
1017 | { | |
1018 | png_charp tmp; | |
1019 | ||
1020 | tmp = text; | |
1021 | text = (png_charp)png_malloc(png_ptr, text_size + | |
1022 | (png_uint_32)(png_ptr->zbuf_size - png_ptr->zstream.avail_out | |
1023 | + 1)); | |
1024 | png_memcpy(text, tmp, text_size); | |
1025 | png_free(png_ptr, tmp); | |
1026 | png_memcpy(text + text_size, png_ptr->zbuf, | |
1027 | png_ptr->zbuf_size - png_ptr->zstream.avail_out); | |
1028 | text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out; | |
1029 | *(text + text_size) = '\0'; | |
1030 | } | |
1031 | if (ret != Z_STREAM_END) | |
1032 | { | |
1033 | png_ptr->zstream.next_out = png_ptr->zbuf; | |
1034 | png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; | |
1035 | } | |
1036 | } | |
1037 | else | |
1038 | { | |
1039 | break; | |
1040 | } | |
1041 | ||
1042 | if (ret == Z_STREAM_END) | |
1043 | break; | |
1044 | } | |
1045 | ||
1046 | inflateReset(&png_ptr->zstream); | |
1047 | png_ptr->zstream.avail_in = 0; | |
1048 | ||
1049 | if (ret != Z_STREAM_END) | |
1050 | { | |
1051 | png_free(png_ptr, key); | |
1052 | png_free(png_ptr, text); | |
1053 | return; | |
1054 | } | |
1055 | ||
1056 | png_free(png_ptr, key); | |
1057 | key = text; | |
1058 | text += key_size; | |
1059 | ||
1060 | text_ptr = (png_textp)png_malloc(png_ptr, (png_uint_32)sizeof(png_text)); | |
1061 | text_ptr->compression = PNG_TEXT_COMPRESSION_zTXt; | |
1062 | text_ptr->key = key; | |
1063 | text_ptr->text = text; | |
1064 | ||
1065 | png_set_text(png_ptr, info_ptr, text_ptr, 1); | |
1066 | ||
1067 | png_free(png_ptr, text_ptr); | |
1068 | } | |
1069 | } | |
1070 | #endif | |
1071 | ||
1072 | /* This function is called when we haven't found a handler for this | |
1073 | * chunk. In the future we will have code here which can handle | |
1074 | * user-defined callback functions for unknown chunks before they are | |
1075 | * ignored or cause an error. If there isn't a problem with the | |
1076 | * chunk itself (ie a bad chunk name or a critical chunk), the chunk | |
1077 | * is (currently) silently ignored. | |
1078 | */ | |
1079 | void | |
1080 | png_push_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length) | |
1081 | { | |
1082 | png_check_chunk_name(png_ptr, png_ptr->chunk_name); | |
1083 | ||
1084 | if (!(png_ptr->chunk_name[0] & 0x20)) | |
1085 | { | |
1086 | png_chunk_error(png_ptr, "unknown critical chunk"); | |
1087 | /* to quiet some compiler warnings */ | |
1088 | if(info_ptr == NULL) return; | |
1089 | } | |
1090 | ||
1091 | png_push_crc_skip(png_ptr, length); | |
1092 | } | |
1093 | ||
1094 | void | |
1095 | png_push_have_info(png_structp png_ptr, png_infop info_ptr) | |
1096 | { | |
1097 | if (png_ptr->info_fn != NULL) | |
1098 | (*(png_ptr->info_fn))(png_ptr, info_ptr); | |
1099 | } | |
1100 | ||
1101 | void | |
1102 | png_push_have_end(png_structp png_ptr, png_infop info_ptr) | |
1103 | { | |
1104 | if (png_ptr->end_fn != NULL) | |
1105 | (*(png_ptr->end_fn))(png_ptr, info_ptr); | |
1106 | } | |
1107 | ||
1108 | void | |
1109 | png_push_have_row(png_structp png_ptr, png_bytep row) | |
1110 | { | |
1111 | if (png_ptr->row_fn != NULL) | |
1112 | (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number, | |
1113 | (int)png_ptr->pass); | |
1114 | } | |
1115 | ||
1116 | void | |
1117 | png_progressive_combine_row (png_structp png_ptr, | |
1118 | png_bytep old_row, png_bytep new_row) | |
1119 | { | |
1120 | if (new_row != NULL) | |
1121 | png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]); | |
1122 | } | |
1123 | ||
1124 | void | |
1125 | png_set_progressive_read_fn(png_structp png_ptr, png_voidp progressive_ptr, | |
1126 | png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn, | |
1127 | png_progressive_end_ptr end_fn) | |
1128 | { | |
1129 | png_ptr->info_fn = info_fn; | |
1130 | png_ptr->row_fn = row_fn; | |
1131 | png_ptr->end_fn = end_fn; | |
1132 | ||
1133 | png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer); | |
1134 | } | |
1135 | ||
1136 | png_voidp | |
1137 | png_get_progressive_ptr(png_structp png_ptr) | |
1138 | { | |
1139 | return png_ptr->io_ptr; | |
1140 | } | |
1141 | ||
1142 | #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */ | |
1143 |