blob: 84e578584d6adf774afef12c856a542fde047c94 [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001/*-
Lev Walkin59b176e2005-11-26 11:25:14 +00002 * Copyright (c) 2003, 2005 Lev Walkin <vlm@lionet.info>. All rights reserved.
Lev Walkinf15320b2004-06-03 03:38:44 +00003 * Redistribution and modifications are permitted subject to BSD license.
4 */
Lev Walkina9cc46e2004-09-22 16:06:28 +00005#include <asn_internal.h>
Lev Walkin867ac192004-10-22 08:11:25 +00006#include <asn_codecs_prim.h>
Lev Walkinf15320b2004-06-03 03:38:44 +00007#include <BOOLEAN.h>
8
9/*
10 * BOOLEAN basic type description.
11 */
Lev Walkin5e033762004-09-29 13:26:15 +000012static ber_tlv_tag_t asn_DEF_BOOLEAN_tags[] = {
Lev Walkinf15320b2004-06-03 03:38:44 +000013 (ASN_TAG_CLASS_UNIVERSAL | (1 << 2))
14};
Lev Walkin5e033762004-09-29 13:26:15 +000015asn_TYPE_descriptor_t asn_DEF_BOOLEAN = {
Lev Walkinf15320b2004-06-03 03:38:44 +000016 "BOOLEAN",
Lev Walkindc06f6b2004-10-20 15:50:55 +000017 "BOOLEAN",
Lev Walkina9cc46e2004-09-22 16:06:28 +000018 BOOLEAN_free,
19 BOOLEAN_print,
Lev Walkinf15320b2004-06-03 03:38:44 +000020 asn_generic_no_constraint,
21 BOOLEAN_decode_ber,
22 BOOLEAN_encode_der,
Lev Walkin867ac192004-10-22 08:11:25 +000023 BOOLEAN_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000024 BOOLEAN_encode_xer,
Lev Walkin59b176e2005-11-26 11:25:14 +000025 BOOLEAN_decode_uper, /* Unaligned PER decoder */
Lev Walkinf15320b2004-06-03 03:38:44 +000026 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000027 asn_DEF_BOOLEAN_tags,
28 sizeof(asn_DEF_BOOLEAN_tags) / sizeof(asn_DEF_BOOLEAN_tags[0]),
29 asn_DEF_BOOLEAN_tags, /* Same as above */
30 sizeof(asn_DEF_BOOLEAN_tags) / sizeof(asn_DEF_BOOLEAN_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000031 0, /* No PER visible constraints */
Lev Walkin449f8322004-08-20 13:23:42 +000032 0, 0, /* No members */
Lev Walkind9bd7752004-06-05 08:17:50 +000033 0 /* No specifics */
Lev Walkinf15320b2004-06-03 03:38:44 +000034};
35
36/*
37 * Decode BOOLEAN type.
38 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000039asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000040BOOLEAN_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
41 asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +000042 void **bool_value, const void *buf_ptr, size_t size,
Lev Walkinf15320b2004-06-03 03:38:44 +000043 int tag_mode) {
Lev Walkin958d7a82004-08-19 13:26:36 +000044 BOOLEAN_t *st = (BOOLEAN_t *)*bool_value;
Lev Walkindc06f6b2004-10-20 15:50:55 +000045 asn_dec_rval_t rval;
Lev Walkinf15320b2004-06-03 03:38:44 +000046 ber_tlv_len_t length;
47 ber_tlv_len_t lidx;
48
49 if(st == NULL) {
Lev Walkin8e8078a2004-09-26 13:10:40 +000050 st = (BOOLEAN_t *)(*bool_value = CALLOC(1, sizeof(*st)));
Lev Walkinf15320b2004-06-03 03:38:44 +000051 if(st == NULL) {
52 rval.code = RC_FAIL;
53 rval.consumed = 0;
54 return rval;
55 }
56 }
57
58 ASN_DEBUG("Decoding %s as BOOLEAN (tm=%d)",
59 td->name, tag_mode);
60
61 /*
62 * Check tags.
63 */
Lev Walkin5e033762004-09-29 13:26:15 +000064 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
65 tag_mode, 0, &length, 0);
Lev Walkinf15320b2004-06-03 03:38:44 +000066 if(rval.code != RC_OK)
67 return rval;
68
69 ASN_DEBUG("Boolean length is %d bytes", (int)length);
70
Lev Walkin8c3b8542005-03-10 18:52:02 +000071 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
Lev Walkinf15320b2004-06-03 03:38:44 +000072 size -= rval.consumed;
Lev Walkind9bd7752004-06-05 08:17:50 +000073 if(length > (ber_tlv_len_t)size) {
Lev Walkinf15320b2004-06-03 03:38:44 +000074 rval.code = RC_WMORE;
75 rval.consumed = 0;
76 return rval;
77 }
78
79 /*
80 * Compute boolean value.
81 */
Lev Walkin958d7a82004-08-19 13:26:36 +000082 for(*st = 0, lidx = 0;
83 (lidx < length) && *st == 0; lidx++) {
Lev Walkinf15320b2004-06-03 03:38:44 +000084 /*
85 * Very simple approach: read bytes until the end or
86 * value is already TRUE.
87 * BOOLEAN is not supposed to contain meaningful data anyway.
88 */
Lev Walkin8c3b8542005-03-10 18:52:02 +000089 *st |= ((const uint8_t *)buf_ptr)[lidx];
Lev Walkinf15320b2004-06-03 03:38:44 +000090 }
91
92 rval.code = RC_OK;
93 rval.consumed += length;
94
Lev Walkin958d7a82004-08-19 13:26:36 +000095 ASN_DEBUG("Took %ld/%ld bytes to encode %s, value=%d",
Lev Walkinf15320b2004-06-03 03:38:44 +000096 (long)rval.consumed, (long)length,
Lev Walkin958d7a82004-08-19 13:26:36 +000097 td->name, *st);
Lev Walkinf15320b2004-06-03 03:38:44 +000098
99 return rval;
100}
101
Lev Walkina9cc46e2004-09-22 16:06:28 +0000102asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000103BOOLEAN_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkinf15320b2004-06-03 03:38:44 +0000104 int tag_mode, ber_tlv_tag_t tag,
105 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000106 asn_enc_rval_t erval;
Lev Walkinc2346572004-08-11 09:07:36 +0000107 BOOLEAN_t *st = (BOOLEAN_t *)sptr;
Lev Walkinf15320b2004-06-03 03:38:44 +0000108
Lev Walkin8e8078a2004-09-26 13:10:40 +0000109 erval.encoded = der_write_tags(td, 1, tag_mode, 0, tag, cb, app_key);
Lev Walkinf15320b2004-06-03 03:38:44 +0000110 if(erval.encoded == -1) {
111 erval.failed_type = td;
112 erval.structure_ptr = sptr;
113 return erval;
114 }
115
116 if(cb) {
117 uint8_t bool_value;
Lev Walkinf15320b2004-06-03 03:38:44 +0000118
Lev Walkin8e8078a2004-09-26 13:10:40 +0000119 bool_value = *st ? 0xff : 0; /* 0xff mandated by DER */
120
121 if(cb(&bool_value, 1, app_key) < 0) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000122 erval.encoded = -1;
123 erval.failed_type = td;
124 erval.structure_ptr = sptr;
125 return erval;
126 }
127 }
128
129 erval.encoded += 1;
130
Lev Walkin59b176e2005-11-26 11:25:14 +0000131 _ASN_ENCODED_OK(erval);
Lev Walkinf15320b2004-06-03 03:38:44 +0000132}
133
Lev Walkin867ac192004-10-22 08:11:25 +0000134
135/*
136 * Decode the chunk of XML text encoding INTEGER.
137 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000138static enum xer_pbd_rval
139BOOLEAN__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkin867ac192004-10-22 08:11:25 +0000140 BOOLEAN_t *st = (BOOLEAN_t *)sptr;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000141 const char *p = (const char *)chunk_buf;
Lev Walkin867ac192004-10-22 08:11:25 +0000142
Lev Walkine0b56e02005-02-25 12:10:27 +0000143 (void)td;
144
Lev Walkin0fab1a62005-03-09 22:19:25 +0000145 if(chunk_size && p[0] == 0x3c /* '<' */) {
Lev Walkin867ac192004-10-22 08:11:25 +0000146 switch(xer_check_tag(chunk_buf, chunk_size, "false")) {
147 case XCT_BOTH:
148 /* "<false/>" */
149 *st = 0;
150 break;
Lev Walkin1e443962005-02-18 18:06:36 +0000151 case XCT_UNKNOWN_BO:
Lev Walkin867ac192004-10-22 08:11:25 +0000152 if(xer_check_tag(chunk_buf, chunk_size, "true")
153 != XCT_BOTH)
Lev Walkin0fab1a62005-03-09 22:19:25 +0000154 return XPBD_BROKEN_ENCODING;
Lev Walkin867ac192004-10-22 08:11:25 +0000155 /* "<true/>" */
156 *st = 1; /* Or 0xff as in DER?.. */
157 break;
158 default:
Lev Walkin0fab1a62005-03-09 22:19:25 +0000159 return XPBD_BROKEN_ENCODING;
Lev Walkin867ac192004-10-22 08:11:25 +0000160 }
Lev Walkin0fab1a62005-03-09 22:19:25 +0000161 return XPBD_BODY_CONSUMED;
Lev Walkin867ac192004-10-22 08:11:25 +0000162 } else {
Lev Walkin0fab1a62005-03-09 22:19:25 +0000163 if(xer_is_whitespace(chunk_buf, chunk_size))
164 return XPBD_NOT_BODY_IGNORE;
165 else
166 return XPBD_BROKEN_ENCODING;
Lev Walkin867ac192004-10-22 08:11:25 +0000167 }
Lev Walkin867ac192004-10-22 08:11:25 +0000168}
169
170
171asn_dec_rval_t
172BOOLEAN_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
173 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000174 const void *buf_ptr, size_t size) {
Lev Walkin867ac192004-10-22 08:11:25 +0000175
176 return xer_decode_primitive(opt_codec_ctx, td,
177 sptr, sizeof(BOOLEAN_t), opt_mname, buf_ptr, size,
178 BOOLEAN__xer_body_decode);
179}
180
Lev Walkina9cc46e2004-09-22 16:06:28 +0000181asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000182BOOLEAN_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000183 int ilevel, enum xer_encoder_flags_e flags,
184 asn_app_consume_bytes_f *cb, void *app_key) {
185 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
186 asn_enc_rval_t er;
187
188 (void)ilevel;
189 (void)flags;
190
191 if(!st) _ASN_ENCODE_FAILED;
192
193 if(*st) {
194 _ASN_CALLBACK("<true/>", 7);
195 er.encoded = 7;
196 } else {
197 _ASN_CALLBACK("<false/>", 8);
198 er.encoded = 8;
199 }
200
Lev Walkin59b176e2005-11-26 11:25:14 +0000201 _ASN_ENCODED_OK(er);
Lev Walkin942fd082004-10-03 09:13:02 +0000202cb_failed:
203 _ASN_ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000204}
205
Lev Walkinf15320b2004-06-03 03:38:44 +0000206int
Lev Walkin5e033762004-09-29 13:26:15 +0000207BOOLEAN_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkinf15320b2004-06-03 03:38:44 +0000208 asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkinc2346572004-08-11 09:07:36 +0000209 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000210 const char *buf;
211 size_t buflen;
Lev Walkinf15320b2004-06-03 03:38:44 +0000212
Lev Walkind9bd7752004-06-05 08:17:50 +0000213 (void)td; /* Unused argument */
214 (void)ilevel; /* Unused argument */
215
Lev Walkinf15320b2004-06-03 03:38:44 +0000216 if(st) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000217 if(*st) {
218 buf = "TRUE";
219 buflen = 4;
220 } else {
221 buf = "FALSE";
222 buflen = 5;
223 }
Lev Walkinf15320b2004-06-03 03:38:44 +0000224 } else {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000225 buf = "<absent>";
226 buflen = 8;
Lev Walkinf15320b2004-06-03 03:38:44 +0000227 }
Lev Walkin8e8078a2004-09-26 13:10:40 +0000228
229 return (cb(buf, buflen, app_key) < 0) ? -1 : 0;
Lev Walkinf15320b2004-06-03 03:38:44 +0000230}
231
232void
Lev Walkin5e033762004-09-29 13:26:15 +0000233BOOLEAN_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkinf15320b2004-06-03 03:38:44 +0000234 if(td && ptr && !contents_only) {
235 FREEMEM(ptr);
236 }
237}
238
Lev Walkin59b176e2005-11-26 11:25:14 +0000239asn_dec_rval_t
240BOOLEAN_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
241 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
242 asn_dec_rval_t rv;
243 BOOLEAN_t *st = (BOOLEAN_t *)*sptr;
244
245 (void)opt_codec_ctx;
246 (void)constraints;
247
248 if(!st) {
249 st = (BOOLEAN_t *)(*sptr = MALLOC(sizeof(*st)));
250 if(!st) _ASN_DECODE_FAILED;
251 }
252
253 /*
254 * Extract a single bit
255 */
256 switch(per_get_few_bits(pd, 1)) {
257 case 1: *st = 1; break;
258 case 0: *st = 0; break;
259 case -1: default: _ASN_DECODE_FAILED;
260 }
261
262 ASN_DEBUG("%s decoded as %s", td->name, *st ? "TRUE" : "FALSE");
263
264 rv.code = RC_OK;
265 rv.consumed = 1;
266 return rv;
267}
268