blob: f1d06650e95e6179835cecfcaf6e78e93b691ea1 [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 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 Walkinfe374312017-10-07 00:24:51 -070027#define NAN (0.0/0.0)
Lev Walkin40319a12004-09-14 13:40:42 +000028#endif
Lev Walkin1aea6982004-10-26 09:35:25 +000029#ifndef INFINITY
Lev Walkinfe374312017-10-07 00:24:51 -070030#define INFINITY (1.0/0.0)
Lev Walkin1aea6982004-10-26 09:35:25 +000031#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 Walkin41ba1f22004-09-14 12:46:35 +000063/*
64 * REAL basic type description.
65 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070066static const ber_tlv_tag_t asn_DEF_REAL_tags[] = {
Lev Walkin41ba1f22004-09-14 12:46:35 +000067 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
68};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080069asn_TYPE_operation_t asn_OP_REAL = {
70 ASN__PRIMITIVE_TYPE_free,
71 REAL_print,
72 REAL_compare,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080073 ber_decode_primitive,
74 der_encode_primitive,
75 REAL_decode_xer,
76 REAL_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070077#ifdef ASN_DISABLE_OER_SUPPORT
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080078 0,
79 0,
Lev Walkincc159472017-07-06 08:26:36 -070080#else
Lev Walkin43292722017-10-05 00:33:32 -070081 REAL_decode_oer,
82 REAL_encode_oer,
Lev Walkincc159472017-07-06 08:26:36 -070083#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040084#ifdef ASN_DISABLE_PER_SUPPORT
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080085 0,
86 0,
Lev Walkinb33425f2017-07-14 14:59:52 +040087#else
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080088 REAL_decode_uper,
89 REAL_encode_uper,
Lev Walkinb33425f2017-07-14 14:59:52 +040090#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkina5972be2017-09-29 23:15:58 -070091 REAL_random_fill,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080092 0 /* Use generic outmost tag fetcher */
93};
94asn_TYPE_descriptor_t asn_DEF_REAL = {
95 "REAL",
96 "REAL",
97 &asn_OP_REAL,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080098 asn_DEF_REAL_tags,
99 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
100 asn_DEF_REAL_tags, /* Same as above */
101 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
Lev Walkina5972be2017-09-29 23:15:58 -0700102 { 0, 0, asn_generic_no_constraint },
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +0800103 0,
104 0, /* No members */
105 0 /* No specifics */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000106};
107
Lev Walkin5f560912004-10-21 13:37:57 +0000108typedef enum specialRealValue {
109 SRV__NOT_A_NUMBER,
110 SRV__MINUS_INFINITY,
111 SRV__PLUS_INFINITY
112} specialRealValue_e;
113static struct specialRealValue_s {
114 char *string;
Lev Walkin8471cec2004-10-21 14:02:19 +0000115 size_t length;
Lev Walkin1aea6982004-10-26 09:35:25 +0000116 long dv;
Lev Walkin5f560912004-10-21 13:37:57 +0000117} specialRealValue[] = {
118#define SRV_SET(foo, val) { foo, sizeof(foo) - 1, val }
Lev Walkin1aea6982004-10-26 09:35:25 +0000119 SRV_SET("<NOT-A-NUMBER/>", 0),
120 SRV_SET("<MINUS-INFINITY/>", -1),
121 SRV_SET("<PLUS-INFINITY/>", 1),
Lev Walkin5f560912004-10-21 13:37:57 +0000122#undef SRV_SET
123};
124
Lev Walkina9cc46e2004-09-22 16:06:28 +0000125ssize_t
126REAL__dump(double d, int canonical, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin92b35d22004-09-27 20:52:18 +0000127 char local_buf[64];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000128 char *buf = local_buf;
129 ssize_t buflen = sizeof(local_buf);
Lev Walkinab1d1e12017-10-03 18:43:12 -0700130 const char *fmt = canonical ? "%.17E" /* Precise */ : "%.15f" /* Pleasant*/;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000131 ssize_t ret;
132
Lev Walkin8e8078a2004-09-26 13:10:40 +0000133 /*
134 * Check whether it is a special value.
135 */
Lev Walkinc51e7d62004-09-27 22:16:18 +0000136 /* fpclassify(3) is not portable yet */
Lev Walkineff98a52017-09-13 22:24:35 +0000137 if(asn_isnan(d)) {
Lev Walkin5f560912004-10-21 13:37:57 +0000138 buf = specialRealValue[SRV__NOT_A_NUMBER].string;
139 buflen = specialRealValue[SRV__NOT_A_NUMBER].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000140 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
Lev Walkineff98a52017-09-13 22:24:35 +0000141 } else if(!asn_isfinite(d)) {
Lev Walkinc51e7d62004-09-27 22:16:18 +0000142 if(copysign(1.0, d) < 0.0) {
Lev Walkin5f560912004-10-21 13:37:57 +0000143 buf = specialRealValue[SRV__MINUS_INFINITY].string;
144 buflen = specialRealValue[SRV__MINUS_INFINITY].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000145 } else {
Lev Walkin5f560912004-10-21 13:37:57 +0000146 buf = specialRealValue[SRV__PLUS_INFINITY].string;
147 buflen = specialRealValue[SRV__PLUS_INFINITY].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000148 }
149 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
150 } else if(ilogb(d) <= -INT_MAX) {
151 if(copysign(1.0, d) < 0.0) {
152 buf = "-0";
153 buflen = 2;
154 } else {
155 buf = "0";
156 buflen = 1;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000157 }
158 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
159 }
160
161 /*
162 * Use the libc's double printing, hopefully they got it right.
163 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000164 do {
165 ret = snprintf(buf, buflen, fmt, d);
166 if(ret < 0) {
Lev Walkin63a89da2017-10-08 02:48:32 -0700167 /* There are some old broken APIs. */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000168 buflen <<= 1;
Lev Walkin63a89da2017-10-08 02:48:32 -0700169 if(buflen > 4096) {
170 /* Should be plenty. */
171 if(buf != local_buf) FREEMEM(buf);
172 return -1;
173 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000174 } else if(ret >= buflen) {
175 buflen = ret + 1;
176 } else {
177 buflen = ret;
178 break;
179 }
Lev Walkin419f6752006-09-13 04:02:00 +0000180 if(buf != local_buf) FREEMEM(buf);
Lev Walkin8e8078a2004-09-26 13:10:40 +0000181 buf = (char *)MALLOC(buflen);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000182 if(!buf) return -1;
183 } while(1);
184
Lev Walkina9cc46e2004-09-22 16:06:28 +0000185 if(canonical) {
Lev Walkin92b35d22004-09-27 20:52:18 +0000186 /*
187 * Transform the "[-]d.dddE+-dd" output into "[-]d.dddE[-]d"
Lev Walkin5f560912004-10-21 13:37:57 +0000188 * Check that snprintf() constructed the output correctly.
Lev Walkin92b35d22004-09-27 20:52:18 +0000189 */
Lev Walkin8ca13c82016-01-24 22:40:00 -0800190 char *dot;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000191 char *end = buf + buflen;
Lev Walkin92b35d22004-09-27 20:52:18 +0000192 char *last_zero;
Lev Walkin63a89da2017-10-08 02:48:32 -0700193 char *first_zero_in_run;
Lev Walkin8ca13c82016-01-24 22:40:00 -0800194 char *s;
195
196 enum {
197 LZSTATE_NOTHING,
Lev Walkin63a89da2017-10-08 02:48:32 -0700198 LZSTATE_ZEROES
Lev Walkin8ca13c82016-01-24 22:40:00 -0800199 } lz_state = LZSTATE_NOTHING;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000200
Lev Walkin5f560912004-10-21 13:37:57 +0000201 dot = (buf[0] == 0x2d /* '-' */) ? (buf + 2) : (buf + 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000202 if(*dot >= 0x30) {
Lev Walkin78d917d2012-01-07 15:29:08 -0800203 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000204 errno = EINVAL;
205 return -1; /* Not a dot, really */
206 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000207 *dot = 0x2e; /* Replace possible comma */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000208
Lev Walkin63a89da2017-10-08 02:48:32 -0700209 for(first_zero_in_run = last_zero = s = dot + 2; s < end; s++) {
Lev Walkin8ca13c82016-01-24 22:40:00 -0800210 switch(*s) {
211 case 0x45: /* 'E' */
Lev Walkin63a89da2017-10-08 02:48:32 -0700212 if(lz_state == LZSTATE_ZEROES) last_zero = first_zero_in_run;
Lev Walkin8ca13c82016-01-24 22:40:00 -0800213 break;
Lev Walkin63a89da2017-10-08 02:48:32 -0700214 case 0x30: /* '0' */
215 if(lz_state == LZSTATE_NOTHING) first_zero_in_run = s;
216 lz_state = LZSTATE_ZEROES;
Lev Walkin8ca13c82016-01-24 22:40:00 -0800217 continue;
218 default:
219 lz_state = LZSTATE_NOTHING;
220 continue;
221 }
222 break;
223 }
224
225 if(s == end) {
Lev Walkin78d917d2012-01-07 15:29:08 -0800226 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000227 errno = EINVAL;
228 return -1; /* No promised E */
229 }
Lev Walkin8ca13c82016-01-24 22:40:00 -0800230
231 assert(*s == 0x45);
232 {
233 char *E = s;
234 char *expptr = ++E;
235 char *s = expptr;
236 int sign;
237
238 if(*expptr == 0x2b /* '+' */) {
239 /* Skip the "+" */
240 buflen -= 1;
241 sign = 0;
242 } else {
243 sign = 1;
244 s++;
245 }
246 expptr++;
247 if(expptr > end) {
248 if(buf != local_buf) FREEMEM(buf);
249 errno = EINVAL;
250 return -1;
251 }
252 if(*expptr == 0x30) {
253 buflen--;
254 expptr++;
255 }
Lev Walkin63a89da2017-10-08 02:48:32 -0700256 if(lz_state == LZSTATE_ZEROES) {
Lev Walkin8ca13c82016-01-24 22:40:00 -0800257 *last_zero = 0x45; /* E */
258 buflen -= s - (last_zero + 1);
259 s = last_zero + 1;
260 if(sign) {
261 *s++ = 0x2d /* '-' */;
262 buflen++;
263 }
264 }
265 for(; expptr <= end; s++, expptr++)
266 *s = *expptr;
267 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000268 } else {
269 /*
270 * Remove trailing zeros.
271 */
272 char *end = buf + buflen;
273 char *last_zero = end;
Lev Walkinf0b808d2005-04-25 21:08:25 +0000274 int stoplooking = 0;
Lev Walkin92b35d22004-09-27 20:52:18 +0000275 char *z;
276 for(z = end - 1; z > buf; z--) {
277 switch(*z) {
Lev Walkinf0b808d2005-04-25 21:08:25 +0000278 case 0x30:
279 if(!stoplooking)
280 last_zero = z;
281 continue;
Lev Walkin92b35d22004-09-27 20:52:18 +0000282 case 0x31: case 0x32: case 0x33: case 0x34:
283 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinf0b808d2005-04-25 21:08:25 +0000284 stoplooking = 1;
Lev Walkin92b35d22004-09-27 20:52:18 +0000285 continue;
286 default: /* Catch dot and other separators */
Lev Walkinf0b808d2005-04-25 21:08:25 +0000287 /*
288 * Replace possible comma (which may even
289 * be not a comma at all: locale-defined).
290 */
291 *z = 0x2e;
Lev Walkin92b35d22004-09-27 20:52:18 +0000292 if(last_zero == z + 1) { /* leave x.0 */
293 last_zero++;
294 }
295 buflen = last_zero - buf;
296 *last_zero = '\0';
297 break;
298 }
299 break;
300 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000301 }
302
303 ret = cb(buf, buflen, app_key);
Lev Walkin419f6752006-09-13 04:02:00 +0000304 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000305 return (ret < 0) ? -1 : buflen;
306}
307
Lev Walkin41ba1f22004-09-14 12:46:35 +0000308int
Lev Walkin20696a42017-10-17 21:27:33 -0700309REAL_print(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
310 asn_app_consume_bytes_f *cb, void *app_key) {
311 const REAL_t *st = (const REAL_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000312 ssize_t ret;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000313 double d;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000314
315 (void)td; /* Unused argument */
316 (void)ilevel; /* Unused argument */
317
Lev Walkina9cc46e2004-09-22 16:06:28 +0000318 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000319 ret = cb("<absent>", 8, app_key);
Lev Walkin5e033762004-09-29 13:26:15 +0000320 else if(asn_REAL2double(st, &d))
Lev Walkin8e8078a2004-09-26 13:10:40 +0000321 ret = cb("<error>", 7, app_key);
322 else
323 ret = REAL__dump(d, 0, cb, app_key);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000324
Lev Walkin8e8078a2004-09-26 13:10:40 +0000325 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000326}
Lev Walkin41ba1f22004-09-14 12:46:35 +0000327
Lev Walkincd2f48e2017-08-10 02:14:59 -0700328int
329REAL_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
330 const void *bptr) {
331 const REAL_t *a = aptr;
332 const REAL_t *b = bptr;
333
334 (void)td;
335
336 if(a && b) {
337 double adbl, bdbl;
338 int ra, rb;
339 ra = asn_REAL2double(a, &adbl);
340 rb = asn_REAL2double(b, &bdbl);
341 if(ra == 0 && rb == 0) {
Lev Walkineff98a52017-09-13 22:24:35 +0000342 if(asn_isnan(adbl)) {
343 if(asn_isnan(bdbl)) {
Lev Walkincd2f48e2017-08-10 02:14:59 -0700344 return 0;
345 } else {
346 return -1;
347 }
Lev Walkineff98a52017-09-13 22:24:35 +0000348 } else if(asn_isnan(bdbl)) {
Lev Walkincd2f48e2017-08-10 02:14:59 -0700349 return 1;
350 }
351 /* Value comparison. */
352 if(adbl < bdbl) {
353 return -1;
354 } else if(adbl > bdbl) {
355 return 1;
356 } else {
357 return 0;
358 }
359 } else if(ra) {
360 return -1;
361 } else {
362 return 1;
363 }
364 } else if(!a) {
365 return -1;
366 } else {
367 return 1;
368 }
369}
370
Lev Walkina9cc46e2004-09-22 16:06:28 +0000371asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700372REAL_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
373 enum xer_encoder_flags_e flags, asn_app_consume_bytes_f *cb,
374 void *app_key) {
375 const REAL_t *st = (const REAL_t *)sptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000376 asn_enc_rval_t er;
377 double d;
378
379 (void)ilevel;
380
Lev Walkin5e033762004-09-29 13:26:15 +0000381 if(!st || !st->buf || asn_REAL2double(st, &d))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700382 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000383
384 er.encoded = REAL__dump(d, flags & XER_F_CANONICAL, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700385 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000386
Lev Walkin7c1dc052016-03-14 03:08:15 -0700387 ASN__ENCODED_OK(er);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000388}
389
Lev Walkin5f560912004-10-21 13:37:57 +0000390
391/*
392 * Decode the chunk of XML text encoding REAL.
393 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000394static enum xer_pbd_rval
Lev Walkin20696a42017-10-17 21:27:33 -0700395REAL__xer_body_decode(const asn_TYPE_descriptor_t *td, void *sptr,
396 const void *chunk_buf, size_t chunk_size) {
397 REAL_t *st = (REAL_t *)sptr;
Lev Walkin5f560912004-10-21 13:37:57 +0000398 double value;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000399 const char *xerdata = (const char *)chunk_buf;
Lev Walkin5f560912004-10-21 13:37:57 +0000400 char *endptr = 0;
401 char *b;
402
Lev Walkine0b56e02005-02-25 12:10:27 +0000403 (void)td;
404
Lev Walkin0fab1a62005-03-09 22:19:25 +0000405 if(!chunk_size) return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000406
407 /*
408 * Decode an XMLSpecialRealValue: <MINUS-INFINITY>, etc.
409 */
410 if(xerdata[0] == 0x3c /* '<' */) {
411 size_t i;
412 for(i = 0; i < sizeof(specialRealValue)
413 / sizeof(specialRealValue[0]); i++) {
414 struct specialRealValue_s *srv = &specialRealValue[i];
Lev Walkin1aea6982004-10-26 09:35:25 +0000415 double dv;
416
Lev Walkin5f560912004-10-21 13:37:57 +0000417 if(srv->length != chunk_size
418 || memcmp(srv->string, chunk_buf, chunk_size))
419 continue;
420
Lev Walkin1aea6982004-10-26 09:35:25 +0000421 /*
422 * It could've been done using
423 * (double)srv->dv / real_zero,
424 * but it summons fp exception on some platforms.
425 */
426 switch(srv->dv) {
427 case -1: dv = - INFINITY; break;
428 case 0: dv = NAN; break;
429 case 1: dv = INFINITY; break;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000430 default: return XPBD_SYSTEM_FAILURE;
Lev Walkin1aea6982004-10-26 09:35:25 +0000431 }
432
Lev Walkin0fab1a62005-03-09 22:19:25 +0000433 if(asn_double2REAL(st, dv))
434 return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000435
Lev Walkin0fab1a62005-03-09 22:19:25 +0000436 return XPBD_BODY_CONSUMED;
Lev Walkin5f560912004-10-21 13:37:57 +0000437 }
438 ASN_DEBUG("Unknown XMLSpecialRealValue");
Lev Walkin0fab1a62005-03-09 22:19:25 +0000439 return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000440 }
441
442 /*
443 * Copy chunk into the nul-terminated string, and run strtod.
444 */
Lev Walkin8471cec2004-10-21 14:02:19 +0000445 b = (char *)MALLOC(chunk_size + 1);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000446 if(!b) return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000447 memcpy(b, chunk_buf, chunk_size);
Lev Walkin8471cec2004-10-21 14:02:19 +0000448 b[chunk_size] = 0; /* nul-terminate */
Lev Walkin5f560912004-10-21 13:37:57 +0000449
450 value = strtod(b, &endptr);
Lev Walkin419f6752006-09-13 04:02:00 +0000451 FREEMEM(b);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000452 if(endptr == b) return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000453
454 if(asn_double2REAL(st, value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000455 return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000456
Lev Walkin0fab1a62005-03-09 22:19:25 +0000457 return XPBD_BODY_CONSUMED;
Lev Walkin5f560912004-10-21 13:37:57 +0000458}
459
460asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700461REAL_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin20696a42017-10-17 21:27:33 -0700462 const asn_TYPE_descriptor_t *td, void **sptr,
463 const char *opt_mname, const void *buf_ptr, size_t size) {
464 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000465 sptr, sizeof(REAL_t), opt_mname,
Lev Walkin5f560912004-10-21 13:37:57 +0000466 buf_ptr, size, REAL__xer_body_decode);
467}
468
Lev Walkin41ba1f22004-09-14 12:46:35 +0000469int
Lev Walkin5e033762004-09-29 13:26:15 +0000470asn_REAL2double(const REAL_t *st, double *dbl_value) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000471 unsigned int octv;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000472
473 if(!st || !st->buf) {
474 errno = EINVAL;
475 return -1;
476 }
477
478 if(st->size == 0) {
479 *dbl_value = 0;
480 return 0;
481 }
482
483 octv = st->buf[0]; /* unsigned byte */
484
485 switch(octv & 0xC0) {
Lev Walkin5fda7d52012-01-09 03:20:19 -0800486 case 0x40: /* X.690: 8.5.6 a) => 8.5.9 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000487 /* "SpecialRealValue" */
488
489 /* Be liberal in what you accept...
Lev Walkin749916f2012-01-07 17:03:39 -0800490 * http://en.wikipedia.org/wiki/Robustness_principle
Lev Walkin41ba1f22004-09-14 12:46:35 +0000491 if(st->size != 1) ...
492 */
493
494 switch(st->buf[0]) {
495 case 0x40: /* 01000000: PLUS-INFINITY */
Lev Walkin1aea6982004-10-26 09:35:25 +0000496 *dbl_value = INFINITY;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000497 return 0;
498 case 0x41: /* 01000001: MINUS-INFINITY */
Lev Walkin1aea6982004-10-26 09:35:25 +0000499 *dbl_value = - INFINITY;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000500 return 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000501 case 0x42: /* 01000010: NOT-A-NUMBER */
502 *dbl_value = NAN;
503 return 0;
504 case 0x43: /* 01000011: minus zero */
Lev Walkin2a789d92004-09-27 21:36:59 +0000505 *dbl_value = -0.0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000506 return 0;
507 }
508
509 errno = EINVAL;
510 return -1;
Lev Walkin0959ffb2011-06-25 15:48:52 -0700511 case 0x00: { /* X.690: 8.5.7 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000512 /*
Lev Walkin5fda7d52012-01-09 03:20:19 -0800513 * Decimal. NR{1,2,3} format from ISO 6093.
514 * NR1: [ ]*[+-]?[0-9]+
515 * NR2: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)
516 * NR3: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)[Ee][+-]?[0-9]+
Lev Walkin41ba1f22004-09-14 12:46:35 +0000517 */
518 double d;
Lev Walkine2bbbf82017-10-08 18:52:37 -0700519 char *source = 0;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800520 char *endptr;
521 int used_malloc = 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000522
Lev Walkin5fda7d52012-01-09 03:20:19 -0800523 if(octv == 0 || (octv & 0x3C)) {
Lev Walkin0959ffb2011-06-25 15:48:52 -0700524 /* Remaining values of bits 6 to 1 are Reserved. */
525 errno = EINVAL;
526 return -1;
527 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000528
Lev Walkin53e5ae62017-08-23 10:56:19 -0700529 /* 1. By contract, an input buffer should be '\0'-terminated.
Lev Walkin5fda7d52012-01-09 03:20:19 -0800530 * OCTET STRING decoder ensures that, as is asn_double2REAL().
531 * 2. ISO 6093 specifies COMMA as a possible decimal separator.
532 * However, strtod() can't always deal with COMMA.
533 * So her we fix both by reallocating, copying and fixing.
534 */
Lev Walkin53e5ae62017-08-23 10:56:19 -0700535 if(st->buf[st->size] != '\0' || memchr(st->buf, ',', st->size)) {
Lev Walkine2bbbf82017-10-08 18:52:37 -0700536 const uint8_t *p, *end;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800537 char *b;
Lev Walkine2bbbf82017-10-08 18:52:37 -0700538
539 b = source = (char *)MALLOC(st->size + 1);
540 if(!source) return -1;
541 used_malloc = 1;
542
Lev Walkin5fda7d52012-01-09 03:20:19 -0800543 /* Copy without the first byte and with 0-termination */
544 for(p = st->buf + 1, end = st->buf + st->size;
545 p < end; b++, p++)
546 *b = (*p == ',') ? '.' : *p;
547 *b = '\0';
548 } else {
Lev Walkine2bbbf82017-10-08 18:52:37 -0700549 source = (char *)&st->buf[1];
Lev Walkin5fda7d52012-01-09 03:20:19 -0800550 }
551
Lev Walkine2bbbf82017-10-08 18:52:37 -0700552 endptr = source;
553 d = strtod(source, &endptr);
Lev Walkin5fda7d52012-01-09 03:20:19 -0800554 if(*endptr != '\0') {
555 /* Format is not consistent with ISO 6093 */
Lev Walkine2bbbf82017-10-08 18:52:37 -0700556 if(used_malloc) FREEMEM(source);
Lev Walkin0959ffb2011-06-25 15:48:52 -0700557 errno = EINVAL;
558 return -1;
559 }
Lev Walkine2bbbf82017-10-08 18:52:37 -0700560 if(used_malloc) FREEMEM(source);
Lev Walkineff98a52017-09-13 22:24:35 +0000561 if(asn_isfinite(d)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000562 *dbl_value = d;
563 return 0;
564 } else {
565 errno = ERANGE;
Lev Walkin0959ffb2011-06-25 15:48:52 -0700566 return -1;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000567 }
568 }
569 }
570
571 /*
572 * Binary representation.
573 */
574 {
575 double m;
Lev Walkinab1d1e12017-10-03 18:43:12 -0700576 int32_t expval; /* exponent value */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000577 unsigned int elen; /* exponent value length, in octets */
Lev Walkin9c57f332017-09-17 22:30:49 -0700578 int scaleF;
579 int baseF;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000580 uint8_t *ptr;
581 uint8_t *end;
582 int sign;
583
584 switch((octv & 0x30) >> 4) {
585 case 0x00: baseF = 1; break; /* base 2 */
586 case 0x01: baseF = 3; break; /* base 8 */
587 case 0x02: baseF = 4; break; /* base 16 */
588 default:
589 /* Reserved field, can't parse now. */
590 errno = EINVAL;
591 return -1;
592 }
593
594 sign = (octv & 0x40); /* bit 7 */
595 scaleF = (octv & 0x0C) >> 2; /* bits 4 to 3 */
596
Lev Walkin494fb702017-08-07 20:07:00 -0700597 if(st->size <= 1 + (octv & 0x03)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000598 errno = EINVAL;
599 return -1;
600 }
601
Lev Walkine78753d2007-06-23 17:02:00 +0000602 elen = (octv & 0x03); /* bits 2 to 1; 8.5.6.4 */
603 if(elen == 0x03) { /* bits 2 to 1 = 11; 8.5.6.4, case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000604 elen = st->buf[1]; /* unsigned binary number */
Lev Walkin494fb702017-08-07 20:07:00 -0700605 if(elen == 0 || st->size <= (2 + elen)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000606 errno = EINVAL;
607 return -1;
608 }
Lev Walkine78753d2007-06-23 17:02:00 +0000609 /* FIXME: verify constraints of case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000610 ptr = &st->buf[2];
611 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000612 ptr = &st->buf[1];
613 }
614
615 /* Fetch the multibyte exponent */
616 expval = (int)(*(int8_t *)ptr);
Lev Walkinab1d1e12017-10-03 18:43:12 -0700617 if(elen >= sizeof(expval)-1) {
618 errno = ERANGE;
619 return -1;
620 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000621 end = ptr + elen + 1;
622 for(ptr++; ptr < end; ptr++)
623 expval = (expval * 256) + *ptr;
624
625 m = 0.0; /* Initial mantissa value */
626
627 /* Okay, the exponent is here. Now, what about mantissa? */
628 end = st->buf + st->size;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800629 for(; ptr < end; ptr++)
630 m = ldexp(m, 8) + *ptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000631
Lev Walkin5f560912004-10-21 13:37:57 +0000632 if(0)
Lev Walkinfb469122017-10-05 18:02:38 +0000633 ASN_DEBUG("m=%.10f, scF=%d, bF=%d, expval=%d, ldexp()=%f, ldexp()=%f\n",
634 m, scaleF, baseF, expval,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000635 ldexp(m, expval * baseF + scaleF),
Lev Walkin77aa90f2005-04-28 22:56:36 +0000636 ldexp(m, scaleF) * pow(pow(2, baseF), expval)
Lev Walkin41ba1f22004-09-14 12:46:35 +0000637 );
638
639 /*
640 * (S * N * 2^F) * B^E
641 * Essentially:
Lev Walkinab1d1e12017-10-03 18:43:12 -0700642 m = ldexp(m, scaleF) * pow(pow(2, baseF), expval);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000643 */
644 m = ldexp(m, expval * baseF + scaleF);
Lev Walkineff98a52017-09-13 22:24:35 +0000645 if(asn_isfinite(m)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000646 *dbl_value = sign ? -m : m;
647 } else {
648 errno = ERANGE;
649 return -1;
650 }
651
652 } /* if(binary_format) */
653
654 return 0;
655}
656
657/*
658 * Assume IEEE 754 floating point: standard 64 bit double.
659 * [1 bit sign] [11 bits exponent] [52 bits mantissa]
660 */
661int
Lev Walkin5e033762004-09-29 13:26:15 +0000662asn_double2REAL(REAL_t *st, double dbl_value) {
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700663 double test = -0.0;
664 int float_big_endian = *(const char *)&test != 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000665 uint8_t buf[16]; /* More than enough for 8-byte dbl_value */
666 uint8_t dscr[sizeof(dbl_value)]; /* double value scratch pad */
667 /* Assertion guards: won't even compile, if unexpected double size */
Lev Walkin3f995632017-09-26 18:27:32 -0700668 char assertion_buffer1[9 - sizeof(dbl_value)] CC_NOTUSED;
669 char assertion_buffer2[sizeof(dbl_value) - 7] CC_NOTUSED;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000670 uint8_t *ptr = buf;
671 uint8_t *mstop; /* Last byte of mantissa */
672 unsigned int mval; /* Value of the last byte of mantissa */
673 unsigned int bmsign; /* binary mask with sign */
674 unsigned int buflen;
675 unsigned int accum;
676 int expval;
677
678 if(!st) {
679 errno = EINVAL;
680 return -1;
681 }
682
Lev Walkin057fb732004-09-14 13:58:10 +0000683 /*
684 * ilogb(+-0) returns -INT_MAX or INT_MIN (platform-dependent)
Lev Walkin2f505022005-07-01 08:28:18 +0000685 * ilogb(+-inf) returns INT_MAX, logb(+-inf) returns +inf
Lev Walkin2a789d92004-09-27 21:36:59 +0000686 * ilogb(NaN) returns INT_MIN or INT_MAX (platform-dependent)
Lev Walkin057fb732004-09-14 13:58:10 +0000687 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000688 expval = ilogb(dbl_value);
Lev Walkin2a789d92004-09-27 21:36:59 +0000689 if(expval <= -INT_MAX /* Also catches +-0 and maybe isnan() */
690 || expval == INT_MAX /* catches isfin() and maybe isnan() */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000691 ) {
692 if(!st->buf || st->size < 2) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000693 ptr = (uint8_t *)MALLOC(2);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000694 if(!ptr) return -1;
Lev Walkindcae9ce2017-09-18 20:13:36 -0700695 if(st->buf) FREEMEM(st->buf);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000696 st->buf = ptr;
697 }
698 /* fpclassify(3) is not portable yet */
Lev Walkineff98a52017-09-13 22:24:35 +0000699 if(asn_isnan(dbl_value)) {
Lev Walkin2a789d92004-09-27 21:36:59 +0000700 st->buf[0] = 0x42; /* NaN */
701 st->buf[1] = 0;
702 st->size = 1;
Lev Walkineff98a52017-09-13 22:24:35 +0000703 } else if(!asn_isfinite(dbl_value)) {
Lev Walkin7e03db92004-09-14 13:50:21 +0000704 if(copysign(1.0, dbl_value) < 0.0) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000705 st->buf[0] = 0x41; /* MINUS-INFINITY */
706 } else {
707 st->buf[0] = 0x40; /* PLUS-INFINITY */
708 }
709 st->buf[1] = 0;
710 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000711 } else {
Lev Walkin5fe72e92012-01-07 17:00:29 -0800712 if(copysign(1.0, dbl_value) >= 0.0) {
Lev Walkinc51e7d62004-09-27 22:16:18 +0000713 /* no content octets: positive zero */
714 st->buf[0] = 0; /* JIC */
715 st->size = 0;
Lev Walkin5fe72e92012-01-07 17:00:29 -0800716 } else {
717 /* Negative zero. #8.5.3, 8.5.9 */
718 st->buf[0] = 0x43;
Lev Walkindcae9ce2017-09-18 20:13:36 -0700719 st->buf[1] = 0;
Lev Walkin5fe72e92012-01-07 17:00:29 -0800720 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000721 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000722 }
723 return 0;
724 }
725
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700726 if(float_big_endian) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000727 uint8_t *s = ((uint8_t *)&dbl_value) + 1;
728 uint8_t *end = ((uint8_t *)&dbl_value) + sizeof(double);
729 uint8_t *d;
730
731 bmsign = 0x80 | ((s[-1] >> 1) & 0x40); /* binary mask & - */
732 for(mstop = d = dscr; s < end; d++, s++) {
733 *d = *s;
734 if(*d) mstop = d;
735 }
Lev Walkin6cbed3d2017-10-07 16:42:41 -0700736 } else {
737 uint8_t *s = ((uint8_t *)&dbl_value) + sizeof(dbl_value) - 2;
738 uint8_t *start = ((uint8_t *)&dbl_value);
739 uint8_t *d;
740
741 bmsign = 0x80 | ((s[1] >> 1) & 0x40); /* binary mask & - */
742 for(mstop = d = dscr; s >= start; d++, s--) {
743 *d = *s;
744 if(*d) mstop = d;
745 }
746 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000747
748 /* Remove parts of the exponent, leave mantissa and explicit 1. */
749 dscr[0] = 0x10 | (dscr[0] & 0x0f);
750
751 /* Adjust exponent in a very unobvious way */
752 expval -= 8 * ((mstop - dscr) + 1) - 4;
753
754 /* This loop ensures DER conformance by forcing mantissa odd: 11.3.1 */
755 mval = *mstop;
756 if(mval && !(mval & 1)) {
Lev Walkin9c57f332017-09-17 22:30:49 -0700757 int shift_count = 1;
758 int ishift;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000759 uint8_t *mptr;
760
761 /*
762 * Figure out what needs to be done to make mantissa odd.
763 */
764 if(!(mval & 0x0f)) /* Speed-up a little */
765 shift_count = 4;
766 while(((mval >> shift_count) & 1) == 0)
767 shift_count++;
768
769 ishift = 8 - shift_count;
770 accum = 0;
771
772 /* Go over the buffer, shifting it shift_count bits right. */
773 for(mptr = dscr; mptr <= mstop; mptr++) {
774 mval = *mptr;
775 *mptr = accum | (mval >> shift_count);
776 accum = mval << ishift;
777 }
778
Lev Walkinb89b3402012-01-09 18:26:30 -0800779 /* Adjust exponent appropriately. */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000780 expval += shift_count;
781 }
782
783 if(expval < 0) {
784 if((expval >> 7) == -1) {
785 *ptr++ = bmsign | 0x00;
786 *ptr++ = expval;
787 } else if((expval >> 15) == -1) {
788 *ptr++ = bmsign | 0x01;
789 *ptr++ = expval >> 8;
790 *ptr++ = expval;
791 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000792 *ptr++ = bmsign | 0x02;
793 *ptr++ = expval >> 16;
794 *ptr++ = expval >> 8;
795 *ptr++ = expval;
796 }
797 } else if(expval <= 0x7f) {
798 *ptr++ = bmsign | 0x00;
799 *ptr++ = expval;
800 } else if(expval <= 0x7fff) {
801 *ptr++ = bmsign | 0x01;
802 *ptr++ = expval >> 8;
803 *ptr++ = expval;
804 } else {
805 assert(expval <= 0x7fffff);
806 *ptr++ = bmsign | 0x02;
807 *ptr++ = expval >> 16;
808 *ptr++ = expval >> 8;
809 *ptr++ = expval;
810 }
811
812 buflen = (mstop - dscr) + 1;
813 memcpy(ptr, dscr, buflen);
814 ptr += buflen;
815 buflen = ptr - buf;
816
Lev Walkinc17d90f2005-01-17 14:32:45 +0000817 ptr = (uint8_t *)MALLOC(buflen + 1);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000818 if(!ptr) return -1;
819
820 memcpy(ptr, buf, buflen);
821 buf[buflen] = 0; /* JIC */
822
823 if(st->buf) FREEMEM(st->buf);
824 st->buf = ptr;
825 st->size = buflen;
826
827 return 0;
828}
Lev Walkin41d972b2017-08-23 23:30:59 -0700829
Lev Walkin68079d32017-10-08 15:41:20 -0700830int CC_ATTR_NO_SANITIZE("float-cast-overflow")
831asn_double2float(double d, float *outcome) {
832 float f = d;
833
834 *outcome = f;
835
836 if(asn_isfinite(d) == asn_isfinite(f)) {
837 return 0;
838 } else {
839 return -1;
840 }
841}
842
Lev Walkin43292722017-10-05 00:33:32 -0700843#ifndef ASN_DISABLE_OER_SUPPORT
844
845/*
846 * Encode as Canonical OER
847 */
848asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700849REAL_encode_oer(const asn_TYPE_descriptor_t *td,
850 const asn_oer_constraints_t *constraints, const void *sptr,
Lev Walkin43292722017-10-05 00:33:32 -0700851 asn_app_consume_bytes_f *cb, void *app_key) {
852 const REAL_t *st = sptr;
853 asn_enc_rval_t er;
854 ssize_t len_len;
855
856 if(!st || !st->buf || !td)
857 ASN__ENCODE_FAILED;
858
859 if(!constraints) constraints = td->encoding_constraints.oer_constraints;
860 if(constraints && constraints->value.width != 0) {
861 /* If we're constrained to a narrow float/double representation, we
862 * shouldn't have ended up using REAL. Expecting NativeReal. */
863 ASN__ENCODE_FAILED;
864 }
865
866 /* Encode a fake REAL */
867 len_len = oer_serialize_length(st->size, cb, app_key);
868 if(len_len < 0 || cb(st->buf, st->size, app_key) < 0) {
869 ASN__ENCODE_FAILED;
870 } else {
871 er.encoded = len_len + st->size;
872 ASN__ENCODED_OK(er);
873 }
874}
875
876asn_dec_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700877REAL_decode_oer(const asn_codec_ctx_t *opt_codec_ctx,
878 const asn_TYPE_descriptor_t *td,
Lev Walkin43292722017-10-05 00:33:32 -0700879 const asn_oer_constraints_t *constraints, void **sptr,
880 const void *ptr, size_t size) {
881 asn_dec_rval_t ok = {RC_OK, 0};
882 REAL_t *st;
883 uint8_t *buf;
884 ssize_t len_len;
885 size_t real_body_len;
886
Lev Walkin9cbf6782017-10-05 16:31:53 -0700887 (void)opt_codec_ctx;
888
Lev Walkin43292722017-10-05 00:33:32 -0700889 if(!constraints) constraints = td->encoding_constraints.oer_constraints;
890 if(constraints && constraints->value.width != 0) {
891 /* If we're constrained to a narrow float/double representation, we
892 * shouldn't have ended up using REAL. Expecting NativeReal. */
893 ASN__DECODE_FAILED;
894 }
895
896 len_len = oer_fetch_length(ptr, size, &real_body_len);
897 if(len_len < 0) ASN__DECODE_FAILED;
898 if(len_len == 0) ASN__DECODE_STARVED;
899
900 ptr = (const char *)ptr + len_len;
901 size -= len_len;
902
903 if(real_body_len > size) ASN__DECODE_STARVED;
904
905 buf = CALLOC(1, real_body_len + 1);
906 if(!buf) ASN__DECODE_FAILED;
907
908 if(!(st = *sptr)) {
909 st = (*sptr = CALLOC(1, sizeof(REAL_t)));
910 if(!st) {
911 FREEMEM(buf);
912 ASN__DECODE_FAILED;
913 }
914 } else {
915 FREEMEM(st->buf);
916 }
917
918 memcpy(buf, ptr, real_body_len);
919 buf[real_body_len] = '\0';
920
921 st->buf = buf;
922 st->size = real_body_len;
923
924 ok.consumed = len_len + real_body_len;
925 return ok;
926}
927
928#endif /* ASN_DISABLE_OER_SUPPORT */
929
Lev Walkin41d972b2017-08-23 23:30:59 -0700930#ifndef ASN_DISABLE_PER_SUPPORT
931
932asn_dec_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700933REAL_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
934 const asn_TYPE_descriptor_t *td,
Lev Walkin41d972b2017-08-23 23:30:59 -0700935 const asn_per_constraints_t *constraints, void **sptr,
936 asn_per_data_t *pd) {
937 (void)constraints; /* No PER visible constraints */
938 return OCTET_STRING_decode_uper(opt_codec_ctx, td, 0, sptr, pd);
939}
940
941asn_enc_rval_t
Lev Walkin20696a42017-10-17 21:27:33 -0700942REAL_encode_uper(const asn_TYPE_descriptor_t *td,
943 const asn_per_constraints_t *constraints, const void *sptr,
Lev Walkin41d972b2017-08-23 23:30:59 -0700944 asn_per_outp_t *po) {
945 (void)constraints; /* No PER visible constraints */
946 return OCTET_STRING_encode_uper(td, 0, sptr, po);
947}
948
949#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkina5972be2017-09-29 23:15:58 -0700950
Lev Walkina5972be2017-09-29 23:15:58 -0700951asn_random_fill_result_t
952REAL_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
953 const asn_encoding_constraints_t *constraints,
954 size_t max_length) {
955 asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
956 asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
957 asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
Lev Walkinab1d1e12017-10-03 18:43:12 -0700958 static const double values[] = {
959 0, -0.0, -1, 1, -M_E, M_E, -3.14, 3.14, -M_PI, M_PI, -255, 255,
960 /* 2^51 */
961 -2251799813685248.0, 2251799813685248.0,
962 /* 2^52 */
963 -4503599627370496.0, 4503599627370496.0,
964 /* 2^100 */
965 -1267650600228229401496703205376.0, 1267650600228229401496703205376.0,
Lev Walkinad4c63d2017-10-05 18:07:15 +0000966 -FLT_MIN, FLT_MIN,
Jon Ringle35c3f0d2017-10-04 21:54:39 -0400967 -FLT_MAX, FLT_MAX,
Lev Walkinad4c63d2017-10-05 18:07:15 +0000968 -DBL_MIN, DBL_MIN,
969 -DBL_MAX, DBL_MAX,
970#ifdef FLT_TRUE_MIN
971 -FLT_TRUE_MIN, FLT_TRUE_MIN,
972#endif
973#ifdef DBL_TRUE_MIN
Lev Walkin43292722017-10-05 00:33:32 -0700974 -DBL_TRUE_MIN, DBL_TRUE_MIN,
Lev Walkin4ca41492017-10-03 20:29:54 -0700975#endif
976 INFINITY, -INFINITY, NAN};
Lev Walkina5972be2017-09-29 23:15:58 -0700977 REAL_t *st;
978 double d;
979
980 (void)constraints;
981
982 if(max_length == 0) return result_skipped;
983
984 d = values[asn_random_between(0, sizeof(values) / sizeof(values[0]) - 1)];
985
986 if(*sptr) {
987 st = *sptr;
988 } else {
989 st = (REAL_t*)(*sptr = CALLOC(1, sizeof(REAL_t)));
990 if(!st) {
991 return result_failed;
992 }
993 }
994
995 if(asn_double2REAL(st, d)) {
996 if(st == *sptr) {
997 ASN_STRUCT_RESET(*td, st);
998 } else {
999 ASN_STRUCT_FREE(*td, st);
1000 }
1001 return result_failed;
1002 }
1003
1004 result_ok.length = st->size;
1005 return result_ok;
1006}
Lev Walkinab1d1e12017-10-03 18:43:12 -07001007