Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
Lev Walkin | b1107e6 | 2004-09-27 20:54:44 +0000 | [diff] [blame] | 2 | #include <string.h> |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 3 | #include <stdlib.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 4 | #include <sys/types.h> |
| 5 | #include <sys/stat.h> |
| 6 | #include <assert.h> |
| 7 | #include <errno.h> |
| 8 | |
| 9 | #include "asn1parser.h" |
| 10 | #include "asn1p_list.h" |
| 11 | |
| 12 | int asn1p_parse(void **param); |
| 13 | |
| 14 | void *asn1p__scan_bytes(const char *, int len); |
| 15 | void *asn1p__delete_buffer(void *); |
| 16 | void *asn1p_restart(FILE *); |
| 17 | |
| 18 | extern int asn1p_lineno; |
| 19 | |
| 20 | static int _asn1p_set_flags(enum asn1p_flags flags); |
Lev Walkin | 070a52d | 2004-08-22 03:19:54 +0000 | [diff] [blame] | 21 | static int _asn1p_fix_modules(asn1p_t *a, const char *fname); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 22 | |
| 23 | /* |
| 24 | * Parse the given buffer. |
| 25 | */ |
| 26 | asn1p_t * |
Lev Walkin | be518fa | 2017-09-07 02:05:28 -0700 | [diff] [blame] | 27 | asn1p_parse_buffer(const char *buffer, int size /* = -1 */, const char *debug_filename, int initial_lineno, enum asn1p_flags flags) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 28 | asn1p_t *a = 0; |
| 29 | void *ap; |
| 30 | void *ybuf; |
| 31 | int ret; |
| 32 | |
| 33 | if(_asn1p_set_flags(flags)) { |
| 34 | /* EINVAL */ |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | if(size < 0) |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 39 | size = (int)strlen(buffer); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 40 | |
| 41 | ybuf = asn1p__scan_bytes(buffer, size); |
| 42 | if(!ybuf) { |
| 43 | assert(ybuf); |
| 44 | return 0; |
| 45 | } |
| 46 | |
Lev Walkin | be518fa | 2017-09-07 02:05:28 -0700 | [diff] [blame] | 47 | asn1p_lineno = initial_lineno; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 48 | |
| 49 | ap = (void *)&a; |
Lev Walkin | be518fa | 2017-09-07 02:05:28 -0700 | [diff] [blame] | 50 | asn1p_parse_debug_filename = debug_filename; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 51 | ret = asn1p_parse(ap); |
Lev Walkin | be518fa | 2017-09-07 02:05:28 -0700 | [diff] [blame] | 52 | asn1p_parse_debug_filename = NULL; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 53 | |
| 54 | asn1p__delete_buffer(ybuf); |
| 55 | |
| 56 | if(ret == 0) { |
| 57 | assert(a); |
Bi-Ruei, Chiu | 3dcf05b | 2017-05-04 21:45:05 +0800 | [diff] [blame] | 58 | if(_asn1p_fix_modules(a, "-")) { |
| 59 | asn1p_delete(a); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 60 | return NULL; /* FIXME: destroy (a) */ |
Bi-Ruei, Chiu | 3dcf05b | 2017-05-04 21:45:05 +0800 | [diff] [blame] | 61 | } |
Lev Walkin | 4cd4304 | 2005-04-08 10:14:30 +0000 | [diff] [blame] | 62 | } else if(a) { |
Lev Walkin | 9b5df84 | 2006-09-12 04:21:30 +0000 | [diff] [blame] | 63 | asn1p_delete(a); |
Lev Walkin | 4cd4304 | 2005-04-08 10:14:30 +0000 | [diff] [blame] | 64 | a = NULL; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | return a; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /* |
| 72 | * Parse the file identified by its name. |
| 73 | */ |
| 74 | asn1p_t * |
| 75 | asn1p_parse_file(const char *filename, enum asn1p_flags flags) { |
Lev Walkin | 9365956 | 2010-11-20 09:47:13 -0800 | [diff] [blame] | 76 | #ifndef _WIN32 |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 77 | struct stat sb; |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 78 | #endif |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 79 | asn1p_t *a = 0; |
| 80 | void *ap; |
| 81 | FILE *fp; |
| 82 | int ret; |
| 83 | |
| 84 | if(_asn1p_set_flags(flags)) { |
| 85 | /* EINVAL */ |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | fp = fopen(filename, "r"); |
| 90 | if(fp == NULL) { |
| 91 | perror(filename); |
| 92 | return NULL; |
| 93 | } |
| 94 | |
Lev Walkin | 9365956 | 2010-11-20 09:47:13 -0800 | [diff] [blame] | 95 | #ifndef _WIN32 |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 96 | if(fstat(fileno(fp), &sb) |
| 97 | || !S_ISREG(sb.st_mode)) { |
| 98 | fclose(fp); |
| 99 | fprintf(stderr, "%s file not recognized: Bad file format\n", |
| 100 | filename); |
| 101 | errno = EINVAL; |
| 102 | return NULL; |
| 103 | } |
Lev Walkin | 9365956 | 2010-11-20 09:47:13 -0800 | [diff] [blame] | 104 | #endif /* _WIN32 */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 105 | |
| 106 | asn1p_lineno = 1; |
| 107 | |
| 108 | asn1p_restart(fp); |
| 109 | |
| 110 | ap = (void *)&a; |
Lev Walkin | d523ea4 | 2017-09-06 22:15:08 -0700 | [diff] [blame] | 111 | asn1p_parse_debug_filename = filename; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 112 | ret = asn1p_parse(ap); |
Lev Walkin | d523ea4 | 2017-09-06 22:15:08 -0700 | [diff] [blame] | 113 | asn1p_parse_debug_filename = NULL; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 114 | |
| 115 | fclose(fp); |
| 116 | |
| 117 | if(ret == 0) { |
| 118 | assert(a); |
Bi-Ruei, Chiu | 3dcf05b | 2017-05-04 21:45:05 +0800 | [diff] [blame] | 119 | if(_asn1p_fix_modules(a, filename)) { |
| 120 | asn1p_delete(a); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 121 | return NULL; /* FIXME: destroy (a) */ |
Bi-Ruei, Chiu | 3dcf05b | 2017-05-04 21:45:05 +0800 | [diff] [blame] | 122 | } |
Lev Walkin | 4cd4304 | 2005-04-08 10:14:30 +0000 | [diff] [blame] | 123 | } else if(a) { |
Lev Walkin | 9b5df84 | 2006-09-12 04:21:30 +0000 | [diff] [blame] | 124 | asn1p_delete(a); |
Lev Walkin | c16cf43 | 2005-04-08 10:12:40 +0000 | [diff] [blame] | 125 | a = NULL; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | return a; |
| 129 | } |
| 130 | |
| 131 | extern int asn1p_lexer_types_year; |
| 132 | extern int asn1p_lexer_constructs_year; |
Lev Walkin | be518fa | 2017-09-07 02:05:28 -0700 | [diff] [blame] | 133 | extern int asn1p_lexer_extended_values; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 134 | extern int asn1p__flex_debug; |
Lev Walkin | 0c68645 | 2017-09-07 22:59:36 -0700 | [diff] [blame] | 135 | extern int asn1p_debug; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 136 | |
| 137 | static int |
| 138 | _asn1p_set_flags(enum asn1p_flags flags) { |
| 139 | |
| 140 | asn1p_lexer_types_year = 0; |
| 141 | asn1p_lexer_constructs_year = 0; |
| 142 | asn1p__flex_debug = 0; |
Lev Walkin | 0c68645 | 2017-09-07 22:59:36 -0700 | [diff] [blame] | 143 | asn1p_debug = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 144 | |
Lev Walkin | 0c68645 | 2017-09-07 22:59:36 -0700 | [diff] [blame] | 145 | if(flags & A1P_DEBUG_LEXER) { |
| 146 | flags &= ~A1P_DEBUG_LEXER; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 147 | asn1p__flex_debug = 1; |
Lev Walkin | be518fa | 2017-09-07 02:05:28 -0700 | [diff] [blame] | 148 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 149 | |
Lev Walkin | 0c68645 | 2017-09-07 22:59:36 -0700 | [diff] [blame] | 150 | if(flags & A1P_DEBUG_PARSER) { |
| 151 | flags &= ~A1P_DEBUG_PARSER; |
| 152 | asn1p_debug = 1; |
| 153 | } |
| 154 | |
Lev Walkin | be518fa | 2017-09-07 02:05:28 -0700 | [diff] [blame] | 155 | if(flags & A1P_EXTENDED_VALUES) { |
| 156 | flags &= ~A1P_EXTENDED_VALUES; |
| 157 | asn1p_lexer_extended_values = 1; |
| 158 | } else { |
| 159 | asn1p_lexer_extended_values = 0; |
| 160 | } |
| 161 | |
| 162 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 163 | * Check that we haven't missed an unknown flag. |
| 164 | */ |
| 165 | if(flags) { |
| 166 | errno = EINVAL; |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static int |
Lev Walkin | 070a52d | 2004-08-22 03:19:54 +0000 | [diff] [blame] | 174 | _asn1p_fix_modules(asn1p_t *a, const char *fname) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 175 | asn1p_module_t *mod; |
| 176 | TQ_FOR(mod, &(a->modules), mod_next) { |
Lev Walkin | b1107e6 | 2004-09-27 20:54:44 +0000 | [diff] [blame] | 177 | mod->source_file_name = strdup(fname); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 178 | if(mod->source_file_name == NULL) |
| 179 | return -1; |
Lev Walkin | a9532f4 | 2006-09-17 04:52:50 +0000 | [diff] [blame] | 180 | mod->asn1p = a; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 181 | } |
| 182 | return 0; |
| 183 | } |