Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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 | |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 9 | asn1p_ioc_row_t * |
| 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 |
| 44 | asn1p_ioc_row_delete(asn1p_ioc_row_t *row) { |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 45 | int i; |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 46 | if(row) { |
| 47 | if(row->column) { |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 48 | 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 Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 59 | free(row->column); |
| 60 | } |
| 61 | free(row); |
| 62 | } |
| 63 | } |
| 64 | |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 65 | int |
| 66 | asn1p_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 Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 95 | struct asn1p_ioc_cell_s * |
| 96 | asn1p_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 Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 106 | asn1p_wsyntx_chunk_t * |
| 107 | asn1p_wsyntx_chunk_new() { |
| 108 | asn1p_wsyntx_chunk_t *wc; |
| 109 | |
| 110 | wc = calloc(1, sizeof(*wc)); |
| 111 | |
| 112 | return wc; |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | asn1p_wsyntx_chunk_free(asn1p_wsyntx_chunk_t *wc) { |
| 117 | if(wc) { |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 118 | switch(wc->type) { |
Lev Walkin | 57074f1 | 2006-03-16 05:11:14 +0000 | [diff] [blame] | 119 | case WC_LITERAL: |
| 120 | case WC_WHITESPACE: |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 121 | case WC_FIELD: |
Lev Walkin | 57074f1 | 2006-03-16 05:11:14 +0000 | [diff] [blame] | 122 | free(wc->content.token); break; |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 123 | case WC_OPTIONALGROUP: |
| 124 | asn1p_wsyntx_free(wc->content.syntax); |
| 125 | break; |
| 126 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 127 | free(wc); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | asn1p_wsyntx_chunk_t * |
| 132 | asn1p_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 Walkin | 57074f1 | 2006-03-16 05:11:14 +0000 | [diff] [blame] | 137 | nc->type = wc->type; |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 138 | switch(wc->type) { |
| 139 | case WC_LITERAL: |
Lev Walkin | 57074f1 | 2006-03-16 05:11:14 +0000 | [diff] [blame] | 140 | case WC_WHITESPACE: |
Lev Walkin | d370e9f | 2006-03-16 10:03:35 +0000 | [diff] [blame] | 141 | case WC_FIELD: |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 142 | nc->content.token = malloc(strlen(wc->content.token)+1); |
| 143 | strcpy(nc->content.token, wc->content.token); |
| 144 | break; |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 145 | case WC_OPTIONALGROUP: |
| 146 | nc->content.syntax = asn1p_wsyntx_clone(wc->content.syntax); |
| 147 | break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | return nc; |
| 152 | } |
| 153 | |
| 154 | asn1p_wsyntx_t * |
| 155 | asn1p_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 | |
| 166 | void |
| 167 | asn1p_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 | |
| 176 | asn1p_wsyntx_t * |
| 177 | asn1p_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 | |
| 198 | asn1p_wsyntx_chunk_t * |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 199 | asn1p_wsyntx_chunk_fromstring(char *token, int do_copy) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 200 | asn1p_wsyntx_chunk_t *wc; |
| 201 | |
| 202 | if(do_copy) { |
| 203 | static asn1p_wsyntx_chunk_t tmp; |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 204 | tmp.type = WC_LITERAL; |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 205 | tmp.content.token = token; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 206 | wc = asn1p_wsyntx_chunk_clone(&tmp); |
| 207 | } else { |
| 208 | wc = asn1p_wsyntx_chunk_new(); |
| 209 | if(wc) { |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 210 | wc->type = WC_LITERAL; |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 211 | wc->content.token = token; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | return wc; |
| 216 | } |
| 217 | |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 218 | |
| 219 | asn1p_wsyntx_chunk_t * |
| 220 | asn1p_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, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 227 | syntax->parent = wc; |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | return wc; |
| 231 | } |
| 232 | |