From 6c54e8119e8ff17c3e652cf500a481b5ec4dff78 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sat, 21 Feb 2015 03:57:34 +0100 Subject: [PATCH] (NBIO) Make all nbio functions nonblocking --- .../formats/png/rpng_decode_fnbio.c | 55 ++++++++++++------- libretro-common/formats/png/rpng_test.c | 16 ++++-- libretro-common/include/formats/rpng.h | 2 + 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/libretro-common/formats/png/rpng_decode_fnbio.c b/libretro-common/formats/png/rpng_decode_fnbio.c index 6161b457a5..a9292f6ce5 100644 --- a/libretro-common/formats/png/rpng_decode_fnbio.c +++ b/libretro-common/formats/png/rpng_decode_fnbio.c @@ -331,10 +331,7 @@ void rpng_nbio_load_image_free(struct rpng_t *rpng) struct rpng_t *rpng_nbio_load_image_argb_init(const char *path) { - unsigned i; - char header[8]; size_t file_len; - bool looped = false; struct nbio_t* nbread = NULL; struct rpng_t *rpng = (struct rpng_t*)calloc(1, sizeof(struct rpng_t)); @@ -352,22 +349,6 @@ struct rpng_t *rpng_nbio_load_image_argb_init(const char *path) nbio_begin_read(nbread); - while (!nbio_iterate(nbread)) - looped = true; - - rpng->ptr = nbio_get_ptr(nbread, &file_len); - (void)looped; - - rpng->buff_data = (uint8_t*)rpng->ptr; - - for (i = 0; i < 8; i++) - header[i] = rpng->buff_data[i]; - - if (memcmp(header, png_magic, sizeof(png_magic)) != 0) - goto error; - - rpng->buff_data += 8; - return rpng; error: @@ -377,3 +358,39 @@ error: free(rpng); return NULL; } + +bool rpng_nbio_load_image_argb_start(struct rpng_t *rpng) +{ + unsigned i; + size_t file_len; + char header[8]; + struct nbio_t *nbread = NULL; + + if (!rpng) + return false; + + nbread = (struct nbio_t*)rpng->userdata; + + if (!nbread) + return false; + + rpng->ptr = nbio_get_ptr(nbread, &file_len); + + if (!rpng->ptr) + return false; + + rpng->buff_data = (uint8_t*)rpng->ptr; + + if (!rpng->buff_data) + return false; + + for (i = 0; i < 8; i++) + header[i] = rpng->buff_data[i]; + + if (memcmp(header, png_magic, sizeof(png_magic)) != 0) + return false; + + rpng->buff_data += 8; + + return true; +} diff --git a/libretro-common/formats/png/rpng_test.c b/libretro-common/formats/png/rpng_test.c index add6c4b343..68b1b6beaf 100644 --- a/libretro-common/formats/png/rpng_test.c +++ b/libretro-common/formats/png/rpng_test.c @@ -21,6 +21,7 @@ */ #include +#include #include #include #include @@ -42,12 +43,17 @@ static bool rpng_nbio_load_image_argb(const char *path, uint32_t **data, goto end; } - while (1) - { - if (!rpng_nbio_load_image_argb_iterate( - rpng->buff_data, rpng)) - break; + while (!nbio_iterate((struct nbio_t*)rpng->userdata)); + if (!rpng_nbio_load_image_argb_start(rpng)) + { + ret = false; + goto end; + } + + while (rpng_nbio_load_image_argb_iterate( + rpng->buff_data, rpng)) + { rpng->buff_data += 4 + 4 + rpng->chunk.size + 4; } diff --git a/libretro-common/include/formats/rpng.h b/libretro-common/include/formats/rpng.h index e5d77561f6..757b3bafb3 100644 --- a/libretro-common/include/formats/rpng.h +++ b/libretro-common/include/formats/rpng.h @@ -90,6 +90,8 @@ bool rpng_nbio_load_image_argb_iterate(uint8_t *buf, bool rpng_nbio_load_image_argb_process(struct rpng_t *rpng, uint32_t **data, unsigned *width, unsigned *height); +bool rpng_nbio_load_image_argb_start(struct rpng_t *rpng); + #ifdef HAVE_ZLIB_DEFLATE bool rpng_save_image_argb(const char *path, const uint32_t *data, unsigned width, unsigned height, unsigned pitch);