ITS
BOOLEAN.c
Go to the documentation of this file.
1 /*-
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  */
13  (ASN_TAG_CLASS_UNIVERSAL | (1 << 2))
14 };
16  "BOOLEAN",
17  "BOOLEAN",
25  BOOLEAN_decode_uper, /* Unaligned PER decoder */
26  BOOLEAN_encode_uper, /* Unaligned PER encoder */
27  0, /* Use generic outmost tag fetcher */
29  sizeof(asn_DEF_BOOLEAN_tags) / sizeof(asn_DEF_BOOLEAN_tags[0]),
30  asn_DEF_BOOLEAN_tags, /* Same as above */
31  sizeof(asn_DEF_BOOLEAN_tags) / sizeof(asn_DEF_BOOLEAN_tags[0]),
32  0, /* No PER visible constraints */
33  0, 0, /* No members */
34  0 /* No specifics */
35 };
36 
37 /*
38  * Decode BOOLEAN type.
39  */
43  void **bool_value, const void *buf_ptr, size_t size,
44  int tag_mode) {
45  BOOLEAN_t *st = (BOOLEAN_t *)*bool_value;
46  asn_dec_rval_t rval;
47  ber_tlv_len_t length;
48  ber_tlv_len_t lidx;
49 
50  if(st == NULL) {
51  st = (BOOLEAN_t *)(*bool_value = CALLOC(1, sizeof(*st)));
52  if(st == NULL) {
53  rval.code = RC_FAIL;
54  rval.consumed = 0;
55  return rval;
56  }
57  }
58 
59  ASN_DEBUG("Decoding %s as BOOLEAN (tm=%d)",
60  td->name, tag_mode);
61 
62  /*
63  * Check tags.
64  */
65  rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
66  tag_mode, 0, &length, 0);
67  if(rval.code != RC_OK)
68  return rval;
69 
70  ASN_DEBUG("Boolean length is %d bytes", (int)length);
71 
72  buf_ptr = ((const char *)buf_ptr) + rval.consumed;
73  size -= rval.consumed;
74  if(length > (ber_tlv_len_t)size) {
75  rval.code = RC_WMORE;
76  rval.consumed = 0;
77  return rval;
78  }
79 
80  /*
81  * Compute boolean value.
82  */
83  for(*st = 0, lidx = 0;
84  (lidx < length) && *st == 0; lidx++) {
85  /*
86  * Very simple approach: read bytes until the end or
87  * value is already TRUE.
88  * BOOLEAN is not supposed to contain meaningful data anyway.
89  */
90  *st |= ((const uint8_t *)buf_ptr)[lidx];
91  }
92 
93  rval.code = RC_OK;
94  rval.consumed += length;
95 
96  ASN_DEBUG("Took %ld/%ld bytes to encode %s, value=%d",
97  (long)rval.consumed, (long)length,
98  td->name, *st);
99 
100  return rval;
101 }
102 
105  int tag_mode, ber_tlv_tag_t tag,
106  asn_app_consume_bytes_f *cb, void *app_key) {
107  asn_enc_rval_t erval;
108  BOOLEAN_t *st = (BOOLEAN_t *)sptr;
109 
110  erval.encoded = der_write_tags(td, 1, tag_mode, 0, tag, cb, app_key);
111  if(erval.encoded == -1) {
112  erval.failed_type = td;
113  erval.structure_ptr = sptr;
114  return erval;
115  }
116 
117  if(cb) {
118  uint8_t bool_value;
119 
120  bool_value = *st ? 0xff : 0; /* 0xff mandated by DER */
121 
122  if(cb(&bool_value, 1, app_key) < 0) {
123  erval.encoded = -1;
124  erval.failed_type = td;
125  erval.structure_ptr = sptr;
126  return erval;
127  }
128  }
129 
130  erval.encoded += 1;
131 
132  _ASN_ENCODED_OK(erval);
133 }
134 
135 
136 /*
137  * Decode the chunk of XML text encoding INTEGER.
138  */
139 static enum xer_pbd_rval
140 BOOLEAN__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
141  BOOLEAN_t *st = (BOOLEAN_t *)sptr;
142  const char *p = (const char *)chunk_buf;
143 
144  (void)td;
145 
146  if(chunk_size && p[0] == 0x3c /* '<' */) {
147  switch(xer_check_tag(chunk_buf, chunk_size, "false")) {
148  case XCT_BOTH:
149  /* "<false/>" */
150  *st = 0;
151  break;
152  case XCT_UNKNOWN_BO:
153  if(xer_check_tag(chunk_buf, chunk_size, "true")
154  != XCT_BOTH)
155  return XPBD_BROKEN_ENCODING;
156  /* "<true/>" */
157  *st = 1; /* Or 0xff as in DER?.. */
158  break;
159  default:
160  return XPBD_BROKEN_ENCODING;
161  }
162  return XPBD_BODY_CONSUMED;
163  } else {
164  return XPBD_BROKEN_ENCODING;
165  }
166 }
167 
168 
171  asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
172  const void *buf_ptr, size_t size) {
173 
174  return xer_decode_primitive(opt_codec_ctx, td,
175  sptr, sizeof(BOOLEAN_t), opt_mname, buf_ptr, size,
177 }
178 
181  int ilevel, enum xer_encoder_flags_e flags,
182  asn_app_consume_bytes_f *cb, void *app_key) {
183  const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
184  asn_enc_rval_t er;
185 
186  (void)ilevel;
187  (void)flags;
188 
189  if(!st) _ASN_ENCODE_FAILED;
190 
191  if(*st) {
192  _ASN_CALLBACK("<true/>", 7);
193  er.encoded = 7;
194  } else {
195  _ASN_CALLBACK("<false/>", 8);
196  er.encoded = 8;
197  }
198 
199  _ASN_ENCODED_OK(er);
200 cb_failed:
202 }
203 
204 int
205 BOOLEAN_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
206  asn_app_consume_bytes_f *cb, void *app_key) {
207  const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
208  const char *buf;
209  size_t buflen;
210 
211  (void)td; /* Unused argument */
212  (void)ilevel; /* Unused argument */
213 
214  if(st) {
215  if(*st) {
216  buf = "TRUE";
217  buflen = 4;
218  } else {
219  buf = "FALSE";
220  buflen = 5;
221  }
222  } else {
223  buf = "<absent>";
224  buflen = 8;
225  }
226 
227  return (cb(buf, buflen, app_key) < 0) ? -1 : 0;
228 }
229 
230 void
231 BOOLEAN_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
232  if(td && ptr && !contents_only) {
233  FREEMEM(ptr);
234  }
235 }
236 
239  asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd) {
240  asn_dec_rval_t rv;
241  BOOLEAN_t *st = (BOOLEAN_t *)*sptr;
242 
243  (void)opt_codec_ctx;
244  (void)constraints;
245 
246  if(!st) {
247  st = (BOOLEAN_t *)(*sptr = MALLOC(sizeof(*st)));
248  if(!st) _ASN_DECODE_FAILED;
249  }
250 
251  /*
252  * Extract a single bit
253  */
254  switch(per_get_few_bits(pd, 1)) {
255  case 1: *st = 1; break;
256  case 0: *st = 0; break;
257  case -1: default: _ASN_DECODE_STARVED;
258  }
259 
260  ASN_DEBUG("%s decoded as %s", td->name, *st ? "TRUE" : "FALSE");
261 
262  rv.code = RC_OK;
263  rv.consumed = 1;
264  return rv;
265 }
266 
267 
270  asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
271  const BOOLEAN_t *st = (const BOOLEAN_t *)sptr;
272  asn_enc_rval_t er = { 0, 0, 0 };
273 
274  (void)constraints;
275 
276  if(!st) _ASN_ENCODE_FAILED;
277 
278  if(per_put_few_bits(po, *st ? 1 : 0, 1))
280 
281  _ASN_ENCODED_OK(er);
282 }
static enum xer_pbd_rval BOOLEAN__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size)
Definition: BOOLEAN.c:140
xer_check_tag
Definition: xer_decoder.h:76
ssize_t der_write_tags(struct asn_TYPE_descriptor_s *type_descriptor, size_t struct_length, int tag_mode, int last_tag_form, ber_tlv_tag_t tag, asn_app_consume_bytes_f *consume_bytes_cb, void *app_key)
Definition: der_encoder.c:77
int BOOLEAN_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, asn_app_consume_bytes_f *cb, void *app_key)
Definition: BOOLEAN.c:205
void * structure_ptr
Definition: asn_codecs.h:58
struct asn_TYPE_descriptor_s * failed_type
Definition: asn_codecs.h:55
asn_enc_rval_t BOOLEAN_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, int ilevel, enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb, void *app_key)
Definition: BOOLEAN.c:180
asn_dec_rval_t xer_decode_primitive(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *type_descriptor, void **struct_ptr, size_t struct_size, const char *opt_mname, const void *buf_ptr, size_t size, xer_primitive_body_decoder_f *prim_body_decoder)
asn_dec_rval_t BOOLEAN_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, void **bool_value, const void *buf_ptr, size_t size, int tag_mode)
Definition: BOOLEAN.c:41
#define _ASN_DECODE_STARVED
Definition: asn_codecs.h:98
asn_dec_rval_t BOOLEAN_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints, void **sptr, asn_per_data_t *pd)
Definition: BOOLEAN.c:238
int per_put_few_bits(asn_per_outp_t *per_data, uint32_t bits, int obits)
Definition: per_support.c:323
static ber_tlv_tag_t asn_DEF_BOOLEAN_tags[]
Definition: BOOLEAN.c:12
xer_encoder_flags_e
Definition: xer_encoder.h:17
asn_constr_check_f asn_generic_no_constraint
Definition: constraints.h:51
int BOOLEAN_t
Definition: BOOLEAN.h:19
ssize_t encoded
Definition: asn_codecs.h:48
ssize_t ber_tlv_len_t
#define CALLOC(nmemb, size)
Definition: asn_internal.h:26
asn_TYPE_descriptor_t asn_DEF_BOOLEAN
Definition: BOOLEAN.c:15
#define _ASN_CALLBACK(buf, size)
Definition: asn_internal.h:73
#define _ASN_ENCODED_OK(rval)
Definition: asn_codecs.h:68
asn_dec_rval_t ber_check_tags(struct asn_codec_ctx_s *opt_codec_ctx, struct asn_TYPE_descriptor_s *type_descriptor, asn_struct_ctx_t *opt_ctx, const void *ptr, size_t size, int tag_mode, int last_tag_form, ber_tlv_len_t *last_length, int *opt_tlv_form)
Definition: ber_decoder.c:65
#define FREEMEM(ptr)
Definition: asn_internal.h:29
#define _ASN_ENCODE_FAILED
Definition: asn_codecs.h:60
xer_pbd_rval
asn_enc_rval_t BOOLEAN_encode_der(asn_TYPE_descriptor_t *td, void *sptr, int tag_mode, ber_tlv_tag_t tag, asn_app_consume_bytes_f *cb, void *app_key)
Definition: BOOLEAN.c:104
void BOOLEAN_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only)
Definition: BOOLEAN.c:231
int() asn_app_consume_bytes_f(const void *buffer, size_t size, void *application_specific_key)
int32_t per_get_few_bits(asn_per_data_t *per_data, int get_nbits)
Definition: per_support.c:38
size_t consumed
Definition: asn_codecs.h:89
enum asn_dec_rval_code_e code
Definition: asn_codecs.h:88
static void ASN_DEBUG(const char *fmt,...)
Definition: asn_internal.h:62
asn_dec_rval_t BOOLEAN_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, const void *buf_ptr, size_t size)
Definition: BOOLEAN.c:170
#define MALLOC(size)
Definition: asn_internal.h:27
asn_enc_rval_t BOOLEAN_encode_uper(asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po)
Definition: BOOLEAN.c:269
#define _ASN_DECODE_FAILED
Definition: asn_codecs.h:91
unsigned ber_tlv_tag_t
Definition: ber_tlv_tag.h:18