Peter Szilagyi | fbc56f9 | 2019-07-23 19:29:46 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
| 5 | #include <asn_internal.h> |
| 6 | #include <stdio.h> |
| 7 | #include <errno.h> |
| 8 | |
| 9 | /* |
| 10 | * The XER encoder of any type. May be invoked by the application. |
| 11 | */ |
| 12 | asn_enc_rval_t |
| 13 | xer_encode(const asn_TYPE_descriptor_t *td, const void *sptr, |
| 14 | enum xer_encoder_flags_e xer_flags, asn_app_consume_bytes_f *cb, |
| 15 | void *app_key) { |
| 16 | asn_enc_rval_t er = {0, 0, 0}; |
| 17 | asn_enc_rval_t tmper; |
| 18 | const char *mname; |
| 19 | size_t mlen; |
| 20 | int xcan = (xer_flags & XER_F_CANONICAL) ? 1 : 2; |
| 21 | |
| 22 | if(!td || !sptr) goto cb_failed; |
| 23 | |
| 24 | mname = td->xml_tag; |
| 25 | mlen = strlen(mname); |
| 26 | |
| 27 | ASN__CALLBACK3("<", 1, mname, mlen, ">", 1); |
| 28 | |
| 29 | tmper = td->op->xer_encoder(td, sptr, 1, xer_flags, cb, app_key); |
| 30 | if(tmper.encoded == -1) return tmper; |
| 31 | er.encoded += tmper.encoded; |
| 32 | |
| 33 | ASN__CALLBACK3("</", 2, mname, mlen, ">\n", xcan); |
| 34 | |
| 35 | ASN__ENCODED_OK(er); |
| 36 | cb_failed: |
| 37 | ASN__ENCODE_FAILED; |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * This is a helper function for xer_fprint, which directs all incoming data |
| 42 | * into the provided file descriptor. |
| 43 | */ |
| 44 | static int |
| 45 | xer__print2fp(const void *buffer, size_t size, void *app_key) { |
| 46 | FILE *stream = (FILE *)app_key; |
| 47 | |
| 48 | if(fwrite(buffer, 1, size, stream) != size) |
| 49 | return -1; |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | int |
| 55 | xer_fprint(FILE *stream, const asn_TYPE_descriptor_t *td, const void *sptr) { |
| 56 | asn_enc_rval_t er = {0,0,0}; |
| 57 | |
| 58 | if(!stream) stream = stdout; |
| 59 | if(!td || !sptr) |
| 60 | return -1; |
| 61 | |
| 62 | er = xer_encode(td, sptr, XER_F_BASIC, xer__print2fp, stream); |
| 63 | if(er.encoded == -1) |
| 64 | return -1; |
| 65 | |
| 66 | return fflush(stream); |
| 67 | } |
| 68 | |
| 69 | struct xer_buffer { |
| 70 | char *buffer; |
| 71 | size_t buffer_size; |
| 72 | size_t allocated_size; |
| 73 | }; |
| 74 | |
| 75 | static int |
| 76 | xer__buffer_append(const void *buffer, size_t size, void *app_key) { |
| 77 | struct xer_buffer *xb = app_key; |
| 78 | |
| 79 | while(xb->buffer_size + size + 1 > xb->allocated_size) { |
| 80 | size_t new_size = 2 * (xb->allocated_size ? xb->allocated_size : 64); |
| 81 | char *new_buf = MALLOC(new_size); |
| 82 | if(!new_buf) return -1; |
| 83 | if (xb->buffer) { |
| 84 | memcpy(new_buf, xb->buffer, xb->buffer_size); |
| 85 | } |
| 86 | FREEMEM(xb->buffer); |
| 87 | xb->buffer = new_buf; |
| 88 | xb->allocated_size = new_size; |
| 89 | } |
| 90 | |
| 91 | memcpy(xb->buffer + xb->buffer_size, buffer, size); |
| 92 | xb->buffer_size += size; |
| 93 | xb->buffer[xb->buffer_size] = '\0'; |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | enum xer_equivalence_e |
| 98 | xer_equivalent(const struct asn_TYPE_descriptor_s *td, const void *struct1, |
| 99 | const void *struct2, FILE *opt_debug_stream) { |
| 100 | struct xer_buffer xb1 = {0, 0, 0}; |
| 101 | struct xer_buffer xb2 = {0, 0, 0}; |
| 102 | asn_enc_rval_t e1, e2; |
| 103 | asn_dec_rval_t rval; |
| 104 | void *sptr = NULL; |
| 105 | |
| 106 | if(!td || !struct1 || !struct2) { |
| 107 | if(opt_debug_stream) { |
| 108 | if(!td) fprintf(opt_debug_stream, "Type descriptor missing\n"); |
| 109 | if(!struct1) fprintf(opt_debug_stream, "Structure 1 missing\n"); |
| 110 | if(!struct2) fprintf(opt_debug_stream, "Structure 2 missing\n"); |
| 111 | } |
| 112 | return XEQ_FAILURE; |
| 113 | } |
| 114 | |
| 115 | e1 = xer_encode(td, struct1, XER_F_BASIC, xer__buffer_append, &xb1); |
| 116 | if(e1.encoded == -1) { |
| 117 | if(opt_debug_stream) { |
| 118 | fprintf(stderr, "XER Encoding of %s failed\n", td->name); |
| 119 | } |
| 120 | FREEMEM(xb1.buffer); |
| 121 | return XEQ_ENCODE1_FAILED; |
| 122 | } |
| 123 | |
| 124 | e2 = xer_encode(td, struct2, XER_F_BASIC, xer__buffer_append, &xb2); |
| 125 | if(e2.encoded == -1) { |
| 126 | if(opt_debug_stream) { |
| 127 | fprintf(stderr, "XER Encoding of %s failed\n", td->name); |
| 128 | } |
| 129 | FREEMEM(xb1.buffer); |
| 130 | FREEMEM(xb2.buffer); |
| 131 | return XEQ_ENCODE1_FAILED; |
| 132 | } |
| 133 | |
| 134 | if(xb1.buffer_size != xb2.buffer_size |
| 135 | || memcmp(xb1.buffer, xb2.buffer, xb1.buffer_size) != 0) { |
| 136 | if(opt_debug_stream) { |
| 137 | fprintf(opt_debug_stream, |
| 138 | "Structures XER-encoded into different byte streams:\n=== " |
| 139 | "Structure 1 ===\n%s\n=== Structure 2 ===\n%s\n", |
| 140 | xb1.buffer, xb2.buffer); |
| 141 | } |
| 142 | FREEMEM(xb1.buffer); |
| 143 | FREEMEM(xb2.buffer); |
| 144 | return XEQ_DIFFERENT; |
| 145 | } else { |
| 146 | if(opt_debug_stream) { |
| 147 | fprintf(opt_debug_stream, |
| 148 | "Both structures encoded into the same XER byte stream " |
| 149 | "of size %" ASN_PRI_SIZE ":\n%s", |
| 150 | xb1.buffer_size, xb1.buffer); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | rval = xer_decode(NULL, td, (void **)&sptr, xb1.buffer, |
| 155 | xb1.buffer_size); |
| 156 | switch(rval.code) { |
| 157 | case RC_OK: |
| 158 | break; |
| 159 | case RC_WMORE: |
| 160 | if(opt_debug_stream) { |
| 161 | fprintf(opt_debug_stream, |
| 162 | "Structure %s XER decode unexpectedly requires " |
| 163 | "more data:\n%s\n", |
| 164 | td->name, xb1.buffer); |
| 165 | } |
| 166 | /* Fall through */ |
| 167 | case RC_FAIL: |
| 168 | default: |
| 169 | if(opt_debug_stream) { |
| 170 | fprintf(opt_debug_stream, |
| 171 | "Structure %s XER decoding resulted in failure.\n", |
| 172 | td->name); |
| 173 | } |
| 174 | ASN_STRUCT_FREE(*td, sptr); |
| 175 | FREEMEM(xb1.buffer); |
| 176 | FREEMEM(xb2.buffer); |
| 177 | return XEQ_DECODE_FAILED; |
| 178 | } |
| 179 | |
| 180 | if(rval.consumed != xb1.buffer_size |
| 181 | && ((rval.consumed > xb1.buffer_size) |
| 182 | || xer_whitespace_span(xb1.buffer + rval.consumed, |
| 183 | xb1.buffer_size - rval.consumed) |
| 184 | != (xb1.buffer_size - rval.consumed))) { |
| 185 | if(opt_debug_stream) { |
| 186 | fprintf(opt_debug_stream, |
| 187 | "Round-trip decode of %s required less bytes (%" ASN_PRI_SIZE ") than " |
| 188 | "encoded (%" ASN_PRI_SIZE ")\n", |
| 189 | td->name, rval.consumed, xb1.buffer_size); |
| 190 | } |
| 191 | ASN_STRUCT_FREE(*td, sptr); |
| 192 | FREEMEM(xb1.buffer); |
| 193 | FREEMEM(xb2.buffer); |
| 194 | return XEQ_ROUND_TRIP_FAILED; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Reuse xb2 to encode newly decoded structure. |
| 199 | */ |
| 200 | FREEMEM(xb2.buffer); |
| 201 | memset(&xb2, 0, sizeof(xb2)); |
| 202 | |
| 203 | e2 = xer_encode(td, sptr, XER_F_BASIC, xer__buffer_append, &xb2); |
| 204 | if(e2.encoded == -1) { |
| 205 | if(opt_debug_stream) { |
| 206 | fprintf(stderr, "XER Encoding of round-trip decode of %s failed\n", |
| 207 | td->name); |
| 208 | } |
| 209 | ASN_STRUCT_FREE(*td, sptr); |
| 210 | FREEMEM(xb1.buffer); |
| 211 | FREEMEM(xb2.buffer); |
| 212 | return XEQ_ROUND_TRIP_FAILED; |
| 213 | } |
| 214 | |
| 215 | ASN_STRUCT_FREE(*td, sptr); |
| 216 | sptr = 0; |
| 217 | |
| 218 | if(xb1.buffer_size != xb2.buffer_size |
| 219 | || memcmp(xb1.buffer, xb2.buffer, xb1.buffer_size) != 0) { |
| 220 | if(opt_debug_stream) { |
| 221 | fprintf(opt_debug_stream, |
| 222 | "XER Encoding of round-trip decode of %s resulted in " |
| 223 | "different byte stream:\n" |
| 224 | "=== Original ===\n%s\n" |
| 225 | "=== Round-tripped ===\n%s\n", |
| 226 | xb1.buffer, xb2.buffer, td->name); |
| 227 | } |
| 228 | FREEMEM(xb1.buffer); |
| 229 | FREEMEM(xb2.buffer); |
| 230 | return XEQ_ROUND_TRIP_FAILED; |
| 231 | } |
| 232 | |
| 233 | FREEMEM(xb1.buffer); |
| 234 | FREEMEM(xb2.buffer); |
| 235 | return XEQ_SUCCESS; |
| 236 | } |
| 237 | |