blob: 3767b970e67877e48ccae8dfa7aeb1a50d3fc330 [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 Walkin50b0f6c2006-10-16 12:01:36 +000023static volatile double real_zero GCC_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 Walkindaeb2162014-01-13 23:08:35 -080032#ifdef isfinite
33#define _asn_isfinite(d) isfinite(d) /* ISO C99 */
34#else
35#define _asn_isfinite(d) finite(d) /* Deprecated on Mac OS X 10.9 */
36#endif
37
Lev Walkin41ba1f22004-09-14 12:46:35 +000038/*
39 * REAL basic type description.
40 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070041static const ber_tlv_tag_t asn_DEF_REAL_tags[] = {
Lev Walkin41ba1f22004-09-14 12:46:35 +000042 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
43};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080044asn_TYPE_operation_t asn_OP_REAL = {
45 ASN__PRIMITIVE_TYPE_free,
46 REAL_print,
47 REAL_compare,
48 asn_generic_no_constraint,
49 ber_decode_primitive,
50 der_encode_primitive,
51 REAL_decode_xer,
52 REAL_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070053#ifdef ASN_DISABLE_OER_SUPPORT
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080054 0,
55 0,
Lev Walkincc159472017-07-06 08:26:36 -070056#else
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080057 0,
58 0,
Lev Walkincc159472017-07-06 08:26:36 -070059#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040060#ifdef ASN_DISABLE_PER_SUPPORT
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080061 0,
62 0,
Lev Walkinb33425f2017-07-14 14:59:52 +040063#else
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080064 REAL_decode_uper,
65 REAL_encode_uper,
Lev Walkinb33425f2017-07-14 14:59:52 +040066#endif /* ASN_DISABLE_PER_SUPPORT */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080067 0 /* Use generic outmost tag fetcher */
68};
69asn_TYPE_descriptor_t asn_DEF_REAL = {
70 "REAL",
71 "REAL",
72 &asn_OP_REAL,
73 asn_generic_no_constraint,
74 asn_DEF_REAL_tags,
75 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
76 asn_DEF_REAL_tags, /* Same as above */
77 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
78 0, /* No OER visible constraints */
79 0, /* No PER visible constraints */
80 0,
81 0, /* No members */
82 0 /* No specifics */
Lev Walkin41ba1f22004-09-14 12:46:35 +000083};
84
Lev Walkin5f560912004-10-21 13:37:57 +000085typedef enum specialRealValue {
86 SRV__NOT_A_NUMBER,
87 SRV__MINUS_INFINITY,
88 SRV__PLUS_INFINITY
89} specialRealValue_e;
90static struct specialRealValue_s {
91 char *string;
Lev Walkin8471cec2004-10-21 14:02:19 +000092 size_t length;
Lev Walkin1aea6982004-10-26 09:35:25 +000093 long dv;
Lev Walkin5f560912004-10-21 13:37:57 +000094} specialRealValue[] = {
95#define SRV_SET(foo, val) { foo, sizeof(foo) - 1, val }
Lev Walkin1aea6982004-10-26 09:35:25 +000096 SRV_SET("<NOT-A-NUMBER/>", 0),
97 SRV_SET("<MINUS-INFINITY/>", -1),
98 SRV_SET("<PLUS-INFINITY/>", 1),
Lev Walkin5f560912004-10-21 13:37:57 +000099#undef SRV_SET
100};
101
Lev Walkina9cc46e2004-09-22 16:06:28 +0000102ssize_t
103REAL__dump(double d, int canonical, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin92b35d22004-09-27 20:52:18 +0000104 char local_buf[64];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000105 char *buf = local_buf;
106 ssize_t buflen = sizeof(local_buf);
Lev Walkin92b35d22004-09-27 20:52:18 +0000107 const char *fmt = canonical?"%.15E":"%.15f";
Lev Walkina9cc46e2004-09-22 16:06:28 +0000108 ssize_t ret;
109
Lev Walkin8e8078a2004-09-26 13:10:40 +0000110 /*
111 * Check whether it is a special value.
112 */
Lev Walkinc51e7d62004-09-27 22:16:18 +0000113 /* fpclassify(3) is not portable yet */
114 if(isnan(d)) {
Lev Walkin5f560912004-10-21 13:37:57 +0000115 buf = specialRealValue[SRV__NOT_A_NUMBER].string;
116 buflen = specialRealValue[SRV__NOT_A_NUMBER].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000117 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
Lev Walkindaeb2162014-01-13 23:08:35 -0800118 } else if(!_asn_isfinite(d)) {
Lev Walkinc51e7d62004-09-27 22:16:18 +0000119 if(copysign(1.0, d) < 0.0) {
Lev Walkin5f560912004-10-21 13:37:57 +0000120 buf = specialRealValue[SRV__MINUS_INFINITY].string;
121 buflen = specialRealValue[SRV__MINUS_INFINITY].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000122 } else {
Lev Walkin5f560912004-10-21 13:37:57 +0000123 buf = specialRealValue[SRV__PLUS_INFINITY].string;
124 buflen = specialRealValue[SRV__PLUS_INFINITY].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000125 }
126 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
127 } else if(ilogb(d) <= -INT_MAX) {
128 if(copysign(1.0, d) < 0.0) {
129 buf = "-0";
130 buflen = 2;
131 } else {
132 buf = "0";
133 buflen = 1;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000134 }
135 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
136 }
137
138 /*
139 * Use the libc's double printing, hopefully they got it right.
140 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000141 do {
142 ret = snprintf(buf, buflen, fmt, d);
143 if(ret < 0) {
144 buflen <<= 1;
145 } else if(ret >= buflen) {
146 buflen = ret + 1;
147 } else {
148 buflen = ret;
149 break;
150 }
Lev Walkin419f6752006-09-13 04:02:00 +0000151 if(buf != local_buf) FREEMEM(buf);
Lev Walkin8e8078a2004-09-26 13:10:40 +0000152 buf = (char *)MALLOC(buflen);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000153 if(!buf) return -1;
154 } while(1);
155
Lev Walkina9cc46e2004-09-22 16:06:28 +0000156 if(canonical) {
Lev Walkin92b35d22004-09-27 20:52:18 +0000157 /*
158 * Transform the "[-]d.dddE+-dd" output into "[-]d.dddE[-]d"
Lev Walkin5f560912004-10-21 13:37:57 +0000159 * Check that snprintf() constructed the output correctly.
Lev Walkin92b35d22004-09-27 20:52:18 +0000160 */
Lev Walkin8ca13c82016-01-24 22:40:00 -0800161 char *dot;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000162 char *end = buf + buflen;
Lev Walkin92b35d22004-09-27 20:52:18 +0000163 char *last_zero;
Lev Walkin8ca13c82016-01-24 22:40:00 -0800164 char *prev_zero;
165 char *s;
166
167 enum {
168 LZSTATE_NOTHING,
169 LZSTATE_SEEN_ZERO
170 } lz_state = LZSTATE_NOTHING;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000171
Lev Walkin5f560912004-10-21 13:37:57 +0000172 dot = (buf[0] == 0x2d /* '-' */) ? (buf + 2) : (buf + 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000173 if(*dot >= 0x30) {
Lev Walkin78d917d2012-01-07 15:29:08 -0800174 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000175 errno = EINVAL;
176 return -1; /* Not a dot, really */
177 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000178 *dot = 0x2e; /* Replace possible comma */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000179
Lev Walkin8ca13c82016-01-24 22:40:00 -0800180 for(prev_zero = last_zero = s = dot + 2; s < end; s++) {
181 switch(*s) {
182 case 0x45: /* 'E' */
183 if(lz_state == LZSTATE_SEEN_ZERO)
184 last_zero = prev_zero;
185 break;
186 case 0x30: /* '0' */
187 if(lz_state == LZSTATE_NOTHING)
188 prev_zero = s;
189 lz_state = LZSTATE_SEEN_ZERO;
190 continue;
191 default:
192 lz_state = LZSTATE_NOTHING;
193 continue;
194 }
195 break;
196 }
197
198 if(s == end) {
Lev Walkin78d917d2012-01-07 15:29:08 -0800199 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000200 errno = EINVAL;
201 return -1; /* No promised E */
202 }
Lev Walkin8ca13c82016-01-24 22:40:00 -0800203
204 assert(*s == 0x45);
205 {
206 char *E = s;
207 char *expptr = ++E;
208 char *s = expptr;
209 int sign;
210
211 if(*expptr == 0x2b /* '+' */) {
212 /* Skip the "+" */
213 buflen -= 1;
214 sign = 0;
215 } else {
216 sign = 1;
217 s++;
218 }
219 expptr++;
220 if(expptr > end) {
221 if(buf != local_buf) FREEMEM(buf);
222 errno = EINVAL;
223 return -1;
224 }
225 if(*expptr == 0x30) {
226 buflen--;
227 expptr++;
228 }
229 if(*last_zero == 0x30) {
230 *last_zero = 0x45; /* E */
231 buflen -= s - (last_zero + 1);
232 s = last_zero + 1;
233 if(sign) {
234 *s++ = 0x2d /* '-' */;
235 buflen++;
236 }
237 }
238 for(; expptr <= end; s++, expptr++)
239 *s = *expptr;
240 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000241 } else {
242 /*
243 * Remove trailing zeros.
244 */
245 char *end = buf + buflen;
246 char *last_zero = end;
Lev Walkinf0b808d2005-04-25 21:08:25 +0000247 int stoplooking = 0;
Lev Walkin92b35d22004-09-27 20:52:18 +0000248 char *z;
249 for(z = end - 1; z > buf; z--) {
250 switch(*z) {
Lev Walkinf0b808d2005-04-25 21:08:25 +0000251 case 0x30:
252 if(!stoplooking)
253 last_zero = z;
254 continue;
Lev Walkin92b35d22004-09-27 20:52:18 +0000255 case 0x31: case 0x32: case 0x33: case 0x34:
256 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinf0b808d2005-04-25 21:08:25 +0000257 stoplooking = 1;
Lev Walkin92b35d22004-09-27 20:52:18 +0000258 continue;
259 default: /* Catch dot and other separators */
Lev Walkinf0b808d2005-04-25 21:08:25 +0000260 /*
261 * Replace possible comma (which may even
262 * be not a comma at all: locale-defined).
263 */
264 *z = 0x2e;
Lev Walkin92b35d22004-09-27 20:52:18 +0000265 if(last_zero == z + 1) { /* leave x.0 */
266 last_zero++;
267 }
268 buflen = last_zero - buf;
269 *last_zero = '\0';
270 break;
271 }
272 break;
273 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000274 }
275
276 ret = cb(buf, buflen, app_key);
Lev Walkin419f6752006-09-13 04:02:00 +0000277 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000278 return (ret < 0) ? -1 : buflen;
279}
280
Lev Walkin41ba1f22004-09-14 12:46:35 +0000281int
Lev Walkin5e033762004-09-29 13:26:15 +0000282REAL_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000283 asn_app_consume_bytes_f *cb, void *app_key) {
284 const REAL_t *st = (const REAL_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000285 ssize_t ret;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000286 double d;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000287
288 (void)td; /* Unused argument */
289 (void)ilevel; /* Unused argument */
290
Lev Walkina9cc46e2004-09-22 16:06:28 +0000291 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000292 ret = cb("<absent>", 8, app_key);
Lev Walkin5e033762004-09-29 13:26:15 +0000293 else if(asn_REAL2double(st, &d))
Lev Walkin8e8078a2004-09-26 13:10:40 +0000294 ret = cb("<error>", 7, app_key);
295 else
296 ret = REAL__dump(d, 0, cb, app_key);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000297
Lev Walkin8e8078a2004-09-26 13:10:40 +0000298 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000299}
Lev Walkin41ba1f22004-09-14 12:46:35 +0000300
Lev Walkincd2f48e2017-08-10 02:14:59 -0700301int
302REAL_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
303 const void *bptr) {
304 const REAL_t *a = aptr;
305 const REAL_t *b = bptr;
306
307 (void)td;
308
309 if(a && b) {
310 double adbl, bdbl;
311 int ra, rb;
312 ra = asn_REAL2double(a, &adbl);
313 rb = asn_REAL2double(b, &bdbl);
314 if(ra == 0 && rb == 0) {
315 if(isnan(adbl)) {
316 if(isnan(bdbl)) {
317 return 0;
318 } else {
319 return -1;
320 }
321 } else if(isnan(bdbl)) {
322 return 1;
323 }
324 /* Value comparison. */
325 if(adbl < bdbl) {
326 return -1;
327 } else if(adbl > bdbl) {
328 return 1;
329 } else {
330 return 0;
331 }
332 } else if(ra) {
333 return -1;
334 } else {
335 return 1;
336 }
337 } else if(!a) {
338 return -1;
339 } else {
340 return 1;
341 }
342}
343
Lev Walkina9cc46e2004-09-22 16:06:28 +0000344asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000345REAL_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000346 int ilevel, enum xer_encoder_flags_e flags,
347 asn_app_consume_bytes_f *cb, void *app_key) {
348 REAL_t *st = (REAL_t *)sptr;
349 asn_enc_rval_t er;
350 double d;
351
352 (void)ilevel;
353
Lev Walkin5e033762004-09-29 13:26:15 +0000354 if(!st || !st->buf || asn_REAL2double(st, &d))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700355 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000356
357 er.encoded = REAL__dump(d, flags & XER_F_CANONICAL, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700358 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000359
Lev Walkin7c1dc052016-03-14 03:08:15 -0700360 ASN__ENCODED_OK(er);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000361}
362
Lev Walkin5f560912004-10-21 13:37:57 +0000363
364/*
365 * Decode the chunk of XML text encoding REAL.
366 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000367static enum xer_pbd_rval
368REAL__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 +0000369 REAL_t *st = (REAL_t *)sptr;
Lev Walkin5f560912004-10-21 13:37:57 +0000370 double value;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000371 const char *xerdata = (const char *)chunk_buf;
Lev Walkin5f560912004-10-21 13:37:57 +0000372 char *endptr = 0;
373 char *b;
374
Lev Walkine0b56e02005-02-25 12:10:27 +0000375 (void)td;
376
Lev Walkin0fab1a62005-03-09 22:19:25 +0000377 if(!chunk_size) return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000378
379 /*
380 * Decode an XMLSpecialRealValue: <MINUS-INFINITY>, etc.
381 */
382 if(xerdata[0] == 0x3c /* '<' */) {
383 size_t i;
384 for(i = 0; i < sizeof(specialRealValue)
385 / sizeof(specialRealValue[0]); i++) {
386 struct specialRealValue_s *srv = &specialRealValue[i];
Lev Walkin1aea6982004-10-26 09:35:25 +0000387 double dv;
388
Lev Walkin5f560912004-10-21 13:37:57 +0000389 if(srv->length != chunk_size
390 || memcmp(srv->string, chunk_buf, chunk_size))
391 continue;
392
Lev Walkin1aea6982004-10-26 09:35:25 +0000393 /*
394 * It could've been done using
395 * (double)srv->dv / real_zero,
396 * but it summons fp exception on some platforms.
397 */
398 switch(srv->dv) {
399 case -1: dv = - INFINITY; break;
400 case 0: dv = NAN; break;
401 case 1: dv = INFINITY; break;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000402 default: return XPBD_SYSTEM_FAILURE;
Lev Walkin1aea6982004-10-26 09:35:25 +0000403 }
404
Lev Walkin0fab1a62005-03-09 22:19:25 +0000405 if(asn_double2REAL(st, dv))
406 return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000407
Lev Walkin0fab1a62005-03-09 22:19:25 +0000408 return XPBD_BODY_CONSUMED;
Lev Walkin5f560912004-10-21 13:37:57 +0000409 }
410 ASN_DEBUG("Unknown XMLSpecialRealValue");
Lev Walkin0fab1a62005-03-09 22:19:25 +0000411 return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000412 }
413
414 /*
415 * Copy chunk into the nul-terminated string, and run strtod.
416 */
Lev Walkin8471cec2004-10-21 14:02:19 +0000417 b = (char *)MALLOC(chunk_size + 1);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000418 if(!b) return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000419 memcpy(b, chunk_buf, chunk_size);
Lev Walkin8471cec2004-10-21 14:02:19 +0000420 b[chunk_size] = 0; /* nul-terminate */
Lev Walkin5f560912004-10-21 13:37:57 +0000421
422 value = strtod(b, &endptr);
Lev Walkin419f6752006-09-13 04:02:00 +0000423 FREEMEM(b);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000424 if(endptr == b) return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000425
426 if(asn_double2REAL(st, value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000427 return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000428
Lev Walkin0fab1a62005-03-09 22:19:25 +0000429 return XPBD_BODY_CONSUMED;
Lev Walkin5f560912004-10-21 13:37:57 +0000430}
431
432asn_dec_rval_t
433REAL_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
434 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000435 const void *buf_ptr, size_t size) {
Lev Walkin5f560912004-10-21 13:37:57 +0000436
437 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000438 sptr, sizeof(REAL_t), opt_mname,
Lev Walkin5f560912004-10-21 13:37:57 +0000439 buf_ptr, size, REAL__xer_body_decode);
440}
441
Lev Walkin41ba1f22004-09-14 12:46:35 +0000442int
Lev Walkin5e033762004-09-29 13:26:15 +0000443asn_REAL2double(const REAL_t *st, double *dbl_value) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000444 unsigned int octv;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000445
446 if(!st || !st->buf) {
447 errno = EINVAL;
448 return -1;
449 }
450
451 if(st->size == 0) {
452 *dbl_value = 0;
453 return 0;
454 }
455
456 octv = st->buf[0]; /* unsigned byte */
457
458 switch(octv & 0xC0) {
Lev Walkin5fda7d52012-01-09 03:20:19 -0800459 case 0x40: /* X.690: 8.5.6 a) => 8.5.9 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000460 /* "SpecialRealValue" */
461
462 /* Be liberal in what you accept...
Lev Walkin749916f2012-01-07 17:03:39 -0800463 * http://en.wikipedia.org/wiki/Robustness_principle
Lev Walkin41ba1f22004-09-14 12:46:35 +0000464 if(st->size != 1) ...
465 */
466
467 switch(st->buf[0]) {
468 case 0x40: /* 01000000: PLUS-INFINITY */
Lev Walkin1aea6982004-10-26 09:35:25 +0000469 *dbl_value = INFINITY;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000470 return 0;
471 case 0x41: /* 01000001: MINUS-INFINITY */
Lev Walkin1aea6982004-10-26 09:35:25 +0000472 *dbl_value = - INFINITY;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000473 return 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000474 case 0x42: /* 01000010: NOT-A-NUMBER */
475 *dbl_value = NAN;
476 return 0;
477 case 0x43: /* 01000011: minus zero */
Lev Walkin2a789d92004-09-27 21:36:59 +0000478 *dbl_value = -0.0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000479 return 0;
480 }
481
482 errno = EINVAL;
483 return -1;
Lev Walkin0959ffb2011-06-25 15:48:52 -0700484 case 0x00: { /* X.690: 8.5.7 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000485 /*
Lev Walkin5fda7d52012-01-09 03:20:19 -0800486 * Decimal. NR{1,2,3} format from ISO 6093.
487 * NR1: [ ]*[+-]?[0-9]+
488 * NR2: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)
489 * NR3: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)[Ee][+-]?[0-9]+
Lev Walkin41ba1f22004-09-14 12:46:35 +0000490 */
491 double d;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800492 char *buf;
493 char *endptr;
494 int used_malloc = 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000495
Lev Walkin5fda7d52012-01-09 03:20:19 -0800496 if(octv == 0 || (octv & 0x3C)) {
Lev Walkin0959ffb2011-06-25 15:48:52 -0700497 /* Remaining values of bits 6 to 1 are Reserved. */
498 errno = EINVAL;
499 return -1;
500 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000501
Lev Walkin5fda7d52012-01-09 03:20:19 -0800502
Lev Walkin53e5ae62017-08-23 10:56:19 -0700503 /* 1. By contract, an input buffer should be '\0'-terminated.
Lev Walkin5fda7d52012-01-09 03:20:19 -0800504 * OCTET STRING decoder ensures that, as is asn_double2REAL().
505 * 2. ISO 6093 specifies COMMA as a possible decimal separator.
506 * However, strtod() can't always deal with COMMA.
507 * So her we fix both by reallocating, copying and fixing.
508 */
Lev Walkin53e5ae62017-08-23 10:56:19 -0700509 if(st->buf[st->size] != '\0' || memchr(st->buf, ',', st->size)) {
Lev Walkin5fda7d52012-01-09 03:20:19 -0800510 uint8_t *p, *end;
511 char *b;
512 if(st->size > 100) {
513 /* Avoid malicious stack overflow in alloca() */
514 buf = (char *)MALLOC(st->size);
515 if(!buf) return -1;
516 used_malloc = 1;
517 } else {
518 buf = alloca(st->size);
519 }
520 b = buf;
521 /* Copy without the first byte and with 0-termination */
522 for(p = st->buf + 1, end = st->buf + st->size;
523 p < end; b++, p++)
524 *b = (*p == ',') ? '.' : *p;
525 *b = '\0';
526 } else {
527 buf = (char *)&st->buf[1];
528 }
529
530 endptr = buf;
531 d = strtod(buf, &endptr);
532 if(*endptr != '\0') {
533 /* Format is not consistent with ISO 6093 */
534 if(used_malloc) FREEMEM(buf);
Lev Walkin0959ffb2011-06-25 15:48:52 -0700535 errno = EINVAL;
536 return -1;
537 }
Lev Walkin5fda7d52012-01-09 03:20:19 -0800538 if(used_malloc) FREEMEM(buf);
Lev Walkindaeb2162014-01-13 23:08:35 -0800539 if(_asn_isfinite(d)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000540 *dbl_value = d;
541 return 0;
542 } else {
543 errno = ERANGE;
Lev Walkin0959ffb2011-06-25 15:48:52 -0700544 return -1;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000545 }
546 }
547 }
548
549 /*
550 * Binary representation.
551 */
552 {
553 double m;
554 int expval; /* exponent value */
555 unsigned int elen; /* exponent value length, in octets */
556 unsigned int scaleF;
557 unsigned int baseF;
558 uint8_t *ptr;
559 uint8_t *end;
560 int sign;
561
562 switch((octv & 0x30) >> 4) {
563 case 0x00: baseF = 1; break; /* base 2 */
564 case 0x01: baseF = 3; break; /* base 8 */
565 case 0x02: baseF = 4; break; /* base 16 */
566 default:
567 /* Reserved field, can't parse now. */
568 errno = EINVAL;
569 return -1;
570 }
571
572 sign = (octv & 0x40); /* bit 7 */
573 scaleF = (octv & 0x0C) >> 2; /* bits 4 to 3 */
574
Lev Walkin494fb702017-08-07 20:07:00 -0700575 if(st->size <= 1 + (octv & 0x03)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000576 errno = EINVAL;
577 return -1;
578 }
579
Lev Walkine78753d2007-06-23 17:02:00 +0000580 elen = (octv & 0x03); /* bits 2 to 1; 8.5.6.4 */
581 if(elen == 0x03) { /* bits 2 to 1 = 11; 8.5.6.4, case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000582 elen = st->buf[1]; /* unsigned binary number */
Lev Walkin494fb702017-08-07 20:07:00 -0700583 if(elen == 0 || st->size <= (2 + elen)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000584 errno = EINVAL;
585 return -1;
586 }
Lev Walkine78753d2007-06-23 17:02:00 +0000587 /* FIXME: verify constraints of case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000588 ptr = &st->buf[2];
589 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000590 ptr = &st->buf[1];
591 }
592
593 /* Fetch the multibyte exponent */
594 expval = (int)(*(int8_t *)ptr);
595 end = ptr + elen + 1;
596 for(ptr++; ptr < end; ptr++)
597 expval = (expval * 256) + *ptr;
598
599 m = 0.0; /* Initial mantissa value */
600
601 /* Okay, the exponent is here. Now, what about mantissa? */
602 end = st->buf + st->size;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800603 for(; ptr < end; ptr++)
604 m = ldexp(m, 8) + *ptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000605
Lev Walkin5f560912004-10-21 13:37:57 +0000606 if(0)
Lev Walkin5fda7d52012-01-09 03:20:19 -0800607 ASN_DEBUG("m=%.10f, scF=%d, bF=%d, expval=%d, ldexp()=%f, ldexp()=%f\n",
Lev Walkin41ba1f22004-09-14 12:46:35 +0000608 m, scaleF, baseF, expval,
609 ldexp(m, expval * baseF + scaleF),
Lev Walkin77aa90f2005-04-28 22:56:36 +0000610 ldexp(m, scaleF) * pow(pow(2, baseF), expval)
Lev Walkin41ba1f22004-09-14 12:46:35 +0000611 );
612
613 /*
614 * (S * N * 2^F) * B^E
615 * Essentially:
Lev Walkin77aa90f2005-04-28 22:56:36 +0000616 m = ldexp(m, scaleF) * pow(pow(2, base), expval);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000617 */
618 m = ldexp(m, expval * baseF + scaleF);
Lev Walkindaeb2162014-01-13 23:08:35 -0800619 if(_asn_isfinite(m)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000620 *dbl_value = sign ? -m : m;
621 } else {
622 errno = ERANGE;
623 return -1;
624 }
625
626 } /* if(binary_format) */
627
628 return 0;
629}
630
631/*
632 * Assume IEEE 754 floating point: standard 64 bit double.
633 * [1 bit sign] [11 bits exponent] [52 bits mantissa]
634 */
635int
Lev Walkin5e033762004-09-29 13:26:15 +0000636asn_double2REAL(REAL_t *st, double dbl_value) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000637#ifdef WORDS_BIGENDIAN /* Known to be big-endian */
638 int littleEndian = 0;
639#else /* need to test: have no explicit information */
640 unsigned int LE = 1;
641 int littleEndian = *(unsigned char *)&LE;
642#endif
643 uint8_t buf[16]; /* More than enough for 8-byte dbl_value */
644 uint8_t dscr[sizeof(dbl_value)]; /* double value scratch pad */
645 /* Assertion guards: won't even compile, if unexpected double size */
Lev Walkin50b0f6c2006-10-16 12:01:36 +0000646 char assertion_buffer1[9 - sizeof(dbl_value)] GCC_NOTUSED;
647 char assertion_buffer2[sizeof(dbl_value) - 7] GCC_NOTUSED;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000648 uint8_t *ptr = buf;
649 uint8_t *mstop; /* Last byte of mantissa */
650 unsigned int mval; /* Value of the last byte of mantissa */
651 unsigned int bmsign; /* binary mask with sign */
652 unsigned int buflen;
653 unsigned int accum;
654 int expval;
655
656 if(!st) {
657 errno = EINVAL;
658 return -1;
659 }
660
Lev Walkin057fb732004-09-14 13:58:10 +0000661 /*
662 * ilogb(+-0) returns -INT_MAX or INT_MIN (platform-dependent)
Lev Walkin2f505022005-07-01 08:28:18 +0000663 * ilogb(+-inf) returns INT_MAX, logb(+-inf) returns +inf
Lev Walkin2a789d92004-09-27 21:36:59 +0000664 * ilogb(NaN) returns INT_MIN or INT_MAX (platform-dependent)
Lev Walkin057fb732004-09-14 13:58:10 +0000665 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000666 expval = ilogb(dbl_value);
Lev Walkin2a789d92004-09-27 21:36:59 +0000667 if(expval <= -INT_MAX /* Also catches +-0 and maybe isnan() */
668 || expval == INT_MAX /* catches isfin() and maybe isnan() */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000669 ) {
670 if(!st->buf || st->size < 2) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000671 ptr = (uint8_t *)MALLOC(2);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000672 if(!ptr) return -1;
673 st->buf = ptr;
674 }
675 /* fpclassify(3) is not portable yet */
Lev Walkin2a789d92004-09-27 21:36:59 +0000676 if(isnan(dbl_value)) {
677 st->buf[0] = 0x42; /* NaN */
678 st->buf[1] = 0;
679 st->size = 1;
Lev Walkindaeb2162014-01-13 23:08:35 -0800680 } else if(!_asn_isfinite(dbl_value)) {
Lev Walkin7e03db92004-09-14 13:50:21 +0000681 if(copysign(1.0, dbl_value) < 0.0) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000682 st->buf[0] = 0x41; /* MINUS-INFINITY */
683 } else {
684 st->buf[0] = 0x40; /* PLUS-INFINITY */
685 }
686 st->buf[1] = 0;
687 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000688 } else {
Lev Walkin5fe72e92012-01-07 17:00:29 -0800689 if(copysign(1.0, dbl_value) >= 0.0) {
Lev Walkinc51e7d62004-09-27 22:16:18 +0000690 /* no content octets: positive zero */
691 st->buf[0] = 0; /* JIC */
692 st->size = 0;
Lev Walkin5fe72e92012-01-07 17:00:29 -0800693 } else {
694 /* Negative zero. #8.5.3, 8.5.9 */
695 st->buf[0] = 0x43;
696 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000697 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000698 }
699 return 0;
700 }
701
702 if(littleEndian) {
703 uint8_t *s = ((uint8_t *)&dbl_value) + sizeof(dbl_value) - 2;
Lev Walkin057fb732004-09-14 13:58:10 +0000704 uint8_t *start = ((uint8_t *)&dbl_value);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000705 uint8_t *d;
706
707 bmsign = 0x80 | ((s[1] >> 1) & 0x40); /* binary mask & - */
Lev Walkin057fb732004-09-14 13:58:10 +0000708 for(mstop = d = dscr; s >= start; d++, s--) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000709 *d = *s;
710 if(*d) mstop = d;
711 }
712 } else {
713 uint8_t *s = ((uint8_t *)&dbl_value) + 1;
714 uint8_t *end = ((uint8_t *)&dbl_value) + sizeof(double);
715 uint8_t *d;
716
717 bmsign = 0x80 | ((s[-1] >> 1) & 0x40); /* binary mask & - */
718 for(mstop = d = dscr; s < end; d++, s++) {
719 *d = *s;
720 if(*d) mstop = d;
721 }
722 }
723
724 /* Remove parts of the exponent, leave mantissa and explicit 1. */
725 dscr[0] = 0x10 | (dscr[0] & 0x0f);
726
727 /* Adjust exponent in a very unobvious way */
728 expval -= 8 * ((mstop - dscr) + 1) - 4;
729
730 /* This loop ensures DER conformance by forcing mantissa odd: 11.3.1 */
731 mval = *mstop;
732 if(mval && !(mval & 1)) {
733 unsigned int shift_count = 1;
734 unsigned int ishift;
735 uint8_t *mptr;
736
737 /*
738 * Figure out what needs to be done to make mantissa odd.
739 */
740 if(!(mval & 0x0f)) /* Speed-up a little */
741 shift_count = 4;
742 while(((mval >> shift_count) & 1) == 0)
743 shift_count++;
744
745 ishift = 8 - shift_count;
746 accum = 0;
747
748 /* Go over the buffer, shifting it shift_count bits right. */
749 for(mptr = dscr; mptr <= mstop; mptr++) {
750 mval = *mptr;
751 *mptr = accum | (mval >> shift_count);
752 accum = mval << ishift;
753 }
754
Lev Walkinb89b3402012-01-09 18:26:30 -0800755 /* Adjust exponent appropriately. */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000756 expval += shift_count;
757 }
758
759 if(expval < 0) {
760 if((expval >> 7) == -1) {
761 *ptr++ = bmsign | 0x00;
762 *ptr++ = expval;
763 } else if((expval >> 15) == -1) {
764 *ptr++ = bmsign | 0x01;
765 *ptr++ = expval >> 8;
766 *ptr++ = expval;
767 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000768 *ptr++ = bmsign | 0x02;
769 *ptr++ = expval >> 16;
770 *ptr++ = expval >> 8;
771 *ptr++ = expval;
772 }
773 } else if(expval <= 0x7f) {
774 *ptr++ = bmsign | 0x00;
775 *ptr++ = expval;
776 } else if(expval <= 0x7fff) {
777 *ptr++ = bmsign | 0x01;
778 *ptr++ = expval >> 8;
779 *ptr++ = expval;
780 } else {
781 assert(expval <= 0x7fffff);
782 *ptr++ = bmsign | 0x02;
783 *ptr++ = expval >> 16;
784 *ptr++ = expval >> 8;
785 *ptr++ = expval;
786 }
787
788 buflen = (mstop - dscr) + 1;
789 memcpy(ptr, dscr, buflen);
790 ptr += buflen;
791 buflen = ptr - buf;
792
Lev Walkinc17d90f2005-01-17 14:32:45 +0000793 ptr = (uint8_t *)MALLOC(buflen + 1);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000794 if(!ptr) return -1;
795
796 memcpy(ptr, buf, buflen);
797 buf[buflen] = 0; /* JIC */
798
799 if(st->buf) FREEMEM(st->buf);
800 st->buf = ptr;
801 st->size = buflen;
802
803 return 0;
804}
Lev Walkin41d972b2017-08-23 23:30:59 -0700805
806#ifndef ASN_DISABLE_PER_SUPPORT
807
808asn_dec_rval_t
809REAL_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
810 const asn_per_constraints_t *constraints, void **sptr,
811 asn_per_data_t *pd) {
812 (void)constraints; /* No PER visible constraints */
813 return OCTET_STRING_decode_uper(opt_codec_ctx, td, 0, sptr, pd);
814}
815
816asn_enc_rval_t
817REAL_encode_uper(asn_TYPE_descriptor_t *td,
818 const asn_per_constraints_t *constraints, void *sptr,
819 asn_per_outp_t *po) {
820 (void)constraints; /* No PER visible constraints */
821 return OCTET_STRING_encode_uper(td, 0, sptr, po);
822}
823
824#endif /* ASN_DISABLE_PER_SUPPORT */