diff --git a/libretro-common/formats/png/rpng.c b/libretro-common/formats/png/rpng.c index 94c2b5eea0..53ed97533d 100644 --- a/libretro-common/formats/png/rpng.c +++ b/libretro-common/formats/png/rpng.c @@ -22,6 +22,12 @@ #include +/* + * temporarily added by cxd4 (due to slowness at learning the NBIO API) + * In a future commit, this should be replaced with . + */ +#include + #include #include @@ -768,22 +774,22 @@ bool rpng_load_image_argb_process(uint8_t *inflate_buf, return true; } -static bool rpng_load_image_argb_init(FILE *file, +static bool rpng_load_image_argb_init( + const char * path, uint32_t **data, unsigned *width, unsigned *height, - long *file_len) + ssize_t *file_len) { - char header[8]; + uint8_t * header; *data = NULL; *width = 0; *height = 0; - fseek(file, 0, SEEK_END); - *file_len = ftell(file); - rewind(file); + if (!read_file(path, (void **)&header, file_len)) + return false; - if (fread(header, 1, sizeof(header), file) != sizeof(header)) + if (*file_len < sizeof(png_magic)) return false; if (memcmp(header, png_magic, sizeof(png_magic)) != 0) @@ -795,7 +801,8 @@ static bool rpng_load_image_argb_init(FILE *file, bool rpng_load_image_argb(const char *path, uint32_t **data, unsigned *width, unsigned *height) { - long pos, file_len; + ssize_t pos, file_len; + FILE * file; uint8_t *inflate_buf = NULL; struct idat_buffer idat_buf = {0}; struct png_ihdr ihdr = {0}; @@ -805,20 +812,20 @@ bool rpng_load_image_argb(const char *path, uint32_t **data, bool has_iend = false; bool has_plte = false; bool ret = true; - FILE *file = fopen(path, "rb"); - if (!file) + if (!rpng_load_image_argb_init(path, data, width, height, &file_len)) + GOTO_END_ERROR(); + + /* feof() apparently isn't triggered after a seek (IEND). */ + + file = fopen(path, "rb"); + if (file == NULL) { fprintf(stderr, "Tried to open file: %s\n", path); GOTO_END_ERROR(); } - if (!rpng_load_image_argb_init(file, data, width, height, &file_len)) - GOTO_END_ERROR(); - - /* feof() apparently isn't triggered after a seek (IEND). */ - - for (pos = 0; pos < file_len && pos >= 0; pos = ftell(file)) + for (pos = 0; pos < file_len && pos >= 0; pos = (ssize_t)ftell(file)) { size_t increment = 0; struct png_chunk chunk = {0};