2 * Copyright (c) 2003, 2005 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
5 #include <asn_internal.h>
6 #include <asn_codecs_prim.h>
10 * BOOLEAN basic type description.
12 static const ber_tlv_tag_t asn_DEF_BOOLEAN_tags[] = {
13 (ASN_TAG_CLASS_UNIVERSAL | (1 << 2))
15 asn_TYPE_operation_t asn_OP_BOOLEAN = {
23 #ifdef ASN_DISABLE_OER_SUPPORT
29 #endif /* ASN_DISABLE_OER_SUPPORT */
30 #ifdef ASN_DISABLE_PER_SUPPORT
36 BOOLEAN_decode_uper, /* Unaligned PER decoder */
37 BOOLEAN_encode_uper, /* Unaligned PER encoder */
38 BOOLEAN_decode_aper, /* Aligned PER decoder */
39 BOOLEAN_encode_aper, /* Aligned PER encoder */
40 #endif /* ASN_DISABLE_PER_SUPPORT */
42 0 /* Use generic outmost tag fetcher */
44 asn_TYPE_descriptor_t asn_DEF_BOOLEAN = {
49 sizeof(asn_DEF_BOOLEAN_tags) / sizeof(asn_DEF_BOOLEAN_tags[0]),
50 asn_DEF_BOOLEAN_tags, /* Same as above */
51 sizeof(asn_DEF_BOOLEAN_tags) / sizeof(asn_DEF_BOOLEAN_tags[0]),
52 { 0, 0, asn_generic_no_constraint },
53 0, 0, /* No members */
58 * Decode BOOLEAN type.
61 BOOLEAN_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
62 const asn_TYPE_descriptor_t *td, void **bool_value,
63 const void *buf_ptr, size_t size, int tag_mode) {
64 BOOLEAN_t *st = (BOOLEAN_t *)*bool_value;
70 st = (BOOLEAN_t *)(*bool_value = CALLOC(1, sizeof(*st)));
78 ASN_DEBUG("Decoding %s as BOOLEAN (tm=%d)",
84 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
85 tag_mode, 0, &length, 0);
86 if(rval.code != RC_OK)
89 ASN_DEBUG("Boolean length is %d bytes", (int)length);
91 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
92 size -= rval.consumed;
93 if(length > (ber_tlv_len_t)size) {
100 * Compute boolean value.
102 for(*st = 0, lidx = 0;
103 (lidx < length) && *st == 0; lidx++) {
105 * Very simple approach: read bytes until the end or
106 * value is already TRUE.
107 * BOOLEAN is not supposed to contain meaningful data anyway.
109 *st |= ((const uint8_t *)buf_ptr)[lidx];
113 rval.consumed += length;
115 ASN_DEBUG("Took %ld/%ld bytes to encode %s, value=%d",
116 (long)rval.consumed, (long)length,
123 BOOLEAN_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
124 int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb,
126 asn_enc_rval_t erval = {0,0,0};
127 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
129 erval.encoded = der_write_tags(td, 1, tag_mode, 0, tag, cb, app_key);
130 if(erval.encoded == -1) {
131 erval.failed_type = td;
132 erval.structure_ptr = sptr;
139 bool_value = *st ? 0xff : 0; /* 0xff mandated by DER */
141 if(cb(&bool_value, 1, app_key) < 0) {
143 erval.failed_type = td;
144 erval.structure_ptr = sptr;
151 ASN__ENCODED_OK(erval);
156 * Decode the chunk of XML text encoding INTEGER.
158 static enum xer_pbd_rval
159 BOOLEAN__xer_body_decode(const asn_TYPE_descriptor_t *td, void *sptr,
160 const void *chunk_buf, size_t chunk_size) {
161 BOOLEAN_t *st = (BOOLEAN_t *)sptr;
162 const char *p = (const char *)chunk_buf;
166 if(chunk_size && p[0] == 0x3c /* '<' */) {
167 switch(xer_check_tag(chunk_buf, chunk_size, "false")) {
173 if(xer_check_tag(chunk_buf, chunk_size, "true")
175 return XPBD_BROKEN_ENCODING;
177 *st = 1; /* Or 0xff as in DER?.. */
180 return XPBD_BROKEN_ENCODING;
182 return XPBD_BODY_CONSUMED;
184 return XPBD_BROKEN_ENCODING;
190 BOOLEAN_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
191 const asn_TYPE_descriptor_t *td, void **sptr,
192 const char *opt_mname, const void *buf_ptr, size_t size) {
193 return xer_decode_primitive(opt_codec_ctx, td,
194 sptr, sizeof(BOOLEAN_t), opt_mname, buf_ptr, size,
195 BOOLEAN__xer_body_decode);
199 BOOLEAN_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
200 int ilevel, enum xer_encoder_flags_e flags,
201 asn_app_consume_bytes_f *cb, void *app_key) {
202 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
203 asn_enc_rval_t er = {0, 0, 0};
208 if(!st) ASN__ENCODE_FAILED;
211 ASN__CALLBACK("<true/>", 7);
213 ASN__CALLBACK("<false/>", 8);
222 BOOLEAN_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
223 asn_app_consume_bytes_f *cb, void *app_key) {
224 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
228 (void)td; /* Unused argument */
229 (void)ilevel; /* Unused argument */
244 return (cb(buf, buflen, app_key) < 0) ? -1 : 0;
248 BOOLEAN_free(const asn_TYPE_descriptor_t *td, void *ptr,
249 enum asn_struct_free_method method) {
252 case ASFM_FREE_EVERYTHING:
255 case ASFM_FREE_UNDERLYING:
257 case ASFM_FREE_UNDERLYING_AND_RESET:
258 memset(ptr, 0, sizeof(BOOLEAN_t));
264 #ifndef ASN_DISABLE_PER_SUPPORT
267 BOOLEAN_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
268 const asn_TYPE_descriptor_t *td,
269 const asn_per_constraints_t *constraints, void **sptr,
270 asn_per_data_t *pd) {
272 BOOLEAN_t *st = (BOOLEAN_t *)*sptr;
279 st = (BOOLEAN_t *)(*sptr = MALLOC(sizeof(*st)));
280 if(!st) ASN__DECODE_FAILED;
284 * Extract a single bit
286 switch(per_get_few_bits(pd, 1)) {
287 case 1: *st = 1; break;
288 case 0: *st = 0; break;
289 case -1: default: ASN__DECODE_STARVED;
292 ASN_DEBUG("%s decoded as %s", td->name, *st ? "TRUE" : "FALSE");
301 BOOLEAN_encode_uper(const asn_TYPE_descriptor_t *td,
302 const asn_per_constraints_t *constraints, const void *sptr,
303 asn_per_outp_t *po) {
304 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
305 asn_enc_rval_t er = { 0, 0, 0 };
309 if(!st) ASN__ENCODE_FAILED;
311 if(per_put_few_bits(po, *st ? 1 : 0, 1))
318 BOOLEAN_decode_aper(const asn_codec_ctx_t *opt_codec_ctx, const asn_TYPE_descriptor_t *td,
319 const asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
321 BOOLEAN_t *st = (BOOLEAN_t *)*sptr;
328 st = (BOOLEAN_t *)(*sptr = MALLOC(sizeof(*st)));
329 if(!st) ASN__DECODE_FAILED;
333 * Extract a single bit
335 switch(per_get_few_bits(pd, 1)) {
347 ASN_DEBUG("%s decoded as %s", td->name, *st ? "TRUE" : "FALSE");
355 BOOLEAN_encode_aper(const asn_TYPE_descriptor_t *td,
356 const asn_per_constraints_t *constraints,
357 const void *sptr, asn_per_outp_t *po) {
358 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
359 asn_enc_rval_t er = { 0, 0, 0 };
363 if(!st) ASN__ENCODE_FAILED;
365 if(per_put_few_bits(po, *st ? 1 : 0, 1))
371 #endif /* ASN_DISABLE_PER_SUPPORT */
373 #ifndef ASN_DISABLE_OER_SUPPORT
376 * Encode as Canonical OER.
379 BOOLEAN_encode_oer(const asn_TYPE_descriptor_t *td,
380 const asn_oer_constraints_t *constraints, const void *sptr,
381 asn_app_consume_bytes_f *cb, void *app_key) {
382 asn_enc_rval_t er = { 1, 0, 0 };
383 const BOOLEAN_t *st = sptr;
384 uint8_t bool_value = *st ? 0xff : 0; /* 0xff mandated by OER */
387 (void)constraints; /* Constraints are unused in OER */
389 if(cb(&bool_value, 1, app_key) < 0) {
397 BOOLEAN_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
398 const asn_TYPE_descriptor_t *td,
399 const asn_oer_constraints_t *constraints, void **sptr,
400 const void *ptr, size_t size) {
401 asn_dec_rval_t ok = {RC_OK, 1};
406 (void)constraints; /* Constraints are unused in OER */
413 st = (BOOLEAN_t *)(*sptr = CALLOC(1, sizeof(*st)));
414 if(!st) ASN__DECODE_FAILED;
417 *st = *(const uint8_t *)ptr;
427 BOOLEAN_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
429 const BOOLEAN_t *a = aptr;
430 const BOOLEAN_t *b = bptr;
435 if(!*a == !*b) { /* TRUE can be encoded by any non-zero byte. */
449 asn_random_fill_result_t
450 BOOLEAN_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
451 const asn_encoding_constraints_t *constraints,
453 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
454 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
455 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
456 BOOLEAN_t *st = *sptr;
458 if(max_length == 0) return result_skipped;
461 st = (BOOLEAN_t *)(*sptr = CALLOC(1, sizeof(*st)));
463 return result_failed;
467 if(!constraints || !constraints->per_constraints)
468 constraints = &td->encoding_constraints;
469 if(constraints->per_constraints) {
470 const asn_per_constraint_t *pc = &constraints->per_constraints->value;
471 if(pc->flags & APC_CONSTRAINED) {
472 *st = asn_random_between(pc->lower_bound, pc->upper_bound);
477 /* Simulate booleans that are sloppily set and biased. */
478 switch(asn_random_between(0, 7)) {
483 case 3: *st = -1; break;
484 case 4: *st = 1; break;
485 case 5: *st = INT_MIN; break;
486 case 6: *st = INT_MAX; break;
488 *st = asn_random_between(INT_MIN, INT_MAX);