new function psl_load_fp()

This commit is contained in:
Tim Ruehsen 2014-03-22 14:28:55 +01:00
parent de7d394223
commit a707b267c9
2 changed files with 45 additions and 34 deletions

View File

@ -27,6 +27,8 @@
#ifndef _LIBPSL_LIBPSL_H #ifndef _LIBPSL_LIBPSL_H
#define _LIBPSL_LIBPSL_H #define _LIBPSL_LIBPSL_H
#include <stdio.h>
// Let C++ include C headers // Let C++ include C headers
#ifdef __cplusplus #ifdef __cplusplus
# define PSL_BEGIN_DECLS extern "C" { # define PSL_BEGIN_DECLS extern "C" {
@ -52,6 +54,8 @@ void
psl_free(psl_ctx_t **psl); psl_free(psl_ctx_t **psl);
psl_ctx_t * psl_ctx_t *
psl_load_file(const char *fname); psl_load_file(const char *fname);
psl_ctx_t *
psl_load_fp(FILE *fp);
int int
psl_is_public(const psl_ctx_t *psl, const char *domain); psl_is_public(const psl_ctx_t *psl, const char *domain);

View File

@ -265,58 +265,65 @@ int psl_is_public(const psl_ctx_t *psl, const char *domain)
} }
psl_ctx_t *psl_load_file(const char *fname) psl_ctx_t *psl_load_file(const char *fname)
{
FILE *fp;
psl_ctx_t *psl = NULL;
if ((fp = fopen(fname, "r"))) {
psl = psl_load_fp(fp);
fclose(fp);
}
return psl;
}
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;
FILE *fp;
int nsuffixes = 0; int nsuffixes = 0;
char buf[256], *linep, *p; char buf[256], *linep, *p;
if (!fp)
return NULL;
if (!(psl = calloc(1, sizeof(psl_ctx_t)))) if (!(psl = calloc(1, sizeof(psl_ctx_t))))
return NULL; return NULL;
if ((fp = fopen(fname, "r"))) { // as of 02.11.2012, the list at http://publicsuffix.org/list/ contains ~6000 rules and 40 exceptions.
// as of 02.11.2012, the list at http://publicsuffix.org/list/ contains ~6000 rules and 40 exceptions. // as of 19.02.2014, the list at http://publicsuffix.org/list/ contains ~6500 rules and 19 exceptions.
// as of 19.02.2014, the list at http://publicsuffix.org/list/ contains ~6500 rules and 19 exceptions. psl->suffixes = _vector_alloc(8*1024, _suffix_compare);
psl->suffixes = _vector_alloc(8*1024, _suffix_compare); psl->suffix_exceptions = _vector_alloc(64, _suffix_compare);
psl->suffix_exceptions = _vector_alloc(64, _suffix_compare);
while ((linep = fgets(buf, sizeof(buf), fp))) { while ((linep = fgets(buf, sizeof(buf), fp))) {
while (isspace(*linep)) linep++; // ignore leading whitespace while (isspace(*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] == '/')
continue; // skip comments continue; // skip comments
// parse suffix rule // parse suffix rule
for (p = linep; *linep && !isspace(*linep);) linep++; for (p = linep; *linep && !isspace(*linep);) linep++;
*linep = 0; *linep = 0;
if (*p == '!') { if (*p == '!') {
// add to exceptions // add to exceptions
_suffix_init(&suffix, p + 1, linep - p - 1); _suffix_init(&suffix, p + 1, linep - p - 1);
suffixp = _vector_get(psl->suffix_exceptions, _vector_add(psl->suffix_exceptions, &suffix)); suffixp = _vector_get(psl->suffix_exceptions, _vector_add(psl->suffix_exceptions, &suffix));
} else { } else {
_suffix_init(&suffix, p, linep - p); _suffix_init(&suffix, p, linep - p);
suffixp = _vector_get(psl->suffixes, _vector_add(psl->suffixes, &suffix)); suffixp = _vector_get(psl->suffixes, _vector_add(psl->suffixes, &suffix));
}
if (suffixp)
suffixp->label = suffixp->label_buf; // set label to changed address
nsuffixes++;;
} }
fclose(fp); if (suffixp)
suffixp->label = suffixp->label_buf; // set label to changed address
_vector_sort(psl->suffix_exceptions); nsuffixes++;;
_vector_sort(psl->suffixes);
} else {
free(psl);
return NULL;
} }
_vector_sort(psl->suffix_exceptions);
_vector_sort(psl->suffixes);
return psl; return psl;
} }