/* * 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 . * * * Precompile Public Suffix List into * * Changelog * 22.03.2014 Tim Ruehsen created * */ #if HAVE_CONFIG_H # include #endif #include #include #include //#ifdef WITH_LIBIDN2 # include //#endif #include "psl.c" static void _print_psl_entries(FILE *fpout, const _psl_vector_t *v, const char *varname) { int it; fprintf(fpout, "// automatically generated by psl2c\n"); fprintf(fpout, "static _psl_entry_t %s[] = {\n", varname); for (it = 0; it < v->cur; it++) { _psl_entry_t *e = _vector_get(v, it); fprintf(fpout, "\t{ \"%s\", NULL, %hd, %hhd, %hhd },\n", e->label_buf, e->length, e->nlabels, e->wildcard); } fprintf(fpout, "};\n"); } 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; char *asc = NULL; int rc; if ((rc = idn2_lookup_u8((uint8_t *)e->label_buf, (uint8_t **)&asc, 0)) == IDN2_OK) { // fprintf(stderr, "idn2 '%s' -> '%s'\n", e->label_buf, asc); _suffix_init(&suffix, asc, strlen(asc)); suffix.wildcard = e->wildcard; _vector_add(v, &suffix); } else fprintf(stderr, "toASCII(%s) failed (%d): %s\n", e->label_buf, rc, idn2_strerror(rc)); } } _vector_sort(v); } int main(int argc, const char **argv) { FILE *fpout; psl_ctx_t *psl; int ret = 0; if (argc != 3) { fprintf(stderr, "Usage: psl2c \n"); fprintf(stderr, " is the 'effective_tld_names.dat' (aka Public Suffix List)\n"); fprintf(stderr, " is the the C filename to be generated from \n"); return 1; } 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] = ""; _add_punycode_if_needed(psl->suffixes); _add_punycode_if_needed(psl->suffix_exceptions); _print_psl_entries(fpout, psl->suffixes, "suffixes"); _print_psl_entries(fpout, psl->suffix_exceptions, "suffix_exceptions"); snprintf(cmd, sizeof(cmd), "sha1sum %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_sha1_checksum[] = \"%s\";\n", checksum); if (fclose(fpout) != 0) ret = 4; } else { fprintf(stderr, "Failed to write open '%s'\n", argv[2]); ret = 3; } psl_free(&psl); return ret; }