Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <string.h> |
| 3 | #include <errno.h> |
| 4 | #include <assert.h> |
| 5 | |
| 6 | #include "asn1parser.h" |
| 7 | |
| 8 | |
| 9 | asn1p_value_t * |
| 10 | asn1p_value_fromref(asn1p_ref_t *ref, int do_copy) { |
| 11 | if(ref) { |
| 12 | asn1p_value_t *v = calloc(1, sizeof *v); |
| 13 | if(v) { |
| 14 | if(do_copy) { |
| 15 | v->value.reference = asn1p_ref_clone(ref); |
| 16 | if(v->value.reference == NULL) { |
| 17 | free(v); |
| 18 | return NULL; |
| 19 | } |
| 20 | } else { |
| 21 | v->value.reference = ref; |
| 22 | } |
| 23 | v->type = ATV_REFERENCED; |
| 24 | } |
| 25 | return v; |
| 26 | } else { |
| 27 | errno = EINVAL; |
| 28 | return NULL; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | asn1p_value_t * |
| 33 | asn1p_value_frombits(uint8_t *bits, int size_in_bits, int do_copy) { |
| 34 | if(bits) { |
| 35 | asn1p_value_t *v = calloc(1, sizeof *v); |
| 36 | assert(size_in_bits >= 0); |
| 37 | if(v) { |
| 38 | if(do_copy) { |
| 39 | int size = ((size_in_bits + 7) >> 3); |
| 40 | void *p; |
| 41 | p = malloc(size + 1); |
| 42 | if(p) { |
| 43 | memcpy(p, bits, size); |
| 44 | ((char *)p)[size] = '\0'; /* JIC */ |
| 45 | } else { |
| 46 | free(v); |
| 47 | return NULL; |
| 48 | } |
| 49 | v->value.binary_vector.bits = p; |
| 50 | } else { |
| 51 | v->value.binary_vector.bits = (void *)bits; |
| 52 | } |
| 53 | v->value.binary_vector.size_in_bits = size_in_bits; |
| 54 | v->type = ATV_BITVECTOR; |
| 55 | } |
| 56 | return v; |
| 57 | } else { |
| 58 | errno = EINVAL; |
| 59 | return NULL; |
| 60 | } |
| 61 | } |
| 62 | asn1p_value_t * |
| 63 | asn1p_value_frombuf(char *buffer, int size, int do_copy) { |
| 64 | if(buffer) { |
| 65 | asn1p_value_t *v = calloc(1, sizeof *v); |
| 66 | assert(size >= 0); |
| 67 | if(v) { |
| 68 | if(do_copy) { |
| 69 | void *p = malloc(size + 1); |
| 70 | if(p) { |
| 71 | memcpy(p, buffer, size); |
| 72 | ((char *)p)[size] = '\0'; /* JIC */ |
| 73 | } else { |
| 74 | free(v); |
| 75 | return NULL; |
| 76 | } |
| 77 | v->value.string.buf = p; |
| 78 | } else { |
| 79 | v->value.string.buf = buffer; |
| 80 | } |
| 81 | v->value.string.size = size; |
| 82 | v->type = ATV_STRING; |
| 83 | } |
| 84 | return v; |
| 85 | } else { |
| 86 | errno = EINVAL; |
| 87 | return NULL; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | asn1p_value_t * |
| 92 | asn1p_value_fromdouble(double d) { |
| 93 | asn1p_value_t *v = calloc(1, sizeof *v); |
| 94 | if(v) { |
| 95 | v->value.v_double = d; |
| 96 | v->type = ATV_REAL; |
| 97 | } |
| 98 | return v; |
| 99 | } |
| 100 | |
| 101 | asn1p_value_t * |
| 102 | asn1p_value_fromint(asn1_integer_t i) { |
| 103 | asn1p_value_t *v = calloc(1, sizeof *v); |
| 104 | if(v) { |
| 105 | v->value.v_integer = i; |
| 106 | v->type = ATV_INTEGER; |
| 107 | } |
| 108 | return v; |
| 109 | } |
| 110 | |
| 111 | asn1p_value_t * |
| 112 | asn1p_value_clone(asn1p_value_t *v) { |
| 113 | asn1p_value_t *clone; |
| 114 | if(v) { |
| 115 | switch(v->type) { |
| 116 | case ATV_NOVALUE: |
| 117 | return calloc(1, sizeof(*v)); |
| 118 | case ATV_REFERENCED: |
| 119 | return asn1p_value_fromref(v->value.reference, 1); |
| 120 | case ATV_REAL: |
| 121 | return asn1p_value_fromdouble(v->value.v_double); |
| 122 | case ATV_INTEGER: |
| 123 | case ATV_MIN: |
| 124 | case ATV_MAX: |
| 125 | case ATV_FALSE: |
| 126 | case ATV_TRUE: |
| 127 | clone = asn1p_value_fromint(v->value.v_integer); |
| 128 | if(clone) clone->type = v->type; |
| 129 | return clone; |
| 130 | case ATV_STRING: |
| 131 | clone = asn1p_value_frombuf(v->value.string.buf, |
| 132 | v->value.string.size, 1); |
| 133 | if(clone) clone->type = v->type; |
| 134 | return clone; |
| 135 | case ATV_UNPARSED: |
| 136 | clone = asn1p_value_frombuf(v->value.string.buf, |
| 137 | v->value.string.size, 1); |
| 138 | if(clone) clone->type = ATV_UNPARSED; |
| 139 | return clone; |
| 140 | case ATV_BITVECTOR: |
| 141 | return asn1p_value_frombuf(v->value.binary_vector.bits, |
| 142 | v->value.binary_vector.size_in_bits, 1); |
| 143 | } |
| 144 | } |
| 145 | return v; |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | asn1p_value_free(asn1p_value_t *v) { |
| 150 | if(v) { |
| 151 | switch(v->type) { |
| 152 | case ATV_NOVALUE: |
| 153 | break; |
| 154 | case ATV_REFERENCED: |
| 155 | asn1p_ref_free(v->value.reference); |
| 156 | break; |
| 157 | case ATV_INTEGER: |
| 158 | case ATV_MIN: |
| 159 | case ATV_MAX: |
| 160 | case ATV_FALSE: |
| 161 | case ATV_TRUE: |
| 162 | case ATV_REAL: |
| 163 | /* No freeing necessary */ |
| 164 | break; |
| 165 | case ATV_STRING: |
| 166 | case ATV_UNPARSED: |
| 167 | assert(v->value.string.buf); |
| 168 | free(v->value.string.buf); |
| 169 | break; |
| 170 | case ATV_BITVECTOR: |
| 171 | assert(v->value.binary_vector.bits); |
| 172 | free(v->value.binary_vector.bits); |
| 173 | break; |
| 174 | } |
| 175 | free(v); |
| 176 | } |
| 177 | } |
| 178 | |