blob: 838ba2dddd8444f07bf28010a513050f964bbd6e [file] [log] [blame]
Lev Walkin41ba1f22004-09-14 12:46:35 +00001/*-
Lev Walkin725883b2006-10-09 12:07:58 +00002 * Copyright (c) 2004, 2006 Lev Walkin <vlm@lionet.info>. All rights reserved.
Lev Walkin41ba1f22004-09-14 12:46:35 +00003 * Redistribution and modifications are permitted subject to BSD license.
4 */
5/*
Lev Walkin41635d32006-03-18 05:06:57 +00006 * Read the NativeReal.h for the explanation wrt. differences between
Lev Walkin41ba1f22004-09-14 12:46:35 +00007 * REAL and NativeReal.
8 * Basically, both are decoders and encoders of ASN.1 REAL type, but this
9 * implementation deals with the standard (machine-specific) representation
10 * of them instead of using the platform-independent buffer.
11 */
Lev Walkina9cc46e2004-09-22 16:06:28 +000012#include <asn_internal.h>
Lev Walkin41ba1f22004-09-14 12:46:35 +000013#include <NativeReal.h>
Lev Walkin41ba1f22004-09-14 12:46:35 +000014#include <REAL.h>
Lev Walkin725883b2006-10-09 12:07:58 +000015#include <OCTET_STRING.h>
Lev Walkincd2f48e2017-08-10 02:14:59 -070016#include <math.h>
Lev Walkin41ba1f22004-09-14 12:46:35 +000017
18/*
19 * NativeReal basic type description.
20 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070021static const ber_tlv_tag_t asn_DEF_NativeReal_tags[] = {
Lev Walkin41ba1f22004-09-14 12:46:35 +000022 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
23};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080024asn_TYPE_operation_t asn_OP_NativeReal = {
Lev Walkina9cc46e2004-09-22 16:06:28 +000025 NativeReal_free,
26 NativeReal_print,
Lev Walkincd2f48e2017-08-10 02:14:59 -070027 NativeReal_compare,
Lev Walkin41ba1f22004-09-14 12:46:35 +000028 asn_generic_no_constraint,
29 NativeReal_decode_ber,
30 NativeReal_encode_der,
Lev Walkin8471cec2004-10-21 14:02:19 +000031 NativeReal_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000032 NativeReal_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070033#ifdef ASN_DISABLE_OER_SUPPORT
34 0,
35 0,
36#else
37 0,
38 0,
39#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040040#ifdef ASN_DISABLE_PER_SUPPORT
41 0,
42 0,
43#else
44 NativeReal_decode_uper,
45 NativeReal_encode_uper,
46#endif /* ASN_DISABLE_PER_SUPPORT */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080047 0 /* Use generic outmost tag fetcher */
48};
49asn_TYPE_descriptor_t asn_DEF_NativeReal = {
50 "REAL", /* The ASN.1 type is still REAL */
51 "REAL",
52 &asn_OP_NativeReal,
53 asn_generic_no_constraint,
Lev Walkin5e033762004-09-29 13:26:15 +000054 asn_DEF_NativeReal_tags,
55 sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]),
56 asn_DEF_NativeReal_tags, /* Same as above */
57 sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070058 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000059 0, /* No PER visible constraints */
Lev Walkin41ba1f22004-09-14 12:46:35 +000060 0, 0, /* No members */
61 0 /* No specifics */
62};
63
64/*
65 * Decode REAL type.
66 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000067asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000068NativeReal_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
69 asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +000070 void **dbl_ptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkin41ba1f22004-09-14 12:46:35 +000071 double *Dbl = (double *)*dbl_ptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +000072 asn_dec_rval_t rval;
Lev Walkin41ba1f22004-09-14 12:46:35 +000073 ber_tlv_len_t length;
74
75 /*
76 * If the structure is not there, allocate it.
77 */
78 if(Dbl == NULL) {
Lev Walkinc17d90f2005-01-17 14:32:45 +000079 *dbl_ptr = CALLOC(1, sizeof(*Dbl));
80 Dbl = (double *)*dbl_ptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +000081 if(Dbl == NULL) {
82 rval.code = RC_FAIL;
83 rval.consumed = 0;
84 return rval;
85 }
86 }
87
88 ASN_DEBUG("Decoding %s as REAL (tm=%d)",
89 td->name, tag_mode);
90
91 /*
92 * Check tags.
93 */
Lev Walkin5e033762004-09-29 13:26:15 +000094 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
95 tag_mode, 0, &length, 0);
Lev Walkin41ba1f22004-09-14 12:46:35 +000096 if(rval.code != RC_OK)
97 return rval;
98
99 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
100
101 /*
102 * Make sure we have this length.
103 */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000104 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000105 size -= rval.consumed;
106 if(length > (ber_tlv_len_t)size) {
107 rval.code = RC_WMORE;
108 rval.consumed = 0;
109 return rval;
110 }
111
112 /*
113 * ASN.1 encoded REAL: buf_ptr, length
114 * Fill the Dbl, at the same time checking for overflow.
115 * If overflow occured, return with RC_FAIL.
116 */
117 {
118 REAL_t tmp;
Lev Walkin7e033b52005-07-02 08:19:17 +0000119 union {
120 const void *constbuf;
121 void *nonconstbuf;
122 } unconst_buf;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000123 double d;
Lev Walkin7e033b52005-07-02 08:19:17 +0000124
125 unconst_buf.constbuf = buf_ptr;
126 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000127 tmp.size = length;
128
Lev Walkin0959ffb2011-06-25 15:48:52 -0700129 if(length < (ber_tlv_len_t)size) {
130 int ret;
131 uint8_t saved_byte = tmp.buf[tmp.size];
132 tmp.buf[tmp.size] = '\0';
133 ret = asn_REAL2double(&tmp, &d);
134 tmp.buf[tmp.size] = saved_byte;
135 if(ret) {
136 rval.code = RC_FAIL;
137 rval.consumed = 0;
138 return rval;
139 }
140 } else if(length < 48 /* Enough for longish %f value. */) {
141 tmp.buf = alloca(length + 1);
142 tmp.size = length;
143 memcpy(tmp.buf, buf_ptr, length);
144 tmp.buf[tmp.size] = '\0';
145 if(asn_REAL2double(&tmp, &d)) {
146 rval.code = RC_FAIL;
147 rval.consumed = 0;
148 return rval;
149 }
150 } else {
151 /* This should probably never happen: impractically long value */
152 tmp.buf = CALLOC(1, length + 1);
153 tmp.size = length;
154 if(tmp.buf) memcpy(tmp.buf, buf_ptr, length);
155 if(!tmp.buf || asn_REAL2double(&tmp, &d)) {
156 FREEMEM(tmp.buf);
157 rval.code = RC_FAIL;
158 rval.consumed = 0;
159 return rval;
160 }
161 FREEMEM(tmp.buf);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000162 }
163
164 *Dbl = d;
165 }
166
167 rval.code = RC_OK;
168 rval.consumed += length;
169
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000170 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%f)",
Lev Walkin41ba1f22004-09-14 12:46:35 +0000171 (long)rval.consumed, (long)length, td->name, *Dbl);
172
173 return rval;
174}
175
176/*
177 * Encode the NativeReal using the standard REAL type DER encoder.
178 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000179asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000180NativeReal_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000181 int tag_mode, ber_tlv_tag_t tag,
182 asn_app_consume_bytes_f *cb, void *app_key) {
183 double Dbl = *(const double *)ptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000184 asn_enc_rval_t erval;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000185 REAL_t tmp;
186
Lev Walkina8bbbda2005-02-06 04:29:03 +0000187 /* Prepare a temporary clean structure */
188 memset(&tmp, 0, sizeof(tmp));
189
Lev Walkin5e033762004-09-29 13:26:15 +0000190 if(asn_double2REAL(&tmp, Dbl)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000191 erval.encoded = -1;
192 erval.failed_type = td;
193 erval.structure_ptr = ptr;
194 return erval;
195 }
196
Lev Walkin8e8078a2004-09-26 13:10:40 +0000197 /* Encode a fake REAL */
198 erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000199 if(erval.encoded == -1) {
200 assert(erval.structure_ptr == &tmp);
201 erval.structure_ptr = ptr;
202 }
Lev Walkina8bbbda2005-02-06 04:29:03 +0000203
204 /* Free possibly allocated members of the temporary structure */
Lev Walkinadcb5862006-03-17 02:11:12 +0000205 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
Lev Walkina8bbbda2005-02-06 04:29:03 +0000206
Lev Walkin41ba1f22004-09-14 12:46:35 +0000207 return erval;
208}
209
Lev Walkin41d972b2017-08-23 23:30:59 -0700210#ifndef ASN_DISABLE_PER_SUPPORT
211
Lev Walkin725883b2006-10-09 12:07:58 +0000212/*
213 * Decode REAL type using PER.
214 */
215asn_dec_rval_t
216NativeReal_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin494fb702017-08-07 20:07:00 -0700217 asn_TYPE_descriptor_t *td,
218 const asn_per_constraints_t *constraints, void **dbl_ptr,
219 asn_per_data_t *pd) {
220 double *Dbl = (double *)*dbl_ptr;
Lev Walkin725883b2006-10-09 12:07:58 +0000221 asn_dec_rval_t rval;
222 REAL_t tmp;
223 void *ptmp = &tmp;
224 int ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000225
Lev Walkin725883b2006-10-09 12:07:58 +0000226 (void)constraints;
227
228 /*
229 * If the structure is not there, allocate it.
230 */
231 if(Dbl == NULL) {
232 *dbl_ptr = CALLOC(1, sizeof(*Dbl));
233 Dbl = (double *)*dbl_ptr;
234 if(Dbl == NULL)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700235 ASN__DECODE_FAILED;
Lev Walkin725883b2006-10-09 12:07:58 +0000236 }
237
238 memset(&tmp, 0, sizeof(tmp));
239 rval = OCTET_STRING_decode_uper(opt_codec_ctx, td, NULL,
240 &ptmp, pd);
241 if(rval.code != RC_OK) {
242 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
243 return rval;
244 }
245
246 ret = asn_REAL2double(&tmp, Dbl);
247 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700248 if(ret) ASN__DECODE_FAILED;
Lev Walkin725883b2006-10-09 12:07:58 +0000249
250 return rval;
251}
252
253/*
254 * Encode the NativeReal using the OCTET STRING PER encoder.
255 */
256asn_enc_rval_t
257NativeReal_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700258 const asn_per_constraints_t *constraints, void *sptr,
259 asn_per_outp_t *po) {
260 double Dbl = *(const double *)sptr;
Lev Walkin725883b2006-10-09 12:07:58 +0000261 asn_enc_rval_t erval;
262 REAL_t tmp;
263
264 (void)constraints;
265
266 /* Prepare a temporary clean structure */
267 memset(&tmp, 0, sizeof(tmp));
268
269 if(asn_double2REAL(&tmp, Dbl))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700270 ASN__ENCODE_FAILED;
Lev Walkin725883b2006-10-09 12:07:58 +0000271
272 /* Encode a DER REAL */
273 erval = OCTET_STRING_encode_uper(td, NULL, &tmp, po);
274 if(erval.encoded == -1)
275 erval.structure_ptr = sptr;
276
277 /* Free possibly allocated members of the temporary structure */
278 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
279
280 return erval;
281}
Lev Walkin8471cec2004-10-21 14:02:19 +0000282
Lev Walkin41d972b2017-08-23 23:30:59 -0700283#endif /* ASN_DISABLE_PER_SUPPORT */
284
Lev Walkin8471cec2004-10-21 14:02:19 +0000285/*
286 * Decode the chunk of XML text encoding REAL.
287 */
288asn_dec_rval_t
289NativeReal_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
290 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000291 const void *buf_ptr, size_t size) {
Lev Walkin8471cec2004-10-21 14:02:19 +0000292 asn_dec_rval_t rval;
293 REAL_t *st = 0;
Lev Walkin1eded352006-07-13 11:19:01 +0000294 REAL_t **stp = &st;
Lev Walkin8471cec2004-10-21 14:02:19 +0000295 double *Dbl = (double *)*sptr;
296
297 if(!Dbl) {
Lev Walkinc17d90f2005-01-17 14:32:45 +0000298 *sptr = CALLOC(1, sizeof(double));
299 Dbl = (double *)*sptr;
Lev Walkin8471cec2004-10-21 14:02:19 +0000300 if(!Dbl) {
301 rval.code = RC_FAIL;
302 rval.consumed = 0;
303 return rval;
304 }
305 }
306
Lev Walkin1eded352006-07-13 11:19:01 +0000307 rval = REAL_decode_xer(opt_codec_ctx, td, (void **)stp, opt_mname,
Lev Walkin8471cec2004-10-21 14:02:19 +0000308 buf_ptr, size);
309 if(rval.code == RC_OK) {
310 if(asn_REAL2double(st, Dbl)) {
311 rval.code = RC_FAIL;
312 rval.consumed = 0;
313 }
314 } else {
315 rval.consumed = 0;
316 }
Lev Walkinadcb5862006-03-17 02:11:12 +0000317 ASN_STRUCT_FREE(asn_DEF_REAL, st);
Lev Walkin8471cec2004-10-21 14:02:19 +0000318 return rval;
319}
320
Lev Walkina9cc46e2004-09-22 16:06:28 +0000321asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000322NativeReal_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000323 int ilevel, enum xer_encoder_flags_e flags,
324 asn_app_consume_bytes_f *cb, void *app_key) {
325 const double *Dbl = (const double *)sptr;
326 asn_enc_rval_t er;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000327
328 (void)ilevel;
329
Lev Walkin7c1dc052016-03-14 03:08:15 -0700330 if(!Dbl) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000331
Lev Walkin8e8078a2004-09-26 13:10:40 +0000332 er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700333 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000334
Lev Walkin7c1dc052016-03-14 03:08:15 -0700335 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000336}
337
Lev Walkin41ba1f22004-09-14 12:46:35 +0000338/*
339 * REAL specific human-readable output.
340 */
341int
Lev Walkin5e033762004-09-29 13:26:15 +0000342NativeReal_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000343 asn_app_consume_bytes_f *cb, void *app_key) {
344 const double *Dbl = (const double *)sptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000345
346 (void)td; /* Unused argument */
347 (void)ilevel; /* Unused argument */
348
Lev Walkin8e8078a2004-09-26 13:10:40 +0000349 if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000350
351 return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000352}
353
Lev Walkincd2f48e2017-08-10 02:14:59 -0700354int
355NativeReal_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
356 const void *bptr) {
357 const double *a = aptr;
358 const double *b = bptr;
359 (void)td;
360
361 if(a && b) {
362 /* NaN sorted above everything else */
363 if(isnan(*a)) {
364 if(isnan(*b)) {
365 return 0;
366 } else {
367 return -1;
368 }
369 } else if(isnan(*b)) {
370 return 1;
371 }
372 /* Value comparison. */
373 if(*a < *b) {
374 return -1;
375 } else if(*a > *b) {
376 return 1;
377 } else {
378 return 0;
379 }
380 } else if(!a) {
381 return -1;
382 } else {
383 return 1;
384 }
385}
386
Lev Walkin41ba1f22004-09-14 12:46:35 +0000387void
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700388NativeReal_free(const asn_TYPE_descriptor_t *td, void *ptr,
389 enum asn_struct_free_method method) {
Lev Walkinf6853ce2017-08-11 00:50:27 -0700390 if(!td || !ptr)
Lev Walkin41ba1f22004-09-14 12:46:35 +0000391 return;
392
393 ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)",
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700394 td->name, method, ptr);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000395
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700396 switch(method) {
397 case ASFM_FREE_EVERYTHING:
398 FREEMEM(ptr);
399 break;
400 case ASFM_FREE_UNDERLYING:
401 break;
402 case ASFM_FREE_UNDERLYING_AND_RESET:
403 memset(ptr, 0, sizeof(double));
404 break;
405 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000406}
407