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 | |
| 9 | asn1p_wsyntx_chunk_t * |
| 10 | asn1p_wsyntx_chunk_new() { |
| 11 | asn1p_wsyntx_chunk_t *wc; |
| 12 | |
| 13 | wc = calloc(1, sizeof(*wc)); |
| 14 | |
| 15 | return wc; |
| 16 | } |
| 17 | |
| 18 | void |
| 19 | asn1p_wsyntx_chunk_free(asn1p_wsyntx_chunk_t *wc) { |
| 20 | if(wc) { |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame^] | 21 | switch(wc->type) { |
| 22 | case WC_LITERAL: free(wc->content.token); break; |
| 23 | case WC_REFERENCE: asn1p_ref_free(wc->content.ref); break; |
| 24 | case WC_OPTIONALGROUP: |
| 25 | asn1p_wsyntx_free(wc->content.syntax); |
| 26 | break; |
| 27 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 28 | free(wc); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | asn1p_wsyntx_chunk_t * |
| 33 | asn1p_wsyntx_chunk_clone(asn1p_wsyntx_chunk_t *wc) { |
| 34 | asn1p_wsyntx_chunk_t *nc; |
| 35 | |
| 36 | nc = asn1p_wsyntx_chunk_new(); |
| 37 | if(nc) { |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame^] | 38 | switch(wc->type) { |
| 39 | case WC_LITERAL: |
| 40 | nc->content.token = malloc(strlen(wc->content.token)+1); |
| 41 | strcpy(nc->content.token, wc->content.token); |
| 42 | break; |
| 43 | case WC_REFERENCE: |
| 44 | nc->content.ref = asn1p_ref_clone(wc->content.ref); |
| 45 | break; |
| 46 | case WC_OPTIONALGROUP: |
| 47 | nc->content.syntax = asn1p_wsyntx_clone(wc->content.syntax); |
| 48 | break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | return nc; |
| 53 | } |
| 54 | |
| 55 | asn1p_wsyntx_t * |
| 56 | asn1p_wsyntx_new() { |
| 57 | asn1p_wsyntx_t *wx; |
| 58 | |
| 59 | wx = calloc(1, sizeof(*wx)); |
| 60 | if(wx) { |
| 61 | TQ_INIT(&(wx->chunks)); |
| 62 | } |
| 63 | |
| 64 | return wx; |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | asn1p_wsyntx_free(asn1p_wsyntx_t *wx) { |
| 69 | if(wx) { |
| 70 | asn1p_wsyntx_chunk_t *wc; |
| 71 | while((wc = TQ_REMOVE(&(wx->chunks), next))) |
| 72 | asn1p_wsyntx_chunk_free(wc); |
| 73 | free(wx); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | asn1p_wsyntx_t * |
| 78 | asn1p_wsyntx_clone(asn1p_wsyntx_t *wx) { |
| 79 | asn1p_wsyntx_t *nw; |
| 80 | |
| 81 | nw = asn1p_wsyntx_new(); |
| 82 | if(nw) { |
| 83 | asn1p_wsyntx_chunk_t *wc; |
| 84 | asn1p_wsyntx_chunk_t *nc; |
| 85 | TQ_FOR(wc, &(wx->chunks), next) { |
| 86 | nc = asn1p_wsyntx_chunk_clone(wc); |
| 87 | if(nc) { |
| 88 | TQ_ADD(&(nw->chunks), nc, next); |
| 89 | } else { |
| 90 | asn1p_wsyntx_free(nw); |
| 91 | return NULL; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return nw; |
| 97 | } |
| 98 | |
| 99 | asn1p_wsyntx_chunk_t * |
| 100 | asn1p_wsyntx_chunk_fromref(asn1p_ref_t *ref, int do_copy) { |
| 101 | asn1p_wsyntx_chunk_t *wc; |
| 102 | |
| 103 | if(do_copy) { |
| 104 | static asn1p_wsyntx_chunk_t tmp; |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame^] | 105 | tmp.type = WC_REFERENCE; |
| 106 | tmp.content.ref = ref; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 107 | wc = asn1p_wsyntx_chunk_clone(&tmp); |
| 108 | } else { |
| 109 | wc = asn1p_wsyntx_chunk_new(); |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame^] | 110 | if(wc) { |
| 111 | wc->type = WC_REFERENCE; |
| 112 | wc->content.ref = ref; |
| 113 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | return wc; |
| 117 | } |
| 118 | |
| 119 | asn1p_wsyntx_chunk_t * |
| 120 | asn1p_wsyntx_chunk_frombuf(char *buf, int len, int do_copy) { |
| 121 | asn1p_wsyntx_chunk_t *wc; |
| 122 | |
| 123 | if(do_copy) { |
| 124 | static asn1p_wsyntx_chunk_t tmp; |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame^] | 125 | tmp.type = WC_LITERAL; |
| 126 | tmp.content.token = buf; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 127 | wc = asn1p_wsyntx_chunk_clone(&tmp); |
| 128 | } else { |
| 129 | wc = asn1p_wsyntx_chunk_new(); |
| 130 | if(wc) { |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame^] | 131 | wc->type = WC_LITERAL; |
| 132 | wc->content.token = buf; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | return wc; |
| 137 | } |
| 138 | |
Lev Walkin | 9d542d2 | 2006-03-14 16:31:37 +0000 | [diff] [blame^] | 139 | |
| 140 | asn1p_wsyntx_chunk_t * |
| 141 | asn1p_wsyntx_chunk_fromsyntax(asn1p_wsyntx_t *syntax) { |
| 142 | asn1p_wsyntx_chunk_t *wc; |
| 143 | |
| 144 | wc = asn1p_wsyntx_chunk_new(); |
| 145 | if(wc) { |
| 146 | wc->type = WC_OPTIONALGROUP; |
| 147 | wc->content.syntax = syntax; |
| 148 | } |
| 149 | |
| 150 | return wc; |
| 151 | } |
| 152 | |