NULL.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*-
  2. * Copyright (c) 2003, 2005 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 <asn_codecs_prim.h>
  7. #include <NULL.h>
  8. /*
  9. * NULL basic type description.
  10. */
  11. static const ber_tlv_tag_t asn_DEF_NULL_tags[] = {
  12. (ASN_TAG_CLASS_UNIVERSAL | (5 << 2))
  13. };
  14. asn_TYPE_operation_t asn_OP_NULL = {
  15. NULL_free,
  16. NULL_print,
  17. NULL_compare,
  18. NULL_decode_ber,
  19. NULL_encode_der, /* Special handling of DER encoding */
  20. NULL_decode_xer,
  21. NULL_encode_xer,
  22. #ifdef ASN_DISABLE_OER_SUPPORT
  23. 0,
  24. 0,
  25. #else
  26. NULL_decode_oer,
  27. NULL_encode_oer,
  28. #endif /* ASN_DISABLE_OER_SUPPORT */
  29. #ifdef ASN_DISABLE_PER_SUPPORT
  30. 0,
  31. 0,
  32. #else
  33. NULL_decode_uper, /* Unaligned PER decoder */
  34. NULL_encode_uper, /* Unaligned PER encoder */
  35. #endif /* ASN_DISABLE_PER_SUPPORT */
  36. NULL_random_fill,
  37. 0 /* Use generic outmost tag fetcher */
  38. };
  39. asn_TYPE_descriptor_t asn_DEF_NULL = {
  40. "NULL",
  41. "NULL",
  42. &asn_OP_NULL,
  43. asn_DEF_NULL_tags,
  44. sizeof(asn_DEF_NULL_tags) / sizeof(asn_DEF_NULL_tags[0]),
  45. asn_DEF_NULL_tags, /* Same as above */
  46. sizeof(asn_DEF_NULL_tags) / sizeof(asn_DEF_NULL_tags[0]),
  47. { 0, 0, asn_generic_no_constraint },
  48. 0, 0, /* No members */
  49. 0 /* No specifics */
  50. };
  51. void
  52. NULL_free(const asn_TYPE_descriptor_t *td, void *ptr,
  53. enum asn_struct_free_method method) {
  54. if(td && ptr) {
  55. switch(method) {
  56. case ASFM_FREE_EVERYTHING:
  57. FREEMEM(ptr);
  58. break;
  59. case ASFM_FREE_UNDERLYING:
  60. break;
  61. case ASFM_FREE_UNDERLYING_AND_RESET:
  62. memset(ptr, 0, sizeof(NULL_t));
  63. break;
  64. }
  65. }
  66. }
  67. /*
  68. * Decode NULL type.
  69. */
  70. asn_dec_rval_t
  71. NULL_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
  72. const asn_TYPE_descriptor_t *td, void **bool_value,
  73. const void *buf_ptr, size_t size, int tag_mode) {
  74. NULL_t *st = (NULL_t *)*bool_value;
  75. asn_dec_rval_t rval;
  76. ber_tlv_len_t length;
  77. if(st == NULL) {
  78. st = (NULL_t *)(*bool_value = CALLOC(1, sizeof(*st)));
  79. if(st == NULL) {
  80. rval.code = RC_FAIL;
  81. rval.consumed = 0;
  82. return rval;
  83. }
  84. }
  85. ASN_DEBUG("Decoding %s as NULL (tm=%d)", td->name, tag_mode);
  86. /*
  87. * Check tags.
  88. */
  89. rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size, tag_mode, 0,
  90. &length, 0);
  91. if(rval.code != RC_OK) {
  92. return rval;
  93. }
  94. // X.690-201508, #8.8.2, length shall be zero.
  95. if(length != 0) {
  96. ASN_DEBUG("Decoding %s as NULL failed: too much data", td->name);
  97. rval.code = RC_FAIL;
  98. rval.consumed = 0;
  99. return rval;
  100. }
  101. return rval;
  102. }
  103. asn_enc_rval_t
  104. NULL_encode_der(const asn_TYPE_descriptor_t *td, const void *ptr, int tag_mode,
  105. ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb, void *app_key) {
  106. asn_enc_rval_t erval;
  107. erval.encoded = der_write_tags(td, 0, tag_mode, 0, tag, cb, app_key);
  108. if(erval.encoded == -1) {
  109. erval.failed_type = td;
  110. erval.structure_ptr = ptr;
  111. }
  112. ASN__ENCODED_OK(erval);
  113. }
  114. asn_enc_rval_t
  115. NULL_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
  116. enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb,
  117. void *app_key) {
  118. asn_enc_rval_t er;
  119. (void)td;
  120. (void)sptr;
  121. (void)ilevel;
  122. (void)flags;
  123. (void)cb;
  124. (void)app_key;
  125. /* XMLNullValue is empty */
  126. er.encoded = 0;
  127. ASN__ENCODED_OK(er);
  128. }
  129. static enum xer_pbd_rval
  130. NULL__xer_body_decode(const asn_TYPE_descriptor_t *td, void *sptr,
  131. const void *chunk_buf, size_t chunk_size) {
  132. (void)td;
  133. (void)sptr;
  134. (void)chunk_buf; /* Going to be empty according to the rules below. */
  135. /*
  136. * There must be no content in self-terminating <NULL/> tag.
  137. */
  138. if(chunk_size)
  139. return XPBD_BROKEN_ENCODING;
  140. else
  141. return XPBD_BODY_CONSUMED;
  142. }
  143. asn_dec_rval_t
  144. NULL_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
  145. const asn_TYPE_descriptor_t *td, void **sptr,
  146. const char *opt_mname, const void *buf_ptr, size_t size) {
  147. return xer_decode_primitive(opt_codec_ctx, td,
  148. sptr, sizeof(NULL_t), opt_mname, buf_ptr, size,
  149. NULL__xer_body_decode);
  150. }
  151. int
  152. NULL_compare(const asn_TYPE_descriptor_t *td, const void *a, const void *b) {
  153. (void)td;
  154. (void)a;
  155. (void)b;
  156. return 0;
  157. }
  158. int
  159. NULL_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
  160. asn_app_consume_bytes_f *cb, void *app_key) {
  161. (void)td; /* Unused argument */
  162. (void)ilevel; /* Unused argument */
  163. if(sptr) {
  164. return (cb("<present>", 9, app_key) < 0) ? -1 : 0;
  165. } else {
  166. return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
  167. }
  168. }
  169. #ifndef ASN_DISABLE_OER_SUPPORT
  170. asn_dec_rval_t
  171. NULL_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
  172. const asn_TYPE_descriptor_t *td,
  173. const asn_oer_constraints_t *constraints, void **sptr,
  174. const void *ptr, size_t size) {
  175. asn_dec_rval_t rv = {RC_OK, 0};
  176. (void)opt_codec_ctx;
  177. (void)td;
  178. (void)constraints;
  179. (void)ptr;
  180. (void)size;
  181. if(!*sptr) {
  182. *sptr = MALLOC(sizeof(NULL_t));
  183. if(*sptr) {
  184. *(NULL_t *)*sptr = 0;
  185. } else {
  186. ASN__DECODE_FAILED;
  187. }
  188. }
  189. return rv;
  190. }
  191. asn_enc_rval_t
  192. NULL_encode_oer(const asn_TYPE_descriptor_t *td,
  193. const asn_oer_constraints_t *constraints, const void *sptr,
  194. asn_app_consume_bytes_f *cb, void *app_key) {
  195. asn_enc_rval_t er;
  196. (void)td;
  197. (void)sptr;
  198. (void)constraints;
  199. (void)cb;
  200. (void)app_key;
  201. er.encoded = 0; /* Encoding in 0 bytes. */
  202. ASN__ENCODED_OK(er);
  203. }
  204. #endif /* ASN_DISABLE_OER_SUPPORT */
  205. #ifndef ASN_DISABLE_PER_SUPPORT
  206. asn_dec_rval_t
  207. NULL_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
  208. const asn_TYPE_descriptor_t *td,
  209. const asn_per_constraints_t *constraints, void **sptr,
  210. asn_per_data_t *pd) {
  211. asn_dec_rval_t rv;
  212. (void)opt_codec_ctx;
  213. (void)td;
  214. (void)constraints;
  215. (void)pd;
  216. if(!*sptr) {
  217. *sptr = MALLOC(sizeof(NULL_t));
  218. if(*sptr) {
  219. *(NULL_t *)*sptr = 0;
  220. } else {
  221. ASN__DECODE_FAILED;
  222. }
  223. }
  224. /*
  225. * NULL type does not have content octets.
  226. */
  227. rv.code = RC_OK;
  228. rv.consumed = 0;
  229. return rv;
  230. }
  231. asn_enc_rval_t
  232. NULL_encode_uper(const asn_TYPE_descriptor_t *td,
  233. const asn_per_constraints_t *constraints, const void *sptr,
  234. asn_per_outp_t *po) {
  235. asn_enc_rval_t er;
  236. (void)td;
  237. (void)constraints;
  238. (void)sptr;
  239. (void)po;
  240. er.encoded = 0;
  241. ASN__ENCODED_OK(er);
  242. }
  243. #endif /* ASN_DISABLE_PER_SUPPORT */
  244. asn_random_fill_result_t
  245. NULL_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
  246. const asn_encoding_constraints_t *constr,
  247. size_t max_length) {
  248. asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
  249. asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
  250. asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
  251. NULL_t *st = *sptr;
  252. (void)td;
  253. (void)constr;
  254. if(max_length == 0) return result_skipped;
  255. if(st == NULL) {
  256. st = (NULL_t *)(*sptr = CALLOC(1, sizeof(*st)));
  257. if(st == NULL) {
  258. return result_failed;
  259. }
  260. }
  261. return result_ok;
  262. }