blob: 4337981f9add2d78d53122134c35059ac539e732 [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <errno.h>
5#include <assert.h>
6
7#include "asn1parser.h"
8
9/*
10 * Construct a new empty types collection.
11 */
12asn1p_expr_t *
13asn1p_expr_new(int _lineno) {
14 asn1p_expr_t *expr;
15
16 expr = calloc(1, sizeof *expr);
17 if(expr) {
18 TQ_INIT(&(expr->members));
19 expr->_lineno = _lineno;
20 }
21
22 return expr;
23}
24
25asn1p_expr_t *
vlmec8f6812004-08-22 03:19:54 +000026asn1p_expr_clone(asn1p_expr_t *expr, int skip_extensions) {
vlmfa67ddc2004-06-03 03:38:44 +000027 asn1p_expr_t *clone;
28 asn1p_expr_t *tcmemb; /* Child of tc */
vlmec8f6812004-08-22 03:19:54 +000029 int hit_ext = 0;
vlmfa67ddc2004-06-03 03:38:44 +000030
31 clone = asn1p_expr_new(expr->_lineno);
32 if(clone == NULL) return NULL;
33
34#define CLCOPY(field) do { clone->field = expr->field; } while(0)
35#define CLCLONE(field, func) do { if(expr->field) { \
36 clone->field = func(expr->field); \
37 if(clone->field == NULL) { \
38 asn1p_expr_free(clone); \
39 return NULL; \
40 } \
41 } } while(0)
42
43 /*
44 * Copy simple fields.
45 */
46 CLCOPY(meta_type);
47 CLCOPY(expr_type);
48 CLCOPY(tag);
vlmec8f6812004-08-22 03:19:54 +000049 CLCOPY(marker); /* OPTIONAL/DEFAULT */
50 CLCOPY(module);
vlmfa67ddc2004-06-03 03:38:44 +000051 CLCOPY(_mark);
52
53 clone->data = 0; /* Do not clone this */
54 clone->data_free = 0; /* Do not clone this */
55
56 /*
57 * Clone complex fields.
58 */
59 CLCLONE(Identifier, strdup);
60 CLCLONE(reference, asn1p_ref_clone);
61 CLCLONE(constraints, asn1p_constraint_clone);
vlm9283dbe2004-08-18 04:59:12 +000062 CLCLONE(combined_constraints, asn1p_constraint_clone);
vlmfa67ddc2004-06-03 03:38:44 +000063 CLCLONE(params, asn1p_paramlist_clone);
64 CLCLONE(value, asn1p_value_clone);
65 CLCLONE(with_syntax, asn1p_wsyntx_clone);
66
67 /*
68 * Copy all the children of this expr.
69 */
70 TQ_FOR(tcmemb, &(expr->members), next) {
vlmec8f6812004-08-22 03:19:54 +000071 asn1p_expr_t *cmemb;
72
73 if(skip_extensions
74 && tcmemb->expr_type == A1TC_EXTENSIBLE) {
75 hit_ext++; /* Even if hit_ext wraps around, we're OK. */
76 continue;
77 }
78 if(hit_ext == 1) continue; /* Skip between ...'s */
79
80 cmemb = asn1p_expr_clone(tcmemb, skip_extensions);
vlmfa67ddc2004-06-03 03:38:44 +000081 if(cmemb == NULL) {
82 asn1p_expr_free(clone);
83 return NULL;
84 }
85 TQ_ADD(&(clone->members), cmemb, next);
86 }
87
88 return clone;
89}
90
91/*
92 * Destruct the types collection structure.
93 */
94void
95asn1p_expr_free(asn1p_expr_t *expr) {
96 if(expr) {
97 asn1p_expr_t *tm;
98
99 if(expr->Identifier)
100 free(expr->Identifier);
101 if(expr->reference)
102 asn1p_ref_free(expr->reference);
103 if(expr->constraints)
104 asn1p_constraint_free(expr->constraints);
vlm9283dbe2004-08-18 04:59:12 +0000105 if(expr->combined_constraints)
106 asn1p_constraint_free(expr->combined_constraints);
vlmfa67ddc2004-06-03 03:38:44 +0000107 if(expr->params)
108 asn1p_paramlist_free(expr->params);
109 if(expr->value)
110 asn1p_value_free(expr->value);
111 if(expr->with_syntax)
112 asn1p_wsyntx_free(expr->with_syntax);
113
114 /* Remove all children */
115 while((tm = TQ_REMOVE(&(expr->members), next))) {
116 asn1p_expr_free(tm);
117 }
118
119 if(expr->data && expr->data_free)
120 expr->data_free(expr->data);
121
122 memset(expr, 0, sizeof(*expr));
123 free(expr);
124 }
125}
126