blob: 7932e5d8b40b310afc3e1f1835fd638105e7f090 [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 Walkin73823e12013-03-28 00:24:48 -07009#if defined(__alpha)
Lev Walkin1aea6982004-10-26 09:35:25 +000010#include <sys/resource.h> /* For INFINITY */
Lev Walkin33700162004-10-26 09:03:31 +000011#endif
Lev Walkina9cc46e2004-09-22 16:06:28 +000012#include <asn_internal.h>
Lev Walkin41ba1f22004-09-14 12:46:35 +000013#include <stdlib.h> /* for strtod(3) */
14#include <math.h>
Jon Ringle35c3f0d2017-10-04 21:54:39 -040015#include <float.h>
Lev Walkin41ba1f22004-09-14 12:46:35 +000016#include <errno.h>
Lev Walkin33700162004-10-26 09:03:31 +000017#include <REAL.h>
Lev Walkin725883b2006-10-09 12:07:58 +000018#include <OCTET_STRING.h>
Lev Walkin41ba1f22004-09-14 12:46:35 +000019
20#undef INT_MAX
21#define INT_MAX ((int)(((unsigned int)-1) >> 1))
22
Lev Walkin1aea6982004-10-26 09:35:25 +000023#if !(defined(NAN) || defined(INFINITY))
Lev Walkin3f995632017-09-26 18:27:32 -070024static volatile double real_zero CC_NOTUSED = 0.0;
Lev Walkin1aea6982004-10-26 09:35:25 +000025#endif
Lev Walkinae1bce92004-09-27 22:18:34 +000026#ifndef NAN
Lev Walkinc51e7d62004-09-27 22:16:18 +000027#define NAN (real_zero/real_zero)
Lev Walkin40319a12004-09-14 13:40:42 +000028#endif
Lev Walkin1aea6982004-10-26 09:35:25 +000029#ifndef INFINITY
30#define INFINITY (1.0/real_zero)
31#endif
Lev Walkin40319a12004-09-14 13:40:42 +000032
Lev Walkineff98a52017-09-13 22:24:35 +000033#if defined(__clang__)
34/*
35 * isnan() is defined using generic selections and won't compile in
36 * strict C89 mode because of too fancy system's standard library.
37 * However, prior to C11 the math had a perfectly working isnan()
38 * in the math library.
39 * Disable generic selection warning so we can test C89 mode with newer libc.
40 */
41#pragma clang diagnostic push
42#pragma clang diagnostic ignored "-Wc11-extensions"
43static int asn_isnan(double d) {
44 return isnan(d);
45}
46static int asn_isfinite(double d) {
Lev Walkindaeb2162014-01-13 23:08:35 -080047#ifdef isfinite
Lev Walkineff98a52017-09-13 22:24:35 +000048 return isfinite(d); /* ISO C99 */
Lev Walkindaeb2162014-01-13 23:08:35 -080049#else
Lev Walkineff98a52017-09-13 22:24:35 +000050 return finite(d); /* Deprecated on Mac OS X 10.9 */
Lev Walkindaeb2162014-01-13 23:08:35 -080051#endif
Lev Walkineff98a52017-09-13 22:24:35 +000052}
53#pragma clang diagnostic pop
54#else /* !clang */
55#define asn_isnan(v) isnan(v)
56#ifdef isfinite
57#define asn_isfinite(d) isfinite(d) /* ISO C99 */
58#else
59#define asn_isfinite(d) finite(d) /* Deprecated on Mac OS X 10.9 */
60#endif
61#endif /* clang */
62
Lev Walkindaeb2162014-01-13 23:08:35 -080063
Lev Walkin41ba1f22004-09-14 12:46:35 +000064/*
65 * REAL basic type description.
66 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070067static const ber_tlv_tag_t asn_DEF_REAL_tags[] = {
Lev Walkin41ba1f22004-09-14 12:46:35 +000068 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
69};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080070asn_TYPE_operation_t asn_OP_REAL = {
71 ASN__PRIMITIVE_TYPE_free,
72 REAL_print,
73 REAL_compare,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080074 ber_decode_primitive,
75 der_encode_primitive,
76 REAL_decode_xer,
77 REAL_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070078#ifdef ASN_DISABLE_OER_SUPPORT
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080079 0,
80 0,
Lev Walkincc159472017-07-06 08:26:36 -070081#else
Lev Walkin43292722017-10-05 00:33:32 -070082 REAL_decode_oer,
83 REAL_encode_oer,
Lev Walkincc159472017-07-06 08:26:36 -070084#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040085#ifdef ASN_DISABLE_PER_SUPPORT
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080086 0,
87 0,
Lev Walkinb33425f2017-07-14 14:59:52 +040088#else
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080089 REAL_decode_uper,
90 REAL_encode_uper,
Lev Walkinb33425f2017-07-14 14:59:52 +040091#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkina5972be2017-09-29 23:15:58 -070092 REAL_random_fill,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080093 0 /* Use generic outmost tag fetcher */
94};
95asn_TYPE_descriptor_t asn_DEF_REAL = {
96 "REAL",
97 "REAL",
98 &asn_OP_REAL,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080099 asn_DEF_REAL_tags,
100 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
101 asn_DEF_REAL_tags, /* Same as above */
102 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
Lev Walkina5972be2017-09-29 23:15:58 -0700103 { 0, 0, asn_generic_no_constraint },
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800104 0,
105 0, /* No members */
106 0 /* No specifics */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000107};
108
Lev Walkin5f560912004-10-21 13:37:57 +0000109typedef enum specialRealValue {
110 SRV__NOT_A_NUMBER,
111 SRV__MINUS_INFINITY,
112 SRV__PLUS_INFINITY
113} specialRealValue_e;
114static struct specialRealValue_s {
115 char *string;
Lev Walkin8471cec2004-10-21 14:02:19 +0000116 size_t length;
Lev Walkin1aea6982004-10-26 09:35:25 +0000117 long dv;
Lev Walkin5f560912004-10-21 13:37:57 +0000118} specialRealValue[] = {
119#define SRV_SET(foo, val) { foo, sizeof(foo) - 1, val }
Lev Walkin1aea6982004-10-26 09:35:25 +0000120 SRV_SET("<NOT-A-NUMBER/>", 0),
121 SRV_SET("<MINUS-INFINITY/>", -1),
122 SRV_SET("<PLUS-INFINITY/>", 1),
Lev Walkin5f560912004-10-21 13:37:57 +0000123#undef SRV_SET
124};
125
Lev Walkina9cc46e2004-09-22 16:06:28 +0000126ssize_t
127REAL__dump(double d, int canonical, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin92b35d22004-09-27 20:52:18 +0000128 char local_buf[64];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000129 char *buf = local_buf;
130 ssize_t buflen = sizeof(local_buf);
Lev Walkinab1d1e12017-10-03 18:43:12 -0700131 const char *fmt = canonical ? "%.17E" /* Precise */ : "%.15f" /* Pleasant*/;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000132 ssize_t ret;
133
Lev Walkin8e8078a2004-09-26 13:10:40 +0000134 /*
135 * Check whether it is a special value.
136 */
Lev Walkinc51e7d62004-09-27 22:16:18 +0000137 /* fpclassify(3) is not portable yet */
Lev Walkineff98a52017-09-13 22:24:35 +0000138 if(asn_isnan(d)) {
Lev Walkin5f560912004-10-21 13:37:57 +0000139 buf = specialRealValue[SRV__NOT_A_NUMBER].string;
140 buflen = specialRealValue[SRV__NOT_A_NUMBER].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000141 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
Lev Walkineff98a52017-09-13 22:24:35 +0000142 } else if(!asn_isfinite(d)) {
Lev Walkinc51e7d62004-09-27 22:16:18 +0000143 if(copysign(1.0, d) < 0.0) {
Lev Walkin5f560912004-10-21 13:37:57 +0000144 buf = specialRealValue[SRV__MINUS_INFINITY].string;
145 buflen = specialRealValue[SRV__MINUS_INFINITY].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000146 } else {
Lev Walkin5f560912004-10-21 13:37:57 +0000147 buf = specialRealValue[SRV__PLUS_INFINITY].string;
148 buflen = specialRealValue[SRV__PLUS_INFINITY].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000149 }
150 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
151 } else if(ilogb(d) <= -INT_MAX) {
152 if(copysign(1.0, d) < 0.0) {
153 buf = "-0";
154 buflen = 2;
155 } else {
156 buf = "0";
157 buflen = 1;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000158 }
159 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
160 }
161
162 /*
163 * Use the libc's double printing, hopefully they got it right.
164 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000165 do {
166 ret = snprintf(buf, buflen, fmt, d);
167 if(ret < 0) {
168 buflen <<= 1;
169 } else if(ret >= buflen) {
170 buflen = ret + 1;
171 } else {
172 buflen = ret;
173 break;
174 }
Lev Walkin419f6752006-09-13 04:02:00 +0000175 if(buf != local_buf) FREEMEM(buf);
Lev Walkin8e8078a2004-09-26 13:10:40 +0000176 buf = (char *)MALLOC(buflen);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000177 if(!buf) return -1;
178 } while(1);
179
Lev Walkina9cc46e2004-09-22 16:06:28 +0000180 if(canonical) {
Lev Walkin92b35d22004-09-27 20:52:18 +0000181 /*
182 * Transform the "[-]d.dddE+-dd" output into "[-]d.dddE[-]d"
Lev Walkin5f560912004-10-21 13:37:57 +0000183 * Check that snprintf() constructed the output correctly.
Lev Walkin92b35d22004-09-27 20:52:18 +0000184 */
Lev Walkin8ca13c82016-01-24 22:40:00 -0800185 char *dot;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000186 char *end = buf + buflen;
Lev Walkin92b35d22004-09-27 20:52:18 +0000187 char *last_zero;
Lev Walkin8ca13c82016-01-24 22:40:00 -0800188 char *prev_zero;
189 char *s;
190
191 enum {
192 LZSTATE_NOTHING,
193 LZSTATE_SEEN_ZERO
194 } lz_state = LZSTATE_NOTHING;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000195
Lev Walkin5f560912004-10-21 13:37:57 +0000196 dot = (buf[0] == 0x2d /* '-' */) ? (buf + 2) : (buf + 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000197 if(*dot >= 0x30) {
Lev Walkin78d917d2012-01-07 15:29:08 -0800198 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000199 errno = EINVAL;
200 return -1; /* Not a dot, really */
201 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000202 *dot = 0x2e; /* Replace possible comma */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000203
Lev Walkin8ca13c82016-01-24 22:40:00 -0800204 for(prev_zero = last_zero = s = dot + 2; s < end; s++) {
205 switch(*s) {
206 case 0x45: /* 'E' */
207 if(lz_state == LZSTATE_SEEN_ZERO)
208 last_zero = prev_zero;
209 break;
210 case 0x30: /* '0' */
211 if(lz_state == LZSTATE_NOTHING)
212 prev_zero = s;
213 lz_state = LZSTATE_SEEN_ZERO;
214 continue;
215 default:
216 lz_state = LZSTATE_NOTHING;
217 continue;
218 }
219 break;
220 }
221
222 if(s == end) {
Lev Walkin78d917d2012-01-07 15:29:08 -0800223 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000224 errno = EINVAL;
225 return -1; /* No promised E */
226 }
Lev Walkin8ca13c82016-01-24 22:40:00 -0800227
228 assert(*s == 0x45);
229 {
230 char *E = s;
231 char *expptr = ++E;
232 char *s = expptr;
233 int sign;
234
235 if(*expptr == 0x2b /* '+' */) {
236 /* Skip the "+" */
237 buflen -= 1;
238 sign = 0;
239 } else {
240 sign = 1;
241 s++;
242 }
243 expptr++;
244 if(expptr > end) {
245 if(buf != local_buf) FREEMEM(buf);
246 errno = EINVAL;
247 return -1;
248 }
249 if(*expptr == 0x30) {
250 buflen--;
251 expptr++;
252 }
253 if(*last_zero == 0x30) {
254 *last_zero = 0x45; /* E */
255 buflen -= s - (last_zero + 1);
256 s = last_zero + 1;
257 if(sign) {
258 *s++ = 0x2d /* '-' */;
259 buflen++;
260 }
261 }
262 for(; expptr <= end; s++, expptr++)
263 *s = *expptr;
264 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000265 } else {
266 /*
267 * Remove trailing zeros.
268 */
269 char *end = buf + buflen;
270 char *last_zero = end;
Lev Walkinf0b808d2005-04-25 21:08:25 +0000271 int stoplooking = 0;
Lev Walkin92b35d22004-09-27 20:52:18 +0000272 char *z;
273 for(z = end - 1; z > buf; z--) {
274 switch(*z) {
Lev Walkinf0b808d2005-04-25 21:08:25 +0000275 case 0x30:
276 if(!stoplooking)
277 last_zero = z;
278 continue;
Lev Walkin92b35d22004-09-27 20:52:18 +0000279 case 0x31: case 0x32: case 0x33: case 0x34:
280 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinf0b808d2005-04-25 21:08:25 +0000281 stoplooking = 1;
Lev Walkin92b35d22004-09-27 20:52:18 +0000282 continue;
283 default: /* Catch dot and other separators */
Lev Walkinf0b808d2005-04-25 21:08:25 +0000284 /*
285 * Replace possible comma (which may even
286 * be not a comma at all: locale-defined).
287 */
288 *z = 0x2e;
Lev Walkin92b35d22004-09-27 20:52:18 +0000289 if(last_zero == z + 1) { /* leave x.0 */
290 last_zero++;
291 }
292 buflen = last_zero - buf;
293 *last_zero = '\0';
294 break;
295 }
296 break;
297 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000298 }
299
300 ret = cb(buf, buflen, app_key);
Lev Walkin419f6752006-09-13 04:02:00 +0000301 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000302 return (ret < 0) ? -1 : buflen;
303}
304
Lev Walkin41ba1f22004-09-14 12:46:35 +0000305int
Lev Walkin5e033762004-09-29 13:26:15 +0000306REAL_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000307 asn_app_consume_bytes_f *cb, void *app_key) {
308 const REAL_t *st = (const REAL_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000309 ssize_t ret;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000310 double d;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000311
312 (void)td; /* Unused argument */
313 (void)ilevel; /* Unused argument */
314
Lev Walkina9cc46e2004-09-22 16:06:28 +0000315 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000316 ret = cb("<absent>", 8, app_key);
Lev Walkin5e033762004-09-29 13:26:15 +0000317 else if(asn_REAL2double(st, &d))
Lev Walkin8e8078a2004-09-26 13:10:40 +0000318 ret = cb("<error>", 7, app_key);
319 else
320 ret = REAL__dump(d, 0, cb, app_key);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000321
Lev Walkin8e8078a2004-09-26 13:10:40 +0000322 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000323}
Lev Walkin41ba1f22004-09-14 12:46:35 +0000324
Lev Walkincd2f48e2017-08-10 02:14:59 -0700325int
326REAL_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
327 const void *bptr) {
328 const REAL_t *a = aptr;
329 const REAL_t *b = bptr;
330
331 (void)td;
332
333 if(a && b) {
334 double adbl, bdbl;
335 int ra, rb;
336 ra = asn_REAL2double(a, &adbl);
337 rb = asn_REAL2double(b, &bdbl);
338 if(ra == 0 && rb == 0) {
Lev Walkineff98a52017-09-13 22:24:35 +0000339 if(asn_isnan(adbl)) {
340 if(asn_isnan(bdbl)) {
Lev Walkincd2f48e2017-08-10 02:14:59 -0700341 return 0;
342 } else {
343 return -1;
344 }
Lev Walkineff98a52017-09-13 22:24:35 +0000345 } else if(asn_isnan(bdbl)) {
Lev Walkincd2f48e2017-08-10 02:14:59 -0700346 return 1;
347 }
348 /* Value comparison. */
349 if(adbl < bdbl) {
350 return -1;
351 } else if(adbl > bdbl) {
352 return 1;
353 } else {
354 return 0;
355 }
356 } else if(ra) {
357 return -1;
358 } else {
359 return 1;
360 }
361 } else if(!a) {
362 return -1;
363 } else {
364 return 1;
365 }
366}
367
Lev Walkina9cc46e2004-09-22 16:06:28 +0000368asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000369REAL_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000370 int ilevel, enum xer_encoder_flags_e flags,
371 asn_app_consume_bytes_f *cb, void *app_key) {
372 REAL_t *st = (REAL_t *)sptr;
373 asn_enc_rval_t er;
374 double d;
375
376 (void)ilevel;
377
Lev Walkin5e033762004-09-29 13:26:15 +0000378 if(!st || !st->buf || asn_REAL2double(st, &d))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700379 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000380
381 er.encoded = REAL__dump(d, flags & XER_F_CANONICAL, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700382 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000383
Lev Walkin7c1dc052016-03-14 03:08:15 -0700384 ASN__ENCODED_OK(er);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000385}
386
Lev Walkin5f560912004-10-21 13:37:57 +0000387
388/*
389 * Decode the chunk of XML text encoding REAL.
390 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000391static enum xer_pbd_rval
392REAL__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 +0000393 REAL_t *st = (REAL_t *)sptr;
Lev Walkin5f560912004-10-21 13:37:57 +0000394 double value;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000395 const char *xerdata = (const char *)chunk_buf;
Lev Walkin5f560912004-10-21 13:37:57 +0000396 char *endptr = 0;
397 char *b;
398
Lev Walkine0b56e02005-02-25 12:10:27 +0000399 (void)td;
400
Lev Walkin0fab1a62005-03-09 22:19:25 +0000401 if(!chunk_size) return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000402
403 /*
404 * Decode an XMLSpecialRealValue: <MINUS-INFINITY>, etc.
405 */
406 if(xerdata[0] == 0x3c /* '<' */) {
407 size_t i;
408 for(i = 0; i < sizeof(specialRealValue)
409 / sizeof(specialRealValue[0]); i++) {
410 struct specialRealValue_s *srv = &specialRealValue[i];
Lev Walkin1aea6982004-10-26 09:35:25 +0000411 double dv;
412
Lev Walkin5f560912004-10-21 13:37:57 +0000413 if(srv->length != chunk_size
414 || memcmp(srv->string, chunk_buf, chunk_size))
415 continue;
416
Lev Walkin1aea6982004-10-26 09:35:25 +0000417 /*
418 * It could've been done using
419 * (double)srv->dv / real_zero,
420 * but it summons fp exception on some platforms.
421 */
422 switch(srv->dv) {
423 case -1: dv = - INFINITY; break;
424 case 0: dv = NAN; break;
425 case 1: dv = INFINITY; break;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000426 default: return XPBD_SYSTEM_FAILURE;
Lev Walkin1aea6982004-10-26 09:35:25 +0000427 }
428
Lev Walkin0fab1a62005-03-09 22:19:25 +0000429 if(asn_double2REAL(st, dv))
430 return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000431
Lev Walkin0fab1a62005-03-09 22:19:25 +0000432 return XPBD_BODY_CONSUMED;
Lev Walkin5f560912004-10-21 13:37:57 +0000433 }
434 ASN_DEBUG("Unknown XMLSpecialRealValue");
Lev Walkin0fab1a62005-03-09 22:19:25 +0000435 return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000436 }
437
438 /*
439 * Copy chunk into the nul-terminated string, and run strtod.
440 */
Lev Walkin8471cec2004-10-21 14:02:19 +0000441 b = (char *)MALLOC(chunk_size + 1);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000442 if(!b) return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000443 memcpy(b, chunk_buf, chunk_size);
Lev Walkin8471cec2004-10-21 14:02:19 +0000444 b[chunk_size] = 0; /* nul-terminate */
Lev Walkin5f560912004-10-21 13:37:57 +0000445
446 value = strtod(b, &endptr);
Lev Walkin419f6752006-09-13 04:02:00 +0000447 FREEMEM(b);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000448 if(endptr == b) return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000449
450 if(asn_double2REAL(st, value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000451 return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000452
Lev Walkin0fab1a62005-03-09 22:19:25 +0000453 return XPBD_BODY_CONSUMED;
Lev Walkin5f560912004-10-21 13:37:57 +0000454}
455
456asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700457REAL_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin5f560912004-10-21 13:37:57 +0000458 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000459 const void *buf_ptr, size_t size) {
Lev Walkin5f560912004-10-21 13:37:57 +0000460
461 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000462 sptr, sizeof(REAL_t), opt_mname,
Lev Walkin5f560912004-10-21 13:37:57 +0000463 buf_ptr, size, REAL__xer_body_decode);
464}
465
Lev Walkin41ba1f22004-09-14 12:46:35 +0000466int
Lev Walkin5e033762004-09-29 13:26:15 +0000467asn_REAL2double(const REAL_t *st, double *dbl_value) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000468 unsigned int octv;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000469
470 if(!st || !st->buf) {
471 errno = EINVAL;
472 return -1;
473 }
474
475 if(st->size == 0) {
476 *dbl_value = 0;
477 return 0;
478 }
479
480 octv = st->buf[0]; /* unsigned byte */
481
482 switch(octv & 0xC0) {
Lev Walkin5fda7d52012-01-09 03:20:19 -0800483 case 0x40: /* X.690: 8.5.6 a) => 8.5.9 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000484 /* "SpecialRealValue" */
485
486 /* Be liberal in what you accept...
Lev Walkin749916f2012-01-07 17:03:39 -0800487 * http://en.wikipedia.org/wiki/Robustness_principle
Lev Walkin41ba1f22004-09-14 12:46:35 +0000488 if(st->size != 1) ...
489 */
490
491 switch(st->buf[0]) {
492 case 0x40: /* 01000000: PLUS-INFINITY */
Lev Walkin1aea6982004-10-26 09:35:25 +0000493 *dbl_value = INFINITY;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000494 return 0;
495 case 0x41: /* 01000001: MINUS-INFINITY */
Lev Walkin1aea6982004-10-26 09:35:25 +0000496 *dbl_value = - INFINITY;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000497 return 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000498 case 0x42: /* 01000010: NOT-A-NUMBER */
499 *dbl_value = NAN;
500 return 0;
501 case 0x43: /* 01000011: minus zero */
Lev Walkin2a789d92004-09-27 21:36:59 +0000502 *dbl_value = -0.0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000503 return 0;
504 }
505
506 errno = EINVAL;
507 return -1;
Lev Walkin0959ffb2011-06-25 15:48:52 -0700508 case 0x00: { /* X.690: 8.5.7 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000509 /*
Lev Walkin5fda7d52012-01-09 03:20:19 -0800510 * Decimal. NR{1,2,3} format from ISO 6093.
511 * NR1: [ ]*[+-]?[0-9]+
512 * NR2: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)
513 * NR3: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)[Ee][+-]?[0-9]+
Lev Walkin41ba1f22004-09-14 12:46:35 +0000514 */
515 double d;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800516 char *buf;
517 char *endptr;
518 int used_malloc = 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000519
Lev Walkin5fda7d52012-01-09 03:20:19 -0800520 if(octv == 0 || (octv & 0x3C)) {
Lev Walkin0959ffb2011-06-25 15:48:52 -0700521 /* Remaining values of bits 6 to 1 are Reserved. */
522 errno = EINVAL;
523 return -1;
524 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000525
Lev Walkin53e5ae62017-08-23 10:56:19 -0700526 /* 1. By contract, an input buffer should be '\0'-terminated.
Lev Walkin5fda7d52012-01-09 03:20:19 -0800527 * OCTET STRING decoder ensures that, as is asn_double2REAL().
528 * 2. ISO 6093 specifies COMMA as a possible decimal separator.
529 * However, strtod() can't always deal with COMMA.
530 * So her we fix both by reallocating, copying and fixing.
531 */
Lev Walkin53e5ae62017-08-23 10:56:19 -0700532 if(st->buf[st->size] != '\0' || memchr(st->buf, ',', st->size)) {
Lev Walkin5fda7d52012-01-09 03:20:19 -0800533 uint8_t *p, *end;
534 char *b;
535 if(st->size > 100) {
536 /* Avoid malicious stack overflow in alloca() */
537 buf = (char *)MALLOC(st->size);
538 if(!buf) return -1;
539 used_malloc = 1;
540 } else {
541 buf = alloca(st->size);
542 }
543 b = buf;
544 /* Copy without the first byte and with 0-termination */
545 for(p = st->buf + 1, end = st->buf + st->size;
546 p < end; b++, p++)
547 *b = (*p == ',') ? '.' : *p;
548 *b = '\0';
549 } else {
550 buf = (char *)&st->buf[1];
551 }
552
553 endptr = buf;
554 d = strtod(buf, &endptr);
555 if(*endptr != '\0') {
556 /* Format is not consistent with ISO 6093 */
557 if(used_malloc) FREEMEM(buf);
Lev Walkin0959ffb2011-06-25 15:48:52 -0700558 errno = EINVAL;
559 return -1;
560 }
Lev Walkin5fda7d52012-01-09 03:20:19 -0800561 if(used_malloc) FREEMEM(buf);
Lev Walkineff98a52017-09-13 22:24:35 +0000562 if(asn_isfinite(d)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000563 *dbl_value = d;
564 return 0;
565 } else {
566 errno = ERANGE;
Lev Walkin0959ffb2011-06-25 15:48:52 -0700567 return -1;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000568 }
569 }
570 }
571
572 /*
573 * Binary representation.
574 */
575 {
576 double m;
Lev Walkinab1d1e12017-10-03 18:43:12 -0700577 int32_t expval; /* exponent value */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000578 unsigned int elen; /* exponent value length, in octets */
Lev Walkin9c57f332017-09-17 22:30:49 -0700579 int scaleF;
580 int baseF;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000581 uint8_t *ptr;
582 uint8_t *end;
583 int sign;
584
585 switch((octv & 0x30) >> 4) {
586 case 0x00: baseF = 1; break; /* base 2 */
587 case 0x01: baseF = 3; break; /* base 8 */
588 case 0x02: baseF = 4; break; /* base 16 */
589 default:
590 /* Reserved field, can't parse now. */
591 errno = EINVAL;
592 return -1;
593 }
594
595 sign = (octv & 0x40); /* bit 7 */
596 scaleF = (octv & 0x0C) >> 2; /* bits 4 to 3 */
597
Lev Walkin494fb702017-08-07 20:07:00 -0700598 if(st->size <= 1 + (octv & 0x03)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000599 errno = EINVAL;
600 return -1;
601 }
602
Lev Walkine78753d2007-06-23 17:02:00 +0000603 elen = (octv & 0x03); /* bits 2 to 1; 8.5.6.4 */
604 if(elen == 0x03) { /* bits 2 to 1 = 11; 8.5.6.4, case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000605 elen = st->buf[1]; /* unsigned binary number */
Lev Walkin494fb702017-08-07 20:07:00 -0700606 if(elen == 0 || st->size <= (2 + elen)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000607 errno = EINVAL;
608 return -1;
609 }
Lev Walkine78753d2007-06-23 17:02:00 +0000610 /* FIXME: verify constraints of case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000611 ptr = &st->buf[2];
612 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000613 ptr = &st->buf[1];
614 }
615
616 /* Fetch the multibyte exponent */
617 expval = (int)(*(int8_t *)ptr);
Lev Walkinab1d1e12017-10-03 18:43:12 -0700618 if(elen >= sizeof(expval)-1) {
619 errno = ERANGE;
620 return -1;
621 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000622 end = ptr + elen + 1;
623 for(ptr++; ptr < end; ptr++)
624 expval = (expval * 256) + *ptr;
625
626 m = 0.0; /* Initial mantissa value */
627
628 /* Okay, the exponent is here. Now, what about mantissa? */
629 end = st->buf + st->size;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800630 for(; ptr < end; ptr++)
631 m = ldexp(m, 8) + *ptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000632
Lev Walkin5f560912004-10-21 13:37:57 +0000633 if(0)
Lev Walkinfb469122017-10-05 18:02:38 +0000634 ASN_DEBUG("m=%.10f, scF=%d, bF=%d, expval=%d, ldexp()=%f, ldexp()=%f\n",
635 m, scaleF, baseF, expval,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000636 ldexp(m, expval * baseF + scaleF),
Lev Walkin77aa90f2005-04-28 22:56:36 +0000637 ldexp(m, scaleF) * pow(pow(2, baseF), expval)
Lev Walkin41ba1f22004-09-14 12:46:35 +0000638 );
639
640 /*
641 * (S * N * 2^F) * B^E
642 * Essentially:
Lev Walkinab1d1e12017-10-03 18:43:12 -0700643 m = ldexp(m, scaleF) * pow(pow(2, baseF), expval);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000644 */
645 m = ldexp(m, expval * baseF + scaleF);
Lev Walkineff98a52017-09-13 22:24:35 +0000646 if(asn_isfinite(m)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000647 *dbl_value = sign ? -m : m;
648 } else {
649 errno = ERANGE;
650 return -1;
651 }
652
653 } /* if(binary_format) */
654
655 return 0;
656}
657
658/*
659 * Assume IEEE 754 floating point: standard 64 bit double.
660 * [1 bit sign] [11 bits exponent] [52 bits mantissa]
661 */
662int
Lev Walkin5e033762004-09-29 13:26:15 +0000663asn_double2REAL(REAL_t *st, double dbl_value) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000664#ifdef WORDS_BIGENDIAN /* Known to be big-endian */
665 int littleEndian = 0;
666#else /* need to test: have no explicit information */
667 unsigned int LE = 1;
668 int littleEndian = *(unsigned char *)&LE;
669#endif
670 uint8_t buf[16]; /* More than enough for 8-byte dbl_value */
671 uint8_t dscr[sizeof(dbl_value)]; /* double value scratch pad */
672 /* Assertion guards: won't even compile, if unexpected double size */
Lev Walkin3f995632017-09-26 18:27:32 -0700673 char assertion_buffer1[9 - sizeof(dbl_value)] CC_NOTUSED;
674 char assertion_buffer2[sizeof(dbl_value) - 7] CC_NOTUSED;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000675 uint8_t *ptr = buf;
676 uint8_t *mstop; /* Last byte of mantissa */
677 unsigned int mval; /* Value of the last byte of mantissa */
678 unsigned int bmsign; /* binary mask with sign */
679 unsigned int buflen;
680 unsigned int accum;
681 int expval;
682
683 if(!st) {
684 errno = EINVAL;
685 return -1;
686 }
687
Lev Walkin057fb732004-09-14 13:58:10 +0000688 /*
689 * ilogb(+-0) returns -INT_MAX or INT_MIN (platform-dependent)
Lev Walkin2f505022005-07-01 08:28:18 +0000690 * ilogb(+-inf) returns INT_MAX, logb(+-inf) returns +inf
Lev Walkin2a789d92004-09-27 21:36:59 +0000691 * ilogb(NaN) returns INT_MIN or INT_MAX (platform-dependent)
Lev Walkin057fb732004-09-14 13:58:10 +0000692 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000693 expval = ilogb(dbl_value);
Lev Walkin2a789d92004-09-27 21:36:59 +0000694 if(expval <= -INT_MAX /* Also catches +-0 and maybe isnan() */
695 || expval == INT_MAX /* catches isfin() and maybe isnan() */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000696 ) {
697 if(!st->buf || st->size < 2) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000698 ptr = (uint8_t *)MALLOC(2);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000699 if(!ptr) return -1;
Lev Walkindcae9ce2017-09-18 20:13:36 -0700700 if(st->buf) FREEMEM(st->buf);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000701 st->buf = ptr;
702 }
703 /* fpclassify(3) is not portable yet */
Lev Walkineff98a52017-09-13 22:24:35 +0000704 if(asn_isnan(dbl_value)) {
Lev Walkin2a789d92004-09-27 21:36:59 +0000705 st->buf[0] = 0x42; /* NaN */
706 st->buf[1] = 0;
707 st->size = 1;
Lev Walkineff98a52017-09-13 22:24:35 +0000708 } else if(!asn_isfinite(dbl_value)) {
Lev Walkin7e03db92004-09-14 13:50:21 +0000709 if(copysign(1.0, dbl_value) < 0.0) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000710 st->buf[0] = 0x41; /* MINUS-INFINITY */
711 } else {
712 st->buf[0] = 0x40; /* PLUS-INFINITY */
713 }
714 st->buf[1] = 0;
715 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000716 } else {
Lev Walkin5fe72e92012-01-07 17:00:29 -0800717 if(copysign(1.0, dbl_value) >= 0.0) {
Lev Walkinc51e7d62004-09-27 22:16:18 +0000718 /* no content octets: positive zero */
719 st->buf[0] = 0; /* JIC */
720 st->size = 0;
Lev Walkin5fe72e92012-01-07 17:00:29 -0800721 } else {
722 /* Negative zero. #8.5.3, 8.5.9 */
723 st->buf[0] = 0x43;
Lev Walkindcae9ce2017-09-18 20:13:36 -0700724 st->buf[1] = 0;
Lev Walkin5fe72e92012-01-07 17:00:29 -0800725 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000726 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000727 }
728 return 0;
729 }
730
731 if(littleEndian) {
732 uint8_t *s = ((uint8_t *)&dbl_value) + sizeof(dbl_value) - 2;
Lev Walkin057fb732004-09-14 13:58:10 +0000733 uint8_t *start = ((uint8_t *)&dbl_value);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000734 uint8_t *d;
735
736 bmsign = 0x80 | ((s[1] >> 1) & 0x40); /* binary mask & - */
Lev Walkin057fb732004-09-14 13:58:10 +0000737 for(mstop = d = dscr; s >= start; d++, s--) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000738 *d = *s;
739 if(*d) mstop = d;
740 }
741 } else {
742 uint8_t *s = ((uint8_t *)&dbl_value) + 1;
743 uint8_t *end = ((uint8_t *)&dbl_value) + sizeof(double);
744 uint8_t *d;
745
746 bmsign = 0x80 | ((s[-1] >> 1) & 0x40); /* binary mask & - */
747 for(mstop = d = dscr; s < end; d++, s++) {
748 *d = *s;
749 if(*d) mstop = d;
750 }
751 }
752
753 /* Remove parts of the exponent, leave mantissa and explicit 1. */
754 dscr[0] = 0x10 | (dscr[0] & 0x0f);
755
756 /* Adjust exponent in a very unobvious way */
757 expval -= 8 * ((mstop - dscr) + 1) - 4;
758
759 /* This loop ensures DER conformance by forcing mantissa odd: 11.3.1 */
760 mval = *mstop;
761 if(mval && !(mval & 1)) {
Lev Walkin9c57f332017-09-17 22:30:49 -0700762 int shift_count = 1;
763 int ishift;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000764 uint8_t *mptr;
765
766 /*
767 * Figure out what needs to be done to make mantissa odd.
768 */
769 if(!(mval & 0x0f)) /* Speed-up a little */
770 shift_count = 4;
771 while(((mval >> shift_count) & 1) == 0)
772 shift_count++;
773
774 ishift = 8 - shift_count;
775 accum = 0;
776
777 /* Go over the buffer, shifting it shift_count bits right. */
778 for(mptr = dscr; mptr <= mstop; mptr++) {
779 mval = *mptr;
780 *mptr = accum | (mval >> shift_count);
781 accum = mval << ishift;
782 }
783
Lev Walkinb89b3402012-01-09 18:26:30 -0800784 /* Adjust exponent appropriately. */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000785 expval += shift_count;
786 }
787
788 if(expval < 0) {
789 if((expval >> 7) == -1) {
790 *ptr++ = bmsign | 0x00;
791 *ptr++ = expval;
792 } else if((expval >> 15) == -1) {
793 *ptr++ = bmsign | 0x01;
794 *ptr++ = expval >> 8;
795 *ptr++ = expval;
796 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000797 *ptr++ = bmsign | 0x02;
798 *ptr++ = expval >> 16;
799 *ptr++ = expval >> 8;
800 *ptr++ = expval;
801 }
802 } else if(expval <= 0x7f) {
803 *ptr++ = bmsign | 0x00;
804 *ptr++ = expval;
805 } else if(expval <= 0x7fff) {
806 *ptr++ = bmsign | 0x01;
807 *ptr++ = expval >> 8;
808 *ptr++ = expval;
809 } else {
810 assert(expval <= 0x7fffff);
811 *ptr++ = bmsign | 0x02;
812 *ptr++ = expval >> 16;
813 *ptr++ = expval >> 8;
814 *ptr++ = expval;
815 }
816
817 buflen = (mstop - dscr) + 1;
818 memcpy(ptr, dscr, buflen);
819 ptr += buflen;
820 buflen = ptr - buf;
821
Lev Walkinc17d90f2005-01-17 14:32:45 +0000822 ptr = (uint8_t *)MALLOC(buflen + 1);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000823 if(!ptr) return -1;
824
825 memcpy(ptr, buf, buflen);
826 buf[buflen] = 0; /* JIC */
827
828 if(st->buf) FREEMEM(st->buf);
829 st->buf = ptr;
830 st->size = buflen;
831
832 return 0;
833}
Lev Walkin41d972b2017-08-23 23:30:59 -0700834
Lev Walkin43292722017-10-05 00:33:32 -0700835#ifndef ASN_DISABLE_OER_SUPPORT
836
837/*
838 * Encode as Canonical OER
839 */
840asn_enc_rval_t
841REAL_encode_oer(asn_TYPE_descriptor_t *td,
842 const asn_oer_constraints_t *constraints, void *sptr,
843 asn_app_consume_bytes_f *cb, void *app_key) {
844 const REAL_t *st = sptr;
845 asn_enc_rval_t er;
846 ssize_t len_len;
847
848 if(!st || !st->buf || !td)
849 ASN__ENCODE_FAILED;
850
851 if(!constraints) constraints = td->encoding_constraints.oer_constraints;
852 if(constraints && constraints->value.width != 0) {
853 /* If we're constrained to a narrow float/double representation, we
854 * shouldn't have ended up using REAL. Expecting NativeReal. */
855 ASN__ENCODE_FAILED;
856 }
857
858 /* Encode a fake REAL */
859 len_len = oer_serialize_length(st->size, cb, app_key);
860 if(len_len < 0 || cb(st->buf, st->size, app_key) < 0) {
861 ASN__ENCODE_FAILED;
862 } else {
863 er.encoded = len_len + st->size;
864 ASN__ENCODED_OK(er);
865 }
866}
867
868asn_dec_rval_t
869REAL_decode_oer(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
870 const asn_oer_constraints_t *constraints, void **sptr,
871 const void *ptr, size_t size) {
872 asn_dec_rval_t ok = {RC_OK, 0};
873 REAL_t *st;
874 uint8_t *buf;
875 ssize_t len_len;
876 size_t real_body_len;
877
Lev Walkin9cbf6782017-10-05 16:31:53 -0700878 (void)opt_codec_ctx;
879
Lev Walkin43292722017-10-05 00:33:32 -0700880 if(!constraints) constraints = td->encoding_constraints.oer_constraints;
881 if(constraints && constraints->value.width != 0) {
882 /* If we're constrained to a narrow float/double representation, we
883 * shouldn't have ended up using REAL. Expecting NativeReal. */
884 ASN__DECODE_FAILED;
885 }
886
887 len_len = oer_fetch_length(ptr, size, &real_body_len);
888 if(len_len < 0) ASN__DECODE_FAILED;
889 if(len_len == 0) ASN__DECODE_STARVED;
890
891 ptr = (const char *)ptr + len_len;
892 size -= len_len;
893
894 if(real_body_len > size) ASN__DECODE_STARVED;
895
896 buf = CALLOC(1, real_body_len + 1);
897 if(!buf) ASN__DECODE_FAILED;
898
899 if(!(st = *sptr)) {
900 st = (*sptr = CALLOC(1, sizeof(REAL_t)));
901 if(!st) {
902 FREEMEM(buf);
903 ASN__DECODE_FAILED;
904 }
905 } else {
906 FREEMEM(st->buf);
907 }
908
909 memcpy(buf, ptr, real_body_len);
910 buf[real_body_len] = '\0';
911
912 st->buf = buf;
913 st->size = real_body_len;
914
915 ok.consumed = len_len + real_body_len;
916 return ok;
917}
918
919#endif /* ASN_DISABLE_OER_SUPPORT */
920
Lev Walkin41d972b2017-08-23 23:30:59 -0700921#ifndef ASN_DISABLE_PER_SUPPORT
922
923asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700924REAL_decode_uper(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin41d972b2017-08-23 23:30:59 -0700925 const asn_per_constraints_t *constraints, void **sptr,
926 asn_per_data_t *pd) {
927 (void)constraints; /* No PER visible constraints */
928 return OCTET_STRING_decode_uper(opt_codec_ctx, td, 0, sptr, pd);
929}
930
931asn_enc_rval_t
932REAL_encode_uper(asn_TYPE_descriptor_t *td,
933 const asn_per_constraints_t *constraints, void *sptr,
934 asn_per_outp_t *po) {
935 (void)constraints; /* No PER visible constraints */
936 return OCTET_STRING_encode_uper(td, 0, sptr, po);
937}
938
939#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkina5972be2017-09-29 23:15:58 -0700940
941
942asn_random_fill_result_t
943REAL_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
944 const asn_encoding_constraints_t *constraints,
945 size_t max_length) {
946 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
947 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
948 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
Lev Walkinab1d1e12017-10-03 18:43:12 -0700949 static const double values[] = {
950 0, -0.0, -1, 1, -M_E, M_E, -3.14, 3.14, -M_PI, M_PI, -255, 255,
951 /* 2^51 */
952 -2251799813685248.0, 2251799813685248.0,
953 /* 2^52 */
954 -4503599627370496.0, 4503599627370496.0,
955 /* 2^100 */
956 -1267650600228229401496703205376.0, 1267650600228229401496703205376.0,
Lev Walkinad4c63d2017-10-05 18:07:15 +0000957 -FLT_MIN, FLT_MIN,
Jon Ringle35c3f0d2017-10-04 21:54:39 -0400958 -FLT_MAX, FLT_MAX,
Lev Walkinad4c63d2017-10-05 18:07:15 +0000959 -DBL_MIN, DBL_MIN,
960 -DBL_MAX, DBL_MAX,
961#ifdef FLT_TRUE_MIN
962 -FLT_TRUE_MIN, FLT_TRUE_MIN,
963#endif
964#ifdef DBL_TRUE_MIN
Lev Walkin43292722017-10-05 00:33:32 -0700965 -DBL_TRUE_MIN, DBL_TRUE_MIN,
Lev Walkin4ca41492017-10-03 20:29:54 -0700966#endif
967 INFINITY, -INFINITY, NAN};
Lev Walkina5972be2017-09-29 23:15:58 -0700968 REAL_t *st;
969 double d;
970
971 (void)constraints;
972
973 if(max_length == 0) return result_skipped;
974
975 d = values[asn_random_between(0, sizeof(values) / sizeof(values[0]) - 1)];
976
977 if(*sptr) {
978 st = *sptr;
979 } else {
980 st = (REAL_t*)(*sptr = CALLOC(1, sizeof(REAL_t)));
981 if(!st) {
982 return result_failed;
983 }
984 }
985
986 if(asn_double2REAL(st, d)) {
987 if(st == *sptr) {
988 ASN_STRUCT_RESET(*td, st);
989 } else {
990 ASN_STRUCT_FREE(*td, st);
991 }
992 return result_failed;
993 }
994
995 result_ok.length = st->size;
996 return result_ok;
997}
Lev Walkinab1d1e12017-10-03 18:43:12 -0700998