3 * Copyright (c) 2003, 2005 Lev Walkin <vlm@lionet.info>. All rights reserved.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
7 #include <asn_codecs_prim.h>
11 * BOOLEAN basic type description.
13 static const ber_tlv_tag_t asn_DEF_BOOLEAN_tags[] = {
14 (ASN_TAG_CLASS_UNIVERSAL | (1 << 2))
16 asn_TYPE_operation_t asn_OP_BOOLEAN = {
24 #ifdef ASN_DISABLE_OER_SUPPORT
30 #endif /* ASN_DISABLE_OER_SUPPORT */
31 #ifdef ASN_DISABLE_PER_SUPPORT
37 BOOLEAN_decode_uper, /* Unaligned PER decoder */
38 BOOLEAN_encode_uper, /* Unaligned PER encoder */
39 BOOLEAN_decode_aper, /* Aligned PER decoder */
40 BOOLEAN_encode_aper, /* Aligned PER encoder */
41 #endif /* ASN_DISABLE_PER_SUPPORT */
43 0 /* Use generic outmost tag fetcher */
45 asn_TYPE_descriptor_t asn_DEF_BOOLEAN = {
50 sizeof(asn_DEF_BOOLEAN_tags) / sizeof(asn_DEF_BOOLEAN_tags[0]),
51 asn_DEF_BOOLEAN_tags, /* Same as above */
52 sizeof(asn_DEF_BOOLEAN_tags) / sizeof(asn_DEF_BOOLEAN_tags[0]),
53 { 0, 0, asn_generic_no_constraint },
54 0, 0, /* No members */
59 * Decode BOOLEAN type.
62 BOOLEAN_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
63 const asn_TYPE_descriptor_t *td, void **bool_value,
64 const void *buf_ptr, size_t size, int tag_mode) {
65 BOOLEAN_t *st = (BOOLEAN_t *)*bool_value;
71 st = (BOOLEAN_t *)(*bool_value = CALLOC(1, sizeof(*st)));
79 ASN_DEBUG("Decoding %s as BOOLEAN (tm=%d)",
85 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
86 tag_mode, 0, &length, 0);
87 if(rval.code != RC_OK)
90 ASN_DEBUG("Boolean length is %d bytes", (int)length);
92 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
93 size -= rval.consumed;
94 if(length > (ber_tlv_len_t)size) {
101 * Compute boolean value.
103 for(*st = 0, lidx = 0;
104 (lidx < length) && *st == 0; lidx++) {
106 * Very simple approach: read bytes until the end or
107 * value is already TRUE.
108 * BOOLEAN is not supposed to contain meaningful data anyway.
110 *st |= ((const uint8_t *)buf_ptr)[lidx];
114 rval.consumed += length;
116 ASN_DEBUG("Took %ld/%ld bytes to encode %s, value=%d",
117 (long)rval.consumed, (long)length,
124 BOOLEAN_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
125 int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb,
127 asn_enc_rval_t erval = {0,0,0};
128 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
130 erval.encoded = der_write_tags(td, 1, tag_mode, 0, tag, cb, app_key);
131 if(erval.encoded == -1) {
132 erval.failed_type = td;
133 erval.structure_ptr = sptr;
140 bool_value = *st ? 0xff : 0; /* 0xff mandated by DER */
142 if(cb(&bool_value, 1, app_key) < 0) {
144 erval.failed_type = td;
145 erval.structure_ptr = sptr;
152 ASN__ENCODED_OK(erval);
157 * Decode the chunk of XML text encoding INTEGER.
159 static enum xer_pbd_rval
160 BOOLEAN__xer_body_decode(const asn_TYPE_descriptor_t *td, void *sptr,
161 const void *chunk_buf, size_t chunk_size) {
162 BOOLEAN_t *st = (BOOLEAN_t *)sptr;
163 const char *p = (const char *)chunk_buf;
167 if(chunk_size && p[0] == 0x3c /* '<' */) {
168 switch(xer_check_tag(chunk_buf, chunk_size, "false")) {
174 if(xer_check_tag(chunk_buf, chunk_size, "true")
176 return XPBD_BROKEN_ENCODING;
178 *st = 1; /* Or 0xff as in DER?.. */
181 return XPBD_BROKEN_ENCODING;
183 return XPBD_BODY_CONSUMED;
185 return XPBD_BROKEN_ENCODING;
191 BOOLEAN_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
192 const asn_TYPE_descriptor_t *td, void **sptr,
193 const char *opt_mname, const void *buf_ptr, size_t size) {
194 return xer_decode_primitive(opt_codec_ctx, td,
195 sptr, sizeof(BOOLEAN_t), opt_mname, buf_ptr, size,
196 BOOLEAN__xer_body_decode);
200 BOOLEAN_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
201 int ilevel, enum xer_encoder_flags_e flags,
202 asn_app_consume_bytes_f *cb, void *app_key) {
203 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
204 asn_enc_rval_t er = {0, 0, 0};
209 if(!st) ASN__ENCODE_FAILED;
212 ASN__CALLBACK("<true/>", 7);
214 ASN__CALLBACK("<false/>", 8);
223 BOOLEAN_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
224 asn_app_consume_bytes_f *cb, void *app_key) {
225 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
229 (void)td; /* Unused argument */
230 (void)ilevel; /* Unused argument */
245 return (cb(buf, buflen, app_key) < 0) ? -1 : 0;
249 BOOLEAN_free(const asn_TYPE_descriptor_t *td, void *ptr,
250 enum asn_struct_free_method method) {
253 case ASFM_FREE_EVERYTHING:
256 case ASFM_FREE_UNDERLYING:
258 case ASFM_FREE_UNDERLYING_AND_RESET:
259 memset(ptr, 0, sizeof(BOOLEAN_t));
265 #ifndef ASN_DISABLE_PER_SUPPORT
268 BOOLEAN_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
269 const asn_TYPE_descriptor_t *td,
270 const asn_per_constraints_t *constraints, void **sptr,
271 asn_per_data_t *pd) {
273 BOOLEAN_t *st = (BOOLEAN_t *)*sptr;
280 st = (BOOLEAN_t *)(*sptr = MALLOC(sizeof(*st)));
281 if(!st) ASN__DECODE_FAILED;
285 * Extract a single bit
287 switch(per_get_few_bits(pd, 1)) {
288 case 1: *st = 1; break;
289 case 0: *st = 0; break;
290 case -1: default: ASN__DECODE_STARVED;
293 ASN_DEBUG("%s decoded as %s", td->name, *st ? "TRUE" : "FALSE");
302 BOOLEAN_encode_uper(const asn_TYPE_descriptor_t *td,
303 const asn_per_constraints_t *constraints, const void *sptr,
304 asn_per_outp_t *po) {
305 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
306 asn_enc_rval_t er = { 0, 0, 0 };
310 if(!st) ASN__ENCODE_FAILED;
312 if(per_put_few_bits(po, *st ? 1 : 0, 1))
319 BOOLEAN_decode_aper(const asn_codec_ctx_t *opt_codec_ctx, const asn_TYPE_descriptor_t *td,
320 const asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
322 BOOLEAN_t *st = (BOOLEAN_t *)*sptr;
329 st = (BOOLEAN_t *)(*sptr = MALLOC(sizeof(*st)));
330 if(!st) ASN__DECODE_FAILED;
334 * Extract a single bit
336 switch(per_get_few_bits(pd, 1)) {
348 ASN_DEBUG("%s decoded as %s", td->name, *st ? "TRUE" : "FALSE");
356 BOOLEAN_encode_aper(const asn_TYPE_descriptor_t *td,
357 const asn_per_constraints_t *constraints,
358 const void *sptr, asn_per_outp_t *po) {
359 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
360 asn_enc_rval_t er = { 0, 0, 0 };
364 if(!st) ASN__ENCODE_FAILED;
366 if(per_put_few_bits(po, *st ? 1 : 0, 1))
372 #endif /* ASN_DISABLE_PER_SUPPORT */
374 #ifndef ASN_DISABLE_OER_SUPPORT
377 * Encode as Canonical OER.
380 BOOLEAN_encode_oer(const asn_TYPE_descriptor_t *td,
381 const asn_oer_constraints_t *constraints, const void *sptr,
382 asn_app_consume_bytes_f *cb, void *app_key) {
383 asn_enc_rval_t er = { 1, 0, 0 };
384 const BOOLEAN_t *st = sptr;
385 uint8_t bool_value = *st ? 0xff : 0; /* 0xff mandated by OER */
388 (void)constraints; /* Constraints are unused in OER */
390 if(cb(&bool_value, 1, app_key) < 0) {
398 BOOLEAN_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
399 const asn_TYPE_descriptor_t *td,
400 const asn_oer_constraints_t *constraints, void **sptr,
401 const void *ptr, size_t size) {
402 asn_dec_rval_t ok = {RC_OK, 1};
407 (void)constraints; /* Constraints are unused in OER */
414 st = (BOOLEAN_t *)(*sptr = CALLOC(1, sizeof(*st)));
415 if(!st) ASN__DECODE_FAILED;
418 *st = *(const uint8_t *)ptr;
428 BOOLEAN_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
430 const BOOLEAN_t *a = aptr;
431 const BOOLEAN_t *b = bptr;
436 if(!*a == !*b) { /* TRUE can be encoded by any non-zero byte. */
450 asn_random_fill_result_t
451 BOOLEAN_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
452 const asn_encoding_constraints_t *constraints,
454 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
455 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
456 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
457 BOOLEAN_t *st = *sptr;
459 if(max_length == 0) return result_skipped;
462 st = (BOOLEAN_t *)(*sptr = CALLOC(1, sizeof(*st)));
464 return result_failed;
468 if(!constraints || !constraints->per_constraints)
469 constraints = &td->encoding_constraints;
470 if(constraints->per_constraints) {
471 const asn_per_constraint_t *pc = &constraints->per_constraints->value;
472 if(pc->flags & APC_CONSTRAINED) {
473 *st = asn_random_between(pc->lower_bound, pc->upper_bound);
478 /* Simulate booleans that are sloppily set and biased. */
479 switch(asn_random_between(0, 7)) {
484 case 3: *st = -1; break;
485 case 4: *st = 1; break;
486 case 5: *st = INT_MIN; break;
487 case 6: *st = INT_MAX; break;
489 *st = asn_random_between(INT_MIN, INT_MAX);