Remove leading underscores from symbol definitions, since they are

reserved.
This commit is contained in:
Alessandro Vesely 2017-10-27 18:13:29 +02:00
parent 16bf63a6bf
commit 84e69a96fe
2 changed files with 139 additions and 139 deletions

View File

@ -8,15 +8,15 @@
#include <stddef.h> #include <stddef.h>
#if defined(__GNUC__) && defined(__GNUC_MINOR__) #if defined(__GNUC__) && defined(__GNUC_MINOR__)
# define _GCC_VERSION_AT_LEAST(major, minor) ((__GNUC__ > (major)) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) # define GCC_VERSION_AT_LEAST(major, minor) ((__GNUC__ > (major)) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
#else #else
# define _GCC_VERSION_AT_LEAST(major, minor) 0 # define GCC_VERSION_AT_LEAST(major, minor) 0
#endif #endif
#if _GCC_VERSION_AT_LEAST(4,0) #if GCC_VERSION_AT_LEAST(4,0)
# define _HIDDEN __attribute__ ((visibility ("hidden"))) # define HIDDEN __attribute__ ((visibility ("hidden")))
#else #else
# define _HIDDEN # define HIDDEN
#endif #endif
#define CHECK_LT(a, b) if ((a) >= b) return 0 #define CHECK_LT(a, b) if ((a) >= b) return 0
@ -203,9 +203,9 @@ static int GetReturnValue(const unsigned char* offset,
*/ */
/* prototype to skip warning with -Wmissing-prototypes */ /* prototype to skip warning with -Wmissing-prototypes */
int _HIDDEN LookupStringInFixedSet(const unsigned char*, size_t,const char*, size_t); int HIDDEN LookupStringInFixedSet(const unsigned char*, size_t,const char*, size_t);
int _HIDDEN LookupStringInFixedSet(const unsigned char* graph, int HIDDEN LookupStringInFixedSet(const unsigned char* graph,
size_t length, size_t length,
const char* key, const char* key,
size_t key_length) size_t key_length)
@ -277,9 +277,9 @@ int _HIDDEN LookupStringInFixedSet(const unsigned char* graph,
} }
/* prototype to skip warning with -Wmissing-prototypes */ /* prototype to skip warning with -Wmissing-prototypes */
int _HIDDEN GetUtfMode(const unsigned char *graph, size_t length); int HIDDEN GetUtfMode(const unsigned char *graph, size_t length);
int _HIDDEN GetUtfMode(const unsigned char *graph, size_t length) int HIDDEN GetUtfMode(const unsigned char *graph, size_t length)
{ {
return length > 0 && graph[length - 1] < 0x80; return length > 0 && graph[length - 1] < 0x80;
} }

260
src/psl.c
View File

