blob: 68157dea258aff60e26ca45bd109c809bfc4fcea [file] [log] [blame]
Lev Walkin41ba1f22004-09-14 12:46:35 +00001/*-
Lev Walkina5972be2017-09-29 23:15:58 -07002 * Copyright (c) 2004-2017 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 */
Lev Walkine9360e62013-03-28 00:21:57 -07005#define _ISOC99_SOURCE /* For ilogb() and quiet NAN */
Vasil Velichkovadf65942016-08-01 16:52:02 +03006#ifndef _BSD_SOURCE
Lev Walkin1aea6982004-10-26 09:35:25 +00007#define _BSD_SOURCE /* To reintroduce finite(3) */
Vasil Velichkovadf65942016-08-01 16:52:02 +03008#endif
Lev Walkinfe374312017-10-07 00:24:51 -07009#include <asn_internal.h>
Lev Walkin73823e12013-03-28 00:24:48 -070010#if defined(__alpha)
Lev Walkin1aea6982004-10-26 09:35:25 +000011#include <sys/resource.h> /* For INFINITY */
Lev Walkin33700162004-10-26 09:03:31 +000012#endif
Lev Walkinfe374312017-10-07 00:24:51 -070013#if defined(sun) || defined(__sun)
14#include <ieeefp.h>
15#endif
Lev Walkin41ba1f22004-09-14 12:46:35 +000016#include <stdlib.h> /* for strtod(3) */
17#include <math.h>
Jon Ringle35c3f0d2017-10-04 21:54:39 -040018#include <float.h>
Lev Walkin41ba1f22004-09-14 12:46:35 +000019#include <errno.h>
Lev Walkin33700162004-10-26 09:03:31 +000020#include <REAL.h>
Lev Walkin725883b2006-10-09 12:07:58 +000021#include <OCTET_STRING.h>
Lev Walkin41ba1f22004-09-14 12:46:35 +000022
23#undef INT_MAX
24#define INT_MAX ((int)(((unsigned int)-1) >> 1))
25
Lev Walkin1aea6982004-10-26 09:35:25 +000026#if !(defined(NAN) || defined(INFINITY))
Lev Walkin3f995632017-09-26 18:27:32 -070027static volatile double real_zero CC_NOTUSED = 0.0;
Lev Walkin1aea6982004-10-26 09:35:25 +000028#endif
Lev Walkinae1bce92004-09-27 22:18:34 +000029#ifndef NAN
Lev Walkinfe374312017-10-07 00:24:51 -070030#define NAN (0.0/0.0)
Lev Walkin40319a12004-09-14 13:40:42 +000031#endif
Lev Walkin1aea6982004-10-26 09:35:25 +000032#ifndef INFINITY
Lev Walkinfe374312017-10-07 00:24:51 -070033#define INFINITY (1.0/0.0)
Lev Walkin1aea6982004-10-26 09:35:25 +000034#endif
Lev Walkin40319a12004-09-14 13:40:42 +000035
Lev Walkineff98a52017-09-13 22:24:35 +000036#if defined(__clang__)
37/*
38 * isnan() is defined using generic selections and won't compile in
39 * strict C89 mode because of too fancy system's standard library.
40 * However, prior to C11 the math had a perfectly working isnan()
41 * in the math library.
42 * Disable generic selection warning so we can test C89 mode with newer libc.
43 */
44#pragma clang diagnostic push
45#pragma clang diagnostic ignored "-Wc11-extensions"
46static int asn_isnan(double d) {
47 return isnan(d);
48}
49static int asn_isfinite(double d) {
Lev Walkindaeb2162014-01-13 23:08:35 -080050#ifdef isfinite
Lev Walkineff98a52017-09-13 22:24:35 +000051 return isfinite(d); /* ISO C99 */
Lev Walkindaeb2162014-01-13 23:08:35 -080052#else
Lev Walkineff98a52017-09-13 22:24:35 +000053 return finite(d); /* Deprecated on Mac OS X 10.9 */
Lev Walkindaeb2162014-01-13 23:08:35 -080054#endif
Lev Walkineff98a52017-09-13 22:24:35 +000055}
56#pragma clang diagnostic pop
57#else /* !clang */
58#define asn_isnan(v) isnan(v)
59#ifdef isfinite
60#define asn_isfinite(d) isfinite(d) /* ISO C99 */
61#else
62#define asn_isfinite(d) finite(d) /* Deprecated on Mac OS X 10.9 */
63#endif
64#endif /* clang */
65
Lev Walkindaeb2162014-01-13 23:08:35 -080066
Lev Walkin41ba1f22004-09-14 12:46:35 +000067/*
68 * REAL basic type description.
69 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070070static const ber_tlv_tag_t asn_DEF_REAL_tags[] = {
Lev Walkin41ba1f22004-09-14 12:46:35 +000071 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
72};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080073asn_TYPE_operation_t asn_OP_REAL = {
74 ASN__PRIMITIVE_TYPE_free,
75 REAL_print,
76 REAL_compare,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080077 ber_decode_primitive,
78 der_encode_primitive,
79 REAL_decode_xer,
80 REAL_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070081#ifdef ASN_DISABLE_OER_SUPPORT
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080082 0,
83 0,
Lev Walkincc159472017-07-06 08:26:36 -070084#else
Lev Walkin43292722017-10-05 00:33:32 -070085 REAL_decode_oer,
86 REAL_encode_oer,
Lev Walkincc159472017-07-06 08:26:36 -070087#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040088#ifdef ASN_DISABLE_PER_SUPPORT
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080089 0,
90 0,
Lev Walkinb33425f2017-07-14 14:59:52 +040091#else
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080092 REAL_decode_uper,
93 REAL_encode_uper,
Lev Walkinb33425f2017-07-14 14:59:52 +040094#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkina5972be2017-09-29 23:15:58 -070095 REAL_random_fill,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080096 0 /* Use generic outmost tag fetcher */
97};
98asn_TYPE_descriptor_t asn_DEF_REAL = {
99 "REAL",
100 "REAL",
101 &asn_OP_REAL,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800102 asn_DEF_REAL_tags,
103 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
104 asn_DEF_REAL_tags, /* Same as above */
105 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
Lev Walkina5972be2017-09-29 23:15:58 -0700106 { 0, 0, asn_generic_no_constraint },
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800107 0,
108 0, /* No members */
109 0 /* No specifics */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000110};
111
Lev Walkin5f560912004-10-21 13:37:57 +0000112typedef enum specialRealValue {
113 SRV__NOT_A_NUMBER,
114 SRV__MINUS_INFINITY,
115 SRV__PLUS_INFINITY
116} specialRealValue_e;
117static struct specialRealValue_s {
118 char *string;
Lev Walkin8471cec2004-10-21 14:02:19 +0000119 size_t length;
Lev Walkin1aea6982004-10-26 09:35:25 +0000120 long dv;
Lev Walkin5f560912004-10-21 13:37:57 +0000121} specialRealValue[] = {
122#define SRV_SET(foo, val) { foo, sizeof(foo) - 1, val }
Lev Walkin1aea6982004-10-26 09:35:25 +0000123 SRV_SET("<NOT-A-NUMBER/>", 0),
124 SRV_SET("<MINUS-INFINITY/>", -1),
125 SRV_SET("<PLUS-INFINITY/>", 1),
Lev Walkin5f560912004-10-21 13:37:57 +0000126#undef SRV_SET
127};
128
Lev Walkina9cc46e2004-09-22 16:06:28 +0000129ssize_t
130REAL__dump(double d, int canonical, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin92b35d22004-09-27 20:52:18 +0000131 char local_buf[64];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000132 char *buf = local_buf;
133 ssize_t buflen = sizeof(local_buf);
Lev Walkinab1d1e12017-10-03 18:43:12 -0700134 const char *fmt = canonical ? "%.17E" /* Precise */ : "%.15f" /* Pleasant*/;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000135 ssize_t ret;
136
Lev Walkin8e8078a2004-09-26 13:10:40 +0000137 /*
138 * Check whether it is a special value.
139 */
Lev Walkinc51e7d62004-09-27 22:16:18 +0000140 /* fpclassify(3) is not portable yet */
Lev Walkineff98a52017-09-13 22:24:35 +0000141 if(asn_isnan(d)) {
Lev Walkin5f560912004-10-21 13:37:57 +0000142 buf = specialRealValue[SRV__NOT_A_NUMBER].string;
143 buflen = specialRealValue[SRV__NOT_A_NUMBER].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000144 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
Lev Walkineff98a52017-09-13 22:24:35 +0000145 } else if(!asn_isfinite(d)) {
Lev Walkinc51e7d62004-09-27 22:16:18 +0000146 if(copysign(1.0, d) < 0.0) {
Lev Walkin5f560912004-10-21 13:37:57 +0000147 buf = specialRealValue[SRV__MINUS_INFINITY].string;
148 buflen = specialRealValue[SRV__MINUS_INFINITY].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000149 } else {
Lev Walkin5f560912004-10-21 13:37:57 +0000150 buf = specialRealValue[SRV__PLUS_INFINITY].string;
151 buflen = specialRealValue[SRV__PLUS_INFINITY].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000152 }
153 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
154 } else if(ilogb(d) <= -INT_MAX) {
155 if(copysign(1.0, d) < 0.0) {
156 buf = "-0";
157 buflen = 2;
158 } else {
159 buf = "0";
160 buflen = 1;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000161 }
162 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
163 }
164
165 /*
166 * Use the libc's double printing, hopefully they got it right.
167 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000168 do {
169 ret = snprintf(buf, buflen, fmt, d);
170 if(ret < 0) {
Lev Walkin63a89da2017-10-08 02:48:32 -0700171 /* There are some old broken APIs. */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000172 buflen <<= 1;
Lev Walkin63a89da2017-10-08 02:48:32 -0700173 if(buflen > 4096) {
174 /* Should be plenty. */
175 if(buf != local_buf) FREEMEM(buf);
176 return -1;
177 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000178 } else if(ret >= buflen) {
179 buflen = ret + 1;
180 } else {
181 buflen = ret;
182 break;
183 }
Lev Walkin419f6752006-09-13 04:02:00 +0000184 if(buf != local_buf) FREEMEM(buf);
Lev Walkin8e8078a2004-09-26 13:10:40 +0000185 buf = (char *)MALLOC(buflen);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000186 if(!buf) return -1;
187 } while(1);
188
Lev Walkina9cc46e2004-09-22 16:06:28 +0000189 if(canonical) {
Lev Walkin92b35d22004-09-27 20:52:18 +0000190 /*
191 * Transform the "[-]d.dddE+-dd" output into "[-]d.dddE[-]d"
Lev Walkin5f560912004-10-21 13:37:57 +0000192 * Check that snprintf() constructed the output correctly.
Lev Walkin92b35d22004-09-27 20:52:18 +0000193 */
Lev Walkin8ca13c82016-01-24 22:40:00 -0800194 char *dot;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000195 char *end = buf + buflen;
Lev Walkin92b35d22004-09-27 20:52:18 +0000196 char *last_zero;
Lev Walkin63a89da2017-10-08 02:48:32 -0700197 char *first_zero_in_run;
Lev Walkin8ca13c82016-01-24 22:40:00 -0800198 char *s;
199
200 enum {
201 LZSTATE_NOTHING,
Lev Walkin63a89da2017-10-08 02:48:32 -0700202 LZSTATE_ZEROES
Lev Walkin8ca13c82016-01-24 22:40:00 -0800203 } lz_state = LZSTATE_NOTHING;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000204
Lev Walkin5f560912004-10-21 13:37:57 +0000205 dot = (buf[0] == 0x2d /* '-' */) ? (buf + 2) : (buf + 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000206 if(*dot >= 0x30) {
Lev Walkin78d917d2012-01-07 15:29:08 -0800207 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000208 errno = EINVAL;
209 return -1; /* Not a dot, really */
210 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000211 *dot = 0x2e; /* Replace possible comma */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000212
Lev Walkin63a89da2017-10-08 02:48:32 -0700213 for(first_zero_in_run = last_zero = s = dot + 2; s < end; s++) {
Lev Walkin8ca13c82016-01-24 22:40:00 -0800214 switch(*s) {
215 case 0x45: /* 'E' */
Lev Walkin63a89da2017-10-08 02:48:32 -0700216 if(lz_state == LZSTATE_ZEROES) last_zero = first_zero_in_run;
Lev Walkin8ca13c82016-01-24 22:40:00 -0800217 break;
Lev Walkin63a89da2017-10-08 02:48:32 -0700218 case 0x30: /* '0' */
219 if(lz_state == LZSTATE_NOTHING) first_zero_in_run = s;
220 lz_state = LZSTATE_ZEROES;
Lev Walkin8ca13c82016-01-24 22:40:00 -0800221 continue;
222 default:
223 lz_state = LZSTATE_NOTHING;
224 continue;
225 }
226 break;
227 }
228
229 if(s == end) {
Lev Walkin78d917d2012-01-07 15:29:08 -0800230 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000231 errno = EINVAL;
232 return -1; /* No promised E */
233 }
Lev Walkin8ca13c82016-01-24 22:40:00 -0800234
235 assert(*s == 0x45);
236 {
237 char *E = s;
238 char *expptr = ++E;
239 char *s = expptr;
240 int sign;
241
242 if(*expptr == 0x2b /* '+' */) {
243 /* Skip the "+" */
244 buflen -= 1;
245 sign = 0;
246 } else {
247 sign = 1;
248 s++;
249 }
250 expptr++;
251 if(expptr > end) {
252 if(buf != local_buf) FREEMEM(buf);
253 errno = EINVAL;
254 return -1;
255 }
256 if(*expptr == 0x30) {
257 buflen--;
258 expptr++;
259 }
Lev Walkin63a89da2017-10-08 02:48:32 -0700260 if(lz_state == LZSTATE_ZEROES) {
Lev Walkin8ca13c82016-01-24 22:40:00 -0800261 *last_zero = 0x45; /* E */
262 buflen -= s - (last_zero + 1);
263 s = last_zero + 1;
264 if(sign) {
265 *s++ = 0x2d /* '-' */;
266 buflen++;
267 }
268 }
269 for(; expptr <= end; s++, expptr++)
270 *s = *expptr;
271 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000272 } else {
273 /*
274 * Remove trailing zeros.
275 */
276 char *end = buf + buflen;
277 char *last_zero = end;
Lev Walkinf0b808d2005-04-25 21:08:25 +0000278 int stoplooking = 0;
Lev Walkin92b35d22004-09-27 20:52:18 +0000279 char *z;
280 for(z = end - 1; z > buf; z--) {
281 switch(*z) {
Lev Walkinf0b808d2005-04-25 21:08:25 +0000282 case 0x30:
283 if(!stoplooking)
284 last_zero = z;
285 continue;
Lev Walkin92b35d22004-09-27 20:52:18 +0000286 case 0x31: case 0x32: case 0x33: case 0x34:
287 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinf0b808d2005-04-25 21:08:25 +0000288 stoplooking = 1;
Lev Walkin92b35d22004-09-27 20:52:18 +0000289 continue;
290 default: /* Catch dot and other separators */
Lev Walkinf0b808d2005-04-25 21:08:25 +0000291 /*
292 * Replace possible comma (which may even
293 * be not a comma at all: locale-defined).
294 */
295 *z = 0x2e;
Lev Walkin92b35d22004-09-27 20:52:18 +0000296 if(last_zero == z + 1) { /* leave x.0 */
297 last_zero++;
298 }
299 buflen = last_zero - buf;
300 *last_zero = '\0';
301 break;
302 }
303 break;
304 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000305 }
306
307 ret = cb(buf, buflen, app_key);
Lev Walkin419f6752006-09-13 04:02:00 +0000308 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000309 return (ret < 0) ? -1 : buflen;
310}
311
Lev Walkin41ba1f22004-09-14 12:46:35 +0000312int
Lev Walkin5e033762004-09-29 13:26:15 +0000313REAL_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000314 asn_app_consume_bytes_f *cb, void *app_key) {
315 const REAL_t *st = (const REAL_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000316 ssize_t ret;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000317 double d;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000318
319 (void)td; /* Unused argument */
320 (void)ilevel; /* Unused argument */
321
Lev Walkina9cc46e2004-09-22 16:06:28 +0000322 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000323 ret = cb("<absent>", 8, app_key);
Lev Walkin5e033762004-09-29 13:26:15 +0000324 else if(asn_REAL2double(st, &d))
Lev Walkin8e8078a2004-09-26 13:10:40 +0000325 ret = cb("<error>", 7, app_key);
326 else
327 ret = REAL__dump(d, 0, cb, app_key);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000328
Lev Walkin8e8078a2004-09-26 13:10:40 +0000329 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000330}
Lev Walkin41ba1f22004-09-14 12:46:35 +0000331
Lev Walkincd2f48e2017-08-10 02:14:59 -0700332int
333REAL_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
334 const void *bptr) {
335 const REAL_t *a = aptr;
336 const REAL_t *b = bptr;
337
338 (void)td;
339
340 if(a && b) {
341 double adbl, bdbl;
342 int ra, rb;
343 ra = asn_REAL2double(a, &adbl);
344 rb = asn_REAL2double(b, &bdbl);
345 if(ra == 0 && rb == 0) {
Lev Walkineff98a52017-09-13 22:24:35 +0000346 if(asn_isnan(adbl)) {
347 if(asn_isnan(bdbl)) {
Lev Walkincd2f48e2017-08-10 02:14:59 -0700348 return 0;
349 } else {
350 return -1;
351 }
Lev Walkineff98a52017-09-13 22:24:35 +0000352 } else if(asn_isnan(bdbl)) {
Lev Walkincd2f48e2017-08-10 02:14:59 -0700353 return 1;
354 }
355 /* Value comparison. */
356 if(adbl < bdbl) {
357 return -1;
358 } else if(adbl > bdbl) {
359 return 1;
360 } else {
361 return 0;
362 }
363 } else if(ra) {
364 return -1;
365 } else {
366 return 1;
367 }
368 } else if(!a) {
369 return -1;
370 } else {
371 return 1;
372 }
373}
374
Lev Walkina9cc46e2004-09-22 16:06:28 +0000375asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000376REAL_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000377 int ilevel, enum xer_encoder_flags_e flags,
378 asn_app_consume_bytes_f *cb, void *app_key) {
379 REAL_t *st = (REAL_t *)sptr;
380 asn_enc_rval_t er;
381 double d;
382
383 (void)ilevel;
384
Lev Walkin5e033762004-09-29 13:26:15 +0000385 if(!st || !st->buf || asn_REAL2double(st, &d))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700386 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000387
388 er.encoded = REAL__dump(d, flags & XER_F_CANONICAL, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700389 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000390
Lev Walkin7c1dc052016-03-14 03:08:15 -0700391 ASN__ENCODED_OK(er);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000392}
393
Lev Walkin5f560912004-10-21 13:37:57 +0000394
395/*
396 * Decode the chunk of XML text encoding REAL.
397 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000398static enum xer_pbd_rval
399REAL__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
Lev Walkin8471cec2004-10-21 14:02:19 +0000400 REAL_t *st = (REAL_t *)sptr;
Lev Walkin5f560912004-10-21 13:37:57 +0000401 double value;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000402 const char *xerdata = (const char *)chunk_buf;
Lev Walkin5f560912004-10-21 13:37:57 +0000403 char *endptr = 0;
404 char *b;
405
Lev Walkine0b56e02005-02-25 12:10:27 +0000406 (void)td;
407
Lev Walkin0fab1a62005-03-09 22:19:25 +0000408 if(!chunk_size) return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000409
410 /*
411 * Decode an XMLSpecialRealValue: <MINUS-INFINITY>, etc.
412 */
413 if(xerdata[0] == 0x3c /* '<' */) {
414 size_t i;
415 for(i = 0; i < sizeof(specialRealValue)
416 / sizeof(specialRealValue[0]); i++) {
417 struct specialRealValue_s *srv = &specialRealValue[i];
Lev Walkin1aea6982004-10-26 09:35:25 +0000418 double dv;
419
Lev Walkin5f560912004-10-21 13:37:57 +0000420 if(srv->length != chunk_size
421 || memcmp(srv->string, chunk_buf, chunk_size))
422 continue;
423
Lev Walkin1aea6982004-10-26 09:35:25 +0000424 /*
425 * It could've been done using
426 * (double)srv->dv / real_zero,
427 * but it summons fp exception on some platforms.
428 */
429 switch(srv->dv) {
430 case -1: dv = - INFINITY; break;
431 case 0: dv = NAN; break;
432 case 1: dv = INFINITY; break;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000433 default: return XPBD_SYSTEM_FAILURE;
Lev Walkin1aea6982004-10-26 09:35:25 +0000434 }
435
Lev Walkin0fab1a62005-03-09 22:19:25 +0000436 if(asn_double2REAL(st, dv))
437 return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000438
Lev Walkin0fab1a62005-03-09 22:19:25 +0000439 return XPBD_BODY_CONSUMED;
Lev Walkin5f560912004-10-21 13:37:57 +0000440 }
441 ASN_DEBUG("Unknown XMLSpecialRealValue");
Lev Walkin0fab1a62005-03-09 22:19:25 +0000442 return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000443 }
444
445 /*
446 * Copy chunk into the nul-terminated string, and run strtod.
447 */
Lev Walkin8471cec2004-10-21 14:02:19 +0000448 b = (char *)MALLOC(chunk_size + 1);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000449 if(!b) return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000450 memcpy(b, chunk_buf, chunk_size);
Lev Walkin8471cec2004-10-21 14:02:19 +0000451 b[chunk_size] = 0; /* nul-terminate */
Lev Walkin5f560912004-10-21 13:37:57 +0000452
453 value = strtod(b, &endptr);
Lev Walkin419f6752006-09-13 04:02:00 +0000454 FREEMEM(b);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000455 if(endptr == b) return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000456
457 if(asn_double2REAL(st, value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000458 return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000459
Lev Walkin0fab1a62005-03-09 22:19:25 +0000460 return XPBD_BODY_CONSUMED;
Lev Walkin5f560912004-10-21 13:37:57 +0000461}
462
463asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700464REAL_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin5f560912004-10-21 13:37:57 +0000465 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000466 const void *buf_ptr, size_t size) {
Lev Walkin5f560912004-10-21 13:37:57 +0000467
468 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000469 sptr, sizeof(REAL_t), opt_mname,
Lev Walkin5f560912004-10-21 13:37:57 +0000470 buf_ptr, size, REAL__xer_body_decode);
471}
472
Lev Walkin41ba1f22004-09-14 12:46:35 +0000473int
Lev Walkin5e033762004-09-29 13:26:15 +0000474asn_REAL2double(const REAL_t *st, double *dbl_value) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000475 unsigned int octv;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000476
477 if(!st || !st->buf) {
478 errno = EINVAL;
479 return -1;
480 }
481
482 if(st->size == 0) {
483 *dbl_value = 0;
484 return 0;
485 }
486
487 octv = st->buf[0]; /* unsigned byte */
488
489 switch(octv & 0xC0) {
Lev Walkin5fda7d52012-01-09 03:20:19 -0800490 case 0x40: /* X.690: 8.5.6 a) => 8.5.9 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000491 /* "SpecialRealValue" */
492
493 /* Be liberal in what you accept...
Lev Walkin749916f2012-01-07 17:03:39 -0800494 * http://en.wikipedia.org/wiki/Robustness_principle
Lev Walkin41ba1f22004-09-14 12:46:35 +0000495 if(st->size != 1) ...
496 */
497
498 switch(st->buf[0]) {
499 case 0x40: /* 01000000: PLUS-INFINITY */
Lev Walkin1aea6982004-10-26 09:35:25 +0000500 *dbl_value = INFINITY;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000501 return 0;
502 case 0x41: /* 01000001: MINUS-INFINITY */
Lev Walkin1aea6982004-10-26 09:35:25 +0000503 *dbl_value = - INFINITY;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000504 return 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000505 case 0x42: /* 01000010: NOT-A-NUMBER */
506 *dbl_value = NAN;
507 return 0;
508 case 0x43: /* 01000011: minus zero */
Lev Walkin2a789d92004-09-27 21:36:59 +0000509 *dbl_value = -0.0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000510 return 0;
511 }
512
513 errno = EINVAL;
514 return -1;
Lev Walkin0959ffb2011-06-25 15:48:52 -0700515 case 0x00: { /* X.690: 8.5.7 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000516 /*
Lev Walkin5fda7d52012-01-09 03:20:19 -0800517 * Decimal. NR{1,2,3} format from ISO 6093.
518 * NR1: [ ]*[+-]?[0-9]+
519 * NR2: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)
520 * NR3: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)[Ee][+-]?[0-9]+
Lev Walkin41ba1f22004-09-14 12:46:35 +0000521 */
522 double d;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800523 char *buf;
524 char *endptr;
525 int used_malloc = 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000526
Lev Walkin5fda7d52012-01-09 03:20:19 -0800527 if(octv == 0 || (octv & 0x3C)) {
Lev Walkin0959ffb2011-06-25 15:48:52 -0700528 /* Remaining values of bits 6 to 1 are Reserved. */
529 errno = EINVAL;
530 return -1;
531 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000532
Lev Walkin53e5ae62017-08-23 10:56:19 -0700533 /* 1. By contract, an input buffer should be '\0'-terminated.
Lev Walkin5fda7d52012-01-09 03:20:19 -0800534 * OCTET STRING decoder ensures that, as is asn_double2REAL().
535 * 2. ISO 6093 specifies COMMA as a possible decimal separator.
536 * However, strtod() can't always deal with COMMA.
537 * So her we fix both by reallocating, copying and fixing.
538 */
Lev Walkin53e5ae62017-08-23 10:56:19 -0700539 if(st->buf[st->size] != '\0' || memchr(st->buf, ',', st->size)) {
Lev Walkin5fda7d52012-01-09 03:20:19 -0800540 uint8_t *p, *end;
541 char *b;
542 if(st->size > 100) {
543 /* Avoid malicious stack overflow in alloca() */
544 buf = (char *)MALLOC(st->size);
545 if(!buf) return -1;
546 used_malloc = 1;
547 } else {
548 buf = alloca(st->size);
549 }
550 b = buf;
551 /* Copy without the first byte and with 0-termination */
552 for(p = st->buf + 1, end = st->buf + st->size;
553 p < end; b++, p++)
554 *b = (*p == ',') ? '.' : *p;
555 *b = '\0';
556 } else {
557 buf = (char *)&st->buf[1];
558 }
559
560 endptr = buf;
561 d = strtod(buf, &endptr);
562 if(*endptr != '\0') {
563 /* Format is not consistent with ISO 6093 */
564 if(used_malloc) FREEMEM(buf);
Lev Walkin0959ffb2011-06-25 15:48:52 -0700565 errno = EINVAL;
566 return -1;
567 }
Lev Walkin5fda7d52012-01-09 03:20:19 -0800568 if(used_malloc) FREEMEM(buf);
Lev Walkineff98a52017-09-13 22:24:35 +0000569 if(asn_isfinite(d)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000570 *dbl_value = d;
571 return 0;
572 } else {
573 errno = ERANGE;
Lev Walkin0959ffb2011-06-25 15:48:52 -0700574 return -1;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000575 }
576 }
577 }
578
579 /*
580 * Binary representation.
581 */
582 {
583 double m;
Lev Walkinab1d1e12017-10-03 18:43:12 -0700584 int32_t expval; /* exponent value */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000585 unsigned int elen; /* exponent value length, in octets */
Lev Walkin9c57f332017-09-17 22:30:49 -0700586 int scaleF;
587 int baseF;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000588 uint8_t *ptr;
589 uint8_t *end;
590 int sign;
591
592 switch((octv & 0x30) >> 4) {
593 case 0x00: baseF = 1; break; /* base 2 */
594 case 0x01: baseF = 3; break; /* base 8 */
595 case 0x02: baseF = 4; break; /* base 16 */
596 default:
597 /* Reserved field, can't parse now. */
598 errno = EINVAL;
599 return -1;
600 }
601
602 sign = (octv & 0x40); /* bit 7 */
603 scaleF = (octv & 0x0C) >> 2; /* bits 4 to 3 */
604
Lev Walkin494fb702017-08-07 20:07:00 -0700605 if(st->size <= 1 + (octv & 0x03)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000606 errno = EINVAL;
607 return -1;
608 }
609
Lev Walkine78753d2007-06-23 17:02:00 +0000610 elen = (octv & 0x03); /* bits 2 to 1; 8.5.6.4 */
611 if(elen == 0x03) { /* bits 2 to 1 = 11; 8.5.6.4, case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000612 elen = st->buf[1]; /* unsigned binary number */
Lev Walkin494fb702017-08-07 20:07:00 -0700613 if(elen == 0 || st->size <= (2 + elen)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000614 errno = EINVAL;
615 return -1;
616 }
Lev Walkine78753d2007-06-23 17:02:00 +0000617 /* FIXME: verify constraints of case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000618 ptr = &st->buf[2];
619 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000620 ptr = &st->buf[1];
621 }
622
623 /* Fetch the multibyte exponent */
624 expval = (int)(*(int8_t *)ptr);
Lev Walkinab1d1e12017-10-03 18:43:12 -0700625 if(elen >= sizeof(expval)-1) {
626 errno = ERANGE;
627 return -1;
628 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000629 end = ptr + elen + 1;
630 for(ptr++; ptr < end; ptr++)
631 expval = (expval * 256) + *ptr;
632
633 m = 0.0; /* Initial mantissa value */
634
635 /* Okay, the exponent is here. Now, what about mantissa? */
636 end = st->buf + st->size;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800637 for(; ptr < end; ptr++)
638 m = ldexp(m, 8) + *ptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000639
Lev Walkin5f560912004-10-21 13:37:57 +0000640 if(0)
Lev Walkinfb469122017-10-05 18:02:38 +0000641 ASN_DEBUG("m=%.10f, scF=%d, bF=%d, expval=%d, ldexp()=%f, ldexp()=%f\n",
642 m, scaleF, baseF, expval,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000643 ldexp(m, expval * baseF + scaleF),
Lev Walkin77aa90f2005-04-28 22:56:36 +0000644 ldexp(m, scaleF) * pow(pow(2, baseF), expval)
Lev Walkin41ba1f22004-09-14 12:46:35 +0000645 );
646
647 /*
648 * (S * N * 2^F) * B^E
649 * Essentially:
Lev Walkinab1d1e12017-10-03 18:43:12 -0700650 m = ldexp(m, scaleF) * pow(pow(2, baseF), expval);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000651 */
652 m = ldexp(m, expval * baseF + scaleF);
Lev Walkineff98a52017-09-13 22:24:35 +0000653 if(asn_isfinite(m)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000654 *dbl_value = sign ? -m : m;
655 } else {
656 errno = ERANGE;
657 return -1;
658 }
659
660 } /* if(binary_format) */
661
662 return 0;
663}
664
665/*
666 * Assume IEEE 754 floating point: standard 64 bit double.
667 * [1 bit sign] [11 bits exponent] [52 bits mantissa]
668 */
669int
Lev Walkin5e033762004-09-29 13:26:15 +0000670asn_double2REAL(REAL_t *st, double dbl_value) {
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700671 double test = -0.0;
672 int float_big_endian = *(const char *)&test != 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000673 uint8_t buf[16]; /* More than enough for 8-byte dbl_value */
674 uint8_t dscr[sizeof(dbl_value)]; /* double value scratch pad */
675 /* Assertion guards: won't even compile, if unexpected double size */
Lev Walkin3f995632017-09-26 18:27:32 -0700676 char assertion_buffer1[9 - sizeof(dbl_value)] CC_NOTUSED;
677 char assertion_buffer2[sizeof(dbl_value) - 7] CC_NOTUSED;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000678 uint8_t *ptr = buf;
679 uint8_t *mstop; /* Last byte of mantissa */
680 unsigned int mval; /* Value of the last byte of mantissa */
681 unsigned int bmsign; /* binary mask with sign */
682 unsigned int buflen;
683 unsigned int accum;
684 int expval;
685
686 if(!st) {
687 errno = EINVAL;
688 return -1;
689 }
690
Lev Walkin057fb732004-09-14 13:58:10 +0000691 /*
692 * ilogb(+-0) returns -INT_MAX or INT_MIN (platform-dependent)
Lev Walkin2f505022005-07-01 08:28:18 +0000693 * ilogb(+-inf) returns INT_MAX, logb(+-inf) returns +inf
Lev Walkin2a789d92004-09-27 21:36:59 +0000694 * ilogb(NaN) returns INT_MIN or INT_MAX (platform-dependent)
Lev Walkin057fb732004-09-14 13:58:10 +0000695 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000696 expval = ilogb(dbl_value);
Lev Walkin2a789d92004-09-27 21:36:59 +0000697 if(expval <= -INT_MAX /* Also catches +-0 and maybe isnan() */
698 || expval == INT_MAX /* catches isfin() and maybe isnan() */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000699 ) {
700 if(!st->buf || st->size < 2) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000701 ptr = (uint8_t *)MALLOC(2);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000702 if(!ptr) return -1;
Lev Walkindcae9ce2017-09-18 20:13:36 -0700703 if(st->buf) FREEMEM(st->buf);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000704 st->buf = ptr;
705 }
706 /* fpclassify(3) is not portable yet */
Lev Walkineff98a52017-09-13 22:24:35 +0000707 if(asn_isnan(dbl_value)) {
Lev Walkin2a789d92004-09-27 21:36:59 +0000708 st->buf[0] = 0x42; /* NaN */
709 st->buf[1] = 0;
710 st->size = 1;
Lev Walkineff98a52017-09-13 22:24:35 +0000711 } else if(!asn_isfinite(dbl_value)) {
Lev Walkin7e03db92004-09-14 13:50:21 +0000712 if(copysign(1.0, dbl_value) < 0.0) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000713 st->buf[0] = 0x41; /* MINUS-INFINITY */
714 } else {
715 st->buf[0] = 0x40; /* PLUS-INFINITY */
716 }
717 st->buf[1] = 0;
718 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000719 } else {
Lev Walkin5fe72e92012-01-07 17:00:29 -0800720 if(copysign(1.0, dbl_value) >= 0.0) {
Lev Walkinc51e7d62004-09-27 22:16:18 +0000721 /* no content octets: positive zero */
722 st->buf[0] = 0; /* JIC */
723 st->size = 0;
Lev Walkin5fe72e92012-01-07 17:00:29 -0800724 } else {
725 /* Negative zero. #8.5.3, 8.5.9 */
726 st->buf[0] = 0x43;
Lev Walkindcae9ce2017-09-18 20:13:36 -0700727 st->buf[1] = 0;
Lev Walkin5fe72e92012-01-07 17:00:29 -0800728 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000729 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000730 }
731 return 0;
732 }
733
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700734 if(float_big_endian) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000735 uint8_t *s = ((uint8_t *)&dbl_value) + 1;
736 uint8_t *end = ((uint8_t *)&dbl_value) + sizeof(double);
737 uint8_t *d;
738
739 bmsign = 0x80 | ((s[-1] >> 1) & 0x40); /* binary mask & - */
740 for(mstop = d = dscr; s < end; d++, s++) {
741 *d = *s;
742 if(*d) mstop = d;
743 }
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700744 } else {
745 uint8_t *s = ((uint8_t *)&dbl_value) + sizeof(dbl_value) - 2;
746 uint8_t *start = ((uint8_t *)&dbl_value);
747 uint8_t *d;
748
749 bmsign = 0x80 | ((s[1] >> 1) & 0x40); /* binary mask & - */
750 for(mstop = d = dscr; s >= start; d++, s--) {
751 *d = *s;
752 if(*d) mstop = d;
753 }
754 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000755
756 /* Remove parts of the exponent, leave mantissa and explicit 1. */
757 dscr[0] = 0x10 | (dscr[0] & 0x0f);
758
759 /* Adjust exponent in a very unobvious way */
760 expval -= 8 * ((mstop - dscr) + 1) - 4;
761
762 /* This loop ensures DER conformance by forcing mantissa odd: 11.3.1 */
763 mval = *mstop;
764 if(mval && !(mval & 1)) {
Lev Walkin9c57f332017-09-17 22:30:49 -0700765 int shift_count = 1;
766 int ishift;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000767 uint8_t *mptr;
768
769 /*
770 * Figure out what needs to be done to make mantissa odd.
771 */
772 if(!(mval & 0x0f)) /* Speed-up a little */
773 shift_count = 4;
774 while(((mval >> shift_count) & 1) == 0)
775 shift_count++;
776
777 ishift = 8 - shift_count;
778 accum = 0;
779
780 /* Go over the buffer, shifting it shift_count bits right. */
781 for(mptr = dscr; mptr <= mstop; mptr++) {
782 mval = *mptr;
783 *mptr = accum | (mval >> shift_count);
784 accum = mval << ishift;
785 }
786
Lev Walkinb89b3402012-01-09 18:26:30 -0800787 /* Adjust exponent appropriately. */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000788 expval += shift_count;
789 }
790
791 if(expval < 0) {
792 if((expval >> 7) == -1) {
793 *ptr++ = bmsign | 0x00;
794 *ptr++ = expval;
795 } else if((expval >> 15) == -1) {
796 *ptr++ = bmsign | 0x01;
797 *ptr++ = expval >> 8;
798 *ptr++ = expval;
799 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000800 *ptr++ = bmsign | 0x02;
801 *ptr++ = expval >> 16;
802 *ptr++ = expval >> 8;
803 *ptr++ = expval;
804 }
805 } else if(expval <= 0x7f) {
806 *ptr++ = bmsign | 0x00;
807 *ptr++ = expval;
808 } else if(expval <= 0x7fff) {
809 *ptr++ = bmsign | 0x01;
810 *ptr++ = expval >> 8;
811 *ptr++ = expval;
812 } else {
813 assert(expval <= 0x7fffff);
814 *ptr++ = bmsign | 0x02;
815 *ptr++ = expval >> 16;
816 *ptr++ = expval >> 8;
817 *ptr++ = expval;
818 }
819
820 buflen = (mstop - dscr) + 1;
821 memcpy(ptr, dscr, buflen);
822 ptr += buflen;
823 buflen = ptr - buf;
824
Lev Walkinc17d90f2005-01-17 14:32:45 +0000825 ptr = (uint8_t *)MALLOC(buflen + 1);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000826 if(!ptr) return -1;
827
828 memcpy(ptr, buf, buflen);
829 buf[buflen] = 0; /* JIC */
830
831 if(st->buf) FREEMEM(st->buf);
832 st->buf = ptr;
833 st->size = buflen;
834
835 return 0;
836}
Lev Walkin41d972b2017-08-23 23:30:59 -0700837
Lev Walkin43292722017-10-05 00:33:32 -0700838#ifndef ASN_DISABLE_OER_SUPPORT
839
840/*
841 * Encode as Canonical OER
842 */
843asn_enc_rval_t
844REAL_encode_oer(asn_TYPE_descriptor_t *td,
845 const asn_oer_constraints_t *constraints, void *sptr,
846 asn_app_consume_bytes_f *cb, void *app_key) {
847 const REAL_t *st = sptr;
848 asn_enc_rval_t er;
849 ssize_t len_len;
850
851 if(!st || !st->buf || !td)
852 ASN__ENCODE_FAILED;
853
854 if(!constraints) constraints = td->encoding_constraints.oer_constraints;
855 if(constraints && constraints->value.width != 0) {
856 /* If we're constrained to a narrow float/double representation, we
857 * shouldn't have ended up using REAL. Expecting NativeReal. */
858 ASN__ENCODE_FAILED;
859 }
860
861 /* Encode a fake REAL */
862 len_len = oer_serialize_length(st->size, cb, app_key);
863 if(len_len < 0 || cb(st->buf, st->size, app_key) < 0) {
864 ASN__ENCODE_FAILED;
865 } else {
866 er.encoded = len_len + st->size;
867 ASN__ENCODED_OK(er);
868 }
869}
870
871asn_dec_rval_t
872REAL_decode_oer(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
873 const asn_oer_constraints_t *constraints, void **sptr,
874 const void *ptr, size_t size) {
875 asn_dec_rval_t ok = {RC_OK, 0};
876 REAL_t *st;
877 uint8_t *buf;
878 ssize_t len_len;
879 size_t real_body_len;
880
Lev Walkin9cbf6782017-10-05 16:31:53 -0700881 (void)opt_codec_ctx;
882
Lev Walkin43292722017-10-05 00:33:32 -0700883 if(!constraints) constraints = td->encoding_constraints.oer_constraints;
884 if(constraints && constraints->value.width != 0) {
885 /* If we're constrained to a narrow float/double representation, we
886 * shouldn't have ended up using REAL. Expecting NativeReal. */
887 ASN__DECODE_FAILED;
888 }
889
890 len_len = oer_fetch_length(ptr, size, &real_body_len);
891 if(len_len < 0) ASN__DECODE_FAILED;
892 if(len_len == 0) ASN__DECODE_STARVED;
893
894 ptr = (const char *)ptr + len_len;
895 size -= len_len;
896
897 if(real_body_len > size) ASN__DECODE_STARVED;
898
899 buf = CALLOC(1, real_body_len + 1);
900 if(!buf) ASN__DECODE_FAILED;
901
902 if(!(st = *sptr)) {
903 st = (*sptr = CALLOC(1, sizeof(REAL_t)));
904 if(!st) {
905 FREEMEM(buf);
906 ASN__DECODE_FAILED;
907 }
908 } else {
909 FREEMEM(st->buf);
910 }
911
912 memcpy(buf, ptr, real_body_len);
913 buf[real_body_len] = '\0';
914
915 st->buf = buf;
916 st->size = real_body_len;
917
918 ok.consumed = len_len + real_body_len;
919 return ok;
920}
921
922#endif /* ASN_DISABLE_OER_SUPPORT */
923
Lev Walkin41d972b2017-08-23 23:30:59 -0700924#ifndef ASN_DISABLE_PER_SUPPORT
925
926asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700927REAL_decode_uper(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin41d972b2017-08-23 23:30:59 -0700928 const asn_per_constraints_t *constraints, void **sptr,
929 asn_per_data_t *pd) {
930 (void)constraints; /* No PER visible constraints */
931 return OCTET_STRING_decode_uper(opt_codec_ctx, td, 0, sptr, pd);
932}
933
934asn_enc_rval_t
935REAL_encode_uper(asn_TYPE_descriptor_t *td,
936 const asn_per_constraints_t *constraints, void *sptr,
937 asn_per_outp_t *po) {
938 (void)constraints; /* No PER visible constraints */
939 return OCTET_STRING_encode_uper(td, 0, sptr, po);
940}
941
942#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkina5972be2017-09-29 23:15:58 -0700943
944
945asn_random_fill_result_t
946REAL_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
947 const asn_encoding_constraints_t *constraints,
948 size_t max_length) {
949 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
950 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
951 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
Lev Walkinab1d1e12017-10-03 18:43:12 -0700952 static const double values[] = {
953 0, -0.0, -1, 1, -M_E, M_E, -3.14, 3.14, -M_PI, M_PI, -255, 255,
954 /* 2^51 */
955 -2251799813685248.0, 2251799813685248.0,
956 /* 2^52 */
957 -4503599627370496.0, 4503599627370496.0,
958 /* 2^100 */
959 -1267650600228229401496703205376.0, 1267650600228229401496703205376.0,
Lev Walkinad4c63d2017-10-05 18:07:15 +0000960 -FLT_MIN, FLT_MIN,
Jon Ringle35c3f0d2017-10-04 21:54:39 -0400961 -FLT_MAX, FLT_MAX,
Lev Walkinad4c63d2017-10-05 18:07:15 +0000962 -DBL_MIN, DBL_MIN,
963 -DBL_MAX, DBL_MAX,
964#ifdef FLT_TRUE_MIN
965 -FLT_TRUE_MIN, FLT_TRUE_MIN,
966#endif
967#ifdef DBL_TRUE_MIN
Lev Walkin43292722017-10-05 00:33:32 -0700968 -DBL_TRUE_MIN, DBL_TRUE_MIN,
Lev Walkin4ca41492017-10-03 20:29:54 -0700969#endif
970 INFINITY, -INFINITY, NAN};
Lev Walkina5972be2017-09-29 23:15:58 -0700971 REAL_t *st;
972 double d;
973
974 (void)constraints;
975
976 if(max_length == 0) return result_skipped;
977
978 d = values[asn_random_between(0, sizeof(values) / sizeof(values[0]) - 1)];
979
980 if(*sptr) {
981 st = *sptr;
982 } else {
983 st = (REAL_t*)(*sptr = CALLOC(1, sizeof(REAL_t)));
984 if(!st) {
985 return result_failed;
986 }
987 }
988
989 if(asn_double2REAL(st, d)) {
990 if(st == *sptr) {
991 ASN_STRUCT_RESET(*td, st);
992 } else {
993 ASN_STRUCT_FREE(*td, st);
994 }
995 return result_failed;
996 }
997
998 result_ok.length = st->size;
999 return result_ok;
1000}
Lev Walkinab1d1e12017-10-03 18:43:12 -07001001