vlm | fa67ddc | 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) { |
| 21 | if(wc->ref) |
| 22 | asn1p_ref_free(wc->ref); |
| 23 | if(wc->buf) |
| 24 | free(wc->buf); |
| 25 | free(wc); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | asn1p_wsyntx_chunk_t * |
| 30 | asn1p_wsyntx_chunk_clone(asn1p_wsyntx_chunk_t *wc) { |
| 31 | asn1p_wsyntx_chunk_t *nc; |
| 32 | |
| 33 | nc = asn1p_wsyntx_chunk_new(); |
| 34 | if(nc) { |
| 35 | if(wc->buf) { |
| 36 | nc->buf = malloc(wc->len + 1); |
| 37 | if(nc->buf) { |
| 38 | nc->len = wc->len; |
| 39 | memcpy(nc->buf, wc->buf, wc->len); |
| 40 | nc->buf[nc->len] = '\0'; |
| 41 | } |
| 42 | } |
| 43 | if(wc->ref) { |
| 44 | nc->ref = asn1p_ref_clone(wc->ref); |
| 45 | } |
| 46 | |
| 47 | if(!nc->ref && !nc->buf) { |
| 48 | asn1p_wsyntx_chunk_free(nc); |
| 49 | return NULL; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return nc; |
| 54 | } |
| 55 | |
| 56 | asn1p_wsyntx_t * |
| 57 | asn1p_wsyntx_new() { |
| 58 | asn1p_wsyntx_t *wx; |
| 59 | |
| 60 | wx = calloc(1, sizeof(*wx)); |
| 61 | if(wx) { |
| 62 | TQ_INIT(&(wx->chunks)); |
| 63 | } |
| 64 | |
| 65 | return wx; |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | asn1p_wsyntx_free(asn1p_wsyntx_t *wx) { |
| 70 | if(wx) { |
| 71 | asn1p_wsyntx_chunk_t *wc; |
| 72 | while((wc = TQ_REMOVE(&(wx->chunks), next))) |
| 73 | asn1p_wsyntx_chunk_free(wc); |
| 74 | free(wx); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | asn1p_wsyntx_t * |
| 79 | asn1p_wsyntx_clone(asn1p_wsyntx_t *wx) { |
| 80 | asn1p_wsyntx_t *nw; |
| 81 | |
| 82 | nw = asn1p_wsyntx_new(); |
| 83 | if(nw) { |
| 84 | asn1p_wsyntx_chunk_t *wc; |
| 85 | asn1p_wsyntx_chunk_t *nc; |
| 86 | TQ_FOR(wc, &(wx->chunks), next) { |
| 87 | nc = asn1p_wsyntx_chunk_clone(wc); |
| 88 | if(nc) { |
| 89 | TQ_ADD(&(nw->chunks), nc, next); |
| 90 | } else { |
| 91 | asn1p_wsyntx_free(nw); |
| 92 | return NULL; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return nw; |
| 98 | } |
| 99 | |
| 100 | asn1p_wsyntx_chunk_t * |
| 101 | asn1p_wsyntx_chunk_fromref(asn1p_ref_t *ref, int do_copy) { |
| 102 | asn1p_wsyntx_chunk_t *wc; |
| 103 | |
| 104 | if(do_copy) { |
| 105 | static asn1p_wsyntx_chunk_t tmp; |
| 106 | tmp.ref = ref; |
| 107 | wc = asn1p_wsyntx_chunk_clone(&tmp); |
| 108 | } else { |
| 109 | wc = asn1p_wsyntx_chunk_new(); |
| 110 | if(wc) wc->ref = ref; |
| 111 | } |
| 112 | |
| 113 | return wc; |
| 114 | } |
| 115 | |
| 116 | asn1p_wsyntx_chunk_t * |
| 117 | asn1p_wsyntx_chunk_frombuf(char *buf, int len, int do_copy) { |
| 118 | asn1p_wsyntx_chunk_t *wc; |
| 119 | |
| 120 | if(do_copy) { |
| 121 | static asn1p_wsyntx_chunk_t tmp; |
| 122 | tmp.buf = buf; |
| 123 | tmp.len = len; |
| 124 | wc = asn1p_wsyntx_chunk_clone(&tmp); |
| 125 | } else { |
| 126 | wc = asn1p_wsyntx_chunk_new(); |
| 127 | if(wc) { |
| 128 | wc->buf = buf; |
| 129 | wc->len = len; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return wc; |
| 134 | } |
| 135 | |