blob: 741d98e178f7791dcc8d1beb0677af31cc6bb6db [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <errno.h>
5#include <assert.h>
6
7#include "asn1parser.h"
8
9asn1p_wsyntx_chunk_t *
10asn1p_wsyntx_chunk_new() {
11 asn1p_wsyntx_chunk_t *wc;
12
13 wc = calloc(1, sizeof(*wc));
14
15 return wc;
16}
17
18void
19asn1p_wsyntx_chunk_free(asn1p_wsyntx_chunk_t *wc) {
20 if(wc) {
Lev Walkin9d542d22006-03-14 16:31:37 +000021 switch(wc->type) {
22 case WC_LITERAL: free(wc->content.token); break;
23 case WC_REFERENCE: asn1p_ref_free(wc->content.ref); break;
24 case WC_OPTIONALGROUP:
25 asn1p_wsyntx_free(wc->content.syntax);
26 break;
27 }
Lev Walkinf15320b2004-06-03 03:38:44 +000028 free(wc);
29 }
30}
31
32asn1p_wsyntx_chunk_t *
33asn1p_wsyntx_chunk_clone(asn1p_wsyntx_chunk_t *wc) {
34 asn1p_wsyntx_chunk_t *nc;
35
36 nc = asn1p_wsyntx_chunk_new();
37 if(nc) {
Lev Walkin9d542d22006-03-14 16:31:37 +000038 switch(wc->type) {
39 case WC_LITERAL:
40 nc->content.token = malloc(strlen(wc->content.token)+1);
41 strcpy(nc->content.token, wc->content.token);
42 break;
43 case WC_REFERENCE:
44 nc->content.ref = asn1p_ref_clone(wc->content.ref);
45 break;
46 case WC_OPTIONALGROUP:
47 nc->content.syntax = asn1p_wsyntx_clone(wc->content.syntax);
48 break;
Lev Walkinf15320b2004-06-03 03:38:44 +000049 }
50 }
51
52 return nc;
53}
54
55asn1p_wsyntx_t *
56asn1p_wsyntx_new() {
57 asn1p_wsyntx_t *wx;
58
59 wx = calloc(1, sizeof(*wx));
60 if(wx) {
61 TQ_INIT(&(wx->chunks));
62 }
63
64 return wx;
65}
66
67void
68asn1p_wsyntx_free(asn1p_wsyntx_t *wx) {
69 if(wx) {
70 asn1p_wsyntx_chunk_t *wc;
71 while((wc = TQ_REMOVE(&(wx->chunks), next)))
72 asn1p_wsyntx_chunk_free(wc);
73 free(wx);
74 }
75}
76
77asn1p_wsyntx_t *
78asn1p_wsyntx_clone(asn1p_wsyntx_t *wx) {
79 asn1p_wsyntx_t *nw;
80
81 nw = asn1p_wsyntx_new();
82 if(nw) {
83 asn1p_wsyntx_chunk_t *wc;
84 asn1p_wsyntx_chunk_t *nc;
85 TQ_FOR(wc, &(wx->chunks), next) {
86 nc = asn1p_wsyntx_chunk_clone(wc);
87 if(nc) {
88 TQ_ADD(&(nw->chunks), nc, next);
89 } else {
90 asn1p_wsyntx_free(nw);
91 return NULL;
92 }
93 }
94 }
95
96 return nw;
97}
98
99asn1p_wsyntx_chunk_t *
100asn1p_wsyntx_chunk_fromref(asn1p_ref_t *ref, int do_copy) {
101 asn1p_wsyntx_chunk_t *wc;
102
103 if(do_copy) {
104 static asn1p_wsyntx_chunk_t tmp;
Lev Walkin9d542d22006-03-14 16:31:37 +0000105 tmp.type = WC_REFERENCE;
106 tmp.content.ref = ref;
Lev Walkinf15320b2004-06-03 03:38:44 +0000107 wc = asn1p_wsyntx_chunk_clone(&tmp);
108 } else {
109 wc = asn1p_wsyntx_chunk_new();
Lev Walkin9d542d22006-03-14 16:31:37 +0000110 if(wc) {
111 wc->type = WC_REFERENCE;
112 wc->content.ref = ref;
113 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000114 }
115
116 return wc;
117}
118
119asn1p_wsyntx_chunk_t *
120asn1p_wsyntx_chunk_frombuf(char *buf, int len, int do_copy) {
121 asn1p_wsyntx_chunk_t *wc;
122
123 if(do_copy) {
124 static asn1p_wsyntx_chunk_t tmp;
Lev Walkin9d542d22006-03-14 16:31:37 +0000125 tmp.type = WC_LITERAL;
126 tmp.content.token = buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000127 wc = asn1p_wsyntx_chunk_clone(&tmp);
128 } else {
129 wc = asn1p_wsyntx_chunk_new();
130 if(wc) {
Lev Walkin9d542d22006-03-14 16:31:37 +0000131 wc->type = WC_LITERAL;
132 wc->content.token = buf;
Lev Walkinf15320b2004-06-03 03:38:44 +0000133 }
134 }
135
136 return wc;
137}
138
Lev Walkin9d542d22006-03-14 16:31:37 +0000139
140asn1p_wsyntx_chunk_t *
141asn1p_wsyntx_chunk_fromsyntax(asn1p_wsyntx_t *syntax) {
142 asn1p_wsyntx_chunk_t *wc;
143
144 wc = asn1p_wsyntx_chunk_new();
145 if(wc) {
146 wc->type = WC_OPTIONALGROUP;
147 wc->content.syntax = syntax;
148 }
149
150 return wc;
151}
152