aboutsummaryrefslogtreecommitdiff
path: root/plugins/HostlistFilter/filterlist.cpp
blob: 8faa4ef379281e1309ebab2ffe0f289bf6f330d1 (plain)
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
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://library.iserlohn-fortress.net/aqua/smolbote.git
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "filterlist.h"
#include <QIODevice>
#include <QTextStream>

inline void Hostlist::parseRule(const QString &line, QStringList &blocked, QHash<QString, QString> &redirected)
{
    if(line.isEmpty() || line.at(0) == '#')
        return;

    auto parts = line.trimmed().split(' ');
    if(parts.size() < 2)
        return;

    if(parts[0] == "0.0.0.0") {
        parts.pop_front();
        blocked << parts;
        return;
    }

    for(int i = 1; i < parts.size(); ++i) {
        redirected.insert(parts[i], parts[0]);
    }
}

#ifdef FUZZER
extern "C" int LLVMFuzzerTestOneInput(const char *Data, long long Size)
{
    QStringList b;
    QHash<QString, QString> r;
    Hostlist::parseRule(QString::fromLatin1(Data, static_cast<int>(Size)), b, r);
    b.clear();
    r.clear();
    return 0;
}
#endif

bool Hostlist::Filterlist::load(QIODevice &from)
{
    if(!from.isReadable() || !from.isTextModeEnabled()) {
        return false;
    }

    while(from.bytesAvailable() > 0) {
        const auto line = from.readLine(512);
        parseRule(line, blocked, redirected);
    }
    return true;
}