diff --git a/tools/itscertgen/checker/certinfo.c b/tools/itscertgen/checker/certinfo.c index 965cd848e749ea3d484f35bd812c8ecf568a355b..281c0fdb882b18df9a279244f1228335f6f56170 100644 --- a/tools/itscertgen/checker/certinfo.c +++ b/tools/itscertgen/checker/certinfo.c @@ -21,7 +21,7 @@ #include "../cshared/copts.h" #include "../cshared/cserialize.h" #include "../cxml/cxml.h" -#include "../generator/mkgmtime.h" +#include "mkgmtime.h" static size_t load_certificate(const char * path, char ** p); static EC_KEY * load_public_key(const char* path, const EC_GROUP * group); @@ -169,13 +169,13 @@ char * _es_bufs[8][64]; int _es_bufs_idx = 0; static const char * _enumstring(int value, const char ** names, int nsize) { - const char * ret; + char * ret; if (!_numeric && value >= 0 && value < nsize && names[value]){ return names[value]; } - ret = _es_bufs[(++_es_bufs_idx) & 7]; + ret = (char*)_es_bufs[(++_es_bufs_idx) & 7]; sprintf(ret, "%d", value); - return ret; + return (const char*)ret; } #define ENUMSTRING(V,L) _enumstring(V, L, sizeof(L)/sizeof(L[0])) @@ -187,10 +187,10 @@ static int EccPoint_Size(const EccPoint * p){ static void EccPoint_Print(FILE * f, const char * prefix, const EccPoint * const p) { fprintf(f, "%s\n%s\t", prefix, ENUMSTRING(p->type, _point_types), prefix); - print_x(f, p->x, 32); + print_x(f, (const char*)p->x, 32); if (p->type == 4){ fprintf(f, "\n%s\t", prefix); - print_x(f, p->y, 32); + print_x(f, (const char*)p->y, 32); } fprintf(f, "\n%s\n", prefix); } @@ -240,7 +240,7 @@ int main(int argc, char ** argv) } if (_xmlOutput){ - int length; + uint32_t length; char digest[8]; const char * p = cert; const char * e = cert + certlen; @@ -278,11 +278,11 @@ int main(int argc, char ** argv) } fprintf(stdout, ">\n"); p += 2 + si->name_length; - length = cintx_read(&p, e, NULL); + length = (uint32_t)cintx_read(&p, e, NULL); print_attributes(&p, p + length); fprintf(stdout, "\t\n"); - length = cintx_read(&p, e, NULL); + length = (uint32_t)cintx_read(&p, e, NULL); fprintf(stdout, "\t\n"); print_validity(&p, p + length); fprintf(stdout, "\t\n"); @@ -390,7 +390,7 @@ static size_t load_certificate(const char * path, char ** p) } if (cert[0] != 2) { // try hexadecimal - int i; + size_t i; for (i = 0; i < size; i++){ char ch1 = cert[i]; if (ch1 < '0' @@ -513,9 +513,10 @@ static void calculate_certificate_digest(const char* data, int length, char static void print_x(FILE * f, const char * ptr, int len) { - const unsigned char * e = (const unsigned char *)(ptr + len); - for (; ptr < e; ptr++){ - unsigned char c = *ptr; + const unsigned char * p = (const unsigned char *)ptr; + const unsigned char * e = p + len; + for (; p < e; p++){ + unsigned char c = *p; fprintf(f, "%02X", c); } } @@ -528,7 +529,7 @@ static int print_attributes(const char ** pp, const char * e) int rc = 0; const char * p = *pp; unsigned char atype; - int length; + uint32_t length; while (rc == 0 && p < e){ const PublicKey * key; const EccPoint * point; @@ -577,11 +578,11 @@ static int print_attributes(const char ** pp, const char * e) p += EccPoint_Size(point); break; case its_aid_list: - length = cintx_read(&p, e, NULL); + length = (uint32_t)cintx_read(&p, e, NULL); rc = print_aid_list(&p, p + length); break; case its_aid_ssp_list: - length = cintx_read(&p, e, NULL); + length = (uint32_t)cintx_read(&p, e, NULL); rc = print_aid_ssp_list(&p, p + length); break; default: @@ -600,7 +601,7 @@ static int print_aid_list(const char ** pp, const char * e) int rc = 0; const char *p = *pp; while (rc == 0 && p < e){ - int n = cintx_read(&p, e, &rc); + uint32_t n = (uint32_t)cintx_read(&p, e, &rc); fprintf(stdout, "\t\t\t\n", n); } *pp = e; @@ -613,8 +614,8 @@ static int print_aid_ssp_list(const char ** pp, const char * e) const char *p = *pp; while (rc == 0 && p < e){ char * data; - int n = cintx_read(&p, e, &rc); - int len = cintx_read(&p, e, &rc); + uint32_t n = (uint32_t)cintx_read(&p, e, &rc); + uint32_t len = (uint32_t)cintx_read(&p, e, &rc); fprintf(stdout, "\t\t\t", n); if (len){ int r = cxml_text_encode(NULL, &data, p, len); @@ -726,7 +727,7 @@ static int print_validity(const char ** pp, const char * e) if (rc == 0){ uint16_t id = cint16_read(&p, e, &rc); if (rc == 0){ - int local = cintx_read(&p, e, &rc); + uint32_t local = (uint32_t)cintx_read(&p, e, &rc); if (rc == 0){ fprintf(stdout, "\t\t\t\n", ENUMSTRING(dict, _id_region_dicts), id, local); diff --git a/tools/itscertgen/cshared/Makefile b/tools/itscertgen/cshared/Makefile index b24ac37355968de5e7fe8a8fdaf9370f55903d4a..01d20c320d2b468a1efd4d1e5bbec12a47e1022b 100644 --- a/tools/itscertgen/cshared/Makefile +++ b/tools/itscertgen/cshared/Makefile @@ -1,14 +1,15 @@ -PROJECTROOT = .. -BUILDROOT = ../build -PROJECT = cshared -DEBUG = yes -testdir = tests - -alibs = $(PROJECT) -solibs = $(PROJECT) - -sources := copts.c cserialize.c cstr.c -headers := copts.h cserialize.h cstr.h -tests := test_copts.c - -include ../common.mk +PROJECTROOT = .. +BUILDROOT = ../build/ +PROJECT = cshared +DEBUG = yes +testdir = tests + +alibs = $(PROJECT) +solibs = $(PROJECT) + +sources := copts.c cserialize.c cstr.c cring.c e4c_lite.c +sources-WIN32 := cdir_win.c +headers := copts.h cserialize.h cstr.h cdir.h cring.h cmem.h e4c_lite.h +tests := test_copts.c + +include ../common.mk diff --git a/tools/itscertgen/cshared/cdir.h b/tools/itscertgen/cshared/cdir.h new file mode 100644 index 0000000000000000000000000000000000000000..d815265fab6784c07f31b49fae52142df325e616 --- /dev/null +++ b/tools/itscertgen/cshared/cdir.h @@ -0,0 +1,33 @@ +#ifndef cdir_h +#define cdir_h +#include "cstr.h" +#include "cserialize.h" +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct cdir_t cdir_t; +enum { + e_cdir_recursive = 1, + e_cdir_nofiles = 4, + e_cdir_nodirs = 8, +}; + +typedef struct { + const char * path; + const char * fname; + int flags; + uint64_t size; +}cdir_stat_t; + +cdir_t * cdir_open(const pchar_t * path, const char * mask, int flags); +void cdir_close(cdir_t * dir); +cdir_t * cdir_rewind(cdir_t * dir); +const cdir_stat_t * cdir_next(cdir_t * dir); + +int cdir_glob(const char * mask, const char * fname); +#ifdef __cplusplus +} +#endif +#endif + diff --git a/tools/itscertgen/cshared/cdir_win.c b/tools/itscertgen/cshared/cdir_win.c new file mode 100644 index 0000000000000000000000000000000000000000..066bf580c1f4cd6b7035c0385d7ddaf1f29ac82d --- /dev/null +++ b/tools/itscertgen/cshared/cdir_win.c @@ -0,0 +1,104 @@ +#define _CRT_SECURE_NO_WARNINGS +#include "cdir.h" +#include "cmem.h" +#include "cstr.h" +#include + + +struct cdir_t { + WIN32_FIND_DATA fd; + HANDLE h; + int flags; + char * path; + char * fname; + cdir_stat_t st; +}; + +static void _cdir_apply_filter(cdir_t * dir) +{ + do { + if (dir->fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { + if (strcmp(".", dir->fd.cFileName) && strcmp("..", dir->fd.cFileName)){ + if (0 == (dir->flags & e_cdir_nodirs)) return; + } + } + else { + if (0 == (dir->flags & e_cdir_nofiles)) return; + } + //skip this file + } while (FindNextFile(dir->h, &dir->fd)); + FindClose(dir->h); + dir->h = INVALID_HANDLE_VALUE; +} + +cdir_t * cdir_open(const pchar_t * path, const char * mask, int flags) +{ + cdir_t * dir; + int plen = path ? pchar_len(path) : 0; + dir = cnew(cdir_t); + dir->path = pchar_alloc(plen + MAX_PATH + 1); + pchar_cpy(dir->path, path); + while (plen > 0 && (dir->path[plen - 1] == '/' || dir->path[plen - 1] == '\\'))plen--; + if (plen > 0) { + dir->path[plen] = '\\'; + dir->fname = dir->path + plen + 1; + } + else{ + dir->fname = dir->path; + } + dir->flags = flags; + if (mask == NULL) mask = "*"; + strcpy(dir->fname, mask); + dir->h = FindFirstFile(dir->path, &dir->fd); + if (INVALID_HANDLE_VALUE == dir->h){ + cfree(dir); + return NULL; + } + _cdir_apply_filter(dir); + dir->st.path = dir->path; + dir->st.fname = dir->fname; + return dir; +} + +void cdir_close(cdir_t * dir) +{ + if(dir){ + if(dir->h != INVALID_HANDLE_VALUE){ + FindClose(dir->h); + } + cfree(dir); + } +} + +cdir_t * cdir_rewind(cdir_t * dir) +{ + if(dir){ + if(dir->h != INVALID_HANDLE_VALUE){ + FindClose(dir->h); + } + *dir->fname = 0; + dir->h = FindFirstFile(dir->path, &dir->fd); + if(INVALID_HANDLE_VALUE == dir->h){ + cfree(dir); + dir = NULL; + } + } + return dir; +} + +const cdir_stat_t* cdir_next(cdir_t * dir) +{ + if (dir && dir->h != INVALID_HANDLE_VALUE){ + pchar_cpy(dir->fname, dir->fd.cFileName); + dir->st.size = dir->fd.nFileSizeHigh; + dir->st.size = (dir->st.size << 32) | dir->fd.nFileSizeLow; + if (FindNextFile(dir->h, &dir->fd)){ + _cdir_apply_filter(dir); + }else { + FindClose(dir->h); + dir->h = INVALID_HANDLE_VALUE; + } + return &dir->st; + } + return NULL; +} diff --git a/tools/itscertgen/cshared/clog.c b/tools/itscertgen/cshared/clog.c new file mode 100644 index 0000000000000000000000000000000000000000..b788565c618f68b07ca7a4b2d614efd4f5ba2759 --- /dev/null +++ b/tools/itscertgen/cshared/clog.c @@ -0,0 +1,109 @@ +/********************************************************************* +###################################################################### +## +## Created by: Denis Filatov +## Date : 10.11.2005 +## +## Copyleft (c) 2003 - 2015 +## This code is provided under the CeCill-C license agreement. +###################################################################### +*********************************************************************/ +#include +#include +#include +#include "clog.h" + +static int _clog_level = CLOG_INFO; +static FILE * _clog_out[CLOG_LASTLEVEL]; +static const char * _clog_lnames[CLOG_LASTLEVEL] = { + "FATAL", + "ERROR", + "WARNING", + "INFO", + "DEBUG", +}; + +static int _clog_out_initialized = 0; + +static void _clog_out_initialize(void) +{ + int i; + _clog_out[CLOG_FATAL] = stderr; + _clog_out[CLOG_ERROR] = stderr; + _clog_out[CLOG_WARNING] = stderr; + for(i=CLOG_INFO; i= 0 && level <= CLOG_DEBUG){ + _clog_level = level; + } +} +const char * clog_level_name(int const level) +{ + const char * ret = NULL; + if(level < CLOG_LASTLEVEL) + ret = _clog_lnames[level]; + return ret ? ret : CLOG_DEFAULT_LEVEL_NAME; +} + +void clog_set_level_name(int const level, const char * const name) +{ + if(level < CLOG_LASTLEVEL) + _clog_lnames[level] = name; +} + +void clog_set_output(int const level, void * const out) +{ + if(0 == _clog_out_initialized){ + _clog_out_initialize(); + } + if(level < 0){ + int i; + for(i=0; i= sizeof(_clog_out)/sizeof(_clog_out[0])){ + out = _clog_out[sizeof(_clog_out)/sizeof(_clog_out[0]) - 1]; + }else{ + out = _clog_out[level]; + } + } + if(out){ + va_start(ap, format); + vfprintf(out, format, ap); + va_end(ap); + { + int l = strlen(format); + if(l == 0 || format[l-1]!= '\n'){ + fprintf(out, "\n"); + } + } + } + } +} diff --git a/tools/itscertgen/cshared/clog.h b/tools/itscertgen/cshared/clog.h new file mode 100644 index 0000000000000000000000000000000000000000..164343992fc135a3f450ef1b861a0bb882faa783 --- /dev/null +++ b/tools/itscertgen/cshared/clog.h @@ -0,0 +1,67 @@ +/********************************************************************* +###################################################################### +## +## Created by: Denis Filatov +## Date : 10.11.2005 +## +## Copyleft (c) 2003 - 2015 +## This code is provided under the CeCill-C license agreement. +###################################################################### +*********************************************************************/ +#ifndef clog_h +#define clog_h +enum{ + CLOG_FATAL=0, + CLOG_ERROR, + CLOG_WARNING, + CLOG_INFO, + CLOG_DEBUG, + + CLOG_LASTLEVEL +}; + +int clog_level(void); +void clog_set_level(int const level); +const char * clog_level_name(int const level); +void clog_set_level_name(int const level, const char * const name); +void clog_set_output(int const level, void * const out); + +#define CLOG_DEFAULT_LEVEL_NAME "" + +#define STRMODULE(M) #M +#define XSTRMODULE(M) STRMODULE(M) + +#define clog_flm(F, SMODULE, LEVEL, FORMAT, ...) clog_fprintf(F, LEVEL, "%10.10s: %7.7s: " FORMAT, SMODULE, clog_level_name(LEVEL), ##__VA_ARGS__) +#define clog_lm(SMODULE,LEVEL,FORMAT, ...) clog_flm((void*)0, SMODULE, LEVEL, FORMAT, ##__VA_ARGS__) +#define clog_fl(F,LEVEL,FORMAT, ...) clog_flm(F, XSTRMODULE(__MODULE__), LEVEL, FORMAT, ##__VA_ARGS__) +#define clog_l(LEVEL,FORMAT, ...) clog_flm((void*)0, XSTRMODULE(__MODULE__), LEVEL, FORMAT, ##__VA_ARGS__) + +#define clog_fatal(FORMAT, ...) clog_l(CLOG_FATAL, FORMAT, ##__VA_ARGS__) +#define clog_error(FORMAT, ...) clog_l(CLOG_ERROR, FORMAT, ##__VA_ARGS__) +#define clog_warning(FORMAT, ...) clog_l(CLOG_WARNING, FORMAT, ##__VA_ARGS__) +#define clog_info(FORMAT, ...) clog_l(CLOG_INFO, FORMAT, ##__VA_ARGS__) +#define clog_debug(FORMAT, ...) clog_l(CLOG_DEBUG, FORMAT, ##__VA_ARGS__) + +#define fclog_fatal(F,FORMAT, ...) clog_fl(F, CLOG_FATAL, FORMAT, ##__VA_ARGS__) +#define fclog_error(F,FORMAT, ...) clog_fl(F, CLOG_ERROR, FORMAT, ##__VA_ARGS__) +#define fclog_warning(F,FORMAT, ...) clog_fl(F, CLOG_WARNING, FORMAT, ##__VA_ARGS__) +#define fclog_info(F,FORMAT, ...) clog_fl(F, CLOG_INFO, FORMAT, ##__VA_ARGS__) +#define fclog_debug(F,FORMAT, ...) clog_fl(F, CLOG_DEBUG, FORMAT, ##__VA_ARGS__) + +#define mclog_fatal(MODULE, FORMAT, ...) clog_lm(#MODULE, CLOG_FATAL, FORMAT, ##__VA_ARGS__) +#define mclog_error(MODULE, FORMAT, ...) clog_lm(#MODULE, CLOG_ERROR, FORMAT, ##__VA_ARGS__) +#define mclog_warning(MODULE, FORMAT, ...) clog_lm(#MODULE, CLOG_WARNING, FORMAT, ##__VA_ARGS__) +#define mclog_info(MODULE, FORMAT, ...) clog_lm(#MODULE, CLOG_INFO, FORMAT, ##__VA_ARGS__) +#define mclog_debug(MODULE, FORMAT, ...) clog_lm(#MODULE, CLOG_DEBUG, FORMAT, ##__VA_ARGS__) + +#define fmclog_fatal(F, MODULE, FORMAT, ...) clog_flm(F, #MODULE, CLOG_FATAL, FORMAT, ##__VA_ARGS__) +#define fmclog_error(F, MODULE, FORMAT, ...) clog_flm(F, #MODULE, CLOG_ERROR, FORMAT, ##__VA_ARGS__) +#define fmclog_warning(F, MODULE, FORMAT, ...) clog_flm(F, #MODULE, CLOG_WARNING, FORMAT, ##__VA_ARGS__) +#define fmclog_info(F, MODULE, FORMAT, ...) clog_flm(F, #MODULE, CLOG_INFO, FORMAT, ##__VA_ARGS__) +#define fmclog_debug(F, MODULE, FORMAT, ...) clog_flm(F, #MODULE, CLOG_DEBUG, FORMAT, ##__VA_ARGS__) + +void clog_fprintf(void * const f, int const level, const char * format, ...); + +#undef __CLOG_MODULE + +#endif diff --git a/tools/itscertgen/cshared/cmem.h b/tools/itscertgen/cshared/cmem.h new file mode 100644 index 0000000000000000000000000000000000000000..c007c6ba11761a7a83f3888a7ffb93f7101fbb09 --- /dev/null +++ b/tools/itscertgen/cshared/cmem.h @@ -0,0 +1,84 @@ +/********************************************************************* +###################################################################### +## +## Created by: Denis Filatov +## Date : 10.11.2005 +## +## Copyleft (c) 2003 - 2015 +## This code is provided under the CeCill-C license agreement. +###################################################################### +*********************************************************************/ +#ifndef cmem_h +#define cmem_h +#include +#include +#define callocate(N) malloc(N) +#define callocate0(N) calloc(1,N) +#define cfree(P) free(P) +#define cnew(T) (T*)callocate(sizeof(T)) +#define cnew0(T) (T*)callocate0(sizeof(T)) +typedef void(cdestructor_fn)(void*); + +#if defined(__GNUC__) +#define cfetch_and_add(P,X) __sync_fetch_and_add(P, X) +#define cfetch_and_sub(P,X) __sync_fetch_and_sub(P, X) +#define cadd_and_fetch(P,X) __sync_add_and_fetch(P, X) +#define csub_and_fetch(P,X) __sync_sub_and_fetch(P, X) +#define cfetch_and_inc(P) cfetch_and_add(P,1) +#define cfetch_and_dec(P) cfetch_and_sub(P,1) +#define cinc_and_fetch(P) cadd_and_fetch(P,1) +#define cdec_and_fetch(P) csub_and_fetch(P,1) + +#elif defined (_MSC_VER) +#include +#define cfetch_and_add(P,X) (InterlockedAddNoFence(P,X)-X) +#define cfetch_and_sub(P,X) (InterlockedAddNoFence(P,-X)+X) +#define cadd_and_fetch(P,X) InterlockedAddNoFence(P,X) +#define csub_and_fetch(P,X) InterlockedAddNoFence(P,-X) +#define cfetch_and_inc(P) (InterlockedIncrementNoFence(P)-1) +#define cfetch_and_dec(P) (InterlockedDecrementNoFence(P)+1) +#define cinc_and_fetch(P) InterlockedIncrementNoFence(P) +#define cdec_and_fetch(P) InterlockedDecrementNoFence(P) +#ifndef __cplusplus +#define inline _inline +#endif +#endif + +__inline static void * cmemdup(const void * const ptr, int size) { + void * p; + if (size > 0){ + p = callocate(size); + if (p && ptr) { + memcpy(p, ptr, size); + } + } + else{ + p = NULL; + } + return p; +} + +__inline static void * _cretain(void*p, int*prcntr) { + if (*prcntr != 0){ + cinc_and_fetch(prcntr); + } + return p; +} +__inline static void _crelease(void*p, int*prcntr, void * destr) { + if (*prcntr != 0){ + if(0 == cdec_and_fetch(prcntr)){ + if (destr) ((cdestructor_fn *)destr)(p); + else free(p); + } + } +} + +#if defined(__GNUC__) +#define cretain(S) (__typeof__(S)*)_cretain(S, &(S)->_rcntr) +#elif defined (_MSC_VER) +#define cretain(S) _cretain(S, &(S)->_rcntr) +#endif + +#define crelease(S,D) _crelease(S, &(S)->_rcntr, D) + +#endif diff --git a/tools/itscertgen/cshared/copts.c b/tools/itscertgen/cshared/copts.c index cb915c708a725f79fd0c7f7a2b889f1c1afebdc3..f6a5eedd3d96132f6d6d7047082bc0d6933d9616 100644 --- a/tools/itscertgen/cshared/copts.c +++ b/tools/itscertgen/cshared/copts.c @@ -1,776 +1,778 @@ -/********************************************************************* -###################################################################### -## -## Created by: Denis Filatov -## Date : 10.11.2005 -## -## Copyleft (c) 2003 - 2006 -## This code is provided under the CeCill-C license agreement. -###################################################################### -*********************************************************************/ -#ifdef _MSC_VER -#define _CRT_SECURE_NO_WARNINGS -#endif -#include -#include -#include -#include -#include - -#include "copts.h" - -#ifdef _MSC_VER -#define inline __inline -#define strcasecmp _stricmp -#define strdup _strdup -#define _CRT_SECURE_NO_WARNINGS -#endif - -#define COPT_AFLAG_ERROR 0x10000 -#define COPT_AFLAG_PRESCAN 0x08000 -#define COPT_AFLAG_LOPT 0x04000 -#define COPT_AFLAG_AUTOHELP 0x02000 - -static int do_coptions (int argc, char* argv[], int flags, copt_t* opts); -static int set_option_value(copt_t* opt, const char * option, char* val); -static int copt_addr_assign(unsigned long* addr,const char* val); -inline -static int call_callback(const copt_t * opt, const char * option, const copt_value_t * value); -static void print_opt_help(FILE* f, copt_t* opt, int err, const char* errval); - -int coptions(int argc, char* argv[], int flags, copt_t* opts) -{ - int rc = 1, ahflag = 0; - copt_t *o; - - if(0 == (flags & COPT_NOAUTOHELP)){ - ahflag = COPT_AFLAG_AUTOHELP; - } - - if(0 == (flags & COPT_NOCONFIG) || ahflag ){ - for(o=opts; o->type != COPT_END; o++){ - unsigned short t = o->type&COPT_TYPE_MASK; - - /* check for config files */ - if(0==(flags & COPT_NOCONFIG) && t == COPT_CFGFILE){ - /* check for config options. - * just options with type COPT_CFGFILE - * will be processed */ - rc = do_coptions(argc, argv, flags|COPT_AFLAG_PRESCAN|COPT_NOREORDER, opts); - if(rc < 0){ - if(rc == COPT_EHELP){ - if(0==(flags&COPT_NOHELP_MSG)){ - coptions_help_ex(stdout, argv[0], flags, opts, NULL, NULL, NULL); - } - return rc; - } - if(0==(flags&COPT_NOERR_UNKNOWN)){ - return rc; - } - } - if(o->vptr){ - const char * cfgname = *((const char **)o->vptr); - if(cfgname){ - rc = coptions_load(cfgname, NULL, flags, opts); - if(rc < 0){ - if(0 == (flags&COPT_NOERR_MSG)){ - fprintf(stderr, "%s: config load error\n", cfgname); - } - if(flags & COPT_ERR_CONFIG){ - return rc; - } - } - } - } - } else if(t == COPT_HELP && ahflag){ - /* check for autohelp */ - ahflag = 0; - } - } - } - /* process other options except of COPT_CFGFILE */ - return do_coptions(argc, argv, flags|ahflag, opts); -} - -static void move_args(char * argv[], int to, int from, int count) -{ - if( from > to){ - while(from > to){ - int i; - for(i=from; i=from; i--){ - char * p = argv[i]; - argv[i] = argv[i+1]; - argv[i+1] = p; - } - from ++; - } - } -} - -static int do_coptions(int argc, char* argv[], int flags, copt_t* opts) -{ - int i, rc; - int searchopts = 1; - copt_t* o=NULL; - copt_t* copt = NULL; - copt_t* onopt = NULL; - coptype_t t; - int nargc=argc; - - assert(opts); - - for(i=1; itype; o++){ - int ol; - if(NULL == o->lopt) continue; - t = o->type&COPT_TYPE_MASK; - ol = (int)strlen(o->lopt); - if( (aopt[2+ol]==0 || aopt[2+ol]=='=') - && 0==strncmp(o->lopt, aopt+2, ol)){ - - /* option found */ - if(t == COPT_HELP){ - return COPT_EHELP; - } - if(aopt[2+ol]=='='){ - if(0==(flags&COPT_AFLAG_PRESCAN) || t == COPT_CFGFILE){ - rc = set_option_value(o, argv[i], aopt+ol+3); - if(rc<0){ - if(0==(flags&COPT_NOERR_MSG)){ - print_opt_help(stderr, o, flags|COPT_AFLAG_ERROR, argv[i]); - } - if(0==(flags&COPT_NOERR_ARG)){ - return -i; - } - } - } - }else if(t == COPT_BOOL || t == COPT_BOOLI){ - if(0==(flags & COPT_AFLAG_PRESCAN)){ - set_option_value(o, argv[i], NULL); - } - }else{ - copt = o; - } - /* move this option argument to the end of list */ - move_args(argv, argc-1, i, 1); - i--; - nargc--; - break; - } - } - if(COPT_END == o->type){ - if(0==(flags&COPT_NOERR_MSG)){ - fprintf(stderr, "Unknown option '%s'\n", argv[i]); - } - if(0==(flags & COPT_NOERR_UNKNOWN)){ - return 0-i; - } - } - }else{ - /* short options */ - aopt++; /* skip first '-' */ - while(*aopt){ - - /* check autohelp */ - if(flags & COPT_AFLAG_AUTOHELP){ - if(*aopt == 'h' || *aopt == '?'){ - return COPT_EHELP; - } - } - - for(o = opts; COPT_END != o->type; o++){ - if(NULL == o->sopts) continue; - t = o->type&COPT_TYPE_MASK; - if(strchr(o->sopts, *aopt)){ - /* option found */ - char szopt[3]={'-',0,0}; - szopt[1] = *aopt; - - if(t == COPT_HELP){ - return COPT_EHELP; - } - if(aopt[1] == 0){ /* -p -b 13 */ - if(t == COPT_BOOL || t == COPT_BOOLI){ - if(0 == (flags&COPT_AFLAG_PRESCAN)){ - set_option_value(o, szopt, NULL); - } - }else{ - copt = o; - } - aopt++; - }else if(aopt[1] == '='){ /* -p=123 */ - if(0==(flags&COPT_AFLAG_PRESCAN) || t == COPT_CFGFILE){ - rc = set_option_value(o, argv[i], aopt+2); - if(rc<0){ - if(0==(flags&COPT_NOERR_MSG)){ - print_opt_help(stderr, o, flags|COPT_AFLAG_ERROR, argv[i]); - } - if(0==(flags&COPT_NOERR_ARG)){ - return -i; - } - } - } - while(*aopt) aopt++; - }else{ /* -p123*/ - if(0==(flags&COPT_AFLAG_PRESCAN) || t == COPT_CFGFILE){ - if( t == COPT_BOOL || t == COPT_BOOLI ){ - set_option_value(o, szopt, NULL); - }else{ - rc = set_option_value(o, szopt, aopt+1); - if(rc<0){ - if(0==(flags&COPT_NOERR_MSG)){ - print_opt_help(stderr, o, flags | COPT_AFLAG_ERROR, argv[i]); - } - if(0==(flags&COPT_NOERR_ARG)){ - return -i; - } - } - } - } - while(*aopt) aopt++; - } - break; - } - } - if(COPT_END == o->type){ - if(0==(flags&COPT_NOERR_MSG)){ - fprintf(stderr, "Unknown option '%s'\n", argv[i]); - } - if(0==(flags & COPT_NOERR_UNKNOWN)){ - return 0-i; - } - aopt++; - }else{ - /* move this option argument to the end of list */ - move_args(argv, argc-1, i, 1); - i--; - nargc--; - } - } - } - }else{ - /* check for nonoption callback */ - if(0 == (flags & COPT_AFLAG_PRESCAN)){ - if(onopt == NULL){ - onopt = (void*)-1; - for(o = opts; COPT_END != o->type; o++){ - if(o->type & COPT_CALLBACK){ - if(o->lopt == NULL && o->sopts == NULL && o->vptr){ - onopt = o; - break; - } - } - } - } - if(onopt != (void*)-1){ - copt_value_t v; - v.v_str = argv[i]; - rc = call_callback(onopt, NULL, &v); - if(rc < 0){ - if(rc == COPT_EHELP) return rc; - if(0==(flags&COPT_NOERR_ARG)){ - return -i; - } - } - } - } - } - }else{ /* prev is option */ - t = copt->type & COPT_TYPE_MASK; - if(0==(flags&COPT_AFLAG_PRESCAN) || t == COPT_CFGFILE){ - rc = set_option_value(copt, argv[i-1], argv[i]); - if(rc < 0){ - if(0==(flags&COPT_NOERR_MSG)){ - print_opt_help(stderr, o, flags | COPT_AFLAG_ERROR, argv[i - 1]); - } - if(0==(flags&COPT_NOERR_ARG)){ - return 0-i-1; - } - } - } - copt = NULL; - /* move this option argument to the end of list */ - move_args(argv, argc-1, i, 1); - nargc--; - i--; - } - } - if(copt){ - /* last option was not completed */ - if(0==(flags&COPT_NOERR_MSG)){ - print_opt_help(stderr, o, flags | COPT_AFLAG_ERROR, argv[i - 1]); - } - if(0==(flags&COPT_NOERR_ARG)){ - return 0-i-1; - } - } - return nargc; -} - -static int set_option_value(copt_t* opt, const char * arg, char* val) -{ - char* np; - copt_value_t value; - - value.v_boolean = 0; - - switch(opt->type & COPT_TYPE_MASK){ - case COPT_HELP: - case COPT_BOOL: - value.v_boolean = 1; - case COPT_BOOLI: - if(val){ - if( val[0] == '0' - ||0==strcasecmp(val, "false") - ||0==strcasecmp(val, "no") - ){ - value.v_boolean = !value.v_boolean; - }else if( !isdigit(val[0]) - && strcasecmp(val, "true") - && strcasecmp(val, "yes") - ){ - return -1; - } - } - break; - case COPT_LONG: - if(!val) return -1; - value.v_long = strtol(val, &np, 0); - if(*np != 0) return -1; - break; - case COPT_ULONG: - if(!val) return -1; - value.v_ulong = strtoul(val, &np, 0); - if(*np != 0) return -1; - break; - case COPT_SHORT: - if(!val || !opt->vptr) return -1; - value.v_long = strtol(val, &np, 0); - if(*np != 0) return -1; - if(value.v_long>SHRT_MAX || value.v_longvptr) return -1; - value.v_ulong = strtoul(val, &np, 0); - if(*np != 0) return -1; - if(value.v_ulong>USHRT_MAX){ - return -1; - } - value.v_ushort = (unsigned short) value.v_ulong; - break; - - case COPT_CHAR: - if(!val || !opt->vptr) return -1; - if(0==val[1]){ - value.v_char = val[0]; - }else{ - value.v_ulong = strtoul(val, &np, 0); - if(*np != 0) return -1; - if(value.v_ulong>UCHAR_MAX){ - return -1; - } - value.v_char = (unsigned char)value.v_long; - } - break; - case COPT_STRENUM: - { - const char ** p = (const char**)opt->vptr; - for(; *p && strcmp(*p, val); p++); - if(NULL == *p){ - return -1; - } - value.v_long = p - (const char**)opt->vptr; - } - break; - case COPT_STRLIST: - if(!opt->vptr || (opt->type & COPT_CALLBACK)) return -1; - case COPT_CFGFILE: - case COPT_STR: - value.v_str = val; - break; - case COPT_HOST: - if(0 > copt_addr_assign(&value.v_ulong, val)){ - return -1; - } - break; - default: - return -1; - } - - if(opt->type & COPT_CALLBACK){ - return call_callback(opt, arg, &value); - } - - switch(opt->type & COPT_TYPE_MASK){ - case COPT_HELP: - case COPT_BOOL: - case COPT_BOOLI: - if(opt->vptr && opt->vptr != (void*)1){ - *(int*)opt->vptr = value.v_boolean; - }else{ - opt->vptr = ((char*)NULL) + value.v_boolean; - } - break; - case COPT_LONG: - case COPT_ULONG: - case COPT_CFGFILE: - case COPT_STR: - case COPT_HOST: - if(opt->vptr){ - *(long*)opt->vptr = value.v_long; - }else{ - opt->vptr = (void*)value.v_long; - } - break; - case COPT_STRLIST: - { - char ** p = opt->vptr; - *p = value.v_str; - opt->vptr = p + 1; - } - break; - case COPT_STRENUM: - opt->vptr = (void*)(((const char**)opt->vptr) + value.v_long); - break; - case COPT_SHORT: - case COPT_USHORT: - *(short*)opt->vptr = value.v_short; - break; - case COPT_CHAR: - *(char*)opt->vptr = value.v_char; - break; - default: - return -1; - }; - return 0; -} - -static int copt_addr_assign(unsigned long* addr,const char* val) -{ - unsigned long n = 0; - unsigned long s = 0; - int cnt = 0; - while(*val){ - if(*val == '.'){ - if(cnt == 3){ - return -1; - } - n = (n<<8) | s; - s = 0; - cnt++; - }else{ - unsigned int c = *val - '0'; - if(c > 9){ - return -1; - } - s = s*10 + c; - } - val++; - } - if(cnt != 3){ - return -1; - } - n = (n<<8) | s; - - *addr = n; - return 0; -} - -inline static int call_callback(const copt_t * opt, const char * option, const copt_value_t * value) -{ - if(opt->vptr){ - return ((copt_callback *)opt->vptr)(opt, option, value); - } - return -1; -} - -void coptions_help_ex(FILE * f, const char * prgname, int flags, copt_t* opt, const char* usagestr, - const char* header, const char* footer) -{ - const char * progname; - if(header){ - fputs(header, f); - } - if(prgname){ - progname = strrchr(prgname, '/'); - if(progname) progname++; - else progname = prgname; - if(usagestr){ - fprintf(f, "Usage: %s %s\n", progname, usagestr); - } - fprintf(f, "Options:\n"); - }else{ - if(usagestr){ - fprintf(f, "%s\n", usagestr); - } - } - while(opt->type != COPT_END){ - if(opt->sopts || opt->lopt){ - fprintf(f, " "); - print_opt_help(f, opt, flags, NULL); - } - opt++; - } - if(footer){ - fputs(footer, f); - } -} - -static int sprintf_option_value(const copt_t* const opt, char * const buf) -{ - int ret = 0; - switch(opt->type&COPT_TYPE_MASK){ - case COPT_BOOL: - if(opt->vptr && opt->vptr != (void*)1){ - ret = sprintf(buf, "%s", ((*(int*)opt->vptr)?"true":"false")); - }else{ - ret = sprintf(buf, "%s", ((opt->vptr)?"true":"false")); - } - break; - case COPT_BOOLI: - if(opt->vptr && opt->vptr != (void*)1){ - ret = sprintf(buf, "%s", (*(int*)opt->vptr)?"false":"true"); - }else{ - ret = sprintf(buf, "%s", (opt->vptr)?"false":"true"); - } - break; - case COPT_LONG: - ret = sprintf(buf, "%ld", *((long*)opt->vptr)); - break; - case COPT_ULONG: - ret = sprintf(buf, "%lu", *((unsigned long*)opt->vptr)); - break; - case COPT_SHORT: - ret = sprintf(buf, "%u", *((short*)opt->vptr)); - break; - case COPT_USHORT: - ret = sprintf(buf, "%u", *((unsigned short*)opt->vptr)); - break; - case COPT_CHAR: - ret = sprintf(buf, "%c", *((char*)opt->vptr)); - break; - case COPT_STR: - case COPT_CFGFILE: - case COPT_STRENUM: - if(*(char**)opt->vptr) - ret = sprintf(buf, "%s", *(char**)opt->vptr); - else - ret = sprintf(buf, "NULL"); - break; - case COPT_HOST: - { - unsigned int n = *(unsigned int*)opt->vptr; - sprintf(buf, "%u.%u.%u.%u", - n>>24, (n>>16)&0xFF, (n>>8)&0xFF, n&0xFF); - } - break; - } - return ret; -} - -static const char* valnames[] = { - NULL, /* COPT_BOOL */ - NULL, /* COPT_BOOLI */ - "num", /* COPT_LONG */ - "num", /* COPT_ULONG */ - "num", /* COPT_SHORT */ - "num", /* COPT_USHORT */ - "chr", /* COPT_CHAR */ - "str", /* COPT_STR */ - "addr",/* COPT_HOST */ - "str", /* COPT_STRLIST*/ - "str", /* COPT_STRENUM*/ - "file",/* COPT_CFGFILE*/ - NULL, /* COPT_HELP */ -}; - -static void print_opt_help(FILE* f, copt_t* opt, int flags, const char* errval) -{ - char *p, sz_help[256]; - p=sz_help; - int err = flags&COPT_AFLAG_ERROR; - int t=opt->type&COPT_TYPE_MASK; - if(opt->sopts){ - const char* po = opt->sopts; - while(*po){ - if(po != opt->sopts) (*p++) = '|'; - *(p++)='-'; *(p++)=(*po); - po++; - } - if(valnames[t]){ - p+=sprintf(p, " <%s>", valnames[t]); - } - } - if(opt->lopt){ - if(opt->sopts) p+=sprintf(p, " | "); - p+=sprintf(p, "--%s", opt->lopt); - if(valnames[t]){ - p+=sprintf(p, "=%s", valnames[t]); - } - } - if(opt->helpstr){ - while(p-sz_help<35){ - *(p++) = ' '; - } - p+=sprintf(p, " : %s", opt->helpstr); - } - if(err){ - *(p++)='\n'; *(p++)='\t'; - if(errval){ - p+=sprintf(p, "%s ", errval); - } - p+=sprintf(p, "Unsupported value"); - }else{ - if (0 == (flags & COPT_HELP_NOVALUES)){ - if (t != COPT_HELP && t != COPT_STRLIST && 0 == (opt->type&COPT_CALLBACK)){ - /* print default value */ - *(p++) = '['; - p += sprintf_option_value(opt, p); - *(p++) = ']'; - *p = 0; - } - } - } - fprintf(f, "%s\n", sz_help); -} - -int coptions_load(const char* filename, const char * section, int flags, copt_t* const opts) -{ - char* buf; - int line = 0; - int err = 0; - int in_sect = 0; - FILE* f = fopen(filename, "r"); - - if(f == NULL){ - if(0 == (flags & COPT_NOERR_MSG)){ - perror(filename); - } - return COPT_ERROR; - } - - buf = (char*)malloc(1024); - while(fgets(buf, 1024, f)){ - char *e, *val=NULL, *key = buf; - copt_t * o; - unsigned short t; - - err = 1; - line ++; - while(*key && isspace(*key)) key++; - if(0 == *key)continue; - /* search for section */ - if(*key == '['){ - if(section){ - if(in_sect){ - /* section loading finished */ - in_sect =0; - break; - } - key++; - while(*key && isspace(*key)) key++; - e = strchr(key, ']'); - if(e == NULL) goto error; - *e = 0; - if(0==strcmp(section, key)){ - in_sect = 1; - } - } - continue; - } - - if(section && 0== in_sect) continue; - if(*key == '#' || *key == ';') continue; - - e = strchr(key, '='); - if(NULL == e) goto error; - val = e+1; - while(e>key && isspace(*(e-1)))e--; - if(e == key) goto error; - *e = 0; - - while(*val && isspace(*val)) val ++; - - for(o=opts; o->type != COPT_END; o++){ - if(o->lopt && 0==strcmp(o->lopt, key)) - break; - } - if(COPT_END == o->type) { - err = 2; - goto error; - } - t = o->type & COPT_TYPE_MASK; - - e = val + strlen(val); - while(e>val && isspace(*(e-1))) *(--e) = 0; - - if( t == COPT_STR - || t == COPT_STRLIST - ){ - if(0==strcmp("NULL", val) || 0==strcmp("null", val)) val = NULL; - else val = strdup(val); - } - - if(set_option_value(o, key, val)){ - err = 3; - goto error; - } - - continue; - - error: - if(0 == (flags & COPT_NOERR_MSG)){ - switch(err){ - case 1: - fprintf(stderr, "%s:%d: Syntax error in config file\n", filename, line); - break; - case 2: - fprintf(stderr, "%s:%d: Option is unknown\n", filename, line); - break; - case 3: - fprintf(stderr, "%s:%d: Unsupported value: %s\n", filename, line, val); - break; - } - } - if(flags & COPT_ERR_CONFIG){ - break; - } - } - free(buf); - fclose(f); - return 0; -} +/********************************************************************* +###################################################################### +## +## Created by: Denis Filatov +## Date : 10.11.2005 +## +## Copyleft (c) 2003 - 2015 +## This code is provided under the CeCill-C license agreement. +###################################################################### +*********************************************************************/ +#ifdef _MSC_VER +#define _CRT_SECURE_NO_WARNINGS +#endif +#include +#include +#include +#include +#include + +#include "copts.h" + +#ifdef _MSC_VER +#define inline __inline +#define strcasecmp _stricmp +#define strdup _strdup +#define _CRT_SECURE_NO_WARNINGS +#endif + +#define COPT_AFLAG_ERROR 0x10000 +#define COPT_AFLAG_PRESCAN 0x08000 +#define COPT_AFLAG_LOPT 0x04000 +#define COPT_AFLAG_AUTOHELP 0x02000 + +static int do_coptions (int argc, char* argv[], int flags, copt_t* opts); +static int set_option_value(copt_t* opt, const char * option, char* val); +static int copt_addr_assign(unsigned long* addr,const char* val); +inline +static int call_callback(const copt_t * opt, const char * option, const copt_value_t * value); +static void print_opt_help(FILE* f, copt_t* opt, int err, const char* errval); + +int coptions(int argc, char* argv[], int flags, copt_t* opts) +{ + int rc = 1, ahflag = 0; + copt_t *o; + + if(0 == (flags & COPT_NOAUTOHELP)){ + ahflag = COPT_AFLAG_AUTOHELP; + } + + if(0 == (flags & COPT_NOCONFIG) || ahflag ){ + for(o=opts; o->type != COPT_END; o++){ + unsigned short t = o->type&COPT_TYPE_MASK; + + /* check for config files */ + if(0==(flags & COPT_NOCONFIG) && t == COPT_CFGFILE){ + /* check for config options. + * just options with type COPT_CFGFILE + * will be processed */ + rc = do_coptions(argc, argv, flags|COPT_AFLAG_PRESCAN|COPT_NOREORDER, opts); + if(rc < 0){ + if(rc == COPT_EHELP){ + if(0==(flags&COPT_NOHELP_MSG)){ + coptions_help_ex(stdout, argv[0], flags, opts, NULL, NULL, NULL); + } + return rc; + } + if(0==(flags&COPT_NOERR_UNKNOWN)){ + return rc; + } + } + if(o->vptr){ + const char * cfgname = *((const char **)o->vptr); + if(cfgname){ + rc = coptions_load(cfgname, NULL, flags, opts); + if(rc < 0){ + if(0 == (flags&COPT_NOERR_MSG)){ + fprintf(stderr, "%s: config load error\n", cfgname); + } + if(flags & COPT_ERR_CONFIG){ + return rc; + } + } + } + } + } else if(t == COPT_HELP && ahflag){ + /* check for autohelp */ + ahflag = 0; + } + } + } + /* process other options except of COPT_CFGFILE */ + return do_coptions(argc, argv, flags|ahflag, opts); +} + +static void move_args(char * argv[], int to, int from, int count) +{ + if( from > to){ + while(from > to){ + int i; + for(i=from; i=from; i--){ + char * p = argv[i]; + argv[i] = argv[i+1]; + argv[i+1] = p; + } + from ++; + } + } +} + +static int do_coptions(int argc, char* argv[], int flags, copt_t* opts) +{ + int i, rc; + int searchopts = 1; + copt_t* o=NULL; + copt_t* copt = NULL; + copt_t* onopt = NULL; + coptype_t t; + int nargc=argc; + + assert(opts); + + for(i=1; itype; o++){ + int ol; + if(NULL == o->lopt) continue; + t = o->type&COPT_TYPE_MASK; + ol = (int)strlen(o->lopt); + if( (aopt[2+ol]==0 || aopt[2+ol]=='=') + && 0==strncmp(o->lopt, aopt+2, ol)){ + + /* option found */ + if(t == COPT_HELP){ + return COPT_EHELP; + } + if(aopt[2+ol]=='='){ + if(0==(flags&COPT_AFLAG_PRESCAN) || t == COPT_CFGFILE){ + rc = set_option_value(o, argv[i], aopt+ol+3); + if(rc<0){ + if(0==(flags&COPT_NOERR_MSG)){ + print_opt_help(stderr, o, flags|COPT_AFLAG_ERROR, argv[i]); + } + if(0==(flags&COPT_NOERR_ARG)){ + return -i; + } + } + } + }else if(t == COPT_BOOL || t == COPT_BOOLI){ + if(0==(flags & COPT_AFLAG_PRESCAN)){ + set_option_value(o, argv[i], NULL); + } + }else{ + copt = o; + } + /* move this option argument to the end of list */ + move_args(argv, argc-1, i, 1); + i--; + nargc--; + break; + } + } + if(COPT_END == o->type){ + if(0==(flags&COPT_NOERR_MSG)){ + fprintf(stderr, "Unknown option '%s'\n", argv[i]); + } + if(0==(flags & COPT_NOERR_UNKNOWN)){ + return 0-i; + } + } + }else{ + /* short options */ + aopt++; /* skip first '-' */ + while(*aopt){ + + /* check autohelp */ + if(flags & COPT_AFLAG_AUTOHELP){ + if(*aopt == 'h' || *aopt == '?'){ + return COPT_EHELP; + } + } + + for(o = opts; COPT_END != o->type; o++){ + if(NULL == o->sopts) continue; + t = o->type&COPT_TYPE_MASK; + if(strchr(o->sopts, *aopt)){ + /* option found */ + char szopt[3]={'-',0,0}; + szopt[1] = *aopt; + + if(t == COPT_HELP){ + return COPT_EHELP; + } + if(aopt[1] == 0){ /* -p -b 13 */ + if(t == COPT_BOOL || t == COPT_BOOLI){ + if(0 == (flags&COPT_AFLAG_PRESCAN)){ + set_option_value(o, szopt, NULL); + } + }else{ + copt = o; + } + aopt++; + }else if(aopt[1] == '='){ /* -p=123 */ + if(0==(flags&COPT_AFLAG_PRESCAN) || t == COPT_CFGFILE){ + rc = set_option_value(o, argv[i], aopt+2); + if(rc<0){ + if(0==(flags&COPT_NOERR_MSG)){ + print_opt_help(stderr, o, flags|COPT_AFLAG_ERROR, argv[i]); + } + if(0==(flags&COPT_NOERR_ARG)){ + return -i; + } + } + } + while(*aopt) aopt++; + }else{ /* -p123*/ + if(0==(flags&COPT_AFLAG_PRESCAN) || t == COPT_CFGFILE){ + if( t == COPT_BOOL || t == COPT_BOOLI ){ + set_option_value(o, szopt, NULL); + }else{ + rc = set_option_value(o, szopt, aopt+1); + if(rc<0){ + if(0==(flags&COPT_NOERR_MSG)){ + print_opt_help(stderr, o, flags | COPT_AFLAG_ERROR, argv[i]); + } + if(0==(flags&COPT_NOERR_ARG)){ + return -i; + } + } + } + } + while(*aopt) aopt++; + } + break; + } + } + if(COPT_END == o->type){ + if(0==(flags&COPT_NOERR_MSG)){ + fprintf(stderr, "Unknown option '%s'\n", argv[i]); + } + if(0==(flags & COPT_NOERR_UNKNOWN)){ + return 0-i; + } + aopt++; + }else{ + /* move this option argument to the end of list */ + move_args(argv, argc-1, i, 1); + i--; + nargc--; + } + } + } + }else{ + /* check for nonoption callback */ + if(0 == (flags & COPT_AFLAG_PRESCAN)){ + if(onopt == NULL){ + onopt = (void*)-1; + for(o = opts; COPT_END != o->type; o++){ + if(o->type & COPT_CALLBACK){ + if(o->lopt == NULL && o->sopts == NULL && o->vptr){ + onopt = o; + break; + } + } + } + } + if(onopt != (void*)-1){ + copt_value_t v; + v.v_str = argv[i]; + rc = call_callback(onopt, NULL, &v); + if(rc < 0){ + if(rc == COPT_EHELP) return rc; + if(0==(flags&COPT_NOERR_ARG)){ + return -i; + } + } + } + } + } + }else{ /* prev is option */ + t = copt->type & COPT_TYPE_MASK; + if(0==(flags&COPT_AFLAG_PRESCAN) || t == COPT_CFGFILE){ + rc = set_option_value(copt, argv[i-1], argv[i]); + if(rc < 0){ + if(0==(flags&COPT_NOERR_MSG)){ + print_opt_help(stderr, o, flags | COPT_AFLAG_ERROR, argv[i - 1]); + } + if(0==(flags&COPT_NOERR_ARG)){ + return 0-i-1; + } + } + } + copt = NULL; + /* move this option argument to the end of list */ + move_args(argv, argc-1, i, 1); + nargc--; + i--; + } + } + if(copt){ + /* last option was not completed */ + if(0==(flags&COPT_NOERR_MSG)){ + print_opt_help(stderr, o, flags | COPT_AFLAG_ERROR, argv[i - 1]); + } + if(0==(flags&COPT_NOERR_ARG)){ + return 0-i-1; + } + } + return nargc; +} + +static int set_option_value(copt_t* opt, const char * arg, char* val) +{ + char* np; + copt_value_t value; + + value.v_boolean = 0; + + switch(opt->type & COPT_TYPE_MASK){ + case COPT_HELP: + case COPT_BOOL: + value.v_boolean = 1; + case COPT_BOOLI: + if(val){ + if( val[0] == '0' + ||0==strcasecmp(val, "false") + ||0==strcasecmp(val, "no") + ){ + value.v_boolean = !value.v_boolean; + }else if( !isdigit((int)(val[0])) + && strcasecmp(val, "true") + && strcasecmp(val, "yes") + ){ + return -1; + } + } + break; + case COPT_LONG: + if(!val) return -1; + value.v_long = strtol(val, &np, 0); + if(*np != 0) return -1; + break; + case COPT_ULONG: + if(!val) return -1; + value.v_ulong = strtoul(val, &np, 0); + if(*np != 0) return -1; + break; + case COPT_SHORT: + if(!val || !opt->vptr) return -1; + value.v_long = strtol(val, &np, 0); + if(*np != 0) return -1; + if(value.v_long>SHRT_MAX || value.v_longvptr) return -1; + value.v_ulong = strtoul(val, &np, 0); + if(*np != 0) return -1; + if(value.v_ulong>USHRT_MAX){ + return -1; + } + value.v_ushort = (unsigned short) value.v_ulong; + break; + + case COPT_CHAR: + if(!val || !opt->vptr) return -1; + if(0==val[1]){ + value.v_char = val[0]; + }else{ + value.v_ulong = strtoul(val, &np, 0); + if(*np != 0) return -1; + if(value.v_ulong>UCHAR_MAX){ + return -1; + } + value.v_char = (unsigned char)value.v_long; + } + break; + case COPT_STRENUM: + { + const char ** p = (const char**)opt->vptr; + for(; *p && strcmp(*p, val); p++); + if(NULL == *p){ + return -1; + } + value.v_long = p - (const char**)opt->vptr; + } + break; + case COPT_STRLIST: + if(!opt->vptr || (opt->type & COPT_CALLBACK)) return -1; + case COPT_CFGFILE: + case COPT_PATH: + case COPT_STR: + value.v_str = val; + break; + case COPT_HOST: + if(0 > copt_addr_assign(&value.v_ulong, val)){ + return -1; + } + break; + default: + return -1; + } + + if(opt->type & COPT_CALLBACK){ + return call_callback(opt, arg, &value); + } + + switch(opt->type & COPT_TYPE_MASK){ + case COPT_HELP: + case COPT_BOOL: + case COPT_BOOLI: + if(opt->vptr && opt->vptr != (void*)1){ + *(int*)opt->vptr = value.v_boolean; + }else{ + opt->vptr = ((char*)NULL) + value.v_boolean; + } + break; + case COPT_LONG: + case COPT_ULONG: + case COPT_CFGFILE: + case COPT_STR: + case COPT_PATH: + case COPT_HOST: + if(opt->vptr){ + *(void**)opt->vptr = value.v_str; + }else{ + opt->vptr = (void*)value.v_str; + } + break; + case COPT_STRLIST: + { + char ** p = opt->vptr; + *p = value.v_str; + opt->vptr = p + 1; + } + break; + case COPT_STRENUM: + opt->vptr = (void*)(((const char**)opt->vptr) + value.v_long); + break; + case COPT_SHORT: + case COPT_USHORT: + *(short*)opt->vptr = value.v_short; + break; + case COPT_CHAR: + *(char*)opt->vptr = value.v_char; + break; + default: + return -1; + }; + return 0; +} + +static int copt_addr_assign(unsigned long* addr,const char* val) +{ + unsigned long n = 0; + unsigned long s = 0; + int cnt = 0; + while(*val){ + if(*val == '.'){ + if(cnt == 3){ + return -1; + } + n = (n<<8) | s; + s = 0; + cnt++; + }else{ + unsigned int c = *val - '0'; + if(c > 9){ + return -1; + } + s = s*10 + c; + } + val++; + } + if(cnt != 3){ + return -1; + } + n = (n<<8) | s; + + *addr = n; + return 0; +} + +inline static int call_callback(const copt_t * opt, const char * option, const copt_value_t * value) +{ + if(opt->vptr){ + return ((copt_callback *)opt->vptr)(opt, option, value); + } + return -1; +} + +void coptions_help_ex(FILE * f, const char * prgname, int flags, copt_t* opt, const char* usagestr, + const char* header, const char* footer) +{ + const char * progname; + if(header){ + fputs(header, f); + } + if(prgname){ + progname = strrchr(prgname, '/'); + if(progname) progname++; + else progname = prgname; + if(usagestr){ + fprintf(f, "Usage: %s %s\n", progname, usagestr); + } + fprintf(f, "Options:\n"); + }else{ + if(usagestr){ + fprintf(f, "%s\n", usagestr); + } + } + while(opt->type != COPT_END){ + if(opt->sopts || opt->lopt){ + fprintf(f, " "); + print_opt_help(f, opt, flags, NULL); + } + opt++; + } + if(footer){ + fputs(footer, f); + } +} + +static int sprintf_option_value(const copt_t* const opt, char * const buf) +{ + int ret = 0; + switch(opt->type&COPT_TYPE_MASK){ + case COPT_BOOL: + if(opt->vptr && opt->vptr != (void*)1){ + ret = sprintf(buf, "%s", ((*(int*)opt->vptr)?"true":"false")); + }else{ + ret = sprintf(buf, "%s", ((opt->vptr)?"true":"false")); + } + break; + case COPT_BOOLI: + if(opt->vptr && opt->vptr != (void*)1){ + ret = sprintf(buf, "%s", (*(int*)opt->vptr)?"false":"true"); + }else{ + ret = sprintf(buf, "%s", (opt->vptr)?"false":"true"); + } + break; + case COPT_LONG: + ret = sprintf(buf, "%ld", *((long*)opt->vptr)); + break; + case COPT_ULONG: + ret = sprintf(buf, "%lu", *((unsigned long*)opt->vptr)); + break; + case COPT_SHORT: + ret = sprintf(buf, "%u", *((short*)opt->vptr)); + break; + case COPT_USHORT: + ret = sprintf(buf, "%u", *((unsigned short*)opt->vptr)); + break; + case COPT_CHAR: + ret = sprintf(buf, "%c", *((char*)opt->vptr)); + break; + case COPT_STR: + case COPT_CFGFILE: + case COPT_STRENUM: + if(*(char**)opt->vptr) + ret = sprintf(buf, "%s", *(char**)opt->vptr); + else + ret = sprintf(buf, "NULL"); + break; + case COPT_HOST: + { + unsigned int n = *(unsigned int*)opt->vptr; + sprintf(buf, "%u.%u.%u.%u", + n>>24, (n>>16)&0xFF, (n>>8)&0xFF, n&0xFF); + } + break; + } + return ret; +} + +static const char* valnames[] = { + NULL, /* COPT_BOOL */ + NULL, /* COPT_BOOLI */ + "num", /* COPT_LONG */ + "num", /* COPT_ULONG */ + "num", /* COPT_SHORT */ + "num", /* COPT_USHORT */ + "chr", /* COPT_CHAR */ + "str", /* COPT_STR */ + "addr",/* COPT_HOST */ + "str", /* COPT_STRLIST*/ + "str", /* COPT_STRENUM*/ + "file",/* COPT_CFGFILE*/ + NULL, /* COPT_HELP */ +}; + +static void print_opt_help(FILE* f, copt_t* opt, int flags, const char* errval) +{ + char *p, sz_help[256]; + p=sz_help; + int err = flags&COPT_AFLAG_ERROR; + int t=opt->type&COPT_TYPE_MASK; + if(opt->sopts){ + const char* po = opt->sopts; + while(*po){ + if(po != opt->sopts) (*p++) = '|'; + *(p++)='-'; *(p++)=(*po); + po++; + } + if(valnames[t]){ + p+=sprintf(p, " <%s>", valnames[t]); + } + } + if(opt->lopt){ + if(opt->sopts) p+=sprintf(p, " | "); + p+=sprintf(p, "--%s", opt->lopt); + if(valnames[t]){ + p+=sprintf(p, "=%s", valnames[t]); + } + } + if(opt->helpstr){ + while(p-sz_help<35){ + *(p++) = ' '; + } + p+=sprintf(p, " : %s", opt->helpstr); + } + if(err){ + *(p++)='\n'; *(p++)='\t'; + if(errval){ + p+=sprintf(p, "%s ", errval); + } + p+=sprintf(p, "Unsupported value"); + }else{ + if (0 == (flags & COPT_HELP_NOVALUES)){ + if (t != COPT_HELP && t != COPT_STRLIST && 0 == (opt->type&COPT_CALLBACK)){ + /* print default value */ + *(p++) = '['; + p += sprintf_option_value(opt, p); + *(p++) = ']'; + *p = 0; + } + } + } + fprintf(f, "%s\n", sz_help); +} + +int coptions_load(const char* filename, const char * section, int flags, copt_t* const opts) +{ + char* buf; + int line = 0; + int err = 0; + int in_sect = 0; + FILE* f = fopen(filename, "r"); + + if(f == NULL){ + if(0 == (flags & COPT_NOERR_MSG)){ + perror(filename); + } + return COPT_ERROR; + } + + buf = (char*)malloc(1024); + while(fgets(buf, 1024, f)){ + char *e, *val=NULL, *key = buf; + copt_t * o; + unsigned short t; + + err = 1; + line ++; + while(*key && isspace((int)(*key))) key++; + if(0 == *key)continue; + /* search for section */ + if(*key == '['){ + if(section){ + if(in_sect){ + /* section loading finished */ + in_sect =0; + break; + } + key++; + while(*key && isspace((int)(*key))) key++; + e = strchr(key, ']'); + if(e == NULL) goto error; + *e = 0; + if(0==strcmp(section, key)){ + in_sect = 1; + } + } + continue; + } + + if(section && 0== in_sect) continue; + if(*key == '#' || *key == ';') continue; + + e = strchr(key, '='); + if(NULL == e) goto error; + val = e+1; + while(e>key && isspace((int)(*(e-1))))e--; + if(e == key) goto error; + *e = 0; + + while(*val && isspace((int)(*val))) val ++; + + for(o=opts; o->type != COPT_END; o++){ + if(o->lopt && 0==strcmp(o->lopt, key)) + break; + } + if(COPT_END == o->type) { + err = 2; + goto error; + } + t = o->type & COPT_TYPE_MASK; + + e = val + strlen(val); + while(e>val && isspace((int)(*(e-1)))) *(--e) = 0; + + if( t == COPT_STR + || t == COPT_STRLIST + ){ + if(0==strcmp("NULL", val) || 0==strcmp("null", val)) val = NULL; + else val = strdup(val); + } + + if(set_option_value(o, key, val)){ + err = 3; + goto error; + } + + continue; + + error: + if(0 == (flags & COPT_NOERR_MSG)){ + switch(err){ + case 1: + fprintf(stderr, "%s:%d: Syntax error in config file\n", filename, line); + break; + case 2: + fprintf(stderr, "%s:%d: Option is unknown\n", filename, line); + break; + case 3: + fprintf(stderr, "%s:%d: Unsupported value: %s\n", filename, line, val); + break; + } + } + if(flags & COPT_ERR_CONFIG){ + break; + } + } + free(buf); + fclose(f); + return 0; +} diff --git a/tools/itscertgen/cshared/copts.h b/tools/itscertgen/cshared/copts.h index 4e78c162f8524f5369550160080f757dd776550d..6400ce3cc5eee8e02139536bcf3cfe116cbcca78 100644 --- a/tools/itscertgen/cshared/copts.h +++ b/tools/itscertgen/cshared/copts.h @@ -1,228 +1,229 @@ -/********************************************************************* -###################################################################### -## -## Created by: Denis Filatov -## Date : 10.11.2005 -## -## C command line arguments and simple config file parser -## -## Copyleft (c) 2003 - 2007 -## This code is provided under the CeCill-C license agreement. -###################################################################### -*********************************************************************/ -#ifndef copts_h -#define copts_h -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** @defgroup COPTS copts - Programm line option processing. - * @{ */ - -#ifndef DOXYGEN - typedef struct copt_t copt_t; - typedef enum coptflag_t coptflag_t; - typedef enum coptype_t coptype_t; - typedef enum copterr_t copterr_t; -#endif - /** @enum coptype_t - * Option types definitions. - */ - enum coptype_t{ - COPT_BOOL , /**< Boolean option. Doesn't wants for argument.*/ - COPT_BOOLI, /**< Inverted Boolean option. Doesn't wants for argument. */ - COPT_LONG , /**< Wants for long argument */ - COPT_ULONG, /**< Wants for unsigned long argument */ - COPT_SHORT, /**< Wants for short (16 bit) argument */ - COPT_USHORT, /**< Wants for unsigned short argument */ - COPT_CHAR , /**< Wants for char or unsigned char argument */ - COPT_STR , /**< Wants for string (const char *) arguments */ - COPT_HOST, /**< Wants for string (const char *) arguments. Checks url syntax. */ - COPT_STRLIST, /**< Wants for string list argument (const char *[]) - * Every time when this opion will be occuren in argv - * the value will be assigned to given pointer and - * this pointer will be incremented. */ - COPT_STRENUM, /**< Wants for one of the string in the array given by vptr) - * Array of strings must be terminated by NULL pointer. - * After option found the vptr pointer will point to the - * to the element given by option */ - COPT_CFGFILE, /**< Wants for string (const char *) arguments. - * Treat it as config file name and load if found. - * If one or more config file options are exists in copt_t list - * this options will be executed before any other options parsing */ - COPT_HELP, /**< Do not wants for argument. - * If this option is occured in command line parsing will be - * terminated imediate and COPT_EHELP will be returned */ - - - COPT_TYPE_MASK = 0x00FF, /**< Option type mask. For internal usage. */ - COPT_CALLBACK = 0x4000, /**< Mask. Can be or-ed with any other option. - * That's mean treat vptr as a callback addres to call - * when option is occured */ - COPT_CONFIG = 0x8000, /**< Mask. Can be or-ed with any other option. - * That's mean this option can be reached from config file - * and have to be writen to.*/ - - COPT_END = 0xFFFF, /**< End of options. - * If vptr is not NULL, treat it as callback to call for unknown - * options and non-option values */ - }; - -#define COPT_INT COPT_LONG -#define COPT_UINT COPT_ULONG -#define COPT_IBOOL COPT_BOOLI - - /** Main options item. - * Have to be used to define options items. - * Short and long options can be defined. - * Possible options notations: - * - Boolean options: - * - -o - * - --option - * - Other types except of boolean: - * - -o value - * - -o=value - * - --option=value - */ - struct copt_t - { - const char* sopts; /**< Short options. */ - const char* lopt; /**< Long option. */ - const coptype_t type; /**< Option type ( see @ref coptype_t ). */ - void* vptr; /**< Option variable pointer. */ - const char* helpstr; /**< Option help string. */ - }; - - /** - * Execute option parser. - * @param argc Command line parameters count (from arguments of main() for example). - * @param argv Array of command line parameters. - * @param flags Configuration flags ( @ref coptflag_t ). - * @param opts Array of possible options. Must be finished by item with COPT_END type. - * @return
  • On success returns the index of the first option argument in the arguments array.
  • On error returns negative index of the invalid option in the arguments array.
