File: khtml_filter.cpp

package info (click to toggle)
khtml 5.54.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,940 kB
  • sloc: cpp: 206,281; java: 4,060; ansic: 2,829; perl: 2,313; yacc: 1,497; python: 339; sh: 141; xml: 37; makefile: 7
file content (330 lines) | stat: -rw-r--r-- 10,528 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/* This file is part of the KDE project

   Copyright (C) 2005 Ivor Hewitt     <ivor@kde.org>
   Copyright (C) 2008 Maksim Orlovich <maksim@kde.org>
   Copyright (C) 2008 Vyacheslav Tokarev <tsjoker@gmail.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "khtml_filter_p.h"
#include "khtml_debug.h"

// rolling hash parameters
#define HASH_P (1997)
#define HASH_Q (17509)
// HASH_MOD = (HASH_P^7) % HASH_Q
#define HASH_MOD (523)

namespace khtml
{

// We only want a subset of features of wildcards -- just the
// star, so we escape the rest before passing to QRegExp.
// The \ is escaped due to a QRegExp bug.
// ### we really should rather parse it ourselves, in order to
// handle adblock-special things like | and ^ properly.
static QRegExp fromAdBlockWildcard(const QString &wcStr)
{
    QRegExp rx;
    rx.setPatternSyntax(QRegExp::Wildcard);

    QString out;
    for (int p = 0; p < wcStr.length(); ++p) {
        QChar c = wcStr[p];
        if (c == QLatin1Char('?')) {
            out += QLatin1String("[?]");
        } else if (c == QLatin1Char('[')) {
            out += QLatin1String("[[]");
        } else if (c == QLatin1Char('\\')) {
            out += QLatin1String("[\\]");
        } else {
            out += c;
        }
    }

    rx.setPattern(out);
    return rx;
}

void FilterSet::addFilter(const QString &filterStr)
{
    QString filter = filterStr.trimmed();
    if (filter.isEmpty()) {
        return;
    }

    /** ignore special lines starting with "[", "!", or "#" or contain "#" (comments or features are not supported by KHTML's AdBlock */
    const QChar firstChar = filter.at(0);
    if (firstChar == QLatin1Char('[') || firstChar == QLatin1Char('!') || firstChar == QLatin1Char('#') || filter.contains(QLatin1Char('#'))) {
        return;
    }

    // Strip leading @@
    if (filter.startsWith(QLatin1String("@@"))) {
        filter.remove(0, 2);
    }

    // Strip options, we ignore them for now.
    bool hadOptions = false;
    int dollar = filter.lastIndexOf(QLatin1Char('$'));
    if (dollar != -1) {
        // Is it adblock's options delimiter or the special '$' char in a regular expression?
        if (!filter.startsWith(QLatin1Char('/')) || !filter.endsWith(QLatin1Char('/'))) {
            filter = filter.mid(0, dollar);
            hadOptions = true;
        }
    }

    // Is it a regexp filter?
    if (filter.length() > 2 && filter.startsWith(QLatin1Char('/')) && filter.endsWith(QLatin1Char('/'))) {
        // Ignore regexp that had options as we do not support them
        if (hadOptions) {
            return;
        }
        QString inside = filter.mid(1, filter.length() - 2);
        QRegExp rx(inside);
        reFilters.append(rx);
        //qCDebug(KHTML_LOG) << "R:" << inside;
        return;
    }
    // Nope, a wildcard one.

    // Disregard the rule if only one char is left or it contains unsupported adblock features ('|', "||", '^')
    if (filter.length() < 2 || filter.contains(QLatin1Char('|')) || filter.contains(QLatin1Char('^'))) {
        return;
    }

    // Strip wildcards at the ends
    int first = 0;
    int last  = filter.length() - 1;

    while (first < filter.length() && filter.at(first) == QLatin1Char('*')) {
        ++first;
    }

    while (last >= 0 && filter.at(last) == QLatin1Char('*')) {
        --last;
    }

    if (first > last) {
        filter = QLatin1String("*"); // erm... Well, they asked for it.
    } else {
        filter = filter.mid(first, last - first + 1);
    }

    // Now, do we still have any wildcard stuff left?
    int aPos = filter.indexOf('*');
    if (aPos != -1) {
        // check if we can use RK first (and then check full RE for the rest) for better performance
        if (aPos > 7) {
            QRegExp rx = fromAdBlockWildcard(filter.mid(aPos) + QLatin1Char('*'));
            // We pad the final r.e. with * so we can check for an exact match
            stringFiltersMatcher.addWildedString(filter.mid(0, aPos), rx);
        } else {
            QRegExp rx = fromAdBlockWildcard(filter);
            reFilters.append(rx);
        }
    } else {
        // Fast path
        stringFiltersMatcher.addString(filter);
    }

}

bool FilterSet::isUrlMatched(const QString &url)
{
    if (stringFiltersMatcher.isMatched(url)) {
        return true;
    }

    for (int c = 0; c < reFilters.size(); ++c) {
        if (url.contains(reFilters[c])) {
            return true;
        }
    }

    return false;
}

