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 * |
| 27 | asn1p_parse_buffer(const char *buffer, int size /* = -1 */, enum asn1p_flags flags) { |
| 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 | |
| 47 | asn1p_lineno = 1; |
| 48 | |
| 49 | ap = (void *)&a; |
| 50 | ret = asn1p_parse(ap); |
| 51 | |
| 52 | asn1p__delete_buffer(ybuf); |
| 53 | |
| 54 | if(ret == 0) { |
| 55 | assert(a); |
Lev Walkin | 070a52d | 2004-08-22 03:19:54 +0000 | [diff] [blame] | 56 | if(_asn1p_fix_modules(a, "-")) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 57 | return NULL; /* FIXME: destroy (a) */ |
Lev Walkin | 4cd4304 | 2005-04-08 10:14:30 +0000 | [diff] [blame] | 58 | } else if(a) { |
Lev Walkin | 9b5df84 | 2006-09-12 04:21:30 +0000 | [diff] [blame] | 59 | asn1p_delete(a); |
Lev Walkin | 4cd4304 | 2005-04-08 10:14:30 +0000 | [diff] [blame] | 60 | a = NULL; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | return a; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | /* |
| 68 | * Parse the file identified by its name. |
| 69 | */ |
| 70 | asn1p_t * |
| 71 | asn1p_parse_file(const char *filename, enum asn1p_flags flags) { |
Lev Walkin | 9365956 | 2010-11-20 09:47:13 -0800 | [diff] [blame] | 72 | #ifndef _WIN32 |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 73 | struct stat sb; |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 74 | #endif |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 75 | asn1p_t *a = 0; |
| 76 | void *ap; |
| 77 | FILE *fp; |
| 78 | int ret; |
| 79 | |
| 80 | if(_asn1p_set_flags(flags)) { |
| 81 | /* EINVAL */ |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | fp = fopen(filename, "r"); |
| 86 | if(fp == NULL) { |
| 87 | perror(filename); |
| 88 | return NULL; |
| 89 | } |
| 90 | |
Lev Walkin | 9365956 | 2010-11-20 09:47:13 -0800 | [diff] [blame] | 91 | #ifndef _WIN32 |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 92 | if(fstat(fileno(fp), &sb) |
| 93 | || !S_ISREG(sb.st_mode)) { |
| 94 | fclose(fp); |
| 95 | fprintf(stderr, "%s file not recognized: Bad file format\n", |
| 96 | filename); |
| 97 | errno = EINVAL; |
| 98 | return NULL; |
| 99 | } |
Lev Walkin | 9365956 | 2010-11-20 09:47:13 -0800 | [diff] [blame] | 100 | #endif /* _WIN32 */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 101 | |
| 102 | asn1p_lineno = 1; |
| 103 | |
| 104 | asn1p_restart(fp); |
| 105 | |
| 106 | ap = (void *)&a; |
| 107 | ret = asn1p_parse(ap); |
| 108 | |
| 109 | fclose(fp); |
| 110 | |
| 111 | if(ret == 0) { |
| 112 | assert(a); |
Lev Walkin | 070a52d | 2004-08-22 03:19:54 +0000 | [diff] [blame] | 113 | if(_asn1p_fix_modules(a, filename)) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 114 | return NULL; /* FIXME: destroy (a) */ |
Lev Walkin | 4cd4304 | 2005-04-08 10:14:30 +0000 | [diff] [blame] | 115 | } else if(a) { |
Lev Walkin | 9b5df84 | 2006-09-12 04:21:30 +0000 | [diff] [blame] | 116 | asn1p_delete(a); |
Lev Walkin | c16cf43 | 2005-04-08 10:12:40 +0000 | [diff] [blame] | 117 | a = NULL; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return a; |
| 121 | } |
| 122 | |
| 123 | extern int asn1p_lexer_types_year; |
| 124 | extern int asn1p_lexer_constructs_year; |
| 125 | extern int asn1p__flex_debug; |
| 126 | |
| 127 | static int |
| 128 | _asn1p_set_flags(enum asn1p_flags flags) { |
| 129 | |
| 130 | asn1p_lexer_types_year = 0; |
| 131 | asn1p_lexer_constructs_year = 0; |
| 132 | asn1p__flex_debug = 0; |
| 133 | |
| 134 | /* |
| 135 | * Enable debugging in lexer. |
| 136 | */ |
| 137 | if(flags & A1P_LEXER_DEBUG) { |
| 138 | flags &= ~A1P_LEXER_DEBUG; |
| 139 | asn1p__flex_debug = 1; |
| 140 | } |
| 141 | |
| 142 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 143 | * Check that we haven't missed an unknown flag. |
| 144 | */ |
| 145 | if(flags) { |
| 146 | errno = EINVAL; |
| 147 | return -1; |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static int |
Lev Walkin | 070a52d | 2004-08-22 03:19:54 +0000 | [diff] [blame] | 154 | _asn1p_fix_modules(asn1p_t *a, const char *fname) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 155 | asn1p_module_t *mod; |
| 156 | TQ_FOR(mod, &(a->modules), mod_next) { |
Lev Walkin | b1107e6 | 2004-09-27 20:54:44 +0000 | [diff] [blame] | 157 | mod->source_file_name = strdup(fname); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 158 | if(mod->source_file_name == NULL) |
| 159 | return -1; |
Lev Walkin | a9532f4 | 2006-09-17 04:52:50 +0000 | [diff] [blame] | 160 | mod->asn1p = a; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 161 | } |
| 162 | return 0; |
| 163 | } |
Lev Walkin | 070a52d | 2004-08-22 03:19:54 +0000 | [diff] [blame] | 164 | |
| 165 | |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 166 | int |
| 167 | asn1p_atoi(const char *ptr, asn1c_integer_t *value) { |
| 168 | errno = 0; /* Clear the error code */ |
| 169 | |
| 170 | if(sizeof(*value) <= sizeof(int)) { |
| 171 | *value = strtol(ptr, 0, 10); |
| 172 | } else { |
| 173 | #ifdef HAVE_STRTOIMAX |
| 174 | *value = strtoimax(ptr, 0, 10); |
| 175 | #elif HAVE_STRTOLL |
| 176 | *value = strtoll(ptr, 0, 10); |
| 177 | #else |
| 178 | *value = strtol(ptr, 0, 10); |
| 179 | #endif |
| 180 | } |
| 181 | |
| 182 | return errno == 0 ? 0 : -1; |
| 183 | } |