diff --git a/src/3rdparty/libpsl/qt_attribution.json b/src/3rdparty/libpsl/qt_attribution.json new file mode 100644 index 0000000000..f2eddec00d --- /dev/null +++ b/src/3rdparty/libpsl/qt_attribution.json @@ -0,0 +1,15 @@ +{ + "Id": "libpsl", + "Name": "libpsl - C library to handle the Public Suffix List", + "QDocModule": "qtnetwork", + "Description": "Libpsl allows checking domains against the Public Suffix List.", + "Files": "src/lookup_string_in_fixed_set.c src/psl-make-dafsa", + "QtUsage": "Used to compress the embedded copy of publicsuffix list and +to lookup entries in it.", + "Homepage": "https://github.com/rockdaboot/libpsl", + "Version": "664f3dc85259ec65e30248a61fa1c45b7b0e4c3f", + "License": "BSD 3-clause \"New\" or \"Revised\" License", + "LicenseFile": "src/LICENSE.chromium", + "LicenseId": "BSD-3-Clause", + "Copyright": "Copyright 2014-2016 The Chromium Authors. All rights reserved." +} diff --git a/src/3rdparty/libpsl/src/LICENSE.chromium b/src/3rdparty/libpsl/src/LICENSE.chromium new file mode 100644 index 0000000000..e29f4ffbe4 --- /dev/null +++ b/src/3rdparty/libpsl/src/LICENSE.chromium @@ -0,0 +1,30 @@ +* The following License is for the source code files + psl-make-dafsa and lookup_string_in_fixed_set.c. + +// Copyright 2015 The Chromium Authors. All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/src/3rdparty/libpsl/src/lookup_string_in_fixed_set.c b/src/3rdparty/libpsl/src/lookup_string_in_fixed_set.c new file mode 100644 index 0000000000..57c2e4ea12 --- /dev/null +++ b/src/3rdparty/libpsl/src/lookup_string_in_fixed_set.c @@ -0,0 +1,273 @@ +/* Copyright 2015-2016 The Chromium Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE.chromium file. + * + * Converted to C89 2015 by Tim Rühsen + */ + +#include + +#define CHECK_LT(a, b) if ((a) >= b) return 0 + +static const char multibyte_length_table[16] = { + 0, 0, 0, 0, /* 0x00-0x3F */ + 0, 0, 0, 0, /* 0x40-0x7F */ + 0, 0, 0, 0, /* 0x80-0xBF */ + 2, 2, 3, 4, /* 0xC0-0xFF */ +}; + + +/* + * Get length of multibyte character sequence starting at a given byte. + * Returns zero if the byte is not a valid leading byte in UTF-8. + */ +static int GetMultibyteLength(char c) { + return multibyte_length_table[((unsigned char)c) >> 4]; +} + +/* + * Moves pointers one byte forward. + */ +static void NextPos(const unsigned char** pos, + const char** key, + const char** multibyte_start) +{ + ++*pos; + if (*multibyte_start) { + /* Advance key to next byte in multibyte sequence. */ + ++*key; + /* Reset multibyte_start if last byte in multibyte sequence was consumed. */ + if (*key - *multibyte_start == GetMultibyteLength(**multibyte_start)) + *multibyte_start = 0; + } else { + if (GetMultibyteLength(**key)) { + /* Multibyte prefix was matched in the dafsa, start matching multibyte + * content in next round. */ + *multibyte_start = *key; + } else { + /* Advance key as a single byte character was matched. */ + ++*key; + } + } +} + +/* + * Read next offset from pos. + * Returns true if an offset could be read, false otherwise. + */ + +static int GetNextOffset(const unsigned char** pos, + const unsigned char* end, + const unsigned char** offset) +{ + size_t bytes_consumed; + + if (*pos == end) + return 0; + + /* When reading an offset the byte array must always contain at least + * three more bytes to consume. First the offset to read, then a node + * to skip over and finally a destination node. No object can be smaller + * than one byte. */ + CHECK_LT(*pos + 2, end); + switch (**pos & 0x60) { + case 0x60: /* Read three byte offset */ + *offset += (((*pos)[0] & 0x1F) << 16) | ((*pos)[1] << 8) | (*pos)[2]; + bytes_consumed = 3; + break; + case 0x40: /* Read two byte offset */ + *offset += (((*pos)[0] & 0x1F) << 8) | (*pos)[1]; + bytes_consumed = 2; + break; + default: + *offset += (*pos)[0] & 0x3F; + bytes_consumed = 1; + } + if ((**pos & 0x80) != 0) { + *pos = end; + } else { + *pos += bytes_consumed; + } + return 1; +} + +/* + * Check if byte at offset is last in label. + */ + +static int IsEOL(const unsigned char* offset, const unsigned char* end) +{ + CHECK_LT(offset, end); + return(*offset & 0x80) != 0; +} + +/* + * Check if byte at offset matches first character in key. + * This version assumes a range check was already performed by the caller. + */ + +static int IsMatchUnchecked(const unsigned char matcher, + const char* key, + const char* multibyte_start) +{ + if (multibyte_start) { + /* Multibyte matching mode. */ + if (multibyte_start == key) { + /* Match leading byte, which will also match the sequence length. */ + return (matcher ^ 0x80) == (const unsigned char)*key; + } else { + /* Match following bytes. */ + return (matcher ^ 0xC0) == (const unsigned char)*key; + } + } + /* If key points at a leading byte in a multibyte sequence, but we are not yet + * in multibyte mode, then the dafsa should contain a special byte to indicate + * a mode switch. */ + if (GetMultibyteLength(*key)) { + return matcher == 0x1F; + } + /* Normal matching of a single byte character. */ + return matcher == (const unsigned char)*key; +} + +/* + * Check if byte at offset matches first character in key. + * This version matches characters not last in label. + */ + +static int IsMatch(const unsigned char* offset, + const unsigned char* end, + const char* key, + const char* multibyte_start) +{ + CHECK_LT(offset, end); + return IsMatchUnchecked(*offset, key, multibyte_start); +} + +/* + * Check if byte at offset matches first character in key. + * This version matches characters last in label. + */ + +static int IsEndCharMatch(const unsigned char* offset, + const unsigned char* end, + const char* key, + const char* multibyte_start) +{ + CHECK_LT(offset, end); + return IsMatchUnchecked(*offset ^ 0x80, key, multibyte_start); +} + +/* + * Read return value at offset. + * Returns true if a return value could be read, false otherwise. + */ + +static int GetReturnValue(const unsigned char* offset, + const unsigned char* end, + const char* multibyte_start, + int* return_value) +{ + CHECK_LT(offset, end); + if (!multibyte_start && (*offset & 0xE0) == 0x80) { + *return_value = *offset & 0x0F; + return 1; + } + return 0; +} + +/* + * Looks up the string |key| with length |key_length| in a fixed set of + * strings. The set of strings must be known at compile time. It is converted to + * a graph structure named a DAFSA (Deterministic Acyclic Finite State + * Automaton) by the script psl-make-dafsa during compilation. This permits + * efficient (in time and space) lookup. The graph generated by psl-make-dafsa + * takes the form of a constant byte array which should be supplied via the + * |graph| and |length| parameters. The return value is kDafsaNotFound, + * kDafsaFound, or a bitmap consisting of one or more of kDafsaExceptionRule, + * kDafsaWildcardRule and kDafsaPrivateRule ORed together. + * + * Lookup a domain key in a byte array generated by psl-make-dafsa. + */ + +/* prototype to skip warning with -Wmissing-prototypes */ +int LookupStringInFixedSet(const unsigned char*, size_t,const char*, size_t); + +int LookupStringInFixedSet(const unsigned char* graph, + size_t length, + const char* key, + size_t key_length) +{ + const unsigned char* pos = graph; + const unsigned char* end = graph + length; + const unsigned char* offset = pos; + const char* key_end = key + key_length; + const char* multibyte_start = 0; + + while (GetNextOffset(&pos, end, &offset)) { + /*char + end_char offsets + * char + return value + * char end_char offsets + * char return value + * end_char offsets + * return_value + */ + int did_consume = 0; + + if (key != key_end && !IsEOL(offset, end)) { + /* Leading is not a match. Don't dive into this child */ + if (!IsMatch(offset, end, key, multibyte_start)) + continue; + did_consume = 1; + NextPos(&offset, &key, &multibyte_start); + /* Possible matches at this point: + * + end_char offsets + * + return value + * end_char offsets + * return value + */ + + /* Remove all remaining nodes possible */ + while (!IsEOL(offset, end) && key != key_end) { + if (!IsMatch(offset, end, key, multibyte_start)) + return -1; + NextPos(&offset, &key, &multibyte_start); + } + } + /* Possible matches at this point: + * end_char offsets + * return_value + * If one or more elements were consumed, a failure + * to match is terminal. Otherwise, try the next node. + */ + if (key == key_end) { + int return_value; + + if (GetReturnValue(offset, end, multibyte_start, &return_value)) + return return_value; + /* The DAFSA guarantees that if the first char is a match, all + * remaining char elements MUST match if the key is truly present. + */ + if (did_consume) + return -1; + continue; + } + if (!IsEndCharMatch(offset, end, key, multibyte_start)) { + if (did_consume) + return -1; /* Unexpected */ + continue; + } + NextPos(&offset, &key, &multibyte_start); + pos = offset; /* Dive into child */ + } + + return -1; /* No match */ +} + +/* prototype to skip warning with -Wmissing-prototypes */ +int GetUtfMode(const unsigned char *graph, size_t length); + +int GetUtfMode(const unsigned char *graph, size_t length) +{ + return length > 0 && graph[length - 1] < 0x80; +} diff --git a/src/3rdparty/libpsl/src/psl-make-dafsa b/src/3rdparty/libpsl/src/psl-make-dafsa new file mode 100755 index 0000000000..2bfd8796fa --- /dev/null +++ b/src/3rdparty/libpsl/src/psl-make-dafsa @@ -0,0 +1,692 @@ +#!/usr/bin/env python +# Copyright 2014 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE.chromium file. + +""" +A Deterministic acyclic finite state automaton (DAFSA) is a compact +representation of an unordered word list (dictionary). + +https://en.wikipedia.org/wiki/Deterministic_acyclic_finite_state_automaton + +This python program converts a list of strings to a byte array in C++. +This python program fetches strings and return values from a gperf file +and generates a C++ file with a byte array representing graph that can be +used as a memory efficient replacement for the perfect hash table. + +The input strings must consist of printable 7-bit ASCII characters or UTF-8 +multibyte sequences. Control characters in the range [0x00-0x1F] are not +allowed. The return values must be one digit integers. . + +In this program a DAFSA is a diamond shaped graph starting at a common +source node and ending at a common sink node. All internal nodes contain +a label and each word is represented by the labels in one path from +the source node to the sink node. + +The following python represention is used for nodes: + + Source node: [ children ] + Internal node: (label, [ children ]) + Sink node: None + +The graph is first compressed by prefixes like a trie. In the next step +suffixes are compressed so that the graph gets diamond shaped. Finally +one to one linked nodes are replaced by nodes with the labels joined. + +The order of the operations is crucial since lookups will be performed +starting from the source with no backtracking. Thus a node must have at +most one child with a label starting by the same character. The output +is also arranged so that all jumps are to increasing addresses, thus forward +in memory. + +The generated output has suffix free decoding so that the sign of leading +bits in a link (a reference to a child node) indicate if it has a size of one, +two or three bytes and if it is the last outgoing link from the actual node. +A node label is terminated by a byte with the leading bit set. + +The generated byte array can described by the following BNF: + + ::= < 8-bit value in range [0x00-0xFF] > + + ::= < byte in range [0x1F-0x7F] > + ::= < char + 0x80, byte in range [0x9F-0xFF] > + ::= < value + 0x80, byte in range [0x80-0x8F] > + + ::= < byte in range [0x00-0x3F] > + ::= < byte in range [0x40-0x5F] > + ::= < byte in range [0x60-0x7F] > + + ::= < byte in range [0x80-0xBF] > + ::= < byte in range [0xC0-0xDF] > + ::= < byte in range [0xE0-0xFF] > + + ::= + +