- */ - int coptions(int argc, char* argv[], int flags, copt_t* opts); - - /** Get enum index from the option variable. - * @param opts @ref copt_t array. - * @param idx Index of the enum option in the array. - * @param ptr The initial value of the @a vptr field of the opion array item. - * @return the integer enum value of the selected item. - */ -#define copts_enum_value(opts,idx,ptr) \ - ((const char **)((opts)[idx]).vptr) - ((const char **)(ptr)) - - /** - * Load options config file. - * @param filename File path to load. - * @param section If not NULL then try to find the last occurance of the - * given section or load the file complet. - * @param flags Configuration flags ( see @ref coptflag_t ). - * @param opts The Array of possible option records. Must be finished - * by the item with COPT_END type. - * @return -
  • On success returns 0.
  • On error returns negative line number of the invalid expression.
- */ - int coptions_load(const char* filename, const char * section, int flags, copt_t* const opts); - - /** - * Save current options to the file - */ - int coptions_save(const char* filename, const copt_t* const opts); - - /** - * Save current options to already opened file - */ - int coptions_fsave(FILE * fd, const copt_t* const opts); - - /** - * Generate and print the help page. - * @param fd File descriptor to print the resulting help page. - * @param prgname Application name. Can be taken from argv[0]. - * @param opt Options array. - * @param usagestr The string to print before option list. - * @param header Help page header. - * @param footer Help page footer. - - */ - void coptions_help_ex(FILE * fd, const char * prgname, int flags, copt_t* opt, const char* usagestr, - const char* header, const char* footer); - /** The lite version of the @ref coptions_help_ex. - * @param fd File descriptor to print the resulting help page. - * @param prgname Application name. Can be taken from argv[0]. - * @param opt Options array. - * @param usagestr The string to print before option list. - */ -#define coptions_help(fd,prgname,flags,opt,usagestr) \ - coptions_help_ex(fd,prgname,flags,opt,usagestr,NULL,NULL) - - /** Wild value definition */ - typedef union{ - int v_boolean; - signed short v_short; - unsigned short v_ushort; - signed long v_long; - unsigned long v_ulong; - char v_char; - char * v_str; - }copt_value_t; - - /** The type of callback function to be called for the option having - @ref COPT_CALLBACK bit set in the @e type field of the @ref copt_t structure. - - These functions must return zero if option was successfully processed, - @ref COPT_EHELP to generate option help string or negative value when - some error was occured. - @param opt The current item of the options array. - @param option String option given. - @param value Pointer to the option value. - */ - - typedef int copt_callback(const copt_t * opt, const char * option, const copt_value_t * value); - -/** Inverted Boolean True. */ -#define IBOOL_YES ((void*)-1) - -/** @enum coptflag_t - Option flag mask values. -*/ - enum coptflag_t - { - COPT_DEFAULT = 0x0000, /**< No special flags given. */ - COPT_NOAUTOHELP = 0x0001, /**< Does not provide automatic help messages. */ - COPT_NOCONFIG = 0x0002, /**< Does not search for config files. */ - COPT_NOREORDER = 0x0004, /**< Does not reorder command line array. */ - COPT_NOERR_MSG = 0x0010, /**< Be silent. */ - COPT_NOERR_UNKNOWN = 0x0020, /**< Treat unknown options as non-option args.*/ - COPT_NOERR_ARG = 0x0040, /**< Does not produce an error if the required - option's argument is omited or have - incompatible type. */ - COPT_NOERR = 0x0070, /**< Does not produce any errors. */ - COPT_ERR_CONFIG = 0x0080, /**< Does not produce config errors. */ - COPT_NOHELP_MSG = 0x0100, /**< Does not print help messages. */ - COPT_HELP_NOVALUES = 0x0200, /**< Does not print default values. */ - }; - - /** @{ - @ref coptions return codes. - */ - /** Help option (-h or --help) vaw invoked. Need to print help page.*/ -#define COPT_EHELP ((int)(0x80000001)) - /** Some error was occured.*/ -#define COPT_ERROR ((int)(0x80000002)) -#define COPT_ERC(rc) (rc < 0 && 0==(rc & 0x8000000)) - /**@}*/ - -/** @} */ - -/** - * @example test_copts.c - */ - -#ifdef __cplusplus -} -#endif - -#endif +/********************************************************************* +###################################################################### +## +## Created by: Denis Filatov +## Date : 10.11.2005 +## +## C command line arguments and simple config file parser +## +## Copyleft (c) 2003 - 2007 +## This code is provided under the CeCill-C license agreement. +###################################################################### +*********************************************************************/ +#ifndef copts_h +#define copts_h +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** @defgroup COPTS copts - Programm line option processing. + * @{ */ + +#ifndef DOXYGEN + typedef struct copt_t copt_t; + typedef enum coptflag_t coptflag_t; + typedef enum coptype_t coptype_t; + typedef enum copterr_t copterr_t; +#endif + /** @enum coptype_t + * Option types definitions. + */ + enum coptype_t{ + COPT_BOOL , /**< Boolean option. Doesn't require arguments.*/ + COPT_BOOLI, /**< Inverted Boolean option. Doesn't require arguments. */ + COPT_LONG , /**< Requires for long argument */ + COPT_ULONG, /**< Requires for unsigned long argument */ + COPT_SHORT, /**< Requires for short (16 bit) argument */ + COPT_USHORT, /**< Requires for unsigned short argument */ + COPT_CHAR , /**< Requires for char or unsigned char argument */ + COPT_STR , /**< Requires for string (const char *) arguments */ + COPT_HOST, /**< Requires for string (const char *) arguments. Checks url syntax. */ + COPT_PATH, /**< Requires for string (const pchar_t *) arguments. */ + COPT_STRLIST, /**< Requires for string list argument (const char *[]) + * Every time when this opion will be occuren in argv + * the value will be assigned to given pointer and + * this pointer will be incremented. */ + COPT_STRENUM, /**< Requires for one of the string in the array given by vptr) + * Array of strings must be terminated by NULL pointer. + * After option found the vptr pointer will point + * to the element corresponding to the argument */ + COPT_CFGFILE, /**< Requires for string (const pchar_t *) arguments. + * Treat it as config file name and load if found. + * If one or more config file options are exists in copt_t list + * this options will be executed before any other options parsing */ + COPT_HELP, /**< Does't require argument. + * If this option is occured in command line parsing will be + * terminated imediate and COPT_EHELP will be returned */ + + + COPT_TYPE_MASK = 0x00FF, /**< Option type mask. For internal usage. */ + COPT_CALLBACK = 0x4000, /**< Mask. Can be or-ed with any other option. + * That's mean treat vptr as a callback addres to call + * when option is occured */ + COPT_CONFIG = 0x8000, /**< Mask. Can be or-ed with any other option. + * That's mean this option can be reached from config file + * and have to be writen to.*/ + + COPT_END = 0xFFFF, /**< End of options. + * If vptr is not NULL, treat it as callback to call for unknown + * options and non-option values */ + }; + +#define COPT_INT COPT_LONG +#define COPT_UINT COPT_ULONG +#define COPT_IBOOL COPT_BOOLI + + /** Main options item. + * Have to be used to define options items. + * Short and long options can be defined. + * Possible options notations: + * - Boolean options: + * - -o + * - --option + * - Other types except of boolean: + * - -o value + * - -o=value + * - --option=value + */ + struct copt_t + { + const char* sopts; /**< Short options. */ + const char* lopt; /**< Long option. */ + const coptype_t type; /**< Option type ( see @ref coptype_t ). */ + void* vptr; /**< Option variable pointer. */ + const char* helpstr; /**< Option help string. */ + }; + + /** + * Execute option parser. + * @param argc Command line parameters count (from arguments of main() for example). + * @param argv Array of command line parameters. + * @param flags Configuration flags ( @ref coptflag_t ). + * @param opts Array of possible options. Must be finished by item with COPT_END type. + * @return
  • On success returns the index of the first option argument in the arguments array.
  • On error returns negative index of the invalid option in the arguments array.
