blob: 69802698b3e0df23adb2ab16d7241591ac83781f [file] [log] [blame]
vlmfa67ddc2004-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
vlma6a12e32005-03-20 12:58:00 +000025 if(ct->containedSubtype)
26 asn1p_value_free(ct->containedSubtype);
vlmfa67ddc2004-06-03 03:38:44 +000027 if(ct->value)
28 asn1p_value_free(ct->value);
29 if(ct->range_start)
30 asn1p_value_free(ct->range_start);
31 if(ct->range_stop)
32 asn1p_value_free(ct->range_stop);
33
34 if(ct->elements) {
35 while(ct->el_count--) {
36 asn1p_constraint_free(
37 ct->elements[ct->el_count]);
38 }
39 free(ct->elements);
40 }
41
42 free(ct);
43 }
44}
45
46asn1p_constraint_t *
47asn1p_constraint_clone(asn1p_constraint_t *src) {
48 asn1p_constraint_t *clone;
49
50#define CLONE(field, func) do { if(src->field) { \
51 clone->field = func(src->field); \
52 if(clone->field == NULL) { \
53 asn1p_constraint_free(clone); \
54 return NULL; \
55 } \
56 } } while(0)
57
58 clone = asn1p_constraint_new(src->_lineno);
59 if(clone) {
vlm36a76d62004-09-05 10:40:28 +000060 unsigned int i;
vlm3b4ff462004-08-13 12:35:29 +000061
vlmfa67ddc2004-06-03 03:38:44 +000062 clone->type = src->type;
vlm3b4ff462004-08-13 12:35:29 +000063 clone->presence = src->presence;
vlma6a12e32005-03-20 12:58:00 +000064 CLONE(containedSubtype, asn1p_value_clone);
65 CLONE(value, asn1p_value_clone);
66 CLONE(range_start, asn1p_value_clone);
67 CLONE(range_stop, asn1p_value_clone);
vlm3b4ff462004-08-13 12:35:29 +000068
69 for(i = 0; i < src->el_count; i++) {
70 asn1p_constraint_t *t;
71 t = asn1p_constraint_clone(src->elements[i]);
72 if(!t) {
73 asn1p_constraint_free(clone);
74 return NULL;
75 }
76 if(asn1p_constraint_insert(clone, t)) {
77 asn1p_constraint_free(clone);
78 asn1p_constraint_free(t);
79 return NULL;
80 }
81 }
82 assert(clone->el_count == src->el_count);
83 clone->_lineno = src->_lineno;
vlmfa67ddc2004-06-03 03:38:44 +000084 }
85
86 return clone;
87}
88
89int
90asn1p_constraint_insert(asn1p_constraint_t *into, asn1p_constraint_t *what) {
91 assert(into);
92 assert(what);
93
94 /*
95 * Make sure there's enough space to add an element.
96 */
97 if(into->el_count == into->el_size) {
vlm36a76d62004-09-05 10:40:28 +000098 unsigned int newsize = into->el_size?into->el_size<<2:4;
vlmfa67ddc2004-06-03 03:38:44 +000099 void *p;
100 p = realloc(into->elements,
101 newsize * sizeof(into->elements[0]));
102 if(p) {
103 into->elements = p;
104 into->el_size = newsize;
105 } else {
106 return -1;
107 }
108 }
109
110 into->elements[into->el_count++] = what;
111
112 return 0;
113}
vlm9283dbe2004-08-18 04:59:12 +0000114
115
116char *
117asn1p_constraint_type2str(enum asn1p_constraint_type_e type) {
118 switch(type) {
119 case ACT_INVALID:
120 return "INVALID";
vlma6a12e32005-03-20 12:58:00 +0000121 case ACT_EL_TYPE:
122 return "ContainedSubtype";
vlm9283dbe2004-08-18 04:59:12 +0000123 case ACT_EL_VALUE:
124 return "SingleValue";
125 case ACT_EL_RANGE:
126 case ACT_EL_LLRANGE:
127 case ACT_EL_RLRANGE:
128 case ACT_EL_ULRANGE:
129 return "ValueRange";
130 case ACT_EL_EXT:
131 return "...";
132 case ACT_CT_SIZE:
133 return "SizeConstraint";
134 case ACT_CT_FROM:
135 return "PermittedAlphabet";
136 case ACT_CT_WCOMP:
137 return "SingleTypeConstraint";
138 case ACT_CT_WCOMPS:
139 return "MultipleTypeConstraints";
140 case ACT_CA_SET:
141 return "SET";
142 case ACT_CA_CRC:
143 return "ComponentRelationConstraint";
144 case ACT_CA_CSV:
145 return "CSV";
146 case ACT_CA_UNI:
147 return "UNION";
148 case ACT_CA_INT:
149 return "INTERSECTION";
150 case ACT_CA_EXC:
151 return "EXCEPT";
152 }
153 return "UNKNOWN";
154}