1/* 2 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#include "gen_locl.h" 35#include <getarg.h> 36#include "lex.h" 37 38RCSID("$Id$"); 39 40extern FILE *yyin; 41 42static getarg_strings preserve; 43static getarg_strings seq; 44 45int 46preserve_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 55int 56seq_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 65const char *fuzzer_string = ""; 66int fuzzer_flag; 67int support_ber; 68int template_flag; 69int rfc1510_bitstring; 70int one_code_file; 71char *option_file; 72int parse_units_flag = 1; 73char *type_file_string = "krb5-types.h"; 74int version_flag; 75int help_flag; 76struct getargs args[] = { 77 { "fuzzer", 0, arg_flag, &fuzzer_flag }, 78 { "template", 0, arg_flag, &template_flag }, 79 { "encode-rfc1510-bit-string", 0, arg_flag, &rfc1510_bitstring }, 80 { "decode-dce-ber", 0, arg_flag, &support_ber }, 81 { "support-ber", 0, arg_flag, &support_ber }, 82 { "preserve-binary", 0, arg_strings, &preserve }, 83 { "sequence", 0, arg_strings, &seq }, 84 { "one-code-file", 0, arg_flag, &one_code_file }, 85 { "option-file", 0, arg_string, &option_file }, 86 { "parse-units", 0, arg_negative_flag, &parse_units_flag }, 87 { "type-file", 0, arg_string, &type_file_string }, 88 { "version", 0, arg_flag, &version_flag }, 89 { "help", 0, arg_flag, &help_flag } 90}; 91int num_args = sizeof(args) / sizeof(args[0]); 92 93static void 94usage(int code) 95{ 96 arg_printusage(args, num_args, NULL, "[asn1-file [name]]"); 97 exit(code); 98} 99 100int error_flag; 101 102int 103main(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