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