vlm | fa67ddc | 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 | |
| 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 | |
| 44 | asn1p_constraint_t * |
| 45 | asn1p_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) { |
vlm | 36a76d6 | 2004-09-05 10:40:28 +0000 | [diff] [blame] | 58 | unsigned int i; |
vlm | 3b4ff46 | 2004-08-13 12:35:29 +0000 | [diff] [blame] | 59 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 60 | clone->type = src->type; |
vlm | 3b4ff46 | 2004-08-13 12:35:29 +0000 | [diff] [blame] | 61 | clone->presence = src->presence; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 62 | CLONE(value, asn1p_value_clone); |
| 63 | CLONE(range_start, asn1p_value_clone); |
| 64 | CLONE(range_stop, asn1p_value_clone); |
vlm | 3b4ff46 | 2004-08-13 12:35:29 +0000 | [diff] [blame] | 65 | |
| 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; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | return clone; |
| 84 | } |
| 85 | |
| 86 | int |
| 87 | asn1p_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) { |
vlm | 36a76d6 | 2004-09-05 10:40:28 +0000 | [diff] [blame] | 95 | unsigned int newsize = into->el_size?into->el_size<<2:4; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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 | } |
vlm | 9283dbe | 2004-08-18 04:59:12 +0000 | [diff] [blame] | 111 | |
| 112 | |
| 113 | char * |
| 114 | asn1p_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 | } |