constr_SEQUENCE.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  1. /*
  2. * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>.
  3. * All rights reserved.
  4. * Redistribution and modifications are permitted subject to BSD license.
  5. */
  6. #include <asn_internal.h>
  7. #include <constr_SEQUENCE.h>
  8. #include <OPEN_TYPE.h>
  9. #include <per_opentype.h>
  10. /*
  11. * Number of bytes left for this structure.
  12. * (ctx->left) indicates the number of bytes _transferred_ for the structure.
  13. * (size) contains the number of bytes in the buffer passed.
  14. */
  15. #define LEFT ((size<(size_t)ctx->left)?size:(size_t)ctx->left)
  16. /*
  17. * If the subprocessor function returns with an indication that it wants
  18. * more data, it may well be a fatal decoding problem, because the
  19. * size is constrained by the <TLV>'s L, even if the buffer size allows
  20. * reading more data.
  21. * For example, consider the buffer containing the following TLVs:
  22. * <T:5><L:1><V> <T:6>...
  23. * The TLV length clearly indicates that one byte is expected in V, but
  24. * if the V processor returns with "want more data" even if the buffer
  25. * contains way more data than the V processor have seen.
  26. */
  27. #define SIZE_VIOLATION (ctx->left >= 0 && (size_t)ctx->left <= size)
  28. /*
  29. * This macro "eats" the part of the buffer which is definitely "consumed",
  30. * i.e. was correctly converted into local representation or rightfully skipped.
  31. */
  32. #undef ADVANCE
  33. #define ADVANCE(num_bytes) do { \
  34. size_t num = num_bytes; \
  35. ptr = ((const char *)ptr) + num; \
  36. size -= num; \
  37. if(ctx->left >= 0) \
  38. ctx->left -= num; \
  39. consumed_myself += num; \
  40. } while(0)
  41. /*
  42. * Switch to the next phase of parsing.
  43. */
  44. #undef NEXT_PHASE
  45. #undef PHASE_OUT
  46. #define NEXT_PHASE(ctx) do { \
  47. ctx->phase++; \
  48. ctx->step = 0; \
  49. } while(0)
  50. #define PHASE_OUT(ctx) do { ctx->phase = 10; } while(0)
  51. /*
  52. * Return a standardized complex structure.
  53. */
  54. #undef RETURN
  55. #define RETURN(_code) do { \
  56. rval.code = _code; \
  57. rval.consumed = consumed_myself;\
  58. return rval; \
  59. } while(0)
  60. /*
  61. * Check whether we are inside the extensions group.
  62. */
  63. #define IN_EXTENSION_GROUP(specs, memb_idx) \
  64. ((specs)->first_extension >= 0 \
  65. && (unsigned)(specs)->first_extension <= (memb_idx))
  66. /*
  67. * Tags are canonically sorted in the tag2element map.
  68. */
  69. static int
  70. _t2e_cmp(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. if(a->el_no > b->el_no)
  80. return 1;
  81. /*
  82. * Important: we do not check
  83. * for a->el_no <= b->el_no!
  84. */
  85. return 0;
  86. } else if(a_value < b_value)
  87. return -1;
  88. else
  89. return 1;
  90. } else if(a_class < b_class) {
  91. return -1;
  92. } else {
  93. return 1;
  94. }
  95. }
  96. /*
  97. * The decoder of the SEQUENCE type.
  98. */
  99. asn_dec_rval_t
  100. SEQUENCE_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
  101. const asn_TYPE_descriptor_t *td, void **struct_ptr,
  102. const void *ptr, size_t size, int tag_mode) {
  103. /*
  104. * Bring closer parts of structure description.
  105. */
  106. const asn_SEQUENCE_specifics_t *specs = (const asn_SEQUENCE_specifics_t *)td->specifics;
  107. const asn_TYPE_member_t *elements = td->elements;
  108. /*
  109. * Parts of the structure being constructed.
  110. */
  111. void *st = *struct_ptr; /* Target structure. */
  112. asn_struct_ctx_t *ctx; /* Decoder context */
  113. ber_tlv_tag_t tlv_tag; /* T from TLV */
  114. asn_dec_rval_t rval; /* Return code from subparsers */
  115. ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
  116. size_t edx; /* SEQUENCE element's index */
  117. ASN_DEBUG("Decoding %s as SEQUENCE", td->name);
  118. /*
  119. * Create the target structure if it is not present already.
  120. */
  121. if(st == 0) {
  122. st = *struct_ptr = CALLOC(1, specs->struct_size);
  123. if(st == 0) {
  124. RETURN(RC_FAIL);
  125. }
  126. }
  127. /*
  128. * Restore parsing context.
  129. */
  130. ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
  131. /*
  132. * Start to parse where left previously
  133. */
  134. switch(ctx->phase) {
  135. case 0:
  136. /*
  137. * PHASE 0.
  138. * Check that the set of tags associated with given structure
  139. * perfectly fits our expectations.
  140. */
  141. rval = ber_check_tags(opt_codec_ctx, td, ctx, ptr, size,
  142. tag_mode, 1, &ctx->left, 0);
  143. if(rval.code != RC_OK) {
  144. ASN_DEBUG("%s tagging check failed: %d",
  145. td->name, rval.code);
  146. return rval;
  147. }
  148. if(ctx->left >= 0)
  149. ctx->left += rval.consumed; /* ?Substracted below! */
  150. ADVANCE(rval.consumed);
  151. NEXT_PHASE(ctx);
  152. ASN_DEBUG("Structure consumes %ld bytes, buffer %ld",
  153. (long)ctx->left, (long)size);
  154. /* Fall through */
  155. case 1:
  156. /*
  157. * PHASE 1.
  158. * From the place where we've left it previously,
  159. * try to decode the next member from the list of
  160. * this structure's elements.
  161. * (ctx->step) stores the member being processed
  162. * between invocations and the microphase {0,1} of parsing
  163. * that member:
  164. * step = (<member_number> * 2 + <microphase>).
  165. */
  166. for(edx = ((size_t)ctx->step >> 1); edx < td->elements_count;
  167. edx++, ctx->step = (ctx->step & ~1) + 2) {
  168. void *memb_ptr; /* Pointer to the member */
  169. void **memb_ptr2; /* Pointer to that pointer */
  170. ssize_t tag_len; /* Length of TLV's T */
  171. size_t opt_edx_end; /* Next non-optional element */
  172. size_t n;
  173. int use_bsearch;
  174. if(ctx->step & 1)
  175. goto microphase2;
  176. /*
  177. * MICROPHASE 1: Synchronize decoding.
  178. */
  179. ASN_DEBUG("In %s SEQUENCE left %d, edx=%" ASN_PRI_SIZE " flags=%d"
  180. " opt=%d ec=%d",
  181. td->name, (int)ctx->left, edx,
  182. elements[edx].flags, elements[edx].optional,
  183. td->elements_count);
  184. if(ctx->left == 0 /* No more stuff is expected */
  185. && (
  186. /* Explicit OPTIONAL specification reaches the end */
  187. (edx + elements[edx].optional == td->elements_count) ||
  188. /* All extensions are optional */
  189. IN_EXTENSION_GROUP(specs, edx))) {
  190. ASN_DEBUG("End of SEQUENCE %s", td->name);
  191. /*
  192. * Found the legitimate end of the structure.
  193. */
  194. PHASE_OUT(ctx);
  195. RETURN(RC_OK);
  196. }
  197. /*
  198. * Fetch the T from TLV.
  199. */
  200. tag_len = ber_fetch_tag(ptr, LEFT, &tlv_tag);
  201. ASN_DEBUG("Current tag in %s SEQUENCE for element %" ASN_PRI_SIZE " "
  202. "(%s) is %s encoded in %d bytes, of frame %ld",
  203. td->name, edx, elements[edx].name,
  204. ber_tlv_tag_string(tlv_tag), (int)tag_len, (long)LEFT);
  205. switch(tag_len) {
  206. case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
  207. /* Fall through */
  208. case -1: RETURN(RC_FAIL);
  209. }
  210. if(ctx->left < 0 && ((const uint8_t *)ptr)[0] == 0) {
  211. if(LEFT < 2) {
  212. if(SIZE_VIOLATION) {
  213. RETURN(RC_FAIL);
  214. } else {
  215. RETURN(RC_WMORE);
  216. }
  217. } else if(((const uint8_t *)ptr)[1] == 0) {
  218. ASN_DEBUG("edx = %" ASN_PRI_SIZE ", opt = %d, ec=%d", edx,
  219. elements[edx].optional, td->elements_count);
  220. if((edx + elements[edx].optional == td->elements_count)
  221. || IN_EXTENSION_GROUP(specs, edx)) {
  222. /*
  223. * Yeah, baby! Found the terminator
  224. * of the indefinite length structure.
  225. */
  226. /*
  227. * Proceed to the canonical
  228. * finalization function.
  229. * No advancing is necessary.
  230. */
  231. goto phase3;
  232. }
  233. }
  234. }
  235. /*
  236. * Find the next available type with this tag.
  237. */
  238. use_bsearch = 0;
  239. opt_edx_end = edx + elements[edx].optional + 1;
  240. if(opt_edx_end > td->elements_count)
  241. opt_edx_end = td->elements_count; /* Cap */
  242. else if(opt_edx_end - edx > 8) {
  243. /* Limit the scope of linear search... */
  244. opt_edx_end = edx + 8;
  245. use_bsearch = 1;
  246. /* ... and resort to bsearch() */
  247. }
  248. for(n = edx; n < opt_edx_end; n++) {
  249. if(BER_TAGS_EQUAL(tlv_tag, elements[n].tag)) {
  250. /*
  251. * Found element corresponding to the tag
  252. * being looked at.
  253. * Reposition over the right element.
  254. */
  255. edx = n;
  256. ctx->step = 1 + 2 * edx; /* Remember! */
  257. goto microphase2;
  258. } else if(elements[n].flags & ATF_ANY_TYPE) {
  259. /*
  260. * This is the ANY type, which may bear
  261. * any flag whatsoever.
  262. */
  263. edx = n;
  264. ctx->step = 1 + 2 * edx; /* Remember! */
  265. goto microphase2;
  266. } else if(elements[n].tag == (ber_tlv_tag_t)-1) {
  267. use_bsearch = 1;
  268. break;
  269. }
  270. }
  271. if(use_bsearch) {
  272. /*
  273. * Resort to a binary search over
  274. * sorted array of tags.
  275. */
  276. const asn_TYPE_tag2member_t *t2m;
  277. asn_TYPE_tag2member_t key = {0, 0, 0, 0};
  278. key.el_tag = tlv_tag;
  279. key.el_no = edx;
  280. t2m = (const asn_TYPE_tag2member_t *)bsearch(&key,
  281. specs->tag2el, specs->tag2el_count,
  282. sizeof(specs->tag2el[0]), _t2e_cmp);
  283. if(t2m) {
  284. const asn_TYPE_tag2member_t *best = 0;
  285. const asn_TYPE_tag2member_t *t2m_f, *t2m_l;
  286. size_t edx_max = edx + elements[edx].optional;
  287. /*
  288. * Rewind to the first element with that tag,
  289. * `cause bsearch() does not guarantee order.
  290. */
  291. t2m_f = t2m + t2m->toff_first;
  292. t2m_l = t2m + t2m->toff_last;
  293. for(t2m = t2m_f; t2m <= t2m_l; t2m++) {
  294. if(t2m->el_no > edx_max) break;
  295. if(t2m->el_no < edx) continue;
  296. best = t2m;
  297. }
  298. if(best) {
  299. edx = best->el_no;
  300. ctx->step = 1 + 2 * edx;
  301. goto microphase2;
  302. }
  303. }
  304. n = opt_edx_end;
  305. }
  306. if(n == opt_edx_end) {
  307. /*
  308. * If tag is unknown, it may be either
  309. * an unknown (thus, incorrect) tag,
  310. * or an extension (...),
  311. * or an end of the indefinite-length structure.
  312. */
  313. if(!IN_EXTENSION_GROUP(specs,
  314. edx + elements[edx].optional)) {
  315. ASN_DEBUG("Unexpected tag %s (at %" ASN_PRI_SIZE ")",
  316. ber_tlv_tag_string(tlv_tag), edx);
  317. ASN_DEBUG("Expected tag %s (%s)%s",
  318. ber_tlv_tag_string(elements[edx].tag),
  319. elements[edx].name,
  320. elements[edx].optional
  321. ?" or alternatives":"");
  322. RETURN(RC_FAIL);
  323. } else {
  324. /* Skip this tag */
  325. ssize_t skip;
  326. edx += elements[edx].optional;
  327. ASN_DEBUG("Skipping unexpected %s (at %" ASN_PRI_SIZE ")",
  328. ber_tlv_tag_string(tlv_tag), edx);
  329. skip = ber_skip_length(opt_codec_ctx,
  330. BER_TLV_CONSTRUCTED(ptr),
  331. (const char *)ptr + tag_len,
  332. LEFT - tag_len);
  333. ASN_DEBUG("Skip length %d in %s",
  334. (int)skip, td->name);
  335. switch(skip) {
  336. case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
  337. /* Fall through */
  338. case -1: RETURN(RC_FAIL);
  339. }
  340. ADVANCE(skip + tag_len);
  341. ctx->step -= 2;
  342. edx--;
  343. continue; /* Try again with the next tag */
  344. }
  345. }
  346. /*
  347. * MICROPHASE 2: Invoke the member-specific decoder.
  348. */
  349. ctx->step |= 1; /* Confirm entering next microphase */
  350. microphase2:
  351. ASN_DEBUG("Inside SEQUENCE %s MF2", td->name);
  352. /*
  353. * Compute the position of the member inside a structure,
  354. * and also a type of containment (it may be contained
  355. * as pointer or using inline inclusion).
  356. */
  357. if(elements[edx].flags & ATF_POINTER) {
  358. /* Member is a pointer to another structure */
  359. memb_ptr2 = (void **)((char *)st + elements[edx].memb_offset);
  360. } else {
  361. /*
  362. * A pointer to a pointer
  363. * holding the start of the structure
  364. */
  365. memb_ptr = (char *)st + elements[edx].memb_offset;
  366. memb_ptr2 = &memb_ptr;
  367. }
  368. /*
  369. * Invoke the member fetch routine according to member's type
  370. */
  371. if(elements[edx].flags & ATF_OPEN_TYPE) {
  372. rval = OPEN_TYPE_ber_get(opt_codec_ctx, td, st, &elements[edx], ptr, LEFT);
  373. } else {
  374. rval = elements[edx].type->op->ber_decoder(opt_codec_ctx,
  375. elements[edx].type,
  376. memb_ptr2, ptr, LEFT,
  377. elements[edx].tag_mode);
  378. }
  379. ASN_DEBUG("In %s SEQUENCE decoded %" ASN_PRI_SIZE " %s of %d "
  380. "in %d bytes rval.code %d, size=%d",
  381. td->name, edx, elements[edx].type->name,
  382. (int)LEFT, (int)rval.consumed, rval.code, (int)size);
  383. switch(rval.code) {
  384. case RC_OK:
  385. break;
  386. case RC_WMORE: /* More data expected */
  387. if(!SIZE_VIOLATION) {
  388. ADVANCE(rval.consumed);
  389. RETURN(RC_WMORE);
  390. }
  391. ASN_DEBUG("Size violation (c->l=%ld <= s=%ld)",
  392. (long)ctx->left, (long)size);
  393. /* Fall through */
  394. case RC_FAIL: /* Fatal error */
  395. RETURN(RC_FAIL);
  396. } /* switch(rval) */
  397. ADVANCE(rval.consumed);
  398. } /* for(all structure members) */
  399. phase3:
  400. ctx->phase = 3;
  401. /* Fall through */
  402. case 3: /* 00 and other tags expected */
  403. case 4: /* only 00's expected */
  404. ASN_DEBUG("SEQUENCE %s Leftover: %ld, size = %ld",
  405. td->name, (long)ctx->left, (long)size);
  406. /*
  407. * Skip everything until the end of the SEQUENCE.
  408. */
  409. while(ctx->left) {
  410. ssize_t tl, ll;
  411. tl = ber_fetch_tag(ptr, LEFT, &tlv_tag);
  412. switch(tl) {
  413. case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
  414. /* Fall through */
  415. case -1: RETURN(RC_FAIL);
  416. }
  417. /*
  418. * If expected <0><0>...
  419. */
  420. if(ctx->left < 0
  421. && ((const uint8_t *)ptr)[0] == 0) {
  422. if(LEFT < 2) {
  423. if(SIZE_VIOLATION)
  424. RETURN(RC_FAIL);
  425. else
  426. RETURN(RC_WMORE);
  427. } else if(((const uint8_t *)ptr)[1] == 0) {
  428. /*
  429. * Correctly finished with <0><0>.
  430. */
  431. ADVANCE(2);
  432. ctx->left++;
  433. ctx->phase = 4;
  434. continue;
  435. }
  436. }
  437. if(!IN_EXTENSION_GROUP(specs, td->elements_count)
  438. || ctx->phase == 4) {
  439. ASN_DEBUG("Unexpected continuation "
  440. "of a non-extensible type "
  441. "%s (SEQUENCE): %s",
  442. td->name,
  443. ber_tlv_tag_string(tlv_tag));
  444. RETURN(RC_FAIL);
  445. }
  446. ll = ber_skip_length(opt_codec_ctx,
  447. BER_TLV_CONSTRUCTED(ptr),
  448. (const char *)ptr + tl, LEFT - tl);
  449. switch(ll) {
  450. case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
  451. /* Fall through */
  452. case -1: RETURN(RC_FAIL);
  453. }
  454. ADVANCE(tl + ll);
  455. }
  456. PHASE_OUT(ctx);
  457. }
  458. RETURN(RC_OK);
  459. }
  460. /*
  461. * The DER encoder of the SEQUENCE type.
  462. */
  463. asn_enc_rval_t
  464. SEQUENCE_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
  465. int tag_mode, ber_tlv_tag_t tag,
  466. asn_app_consume_bytes_f *cb, void *app_key) {
  467. size_t computed_size = 0;
  468. asn_enc_rval_t erval;
  469. ssize_t ret;
  470. size_t edx;
  471. ASN_DEBUG("%s %s as SEQUENCE",
  472. cb?"Encoding":"Estimating", td->name);
  473. /*
  474. * Gather the length of the underlying members sequence.
  475. */
  476. for(edx = 0; edx < td->elements_count; edx++) {
  477. asn_TYPE_member_t *elm = &td->elements[edx];
  478. const void *memb_ptr; /* Pointer to the member */
  479. const void *const *memb_ptr2; /* Pointer to that pointer */
  480. if(elm->flags & ATF_POINTER) {
  481. memb_ptr2 =
  482. (const void *const *)((const char *)sptr + elm->memb_offset);
  483. if(!*memb_ptr2) {
  484. ASN_DEBUG("Element %s %" ASN_PRI_SIZE " not present",
  485. elm->name, edx);
  486. if(elm->optional)
  487. continue;
  488. /* Mandatory element is missing */
  489. ASN__ENCODE_FAILED;
  490. }
  491. } else {
  492. memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
  493. memb_ptr2 = &memb_ptr;
  494. }
  495. /* Eliminate default values */
  496. if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0)
  497. continue;
  498. erval = elm->type->op->der_encoder(elm->type, *memb_ptr2,
  499. elm->tag_mode, elm->tag,
  500. 0, 0);
  501. if(erval.encoded == -1)
  502. return erval;
  503. computed_size += erval.encoded;
  504. ASN_DEBUG("Member %" ASN_PRI_SIZE " %s estimated %ld bytes",
  505. edx, elm->name, (long)erval.encoded);
  506. }
  507. /*
  508. * Encode the TLV for the sequence itself.
  509. */
  510. ret = der_write_tags(td, computed_size, tag_mode, 1, tag, cb, app_key);
  511. ASN_DEBUG("Wrote tags: %ld (+%ld)", (long)ret, (long)computed_size);
  512. if(ret == -1)
  513. ASN__ENCODE_FAILED;
  514. erval.encoded = computed_size + ret;
  515. if(!cb) ASN__ENCODED_OK(erval);
  516. /*
  517. * Encode all members.
  518. */
  519. for(edx = 0; edx < td->elements_count; edx++) {
  520. asn_TYPE_member_t *elm = &td->elements[edx];
  521. asn_enc_rval_t tmperval;
  522. const void *memb_ptr; /* Pointer to the member */
  523. const void *const *memb_ptr2; /* Pointer to that pointer */
  524. if(elm->flags & ATF_POINTER) {
  525. memb_ptr2 =
  526. (const void *const *)((const char *)sptr + elm->memb_offset);
  527. if(!*memb_ptr2) continue;
  528. } else {
  529. memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
  530. memb_ptr2 = &memb_ptr;
  531. }
  532. /* Eliminate default values */
  533. if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0)
  534. continue;
  535. tmperval = elm->type->op->der_encoder(elm->type, *memb_ptr2,
  536. elm->tag_mode, elm->tag, cb, app_key);
  537. if(tmperval.encoded == -1)
  538. return tmperval;
  539. computed_size -= tmperval.encoded;
  540. ASN_DEBUG("Member %" ASN_PRI_SIZE " %s of SEQUENCE %s encoded in %ld bytes",
  541. edx, elm->name, td->name, (long)tmperval.encoded);
  542. }
  543. if(computed_size != 0)
  544. /*
  545. * Encoded size is not equal to the computed size.
  546. */
  547. ASN__ENCODE_FAILED;
  548. ASN__ENCODED_OK(erval);
  549. }
  550. #undef XER_ADVANCE
  551. #define XER_ADVANCE(num_bytes) \
  552. do { \
  553. size_t num = (num_bytes); \
  554. ptr = ((const char *)ptr) + num; \
  555. size -= num; \
  556. consumed_myself += num; \
  557. } while(0)
  558. /*
  559. * Decode the XER (XML) data.
  560. */
  561. asn_dec_rval_t
  562. SEQUENCE_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
  563. const asn_TYPE_descriptor_t *td, void **struct_ptr,
  564. const char *opt_mname, const void *ptr, size_t size) {
  565. /*
  566. * Bring closer parts of structure description.
  567. */
  568. const asn_SEQUENCE_specifics_t *specs
  569. = (const asn_SEQUENCE_specifics_t *)td->specifics;
  570. asn_TYPE_member_t *elements = td->elements;
  571. const char *xml_tag = opt_mname ? opt_mname : td->xml_tag;
  572. /*
  573. * ... and parts of the structure being constructed.
  574. */
  575. void *st = *struct_ptr; /* Target structure. */
  576. asn_struct_ctx_t *ctx; /* Decoder context */
  577. asn_dec_rval_t rval; /* Return value from a decoder */
  578. ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
  579. size_t edx; /* Element index */
  580. /*
  581. * Create the target structure if it is not present already.
  582. */
  583. if(st == 0) {
  584. st = *struct_ptr = CALLOC(1, specs->struct_size);
  585. if(st == 0) RETURN(RC_FAIL);
  586. }
  587. /*
  588. * Restore parsing context.
  589. */
  590. ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
  591. /*
  592. * Phases of XER/XML processing:
  593. * Phase 0: Check that the opening tag matches our expectations.
  594. * Phase 1: Processing body and reacting on closing tag.
  595. * Phase 2: Processing inner type.
  596. * Phase 3: Skipping unknown extensions.
  597. * Phase 4: PHASED OUT
  598. */
  599. for(edx = ctx->step; ctx->phase <= 3;) {
  600. pxer_chunk_type_e ch_type; /* XER chunk type */
  601. ssize_t ch_size; /* Chunk size */
  602. xer_check_tag_e tcv; /* Tag check value */
  603. asn_TYPE_member_t *elm;
  604. /*
  605. * Go inside the inner member of a sequence.
  606. */
  607. if(ctx->phase == 2) {
  608. asn_dec_rval_t tmprval;
  609. void *memb_ptr_dontuse; /* Pointer to the member */
  610. void **memb_ptr2; /* Pointer to that pointer */
  611. elm = &td->elements[edx];
  612. if(elm->flags & ATF_POINTER) {
  613. /* Member is a pointer to another structure */
  614. memb_ptr2 = (void **)((char *)st + elm->memb_offset);
  615. } else {
  616. memb_ptr_dontuse = (char *)st + elm->memb_offset;
  617. memb_ptr2 = &memb_ptr_dontuse; /* Only use of memb_ptr_dontuse */
  618. }
  619. if(elm->flags & ATF_OPEN_TYPE) {
  620. tmprval = OPEN_TYPE_xer_get(opt_codec_ctx, td, st, elm, ptr, size);
  621. } else {
  622. /* Invoke the inner type decoder, m.b. multiple times */
  623. tmprval = elm->type->op->xer_decoder(opt_codec_ctx,
  624. elm->type, memb_ptr2, elm->name,
  625. ptr, size);
  626. }
  627. XER_ADVANCE(tmprval.consumed);
  628. if(tmprval.code != RC_OK)
  629. RETURN(tmprval.code);
  630. ctx->phase = 1; /* Back to body processing */
  631. ctx->step = ++edx;
  632. ASN_DEBUG("XER/SEQUENCE phase => %d, step => %d",
  633. ctx->phase, ctx->step);
  634. /* Fall through */
  635. }
  636. /*
  637. * Get the next part of the XML stream.
  638. */
  639. ch_size = xer_next_token(&ctx->context, ptr, size,
  640. &ch_type);
  641. if(ch_size == -1) {
  642. RETURN(RC_FAIL);
  643. } else {
  644. switch(ch_type) {
  645. case PXER_WMORE:
  646. RETURN(RC_WMORE);
  647. case PXER_COMMENT: /* Got XML comment */
  648. case PXER_TEXT: /* Ignore free-standing text */
  649. XER_ADVANCE(ch_size); /* Skip silently */
  650. continue;
  651. case PXER_TAG:
  652. break; /* Check the rest down there */
  653. }
  654. }
  655. tcv = xer_check_tag(ptr, ch_size, xml_tag);
  656. ASN_DEBUG("XER/SEQUENCE: tcv = %d, ph=%d [%s]",
  657. tcv, ctx->phase, xml_tag);
  658. /* Skip the extensions section */
  659. if(ctx->phase == 3) {
  660. switch(xer_skip_unknown(tcv, &ctx->left)) {
  661. case -1:
  662. ctx->phase = 4;
  663. RETURN(RC_FAIL);
  664. case 0:
  665. XER_ADVANCE(ch_size);
  666. continue;
  667. case 1:
  668. XER_ADVANCE(ch_size);
  669. ctx->phase = 1;
  670. continue;
  671. case 2:
  672. ctx->phase = 1;
  673. break;
  674. }
  675. }
  676. switch(tcv) {
  677. case XCT_CLOSING:
  678. if(ctx->phase == 0) break;
  679. ctx->phase = 0;
  680. /* Fall through */
  681. case XCT_BOTH:
  682. if(ctx->phase == 0) {
  683. if(edx >= td->elements_count ||
  684. /* Explicit OPTIONAL specs reaches the end */
  685. (edx + elements[edx].optional == td->elements_count) ||
  686. /* All extensions are optional */
  687. IN_EXTENSION_GROUP(specs, edx)) {
  688. XER_ADVANCE(ch_size);
  689. ctx->phase = 4; /* Phase out */
  690. RETURN(RC_OK);
  691. } else {
  692. ASN_DEBUG("Premature end of XER SEQUENCE");
  693. RETURN(RC_FAIL);
  694. }
  695. }
  696. /* Fall through */
  697. case XCT_OPENING:
  698. if(ctx->phase == 0) {
  699. XER_ADVANCE(ch_size);
  700. ctx->phase = 1; /* Processing body phase */
  701. continue;
  702. }
  703. /* Fall through */
  704. case XCT_UNKNOWN_OP:
  705. case XCT_UNKNOWN_BO:
  706. ASN_DEBUG("XER/SEQUENCE: tcv=%d, ph=%d, edx=%" ASN_PRI_SIZE "",
  707. tcv, ctx->phase, edx);
  708. if(ctx->phase != 1) {
  709. break; /* Really unexpected */
  710. }
  711. if(edx < td->elements_count) {
  712. /*
  713. * Search which member corresponds to this tag.
  714. */
  715. size_t n;
  716. size_t edx_end = edx + elements[edx].optional + 1;
  717. if(edx_end > td->elements_count)
  718. edx_end = td->elements_count;
  719. for(n = edx; n < edx_end; n++) {
  720. elm = &td->elements[n];
  721. tcv = xer_check_tag(ptr, ch_size, elm->name);
  722. switch(tcv) {
  723. case XCT_BOTH:
  724. case XCT_OPENING:
  725. /*
  726. * Process this member.
  727. */
  728. ctx->step = edx = n;
  729. ctx->phase = 2;
  730. break;
  731. case XCT_UNKNOWN_OP:
  732. case XCT_UNKNOWN_BO:
  733. continue;
  734. default:
  735. n = edx_end;
  736. break; /* Phase out */
  737. }
  738. break;
  739. }
  740. if(n != edx_end)
  741. continue;
  742. } else {
  743. ASN_DEBUG("Out of defined members: %" ASN_PRI_SIZE "/%u",
  744. edx, td->elements_count);
  745. }
  746. /* It is expected extension */
  747. if(IN_EXTENSION_GROUP(specs,
  748. edx + (edx < td->elements_count
  749. ? elements[edx].optional : 0))) {
  750. ASN_DEBUG("Got anticipated extension at %" ASN_PRI_SIZE "",
  751. edx);
  752. /*
  753. * Check for (XCT_BOTH or XCT_UNKNOWN_BO)
  754. * By using a mask. Only record a pure
  755. * <opening> tags.
  756. */
  757. if(tcv & XCT_CLOSING) {
  758. /* Found </extension> without body */
  759. } else {
  760. ctx->left = 1;
  761. ctx->phase = 3; /* Skip ...'s */
  762. }
  763. XER_ADVANCE(ch_size);
  764. continue;
  765. }
  766. /* Fall through */
  767. default:
  768. break;
  769. }
  770. ASN_DEBUG("Unexpected XML tag in SEQUENCE [%c%c%c%c%c%c]",
  771. size>0?((const char *)ptr)[0]:'.',
  772. size>1?((const char *)ptr)[1]:'.',
  773. size>2?((const char *)ptr)[2]:'.',
  774. size>3?((const char *)ptr)[3]:'.',
  775. size>4?((const char *)ptr)[4]:'.',
  776. size>5?((const char *)ptr)[5]:'.');
  777. break;
  778. }
  779. ctx->phase = 4; /* "Phase out" on hard failure */
  780. RETURN(RC_FAIL);
  781. }
  782. asn_enc_rval_t
  783. SEQUENCE_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
  784. int ilevel, enum xer_encoder_flags_e flags,
  785. asn_app_consume_bytes_f *cb, void *app_key) {
  786. asn_enc_rval_t er;
  787. int xcan = (flags & XER_F_CANONICAL);
  788. asn_TYPE_descriptor_t *tmp_def_val_td = 0;
  789. void *tmp_def_val = 0;
  790. size_t edx;
  791. if(!sptr) ASN__ENCODE_FAILED;
  792. er.encoded = 0;
  793. for(edx = 0; edx < td->elements_count; edx++) {
  794. asn_enc_rval_t tmper;
  795. asn_TYPE_member_t *elm = &td->elements[edx];
  796. const void *memb_ptr;
  797. const char *mname = elm->name;
  798. unsigned int mlen = strlen(mname);
  799. if(elm->flags & ATF_POINTER) {
  800. memb_ptr =
  801. *(const void *const *)((const char *)sptr + elm->memb_offset);
  802. if(!memb_ptr) {
  803. assert(tmp_def_val == 0);
  804. if(elm->default_value_set) {
  805. if(elm->default_value_set(&tmp_def_val)) {
  806. ASN__ENCODE_FAILED;
  807. } else {
  808. memb_ptr = tmp_def_val;
  809. tmp_def_val_td = elm->type;
  810. }
  811. } else if(elm->optional) {
  812. continue;
  813. } else {
  814. /* Mandatory element is missing */
  815. ASN__ENCODE_FAILED;
  816. }
  817. }
  818. } else {
  819. memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
  820. }
  821. if(!xcan) ASN__TEXT_INDENT(1, ilevel);
  822. ASN__CALLBACK3("<", 1, mname, mlen, ">", 1);
  823. /* Print the member itself */
  824. tmper = elm->type->op->xer_encoder(elm->type, memb_ptr, ilevel + 1,
  825. flags, cb, app_key);
  826. if(tmp_def_val) {
  827. ASN_STRUCT_FREE(*tmp_def_val_td, tmp_def_val);
  828. tmp_def_val = 0;
  829. }
  830. if(tmper.encoded == -1) return tmper;
  831. er.encoded += tmper.encoded;
  832. ASN__CALLBACK3("</", 2, mname, mlen, ">", 1);
  833. }
  834. if(!xcan) ASN__TEXT_INDENT(1, ilevel - 1);
  835. ASN__ENCODED_OK(er);
  836. cb_failed:
  837. if(tmp_def_val) ASN_STRUCT_FREE(*tmp_def_val_td, tmp_def_val);
  838. ASN__ENCODE_FAILED;
  839. }
  840. int
  841. SEQUENCE_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
  842. asn_app_consume_bytes_f *cb, void *app_key) {
  843. size_t edx;
  844. int ret;
  845. if(!sptr) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
  846. /* Dump preamble */
  847. if(cb(td->name, strlen(td->name), app_key) < 0
  848. || cb(" ::= {", 6, app_key) < 0)
  849. return -1;
  850. for(edx = 0; edx < td->elements_count; edx++) {
  851. asn_TYPE_member_t *elm = &td->elements[edx];
  852. const void *memb_ptr;
  853. if(elm->flags & ATF_POINTER) {
  854. memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
  855. if(!memb_ptr) {
  856. if(elm->optional) continue;
  857. /* Print <absent> line */
  858. /* Fall through */
  859. }
  860. } else {
  861. memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
  862. }
  863. /* Indentation */
  864. _i_INDENT(1);
  865. /* Print the member's name and stuff */
  866. if(cb(elm->name, strlen(elm->name), app_key) < 0
  867. || cb(": ", 2, app_key) < 0)
  868. return -1;
  869. /* Print the member itself */
  870. ret = elm->type->op->print_struct(elm->type, memb_ptr, ilevel + 1,
  871. cb, app_key);
  872. if(ret) return ret;
  873. }
  874. ilevel--;
  875. _i_INDENT(1);
  876. return (cb("}", 1, app_key) < 0) ? -1 : 0;
  877. }
  878. void
  879. SEQUENCE_free(const asn_TYPE_descriptor_t *td, void *sptr,
  880. enum asn_struct_free_method method) {
  881. size_t edx;
  882. const asn_SEQUENCE_specifics_t *specs =
  883. (const asn_SEQUENCE_specifics_t *)td->specifics;
  884. asn_struct_ctx_t *ctx; /* Decoder context */
  885. if(!td || !sptr)
  886. return;
  887. ASN_DEBUG("Freeing %s as SEQUENCE", td->name);
  888. for(edx = 0; edx < td->elements_count; edx++) {
  889. asn_TYPE_member_t *elm = &td->elements[edx];
  890. void *memb_ptr;
  891. if(elm->flags & ATF_POINTER) {
  892. memb_ptr = *(void **)((char *)sptr + elm->memb_offset);
  893. if(memb_ptr)
  894. ASN_STRUCT_FREE(*elm->type, memb_ptr);
  895. } else {
  896. memb_ptr = (void *)((char *)sptr + elm->memb_offset);
  897. ASN_STRUCT_FREE_CONTENTS_ONLY(*elm->type, memb_ptr);
  898. }
  899. }
  900. /* Clean parsing context */
  901. ctx = (asn_struct_ctx_t *)((char *)sptr + specs->ctx_offset);
  902. FREEMEM(ctx->ptr);
  903. switch(method) {
  904. case ASFM_FREE_EVERYTHING:
  905. FREEMEM(sptr);
  906. break;
  907. case ASFM_FREE_UNDERLYING:
  908. break;
  909. case ASFM_FREE_UNDERLYING_AND_RESET:
  910. memset(
  911. sptr, 0,
  912. ((const asn_SEQUENCE_specifics_t *)(td->specifics))->struct_size);
  913. break;
  914. }
  915. }
  916. int
  917. SEQUENCE_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
  918. asn_app_constraint_failed_f *ctfailcb, void *app_key) {
  919. size_t edx;
  920. if(!sptr) {
  921. ASN__CTFAIL(app_key, td, sptr,
  922. "%s: value not given (%s:%d)",
  923. td->name, __FILE__, __LINE__);
  924. return -1;
  925. }
  926. /*
  927. * Iterate over structure members and check their validity.
  928. */
  929. for(edx = 0; edx < td->elements_count; edx++) {
  930. asn_TYPE_member_t *elm = &td->elements[edx];
  931. const void *memb_ptr;
  932. if(elm->flags & ATF_POINTER) {
  933. memb_ptr = *(const void * const *)((const char *)sptr + elm->memb_offset);
  934. if(!memb_ptr) {
  935. if(elm->optional)
  936. continue;
  937. ASN__CTFAIL(app_key, td, sptr,
  938. "%s: mandatory element %s absent (%s:%d)",
  939. td->name, elm->name, __FILE__, __LINE__);
  940. return -1;
  941. }
  942. } else {
  943. memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
  944. }
  945. if(elm->encoding_constraints.general_constraints) {
  946. int ret = elm->encoding_constraints.general_constraints(elm->type, memb_ptr,
  947. ctfailcb, app_key);
  948. if(ret) return ret;
  949. } else {
  950. return elm->type->encoding_constraints.general_constraints(elm->type,
  951. memb_ptr, ctfailcb, app_key);
  952. }
  953. }
  954. return 0;
  955. }
  956. #ifndef ASN_DISABLE_PER_SUPPORT
  957. asn_dec_rval_t
  958. SEQUENCE_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
  959. const asn_TYPE_descriptor_t *td,
  960. const asn_per_constraints_t *constraints, void **sptr,
  961. asn_per_data_t *pd) {
  962. const asn_SEQUENCE_specifics_t *specs = (const asn_SEQUENCE_specifics_t *)td->specifics;
  963. void *st = *sptr; /* Target structure. */
  964. int extpresent; /* Extension additions are present */
  965. uint8_t *opres; /* Presence of optional root members */
  966. asn_per_data_t opmd;
  967. asn_dec_rval_t rv;
  968. size_t edx;
  969. (void)constraints;
  970. if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx))
  971. ASN__DECODE_FAILED;
  972. if(!st) {
  973. st = *sptr = CALLOC(1, specs->struct_size);
  974. if(!st) ASN__DECODE_FAILED;
  975. }
  976. ASN_DEBUG("Decoding %s as SEQUENCE (UPER)", td->name);
  977. /* Handle extensions */
  978. if(specs->first_extension < 0) {
  979. extpresent = 0;
  980. } else {
  981. extpresent = per_get_few_bits(pd, 1);
  982. if(extpresent < 0) ASN__DECODE_STARVED;
  983. }
  984. /* Prepare a place and read-in the presence bitmap */
  985. memset(&opmd, 0, sizeof(opmd));
  986. if(specs->roms_count) {
  987. opres = (uint8_t *)MALLOC(((specs->roms_count + 7) >> 3) + 1);
  988. if(!opres) ASN__DECODE_FAILED;
  989. /* Get the presence map */
  990. if(per_get_many_bits(pd, opres, 0, specs->roms_count)) {
  991. FREEMEM(opres);
  992. ASN__DECODE_STARVED;
  993. }
  994. opmd.buffer = opres;
  995. opmd.nbits = specs->roms_count;
  996. ASN_DEBUG("Read in presence bitmap for %s of %d bits (%x..)",
  997. td->name, specs->roms_count, *opres);
  998. } else {
  999. opres = 0;
  1000. }
  1001. /*
  1002. * Get the sequence ROOT elements.
  1003. */
  1004. for(edx = 0;
  1005. edx < (specs->first_extension < 0 ? td->elements_count
  1006. : (size_t)specs->first_extension);
  1007. edx++) {
  1008. asn_TYPE_member_t *elm = &td->elements[edx];
  1009. void *memb_ptr; /* Pointer to the member */
  1010. void **memb_ptr2; /* Pointer to that pointer */
  1011. assert(!IN_EXTENSION_GROUP(specs, edx));
  1012. /* Fetch the pointer to this member */
  1013. if(elm->flags & ATF_POINTER) {
  1014. memb_ptr2 = (void **)((char *)st + elm->memb_offset);
  1015. } else {
  1016. memb_ptr = (char *)st + elm->memb_offset;
  1017. memb_ptr2 = &memb_ptr;
  1018. }
  1019. /* Deal with optionality */
  1020. if(elm->optional) {
  1021. int present = per_get_few_bits(&opmd, 1);
  1022. ASN_DEBUG("Member %s->%s is optional, p=%d (%d->%d)",
  1023. td->name, elm->name, present,
  1024. (int)opmd.nboff, (int)opmd.nbits);
  1025. if(present == 0) {
  1026. /* This element is not present */
  1027. if(elm->default_value_set) {
  1028. /* Fill-in DEFAULT */
  1029. if(elm->default_value_set(memb_ptr2)) {
  1030. FREEMEM(opres);
  1031. ASN__DECODE_FAILED;
  1032. }
  1033. ASN_DEBUG("Filled-in default");
  1034. }
  1035. /* The member is just not present */
  1036. continue;
  1037. }
  1038. /* Fall through */
  1039. }
  1040. /* Fetch the member from the stream */
  1041. ASN_DEBUG("Decoding member \"%s\" in %s", elm->name, td->name);
  1042. if(elm->flags & ATF_OPEN_TYPE) {
  1043. rv = OPEN_TYPE_uper_get(opt_codec_ctx, td, st, elm, pd);
  1044. } else {
  1045. rv = elm->type->op->uper_decoder(opt_codec_ctx, elm->type,
  1046. elm->encoding_constraints.per_constraints, memb_ptr2, pd);
  1047. }
  1048. if(rv.code != RC_OK) {
  1049. ASN_DEBUG("Failed decode %s in %s",
  1050. elm->name, td->name);
  1051. FREEMEM(opres);
  1052. return rv;
  1053. }
  1054. }
  1055. /* Optionality map is not needed anymore */
  1056. FREEMEM(opres);
  1057. /*
  1058. * Deal with extensions.
  1059. */
  1060. if(extpresent) {
  1061. ssize_t bmlength;
  1062. uint8_t *epres; /* Presence of extension members */
  1063. asn_per_data_t epmd;
  1064. bmlength = uper_get_nslength(pd);
  1065. if(bmlength < 0) ASN__DECODE_STARVED;
  1066. ASN_DEBUG("Extensions %" ASN_PRI_SSIZE " present in %s", bmlength, td->name);
  1067. epres = (uint8_t *)MALLOC((bmlength + 15) >> 3);
  1068. if(!epres) ASN__DECODE_STARVED;
  1069. /* Get the extensions map */
  1070. if(per_get_many_bits(pd, epres, 0, bmlength)) {
  1071. FREEMEM(epres);
  1072. ASN__DECODE_STARVED;
  1073. }
  1074. memset(&epmd, 0, sizeof(epmd));
  1075. epmd.buffer = epres;
  1076. epmd.nbits = bmlength;
  1077. ASN_DEBUG("Read in extensions bitmap for %s of %ld bits (%x..)",
  1078. td->name, (long)bmlength, *epres);
  1079. /* Go over extensions and read them in */
  1080. for(edx = specs->first_extension; edx < td->elements_count; edx++) {
  1081. asn_TYPE_member_t *elm = &td->elements[edx];
  1082. void *memb_ptr; /* Pointer to the member */
  1083. void **memb_ptr2; /* Pointer to that pointer */
  1084. int present;
  1085. /* Fetch the pointer to this member */
  1086. if(elm->flags & ATF_POINTER) {
  1087. memb_ptr2 = (void **)((char *)st + elm->memb_offset);
  1088. } else {
  1089. memb_ptr = (void *)((char *)st + elm->memb_offset);
  1090. memb_ptr2 = &memb_ptr;
  1091. }
  1092. present = per_get_few_bits(&epmd, 1);
  1093. if(present <= 0) {
  1094. if(present < 0) break; /* No more extensions */
  1095. continue;
  1096. }
  1097. ASN_DEBUG("Decoding member %s in %s %p", elm->name, td->name,
  1098. *memb_ptr2);
  1099. rv = uper_open_type_get(opt_codec_ctx, elm->type,
  1100. elm->encoding_constraints.per_constraints,
  1101. memb_ptr2, pd);
  1102. if(rv.code != RC_OK) {
  1103. FREEMEM(epres);
  1104. return rv;
  1105. }
  1106. }
  1107. /* Skip over overflow extensions which aren't present
  1108. * in this system's version of the protocol */
  1109. for(;;) {
  1110. ASN_DEBUG("Getting overflow extensions");
  1111. switch(per_get_few_bits(&epmd, 1)) {
  1112. case -1: break;
  1113. case 0: continue;
  1114. default:
  1115. if(uper_open_type_skip(opt_codec_ctx, pd)) {
  1116. FREEMEM(epres);
  1117. ASN__DECODE_STARVED;
  1118. }
  1119. ASN_DEBUG("Skipped overflow extension");
  1120. continue;
  1121. }
  1122. break;
  1123. }
  1124. FREEMEM(epres);
  1125. }
  1126. if(specs->first_extension >= 0) {
  1127. unsigned i;
  1128. /* Fill DEFAULT members in extensions */
  1129. for(i = specs->roms_count; i < specs->roms_count + specs->aoms_count;
  1130. i++) {
  1131. asn_TYPE_member_t *elm;
  1132. void **memb_ptr2; /* Pointer to member pointer */
  1133. edx = specs->oms[i];
  1134. elm = &td->elements[edx];
  1135. if(!elm->default_value_set) continue;
  1136. /* Fetch the pointer to this member */
  1137. if(elm->flags & ATF_POINTER) {
  1138. memb_ptr2 = (void **)((char *)st + elm->memb_offset);
  1139. if(*memb_ptr2) continue;
  1140. } else {
  1141. continue; /* Extensions are all optionals */
  1142. }
  1143. /* Set default value */
  1144. if(elm->default_value_set(memb_ptr2)) {
  1145. ASN__DECODE_FAILED;
  1146. }
  1147. }
  1148. }
  1149. rv.consumed = 0;
  1150. rv.code = RC_OK;
  1151. return rv;
  1152. }
  1153. static int
  1154. SEQUENCE__handle_extensions(const asn_TYPE_descriptor_t *td, const void *sptr,
  1155. asn_per_outp_t *po1, asn_per_outp_t *po2) {
  1156. const asn_SEQUENCE_specifics_t *specs =
  1157. (const asn_SEQUENCE_specifics_t *)td->specifics;
  1158. int exts_present = 0;
  1159. int exts_count = 0;
  1160. size_t edx;
  1161. if(specs->first_extension < 0) {
  1162. return 0;
  1163. }
  1164. /* Find out which extensions are present */
  1165. for(edx = specs->first_extension; edx < td->elements_count; edx++) {
  1166. asn_TYPE_member_t *elm = &td->elements[edx];
  1167. const void *memb_ptr; /* Pointer to the member */
  1168. const void *const *memb_ptr2; /* Pointer to that pointer */
  1169. int present;
  1170. /* Fetch the pointer to this member */
  1171. if(elm->flags & ATF_POINTER) {
  1172. memb_ptr2 =
  1173. (const void *const *)((const char *)sptr + elm->memb_offset);
  1174. present = (*memb_ptr2 != 0);
  1175. } else {
  1176. memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
  1177. memb_ptr2 = &memb_ptr;
  1178. present = 1;
  1179. }
  1180. ASN_DEBUG("checking %s:%s (@%" ASN_PRI_SIZE ") present => %d", elm->name,
  1181. elm->type->name, edx, present);
  1182. exts_count++;
  1183. exts_present += present;
  1184. /* Encode as presence marker */
  1185. if(po1 && per_put_few_bits(po1, present, 1)) {
  1186. return -1;
  1187. }
  1188. /* Encode as open type field */
  1189. if(po2 && present
  1190. && uper_open_type_put(elm->type,
  1191. elm->encoding_constraints.per_constraints,
  1192. *memb_ptr2, po2))
  1193. return -1;
  1194. }
  1195. return exts_present ? exts_count : 0;
  1196. }
  1197. asn_enc_rval_t
  1198. SEQUENCE_encode_uper(const asn_TYPE_descriptor_t *td,
  1199. const asn_per_constraints_t *constraints, const void *sptr,
  1200. asn_per_outp_t *po) {
  1201. const asn_SEQUENCE_specifics_t *specs
  1202. = (const asn_SEQUENCE_specifics_t *)td->specifics;
  1203. asn_enc_rval_t er;
  1204. int n_extensions;
  1205. size_t edx;
  1206. size_t i;
  1207. (void)constraints;
  1208. if(!sptr)
  1209. ASN__ENCODE_FAILED;
  1210. er.encoded = 0;
  1211. ASN_DEBUG("Encoding %s as SEQUENCE (UPER)", td->name);
  1212. /*
  1213. * X.691#18.1 Whether structure is extensible
  1214. * and whether to encode extensions
  1215. */
  1216. if(specs->first_extension < 0) {
  1217. n_extensions = 0; /* There are no extensions to encode */
  1218. } else {
  1219. n_extensions = SEQUENCE__handle_extensions(td, sptr, 0, 0);
  1220. if(n_extensions < 0) ASN__ENCODE_FAILED;
  1221. if(per_put_few_bits(po, n_extensions ? 1 : 0, 1)) {
  1222. ASN__ENCODE_FAILED;
  1223. }
  1224. }
  1225. /* Encode a presence bitmap */
  1226. for(i = 0; i < specs->roms_count; i++) {
  1227. asn_TYPE_member_t *elm;
  1228. const void *memb_ptr; /* Pointer to the member */
  1229. const void *const *memb_ptr2; /* Pointer to that pointer */
  1230. int present;
  1231. edx = specs->oms[i];
  1232. elm = &td->elements[edx];
  1233. /* Fetch the pointer to this member */
  1234. if(elm->flags & ATF_POINTER) {
  1235. memb_ptr2 =
  1236. (const void *const *)((const char *)sptr + elm->memb_offset);
  1237. present = (*memb_ptr2 != 0);
  1238. } else {
  1239. memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
  1240. memb_ptr2 = &memb_ptr;
  1241. present = 1;
  1242. }
  1243. /* Eliminate default values */
  1244. if(present && elm->default_value_cmp
  1245. && elm->default_value_cmp(*memb_ptr2) == 0)
  1246. present = 0;
  1247. ASN_DEBUG("Element %s %s %s->%s is %s",
  1248. elm->flags & ATF_POINTER ? "ptr" : "inline",
  1249. elm->default_value_cmp ? "def" : "wtv",
  1250. td->name, elm->name, present ? "present" : "absent");
  1251. if(per_put_few_bits(po, present, 1))
  1252. ASN__ENCODE_FAILED;
  1253. }
  1254. /*
  1255. * Encode the sequence ROOT elements.
  1256. */
  1257. ASN_DEBUG("first_extension = %d, elements = %d", specs->first_extension,
  1258. td->elements_count);
  1259. for(edx = 0;
  1260. edx < ((specs->first_extension < 0) ? td->elements_count
  1261. : (size_t)specs->first_extension);
  1262. edx++) {
  1263. asn_TYPE_member_t *elm = &td->elements[edx];
  1264. const void *memb_ptr; /* Pointer to the member */
  1265. const void *const *memb_ptr2; /* Pointer to that pointer */
  1266. ASN_DEBUG("About to encode %s", elm->type->name);
  1267. /* Fetch the pointer to this member */
  1268. if(elm->flags & ATF_POINTER) {
  1269. memb_ptr2 =
  1270. (const void *const *)((const char *)sptr + elm->memb_offset);
  1271. if(!*memb_ptr2) {
  1272. ASN_DEBUG("Element %s %" ASN_PRI_SIZE " not present",
  1273. elm->name, edx);
  1274. if(elm->optional)
  1275. continue;
  1276. /* Mandatory element is missing */
  1277. ASN__ENCODE_FAILED;
  1278. }
  1279. } else {
  1280. memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
  1281. memb_ptr2 = &memb_ptr;
  1282. }
  1283. /* Eliminate default values */
  1284. if(elm->default_value_cmp && elm->default_value_cmp(*memb_ptr2) == 0)
  1285. continue;
  1286. ASN_DEBUG("Encoding %s->%s:%s", td->name, elm->name, elm->type->name);
  1287. er = elm->type->op->uper_encoder(
  1288. elm->type, elm->encoding_constraints.per_constraints, *memb_ptr2,
  1289. po);
  1290. if(er.encoded == -1) return er;
  1291. }
  1292. /* No extensions to encode */
  1293. if(!n_extensions) ASN__ENCODED_OK(er);
  1294. ASN_DEBUG("Length of extensions %d bit-map", n_extensions);
  1295. /* #18.8. Write down the presence bit-map length. */
  1296. if(uper_put_nslength(po, n_extensions))
  1297. ASN__ENCODE_FAILED;
  1298. ASN_DEBUG("Bit-map of %d elements", n_extensions);
  1299. /* #18.7. Encoding the extensions presence bit-map. */
  1300. /* TODO: act upon NOTE in #18.7 for canonical PER */
  1301. if(SEQUENCE__handle_extensions(td, sptr, po, 0) != n_extensions)
  1302. ASN__ENCODE_FAILED;
  1303. ASN_DEBUG("Writing %d extensions", n_extensions);
  1304. /* #18.9. Encode extensions as open type fields. */
  1305. if(SEQUENCE__handle_extensions(td, sptr, 0, po) != n_extensions)
  1306. ASN__ENCODE_FAILED;
  1307. ASN__ENCODED_OK(er);
  1308. }
  1309. #endif /* ASN_DISABLE_PER_SUPPORT */
  1310. int
  1311. SEQUENCE_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
  1312. const void *bptr) {
  1313. size_t edx;
  1314. for(edx = 0; edx < td->elements_count; edx++) {
  1315. asn_TYPE_member_t *elm = &td->elements[edx];
  1316. const void *amemb;
  1317. const void *bmemb;
  1318. int ret;
  1319. if(elm->flags & ATF_POINTER) {
  1320. amemb =
  1321. *(const void *const *)((const char *)aptr + elm->memb_offset);
  1322. bmemb =
  1323. *(const void *const *)((const char *)bptr + elm->memb_offset);
  1324. if(!amemb) {
  1325. if(!bmemb) continue;
  1326. if(elm->default_value_cmp
  1327. && elm->default_value_cmp(bmemb) == 0) {
  1328. /* A is absent, but B is present and equal to DEFAULT */
  1329. continue;
  1330. }
  1331. return -1;
  1332. } else if(!bmemb) {
  1333. if(elm->default_value_cmp
  1334. && elm->default_value_cmp(amemb) == 0) {
  1335. /* B is absent, but A is present and equal to DEFAULT */
  1336. continue;
  1337. }
  1338. return 1;
  1339. }
  1340. } else {
  1341. amemb = (const void *)((const char *)aptr + elm->memb_offset);
  1342. bmemb = (const void *)((const char *)bptr + elm->memb_offset);
  1343. }
  1344. ret = elm->type->op->compare_struct(elm->type, amemb, bmemb);
  1345. if(ret != 0) return ret;
  1346. }
  1347. return 0;
  1348. }
  1349. asn_TYPE_operation_t asn_OP_SEQUENCE = {
  1350. SEQUENCE_free,
  1351. SEQUENCE_print,
  1352. SEQUENCE_compare,
  1353. SEQUENCE_decode_ber,
  1354. SEQUENCE_encode_der,
  1355. SEQUENCE_decode_xer,
  1356. SEQUENCE_encode_xer,
  1357. #ifdef ASN_DISABLE_OER_SUPPORT
  1358. 0,
  1359. 0,
  1360. #else
  1361. SEQUENCE_decode_oer,
  1362. SEQUENCE_encode_oer,
  1363. #endif /* ASN_DISABLE_OER_SUPPORT */
  1364. #ifdef ASN_DISABLE_PER_SUPPORT
  1365. 0,
  1366. 0,
  1367. #else
  1368. SEQUENCE_decode_uper,
  1369. SEQUENCE_encode_uper,
  1370. #endif /* ASN_DISABLE_PER_SUPPORT */
  1371. SEQUENCE_random_fill,
  1372. 0 /* Use generic outmost tag fetcher */
  1373. };
  1374. asn_random_fill_result_t
  1375. SEQUENCE_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
  1376. const asn_encoding_constraints_t *constr,
  1377. size_t max_length) {
  1378. const asn_SEQUENCE_specifics_t *specs =
  1379. (const asn_SEQUENCE_specifics_t *)td->specifics;
  1380. asn_random_fill_result_t result_ok = {ARFILL_OK, 0};
  1381. asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
  1382. asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
  1383. void *st = *sptr;
  1384. size_t edx;
  1385. if(max_length == 0) return result_skipped;
  1386. (void)constr;
  1387. if(st == NULL) {
  1388. st = CALLOC(1, specs->struct_size);
  1389. if(st == NULL) {
  1390. return result_failed;
  1391. }
  1392. }
  1393. for(edx = 0; edx < td->elements_count; edx++) {
  1394. const asn_TYPE_member_t *elm = &td->elements[edx];
  1395. void *memb_ptr; /* Pointer to the member */
  1396. void **memb_ptr2; /* Pointer to that pointer */
  1397. asn_random_fill_result_t tmpres;
  1398. if(elm->optional && asn_random_between(0, 4) == 2) {
  1399. /* Sometimes decide not to fill the optional value */
  1400. continue;
  1401. }
  1402. if(elm->flags & ATF_POINTER) {
  1403. /* Member is a pointer to another structure */
  1404. memb_ptr2 = (void **)((char *)st + elm->memb_offset);
  1405. } else {
  1406. memb_ptr = (char *)st + elm->memb_offset;
  1407. memb_ptr2 = &memb_ptr;
  1408. }
  1409. tmpres = elm->type->op->random_fill(
  1410. elm->type, memb_ptr2, &elm->encoding_constraints,
  1411. max_length > result_ok.length ? max_length - result_ok.length : 0);
  1412. switch(tmpres.code) {
  1413. case ARFILL_OK:
  1414. result_ok.length += tmpres.length;
  1415. continue;
  1416. case ARFILL_SKIPPED:
  1417. assert(!(elm->flags & ATF_POINTER) || *memb_ptr2 == NULL);
  1418. continue;
  1419. case ARFILL_FAILED:
  1420. if(st == *sptr) {
  1421. ASN_STRUCT_RESET(*td, st);
  1422. } else {
  1423. ASN_STRUCT_FREE(*td, st);
  1424. }
  1425. return tmpres;
  1426. }
  1427. }
  1428. *sptr = st;
  1429. return result_ok;
  1430. }