kwo pushed a commit to branch master.

http://git.enlightenment.org/legacy/imlib2.git/commit/?id=88231b52eb8ec9d44f7a48aefa958732e6b90998

commit 88231b52eb8ec9d44f7a48aefa958732e6b90998
Author: Kim Woelders <[email protected]>
Date:   Sun Oct 3 19:36:42 2021 +0200

    XPM loader: Use mmap() for loading
---
 src/modules/loaders/loader_xpm.c | 47 +++++++++++++++++++++++++++++++++-------
 1 file changed, 39 insertions(+), 8 deletions(-)

diff --git a/src/modules/loaders/loader_xpm.c b/src/modules/loaders/loader_xpm.c
index dd7c38e..d58de83 100644
--- a/src/modules/loaders/loader_xpm.c
+++ b/src/modules/loaders/loader_xpm.c
@@ -1,5 +1,33 @@
+#define _GNU_SOURCE             /* memmem() */
 #include "loader_common.h"
 
+#include <sys/mman.h>
+
+static struct {
+   const char         *data, *dptr;
+   unsigned int        size;
+} mdata;
+
+static void
+mm_init(void *src, unsigned int size)
+{
+   mdata.data = mdata.dptr = src;
+   mdata.size = size;
+}
+
+static int
+mm_getc(void)
+{
+   int                 ch;
+
+   if (mdata.dptr + 1 > mdata.data + mdata.size)
+      return -1;                /* Out of data */
+
+   ch = *mdata.dptr++;
+
+   return ch;
+}
+
 static FILE        *rgb_txt = NULL;
 
 static void
@@ -128,6 +156,7 @@ int
 load2(ImlibImage * im, int load_data)
 {
    int                 rc;
+   void               *fdata;
    DATA32             *ptr;
    int                 pc, c, i, j, k, w, h, ncolors, cpp;
    int                 comment, transp, quote, context, len, done, backslash;
@@ -144,15 +173,14 @@ load2(ImlibImage * im, int load_data)
    line = NULL;
    cmap = NULL;
 
-   len = fread(s, 1, sizeof(s) - 1, im->fp);
-   if (len < 9)
-      goto quit;
+   fdata = mmap(NULL, im->fsize, PROT_READ, MAP_SHARED, fileno(im->fp), 0);
+   if (fdata == MAP_FAILED)
+      return rc;
 
-   s[len] = '\0';
-   if (!strstr(s, " XPM */"))
+   if (!memmem(fdata, im->fsize, " XPM */", 7))
       goto quit;
 
-   rewind(im->fp);
+   mm_init(fdata, im->fsize);
 
    j = 0;
    w = 10;
@@ -176,8 +204,8 @@ load2(ImlibImage * im, int load_data)
    while (!done)
      {
         pc = c;
-        c = fgetc(im->fp);
-        if (c == EOF)
+        c = mm_getc();
+        if (c < 0)
            break;
 
         if (!quote)
@@ -452,6 +480,9 @@ load2(ImlibImage * im, int load_data)
 
    xpm_parse_done();
 
+   if (fdata != MAP_FAILED)
+      munmap(fdata, im->fsize);
+
    return rc;
 }
 

-- 


Reply via email to