blob: ce0352265d7a9775b79e82580a8485bca38fcf6b [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};
Lev Walkin5e033762004-09-29 13:26:15 +000044asn_TYPE_descriptor_t asn_DEF_REAL = {
Lev Walkincd2f48e2017-08-10 02:14:59 -070045 "REAL",
46 "REAL",
47 ASN__PRIMITIVE_TYPE_free,
48 REAL_print,
49 REAL_compare,
50 asn_generic_no_constraint,
51 ber_decode_primitive,
52 der_encode_primitive,
53 REAL_decode_xer,
54 REAL_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070055#ifdef ASN_DISABLE_OER_SUPPORT
Lev Walkincd2f48e2017-08-10 02:14:59 -070056 0,
57 0,
Lev Walkincc159472017-07-06 08:26:36 -070058#else
Lev Walkincd2f48e2017-08-10 02:14:59 -070059 0,
60 0,
Lev Walkincc159472017-07-06 08:26:36 -070061#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040062#ifdef ASN_DISABLE_PER_SUPPORT
Lev Walkincd2f48e2017-08-10 02:14:59 -070063 0,
64 0,
Lev Walkinb33425f2017-07-14 14:59:52 +040065#else
Lev Walkincd2f48e2017-08-10 02:14:59 -070066 REAL_decode_uper,
67 REAL_encode_uper,
Lev Walkinb33425f2017-07-14 14:59:52 +040068#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkincd2f48e2017-08-10 02:14:59 -070069 0, /* Use generic outmost tag fetcher */
70 asn_DEF_REAL_tags,
71 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
72 asn_DEF_REAL_tags, /* Same as above */
73 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
74 0, /* No OER visible constraints */
75 0, /* No PER visible constraints */
76 0,
77 0, /* No members */
78 0 /* No specifics */
Lev Walkin41ba1f22004-09-14 12:46:35 +000079};
80
Lev Walkin5f560912004-10-21 13:37:57 +000081typedef enum specialRealValue {
82 SRV__NOT_A_NUMBER,
83 SRV__MINUS_INFINITY,
84 SRV__PLUS_INFINITY
85} specialRealValue_e;
86static struct specialRealValue_s {
87 char *string;
Lev Walkin8471cec2004-10-21 14:02:19 +000088 size_t length;
Lev Walkin1aea6982004-10-26 09:35:25 +000089 long dv;
Lev Walkin5f560912004-10-21 13:37:57 +000090} specialRealValue[] = {
91#define SRV_SET(foo, val) { foo, sizeof(foo) - 1, val }
Lev Walkin1aea6982004-10-26 09:35:25 +000092 SRV_SET("<NOT-A-NUMBER/>", 0),
93 SRV_SET("<MINUS-INFINITY/>", -1),
94 SRV_SET("<PLUS-INFINITY/>", 1),
Lev Walkin5f560912004-10-21 13:37:57 +000095#undef SRV_SET
96};
97
Lev Walkina9cc46e2004-09-22 16:06:28 +000098ssize_t
99REAL__dump(double d, int canonical, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin92b35d22004-09-27 20:52:18 +0000100 char local_buf[64];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000101 char *buf = local_buf;
102 ssize_t buflen = sizeof(local_buf);
Lev Walkin92b35d22004-09-27 20:52:18 +0000103 const char *fmt = canonical?"%.15E":"%.15f";
Lev Walkina9cc46e2004-09-22 16:06:28 +0000104 ssize_t ret;
105
Lev Walkin8e8078a2004-09-26 13:10:40 +0000106 /*
107 * Check whether it is a special value.
108 */
Lev Walkinc51e7d62004-09-27 22:16:18 +0000109 /* fpclassify(3) is not portable yet */
110 if(isnan(d)) {
Lev Walkin5f560912004-10-21 13:37:57 +0000111 buf = specialRealValue[SRV__NOT_A_NUMBER].string;
112 buflen = specialRealValue[SRV__NOT_A_NUMBER].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000113 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
Lev Walkindaeb2162014-01-13 23:08:35 -0800114 } else if(!_asn_isfinite(d)) {
Lev Walkinc51e7d62004-09-27 22:16:18 +0000115 if(copysign(1.0, d) < 0.0) {
Lev Walkin5f560912004-10-21 13:37:57 +0000116 buf = specialRealValue[SRV__MINUS_INFINITY].string;
117 buflen = specialRealValue[SRV__MINUS_INFINITY].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000118 } else {
Lev Walkin5f560912004-10-21 13:37:57 +0000119 buf = specialRealValue[SRV__PLUS_INFINITY].string;
120 buflen = specialRealValue[SRV__PLUS_INFINITY].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000121 }
122 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
123 } else if(ilogb(d) <= -INT_MAX) {
124 if(copysign(1.0, d) < 0.0) {
125 buf = "-0";
126 buflen = 2;
127 } else {
128 buf = "0";
129 buflen = 1;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000130 }
131 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
132 }
133
134 /*
135 * Use the libc's double printing, hopefully they got it right.
136 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000137 do {
138 ret = snprintf(buf, buflen, fmt, d);
139 if(ret < 0) {
140 buflen <<= 1;
141 } else if(ret >= buflen) {
142 buflen = ret + 1;
143 } else {
144 buflen = ret;
145 break;
146 }
Lev Walkin419f6752006-09-13 04:02:00 +0000147 if(buf != local_buf) FREEMEM(buf);
Lev Walkin8e8078a2004-09-26 13:10:40 +0000148 buf = (char *)MALLOC(buflen);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000149 if(!buf) return -1;
150 } while(1);
151
Lev Walkina9cc46e2004-09-22 16:06:28 +0000152 if(canonical) {
Lev Walkin92b35d22004-09-27 20:52:18 +0000153 /*
154 * Transform the "[-]d.dddE+-dd" output into "[-]d.dddE[-]d"
Lev Walkin5f560912004-10-21 13:37:57 +0000155 * Check that snprintf() constructed the output correctly.
Lev Walkin92b35d22004-09-27 20:52:18 +0000156 */
Lev Walkin8ca13c82016-01-24 22:40:00 -0800157 char *dot;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000158 char *end = buf + buflen;
Lev Walkin92b35d22004-09-27 20:52:18 +0000159 char *last_zero;
Lev Walkin8ca13c82016-01-24 22:40:00 -0800160 char *prev_zero;
161 char *s;
162
163 enum {
164 LZSTATE_NOTHING,
165 LZSTATE_SEEN_ZERO
166 } lz_state = LZSTATE_NOTHING;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000167
Lev Walkin5f560912004-10-21 13:37:57 +0000168 dot = (buf[0] == 0x2d /* '-' */) ? (buf + 2) : (buf + 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000169 if(*dot >= 0x30) {
Lev Walkin78d917d2012-01-07 15:29:08 -0800170 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000171 errno = EINVAL;
172 return -1; /* Not a dot, really */
173 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000174 *dot = 0x2e; /* Replace possible comma */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000175
Lev Walkin8ca13c82016-01-24 22:40:00 -0800176 for(prev_zero = last_zero = s = dot + 2; s < end; s++) {
177 switch(*s) {
178 case 0x45: /* 'E' */
179 if(lz_state == LZSTATE_SEEN_ZERO)
180 last_zero = prev_zero;
181 break;
182 case 0x30: /* '0' */
183 if(lz_state == LZSTATE_NOTHING)
184 prev_zero = s;
185 lz_state = LZSTATE_SEEN_ZERO;
186 continue;
187 default:
188 lz_state = LZSTATE_NOTHING;
189 continue;
190 }
191 break;
192 }
193
194 if(s == end) {
Lev Walkin78d917d2012-01-07 15:29:08 -0800195 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000196 errno = EINVAL;
197 return -1; /* No promised E */
198 }
Lev Walkin8ca13c82016-01-24 22:40:00 -0800199
200 assert(*s == 0x45);
201 {
202 char *E = s;
203 char *expptr = ++E;
204 char *s = expptr;
205 int sign;
206
207 if(*expptr == 0x2b /* '+' */) {
208 /* Skip the "+" */
209 buflen -= 1;
210 sign = 0;
211 } else {
212 sign = 1;
213 s++;
214 }
215 expptr++;
216 if(expptr > end) {
217 if(buf != local_buf) FREEMEM(buf);
218 errno = EINVAL;
219 return -1;
220 }
221 if(*expptr == 0x30) {
222 buflen--;
223 expptr++;
224 }
225 if(*last_zero == 0x30) {
226 *last_zero = 0x45; /* E */
227 buflen -= s - (last_zero + 1);
228 s = last_zero + 1;
229 if(sign) {
230 *s++ = 0x2d /* '-' */;
231 buflen++;
232 }
233 }
234 for(; expptr <= end; s++, expptr++)
235 *s = *expptr;
236 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000237 } else {
238 /*
239 * Remove trailing zeros.
240 */
241 char *end = buf + buflen;
242 char *last_zero = end;
Lev Walkinf0b808d2005-04-25 21:08:25 +0000243 int stoplooking = 0;
Lev Walkin92b35d22004-09-27 20:52:18 +0000244 char *z;
245 for(z = end - 1; z > buf; z--) {
246 switch(*z) {
Lev Walkinf0b808d2005-04-25 21:08:25 +0000247 case 0x30:
248 if(!stoplooking)
249 last_zero = z;
250 continue;
Lev Walkin92b35d22004-09-27 20:52:18 +0000251 case 0x31: case 0x32: case 0x33: case 0x34:
252 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinf0b808d2005-04-25 21:08:25 +0000253 stoplooking = 1;
Lev Walkin92b35d22004-09-27 20:52:18 +0000254 continue;
255 default: /* Catch dot and other separators */
Lev Walkinf0b808d2005-04-25 21:08:25 +0000256 /*
257 * Replace possible comma (which may even
258 * be not a comma at all: locale-defined).
259 */
260 *z = 0x2e;
Lev Walkin92b35d22004-09-27 20:52:18 +0000261 if(last_zero == z + 1) { /* leave x.0 */
262 last_zero++;
263 }
264 buflen = last_zero - buf;
265 *last_zero = '\0';
266 break;
267 }
268 break;
269 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000270 }
271
272 ret = cb(buf, buflen, app_key);
Lev Walkin419f6752006-09-13 04:02:00 +0000273 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000274 return (ret < 0) ? -1 : buflen;
275}
276
Lev Walkin41ba1f22004-09-14 12:46:35 +0000277int
Lev Walkin5e033762004-09-29 13:26:15 +0000278REAL_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000279 asn_app_consume_bytes_f *cb, void *app_key) {
280 const REAL_t *st = (const REAL_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000281 ssize_t ret;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000282 double d;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000283
284 (void)td; /* Unused argument */
285 (void)ilevel; /* Unused argument */
286
Lev Walkina9cc46e2004-09-22 16:06:28 +0000287 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000288 ret = cb("<absent>", 8, app_key);
Lev Walkin5e033762004-09-29 13:26:15 +0000289 else if(asn_REAL2double(st, &d))
Lev Walkin8e8078a2004-09-26 13:10:40 +0000290 ret = cb("<error>", 7, app_key);
291 else
292 ret = REAL__dump(d, 0, cb, app_key);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000293
Lev Walkin8e8078a2004-09-26 13:10:40 +0000294 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000295}
Lev Walkin41ba1f22004-09-14 12:46:35 +0000296
Lev Walkincd2f48e2017-08-10 02:14:59 -0700297int
298REAL_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
299 const void *bptr) {
300 const REAL_t *a = aptr;
301 const REAL_t *b = bptr;
302
303 (void)td;
304
305 if(a && b) {
306 double adbl, bdbl;
307 int ra, rb;
308 ra = asn_REAL2double(a, &adbl);
309 rb = asn_REAL2double(b, &bdbl);
310 if(ra == 0 && rb == 0) {
311 if(isnan(adbl)) {
312 if(isnan(bdbl)) {
313 return 0;
314 } else {
315 return -1;
316 }
317 } else if(isnan(bdbl)) {
318 return 1;
319 }
320 /* Value comparison. */
321 if(adbl < bdbl) {
322 return -1;
323 } else if(adbl > bdbl) {
324 return 1;
325 } else {
326 return 0;
327 }
328 } else if(ra) {
329 return -1;
330 } else {
331 return 1;
332 }
333 } else if(!a) {
334 return -1;
335 } else {
336 return 1;
337 }
338}
339
Lev Walkina9cc46e2004-09-22 16:06:28 +0000340asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000341REAL_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000342 int ilevel, enum xer_encoder_flags_e flags,
343 asn_app_consume_bytes_f *cb, void *app_key) {
344 REAL_t *st = (REAL_t *)sptr;
345 asn_enc_rval_t er;
346 double d;
347
348 (void)ilevel;
349
Lev Walkin5e033762004-09-29 13:26:15 +0000350 if(!st || !st->buf || asn_REAL2double(st, &d))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700351 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000352
353 er.encoded = REAL__dump(d, flags & XER_F_CANONICAL, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700354 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000355
Lev Walkin7c1dc052016-03-14 03:08:15 -0700356 ASN__ENCODED_OK(er);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000357}
358
Lev Walkin5f560912004-10-21 13:37:57 +0000359
360/*
361 * Decode the chunk of XML text encoding REAL.
362 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000363static enum xer_pbd_rval
364REAL__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 +0000365 REAL_t *st = (REAL_t *)sptr;
Lev Walkin5f560912004-10-21 13:37:57 +0000366 double value;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000367 const char *xerdata = (const char *)chunk_buf;
Lev Walkin5f560912004-10-21 13:37:57 +0000368 char *endptr = 0;
369 char *b;
370
Lev Walkine0b56e02005-02-25 12:10:27 +0000371 (void)td;
372
Lev Walkin0fab1a62005-03-09 22:19:25 +0000373 if(!chunk_size) return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000374
375 /*
376 * Decode an XMLSpecialRealValue: <MINUS-INFINITY>, etc.
377 */
378 if(xerdata[0] == 0x3c /* '<' */) {
379 size_t i;
380 for(i = 0; i < sizeof(specialRealValue)
381 / sizeof(specialRealValue[0]); i++) {
382 struct specialRealValue_s *srv = &specialRealValue[i];
Lev Walkin1aea6982004-10-26 09:35:25 +0000383 double dv;
384
Lev Walkin5f560912004-10-21 13:37:57 +0000385 if(srv->length != chunk_size
386 || memcmp(srv->string, chunk_buf, chunk_size))
387 continue;
388
Lev Walkin1aea6982004-10-26 09:35:25 +0000389 /*
390 * It could've been done using
391 * (double)srv->dv / real_zero,
392 * but it summons fp exception on some platforms.
393 */
394 switch(srv->dv) {
395 case -1: dv = - INFINITY; break;
396 case 0: dv = NAN; break;
397 case 1: dv = INFINITY; break;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000398 default: return XPBD_SYSTEM_FAILURE;
Lev Walkin1aea6982004-10-26 09:35:25 +0000399 }
400
Lev Walkin0fab1a62005-03-09 22:19:25 +0000401 if(asn_double2REAL(st, dv))
402 return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000403
Lev Walkin0fab1a62005-03-09 22:19:25 +0000404 return XPBD_BODY_CONSUMED;
Lev Walkin5f560912004-10-21 13:37:57 +0000405 }
406 ASN_DEBUG("Unknown XMLSpecialRealValue");
Lev Walkin0fab1a62005-03-09 22:19:25 +0000407 return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000408 }
409
410 /*
411 * Copy chunk into the nul-terminated string, and run strtod.
412 */
Lev Walkin8471cec2004-10-21 14:02:19 +0000413 b = (char *)MALLOC(chunk_size + 1);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000414 if(!b) return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000415 memcpy(b, chunk_buf, chunk_size);
Lev Walkin8471cec2004-10-21 14:02:19 +0000416 b[chunk_size] = 0; /* nul-terminate */
Lev Walkin5f560912004-10-21 13:37:57 +0000417
418 value = strtod(b, &endptr);
Lev Walkin419f6752006-09-13 04:02:00 +0000419 FREEMEM(b);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000420 if(endptr == b) return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000421
422 if(asn_double2REAL(st, value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000423 return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000424
Lev Walkin0fab1a62005-03-09 22:19:25 +0000425 return XPBD_BODY_CONSUMED;
Lev Walkin5f560912004-10-21 13:37:57 +0000426}
427
428asn_dec_rval_t
429REAL_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
430 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000431 const void *buf_ptr, size_t size) {
Lev Walkin5f560912004-10-21 13:37:57 +0000432
433 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000434 sptr, sizeof(REAL_t), opt_mname,
Lev Walkin5f560912004-10-21 13:37:57 +0000435 buf_ptr, size, REAL__xer_body_decode);
436}
437
Lev Walkin725883b2006-10-09 12:07:58 +0000438asn_dec_rval_t
Lev Walkin494fb702017-08-07 20:07:00 -0700439REAL_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
440 const asn_per_constraints_t *constraints, void **sptr,
441 asn_per_data_t *pd) {
442 (void)constraints; /* No PER visible constraints */
Lev Walkin725883b2006-10-09 12:07:58 +0000443 return OCTET_STRING_decode_uper(opt_codec_ctx, td, 0, sptr, pd);
444}
445
446asn_enc_rval_t
447REAL_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700448 const asn_per_constraints_t *constraints, void *sptr,
449 asn_per_outp_t *po) {
450 (void)constraints; /* No PER visible constraints */
Lev Walkin725883b2006-10-09 12:07:58 +0000451 return OCTET_STRING_encode_uper(td, 0, sptr, po);
452}
Lev Walkin5f560912004-10-21 13:37:57 +0000453
Lev Walkin41ba1f22004-09-14 12:46:35 +0000454int
Lev Walkin5e033762004-09-29 13:26:15 +0000455asn_REAL2double(const REAL_t *st, double *dbl_value) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000456 unsigned int octv;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000457
458 if(!st || !st->buf) {
459 errno = EINVAL;
460 return -1;
461 }
462
463 if(st->size == 0) {
464 *dbl_value = 0;
465 return 0;
466 }
467
468 octv = st->buf[0]; /* unsigned byte */
469
470 switch(octv & 0xC0) {
Lev Walkin5fda7d52012-01-09 03:20:19 -0800471 case 0x40: /* X.690: 8.5.6 a) => 8.5.9 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000472 /* "SpecialRealValue" */
473
474 /* Be liberal in what you accept...
Lev Walkin749916f2012-01-07 17:03:39 -0800475 * http://en.wikipedia.org/wiki/Robustness_principle
Lev Walkin41ba1f22004-09-14 12:46:35 +0000476 if(st->size != 1) ...
477 */
478
479 switch(st->buf[0]) {
480 case 0x40: /* 01000000: PLUS-INFINITY */
Lev Walkin1aea6982004-10-26 09:35:25 +0000481 *dbl_value = INFINITY;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000482 return 0;
483 case 0x41: /* 01000001: MINUS-INFINITY */
Lev Walkin1aea6982004-10-26 09:35:25 +0000484 *dbl_value = - INFINITY;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000485 return 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000486 case 0x42: /* 01000010: NOT-A-NUMBER */
487 *dbl_value = NAN;
488 return 0;
489 case 0x43: /* 01000011: minus zero */
Lev Walkin2a789d92004-09-27 21:36:59 +0000490 *dbl_value = -0.0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000491 return 0;
492 }
493
494 errno = EINVAL;
495 return -1;
Lev Walkin0959ffb2011-06-25 15:48:52 -0700496 case 0x00: { /* X.690: 8.5.7 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000497 /*
Lev Walkin5fda7d52012-01-09 03:20:19 -0800498 * Decimal. NR{1,2,3} format from ISO 6093.
499 * NR1: [ ]*[+-]?[0-9]+
500 * NR2: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)
501 * NR3: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)[Ee][+-]?[0-9]+
Lev Walkin41ba1f22004-09-14 12:46:35 +0000502 */
503 double d;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800504 char *buf;
505 char *endptr;
506 int used_malloc = 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000507
Lev Walkin5fda7d52012-01-09 03:20:19 -0800508 if(octv == 0 || (octv & 0x3C)) {
Lev Walkin0959ffb2011-06-25 15:48:52 -0700509 /* Remaining values of bits 6 to 1 are Reserved. */
510 errno = EINVAL;
511 return -1;
512 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000513
Lev Walkin5fda7d52012-01-09 03:20:19 -0800514
515 /* 1. By contract, an input buffer should be null-terminated.
516 * OCTET STRING decoder ensures that, as is asn_double2REAL().
517 * 2. ISO 6093 specifies COMMA as a possible decimal separator.
518 * However, strtod() can't always deal with COMMA.
519 * So her we fix both by reallocating, copying and fixing.
520 */
521 if(st->buf[st->size] || memchr(st->buf, ',', st->size)) {
522 uint8_t *p, *end;
523 char *b;
524 if(st->size > 100) {
525 /* Avoid malicious stack overflow in alloca() */
526 buf = (char *)MALLOC(st->size);
527 if(!buf) return -1;
528 used_malloc = 1;
529 } else {
530 buf = alloca(st->size);
531 }
532 b = buf;
533 /* Copy without the first byte and with 0-termination */
534 for(p = st->buf + 1, end = st->buf + st->size;
535 p < end; b++, p++)
536 *b = (*p == ',') ? '.' : *p;
537 *b = '\0';
538 } else {
539 buf = (char *)&st->buf[1];
540 }
541
542 endptr = buf;
543 d = strtod(buf, &endptr);
544 if(*endptr != '\0') {
545 /* Format is not consistent with ISO 6093 */
546 if(used_malloc) FREEMEM(buf);
Lev Walkin0959ffb2011-06-25 15:48:52 -0700547 errno = EINVAL;
548 return -1;
549 }
Lev Walkin5fda7d52012-01-09 03:20:19 -0800550 if(used_malloc) FREEMEM(buf);
Lev Walkindaeb2162014-01-13 23:08:35 -0800551 if(_asn_isfinite(d)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000552 *dbl_value = d;
553 return 0;
554 } else {
555 errno = ERANGE;
Lev Walkin0959ffb2011-06-25 15:48:52 -0700556 return -1;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000557 }
558 }
559 }
560
561 /*
562 * Binary representation.
563 */
564 {
565 double m;
566 int expval; /* exponent value */
567 unsigned int elen; /* exponent value length, in octets */
568 unsigned int scaleF;
569 unsigned int baseF;
570 uint8_t *ptr;
571 uint8_t *end;
572 int sign;
573
574 switch((octv & 0x30) >> 4) {
575 case 0x00: baseF = 1; break; /* base 2 */
576 case 0x01: baseF = 3; break; /* base 8 */
577 case 0x02: baseF = 4; break; /* base 16 */
578 default:
579 /* Reserved field, can't parse now. */
580 errno = EINVAL;
581 return -1;
582 }
583
584 sign = (octv & 0x40); /* bit 7 */
585 scaleF = (octv & 0x0C) >> 2; /* bits 4 to 3 */
586
Lev Walkin494fb702017-08-07 20:07:00 -0700587 if(st->size <= 1 + (octv & 0x03)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000588 errno = EINVAL;
589 return -1;
590 }
591
Lev Walkine78753d2007-06-23 17:02:00 +0000592 elen = (octv & 0x03); /* bits 2 to 1; 8.5.6.4 */
593 if(elen == 0x03) { /* bits 2 to 1 = 11; 8.5.6.4, case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000594 elen = st->buf[1]; /* unsigned binary number */
Lev Walkin494fb702017-08-07 20:07:00 -0700595 if(elen == 0 || st->size <= (2 + elen)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000596 errno = EINVAL;
597 return -1;
598 }
Lev Walkine78753d2007-06-23 17:02:00 +0000599 /* FIXME: verify constraints of case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000600 ptr = &st->buf[2];
601 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000602 ptr = &st->buf[1];
603 }
604
605 /* Fetch the multibyte exponent */
606 expval = (int)(*(int8_t *)ptr);
607 end = ptr + elen + 1;
608 for(ptr++; ptr < end; ptr++)
609 expval = (expval * 256) + *ptr;
610
611 m = 0.0; /* Initial mantissa value */
612
613 /* Okay, the exponent is here. Now, what about mantissa? */
614 end = st->buf + st->size;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800615 for(; ptr < end; ptr++)
616 m = ldexp(m, 8) + *ptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000617
Lev Walkin5f560912004-10-21 13:37:57 +0000618 if(0)
Lev Walkin5fda7d52012-01-09 03:20:19 -0800619 ASN_DEBUG("m=%.10f, scF=%d, bF=%d, expval=%d, ldexp()=%f, ldexp()=%f\n",
Lev Walkin41ba1f22004-09-14 12:46:35 +0000620 m, scaleF, baseF, expval,
621 ldexp(m, expval * baseF + scaleF),
Lev Walkin77aa90f2005-04-28 22:56:36 +0000622 ldexp(m, scaleF) * pow(pow(2, baseF), expval)
Lev Walkin41ba1f22004-09-14 12:46:35 +0000623 );
624
625 /*
626 * (S * N * 2^F) * B^E
627 * Essentially:
Lev Walkin77aa90f2005-04-28 22:56:36 +0000628 m = ldexp(m, scaleF) * pow(pow(2, base), expval);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000629 */
630 m = ldexp(m, expval * baseF + scaleF);
Lev Walkindaeb2162014-01-13 23:08:35 -0800631 if(_asn_isfinite(m)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000632 *dbl_value = sign ? -m : m;
633 } else {
634 errno = ERANGE;
635 return -1;
636 }
637
638 } /* if(binary_format) */
639
640 return 0;
641}
642
643/*
644 * Assume IEEE 754 floating point: standard 64 bit double.
645 * [1 bit sign] [11 bits exponent] [52 bits mantissa]
646 */
647int
Lev Walkin5e033762004-09-29 13:26:15 +0000648asn_double2REAL(REAL_t *st, double dbl_value) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000649#ifdef WORDS_BIGENDIAN /* Known to be big-endian */
650 int littleEndian = 0;
651#else /* need to test: have no explicit information */
652 unsigned int LE = 1;
653 int littleEndian = *(unsigned char *)&LE;
654#endif
655 uint8_t buf[16]; /* More than enough for 8-byte dbl_value */
656 uint8_t dscr[sizeof(dbl_value)]; /* double value scratch pad */
657 /* Assertion guards: won't even compile, if unexpected double size */
Lev Walkin50b0f6c2006-10-16 12:01:36 +0000658 char assertion_buffer1[9 - sizeof(dbl_value)] GCC_NOTUSED;
659 char assertion_buffer2[sizeof(dbl_value) - 7] GCC_NOTUSED;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000660 uint8_t *ptr = buf;
661 uint8_t *mstop; /* Last byte of mantissa */
662 unsigned int mval; /* Value of the last byte of mantissa */
663 unsigned int bmsign; /* binary mask with sign */
664 unsigned int buflen;
665 unsigned int accum;
666 int expval;
667
668 if(!st) {
669 errno = EINVAL;
670 return -1;
671 }
672
Lev Walkin057fb732004-09-14 13:58:10 +0000673 /*
674 * ilogb(+-0) returns -INT_MAX or INT_MIN (platform-dependent)
Lev Walkin2f505022005-07-01 08:28:18 +0000675 * ilogb(+-inf) returns INT_MAX, logb(+-inf) returns +inf
Lev Walkin2a789d92004-09-27 21:36:59 +0000676 * ilogb(NaN) returns INT_MIN or INT_MAX (platform-dependent)
Lev Walkin057fb732004-09-14 13:58:10 +0000677 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000678 expval = ilogb(dbl_value);
Lev Walkin2a789d92004-09-27 21:36:59 +0000679 if(expval <= -INT_MAX /* Also catches +-0 and maybe isnan() */
680 || expval == INT_MAX /* catches isfin() and maybe isnan() */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000681 ) {
682 if(!st->buf || st->size < 2) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000683 ptr = (uint8_t *)MALLOC(2);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000684 if(!ptr) return -1;
685 st->buf = ptr;
686 }
687 /* fpclassify(3) is not portable yet */
Lev Walkin2a789d92004-09-27 21:36:59 +0000688 if(isnan(dbl_value)) {
689 st->buf[0] = 0x42; /* NaN */
690 st->buf[1] = 0;
691 st->size = 1;
Lev Walkindaeb2162014-01-13 23:08:35 -0800692 } else if(!_asn_isfinite(dbl_value)) {
Lev Walkin7e03db92004-09-14 13:50:21 +0000693 if(copysign(1.0, dbl_value) < 0.0) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000694 st->buf[0] = 0x41; /* MINUS-INFINITY */
695 } else {
696 st->buf[0] = 0x40; /* PLUS-INFINITY */
697 }
698 st->buf[1] = 0;
699 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000700 } else {
Lev Walkin5fe72e92012-01-07 17:00:29 -0800701 if(copysign(1.0, dbl_value) >= 0.0) {
Lev Walkinc51e7d62004-09-27 22:16:18 +0000702 /* no content octets: positive zero */
703 st->buf[0] = 0; /* JIC */
704 st->size = 0;
Lev Walkin5fe72e92012-01-07 17:00:29 -0800705 } else {
706 /* Negative zero. #8.5.3, 8.5.9 */
707 st->buf[0] = 0x43;
708 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000709 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000710 }
711 return 0;
712 }
713
714 if(littleEndian) {
715 uint8_t *s = ((uint8_t *)&dbl_value) + sizeof(dbl_value) - 2;
Lev Walkin057fb732004-09-14 13:58:10 +0000716 uint8_t *start = ((uint8_t *)&dbl_value);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000717 uint8_t *d;
718
719 bmsign = 0x80 | ((s[1] >> 1) & 0x40); /* binary mask & - */
Lev Walkin057fb732004-09-14 13:58:10 +0000720 for(mstop = d = dscr; s >= start; d++, s--) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000721 *d = *s;
722 if(*d) mstop = d;
723 }
724 } else {
725 uint8_t *s = ((uint8_t *)&dbl_value) + 1;
726 uint8_t *end = ((uint8_t *)&dbl_value) + sizeof(double);
727 uint8_t *d;
728
729 bmsign = 0x80 | ((s[-1] >> 1) & 0x40); /* binary mask & - */
730 for(mstop = d = dscr; s < end; d++, s++) {
731 *d = *s;
732 if(*d) mstop = d;
733 }
734 }
735
736 /* Remove parts of the exponent, leave mantissa and explicit 1. */
737 dscr[0] = 0x10 | (dscr[0] & 0x0f);
738
739 /* Adjust exponent in a very unobvious way */
740 expval -= 8 * ((mstop - dscr) + 1) - 4;
741
742 /* This loop ensures DER conformance by forcing mantissa odd: 11.3.1 */
743 mval = *mstop;
744 if(mval && !(mval & 1)) {
745 unsigned int shift_count = 1;
746 unsigned int ishift;
747 uint8_t *mptr;
748
749 /*
750 * Figure out what needs to be done to make mantissa odd.
751 */
752 if(!(mval & 0x0f)) /* Speed-up a little */
753 shift_count = 4;
754 while(((mval >> shift_count) & 1) == 0)
755 shift_count++;
756
757 ishift = 8 - shift_count;
758 accum = 0;
759
760 /* Go over the buffer, shifting it shift_count bits right. */
761 for(mptr = dscr; mptr <= mstop; mptr++) {
762 mval = *mptr;
763 *mptr = accum | (mval >> shift_count);
764 accum = mval << ishift;
765 }
766
Lev Walkinb89b3402012-01-09 18:26:30 -0800767 /* Adjust exponent appropriately. */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000768 expval += shift_count;
769 }
770
771 if(expval < 0) {
772 if((expval >> 7) == -1) {
773 *ptr++ = bmsign | 0x00;
774 *ptr++ = expval;
775 } else if((expval >> 15) == -1) {
776 *ptr++ = bmsign | 0x01;
777 *ptr++ = expval >> 8;
778 *ptr++ = expval;
779 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000780 *ptr++ = bmsign | 0x02;
781 *ptr++ = expval >> 16;
782 *ptr++ = expval >> 8;
783 *ptr++ = expval;
784 }
785 } else if(expval <= 0x7f) {
786 *ptr++ = bmsign | 0x00;
787 *ptr++ = expval;
788 } else if(expval <= 0x7fff) {
789 *ptr++ = bmsign | 0x01;
790 *ptr++ = expval >> 8;
791 *ptr++ = expval;
792 } else {
793 assert(expval <= 0x7fffff);
794 *ptr++ = bmsign | 0x02;
795 *ptr++ = expval >> 16;
796 *ptr++ = expval >> 8;
797 *ptr++ = expval;
798 }
799
800 buflen = (mstop - dscr) + 1;
801 memcpy(ptr, dscr, buflen);
802 ptr += buflen;
803 buflen = ptr - buf;
804
Lev Walkinc17d90f2005-01-17 14:32:45 +0000805 ptr = (uint8_t *)MALLOC(buflen + 1);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000806 if(!ptr) return -1;
807
808 memcpy(ptr, buf, buflen);
809 buf[buflen] = 0; /* JIC */
810
811 if(st->buf) FREEMEM(st->buf);
812 st->buf = ptr;
813 st->size = buflen;
814
815 return 0;
816}