blob: 42374b815a07b8644566de6d121b2de09ad6a0c3 [file] [log] [blame]
Harald Welte92c45f32010-06-12 18:59:38 +02001/*-
2 * Copyright (c) 2003, 2005 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5#include <asn_internal.h>
6#include <asn_codecs_prim.h>
7#include <BOOLEAN.h>
8
9/*
10 * BOOLEAN basic type description.
11 */
Harald Welte41b85d52015-08-31 08:56:53 +020012static const ber_tlv_tag_t asn_DEF_BOOLEAN_tags[] = {
Harald Welte92c45f32010-06-12 18:59:38 +020013 (ASN_TAG_CLASS_UNIVERSAL | (1 << 2))
14};
15asn_TYPE_descriptor_t asn_DEF_BOOLEAN = {
16 "BOOLEAN",
17 "BOOLEAN",
18 BOOLEAN_free,
19 BOOLEAN_print,
20 asn_generic_no_constraint,
21 BOOLEAN_decode_ber,
22 BOOLEAN_encode_der,
23 BOOLEAN_decode_xer,
24 BOOLEAN_encode_xer,
25 BOOLEAN_decode_uper, /* Unaligned PER decoder */
26 BOOLEAN_encode_uper, /* Unaligned PER encoder */
Harald Welte41b85d52015-08-31 08:56:53 +020027 BOOLEAN_decode_aper, /* Aligned PER decoder */
28 BOOLEAN_encode_aper, /* Aligned PER encoder */
Harald Welte92c45f32010-06-12 18:59:38 +020029 0, /* Use generic outmost tag fetcher */
30 asn_DEF_BOOLEAN_tags,
31 sizeof(asn_DEF_BOOLEAN_tags) / sizeof(asn_DEF_BOOLEAN_tags[0]),
32 asn_DEF_BOOLEAN_tags, /* Same as above */
33 sizeof(asn_DEF_BOOLEAN_tags) / sizeof(asn_DEF_BOOLEAN_tags[0]),
34 0, /* No PER visible constraints */
35 0, 0, /* No members */
36 0 /* No specifics */
37};
38
39/*
40 * Decode BOOLEAN type.
41 */
42asn_dec_rval_t
43BOOLEAN_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
44 asn_TYPE_descriptor_t *td,
45 void **bool_value, const void *buf_ptr, size_t size,
46 int tag_mode) {
47 BOOLEAN_t *st = (BOOLEAN_t *)*bool_value;
48 asn_dec_rval_t rval;
49 ber_tlv_len_t length;
50 ber_tlv_len_t lidx;
51
52 if(st == NULL) {
53 st = (BOOLEAN_t *)(*bool_value = CALLOC(1, sizeof(*st)));
54 if(st == NULL) {
55 rval.code = RC_FAIL;
56 rval.consumed = 0;
57 return rval;
58 }
59 }
60
61 ASN_DEBUG("Decoding %s as BOOLEAN (tm=%d)",
62 td->name, tag_mode);
63
64 /*
65 * Check tags.
66 */
67 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
68 tag_mode, 0, &length, 0);
69 if(rval.code != RC_OK)
70 return rval;
71
72 ASN_DEBUG("Boolean length is %d bytes", (int)length);
73
74 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
75 size -= rval.consumed;
76 if(length > (ber_tlv_len_t)size) {
77 rval.code = RC_WMORE;
78 rval.consumed = 0;
79 return rval;
80 }
81
82 /*
83 * Compute boolean value.
84 */
85 for(*st = 0, lidx = 0;
86 (lidx < length) && *st == 0; lidx++) {
87 /*
88 * Very simple approach: read bytes until the end or
89 * value is already TRUE.
90 * BOOLEAN is not supposed to contain meaningful data anyway.
91 */
92 *st |= ((const uint8_t *)buf_ptr)[lidx];
93 }
94
95 rval.code = RC_OK;
96 rval.consumed += length;
97
98 ASN_DEBUG("Took %ld/%ld bytes to encode %s, value=%d",
99 (long)rval.consumed, (long)length,
100 td->name, *st);
101
102 return rval;
103}
104
105asn_enc_rval_t
106BOOLEAN_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
107 int tag_mode, ber_tlv_tag_t tag,
108 asn_app_consume_bytes_f *cb, void *app_key) {
109 asn_enc_rval_t erval;
110 BOOLEAN_t *st = (BOOLEAN_t *)sptr;
111
112 erval.encoded = der_write_tags(td, 1, tag_mode, 0, tag, cb, app_key);
113 if(erval.encoded == -1) {
114 erval.failed_type = td;
115 erval.structure_ptr = sptr;
116 return erval;
117 }
118
119 if(cb) {
120 uint8_t bool_value;
121
122 bool_value = *st ? 0xff : 0; /* 0xff mandated by DER */
123
124 if(cb(&bool_value, 1, app_key) < 0) {
125 erval.encoded = -1;
126 erval.failed_type = td;
127 erval.structure_ptr = sptr;
128 return erval;
129 }
130 }
131
132 erval.encoded += 1;
133
134 _ASN_ENCODED_OK(erval);
135}
136
137
138/*
139 * Decode the chunk of XML text encoding INTEGER.
140 */
141static enum xer_pbd_rval
142BOOLEAN__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
143 BOOLEAN_t *st = (BOOLEAN_t *)sptr;
144 const char *p = (const char *)chunk_buf;
145
146 (void)td;
147
148 if(chunk_size && p[0] == 0x3c /* '<' */) {
149 switch(xer_check_tag(chunk_buf, chunk_size, "false")) {
150 case XCT_BOTH:
151 /* "<false/>" */
152 *st = 0;
153 break;
154 case XCT_UNKNOWN_BO:
155 if(xer_check_tag(chunk_buf, chunk_size, "true")
156 != XCT_BOTH)
157 return XPBD_BROKEN_ENCODING;
158 /* "<true/>" */
159 *st = 1; /* Or 0xff as in DER?.. */
160 break;
161 default:
162 return XPBD_BROKEN_ENCODING;
163 }
164 return XPBD_BODY_CONSUMED;
165 } else {
Harald Welte41b85d52015-08-31 08:56:53 +0200166 return XPBD_BROKEN_ENCODING;
Harald Welte92c45f32010-06-12 18:59:38 +0200167 }
168}
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,
174 const void *buf_ptr, size_t size) {
175
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
181asn_enc_rval_t
182BOOLEAN_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
183 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
201 _ASN_ENCODED_OK(er);
202cb_failed:
203 _ASN_ENCODE_FAILED;
204}
205
206int
207BOOLEAN_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
208 asn_app_consume_bytes_f *cb, void *app_key) {
209 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
210 const char *buf;
211 size_t buflen;
212
213 (void)td; /* Unused argument */
214 (void)ilevel; /* Unused argument */
215
216 if(st) {
217 if(*st) {
218 buf = "TRUE";
219 buflen = 4;
220 } else {
221 buf = "FALSE";
222 buflen = 5;
223 }
224 } else {
225 buf = "<absent>";
226 buflen = 8;
227 }
228
229 return (cb(buf, buflen, app_key) < 0) ? -1 : 0;
230}
231
232void
233BOOLEAN_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
234 if(td && ptr && !contents_only) {
235 FREEMEM(ptr);
236 }
237}
238
239asn_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;
Harald Welteec0e2172010-07-20 00:03:44 +0200259 case -1: default: _ASN_DECODE_STARVED;
Harald Welte92c45f32010-06-12 18:59:38 +0200260 }
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
Harald Welte41b85d52015-08-31 08:56:53 +0200269asn_dec_rval_t
270BOOLEAN_decode_aper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
271 asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
272 asn_dec_rval_t rv;
273 BOOLEAN_t *st = (BOOLEAN_t *)*sptr;
274
275 (void)opt_codec_ctx;
276 (void)constraints;
277
278 if(!st) {
279 st = (BOOLEAN_t *)(*sptr = MALLOC(sizeof(*st)));
280 if(!st) _ASN_DECODE_FAILED;
281 }
282
283 /*
284 * Extract a single bit
285 */
286 switch(per_get_few_bits(pd, 1)) {
287 case 1: *st = 1; break;
288 case 0: *st = 0; break;
289 case -1: default: _ASN_DECODE_STARVED;
290 }
291
292 ASN_DEBUG("%s decoded as %s", td->name, *st ? "TRUE" : "FALSE");
293
294 rv.code = RC_OK;
295 rv.consumed = 1;
296 return rv;
297}
Harald Welte92c45f32010-06-12 18:59:38 +0200298
299asn_enc_rval_t
300BOOLEAN_encode_uper(asn_TYPE_descriptor_t *td,
301 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
302 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
Harald Welte41b85d52015-08-31 08:56:53 +0200303 asn_enc_rval_t er = { 0, 0, 0 };
Harald Welte92c45f32010-06-12 18:59:38 +0200304
305 (void)constraints;
306
307 if(!st) _ASN_ENCODE_FAILED;
308
Harald Welte41b85d52015-08-31 08:56:53 +0200309 if(per_put_few_bits(po, *st ? 1 : 0, 1))
310 _ASN_ENCODE_FAILED;
Harald Welte92c45f32010-06-12 18:59:38 +0200311
312 _ASN_ENCODED_OK(er);
313}
Harald Welte41b85d52015-08-31 08:56:53 +0200314
315asn_enc_rval_t
316BOOLEAN_encode_aper(asn_TYPE_descriptor_t *td,
317 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
318 const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
319 asn_enc_rval_t er;
320
321 (void)constraints;
322
323 if(!st) _ASN_ENCODE_FAILED;
324
325 per_put_few_bits(po, *st ? 1 : 0, 1);
326
327 _ASN_ENCODED_OK(er);
328}