6 #include "asn1parser.h"
7 #include "asn1p_expr.h"
10 asn1p_value_set_source(asn1p_value_t *value, asn1p_module_t *module,
15 asn1p_expr_set_source(value->value.v_type, module, lineno);
18 asn1p_ref_set_source(value->value.reference, module, lineno);
21 asn1p_constraint_set_source(value->value.constraint, module,
31 asn1p_value_compare(const asn1p_value_t *a, const asn1p_value_t *b) {
32 if(a->type != b->type) {
45 return asn1p_expr_compare(a->value.v_type, b->value.v_type);
47 return (a->value.v_double == b->value.v_double) ? 0 : -1;
51 return (a->value.v_integer == b->value.v_integer) ? 0 : -1;
54 if(a->value.string.size != b->value.string.size
55 || memcmp(a->value.string.buf, b->value.string.buf,
62 if(a->value.binary_vector.size_in_bits
63 != b->value.binary_vector.size_in_bits
64 || memcmp(a->value.binary_vector.bits, b->value.binary_vector.bits,
65 (a->value.binary_vector.size_in_bits+7) >> 3)
71 return asn1p_constraint_compare(a->value.constraint,
74 return asn1p_ref_compare(a->value.reference, b->value.reference);
75 case ATV_CHOICE_IDENTIFIER:
76 if(strcmp(a->value.choice_identifier.identifier,
77 b->value.choice_identifier.identifier)
81 return asn1p_value_compare(a->value.choice_identifier.value,
82 b->value.choice_identifier.value);
89 asn1p_value_fromref(asn1p_ref_t *ref, int do_copy) {
91 asn1p_value_t *v = calloc(1, sizeof *v);
94 v->value.reference = asn1p_ref_clone(ref);
95 if(v->value.reference == NULL) {
100 v->value.reference = ref;
102 v->type = ATV_REFERENCED;
112 asn1p_value_fromconstr(asn1p_constraint_t *ct, int do_copy) {
114 asn1p_value_t *v = calloc(1, sizeof *v);
118 = asn1p_constraint_clone(ct);
119 if(v->value.constraint == NULL) {
124 v->value.constraint = ct;
126 v->type = ATV_VALUESET;
136 asn1p_value_frombits(uint8_t *bits, int size_in_bits, int do_copy) {
138 asn1p_value_t *v = calloc(1, sizeof *v);
139 assert(size_in_bits >= 0);
142 int size = ((size_in_bits + 7) >> 3);
144 p = malloc(size + 1);
146 memcpy(p, bits, size);
147 ((char *)p)[size] = '\0'; /* JIC */
152 v->value.binary_vector.bits = p;
154 v->value.binary_vector.bits = (void *)bits;
156 v->value.binary_vector.size_in_bits = size_in_bits;
157 v->type = ATV_BITVECTOR;
167 asn1p_value_frombuf(char *buffer, int size, int do_copy) {
169 asn1p_value_t *v = calloc(1, sizeof *v);
173 void *p = malloc(size + 1);
175 memcpy(p, buffer, size);
176 ((char *)p)[size] = '\0'; /* JIC */
181 v->value.string.buf = p;
183 v->value.string.buf = (uint8_t *)buffer;
185 v->value.string.size = size;
186 v->type = ATV_STRING;
196 asn1p_value_fromdouble(double d) {
197 asn1p_value_t *v = calloc(1, sizeof *v);
199 v->value.v_double = d;
206 asn1p_value_fromint(asn1c_integer_t i) {
207 asn1p_value_t *v = calloc(1, sizeof *v);
209 v->value.v_integer = i;
210 v->type = ATV_INTEGER;
216 asn1p_value_fromtype(asn1p_expr_t *expr) {
217 asn1p_value_t *v = calloc(1, sizeof *v);
219 v->value.v_type = expr;
227 asn1p_value_clone(asn1p_value_t *v) {
228 return asn1p_value_clone_with_resolver(v, 0, 0);
232 asn1p_value_clone_with_resolver(asn1p_value_t *v,
233 asn1p_value_t *(*resolver)(asn1p_value_t *, void *rarg),
235 asn1p_value_t *clone = NULL;
240 return calloc(1, sizeof(*clone));
242 return asn1p_value_fromdouble(v->value.v_double);
244 return asn1p_value_fromtype(v->value.v_type);
252 clone = asn1p_value_fromint(v->value.v_integer);
253 if(clone) clone->type = v->type;
256 clone = asn1p_value_frombuf((char *)v->value.string.buf,
257 v->value.string.size, 1);
258 if(clone) clone->type = v->type;
261 clone = asn1p_value_frombuf((char *)v->value.string.buf,
262 v->value.string.size, 1);
263 if(clone) clone->type = ATV_UNPARSED;
266 return asn1p_value_frombits(v->value.binary_vector.bits,
267 v->value.binary_vector.size_in_bits, 1);
270 clone = resolver(v, rarg);
271 if(clone) return clone;
272 else if(errno != ESRCH) return NULL;
274 return asn1p_value_fromref(v->value.reference, 1);
277 clone = resolver(v, rarg);
278 if(clone) return clone;
279 else if(errno != ESRCH) return NULL;
281 return asn1p_value_fromconstr(v->value.constraint, 1);
282 case ATV_CHOICE_IDENTIFIER: {
283 char *id = v->value.choice_identifier.identifier;
284 clone = calloc(1, sizeof(*clone));
285 if(!clone) return NULL;
286 clone->type = v->type;
288 if(!id) { asn1p_value_free(clone); return NULL; }
289 clone->value.choice_identifier.identifier = id;
290 v = asn1p_value_clone(v->value.choice_identifier.value);
291 if(!v) { asn1p_value_free(clone); return NULL; }
292 clone->value.choice_identifier.value = v;
297 assert(!"UNREACHABLE");
303 asn1p_value_free(asn1p_value_t *v) {
310 asn1p_expr_free(v->value.v_type);
320 /* No freeing necessary */
324 assert(v->value.string.buf);
325 free(v->value.string.buf);
328 assert(v->value.binary_vector.bits);
329 free(v->value.binary_vector.bits);
332 asn1p_ref_free(v->value.reference);
335 asn1p_constraint_free(v->value.constraint);
337 case ATV_CHOICE_IDENTIFIER:
338 free(v->value.choice_identifier.identifier);
339 asn1p_value_free(v->value.choice_identifier.value);
342 memset(v, 0, sizeof(*v));