blob: aba278c0612a792d90d3c77d42dde9c3ea2de05b [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
Lev Walkineff98a52017-09-13 22:24:35 +000018#if defined(__clang__)
19/*
20 * isnan() is defined using generic selections and won't compile in
21 * strict C89 mode because of too fancy system's standard library.
22 * However, prior to C11 the math had a perfectly working isnan()
23 * in the math library.
24 * Disable generic selection warning so we can test C89 mode with newer libc.
25 */
26#pragma clang diagnostic push
27#pragma clang diagnostic ignored "-Wc11-extensions"
28static int asn_isnan(double d) {
29 return isnan(d);
30}
31#pragma clang diagnostic pop
32#else
33#define asn_isnan(v) isnan(v)
34#endif /* generic selections */
35
Lev Walkin41ba1f22004-09-14 12:46:35 +000036/*
37 * NativeReal basic type description.
38 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070039static const ber_tlv_tag_t asn_DEF_NativeReal_tags[] = {
Lev Walkin41ba1f22004-09-14 12:46:35 +000040 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
41};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080042asn_TYPE_operation_t asn_OP_NativeReal = {
Lev Walkina9cc46e2004-09-22 16:06:28 +000043 NativeReal_free,
44 NativeReal_print,
Lev Walkincd2f48e2017-08-10 02:14:59 -070045 NativeReal_compare,
Lev Walkin41ba1f22004-09-14 12:46:35 +000046 NativeReal_decode_ber,
47 NativeReal_encode_der,
Lev Walkin8471cec2004-10-21 14:02:19 +000048 NativeReal_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000049 NativeReal_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070050#ifdef ASN_DISABLE_OER_SUPPORT
51 0,
52 0,
53#else
54 0,
55 0,
56#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040057#ifdef ASN_DISABLE_PER_SUPPORT
58 0,
59 0,
60#else
61 NativeReal_decode_uper,
62 NativeReal_encode_uper,
63#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkina5972be2017-09-29 23:15:58 -070064 NativeReal_random_fill,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080065 0 /* Use generic outmost tag fetcher */
66};
67asn_TYPE_descriptor_t asn_DEF_NativeReal = {
68 "REAL", /* The ASN.1 type is still REAL */
69 "REAL",
70 &asn_OP_NativeReal,
Lev Walkin5e033762004-09-29 13:26:15 +000071 asn_DEF_NativeReal_tags,
72 sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]),
73 asn_DEF_NativeReal_tags, /* Same as above */
74 sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]),
Lev Walkina5972be2017-09-29 23:15:58 -070075 { 0, 0, asn_generic_no_constraint },
Lev Walkin41ba1f22004-09-14 12:46:35 +000076 0, 0, /* No members */
77 0 /* No specifics */
78};
79
80/*
81 * Decode REAL type.
82 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000083asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -070084NativeReal_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin5e033762004-09-29 13:26:15 +000085 asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +000086 void **dbl_ptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkin41ba1f22004-09-14 12:46:35 +000087 double *Dbl = (double *)*dbl_ptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +000088 asn_dec_rval_t rval;
Lev Walkin41ba1f22004-09-14 12:46:35 +000089 ber_tlv_len_t length;
90
91 /*
92 * If the structure is not there, allocate it.
93 */
94 if(Dbl == NULL) {
Lev Walkinc17d90f2005-01-17 14:32:45 +000095 *dbl_ptr = CALLOC(1, sizeof(*Dbl));
96 Dbl = (double *)*dbl_ptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +000097 if(Dbl == NULL) {
98 rval.code = RC_FAIL;
99 rval.consumed = 0;
100 return rval;
101 }
102 }
103
104 ASN_DEBUG("Decoding %s as REAL (tm=%d)",
105 td->name, tag_mode);
106
107 /*
108 * Check tags.
109 */
Lev Walkin5e033762004-09-29 13:26:15 +0000110 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
111 tag_mode, 0, &length, 0);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000112 if(rval.code != RC_OK)
113 return rval;
114
115 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
116
117 /*
118 * Make sure we have this length.
119 */
Lev Walkin8c3b8542005-03-10 18:52:02 +0000120 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000121 size -= rval.consumed;
122 if(length > (ber_tlv_len_t)size) {
123 rval.code = RC_WMORE;
124 rval.consumed = 0;
125 return rval;
126 }
127
128 /*
129 * ASN.1 encoded REAL: buf_ptr, length
130 * Fill the Dbl, at the same time checking for overflow.
131 * If overflow occured, return with RC_FAIL.
132 */
133 {
134 REAL_t tmp;
Lev Walkin7e033b52005-07-02 08:19:17 +0000135 union {
136 const void *constbuf;
137 void *nonconstbuf;
138 } unconst_buf;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000139 double d;
Lev Walkin7e033b52005-07-02 08:19:17 +0000140
141 unconst_buf.constbuf = buf_ptr;
142 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000143 tmp.size = length;
144
Lev Walkin0959ffb2011-06-25 15:48:52 -0700145 if(length < (ber_tlv_len_t)size) {
146 int ret;
147 uint8_t saved_byte = tmp.buf[tmp.size];
148 tmp.buf[tmp.size] = '\0';
149 ret = asn_REAL2double(&tmp, &d);
150 tmp.buf[tmp.size] = saved_byte;
151 if(ret) {
152 rval.code = RC_FAIL;
153 rval.consumed = 0;
154 return rval;
155 }
156 } else if(length < 48 /* Enough for longish %f value. */) {
157 tmp.buf = alloca(length + 1);
158 tmp.size = length;
159 memcpy(tmp.buf, buf_ptr, length);
160 tmp.buf[tmp.size] = '\0';
161 if(asn_REAL2double(&tmp, &d)) {
162 rval.code = RC_FAIL;
163 rval.consumed = 0;
164 return rval;
165 }
166 } else {
167 /* This should probably never happen: impractically long value */
168 tmp.buf = CALLOC(1, length + 1);
169 tmp.size = length;
170 if(tmp.buf) memcpy(tmp.buf, buf_ptr, length);
171 if(!tmp.buf || asn_REAL2double(&tmp, &d)) {
172 FREEMEM(tmp.buf);
173 rval.code = RC_FAIL;
174 rval.consumed = 0;
175 return rval;
176 }
177 FREEMEM(tmp.buf);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000178 }
179
180 *Dbl = d;
181 }
182
183 rval.code = RC_OK;
184 rval.consumed += length;
185
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000186 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%f)",
Lev Walkin41ba1f22004-09-14 12:46:35 +0000187 (long)rval.consumed, (long)length, td->name, *Dbl);
188
189 return rval;
190}
191
192/*
193 * Encode the NativeReal using the standard REAL type DER encoder.
194 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000195asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000196NativeReal_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000197 int tag_mode, ber_tlv_tag_t tag,
198 asn_app_consume_bytes_f *cb, void *app_key) {
199 double Dbl = *(const double *)ptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000200 asn_enc_rval_t erval;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000201 REAL_t tmp;
202
Lev Walkina8bbbda2005-02-06 04:29:03 +0000203 /* Prepare a temporary clean structure */
204 memset(&tmp, 0, sizeof(tmp));
205
Lev Walkin5e033762004-09-29 13:26:15 +0000206 if(asn_double2REAL(&tmp, Dbl)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000207 erval.encoded = -1;
208 erval.failed_type = td;
209 erval.structure_ptr = ptr;
210 return erval;
211 }
212
Lev Walkin8e8078a2004-09-26 13:10:40 +0000213 /* Encode a fake REAL */
214 erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000215 if(erval.encoded == -1) {
216 assert(erval.structure_ptr == &tmp);
217 erval.structure_ptr = ptr;
218 }
Lev Walkina8bbbda2005-02-06 04:29:03 +0000219
220 /* Free possibly allocated members of the temporary structure */
Lev Walkinadcb5862006-03-17 02:11:12 +0000221 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
Lev Walkina8bbbda2005-02-06 04:29:03 +0000222
Lev Walkin41ba1f22004-09-14 12:46:35 +0000223 return erval;
224}
225
Lev Walkin41d972b2017-08-23 23:30:59 -0700226#ifndef ASN_DISABLE_PER_SUPPORT
227
Lev Walkin725883b2006-10-09 12:07:58 +0000228/*
229 * Decode REAL type using PER.
230 */
231asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700232NativeReal_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin494fb702017-08-07 20:07:00 -0700233 asn_TYPE_descriptor_t *td,
234 const asn_per_constraints_t *constraints, void **dbl_ptr,
235 asn_per_data_t *pd) {
236 double *Dbl = (double *)*dbl_ptr;
Lev Walkin725883b2006-10-09 12:07:58 +0000237 asn_dec_rval_t rval;
238 REAL_t tmp;
239 void *ptmp = &tmp;
240 int ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000241
Lev Walkin725883b2006-10-09 12:07:58 +0000242 (void)constraints;
243
244 /*
245 * If the structure is not there, allocate it.
246 */
247 if(Dbl == NULL) {
248 *dbl_ptr = CALLOC(1, sizeof(*Dbl));
249 Dbl = (double *)*dbl_ptr;
250 if(Dbl == NULL)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700251 ASN__DECODE_FAILED;
Lev Walkin725883b2006-10-09 12:07:58 +0000252 }
253
254 memset(&tmp, 0, sizeof(tmp));
255 rval = OCTET_STRING_decode_uper(opt_codec_ctx, td, NULL,
256 &ptmp, pd);
257 if(rval.code != RC_OK) {
258 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
259 return rval;
260 }
261
262 ret = asn_REAL2double(&tmp, Dbl);
263 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700264 if(ret) ASN__DECODE_FAILED;
Lev Walkin725883b2006-10-09 12:07:58 +0000265
266 return rval;
267}
268
269/*
270 * Encode the NativeReal using the OCTET STRING PER encoder.
271 */
272asn_enc_rval_t
273NativeReal_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700274 const asn_per_constraints_t *constraints, void *sptr,
275 asn_per_outp_t *po) {
276 double Dbl = *(const double *)sptr;
Lev Walkin725883b2006-10-09 12:07:58 +0000277 asn_enc_rval_t erval;
278 REAL_t tmp;
279
280 (void)constraints;
281
282 /* Prepare a temporary clean structure */
283 memset(&tmp, 0, sizeof(tmp));
284
285 if(asn_double2REAL(&tmp, Dbl))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700286 ASN__ENCODE_FAILED;
Lev Walkin725883b2006-10-09 12:07:58 +0000287
288 /* Encode a DER REAL */
289 erval = OCTET_STRING_encode_uper(td, NULL, &tmp, po);
290 if(erval.encoded == -1)
291 erval.structure_ptr = sptr;
292
293 /* Free possibly allocated members of the temporary structure */
294 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
295
296 return erval;
297}
Lev Walkin8471cec2004-10-21 14:02:19 +0000298
Lev Walkin41d972b2017-08-23 23:30:59 -0700299#endif /* ASN_DISABLE_PER_SUPPORT */
300
Lev Walkin8471cec2004-10-21 14:02:19 +0000301/*
302 * Decode the chunk of XML text encoding REAL.
303 */
304asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700305NativeReal_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin8471cec2004-10-21 14:02:19 +0000306 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000307 const void *buf_ptr, size_t size) {
Lev Walkin8471cec2004-10-21 14:02:19 +0000308 asn_dec_rval_t rval;
309 REAL_t *st = 0;
Lev Walkin1eded352006-07-13 11:19:01 +0000310 REAL_t **stp = &st;
Lev Walkin8471cec2004-10-21 14:02:19 +0000311 double *Dbl = (double *)*sptr;
312
313 if(!Dbl) {
Lev Walkinc17d90f2005-01-17 14:32:45 +0000314 *sptr = CALLOC(1, sizeof(double));
315 Dbl = (double *)*sptr;
Lev Walkin8471cec2004-10-21 14:02:19 +0000316 if(!Dbl) {
317 rval.code = RC_FAIL;
318 rval.consumed = 0;
319 return rval;
320 }
321 }
322
Lev Walkin1eded352006-07-13 11:19:01 +0000323 rval = REAL_decode_xer(opt_codec_ctx, td, (void **)stp, opt_mname,
Lev Walkin8471cec2004-10-21 14:02:19 +0000324 buf_ptr, size);
325 if(rval.code == RC_OK) {
326 if(asn_REAL2double(st, Dbl)) {
327 rval.code = RC_FAIL;
328 rval.consumed = 0;
329 }
330 } else {
331 rval.consumed = 0;
332 }
Lev Walkinadcb5862006-03-17 02:11:12 +0000333 ASN_STRUCT_FREE(asn_DEF_REAL, st);
Lev Walkin8471cec2004-10-21 14:02:19 +0000334 return rval;
335}
336
Lev Walkina9cc46e2004-09-22 16:06:28 +0000337asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000338NativeReal_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000339 int ilevel, enum xer_encoder_flags_e flags,
340 asn_app_consume_bytes_f *cb, void *app_key) {
341 const double *Dbl = (const double *)sptr;
342 asn_enc_rval_t er;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000343
344 (void)ilevel;
345
Lev Walkin7c1dc052016-03-14 03:08:15 -0700346 if(!Dbl) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000347
Lev Walkin8e8078a2004-09-26 13:10:40 +0000348 er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700349 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000350
Lev Walkin7c1dc052016-03-14 03:08:15 -0700351 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000352}
353
Lev Walkin41ba1f22004-09-14 12:46:35 +0000354/*
355 * REAL specific human-readable output.
356 */
357int
Lev Walkin5e033762004-09-29 13:26:15 +0000358NativeReal_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000359 asn_app_consume_bytes_f *cb, void *app_key) {
360 const double *Dbl = (const double *)sptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000361
362 (void)td; /* Unused argument */
363 (void)ilevel; /* Unused argument */
364
Lev Walkin8e8078a2004-09-26 13:10:40 +0000365 if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000366
367 return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000368}
369
Lev Walkincd2f48e2017-08-10 02:14:59 -0700370int
371NativeReal_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
372 const void *bptr) {
373 const double *a = aptr;
374 const double *b = bptr;
375 (void)td;
376
377 if(a && b) {
378 /* NaN sorted above everything else */
Lev Walkineff98a52017-09-13 22:24:35 +0000379 if(asn_isnan(*a)) {
380 if(asn_isnan(*b)) {
Lev Walkincd2f48e2017-08-10 02:14:59 -0700381 return 0;
382 } else {
383 return -1;
384 }
Lev Walkineff98a52017-09-13 22:24:35 +0000385 } else if(asn_isnan(*b)) {
Lev Walkincd2f48e2017-08-10 02:14:59 -0700386 return 1;
387 }
388 /* Value comparison. */
389 if(*a < *b) {
390 return -1;
391 } else if(*a > *b) {
392 return 1;
393 } else {
394 return 0;
395 }
396 } else if(!a) {
397 return -1;
398 } else {
399 return 1;
400 }
401}
402
Lev Walkin41ba1f22004-09-14 12:46:35 +0000403void
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700404NativeReal_free(const asn_TYPE_descriptor_t *td, void *ptr,
405 enum asn_struct_free_method method) {
Lev Walkinf6853ce2017-08-11 00:50:27 -0700406 if(!td || !ptr)
Lev Walkin41ba1f22004-09-14 12:46:35 +0000407 return;
408
409 ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)",
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700410 td->name, method, ptr);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000411
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700412 switch(method) {
413 case ASFM_FREE_EVERYTHING:
414 FREEMEM(ptr);
415 break;
416 case ASFM_FREE_UNDERLYING:
417 break;
418 case ASFM_FREE_UNDERLYING_AND_RESET:
419 memset(ptr, 0, sizeof(double));
420 break;
421 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000422}
423
Lev Walkina5972be2017-09-29 23:15:58 -0700424
425asn_random_fill_result_t
426NativeReal_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
427 const asn_encoding_constraints_t *constraints,
428 size_t max_length) {
429 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
430 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
431 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
432 static const double values[] = {0, -0.0, -1, 1, INFINITY, -INFINITY, NAN};
433 double *st;
434 double d;
435
436 (void)td;
437 (void)constraints;
438
439 if(max_length == 0) return result_skipped;
440
441 d = values[asn_random_between(0, sizeof(values) / sizeof(values[0]) - 1)];
442
443 if(*sptr) {
444 st = *sptr;
445 } else {
446 st = (double *)(*sptr = CALLOC(1, sizeof(double)));
447 if(!st) {
448 return result_failed;
449 }
450 }
451
452 *st = d;
453
454 result_ok.length = sizeof(double);
455 return result_ok;
456}