Lines Matching refs:file
833 struct file struct
861 FILE * file; /* Original PNG file */ member
902 void (*alloc)(struct file*,int idat); argument
949 emit_error(struct file *file, int code, const char *what) in emit_error() argument
964 err = file->read_errno; in emit_error()
967 err = file->write_errno; in emit_error()
970 err = file->read_errno; in emit_error()
972 err = file->write_errno; in emit_error()
978 fprintf(stderr, "%s: %s %s [%s]\n", file->file_name, reason, what, in emit_error()
982 fprintf(stderr, "%s: %s %s\n", file->file_name, reason, what); in emit_error()
989 file_end(struct file *file) in file_end() argument
996 if (file->idat != NULL) in file_end()
997 IDAT_end(&file->idat); in file_end()
999 if (file->chunk != NULL) in file_end()
1000 chunk_end(&file->chunk); in file_end()
1002 rc = file->status_code; in file_end()
1004 if (file->file != NULL) in file_end()
1005 (void)fclose(file->file); in file_end()
1007 if (file->out != NULL) in file_end()
1012 if (ferror(file->out) | fflush(file->out) | fclose(file->out)) in file_end()
1014 perror(file->out_name); in file_end()
1015 emit_error(file, READ_ERROR_CODE, "output write error"); in file_end()
1021 file->global->status_code |= rc; in file_end()
1023 CLEAR(*file); in file_end()
1029 file_init(struct file *file, struct global *global, const char *file_name, in file_init() argument
1030 const char *out_name, void *alloc_ptr, void (*alloc)(struct file*,int)) in file_init() argument
1036 CLEAR(*file); in file_init()
1037 file->global = global; in file_init()
1039 file->file_name = file_name; in file_init()
1040 file->out_name = out_name; in file_init()
1041 file->status_code = 0; in file_init()
1042 file->read_errno = 0; in file_init()
1043 file->write_errno = 0; in file_init()
1045 file->file = NULL; in file_init()
1046 file->out = NULL; in file_init()
1049 file->read_count = 0; in file_init()
1050 file->state = STATE_SIGNATURE; in file_init()
1052 file->chunk = NULL; in file_init()
1053 file->idat = NULL; in file_init()
1055 file->alloc_ptr = alloc_ptr; in file_init()
1056 file->alloc = alloc; in file_init()
1060 file->file = fopen(file_name, "rb"); in file_init()
1062 if (file->file == NULL) in file_init()
1064 file->read_errno = errno; in file_init()
1065 file->status_code |= FILE_ERROR; in file_init()
1073 file->out = fopen(out_name, "wb"); in file_init()
1075 if (file->out == NULL) in file_init()
1077 file->write_errno = errno; in file_init()
1078 file->status_code |= WRITE_ERROR; in file_init()
1088 log_error(struct file *file, int code, const char *what) in log_error() argument
1091 if (file->global->errors) in log_error()
1092 emit_error(file, code, what); in log_error()
1127 static png_uint_32 current_type(struct file *file, int code);
1130 stop(struct file *file, int code, const char *what) in stop() argument
1135 log_error(file, code, what); in stop()
1150 if (file->global->quiet < 2) /* need two quiets to stop this. */ in stop()
1154 if (file->chunk != NULL) in stop()
1155 type = current_type(file, code); /* Gropes in struct chunk and IDAT */ in stop()
1158 type = file->type; in stop()
1166 printf(" ERR %.2x %s ", file->status_code, strcode(code)); in stop()
1170 emit_string(strerror(file->read_errno), stdout); in stop()
1172 emit_string(strerror(file->write_errno), stdout); in stop()
1176 fputs(file->file_name, stdout); in stop()
1180 file->status_code |= FILE_ERROR; in stop()
1181 longjmp(file->jmpbuf, code); in stop()
1185 stop_invalid(struct file *file, const char *what) in stop_invalid() argument
1187 stop(file, INVALID_ERROR_CODE, what); in stop_invalid()
1191 type_message(struct file *file, png_uint_32 type, const char *what) in type_message() argument
1194 if (file->global->errors) in type_message()
1196 fputs(file->file_name, stderr); in type_message()
1209 file_getpos(struct file *file, fpos_t *pos) in file_getpos() argument
1211 if (fgetpos(file->file, pos)) in file_getpos()
1214 perror(file->file_name); in file_getpos()
1215 stop(file, READ_ERROR_CODE, "fgetpos"); in file_getpos()
1220 file_setpos(struct file *file, const fpos_t *pos) in file_setpos() argument
1222 if (fsetpos(file->file, pos)) in file_setpos()
1224 perror(file->file_name); in file_setpos()
1225 stop(file, READ_ERROR_CODE, "fsetpos"); in file_setpos()
1230 getpos(struct file *file) in getpos() argument
1236 file_getpos(file, &file->data_pos); in getpos()
1245 read_byte(struct file *file) in read_byte() argument
1247 int ch = getc(file->file); in read_byte()
1251 ++(file->read_count); in read_byte()
1257 file->status_code |= INTERNAL_ERROR; in read_byte()
1258 file->read_errno = ERANGE; /* out of range character */ in read_byte()
1261 emit_error(file, UNEXPECTED_ERROR_CODE, "file read"); in read_byte()
1268 return read_byte(file); in read_byte()
1277 if (ferror(file->file)) in read_byte()
1278 file->read_errno = errno; in read_byte()
1280 else if (feof(file->file)) in read_byte()
1281 file->read_errno = 0; /* I.e. a regular EOF, no error */ in read_byte()
1284 file->read_errno = EDOM; in read_byte()
1291 file->status_code |= TRUNCATED; in read_byte()
1296 reread_byte(struct file *file) in reread_byte() argument
1301 int ch = getc(file->file); in reread_byte()
1304 file->read_errno = errno; in reread_byte()
1307 stop(file, UNEXPECTED_ERROR_CODE, "reread"); in reread_byte()
1313 reread_4(struct file *file) in reread_4() argument
1320 result = (result << 8) + reread_byte(file); in reread_4()
1326 skip_12(struct file *file) in skip_12() argument
1332 if (fseek(file->file, 12, SEEK_CUR) != 0) in skip_12()
1335 file->read_errno = errno; in skip_12()
1337 stop(file, UNEXPECTED_ERROR_CODE, "reskip"); in skip_12()
1342 write_byte(struct file *file, int b) in write_byte() argument
1348 if (file->out != NULL) in write_byte()
1350 if (putc(b, file->out) != b) in write_byte()
1352 file->write_errno = errno; in write_byte()
1353 file->status_code |= WRITE_ERROR; in write_byte()
1354 stop(file, WRITE_ERROR_CODE, "write byte"); in write_byte()
1358 ++(file->write_count); in write_byte()
1363 read_4(struct file *file, png_uint_32 *pu) in read_4() argument
1373 int ch = read_byte(file); in read_4()
1387 crc_read_many(struct file *file, png_uint_32 length) in crc_read_many() argument
1394 png_uint_32 crc = file->crc; in crc_read_many()
1398 int ch = read_byte(file); in crc_read_many()
1407 file->crc = crc; in crc_read_many()
1414 calc_image_size(struct file *file) in calc_image_size() argument
1419 png_uint_16 pd = file->bit_depth; in calc_image_size()
1421 switch (file->color_type) in calc_image_size()
1424 stop_invalid(file, "IHDR: colour type"); in calc_image_size()
1427 stop_invalid(file, "IHDR: bit depth"); in calc_image_size()
1461 if (file->width < 1 || file->width > 0x7fffffff) in calc_image_size()
1462 stop_invalid(file, "IHDR: width"); in calc_image_size()
1464 else if (file->height < 1 || file->height > 0x7fffffff) in calc_image_size()
1465 stop_invalid(file, "IHDR: height"); in calc_image_size()
1467 else if (file->compression_method != 0) in calc_image_size()
1468 stop_invalid(file, "IHDR: compression method"); in calc_image_size()
1470 else if (file->filter_method != 0) in calc_image_size()
1471 stop_invalid(file, "IHDR: filter method"); in calc_image_size()
1473 else switch (file->interlace_method) in calc_image_size()
1486 png_uint_32 pw = PNG_PASS_COLS(file->width, pass); in calc_image_size()
1500 image_digits = uarb_mult32(file->image_bytes, image_digits, in calc_image_size()
1502 PNG_PASS_ROWS(file->height, pass)); in calc_image_size()
1506 file->image_digits = image_digits; in calc_image_size()
1517 row_width, uarb_set(row_width, file->width), pd); in calc_image_size()
1522 file->image_digits = uarb_mult32(file->image_bytes, 0, in calc_image_size()
1523 row_bytes, digits, file->height); in calc_image_size()
1528 stop_invalid(file, "IHDR: interlace method"); in calc_image_size()
1531 assert(file->image_digits >= 1 && file->image_digits <= 5); in calc_image_size()
1543 struct file * file; member
1570 type_message(chunk->file, chunk->chunk_type, message); in chunk_message()
1583 chunk_init(struct chunk * const chunk, struct file * const file) in chunk_init() argument
1591 assert(file->chunk == NULL); in chunk_init()
1595 chunk->file = file; in chunk_init()
1596 chunk->global = file->global; in chunk_init()
1598 chunk->chunk_data_pos = file->data_pos; in chunk_init()
1599 chunk->chunk_length = file->length; in chunk_init()
1600 chunk->chunk_type = file->type; in chunk_init()
1608 file->chunk = chunk; in chunk_init()
1612 current_type(struct file *file, int code) in current_type() argument
1620 if (file->chunk != NULL) in current_type()
1622 png_uint_32 type = file->chunk->chunk_type; in current_type()
1629 file->write_count == 8) in current_type()
1636 return file->type; in current_type()
1646 chunk->file->read_count = 8; in setpos()
1647 file_setpos(chunk->file, &chunk->chunk_data_pos); in setpos()
1661 static int zlib_check(struct file *file, png_uint_32 offset);
1664 process_zTXt_iCCP(struct file *file) in process_zTXt_iCCP() argument
1669 struct chunk *chunk = file->chunk; in process_zTXt_iCCP()
1673 assert(chunk != NULL && file->idat == NULL); in process_zTXt_iCCP()
1681 if (reread_byte(file) == 0) /* keyword null terminator */ in process_zTXt_iCCP()
1685 (void)reread_byte(file); /* compression method */ in process_zTXt_iCCP()
1686 return zlib_check(file, index); in process_zTXt_iCCP()
1695 process_iTXt(struct file *file) in process_iTXt() argument
1698 struct chunk *chunk = file->chunk; in process_iTXt()
1702 assert(chunk != NULL && file->idat == NULL); in process_iTXt()
1710 if (reread_byte(file) == 0) /* keyword null terminator */ in process_iTXt()
1714 if (reread_byte(file) == 0) /* uncompressed text */ in process_iTXt()
1719 (void)reread_byte(file); /* compression method */ in process_iTXt()
1726 if (reread_byte(file) == 0) /* terminator */ in process_iTXt()
1733 if (reread_byte(file) == 0) /* terminator */ in process_iTXt()
1734 return zlib_check(file, index); in process_iTXt()
1744 log_error(file, INVALID_ERROR_CODE, "iTXt chunk length"); in process_iTXt()
1753 struct file * file; member
1775 struct file *file = idat->file; in IDAT_end() local
1781 assert(file->chunk != NULL); in IDAT_end()
1782 chunk_end(&file->chunk); in IDAT_end()
1788 file->state = STATE_CHUNKS; in IDAT_end()
1792 IDAT_init(struct IDAT * const idat, struct file * const file) in IDAT_init() argument
1800 assert(file->chunk == NULL); in IDAT_init()
1801 assert(file->idat == NULL); in IDAT_init()
1805 idat->file = file; in IDAT_init()
1806 idat->global = file->global; in IDAT_init()
1817 file->alloc(file, 0/*chunk*/); in IDAT_init()
1818 assert(file->chunk != NULL); in IDAT_init()
1823 file->idat = idat; in IDAT_init()
1908 process_IDAT(struct file *file) in process_IDAT() argument
1927 assert(file->idat != NULL && file->chunk != NULL); in process_IDAT()
1938 list = file->idat->idat_list_tail; in process_IDAT()
1945 stop(file, READ_ERROR_CODE, "out of memory"); in process_IDAT()
1949 file->idat->idat_list_tail = list; in process_IDAT()
1953 list->lengths[(list->count)++] = file->chunk->chunk_length; in process_IDAT()
1958 if (file->type == png_IDAT) in process_IDAT()
1966 setpos(file->chunk); in process_IDAT()
1968 if (zlib_check(file, 0)) in process_IDAT()
1976 cmp = uarb_cmp(file->image_bytes, file->image_digits, in process_IDAT()
1977 file->chunk->uncompressed_bytes, file->chunk->uncompressed_digits); in process_IDAT()
1980 type_message(file, png_IDAT, "extra uncompressed data"); in process_IDAT()
1983 stop(file, LIBPNG_ERROR_CODE, "IDAT: uncompressed data too small"); in process_IDAT()
1989 setpos(file->chunk); in process_IDAT()
1991 idat = file->idat; in process_IDAT()
1998 file->chunk->chunk_length = rechunk_length(idat); in process_IDAT()
2001 file->state = STATE_IDAT; in process_IDAT()
2007 stop(file, ZLIB_ERROR_CODE, "could not uncompress IDAT"); in process_IDAT()
2016 struct file * file; member
2085 fputs(zlib->file->file_name, stderr); in zlib_message()
2129 fputs(zlib->file->file_name, stdout); in zlib_end()
2153 fputs(zlib->file->file_name, stdout); in zlib_end()
2204 zlib->file = chunk->file; in zlib_init()
2286 struct file *file = zlib->file; in zlib_advance() local
2294 png_byte bIn = reread_byte(file); in zlib_advance()
2526 skip_12(zlib->file); in zlib_run()
2608 zlib_check(struct file *file, png_uint_32 offset) in zlib_check() argument
2626 file_getpos(file, &start_pos); in zlib_check()
2629 if (zlib_init(&zlib, file->idat, file->chunk, 0/*window bits*/, offset)) in zlib_check()
2640 file->status_code |= TOO_FAR_BACK; in zlib_check()
2696 file_setpos(file, &start_pos); in zlib_check()
2783 static void read_chunk(struct file *file);
2785 process_chunk(struct file *file, png_uint_32 file_crc, png_uint_32 next_length, in process_chunk() argument
2795 const png_uint_32 type = file->type; in process_chunk()
2797 if (file->global->verbose > 1) in process_chunk()
2800 type_name(file->type, stderr); in process_chunk()
2801 fprintf(stderr, " %lu 0x%.8x 0x%.8x\n", (unsigned long)file->length, in process_chunk()
2802 file->crc ^ 0xffffffff, file_crc); in process_chunk()
2809 if ((file->crc ^ 0xffffffff) != file_crc) in process_chunk()
2816 file->status_code |= CRC_ERROR; in process_chunk()
2819 if (file->global->skip != SKIP_BAD_CRC) in process_chunk()
2820 type_message(file, type, "bad CRC"); in process_chunk()
2824 stop(file, READ_ERROR_CODE, "bad CRC in critical chunk"); in process_chunk()
2828 type_message(file, type, "skipped: bad CRC"); in process_chunk()
2839 if (skip_chunk_type(file->global, type)) in process_chunk()
2848 file->alloc(file, 0/*chunk*/); in process_chunk()
2850 else if (file->idat == NULL) in process_chunk()
2851 file->alloc(file, 1/*IDAT*/); in process_chunk()
2856 assert(file->chunk != NULL); in process_chunk()
2857 assert(file->chunk->chunk_type == png_IDAT); in process_chunk()
2858 file->chunk->chunk_length = file->length; in process_chunk()
2866 file->length = next_length; in process_chunk()
2867 file->type = next_type; in process_chunk()
2868 getpos(file); in process_chunk()
2874 file->chunk->rewrite_length = 0; in process_chunk()
2875 file->chunk->rewrite_offset = 0; in process_chunk()
2887 struct chunk *chunk = file->chunk; in process_chunk()
2890 stop_invalid(file, "IHDR length"); in process_chunk()
2894 file->width = reread_4(file); in process_chunk()
2895 file->height = reread_4(file); in process_chunk()
2896 file->bit_depth = reread_byte(file); in process_chunk()
2897 file->color_type = reread_byte(file); in process_chunk()
2898 file->compression_method = reread_byte(file); in process_chunk()
2899 file->filter_method = reread_byte(file); in process_chunk()
2900 file->interlace_method = reread_byte(file); in process_chunk()
2905 calc_image_size(file); in process_chunk()
2911 if (process_zTXt_iCCP(file)) in process_chunk()
2913 chunk_end(&file->chunk); in process_chunk()
2914 file_setpos(file, &file->data_pos); in process_chunk()
2918 if (process_iTXt(file)) in process_chunk()
2920 chunk_end(&file->chunk); in process_chunk()
2921 file_setpos(file, &file->data_pos); in process_chunk()
2925 if (process_IDAT(file)) in process_chunk()
2939 read_chunk(file); in process_chunk()
2946 file->length = next_length; in process_chunk()
2947 file->type = next_type; in process_chunk()
2948 getpos(file); in process_chunk()
2949 read_chunk(file); in process_chunk()
2965 sync_stream(struct file *file) in sync_stream() argument
2972 file->status_code |= STREAM_ERROR; in sync_stream()
2974 if (file->global->verbose) in sync_stream()
2977 type_name(file->type, stderr); in sync_stream()
2982 file_setpos(file, &file->data_pos); in sync_stream()
2983 file->read_count = 8; in sync_stream()
2985 if (read_4(file, &file_crc) == 4) /* else completely truncated */ in sync_stream()
2993 png_uint_32 type = file->type; in sync_stream()
3009 file->length = length; in sync_stream()
3010 process_chunk(file, file_crc, 0, 0); in sync_stream()
3019 ch = read_byte(file); in sync_stream()
3039 file->read_count -= 8; in sync_stream()
3040 process_chunk(file, file_crc, next_length, next_type); in sync_stream()
3060 ch = read_byte(file); in sync_stream()
3079 stop(file, READ_ERROR_CODE, "damaged PNG stream"); in sync_stream()
3083 read_chunk(struct file *file) in read_chunk() argument
3097 png_uint_32 length = file->length; in read_chunk()
3098 png_uint_32 type = file->type; in read_chunk()
3104 stop(file, UNEXPECTED_ERROR_CODE, "read beyond IEND"); in read_chunk()
3106 if (file->global->verbose > 2) in read_chunk()
3117 file->crc = crc_init_4(type); in read_chunk()
3118 if (crc_read_many(file, length)) /* else it was truncated */ in read_chunk()
3121 unsigned int nread = read_4(file, &file_crc); in read_chunk()
3129 nread += read_4(file, &next_length); in read_chunk()
3134 nread += read_4(file, &next_type); in read_chunk()
3141 file->read_count -= 8; in read_chunk()
3142 process_chunk(file, file_crc, next_length, next_type); in read_chunk()
3150 process_chunk(file, file_crc, 0, 0); in read_chunk()
3163 sync_stream(file); in read_chunk()
3167 static struct file *get_control(png_const_structrp png_ptr);
3178 struct file *file = get_control(png_ptr); in warning_handler() local
3180 if (file->global->warnings) in warning_handler()
3181 emit_error(file, LIBPNG_WARNING_CODE, message); in warning_handler()
3191 struct file *file = get_control(png_ptr); in read_callback() local
3197 stop(file, UNEXPECTED_ERROR_CODE, "read callback for 0 bytes"); in read_callback()
3204 if (file->read_count < 8) in read_callback()
3206 assert(file->read_count == 0); in read_callback()
3207 assert((file->status_code & TRUNCATED) == 0); in read_callback()
3209 (void)read_4(file, &file->length); in read_callback()
3211 if (file->read_count == 4) in read_callback()
3212 (void)read_4(file, &file->type); in read_callback()
3214 if (file->read_count < 8) in read_callback()
3216 assert((file->status_code & TRUNCATED) != 0); in read_callback()
3217 stop(file, READ_ERROR_CODE, "not a PNG (too short)"); in read_callback()
3220 if (file->state == STATE_SIGNATURE) in read_callback()
3222 if (file->length != sig1 || file->type != sig2) in read_callback()
3223 stop(file, LIBPNG_ERROR_CODE, "not a PNG (signature)"); in read_callback()
3228 file->write_count = 0; in read_callback()
3233 assert(file->state == STATE_CHUNKS); in read_callback()
3238 if (file->length != 13 || file->type != png_IHDR) in read_callback()
3239 stop(file, LIBPNG_ERROR_CODE, "not a PNG (IHDR)"); in read_callback()
3242 getpos(file); in read_callback()
3249 chunk = file->chunk; in read_callback()
3262 length = file->length; in read_callback()
3263 type = file->type; in read_callback()
3280 if (file->state != STATE_SIGNATURE && chunk == NULL) in read_callback()
3282 assert(file->read_count == 8); in read_callback()
3283 assert(file->idat == NULL); in read_callback()
3284 read_chunk(file); in read_callback()
3285 chunk = file->chunk; in read_callback()
3293 file->write_count = 0; in read_callback()
3300 switch (file->write_count) in read_callback()
3317 if (file->state == STATE_SIGNATURE) in read_callback()
3324 file->read_count = 0; /* Forces a header read */ in read_callback()
3325 file->state = STATE_CHUNKS; /* IHDR: checked above */ in read_callback()
3341 if (file->state != STATE_IDAT && length > 0) in read_callback()
3352 switch (file->write_count - length) in read_callback()
3361 if (file->state == STATE_IDAT) in read_callback()
3363 struct IDAT *idat = file->idat; in read_callback()
3393 stop(file, UNEXPECTED_ERROR_CODE, in read_callback()
3407 skip_12(file); in read_callback()
3418 b = reread_byte(file); in read_callback()
3453 if (file->global->verbose > 2) in read_callback()
3468 if (file->state == STATE_IDAT && in read_callback()
3469 (file->idat->idat_index < file->idat->idat_length || in read_callback()
3470 1+file->idat->idat_count < file->idat->idat_cur->count || in read_callback()
3471 file->idat->idat_cur != file->idat->idat_list_tail)) in read_callback()
3476 length = chunk->chunk_length = rechunk_length(file->idat); in read_callback()
3478 file->write_count = 0; /* for the new chunk */ in read_callback()
3479 --(file->write_count); /* fake out the increment below */ in read_callback()
3488 stop(file, UNEXPECTED_ERROR_CODE, "pending rewrite"); in read_callback()
3494 file->read_count = 8; in read_callback()
3495 file_setpos(file, &file->data_pos); in read_callback()
3497 if (file->idat == NULL) in read_callback()
3498 chunk_end(&file->chunk); in read_callback()
3501 IDAT_end(&file->idat); in read_callback()
3515 write_byte(file, (png_byte)b); /* increments chunk_write */ in read_callback()
3525 struct file file; member
3533 return file_end(&control->file); in control_end()
3536 static struct file *
3544 return &control->file; in get_control()
3548 allocate(struct file *file, int allocate_idat) in allocate() argument
3550 struct control *control = voidcast(struct control*, file->alloc_ptr); in allocate()
3554 assert(file->idat == NULL); in allocate()
3555 IDAT_init(&control->idat, file); in allocate()
3560 assert(file->chunk == NULL); in allocate()
3561 chunk_init(&control->chunk, file); in allocate()
3572 return file_init(&control->file, global, file_name, out_name, control, in control_init()
3592 log_error(&control->file, LIBPNG_ERROR_CODE, "OOM allocating png_struct"); in read_png()
3593 control->file.status_code |= INTERNAL_ERROR; in read_png()
3597 rc = setjmp(control->file.jmpbuf); in read_png()
3616 if (control->file.global->verbose) in read_png()
3640 if (control->file.global->verbose) in read_png()