QString FilterSet::urlMatchedBy(const QString &url)
{
    QString by;

    if (stringFiltersMatcher.isMatched(url, &by)) {
        return by;
    }

    for (int c = 0; c < reFilters.size(); ++c) {
        if (url.contains(reFilters[c])) {
            by = reFilters[c].pattern();
            break;
        }
    }

    return by;
}

void FilterSet::clear()
{
    reFilters.clear();
    stringFiltersMatcher.clear();
}

void StringsMatcher::addString(const QString &pattern)
{
    if (pattern.length() < 8) {
        // handle short string differently
        shortStringFilters.append(pattern);
    } else {
        // use modified Rabin-Karp's algorithm with 8-length string hash
        // i.e. store hash of first 8 chars in the HashMap for fast look-up
        stringFilters.append(pattern);
        int ind = stringFilters.size() - 1;
        int current = 0;

        // compute hash using rolling hash
        // hash for string: x0,x1,x2...xn-1 will be:
        // (p^(n-1)*x0 + p^(n-2)*x1 + ... + p * xn-2 + xn-1) % q
        // where p and q some wisely-chosen integers
        /*for (int k = 0; k < 8; ++k)*/
        int len = pattern.length();
        for (int k = len - 8; k < len; ++k) {
            current = (current * HASH_P + pattern[k].unicode()) % HASH_Q;
        }

        // insert computed hash value into HashMap
        WTF::HashMap<int, QVector<int> >::iterator it = stringFiltersHash.find(current + 1);
        if (it == stringFiltersHash.end()) {
            QVector<int> list;
            list.append(ind);
            stringFiltersHash.add(current + 1, list);
            fastLookUp.setBit(current);
        } else {
            it->second.append(ind);
        }
    }
}

void StringsMatcher::addWildedString(const QString &prefix, const QRegExp &rx)
{
    rePrefixes.append(prefix);
    reFilters.append(rx);
    int index = -rePrefixes.size();

    int current = 0;
    for (int k = 0; k < 8; ++k) {
        current = (current * HASH_P + prefix[k].unicode()) % HASH_Q;
    }

    // insert computed hash value into HashMap
    WTF::HashMap<int, QVector<int> >::iterator it = stringFiltersHash.find(current + 1);
    if (it == stringFiltersHash.end()) {
        QVector<int> list;
        list.append(index);
        stringFiltersHash.add(current + 1, list);
        fastLookUp.setBit(current);
    } else {
        it->second.append(index);
    }
}

bool StringsMatcher::isMatched(const QString &str, QString *by) const
{
    // check short strings first
    for (int i = 0; i < shortStringFilters.size(); ++i) {
        if (str.contains(shortStringFilters[i])) {
            if (by != nullptr) {
                *by = shortStringFilters[i];
            }
            return true;
        }
    }

    int len = str.length();
    int k;

    int current = 0;
    int next = 0;
    // compute hash for first 8 characters
    for (k = 0; k < 8 && k < len; ++k) {
        current = (current * HASH_P + str[k].unicode()) % HASH_Q;
    }

    WTF::HashMap<int, QVector<int> >::const_iterator hashEnd = stringFiltersHash.end();
    // main Rabin-Karp's algorithm loop
    for (k = 7; k < len; ++k, current = next) {
        // roll the hash if not at the end
        // (calculate hash for the next iteration)
        if (k + 1 < len) {
            next = (HASH_P * ((current + HASH_Q - ((HASH_MOD * str[k - 7].unicode()) % HASH_Q)) % HASH_Q) + str[k + 1].unicode()) % HASH_Q;
        }

        if (!fastLookUp.testBit(current)) {
            continue;
        }

        // look-up the hash in the HashMap and check all strings
        WTF::HashMap<int, QVector<int> >::const_iterator it = stringFiltersHash.find(current + 1);

        // check possible strings
        if (it != hashEnd) {
            for (int j = 0; j < it->second.size(); ++j) {
                int index = it->second[j];
                // check if we got simple string or REs prefix
                if (index >= 0) {
                    int flen = stringFilters[index].length();
                    if (k - flen + 1 >= 0 && stringFilters[index] == str.midRef(k - flen + 1, flen)) {
                        if (by != nullptr) {
                            *by = stringFilters[index];
                        }
                        return true;
                    }
                } else {
                    index = -index - 1;
                    int flen = rePrefixes[index].length();
                    if (k - 8 + flen < len && rePrefixes[index] == str.midRef(k - 7, flen)) {
                        int remStart = k - 7 + flen;
                        QString remainder = QString::fromRawData(str.unicode() + remStart,
                                            str.length() - remStart);
                        if (reFilters[index].exactMatch(remainder)) {
                            if (by != nullptr) {
                                *by = rePrefixes[index] + reFilters[index].pattern();
                            }
                            return true;
                        }
                    }
                }
            }
        }
    }

    return false;
}

void StringsMatcher::clear()
{
    stringFilters.clear();
    shortStringFilters.clear();
    reFilters.clear();
    rePrefixes.clear();
    stringFiltersHash.clear();
    fastLookUp.resize(HASH_Q);
    fastLookUp.fill(0, 0, HASH_Q);
}

}