blob: 6e198892165dbffb931a596dc40338f65b32c939 [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) {
58 clone->type = src->type;
59 CLONE(value, asn1p_value_clone);
60 CLONE(range_start, asn1p_value_clone);
61 CLONE(range_stop, asn1p_value_clone);
62 }
63
64 return clone;
65}
66
67int
68asn1p_constraint_insert(asn1p_constraint_t *into, asn1p_constraint_t *what) {
69 assert(into);
70 assert(what);
71
72 /*
73 * Make sure there's enough space to add an element.
74 */
75 if(into->el_count == into->el_size) {
76 int newsize = into->el_size?into->el_size<<2:4;
77 void *p;
78 p = realloc(into->elements,
79 newsize * sizeof(into->elements[0]));
80 if(p) {
81 into->elements = p;
82 into->el_size = newsize;
83 } else {
84 return -1;
85 }
86 }
87
88 into->elements[into->el_count++] = what;
89
90 return 0;
91}