7 #include "asn1parser.h"
10 asn1p_constraint_set_source(asn1p_constraint_t *ct,
11 struct asn1p_module_s *module, int lineno) {
15 asn1p_value_set_source(ct->containedSubtype,module,lineno);
16 asn1p_value_set_source(ct->value,module,lineno);
17 asn1p_value_set_source(ct->range_start,module,lineno);
18 asn1p_value_set_source(ct->range_stop,module,lineno);
19 for(size_t i = 0; i < ct->el_count; i++) {
20 asn1p_constraint_set_source(ct->elements[i], module, lineno);
25 int asn1p_constraint_compare(const asn1p_constraint_t *a,
26 const asn1p_constraint_t *b) {
29 if(a->type != b->type)
32 /* Currently we only check VALUESET as a reference */
33 if(a->type == ACT_EL_TYPE) {
34 return strcmp(a->containedSubtype->value.v_type->reference->components[0].name,
35 b->containedSubtype->value.v_type->reference->components[0].name);
42 asn1p_constraint_new(int _lineno, asn1p_module_t *mod) {
43 asn1p_constraint_t *ct;
45 ct = calloc(1, sizeof(*ct));
47 ct->_lineno = _lineno;
56 asn1p_constraint_free(asn1p_constraint_t *ct) {
59 asn1p_value_free(ct->containedSubtype);
60 asn1p_value_free(ct->value);
61 asn1p_value_free(ct->range_start);
62 asn1p_value_free(ct->range_stop);
65 while(ct->el_count--) {
66 asn1p_constraint_free(
67 ct->elements[ct->el_count]);
77 asn1p_constraint_clone(asn1p_constraint_t *src) {
78 return asn1p_constraint_clone_with_resolver(src, 0, 0);
82 asn1p_constraint_clone_with_resolver(asn1p_constraint_t *src,
83 asn1p_value_t *(*vr)(asn1p_value_t *, void *varg), void *varg) {
84 asn1p_constraint_t *clone;
86 #define CLONE(field, func) do { if(src->field) { \
87 clone->field = func(src->field, vr, varg); \
88 if(clone->field == NULL) { \
89 asn1p_constraint_free(clone); \
94 clone = asn1p_constraint_new(src->_lineno, src->module);
98 clone->type = src->type;
99 clone->presence = src->presence;
100 CLONE(containedSubtype, asn1p_value_clone_with_resolver);
101 CLONE(value, asn1p_value_clone_with_resolver);
102 CLONE(range_start, asn1p_value_clone_with_resolver);
103 CLONE(range_stop, asn1p_value_clone_with_resolver);
105 for(i = 0; i < src->el_count; i++) {
106 asn1p_constraint_t *t;
107 t = asn1p_constraint_clone_with_resolver(src->elements[i], vr, varg);
109 asn1p_constraint_free(clone);
112 if(asn1p_constraint_insert(clone, t)) {
113 asn1p_constraint_free(clone);
114 asn1p_constraint_free(t);
118 assert(clone->el_count == src->el_count);
119 clone->_lineno = src->_lineno;
126 * Make sure there's enough space to add an element.
129 asn1p_constraint_make_memory(asn1p_constraint_t *ct) {
130 if(ct->el_count == ct->el_size) {
131 unsigned int newsize = ct->el_size ? ct->el_size << 2 : 4;
133 p = realloc(ct->elements, newsize * sizeof(ct->elements[0]));
136 ct->el_size = newsize;
145 asn1p_constraint_insert(asn1p_constraint_t *into, asn1p_constraint_t *what) {
149 if(asn1p_constraint_make_memory(into))
152 into->elements[into->el_count++] = what;
153 what->parent_ct = into;
159 asn1p_constraint_prepend(asn1p_constraint_t *before, asn1p_constraint_t *what) {
163 if(asn1p_constraint_make_memory(before))
166 memmove(&before->elements[1], &before->elements[0],
167 before->el_count * sizeof(before->elements[0]));
169 before->elements[0] = what;
171 what->parent_ct = before;
178 asn1p_constraint_type2str(enum asn1p_constraint_type_e type) {
183 return "ContainedSubtype";
185 return "SingleValue";
194 return "SizeConstraint";
196 return "PermittedAlphabet";
198 return "SingleTypeConstraint";
200 return "MultipleTypeConstraints";
202 return "UserDefinedConstraint";
204 return "ContentsConstraint";
206 return "PatternConstraint";
210 return "ComponentRelationConstraint";
216 return "INTERSECTION";
225 const asn1p_constraint_t *
226 asn1p_get_component_relation_constraint(asn1p_constraint_t *ct) {
228 if(ct->type == ACT_CA_CRC)
230 if(ct->type == ACT_CA_SET) {
231 for(size_t i = 0; i < ct->el_count; i++) {
232 const asn1p_constraint_t *tmp =
233 asn1p_get_component_relation_constraint(ct->elements[i]);