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