blob: 4603d1888bef9b64cc7d75dbf13f23f80167b8d9 [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
Lev Walkind370e9f2006-03-16 10:03:35 +00009asn1p_ioc_row_t *
10asn1p_ioc_row_new(asn1p_expr_t *oclass) {
11 asn1p_ioc_row_t *row;
12 asn1p_expr_t *field;
13 int columns = 0;
14
15 assert(oclass->expr_type == A1TC_CLASSDEF);
16
17 row = calloc(1, sizeof *row);
18 if(!row) return NULL;
19
20 TQ_FOR(field, &oclass->members, next)
21 columns++;
22
23 row->column = calloc(columns, sizeof *row->column);
24 if(!row->column) {
25 free(row);
26 return NULL;
27 }
28 row->columns = columns;
29
30 columns = 0;
31 TQ_FOR(field, &oclass->members, next) {
32 int fieldIdLen = strlen(field->Identifier);
33 if(fieldIdLen > row->max_identifier_length)
34 row->max_identifier_length = fieldIdLen;
35 row->column[columns].field = field;
36 row->column[columns].value = NULL;
37 columns++;
38 }
39
40 return row;
41}
42
43void
44asn1p_ioc_row_delete(asn1p_ioc_row_t *row) {
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +080045 int i;
Lev Walkind370e9f2006-03-16 10:03:35 +000046 if(row) {
47 if(row->column) {
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +080048 for(i = 0; i < row->columns; i++) {
49 if(!row->column[i].new_ref && row->column[i].value) {
50 /*
51 * Field 'reference' comes from asn1fix_cws.c :
52 * TQ_FIRST(&cell->field->members)->reference
53 * so it should not be freed here.
54 */
55 row->column[i].value->reference = NULL;
56 }
57 asn1p_expr_free(row->column[i].value);
58 }
Lev Walkind370e9f2006-03-16 10:03:35 +000059 free(row->column);
60 }
61 free(row);
62 }
63}
64
65struct asn1p_ioc_cell_s *
66asn1p_ioc_row_cell_fetch(asn1p_ioc_row_t *row, const char *fieldname) {
67 int i;
68 for(i = 0; i < row->columns; i++) {
69 if(strcmp(row->column[i].field->Identifier, fieldname) == 0)
70 return &row->column[i];
71 }
72 errno = ESRCH;
73 return NULL;
74}
75
Lev Walkinf15320b2004-06-03 03:38:44 +000076asn1p_wsyntx_chunk_t *
77asn1p_wsyntx_chunk_new() {
78 asn1p_wsyntx_chunk_t *wc;
79
80 wc = calloc(1, sizeof(*wc));
81
82 return wc;
83}
84
85void
86asn1p_wsyntx_chunk_free(asn1p_wsyntx_chunk_t *wc) {
87 if(wc) {
Lev Walkin9d542d22006-03-14 16:31:37 +000088 switch(wc->type) {
Lev Walkin57074f12006-03-16 05:11:14 +000089 case WC_LITERAL:
90 case WC_WHITESPACE:
Lev Walkind370e9f2006-03-16 10:03:35 +000091 case WC_FIELD:
Lev Walkin57074f12006-03-16 05:11:14 +000092 free(wc->content.token); break;
Lev Walkin9d542d22006-03-14 16:31:37 +000093 case WC_OPTIONALGROUP:
94 asn1p_wsyntx_free(wc->content.syntax);
95 break;
96 }
Lev Walkinf15320b2004-06-03 03:38:44 +000097 free(wc);
98 }
99}
100
101asn1p_wsyntx_chunk_t *
102asn1p_wsyntx_chunk_clone(asn1p_wsyntx_chunk_t *wc) {
103 asn1p_wsyntx_chunk_t *nc;
104
105 nc = asn1p_wsyntx_chunk_new();
106 if(nc) {
Lev Walkin57074f12006-03-16 05:11:14 +0000107 nc->type = wc->type;
Lev Walkin9d542d22006-03-14 16:31:37 +0000108 switch(wc->type) {
109 case WC_LITERAL:
Lev Walkin57074f12006-03-16 05:11:14 +0000110 case WC_WHITESPACE:
Lev Walkind370e9f2006-03-16 10:03:35 +0000111 case WC_FIELD:
Lev Walkin9d542d22006-03-14 16:31:37 +0000112 nc->content.token = malloc(strlen(wc->content.token)+1);
113 strcpy(nc->content.token, wc->content.token);
114 break;
Lev Walkin9d542d22006-03-14 16:31:37 +0000115 case WC_OPTIONALGROUP:
116 nc->content.syntax = asn1p_wsyntx_clone(wc->content.syntax);
117 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000118 }
119 }
120
121 return nc;
122}
123
124asn1p_wsyntx_t *
125asn1p_wsyntx_new() {
126 asn1p_wsyntx_t *wx;
127
128 wx = calloc(1, sizeof(*wx));
129 if(wx) {
130 TQ_INIT(&(wx->chunks));
131 }
132
133 return wx;
134}
135
136void
137asn1p_wsyntx_free(asn1p_wsyntx_t *wx) {
138 if(wx) {
139 asn1p_wsyntx_chunk_t *wc;
140 while((wc = TQ_REMOVE(&(wx->chunks), next)))
141 asn1p_wsyntx_chunk_free(wc);
142 free(wx);
143 }
144}
145
146asn1p_wsyntx_t *
147asn1p_wsyntx_clone(asn1p_wsyntx_t *wx) {
148 asn1p_wsyntx_t *nw;
149
150 nw = asn1p_wsyntx_new();
151 if(nw) {
152 asn1p_wsyntx_chunk_t *wc;
153 asn1p_wsyntx_chunk_t *nc;
154 TQ_FOR(wc, &(wx->chunks), next) {
155 nc = asn1p_wsyntx_chunk_clone(wc);
156 if(nc) {
157 TQ_ADD(&(nw->chunks), nc, next);
158 } else {
159 asn1p_wsyntx_free(nw);
160 return NULL;
161 }
162 }
163 }
164
165 return nw;
166}
167
168asn1p_wsyntx_chunk_t *
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000169asn1p_wsyntx_chunk_fromstring(char *token, int do_copy) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000170 asn1p_wsyntx_chunk_t *wc;
171
172 if(do_copy) {
173 static asn1p_wsyntx_chunk_t tmp;
Lev Walkin9d542d22006-03-14 16:31:37 +0000174 tmp.type = WC_LITERAL;
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000175 tmp.content.token = token;
Lev Walkinf15320b2004-06-03 03:38:44 +0000176 wc = asn1p_wsyntx_chunk_clone(&tmp);
177 } else {
178 wc = asn1p_wsyntx_chunk_new();
179 if(wc) {
Lev Walkin9d542d22006-03-14 16:31:37 +0000180 wc->type = WC_LITERAL;
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000181 wc->content.token = token;
Lev Walkinf15320b2004-06-03 03:38:44 +0000182 }
183 }
184
185 return wc;
186}
187
Lev Walkin9d542d22006-03-14 16:31:37 +0000188
189asn1p_wsyntx_chunk_t *
190asn1p_wsyntx_chunk_fromsyntax(asn1p_wsyntx_t *syntax) {
191 asn1p_wsyntx_chunk_t *wc;
192
193 wc = asn1p_wsyntx_chunk_new();
194 if(wc) {
195 wc->type = WC_OPTIONALGROUP;
196 wc->content.syntax = syntax;
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800197 syntax->parent = wc;
Lev Walkin9d542d22006-03-14 16:31:37 +0000198 }
199
200 return wc;
201}
202