added info functions

This commit is contained in:
Tim Ruehsen 2014-03-23 21:49:19 +01:00
parent 63fc945fff
commit 0771255742
5 changed files with 88 additions and 19 deletions

View File

@ -62,6 +62,16 @@ int
int int
psl_inline_suffix_exception_count(void); psl_inline_suffix_exception_count(void);
// returns compilation time
time_t
psl_inline_builtin_compile_time(void);
// returns mtime of PSL source file
time_t
psl_inline_builtin_file_time(void);
// returns MD5 checksum of PSL source file
const char *
psl_inline_builtin_md5sum(void);
PSL_END_DECLS PSL_END_DECLS
#endif /* _LIBPSL_LIBPSL_INLINE_H */ #endif /* _LIBPSL_LIBPSL_INLINE_H */

View File

@ -27,4 +27,4 @@ psl2c_LDADD = -lidn2
# Build rule for suffix.c # Build rule for suffix.c
suffixes.c: $(top_srcdir)/data/effective_tld_names.dat psl2c$(EXEEXT) suffixes.c: $(top_srcdir)/data/effective_tld_names.dat psl2c$(EXEEXT)
./psl2c$(EXEEXT) <$(top_srcdir)/data/effective_tld_names.dat >suffixes.c ./psl2c$(EXEEXT) $(top_srcdir)/data/effective_tld_names.dat suffixes.c

View File

@ -24,11 +24,6 @@
* *
*/ */
// need _GNU_SOURCE for qsort_r()
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#if HAVE_CONFIG_H #if HAVE_CONFIG_H
# include <config.h> # include <config.h>
#endif #endif
@ -151,3 +146,21 @@ int psl_inline_suffix_exception_count(void)
{ {
return countof(suffix_exceptions); return countof(suffix_exceptions);
} }
// returns compilation time
time_t psl_inline_builtin_compile_time(void)
{
return _psl_compile_time;
}
// returns mtime of PSL source file
time_t psl_inline_builtin_file_time(void)
{
return _psl_file_time;
}
// returns MD5 checksum of PSL source file
const char *psl_inline_builtin_md5sum(void)
{
return _psl_md5_checksum;
}

View File

@ -28,27 +28,31 @@
# include <config.h> # include <config.h>
#endif #endif
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
//#ifdef WITH_LIBIDN2 //#ifdef WITH_LIBIDN2
# include <idn2.h> # include <idn2.h>
//#endif //#endif
#include "psl.c" #include "psl.c"
static void _print_psl_entries(_psl_vector_t *v, const char *varname) static void _print_psl_entries(FILE *fpout, const _psl_vector_t *v, const char *varname)
{ {
int it; int it;
printf("// automatically generated by psl2c\n"); fprintf(fpout, "// automatically generated by psl2c\n");
printf("static _psl_entry_t %s[] = {\n", varname); fprintf(fpout, "static _psl_entry_t %s[] = {\n", varname);
for (it = 0; it < v->cur; it++) { for (it = 0; it < v->cur; it++) {
_psl_entry_t *e = _vector_get(v, it); _psl_entry_t *e = _vector_get(v, it);
printf("\t{ \"%s\", NULL, %hd, %hhd, %hhd },\n", fprintf(fpout, "\t{ \"%s\", NULL, %hd, %hhd, %hhd },\n",
e->label_buf, e->length, e->nlabels, e->wildcard); e->label_buf, e->length, e->nlabels, e->wildcard);
} }
printf("};\n"); fprintf(fpout, "};\n");
} }
static int _str_needs_encoding(const char *s) static int _str_needs_encoding(const char *s)
@ -83,20 +87,53 @@ static void _add_punycode_if_needed(_psl_vector_t *v)
_vector_sort(v); _vector_sort(v);
} }
// int main(int argc, const char **argv) int main(int argc, const char **argv)
int main(void)
{ {
FILE *fpout;
psl_ctx_t *psl; psl_ctx_t *psl;
int ret = 0;
if (!(psl = psl_load_fp(stdin))) 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");
return 1; return 1;
}
_add_punycode_if_needed(psl->suffixes); if (!(psl = psl_load_file(argv[1])))
_add_punycode_if_needed(psl->suffix_exceptions); return 2;
_print_psl_entries(psl->suffixes, "suffixes"); if ((fpout = fopen(argv[2], "w"))) {
_print_psl_entries(psl->suffix_exceptions, "suffix_exceptions"); 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), "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;
}
psl_free(&psl); psl_free(&psl);
return 0; return ret;
} }

View File

@ -83,6 +83,15 @@ static void test_psl(void)
} }
} }
printf("psl_builtin_compile_time()=%ld\n", psl_inline_builtin_compile_time());
psl_inline_builtin_compile_time() == 0 ? failed++ : ok++;
printf("psl_builtin_file_time()=%ld\n", psl_inline_builtin_file_time());
psl_inline_builtin_file_time() == 0 ? failed++ : ok++;
printf("psl_builtin_md5sum()=%s\n", psl_inline_builtin_md5sum());
*psl_inline_builtin_md5sum() == 0 ? failed++ : ok++;
psl_inline_deinit(); psl_inline_deinit();
} }