| 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 |
|
| 38 |
extern FILE *yyin; |
| 39 |
|
| 40 |
static getarg_strings preserve; |
| 41 |
static getarg_strings seq; |
| 42 |
|
| 43 |
int |
| 44 |
preserve_type(const char *p) |
| 45 |
{ |
| 46 |
int i; |
| 47 |
for (i = 0; i < preserve.num_strings; i++) |
| 48 |
if (strcmp(preserve.strings[i], p) == 0) |
| 49 |
return 1; |
| 50 |
return 0; |
| 51 |
} |
| 52 |
|
| 53 |
int |
| 54 |
seq_type(const char *p) |
| 55 |
{ |
| 56 |
int i; |
| 57 |
for (i = 0; i < seq.num_strings; i++) |
| 58 |
if (strcmp(seq.strings[i], p) == 0) |
| 59 |
return 1; |
| 60 |
return 0; |
| 61 |
} |
| 62 |
|
| 63 |
const char *fuzzer_string = ""; |
| 64 |
int fuzzer_flag; |
| 65 |
int support_ber; |
| 66 |
int template_flag; |
| 67 |
int rfc1510_bitstring; |
| 68 |
int one_code_file; |
| 69 |
char *option_file; |
| 70 |
int parse_units_flag = 1; |
| 71 |
char *type_file_string = "krb5-types.h"; |
| 72 |
int version_flag; |
| 73 |
int help_flag; |
| 74 |
struct getargs args[] = { |
| 75 |
{ "fuzzer", 0, arg_flag, &fuzzer_flag, NULL, NULL }, |
| 76 |
{ "template", 0, arg_flag, &template_flag, NULL, NULL }, |
| 77 |
{ "encode-rfc1510-bit-string", 0, arg_flag, &rfc1510_bitstring, NULL, NULL }, |
| 78 |
{ "decode-dce-ber", 0, arg_flag, &support_ber, NULL, NULL }, |
| 79 |
{ "support-ber", 0, arg_flag, &support_ber, NULL, NULL }, |
| 80 |
{ "preserve-binary", 0, arg_strings, &preserve, NULL, NULL }, |
| 81 |
{ "sequence", 0, arg_strings, &seq, NULL, NULL }, |
| 82 |
{ "one-code-file", 0, arg_flag, &one_code_file, NULL, NULL }, |
| 83 |
{ "option-file", 0, arg_string, &option_file, NULL, NULL }, |
| 84 |
{ "parse-units", 0, arg_negative_flag, &parse_units_flag, NULL, NULL }, |
| 85 |
{ "type-file", 0, arg_string, &type_file_string, NULL, NULL }, |
| 86 |
{ "version", 0, arg_flag, &version_flag, NULL, NULL }, |
| 87 |
{ "help", 0, arg_flag, &help_flag, NULL, NULL } |
| 88 |
}; |
| 89 |
int num_args = sizeof(args) / sizeof(args[0]); |
| 90 |
|
| 91 |
static void |
| 92 |
usage(int code) |
| 93 |
{ |
| 94 |
arg_printusage(args, num_args, NULL, "[asn1-file [name]]"); |
| 95 |
exit(code); |
| 96 |
} |
| 97 |
|
| 98 |
int error_flag; |
| 99 |
|
| 100 |
int |
| 101 |
main(int argc, char **argv) |
| 102 |
{ |
| 103 |
int ret; |
| 104 |
const char *file; |
| 105 |
const char *name = NULL; |
| 106 |
int optidx = 0; |
| 107 |
char **arg = NULL; |
| 108 |
int len = 0, i; |
| 109 |
|
| 110 |
setprogname(argv[0]); |
| 111 |
if(getarg(args, num_args, argc, argv, &optidx)) |
| 112 |
usage(1); |
| 113 |
if(help_flag) |
| 114 |
usage(0); |
| 115 |
if(version_flag) { |
| 116 |
print_version(NULL); |
| 117 |
exit(0); |
| 118 |
} |
| 119 |
if (argc == optidx) { |
| 120 |
file = "stdin"; |
| 121 |
name = "stdin"; |
| 122 |
yyin = stdin; |
| 123 |
} else { |
| 124 |
file = argv[optidx]; |
| 125 |
yyin = fopen (file, "r"); |
| 126 |
if (yyin == NULL) |
| 127 |
err (1, "open %s", file); |
| 128 |
if (argc == optidx + 1) { |
| 129 |
char *p; |
| 130 |
name = estrdup(file); |
| 131 |
p = strrchr(name, '.'); |
| 132 |
if (p) |
| 133 |
*p = '\0'; |
| 134 |
} else |
| 135 |
name = argv[optidx + 1]; |
| 136 |
} |
| 137 |
|
| 138 |
/* |
| 139 |
* Parse extra options file |
| 140 |
*/ |
| 141 |
if (option_file) { |
| 142 |
char buf[1024]; |
| 143 |
FILE *opt; |
| 144 |
|
| 145 |
opt = fopen(option_file, "r"); |
| 146 |
if (opt == NULL) { |
| 147 |
perror("open"); |
| 148 |
exit(1); |
| 149 |
} |
| 150 |
|
| 151 |
arg = calloc(2, sizeof(arg[0])); |
| 152 |
if (arg == NULL) { |
| 153 |
perror("calloc"); |
| 154 |
exit(1); |
| 155 |
} |
| 156 |
arg[0] = option_file; |
| 157 |
arg[1] = NULL; |
| 158 |
len = 1; |
| 159 |
|
| 160 |
while (fgets(buf, sizeof(buf), opt) != NULL) { |
| 161 |
buf[strcspn(buf, "\n\r")] = '\0'; |
| 162 |
|
| 163 |
arg = realloc(arg, (len + 2) * sizeof(arg[0])); |
| 164 |
if (arg == NULL) { |
| 165 |
perror("malloc"); |
| 166 |
exit(1); |
| 167 |
} |
| 168 |
arg[len] = strdup(buf); |
| 169 |
if (arg[len] == NULL) { |
| 170 |
perror("strdup"); |
| 171 |
exit(1); |
| 172 |
} |
| 173 |
arg[len + 1] = NULL; |
| 174 |
len++; |
| 175 |
} |
| 176 |
fclose(opt); |
| 177 |
|
| 178 |
optidx = 0; |
| 179 |
if(getarg(args, num_args, len, arg, &optidx)) |
| 180 |
usage(1); |
| 181 |
|
| 182 |
if (len != optidx) { |
| 183 |
fprintf(stderr, "extra args"); |
| 184 |
exit(1); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
if (fuzzer_flag) { |
| 189 |
if (!template_flag) { |
| 190 |
printf("can't do fuzzer w/o --template"); |
| 191 |
exit(1); |
| 192 |
} |
| 193 |
#ifdef ASN1_FUZZER |
| 194 |
fuzzer_string = "_fuzzer"; |
| 195 |
#endif |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
init_generate (file, name); |
| 200 |
|
| 201 |
if (one_code_file) |
| 202 |
generate_header_of_codefile(name); |
| 203 |
|
| 204 |
initsym (); |
| 205 |
ret = yyparse (); |
| 206 |
if(ret != 0 || error_flag != 0) |
| 207 |
exit(1); |
| 208 |
close_generate (); |
| 209 |
if (argc != optidx) |
| 210 |
fclose(yyin); |
| 211 |
|
| 212 |
if (one_code_file) |
| 213 |
close_codefile(); |
| 214 |
|
| 215 |
if (arg) { |
| 216 |
for (i = 1; i < len; i++) |
| 217 |
free(arg[i]); |
| 218 |
free(arg); |
| 219 |
} |
| 220 |
|
| 221 |
return 0; |
| 222 |
} |