+ */ + int coptions(int argc, char* argv[], int flags, copt_t* opts); + + /** Get enum index from the option variable. + * @param opts @ref copt_t array. + * @param idx Index of the enum option in the array. + * @param ptr The initial value of the @a vptr field of the opion array item. + * @return the integer enum value of the selected item. + */ +#define copts_enum_value(opts,idx,ptr) \ + ((const char **)((opts)[idx]).vptr) - ((const char **)(ptr)) + + /** + * Load options config file. + * @param filename File path to load. + * @param section If not NULL then try to find the last occurance of the + * given section or load the file complet. + * @param flags Configuration flags ( see @ref coptflag_t ). + * @param opts The Array of possible option records. Must be finished + * by the item with COPT_END type. + * @return +
  • On success returns 0.
  • On error returns negative line number of the invalid expression.
+ */ + int coptions_load(const char* filename, const char * section, int flags, copt_t* const opts); + + /** + * Save current options to the file + */ + int coptions_save(const char* filename, const copt_t* const opts); + + /** + * Save current options to already opened file + */ + int coptions_fsave(FILE * fd, const copt_t* const opts); + + /** + * Generate and print the help page. + * @param fd File descriptor to print the resulting help page. + * @param prgname Application name. Can be taken from argv[0]. + * @param opt Options array. + * @param usagestr The string to print before option list. + * @param header Help page header. + * @param footer Help page footer. + + */ + void coptions_help_ex(FILE * fd, const char * prgname, int flags, copt_t* opt, const char* usagestr, + const char* header, const char* footer); + /** The lite version of the @ref coptions_help_ex. + * @param fd File descriptor to print the resulting help page. + * @param prgname Application name. Can be taken from argv[0]. + * @param opt Options array. + * @param usagestr The string to print before option list. + */ +#define coptions_help(fd,prgname,flags,opt,usagestr) \ + coptions_help_ex(fd,prgname,flags,opt,usagestr,NULL,NULL) + + /** Wild value definition */ + typedef union{ + int v_boolean; + signed short v_short; + unsigned short v_ushort; + signed long v_long; + unsigned long v_ulong; + char v_char; + char * v_str; + }copt_value_t; + + /** The type of callback function to be called for the option having + @ref COPT_CALLBACK bit set in the @e type field of the @ref copt_t structure. + + These functions must return zero if option was successfully processed, + @ref COPT_EHELP to generate option help string or negative value when + some error was occured. + @param opt The current item of the options array. + @param option String option given. + @param value Pointer to the option value. + */ + + typedef int copt_callback(const copt_t * opt, const char * option, const copt_value_t * value); + +/** Inverted Boolean True. */ +#define IBOOL_YES ((void*)-1) + +/** @enum coptflag_t + Option flag mask values. +*/ + enum coptflag_t + { + COPT_DEFAULT = 0x0000, /**< No special flags given. */ + COPT_NOAUTOHELP = 0x0001, /**< Does not provide automatic help messages. */ + COPT_NOCONFIG = 0x0002, /**< Does not search for config files. */ + COPT_NOREORDER = 0x0004, /**< Does not reorder command line array. */ + COPT_NOERR_MSG = 0x0010, /**< Be silent. */ + COPT_NOERR_UNKNOWN = 0x0020, /**< Treat unknown options as non-option args.*/ + COPT_NOERR_ARG = 0x0040, /**< Does not produce an error if the required + option's argument is omited or have + incompatible type. */ + COPT_NOERR = 0x0070, /**< Does not produce any errors. */ + COPT_ERR_CONFIG = 0x0080, /**< Does not produce config errors. */ + COPT_NOHELP_MSG = 0x0100, /**< Does not print help messages. */ + COPT_HELP_NOVALUES = 0x0200, /**< Does not print default values. */ + }; + + /** @{ + @ref coptions return codes. + */ + /** Help option (-h or --help) vaw invoked. Need to print help page.*/ +#define COPT_EHELP ((int)(0x80000001)) + /** Some error was occured.*/ +#define COPT_ERROR ((int)(0x80000002)) +#define COPT_ERC(rc) (rc < 0 && 0==(rc & 0x8000000)) + /**@}*/ + +/** @} */ + +/** + * @example test_copts.c + */ + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/tools/itscertgen/cshared/cring.c b/tools/itscertgen/cshared/cring.c new file mode 100644 index 0000000000000000000000000000000000000000..075fc09389ec360f93738d3d3b3d29222a2e793d --- /dev/null +++ b/tools/itscertgen/cshared/cring.c @@ -0,0 +1,308 @@ +/********************************************************************* +###################################################################### +## +## Created by: Denis Filatov +## Date : 10.11.2005 +## +## Copyleft (c) 2003 - 2015 +## This code is provided under the CeCill-C license agreement. +###################################################################### +*********************************************************************/ +#include "cring.h" +#include "cmem.h" +#include +void _cring_init( cring_t * const r ) +{ + r->next = r; + r->prev = r; +} + +cring_t * _cring_erase( cring_t * const x ) +{ + cring_t * n = x->next; + cring_t * p = x->prev; + n->prev = p; + p->next = n; + x->next = x; + x->prev = x; + return n; +} + +cring_t * _cring_insert_after( cring_t * const r, cring_t * const i) +{ + cring_t * n = r->next; + i->prev = r; + r->next = i; + i->next = n; + n->prev = i; + return n; +} + +cring_t * _cring_insert_before( cring_t * const r, cring_t * const i) +{ + cring_t * p = r->prev; + i->next = r; + r->prev = i; + i->prev = p; + p->next = i; + return p; +} + +cring_t * _cring_insert_ring_after( cring_t * const p, cring_t * const b) +{ + cring_t *n, *e; + + if(b->next == b){ + return _cring_insert_after(p, b); + } + + n = p->next; + e = b->prev; + + p->next = b; + b->prev = p; + n->prev = e; + e->next = n; + return n; +} + +cring_t * _cring_insert_ring_before( cring_t * const n, cring_t * const b) +{ + cring_t *p, *e; + + if(b->next == b){ + return _cring_insert_before(n, b); + } + p = n->prev; + e = b->prev; + + p->next = b; + b->prev = p; + n->prev = e; + e->next = n; + return p; +} + +cring_t * _cring_erase_ring( cring_t * const f, cring_t * const l) +{ + cring_t *p, *n; + + if(f == l){ + return _cring_erase(f); + } + + p = f->prev; + n = l->next; + + f->prev = l; + l->next = f; + + p->next = n; + n->prev = p; + return n; +} + +int cring_is_empty( cring_t * const r ) +{ + return r->next == r; +} + +void cring_cleanup(cring_t * const r, void * const fn_destructor) +{ + while(r->next != r){ + cring_t * x = r->next; + _cring_erase(x); + if(fn_destructor){ + ((void(*)(void*))fn_destructor)(x); + } + } +} + +cring_t * _cring_insert_sorted(cring_t * const r, cring_t * const n, cring_compare_fn * const fn_compare) +{ + cring_t * i = r->next; + for (; i != r; i = i->next){ + int x = fn_compare(i, n); + if (x == 0) + return i; + if (x > 0) /* i > n */ + break; + } + _cring_insert_before(i, n); + return n; +} + +cring_t * _cring_find_sorted(cring_t * const r, cring_t * const n, cring_compare_fn * const fn_compare) +{ + cring_t * i = r->next; + for (; i != r; i = i->next){ + int x = fn_compare(i, n); + if (x == 0) + return i; + if (x > 0) /* i > n */ + break; + } + return NULL; +} + +cring_t * cring_zerase( cring_t * * const root, cring_t * const r ) +{ + if(r->next == r){ + *root = NULL; + return r; + } + + if(*root == r){ + *root = r->next; + } + return _cring_erase(r); +} + +cring_t * cring_zinsert_after (cring_t * * const root, cring_t * const i) +{ + if(*root == NULL) { + *root = i; + return i; + } + return _cring_insert_ring_after(*root, i); +} + +cring_t * cring_zinsert_before(cring_t * * const root, cring_t * const i) +{ + if(*root == NULL) { + *root = i; + return i; + } + return _cring_insert_ring_before(*root, i); +} + +cring_t * cring_zerase_ring( cring_t * * const root, cring_t * const f, cring_t * const l) +{ + cring_t *i, *ret; + if(f->prev == l){ + /* remove full ring */ + *root = NULL; + return NULL; + } + + ret = _cring_erase_ring( f, l); + + /* need to check if *root occurs in erased space */ + i = f; + do{ + if(i == *root){ + *root = ret->prev; + break; + } + if(i == l){ + break; + } + i=i->next; + }while(1); + return ret; +} + +void cring_zcleunup( cring_t * * const root, void * const fn_destructor) +{ + cring_t * r = *root; + if(r){ + *root = NULL; + do{ + cring_t * n = r->next; + _cring_erase(r); + if(fn_destructor){ + ((void(*)(void*))fn_destructor)(r); + } + if(r == n){ + break; + } + r=n; + }while(1); + } +} + + +static cring_t __xcring_pool = {&__xcring_pool, &__xcring_pool}; + +xcring_t * xcring_new (void * const data) +{ + xcring_t * r; + if(__xcring_pool.next == &__xcring_pool){ + int i; + /* preallocate PREALLOC_D items */ + r = callocate(sizeof(xcring_t)*XRING_PREALLOC); + if(NULL == r) return NULL; + for(i = 0; inext = r; + r->prev = r; + r->data = NULL; + _cring_insert_after( &__xcring_pool, (cring_t *) r); + } + /* use last allocated item */ + r->next = r; + r->prev = r; + }else{ + r = (xcring_t *)__xcring_pool.next; + _cring_erase( __xcring_pool.next ); + } + r->data = data; + return r; +} + +void xcring_free(xcring_t * const r, void * const fn_destructor) +{ + if(r->data){ + if(fn_destructor){ + ((void(*)(void*))fn_destructor)(r->data); + } + r->data = NULL; + } + _cring_insert_after( &__xcring_pool, (cring_t *) r); +} + +void xcring_cleanup(cring_t * const root, void * const fn_destructor) +{ + while(!cring_is_empty(root)){ + xcring_t * r = (xcring_t *)root->next; + _cring_erase(root->next); + xcring_free(r, fn_destructor); + } +} + +xcring_t * xcring_enqueue(cring_t * const root, void * const data) +{ + xcring_t * r = xcring_new (data); + _cring_insert_before(root, (cring_t*)r); + return r; +} + +void * xcring_dequeue(cring_t * const root) +{ + void * data; + xcring_t * r; + + if(root->next == root){ + return NULL; + } + + r = (xcring_t*)root->next; + _cring_erase((cring_t*)r); + data=r->data; + r->data = NULL; + _cring_insert_after( &__xcring_pool, (cring_t *) r); + return data; +} + +void * xcring_find (cring_t * const root, void * const pattern, + int(*comparator)(const void * const pattern, + const void * const data)) +{ + xcring_t * r = (xcring_t *)root->next; + for(;r!=(xcring_t *)root; r=r->next){ + if(0==comparator(pattern, r->data)){ + return r->data; + } + } + return NULL; +} + diff --git a/tools/itscertgen/cshared/cring.h b/tools/itscertgen/cshared/cring.h new file mode 100644 index 0000000000000000000000000000000000000000..63b5be71d27f157db2ce1ff189a6d81cc461d330 --- /dev/null +++ b/tools/itscertgen/cshared/cring.h @@ -0,0 +1,211 @@ +/********************************************************************* +###################################################################### +## +## Created by: Denis Filatov +## Date : 10.11.2005 +## +## Copyleft (c) 2003 - 2015 +## This code is provided under the CeCill-C license agreement. +###################################################################### +*********************************************************************/ +#ifndef cring_h +#define cring_h + +/** @defgroup CRING Double-Linked Rings. + * @{ + The @ref cring_t structure it is the easiest way to organize rounded + double-linked lists of any data items. Insertion and removing of items + are very lightweight operations. Rings are usefull for any types of + queues, stacks and other types of data storage does not need the index-based + access to the items. + + Every ring item have two pointers to the next and previous ones. This + structure can be embedded to the data item structure at the begining or + in any position (see @ref cring_cast). + + Here we have 3 data items inserted to the ring, headed by @c items member of + Container structure. + + @code ++==============+ +----->+===========+<--------+ +I Container I | I Data item I | +I structure I | I cring_t I | +I I | I {next}--I-----+ | +I . . . I | +---I---{prev} I | | +I I | | I . . . I | | +Icring_t items I<-|--+ +===========+ +----->+===========+ +I {next}-I--+ | | | I Data item I +I {prev}-I---+ | | | I cring_t I +I I | | | +--I---{prev} I +I . . . I | | +===========+<-----------I---{next} I +I I +---->I Data item I | I . . . I +I I | I cring_t I | +===========+ +I I | I {prev}--I-----+ +I I +---I---{next} I +I I I . . . I ++==============+ +===========+ +@endcode + The next and prev pointer of an empty @ref cring_t item points to itself. + */ +#ifdef _MSC_VER +#define __typeof__ __typeof +#endif + +typedef struct cring_t cring_t; + +/** Base ring structure. */ +struct cring_t +{ + cring_t * next; /**< pointer to the next ring element. */ + cring_t * prev; /**< pointer to the next ring element. */ +}; + +#ifndef offsetof +/** @def offsetof(TYPE, MEMBER) + Return the offset of given member from the begining of the given + structure type. + @param TYPE Structure type. + @param MEMBER Structure member name. + + Example: + @code + struct Base { + int n1; + int n2; + . . . + }; + int ofs = + @endcode + In this example offsetof(Base,n2) is equal to 4 (sizeof(int)) + */ + #define offsetof(TYPE, MEMBER) (unsigned int) &((TYPE *) 0)->MEMBER +#endif + +/** Cast the pointer to the member of structure to the pointer + to the base structure. + * @param TYPE Base structure type. + * @param MEMBER Member of the base structure. + * @param PTR Pointer to this member. + * @return Pointer to the base structure. + * + * Example: + * The base structure looks like: + * @code + * struct Base { + * cring_t ring1; + * cring_t ring2; + * . . . + * }; + * @endcode + * We need to organize this type objects to two independent rings. + * @code + * static cring_t r1; + * static cring_t r2; + * @endcode + * We will use @a ring1 member for work with the first ring and @a ring2 + * for the second one. + * To take the first item in @c r1 we need just cast @c r1.next to the + * (Base *) type or use the special macro. + * @code + * struct Base * ptr = cring_next_cast(&r1, struct Base); + * @endcode + * But we will make the same operations for the second ring, we will got + * a pointer to the @a ring2 member of Base of the first ring item. + * To cast it to the @a Base type we need for @a cring_cast. + * @code + * struct cring_t * r = cring_next(&r2); + * struct Base * ptr = cring_cast(struct Base, ring2, r); + * @endcode + */ +#define cring_cast(TYPE,MEMBER,PTR) ( (TYPE*) ((char*)(PTR) - offsetof(TYPE,MEMBER))) + +/** Return next ring item. + * @param x Pointer could been converted to the @ref cring_t structure. + * @return Next ring item automaticaly converted to the type of x. */ +#define cring_next(x) ((__typeof__(x))(((cring_t*)(x))->next)) + +/** Return previous ring item. + * @param x Pointer could been converted to the @ref cring_t structure. + * @return Previous ring item automaticaly converted to the type of x. */ +#define cring_prev(x) ((__typeof__(x))(((cring_t*)(x))->prev)) + +/** Return next ring item with cast conversion. + * @param x Pointer could been converted to the @ref cring_t structure. + * @param t Type to cast to. + * @return Next ring item converted to the given type. */ +#define cring_next_cast(x,t) ((t*)((cring_t*)(x))->next) + +/** Return next ring item with cast conversion. + * @param x Pointer could been converted to the @ref cring_t structure. + * @param t Type to cast to. + * @return Previous ring item converted to the given type. */ +#define cring_prev_cast(x,t) ((t*)((cring_t*)(x))->prev) + +/** Return first ring item with cast conversion + * @param x Root ring item. + * @param t Type to cast to. + * @return The first item of the ring converted to the given type. */ +#define cring_first_cast(x,t) cring_next_cast(&x,t) + +/** Return last ring item with cast conversion + * @param x Root ring item. + * @param t Type to cast to. + * @return The last item of the ring converted to the given type. */ +#define cring_last_cast(x,t) cring_prev_cast(&x,t) + +void _cring_init ( cring_t * const r ); +#define cring_init(R) _cring_init((cring_t*)(R)) +cring_t * _cring_erase ( cring_t * const r ); +#define cring_erase(R) _cring_erase((cring_t*)(R)) +cring_t * _cring_insert_after(cring_t * const r, cring_t * const i); +#define cring_insert_after(R,I) _cring_insert_after((cring_t*)(R), (cring_t*)(I)) +cring_t * _cring_insert_before(cring_t * const r, cring_t * const i); +#define cring_insert_before(R,I) _cring_insert_before((cring_t*)(R), (cring_t*)(I)) +#define cring_enqueue(R,I) cring_insert_before(R,I) +cring_t * _cring_insert_ring_after(cring_t * const r, cring_t * const i); +#define cring_insert_ring_after(R,I) _cring_insert_ring_after((cring_t*)(R), (cring_t*)(I)) +cring_t * _cring_insert_ring_before(cring_t * const r, cring_t * const i); +#define cring_insert_ring_before(R,I) _cring_insert_ring_before((cring_t*)(R), (cring_t*)(I)) +cring_t * _cring_erase_ring(cring_t * const from, cring_t * const to); +#define cring_erase_ring(F,T) _cring_erase_ring((cring_t*)(F), (cring_t*)(T)) +int cring_is_empty(cring_t * const r); + +void cring_cleanup(cring_t * const r, void * const fn_destructor); + +typedef int cring_compare_fn(void * const p1, void * const p2); +cring_t * _cring_insert_sorted(cring_t * const r, cring_t * const i, cring_compare_fn * const fn_compare); +cring_t * _cring_find_sorted(cring_t * const r, cring_t * const n, cring_compare_fn * const fn_compare); +#define cring_insert_sorted(R,I,C) _cring_insert_sorted((cring_t*)(R), (cring_t*)(I), (cring_compare_fn *)(C)) +#define cring_find_sorted(R,I,C) _cring_find_sorted((cring_t*)(R), (cring_t*)(I), (cring_compare_fn *)(C)) + +cring_t * cring_zerase( cring_t * * const root, cring_t * const r ); +cring_t * cring_zerase_ring( cring_t * * const root, cring_t * const from, cring_t * const to); +cring_t * cring_zinsert_after (cring_t * * const root, cring_t * const i); +cring_t * cring_zinsert_before(cring_t * * const root, cring_t * const i); +void cring_zcleunup( cring_t * * const root, void * const fn_destructor); + + + +#define XRING_PREALLOC 16 + +typedef struct xcring_t +{ + struct xcring_t * next; + struct xcring_t * prev; + void * data; +} xcring_t; + +xcring_t * xcring_new (void * const data); +void xcring_free(xcring_t * const r, void * const fn_destructor); +void xcring_cleanup(cring_t * const root, void * const fn_destructor); +#define xcring_data(TYPE,R) ( (TYPE*) ((xcring_t*)R)->data ) + +xcring_t * xcring_enqueue(cring_t * const root, void * const data); +void * xcring_dequeue(cring_t * const root); +void * xcring_find (cring_t * const root, void * const pattern, + int(*comparator)(const void * const pattern, + const void * const data)); + +/** @} */ + +#endif diff --git a/tools/itscertgen/cshared/cserialize.c b/tools/itscertgen/cshared/cserialize.c index 4be561ae53f5931b9b62656ffac4e6d3443ee995..0b731355764fab981adf68c3b337fc15cb75315d 100644 --- a/tools/itscertgen/cshared/cserialize.c +++ b/tools/itscertgen/cshared/cserialize.c @@ -1,260 +1,375 @@ -/********************************************************************* -###################################################################### -## -## Created by: Denis Filatov -## -## Copyleft (c) 2003 - 2015 -## This code is provided under the CeCill-C license agreement. -###################################################################### -*********************************************************************/ - -#include "cserialize.h" -#include - -#ifdef __GNUC__ -#define cint_cpy(D,N,S) __builtin_memcpy((char*)(D),N,S) -#else -#include -#define cint_cpy(D,N,S) memcpy((char*)(D),N,S) -uint64_t cint64_swap(uint64_t); -uint32_t cint32_swap(uint32_t); -uint16_t cint16_swap(uint16_t); -static const uint64_t one64 = 1; -#endif - -int _cint64_write(const uint64_t value, char** const ptr, const char* const end, int * const error) -{ - register unsigned char* p = (unsigned char*)*ptr; - if(p + 8 <= (unsigned char*)end){ - if(0 == (((int)p)&0x7)){ - *((uint64_t*)p) = cint64_hton(value); - p+=8; - }else{ - int i; - for(i=7; i>=0; i--){ - *p++ = (value>>(8*i))&0xFF; - } - } - *ptr = (char*)p; - if(error) *error = 0; - return 0; - } - if(error) *error = ENOMEM; - return -1; -} - -int _cint32_write(const uint32_t value, char** const ptr, const char* const end, int * const error) -{ - register unsigned char* p = (unsigned char*)*ptr; - if(p + 4 <= (unsigned char*)end){ - if(0 == (((int)p)&0x3)){ - *((uint32_t*)p) = cint32_hton(value); - p+=4; - }else{ - int i; - for(i=3; i>=0; i--){ - *p++ = (value>>(8*i))&0xFF; - } - } - *ptr = (char*)p; - if(error) *error = 0; - return 0; - } - if(error) *error = ENOMEM; - return -1; -} - -int _cint16_write(const uint16_t value, char** const ptr, const char* const end, int * const error) -{ - register unsigned char* p = (unsigned char*)*ptr; - if(p + 2 <= (unsigned char*)end){ - *p++ = (value>>8)&0xFF; - *p++ = value&0xFF; - *ptr = (char*)p; - if(error) *error = 0; - return 0; - } - if(error) *error = ENOMEM; - return -1; -} - -int _cint8_write(const uint8_t value, char** const ptr, const char* const end, int * const error) -{ - if(*ptr < end) { - *((uint8_t*)*ptr) = value; - (*ptr) ++; - if(error) *error = 0; - return 0; - } - if(error) *error = ENOMEM; - return -1; -} - -uint64_t cint64_read(const char** const ptr, const char* const end, int * const error) -{ - uint64_t value; - register const uint8_t * p = (const uint8_t *)*ptr; - if(p + 8 <= (const uint8_t *)end) { - if(0 == (((int)p)&0x7)){ - value = cint64_hton(*p); - *ptr = (char*)(p+8); - }else{ - int i; - value=0; - for(i=0; i<8; i++){ - value = (value<<8) | *(p++); - } - *ptr = (char*)p; - } - if(error) *error = 0; - }else{ - value = (unsigned)-1; - if(error) *error = ENOMEM; - } - return value; -} - -uint32_t cint32_read(const char** const ptr, const char* const end, int * const error) -{ - uint32_t value; - register const uint8_t * p = (const uint8_t*)*ptr; - if(p + 4 <= (const uint8_t *)end) { - value = ((uint32_t)p[0])<<24 | ((uint32_t)p[1])<<16 | ((uint32_t)p[2])<<8 | p[3]; - *ptr = (char*)(p+4); - if(error) *error = 0; - }else{ - value = (unsigned)-1; - if(error) *error = ENOMEM; - } - return value; -} - -uint16_t cint16_read(const char** const ptr, const char* const end, int * const error) -{ - uint32_t value; - register const uint8_t * p = (const uint8_t*)*ptr; - if(p + 2 > (const uint8_t*)end) { - value = (unsigned)-1; - if(error) *error = ENOMEM; - }else{ - value = ((uint16_t)p[0])<<8 | p[1]; - *ptr = (const char*)(p+2); - if(error) *error = 0; - } - return value; -} - -uint8_t cint8_read(const char** const ptr, const char* const end, int * const error) -{ - if(error) *error = 0; - if(*ptr >= end) { - if(error) *error = ENOMEM; - return -1; - } - return *(const uint8_t*)((*ptr)++); -} - -int cintx_bytecount(uint64_t value) -{ - int num_bytes = 0; -#ifdef __GNUC__ - if(value){ - num_bytes = (64 + 6 - __builtin_clzll(value))/7; - }else{ - num_bytes = 1; - } -#else - uint64_t overflow = 0; - while(value >= overflow){ - num_bytes++; - overflow = one64 << (7*num_bytes); - } -#endif - return num_bytes; -} - -int _cintx_write (const uint64_t value, char ** const ptr, const char * const end, int * const error) -{ - int num_bytes = 0; - uint8_t c; - uint8_t *out = (uint8_t*)(*ptr); - num_bytes = cintx_bytecount(value); - if(num_bytes > 8 || out+num_bytes > ((const uint8_t*)end)){ - if(error) *error = ENOMEM; - return -1; - } - num_bytes--; - c = ~((1<<(8-num_bytes))-1); - c |= (value >> (num_bytes*8)) & 0xFF; - *out++ = c; - while(num_bytes){ - num_bytes--; - c = (value >> (num_bytes*8)) & 0xFF; - *out++ = c; - } - *ptr = (char*)out; - if(error) *error = 0; - return 0; -} - -static int countof1(int c) -{ - int r = 0; - while(c & 0x80){ -#ifdef __GNUC__ - return 1 + __builtin_clrsb(c<<24); -#else - r++; - c<<=1; -#endif - } - return r; -} - -uint64_t cintx_read(const char** const ptr, const char* const end, int * const error) -{ - uint8_t c; - const uint8_t* in; - int i, lead_ones; - in = (const uint8_t*)*ptr; - if(in <= (const uint8_t*)end){ - c = *in; - lead_ones = countof1(c); - if(in + 1 + lead_ones <= (const uint8_t*)end) { - uint64_t value; - value = c & ((1<<(7-lead_ones))-1); - for(i=1; i<=lead_ones; i++){ - value = (value<<8) | in[i]; - } - *ptr = (const char*)(in + 1 + lead_ones); - if(error) *error = 0; - return value; - } - } - if(error) *error = ENOMEM; - return -1; -} - -int cbuf_write(const void * const p, int length, char ** const ptr, const char * const end, int * const error) -{ - if(error) *error = 0; - if((*ptr) + length >= end) { - if(error) *error = ENOMEM; - return -1; - } - cint_cpy(*ptr, p, length); - *ptr = (*ptr) + length; - return 0; -} - -int cbuf_read (void * const p, int length, const char ** const ptr, const char * const end, int * const error) -{ - if(error) *error = 0; - if((*ptr) + length >= end) { - if(error) *error = ENOMEM; - return -1; - } - cint_cpy(p, *ptr, length); - *ptr = (*ptr) + length; - return 0; -} +/********************************************************************* +###################################################################### +## +## Created by: Denis Filatov +## +## Copyleft (c) 2003 - 2015 +## This code is provided under the CeCill-C license agreement. +###################################################################### +*********************************************************************/ + +#include "cserialize.h" +#include "cstr.h" +#include "e4c_lite.h" +#include +#include + +#ifdef __GNUC__ +#define cint_cpy(D,N,S) __builtin_memcpy((char*)(D),N,S) +#else +#define cint_cpy(D,N,S) memcpy((char*)(D),N,S) +static const uint64_t __one64 = 1; +#if !defined (_MSC_VER) +uint64_t cint64_swap(uint64_t); +uint32_t cint32_swap(uint32_t); +uint16_t cint16_swap(uint16_t); +#endif +#endif + +#define C_ERROR(E) \ + if (perror) *perror = E; \ + throw(RuntimeException, E, NULL) + +int _cint64_write(const uint64_t value, char** const ptr, const char* const end, int * const perror) +{ + register unsigned char* p = (unsigned char*)*ptr; + if (p + 8 > (unsigned char*)end){ + C_ERROR(ENOSPC); + return -1; + } + if (0 == (((intptr_t)p) & 0x7)){ + *((uint64_t*)p) = cint64_hton(value); + p+=8; + }else{ + int i; + for(i=7; i>=0; i--){ + *p++ = (value>>(8*i))&0xFF; + } + } + *ptr = (char*)p; + if (perror) *perror = 0; + return 0; +} + +int _cint32_write(const uint32_t value, char** const ptr, const char* const end, int * const perror) +{ + register unsigned char* p = (unsigned char*)*ptr; + if(p + 4 > (unsigned char*)end){ + C_ERROR(ENOSPC); + return -1; + } + if (0 == (((intptr_t)p) & 0x3)){ + *((uint32_t*)p) = cint32_hton(value); + p+=4; + }else{ + int i; + for(i=3; i>=0; i--){ + *p++ = (value>>(8*i))&0xFF; + } + } + *ptr = (char*)p; + if (perror) *perror = 0; + return 0; +} + +int _cint16_write(const uint16_t value, char** const ptr, const char* const end, int * const perror) +{ + register unsigned char* p = (unsigned char*)*ptr; + if (p + 2 > (unsigned char*)end){ + C_ERROR(ENOSPC); + return -1; + } + *p++ = (value >> 8) & 0xFF; + *p++ = value&0xFF; + *ptr = (char*)p; + if (perror) *perror = 0; + return 0; +} + +int _cint8_write(const uint8_t value, char** const ptr, const char* const end, int * const perror) +{ + if (*ptr >= end) { + C_ERROR(ENOSPC); + return -1; + } + if (perror) *perror = 0; + *((uint8_t*)*ptr) = value; + (*ptr) ++; + return 0; +} + +uint64_t cint64_read(const char** const ptr, const char* const end, int * const perror) +{ + uint64_t value; + register const uint8_t * p = (const uint8_t *)*ptr; + if (p + 8 > (const uint8_t *)end) { + C_ERROR(EFAULT); + return (unsigned)-1; + } + if (0 == (((intptr_t)p) & 0x7)){ + value = *(uint64_t*)p; + value = cint64_hton(value); + *ptr = (char*)(p+8); + }else{ + int i; + value=0; + for(i=0; i<8; i++){ + value = (value<<8) | *(p++); + } + *ptr = (char*)p; + } + if (perror) *perror = 0; + return value; +} + +uint32_t cint32_read(const char** const ptr, const char* const end, int * const perror) +{ + uint32_t value; + register const uint8_t * p = (const uint8_t*)*ptr; + if(p + 4 > (const uint8_t *)end) { + C_ERROR(EFAULT); + return (unsigned)-1; + } + if (perror) *perror = 0; + value = ((uint32_t)p[0]) << 24 | ((uint32_t)p[1]) << 16 | ((uint32_t)p[2]) << 8 | p[3]; + *ptr = (char*)(p+4); + return value; +} + +uint16_t cint16_read(const char** const ptr, const char* const end, int * const perror) +{ + uint32_t value; + register const uint8_t * p = (const uint8_t*)*ptr; + if (p + 2 > (const uint8_t *)end) { + C_ERROR(EFAULT); + return (uint16_t)-1; + } + if (perror) *perror = 0; + value = ((uint16_t)p[0]) << 8 | p[1]; + *ptr = (const char*)(p+2); + return value; +} + +uint8_t cint8_read(const char** const ptr, const char* const end, int * const perror) +{ + if (*ptr >= end) { + C_ERROR(EFAULT); + return (uint8_t)-1; + } + if (perror) *perror = 0; + return *(const uint8_t*)((*ptr)++); +} + +int cintx_bytecount(uint64_t value) +{ + int num_bytes = 0; +#ifdef __GNUC__ + if(value){ + num_bytes = (64 + 6 - __builtin_clzll(value))/7; + }else{ + num_bytes = 1; + } +#else + uint64_t overflow = 0; + while(value >= overflow){ + num_bytes++; + overflow = __one64 << (7*num_bytes); + } +#endif + return num_bytes; +} + +int _cintx_write (const uint64_t value, char ** const ptr, const char * const end, int * const perror) +{ + int num_bytes = 0; + uint8_t c; + uint8_t *out = (uint8_t*)(*ptr); + num_bytes = cintx_bytecount(value); + if(num_bytes > 8 || out+num_bytes > ((const uint8_t*)end)){ + C_ERROR(ENOSPC); + return (unsigned)-1; + } + num_bytes--; + c = ~((1<<(8-num_bytes))-1); + c |= (value >> (num_bytes*8)) & 0xFF; + *out++ = c; + while(num_bytes){ + num_bytes--; + c = (value >> (num_bytes*8)) & 0xFF; + *out++ = c; + } + *ptr = (char*)out; + if (perror) *perror = 0; + return 0; +} + +static int countof1(int c) +{ + int r = 0; + while(c & 0x80){ +#if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 407) && !defined(__INTEL_COMPILER) + return 1 + __builtin_clrsb(c<<24); +#else + r++; + c<<=1; +#endif + } + return r; +} + +uint64_t cintx_read(const char** const ptr, const char* const end, int * const perror) +{ + uint8_t c; + const uint8_t* in; + int i, lead_ones; + in = (const uint8_t*)*ptr; + if(in <= (const uint8_t*)end){ + c = *in; + lead_ones = countof1(c); + if(in + 1 + lead_ones <= (const uint8_t*)end) { + uint64_t value; + value = c & ((1<<(7-lead_ones))-1); + for(i=1; i<=lead_ones; i++){ + value = (value<<8) | in[i]; + } + *ptr = (const char*)(in + 1 + lead_ones); + if (perror) *perror = 0; + return value; + } + } + C_ERROR(EFAULT); + return (unsigned)-1; +} + +uint32_t cxsize_read(const char ** const ptr, const char * const end, int * const perror) +{ + uint32_t len = (uint32_t)cintx_read(ptr, end, perror); + if (perror == 0){ + if (*ptr + len > end){ + C_ERROR(EFAULT); + return (unsigned)-1; + } + } + if (perror) *perror = 0; + return len; +} + +int cbuf_write(const void * const p, int length, char ** const ptr, const char * const end, int * const perror) +{ + if((*ptr) + length > end) { + C_ERROR(ENOSPC); + return (unsigned)-1; + } + cint_cpy(*ptr, p, length); + *ptr = (*ptr) + length; + if (perror) *perror = 0; + return 0; +} + +int cbuf_read (void * const p, int length, const char ** const ptr, const char * const end, int * const perror) +{ + if((*ptr) + length > end) { + C_ERROR(EFAULT); + return -1; + } + if (p){ + cint_cpy(p, *ptr, length); + } + *ptr = (*ptr) + length; + if (perror) *perror = 0; + return 0; +} + +int cstr_write(const char * const p, char ** const ptr, const char * const end, int * const perror) +{ + int ret; + int len = cstrlen(p); + // write size + ret = cintx_write(len, ptr, end, perror); + if (ret == 0 && len > 0 ) { + ret = cbuf_write(p, len, ptr, end, perror); + } + return ret; +} + +int cstrn_write(const char * const p, int length, char ** const ptr, const char * const end, int * const perror) +{ + int ret; + int len = cstrnlen(p, length); + // write size + ret = cintx_write(len, ptr, end, perror); + if (ret == 0 && len > 0) { + ret = cbuf_write(p, len, ptr, end, perror); + } + return ret; +} + +int cstr_read(char * const p, const char ** const ptr, const char * const end, int * const perror) +{ + // read size + int len = (int)cintx_read(ptr, end, perror); + if (*perror == 0){ + return cbuf_read(p, len, ptr, end, perror); + } + return -1; +} + +int cstrn_read(char * const p, int length, const char ** const ptr, const char * const end, int * const perror) +{ + // read size + int len = (int)cintx_read(ptr, end, perror); + if (len <= length){ + // read buf + return cbuf_read(p, len, ptr, end, perror); + } + C_ERROR(EFAULT); + return -1; +} + +int cbookmark_store(cbookmark * bm, char ** const ptr, const char * const end, int * const perror) +{ + char * p = *ptr; + if (bm->idx >= sizeof(bm->ptrs) / sizeof(bm->ptrs[0])){ + C_ERROR(E2BIG); + return -1; + } + if (p >= end){ + C_ERROR(ENOSPC); + return -1; + } + bm->ptrs[bm->idx] = p; + bm->idx++; + *ptr = p + 1; + if (perror) *perror = 0; + return 0; +} + +int cbookmark_apply(cbookmark * bm, char ** const ptr, const char * const end, int * const perror) +{ + int size, bcount; + char *p, * psize; + + p = *ptr; + if (bm->idx == 0){ + C_ERROR(E2BIG); + return -1; + } + psize = bm->ptrs[--bm->idx]; + size = p - psize - 1; + bcount = cintx_bytecount(size); + if (bcount == 1){ + *(unsigned char*)psize = size; + size = 0; // return value; + } + else{ + if (p + bcount - 1 > end){ + C_ERROR(ENOSPC); + return (unsigned)-1; + } + memmove(psize + bcount, psize + 1, p - psize - 1); + *ptr = p + bcount - 1; + size = cintx_write(size, &psize, psize + bcount, perror); + } + if (perror) *perror = 0; + return size; // size is overridden to be 0 or -1 +} diff --git a/tools/itscertgen/cshared/cserialize.h b/tools/itscertgen/cshared/cserialize.h index b7ba4549ae01acc59930f799c8f8188ab7443f0b..7b1f434e3c9551c44f67f9725f3351758a793a8e 100644 --- a/tools/itscertgen/cshared/cserialize.h +++ b/tools/itscertgen/cshared/cserialize.h @@ -1,72 +1,94 @@ -/********************************************************************* -###################################################################### -## -## Created by: Denis Filatov -## -## Copyleft (c) 2003 - 2015 -## This code is provided under the CeCill-C license agreement. -###################################################################### -*********************************************************************/ - -#ifndef cint_h -#define cint_h -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#if defined(__GNUC__) -# define cint64_swap(X) __builtin_bswap64(X) -# define cint32_swap(X) __builtin_bswap32(X) -# define cint16_swap(X) __builtin_bswap16(X) -#elif defined (_MSC_VER) -# define cint64_swap(X) _byteswap_uint64(X) -# define cint32_swap(X) _byteswap_ulong (X) -# define cint16_swap(X) _byteswap_ushort(X) -#define __ORDER_LITTLE_ENDIAN__ 1 -#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ -#else -uint64_t cint64_swap(const uint64_t); -uint32_t cint32_swap(const uint32_t); -uint16_t cint16_swap(const uint16_t); -#endif -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ -# define cint64_hton(X) cint64_swap(X) -# define cint32_hton(X) cint32_swap(X) -# define cint16_hton(X) cint16_swap(X) -#else -# define cint64_hton(X) (X) -# define cint32_hton(X) (X) -# define cint16_hton(X) (X) -#endif /* __BYTE_ORDER__ */ - -/* serialisation */ -int _cint64_write(const uint64_t n, char ** const ptr, const char * const end, int * const error); -int _cint32_write(const uint32_t n, char ** const ptr, const char * const end, int * const error); -int _cint16_write(const uint16_t n, char ** const ptr, const char * const end, int * const error); -int _cint8_write (const uint8_t n, char ** const ptr, const char * const end, int * const error); -int _cintx_write (const uint64_t n, char ** const ptr, const char * const end, int * const error); - -#define cint64_write(N,P,S,E) _cint64_write((uint64_t)(N), P, S, E) -#define cint32_write(N,P,S,E) _cint32_write((uint32_t)(N), P, S, E) -#define cint16_write(N,P,S,E) _cint16_write((uint16_t)(N), P, S, E) -#define cint8_write(N,P,S,E) _cint8_write ((uint8_t)(N), P, S, E) -#define cintx_write(N,P,S,E) _cintx_write ((uint32_t)(N), P, S, E) - -uint64_t cint64_read (const char ** const ptr, const char * const end, int * const error); -uint32_t cint32_read (const char ** const ptr, const char * const end, int * const error); -uint16_t cint16_read (const char ** const ptr, const char * const end, int * const error); -uint8_t cint8_read (const char ** const ptr, const char * const end, int * const error); -uint64_t cintx_read (const char ** const ptr, const char * const end, int * const error); - -int cintx_bytecount(uint64_t); - -int cbuf_write(const void * const p, int length, char ** const ptr, const char * const end, int * const error); -int cbuf_read (void * const p, int length, const char ** const ptr, const char * const end, int * const error); - -#ifdef __cplusplus -} -#endif - -#endif +/********************************************************************* +###################################################################### +## +## Created by: Denis Filatov +## +## Copyleft (c) 2003 - 2015 +## This code is provided under the CeCill-C license agreement. +###################################################################### +*********************************************************************/ + +#ifndef cint_h +#define cint_h +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#include "e4c_lite.h" + +#if defined(__GNUC__) +# define cint64_swap(X) __builtin_bswap64(X) +# define cint32_swap(X) __builtin_bswap32(X) +# define cint16_swap(X) __builtin_bswap16(X) +#elif defined (_MSC_VER) +# define cint64_swap(X) _byteswap_uint64(X) +# define cint32_swap(X) _byteswap_ulong (X) +# define cint16_swap(X) _byteswap_ushort(X) +#define __ORDER_LITTLE_ENDIAN__ 1 +#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ +#else +uint64_t cint64_swap(const uint64_t); +uint32_t cint32_swap(const uint32_t); +uint16_t cint16_swap(const uint16_t); +#endif +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +# define cint64_hton(X) cint64_swap(X) +# define cint32_hton(X) cint32_swap(X) +# define cint16_hton(X) cint16_swap(X) +# define cint64_lsb(X) ((uint8_t)(((uint64_t)(X))>>56)) +# define cint32_lsb(X) ((uint8_t)(((uint32_t)(X))>24)) +# define cint24_lsb(X) ((uint8_t)(((uint32_t)(X))>16)) +# define cint64_lsb3(X) ((uint32_t)(((uint64_t)(X))>>40)) +# define cint32_lsb3(X) ((uint32_t)(((uint64_t)(X))>>8)) +#else +# define cint64_hton(X) (X) +# define cint32_hton(X) (X) +# define cint16_hton(X) (X) +# define cint64_lsb(X) ((uint8_t)((X)&0xFF)) +# define cint32_lsb(X) ((uint8_t)((X)&0xFF)) +# define cint24_lsb(X) ((uint8_t)((X)&0xFF)) +# define cint64_lsb3(X) ((uint32_t)((X)&0xFFFFFF)) +# define cint32_lsb3(X) ((uint32_t)((X)&0xFFFFFF)) +#endif /* __BYTE_ORDER__ */ + +/* serialisation */ +int _cint64_write(const uint64_t n, char ** const ptr, const char * const end, int * const error); +int _cint32_write(const uint32_t n, char ** const ptr, const char * const end, int * const error); +int _cint16_write(const uint16_t n, char ** const ptr, const char * const end, int * const error); +int _cint8_write (const uint8_t n, char ** const ptr, const char * const end, int * const error); +int _cintx_write (const uint64_t n, char ** const ptr, const char * const end, int * const error); + +#define cint64_write(N,P,S,E) _cint64_write((uint64_t)(N), P, S, E) +#define cint32_write(N,P,S,E) _cint32_write((uint32_t)(N), P, S, E) +#define cint16_write(N,P,S,E) _cint16_write((uint16_t)(N), P, S, E) +#define cint8_write(N,P,S,E) _cint8_write ((uint8_t)(N), P, S, E) +#define cintx_write(N,P,S,E) _cintx_write ((uint32_t)(N), P, S, E) + +uint64_t cint64_read (const char ** const ptr, const char * const end, int * const error); +uint32_t cint32_read (const char ** const ptr, const char * const end, int * const error); +uint16_t cint16_read (const char ** const ptr, const char * const end, int * const error); +uint8_t cint8_read (const char ** const ptr, const char * const end, int * const error); +uint64_t cintx_read (const char ** const ptr, const char * const end, int * const error); +uint32_t cxsize_read(const char ** const ptr, const char * const end, int * const error); +int cintx_bytecount(uint64_t); + +int cbuf_write(const void * const p, int length, char ** const ptr, const char * const end, int * const error); +int cbuf_read (void * const p, int length, const char ** const ptr, const char * const end, int * const error); + +typedef struct { + int idx; + void * ptrs[6]; +}cbookmark; +int cbookmark_store(cbookmark * bm, char ** const ptr, const char * const end, int * const error); +int cbookmark_apply(cbookmark * bm, char ** const ptr, const char * const end, int * const error); + +E4C_DECLARE_EXCEPTION(cexc_readbuf); +E4C_DECLARE_EXCEPTION(RuntimeException); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/tools/itscertgen/cshared/cshared.vcxproj b/tools/itscertgen/cshared/cshared.vcxproj index bdd438b6a6fe1bd994009647395ab54d73268aee..c11a01283bf3dc5af3939b7b1de0f97e07c78268 100644 --- a/tools/itscertgen/cshared/cshared.vcxproj +++ b/tools/itscertgen/cshared/cshared.vcxproj @@ -11,14 +11,23 @@ + + + + + + + + + {D5918B85-FA45-4F75-9B50-C2D3E34ABA17} @@ -30,14 +39,14 @@ StaticLibrary true v120 - Unicode + MultiByte StaticLibrary false v120 true - Unicode + MultiByte @@ -50,8 +59,12 @@ - $(SolutionDir)build\msvc\$(Configuration)\ - $(SolutionDir)build\msvc\$(Configuration)\$(ProjectName)\ + $(SolutionDir)build\MSVC-$(Configuration)\ + $(SolutionDir)build\MSVC-$(Configuration)\obj\$(ProjectName)\ + + + $(SolutionDir)build\MSVC-$(Configuration)\ + $(SolutionDir)build\MSVC-$(Configuration)\obj\$(ProjectName)\ diff --git a/tools/itscertgen/cshared/cstr.c b/tools/itscertgen/cshared/cstr.c index b90d0d93ea570fee2268fe3aae58804d1ccd1481..024822cdb9eb41e0684abb497d1caa07207723ac 100644 --- a/tools/itscertgen/cshared/cstr.c +++ b/tools/itscertgen/cshared/cstr.c @@ -7,16 +7,33 @@ ## This code is provided under the CeCill-C license agreement. ###################################################################### *********************************************************************/ +#define _CRT_SECURE_NO_WARNINGS #include "cstr.h" #include #include #include #include +#include int cstrlen(const char * str) { return str ? strlen(str) : 0; } +int cstrnlen(const char * str, int maxsize) +{ + int len = str ? strlen(str) : 0; + return len > maxsize ? maxsize : len; +} + +char* cstrend(const char * str) +{ + return (char*)(str ? str + strlen(str) : NULL); +} + +int cstrequal(const char * s1, const char * s2) +{ + return (s1 == s2) || (s1 && s2 && 0 == strcmp(s1, s2)); +} char * cstrcpy(char * dst, const char * src) { @@ -40,11 +57,11 @@ char * cstrncpy(char * dst, int maxsize, const char * src) unsigned int len = 0; if(src && ms > 0){ len = strlen(src); - if(len > ms){ + if (len > ms){ len = ms; - if(len){ - memcpy(dst, src, len); - } + } + if(len){ + memcpy(dst, src, len); } } dst[len]=0; @@ -141,29 +158,29 @@ char * cvstrdup(const char * ptr, ...) return dst; } -char * cstrnload(char * dst, int max_size, const char * path) +char * cstrnload(char * dst, int max_size, const pchar_t * path) { - FILE * f = fopen(path, "rb"); - unsigned long len; + FILE * f = pchar_fopen(path, _PCH("rb")); + unsigned long len, rl; unsigned long ms = max_size; if (!f) return NULL; fseek(f, 0, SEEK_END); len = ftell(f); fseek(f, 0, SEEK_SET); if (len > ms) len = ms; - ms = fread(dst, 1, len, f); + rl = fread(dst, 1, len, f); fclose(f); - if ((int)ms < 0){ + if ((int)rl < 0){ return NULL; } - dst[len] = 0; + if (len < ms)dst[len] = 0; return dst + len; } -char * cstraload(char ** p, const char * path) +char * cstraload(char ** p, const pchar_t * path) { char * ret = NULL; - FILE * f = fopen(path, "rb"); + FILE * f = pchar_fopen(path, _PCH("rb")); int len; if (f){ fseek(f, 0, SEEK_END); @@ -188,15 +205,28 @@ char * cstraload(char ** p, const char * path) return ret; } -const char * cstrlastpathelement(const char * str) +const pchar_t * cstrlastpathelement(const pchar_t * str) { - const char * p = strrchr(str, '/'); - const char * p2 = strrchr(str, '/'); + const pchar_t * p = pchar_rchr(str, '/'); + const pchar_t * p2 = pchar_rchr(str, '/'); if(p= '0' && ch <= '9') continue; if (ch >= 'A' && ch <= 'F') continue; if (ch >= 'a' && ch <= 'f') continue; @@ -235,6 +265,7 @@ char * cstr_hex2bin(char * bin, int blen, const char * hex, int hlen) } return b; } + static const char* _hexDigits = "0123456789ABCDEF"; char * cstr_bin2hex(char * hex, int hlen, const char * bin, int blen) { @@ -254,3 +285,9 @@ char * cstr_bin2hex(char * hex, int hlen, const char * bin, int blen) } return hex + blen * 2; } + +int cstr_write(const char * const p, char ** const ptr, const char * const end, int * const error); +int cstr_read(char * const p, const char ** const ptr, const char * const end, int * const error); + +int cstrn_write(const char * const p, int length, char ** const ptr, const char * const end, int * const error); +int cstrn_read(char * const p, int length, const char ** const ptr, const char * const end, int * const error); diff --git a/tools/itscertgen/cshared/cstr.h b/tools/itscertgen/cshared/cstr.h index e2b0ecba55d28c540c2cf208af571dbeec7fe264..adf58b6926d6fc1bc8ec141cd7b33774d118b2a4 100644 --- a/tools/itscertgen/cshared/cstr.h +++ b/tools/itscertgen/cshared/cstr.h @@ -9,12 +9,19 @@ *********************************************************************/ #ifndef CSTR_H #define CSTR_H +#include +#include #ifdef __cplusplus extern "C" { #endif +#define cisspace(X) isspace((int)(X)) +#define cisdigit(X) isdigit((int)(X)) + int cstrlen(const char * str); -/* copy src to dst and return pointer to the next byte after the end */ +int cstrnlen(const char * str, int maxsize); +char* cstrend(const char * str); +/* copy src to dst and return pointer to the next byte after the end */ char * cstrcpy(char * dst, const char * src); /* copy up to maxsize characters from src to dst and return pointer to the next byte after the end */ @@ -23,21 +30,16 @@ char * cstrncpy(char * dst, int maxsize, const char * src); /* copy up to maxsize characters from parameters to dst and return pointer to the next byte after the end */ char * cvstrncpy(char * dst, int maxsize, const char * ptr, ...); +int cstrequal(const char * s1, const char * s2); /* allocate copy of the str */ char * cstralloc(int size); char * cstrdup(const char * str); char * cstrndup(const char * str, int max_size); - /* allocate new str and collect all paramaters */ char * cvstrdup(const char * ptr, ...); -char * cstrnload(char * dst, int max_size, const char * path); -char * cstraload(char ** p, const char * path); - -const char * cstrlastpathelement(const char * str); - int cstr_write(const char * const p, char ** const ptr, const char * const end, int * const error); int cstr_read (char * const p, const char ** const ptr, const char * const end, int * const error); @@ -46,6 +48,47 @@ int cstrn_read (char * const p, int length, const char ** const ptr, const char char * cstr_hex2bin(char * bin, int blen, const char * hex, int hlen); char * cstr_bin2hex(char * hex, int hlen, const char * bin, int blen); + +/* file access functions */ +#ifndef PCHAR_T_DEFINED +#if defined(UNICODE) && ( defined(_WIN32) || defined(_WIN64) ) +#define PCHAR16BITS +#include +typedef wchar_t pchar_t; +#define _PCH(X) L##X +#else +typedef char pchar_t; +#define _PCH(X) X +#endif +#endif + +/* load file content to the buffer and return pointer to the next byte after the buffer */ +char * cstrnload(char * dst, int max_size, const pchar_t * path); +/* load file content to the new allocated buffer, assign it to p and return pointer to the next byte after the buffer */ +char * cstraload(char ** p, const pchar_t * path); + +/* return the last element of the path */ +const pchar_t * cstrlastpathelement(const pchar_t * str); +pchar_t * cstrpathextension(const pchar_t * str); + + +#ifdef PCHAR16BITS +#define pchar_len(S) wcslen(S) +#define pchar_ncpy(D,S,N) wcsncpy(D,S,N) +#define pchar_cpy(D,S) wcscpy(D,S) +#define pchar_rchr(S,C) wcsrchr(S,C) +#define pchar_fopen(P,M) _wfopen(P, M) +#define pchar_main(A,V) wmain(A,V) +#else +#define pchar_len(S) strlen(S) +#define pchar_ncpy(D,S,N) strncpy(D,S,N) +#define pchar_cpy(D,S) strcpy(D,S) +#define pchar_rchr(S,C) strrchr(S,C) +#define pchar_fopen(P,M) fopen(P, M) +#define pchar_main(A,V) main(A,V) +#endif +#define pchar_alloc(S) ((pchar_t*)malloc(sizeof(pchar_t)*((S)+1))) + #ifdef __cplusplus } #endif diff --git a/tools/itscertgen/cshared/e4c_lite.c b/tools/itscertgen/cshared/e4c_lite.c new file mode 100644 index 0000000000000000000000000000000000000000..e12b27f2f08fad4c38922cc922cdfb82574c8a3a --- /dev/null +++ b/tools/itscertgen/cshared/e4c_lite.c @@ -0,0 +1,108 @@ +/* + * exceptions4c lightweight version 1.0 + * + * Copyright (c) 2014 Guillermo Calvo + * Licensed under the GNU Lesser General Public License + * Small modification was made by Denis Filatov + */ + +#ifdef _MSC_VER +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include +#include +#include +#include "e4c_lite.h" + + +E4C_DEFINE_EXCEPTION(RuntimeException, "Runtime exception.", RuntimeException); +E4C_DEFINE_EXCEPTION(NullPointerException, "Null pointer.", RuntimeException); + +struct e4c_context e4c = {0}; +static const char * err_msg[] = {"\n\nError: %s (%s)\n\n", "\n\nUncaught %s: %s\n\n thrown at %s:%d\n\n"}; + +#ifdef E4C_DEBUG +static const char * __stages[] = { + "beginning", "trying", "catching", "finalizing", "done" +}; +#endif +static void e4c_propagate(void){ + + e4c.frame[e4c.frames].uncaught = 1; + + if(e4c.frames > 0) longjmp(e4c.jump[e4c.frames - 1], 1); + + if(fprintf(stderr, e4c.err.file == NULL ? err_msg[0] : err_msg[1], e4c.err.type->name, e4c.err.message, e4c.err.file, e4c.err.line) > 0) + (void)fflush(stderr); + + exit(EXIT_FAILURE); +} + +int e4c_try(const char * file, int line){ + + if(e4c.frames >= E4C_MAX_FRAMES) e4c_throw(&RuntimeException, file, line, E2BIG, "Too many `try` blocks nested."); + + e4c.frames++; + e4c.frame[e4c.frames].stage = e4c_beginning; + e4c.frame[e4c.frames].uncaught = 0; + + e4c.err.err = 0; + +#ifdef E4C_DEBUG + fprintf(stderr, "TRY %d [%s]: %s:%d\n", e4c.frames, __stages[e4c.frame[e4c.frames].stage], file, line); +#endif + return 1; +} + +int e4c_hook(int is_catch){ + + int uncaught; + + if (is_catch){ +#ifdef E4C_DEBUG + fprintf(stderr, "CATCH %d [%s]:\n", e4c.frames, __stages[e4c.frame[e4c.frames].stage]); +#endif + return !(e4c.frame[e4c.frames].uncaught = 0); + } + + uncaught = e4c.frame[e4c.frames].uncaught; + + e4c.frame[e4c.frames].stage++; + if(e4c.frame[e4c.frames].stage == e4c_catching && !uncaught) e4c.frame[e4c.frames].stage++; + +#ifdef E4C_DEBUG + fprintf(stderr, "HOOK %d [%s]:\n", e4c.frames, __stages[e4c.frame[e4c.frames].stage]); +#endif + if (e4c.frame[e4c.frames].stage < e4c_done) return 1; + + + e4c.frames--; + + if(uncaught) e4c_propagate(); + + return 0; +} + +int e4c_extends(const struct e4c_exception_type * child, const struct e4c_exception_type * parent){ + + for(; child != NULL && child->supertype != child; child = child->supertype) + if(child->supertype == parent) return 1; + + return 0; +} + +void e4c_throw(const struct e4c_exception_type * exception_type, const char * file, int line, int err, const char * message){ + + e4c.err.type = (exception_type != NULL ? exception_type : &NullPointerException); + e4c.err.file = file; + e4c.err.line = line; + e4c.err.err |= err; + +#ifdef E4C_DEBUG + fprintf(stderr, "THROW %d [%s]:\n", e4c.frames, __stages[e4c.frame[e4c.frames].stage]); +#endif + (void)sprintf(e4c.err.message, "%.*s", (int)E4C_MESSAGE_SIZE - 1, (message != NULL ? message : e4c.err.type->default_message)); + + e4c_propagate(); +} diff --git a/tools/itscertgen/cshared/e4c_lite.h b/tools/itscertgen/cshared/e4c_lite.h new file mode 100644 index 0000000000000000000000000000000000000000..2b84994fe0c7519caa23d702b575a77ac430409d --- /dev/null +++ b/tools/itscertgen/cshared/e4c_lite.h @@ -0,0 +1,83 @@ +/* + * exceptions4c lightweight version 1.0 + * + * Copyright (c) 2014 Guillermo Calvo + * Licensed under the GNU Lesser General Public License + */ + +#ifndef EXCEPTIONS4C_LITE +#define EXCEPTIONS4C_LITE + +#include +#include + +/* Maximum number of nested `try` blocks */ +#ifndef E4C_MAX_FRAMES +# define E4C_MAX_FRAMES 16 +#endif + +/* Maximum length (in bytes) of an exception message */ +#ifndef E4C_MESSAGE_SIZE +# define E4C_MESSAGE_SIZE 128 +#endif + +/* Exception handling keywords: try/catch/finally/throw */ +#ifndef E4C_NOKEYWORDS +# define try E4C_TRY +# define catch(type) E4C_CATCH(type) +# define finally E4C_FINALLY +# define throw(type, err, message) E4C_THROW(type, err, message) +# define rethrow E4C_RETHROW +#endif + +/* Represents an exception type */ +struct e4c_exception_type{ + const char * name; + const char * default_message; + const struct e4c_exception_type * supertype; +}; + +/* Declarations and definitions of exception types */ +#define E4C_DECLARE_EXCEPTION(name) extern const struct e4c_exception_type name +#define E4C_DEFINE_EXCEPTION(name, default_message, supertype) const struct e4c_exception_type name = { #name, default_message, &supertype } + +/* Predefined exception types */ +E4C_DECLARE_EXCEPTION(RuntimeException); +E4C_DECLARE_EXCEPTION(NullPointerException); + +/* Represents an instance of an exception type */ +struct e4c_exception{ + char message[E4C_MESSAGE_SIZE]; + int err; + const char * file; + int line; + const struct e4c_exception_type * type; +}; + +/* Retrieve current thrown exception */ +#define E4C_EXCEPTION e4c.err + +/* Returns whether current exception is of a given type */ +#define E4C_IS_INSTANCE_OF(t) ( e4c.err.type == &t || e4c_extends(e4c.err.type, &t) ) + +/* Implementation details */ +#define E4C_TRY if(e4c_try(E4C_INFO) && setjmp(e4c.jump[e4c.frames - 1]) >= 0) while(e4c_hook(0)) if(e4c.frame[e4c.frames].stage == e4c_trying) +#define E4C_CATCH(type) else if(e4c.frame[e4c.frames].stage == e4c_catching && E4C_IS_INSTANCE_OF(type) && e4c_hook(1)) +#define E4C_FINALLY else if(e4c.frame[e4c.frames].stage == e4c_finalizing) +#define E4C_THROW(type, err, message) e4c_throw(&type, E4C_INFO, err, message) +#define E4C_RETHROW e4c_throw(e4c.err.type, e4c.err.file, e4c.err.line, e4c.err.err, e4c.err.message) +#define FN_THROW(X) +#ifndef NDEBUG +# define E4C_INFO __FILE__, __LINE__ +#else +# define E4C_INFO NULL, 0 +#endif + +enum e4c_stage{e4c_beginning, e4c_trying, e4c_catching, e4c_finalizing, e4c_done}; +extern struct e4c_context{jmp_buf jump[E4C_MAX_FRAMES]; struct e4c_exception err; struct{unsigned char stage; unsigned char uncaught;} frame[E4C_MAX_FRAMES + 1]; int frames;} e4c; +extern int e4c_try(const char * file, int line); +extern int e4c_hook(int is_catch); +extern int e4c_extends(const struct e4c_exception_type * child, const struct e4c_exception_type * parent); +extern void e4c_throw(const struct e4c_exception_type * exception_type, const char * file, int line, int err, const char * message); + +# endif diff --git a/tools/itscertgen/cshared/tests/test_copts.c b/tools/itscertgen/cshared/tests/test_copts.c index d0aa7f7a975cb30e67b9b71bb81f79e1ed5de277..b074c55be426008cfd43e27db86d3b51d56040fd 100644 --- a/tools/itscertgen/cshared/tests/test_copts.c +++ b/tools/itscertgen/cshared/tests/test_copts.c @@ -1,115 +1,115 @@ -/********************************************************************* -###################################################################### -## -## Created by: Denis Filatov -## -## Copyleft (c) 2003 - 2015 -## This code is provided under the CeCill-C license agreement. -###################################################################### -*********************************************************************/ - -#include "../copts.h" -static const char* logfile = NULL; -static int use_daemon = 1; -static unsigned short port = 0; -static unsigned long addr = 0; -static const char* cfgfile = "not set"; - -static char* strs[20]; -static const char * strenum[] = { - "strenum0", - "strenum1", - "strenum2", - "strenum3", - "strenum4", - NULL -}; - - -static int str_callback(const copt_t * opt, const char * option, copt_value_t * value) -{ - printf("STR_CALLBACK: %s: %s\n", option, value->v_str); - return 0; -} -static int long_callback(const copt_t * opt, const char * option, copt_value_t * value) -{ - printf("LONG_CALLBACK: %s %ld\n", option, value->v_long); - return 0; -} - -static int short_callback(const copt_t * opt, const char * option, copt_value_t * value) -{ - printf("SHORT_CALLBACK: %s %d\n", option, value->v_short); - return 0; -} - -static int arg_callback(const copt_t * opt, const char * option, copt_value_t * value) -{ - printf("ARGUMENT: %s \n", value->v_str); - return 0; -} - -static copt_t options [] = { - {"h?", "help", COPT_HELP, NULL , "Print this help page"}, - {"C", "config", COPT_CFGFILE, &cfgfile , "Config file" }, - {"b", "boolvalue", COPT_BOOL |COPT_CONFIG, NULL , "Bool test value" }, - {"d", "nodaemon", COPT_BOOLI |COPT_CONFIG, &use_daemon , "Start as daemon" }, - {"l", "logfile", COPT_STR |COPT_CONFIG, &logfile , "Use this log file" }, - {"e", "strenum", COPT_STRENUM|COPT_CONFIG, &strenum[0] , "String enum values" }, - {"pP", "port", COPT_USHORT |COPT_CONFIG, &port , "Bind to port" }, - {"aA", "addr", COPT_HOST |COPT_CONFIG, &addr , "Bind to ip-address" }, - {"s", "strings", COPT_STRLIST|COPT_CONFIG, &strs[0] , "String list" }, - {"S", "str_cb", COPT_STR |COPT_CALLBACK,&str_callback,"String throw callback" }, - {"L", "l_cb", COPT_LONG |COPT_CALLBACK,&long_callback,"Long throw callback" }, - {"H", "s_cb", COPT_SHORT |COPT_CALLBACK,&short_callback,"Short throw callback" }, - {NULL, NULL, COPT_CALLBACK, &arg_callback,NULL }, - {NULL, NULL, COPT_END, NULL , NULL } -}; - -int main(int argc, char** argv) -{ - int i, j; - int flags = COPT_DEFAULT|COPT_NOERR_UNKNOWN|COPT_NOAUTOHELP; - - i = coptions(argc, argv, flags, options); - - if(i<0){ - if(COPT_ERC(i)){ - if(i == COPT_EHELP){ - coptions_help(stdout, argv[0], options, "nonarg params"); - } - }else{ - printf("Unknown option %s\n", argv[0-i]); - } - } - - printf("help = %s\n", options[0].vptr?"yes":"no"); - printf("bool = %s\n", options[1].vptr?"yes":"no"); - printf("use_daemon = %s\n", use_daemon?"yes":"no"); - printf("logfile = %s\n", logfile); - printf("port = %d\n", port); - printf("addr = %08lX\n", addr); - printf("strenum=%d(%s)\n", - (int)(((const char **)options[5].vptr) - &strenum[0]), - *(const char **)options[5].vptr ); - { - char ** b = &strs[0]; - char ** e = options[8].vptr; - printf("strlist: count=%d\n", (int)(e - b)); - while(b < e){ - printf("\t%s\n", *b); - b++; - } - } - if(i>0){ - printf("\nNonoptions:\n"); - for(j=1; jv_str); + return 0; +} +static int long_callback(const copt_t * opt, const char * option, copt_value_t * value) +{ + printf("LONG_CALLBACK: %s %ld\n", option, value->v_long); + return 0; +} + +static int short_callback(const copt_t * opt, const char * option, copt_value_t * value) +{ + printf("SHORT_CALLBACK: %s %d\n", option, value->v_short); + return 0; +} + +static int arg_callback(const copt_t * opt, const char * option, copt_value_t * value) +{ + printf("ARGUMENT: %s \n", value->v_str); + return 0; +} + +static copt_t options [] = { + {"h?", "help", COPT_HELP, NULL , "Print this help page"}, + {"C", "config", COPT_CFGFILE, &cfgfile , "Config file" }, + {"b", "boolvalue", COPT_BOOL |COPT_CONFIG, NULL , "Bool test value" }, + {"d", "nodaemon", COPT_BOOLI |COPT_CONFIG, &use_daemon , "Start as daemon" }, + {"l", "logfile", COPT_STR |COPT_CONFIG, &logfile , "Use this log file" }, + {"e", "strenum", COPT_STRENUM|COPT_CONFIG, &strenum[0] , "String enum values" }, + {"pP", "port", COPT_USHORT |COPT_CONFIG, &port , "Bind to port" }, + {"aA", "addr", COPT_HOST |COPT_CONFIG, &addr , "Bind to ip-address" }, + {"s", "strings", COPT_STRLIST|COPT_CONFIG, &strs[0] , "String list" }, + {"S", "str_cb", COPT_STR |COPT_CALLBACK,&str_callback,"String throw callback" }, + {"L", "l_cb", COPT_LONG |COPT_CALLBACK,&long_callback,"Long throw callback" }, + {"H", "s_cb", COPT_SHORT |COPT_CALLBACK,&short_callback,"Short throw callback" }, + {NULL, NULL, COPT_CALLBACK, &arg_callback,NULL }, + {NULL, NULL, COPT_END, NULL , NULL } +}; + +int main(int argc, char** argv) +{ + int i, j; + int flags = COPT_DEFAULT|COPT_NOERR_UNKNOWN|COPT_NOAUTOHELP; + + i = coptions(argc, argv, flags, options); + + if(i<0){ + if(COPT_ERC(i)){ + if(i == COPT_EHELP){ + coptions_help(stdout, argv[0], options, "nonarg params"); + } + }else{ + printf("Unknown option %s\n", argv[0-i]); + } + } + + printf("help = %s\n", options[0].vptr?"yes":"no"); + printf("bool = %s\n", options[1].vptr?"yes":"no"); + printf("use_daemon = %s\n", use_daemon?"yes":"no"); + printf("logfile = %s\n", logfile); + printf("port = %d\n", port); + printf("addr = %08lX\n", addr); + printf("strenum=%d(%s)\n", + (int)(((const char **)options[5].vptr) - &strenum[0]), + *(const char **)options[5].vptr ); + { + char ** b = &strs[0]; + char ** e = options[8].vptr; + printf("strlist: count=%d\n", (int)(e - b)); + while(b < e){ + printf("\t%s\n", *b); + b++; + } + } + if(i>0){ + printf("\nNonoptions:\n"); + for(j=1; jvalue){ const char * p = a->value; - if(isdigit(*p)){ + if(cisdigit(*p)){ while(*p == '0')p++; - return isdigit(*p) ? 1 : 0 ; + return cisdigit(*p) ? 1 : 0 ; } if(0 == strcmp(p, "true") || @@ -206,10 +207,10 @@ int cxml_tag_attr_int(cxml_tag_t * const tag, const char * const key) if(a && a->value){ char *e; const char *b = a->value; - while(isspace(*b))b++; + while(cisspace(*b))b++; if(*b){ ret = strtol(b, &e, 0); - while(isspace(*e))e++; + while(cisspace(*e))e++; if(*e != 0){ ret = -1; } @@ -225,10 +226,10 @@ uint cxml_tag_attr_uint(cxml_tag_t * const tag, const char * const key) if(a && a->value){ char *e; const char *b = a->value; - while(isspace(*b))b++; + while(cisspace(*b))b++; if(*b){ ret = strtoul(b, &e, 0); - while(isspace(*e))e++; + while(cisspace(*e))e++; if(*e != 0){ ret = (uint)-1; } @@ -315,10 +316,10 @@ static int cxml_handler_parse_tag(cxml_handler_t * h, cxml_tag_t * * const tag, } else { type = CXML_TAG_OPEN; } - if(0==*b || isspace(*b)) goto err; + if(0==*b || cisspace(*b)) goto err; e = b; do{ - if(isspace(*e))break; + if(cisspace(*e))break; if(0==*e) ASSERT_GOTO(err); if(istagend(type, e))break; e++; @@ -326,23 +327,23 @@ static int cxml_handler_parse_tag(cxml_handler_t * h, cxml_tag_t * * const tag, if(e>b){ /* b:e - tag name */ tend = e; - while(*e && isspace(*e))e++; + while(*e && cisspace(*e))e++; t = cxml_tag_new(h, type, b); b = e; /* parse attributes */ while(*b && !istagend(type, b)){ - while(*b && isspace(*b))b++; + while(*b && cisspace(*b))b++; e = b; while(isxmlalnum(*e))e++; if(e>b){ if(aend) *aend = 0; aend = e; - while(*e && isspace(*e))e++; + while(*e && cisspace(*e))e++; a = cxml_attr_new(h, b); if(*e == '='){ e++; b = e; - while(*b && isspace(*b))b++; + while(*b && cisspace(*b))b++; if(*b == '"'){ b++; e = strchr(b, '"'); @@ -354,7 +355,7 @@ static int cxml_handler_parse_tag(cxml_handler_t * h, cxml_tag_t * * const tag, a->value = b; }else{ e = b; - while(*e && !istagend(type, e) && !isspace(*e))e++; + while(*e && !istagend(type, e) && !cisspace(*e))e++; if(0==*e)ASSERT_GOTO(err); if(vend){ *vend = 0; @@ -432,7 +433,7 @@ static int cxml_handler_parse_buffer(void * const ph, char* const text) b = text; /* strip leading spaces */ - while(*b && isspace(*b))b++; + while(*b && cisspace(*b))b++; e = b; while(*e){ cxml_tag_t * tag = NULL; diff --git a/tools/itscertgen/cxml/cxml.h b/tools/itscertgen/cxml/cxml.h index c0600376b5bee490d8cab8f0706d3c3f586e84c9..3ec90d58fcd6f37f2674481a20d3ad03e966ec95 100644 --- a/tools/itscertgen/cxml/cxml.h +++ b/tools/itscertgen/cxml/cxml.h @@ -113,6 +113,7 @@ extern "C" { #define _CXML_HANDLER_PARSE_STREAM_DEFINED int cxml_handler_parse_stream(void * const h, FILE * f); #endif + void cxml_handler_add_default_entities(void * const h); /* Internal functions for tags manipulations */ cxml_tag_t * cxml_tag_new(void * const h, const cxml_tag_type type, const char * const name); diff --git a/tools/itscertgen/cxml/cxml_encode.c b/tools/itscertgen/cxml/cxml_encode.c index 6fbf0bef48a57b3e44f1b6c37349755111cd748e..2ea347b42d757fa58ad3b0e1b7a50d90794d9ef8 100644 --- a/tools/itscertgen/cxml/cxml_encode.c +++ b/tools/itscertgen/cxml/cxml_encode.c @@ -41,7 +41,6 @@ static int _prepare(void * const handler, int cxml_text_encode(void * const handler, char * * const p_dst, const char * const src, int const len) { - cxml_handler_t * h = (cxml_handler_t *)handler; int srclen; if(len <= 0 ) srclen = strlen(src); else srclen = len; diff --git a/tools/itscertgen/generator/certgen.c b/tools/itscertgen/generator/certgen.c index 14c545f9b16252236521989bdaaba38362bc423d..7297e6062f41d92a034fd8976e7618d0ddc11785 100644 --- a/tools/itscertgen/generator/certgen.c +++ b/tools/itscertgen/generator/certgen.c @@ -8,11 +8,13 @@ ###################################################################### *********************************************************************/ #define _CRT_SECURE_NO_WARNINGS +#define _USE_MATH_DEFINES + #include -#include "cxml/cxml.h" -#include "cshared/copts.h" -#include "cshared/cserialize.h" -#include "cshared/cstr.h" +#include "../cxml/cxml.h" +#include "../cshared/copts.h" +#include "../cshared/cserialize.h" +#include "../cshared/cstr.h" #include "ecc_api.h" #include "mkgmtime.h" @@ -24,6 +26,10 @@ #include #include +#if defined(__CYGWIN__) +#define floorl(X) __builtin_floor(X) +#endif + //#define DEBUG_BOOKMARKS //#define DEBUG_DATA #define MAX_BOOKMARKS 16 @@ -125,9 +131,9 @@ static const cxml_taghandler_t h_attribute[] = { { "ecc-point", attribute_eccpoint_tag, attribute_eccpoint_text, NULL }, { "ecc_point", attribute_eccpoint_tag, attribute_eccpoint_text, NULL }, { "assurance", attribute_assurance_tag, NULL, NULL }, - { "aid", attribute_aid_tag, NULL, NULL }, - { "ssp", attribute_ssp_tag, attribute_ssp_text, NULL }, - {NULL} + { "aid", attribute_aid_tag, NULL, NULL }, + { "ssp", attribute_ssp_tag, attribute_ssp_text, NULL }, + {NULL} }; static int subject_attribute_tag (cxml_handler_t* const h, cxml_tag_t * const tag); @@ -200,7 +206,7 @@ static cxml_handler_class Class = { static int _str_2_enum(const char ** values, size_t size, const char * v) { - while (*v && isspace(*v))v++; + while (*v && cisspace(*v))v++; if (*v){ size_t i; for (i = 0; itm_year; + _defaultTime = mkitstime32(&tm); +} static int _time_option(const copt_t * opt, const char * option, const copt_value_t * value) { @@ -264,6 +284,7 @@ static int _refPoint_option(const copt_t * opt, const char * option, const copt_ _refLat = (int32_t)floorl(lat); if (lon <= 180.0 && lon >= -180.0) lon *= 10000000.0; // degree _refLon = (int32_t)floorl(lon); + _lonTMDPerMetter = 180.0 / M_PI / 0.6378 / cos((lat/10000000.0) * M_PI / 180.0); return 0; } } @@ -301,6 +322,9 @@ int main(int argc, char ** argv) h->vr_type = -1; h->sa_type = -1; + // set default time to the begining of this year + _setup_default_time(); + argc = coptions(argc, argv, COPT_HELP_NOVALUES , options); if(argc < 2){ @@ -385,8 +409,9 @@ static int root_certificate_tag (cxml_handler_t* const _h, cxml_tag_t * const ta } }else{ FILE * f; - char * path = h->ptr; - char * end = cvstrncpy(path, h->end-path, _outPath, "/", _certName, ".crt", NULL); + char * path; + path = h->ptr; + cvstrncpy(path, h->end-path, _outPath, "/", _certName, ".crt", NULL); f = fopen(path, "wb"); if (!f){ fprintf(stderr, "ERROR: Certificate write failed to '%s'\n", path); @@ -431,7 +456,7 @@ static int certificate_version_tag (cxml_handler_t* const _h, cxml_tag_t * const const char * v = cxml_tag_attr_value(tag, "version"); if(v == NULL)v = cxml_tag_attr_value(tag, "value"); if(v){ - while(isspace(*v))v++; + while(cisspace(*v))v++; h->nTmpValue = strtoul(v, NULL, 0); } }else{ @@ -445,7 +470,7 @@ static int certificate_version_text(cxml_handler_t* const _h, char * const text, cert_cxml_handler_t * h = (cert_cxml_handler_t *)_h; if(text){ const char * p = text; - while(*p && isspace(*p))p++; + while(*p && cisspace(*p))p++; if(*p) h->nTmpValue = strtoul(p, NULL, 0); } return 0; @@ -477,7 +502,7 @@ static size_t load_certificate(const char * path, char * ptr, const char * e) fprintf(stderr, "%s: File too small\n", path); return -1; } - if (size > (e - cert)){ + if (size > (size_t)(e - cert)){ fprintf(stderr, "%s: no enough space\n", path); return -1; } @@ -848,7 +873,7 @@ static int attribute_assurance_tag (cxml_handler_t* const _h, cxml_tag_t * cons uint8_t assurance = 0; const char * v = cxml_tag_attr_value(tag, "level"); if(v){ - while(*v && isspace(*v))v++; + while(*v && cisspace(*v))v++; if(*v){ long n = strtol(v, NULL, 0); if(n<0 || n > 7){ @@ -860,7 +885,7 @@ static int attribute_assurance_tag (cxml_handler_t* const _h, cxml_tag_t * cons } v = cxml_tag_attr_value(tag, "confidence"); if(v){ - while(*v && isspace(*v))v++; + while(*v && cisspace(*v))v++; if(*v){ long n = strtol(v, NULL, 0); if(n<0 || n > 3){ @@ -886,8 +911,8 @@ static int attribute_aid_tag (cxml_handler_t* const _h, cxml_tag_t * cons fprintf(stderr, "WARNING: Value required for AID tag. Item was skiped.\n"); }else{ uint32_t n; - while(isspace(*v))v++; - if(!isdigit(*v)){ + while(cisspace(*v))v++; + if(!cisdigit(*v)){ fprintf(stderr, "ERROR: Invalid AID '%s'\n", v); rc = -1; }else{ @@ -910,8 +935,8 @@ static int attribute_ssp_tag (cxml_handler_t* const _h, cxml_tag_t * cons fprintf(stderr, "ERROR: AID shall be supplied for SSP.\n"); return -1; } - while(isspace(*v))v++; - if(!isdigit(*v)){ + while(cisspace(*v))v++; + if(!cisdigit(*v)){ fprintf(stderr, "ERROR: Invalid AID '%s' in SSP\n", v); return -1; } @@ -954,7 +979,7 @@ static unsigned int _convert_time(const char * v) char * e; struct tm tm; - // can me a difference of the predefined point in time + // can be a difference from the base time point if(*v == '-' || *v == '+'){ const char * p; if(_defaultTime == 0){ @@ -974,7 +999,7 @@ static unsigned int _convert_time(const char * v) case 's': e++; } ret += n; - if(isdigit(*e)){ + if(cisdigit(*e)){ p = e; continue; } @@ -1023,7 +1048,7 @@ static int validity_restriction_tag (cxml_handler_t* const _h, cxml_tag_t * cons return -1; } - while(isspace(*v))v++; + while(cisspace(*v))v++; if(0 == strcmp("time", v)){ start = cxml_tag_attr_value(tag, "start"); end = cxml_tag_attr_value(tag, "end"); @@ -1038,7 +1063,7 @@ static int validity_restriction_tag (cxml_handler_t* const _h, cxml_tag_t * cons } }else if(0 == strcmp("region", v)){ vr_type = 3; - }else if(isdigit(*v)){ + }else if(cisdigit(*v)){ vr_type=strtoul(v, NULL,0); }else{ fprintf(stderr, "%s: Unknown validity restriction type.\n", v); @@ -1096,12 +1121,15 @@ static int region_circle_tag (cxml_handler_t* const _h, cxml_tag_t * const tag) if(0 == rc){ //radius uint32_t n; + char * e; const char * v = cxml_tag_attr_value(tag, "radius"); if(NULL == v){ fprintf(stderr, "ERROR: radius shall be specified for circle.\n"); return -1; } - n = strtoul(v, NULL, 0); + n = strtoul(v, &e, 0); + if( (e[0] == 'k' || e[0] == 'K') && (e[1] == 'm' || e[1] == 'M') ) + n *= 1000; if(n > 0xFFFF){ fprintf(stderr, "ERROR: %ul: radius is too big.\n", n); return -1; @@ -1151,14 +1179,20 @@ static int location_tag (cxml_handler_t* const _h, cxml_tag_t * const tag) if (cxml_tag_is_open(tag)){ int32_t lat, lon; long double d; + char * e; cert_cxml_handler_t * h = (cert_cxml_handler_t *)_h; const char * v = cxml_tag_attr_value(tag, "latitude"); if(v == NULL){ fprintf(stderr, "ERROR: Latitude shall be specified for location.\n"); return -1; } - d = strtold(v, NULL); - if (d <= 90.0 && d >= -90.0) d *= 10000000.0; // degree + e = NULL; d = strtold(v, &e); + if(e[0] == 'm' || (e[0] == 'k' && e[1] == 'm')){ + if(e[0] == 'k') d*=1000.0; + d = d * _latTMDPerMetter; + }else{ + if (d <= 90.0 && d >= -90.0) d *= 10000000.0; // degree + } lat = (int32_t)floorl(d+_refLat); v = cxml_tag_attr_value(tag, "longitude"); @@ -1166,8 +1200,14 @@ static int location_tag (cxml_handler_t* const _h, cxml_tag_t * const tag) fprintf(stderr, "ERROR: Longitude shall be specified for location.\n"); return -1; } - d = strtold(v, NULL); - if (d <= 180.0 && d >= -180.0) d *= 10000000.0; // degree + e = NULL; d = strtold(v, &e); + if(e[0] == 'm' || (e[0] == 'k' && e[1] == 'm')){ + if(e[0] == 'k') d*=1000.0; + // convert metters to degree + d = d * _lonTMDPerMetter; + }else{ + if (d <= 180.0 && d >= -180.0) d *= 10000000.0; // degree + } lon = (int32_t)floorl(d + _refLon); cint32_write(lat, &h->ptr, h->end, &rc); @@ -1212,7 +1252,7 @@ static int region_id_tag (cxml_handler_t* const _h, cxml_tag_t * const tag) fprintf(stderr, "ERROR: Region identifier must be set\n"); return -1; } - while(isspace(*v))v++; + while(cisspace(*v))v++; uvalue = strtoul(v, NULL, 0); if(uvalue > 0xFFFF){ fprintf(stderr, "%s: Invalid region identifier\n", v); @@ -1225,9 +1265,9 @@ static int region_id_tag (cxml_handler_t* const _h, cxml_tag_t * const tag) uvalue = 0; v = cxml_tag_attr_value(tag, "local"); if(v){ - while(isspace(*v))v++; + while(cisspace(*v))v++; uvalue = strtoul(v, NULL, 0); - if(!isdigit(*v) || uvalue > 0xFFFF){ + if(!cisdigit(*v) || uvalue > 0xFFFF){ fprintf(stderr, "%s: Invalid region identifier\n", v); return -1; } diff --git a/tools/itscertgen/generator/ecc_openssl.c b/tools/itscertgen/generator/ecc_openssl.c index 8c2a017d49a7ee4c93757b070f8e4423787a00a0..f5e482ed2ff0f1449a88892ee1396707bc8f46e5 100644 --- a/tools/itscertgen/generator/ecc_openssl.c +++ b/tools/itscertgen/generator/ecc_openssl.c @@ -355,10 +355,10 @@ void * ecc_api_key_public_set(ecc_pk_algorithm pk_alg, ecc_point_type ptype, con group = _curves[0]; point = EC_POINT_new(group); - x = BN_bin2bn(px, 32, NULL); + x = BN_bin2bn((const unsigned char*)px, 32, NULL); if (x){ if (ptype == ecc_uncompressed){ - y = BN_bin2bn(py, 32, NULL); + y = BN_bin2bn((const unsigned char*)py, 32, NULL); if (y){ rc = EC_POINT_set_affine_coordinates_GFp(group, point, x, y, NULL); BN_clear_free(y); diff --git a/tools/itscertgen/generator/mkgmtime.c b/tools/itscertgen/generator/mkgmtime.c index 19a4c1087e5f2809f04b0a6c8cfb01b54d0a7443..d72c43ea8635f357c6b4f0800ea9b7a2d4471b2a 100644 --- a/tools/itscertgen/generator/mkgmtime.c +++ b/tools/itscertgen/generator/mkgmtime.c @@ -190,7 +190,7 @@ unsigned long long mkitstime64(struct tm *tim_p) { unsigned long long ret = mktaitime64(tim_p); if (ret > 0){ - ret -= ITS_UTC_EPOCH * 1000; + ret -= ((unsigned long long)ITS_UTC_EPOCH) * 1000; } return ret; } @@ -206,7 +206,7 @@ time_t mktaitime(struct tm *tim_p) unsigned long long mktaitime64(struct tm *tim_p) { - t = mkgmtime(tim_p); + time_t t = mkgmtime(tim_p); if (t >= 0){ t = addleapseconds(t); } diff --git a/tools/itscertgen/generator/mkgmtime.h b/tools/itscertgen/generator/mkgmtime.h index d3122a955bf1927e1450ad2623ed5eb23d9b1208..f17d981968f005780d26bf27bea4e2702243264f 100644 --- a/tools/itscertgen/generator/mkgmtime.h +++ b/tools/itscertgen/generator/mkgmtime.h @@ -30,10 +30,15 @@ extern "C" { extern time_t mkgmtime(struct tm *tim_p); extern time_t mktaitime(struct tm *tim_p); extern time_t addleapseconds(time_t t); + + #define mktaitime32(X) ((unsigned long)mktaitime(X)) extern unsigned long mkitstime32(struct tm *tim_p); - extern unsigned long long mkitstime64(struct tm *tim_p); extern unsigned long unix2itstime32(time_t t); + + extern unsigned long long mktaitime64(struct tm *tim_p); + extern unsigned long long mkitstime64(struct tm *tim_p); extern unsigned long long unix2itstime64(time_t t); + extern const char * stritsdate32(time_t t); extern const char * strtaidate(time_t t); extern const char * strgmtdate(time_t t); diff --git a/tools/itscertgen/mkunstats/Makefile b/tools/itscertgen/mkunstats/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..cea89749dbf856bfe40b387d15cb8d38efeac292 --- /dev/null +++ b/tools/itscertgen/mkunstats/Makefile @@ -0,0 +1,15 @@ +###################################################################### +## +## Created by: Denis Filatov +## +## Copyleft (c) 2015 +## This code is provided under the CeCill-C license agreement. +###################################################################### +PROJECTROOT = .. +BUILDROOT = ../build +PROJECT = mkunstats +DEBUG = yes +bins = mkunstats +sources := mkunstats.c +packages += openssl cshared +include $(PROJECTROOT)/common.mk diff --git a/tools/itscertgen/mkunstats/fitsec_unstats.h b/tools/itscertgen/mkunstats/fitsec_unstats.h new file mode 100644 index 0000000000000000000000000000000000000000..a7e011b9df429989ca956e2ae18224c246207997 --- /dev/null +++ b/tools/itscertgen/mkunstats/fitsec_unstats.h @@ -0,0 +1,272 @@ +static unsigned short _un_stats_regions[] = { + [1] = 0, /* World */ + [2] = 1, /* Africa */ + [4] = 34, /* Afghanistan */ + [5] = 419, /* South America */ + [8] = 39, /* Albania */ + [9] = 1, /* Oceania */ + [11] = 2, /* Western Africa */ + [12] = 15, /* Algeria */ + [13] = 419, /* Central America */ + [14] = 2, /* Eastern Africa */ + [15] = 2, /* Northern Africa */ + [16] = 61, /* American Samoa */ + [17] = 2, /* Middle Africa */ + [18] = 2, /* Southern Africa */ + [19] = 1, /* Americas */ + [20] = 39, /* Andorra */ + [21] = 19, /* Northern America */ + [24] = 17, /* Angola */ + [28] = 29, /* Antigua and Barbuda */ + [29] = 419, /* Caribbean */ + [30] = 142, /* Eastern Asia */ + [31] = 145, /* Azerbaijan */ + [32] = 5, /* Argentina */ + [34] = 142, /* Southern Asia */ + [35] = 142, /* South-Eastern Asia */ + [36] = 53, /* Australia */ + [39] = 150, /* Southern Europe */ + [40] = 155, /* Austria */ + [44] = 29, /* Bahamas */ + [48] = 145, /* Bahrain */ + [50] = 34, /* Bangladesh */ + [51] = 145, /* Armenia */ + [52] = 29, /* Barbados */ + [53] = 9, /* Australia and New Zealand */ + [54] = 9, /* Melanesia */ + [56] = 155, /* Belgium */ + [57] = 9, /* Micronesia */ + [60] = 21, /* Bermuda */ + [61] = 9, /* Polynesia */ + [64] = 34, /* Bhutan */ + [68] = 5, /* Bolivia (Plurinational State of) */ + [70] = 39, /* Bosnia and Herzegovina */ + [72] = 18, /* Botswana */ + [76] = 5, /* Brazil */ + [84] = 13, /* Belize */ + [90] = 54, /* Solomon Islands */ + [92] = 29, /* British Virgin Islands */ + [96] = 35, /* Brunei Darussalam */ + [100] = 151, /* Bulgaria */ + [104] = 35, /* Myanmar */ + [108] = 14, /* Burundi */ + [112] = 151, /* Belarus */ + [114] = 35, /* Cambodia */ + [115] = 17, /* Cameroon */ + [116] = 21, /* Canada */ + [132] = 11, /* Cabo Verde */ + [136] = 29, /* Cayman Islands */ + [140] = 17, /* Central African Republic */ + [142] = 1, /* Asia */ + [143] = 142, /* Central Asia */ + [144] = 34, /* Sri Lanka */ + [145] = 142, /* Western Asia */ + [148] = 17, /* Chad */ + [150] = 1, /* Europe */ + [151] = 150, /* Eastern Europe */ + [152] = 5, /* Chile */ + [154] = 150, /* Northern Europe */ + [155] = 150, /* Western Europe */ + [156] = 30, /* China */ + [170] = 5, /* Colombia */ + [174] = 14, /* Comoros */ + [175] = 14, /* Mayotte */ + [178] = 17, /* Congo */ + [180] = 17, /* Democratic Republic of the Congo */ + [184] = 61, /* Cook Islands */ + [188] = 13, /* Costa Rica */ + [191] = 39, /* Croatia */ + [192] = 29, /* Cuba */ + [196] = 145, /* Cyprus */ + [203] = 151, /* Czech Republic */ + [204] = 11, /* Benin */ + [208] = 154, /* Denmark */ + [212] = 29, /* Dominica */ + [214] = 29, /* Dominican Republic */ + [218] = 5, /* Ecuador */ + [222] = 13, /* El Salvador */ + [226] = 17, /* Equatorial Guinea */ + [231] = 14, /* Ethiopia */ + [232] = 14, /* Eritrea */ + [233] = 154, /* Estonia */ + [234] = 154, /* Faeroe Islands */ + [238] = 5, /* Falkland Islands (Malvinas) */ + [242] = 54, /* Fiji */ + [246] = 154, /* Finland */ + [248] = 154, /* Åland Islands */ + [250] = 155, /* France */ + [254] = 5, /* French Guiana */ + [258] = 61, /* French Polynesia */ + [262] = 14, /* Djibouti */ + [266] = 17, /* Gabon */ + [268] = 145, /* Georgia */ + [270] = 11, /* Gambia */ + [275] = 145, /* State of Palestine */ + [276] = 155, /* Germany */ + [288] = 11, /* Ghana */ + [292] = 39, /* Gibraltar */ + [296] = 57, /* Kiribati */ + [300] = 39, /* Greece */ + [304] = 21, /* Greenland */ + [308] = 29, /* Grenada */ + [312] = 29, /* Guadeloupe */ + [316] = 57, /* Guam */ + [320] = 13, /* Guatemala */ + [324] = 11, /* Guinea */ + [328] = 5, /* Guyana */ + [332] = 29, /* Haiti */ + [336] = 39, /* Holy See */ + [340] = 13, /* Honduras */ + [344] = 30, /* "China, Hong Kong Special Administrative Region" */ + [348] = 151, /* Hungary */ + [352] = 154, /* Iceland */ + [356] = 34, /* India */ + [360] = 35, /* Indonesia */ + [364] = 34, /* Iran (Islamic Republic of) */ + [368] = 145, /* Iraq */ + [372] = 154, /* Ireland */ + [376] = 145, /* Israel */ + [380] = 39, /* Italy */ + [384] = 11, /* Cote d'Ivoire */ + [388] = 29, /* Jamaica */ + [392] = 30, /* Japan */ + [398] = 143, /* Kazakhstan */ + [400] = 145, /* Jordan */ + [404] = 14, /* Kenya */ + [408] = 30, /* Democratic People's Republic of Korea */ + [410] = 30, /* Republic of Korea */ + [414] = 145, /* Kuwait */ + [417] = 143, /* Kyrgyzstan */ + [418] = 35, /* Lao People's Democratic Republic */ + [419] = 19, /* Latin America and the Caribbean */ + [422] = 145, /* Lebanon */ + [426] = 18, /* Lesotho */ + [428] = 154, /* Latvia */ + [430] = 11, /* Liberia */ + [434] = 15, /* Libya */ + [438] = 155, /* Liechtenstein */ + [440] = 154, /* Lithuania */ + [442] = 155, /* Luxembourg */ + [446] = 30, /* "China, Macao Special Administrative Region" */ + [450] = 14, /* Madagascar */ + [454] = 14, /* Malawi */ + [458] = 35, /* Malaysia */ + [462] = 34, /* Maldives */ + [466] = 11, /* Mali */ + [470] = 39, /* Malta */ + [474] = 29, /* Martinique */ + [478] = 11, /* Mauritania */ + [480] = 14, /* Mauritius */ + [484] = 13, /* Mexico */ + [492] = 155, /* Monaco */ + [496] = 30, /* Mongolia */ + [498] = 151, /* Republic of Moldova */ + [499] = 39, /* Montenegro */ + [500] = 29, /* Montserrat */ + [504] = 15, /* Morocco */ + [508] = 14, /* Mozambique */ + [512] = 145, /* Oman */ + [516] = 18, /* Namibia */ + [520] = 57, /* Nauru */ + [524] = 34, /* Nepal */ + [528] = 155, /* Netherlands */ + [531] = 29, /* Curaçao */ + [533] = 29, /* Aruba */ + [534] = 29, /* Sint Maarten (Dutch part) */ + [535] = 29, /* "Bonaire, Sint Eustatius and Saba" */ + [540] = 54, /* New Caledonia */ + [548] = 54, /* Vanuatu */ + [554] = 53, /* New Zealand */ + [558] = 13, /* Nicaragua */ + [562] = 11, /* Niger */ + [566] = 11, /* Nigeria */ + [570] = 61, /* Niue */ + [574] = 53, /* Norfolk Island */ + [578] = 154, /* Norway */ + [580] = 57, /* Northern Mariana Islands */ + [583] = 57, /* Micronesia (Federated States of) */ + [584] = 57, /* Marshall Islands */ + [585] = 57, /* Palau */ + [586] = 34, /* Pakistan */ + [591] = 13, /* Panama */ + [598] = 54, /* Papua New Guinea */ + [600] = 5, /* Paraguay */ + [604] = 5, /* Peru */ + [608] = 35, /* Philippines */ + [612] = 61, /* Pitcairn */ + [616] = 151, /* Poland */ + [620] = 39, /* Portugal */ + [624] = 11, /* Guinea-Bissau */ + [626] = 35, /* Timor-Leste */ + [630] = 29, /* Puerto Rico */ + [634] = 145, /* Qatar */ + [638] = 14, /* Réunion */ + [642] = 151, /* Romania */ + [643] = 151, /* Russian Federation */ + [646] = 14, /* Rwanda */ + [652] = 29, /* Saint-Barthélemy */ + [654] = 11, /* Saint Helena */ + [659] = 29, /* Saint Kitts and Nevis */ + [660] = 29, /* Anguilla */ + [662] = 29, /* Saint Lucia */ + [663] = 29, /* Saint Martin (French part) */ + [666] = 21, /* Saint Pierre and Miquelon */ + [670] = 29, /* Saint Vincent and the Grenadines */ + [674] = 39, /* San Marino */ + [678] = 17, /* Sao Tome and Principe */ + [680] = 154, /* Sark */ + [682] = 145, /* Saudi Arabia */ + [686] = 11, /* Senegal */ + [688] = 39, /* Serbia */ + [690] = 14, /* Seychelles */ + [694] = 11, /* Sierra Leone */ + [702] = 35, /* Singapore */ + [703] = 151, /* Slovakia */ + [704] = 35, /* Viet Nam */ + [705] = 39, /* Slovenia */ + [706] = 14, /* Somalia */ + [710] = 18, /* South Africa */ + [716] = 14, /* Zimbabwe */ + [724] = 39, /* Spain */ + [728] = 14, /* South Sudan */ + [729] = 15, /* Sudan */ + [732] = 15, /* Western Sahara */ + [740] = 5, /* Suriname */ + [744] = 154, /* Svalbard and Jan Mayen Islands */ + [748] = 18, /* Swaziland */ + [752] = 154, /* Sweden */ + [756] = 155, /* Switzerland */ + [760] = 145, /* Syrian Arab Republic */ + [762] = 143, /* Tajikistan */ + [764] = 35, /* Thailand */ + [768] = 11, /* Togo */ + [772] = 61, /* Tokelau */ + [776] = 61, /* Tonga */ + [780] = 29, /* Trinidad and Tobago */ + [784] = 145, /* United Arab Emirates */ + [788] = 15, /* Tunisia */ + [792] = 145, /* Turkey */ + [795] = 143, /* Turkmenistan */ + [796] = 29, /* Turks and Caicos Islands */ + [798] = 61, /* Tuvalu */ + [800] = 14, /* Uganda */ + [804] = 151, /* Ukraine */ + [807] = 39, /* The former Yugoslav Republic of Macedonia */ + [818] = 15, /* Egypt */ + [826] = 154, /* United Kingdom of Great Britain and Northern Ireland */ + [830] = 154, /* Channel Islands */ + [831] = 154, /* Guernsey */ + [832] = 154, /* Jersey */ + [833] = 154, /* Isle of Man */ + [834] = 14, /* United Republic of Tanzania */ + [840] = 21, /* United States of America */ + [850] = 29, /* United States Virgin Islands */ + [854] = 11, /* Burkina Faso */ + [858] = 5, /* Uruguay */ + [860] = 143, /* Uzbekistan */ + [862] = 5, /* Venezuela (Bolivarian Republic of) */ + [876] = 61, /* Wallis and Futuna Islands */ + [882] = 61, /* Samoa */ + [887] = 145, /* Yemen */ + [894] = 14, /* Zambia */ +}; diff --git a/tools/itscertgen/mkunstats/mkunstats.c b/tools/itscertgen/mkunstats/mkunstats.c new file mode 100644 index 0000000000000000000000000000000000000000..8ebca3f9774bc4bfdb867cfc508c9c5658e2855a --- /dev/null +++ b/tools/itscertgen/mkunstats/mkunstats.c @@ -0,0 +1,13 @@ +#include +#include "fitsec_unstats.h" + +int main(int argc, char ** argv){ + int i; + printf("static unsigned short _un_stats_regions[] = {"); + for(i=0; i