constr_CHOICE_oer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
  3. * All rights reserved.
  4. * Redistribution and modifications are permitted subject to BSD license.
  5. */
  6. #ifndef ASN_DISABLE_OER_SUPPORT
  7. #include <asn_internal.h>
  8. #include <constr_CHOICE.h>
  9. #include <errno.h>
  10. /*
  11. * Return a standardized complex structure.
  12. */
  13. #undef RETURN
  14. #define RETURN(_code) \
  15. do { \
  16. asn_dec_rval_t rval; \
  17. rval.code = _code; \
  18. rval.consumed = consumed_myself; \
  19. return rval; \
  20. } while(0)
  21. #undef ADVANCE
  22. #define ADVANCE(num_bytes) \
  23. do { \
  24. size_t num = num_bytes; \
  25. ptr = ((const char *)ptr) + num; \
  26. size -= num; \
  27. consumed_myself += num; \
  28. } while(0)
  29. /*
  30. * Switch to the next phase of parsing.
  31. */
  32. #undef NEXT_PHASE
  33. #define NEXT_PHASE(ctx) \
  34. do { \
  35. ctx->phase++; \
  36. ctx->step = 0; \
  37. } while(0)
  38. #undef SET_PHASE
  39. #define SET_PHASE(ctx, value) \
  40. do { \
  41. ctx->phase = value; \
  42. ctx->step = 0; \
  43. } while(0)
  44. /*
  45. * Tags are canonically sorted in the tag to member table.
  46. */
  47. static int
  48. _search4tag(const void *ap, const void *bp) {
  49. const asn_TYPE_tag2member_t *a = (const asn_TYPE_tag2member_t *)ap;
  50. const asn_TYPE_tag2member_t *b = (const asn_TYPE_tag2member_t *)bp;
  51. int a_class = BER_TAG_CLASS(a->el_tag);
  52. int b_class = BER_TAG_CLASS(b->el_tag);
  53. if(a_class == b_class) {
  54. ber_tlv_tag_t a_value = BER_TAG_VALUE(a->el_tag);
  55. ber_tlv_tag_t b_value = BER_TAG_VALUE(b->el_tag);
  56. if(a_value == b_value)
  57. return 0;
  58. else if(a_value < b_value)
  59. return -1;
  60. else
  61. return 1;
  62. } else if(a_class < b_class) {
  63. return -1;
  64. } else {
  65. return 1;
  66. }
  67. }
  68. /*
  69. * X.696 (08/2015) #8.7 Encoding of tags
  70. */
  71. static ssize_t
  72. oer_fetch_tag(const void *ptr, size_t size, ber_tlv_tag_t *tag_r) {
  73. ber_tlv_tag_t val;
  74. ber_tlv_tag_t tclass;
  75. size_t skipped;
  76. if(size == 0)
  77. return 0;
  78. val = *(const uint8_t *)ptr;
  79. tclass = (val >> 6);
  80. if((val & 0x3F) != 0x3F) {
  81. /* #8.7.1 */
  82. *tag_r = ((val & 0x3F) << 2) | tclass;
  83. return 1;
  84. }
  85. /*
  86. * Each octet contains 7 bits of useful information.
  87. * The MSB is 0 if it is the last octet of the tag.
  88. */
  89. for(val = 0, ptr = ((const char *)ptr) + 1, skipped = 2; skipped <= size;
  90. ptr = ((const char *)ptr) + 1, skipped++) {
  91. unsigned int oct = *(const uint8_t *)ptr;
  92. if(oct & 0x80) {
  93. val = (val << 7) | (oct & 0x7F);
  94. /*
  95. * Make sure there are at least 9 bits spare
  96. * at the MS side of a value.
  97. */
  98. if(val >> ((8 * sizeof(val)) - 9)) {
  99. /*
  100. * We would not be able to accomodate
  101. * any more tag bits.
  102. */
  103. return -1;
  104. }
  105. } else {
  106. val = (val << 7) | oct;
  107. *tag_r = (val << 2) | tclass;
  108. return skipped;
  109. }
  110. }
  111. return 0; /* Want more */
  112. }
  113. asn_dec_rval_t
  114. CHOICE_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
  115. const asn_TYPE_descriptor_t *td,
  116. const asn_oer_constraints_t *constraints, void **struct_ptr,
  117. const void *ptr, size_t size) {
  118. /*
  119. * Bring closer parts of structure description.
  120. */
  121. const asn_CHOICE_specifics_t *specs =
  122. (const asn_CHOICE_specifics_t *)td->specifics;
  123. asn_TYPE_member_t *elements = td->elements;
  124. /*
  125. * Parts of the structure being constructed.
  126. */
  127. void *st = *struct_ptr; /* Target structure. */
  128. asn_struct_ctx_t *ctx; /* Decoder context */
  129. ssize_t consumed_myself = 0; /* Consumed bytes from ptr */
  130. (void)constraints;
  131. ASN_DEBUG("Decoding %s as CHOICE", td->name);
  132. /*
  133. * Create the target structure if it is not present already.
  134. */
  135. if(st == 0) {
  136. st = *struct_ptr = CALLOC(1, specs->struct_size);
  137. if(st == 0) {
  138. RETURN(RC_FAIL);
  139. }
  140. }
  141. /*
  142. * Restore parsing context.
  143. */
  144. ctx = (asn_struct_ctx_t *)((char *)st + specs->ctx_offset);
  145. switch(ctx->phase) {
  146. case 0: {
  147. /*
  148. * Discover the tag.
  149. */
  150. ber_tlv_tag_t tlv_tag; /* T from TLV */
  151. ssize_t tag_len; /* Length of TLV's T */
  152. tag_len = oer_fetch_tag(ptr, size, &tlv_tag);
  153. switch(tag_len) {
  154. case 0:
  155. ASN__DECODE_STARVED;
  156. case -1:
  157. ASN__DECODE_FAILED;
  158. }
  159. do {
  160. const asn_TYPE_tag2member_t *t2m;
  161. asn_TYPE_tag2member_t key = {0, 0, 0, 0};
  162. key.el_tag = tlv_tag;
  163. t2m = (const asn_TYPE_tag2member_t *)bsearch(
  164. &key, specs->tag2el, specs->tag2el_count,
  165. sizeof(specs->tag2el[0]), _search4tag);
  166. if(t2m) {
  167. /*
  168. * Found the element corresponding to the tag.
  169. */
  170. NEXT_PHASE(ctx);
  171. ctx->step = t2m->el_no;
  172. break;
  173. } else if(specs->ext_start == -1) {
  174. ASN_DEBUG(
  175. "Unexpected tag %s "
  176. "in non-extensible CHOICE %s",
  177. ber_tlv_tag_string(tlv_tag), td->name);
  178. RETURN(RC_FAIL);
  179. } else {
  180. /* Skip open type extension */
  181. ASN_DEBUG(
  182. "Not implemented skipping open type extension for tag %s",
  183. ber_tlv_tag_string(tlv_tag));
  184. RETURN(RC_FAIL);
  185. }
  186. } while(0);
  187. ADVANCE(tag_len);
  188. }
  189. /* Fall through */
  190. case 1: {
  191. asn_TYPE_member_t *elm = &elements[ctx->step]; /* CHOICE's element */
  192. void *memb_ptr; /* Pointer to the member */
  193. void **memb_ptr2; /* Pointer to that pointer */
  194. asn_dec_rval_t rval;
  195. /*
  196. * Compute the position of the member inside a structure,
  197. * and also a type of containment (it may be contained
  198. * as pointer or using inline inclusion).
  199. */
  200. if(elm->flags & ATF_POINTER) {
  201. /* Member is a pointer to another structure */
  202. memb_ptr2 = (void **)((char *)st + elm->memb_offset);
  203. } else {
  204. /*
  205. * A pointer to a pointer
  206. * holding the start of the structure
  207. */
  208. memb_ptr = (char *)st + elm->memb_offset;
  209. memb_ptr2 = &memb_ptr;
  210. }
  211. /* Set presence to be able to free it properly at any time */
  212. (void)CHOICE_variant_set_presence(td, st, ctx->step + 1);
  213. if(specs->ext_start >= 0 && specs->ext_start <= ctx->step) {
  214. ssize_t got =
  215. oer_open_type_get(opt_codec_ctx, elm->type,
  216. elm->encoding_constraints.oer_constraints,
  217. memb_ptr2, ptr, size);
  218. if(got < 0) ASN__DECODE_FAILED;
  219. if(got == 0) ASN__DECODE_STARVED;
  220. rval.code = RC_OK;
  221. rval.consumed = got;
  222. } else {
  223. rval = elm->type->op->oer_decoder(
  224. opt_codec_ctx, elm->type,
  225. elm->encoding_constraints.oer_constraints, memb_ptr2, ptr,
  226. size);
  227. }
  228. rval.consumed += consumed_myself;
  229. switch(rval.code) {
  230. case RC_OK:
  231. NEXT_PHASE(ctx);
  232. case RC_WMORE:
  233. break;
  234. case RC_FAIL:
  235. SET_PHASE(ctx, 3); /* => 3 */
  236. }
  237. return rval;
  238. }
  239. case 2:
  240. /* Already decoded everything */
  241. RETURN(RC_OK);
  242. case 3:
  243. /* Failed to decode, after all */
  244. RETURN(RC_FAIL);
  245. }
  246. RETURN(RC_FAIL);
  247. }
  248. /*
  249. * X.696 (08/2015) #8.7 Encoding of tags
  250. */
  251. static ssize_t
  252. oer_put_tag(ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb, void *app_key) {
  253. uint8_t tclass = BER_TAG_CLASS(tag);
  254. ber_tlv_tag_t tval = BER_TAG_VALUE(tag);
  255. if(tval < 0x3F) {
  256. uint8_t b = (uint8_t)((tclass << 6) | tval);
  257. if(cb(&b, 1, app_key) < 0) {
  258. return -1;
  259. }
  260. return 1;
  261. } else {
  262. uint8_t buf[1 + 2 * sizeof(tval)];
  263. uint8_t *b = &buf[sizeof(buf)-1]; /* Last addressable */
  264. size_t encoded;
  265. for(; ; tval >>= 7) {
  266. if(tval >> 7) {
  267. *b-- = 0x80 | (tval & 0x7f);
  268. } else {
  269. *b-- = tval & 0x7f;
  270. break;
  271. }
  272. }
  273. *b = (uint8_t)((tclass << 6) | 0x3F);
  274. encoded = sizeof(buf) - (b - buf);
  275. if(cb(b, encoded, app_key) < 0) {
  276. return -1;
  277. }
  278. return encoded;
  279. }
  280. }
  281. /*
  282. * Encode as Canonical OER.
  283. */
  284. asn_enc_rval_t
  285. CHOICE_encode_oer(const asn_TYPE_descriptor_t *td,
  286. const asn_oer_constraints_t *constraints, const void *sptr,
  287. asn_app_consume_bytes_f *cb, void *app_key) {
  288. const asn_CHOICE_specifics_t *specs =
  289. (const asn_CHOICE_specifics_t *)td->specifics;
  290. asn_TYPE_member_t *elm; /* CHOICE element */
  291. unsigned present;
  292. const void *memb_ptr;
  293. ber_tlv_tag_t tag;
  294. ssize_t tag_len;
  295. asn_enc_rval_t er = {0, 0, 0};
  296. (void)constraints;
  297. if(!sptr) ASN__ENCODE_FAILED;
  298. ASN_DEBUG("OER %s encoding as CHOICE", td->name);
  299. present = CHOICE_variant_get_presence(td, sptr);
  300. if(present == 0 || present > td->elements_count) {
  301. ASN_DEBUG("CHOICE %s member is not selected", td->name);
  302. ASN__ENCODE_FAILED;
  303. }
  304. elm = &td->elements[present-1];
  305. if(elm->flags & ATF_POINTER) {
  306. memb_ptr =
  307. *(const void *const *)((const char *)sptr + elm->memb_offset);
  308. if(memb_ptr == 0) {
  309. /* Mandatory element absent */
  310. ASN__ENCODE_FAILED;
  311. }
  312. } else {
  313. memb_ptr = (const void *)((const char *)sptr + elm->memb_offset);
  314. }
  315. tag = asn_TYPE_outmost_tag(elm->type, memb_ptr, elm->tag_mode, elm->tag);
  316. if(tag == 0) {
  317. ASN__ENCODE_FAILED;
  318. }
  319. tag_len = oer_put_tag(tag, cb, app_key);
  320. if(tag_len < 0) {
  321. ASN__ENCODE_FAILED;
  322. }
  323. if(specs->ext_start >= 0 && (unsigned)specs->ext_start <= (present-1)) {
  324. ssize_t encoded = oer_open_type_put(elm->type,
  325. elm->encoding_constraints.oer_constraints,
  326. memb_ptr, cb, app_key);
  327. if(encoded < 0) ASN__ENCODE_FAILED;
  328. er.encoded = tag_len + encoded;
  329. } else {
  330. er = elm->type->op->oer_encoder(
  331. elm->type, elm->encoding_constraints.oer_constraints, memb_ptr, cb,
  332. app_key);
  333. if(er.encoded >= 0) er.encoded += tag_len;
  334. }
  335. return er;
  336. }
  337. #endif /* ASN_DISABLE_OER_SUPPORT */