Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <errno.h> |
| 4 | #include <assert.h> |
| 5 | |
| 6 | #include "asn1parser.h" |
| 7 | |
| 8 | asn1p_constraint_t * |
| 9 | asn1p_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 | |
| 21 | void |
| 22 | asn1p_constraint_free(asn1p_constraint_t *ct) { |
| 23 | if(ct) { |
| 24 | |
Lev Walkin | ff7dd14 | 2005-03-20 12:58:00 +0000 | [diff] [blame] | 25 | if(ct->containedSubtype) |
| 26 | asn1p_value_free(ct->containedSubtype); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 27 | 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 | |
| 46 | asn1p_constraint_t * |
| 47 | asn1p_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) { |
Lev Walkin | e972d93 | 2004-09-05 10:40:28 +0000 | [diff] [blame] | 60 | unsigned int i; |
Lev Walkin | 26b6e04 | 2004-08-13 12:35:29 +0000 | [diff] [blame] | 61 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 62 | clone->type = src->type; |
Lev Walkin | 26b6e04 | 2004-08-13 12:35:29 +0000 | [diff] [blame] | 63 | clone->presence = src->presence; |
Lev Walkin | ff7dd14 | 2005-03-20 12:58:00 +0000 | [diff] [blame] | 64 | 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); |
Lev Walkin | 26b6e04 | 2004-08-13 12:35:29 +0000 | [diff] [blame] | 68 | |
| 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; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | return clone; |
| 87 | } |
| 88 | |
| 89 | int |
| 90 | asn1p_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) { |
Lev Walkin | e972d93 | 2004-09-05 10:40:28 +0000 | [diff] [blame] | 98 | unsigned int newsize = into->el_size?into->el_size<<2:4; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 99 | 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 | } |
Lev Walkin | f59d075 | 2004-08-18 04:59:12 +0000 | [diff] [blame] | 114 | |
| 115 | |
| 116 | char * |
| 117 | asn1p_constraint_type2str(enum asn1p_constraint_type_e type) { |
| 118 | switch(type) { |
| 119 | case ACT_INVALID: |
| 120 | return "INVALID"; |
Lev Walkin | ff7dd14 | 2005-03-20 12:58:00 +0000 | [diff] [blame] | 121 | case ACT_EL_TYPE: |
| 122 | return "ContainedSubtype"; |
Lev Walkin | f59d075 | 2004-08-18 04:59:12 +0000 | [diff] [blame] | 123 | 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"; |
Lev Walkin | 1893ddf | 2005-03-20 14:28:32 +0000 | [diff] [blame] | 140 | case ACT_CT_CTDBY: |
| 141 | return "UserDefinedConstraint"; |
Lev Walkin | f59d075 | 2004-08-18 04:59:12 +0000 | [diff] [blame] | 142 | case ACT_CA_SET: |
| 143 | return "SET"; |
| 144 | case ACT_CA_CRC: |
| 145 | return "ComponentRelationConstraint"; |
| 146 | case ACT_CA_CSV: |
| 147 | return "CSV"; |
| 148 | case ACT_CA_UNI: |
| 149 | return "UNION"; |
| 150 | case ACT_CA_INT: |
| 151 | return "INTERSECTION"; |
| 152 | case ACT_CA_EXC: |
| 153 | return "EXCEPT"; |
Lev Walkin | e4861a6 | 2005-03-24 14:29:35 +0000 | [diff] [blame] | 154 | case ACT_CA_AEX: |
| 155 | return "ALL EXCEPT"; |
Lev Walkin | f59d075 | 2004-08-18 04:59:12 +0000 | [diff] [blame] | 156 | } |
| 157 | return "UNKNOWN"; |
| 158 | } |