Home | History | Annotate | Line # | Download | only in asn1
      1  1.3  christos /*	$NetBSD: main.c,v 1.4 2023/06/19 21:41:42 christos Exp $	*/
      2  1.1     elric 
      3  1.1     elric /*
      4  1.1     elric  * Copyright (c) 1997-2005 Kungliga Tekniska Hgskolan
      5  1.1     elric  * (Royal Institute of Technology, Stockholm, Sweden).
      6  1.1     elric  * All rights reserved.
      7  1.1     elric  *
      8  1.1     elric  * Redistribution and use in source and binary forms, with or without
      9  1.1     elric  * modification, are permitted provided that the following conditions
     10  1.1     elric  * are met:
     11  1.1     elric  *
     12  1.1     elric  * 1. Redistributions of source code must retain the above copyright
     13  1.1     elric  *    notice, this list of conditions and the following disclaimer.
     14  1.1     elric  *
     15  1.1     elric  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     elric  *    notice, this list of conditions and the following disclaimer in the
     17  1.1     elric  *    documentation and/or other materials provided with the distribution.
     18  1.1     elric  *
     19  1.1     elric  * 3. Neither the name of the Institute nor the names of its contributors
     20  1.1     elric  *    may be used to endorse or promote products derived from this software
     21  1.1     elric  *    without specific prior written permission.
     22  1.1     elric  *
     23  1.1     elric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
     24  1.1     elric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1     elric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1     elric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
     27  1.1     elric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1     elric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1     elric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1     elric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1     elric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1     elric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1     elric  * SUCH DAMAGE.
     34  1.1     elric  */
     35  1.1     elric 
     36  1.1     elric #include "gen_locl.h"
     37  1.3  christos #include "getarg.h"
     38  1.1     elric #include "lex.h"
     39  1.1     elric 
     40  1.1     elric extern FILE *yyin;
     41  1.1     elric 
     42  1.1     elric static getarg_strings preserve;
     43  1.1     elric static getarg_strings seq;
     44  1.1     elric 
     45  1.1     elric int
     46  1.1     elric preserve_type(const char *p)
     47  1.1     elric {
     48  1.1     elric     int i;
     49  1.1     elric     for (i = 0; i < preserve.num_strings; i++)
     50  1.1     elric 	if (strcmp(preserve.strings[i], p) == 0)
     51  1.1     elric 	    return 1;
     52  1.1     elric     return 0;
     53  1.1     elric }
     54  1.1     elric 
     55  1.1     elric int
     56  1.1     elric seq_type(const char *p)
     57  1.1     elric {
     58  1.1     elric     int i;
     59  1.1     elric     for (i = 0; i < seq.num_strings; i++)
     60  1.1     elric 	if (strcmp(seq.strings[i], p) == 0)
     61  1.1     elric 	    return 1;
     62  1.1     elric     return 0;
     63  1.1     elric }
     64  1.1     elric 
     65  1.2  christos const char *fuzzer_string = "";
     66  1.2  christos int fuzzer_flag;
     67  1.1     elric int support_ber;
     68  1.1     elric int template_flag;
     69  1.1     elric int rfc1510_bitstring;
     70  1.1     elric int one_code_file;
     71  1.1     elric char *option_file;
     72  1.2  christos int parse_units_flag = 1;
     73  1.2  christos char *type_file_string = "krb5-types.h";
     74  1.1     elric int version_flag;
     75  1.1     elric int help_flag;
     76  1.1     elric struct getargs args[] = {
     77  1.2  christos     { "fuzzer", 0, arg_flag, &fuzzer_flag, NULL, NULL },
     78  1.2  christos     { "template", 0, arg_flag, &template_flag, NULL, NULL },
     79  1.2  christos     { "encode-rfc1510-bit-string", 0, arg_flag, &rfc1510_bitstring, NULL, NULL },
     80  1.2  christos     { "decode-dce-ber", 0, arg_flag, &support_ber, NULL, NULL },
     81  1.2  christos     { "support-ber", 0, arg_flag, &support_ber, NULL, NULL },
     82  1.2  christos     { "preserve-binary", 0, arg_strings, &preserve, NULL, NULL },
     83  1.2  christos     { "sequence", 0, arg_strings, &seq, NULL, NULL },
     84  1.2  christos     { "one-code-file", 0, arg_flag, &one_code_file, NULL, NULL },
     85  1.2  christos     { "option-file", 0, arg_string, &option_file, NULL, NULL },
     86  1.2  christos     { "parse-units", 0, arg_negative_flag, &parse_units_flag, NULL, NULL },
     87  1.2  christos     { "type-file", 0, arg_string, &type_file_string, NULL, NULL },
     88  1.2  christos     { "version", 0, arg_flag, &version_flag, NULL, NULL },
     89  1.2  christos     { "help", 0, arg_flag, &help_flag, NULL, NULL }
     90  1.1     elric };
     91  1.1     elric int num_args = sizeof(args) / sizeof(args[0]);
     92  1.1     elric 
     93  1.1     elric static void
     94  1.1     elric usage(int code)
     95  1.1     elric {
     96  1.1     elric     arg_printusage(args, num_args, NULL, "[asn1-file [name]]");
     97  1.1     elric     exit(code);
     98  1.1     elric }
     99  1.1     elric 
    100  1.1     elric int error_flag;
    101  1.1     elric 
    102  1.1     elric int
    103  1.1     elric main(int argc, char **argv)
    104  1.1     elric {
    105  1.1     elric     int ret;
    106  1.1     elric     const char *file;
    107  1.1     elric     const char *name = NULL;
    108  1.1     elric     int optidx = 0;
    109  1.1     elric     char **arg = NULL;
    110  1.2  christos     int len = 0, i;
    111  1.1     elric 
    112  1.1     elric     setprogname(argv[0]);
    113  1.1     elric     if(getarg(args, num_args, argc, argv, &optidx))
    114  1.1     elric 	usage(1);
    115  1.1     elric     if(help_flag)
    116  1.1     elric 	usage(0);
    117  1.1     elric     if(version_flag) {
    118  1.1     elric 	print_version(NULL);
    119  1.1     elric 	exit(0);
    120  1.1     elric     }
    121  1.1     elric     if (argc == optidx) {
    122  1.1     elric 	file = "stdin";
    123  1.1     elric 	name = "stdin";
    124  1.1     elric 	yyin = stdin;
    125  1.1     elric     } else {
    126  1.1     elric 	file = argv[optidx];
    127  1.1     elric 	yyin = fopen (file, "r");
    128  1.1     elric 	if (yyin == NULL)
    129  1.1     elric 	    err (1, "open %s", file);
    130  1.1     elric 	if (argc == optidx + 1) {
    131  1.1     elric 	    char *p;
    132  1.1     elric 	    name = estrdup(file);
    133  1.1     elric 	    p = strrchr(name, '.');
    134  1.1     elric 	    if (p)
    135  1.1     elric 		*p = '\0';
    136  1.1     elric 	} else
    137  1.1     elric 	    name = argv[optidx + 1];
    138  1.1     elric     }
    139  1.1     elric 
    140  1.1     elric     /*
    141  1.1     elric      * Parse extra options file
    142  1.1     elric      */
    143  1.1     elric     if (option_file) {
    144  1.1     elric 	char buf[1024];
    145  1.1     elric 	FILE *opt;
    146  1.1     elric 
    147  1.1     elric 	opt = fopen(option_file, "r");
    148  1.1     elric 	if (opt == NULL) {
    149  1.1     elric 	    perror("open");
    150  1.1     elric 	    exit(1);
    151  1.1     elric 	}
    152  1.1     elric 
    153  1.1     elric 	arg = calloc(2, sizeof(arg[0]));
    154  1.1     elric 	if (arg == NULL) {
    155  1.1     elric 	    perror("calloc");
    156  1.1     elric 	    exit(1);
    157  1.1     elric 	}
    158  1.1     elric 	arg[0] = option_file;
    159  1.1     elric 	arg[1] = NULL;
    160  1.1     elric 	len = 1;
    161  1.1     elric 
    162  1.1     elric 	while (fgets(buf, sizeof(buf), opt) != NULL) {
    163  1.1     elric 	    buf[strcspn(buf, "\n\r")] = '\0';
    164  1.1     elric 
    165  1.1     elric 	    arg = realloc(arg, (len + 2) * sizeof(arg[0]));
    166  1.1     elric 	    if (arg == NULL) {
    167  1.1     elric 		perror("malloc");
    168  1.1     elric 		exit(1);
    169  1.1     elric 	    }
    170  1.1     elric 	    arg[len] = strdup(buf);
    171  1.1     elric 	    if (arg[len] == NULL) {
    172  1.1     elric 		perror("strdup");
    173  1.1     elric 		exit(1);
    174  1.1     elric 	    }
    175  1.1     elric 	    arg[len + 1] = NULL;
    176  1.1     elric 	    len++;
    177  1.1     elric 	}
    178  1.1     elric 	fclose(opt);
    179  1.1     elric 
    180  1.1     elric 	optidx = 0;
    181  1.1     elric 	if(getarg(args, num_args, len, arg, &optidx))
    182  1.1     elric 	    usage(1);
    183  1.1     elric 
    184  1.1     elric 	if (len != optidx) {
    185  1.1     elric 	    fprintf(stderr, "extra args");
    186  1.1     elric 	    exit(1);
    187  1.1     elric 	}
    188  1.1     elric     }
    189  1.1     elric 
    190  1.2  christos     if (fuzzer_flag) {
    191  1.2  christos 	if (!template_flag) {
    192  1.2  christos 	    printf("can't do fuzzer w/o --template");
    193  1.2  christos 	    exit(1);
    194  1.2  christos 	}
    195  1.2  christos #ifdef ASN1_FUZZER
    196  1.2  christos 	fuzzer_string = "_fuzzer";
    197  1.2  christos #endif
    198  1.2  christos     }
    199  1.2  christos 
    200  1.1     elric 
    201  1.1     elric     init_generate (file, name);
    202  1.1     elric 
    203  1.1     elric     if (one_code_file)
    204  1.1     elric 	generate_header_of_codefile(name);
    205  1.1     elric 
    206  1.1     elric     initsym ();
    207  1.1     elric     ret = yyparse ();
    208  1.1     elric     if(ret != 0 || error_flag != 0)
    209  1.1     elric 	exit(1);
    210  1.1     elric     close_generate ();
    211  1.1     elric     if (argc != optidx)
    212  1.1     elric 	fclose(yyin);
    213  1.1     elric 
    214  1.1     elric     if (one_code_file)
    215  1.1     elric 	close_codefile();
    216  1.1     elric 
    217  1.1     elric     if (arg) {
    218  1.1     elric 	for (i = 1; i < len; i++)
    219  1.1     elric 	    free(arg[i]);
    220  1.1     elric 	free(arg);
    221  1.1     elric     }
    222  1.2  christos 
    223  1.1     elric     return 0;
    224  1.1     elric }
    225