constr_CHOICE.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. /*
  2. * Copyright (c) 2003-2017 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 <constr_CHOICE.h>
  7. #include <per_opentype.h>
  8. /*
  9. * Number of bytes left for this structure.
  10. * (ctx->left) indicates the number of bytes _transferred_ for the structure.
  11. * (size) contains the number of bytes in the buffer passed.
  12. */
  13. #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
  14. /*
  15. * If the subprocessor function returns with an indication that it wants
  16. * more data, it may well be a fatal decoding problem, because the
  17. * size is constrained by the <TLV>'s L, even if the buffer size allows
  18. * reading more data.
  19. * For example, consider the buffer containing the following TLVs:
  20. * <T:5><L:1><V> <T:6>...
  21. * The TLV length clearly indicates that one byte is expected in V, but
  22. * if the V processor returns with "want more data" even if the buffer
  23. * contains way more data than the V processor have seen.
  24. */
  25. #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
  26. /*
  27. * This macro "eats" the part of the buffer which is definitely "consumed",
  28. * i.e. was correctly converted into local representation or rightfully skipped.
  29. */
  30. #undef ADVANCE
  31. #define ADVANCE(num_bytes) do { \
  32. size_t num = num_bytes; \
  33. ptr = ((const char *)ptr) + num;\
  34. size -= num; \
  35. if(ctx->left >= 0) \
  36. ctx->left -= num; \
  37. consumed_myself += num; \
  38. } while(0)
  39. /*
  40. * Switch to the next phase of parsing.
  41. */
  42. #undef NEXT_PHASE
  43. #define NEXT_PHASE(ctx) do { \
  44. ctx->phase++; \
  45. ctx->step = 0; \
  46. } while(0)
  47. /*
  48. * Return a standardized complex structure.
  49. */
  50. #undef RETURN
  51. #define RETURN(_code) do { \
  52. rval.code = _code; \
  53. rval.consumed = consumed_myself;\
  54. return rval; \
  55. } while(0)
  56. /*
  57. * See the definitions.
  58. */
  59. static unsigned _fetch_present_idx(const void *struct_ptr, unsigned off,
  60. unsigned size);
  61. static void _set_present_idx(void *sptr, unsigned offset, unsigned size,
  62. unsigned pres);
  63. static const void *_get_member_ptr(const asn_TYPE_descriptor_t *,
  64. const void *sptr, asn_TYPE_member_t **elm,
  65. unsigned *present);
  66. /*
  67. * Tags are canonically sorted in the tag to member table.
  68. */
  69. static int
  70. _search4tag(const void *ap, const void *bp) {
  71. const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
  72. const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
  73. int a_class = BER_TAG_CLASS(a->el_tag);
  74. int b_class = BER_TAG_CLASS(b->el_tag);
  75. if(a_class == b_class) {
  76. ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
  77. ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
  78. if(a_value == b_value)
  79. return 0;
  80. else if(a_value < b_value)
  81. return -1;
  82. else
  83. return 1;
  84. } else if(a_class < b_class) {
  85. return -1;
  86. } else {
  87. return 1;
  88. }
  89. }
  90. /*
  91. * The decoder of the CHOICE type.
  92. */
  93. asn_dec_rval_t
  94. CHOICE_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
  95. const asn_TYPE_descriptor_t *td, void **struct_ptr,
  96. const void *ptr, size_t size, int tag_mode) {
  97. /*
  98. * Bring closer parts of structure description.
  99. */
  100. const asn_CHOICE_specifics_t *specs =
  101. (const asn_CHOICE_specifics_t *)td->specifics;
  102. asn_TYPE_member_t *elements = td->elements;
  103. /*
  104. * Parts of the structure being constructed.
  105. */
  106. void *st = *struct_ptr; /* Target structure. */
  107. asn_struct_ctx_t *ctx; /* Decoder context */
  108. ber_tlv_tag_t tlv_tag; /* T from TLV */
  109. ssize_t tag_len; /* Length of TLV's T */
  110. asn_dec_rval_t rval; /* Return code from subparsers */
  111. ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
  112. ASN_DEBUG("Decoding %s as CHOICE", td->name);
  113. /*
  114. * Create the target structure if it is not present already.
  115. */
  116. if(st == 0) {
  117. st = *struct_ptr = CALLOC(1, specs->struct_size);
  118. if(st == 0) {
  119. RETURN(RC_FAIL);
  120. }
  121. }
  122. /*
  123. * Restore parsing context.
  124. */
  125. ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
  126. /*
  127. * Start to parse where left previously
  128. */
  129. switch(ctx->phase) {
  130. case 0:
  131. /*
  132. * PHASE 0.
  133. * Check that the set of tags associated with given structure
  134. * perfectly fits our expectations.
  135. */
  136. if(tag_mode || td->tags_count) {
  137. rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
  138. tag_mode, -1, &ctx->left, 0);
  139. if(rval.code != RC_OK) {
  140. ASN_DEBUG("%s tagging check failed: %d",
  141. td->name, rval.code);
  142. return rval;
  143. }
  144. if(ctx->left >= 0) {
  145. /* ?Substracted below! */
  146. ctx->left += rval.consumed;
  147. }
  148. ADVANCE(rval.consumed);
  149. } else {
  150. ctx->left = -1;
  151. }
  152. NEXT_PHASE(ctx);
  153. ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
  154. (long)ctx->left, (long)size);
  155. /* Fall through */
  156. case 1:
  157. /*
  158. * Fetch the T from TLV.
  159. */
  160. tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
  161. ASN_DEBUG("In %s CHOICE tag length %d", td->name, (int)tag_len);
  162. switch(tag_len) {
  163. case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
  164. /* Fall through */
  165. case -1: RETURN(RC_FAIL);
  166. }
  167. do {
  168. const asn_TYPE_tag2member_t *t2m;
  169. asn_TYPE_tag2member_t key;
  170. key.el_tag = tlv_tag;
  171. t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
  172. specs->tag2el, specs->tag2el_count,
  173. sizeof(specs->tag2el[0]), _search4tag);
  174. if(t2m) {
  175. /*
  176. * Found the element corresponding to the tag.
  177. */
  178. NEXT_PHASE(ctx);
  179. ctx->step = t2m->el_no;
  180. break;
  181. } else if(specs->ext_start == -1) {
  182. ASN_DEBUG("Unexpected tag %s "
  183. "in non-extensible CHOICE %s",
  184. ber_tlv_tag_string(tlv_tag), td->name);
  185. RETURN(RC_FAIL);
  186. } else {
  187. /* Skip this tag */
  188. ssize_t skip;
  189. ASN_DEBUG("Skipping unknown tag %s",
  190. ber_tlv_tag_string(tlv_tag));
  191. skip = ber_skip_length(opt_codec_ctx,
  192. BER_TLV_CONSTRUCTED(ptr),
  193. (const char *)ptr + tag_len,
  194. LEFT - tag_len);
  195. switch(skip) {
  196. case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
  197. /* Fall through */
  198. case -1: RETURN(RC_FAIL);
  199. }
  200. ADVANCE(skip + tag_len);
  201. RETURN(RC_OK);
  202. }
  203. } while(0);
  204. case 2:
  205. /*
  206. * PHASE 2.
  207. * Read in the element.
  208. */
  209. do {
  210. asn_TYPE_member_t *elm;/* CHOICE's element */
  211. void *memb_ptr; /* Pointer to the member */
  212. void **memb_ptr2; /* Pointer to that pointer */
  213. elm = &elements[ctx->step];
  214. /*
  215. * Compute the position of the member inside a structure,
  216. * and also a type of containment (it may be contained
  217. * as pointer or using inline inclusion).
  218. */
  219. if(elm->flags & ATF_POINTER) {
  220. /* Member is a pointer to another structure */
  221. memb_ptr2 = (void **)((char *)st + elm->memb_offset);
  222. } else {
  223. /*
  224. * A pointer to a pointer
  225. * holding the start of the structure
  226. */
  227. memb_ptr = (char *)st + elm->memb_offset;
  228. memb_ptr2 = &memb_ptr;
  229. }
  230. /* Set presence to be able to free it properly at any time */
  231. _set_present_idx(st, specs->pres_offset,
  232. specs->pres_size, ctx->step + 1);
  233. /*
  234. * Invoke the member fetch routine according to member's type
  235. */
  236. rval = elm->type->op->ber_decoder(opt_codec_ctx, elm->type,
  237. memb_ptr2, ptr, LEFT, elm->tag_mode);
  238. switch(rval.code) {
  239. case RC_OK:
  240. break;
  241. case RC_WMORE: /* More data expected */
  242. if(!SIZE_VIOLATION) {
  243. ADVANCE(rval.consumed);
  244. RETURN(RC_WMORE);
  245. }
  246. RETURN(RC_FAIL);
  247. case RC_FAIL: /* Fatal error */
  248. RETURN(rval.code);
  249. } /* switch(rval) */
  250. ADVANCE(rval.consumed);
  251. } while(0);
  252. NEXT_PHASE(ctx);
  253. /* Fall through */
  254. case 3:
  255. ASN_DEBUG("CHOICE %s Leftover: %ld, size = %ld, tm=%d, tc=%d",
  256. td->name, (long)ctx->left, (long)size,
  257. tag_mode, td->tags_count);
  258. if(ctx->left > 0) {
  259. /*
  260. * The type must be fully decoded
  261. * by the CHOICE member-specific decoder.
  262. */
  263. RETURN(RC_FAIL);
  264. }
  265. if(ctx->left == -1
  266. && !(tag_mode || td->tags_count)) {
  267. /*
  268. * This is an untagged CHOICE.
  269. * It doesn't contain nothing
  270. * except for the member itself, including all its tags.
  271. * The decoding is completed.
  272. */
  273. NEXT_PHASE(ctx);
  274. break;
  275. }
  276. /*
  277. * Read in the "end of data chunks"'s.
  278. */
  279. while(ctx->left < 0) {
  280. ssize_t tl;
  281. tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
  282. switch(tl) {
  283. case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
  284. /* Fall through */
  285. case -1: RETURN(RC_FAIL);
  286. }
  287. /*
  288. * Expected <0><0>...
  289. */
  290. if(((const uint8_t *)ptr)[0] == 0) {
  291. if(LEFT < 2) {
  292. if(SIZE_VIOLATION)
  293. RETURN(RC_FAIL);
  294. else
  295. RETURN(RC_WMORE);
  296. } else if(((const uint8_t *)ptr)[1] == 0) {
  297. /*
  298. * Correctly finished with <0><0>.
  299. */
  300. ADVANCE(2);
  301. ctx->left++;
  302. continue;
  303. }
  304. } else {
  305. ASN_DEBUG("Unexpected continuation in %s",
  306. td->name);
  307. RETURN(RC_FAIL);
  308. }
  309. /* UNREACHABLE */
  310. }
  311. NEXT_PHASE(ctx);
  312. case 4:
  313. /* No meaningful work here */
  314. break;
  315. }
  316. RETURN(RC_OK);
  317. }
  318. asn_enc_rval_t
  319. CHOICE_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
  320. int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb,
  321. void *app_key) {
  322. const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
  323. asn_TYPE_member_t *elm; /* CHOICE element */
  324. asn_enc_rval_t erval;
  325. const void *memb_ptr;
  326. size_t computed_size = 0;
  327. unsigned present;
  328. if(!sptr) ASN__ENCODE_FAILED;
  329. ASN_DEBUG("%s %s as CHOICE",
  330. cb?"Encoding":"Estimating", td->name);
  331. present = _fetch_present_idx(sptr,
  332. specs->pres_offset, specs->pres_size);
  333. /*
  334. * If the structure was not initialized, it cannot be encoded:
  335. * can't deduce what to encode in the choice type.
  336. */
  337. if(present == 0 || present > td->elements_count) {
  338. if(present == 0 && td->elements_count == 0) {
  339. /* The CHOICE is empty?! */
  340. erval.encoded = 0;
  341. ASN__ENCODED_OK(erval);
  342. }
  343. ASN__ENCODE_FAILED;
  344. }
  345. /*
  346. * Seek over the present member of the structure.
  347. */
  348. elm = &td->elements[present-1];
  349. if(elm->flags & ATF_POINTER) {
  350. memb_ptr =
  351. *(const void *const *)((const char *)sptr + elm->memb_offset);
  352. if(memb_ptr == 0) {
  353. if(elm->optional) {
  354. erval.encoded = 0;
  355. ASN__ENCODED_OK(erval);
  356. }
  357. /* Mandatory element absent */
  358. ASN__ENCODE_FAILED;
  359. }
  360. } else {
  361. memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
  362. }
  363. /*
  364. * If the CHOICE itself is tagged EXPLICIT:
  365. * T ::= [2] EXPLICIT CHOICE { ... }
  366. * Then emit the appropriate tags.
  367. */
  368. if(tag_mode == 1 || td->tags_count) {
  369. /*
  370. * For this, we need to pre-compute the member.
  371. */
  372. ssize_t ret;
  373. /* Encode member with its tag */
  374. erval = elm->type->op->der_encoder(elm->type, memb_ptr,
  375. elm->tag_mode, elm->tag, 0, 0);
  376. if(erval.encoded == -1)
  377. return erval;
  378. /* Encode CHOICE with parent or my own tag */
  379. ret = der_write_tags(td, erval.encoded, tag_mode, 1, tag,
  380. cb, app_key);
  381. if(ret == -1)
  382. ASN__ENCODE_FAILED;
  383. computed_size += ret;
  384. }
  385. /*
  386. * Encode the single underlying member.
  387. */
  388. erval = elm->type->op->der_encoder(elm->type, memb_ptr,
  389. elm->tag_mode, elm->tag, cb, app_key);
  390. if(erval.encoded == -1)
  391. return erval;
  392. ASN_DEBUG("Encoded CHOICE member in %ld bytes (+%ld)",
  393. (long)erval.encoded, (long)computed_size);
  394. erval.encoded += computed_size;
  395. return erval;
  396. }
  397. ber_tlv_tag_t
  398. CHOICE_outmost_tag(const asn_TYPE_descriptor_t *td, const void *ptr, int tag_mode, ber_tlv_tag_t tag) {
  399. const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
  400. unsigned present;
  401. assert(tag_mode == 0); (void)tag_mode;
  402. assert(tag == 0); (void)tag;
  403. /*
  404. * Figure out which CHOICE element is encoded.
  405. */
  406. present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
  407. if(present > 0 && present <= td->elements_count) {
  408. const asn_TYPE_member_t *elm = &td->elements[present-1];
  409. const void *memb_ptr;
  410. if(elm->flags & ATF_POINTER) {
  411. memb_ptr = *(const void * const *)
  412. ((const char *)ptr + elm->memb_offset);
  413. } else {
  414. memb_ptr = (const void *)
  415. ((const char *)ptr + elm->memb_offset);
  416. }
  417. return asn_TYPE_outmost_tag(elm->type, memb_ptr,
  418. elm->tag_mode, elm->tag);
  419. } else {
  420. return (ber_tlv_tag_t)-1;
  421. }
  422. }
  423. int
  424. CHOICE_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
  425. asn_app_constraint_failed_f *ctfailcb, void *app_key) {
  426. const asn_CHOICE_specifics_t *specs =
  427. (const asn_CHOICE_specifics_t *)td->specifics;
  428. unsigned present;
  429. if(!sptr) {
  430. ASN__CTFAIL(app_key, td, sptr,
  431. "%s: value not given (%s:%d)",
  432. td->name, __FILE__, __LINE__);
  433. return -1;
  434. }
  435. /*
  436. * Figure out which CHOICE element is encoded.
  437. */
  438. present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
  439. if(present > 0 && present <= td->elements_count) {
  440. asn_TYPE_member_t *elm = &td->elements[present-1];
  441. const void *memb_ptr;
  442. if(elm->flags & ATF_POINTER) {
  443. memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
  444. if(!memb_ptr) {
  445. if(elm->optional)
  446. return 0;
  447. ASN__CTFAIL(app_key, td, sptr,
  448. "%s: mandatory CHOICE element %s absent (%s:%d)",
  449. td->name, elm->name, __FILE__, __LINE__);
  450. return -1;
  451. }
  452. } else {
  453. memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
  454. }
  455. if(elm->encoding_constraints.general_constraints) {
  456. return elm->encoding_constraints.general_constraints(elm->type, memb_ptr,
  457. ctfailcb, app_key);
  458. } else {
  459. return elm->type->encoding_constraints.general_constraints(elm->type,
  460. memb_ptr, ctfailcb, app_key);
  461. }
  462. } else {
  463. ASN__CTFAIL(app_key, td, sptr,
  464. "%s: no CHOICE element given (%s:%d)",
  465. td->name, __FILE__, __LINE__);
  466. return -1;
  467. }
  468. }
  469. #undef XER_ADVANCE
  470. #define XER_ADVANCE(num_bytes) do { \
  471. size_t num = num_bytes; \
  472. buf_ptr = (const void *)(((const char *)buf_ptr) + num); \
  473. size -= num; \
  474. consumed_myself += num; \
  475. } while(0)
  476. /*
  477. * Decode the XER (XML) data.
  478. */
  479. asn_dec_rval_t
  480. CHOICE_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
  481. const asn_TYPE_descriptor_t *td, void **struct_ptr,
  482. const char *opt_mname, const void *buf_ptr, size_t size) {
  483. /*
  484. * Bring closer parts of structure description.
  485. */
  486. const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
  487. const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
  488. /*
  489. * Parts of the structure being constructed.
  490. */
  491. void *st = *struct_ptr; /* Target structure. */
  492. asn_struct_ctx_t *ctx; /* Decoder context */
  493. asn_dec_rval_t rval; /* Return value of a decoder */
  494. ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
  495. size_t edx; /* Element index */
  496. /*
  497. * Create the target structure if it is not present already.
  498. */
  499. if(st == 0) {
  500. st = *struct_ptr = CALLOC(1, specs->struct_size);
  501. if(st == 0) RETURN(RC_FAIL);
  502. }
  503. /*
  504. * Restore parsing context.
  505. */
  506. ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
  507. if(ctx->phase == 0 && !*xml_tag)
  508. ctx->phase = 1; /* Skip the outer tag checking phase */
  509. /*
  510. * Phases of XER/XML processing:
  511. * Phase 0: Check that the opening tag matches our expectations.
  512. * Phase 1: Processing body and reacting on closing tag.
  513. * Phase 2: Processing inner type.
  514. * Phase 3: Only waiting for closing tag.
  515. * Phase 4: Skipping unknown extensions.
  516. * Phase 5: PHASED OUT
  517. */
  518. for(edx = ctx->step; ctx->phase <= 4;) {
  519. pxer_chunk_type_e ch_type; /* XER chunk type */
  520. ssize_t ch_size; /* Chunk size */
  521. xer_check_tag_e tcv; /* Tag check value */
  522. asn_TYPE_member_t *elm;
  523. /*
  524. * Go inside the member.
  525. */
  526. if(ctx->phase == 2) {
  527. asn_dec_rval_t tmprval;
  528. void *memb_ptr; /* Pointer to the member */
  529. void **memb_ptr2; /* Pointer to that pointer */
  530. unsigned old_present;
  531. elm = &td->elements[edx];
  532. if(elm->flags & ATF_POINTER) {
  533. /* Member is a pointer to another structure */
  534. memb_ptr2 = (void **)((char *)st
  535. + elm->memb_offset);
  536. } else {
  537. memb_ptr = (char *)st + elm->memb_offset;
  538. memb_ptr2 = &memb_ptr;
  539. }
  540. /* Start/Continue decoding the inner member */
  541. tmprval = elm->type->op->xer_decoder(opt_codec_ctx,
  542. elm->type, memb_ptr2, elm->name,
  543. buf_ptr, size);
  544. XER_ADVANCE(tmprval.consumed);
  545. ASN_DEBUG("XER/CHOICE: itdf: [%s] code=%d",
  546. elm->type->name, tmprval.code);
  547. old_present = _fetch_present_idx(st,
  548. specs->pres_offset, specs->pres_size);
  549. assert(old_present == 0 || old_present == edx + 1);
  550. /* Record what we've got */
  551. _set_present_idx(st,
  552. specs->pres_offset, specs->pres_size, edx + 1);
  553. if(tmprval.code != RC_OK)
  554. RETURN(tmprval.code);
  555. ctx->phase = 3;
  556. /* Fall through */
  557. }
  558. /* No need to wait for closing tag; special mode. */
  559. if(ctx->phase == 3 && !*xml_tag) {
  560. ctx->phase = 5; /* Phase out */
  561. RETURN(RC_OK);
  562. }
  563. /*
  564. * Get the next part of the XML stream.
  565. */
  566. ch_size = xer_next_token(&ctx->context, buf_ptr, size, &ch_type);
  567. if(ch_size == -1) {
  568. RETURN(RC_FAIL);
  569. } else {
  570. switch(ch_type) {
  571. case PXER_WMORE:
  572. RETURN(RC_WMORE);
  573. case PXER_COMMENT: /* Got XML comment */
  574. case PXER_TEXT: /* Ignore free-standing text */
  575. XER_ADVANCE(ch_size); /* Skip silently */
  576. continue;
  577. case PXER_TAG:
  578. break; /* Check the rest down there */
  579. }
  580. }
  581. tcv = xer_check_tag(buf_ptr, ch_size, xml_tag);
  582. ASN_DEBUG("XER/CHOICE checked [%c%c%c%c] vs [%s], tcv=%d",
  583. ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
  584. ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
  585. ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
  586. ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
  587. xml_tag, tcv);
  588. /* Skip the extensions section */
  589. if(ctx->phase == 4) {
  590. ASN_DEBUG("skip_unknown(%d, %ld)",
  591. tcv, (long)ctx->left);
  592. switch(xer_skip_unknown(tcv, &ctx->left)) {
  593. case -1:
  594. ctx->phase = 5;
  595. RETURN(RC_FAIL);
  596. case 1:
  597. ctx->phase = 3;
  598. /* Fall through */
  599. case 0:
  600. XER_ADVANCE(ch_size);
  601. continue;
  602. case 2:
  603. ctx->phase = 3;
  604. break;
  605. }
  606. }
  607. switch(tcv) {
  608. case XCT_BOTH:
  609. break; /* No CHOICE? */
  610. case XCT_CLOSING:
  611. if(ctx->phase != 3)
  612. break;
  613. XER_ADVANCE(ch_size);
  614. ctx->phase = 5; /* Phase out */
  615. RETURN(RC_OK);
  616. case XCT_OPENING:
  617. if(ctx->phase == 0) {
  618. XER_ADVANCE(ch_size);
  619. ctx->phase = 1; /* Processing body phase */
  620. continue;
  621. }
  622. /* Fall through */
  623. case XCT_UNKNOWN_OP:
  624. case XCT_UNKNOWN_BO:
  625. if(ctx->phase != 1)
  626. break; /* Really unexpected */
  627. /*
  628. * Search which inner member corresponds to this tag.
  629. */
  630. for(edx = 0; edx < td->elements_count; edx++) {
  631. elm = &td->elements[edx];
  632. tcv = xer_check_tag(buf_ptr,ch_size,elm->name);
  633. switch(tcv) {
  634. case XCT_BOTH:
  635. case XCT_OPENING:
  636. /*
  637. * Process this member.
  638. */
  639. ctx->step = edx;
  640. ctx->phase = 2;
  641. break;
  642. case XCT_UNKNOWN_OP:
  643. case XCT_UNKNOWN_BO:
  644. continue;
  645. default:
  646. edx = td->elements_count;
  647. break; /* Phase out */
  648. }
  649. break;
  650. }
  651. if(edx != td->elements_count)
  652. continue;
  653. /* It is expected extension */
  654. if(specs->ext_start != -1) {
  655. ASN_DEBUG("Got anticipated extension");
  656. /*
  657. * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
  658. * By using a mask. Only record a pure
  659. * <opening> tags.
  660. */
  661. if(tcv & XCT_CLOSING) {
  662. /* Found </extension> without body */
  663. ctx->phase = 3; /* Terminating */
  664. } else {
  665. ctx->left = 1;
  666. ctx->phase = 4; /* Skip ...'s */
  667. }
  668. XER_ADVANCE(ch_size);
  669. continue;
  670. }
  671. /* Fall through */
  672. default:
  673. break;
  674. }
  675. ASN_DEBUG("Unexpected XML tag [%c%c%c%c] in CHOICE [%s]"
  676. " (ph=%d, tag=%s)",
  677. ch_size>0?((const uint8_t *)buf_ptr)[0]:'?',
  678. ch_size>1?((const uint8_t *)buf_ptr)[1]:'?',
  679. ch_size>2?((const uint8_t *)buf_ptr)[2]:'?',
  680. ch_size>3?((const uint8_t *)buf_ptr)[3]:'?',
  681. td->name, ctx->phase, xml_tag);
  682. break;
  683. }
  684. ctx->phase = 5; /* Phase out, just in case */
  685. RETURN(RC_FAIL);
  686. }
  687. asn_enc_rval_t
  688. CHOICE_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
  689. enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb,
  690. void *app_key) {
  691. const asn_CHOICE_specifics_t *specs =
  692. (const asn_CHOICE_specifics_t *)td->specifics;
  693. asn_enc_rval_t er;
  694. unsigned present;
  695. if(!sptr)
  696. ASN__ENCODE_FAILED;
  697. /*
  698. * Figure out which CHOICE element is encoded.
  699. */
  700. present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
  701. if(present == 0 || present > td->elements_count) {
  702. ASN__ENCODE_FAILED;
  703. } else {
  704. asn_enc_rval_t tmper;
  705. asn_TYPE_member_t *elm = &td->elements[present-1];
  706. const void *memb_ptr;
  707. const char *mname = elm->name;
  708. unsigned int mlen = strlen(mname);
  709. if(elm->flags & ATF_POINTER) {
  710. memb_ptr =
  711. *(const void *const *)((const char *)sptr + elm->memb_offset);
  712. if(!memb_ptr) ASN__ENCODE_FAILED;
  713. } else {
  714. memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
  715. }
  716. er.encoded = 0;
  717. if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel);
  718. ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
  719. tmper = elm->type->op->xer_encoder(elm->type, memb_ptr,
  720. ilevel + 1, flags, cb, app_key);
  721. if(tmper.encoded == -1) return tmper;
  722. er.encoded += tmper.encoded;
  723. ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
  724. }
  725. if(!(flags & XER_F_CANONICAL)) ASN__TEXT_INDENT(1, ilevel - 1);
  726. ASN__ENCODED_OK(er);
  727. cb_failed:
  728. ASN__ENCODE_FAILED;
  729. }
  730. asn_dec_rval_t
  731. CHOICE_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
  732. const asn_TYPE_descriptor_t *td,
  733. const asn_per_constraints_t *constraints, void **sptr,
  734. asn_per_data_t *pd) {
  735. const asn_CHOICE_specifics_t *specs =
  736. (const asn_CHOICE_specifics_t *)td->specifics;
  737. asn_dec_rval_t rv;
  738. const asn_per_constraint_t *ct;
  739. asn_TYPE_member_t *elm; /* CHOICE's element */
  740. void *memb_ptr;
  741. void **memb_ptr2;
  742. void *st = *sptr;
  743. int value;
  744. if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
  745. ASN__DECODE_FAILED;
  746. /*
  747. * Create the target structure if it is not present already.
  748. */
  749. if(!st) {
  750. st = *sptr = CALLOC(1, specs->struct_size);
  751. if(!st) ASN__DECODE_FAILED;
  752. }
  753. if(constraints) ct = &constraints->value;
  754. else if(td->encoding_constraints.per_constraints) ct = &td->encoding_constraints.per_constraints->value;
  755. else ct = 0;
  756. if(ct && ct->flags & APC_EXTENSIBLE) {
  757. value = per_get_few_bits(pd, 1);
  758. if(value < 0) ASN__DECODE_STARVED;
  759. if(value) ct = 0; /* Not restricted */
  760. }
  761. if(ct && ct->range_bits >= 0) {
  762. value = per_get_few_bits(pd, ct->range_bits);
  763. if(value < 0) ASN__DECODE_STARVED;
  764. ASN_DEBUG("CHOICE %s got index %d in range %d",
  765. td->name, value, ct->range_bits);
  766. if(value > ct->upper_bound)
  767. ASN__DECODE_FAILED;
  768. } else {
  769. if(specs->ext_start == -1)
  770. ASN__DECODE_FAILED;
  771. value = uper_get_nsnnwn(pd);
  772. if(value < 0) ASN__DECODE_STARVED;
  773. value += specs->ext_start;
  774. if((unsigned)value >= td->elements_count)
  775. ASN__DECODE_FAILED;
  776. }
  777. /* Adjust if canonical order is different from natural order */
  778. if(specs->from_canonical_order) {
  779. ASN_DEBUG("CHOICE presence from wire %d", value);
  780. value = specs->from_canonical_order[value];
  781. ASN_DEBUG("CHOICE presence index effective %d", value);
  782. }
  783. /* Set presence to be able to free it later */
  784. _set_present_idx(st, specs->pres_offset, specs->pres_size, value + 1);
  785. elm = &td->elements[value];
  786. if(elm->flags & ATF_POINTER) {
  787. /* Member is a pointer to another structure */
  788. memb_ptr2 = (void **)((char *)st + elm->memb_offset);
  789. } else {
  790. memb_ptr = (char *)st + elm->memb_offset;
  791. memb_ptr2 = &memb_ptr;
  792. }
  793. ASN_DEBUG("Discovered CHOICE %s encodes %s", td->name, elm->name);
  794. if(ct && ct->range_bits >= 0) {
  795. rv = elm->type->op->uper_decoder(opt_codec_ctx, elm->type,
  796. elm->encoding_constraints.per_constraints, memb_ptr2, pd);
  797. } else {
  798. rv = uper_open_type_get(opt_codec_ctx, elm->type,
  799. elm->encoding_constraints.per_constraints, memb_ptr2, pd);
  800. }
  801. if(rv.code != RC_OK)
  802. ASN_DEBUG("Failed to decode %s in %s (CHOICE) %d",
  803. elm->name, td->name, rv.code);
  804. return rv;
  805. }
  806. asn_enc_rval_t
  807. CHOICE_encode_uper(const asn_TYPE_descriptor_t *td,
  808. const asn_per_constraints_t *constraints, const void *sptr,
  809. asn_per_outp_t *po) {
  810. const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
  811. asn_TYPE_member_t *elm; /* CHOICE's element */
  812. const asn_per_constraint_t *ct;
  813. const void *memb_ptr;
  814. unsigned present;
  815. int present_enc;
  816. if(!sptr) ASN__ENCODE_FAILED;
  817. ASN_DEBUG("Encoding %s as CHOICE", td->name);
  818. if(constraints) ct = &constraints->value;
  819. else if(td->encoding_constraints.per_constraints)
  820. ct = &td->encoding_constraints.per_constraints->value;
  821. else ct = 0;
  822. present = _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
  823. /*
  824. * If the structure was not initialized properly, it cannot be encoded:
  825. * can't deduce what to encode in the choice type.
  826. */
  827. if(present == 0 || present > td->elements_count)
  828. ASN__ENCODE_FAILED;
  829. else
  830. present--;
  831. ASN_DEBUG("Encoding %s CHOICE element %d", td->name, present);
  832. /* Adjust if canonical order is different from natural order */
  833. if(specs->to_canonical_order)
  834. present_enc = specs->to_canonical_order[present];
  835. else
  836. present_enc = present;
  837. if(ct && ct->range_bits >= 0) {
  838. if(present_enc < ct->lower_bound
  839. || present_enc > ct->upper_bound) {
  840. if(ct->flags & APC_EXTENSIBLE) {
  841. ASN_DEBUG(
  842. "CHOICE member %d (enc %d) is an extension (%ld..%ld)",
  843. present, present_enc, ct->lower_bound, ct->upper_bound);
  844. if(per_put_few_bits(po, 1, 1))
  845. ASN__ENCODE_FAILED;
  846. } else {
  847. ASN__ENCODE_FAILED;
  848. }
  849. ct = 0;
  850. }
  851. }
  852. if(ct && ct->flags & APC_EXTENSIBLE) {
  853. ASN_DEBUG("CHOICE member %d (enc %d) is not an extension (%ld..%ld)",
  854. present, present_enc, ct->lower_bound, ct->upper_bound);
  855. if(per_put_few_bits(po, 0, 1))
  856. ASN__ENCODE_FAILED;
  857. }
  858. elm = &td->elements[present];
  859. ASN_DEBUG("CHOICE member \"%s\" %d (as %d)", elm->name, present,
  860. present_enc);
  861. if(elm->flags & ATF_POINTER) {
  862. /* Member is a pointer to another structure */
  863. memb_ptr =
  864. *(const void *const *)((const char *)sptr + elm->memb_offset);
  865. if(!memb_ptr) ASN__ENCODE_FAILED;
  866. } else {
  867. memb_ptr = (const char *)sptr + elm->memb_offset;
  868. }
  869. if(ct && ct->range_bits >= 0) {
  870. if(per_put_few_bits(po, present_enc, ct->range_bits))
  871. ASN__ENCODE_FAILED;
  872. return elm->type->op->uper_encoder(
  873. elm->type, elm->encoding_constraints.per_constraints, memb_ptr, po);
  874. } else {
  875. asn_enc_rval_t rval;
  876. if(specs->ext_start == -1) ASN__ENCODE_FAILED;
  877. if(uper_put_nsnnwn(po, present_enc - specs->ext_start))
  878. ASN__ENCODE_FAILED;
  879. if(uper_open_type_put(elm->type,
  880. elm->encoding_constraints.per_constraints,
  881. memb_ptr, po))
  882. ASN__ENCODE_FAILED;
  883. rval.encoded = 0;
  884. ASN__ENCODED_OK(rval);
  885. }
  886. }
  887. int
  888. CHOICE_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
  889. asn_app_consume_bytes_f *cb, void *app_key) {
  890. const asn_CHOICE_specifics_t *specs = (const asn_CHOICE_specifics_t *)td->specifics;
  891. unsigned present;
  892. if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
  893. /*
  894. * Figure out which CHOICE element is encoded.
  895. */
  896. present = _fetch_present_idx(sptr, specs->pres_offset,specs->pres_size);
  897. /*
  898. * Print that element.
  899. */
  900. if(present > 0 && present <= td->elements_count) {
  901. asn_TYPE_member_t *elm = &td->elements[present-1];
  902. const void *memb_ptr;
  903. if(elm->flags & ATF_POINTER) {
  904. memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
  905. if(!memb_ptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
  906. } else {
  907. memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
  908. }
  909. /* Print member's name and stuff */
  910. if(0) {
  911. if(cb(elm->name, strlen(elm->name), app_key) < 0
  912. || cb(": ", 2, app_key) < 0)
  913. return -1;
  914. }
  915. return elm->type->op->print_struct(elm->type, memb_ptr, ilevel,
  916. cb, app_key);
  917. } else {
  918. return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
  919. }
  920. }
  921. void
  922. CHOICE_free(const asn_TYPE_descriptor_t *td, void *ptr,
  923. enum asn_struct_free_method method) {
  924. const asn_CHOICE_specifics_t *specs =
  925. (const asn_CHOICE_specifics_t *)td->specifics;
  926. unsigned present;
  927. if(!td || !ptr)
  928. return;
  929. ASN_DEBUG("Freeing %s as CHOICE", td->name);
  930. /*
  931. * Figure out which CHOICE element is encoded.
  932. */
  933. present = _fetch_present_idx(ptr, specs->pres_offset, specs->pres_size);
  934. /*
  935. * Free that element.
  936. */
  937. if(present > 0 && present <= td->elements_count) {
  938. asn_TYPE_member_t *elm = &td->elements[present-1];
  939. void *memb_ptr;
  940. if(elm->flags & ATF_POINTER) {
  941. memb_ptr = *(void **)((char *)ptr + elm->memb_offset);
  942. if(memb_ptr)
  943. ASN_STRUCT_FREE(*elm->type, memb_ptr);
  944. } else {
  945. memb_ptr = (void *)((char *)ptr + elm->memb_offset);
  946. ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
  947. }
  948. }
  949. switch(method) {
  950. case ASFM_FREE_EVERYTHING:
  951. FREEMEM(ptr);
  952. break;
  953. case ASFM_FREE_UNDERLYING:
  954. break;
  955. case ASFM_FREE_UNDERLYING_AND_RESET:
  956. memset(ptr, 0, specs->struct_size);
  957. break;
  958. }
  959. }
  960. /*
  961. * The following functions functions offer protection against -fshort-enums,
  962. * compatible with little- and big-endian machines.
  963. * If assertion is triggered, either disable -fshort-enums, or add an entry
  964. * here with the ->pres_size of your target stracture.
  965. * Unless the target structure is packed, the ".present" member
  966. * is guaranteed to be aligned properly. ASN.1 compiler itself does not
  967. * produce packed code.
  968. */
  969. static unsigned
  970. _fetch_present_idx(const void *struct_ptr, unsigned pres_offset,
  971. unsigned pres_size) {
  972. const void *present_ptr;
  973. unsigned present;
  974. present_ptr = ((const char *)struct_ptr) + pres_offset;
  975. switch(pres_size) {
  976. case sizeof(int): present = *(const unsigned int *)present_ptr; break;
  977. case sizeof(short): present = *(const unsigned short *)present_ptr; break;
  978. case sizeof(char): present = *(const unsigned char *)present_ptr; break;
  979. default:
  980. /* ANSI C mandates enum to be equivalent to integer */
  981. assert(pres_size != sizeof(int));
  982. return 0; /* If not aborted, pass back safe value */
  983. }
  984. return present;
  985. }
  986. static void
  987. _set_present_idx(void *struct_ptr, unsigned pres_offset, unsigned pres_size,
  988. unsigned present) {
  989. void *present_ptr;
  990. present_ptr = ((char *)struct_ptr) + pres_offset;
  991. switch(pres_size) {
  992. case sizeof(int): *(unsigned int *)present_ptr = present; break;
  993. case sizeof(short): *(unsigned short *)present_ptr = present; break;
  994. case sizeof(char): *(unsigned char *)present_ptr = present; break;
  995. default:
  996. /* ANSI C mandates enum to be equivalent to integer */
  997. assert(pres_size != sizeof(int));
  998. }
  999. }
  1000. static const void *
  1001. _get_member_ptr(const asn_TYPE_descriptor_t *td, const void *sptr,
  1002. asn_TYPE_member_t **elm_ptr, unsigned *present_out) {
  1003. const asn_CHOICE_specifics_t *specs =
  1004. (const asn_CHOICE_specifics_t *)td->specifics;
  1005. unsigned present;
  1006. if(!sptr) {
  1007. *elm_ptr = NULL;
  1008. *present_out = 0;
  1009. return NULL;
  1010. }
  1011. /*
  1012. * Figure out which CHOICE element is encoded.
  1013. */
  1014. present = _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
  1015. *present_out = present;
  1016. /*
  1017. * The presence index is intentionally 1-based to avoid
  1018. * treating zeroed structure as a valid one.
  1019. */
  1020. if(present > 0 && present <= td->elements_count) {
  1021. asn_TYPE_member_t *const elm = &td->elements[present - 1];
  1022. const void *memb_ptr;
  1023. if(elm->flags & ATF_POINTER) {
  1024. memb_ptr =
  1025. *(const void *const *)((const char *)sptr + elm->memb_offset);
  1026. } else {
  1027. memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
  1028. }
  1029. *elm_ptr = elm;
  1030. return memb_ptr;
  1031. } else {
  1032. *elm_ptr = NULL;
  1033. return NULL;
  1034. }
  1035. }
  1036. int
  1037. CHOICE_compare(const asn_TYPE_descriptor_t *td, const void *aptr, const void *bptr) {
  1038. asn_TYPE_member_t *aelm;
  1039. asn_TYPE_member_t *belm;
  1040. unsigned apresent = 0;
  1041. unsigned bpresent = 0;
  1042. const void *amember = _get_member_ptr(td, aptr, &aelm, &apresent);
  1043. const void *bmember = _get_member_ptr(td, bptr, &belm, &bpresent);
  1044. if(amember && bmember) {
  1045. if(apresent == bpresent) {
  1046. assert(aelm == belm);
  1047. return aelm->type->op->compare_struct(aelm->type, amember, bmember);
  1048. } else if(apresent < bpresent) {
  1049. return -1;
  1050. } else {
  1051. return 1;
  1052. }
  1053. } else if(!amember) {
  1054. return -1;
  1055. } else {
  1056. return 1;
  1057. }
  1058. }
  1059. /*
  1060. * Return the 1-based choice variant presence index.
  1061. * Returns 0 in case of error.
  1062. */
  1063. unsigned
  1064. CHOICE_variant_get_presence(const asn_TYPE_descriptor_t *td, const void *sptr) {
  1065. const asn_CHOICE_specifics_t *specs =
  1066. (const asn_CHOICE_specifics_t *)td->specifics;
  1067. return _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
  1068. }
  1069. /*
  1070. * Sets or resets the 1-based choice variant presence index.
  1071. * In case a previous index is not zero, the currently selected structure
  1072. * member is freed and zeroed-out first.
  1073. * Returns 0 on success and -1 on error.
  1074. */
  1075. int
  1076. CHOICE_variant_set_presence(const asn_TYPE_descriptor_t *td, void *sptr,
  1077. unsigned present) {
  1078. const asn_CHOICE_specifics_t *specs =
  1079. (const asn_CHOICE_specifics_t *)td->specifics;
  1080. unsigned old_present;
  1081. if(!sptr) {
  1082. return -1;
  1083. }
  1084. if(present > td->elements_count)
  1085. return -1;
  1086. old_present =
  1087. _fetch_present_idx(sptr, specs->pres_offset, specs->pres_size);
  1088. if(present == old_present)
  1089. return 0;
  1090. if(old_present != 0) {
  1091. assert(old_present <= td->elements_count);
  1092. ASN_STRUCT_RESET(*td, sptr);
  1093. }
  1094. _set_present_idx(sptr, specs->pres_offset, specs->pres_size, present);
  1095. return 0;
  1096. }
  1097. asn_random_fill_result_t
  1098. CHOICE_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
  1099. const asn_encoding_constraints_t *constr,
  1100. size_t max_length) {
  1101. const asn_CHOICE_specifics_t *specs =
  1102. (const asn_CHOICE_specifics_t *)td->specifics;
  1103. asn_random_fill_result_t res;
  1104. asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
  1105. asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
  1106. const asn_TYPE_member_t *elm;
  1107. unsigned present;
  1108. void *memb_ptr; /* Pointer to the member */
  1109. void **memb_ptr2; /* Pointer to that pointer */
  1110. void *st = *sptr;
  1111. if(max_length == 0) return result_skipped;
  1112. (void)constr;
  1113. if(st == NULL) {
  1114. st = CALLOC(1, specs->struct_size);
  1115. if(st == NULL) {
  1116. return result_failed;
  1117. }
  1118. }
  1119. present = asn_random_between(1, td->elements_count);
  1120. elm = &td->elements[present - 1];
  1121. if(elm->flags & ATF_POINTER) {
  1122. /* Member is a pointer to another structure */
  1123. memb_ptr2 = (void **)((char *)st + elm->memb_offset);
  1124. } else {
  1125. memb_ptr = (char *)st + elm->memb_offset;
  1126. memb_ptr2 = &memb_ptr;
  1127. }
  1128. res = elm->type->op->random_fill(elm->type, memb_ptr2,
  1129. &elm->encoding_constraints, max_length);
  1130. _set_present_idx(st, specs->pres_offset, specs->pres_size, present);
  1131. if(res.code == ARFILL_OK) {
  1132. *sptr = st;
  1133. } else {
  1134. if(st == *sptr) {
  1135. ASN_STRUCT_RESET(*td, st);
  1136. } else {
  1137. ASN_STRUCT_FREE(*td, st);
  1138. }
  1139. }
  1140. return res;
  1141. }
  1142. asn_TYPE_operation_t asn_OP_CHOICE = {
  1143. CHOICE_free,
  1144. CHOICE_print,
  1145. CHOICE_compare,
  1146. CHOICE_decode_ber,
  1147. CHOICE_encode_der,
  1148. CHOICE_decode_xer,
  1149. CHOICE_encode_xer,
  1150. #ifdef ASN_DISABLE_OER_SUPPORT
  1151. 0,
  1152. 0,
  1153. #else
  1154. CHOICE_decode_oer,
  1155. CHOICE_encode_oer,
  1156. #endif /* ASN_DISABLE_OER_SUPPORT */
  1157. #ifdef ASN_DISABLE_PER_SUPPORT
  1158. 0,
  1159. 0,
  1160. #else
  1161. CHOICE_decode_uper,
  1162. CHOICE_encode_uper,
  1163. #endif /* ASN_DISABLE_PER_SUPPORT */
  1164. CHOICE_random_fill,
  1165. CHOICE_outmost_tag
  1166. };