2014-03-22 20:35:56 +01:00
|
|
|
/*
|
|
|
|
* Copyright(c) 2014 Tim Ruehsen
|
|
|
|
*
|
|
|
|
* This file is part of libpsl.
|
|
|
|
*
|
|
|
|
* Libpsl is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Libpsl 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with libpsl. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Precompile Public Suffix List into
|
|
|
|
*
|
|
|
|
* Changelog
|
|
|
|
* 22.03.2014 Tim Ruehsen created
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2014-03-23 21:49:19 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2014-03-22 22:55:34 +01:00
|
|
|
//#ifdef WITH_LIBIDN2
|
|
|
|
# include <idn2.h>
|
|
|
|
//#endif
|
|
|
|
|
2014-03-22 20:35:56 +01:00
|
|
|
#include "psl.c"
|
|
|
|
|
2014-03-23 21:49:19 +01:00
|
|
|
static void _print_psl_entries(FILE *fpout, const _psl_vector_t *v, const char *varname)
|
2014-03-22 20:35:56 +01:00
|
|
|
{
|
|
|
|
int it;
|
|
|
|
|
2014-03-23 21:49:19 +01:00
|
|
|
fprintf(fpout, "// automatically generated by psl2c\n");
|
|
|
|
fprintf(fpout, "static _psl_entry_t %s[] = {\n", varname);
|
2014-03-22 20:35:56 +01:00
|
|
|
|
|
|
|
for (it = 0; it < v->cur; it++) {
|
|
|
|
_psl_entry_t *e = _vector_get(v, it);
|
|
|
|
|
2014-03-23 21:49:19 +01:00
|
|
|
fprintf(fpout, "\t{ \"%s\", NULL, %hd, %hhd, %hhd },\n",
|
2014-03-22 20:35:56 +01:00
|
|
|
e->label_buf, e->length, e->nlabels, e->wildcard);
|
2014-03-22 22:55:34 +01:00
|
|
|
}
|
|
|
|
|
2014-03-23 21:49:19 +01:00
|
|
|
fprintf(fpout, "};\n");
|
2014-03-22 22:55:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _str_needs_encoding(const char *s)
|
|
|
|
{
|
|
|
|
while (*s > 0) s++;
|
|
|
|
|
|
|
|
return !!*s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _add_punycode_if_needed(_psl_vector_t *v)
|
|
|
|
{
|
|
|
|
int it;
|
|
|
|
|
|
|
|
for (it = 0; it < v->cur; it++) {
|
|
|
|
_psl_entry_t *e = _vector_get(v, it);
|
|
|
|
|
|
|
|
if (_str_needs_encoding(e->label_buf)) {
|
|
|
|
_psl_entry_t suffix;
|
2014-03-22 22:19:20 +01:00
|
|
|
char *asc = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if ((rc = idn2_lookup_u8((uint8_t *)e->label_buf, (uint8_t **)&asc, 0)) == IDN2_OK) {
|
2014-03-22 22:57:10 +01:00
|
|
|
// fprintf(stderr, "idn2 '%s' -> '%s'\n", e->label_buf, asc);
|
2014-03-22 22:55:34 +01:00
|
|
|
_suffix_init(&suffix, asc, strlen(asc));
|
|
|
|
suffix.wildcard = e->wildcard;
|
|
|
|
_vector_add(v, &suffix);
|
2014-03-22 22:19:20 +01:00
|
|
|
} else
|
|
|
|
fprintf(stderr, "toASCII(%s) failed (%d): %s\n", e->label_buf, rc, idn2_strerror(rc));
|
|
|
|
}
|
2014-03-22 20:35:56 +01:00
|
|
|
}
|
|
|
|
|
2014-03-22 22:55:34 +01:00
|
|
|
_vector_sort(v);
|
2014-03-22 20:35:56 +01:00
|
|
|
}
|
|
|
|
|
2014-03-23 21:49:19 +01:00
|
|
|
int main(int argc, const char **argv)
|
2014-03-22 20:35:56 +01:00
|
|
|
{
|
2014-03-23 21:49:19 +01:00
|
|
|
FILE *fpout;
|
2014-03-22 20:35:56 +01:00
|
|
|
psl_ctx_t *psl;
|
2014-03-23 21:49:19 +01:00
|
|
|
int ret = 0;
|
2014-03-22 20:35:56 +01:00
|
|
|
|
2014-03-23 21:49:19 +01:00
|
|
|
if (argc != 3) {
|
|
|
|
fprintf(stderr, "Usage: psl2c <infile> <outfile>\n");
|
|
|
|
fprintf(stderr, " <infile> is the 'effective_tld_names.dat' (aka Public Suffix List)\n");
|
|
|
|
fprintf(stderr, " <outfile> is the the C filename to be generated from <infile>\n");
|
2014-03-22 20:35:56 +01:00
|
|
|
return 1;
|
2014-03-23 21:49:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(psl = psl_load_file(argv[1])))
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
if ((fpout = fopen(argv[2], "w"))) {
|
|
|
|
FILE *pp;
|
|
|
|
struct stat st;
|
|
|
|
char cmd[16 + strlen(argv[1])], checksum[64] = "";
|
2014-03-22 20:35:56 +01:00
|
|
|
|
2014-03-23 21:49:19 +01:00
|
|
|
_add_punycode_if_needed(psl->suffixes);
|
|
|
|
_add_punycode_if_needed(psl->suffix_exceptions);
|
2014-03-22 22:55:34 +01:00
|
|
|
|
2014-03-23 21:49:19 +01:00
|
|
|
_print_psl_entries(fpout, psl->suffixes, "suffixes");
|
|
|
|
_print_psl_entries(fpout, psl->suffix_exceptions, "suffix_exceptions");
|
|
|
|
|
|
|
|
snprintf(cmd, sizeof(cmd), "md5sum %s", argv[1]);
|
|
|
|
if ((pp = popen(cmd, "r"))) {
|
|
|
|
if (fscanf(pp, "%63[0-9a-zA-Z]", checksum) < 1)
|
|
|
|
*checksum = 0;
|
|
|
|
pclose(pp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stat(argv[1], &st) != 0)
|
|
|
|
st.st_mtime = 0;
|
|
|
|
fprintf(fpout, "static time_t _psl_file_time = %lu;\n", st.st_mtime);
|
|
|
|
fprintf(fpout, "static time_t _psl_compile_time = %lu;\n", time(NULL));
|
|
|
|
fprintf(fpout, "static char _psl_md5_checksum[] = \"%s\";\n", checksum);
|
|
|
|
|
|
|
|
if (fclose(fpout) != 0)
|
|
|
|
ret = 4;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Failed to write open '%s'\n", argv[2]);
|
|
|
|
ret = 3;
|
|
|
|
}
|
2014-03-22 20:35:56 +01:00
|
|
|
|
|
|
|
psl_free(&psl);
|
2014-03-23 21:49:19 +01:00
|
|
|
return ret;
|
2014-03-22 20:35:56 +01:00
|
|
|
}
|