2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
5 #include <asn_internal.h>
10 * The XER encoder of any type. May be invoked by the application.
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,
16 asn_enc_rval_t er = {0, 0, 0};
20 int xcan = (xer_flags & XER_F_CANONICAL) ? 1 : 2;
22 if(!td || !sptr) goto cb_failed;
27 ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
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;
33 ASN__CALLBACK3("</", 2, mname, mlen, ">\n", xcan);
41 * This is a helper function for xer_fprint, which directs all incoming data
42 * into the provided file descriptor.
45 xer__print2fp(const void *buffer, size_t size, void *app_key) {
46 FILE *stream = (FILE *)app_key;
48 if(fwrite(buffer, 1, size, stream) != size)
55 xer_fprint(FILE *stream, const asn_TYPE_descriptor_t *td, const void *sptr) {
58 if(!stream) stream = stdout;
62 er = xer_encode(td, sptr, XER_F_BASIC, xer__print2fp, stream);
66 return fflush(stream);
72 size_t allocated_size;
76 xer__buffer_append(const void *buffer, size_t size, void *app_key) {
77 struct xer_buffer *xb = app_key;
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;
84 memcpy(new_buf, xb->buffer, xb->buffer_size);
88 xb->allocated_size = new_size;
91 memcpy(xb->buffer + xb->buffer_size, buffer, size);
92 xb->buffer_size += size;
93 xb->buffer[xb->buffer_size] = '\0';
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;
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");
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);
121 return XEQ_ENCODE1_FAILED;
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);
131 return XEQ_ENCODE1_FAILED;
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);
144 return XEQ_DIFFERENT;
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);
154 rval = xer_decode(NULL, td, (void **)&sptr, xb1.buffer,
160 if(opt_debug_stream) {
161 fprintf(opt_debug_stream,
162 "Structure %s XER decode unexpectedly requires "
164 td->name, xb1.buffer);
169 if(opt_debug_stream) {
170 fprintf(opt_debug_stream,
171 "Structure %s XER decoding resulted in failure.\n",
174 ASN_STRUCT_FREE(*td, sptr);
177 return XEQ_DECODE_FAILED;
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);
191 ASN_STRUCT_FREE(*td, sptr);
194 return XEQ_ROUND_TRIP_FAILED;
198 * Reuse xb2 to encode newly decoded structure.
201 memset(&xb2, 0, sizeof(xb2));
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",
209 ASN_STRUCT_FREE(*td, sptr);
212 return XEQ_ROUND_TRIP_FAILED;
215 ASN_STRUCT_FREE(*td, sptr);
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);
230 return XEQ_ROUND_TRIP_FAILED;