2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
10 * Decode BOOLEAN type.
13 BOOLEAN_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
14 const asn_TYPE_descriptor_t *td, void **bool_value,
15 const void *buf_ptr, size_t size, int tag_mode) {
16 BOOLEAN_t *st = (BOOLEAN_t *)*bool_value;
22 st = (BOOLEAN_t *)(*bool_value = CALLOC(1, sizeof(*st)));
30 ASN_DEBUG("Decoding %s as BOOLEAN (tm=%d)",
36 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
37 tag_mode, 0, &length, 0);
38 if(rval.code != RC_OK)
41 ASN_DEBUG("Boolean length is %d bytes", (int)length);
43 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
44 size -= rval.consumed;
45 if(length > (ber_tlv_len_t)size) {
52 * Compute boolean value.
54 for(*st = 0, lidx = 0;
55 (lidx < length) && *st == 0; lidx++) {
57 * Very simple approach: read bytes until the end or
58 * value is already TRUE.
59 * BOOLEAN is not supposed to contain meaningful data anyway.
61 *st |= ((const uint8_t *)buf_ptr)[lidx];
65 rval.consumed += length;
67 ASN_DEBUG("Took %ld/%ld bytes to encode %s, value=%d",
68 (long)rval.consumed, (long)length,
75 BOOLEAN_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
76 int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb,
78 asn_enc_rval_t erval = {0,0,0};
79 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
81 erval.encoded = der_write_tags(td, 1, tag_mode, 0, tag, cb, app_key);
82 if(erval.encoded == -1) {
83 erval.failed_type = td;
84 erval.structure_ptr = sptr;
91 bool_value = *st ? 0xff : 0; /* 0xff mandated by DER */
93 if(cb(&bool_value, 1, app_key) < 0) {
95 erval.failed_type = td;
96 erval.structure_ptr = sptr;
103 ASN__ENCODED_OK(erval);