kwo pushed a commit to branch master.

http://git.enlightenment.org/legacy/imlib2.git/commit/?id=11806ddb74ee0042f79dc403dee2b063ec82d2e6

commit 11806ddb74ee0042f79dc403dee2b063ec82d2e6
Author: Kim Woelders <[email protected]>
Date:   Sat Apr 9 13:34:32 2022 +0200

    SVG loader: Faster signature check
    
    Faster (when failing) than letting librsvg2 do it.
---
 src/modules/loaders/loader_svg.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/src/modules/loaders/loader_svg.c b/src/modules/loaders/loader_svg.c
index 95ff781..a664443 100644
--- a/src/modules/loaders/loader_svg.c
+++ b/src/modules/loaders/loader_svg.c
@@ -1,3 +1,4 @@
+#define _GNU_SOURCE             /* memmem() */
 #include "loader_common.h"
 
 #include <math.h>
@@ -7,6 +8,24 @@
 
 #define DPI 96
 
+#define MATCHSTR(ptr, len, str) (len >= sizeof(str) && memcmp(ptr, str, 
sizeof(str) - 1) == 0)
+#define FINDSTR(ptr, len, str) (memmem(ptr, len, str, sizeof(str) - 1) != 0)
+
+static int
+_sig_check(const unsigned char *buf, unsigned int len)
+{
+   /* May also be compressed? - forget for now */
+
+   if (len > 4096)
+      len = 4096;
+   if (MATCHSTR(buf, len, "<svg"))
+      return 0;
+   if (!MATCHSTR(buf, len, "<?xml version=") &&
+       !MATCHSTR(buf, len, "<!--") && !MATCHSTR(buf, len, "<!DOCTYPE svg"))
+      return 1;
+   return FINDSTR(buf, len, "<svg") ? 0 : 1;
+}
+
 #if LIBRSVG_CHECK_VERSION(2, 46, 0)
 
 static double
@@ -59,10 +78,15 @@ load2(ImlibImage * im, int load_data)
    if (fdata == MAP_FAILED)
       return LOAD_BADFILE;
 
+   error = NULL;
+   rsvg = NULL;
    surface = NULL;
    cr = NULL;
 
-   error = NULL;
+   /* Signature check */
+   if (_sig_check(fdata, im->fsize))
+      goto quit;
+
    rsvg = rsvg_handle_new_from_data(fdata, im->fsize, &error);
    if (!rsvg)
       goto quit;

-- 


Reply via email to