blob: 7a38f3c1477c21149855395bae140712a3cc955d [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#include <stdio.h>
2#include <stdlib.h>
3#include <errno.h>
4#include <assert.h>
5
6#include "asn1parser.h"
7
8asn1p_constraint_t *
9asn1p_constraint_new(int _lineno) {
10 asn1p_constraint_t *ct;
11
12 ct = calloc(1, sizeof(*ct));
13 if(ct) {
14 ct->_lineno = _lineno;
15 }
16
17 return ct;
18}
19
20
21void
22asn1p_constraint_free(asn1p_constraint_t *ct) {
23 if(ct) {
24
25 if(ct->value)
26 asn1p_value_free(ct->value);
27 if(ct->range_start)
28 asn1p_value_free(ct->range_start);
29 if(ct->range_stop)
30 asn1p_value_free(ct->range_stop);
31
32 if(ct->elements) {
33 while(ct->el_count--) {
34 asn1p_constraint_free(
35 ct->elements[ct->el_count]);
36 }
37 free(ct->elements);
38 }
39
40 free(ct);
41 }
42}
43
44asn1p_constraint_t *
45asn1p_constraint_clone(asn1p_constraint_t *src) {
46 asn1p_constraint_t *clone;
47
48#define CLONE(field, func) do { if(src->field) { \
49 clone->field = func(src->field); \
50 if(clone->field == NULL) { \
51 asn1p_constraint_free(clone); \
52 return NULL; \
53 } \
54 } } while(0)
55
56 clone = asn1p_constraint_new(src->_lineno);
57 if(clone) {
Lev Walkine972d932004-09-05 10:40:28 +000058 unsigned int i;
Lev Walkin26b6e042004-08-13 12:35:29 +000059
Lev Walkinf15320b2004-06-03 03:38:44 +000060 clone->type = src->type;
Lev Walkin26b6e042004-08-13 12:35:29 +000061 clone->presence = src->presence;
Lev Walkinf15320b2004-06-03 03:38:44 +000062 CLONE(value, asn1p_value_clone);
63 CLONE(range_start, asn1p_value_clone);
64 CLONE(range_stop, asn1p_value_clone);
Lev Walkin26b6e042004-08-13 12:35:29 +000065
66 for(i = 0; i < src->el_count; i++) {
67 asn1p_constraint_t *t;
68 t = asn1p_constraint_clone(src->elements[i]);
69 if(!t) {
70 asn1p_constraint_free(clone);
71 return NULL;
72 }
73 if(asn1p_constraint_insert(clone, t)) {
74 asn1p_constraint_free(clone);
75 asn1p_constraint_free(t);
76 return NULL;
77 }
78 }
79 assert(clone->el_count == src->el_count);
80 clone->_lineno = src->_lineno;
Lev Walkinf15320b2004-06-03 03:38:44 +000081 }
82
83 return clone;
84}
85
86int
87asn1p_constraint_insert(asn1p_constraint_t *into, asn1p_constraint_t *what) {
88 assert(into);
89 assert(what);
90
91 /*
92 * Make sure there's enough space to add an element.
93 */
94 if(into->el_count == into->el_size) {
Lev Walkine972d932004-09-05 10:40:28 +000095 unsigned int newsize = into->el_size?into->el_size<<2:4;
Lev Walkinf15320b2004-06-03 03:38:44 +000096 void *p;
97 p = realloc(into->elements,
98 newsize * sizeof(into->elements[0]));
99 if(p) {
100 into->elements = p;
101 into->el_size = newsize;
102 } else {
103 return -1;
104 }
105 }
106
107 into->elements[into->el_count++] = what;
108
109 return 0;
110}
Lev Walkinf59d0752004-08-18 04:59:12 +0000111
112
113char *
114asn1p_constraint_type2str(enum asn1p_constraint_type_e type) {
115 switch(type) {
116 case ACT_INVALID:
117 return "INVALID";
118 case ACT_EL_VALUE:
119 return "SingleValue";
120 case ACT_EL_RANGE:
121 case ACT_EL_LLRANGE:
122 case ACT_EL_RLRANGE:
123 case ACT_EL_ULRANGE:
124 return "ValueRange";
125 case ACT_EL_EXT:
126 return "...";
127 case ACT_CT_SIZE:
128 return "SizeConstraint";
129 case ACT_CT_FROM:
130 return "PermittedAlphabet";
131 case ACT_CT_WCOMP:
132 return "SingleTypeConstraint";
133 case ACT_CT_WCOMPS:
134 return "MultipleTypeConstraints";
135 case ACT_CA_SET:
136 return "SET";
137 case ACT_CA_CRC:
138 return "ComponentRelationConstraint";
139 case ACT_CA_CSV:
140 return "CSV";
141 case ACT_CA_UNI:
142 return "UNION";
143 case ACT_CA_INT:
144 return "INTERSECTION";
145 case ACT_CA_EXC:
146 return "EXCEPT";
147 }
148 return "UNKNOWN";
149}