1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "asn1parser.h"
static asn1p_expr_t *asn1p_expr_clone_impl(asn1p_expr_t *expr, int skip_extensions, asn1p_expr_t *(*)(asn1p_expr_t *, void *), void *);
static asn1p_value_t *value_resolver(asn1p_value_t *, void *arg);
/*
* Construct a new empty types collection.
*/
asn1p_expr_t *
asn1p_expr_new(int _lineno, asn1p_module_t *mod) {
asn1p_expr_t *expr;
expr = calloc(1, sizeof *expr);
if(expr) {
TQ_INIT(&(expr->members));
expr->spec_index = -1;
expr->module = mod;
expr->_lineno = _lineno;
}
return expr;
}
asn1p_expr_t *
asn1p_expr_clone(asn1p_expr_t *expr, int skip_extensions) {
return asn1p_expr_clone_impl(expr, skip_extensions, 0, 0);
}
asn1p_expr_t *
asn1p_expr_clone_with_resolver(asn1p_expr_t *expr, asn1p_expr_t *(*r)(asn1p_expr_t *, void *), void *rarg) {
return asn1p_expr_clone_impl(expr, 0, r, rarg);
}
static asn1p_expr_t *
asn1p_expr_clone_impl(asn1p_expr_t *expr, int skip_extensions, asn1p_expr_t *(*r)(asn1p_expr_t *, void *), void *rarg) {
asn1p_value_t *(*vr)(asn1p_value_t *, void *) = 0;
asn1p_expr_t *clone = 0;
asn1p_expr_t *tcmemb; /* Child of tc */
int hit_ext = 0;
#define CLCOPY(field) do { clone->field = expr->field; } while(0)
#define CLCLONE(field, func) do { if(expr->field) { \
clone->field = func(expr->field); \
if(clone->field == NULL) { \
asn1p_expr_free(clone); \
return NULL; \
} \
} } while(0)
#define CLVRCLONE(field, func) do { if(expr->field) { \
clone->field = func(expr->field, vr, rarg); \
if(clone->field == NULL) { \
asn1p_expr_free(clone); \
return NULL; \
} \
} } while(0)
if(r) {
vr = value_resolver;
clone = r(expr, rarg);
if(clone) {
/* Merge constraints */
if(expr->constraints) {
asn1p_constraint_t *tmpct = asn1p_constraint_clone_with_resolver(expr->constraints, vr, rarg);
if(clone->constraints) {
if(asn1p_constraint_insert(clone->constraints, tmpct)) {
asn1p_constraint_free(tmpct);
asn1p_expr_free(clone);
return NULL;
}
} else {
clone->constraints = tmpct;
}
assert(expr->combined_constraints == 0);
}
/* Merge defaults */
CLCOPY(marker.flags);
CLVRCLONE(marker.default_value,
asn1p_value_clone_with_resolver);
if(clone->tag.tag_class == TC_NOCLASS) {
CLCOPY(tag);
} else if(expr->tag.tag_class != TC_NOCLASS) {
fprintf(stderr, "asn1c does not support "
"nested tagging in parameterization, "
"necessary at line %d\n",
expr->_lineno);
asn1p_expr_free(clone);
return NULL;
}
return clone;
} else if(errno != ESRCH) {
return NULL; /* Hard error */
}
}
if(!clone) clone = asn1p_expr_new(expr->_lineno, expr->module);
if(!clone) return NULL;
/*
* Copy simple fields.
*/
CLCOPY(meta_type);
CLCOPY(expr_type);
CLCOPY(tag);
CLCOPY(marker.flags); /* OPTIONAL/DEFAULT */
CLCOPY(_mark);
clone->data = 0; /* Do not clone this */
clone->data_free = 0; /* Do not clone this */
/*
* Clone complex fields.
*/
CLCLONE(Identifier, strdup);
CLCLONE(reference, asn1p_ref_clone);
CLVRCLONE(constraints, asn1p_constraint_clone_with_resolver);
CLVRCLONE(combined_constraints, asn1p_constraint_clone_with_resolver);
CLCLONE(lhs_params, asn1p_paramlist_clone);
CLVRCLONE(value, asn1p_value_clone_with_resolver);
CLVRCLONE(marker.default_value, asn1p_value_clone_with_resolver);
CLCLONE(with_syntax, asn1p_wsyntx_clone);
/*
* Copy all the children of this expr.
*/
TQ_FOR(tcmemb, &(expr->members), next) {
asn1p_expr_t *cmemb;
if(skip_extensions
&& tcmemb->expr_type == A1TC_EXTENSIBLE) {
hit_ext++; /* Even if hit_ext wraps around, we're OK. */
continue;
}
if(hit_ext == 1) continue; /* Skip between ...'s */
cmemb = asn1p_expr_clone_impl(tcmemb, skip_extensions, r, rarg);
if(cmemb == NULL) {
asn1p_expr_free(clone);
return NULL;
}
asn1p_expr_add(clone, cmemb);
}
return clone;
}
static asn1p_value_t *
value_resolver(asn1p_value_t *value, void *rarg) {
asn1p_value_t *cval;
asn1p_expr_t *tmpexpr;
asn1p_expr_t *target;
asn1p_ref_t *ref;
struct {
asn1p_expr_t *(*expr_resolve)(asn1p_expr_t *, void *arg);
} *varg = rarg;
if(!value || value->type != ATV_REFERENCED) {
errno = ESRCH;
return NULL;
}
ref = value->value.reference;
tmpexpr = asn1p_expr_new(ref->_lineno, 0);
tmpexpr->meta_type = AMT_TYPEREF;
tmpexpr->expr_type = A1TC_REFERENCE;
tmpexpr->reference = ref;
target = varg->expr_resolve(tmpexpr, rarg);
tmpexpr->reference = 0;
asn1p_expr_free(tmpexpr);
if(!target)
return NULL; /* errno's are compatible */
if(target->meta_type == AMT_VALUE) {
if(!target->value) {
fprintf(stderr,
"FATAL: Parameterization did not resolve "
"value reference at line %d\n", ref->_lineno);
asn1p_expr_free(target);
errno = EPERM;
return NULL;
}
cval = asn1p_value_clone(target->value);
} else if(target->meta_type == AMT_VALUESET) {
if(!target->constraints) {
fprintf(stderr,
"FATAL: Parameterization did not resolve "
"value set reference at line %d\n", ref->_lineno);
asn1p_expr_free(target);
errno = EPERM;
return NULL;
}
cval = asn1p_value_fromconstr(target->constraints, 1);
} else {
errno = EPERM;
cval = NULL;
}
asn1p_expr_free(target);
return cval;
}
/*
* Add expression as a member of another.
*/
void
asn1p_expr_add(asn1p_expr_t *to, asn1p_expr_t *what) {
TQ_ADD(&(to->members), what, next);
what->parent_expr = to;
}
/*
* Add inner expressions as members of another.
*/
void
asn1p_expr_add_many(asn1p_expr_t *to, asn1p_expr_t *from_what) {
asn1p_expr_t *expr;
TQ_FOR(expr, &(from_what->members), next) {
expr->parent_expr = to;
}
TQ_CONCAT(&(to->members), &(from_what->members), next);
}
/*
* Destruct the types collection structure.
*/
void
asn1p_expr_free(asn1p_expr_t *expr) {
if(expr) {
asn1p_expr_t *tm;
/* Remove all children */
while((tm = TQ_REMOVE(&(expr->members), next))) {
if(tm->parent_expr != expr)
printf("<%s:%p !-> %s:%p>\n",
tm->Identifier, tm->parent_expr,
expr->Identifier, expr);
assert(tm->parent_expr == expr);
asn1p_expr_free(tm);
}
free(expr->Identifier);
if(expr->reference)
asn1p_ref_free(expr->reference);
if(expr->constraints)
asn1p_constraint_free(expr->constraints);
if(expr->combined_constraints)
asn1p_constraint_free(expr->combined_constraints);
if(expr->lhs_params)
asn1p_paramlist_free(expr->lhs_params);
if(expr->value)
asn1p_value_free(expr->value);
if(expr->marker.default_value)
asn1p_value_free(expr->marker.default_value);
if(expr->with_syntax)
asn1p_wsyntx_free(expr->with_syntax);
if(expr->data && expr->data_free)
expr->data_free(expr->data);
memset(expr, 0, sizeof(*expr));
free(expr);
}
}
char *asn1p_tag2string(struct asn1p_type_tag_s *tag, char *buf) {
static char buf_stat[TAG2STRING_BUFFER_SIZE];
char *start;
char *end;
if(!buf) buf = buf_stat;
start = buf;
end = buf + TAG2STRING_BUFFER_SIZE;
if(tag->tag_class == TC_NOCLASS) {
*buf = 0;
return buf;
}
strcpy(buf, "[");
switch(tag->tag_class) {
case TC_NOCLASS:
assert(tag->tag_class != TC_NOCLASS);
break;
case TC_UNIVERSAL: strcat(buf, "UNIVERSAL "); break;
case TC_PRIVATE: strcat(buf, "PRIVATE "); break;
case TC_APPLICATION: strcat(buf, "APPLICATION "); break;
case TC_CONTEXT_SPECIFIC:
break;
}
buf += snprintf(buf + strlen(buf), end - buf,
"%" PRIdASN "]", tag->tag_value);
assert((unsigned int)(buf - end) > sizeof(" IMPLICIT "));
switch(tag->tag_mode) {
case TM_DEFAULT: break;
case TM_IMPLICIT: strcat(buf, " IMPLICIT"); break;
case TM_EXPLICIT: strcat(buf, " EXPLICIT"); break;
}
return start;
}
|