Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2003 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | #include <BOOLEAN.h> |
| 7 | |
| 8 | /* |
| 9 | * BOOLEAN basic type description. |
| 10 | */ |
| 11 | static ber_tlv_tag_t asn1_DEF_BOOLEAN_tags[] = { |
| 12 | (ASN_TAG_CLASS_UNIVERSAL | (1 << 2)) |
| 13 | }; |
| 14 | asn1_TYPE_descriptor_t asn1_DEF_BOOLEAN = { |
| 15 | "BOOLEAN", |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 16 | BOOLEAN_free, |
| 17 | BOOLEAN_print, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 18 | asn_generic_no_constraint, |
| 19 | BOOLEAN_decode_ber, |
| 20 | BOOLEAN_encode_der, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 21 | 0, /* Not implemented yet */ |
| 22 | BOOLEAN_encode_xer, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 23 | 0, /* Use generic outmost tag fetcher */ |
| 24 | asn1_DEF_BOOLEAN_tags, |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 25 | sizeof(asn1_DEF_BOOLEAN_tags) / sizeof(asn1_DEF_BOOLEAN_tags[0]), |
| 26 | asn1_DEF_BOOLEAN_tags, /* Same as above */ |
| 27 | sizeof(asn1_DEF_BOOLEAN_tags) / sizeof(asn1_DEF_BOOLEAN_tags[0]), |
Lev Walkin | 449f832 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 28 | 0, 0, /* No members */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 29 | 0 /* No specifics */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | /* |
| 33 | * Decode BOOLEAN type. |
| 34 | */ |
| 35 | ber_dec_rval_t |
| 36 | BOOLEAN_decode_ber(asn1_TYPE_descriptor_t *td, |
Lev Walkin | 958d7a8 | 2004-08-19 13:26:36 +0000 | [diff] [blame] | 37 | void **bool_value, void *buf_ptr, size_t size, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 38 | int tag_mode) { |
Lev Walkin | 958d7a8 | 2004-08-19 13:26:36 +0000 | [diff] [blame] | 39 | BOOLEAN_t *st = (BOOLEAN_t *)*bool_value; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 40 | ber_dec_rval_t rval; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 41 | ber_tlv_len_t length; |
| 42 | ber_tlv_len_t lidx; |
| 43 | |
| 44 | if(st == NULL) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 45 | st = (BOOLEAN_t *)(*bool_value = CALLOC(1, sizeof(*st))); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 46 | if(st == NULL) { |
| 47 | rval.code = RC_FAIL; |
| 48 | rval.consumed = 0; |
| 49 | return rval; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | ASN_DEBUG("Decoding %s as BOOLEAN (tm=%d)", |
| 54 | td->name, tag_mode); |
| 55 | |
| 56 | /* |
| 57 | * Check tags. |
| 58 | */ |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 59 | rval = ber_check_tags(td, 0, buf_ptr, size, tag_mode, 0, &length, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 60 | if(rval.code != RC_OK) |
| 61 | return rval; |
| 62 | |
| 63 | ASN_DEBUG("Boolean length is %d bytes", (int)length); |
| 64 | |
Lev Walkin | 4ce78ca | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 65 | buf_ptr = ((char *)buf_ptr) + rval.consumed; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 66 | size -= rval.consumed; |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 67 | if(length > (ber_tlv_len_t)size) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 68 | rval.code = RC_WMORE; |
| 69 | rval.consumed = 0; |
| 70 | return rval; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Compute boolean value. |
| 75 | */ |
Lev Walkin | 958d7a8 | 2004-08-19 13:26:36 +0000 | [diff] [blame] | 76 | for(*st = 0, lidx = 0; |
| 77 | (lidx < length) && *st == 0; lidx++) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 78 | /* |
| 79 | * Very simple approach: read bytes until the end or |
| 80 | * value is already TRUE. |
| 81 | * BOOLEAN is not supposed to contain meaningful data anyway. |
| 82 | */ |
Lev Walkin | 958d7a8 | 2004-08-19 13:26:36 +0000 | [diff] [blame] | 83 | *st |= ((uint8_t *)buf_ptr)[lidx]; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | rval.code = RC_OK; |
| 87 | rval.consumed += length; |
| 88 | |
Lev Walkin | 958d7a8 | 2004-08-19 13:26:36 +0000 | [diff] [blame] | 89 | ASN_DEBUG("Took %ld/%ld bytes to encode %s, value=%d", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 90 | (long)rval.consumed, (long)length, |
Lev Walkin | 958d7a8 | 2004-08-19 13:26:36 +0000 | [diff] [blame] | 91 | td->name, *st); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 92 | |
| 93 | return rval; |
| 94 | } |
| 95 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 96 | asn_enc_rval_t |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 97 | BOOLEAN_encode_der(asn1_TYPE_descriptor_t *td, void *sptr, |
| 98 | int tag_mode, ber_tlv_tag_t tag, |
| 99 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 100 | asn_enc_rval_t erval; |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 101 | BOOLEAN_t *st = (BOOLEAN_t *)sptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 102 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 103 | erval.encoded = der_write_tags(td, 1, tag_mode, 0, tag, cb, app_key); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 104 | if(erval.encoded == -1) { |
| 105 | erval.failed_type = td; |
| 106 | erval.structure_ptr = sptr; |
| 107 | return erval; |
| 108 | } |
| 109 | |
| 110 | if(cb) { |
| 111 | uint8_t bool_value; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 112 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 113 | bool_value = *st ? 0xff : 0; /* 0xff mandated by DER */ |
| 114 | |
| 115 | if(cb(&bool_value, 1, app_key) < 0) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 116 | erval.encoded = -1; |
| 117 | erval.failed_type = td; |
| 118 | erval.structure_ptr = sptr; |
| 119 | return erval; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | erval.encoded += 1; |
| 124 | |
| 125 | return erval; |
| 126 | } |
| 127 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 128 | asn_enc_rval_t |
| 129 | BOOLEAN_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr, |
| 130 | int ilevel, enum xer_encoder_flags_e flags, |
| 131 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 132 | const BOOLEAN_t *st = (const BOOLEAN_t *)sptr; |
| 133 | asn_enc_rval_t er; |
| 134 | |
| 135 | (void)ilevel; |
| 136 | (void)flags; |
| 137 | |
| 138 | if(!st) _ASN_ENCODE_FAILED; |
| 139 | |
| 140 | if(*st) { |
| 141 | _ASN_CALLBACK("<true/>", 7); |
| 142 | er.encoded = 7; |
| 143 | } else { |
| 144 | _ASN_CALLBACK("<false/>", 8); |
| 145 | er.encoded = 8; |
| 146 | } |
| 147 | |
| 148 | return er; |
| 149 | } |
| 150 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 151 | int |
| 152 | BOOLEAN_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 153 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 154 | const BOOLEAN_t *st = (const BOOLEAN_t *)sptr; |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 155 | const char *buf; |
| 156 | size_t buflen; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 157 | |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 158 | (void)td; /* Unused argument */ |
| 159 | (void)ilevel; /* Unused argument */ |
| 160 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 161 | if(st) { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 162 | if(*st) { |
| 163 | buf = "TRUE"; |
| 164 | buflen = 4; |
| 165 | } else { |
| 166 | buf = "FALSE"; |
| 167 | buflen = 5; |
| 168 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 169 | } else { |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 170 | buf = "<absent>"; |
| 171 | buflen = 8; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 172 | } |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 173 | |
| 174 | return (cb(buf, buflen, app_key) < 0) ? -1 : 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void |
| 178 | BOOLEAN_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
| 179 | if(td && ptr && !contents_only) { |
| 180 | FREEMEM(ptr); |
| 181 | } |
| 182 | } |
| 183 | |