3 * Copyright 2020 AT&T Intellectual Property
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * Copyright (c) 2003, 2005 Lev Walkin <vlm@lionet.info>. All rights reserved.
21 * Redistribution and modifications are permitted subject to BSD license.
23 #include <asn_internal.h>
24 #include <asn_codecs_prim.h>
28 * BOOLEAN basic type description.
30 static const ber_tlv_tag_t asn_DEF_BOOLEAN_tags[] = {
31 (ASN_TAG_CLASS_UNIVERSAL | (1 << 2))
33 asn_TYPE_operation_t asn_OP_BOOLEAN = {
41 #ifdef ASN_DISABLE_OER_SUPPORT
47 #endif /* ASN_DISABLE_OER_SUPPORT */
48 #ifdef ASN_DISABLE_PER_SUPPORT
54 BOOLEAN_decode_uper, /* Unaligned PER decoder */
55 BOOLEAN_encode_uper, /* Unaligned PER encoder */
56 BOOLEAN_decode_aper, /* Aligned PER decoder */
57 BOOLEAN_encode_aper, /* Aligned PER encoder */
58 #endif /* ASN_DISABLE_PER_SUPPORT */
60 0 /* Use generic outmost tag fetcher */
62 asn_TYPE_descriptor_t asn_DEF_BOOLEAN = {
67 sizeof(asn_DEF_BOOLEAN_tags) / sizeof(asn_DEF_BOOLEAN_tags[0]),
68 asn_DEF_BOOLEAN_tags, /* Same as above */
69 sizeof(asn_DEF_BOOLEAN_tags) / sizeof(asn_DEF_BOOLEAN_tags[0]),
70 { 0, 0, asn_generic_no_constraint },
71 0, 0, /* No members */
76 * Decode BOOLEAN type.
79 BOOLEAN_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
80 const asn_TYPE_descriptor_t *td, void **bool_value,
81 const void *buf_ptr, size_t size, int tag_mode) {
82 BOOLEAN_t *st = (BOOLEAN_t *)*bool_value;
88 st = (BOOLEAN_t *)(*bool_value = CALLOC(1, sizeof(*st)));
96 ASN_DEBUG("Decoding %s as BOOLEAN (tm=%d)",
102 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
103 tag_mode, 0, &length, 0);
104 if(rval.code != RC_OK)
107 ASN_DEBUG("Boolean length is %d bytes", (int)length);
109 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
110 size -= rval.consumed;
111 if(length > (ber_tlv_len_t)size) {
112 rval.code = RC_WMORE;
118 * Compute boolean value.
120 for(*st = 0, lidx = 0;
121 (lidx < length) && *st == 0; lidx++) {
123 * Very simple approach: read bytes until the end or
124 * value is already TRUE.
125 * BOOLEAN is not supposed to contain meaningful data anyway.
127 *st |= ((const uint8_t *)buf_ptr)[lidx];
131 rval.consumed += length;
133 ASN_DEBUG("Took %ld/%ld bytes to encode %s, value=%d",
134 (long)rval.consumed, (long)length,
141 BOOLEAN_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
142 int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb,
144 asn_enc_rval_t erval = {0,0,0};
145 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
147 erval.encoded = der_write_tags(td, 1, tag_mode, 0, tag, cb, app_key);
148 if(erval.encoded == -1) {
149 erval.failed_type = td;
150 erval.structure_ptr = sptr;
157 bool_value = *st ? 0xff : 0; /* 0xff mandated by DER */
159 if(cb(&bool_value, 1, app_key) < 0) {
161 erval.failed_type = td;
162 erval.structure_ptr = sptr;
169 ASN__ENCODED_OK(erval);
174 * Decode the chunk of XML text encoding INTEGER.
176 static enum xer_pbd_rval
177 BOOLEAN__xer_body_decode(const asn_TYPE_descriptor_t *td, void *sptr,
178 const void *chunk_buf, size_t chunk_size) {
179 BOOLEAN_t *st = (BOOLEAN_t *)sptr;
180 const char *p = (const char *)chunk_buf;
184 if(chunk_size && p[0] == 0x3c /* '<' */) {
185 switch(xer_check_tag(chunk_buf, chunk_size, "false")) {
191 if(xer_check_tag(chunk_buf, chunk_size, "true")
193 return XPBD_BROKEN_ENCODING;
195 *st = 1; /* Or 0xff as in DER?.. */
198 return XPBD_BROKEN_ENCODING;
200 return XPBD_BODY_CONSUMED;
202 return XPBD_BROKEN_ENCODING;
208 BOOLEAN_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
209 const asn_TYPE_descriptor_t *td, void **sptr,
210 const char *opt_mname, const void *buf_ptr, size_t size) {
211 return xer_decode_primitive(opt_codec_ctx, td,
212 sptr, sizeof(BOOLEAN_t), opt_mname, buf_ptr, size,
213 BOOLEAN__xer_body_decode);
217 BOOLEAN_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
218 int ilevel, enum xer_encoder_flags_e flags,
219 asn_app_consume_bytes_f *cb, void *app_key) {
220 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
221 asn_enc_rval_t er = {0, 0, 0};
226 if(!st) ASN__ENCODE_FAILED;
229 ASN__CALLBACK("<true/>", 7);
231 ASN__CALLBACK("<false/>", 8);
240 BOOLEAN_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
241 asn_app_consume_bytes_f *cb, void *app_key) {
242 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
246 (void)td; /* Unused argument */
247 (void)ilevel; /* Unused argument */
262 return (cb(buf, buflen, app_key) < 0) ? -1 : 0;
266 BOOLEAN_free(const asn_TYPE_descriptor_t *td, void *ptr,
267 enum asn_struct_free_method method) {
270 case ASFM_FREE_EVERYTHING:
273 case ASFM_FREE_UNDERLYING:
275 case ASFM_FREE_UNDERLYING_AND_RESET:
276 memset(ptr, 0, sizeof(BOOLEAN_t));
282 #ifndef ASN_DISABLE_PER_SUPPORT
285 BOOLEAN_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
286 const asn_TYPE_descriptor_t *td,
287 const asn_per_constraints_t *constraints, void **sptr,
288 asn_per_data_t *pd) {
290 BOOLEAN_t *st = (BOOLEAN_t *)*sptr;
297 st = (BOOLEAN_t *)(*sptr = MALLOC(sizeof(*st)));
298 if(!st) ASN__DECODE_FAILED;
302 * Extract a single bit
304 switch(per_get_few_bits(pd, 1)) {
305 case 1: *st = 1; break;
306 case 0: *st = 0; break;
307 case -1: default: ASN__DECODE_STARVED;
310 ASN_DEBUG("%s decoded as %s", td->name, *st ? "TRUE" : "FALSE");
319 BOOLEAN_encode_uper(const asn_TYPE_descriptor_t *td,
320 const asn_per_constraints_t *constraints, const void *sptr,
321 asn_per_outp_t *po) {
322 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
323 asn_enc_rval_t er = { 0, 0, 0 };
327 if(!st) ASN__ENCODE_FAILED;
329 if(per_put_few_bits(po, *st ? 1 : 0, 1))
336 BOOLEAN_decode_aper(const asn_codec_ctx_t *opt_codec_ctx, const asn_TYPE_descriptor_t *td,
337 const asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
339 BOOLEAN_t *st = (BOOLEAN_t *)*sptr;
346 st = (BOOLEAN_t *)(*sptr = MALLOC(sizeof(*st)));
347 if(!st) ASN__DECODE_FAILED;
351 * Extract a single bit
353 switch(per_get_few_bits(pd, 1)) {
365 ASN_DEBUG("%s decoded as %s", td->name, *st ? "TRUE" : "FALSE");
373 BOOLEAN_encode_aper(const asn_TYPE_descriptor_t *td,
374 const asn_per_constraints_t *constraints,
375 const void *sptr, asn_per_outp_t *po) {
376 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
377 asn_enc_rval_t er = { 0, 0, 0 };
381 if(!st) ASN__ENCODE_FAILED;
383 if(per_put_few_bits(po, *st ? 1 : 0, 1))
389 #endif /* ASN_DISABLE_PER_SUPPORT */
391 #ifndef ASN_DISABLE_OER_SUPPORT
394 * Encode as Canonical OER.
397 BOOLEAN_encode_oer(const asn_TYPE_descriptor_t *td,
398 const asn_oer_constraints_t *constraints, const void *sptr,
399 asn_app_consume_bytes_f *cb, void *app_key) {
400 asn_enc_rval_t er = { 1, 0, 0 };
401 const BOOLEAN_t *st = sptr;
402 uint8_t bool_value = *st ? 0xff : 0; /* 0xff mandated by OER */
405 (void)constraints; /* Constraints are unused in OER */
407 if(cb(&bool_value, 1, app_key) < 0) {
415 BOOLEAN_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
416 const asn_TYPE_descriptor_t *td,
417 const asn_oer_constraints_t *constraints, void **sptr,
418 const void *ptr, size_t size) {
419 asn_dec_rval_t ok = {RC_OK, 1};
424 (void)constraints; /* Constraints are unused in OER */
431 st = (BOOLEAN_t *)(*sptr = CALLOC(1, sizeof(*st)));
432 if(!st) ASN__DECODE_FAILED;
435 *st = *(const uint8_t *)ptr;
445 BOOLEAN_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
447 const BOOLEAN_t *a = aptr;
448 const BOOLEAN_t *b = bptr;
453 if(!*a == !*b) { /* TRUE can be encoded by any non-zero byte. */
467 asn_random_fill_result_t
468 BOOLEAN_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
469 const asn_encoding_constraints_t *constraints,
471 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
472 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
473 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
474 BOOLEAN_t *st = *sptr;
476 if(max_length == 0) return result_skipped;
479 st = (BOOLEAN_t *)(*sptr = CALLOC(1, sizeof(*st)));
481 return result_failed;
485 if(!constraints || !constraints->per_constraints)
486 constraints = &td->encoding_constraints;
487 if(constraints->per_constraints) {
488 const asn_per_constraint_t *pc = &constraints->per_constraints->value;
489 if(pc->flags & APC_CONSTRAINED) {
490 *st = asn_random_between(pc->lower_bound, pc->upper_bound);
495 /* Simulate booleans that are sloppily set and biased. */
496 switch(asn_random_between(0, 7)) {
501 case 3: *st = -1; break;
502 case 4: *st = 1; break;
503 case 5: *st = INT_MIN; break;
504 case 6: *st = INT_MAX; break;
506 *st = asn_random_between(INT_MIN, INT_MAX);