@ -33,15 +33,15 @@
#endif #endif
#if defined(__GNUC__) && defined(__GNUC_MINOR__) #if defined(__GNUC__) && defined(__GNUC_MINOR__)
# define _GCC_VERSION_AT_LEAST(major, minor) ((__GNUC__ > (major)) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) # define GCC_VERSION_AT_LEAST(major, minor) ((__GNUC__ > (major)) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
#else #else
# define _GCC_VERSION_AT_LEAST(major, minor) 0 # define GCC_VERSION_AT_LEAST(major, minor) 0
#endif #endif
#if _GCC_VERSION_AT_LEAST(2,95) #if GCC_VERSION_AT_LEAST(2,95)
# define _UNUSED __attribute__ ((unused)) # define UNUSED _attribute__ ((unused))
#else #else
# define _UNUSED # define UNUSED
#endif #endif
#if ENABLE_NLS != 0 #if ENABLE_NLS != 0
@ -125,11 +125,11 @@ static char *strndup(const char *s, size_t n)
#define countof(a) (sizeof(a)/sizeof(*(a))) #define countof(a) (sizeof(a)/sizeof(*(a)))
#define _PSL_FLAG_EXCEPTION (1<<0) #define PSL_FLAG_EXCEPTION (1<<0)
#define _PSL_FLAG_WILDCARD (1<<1) #define PSL_FLAG_WILDCARD (1<<1)
#define _PSL_FLAG_ICANN (1<<2) /* entry of ICANN section */ #define PSL_FLAG_ICANN (1<<2) /* entry of ICANN section */
#define _PSL_FLAG_PRIVATE (1<<3) /* entry of PRIVATE section */ #define PSL_FLAG_PRIVATE (1<<3) /* entry of PRIVATE section */
#define _PSL_FLAG_PLAIN (1<<4) /* just used for PSL syntax checking */ #define PSL_FLAG_PLAIN (1<<4) /* just used for PSL syntax checking */
typedef struct { typedef struct {
char char
@ -141,21 +141,21 @@ typedef struct {
unsigned char unsigned char
nlabels, /* number of labels */ nlabels, /* number of labels */
flags; flags;
} _psl_entry_t; } psl_entry_t;
/* stripped down version libmget vector routines */ /* stripped down version libmget vector routines */
typedef struct { typedef struct {
int int
(*cmp)(const _psl_entry_t **, const _psl_entry_t **); /* comparison function */ (*cmp)(const psl_entry_t **, const psl_entry_t **); /* comparison function */
_psl_entry_t psl_entry_t
**entry; /* pointer to array of pointers to elements */ **entry; /* pointer to array of pointers to elements */
int int
max, /* allocated elements */ max, /* allocated elements */
cur; /* number of elements in use */ cur; /* number of elements in use */
} _psl_vector_t; } psl_vector_t;
struct _psl_ctx_st { struct psl_ctx_st {
_psl_vector_t psl_vector_t
*suffixes; *suffixes;
unsigned char unsigned char
*dafsa; *dafsa;
@ -174,22 +174,22 @@ struct _psl_ctx_st {
/* references to these PSLs will result in lookups to built-in data */ /* references to these PSLs will result in lookups to built-in data */
static const psl_ctx_t static const psl_ctx_t
_builtin_psl; builtin_psl;
#ifdef PSL_DISTFILE #ifdef PSL_DISTFILE
static const char _psl_dist_filename[] = PSL_DISTFILE; static const char psl_dist_filename[] = PSL_DISTFILE;
#else #else
static const char _psl_dist_filename[] = ""; static const char psl_dist_filename[] = "";
#endif #endif
static _psl_vector_t *_vector_alloc(int max, int (*cmp)(const _psl_entry_t **, const _psl_entry_t **)) static psl_vector_t *vector_alloc(int max, int (*cmp)(const psl_entry_t **, const psl_entry_t **))
{ {
_psl_vector_t *v; psl_vector_t *v;
if (!(v = calloc(1, sizeof(_psl_vector_t)))) if (!(v = calloc(1, sizeof(psl_vector_t))))
return NULL; return NULL;
if (!(v->entry = malloc(max * sizeof(_psl_entry_t *)))) { if (!(v->entry = malloc(max * sizeof(psl_entry_t *)))) {
free(v); free(v);
return NULL; return NULL;
} }
@ -199,7 +199,7 @@ static _psl_vector_t *_vector_alloc(int max, int (*cmp)(const _psl_entry_t **, c
return v; return v;
} }
static void _vector_free(_psl_vector_t **v) static void vector_free(psl_vector_t **v)
{ {
if (v && *v) { if (v && *v) {
if ((*v)->entry) { if ((*v)->entry) {
@ -214,7 +214,7 @@ static void _vector_free(_psl_vector_t **v)
} }
} }
static _psl_entry_t *_vector_get(const _psl_vector_t *v, int pos) static psl_entry_t *vector_get(const psl_vector_t *v, int pos)
{ {
if (pos < 0 || !v || pos >= v->cur) return NULL; if (pos < 0 || !v || pos >= v->cur) return NULL;
@ -222,7 +222,7 @@ static _psl_entry_t *_vector_get(const _psl_vector_t *v, int pos)
} }
/* the entries must be sorted by */ /* the entries must be sorted by */
static int _vector_find(const _psl_vector_t *v, const _psl_entry_t *elem) static int vector_find(const psl_vector_t *v, const psl_entry_t *elem)
{ {
if (v) { if (v) {
int l, r, m; int l, r, m;
@ -231,7 +231,7 @@ static int _vector_find(const _psl_vector_t *v, const _psl_entry_t *elem)
/* binary search for element (exact match) */ /* binary search for element (exact match) */
for (l = 0, r = v->cur - 1; l <= r;) { for (l = 0, r = v->cur - 1; l <= r;) {
m = (l + r) / 2; m = (l + r) / 2;
if ((res = v->cmp(&elem, (const _psl_entry_t **)&(v->entry[m]))) > 0) l = m + 1; if ((res = v->cmp(&elem, (const psl_entry_t **)&(v->entry[m]))) > 0) l = m + 1;
else if (res < 0) r = m - 1; else if (res < 0) r = m - 1;
else return m; else return m;
} }
@ -240,18 +240,18 @@ static int _vector_find(const _psl_vector_t *v, const _psl_entry_t *elem)
return -1; /* not found */ return -1; /* not found */
} }
static int _vector_add(_psl_vector_t *v, const _psl_entry_t *elem) static int vector_add(psl_vector_t *v, const psl_entry_t *elem)
{ {
if (v) { if (v) {
void *elemp; void *elemp;
if (!(elemp = malloc(sizeof(_psl_entry_t)))) if (!(elemp = malloc(sizeof(psl_entry_t))))
return -1; return -1;
memcpy(elemp, elem, sizeof(_psl_entry_t)); memcpy(elemp, elem, sizeof(psl_entry_t));
if (v->max == v->cur) { if (v->max == v->cur) {
void *m = realloc(v->entry, (v->max *= 2) * sizeof(_psl_entry_t *)); void *m = realloc(v->entry, (v->max *= 2) * sizeof(psl_entry_t *));
if (m) if (m)
v->entry = m; v->entry = m;
@ -268,14 +268,14 @@ static int _vector_add(_psl_vector_t *v, const _psl_entry_t *elem)
return -1; return -1;
} }
static void _vector_sort(_psl_vector_t *v) static void vector_sort(psl_vector_t *v)
{ {
if (v && v->cmp) if (v && v->cmp)
qsort(v->entry, v->cur, sizeof(_psl_vector_t **), (int(*)(const void *, const void *))v->cmp); qsort(v->entry, v->cur, sizeof(psl_vector_t **), (int(*)(const void *, const void *))v->cmp);
} }
/* by this kind of sorting, we can easily see if a domain matches or not */ /* by this kind of sorting, we can easily see if a domain matches or not */
static int _suffix_compare(const _psl_entry_t *s1, const _psl_entry_t *s2) static int suffix_compare(const psl_entry_t *s1, const psl_entry_t *s2)
{ {
int n; int n;
@ -289,12 +289,12 @@ static int _suffix_compare(const _psl_entry_t *s1, const _psl_entry_t *s2)
} }
/* needed to sort array of pointers, given to qsort() */ /* needed to sort array of pointers, given to qsort() */
static int _suffix_compare_array(const _psl_entry_t **s1, const _psl_entry_t **s2) static int suffix_compare_array(const psl_entry_t **s1, const psl_entry_t **s2)
{ {
return _suffix_compare(*s1, *s2); return suffix_compare(*s1, *s2);
} }
static int _suffix_init(_psl_entry_t *suffix, const char *rule, size_t length) static int suffix_init(psl_entry_t *suffix, const char *rule, size_t length)
{ {
const char *src; const char *src;
char *dst; char *dst;
@ -506,7 +506,7 @@ static enum punycode_status punycode_encode(
return punycode_success; return punycode_success;
} }
static ssize_t _utf8_to_utf32(const char *in, size_t inlen, punycode_uint *out, size_t outlen) static ssize_t utf8_to_utf32(const char *in, size_t inlen, punycode_uint *out, size_t outlen)
{ {
size_t n = 0; size_t n = 0;
const unsigned char *s = (void *)in; const unsigned char *s = (void *)in;
@ -547,7 +547,7 @@ static ssize_t _utf8_to_utf32(const char *in, size_t inlen, punycode_uint *out,
return n; return n;
} }
static int _mem_is_ascii(const char *s, size_t n) static int mem_is_ascii(const char *s, size_t n)
{ {
while (n--) while (n--)
if (*((unsigned char *)s++) >= 128) if (*((unsigned char *)s++) >= 128)
@ -556,7 +556,7 @@ static int _mem_is_ascii(const char *s, size_t n)
return 1; return 1;
} }
static int _domain_to_punycode(const char *domain, char *out, size_t outsize) static int domain_to_punycode(const char *domain, char *out, size_t outsize)
{ {
size_t outlen = 0, labellen; size_t outlen = 0, labellen;
punycode_uint input[256]; punycode_uint input[256];
@ -567,7 +567,7 @@ static int _domain_to_punycode(const char *domain, char *out, size_t outsize)
labellen = e ? (size_t) (e - label) : strlen(label); labellen = e ? (size_t) (e - label) : strlen(label);
/* printf("s=%s inlen=%zd\n", label, labellen); */ /* printf("s=%s inlen=%zd\n", label, labellen); */
if (_mem_is_ascii(label, labellen)) { if (mem_is_ascii(label, labellen)) {
if (outlen + labellen + (e != NULL) >= outsize) if (outlen + labellen + (e != NULL) >= outsize)
return 1; return 1;
@ -580,7 +580,7 @@ static int _domain_to_punycode(const char *domain, char *out, size_t outsize)
if (outlen + labellen + (e != NULL) + 4 >= outsize) if (outlen + labellen + (e != NULL) + 4 >= outsize)
return 1; return 1;
if ((inputlen = _utf8_to_utf32(label, labellen, input, countof(input))) < 0) if ((inputlen = utf8_to_utf32(label, labellen, input, countof(input))) < 0)
return 1; return 1;
memcpy(out + outlen, "xn--", 4); memcpy(out + outlen, "xn--", 4);
@ -602,12 +602,12 @@ static int _domain_to_punycode(const char *domain, char *out, size_t outsize)
} }
#endif #endif
static int _isspace_ascii(const char c) static int isspace_ascii(const char c)
{ {
return c == ' ' || c == '\t' || c == '\r' || c == '\n'; return c == ' ' || c == '\t' || c == '\r' || c == '\n';
} }
static int _str_is_ascii(const char *s) static int str_is_ascii(const char *s)
{ {
while (*s && *((unsigned char *)s) < 128) s++; while (*s && *((unsigned char *)s) < 128) s++;
@ -625,7 +625,7 @@ static int _str_is_ascii(const char *s)
* [2] https://lists.gnu.org/archive/html/bug-wget/2015-06/msg00002.html * [2] https://lists.gnu.org/archive/html/bug-wget/2015-06/msg00002.html
* [3] https://curl.haxx.se/mail/lib-2015-06/0143.html * [3] https://curl.haxx.se/mail/lib-2015-06/0143.html
*/ */
static int _utf8_is_valid(const char *utf8) static int utf8_is_valid(const char *utf8)
{ {
const unsigned char *s = (const unsigned char *) utf8; const unsigned char *s = (const unsigned char *) utf8;
@ -652,9 +652,9 @@ static int _utf8_is_valid(const char *utf8)
} }
#endif #endif
typedef void *_psl_idna_t; typedef void *psl_idna_t;
static _psl_idna_t *_psl_idna_open(void) static psl_idna_t *psl_idna_open(void)
{ {
#if defined(WITH_LIBICU) #if defined(WITH_LIBICU)
UErrorCode status = 0; UErrorCode status = 0;
@ -663,7 +663,7 @@ static _psl_idna_t *_psl_idna_open(void)
return NULL; return NULL;
} }
static void _psl_idna_close(_psl_idna_t *idna _UNUSED) static void psl_idna_close(psl_idna_t *idna UNUSED)
{ {
#if defined(WITH_LIBICU) #if defined(WITH_LIBICU)
if (idna) if (idna)
@ -671,7 +671,7 @@ static void _psl_idna_close(_psl_idna_t *idna _UNUSED)
#endif #endif
} }
static int _psl_idna_toASCII(_psl_idna_t *idna _UNUSED, const char *utf8, char **ascii) static int psl_idna_toASCII(psl_idna_t *idna UNUSED, const char *utf8, char **ascii)
{ {
int ret = -1; int ret = -1;
@ -767,7 +767,7 @@ cleanup:
#elif defined(WITH_LIBIDN) #elif defined(WITH_LIBIDN)
int rc; int rc;
if (!_utf8_is_valid(utf8)) { if (!utf8_is_valid(utf8)) {
/* fprintf(_(stderr, "Invalid UTF-8 sequence not converted: '%s'\n"), utf8); */ /* fprintf(_(stderr, "Invalid UTF-8 sequence not converted: '%s'\n"), utf8); */
return -1; return -1;
} }
@ -781,7 +781,7 @@ cleanup:
#else #else
char lookupname[128]; char lookupname[128];
if (_domain_to_punycode(utf8, lookupname, sizeof(lookupname)) == 0) { if (domain_to_punycode(utf8, lookupname, sizeof(lookupname)) == 0) {
if (ascii) if (ascii)
if ((*ascii = strdup(lookupname))) if ((*ascii = strdup(lookupname)))
ret = 0; ret = 0;
@ -791,21 +791,21 @@ cleanup:
return ret; return ret;
} }
static void _add_punycode_if_needed(_psl_idna_t *idna, _psl_vector_t *v, _psl_entry_t *e) static void add_punycode_if_needed(psl_idna_t *idna, psl_vector_t *v, psl_entry_t *e)
{ {
char *lookupname; char *lookupname;
if (_str_is_ascii(e->label_buf)) if (str_is_ascii(e->label_buf))
return; return;
if (_psl_idna_toASCII(idna, e->label_buf, &lookupname) == 0) { if (psl_idna_toASCII(idna, e->label_buf, &lookupname) == 0) {
if (strcmp(e->label_buf, lookupname)) { if (strcmp(e->label_buf, lookupname)) {
_psl_entry_t suffix, *suffixp; psl_entry_t suffix, *suffixp;
/* fprintf(stderr, "toASCII '%s' -> '%s'\n", e->label_buf, lookupname); */ /* fprintf(stderr, "toASCII '%s' -> '%s'\n", e->label_buf, lookupname); */
if (_suffix_init(&suffix, lookupname, strlen(lookupname)) == 0) { if (suffix_init(&suffix, lookupname, strlen(lookupname)) == 0) {
suffix.flags = e->flags; suffix.flags = e->flags;
if ((suffixp = _vector_get(v, _vector_add(v, &suffix)))) if ((suffixp = vector_get(v, vector_add(v, &suffix))))
suffixp->label = suffixp->label_buf; /* set label to changed address */ suffixp->label = suffixp->label_buf; /* set label to changed address */
} }
} /* else ignore */ } /* else ignore */
@ -818,9 +818,9 @@ static void _add_punycode_if_needed(_psl_idna_t *idna, _psl_vector_t *v, _psl_en
int LookupStringInFixedSet(const unsigned char* graph, size_t length, const char* key, size_t key_length); int LookupStringInFixedSet(const unsigned char* graph, size_t length, const char* key, size_t key_length);
int GetUtfMode(const unsigned char *graph, size_t length); int GetUtfMode(const unsigned char *graph, size_t length);
static int _psl_is_public_suffix(const psl_ctx_t *psl, const char *domain, int type) static int psl_is_public_suffix(const psl_ctx_t *psl, const char *domain, int type)
{ {
_psl_entry_t suffix; psl_entry_t suffix;
const char *p; const char *p;
char *punycode = NULL; char *punycode = NULL;
int need_conversion = 0; int need_conversion = 0;
@ -845,18 +845,18 @@ static int _psl_is_public_suffix(const psl_ctx_t *psl, const char *domain, int t
return 1; return 1;
} }
if (psl->utf8 || psl == &_builtin_psl) if (psl->utf8 || psl == &builtin_psl)
need_conversion = 0; need_conversion = 0;
#if defined(WITH_LIBIDN) || defined(WITH_LIBIDN2) || defined(WITH_LIBICU) #if defined(WITH_LIBIDN) || defined(WITH_LIBIDN2) || defined(WITH_LIBICU)
if (psl == &_builtin_psl) if (psl == &builtin_psl)
need_conversion = 0; need_conversion = 0;
#endif #endif
if (need_conversion) { if (need_conversion) {
_psl_idna_t *idna = _psl_idna_open(); psl_idna_t *idna = psl_idna_open();
if (_psl_idna_toASCII(idna, domain, &punycode) == 0) { if (psl_idna_toASCII(idna, domain, &punycode) == 0) {
suffix.label = punycode; suffix.label = punycode;
suffix.length = strlen(punycode); suffix.length = strlen(punycode);
} else { } else {
@ -866,24 +866,24 @@ static int _psl_is_public_suffix(const psl_ctx_t *psl, const char *domain, int t
suffix.length = p - suffix.label; suffix.length = p - suffix.label;
} }
_psl_idna_close(idna); psl_idna_close(idna);
} else { } else {
suffix.label = domain; suffix.label = domain;
suffix.length = p - suffix.label; suffix.length = p - suffix.label;
} }
if (psl == &_builtin_psl || psl->dafsa) { if (psl == &builtin_psl || psl->dafsa) {
size_t dafsa_size = psl == &_builtin_psl ? sizeof(kDafsa) : psl->dafsa_size; size_t dafsa_size = psl == &builtin_psl ? sizeof(kDafsa) : psl->dafsa_size;
const unsigned char *dafsa = psl == &_builtin_psl ? kDafsa : psl->dafsa; const unsigned char *dafsa = psl == &builtin_psl ? kDafsa : psl->dafsa;
int rc = LookupStringInFixedSet(dafsa, dafsa_size, suffix.label, suffix.length); int rc = LookupStringInFixedSet(dafsa, dafsa_size, suffix.label, suffix.length);
if (rc != -1) { if (rc != -1) {
/* check for correct rule type */ /* check for correct rule type */
if (type == PSL_TYPE_ICANN && !(rc & _PSL_FLAG_ICANN)) if (type == PSL_TYPE_ICANN && !(rc & PSL_FLAG_ICANN))
goto suffix_no; goto suffix_no;
else if (type == PSL_TYPE_PRIVATE && !(rc & _PSL_FLAG_PRIVATE)) else if (type == PSL_TYPE_PRIVATE && !(rc & PSL_FLAG_PRIVATE))
goto suffix_no; goto suffix_no;
if (rc & _PSL_FLAG_EXCEPTION) if (rc & PSL_FLAG_EXCEPTION)
goto suffix_no; goto suffix_no;
/* wildcard *.foo.bar implicitly make foo.bar a public suffix */ /* wildcard *.foo.bar implicitly make foo.bar a public suffix */
@ -898,31 +898,31 @@ static int _psl_is_public_suffix(const psl_ctx_t *psl, const char *domain, int t
rc = LookupStringInFixedSet(dafsa, dafsa_size, suffix.label, suffix.length); rc = LookupStringInFixedSet(dafsa, dafsa_size, suffix.label, suffix.length);
if (rc != -1) { if (rc != -1) {
/* check for correct rule type */ /* check for correct rule type */
if (type == PSL_TYPE_ICANN && !(rc & _PSL_FLAG_ICANN)) if (type == PSL_TYPE_ICANN && !(rc & PSL_FLAG_ICANN))
goto suffix_no; goto suffix_no;
else if (type == PSL_TYPE_PRIVATE && !(rc & _PSL_FLAG_PRIVATE)) else if (type == PSL_TYPE_PRIVATE && !(rc & PSL_FLAG_PRIVATE))
goto suffix_no; goto suffix_no;
if (rc & _PSL_FLAG_WILDCARD) if (rc & PSL_FLAG_WILDCARD)
goto suffix_yes; goto suffix_yes;
} }
} }
} else { } else {
_psl_entry_t *rule = _vector_get(psl->suffixes, 0); psl_entry_t *rule = vector_get(psl->suffixes, 0);
if (!rule || rule->nlabels < suffix.nlabels - 1) if (!rule || rule->nlabels < suffix.nlabels - 1)
goto suffix_no; goto suffix_no;
rule = _vector_get(psl->suffixes, _vector_find(psl->suffixes, &suffix)); rule = vector_get(psl->suffixes, vector_find(psl->suffixes, &suffix));
if (rule) { if (rule) {
/* check for correct rule type */ /* check for correct rule type */
if (type == PSL_TYPE_ICANN && !(rule->flags & _PSL_FLAG_ICANN)) if (type == PSL_TYPE_ICANN && !(rule->flags & PSL_FLAG_ICANN))
goto suffix_no; goto suffix_no;
else if (type == PSL_TYPE_PRIVATE && !(rule->flags & _PSL_FLAG_PRIVATE)) else if (type == PSL_TYPE_PRIVATE && !(rule->flags & PSL_FLAG_PRIVATE))
goto suffix_no; goto suffix_no;
if (rule->flags & _PSL_FLAG_EXCEPTION) if (rule->flags & PSL_FLAG_EXCEPTION)
goto suffix_no; goto suffix_no;
/* wildcard *.foo.bar implicitly make foo.bar a public suffix */ /* wildcard *.foo.bar implicitly make foo.bar a public suffix */
@ -937,16 +937,16 @@ static int _psl_is_public_suffix(const psl_ctx_t *psl, const char *domain, int t
suffix.length = strlen(suffix.label); suffix.length = strlen(suffix.label);
suffix.nlabels--; suffix.nlabels--;
rule = _vector_get(psl->suffixes, (pos = _vector_find(psl->suffixes, &suffix))); rule = vector_get(psl->suffixes, (pos = vector_find(psl->suffixes, &suffix)));
if (rule) { if (rule) {
/* check for correct rule type */ /* check for correct rule type */
if (type == PSL_TYPE_ICANN && !(rule->flags & _PSL_FLAG_ICANN)) if (type == PSL_TYPE_ICANN && !(rule->flags & PSL_FLAG_ICANN))
goto suffix_no; goto suffix_no;
else if (type == PSL_TYPE_PRIVATE && !(rule->flags & _PSL_FLAG_PRIVATE)) else if (type == PSL_TYPE_PRIVATE && !(rule->flags & PSL_FLAG_PRIVATE))
goto suffix_no; goto suffix_no;
if (rule->flags & _PSL_FLAG_WILDCARD) if (rule->flags & PSL_FLAG_WILDCARD)
goto suffix_yes; goto suffix_yes;
} }
} }
@ -989,7 +989,7 @@ int psl_is_public_suffix(const psl_ctx_t *psl, const char *domain)
if (!psl || !domain) if (!psl || !domain)
return 1; return 1;
return _psl_is_public_suffix(psl, domain, PSL_TYPE_ANY); return psl_is_public_suffix(psl, domain, PSL_TYPE_ANY);
} }
/** /**
@ -1020,7 +1020,7 @@ int psl_is_public_suffix2(const psl_ctx_t *psl, const char *domain, int type)
if (!psl || !domain) if (!psl || !domain)
return 1; return 1;
return _psl_is_public_suffix(psl, domain, type); return psl_is_public_suffix(psl, domain, type);
} }
/** /**
@ -1053,7 +1053,7 @@ const char *psl_unregistrable_domain(const psl_ctx_t *psl, const char *domain)
* 'forgot.his.name' and 'name' are in the PSL while 'his.name' is not. * 'forgot.his.name' and 'name' are in the PSL while 'his.name' is not.
*/ */
while (!_psl_is_public_suffix(psl, domain, 0)) { while (!psl_is_public_suffix(psl, domain, 0)) {
if ((domain = strchr(domain, '.'))) if ((domain = strchr(domain, '.')))
domain++; domain++;
else else
@ -1095,7 +1095,7 @@ const char *psl_registrable_domain(const psl_ctx_t *psl, const char *domain)
* 'forgot.his.name' and 'name' are in the PSL while 'his.name' is not. * 'forgot.his.name' and 'name' are in the PSL while 'his.name' is not.
*/ */
while (!_psl_is_public_suffix(psl, domain, 0)) { while (!psl_is_public_suffix(psl, domain, 0)) {
if ((p = strchr(domain, '.'))) { if ((p = strchr(domain, '.'))) {
regdom = domain; regdom = domain;
domain = p + 1; domain = p + 1;
@ -1151,10 +1151,10 @@ psl_ctx_t *psl_load_file(const char *fname)
psl_ctx_t *psl_load_fp(FILE *fp) psl_ctx_t *psl_load_fp(FILE *fp)
{ {
psl_ctx_t *psl; psl_ctx_t *psl;
_psl_entry_t suffix, *suffixp; psl_entry_t suffix, *suffixp;
char buf[256], *linep, *p; char buf[256], *linep, *p;
int type = 0, is_dafsa; int type = 0, is_dafsa;
_psl_idna_t *idna; psl_idna_t *idna;
if (!fp) if (!fp)
return NULL; return NULL;
@ -1202,41 +1202,41 @@ psl_ctx_t *psl_load_fp(FILE *fp)
return psl; return psl;
} }
idna = _psl_idna_open(); idna = psl_idna_open();
/* /*
* as of 02.11.2012, the list at https://publicsuffix.org/list/ contains ~6000 rules and 40 exceptions. * as of 02.11.2012, the list at https://publicsuffix.org/list/ contains ~6000 rules and 40 exceptions.
* as of 19.02.2014, the list at https://publicsuffix.org/list/ contains ~6500 rules and 19 exceptions. * as of 19.02.2014, the list at https://publicsuffix.org/list/ contains ~6500 rules and 19 exceptions.
*/ */
psl->suffixes = _vector_alloc(8*1024, _suffix_compare_array); psl->suffixes = vector_alloc(8*1024, suffix_compare_array);
psl->utf8 = 1; /* we put UTF-8 and punycode rules in the lookup vector */ psl->utf8 = 1; /* we put UTF-8 and punycode rules in the lookup vector */
do { do {
while (_isspace_ascii(*linep)) linep++; /* ignore leading whitespace */ while (isspace_ascii(*linep)) linep++; /* ignore leading whitespace */
if (!*linep) continue; /* skip empty lines */ if (!*linep) continue; /* skip empty lines */
if (*linep == '/' && linep[1] == '/') { if (*linep == '/' && linep[1] == '/') {
if (!type) { if (!type) {
if (strstr(linep + 2, "===BEGIN ICANN DOMAINS===")) if (strstr(linep + 2, "===BEGIN ICANN DOMAINS==="))
type = _PSL_FLAG_ICANN; type = PSL_FLAG_ICANN;
else if (!type && strstr(linep + 2, "===BEGIN PRIVATE DOMAINS===")) else if (!type && strstr(linep + 2, "===BEGIN PRIVATE DOMAINS==="))
type = _PSL_FLAG_PRIVATE; type = PSL_FLAG_PRIVATE;
} }
else if (type == _PSL_FLAG_ICANN && strstr(linep + 2, "===END ICANN DOMAINS===")) else if (type == PSL_FLAG_ICANN && strstr(linep + 2, "===END ICANN DOMAINS==="))
type = 0; type = 0;
else if (type == _PSL_FLAG_PRIVATE && strstr(linep + 2, "===END PRIVATE DOMAINS===")) else if (type == PSL_FLAG_PRIVATE && strstr(linep + 2, "===END PRIVATE DOMAINS==="))
type = 0; type = 0;
continue; /* skip comments */ continue; /* skip comments */
} }
/* parse suffix rule */ /* parse suffix rule */
for (p = linep; *linep && !_isspace_ascii(*linep);) linep++; for (p = linep; *linep && !isspace_ascii(*linep);) linep++;
*linep = 0; *linep = 0;
if (*p == '!') { if (*p == '!') {
p++; p++;
suffix.flags = _PSL_FLAG_EXCEPTION | type; suffix.flags = PSL_FLAG_EXCEPTION | type;
psl->nexceptions++; psl->nexceptions++;
} else if (*p == '*') { } else if (*p == '*') {
if (*++p != '.') { if (*++p != '.') {
@ -1245,20 +1245,20 @@ psl_ctx_t *psl_load_fp(FILE *fp)
} }
p++; p++;
/* wildcard *.foo.bar implicitly make foo.bar a public suffix */ /* wildcard *.foo.bar implicitly make foo.bar a public suffix */
suffix.flags = _PSL_FLAG_WILDCARD | _PSL_FLAG_PLAIN | type; suffix.flags = PSL_FLAG_WILDCARD | PSL_FLAG_PLAIN | type;
psl->nwildcards++; psl->nwildcards++;
psl->nsuffixes++; psl->nsuffixes++;
} else { } else {
if (!strchr(p, '.')) if (!strchr(p, '.'))
continue; /* we do not need an explicit plain TLD rule, already covered by implicit '*' rule */ continue; /* we do not need an explicit plain TLD rule, already covered by implicit '*' rule */
suffix.flags = _PSL_FLAG_PLAIN | type; suffix.flags = PSL_FLAG_PLAIN | type;
psl->nsuffixes++; psl->nsuffixes++;
} }
if (_suffix_init(&suffix, p, linep - p) == 0) { if (suffix_init(&suffix, p, linep - p) == 0) {
int index; int index;
if ((index = _vector_find(psl->suffixes, &suffix)) >= 0) { if ((index = vector_find(psl->suffixes, &suffix)) >= 0) {
/* Found existing entry: /* Found existing entry:
* Combination of exception and plain rule is ambiguous * Combination of exception and plain rule is ambiguous
* !foo.bar * !foo.bar
@ -1271,23 +1271,23 @@ psl_ctx_t *psl_load_fp(FILE *fp)
* We do not check here, let's do it later. * We do not check here, let's do it later.
*/ */
suffixp = _vector_get(psl->suffixes, index); suffixp = vector_get(psl->suffixes, index);
suffixp->flags |= suffix.flags; suffixp->flags |= suffix.flags;
} else { } else {
/* New entry */ /* New entry */
suffixp = _vector_get(psl->suffixes, _vector_add(psl->suffixes, &suffix)); suffixp = vector_get(psl->suffixes, vector_add(psl->suffixes, &suffix));
} }
if (suffixp) { if (suffixp) {
suffixp->label = suffixp->label_buf; /* set label to changed address */ suffixp->label = suffixp->label_buf; /* set label to changed address */
_add_punycode_if_needed(idna, psl->suffixes, suffixp); add_punycode_if_needed(idna, psl->suffixes, suffixp);
} }
} }
} while ((linep = fgets(buf, sizeof(buf), fp))); } while ((linep = fgets(buf, sizeof(buf), fp)));
_vector_sort(psl->suffixes); vector_sort(psl->suffixes);
_psl_idna_close(idna); psl_idna_close(idna);
return psl; return psl;
@ -1307,8 +1307,8 @@ fail:
*/ */
void psl_free(psl_ctx_t *psl) void psl_free(psl_ctx_t *psl)
{ {
if (psl && psl != &_builtin_psl) { if (psl && psl != &builtin_psl) {
_vector_free(&psl->suffixes); vector_free(&psl->suffixes);
free(psl->dafsa); free(psl->dafsa);
free(psl); free(psl);
} }
@ -1333,7 +1333,7 @@ void psl_free(psl_ctx_t *psl)
const psl_ctx_t *psl_builtin(void) const psl_ctx_t *psl_builtin(void)
{ {
#if defined(BUILTIN_GENERATOR_LIBICU) || defined(BUILTIN_GENERATOR_LIBIDN2) || defined(BUILTIN_GENERATOR_LIBIDN) #if defined(BUILTIN_GENERATOR_LIBICU) || defined(BUILTIN_GENERATOR_LIBIDN2) || defined(BUILTIN_GENERATOR_LIBIDN)
return &_builtin_psl; return &builtin_psl;
#else #else
return NULL; return NULL;
#endif #endif
@ -1355,8 +1355,8 @@ const psl_ctx_t *psl_builtin(void)
*/ */
int psl_suffix_count(const psl_ctx_t *psl) int psl_suffix_count(const psl_ctx_t *psl)
{ {
if (psl == &_builtin_psl) if (psl == &builtin_psl)
return _psl_nsuffixes; return psl_nsuffixes;
else if (psl) else if (psl)
return psl->dafsa ? -1 : psl->nsuffixes; return psl->dafsa ? -1 : psl->nsuffixes;
else else
@ -1378,8 +1378,8 @@ int psl_suffix_count(const psl_ctx_t *psl)
*/ */
int psl_suffix_exception_count(const psl_ctx_t *psl) int psl_suffix_exception_count(const psl_ctx_t *psl)
{ {
if (psl == &_builtin_psl) if (psl == &builtin_psl)
return _psl_nexceptions; return psl_nexceptions;
else if (psl) else if (psl)
return psl->dafsa ? -1 : psl->nexceptions; return psl->dafsa ? -1 : psl->nexceptions;
else else
@ -1401,8 +1401,8 @@ int psl_suffix_exception_count(const psl_ctx_t *psl)
*/ */
int psl_suffix_wildcard_count(const psl_ctx_t *psl) int psl_suffix_wildcard_count(const psl_ctx_t *psl)
{ {
if (psl == &_builtin_psl) if (psl == &builtin_psl)
return _psl_nwildcards; return psl_nwildcards;
else if (psl) else if (psl)
return psl->dafsa ? -1 : psl->nwildcards; return psl->dafsa ? -1 : psl->nwildcards;
else else
@ -1422,7 +1422,7 @@ int psl_suffix_wildcard_count(const psl_ctx_t *psl)
*/ */
time_t psl_builtin_file_time(void) time_t psl_builtin_file_time(void)
{ {
return _psl_file_time; return psl_file_time;
} }
/** /**
@ -1439,7 +1439,7 @@ time_t psl_builtin_file_time(void)
*/ */
const char *psl_builtin_sha1sum(void) const char *psl_builtin_sha1sum(void)
{ {
return _psl_sha1_checksum; return psl_sha1_checksum;
} }
/** /**
@ -1455,7 +1455,7 @@ const char *psl_builtin_sha1sum(void)
*/ */
const char *psl_builtin_filename(void) const char *psl_builtin_filename(void)
{ {
return _psl_filename; return psl_filename;
} }
/** /**
@ -1475,7 +1475,7 @@ int psl_builtin_outdated(void)
{ {
struct stat st; struct stat st;
if (stat(_psl_filename, &st) == 0 && st.st_mtime > _psl_file_time) if (stat(psl_filename, &st) == 0 && st.st_mtime > psl_file_time)
return 1; return 1;
return 0; return 0;
@ -1495,7 +1495,7 @@ int psl_builtin_outdated(void)
*/ */
const char *psl_dist_filename(void) const char *psl_dist_filename(void)
{ {
return _psl_dist_filename; return psl_dist_filename;
} }
/** /**
@ -1552,7 +1552,7 @@ int psl_check_version_number(int version)
} }
/* return whether hostname is an IP address or not */ /* return whether hostname is an IP address or not */
static int _isip(const char *hostname) static int isip(const char *hostname)
{ {
struct in_addr addr; struct in_addr addr;
struct in6_addr addr6; struct in6_addr addr6;
@ -1599,7 +1599,7 @@ int psl_is_cookie_domain_acceptable(const psl_ctx_t *psl, const char *hostname,
if (!strcmp(hostname, cookie_domain)) if (!strcmp(hostname, cookie_domain))
return 1; /* an exact match is acceptable (and pretty common) */ return 1; /* an exact match is acceptable (and pretty common) */
if (_isip(hostname)) if (isip(hostname))
return 0; /* Hostname is an IP address and these must match fully (RFC 6265, 5.1.3) */ return 0; /* Hostname is an IP address and these must match fully (RFC 6265, 5.1.3) */
cookie_domain_length = strlen(cookie_domain); cookie_domain_length = strlen(cookie_domain);
@ -1662,7 +1662,7 @@ void psl_free_string(char *str)
* *
* Since: 0.4 * Since: 0.4
*/ */
psl_error_t psl_str_to_utf8lower(const char *str, const char *encoding _UNUSED, const char *locale _UNUSED, char **lower) psl_error_t psl_str_to_utf8lower(const char *str, const char *encoding UNUSED, const char *locale UNUSED, char **lower)
{ {
int ret = PSL_ERR_INVALID_ARG; int ret = PSL_ERR_INVALID_ARG;
@ -1670,7 +1670,7 @@ psl_error_t psl_str_to_utf8lower(const char *str, const char *encoding _UNUSED,
return PSL_ERR_INVALID_ARG; return PSL_ERR_INVALID_ARG;
/* shortcut to avoid costly conversion */ /* shortcut to avoid costly conversion */
if (_str_is_ascii(str)) { if (str_is_ascii(str)) {
if (lower) { if (lower) {
char *p, *tmp; char *p, *tmp;
@ -1835,12 +1835,12 @@ out:
} }
/* if file is newer than the builtin data, insert it reverse sorted by mtime */ /* if file is newer than the builtin data, insert it reverse sorted by mtime */
static int _insert_file(const char *fname, const char **psl_fname, time_t *psl_mtime, int n) static int insert_file(const char *fname, const char **psl_fname, time_t *psl_mtime, int n)
{ {
struct stat st; struct stat st;
int it; int it;
if (fname && *fname && stat(fname, &st) == 0 && st.st_mtime > _psl_file_time) { if (fname && *fname && stat(fname, &st) == 0 && st.st_mtime > psl_file_time) {
/* add file name and mtime to end of array */ /* add file name and mtime to end of array */
psl_fname[n] = fname; psl_fname[n] = fname;
psl_mtime[n++] = st.st_mtime; psl_mtime[n++] = st.st_mtime;
@ -1885,13 +1885,13 @@ psl_ctx_t *psl_latest(const char *fname)
psl_fname[0] = NULL; /* silence gcc 6.2 false warning */ psl_fname[0] = NULL; /* silence gcc 6.2 false warning */
/* create array of PSL files reverse sorted by mtime (latest first) */ /* create array of PSL files reverse sorted by mtime (latest first) */
ntimes = _insert_file(fname, psl_fname, psl_mtime, 0); ntimes = insert_file(fname, psl_fname, psl_mtime, 0);
ntimes = _insert_file(_psl_dist_filename, psl_fname, psl_mtime, ntimes); ntimes = insert_file(psl_dist_filename, psl_fname, psl_mtime, ntimes);
ntimes = _insert_file(_psl_filename, psl_fname, psl_mtime, ntimes); ntimes = insert_file(psl_filename, psl_fname, psl_mtime, ntimes);
/* load PSL data from the latest file, falling back to the second recent, ... */ /* load PSL data from the latest file, falling back to the second recent, ... */
for (psl = NULL, it = 0; it < ntimes; it++) { for (psl = NULL, it = 0; it < ntimes; it++) {
if (psl_mtime[it] > _psl_file_time) if (psl_mtime[it] > psl_file_time)
if ((psl = psl_load_file(psl_fname[it]))) if ((psl = psl_load_file(psl_fname[it])))
break; break;
} }