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