1 #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 
9 asn1p_ioc_row_t *
asn1p_ioc_row_new(asn1p_expr_t * oclass)10 asn1p_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 
43 void
asn1p_ioc_row_delete(asn1p_ioc_row_t * row)44 asn1p_ioc_row_delete(asn1p_ioc_row_t *row) {
45 	if(row) {
46 		if(row->column) {
47 			free(row->column);
48 		}
49 		free(row);
50 	}
51 }
52 
53 struct asn1p_ioc_cell_s *
asn1p_ioc_row_cell_fetch(asn1p_ioc_row_t * row,const char * fieldname)54 asn1p_ioc_row_cell_fetch(asn1p_ioc_row_t *row, const char *fieldname) {
55 	int i;
56 	for(i = 0; i < row->columns; i++) {
57 		if(strcmp(row->column[i].field->Identifier, fieldname) == 0)
58 			return &row->column[i];
59 	}
60 	errno = ESRCH;
61 	return NULL;
62 }
63 
64 asn1p_wsyntx_chunk_t *
asn1p_wsyntx_chunk_new()65 asn1p_wsyntx_chunk_new() {
66 	asn1p_wsyntx_chunk_t *wc;
67 
68 	wc = calloc(1, sizeof(*wc));
69 
70 	return wc;
71 }
72 
73 void
asn1p_wsyntx_chunk_free(asn1p_wsyntx_chunk_t * wc)74 asn1p_wsyntx_chunk_free(asn1p_wsyntx_chunk_t *wc) {
75 	if(wc) {
76 		switch(wc->type) {
77 		case WC_LITERAL:
78 		case WC_WHITESPACE:
79 		case WC_FIELD:
80 			free(wc->content.token); break;
81 		case WC_OPTIONALGROUP:
82 			asn1p_wsyntx_free(wc->content.syntax);
83 			break;
84 		}
85 		free(wc);
86 	}
87 }
88 
89 asn1p_wsyntx_chunk_t *
asn1p_wsyntx_chunk_clone(asn1p_wsyntx_chunk_t * wc)90 asn1p_wsyntx_chunk_clone(asn1p_wsyntx_chunk_t *wc) {
91 	asn1p_wsyntx_chunk_t *nc;
92 
93 	nc = asn1p_wsyntx_chunk_new();
94 	if(nc) {
95 		nc->type = wc->type;
96 		switch(wc->type) {
97 		case WC_LITERAL:
98 		case WC_WHITESPACE:
99 		case WC_FIELD:
100 			nc->content.token = malloc(strlen(wc->content.token)+1);
101 			strcpy(nc->content.token, wc->content.token);
102 			break;
103 		case WC_OPTIONALGROUP:
104 			nc->content.syntax = asn1p_wsyntx_clone(wc->content.syntax);
105 			break;
106 		}
107 	}
108 
109 	return nc;
110 }
111 
112 asn1p_wsyntx_t *
asn1p_wsyntx_new()113 asn1p_wsyntx_new() {
114 	asn1p_wsyntx_t *wx;
115 
116 	wx = calloc(1, sizeof(*wx));
117 	if(wx) {
118 		TQ_INIT(&(wx->chunks));
119 	}
120 
121 	return wx;
122 }
123 
124 void
asn1p_wsyntx_free(asn1p_wsyntx_t * wx)125 asn1p_wsyntx_free(asn1p_wsyntx_t *wx) {
126 	if(wx) {
127 		asn1p_wsyntx_chunk_t *wc;
128 		while((wc = TQ_REMOVE(&(wx->chunks), next)))
129 			asn1p_wsyntx_chunk_free(wc);
130 		free(wx);
131 	}
132 }
133 
134 asn1p_wsyntx_t *
asn1p_wsyntx_clone(asn1p_wsyntx_t * wx)135 asn1p_wsyntx_clone(asn1p_wsyntx_t *wx) {
136 	asn1p_wsyntx_t *nw;
137 
138 	nw = asn1p_wsyntx_new();
139 	if(nw) {
140 		asn1p_wsyntx_chunk_t *wc;
141 		asn1p_wsyntx_chunk_t *nc;
142 		TQ_FOR(wc, &(wx->chunks), next) {
143 			nc = asn1p_wsyntx_chunk_clone(wc);
144 			if(nc) {
145 				TQ_ADD(&(nw->chunks), nc, next);
146 			} else {
147 				asn1p_wsyntx_free(nw);
148 				return NULL;
149 			}
150 		}
151 	}
152 
153 	return nw;
154 }
155 
156 asn1p_wsyntx_chunk_t *
asn1p_wsyntx_chunk_fromstring(char * token,int do_copy)157 asn1p_wsyntx_chunk_fromstring(char *token, int do_copy) {
158 	asn1p_wsyntx_chunk_t *wc;
159 
160 	if(do_copy) {
161 		static asn1p_wsyntx_chunk_t tmp;
162 		tmp.type = WC_LITERAL;
163 		tmp.content.token = token;
164 		wc = asn1p_wsyntx_chunk_clone(&tmp);
165 	} else {
166 		wc = asn1p_wsyntx_chunk_new();
167 		if(wc) {
168 			wc->type = WC_LITERAL;
169 			wc->content.token = token;
170 		}
171 	}
172 
173 	return wc;
174 }
175 
176 
177 asn1p_wsyntx_chunk_t *
asn1p_wsyntx_chunk_fromsyntax(asn1p_wsyntx_t * syntax)178 asn1p_wsyntx_chunk_fromsyntax(asn1p_wsyntx_t *syntax) {
179 	asn1p_wsyntx_chunk_t *wc;
180 
181 	wc = asn1p_wsyntx_chunk_new();
182 	if(wc) {
183 		wc->type = WC_OPTIONALGROUP;
184 		wc->content.syntax = syntax;
185 	}
186 
187 	return wc;
188 }
189 
190