vlm | fa67ddc | 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 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
vlm | 480ce97 | 2004-10-22 08:11:25 +0000 | [diff] [blame] | 6 | #include <asn_codecs_prim.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 7 | #include <BOOLEAN.h> |
| 8 | |
| 9 | /* |
| 10 | * BOOLEAN basic type description. |
| 11 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 12 | static ber_tlv_tag_t asn_DEF_BOOLEAN_tags[] = { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 13 | (ASN_TAG_CLASS_UNIVERSAL | (1 << 2)) |
| 14 | }; |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 15 | asn_TYPE_descriptor_t asn_DEF_BOOLEAN = { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 16 | "BOOLEAN", |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 17 | "BOOLEAN", |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 18 | BOOLEAN_free, |
| 19 | BOOLEAN_print, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 20 | asn_generic_no_constraint, |
| 21 | BOOLEAN_decode_ber, |
| 22 | BOOLEAN_encode_der, |
vlm | 480ce97 | 2004-10-22 08:11:25 +0000 | [diff] [blame] | 23 | BOOLEAN_decode_xer, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 24 | BOOLEAN_encode_xer, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 25 | 0, /* Use generic outmost tag fetcher */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 26 | asn_DEF_BOOLEAN_tags, |
| 27 | sizeof(asn_DEF_BOOLEAN_tags) / sizeof(asn_DEF_BOOLEAN_tags[0]), |
| 28 | asn_DEF_BOOLEAN_tags, /* Same as above */ |
| 29 | sizeof(asn_DEF_BOOLEAN_tags) / sizeof(asn_DEF_BOOLEAN_tags[0]), |
vlm | e413c12 | 2004-08-20 13:23:42 +0000 | [diff] [blame] | 30 | 0, 0, /* No members */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 31 | 0 /* No specifics */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | /* |
| 35 | * Decode BOOLEAN type. |
| 36 | */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 37 | asn_dec_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 38 | BOOLEAN_decode_ber(asn_codec_ctx_t *opt_codec_ctx, |
| 39 | asn_TYPE_descriptor_t *td, |
vlm | af9cf13 | 2004-08-19 13:26:36 +0000 | [diff] [blame] | 40 | void **bool_value, void *buf_ptr, size_t size, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 41 | int tag_mode) { |
vlm | af9cf13 | 2004-08-19 13:26:36 +0000 | [diff] [blame] | 42 | BOOLEAN_t *st = (BOOLEAN_t *)*bool_value; |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 43 | asn_dec_rval_t rval; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 44 | ber_tlv_len_t length; |
| 45 | ber_tlv_len_t lidx; |
| 46 | |
| 47 | if(st == NULL) { |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 48 | st = (BOOLEAN_t *)(*bool_value = CALLOC(1, sizeof(*st))); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 49 | if(st == NULL) { |
| 50 | rval.code = RC_FAIL; |
| 51 | rval.consumed = 0; |
| 52 | return rval; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | ASN_DEBUG("Decoding %s as BOOLEAN (tm=%d)", |
| 57 | td->name, tag_mode); |
| 58 | |
| 59 | /* |
| 60 | * Check tags. |
| 61 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 62 | rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size, |
| 63 | tag_mode, 0, &length, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 64 | if(rval.code != RC_OK) |
| 65 | return rval; |
| 66 | |
| 67 | ASN_DEBUG("Boolean length is %d bytes", (int)length); |
| 68 | |
vlm | d86c925 | 2004-08-25 01:34:11 +0000 | [diff] [blame] | 69 | buf_ptr = ((char *)buf_ptr) + rval.consumed; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 70 | size -= rval.consumed; |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 71 | if(length > (ber_tlv_len_t)size) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 72 | rval.code = RC_WMORE; |
| 73 | rval.consumed = 0; |
| 74 | return rval; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Compute boolean value. |
| 79 | */ |
vlm | af9cf13 | 2004-08-19 13:26:36 +0000 | [diff] [blame] | 80 | for(*st = 0, lidx = 0; |
| 81 | (lidx < length) && *st == 0; lidx++) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 82 | /* |
| 83 | * Very simple approach: read bytes until the end or |
| 84 | * value is already TRUE. |
| 85 | * BOOLEAN is not supposed to contain meaningful data anyway. |
| 86 | */ |
vlm | af9cf13 | 2004-08-19 13:26:36 +0000 | [diff] [blame] | 87 | *st |= ((uint8_t *)buf_ptr)[lidx]; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | rval.code = RC_OK; |
| 91 | rval.consumed += length; |
| 92 | |
vlm | af9cf13 | 2004-08-19 13:26:36 +0000 | [diff] [blame] | 93 | ASN_DEBUG("Took %ld/%ld bytes to encode %s, value=%d", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 94 | (long)rval.consumed, (long)length, |
vlm | af9cf13 | 2004-08-19 13:26:36 +0000 | [diff] [blame] | 95 | td->name, *st); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 96 | |
| 97 | return rval; |
| 98 | } |
| 99 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 100 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 101 | BOOLEAN_encode_der(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 102 | int tag_mode, ber_tlv_tag_t tag, |
| 103 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 104 | asn_enc_rval_t erval; |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 105 | BOOLEAN_t *st = (BOOLEAN_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 106 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 107 | erval.encoded = der_write_tags(td, 1, tag_mode, 0, tag, cb, app_key); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 108 | if(erval.encoded == -1) { |
| 109 | erval.failed_type = td; |
| 110 | erval.structure_ptr = sptr; |
| 111 | return erval; |
| 112 | } |
| 113 | |
| 114 | if(cb) { |
| 115 | uint8_t bool_value; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 116 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 117 | bool_value = *st ? 0xff : 0; /* 0xff mandated by DER */ |
| 118 | |
| 119 | if(cb(&bool_value, 1, app_key) < 0) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 120 | erval.encoded = -1; |
| 121 | erval.failed_type = td; |
| 122 | erval.structure_ptr = sptr; |
| 123 | return erval; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | erval.encoded += 1; |
| 128 | |
| 129 | return erval; |
| 130 | } |
| 131 | |
vlm | 480ce97 | 2004-10-22 08:11:25 +0000 | [diff] [blame] | 132 | |
| 133 | /* |
| 134 | * Decode the chunk of XML text encoding INTEGER. |
| 135 | */ |
| 136 | static ssize_t |
| 137 | BOOLEAN__xer_body_decode(void *sptr, void *chunk_buf, size_t chunk_size) { |
| 138 | BOOLEAN_t *st = (BOOLEAN_t *)sptr; |
| 139 | char *p = (char *)chunk_buf; |
vlm | 480ce97 | 2004-10-22 08:11:25 +0000 | [diff] [blame] | 140 | |
| 141 | if(chunk_size == 0) return -1; |
| 142 | |
| 143 | if(p[0] == 0x3c /* '<' */) { |
| 144 | switch(xer_check_tag(chunk_buf, chunk_size, "false")) { |
| 145 | case XCT_BOTH: |
| 146 | /* "<false/>" */ |
| 147 | *st = 0; |
| 148 | break; |
vlm | c70f8f3 | 2005-02-18 18:06:36 +0000 | [diff] [blame] | 149 | case XCT_UNKNOWN_BO: |
vlm | 480ce97 | 2004-10-22 08:11:25 +0000 | [diff] [blame] | 150 | if(xer_check_tag(chunk_buf, chunk_size, "true") |
| 151 | != XCT_BOTH) |
| 152 | return -1; |
| 153 | /* "<true/>" */ |
| 154 | *st = 1; /* Or 0xff as in DER?.. */ |
| 155 | break; |
| 156 | default: |
| 157 | return -1; |
| 158 | } |
| 159 | } else { |
vlm | 2a4245f | 2004-10-22 08:17:16 +0000 | [diff] [blame] | 160 | if(!xer_is_whitespace(chunk_buf, chunk_size)) |
| 161 | return -1; |
vlm | 480ce97 | 2004-10-22 08:11:25 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | return chunk_size; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | asn_dec_rval_t |
| 169 | BOOLEAN_decode_xer(asn_codec_ctx_t *opt_codec_ctx, |
| 170 | asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
| 171 | void *buf_ptr, size_t size) { |
| 172 | |
| 173 | return xer_decode_primitive(opt_codec_ctx, td, |
| 174 | sptr, sizeof(BOOLEAN_t), opt_mname, buf_ptr, size, |
| 175 | BOOLEAN__xer_body_decode); |
| 176 | } |
| 177 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 178 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 179 | BOOLEAN_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 180 | int ilevel, enum xer_encoder_flags_e flags, |
| 181 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 182 | const BOOLEAN_t *st = (const BOOLEAN_t *)sptr; |
| 183 | asn_enc_rval_t er; |
| 184 | |
| 185 | (void)ilevel; |
| 186 | (void)flags; |
| 187 | |
| 188 | if(!st) _ASN_ENCODE_FAILED; |
| 189 | |
| 190 | if(*st) { |
| 191 | _ASN_CALLBACK("<true/>", 7); |
| 192 | er.encoded = 7; |
| 193 | } else { |
| 194 | _ASN_CALLBACK("<false/>", 8); |
| 195 | er.encoded = 8; |
| 196 | } |
| 197 | |
| 198 | return er; |
vlm | 84d551b | 2004-10-03 09:13:02 +0000 | [diff] [blame] | 199 | cb_failed: |
| 200 | _ASN_ENCODE_FAILED; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 201 | } |
| 202 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 203 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 204 | BOOLEAN_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 205 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 206 | const BOOLEAN_t *st = (const BOOLEAN_t *)sptr; |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 207 | const char *buf; |
| 208 | size_t buflen; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 209 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 210 | (void)td; /* Unused argument */ |
| 211 | (void)ilevel; /* Unused argument */ |
| 212 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 213 | if(st) { |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 214 | if(*st) { |
| 215 | buf = "TRUE"; |
| 216 | buflen = 4; |
| 217 | } else { |
| 218 | buf = "FALSE"; |
| 219 | buflen = 5; |
| 220 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 221 | } else { |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 222 | buf = "<absent>"; |
| 223 | buflen = 8; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 224 | } |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 225 | |
| 226 | return (cb(buf, buflen, app_key) < 0) ? -1 : 0; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 230 | BOOLEAN_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 231 | if(td && ptr && !contents_only) { |
| 232 | FREEMEM(ptr); |
| 233 | } |
| 234 | } |
| 235 | |