blob: 666e0a2bcf81dd5d5f1cac326b5dc35f27d6b654 [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
Lev Walkinea6635b2017-08-06 23:23:04 -070065int
66asn1p_ioc_row_match(const asn1p_ioc_row_t *a, const asn1p_ioc_row_t *b) {
67 assert(a && b);
68
69 if(a->columns != b->columns)
70 return -1; /* Bad! */
71
72 for(size_t i = 0; i < a->columns; i++) {
73 assert(a->column[i].field);
74 assert(b->column[i].field);
75 if(strcmp(a->column[i].field->Identifier,
76 b->column[i].field->Identifier)
77 != 0) {
78 return -1; /* Bad! */
79 }
80 if((a->column[i].value && !b->column[i].value)
81 || (!a->column[i].value && b->column[i].value)) {
82 return 1; /* Not match */
83 }
84 if(a->column[i].value && b->column[i].value) {
85 if(asn1p_expr_compare(a->column[i].value, b->column[i].value)
86 != 0) {
87 return 1; /* Not match */
88 }
89 }
90 }
91
92 return 0;
93}
94
Lev Walkind370e9f2006-03-16 10:03:35 +000095struct asn1p_ioc_cell_s *
96asn1p_ioc_row_cell_fetch(asn1p_ioc_row_t *row, const char *fieldname) {
97 int i;
98 for(i = 0; i < row->columns; i++) {
99 if(strcmp(row->column[i].field->Identifier, fieldname) == 0)
100 return &row->column[i];
101 }
102 errno = ESRCH;
103 return NULL;
104}
105
Lev Walkinf15320b2004-06-03 03:38:44 +0000106asn1p_wsyntx_chunk_t *
107asn1p_wsyntx_chunk_new() {
108 asn1p_wsyntx_chunk_t *wc;
109
110 wc = calloc(1, sizeof(*wc));
111
112 return wc;
113}
114
115void
116asn1p_wsyntx_chunk_free(asn1p_wsyntx_chunk_t *wc) {
117 if(wc) {
Lev Walkin9d542d22006-03-14 16:31:37 +0000118 switch(wc->type) {
Lev Walkin57074f12006-03-16 05:11:14 +0000119 case WC_LITERAL:
120 case WC_WHITESPACE:
Lev Walkind370e9f2006-03-16 10:03:35 +0000121 case WC_FIELD:
Lev Walkin57074f12006-03-16 05:11:14 +0000122 free(wc->content.token); break;
Lev Walkin9d542d22006-03-14 16:31:37 +0000123 case WC_OPTIONALGROUP:
124 asn1p_wsyntx_free(wc->content.syntax);
125 break;
126 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000127 free(wc);
128 }
129}
130
131asn1p_wsyntx_chunk_t *
132asn1p_wsyntx_chunk_clone(asn1p_wsyntx_chunk_t *wc) {
133 asn1p_wsyntx_chunk_t *nc;
134
135 nc = asn1p_wsyntx_chunk_new();
136 if(nc) {
Lev Walkin57074f12006-03-16 05:11:14 +0000137 nc->type = wc->type;
Lev Walkin9d542d22006-03-14 16:31:37 +0000138 switch(wc->type) {
139 case WC_LITERAL:
Lev Walkin57074f12006-03-16 05:11:14 +0000140 case WC_WHITESPACE:
Lev Walkind370e9f2006-03-16 10:03:35 +0000141 case WC_FIELD:
Lev Walkin9d542d22006-03-14 16:31:37 +0000142 nc->content.token = malloc(strlen(wc->content.token)+1);
143 strcpy(nc->content.token, wc->content.token);
144 break;
Lev Walkin9d542d22006-03-14 16:31:37 +0000145 case WC_OPTIONALGROUP:
146 nc->content.syntax = asn1p_wsyntx_clone(wc->content.syntax);
147 break;
Lev Walkinf15320b2004-06-03 03:38:44 +0000148 }
149 }
150
151 return nc;
152}
153
154asn1p_wsyntx_t *
155asn1p_wsyntx_new() {
156 asn1p_wsyntx_t *wx;
157
158 wx = calloc(1, sizeof(*wx));
159 if(wx) {
160 TQ_INIT(&(wx->chunks));
161 }
162
163 return wx;
164}
165
166void
167asn1p_wsyntx_free(asn1p_wsyntx_t *wx) {
168 if(wx) {
169 asn1p_wsyntx_chunk_t *wc;
170 while((wc = TQ_REMOVE(&(wx->chunks), next)))
171 asn1p_wsyntx_chunk_free(wc);
172 free(wx);
173 }
174}
175
176asn1p_wsyntx_t *
177asn1p_wsyntx_clone(asn1p_wsyntx_t *wx) {
178 asn1p_wsyntx_t *nw;
179
180 nw = asn1p_wsyntx_new();
181 if(nw) {
182 asn1p_wsyntx_chunk_t *wc;
183 asn1p_wsyntx_chunk_t *nc;
184 TQ_FOR(wc, &(wx->chunks), next) {
185 nc = asn1p_wsyntx_chunk_clone(wc);
186 if(nc) {
187 TQ_ADD(&(nw->chunks), nc, next);
188 } else {
189 asn1p_wsyntx_free(nw);
190 return NULL;
191 }
192 }
193 }
194
195 return nw;
196}
197
198asn1p_wsyntx_chunk_t *
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000199asn1p_wsyntx_chunk_fromstring(char *token, int do_copy) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000200 asn1p_wsyntx_chunk_t *wc;
201
202 if(do_copy) {
203 static asn1p_wsyntx_chunk_t tmp;
Lev Walkin9d542d22006-03-14 16:31:37 +0000204 tmp.type = WC_LITERAL;
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000205 tmp.content.token = token;
Lev Walkinf15320b2004-06-03 03:38:44 +0000206 wc = asn1p_wsyntx_chunk_clone(&tmp);
207 } else {
208 wc = asn1p_wsyntx_chunk_new();
209 if(wc) {
Lev Walkin9d542d22006-03-14 16:31:37 +0000210 wc->type = WC_LITERAL;
Lev Walkinc46b7cb2006-08-18 02:27:55 +0000211 wc->content.token = token;
Lev Walkinf15320b2004-06-03 03:38:44 +0000212 }
213 }
214
215 return wc;
216}
217
Lev Walkin9d542d22006-03-14 16:31:37 +0000218
219asn1p_wsyntx_chunk_t *
220asn1p_wsyntx_chunk_fromsyntax(asn1p_wsyntx_t *syntax) {
221 asn1p_wsyntx_chunk_t *wc;
222
223 wc = asn1p_wsyntx_chunk_new();
224 if(wc) {
225 wc->type = WC_OPTIONALGROUP;
226 wc->content.syntax = syntax;
Bi-Ruei, Chiu80fd3062017-05-07 21:00:51 +0800227 syntax->parent = wc;
Lev Walkin9d542d22006-03-14 16:31:37 +0000228 }
229
230 return wc;
231}
232