dynamically allocate globalcss and sitecss, update TODO - surf-adblock - Surf a… | |
git clone git://git.codemadness.org/surf-adblock | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit 2b4dbc8301f9bc4f3ee3ea807563de705af22c13 | |
parent cb128e7fe7db0319770a0e6d7ccd691385b700ad | |
Author: Hiltjo Posthuma <[email protected]> | |
Date: Sat, 16 Jul 2016 11:53:33 +0200 | |
dynamically allocate globalcss and sitecss, update TODO | |
the matching behaviour will be rewritten, it is too inefficient | |
Diffstat: | |
M TODO | 5 +++++ | |
M surf-adblock.c | 66 ++++++++++++++++++++++++++---… | |
2 files changed, 61 insertions(+), 10 deletions(-) | |
--- | |
diff --git a/TODO b/TODO | |
@@ -2,10 +2,15 @@ Docs: | |
- https://adblockplus.org/en/filter-cheatsheet | |
- https://adblockplus.org/filters | |
+- CSS blocking: don't allow sites to override the appended global and | |
+ site-specific stylesheet. | |
- separate between site-specific and global block rules. | |
- optimize matching: | |
? hashtable per domain for specific rules? | |
- separate general block rules into a separate list. | |
+ - test: just use a simple strstr/strcasestr for patterns with no wildc… | |
+ and no matchbegin/matchend. Use strncmp/strcasecmp for patterns with… | |
+ and matchbegin or matchend set. | |
- optimize memory allocation. | |
- optimize: pregenerate one global stylesheet that applies to all sites? | |
? support exception rules #@# | |
diff --git a/surf-adblock.c b/surf-adblock.c | |
@@ -14,6 +14,13 @@ | |
#include <webkit2/webkit-web-extension.h> | |
#include <webkitdom/webkitdom.h> | |
+/* String data / memory pool */ | |
+typedef struct string { | |
+ char *data; /* data */ | |
+ size_t len; /* string length */ | |
+ size_t bufsiz; /* allocated size */ | |
+} String; | |
+ | |
typedef struct Page { | |
guint64 id; | |
WebKitWebPage *webpage; | |
@@ -100,9 +107,37 @@ struct filtertype filtertypes[] = { | |
}; | |
static Page *pages; | |
-static char globalcss[5000000]; /* TEST: dynamic allocate later */ | |
+static String globalcss; | |
static struct filterrule *rules; | |
+static void | |
+string_buffer_realloc(String *s, size_t newlen) | |
+{ | |
+ size_t alloclen; | |
+ | |
+ for (alloclen = 64; alloclen <= newlen; alloclen *= 2) | |
+ ; | |
+ if (!(s->data = realloc(s->data, alloclen))) { | |
+ fprintf(stderr, "realloc: %s\n", strerror(errno)); | |
+ exit(1); | |
+ } | |
+ s->bufsiz = alloclen; | |
+} | |
+ | |
+static void | |
+string_append(String *s, const char *data, size_t len) | |
+{ | |
+ if (!len) | |
+ return; | |
+ /* check if allocation is necesary, don't shrink buffer, | |
+ * should be more than bufsiz ofcourse. */ | |
+ if (s->len + len >= s->bufsiz) | |
+ string_buffer_realloc(s, s->len + len + 1); | |
+ memcpy(s->data + s->len, data, len); | |
+ s->len += len; | |
+ s->data[s->len] = '\0'; | |
+} | |
+ | |
void * | |
ecalloc(size_t nmemb, size_t size) | |
{ | |
@@ -471,14 +506,18 @@ matchrule(struct filterrule *f, const char *uri, const ch… | |
} | |
if (r && !match(pat, uri, (f->block & FilterTypeMatchCase) ? 0 : 1)) { | |
+#if 0 | |
for (; *type; type++) { | |
for (i = 0; blockstr[i]; i++) { | |
if (blockstr[i] == *type && | |
- f->block & (1 << i)) { | |
+ f->block & (1 << i)) | |
printf("block type '%c'\n", blockstr[i… | |
+ return 1; | |
} | |
} | |
} | |
+ return 0; | |
+#endif | |
return 1; | |
} | |
return 0; | |
@@ -649,7 +688,7 @@ documentloaded(WebKitWebPage *wp, Page *p) | |
WebKitDOMDocument *doc = webkit_web_page_get_dom_document(wp); | |
WebKitDOMHTMLElement *body = webkit_dom_document_get_body(doc); | |
WebKitDOMElement *el; | |
- char sitecss[1000000] = ""; /* TODO: dynamic allocate */ | |
+ String sitecss; | |
struct filterrule *r; | |
char *uri = estrdup((char *)webkit_web_page_get_uri(p->webpage)); | |
char *domain, *s; | |
@@ -666,28 +705,34 @@ documentloaded(WebKitWebPage *wp, Page *p) | |
printf("uri: %s\n", uri); | |
printf("domain: %s\n", domain); | |
+#if 1 | |
/* site-specific CSS */ | |
+ memset(&sitecss, 0, sizeof(sitecss)); | |
for (r = rules; r; r = r->next) { | |
if (!r->css || !r->domains || !matchrule(r, "", "", domain)) | |
continue; | |
- strlcat(sitecss, r->css, sizeof(sitecss)); | |
- strlcat(sitecss, "{display:none;}", sizeof(sitecss)); | |
+ string_append(&sitecss, r->css, strlen(r->css)); | |
+ string_append(&sitecss, STRP("{display:none;}")); | |
} | |
- printf("sitecss: %s\n", sitecss); | |
+ printf("sitecss: %s\n", sitecss.data ? sitecss.data : "<empty>"); | |
+#endif | |
p->view = webkit_dom_document_get_default_view(doc); | |
+#if 1 | |
el = webkit_dom_document_create_element(doc, "style", NULL); | |
webkit_dom_element_set_attribute(el, "type", "text/css", NULL); | |
- webkit_dom_element_set_inner_html(el, globalcss, NULL); | |
+ webkit_dom_element_set_inner_html(el, globalcss.data, NULL); | |
webkit_dom_node_append_child(WEBKIT_DOM_NODE(body), WEBKIT_DOM_NODE(el… | |
el = webkit_dom_document_create_element(doc, "style", NULL); | |
webkit_dom_element_set_attribute(el, "type", "text/css", NULL); | |
- webkit_dom_element_set_inner_html(el, sitecss, NULL); | |
+ webkit_dom_element_set_inner_html(el, sitecss.data, NULL); | |
webkit_dom_node_append_child(WEBKIT_DOM_NODE(body), WEBKIT_DOM_NODE(el… | |
+#endif | |
free(uri); | |
+ free(sitecss.data); | |
} | |
static gboolean | |
@@ -761,8 +806,9 @@ webkit_web_extension_initialize(WebKitWebExtension *e) | |
for (r = rules; r; r = r->next) { | |
if (!r->css || r->domains) | |
continue; | |
- strlcat(globalcss, r->css, sizeof(globalcss)); | |
- strlcat(globalcss, "{display:none;}", sizeof(globalcss)); | |
+ | |
+ string_append(&globalcss, r->css, strlen(r->css)); | |
+ string_append(&globalcss, STRP("{display:none;}")); | |
} | |
g_signal_connect(e, "page-created", |