kwo pushed a commit to branch master.

http://git.enlightenment.org/legacy/imlib2.git/commit/?id=51999ad031c4b6966c48d565316c74b075a36c98

commit 51999ad031c4b6966c48d565316c74b075a36c98
Author: Kim Woelders <[email protected]>
Date:   Tue Sep 14 13:07:07 2021 +0200

    WEBP loader: Use mmap() for loading
---
 src/modules/loaders/loader_webp.c | 50 +++++++++++++--------------------------
 1 file changed, 16 insertions(+), 34 deletions(-)

diff --git a/src/modules/loaders/loader_webp.c 
b/src/modules/loaders/loader_webp.c
index 01a943d..12986dc 100644
--- a/src/modules/loaders/loader_webp.c
+++ b/src/modules/loaders/loader_webp.c
@@ -1,8 +1,10 @@
 #include "loader_common.h"
-#include <sys/stat.h>
+
+#include <sys/mman.h>
 #include <webp/decode.h>
 #include <webp/encode.h>
 
+#if 0                           /* Unused */
 static const char  *
 webp_strerror(VP8StatusCode code)
 {
@@ -28,56 +30,35 @@ webp_strerror(VP8StatusCode code)
         return "Unknown error";
      }
 }
+#endif
 
 int
 load2(ImlibImage * im, int load_data)
 {
-   int                 rc, fd;
-   struct stat         st;
-   uint8_t            *fdata;
+   int                 rc;
+   void               *fdata;
    WebPBitstreamFeatures features;
    VP8StatusCode       vp8return;
-   unsigned int        size;
 
    rc = LOAD_FAIL;
-   fd = fileno(im->fp);
 
-   if (fstat(fd, &st) < 0)
+   if (im->fsize < 12)
       return rc;
 
-   fdata = malloc(st.st_size);
-   if (!fdata)
-      goto quit;
-
-   /* Check signature */
-   size = 12;
-   if (read(fd, fdata, size) != (long)size)
-      goto quit;
-   if (memcmp(fdata + 0, "RIFF", 4) != 0 || memcmp(fdata + 8, "WEBP", 4) != 0)
-      goto quit;
-
-   size = st.st_size;
-   if ((long)size != st.st_size)
+   fdata = mmap(0, im->fsize, PROT_READ, MAP_SHARED, fileno(im->fp), 0);
+   if (fdata == MAP_FAILED)
       goto quit;
 
-   size -= 12;
-   if (read(fd, fdata + 12, size) != (long)size)
+   vp8return = WebPGetFeatures(fdata, im->fsize, &features);
+   if (vp8return != VP8_STATUS_OK)
       goto quit;
 
-   if (WebPGetInfo(fdata, st.st_size, &im->w, &im->h) == 0)
-      goto quit;
+   im->w = features.width;
+   im->h = features.height;
 
    if (!IMAGE_DIMENSIONS_OK(im->w, im->h))
       goto quit;
 
-   vp8return = WebPGetFeatures(fdata, st.st_size, &features);
-   if (vp8return != VP8_STATUS_OK)
-     {
-        fprintf(stderr, "%s: Error reading file header: %s\n",
-                im->real_file, webp_strerror(vp8return));
-        goto quit;
-     }
-
    if (features.has_alpha == 0)
       UNSET_FLAG(im->flags, F_HAS_ALPHA);
    else
@@ -94,7 +75,7 @@ load2(ImlibImage * im, int load_data)
    if (!__imlib_AllocateData(im))
       goto quit;
 
-   if (WebPDecodeBGRAInto(fdata, st.st_size, (uint8_t *) im->data,
+   if (WebPDecodeBGRAInto(fdata, im->fsize, (uint8_t *) im->data,
                           sizeof(DATA32) * im->w * im->h, im->w * 4) == NULL)
       goto quit;
 
@@ -106,7 +87,8 @@ load2(ImlibImage * im, int load_data)
  quit:
    if (rc <= 0)
       __imlib_FreeData(im);
-   free(fdata);
+   if (fdata != MAP_FAILED)
+      munmap(fdata, im->fsize);
 
    return rc;
 }

-- 


Reply via email to