blob: e31e1ad95af4e60d9d3a3137aeb7b15ab2f552f5 [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 Walkin725883b2006-10-09 12:07:58 +0000442asn_dec_rval_t
Lev Walkin494fb702017-08-07 20:07:00 -0700443REAL_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
444 const asn_per_constraints_t *constraints, void **sptr,
445 asn_per_data_t *pd) {
446 (void)constraints; /* No PER visible constraints */
Lev Walkin725883b2006-10-09 12:07:58 +0000447 return OCTET_STRING_decode_uper(opt_codec_ctx, td, 0, sptr, pd);
448}
449
450asn_enc_rval_t
451REAL_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700452 const asn_per_constraints_t *constraints, void *sptr,
453 asn_per_outp_t *po) {
454 (void)constraints; /* No PER visible constraints */
Lev Walkin725883b2006-10-09 12:07:58 +0000455 return OCTET_STRING_encode_uper(td, 0, sptr, po);
456}
Lev Walkin5f560912004-10-21 13:37:57 +0000457
Lev Walkin41ba1f22004-09-14 12:46:35 +0000458int
Lev Walkin5e033762004-09-29 13:26:15 +0000459asn_REAL2double(const REAL_t *st, double *dbl_value) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000460 unsigned int octv;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000461
462 if(!st || !st->buf) {
463 errno = EINVAL;
464 return -1;
465 }
466
467 if(st->size == 0) {
468 *dbl_value = 0;
469 return 0;
470 }
471
472 octv = st->buf[0]; /* unsigned byte */
473
474 switch(octv & 0xC0) {
Lev Walkin5fda7d52012-01-09 03:20:19 -0800475 case 0x40: /* X.690: 8.5.6 a) => 8.5.9 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000476 /* "SpecialRealValue" */
477
478 /* Be liberal in what you accept...
Lev Walkin749916f2012-01-07 17:03:39 -0800479 * http://en.wikipedia.org/wiki/Robustness_principle
Lev Walkin41ba1f22004-09-14 12:46:35 +0000480 if(st->size != 1) ...
481 */
482
483 switch(st->buf[0]) {
484 case 0x40: /* 01000000: PLUS-INFINITY */
Lev Walkin1aea6982004-10-26 09:35:25 +0000485 *dbl_value = INFINITY;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000486 return 0;
487 case 0x41: /* 01000001: MINUS-INFINITY */
Lev Walkin1aea6982004-10-26 09:35:25 +0000488 *dbl_value = - INFINITY;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000489 return 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000490 case 0x42: /* 01000010: NOT-A-NUMBER */
491 *dbl_value = NAN;
492 return 0;
493 case 0x43: /* 01000011: minus zero */
Lev Walkin2a789d92004-09-27 21:36:59 +0000494 *dbl_value = -0.0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000495 return 0;
496 }
497
498 errno = EINVAL;
499 return -1;
Lev Walkin0959ffb2011-06-25 15:48:52 -0700500 case 0x00: { /* X.690: 8.5.7 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000501 /*
Lev Walkin5fda7d52012-01-09 03:20:19 -0800502 * Decimal. NR{1,2,3} format from ISO 6093.
503 * NR1: [ ]*[+-]?[0-9]+
504 * NR2: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)
505 * NR3: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)[Ee][+-]?[0-9]+
Lev Walkin41ba1f22004-09-14 12:46:35 +0000506 */
507 double d;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800508 char *buf;
509 char *endptr;
510 int used_malloc = 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000511
Lev Walkin5fda7d52012-01-09 03:20:19 -0800512 if(octv == 0 || (octv & 0x3C)) {
Lev Walkin0959ffb2011-06-25 15:48:52 -0700513 /* Remaining values of bits 6 to 1 are Reserved. */
514 errno = EINVAL;
515 return -1;
516 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000517
Lev Walkin5fda7d52012-01-09 03:20:19 -0800518
Lev Walkin53e5ae62017-08-23 10:56:19 -0700519 /* 1. By contract, an input buffer should be '\0'-terminated.
Lev Walkin5fda7d52012-01-09 03:20:19 -0800520 * OCTET STRING decoder ensures that, as is asn_double2REAL().
521 * 2. ISO 6093 specifies COMMA as a possible decimal separator.
522 * However, strtod() can't always deal with COMMA.
523 * So her we fix both by reallocating, copying and fixing.
524 */
Lev Walkin53e5ae62017-08-23 10:56:19 -0700525 if(st->buf[st->size] != '\0' || memchr(st->buf, ',', st->size)) {
Lev Walkin5fda7d52012-01-09 03:20:19 -0800526 uint8_t *p, *end;
527 char *b;
528 if(st->size > 100) {
529 /* Avoid malicious stack overflow in alloca() */
530 buf = (char *)MALLOC(st->size);
531 if(!buf) return -1;
532 used_malloc = 1;
533 } else {
534 buf = alloca(st->size);
535 }
536 b = buf;
537 /* Copy without the first byte and with 0-termination */
538 for(p = st->buf + 1, end = st->buf + st->size;
539 p < end; b++, p++)
540 *b = (*p == ',') ? '.' : *p;
541 *b = '\0';
542 } else {
543 buf = (char *)&st->buf[1];
544 }
545
546 endptr = buf;
547 d = strtod(buf, &endptr);
548 if(*endptr != '\0') {
549 /* Format is not consistent with ISO 6093 */
550 if(used_malloc) FREEMEM(buf);
Lev Walkin0959ffb2011-06-25 15:48:52 -0700551 errno = EINVAL;
552 return -1;
553 }
Lev Walkin5fda7d52012-01-09 03:20:19 -0800554 if(used_malloc) FREEMEM(buf);
Lev Walkindaeb2162014-01-13 23:08:35 -0800555 if(_asn_isfinite(d)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000556 *dbl_value = d;
557 return 0;
558 } else {
559 errno = ERANGE;
Lev Walkin0959ffb2011-06-25 15:48:52 -0700560 return -1;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000561 }
562 }
563 }
564
565 /*
566 * Binary representation.
567 */
568 {
569 double m;
570 int expval; /* exponent value */
571 unsigned int elen; /* exponent value length, in octets */
572 unsigned int scaleF;
573 unsigned int baseF;
574 uint8_t *ptr;
575 uint8_t *end;
576 int sign;
577
578 switch((octv & 0x30) >> 4) {
579 case 0x00: baseF = 1; break; /* base 2 */
580 case 0x01: baseF = 3; break; /* base 8 */
581 case 0x02: baseF = 4; break; /* base 16 */
582 default:
583 /* Reserved field, can't parse now. */
584 errno = EINVAL;
585 return -1;
586 }
587
588 sign = (octv & 0x40); /* bit 7 */
589 scaleF = (octv & 0x0C) >> 2; /* bits 4 to 3 */
590
Lev Walkin494fb702017-08-07 20:07:00 -0700591 if(st->size <= 1 + (octv & 0x03)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000592 errno = EINVAL;
593 return -1;
594 }
595
Lev Walkine78753d2007-06-23 17:02:00 +0000596 elen = (octv & 0x03); /* bits 2 to 1; 8.5.6.4 */
597 if(elen == 0x03) { /* bits 2 to 1 = 11; 8.5.6.4, case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000598 elen = st->buf[1]; /* unsigned binary number */
Lev Walkin494fb702017-08-07 20:07:00 -0700599 if(elen == 0 || st->size <= (2 + elen)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000600 errno = EINVAL;
601 return -1;
602 }
Lev Walkine78753d2007-06-23 17:02:00 +0000603 /* FIXME: verify constraints of case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000604 ptr = &st->buf[2];
605 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000606 ptr = &st->buf[1];
607 }
608
609 /* Fetch the multibyte exponent */
610 expval = (int)(*(int8_t *)ptr);
611 end = ptr + elen + 1;
612 for(ptr++; ptr < end; ptr++)
613 expval = (expval * 256) + *ptr;
614
615 m = 0.0; /* Initial mantissa value */
616
617 /* Okay, the exponent is here. Now, what about mantissa? */
618 end = st->buf + st->size;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800619 for(; ptr < end; ptr++)
620 m = ldexp(m, 8) + *ptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000621
Lev Walkin5f560912004-10-21 13:37:57 +0000622 if(0)
Lev Walkin5fda7d52012-01-09 03:20:19 -0800623 ASN_DEBUG("m=%.10f, scF=%d, bF=%d, expval=%d, ldexp()=%f, ldexp()=%f\n",
Lev Walkin41ba1f22004-09-14 12:46:35 +0000624 m, scaleF, baseF, expval,
625 ldexp(m, expval * baseF + scaleF),
Lev Walkin77aa90f2005-04-28 22:56:36 +0000626 ldexp(m, scaleF) * pow(pow(2, baseF), expval)
Lev Walkin41ba1f22004-09-14 12:46:35 +0000627 );
628
629 /*
630 * (S * N * 2^F) * B^E
631 * Essentially:
Lev Walkin77aa90f2005-04-28 22:56:36 +0000632 m = ldexp(m, scaleF) * pow(pow(2, base), expval);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000633 */
634 m = ldexp(m, expval * baseF + scaleF);
Lev Walkindaeb2162014-01-13 23:08:35 -0800635 if(_asn_isfinite(m)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000636 *dbl_value = sign ? -m : m;
637 } else {
638 errno = ERANGE;
639 return -1;
640 }
641
642 } /* if(binary_format) */
643
644 return 0;
645}
646
647/*
648 * Assume IEEE 754 floating point: standard 64 bit double.
649 * [1 bit sign] [11 bits exponent] [52 bits mantissa]
650 */
651int
Lev Walkin5e033762004-09-29 13:26:15 +0000652asn_double2REAL(REAL_t *st, double dbl_value) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000653#ifdef WORDS_BIGENDIAN /* Known to be big-endian */
654 int littleEndian = 0;
655#else /* need to test: have no explicit information */
656 unsigned int LE = 1;
657 int littleEndian = *(unsigned char *)&LE;
658#endif
659 uint8_t buf[16]; /* More than enough for 8-byte dbl_value */
660 uint8_t dscr[sizeof(dbl_value)]; /* double value scratch pad */
661 /* Assertion guards: won't even compile, if unexpected double size */
Lev Walkin50b0f6c2006-10-16 12:01:36 +0000662 char assertion_buffer1[9 - sizeof(dbl_value)] GCC_NOTUSED;
663 char assertion_buffer2[sizeof(dbl_value) - 7] GCC_NOTUSED;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000664 uint8_t *ptr = buf;
665 uint8_t *mstop; /* Last byte of mantissa */
666 unsigned int mval; /* Value of the last byte of mantissa */
667 unsigned int bmsign; /* binary mask with sign */
668 unsigned int buflen;
669 unsigned int accum;
670 int expval;
671
672 if(!st) {
673 errno = EINVAL;
674 return -1;
675 }
676
Lev Walkin057fb732004-09-14 13:58:10 +0000677 /*
678 * ilogb(+-0) returns -INT_MAX or INT_MIN (platform-dependent)
Lev Walkin2f505022005-07-01 08:28:18 +0000679 * ilogb(+-inf) returns INT_MAX, logb(+-inf) returns +inf
Lev Walkin2a789d92004-09-27 21:36:59 +0000680 * ilogb(NaN) returns INT_MIN or INT_MAX (platform-dependent)
Lev Walkin057fb732004-09-14 13:58:10 +0000681 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000682 expval = ilogb(dbl_value);
Lev Walkin2a789d92004-09-27 21:36:59 +0000683 if(expval <= -INT_MAX /* Also catches +-0 and maybe isnan() */
684 || expval == INT_MAX /* catches isfin() and maybe isnan() */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000685 ) {
686 if(!st->buf || st->size < 2) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000687 ptr = (uint8_t *)MALLOC(2);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000688 if(!ptr) return -1;
689 st->buf = ptr;
690 }
691 /* fpclassify(3) is not portable yet */
Lev Walkin2a789d92004-09-27 21:36:59 +0000692 if(isnan(dbl_value)) {
693 st->buf[0] = 0x42; /* NaN */
694 st->buf[1] = 0;
695 st->size = 1;
Lev Walkindaeb2162014-01-13 23:08:35 -0800696 } else if(!_asn_isfinite(dbl_value)) {
Lev Walkin7e03db92004-09-14 13:50:21 +0000697 if(copysign(1.0, dbl_value) < 0.0) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000698 st->buf[0] = 0x41; /* MINUS-INFINITY */
699 } else {
700 st->buf[0] = 0x40; /* PLUS-INFINITY */
701 }
702 st->buf[1] = 0;
703 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000704 } else {
Lev Walkin5fe72e92012-01-07 17:00:29 -0800705 if(copysign(1.0, dbl_value) >= 0.0) {
Lev Walkinc51e7d62004-09-27 22:16:18 +0000706 /* no content octets: positive zero */
707 st->buf[0] = 0; /* JIC */
708 st->size = 0;
Lev Walkin5fe72e92012-01-07 17:00:29 -0800709 } else {
710 /* Negative zero. #8.5.3, 8.5.9 */
711 st->buf[0] = 0x43;
712 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000713 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000714 }
715 return 0;
716 }
717
718 if(littleEndian) {
719 uint8_t *s = ((uint8_t *)&dbl_value) + sizeof(dbl_value) - 2;
Lev Walkin057fb732004-09-14 13:58:10 +0000720 uint8_t *start = ((uint8_t *)&dbl_value);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000721 uint8_t *d;
722
723 bmsign = 0x80 | ((s[1] >> 1) & 0x40); /* binary mask & - */
Lev Walkin057fb732004-09-14 13:58:10 +0000724 for(mstop = d = dscr; s >= start; d++, s--) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000725 *d = *s;
726 if(*d) mstop = d;
727 }
728 } else {
729 uint8_t *s = ((uint8_t *)&dbl_value) + 1;
730 uint8_t *end = ((uint8_t *)&dbl_value) + sizeof(double);
731 uint8_t *d;
732
733 bmsign = 0x80 | ((s[-1] >> 1) & 0x40); /* binary mask & - */
734 for(mstop = d = dscr; s < end; d++, s++) {
735 *d = *s;
736 if(*d) mstop = d;
737 }
738 }
739
740 /* Remove parts of the exponent, leave mantissa and explicit 1. */
741 dscr[0] = 0x10 | (dscr[0] & 0x0f);
742
743 /* Adjust exponent in a very unobvious way */
744 expval -= 8 * ((mstop - dscr) + 1) - 4;
745
746 /* This loop ensures DER conformance by forcing mantissa odd: 11.3.1 */
747 mval = *mstop;
748 if(mval && !(mval & 1)) {
749 unsigned int shift_count = 1;
750 unsigned int ishift;
751 uint8_t *mptr;
752
753 /*
754 * Figure out what needs to be done to make mantissa odd.
755 */
756 if(!(mval & 0x0f)) /* Speed-up a little */
757 shift_count = 4;
758 while(((mval >> shift_count) & 1) == 0)
759 shift_count++;
760
761 ishift = 8 - shift_count;
762 accum = 0;
763
764 /* Go over the buffer, shifting it shift_count bits right. */
765 for(mptr = dscr; mptr <= mstop; mptr++) {
766 mval = *mptr;
767 *mptr = accum | (mval >> shift_count);
768 accum = mval << ishift;
769 }
770
Lev Walkinb89b3402012-01-09 18:26:30 -0800771 /* Adjust exponent appropriately. */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000772 expval += shift_count;
773 }
774
775 if(expval < 0) {
776 if((expval >> 7) == -1) {
777 *ptr++ = bmsign | 0x00;
778 *ptr++ = expval;
779 } else if((expval >> 15) == -1) {
780 *ptr++ = bmsign | 0x01;
781 *ptr++ = expval >> 8;
782 *ptr++ = expval;
783 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000784 *ptr++ = bmsign | 0x02;
785 *ptr++ = expval >> 16;
786 *ptr++ = expval >> 8;
787 *ptr++ = expval;
788 }
789 } else if(expval <= 0x7f) {
790 *ptr++ = bmsign | 0x00;
791 *ptr++ = expval;
792 } else if(expval <= 0x7fff) {
793 *ptr++ = bmsign | 0x01;
794 *ptr++ = expval >> 8;
795 *ptr++ = expval;
796 } else {
797 assert(expval <= 0x7fffff);
798 *ptr++ = bmsign | 0x02;
799 *ptr++ = expval >> 16;
800 *ptr++ = expval >> 8;
801 *ptr++ = expval;
802 }
803
804 buflen = (mstop - dscr) + 1;
805 memcpy(ptr, dscr, buflen);
806 ptr += buflen;
807 buflen = ptr - buf;
808
Lev Walkinc17d90f2005-01-17 14:32:45 +0000809 ptr = (uint8_t *)MALLOC(buflen + 1);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000810 if(!ptr) return -1;
811
812 memcpy(ptr, buf, buflen);
813 buf[buflen] = 0; /* JIC */
814
815 if(st->buf) FREEMEM(st->buf);
816 st->buf = ptr;
817 st->size = buflen;
818
819 return 0;
820}