blob: a45d7f9fdb5282e359e758ef4cafc99d64044673 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00006#include <constr_SET.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007
Lev Walkin2c962732004-08-11 05:58:52 +00008#ifndef WIN32
9#include <netinet/in.h> /* for ntohl() */
10#else
11#include <winsock2.h> /* for ntohl() */
12#endif
13
Lev Walkin6c0df202005-02-14 19:03:17 +000014/* Check that all the mandatory members are present */
15static int _SET_is_populated(asn_TYPE_descriptor_t *td, void *st);
16
Lev Walkinf15320b2004-06-03 03:38:44 +000017/*
18 * Number of bytes left for this structure.
19 * (ctx->left) indicates the number of bytes _transferred_ for the structure.
20 * (size) contains the number of bytes in the buffer passed.
21 */
Lev Walkinec1ffd42004-08-18 04:53:32 +000022#define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
Lev Walkinf15320b2004-06-03 03:38:44 +000023
24/*
25 * If the subprocessor function returns with an indication that it wants
26 * more data, it may well be a fatal decoding problem, because the
27 * size is constrained by the <TLV>'s L, even if the buffer size allows
28 * reading more data.
29 * For example, consider the buffer containing the following TLVs:
30 * <T:5><L:1><V> <T:6>...
31 * The TLV length clearly indicates that one byte is expected in V, but
32 * if the V processor returns with "want more data" even if the buffer
33 * contains way more data than the V processor have seen.
34 */
Lev Walkind9bd7752004-06-05 08:17:50 +000035#define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
Lev Walkinf15320b2004-06-03 03:38:44 +000036
37/*
38 * This macro "eats" the part of the buffer which is definitely "consumed",
39 * i.e. was correctly converted into local representation or rightfully skipped.
40 */
Lev Walkincc6a9102004-09-23 22:06:26 +000041#undef ADVANCE
Lev Walkinf15320b2004-06-03 03:38:44 +000042#define ADVANCE(num_bytes) do { \
43 size_t num = num_bytes; \
Lev Walkin8c3b8542005-03-10 18:52:02 +000044 ptr = ((const char *)ptr) + num;\
Lev Walkinf15320b2004-06-03 03:38:44 +000045 size -= num; \
46 if(ctx->left >= 0) \
47 ctx->left -= num; \
48 consumed_myself += num; \
49 } while(0)
50
51/*
52 * Switch to the next phase of parsing.
53 */
Lev Walkincc6a9102004-09-23 22:06:26 +000054#undef NEXT_PHASE
Lev Walkinf15320b2004-06-03 03:38:44 +000055#define NEXT_PHASE(ctx) do { \
56 ctx->phase++; \
57 ctx->step = 0; \
58 } while(0)
59
60/*
61 * Return a standardized complex structure.
62 */
Lev Walkincc6a9102004-09-23 22:06:26 +000063#undef RETURN
Lev Walkinf15320b2004-06-03 03:38:44 +000064#define RETURN(_code) do { \
65 rval.code = _code; \
66 rval.consumed = consumed_myself;\
67 return rval; \
68 } while(0)
69
70/*
71 * Tags are canonically sorted in the tag2element map.
72 */
73static int
74_t2e_cmp(const void *ap, const void *bp) {
Lev Walkin5e033762004-09-29 13:26:15 +000075 const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
76 const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
Lev Walkinc2346572004-08-11 09:07:36 +000077
Lev Walkinf15320b2004-06-03 03:38:44 +000078 int a_class = BER_TAG_CLASS(a->el_tag);
79 int b_class = BER_TAG_CLASS(b->el_tag);
80
81 if(a_class == b_class) {
82 ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
83 ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
84
85 if(a_value == b_value)
86 return 0;
87 else if(a_value < b_value)
88 return -1;
89 else
90 return 1;
91 } else if(a_class < b_class) {
92 return -1;
93 } else {
94 return 1;
95 }
96}
97
98/*
99 * The decoder of the SET type.
100 */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000101asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000102SET_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000103 void **struct_ptr, const void *ptr, size_t size, int tag_mode) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000104 /*
105 * Bring closer parts of structure description.
106 */
Lev Walkin5e033762004-09-29 13:26:15 +0000107 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
108 asn_TYPE_member_t *elements = td->elements;
Lev Walkinf15320b2004-06-03 03:38:44 +0000109
110 /*
111 * Parts of the structure being constructed.
112 */
113 void *st = *struct_ptr; /* Target structure. */
Lev Walkin5e033762004-09-29 13:26:15 +0000114 asn_struct_ctx_t *ctx; /* Decoder context */
Lev Walkinf15320b2004-06-03 03:38:44 +0000115
116 ber_tlv_tag_t tlv_tag; /* T from TLV */
Lev Walkindc06f6b2004-10-20 15:50:55 +0000117 asn_dec_rval_t rval; /* Return code from subparsers */
Lev Walkinf15320b2004-06-03 03:38:44 +0000118
119 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
120 int edx; /* SET element's index */
121
Lev Walkin449f8322004-08-20 13:23:42 +0000122 ASN_DEBUG("Decoding %s as SET", td->name);
Lev Walkinf15320b2004-06-03 03:38:44 +0000123
124 /*
125 * Create the target structure if it is not present already.
126 */
127 if(st == 0) {
128 st = *struct_ptr = CALLOC(1, specs->struct_size);
129 if(st == 0) {
130 RETURN(RC_FAIL);
131 }
132 }
133
134 /*
135 * Restore parsing context.
136 */
Lev Walkin5e033762004-09-29 13:26:15 +0000137 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000138
139 /*
140 * Start to parse where left previously
141 */
142 switch(ctx->phase) {
143 case 0:
144 /*
145 * PHASE 0.
146 * Check that the set of tags associated with given structure
147 * perfectly fits our expectations.
148 */
149
Lev Walkin5e033762004-09-29 13:26:15 +0000150 rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
Lev Walkin8e8078a2004-09-26 13:10:40 +0000151 tag_mode, 1, &ctx->left, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +0000152 if(rval.code != RC_OK) {
153 ASN_DEBUG("%s tagging check failed: %d",
Lev Walkin449f8322004-08-20 13:23:42 +0000154 td->name, rval.code);
Lev Walkin4ab42cd2004-10-05 06:36:44 +0000155 return rval;
Lev Walkinf15320b2004-06-03 03:38:44 +0000156 }
157
158 if(ctx->left >= 0)
159 ctx->left += rval.consumed; /* ?Substracted below! */
160 ADVANCE(rval.consumed);
161
162 NEXT_PHASE(ctx);
163
164 ASN_DEBUG("Structure advertised %ld bytes, "
165 "buffer contains %ld", (long)ctx->left, (long)size);
166
167 /* Fall through */
168 case 1:
169 /*
170 * PHASE 1.
171 * From the place where we've left it previously,
172 * try to decode the next member from the list of
173 * this structure's elements.
Lev Walkin3e478ed2004-10-28 13:04:02 +0000174 * Note that elements in BER may arrive out of
Lev Walkinf15320b2004-06-03 03:38:44 +0000175 * order, yet DER mandates that they shall arive in the
176 * canonical order of their tags. So, there is a room
177 * for optimization.
178 */
Lev Walkin3e478ed2004-10-28 13:04:02 +0000179 for(;; ctx->step = 0) {
180 asn_TYPE_tag2member_t *t2m;
181 asn_TYPE_tag2member_t key;
Lev Walkinf15320b2004-06-03 03:38:44 +0000182 void *memb_ptr; /* Pointer to the member */
Lev Walkinc2346572004-08-11 09:07:36 +0000183 void **memb_ptr2; /* Pointer to that pointer */
Lev Walkinf15320b2004-06-03 03:38:44 +0000184 ssize_t tag_len; /* Length of TLV's T */
185
Lev Walkin3e478ed2004-10-28 13:04:02 +0000186 if(ctx->step & 1) {
187 edx = ctx->step >> 1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000188 goto microphase2;
Lev Walkin3e478ed2004-10-28 13:04:02 +0000189 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000190
191 /*
192 * MICROPHASE 1: Synchronize decoding.
193 */
194
195 if(ctx->left == 0)
196 /*
197 * No more things to decode.
198 * Exit out of here and check whether all mandatory
199 * elements have been received (in the next phase).
200 */
201 break;
202
203 /*
204 * Fetch the T from TLV.
205 */
206 tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
207 switch(tag_len) {
208 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
209 /* Fall through */
210 case -1: RETURN(RC_FAIL);
211 }
212
Lev Walkin8c3b8542005-03-10 18:52:02 +0000213 if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000214 if(LEFT < 2) {
215 if(SIZE_VIOLATION)
216 RETURN(RC_FAIL);
217 else
218 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000219 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000220 /*
221 * Found the terminator of the
222 * indefinite length structure.
223 * Invoke the generic finalization function.
224 */
225 goto phase3;
226 }
227 }
228
Lev Walkin3e478ed2004-10-28 13:04:02 +0000229 key.el_tag = tlv_tag;
Lev Walkinc17d90f2005-01-17 14:32:45 +0000230 t2m = (asn_TYPE_tag2member_t *)bsearch(&key,
Lev Walkin3e478ed2004-10-28 13:04:02 +0000231 specs->tag2el, specs->tag2el_count,
232 sizeof(specs->tag2el[0]), _t2e_cmp);
233 if(t2m) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000234 /*
Lev Walkin3e478ed2004-10-28 13:04:02 +0000235 * Found the element corresponding to the tag.
Lev Walkinf15320b2004-06-03 03:38:44 +0000236 */
Lev Walkin3e478ed2004-10-28 13:04:02 +0000237 edx = t2m->el_no;
238 ctx->step = (edx << 1) + 1;
239 ASN_DEBUG("Got tag %s (%s), edx %d",
240 ber_tlv_tag_string(tlv_tag), td->name, edx);
241 } else if(specs->extensible == 0) {
242 ASN_DEBUG("Unexpected tag %s "
243 "in non-extensible SET %s",
244 ber_tlv_tag_string(tlv_tag), td->name);
245 RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000246 } else {
Lev Walkin3e478ed2004-10-28 13:04:02 +0000247 /* Skip this tag */
248 ssize_t skip;
Lev Walkinf15320b2004-06-03 03:38:44 +0000249
Lev Walkin3e478ed2004-10-28 13:04:02 +0000250 ASN_DEBUG("Skipping unknown tag %s",
251 ber_tlv_tag_string(tlv_tag));
Lev Walkinf15320b2004-06-03 03:38:44 +0000252
Lev Walkin3e478ed2004-10-28 13:04:02 +0000253 skip = ber_skip_length(opt_codec_ctx,
254 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin8c3b8542005-03-10 18:52:02 +0000255 (const char *)ptr + tag_len, LEFT - tag_len);
Lev Walkinf15320b2004-06-03 03:38:44 +0000256
Lev Walkin3e478ed2004-10-28 13:04:02 +0000257 switch(skip) {
258 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
259 /* Fall through */
260 case -1: RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000261 }
Lev Walkin3e478ed2004-10-28 13:04:02 +0000262
263 ADVANCE(skip + tag_len);
264 continue; /* Try again with the next tag */
Lev Walkinf15320b2004-06-03 03:38:44 +0000265 }
266
267 /*
268 * MICROPHASE 2: Invoke the member-specific decoder.
269 */
Lev Walkinf15320b2004-06-03 03:38:44 +0000270 microphase2:
271
272 /*
273 * Check for duplications: must not overwrite
274 * already decoded elements.
275 */
Lev Walkin4d9528c2004-08-11 08:10:13 +0000276 if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset, edx)) {
Lev Walkinbbe04752004-07-01 00:47:16 +0000277 ASN_DEBUG("SET %s: Duplicate element %s (%d)",
Lev Walkin449f8322004-08-20 13:23:42 +0000278 td->name, elements[edx].name, edx);
Lev Walkinf15320b2004-06-03 03:38:44 +0000279 RETURN(RC_FAIL);
280 }
281
282 /*
283 * Compute the position of the member inside a structure,
284 * and also a type of containment (it may be contained
285 * as pointer or using inline inclusion).
286 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000287 if(elements[edx].flags & ATF_POINTER) {
288 /* Member is a pointer to another structure */
Lev Walkinc2346572004-08-11 09:07:36 +0000289 memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000290 } else {
291 /*
292 * A pointer to a pointer
293 * holding the start of the structure
294 */
295 memb_ptr = (char *)st + elements[edx].memb_offset;
296 memb_ptr2 = &memb_ptr;
297 }
298 /*
299 * Invoke the member fetch routine according to member's type
300 */
Lev Walkin5e033762004-09-29 13:26:15 +0000301 rval = elements[edx].type->ber_decoder(opt_codec_ctx,
Lev Walkinc2346572004-08-11 09:07:36 +0000302 elements[edx].type,
Lev Walkinf15320b2004-06-03 03:38:44 +0000303 memb_ptr2, ptr, LEFT,
304 elements[edx].tag_mode);
305 switch(rval.code) {
306 case RC_OK:
Lev Walkin4d9528c2004-08-11 08:10:13 +0000307 ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx);
Lev Walkinf15320b2004-06-03 03:38:44 +0000308 break;
309 case RC_WMORE: /* More data expected */
310 if(!SIZE_VIOLATION) {
311 ADVANCE(rval.consumed);
312 RETURN(RC_WMORE);
313 }
314 /* Fail through */
315 case RC_FAIL: /* Fatal error */
316 RETURN(RC_FAIL);
317 } /* switch(rval) */
318
319 ADVANCE(rval.consumed);
320 } /* for(all structure members) */
321
322 phase3:
323 ctx->phase = 3;
324 /* Fall through */
325 case 3:
326 case 4: /* Only 00 is expected */
327 ASN_DEBUG("SET %s Leftover: %ld, size = %ld",
Lev Walkin449f8322004-08-20 13:23:42 +0000328 td->name, (long)ctx->left, (long)size);
Lev Walkinf15320b2004-06-03 03:38:44 +0000329
330 /*
331 * Skip everything until the end of the SET.
332 */
333 while(ctx->left) {
334 ssize_t tl, ll;
335
336 tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
337 switch(tl) {
338 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
339 /* Fall through */
340 case -1: RETURN(RC_FAIL);
341 }
342
343 /*
344 * If expected <0><0>...
345 */
346 if(ctx->left < 0
Lev Walkin8c3b8542005-03-10 18:52:02 +0000347 && ((const uint8_t *)ptr)[0] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000348 if(LEFT < 2) {
349 if(SIZE_VIOLATION)
350 RETURN(RC_FAIL);
351 else
352 RETURN(RC_WMORE);
Lev Walkin8c3b8542005-03-10 18:52:02 +0000353 } else if(((const uint8_t *)ptr)[1] == 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000354 /*
355 * Correctly finished with <0><0>.
356 */
357 ADVANCE(2);
358 ctx->left++;
359 ctx->phase = 4;
360 continue;
361 }
362 }
363
364 if(specs->extensible == 0 || ctx->phase == 4) {
365 ASN_DEBUG("Unexpected continuation "
Lev Walkin3e478ed2004-10-28 13:04:02 +0000366 "of a non-extensible type %s "
367 "(ptr=%02x)",
Lev Walkin8c3b8542005-03-10 18:52:02 +0000368 td->name, *(const uint8_t *)ptr);
Lev Walkinf15320b2004-06-03 03:38:44 +0000369 RETURN(RC_FAIL);
370 }
371
Lev Walkinbaaa24f2004-09-29 14:19:14 +0000372 ll = ber_skip_length(opt_codec_ctx,
Lev Walkinf15320b2004-06-03 03:38:44 +0000373 BER_TLV_CONSTRUCTED(ptr),
Lev Walkin8c3b8542005-03-10 18:52:02 +0000374 (const char *)ptr + tl, LEFT - tl);
Lev Walkinf15320b2004-06-03 03:38:44 +0000375 switch(ll) {
376 case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
377 /* Fall through */
378 case -1: RETURN(RC_FAIL);
379 }
380
381 ADVANCE(tl + ll);
382 }
383
384 ctx->phase = 5;
385 case 5:
Lev Walkin6c0df202005-02-14 19:03:17 +0000386 /* Check that all mandatory elements are present. */
387 if(!_SET_is_populated(td, st))
388 RETURN(RC_FAIL);
Lev Walkinf15320b2004-06-03 03:38:44 +0000389
390 NEXT_PHASE(ctx);
391 }
392
393 RETURN(RC_OK);
394}
395
Lev Walkin6c0df202005-02-14 19:03:17 +0000396static int
397_SET_is_populated(asn_TYPE_descriptor_t *td, void *st) {
398 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
399 int edx;
400
401 /*
402 * Check that all mandatory elements are present.
403 */
404 for(edx = 0; edx < td->elements_count;
405 edx += (8 * sizeof(specs->_mandatory_elements[0]))) {
406 unsigned int midx, pres, must;
407
408 midx = edx/(8 * sizeof(specs->_mandatory_elements[0]));
409 pres = ((unsigned int *)((char *)st+specs->pres_offset))[midx];
410 must = ntohl(specs->_mandatory_elements[midx]);
411
412 if((pres & must) == must) {
413 /*
414 * Yes, everything seems to be in place.
415 */
416 } else {
417 ASN_DEBUG("One or more mandatory elements "
418 "of a SET %s %d (%08x.%08x)=%08x "
419 "are not present",
420 td->name,
421 midx,
422 pres,
423 must,
424 (~(pres & must) & must)
425 );
426 return 0;
427 }
428 }
429
430 return 1;
431}
432
Lev Walkinf15320b2004-06-03 03:38:44 +0000433/*
434 * The DER encoder of the SET type.
435 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000436asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000437SET_encode_der(asn_TYPE_descriptor_t *td,
Lev Walkind5193802004-10-03 09:12:07 +0000438 void *sptr, int tag_mode, ber_tlv_tag_t tag,
Lev Walkinf15320b2004-06-03 03:38:44 +0000439 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin5e033762004-09-29 13:26:15 +0000440 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
Lev Walkinf15320b2004-06-03 03:38:44 +0000441 size_t computed_size = 0;
Lev Walkind5193802004-10-03 09:12:07 +0000442 asn_enc_rval_t er;
Lev Walkin449f8322004-08-20 13:23:42 +0000443 int t2m_build_own = (specs->tag2el_count != td->elements_count);
Lev Walkin5e033762004-09-29 13:26:15 +0000444 asn_TYPE_tag2member_t *t2m;
Lev Walkinf15320b2004-06-03 03:38:44 +0000445 int t2m_count;
446 ssize_t ret;
447 int edx;
448
449 /*
450 * Use existing, or build our own tags map.
451 */
452 if(t2m_build_own) {
Lev Walkinc17d90f2005-01-17 14:32:45 +0000453 t2m = (asn_TYPE_tag2member_t *)alloca(
454 td->elements_count * sizeof(t2m[0]));
Lev Walkind5193802004-10-03 09:12:07 +0000455 if(!t2m) _ASN_ENCODE_FAILED; /* There are such platforms */
Lev Walkinc2346572004-08-11 09:07:36 +0000456 t2m_count = 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000457 } else {
458 /*
459 * There is no untagged CHOICE in this SET.
460 * Employ existing table.
461 */
462 t2m = specs->tag2el;
463 t2m_count = specs->tag2el_count;
464 }
465
466 /*
467 * Gather the length of the underlying members sequence.
468 */
Lev Walkin449f8322004-08-20 13:23:42 +0000469 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000470 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkind5193802004-10-03 09:12:07 +0000471 asn_enc_rval_t tmper;
Lev Walkinf15320b2004-06-03 03:38:44 +0000472 void *memb_ptr;
473
474 /*
475 * Compute the length of the encoding of this member.
476 */
Lev Walkincc93b0f2004-09-10 09:18:20 +0000477 if(elm->flags & ATF_POINTER) {
Lev Walkind5193802004-10-03 09:12:07 +0000478 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000479 if(!memb_ptr) {
480 if(t2m_build_own) {
481 t2m[t2m_count].el_no = edx;
482 t2m[t2m_count].el_tag = 0;
483 t2m_count++;
484 }
485 continue;
486 }
487 } else {
Lev Walkind5193802004-10-03 09:12:07 +0000488 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000489 }
Lev Walkind5193802004-10-03 09:12:07 +0000490 tmper = elm->type->der_encoder(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000491 elm->tag_mode, elm->tag,
492 0, 0);
Lev Walkind5193802004-10-03 09:12:07 +0000493 if(tmper.encoded == -1)
494 return tmper;
495 computed_size += tmper.encoded;
Lev Walkinf15320b2004-06-03 03:38:44 +0000496
497 /*
498 * Remember the outmost tag of this member.
499 */
500 if(t2m_build_own) {
501 t2m[t2m_count].el_no = edx;
Lev Walkin5e033762004-09-29 13:26:15 +0000502 t2m[t2m_count].el_tag = asn_TYPE_outmost_tag(
Lev Walkinf15320b2004-06-03 03:38:44 +0000503 elm->type, memb_ptr, elm->tag_mode, elm->tag);
504 t2m_count++;
505 } else {
506 /*
507 * No dynamic sorting is necessary.
508 */
509 }
510 }
511
512 /*
513 * Finalize order of the components.
514 */
Lev Walkin449f8322004-08-20 13:23:42 +0000515 assert(t2m_count == td->elements_count);
Lev Walkinf15320b2004-06-03 03:38:44 +0000516 if(t2m_build_own) {
517 /*
518 * Sort the underlying members according to their
519 * canonical tags order. DER encoding mandates it.
520 */
521 qsort(t2m, t2m_count, sizeof(specs->tag2el[0]), _t2e_cmp);
522 } else {
523 /*
524 * Tags are already sorted by the compiler.
525 */
526 }
527
528 /*
529 * Encode the TLV for the sequence itself.
530 */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000531 ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
Lev Walkind5193802004-10-03 09:12:07 +0000532 if(ret == -1) _ASN_ENCODE_FAILED;
533 er.encoded = computed_size + ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000534
Lev Walkind5193802004-10-03 09:12:07 +0000535 if(!cb) return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000536
537 /*
538 * Encode all members.
539 */
Lev Walkin449f8322004-08-20 13:23:42 +0000540 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000541 asn_TYPE_member_t *elm;
Lev Walkind5193802004-10-03 09:12:07 +0000542 asn_enc_rval_t tmper;
Lev Walkinf15320b2004-06-03 03:38:44 +0000543 void *memb_ptr;
544
545 /* Encode according to the tag order */
Lev Walkin449f8322004-08-20 13:23:42 +0000546 elm = &td->elements[t2m[edx].el_no];
Lev Walkinf15320b2004-06-03 03:38:44 +0000547
Lev Walkincc93b0f2004-09-10 09:18:20 +0000548 if(elm->flags & ATF_POINTER) {
Lev Walkind5193802004-10-03 09:12:07 +0000549 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000550 if(!memb_ptr) continue;
551 } else {
Lev Walkind5193802004-10-03 09:12:07 +0000552 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
Lev Walkinf15320b2004-06-03 03:38:44 +0000553 }
Lev Walkind5193802004-10-03 09:12:07 +0000554 tmper = elm->type->der_encoder(elm->type, memb_ptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000555 elm->tag_mode, elm->tag,
556 cb, app_key);
Lev Walkind5193802004-10-03 09:12:07 +0000557 if(tmper.encoded == -1)
558 return tmper;
559 computed_size -= tmper.encoded;
Lev Walkinf15320b2004-06-03 03:38:44 +0000560 }
561
562 if(computed_size != 0) {
563 /*
564 * Encoded size is not equal to the computed size.
565 */
Lev Walkind5193802004-10-03 09:12:07 +0000566 _ASN_ENCODE_FAILED;
Lev Walkinf15320b2004-06-03 03:38:44 +0000567 }
568
Lev Walkind5193802004-10-03 09:12:07 +0000569 return er;
Lev Walkinf15320b2004-06-03 03:38:44 +0000570}
571
Lev Walkin6c0df202005-02-14 19:03:17 +0000572#undef XER_ADVANCE
573#define XER_ADVANCE(num_bytes) do { \
574 size_t num = num_bytes; \
Lev Walkin8c3b8542005-03-10 18:52:02 +0000575 buf_ptr = ((const char *)buf_ptr) + num;\
Lev Walkin6c0df202005-02-14 19:03:17 +0000576 size -= num; \
577 consumed_myself += num; \
578 } while(0)
579
580/*
581 * Decode the XER (XML) data.
582 */
583asn_dec_rval_t
584SET_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
585 void **struct_ptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000586 const void *buf_ptr, size_t size) {
Lev Walkin6c0df202005-02-14 19:03:17 +0000587 /*
588 * Bring closer parts of structure description.
589 */
590 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
591 asn_TYPE_member_t *elements = td->elements;
592 const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
593
594 /*
595 * ... and parts of the structure being constructed.
596 */
597 void *st = *struct_ptr; /* Target structure. */
598 asn_struct_ctx_t *ctx; /* Decoder context */
599
600 asn_dec_rval_t rval; /* Return value from a decoder */
601 ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
Lev Walkin6c0df202005-02-14 19:03:17 +0000602 int edx; /* Element index */
603
604 /*
605 * Create the target structure if it is not present already.
606 */
607 if(st == 0) {
608 st = *struct_ptr = CALLOC(1, specs->struct_size);
609 if(st == 0) RETURN(RC_FAIL);
610 }
611
612 /*
613 * Restore parsing context.
614 */
615 ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
616
617 /*
618 * Phases of XER/XML processing:
619 * Phase 0: Check that the opening tag matches our expectations.
620 * Phase 1: Processing body and reacting on closing tag.
621 * Phase 2: Processing inner type.
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000622 * Phase 3: Skipping unknown extensions.
623 * Phase 4: PHASED OUT
Lev Walkin6c0df202005-02-14 19:03:17 +0000624 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000625 for(edx = ctx->step; ctx->phase <= 3;) {
Lev Walkin6c0df202005-02-14 19:03:17 +0000626 pxer_chunk_type_e ch_type; /* XER chunk type */
627 ssize_t ch_size; /* Chunk size */
628 xer_check_tag_e tcv; /* Tag check value */
629 asn_TYPE_member_t *elm;
630
631 /*
632 * Go inside the inner member of a set.
633 */
634 if(ctx->phase == 2) {
635 asn_dec_rval_t tmprval;
636 void *memb_ptr; /* Pointer to the member */
637 void **memb_ptr2; /* Pointer to that pointer */
638
639 if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset,
640 edx)) {
641 ASN_DEBUG("SET %s: Duplicate element %s (%d)",
642 td->name, elements[edx].name, edx);
643 RETURN(RC_FAIL);
644 }
645
Lev Walkin8bb4a952005-02-14 20:15:40 +0000646 elm = &elements[edx];
Lev Walkin6c0df202005-02-14 19:03:17 +0000647
648 if(elm->flags & ATF_POINTER) {
649 /* Member is a pointer to another structure */
650 memb_ptr2 = (void **)((char *)st
651 + elm->memb_offset);
652 } else {
653 memb_ptr = (char *)st + elm->memb_offset;
654 memb_ptr2 = &memb_ptr;
655 }
656
657 /* Invoke the inner type decoder, m.b. multiple times */
658 tmprval = elm->type->xer_decoder(opt_codec_ctx,
659 elm->type, memb_ptr2, elm->name,
660 buf_ptr, size);
661 XER_ADVANCE(tmprval.consumed);
662 if(tmprval.code != RC_OK)
663 RETURN(tmprval.code);
664 ctx->phase = 1; /* Back to body processing */
Lev Walkin6c0df202005-02-14 19:03:17 +0000665 ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx);
666 ASN_DEBUG("XER/SET phase => %d", ctx->phase);
667 /* Fall through */
668 }
669
670 /*
671 * Get the next part of the XML stream.
672 */
Lev Walkin1e443962005-02-18 18:06:36 +0000673 ch_size = xer_next_token(&ctx->context,
674 buf_ptr, size, &ch_type);
Lev Walkin6c0df202005-02-14 19:03:17 +0000675 switch(ch_size) {
676 case -1: RETURN(RC_FAIL);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000677 case 0: RETURN(RC_WMORE);
Lev Walkin6c0df202005-02-14 19:03:17 +0000678 default:
679 switch(ch_type) {
680 case PXER_COMMENT: /* Got XML comment */
681 case PXER_TEXT: /* Ignore free-standing text */
682 XER_ADVANCE(ch_size); /* Skip silently */
683 continue;
684 case PXER_TAG:
685 break; /* Check the rest down there */
686 }
687 }
688
689 tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
690 ASN_DEBUG("XER/SET: tcv = %d, ph=%d", tcv, ctx->phase);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000691
692 /* Skip the extensions section */
693 if(ctx->phase == 3) {
694 switch(xer_skip_unknown(tcv, &ctx->left)) {
695 case -1:
696 ctx->phase = 4;
697 RETURN(RC_FAIL);
Lev Walkin1e443962005-02-18 18:06:36 +0000698 case 1:
699 ctx->phase = 1;
700 /* Fall through */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000701 case 0:
702 XER_ADVANCE(ch_size);
703 continue;
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000704 case 2:
705 ctx->phase = 1;
706 break;
707 }
708 }
709
Lev Walkin6c0df202005-02-14 19:03:17 +0000710 switch(tcv) {
711 case XCT_CLOSING:
712 if(ctx->phase == 0) break;
713 ctx->phase = 0;
714 /* Fall through */
715 case XCT_BOTH:
716 if(ctx->phase == 0) {
717 if(_SET_is_populated(td, st)) {
718 XER_ADVANCE(ch_size);
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000719 ctx->phase = 4; /* Phase out */
Lev Walkin6c0df202005-02-14 19:03:17 +0000720 RETURN(RC_OK);
721 } else {
722 ASN_DEBUG("Premature end of XER SET");
723 RETURN(RC_FAIL);
724 }
725 }
726 /* Fall through */
727 case XCT_OPENING:
728 if(ctx->phase == 0) {
729 XER_ADVANCE(ch_size);
730 ctx->phase = 1; /* Processing body phase */
731 continue;
732 }
733 /* Fall through */
Lev Walkin904e65b2005-02-18 14:23:48 +0000734 case XCT_UNKNOWN_OP:
735 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000736
737 ASN_DEBUG("XER/SET: tcv=%d, ph=%d", tcv, ctx->phase);
738 if(ctx->phase != 1)
739 break; /* Really unexpected */
740
741 /*
742 * Search which member corresponds to this tag.
743 */
744 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000745 switch(xer_check_tag(buf_ptr, ch_size,
746 elements[edx].name)) {
Lev Walkin6c0df202005-02-14 19:03:17 +0000747 case XCT_BOTH:
748 case XCT_OPENING:
749 /*
750 * Process this member.
751 */
752 ctx->step = edx;
753 ctx->phase = 2;
754 break;
Lev Walkin904e65b2005-02-18 14:23:48 +0000755 case XCT_UNKNOWN_OP:
756 case XCT_UNKNOWN_BO:
Lev Walkin6c0df202005-02-14 19:03:17 +0000757 continue;
Lev Walkin6c0df202005-02-14 19:03:17 +0000758 default:
759 edx = td->elements_count;
760 break; /* Phase out */
761 }
762 break;
763 }
764 if(edx != td->elements_count)
765 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000766
767 /* It is expected extension */
768 if(specs->extensible) {
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000769 ASN_DEBUG("Got anticipated extension");
Lev Walkin904e65b2005-02-18 14:23:48 +0000770 /*
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000771 * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
772 * By using a mask. Only record a pure
773 * <opening> tags.
Lev Walkin904e65b2005-02-18 14:23:48 +0000774 */
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000775 if(tcv & XCT_CLOSING) {
776 /* Found </extension> without body */
777 } else {
778 ctx->left = 1;
779 ctx->phase = 3; /* Skip ...'s */
780 }
781 XER_ADVANCE(ch_size);
782 continue;
Lev Walkin904e65b2005-02-18 14:23:48 +0000783 }
784
Lev Walkin6c0df202005-02-14 19:03:17 +0000785 /* Fall through */
786 default:
787 break;
788 }
789
Lev Walkine0b56e02005-02-25 12:10:27 +0000790 ASN_DEBUG("Unexpected XML tag in SET, expected \"%s\"",
791 xml_tag);
Lev Walkin6c0df202005-02-14 19:03:17 +0000792 break;
793 }
794
Lev Walkin2eeeedc2005-02-18 16:10:40 +0000795 ctx->phase = 4; /* "Phase out" on hard failure */
Lev Walkin6c0df202005-02-14 19:03:17 +0000796 RETURN(RC_FAIL);
797}
798
Lev Walkina9cc46e2004-09-22 16:06:28 +0000799asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000800SET_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000801 int ilevel, enum xer_encoder_flags_e flags,
802 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkind5193802004-10-03 09:12:07 +0000803 asn_SET_specifics_t *specs = (asn_SET_specifics_t *)td->specifics;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000804 asn_enc_rval_t er;
805 int xcan = (flags & XER_F_CANONICAL);
Lev Walkind5193802004-10-03 09:12:07 +0000806 asn_TYPE_tag2member_t *t2m = specs->tag2el_cxer;
807 int t2m_count = specs->tag2el_cxer_count;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000808 int edx;
809
810 if(!sptr)
811 _ASN_ENCODE_FAILED;
812
Lev Walkind5193802004-10-03 09:12:07 +0000813 assert(t2m_count == td->elements_count);
814
Lev Walkina9cc46e2004-09-22 16:06:28 +0000815 er.encoded = 0;
816
Lev Walkind5193802004-10-03 09:12:07 +0000817 for(edx = 0; edx < t2m_count; edx++) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000818 asn_enc_rval_t tmper;
Lev Walkind5193802004-10-03 09:12:07 +0000819 asn_TYPE_member_t *elm;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000820 void *memb_ptr;
Lev Walkind5193802004-10-03 09:12:07 +0000821 const char *mname;
822 unsigned int mlen;
823
824 elm = &td->elements[t2m[edx].el_no];
825 mname = elm->name;
826 mlen = strlen(elm->name);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000827
828 if(elm->flags & ATF_POINTER) {
829 memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
830 if(!memb_ptr) continue; /* OPTIONAL element? */
831 } else {
832 memb_ptr = (void *)((char *)sptr + elm->memb_offset);
833 }
834
835 if(!xcan)
836 _i_ASN_TEXT_INDENT(1, ilevel);
837 _ASN_CALLBACK3("<", 1, mname, mlen, ">", 1);
838
839 /* Print the member itself */
840 tmper = elm->type->xer_encoder(elm->type, memb_ptr,
841 ilevel + 1, flags, cb, app_key);
842 if(tmper.encoded == -1) return tmper;
843
844 _ASN_CALLBACK3("</", 2, mname, mlen, ">", 1);
845
846 er.encoded += 5 + (2 * mlen) + tmper.encoded;
847 }
848
849 if(!xcan) _i_ASN_TEXT_INDENT(1, ilevel - 1);
850
851 return er;
Lev Walkind5193802004-10-03 09:12:07 +0000852cb_failed:
853 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000854}
855
Lev Walkinf15320b2004-06-03 03:38:44 +0000856int
Lev Walkin5e033762004-09-29 13:26:15 +0000857SET_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000858 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000859 int edx;
860 int ret;
861
Lev Walkin8e8078a2004-09-26 13:10:40 +0000862 if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000863
864 /* Dump preamble */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000865 if(cb(td->name, strlen(td->name), app_key) < 0
866 || cb(" ::= {", 6, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000867 return -1;
868
Lev Walkin449f8322004-08-20 13:23:42 +0000869 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000870 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000871 const void *memb_ptr;
872
Lev Walkincc93b0f2004-09-10 09:18:20 +0000873 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000874 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
875 if(!memb_ptr) continue;
876 } else {
877 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
878 }
879
Lev Walkin8e8078a2004-09-26 13:10:40 +0000880 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000881
882 /* Print the member's name and stuff */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000883 if(cb(elm->name, strlen(elm->name), app_key) < 0
884 || cb(": ", 2, app_key) < 0)
Lev Walkinf15320b2004-06-03 03:38:44 +0000885 return -1;
886
887 /* Print the member itself */
Lev Walkin8e8078a2004-09-26 13:10:40 +0000888 ret = elm->type->print_struct(elm->type, memb_ptr, ilevel + 1,
Lev Walkinf15320b2004-06-03 03:38:44 +0000889 cb, app_key);
890 if(ret) return ret;
Lev Walkinf15320b2004-06-03 03:38:44 +0000891 }
892
Lev Walkin8e8078a2004-09-26 13:10:40 +0000893 ilevel--;
894 _i_INDENT(1);
Lev Walkinf15320b2004-06-03 03:38:44 +0000895
Lev Walkin8e8078a2004-09-26 13:10:40 +0000896 return (cb("}", 1, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000897}
898
899void
Lev Walkin5e033762004-09-29 13:26:15 +0000900SET_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000901 int edx;
902
903 if(!td || !ptr)
904 return;
905
906 ASN_DEBUG("Freeing %s as SET", td->name);
907
Lev Walkin449f8322004-08-20 13:23:42 +0000908 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000909 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000910 void *memb_ptr;
Lev Walkincc93b0f2004-09-10 09:18:20 +0000911 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000912 memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
913 if(memb_ptr)
914 elm->type->free_struct(elm->type, memb_ptr, 0);
915 } else {
916 memb_ptr = (void *)((char *)ptr + elm->memb_offset);
917 elm->type->free_struct(elm->type, memb_ptr, 1);
918 }
919 }
920
921 if(!contents_only) {
922 FREEMEM(ptr);
923 }
924}
925
926int
Lev Walkin5e033762004-09-29 13:26:15 +0000927SET_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000928 asn_app_consume_bytes_f *app_errlog, void *app_key) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000929 int edx;
930
931 if(!sptr) {
Lev Walkinba4e5182004-08-11 09:44:13 +0000932 _ASN_ERRLOG(app_errlog, app_key,
Lev Walkin16835b62004-08-22 13:47:59 +0000933 "%s: value not given (%s:%d)",
934 td->name, __FILE__, __LINE__);
Lev Walkinf15320b2004-06-03 03:38:44 +0000935 return -1;
936 }
937
938 /*
939 * Iterate over structure members and check their validity.
940 */
Lev Walkin449f8322004-08-20 13:23:42 +0000941 for(edx = 0; edx < td->elements_count; edx++) {
Lev Walkin5e033762004-09-29 13:26:15 +0000942 asn_TYPE_member_t *elm = &td->elements[edx];
Lev Walkinf15320b2004-06-03 03:38:44 +0000943 const void *memb_ptr;
944
Lev Walkincc93b0f2004-09-10 09:18:20 +0000945 if(elm->flags & ATF_POINTER) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000946 memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
947 if(!memb_ptr) {
Lev Walkincc93b0f2004-09-10 09:18:20 +0000948 if(elm->optional)
949 continue;
950 _ASN_ERRLOG(app_errlog, app_key,
951 "%s: mandatory element %s absent (%s:%d)",
952 td->name, elm->name, __FILE__, __LINE__);
953 return -1;
Lev Walkinf15320b2004-06-03 03:38:44 +0000954 }
955 } else {
956 memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
957 }
958
Lev Walkin449f8322004-08-20 13:23:42 +0000959 if(elm->memb_constraints) {
960 int ret = elm->memb_constraints(elm->type, memb_ptr,
961 app_errlog, app_key);
962 if(ret) return ret;
963 } else {
964 int ret = elm->type->check_constraints(elm->type,
965 memb_ptr, app_errlog, app_key);
966 if(ret) return ret;
967 /*
968 * Cannot inherit it earlier:
969 * need to make sure we get the updated version.
970 */
971 elm->memb_constraints = elm->type->check_constraints;
972 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000973 }
974
975 return 0;
976}