blob: faec6f2e7dde6d649ef0edc6fde7588ca8b2e25f [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 Walkin26b6e042004-08-13 12:35:29 +000058 int i;
59
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) {
95 int newsize = into->el_size?into->el_size<<2:4;
96 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}