From 121d348c64d202e2b1c1ef5b89f5732fc97342fd Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Wed, 22 Jul 2009 21:42:33 +0200 Subject: [PATCH] Escape adblock filters properly and adjust the tests --- AUTHORS | 1 + extensions/adblock.c | 56 ++++++++++++++++++++++++++++++---------- extensions/wscript_build | 3 --- 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/AUTHORS b/AUTHORS index bc77c4db..6d1db6f2 100644 --- a/AUTHORS +++ b/AUTHORS @@ -17,6 +17,7 @@ Contributors: Johannes Reinhardt Jean-François Guchens Jérôme Geulfucci + Alexander Butenko Graphics: extension: Nancy Runge diff --git a/extensions/adblock.c b/extensions/adblock.c index 8debf5e1..a9e438f0 100644 --- a/extensions/adblock.c +++ b/extensions/adblock.c @@ -1,5 +1,6 @@ /* Copyright (C) 2009 Christian Dywan + Copyright (C) 2009 Alexander Butenko This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -19,6 +20,38 @@ #include #endif +static gchar * +adblock_fixup_regexp (gchar* src) +{ + gchar* dst; + gchar* s; + /* FIXME: Avoid always allocating twice the string */ + s = dst = g_malloc (strlen (src) * 2); + + while (*src) + { + switch (*src) + { + case '*': + *s++ = '.'; + break; + case '.': + *s++ = '\\'; + break; + case '?': + *s++ = '\\'; + break; + case '|': + *s++ = '\\'; + break; + } + *s++ = *src; + src++; + } + *s = 0; + return dst; +} + static void adblock_app_add_browser_cb (MidoriApp* app, MidoriBrowser* browser, @@ -322,12 +355,7 @@ adblock_parse_line (gchar* line) if (line[0] == '[') return NULL; g_strchomp (line); - /* TODO: Replace trailing '*' with '.*' */ - if (line[0] == '*') - return g_strconcat (".", line, NULL); - else if (line[0] == '?') - return g_strconcat ("\\", line, NULL); - return g_strdup (line); + return adblock_fixup_regexp (line); } static GHashTable* @@ -469,11 +497,11 @@ test_adblock_parse (void) g_assert_cmpstr (adblock_parse_line ("*foo"), ==, ".*foo"); g_assert_cmpstr (adblock_parse_line ("?foo"), ==, "\\?foo"); - /* g_assert_cmpstr (adblock_parse_line ("foo*"), ==, "foo.*"); - g_assert_cmpstr (adblock_parse_line ("foo?"), ==, "foo\\?"); */ + g_assert_cmpstr (adblock_parse_line ("foo*"), ==, "foo.*"); + g_assert_cmpstr (adblock_parse_line ("foo?"), ==, "foo\\?"); - g_assert_cmpstr (adblock_parse_line (".*foo/bar"), ==, ".*foo/bar"); - g_assert_cmpstr (adblock_parse_line ("http://bla.blub/.*"), ==, "http://bla.blub/.*"); + g_assert_cmpstr (adblock_parse_line (".*foo/bar"), ==, "\\..*foo/bar"); + g_assert_cmpstr (adblock_parse_line ("http://bla.blub/*"), ==, "http://bla\\.blub/.*"); } static void @@ -486,10 +514,10 @@ test_adblock_pattern (void) temp = g_file_open_tmp ("midori_adblock_match_test_XXXXXX", &filename, NULL); g_file_set_contents (filename, - "*ads.foo.bar.*\n" - ".*ads.bogus.name.*\n" - "http://ads.bla.blub/.*\n" - "http://ads.blub.boing/*.", + "*ads.foo.bar*\n" + "*ads.bogus.name*\n" + "http://ads.bla.blub/*\n" + "http://ads.blub.boing/*", -1, NULL); pattern = adblock_parse_file (filename); diff --git a/extensions/wscript_build b/extensions/wscript_build index 751162ad..8f84a173 100644 --- a/extensions/wscript_build +++ b/extensions/wscript_build @@ -6,9 +6,6 @@ import os extensions = os.listdir ('extensions') for extension in extensions: - # Adblock is incomplete and not ready for release - if extension == 'adblock.c': - continue folder = 'extensions' + os.sep + extension if os.path.isdir (folder): files = os.listdir (folder) -- 2.39.5