7 #include "asn1parser.h"
8 #include "asn1p_class.h"
11 asn1p_ioc_table_new() {
12 asn1p_ioc_table_t *it = calloc(1, sizeof(*it));
18 asn1p_ioc_table_add(asn1p_ioc_table_t *it, asn1p_ioc_row_t *row) {
21 asn1p_ioc_row_t **new_rows =
22 realloc(it->row, (it->rows + 1) * sizeof(it->row[0]));
25 it->row[it->rows++] = row;
29 asn1p_ioc_table_append(asn1p_ioc_table_t *it, asn1p_ioc_table_t *src) {
31 if(!src || !it) return;
33 for(size_t i = 0; i < src->rows; i++) {
34 asn1p_ioc_table_add(it, asn1p_ioc_row_clone(src->row[i]));
39 asn1p_ioc_table_free(asn1p_ioc_table_t *it) {
41 for(size_t i = 0; i < it->rows; i++) {
42 asn1p_ioc_row_delete(it->row[i]);
50 asn1p_ioc_table_max_identifier_length(asn1p_ioc_table_t *it) {
51 size_t max_length = 0;
53 for(size_t i = 0; i < it->rows; i++) {
54 size_t len = asn1p_ioc_row_max_identifier_length(it->row[i]);
55 if(len > max_length) max_length = len;
62 asn1p_ioc_row_max_identifier_length(asn1p_ioc_row_t *row) {
63 size_t max_length = 0;
65 for(size_t i = 0; i < row->columns; i++) {
66 if(row->column[i].value) {
67 size_t len = strlen(row->column[i].value->Identifier);
68 if(len > max_length) max_length = len;
76 asn1p_ioc_row_new(asn1p_expr_t *oclass) {
81 assert(oclass->expr_type == A1TC_CLASSDEF);
83 row = calloc(1, sizeof *row);
86 TQ_FOR(field, &oclass->members, next)
89 row->column = calloc(columns, sizeof *row->column);
94 row->columns = columns;
97 TQ_FOR(field, &oclass->members, next) {
98 row->column[columns].field = field;
99 row->column[columns].value = NULL;
107 asn1p_ioc_row_clone(asn1p_ioc_row_t *src) {
108 asn1p_ioc_row_t *row;
110 row = calloc(1, sizeof *row);
111 if(!row) return NULL;
113 row->column = calloc(src->columns, sizeof *src->column);
118 row->columns = src->columns;
120 for(size_t i = 0; i < src->columns; i++) {
121 row->column[i].field = src->column[i].field;
122 row->column[i].value = 0;
123 if(src->column[i].value) {
124 row->column[i].value = asn1p_expr_clone(src->column[i].value, 0);
126 row->column[i].new_ref = 1;
133 asn1p_ioc_row_delete(asn1p_ioc_row_t *row) {
136 for(size_t i = 0; i < row->columns; i++) {
137 if(!row->column[i].new_ref && row->column[i].value) {
139 * Field 'reference' comes from asn1fix_cws.c :
140 * TQ_FIRST(&cell->field->members)->reference
141 * so it should not be freed here.
143 row->column[i].value->reference = NULL;
145 asn1p_expr_free(row->column[i].value);
154 asn1p_ioc_row_match(const asn1p_ioc_row_t *a, const asn1p_ioc_row_t *b) {
157 if(a->columns != b->columns)
158 return -1; /* Bad! */
160 for(size_t i = 0; i < a->columns; i++) {
161 assert(a->column[i].field);
162 assert(b->column[i].field);
163 if(strcmp(a->column[i].field->Identifier,
164 b->column[i].field->Identifier)
166 return -1; /* Bad! */
168 if((a->column[i].value && !b->column[i].value)
169 || (!a->column[i].value && b->column[i].value)) {
170 return 1; /* Not match */
172 if(a->column[i].value && b->column[i].value) {
173 if(asn1p_expr_compare(a->column[i].value, b->column[i].value)
175 return 1; /* Not match */
183 struct asn1p_ioc_cell_s *
184 asn1p_ioc_row_cell_fetch(asn1p_ioc_row_t *row, const char *fieldname) {
185 for(size_t i = 0; i < row->columns; i++) {
186 if(strcmp(row->column[i].field->Identifier, fieldname) == 0)
187 return &row->column[i];
193 asn1p_wsyntx_chunk_t *
194 asn1p_wsyntx_chunk_new() {
195 asn1p_wsyntx_chunk_t *wc;
197 wc = calloc(1, sizeof(*wc));
203 asn1p_wsyntx_chunk_free(asn1p_wsyntx_chunk_t *wc) {
209 free(wc->content.token); break;
210 case WC_OPTIONALGROUP:
211 asn1p_wsyntx_free(wc->content.syntax);
218 asn1p_wsyntx_chunk_t *
219 asn1p_wsyntx_chunk_clone(asn1p_wsyntx_chunk_t *wc) {
220 asn1p_wsyntx_chunk_t *nc;
222 nc = asn1p_wsyntx_chunk_new();
229 nc->content.token = malloc(strlen(wc->content.token)+1);
230 strcpy(nc->content.token, wc->content.token);
232 case WC_OPTIONALGROUP:
233 nc->content.syntax = asn1p_wsyntx_clone(wc->content.syntax);
245 wx = calloc(1, sizeof(*wx));
247 TQ_INIT(&(wx->chunks));
254 asn1p_wsyntx_free(asn1p_wsyntx_t *wx) {
256 asn1p_wsyntx_chunk_t *wc;
257 while((wc = TQ_REMOVE(&(wx->chunks), next)))
258 asn1p_wsyntx_chunk_free(wc);
264 asn1p_wsyntx_clone(asn1p_wsyntx_t *wx) {
267 nw = asn1p_wsyntx_new();
269 asn1p_wsyntx_chunk_t *wc;
270 asn1p_wsyntx_chunk_t *nc;
271 TQ_FOR(wc, &(wx->chunks), next) {
272 nc = asn1p_wsyntx_chunk_clone(wc);
274 TQ_ADD(&(nw->chunks), nc, next);
276 asn1p_wsyntx_free(nw);
285 asn1p_wsyntx_chunk_t *
286 asn1p_wsyntx_chunk_fromstring(char *token, int do_copy) {
287 asn1p_wsyntx_chunk_t *wc;
290 static asn1p_wsyntx_chunk_t tmp;
291 tmp.type = WC_LITERAL;
292 tmp.content.token = token;
293 wc = asn1p_wsyntx_chunk_clone(&tmp);
295 wc = asn1p_wsyntx_chunk_new();
297 wc->type = WC_LITERAL;
298 wc->content.token = token;
306 asn1p_wsyntx_chunk_t *
307 asn1p_wsyntx_chunk_fromsyntax(asn1p_wsyntx_t *syntax) {
308 asn1p_wsyntx_chunk_t *wc;
310 wc = asn1p_wsyntx_chunk_new();
312 wc->type = WC_OPTIONALGROUP;
313 wc->content.syntax = syntax;