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