blob: cc7d64130b7740e753ce2ace90b2efa1bcca8ba3 [file] [log] [blame]
Lev Walkin41ba1f22004-09-14 12:46:35 +00001/*-
Lev Walkine9360e62013-03-28 00:21:57 -07002 * Copyright (c) 2004-2013 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>
15#include <errno.h>
Lev Walkin33700162004-10-26 09:03:31 +000016#include <REAL.h>
Lev Walkin725883b2006-10-09 12:07:58 +000017#include <OCTET_STRING.h>
Lev Walkin41ba1f22004-09-14 12:46:35 +000018
19#undef INT_MAX
20#define INT_MAX ((int)(((unsigned int)-1) >> 1))
21
Lev Walkin1aea6982004-10-26 09:35:25 +000022#if !(defined(NAN) || defined(INFINITY))
Lev Walkin3f995632017-09-26 18:27:32 -070023static volatile double real_zero CC_NOTUSED = 0.0;
Lev Walkin1aea6982004-10-26 09:35:25 +000024#endif
Lev Walkinae1bce92004-09-27 22:18:34 +000025#ifndef NAN
Lev Walkinc51e7d62004-09-27 22:16:18 +000026#define NAN (real_zero/real_zero)
Lev Walkin40319a12004-09-14 13:40:42 +000027#endif
Lev Walkin1aea6982004-10-26 09:35:25 +000028#ifndef INFINITY
29#define INFINITY (1.0/real_zero)
30#endif
Lev Walkin40319a12004-09-14 13:40:42 +000031
Lev Walkineff98a52017-09-13 22:24:35 +000032#if defined(__clang__)
33/*
34 * isnan() is defined using generic selections and won't compile in
35 * strict C89 mode because of too fancy system's standard library.
36 * However, prior to C11 the math had a perfectly working isnan()
37 * in the math library.
38 * Disable generic selection warning so we can test C89 mode with newer libc.
39 */
40#pragma clang diagnostic push
41#pragma clang diagnostic ignored "-Wc11-extensions"
42static int asn_isnan(double d) {
43 return isnan(d);
44}
45static int asn_isfinite(double d) {
Lev Walkindaeb2162014-01-13 23:08:35 -080046#ifdef isfinite
Lev Walkineff98a52017-09-13 22:24:35 +000047 return isfinite(d); /* ISO C99 */
Lev Walkindaeb2162014-01-13 23:08:35 -080048#else
Lev Walkineff98a52017-09-13 22:24:35 +000049 return finite(d); /* Deprecated on Mac OS X 10.9 */
Lev Walkindaeb2162014-01-13 23:08:35 -080050#endif
Lev Walkineff98a52017-09-13 22:24:35 +000051}
52#pragma clang diagnostic pop
53#else /* !clang */
54#define asn_isnan(v) isnan(v)
55#ifdef isfinite
56#define asn_isfinite(d) isfinite(d) /* ISO C99 */
57#else
58#define asn_isfinite(d) finite(d) /* Deprecated on Mac OS X 10.9 */
59#endif
60#endif /* clang */
61
Lev Walkindaeb2162014-01-13 23:08:35 -080062
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
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080081 0,
82 0,
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 */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080091 0 /* Use generic outmost tag fetcher */
92};
93asn_TYPE_descriptor_t asn_DEF_REAL = {
94 "REAL",
95 "REAL",
96 &asn_OP_REAL,
97 asn_generic_no_constraint,
98 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]),
102 0, /* No OER visible constraints */
103 0, /* No PER visible constraints */
104 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 Walkin92b35d22004-09-27 20:52:18 +0000131 const char *fmt = canonical?"%.15E":"%.15f";
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 Walkin5fda7d52012-01-09 03:20:19 -0800526
Lev Walkin53e5ae62017-08-23 10:56:19 -0700527 /* 1. By contract, an input buffer should be '\0'-terminated.
Lev Walkin5fda7d52012-01-09 03:20:19 -0800528 * OCTET STRING decoder ensures that, as is asn_double2REAL().
529 * 2. ISO 6093 specifies COMMA as a possible decimal separator.
530 * However, strtod() can't always deal with COMMA.
531 * So her we fix both by reallocating, copying and fixing.
532 */
Lev Walkin53e5ae62017-08-23 10:56:19 -0700533 if(st->buf[st->size] != '\0' || memchr(st->buf, ',', st->size)) {
Lev Walkin5fda7d52012-01-09 03:20:19 -0800534 uint8_t *p, *end;
535 char *b;
536 if(st->size > 100) {
537 /* Avoid malicious stack overflow in alloca() */
538 buf = (char *)MALLOC(st->size);
539 if(!buf) return -1;
540 used_malloc = 1;
541 } else {
542 buf = alloca(st->size);
543 }
544 b = buf;
545 /* Copy without the first byte and with 0-termination */
546 for(p = st->buf + 1, end = st->buf + st->size;
547 p < end; b++, p++)
548 *b = (*p == ',') ? '.' : *p;
549 *b = '\0';
550 } else {
551 buf = (char *)&st->buf[1];
552 }
553
554 endptr = buf;
555 d = strtod(buf, &endptr);
556 if(*endptr != '\0') {
557 /* Format is not consistent with ISO 6093 */
558 if(used_malloc) FREEMEM(buf);
Lev Walkin0959ffb2011-06-25 15:48:52 -0700559 errno = EINVAL;
560 return -1;
561 }
Lev Walkin5fda7d52012-01-09 03:20:19 -0800562 if(used_malloc) FREEMEM(buf);
Lev Walkineff98a52017-09-13 22:24:35 +0000563 if(asn_isfinite(d)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000564 *dbl_value = d;
565 return 0;
566 } else {
567 errno = ERANGE;
Lev Walkin0959ffb2011-06-25 15:48:52 -0700568 return -1;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000569 }
570 }
571 }
572
573 /*
574 * Binary representation.
575 */
576 {
577 double m;
578 int expval; /* exponent value */
579 unsigned int elen; /* exponent value length, in octets */
Lev Walkin9c57f332017-09-17 22:30:49 -0700580 int scaleF;
581 int baseF;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000582 uint8_t *ptr;
583 uint8_t *end;
584 int sign;
585
586 switch((octv & 0x30) >> 4) {
587 case 0x00: baseF = 1; break; /* base 2 */
588 case 0x01: baseF = 3; break; /* base 8 */
589 case 0x02: baseF = 4; break; /* base 16 */
590 default:
591 /* Reserved field, can't parse now. */
592 errno = EINVAL;
593 return -1;
594 }
595
596 sign = (octv & 0x40); /* bit 7 */
597 scaleF = (octv & 0x0C) >> 2; /* bits 4 to 3 */
598
Lev Walkin494fb702017-08-07 20:07:00 -0700599 if(st->size <= 1 + (octv & 0x03)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000600 errno = EINVAL;
601 return -1;
602 }
603
Lev Walkine78753d2007-06-23 17:02:00 +0000604 elen = (octv & 0x03); /* bits 2 to 1; 8.5.6.4 */
605 if(elen == 0x03) { /* bits 2 to 1 = 11; 8.5.6.4, case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000606 elen = st->buf[1]; /* unsigned binary number */
Lev Walkin494fb702017-08-07 20:07:00 -0700607 if(elen == 0 || st->size <= (2 + elen)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000608 errno = EINVAL;
609 return -1;
610 }
Lev Walkine78753d2007-06-23 17:02:00 +0000611 /* FIXME: verify constraints of case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000612 ptr = &st->buf[2];
613 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000614 ptr = &st->buf[1];
615 }
616
617 /* Fetch the multibyte exponent */
618 expval = (int)(*(int8_t *)ptr);
619 end = ptr + elen + 1;
620 for(ptr++; ptr < end; ptr++)
621 expval = (expval * 256) + *ptr;
622
623 m = 0.0; /* Initial mantissa value */
624
625 /* Okay, the exponent is here. Now, what about mantissa? */
626 end = st->buf + st->size;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800627 for(; ptr < end; ptr++)
628 m = ldexp(m, 8) + *ptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000629
Lev Walkin5f560912004-10-21 13:37:57 +0000630 if(0)
Lev Walkin5fda7d52012-01-09 03:20:19 -0800631 ASN_DEBUG("m=%.10f, scF=%d, bF=%d, expval=%d, ldexp()=%f, ldexp()=%f\n",
Lev Walkin41ba1f22004-09-14 12:46:35 +0000632 m, scaleF, baseF, expval,
633 ldexp(m, expval * baseF + scaleF),
Lev Walkin77aa90f2005-04-28 22:56:36 +0000634 ldexp(m, scaleF) * pow(pow(2, baseF), expval)
Lev Walkin41ba1f22004-09-14 12:46:35 +0000635 );
636
637 /*
638 * (S * N * 2^F) * B^E
639 * Essentially:
Lev Walkin77aa90f2005-04-28 22:56:36 +0000640 m = ldexp(m, scaleF) * pow(pow(2, base), expval);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000641 */
642 m = ldexp(m, expval * baseF + scaleF);
Lev Walkineff98a52017-09-13 22:24:35 +0000643 if(asn_isfinite(m)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000644 *dbl_value = sign ? -m : m;
645 } else {
646 errno = ERANGE;
647 return -1;
648 }
649
650 } /* if(binary_format) */
651
652 return 0;
653}
654
655/*
656 * Assume IEEE 754 floating point: standard 64 bit double.
657 * [1 bit sign] [11 bits exponent] [52 bits mantissa]
658 */
659int
Lev Walkin5e033762004-09-29 13:26:15 +0000660asn_double2REAL(REAL_t *st, double dbl_value) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000661#ifdef WORDS_BIGENDIAN /* Known to be big-endian */
662 int littleEndian = 0;
663#else /* need to test: have no explicit information */
664 unsigned int LE = 1;
665 int littleEndian = *(unsigned char *)&LE;
666#endif
667 uint8_t buf[16]; /* More than enough for 8-byte dbl_value */
668 uint8_t dscr[sizeof(dbl_value)]; /* double value scratch pad */
669 /* Assertion guards: won't even compile, if unexpected double size */
Lev Walkin3f995632017-09-26 18:27:32 -0700670 char assertion_buffer1[9 - sizeof(dbl_value)] CC_NOTUSED;
671 char assertion_buffer2[sizeof(dbl_value) - 7] CC_NOTUSED;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000672 uint8_t *ptr = buf;
673 uint8_t *mstop; /* Last byte of mantissa */
674 unsigned int mval; /* Value of the last byte of mantissa */
675 unsigned int bmsign; /* binary mask with sign */
676 unsigned int buflen;
677 unsigned int accum;
678 int expval;
679
680 if(!st) {
681 errno = EINVAL;
682 return -1;
683 }
684
Lev Walkin057fb732004-09-14 13:58:10 +0000685 /*
686 * ilogb(+-0) returns -INT_MAX or INT_MIN (platform-dependent)
Lev Walkin2f505022005-07-01 08:28:18 +0000687 * ilogb(+-inf) returns INT_MAX, logb(+-inf) returns +inf
Lev Walkin2a789d92004-09-27 21:36:59 +0000688 * ilogb(NaN) returns INT_MIN or INT_MAX (platform-dependent)
Lev Walkin057fb732004-09-14 13:58:10 +0000689 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000690 expval = ilogb(dbl_value);
Lev Walkin2a789d92004-09-27 21:36:59 +0000691 if(expval <= -INT_MAX /* Also catches +-0 and maybe isnan() */
692 || expval == INT_MAX /* catches isfin() and maybe isnan() */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000693 ) {
694 if(!st->buf || st->size < 2) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000695 ptr = (uint8_t *)MALLOC(2);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000696 if(!ptr) return -1;
Lev Walkindcae9ce2017-09-18 20:13:36 -0700697 if(st->buf) FREEMEM(st->buf);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000698 st->buf = ptr;
699 }
700 /* fpclassify(3) is not portable yet */
Lev Walkineff98a52017-09-13 22:24:35 +0000701 if(asn_isnan(dbl_value)) {
Lev Walkin2a789d92004-09-27 21:36:59 +0000702 st->buf[0] = 0x42; /* NaN */
703 st->buf[1] = 0;
704 st->size = 1;
Lev Walkineff98a52017-09-13 22:24:35 +0000705 } else if(!asn_isfinite(dbl_value)) {
Lev Walkin7e03db92004-09-14 13:50:21 +0000706 if(copysign(1.0, dbl_value) < 0.0) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000707 st->buf[0] = 0x41; /* MINUS-INFINITY */
708 } else {
709 st->buf[0] = 0x40; /* PLUS-INFINITY */
710 }
711 st->buf[1] = 0;
712 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000713 } else {
Lev Walkin5fe72e92012-01-07 17:00:29 -0800714 if(copysign(1.0, dbl_value) >= 0.0) {
Lev Walkinc51e7d62004-09-27 22:16:18 +0000715 /* no content octets: positive zero */
716 st->buf[0] = 0; /* JIC */
717 st->size = 0;
Lev Walkin5fe72e92012-01-07 17:00:29 -0800718 } else {
719 /* Negative zero. #8.5.3, 8.5.9 */
720 st->buf[0] = 0x43;
Lev Walkindcae9ce2017-09-18 20:13:36 -0700721 st->buf[1] = 0;
Lev Walkin5fe72e92012-01-07 17:00:29 -0800722 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000723 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000724 }
725 return 0;
726 }
727
728 if(littleEndian) {
729 uint8_t *s = ((uint8_t *)&dbl_value) + sizeof(dbl_value) - 2;
Lev Walkin057fb732004-09-14 13:58:10 +0000730 uint8_t *start = ((uint8_t *)&dbl_value);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000731 uint8_t *d;
732
733 bmsign = 0x80 | ((s[1] >> 1) & 0x40); /* binary mask & - */
Lev Walkin057fb732004-09-14 13:58:10 +0000734 for(mstop = d = dscr; s >= start; d++, s--) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000735 *d = *s;
736 if(*d) mstop = d;
737 }
738 } else {
739 uint8_t *s = ((uint8_t *)&dbl_value) + 1;
740 uint8_t *end = ((uint8_t *)&dbl_value) + sizeof(double);
741 uint8_t *d;
742
743 bmsign = 0x80 | ((s[-1] >> 1) & 0x40); /* binary mask & - */
744 for(mstop = d = dscr; s < end; d++, s++) {
745 *d = *s;
746 if(*d) mstop = d;
747 }
748 }
749
750 /* Remove parts of the exponent, leave mantissa and explicit 1. */
751 dscr[0] = 0x10 | (dscr[0] & 0x0f);
752
753 /* Adjust exponent in a very unobvious way */
754 expval -= 8 * ((mstop - dscr) + 1) - 4;
755
756 /* This loop ensures DER conformance by forcing mantissa odd: 11.3.1 */
757 mval = *mstop;
758 if(mval && !(mval & 1)) {
Lev Walkin9c57f332017-09-17 22:30:49 -0700759 int shift_count = 1;
760 int ishift;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000761 uint8_t *mptr;
762
763 /*
764 * Figure out what needs to be done to make mantissa odd.
765 */
766 if(!(mval & 0x0f)) /* Speed-up a little */
767 shift_count = 4;
768 while(((mval >> shift_count) & 1) == 0)
769 shift_count++;
770
771 ishift = 8 - shift_count;
772 accum = 0;
773
774 /* Go over the buffer, shifting it shift_count bits right. */
775 for(mptr = dscr; mptr <= mstop; mptr++) {
776 mval = *mptr;
777 *mptr = accum | (mval >> shift_count);
778 accum = mval << ishift;
779 }
780
Lev Walkinb89b3402012-01-09 18:26:30 -0800781 /* Adjust exponent appropriately. */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000782 expval += shift_count;
783 }
784
785 if(expval < 0) {
786 if((expval >> 7) == -1) {
787 *ptr++ = bmsign | 0x00;
788 *ptr++ = expval;
789 } else if((expval >> 15) == -1) {
790 *ptr++ = bmsign | 0x01;
791 *ptr++ = expval >> 8;
792 *ptr++ = expval;
793 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000794 *ptr++ = bmsign | 0x02;
795 *ptr++ = expval >> 16;
796 *ptr++ = expval >> 8;
797 *ptr++ = expval;
798 }
799 } else if(expval <= 0x7f) {
800 *ptr++ = bmsign | 0x00;
801 *ptr++ = expval;
802 } else if(expval <= 0x7fff) {
803 *ptr++ = bmsign | 0x01;
804 *ptr++ = expval >> 8;
805 *ptr++ = expval;
806 } else {
807 assert(expval <= 0x7fffff);
808 *ptr++ = bmsign | 0x02;
809 *ptr++ = expval >> 16;
810 *ptr++ = expval >> 8;
811 *ptr++ = expval;
812 }
813
814 buflen = (mstop - dscr) + 1;
815 memcpy(ptr, dscr, buflen);
816 ptr += buflen;
817 buflen = ptr - buf;
818
Lev Walkinc17d90f2005-01-17 14:32:45 +0000819 ptr = (uint8_t *)MALLOC(buflen + 1);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000820 if(!ptr) return -1;
821
822 memcpy(ptr, buf, buflen);
823 buf[buflen] = 0; /* JIC */
824
825 if(st->buf) FREEMEM(st->buf);
826 st->buf = ptr;
827 st->size = buflen;
828
829 return 0;
830}
Lev Walkin41d972b2017-08-23 23:30:59 -0700831
832#ifndef ASN_DISABLE_PER_SUPPORT
833
834asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700835REAL_decode_uper(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin41d972b2017-08-23 23:30:59 -0700836 const asn_per_constraints_t *constraints, void **sptr,
837 asn_per_data_t *pd) {
838 (void)constraints; /* No PER visible constraints */
839 return OCTET_STRING_decode_uper(opt_codec_ctx, td, 0, sptr, pd);
840}
841
842asn_enc_rval_t
843REAL_encode_uper(asn_TYPE_descriptor_t *td,
844 const asn_per_constraints_t *constraints, void *sptr,
845 asn_per_outp_t *po) {
846 (void)constraints; /* No PER visible constraints */
847 return OCTET_STRING_encode_uper(td, 0, sptr, po);
848}
849
850#endif /* ASN_DISABLE_PER_SUPPORT */