blob: 0bdeb82c0feba8b7afeee0c69afc6ba2919072ab [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,
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080048 ber_decode_primitive,
49 der_encode_primitive,
50 REAL_decode_xer,
51 REAL_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070052#ifdef ASN_DISABLE_OER_SUPPORT
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080053 0,
54 0,
Lev Walkincc159472017-07-06 08:26:36 -070055#else
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080056 0,
57 0,
Lev Walkincc159472017-07-06 08:26:36 -070058#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040059#ifdef ASN_DISABLE_PER_SUPPORT
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080060 0,
61 0,
Lev Walkinb33425f2017-07-14 14:59:52 +040062#else
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080063 REAL_decode_uper,
64 REAL_encode_uper,
Lev Walkinb33425f2017-07-14 14:59:52 +040065#endif /* ASN_DISABLE_PER_SUPPORT */
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080066 0 /* Use generic outmost tag fetcher */
67};
68asn_TYPE_descriptor_t asn_DEF_REAL = {
69 "REAL",
70 "REAL",
71 &asn_OP_REAL,
72 asn_generic_no_constraint,
73 asn_DEF_REAL_tags,
74 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
75 asn_DEF_REAL_tags, /* Same as above */
76 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
77 0, /* No OER visible constraints */
78 0, /* No PER visible constraints */
79 0,
80 0, /* No members */
81 0 /* No specifics */
Lev Walkin41ba1f22004-09-14 12:46:35 +000082};
83
Lev Walkin5f560912004-10-21 13:37:57 +000084typedef enum specialRealValue {
85 SRV__NOT_A_NUMBER,
86 SRV__MINUS_INFINITY,
87 SRV__PLUS_INFINITY
88} specialRealValue_e;
89static struct specialRealValue_s {
90 char *string;
Lev Walkin8471cec2004-10-21 14:02:19 +000091 size_t length;
Lev Walkin1aea6982004-10-26 09:35:25 +000092 long dv;
Lev Walkin5f560912004-10-21 13:37:57 +000093} specialRealValue[] = {
94#define SRV_SET(foo, val) { foo, sizeof(foo) - 1, val }
Lev Walkin1aea6982004-10-26 09:35:25 +000095 SRV_SET("<NOT-A-NUMBER/>", 0),
96 SRV_SET("<MINUS-INFINITY/>", -1),
97 SRV_SET("<PLUS-INFINITY/>", 1),
Lev Walkin5f560912004-10-21 13:37:57 +000098#undef SRV_SET
99};
100
Lev Walkina9cc46e2004-09-22 16:06:28 +0000101ssize_t
102REAL__dump(double d, int canonical, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin92b35d22004-09-27 20:52:18 +0000103 char local_buf[64];
Lev Walkina9cc46e2004-09-22 16:06:28 +0000104 char *buf = local_buf;
105 ssize_t buflen = sizeof(local_buf);
Lev Walkin92b35d22004-09-27 20:52:18 +0000106 const char *fmt = canonical?"%.15E":"%.15f";
Lev Walkina9cc46e2004-09-22 16:06:28 +0000107 ssize_t ret;
108
Lev Walkin8e8078a2004-09-26 13:10:40 +0000109 /*
110 * Check whether it is a special value.
111 */
Lev Walkinc51e7d62004-09-27 22:16:18 +0000112 /* fpclassify(3) is not portable yet */
113 if(isnan(d)) {
Lev Walkin5f560912004-10-21 13:37:57 +0000114 buf = specialRealValue[SRV__NOT_A_NUMBER].string;
115 buflen = specialRealValue[SRV__NOT_A_NUMBER].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000116 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
Lev Walkindaeb2162014-01-13 23:08:35 -0800117 } else if(!_asn_isfinite(d)) {
Lev Walkinc51e7d62004-09-27 22:16:18 +0000118 if(copysign(1.0, d) < 0.0) {
Lev Walkin5f560912004-10-21 13:37:57 +0000119 buf = specialRealValue[SRV__MINUS_INFINITY].string;
120 buflen = specialRealValue[SRV__MINUS_INFINITY].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000121 } else {
Lev Walkin5f560912004-10-21 13:37:57 +0000122 buf = specialRealValue[SRV__PLUS_INFINITY].string;
123 buflen = specialRealValue[SRV__PLUS_INFINITY].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000124 }
125 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
126 } else if(ilogb(d) <= -INT_MAX) {
127 if(copysign(1.0, d) < 0.0) {
128 buf = "-0";
129 buflen = 2;
130 } else {
131 buf = "0";
132 buflen = 1;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000133 }
134 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
135 }
136
137 /*
138 * Use the libc's double printing, hopefully they got it right.
139 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000140 do {
141 ret = snprintf(buf, buflen, fmt, d);
142 if(ret < 0) {
143 buflen <<= 1;
144 } else if(ret >= buflen) {
145 buflen = ret + 1;
146 } else {
147 buflen = ret;
148 break;
149 }
Lev Walkin419f6752006-09-13 04:02:00 +0000150 if(buf != local_buf) FREEMEM(buf);
Lev Walkin8e8078a2004-09-26 13:10:40 +0000151 buf = (char *)MALLOC(buflen);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000152 if(!buf) return -1;
153 } while(1);
154
Lev Walkina9cc46e2004-09-22 16:06:28 +0000155 if(canonical) {
Lev Walkin92b35d22004-09-27 20:52:18 +0000156 /*
157 * Transform the "[-]d.dddE+-dd" output into "[-]d.dddE[-]d"
Lev Walkin5f560912004-10-21 13:37:57 +0000158 * Check that snprintf() constructed the output correctly.
Lev Walkin92b35d22004-09-27 20:52:18 +0000159 */
Lev Walkin8ca13c82016-01-24 22:40:00 -0800160 char *dot;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000161 char *end = buf + buflen;
Lev Walkin92b35d22004-09-27 20:52:18 +0000162 char *last_zero;
Lev Walkin8ca13c82016-01-24 22:40:00 -0800163 char *prev_zero;
164 char *s;
165
166 enum {
167 LZSTATE_NOTHING,
168 LZSTATE_SEEN_ZERO
169 } lz_state = LZSTATE_NOTHING;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000170
Lev Walkin5f560912004-10-21 13:37:57 +0000171 dot = (buf[0] == 0x2d /* '-' */) ? (buf + 2) : (buf + 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000172 if(*dot >= 0x30) {
Lev Walkin78d917d2012-01-07 15:29:08 -0800173 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000174 errno = EINVAL;
175 return -1; /* Not a dot, really */
176 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000177 *dot = 0x2e; /* Replace possible comma */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000178
Lev Walkin8ca13c82016-01-24 22:40:00 -0800179 for(prev_zero = last_zero = s = dot + 2; s < end; s++) {
180 switch(*s) {
181 case 0x45: /* 'E' */
182 if(lz_state == LZSTATE_SEEN_ZERO)
183 last_zero = prev_zero;
184 break;
185 case 0x30: /* '0' */
186 if(lz_state == LZSTATE_NOTHING)
187 prev_zero = s;
188 lz_state = LZSTATE_SEEN_ZERO;
189 continue;
190 default:
191 lz_state = LZSTATE_NOTHING;
192 continue;
193 }
194 break;
195 }
196
197 if(s == end) {
Lev Walkin78d917d2012-01-07 15:29:08 -0800198 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000199 errno = EINVAL;
200 return -1; /* No promised E */
201 }
Lev Walkin8ca13c82016-01-24 22:40:00 -0800202
203 assert(*s == 0x45);
204 {
205 char *E = s;
206 char *expptr = ++E;
207 char *s = expptr;
208 int sign;
209
210 if(*expptr == 0x2b /* '+' */) {
211 /* Skip the "+" */
212 buflen -= 1;
213 sign = 0;
214 } else {
215 sign = 1;
216 s++;
217 }
218 expptr++;
219 if(expptr > end) {
220 if(buf != local_buf) FREEMEM(buf);
221 errno = EINVAL;
222 return -1;
223 }
224 if(*expptr == 0x30) {
225 buflen--;
226 expptr++;
227 }
228 if(*last_zero == 0x30) {
229 *last_zero = 0x45; /* E */
230 buflen -= s - (last_zero + 1);
231 s = last_zero + 1;
232 if(sign) {
233 *s++ = 0x2d /* '-' */;
234 buflen++;
235 }
236 }
237 for(; expptr <= end; s++, expptr++)
238 *s = *expptr;
239 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000240 } else {
241 /*
242 * Remove trailing zeros.
243 */
244 char *end = buf + buflen;
245 char *last_zero = end;
Lev Walkinf0b808d2005-04-25 21:08:25 +0000246 int stoplooking = 0;
Lev Walkin92b35d22004-09-27 20:52:18 +0000247 char *z;
248 for(z = end - 1; z > buf; z--) {
249 switch(*z) {
Lev Walkinf0b808d2005-04-25 21:08:25 +0000250 case 0x30:
251 if(!stoplooking)
252 last_zero = z;
253 continue;
Lev Walkin92b35d22004-09-27 20:52:18 +0000254 case 0x31: case 0x32: case 0x33: case 0x34:
255 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinf0b808d2005-04-25 21:08:25 +0000256 stoplooking = 1;
Lev Walkin92b35d22004-09-27 20:52:18 +0000257 continue;
258 default: /* Catch dot and other separators */
Lev Walkinf0b808d2005-04-25 21:08:25 +0000259 /*
260 * Replace possible comma (which may even
261 * be not a comma at all: locale-defined).
262 */
263 *z = 0x2e;
Lev Walkin92b35d22004-09-27 20:52:18 +0000264 if(last_zero == z + 1) { /* leave x.0 */
265 last_zero++;
266 }
267 buflen = last_zero - buf;
268 *last_zero = '\0';
269 break;
270 }
271 break;
272 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000273 }
274
275 ret = cb(buf, buflen, app_key);
Lev Walkin419f6752006-09-13 04:02:00 +0000276 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000277 return (ret < 0) ? -1 : buflen;
278}
279
Lev Walkin41ba1f22004-09-14 12:46:35 +0000280int
Lev Walkin5e033762004-09-29 13:26:15 +0000281REAL_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000282 asn_app_consume_bytes_f *cb, void *app_key) {
283 const REAL_t *st = (const REAL_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000284 ssize_t ret;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000285 double d;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000286
287 (void)td; /* Unused argument */
288 (void)ilevel; /* Unused argument */
289
Lev Walkina9cc46e2004-09-22 16:06:28 +0000290 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000291 ret = cb("<absent>", 8, app_key);
Lev Walkin5e033762004-09-29 13:26:15 +0000292 else if(asn_REAL2double(st, &d))
Lev Walkin8e8078a2004-09-26 13:10:40 +0000293 ret = cb("<error>", 7, app_key);
294 else
295 ret = REAL__dump(d, 0, cb, app_key);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000296
Lev Walkin8e8078a2004-09-26 13:10:40 +0000297 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000298}
Lev Walkin41ba1f22004-09-14 12:46:35 +0000299
Lev Walkincd2f48e2017-08-10 02:14:59 -0700300int
301REAL_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
302 const void *bptr) {
303 const REAL_t *a = aptr;
304 const REAL_t *b = bptr;
305
306 (void)td;
307
308 if(a && b) {
309 double adbl, bdbl;
310 int ra, rb;
311 ra = asn_REAL2double(a, &adbl);
312 rb = asn_REAL2double(b, &bdbl);
313 if(ra == 0 && rb == 0) {
314 if(isnan(adbl)) {
315 if(isnan(bdbl)) {
316 return 0;
317 } else {
318 return -1;
319 }
320 } else if(isnan(bdbl)) {
321 return 1;
322 }
323 /* Value comparison. */
324 if(adbl < bdbl) {
325 return -1;
326 } else if(adbl > bdbl) {
327 return 1;
328 } else {
329 return 0;
330 }
331 } else if(ra) {
332 return -1;
333 } else {
334 return 1;
335 }
336 } else if(!a) {
337 return -1;
338 } else {
339 return 1;
340 }
341}
342
Lev Walkina9cc46e2004-09-22 16:06:28 +0000343asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000344REAL_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000345 int ilevel, enum xer_encoder_flags_e flags,
346 asn_app_consume_bytes_f *cb, void *app_key) {
347 REAL_t *st = (REAL_t *)sptr;
348 asn_enc_rval_t er;
349 double d;
350
351 (void)ilevel;
352
Lev Walkin5e033762004-09-29 13:26:15 +0000353 if(!st || !st->buf || asn_REAL2double(st, &d))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700354 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000355
356 er.encoded = REAL__dump(d, flags & XER_F_CANONICAL, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700357 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000358
Lev Walkin7c1dc052016-03-14 03:08:15 -0700359 ASN__ENCODED_OK(er);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000360}
361
Lev Walkin5f560912004-10-21 13:37:57 +0000362
363/*
364 * Decode the chunk of XML text encoding REAL.
365 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000366static enum xer_pbd_rval
367REAL__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 +0000368 REAL_t *st = (REAL_t *)sptr;
Lev Walkin5f560912004-10-21 13:37:57 +0000369 double value;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000370 const char *xerdata = (const char *)chunk_buf;
Lev Walkin5f560912004-10-21 13:37:57 +0000371 char *endptr = 0;
372 char *b;
373
Lev Walkine0b56e02005-02-25 12:10:27 +0000374 (void)td;
375
Lev Walkin0fab1a62005-03-09 22:19:25 +0000376 if(!chunk_size) return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000377
378 /*
379 * Decode an XMLSpecialRealValue: <MINUS-INFINITY>, etc.
380 */
381 if(xerdata[0] == 0x3c /* '<' */) {
382 size_t i;
383 for(i = 0; i < sizeof(specialRealValue)
384 / sizeof(specialRealValue[0]); i++) {
385 struct specialRealValue_s *srv = &specialRealValue[i];
Lev Walkin1aea6982004-10-26 09:35:25 +0000386 double dv;
387
Lev Walkin5f560912004-10-21 13:37:57 +0000388 if(srv->length != chunk_size
389 || memcmp(srv->string, chunk_buf, chunk_size))
390 continue;
391
Lev Walkin1aea6982004-10-26 09:35:25 +0000392 /*
393 * It could've been done using
394 * (double)srv->dv / real_zero,
395 * but it summons fp exception on some platforms.
396 */
397 switch(srv->dv) {
398 case -1: dv = - INFINITY; break;
399 case 0: dv = NAN; break;
400 case 1: dv = INFINITY; break;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000401 default: return XPBD_SYSTEM_FAILURE;
Lev Walkin1aea6982004-10-26 09:35:25 +0000402 }
403
Lev Walkin0fab1a62005-03-09 22:19:25 +0000404 if(asn_double2REAL(st, dv))
405 return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000406
Lev Walkin0fab1a62005-03-09 22:19:25 +0000407 return XPBD_BODY_CONSUMED;
Lev Walkin5f560912004-10-21 13:37:57 +0000408 }
409 ASN_DEBUG("Unknown XMLSpecialRealValue");
Lev Walkin0fab1a62005-03-09 22:19:25 +0000410 return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000411 }
412
413 /*
414 * Copy chunk into the nul-terminated string, and run strtod.
415 */
Lev Walkin8471cec2004-10-21 14:02:19 +0000416 b = (char *)MALLOC(chunk_size + 1);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000417 if(!b) return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000418 memcpy(b, chunk_buf, chunk_size);
Lev Walkin8471cec2004-10-21 14:02:19 +0000419 b[chunk_size] = 0; /* nul-terminate */
Lev Walkin5f560912004-10-21 13:37:57 +0000420
421 value = strtod(b, &endptr);
Lev Walkin419f6752006-09-13 04:02:00 +0000422 FREEMEM(b);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000423 if(endptr == b) return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000424
425 if(asn_double2REAL(st, value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000426 return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000427
Lev Walkin0fab1a62005-03-09 22:19:25 +0000428 return XPBD_BODY_CONSUMED;
Lev Walkin5f560912004-10-21 13:37:57 +0000429}
430
431asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700432REAL_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin5f560912004-10-21 13:37:57 +0000433 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000434 const void *buf_ptr, size_t size) {
Lev Walkin5f560912004-10-21 13:37:57 +0000435
436 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000437 sptr, sizeof(REAL_t), opt_mname,
Lev Walkin5f560912004-10-21 13:37:57 +0000438 buf_ptr, size, REAL__xer_body_decode);
439}
440
Lev Walkin41ba1f22004-09-14 12:46:35 +0000441int
Lev Walkin5e033762004-09-29 13:26:15 +0000442asn_REAL2double(const REAL_t *st, double *dbl_value) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000443 unsigned int octv;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000444
445 if(!st || !st->buf) {
446 errno = EINVAL;
447 return -1;
448 }
449
450 if(st->size == 0) {
451 *dbl_value = 0;
452 return 0;
453 }
454
455 octv = st->buf[0]; /* unsigned byte */
456
457 switch(octv & 0xC0) {
Lev Walkin5fda7d52012-01-09 03:20:19 -0800458 case 0x40: /* X.690: 8.5.6 a) => 8.5.9 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000459 /* "SpecialRealValue" */
460
461 /* Be liberal in what you accept...
Lev Walkin749916f2012-01-07 17:03:39 -0800462 * http://en.wikipedia.org/wiki/Robustness_principle
Lev Walkin41ba1f22004-09-14 12:46:35 +0000463 if(st->size != 1) ...
464 */
465
466 switch(st->buf[0]) {
467 case 0x40: /* 01000000: PLUS-INFINITY */
Lev Walkin1aea6982004-10-26 09:35:25 +0000468 *dbl_value = INFINITY;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000469 return 0;
470 case 0x41: /* 01000001: MINUS-INFINITY */
Lev Walkin1aea6982004-10-26 09:35:25 +0000471 *dbl_value = - INFINITY;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000472 return 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000473 case 0x42: /* 01000010: NOT-A-NUMBER */
474 *dbl_value = NAN;
475 return 0;
476 case 0x43: /* 01000011: minus zero */
Lev Walkin2a789d92004-09-27 21:36:59 +0000477 *dbl_value = -0.0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000478 return 0;
479 }
480
481 errno = EINVAL;
482 return -1;
Lev Walkin0959ffb2011-06-25 15:48:52 -0700483 case 0x00: { /* X.690: 8.5.7 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000484 /*
Lev Walkin5fda7d52012-01-09 03:20:19 -0800485 * Decimal. NR{1,2,3} format from ISO 6093.
486 * NR1: [ ]*[+-]?[0-9]+
487 * NR2: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)
488 * NR3: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)[Ee][+-]?[0-9]+
Lev Walkin41ba1f22004-09-14 12:46:35 +0000489 */
490 double d;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800491 char *buf;
492 char *endptr;
493 int used_malloc = 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000494
Lev Walkin5fda7d52012-01-09 03:20:19 -0800495 if(octv == 0 || (octv & 0x3C)) {
Lev Walkin0959ffb2011-06-25 15:48:52 -0700496 /* Remaining values of bits 6 to 1 are Reserved. */
497 errno = EINVAL;
498 return -1;
499 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000500
Lev Walkin5fda7d52012-01-09 03:20:19 -0800501
Lev Walkin53e5ae62017-08-23 10:56:19 -0700502 /* 1. By contract, an input buffer should be '\0'-terminated.
Lev Walkin5fda7d52012-01-09 03:20:19 -0800503 * OCTET STRING decoder ensures that, as is asn_double2REAL().
504 * 2. ISO 6093 specifies COMMA as a possible decimal separator.
505 * However, strtod() can't always deal with COMMA.
506 * So her we fix both by reallocating, copying and fixing.
507 */
Lev Walkin53e5ae62017-08-23 10:56:19 -0700508 if(st->buf[st->size] != '\0' || memchr(st->buf, ',', st->size)) {
Lev Walkin5fda7d52012-01-09 03:20:19 -0800509 uint8_t *p, *end;
510 char *b;
511 if(st->size > 100) {
512 /* Avoid malicious stack overflow in alloca() */
513 buf = (char *)MALLOC(st->size);
514 if(!buf) return -1;
515 used_malloc = 1;
516 } else {
517 buf = alloca(st->size);
518 }
519 b = buf;
520 /* Copy without the first byte and with 0-termination */
521 for(p = st->buf + 1, end = st->buf + st->size;
522 p < end; b++, p++)
523 *b = (*p == ',') ? '.' : *p;
524 *b = '\0';
525 } else {
526 buf = (char *)&st->buf[1];
527 }
528
529 endptr = buf;
530 d = strtod(buf, &endptr);
531 if(*endptr != '\0') {
532 /* Format is not consistent with ISO 6093 */
533 if(used_malloc) FREEMEM(buf);
Lev Walkin0959ffb2011-06-25 15:48:52 -0700534 errno = EINVAL;
535 return -1;
536 }
Lev Walkin5fda7d52012-01-09 03:20:19 -0800537 if(used_malloc) FREEMEM(buf);
Lev Walkindaeb2162014-01-13 23:08:35 -0800538 if(_asn_isfinite(d)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000539 *dbl_value = d;
540 return 0;
541 } else {
542 errno = ERANGE;
Lev Walkin0959ffb2011-06-25 15:48:52 -0700543 return -1;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000544 }
545 }
546 }
547
548 /*
549 * Binary representation.
550 */
551 {
552 double m;
553 int expval; /* exponent value */
554 unsigned int elen; /* exponent value length, in octets */
555 unsigned int scaleF;
556 unsigned int baseF;
557 uint8_t *ptr;
558 uint8_t *end;
559 int sign;
560
561 switch((octv & 0x30) >> 4) {
562 case 0x00: baseF = 1; break; /* base 2 */
563 case 0x01: baseF = 3; break; /* base 8 */
564 case 0x02: baseF = 4; break; /* base 16 */
565 default:
566 /* Reserved field, can't parse now. */
567 errno = EINVAL;
568 return -1;
569 }
570
571 sign = (octv & 0x40); /* bit 7 */
572 scaleF = (octv & 0x0C) >> 2; /* bits 4 to 3 */
573
Lev Walkin494fb702017-08-07 20:07:00 -0700574 if(st->size <= 1 + (octv & 0x03)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000575 errno = EINVAL;
576 return -1;
577 }
578
Lev Walkine78753d2007-06-23 17:02:00 +0000579 elen = (octv & 0x03); /* bits 2 to 1; 8.5.6.4 */
580 if(elen == 0x03) { /* bits 2 to 1 = 11; 8.5.6.4, case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000581 elen = st->buf[1]; /* unsigned binary number */
Lev Walkin494fb702017-08-07 20:07:00 -0700582 if(elen == 0 || st->size <= (2 + elen)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000583 errno = EINVAL;
584 return -1;
585 }
Lev Walkine78753d2007-06-23 17:02:00 +0000586 /* FIXME: verify constraints of case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000587 ptr = &st->buf[2];
588 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000589 ptr = &st->buf[1];
590 }
591
592 /* Fetch the multibyte exponent */
593 expval = (int)(*(int8_t *)ptr);
594 end = ptr + elen + 1;
595 for(ptr++; ptr < end; ptr++)
596 expval = (expval * 256) + *ptr;
597
598 m = 0.0; /* Initial mantissa value */
599
600 /* Okay, the exponent is here. Now, what about mantissa? */
601 end = st->buf + st->size;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800602 for(; ptr < end; ptr++)
603 m = ldexp(m, 8) + *ptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000604
Lev Walkin5f560912004-10-21 13:37:57 +0000605 if(0)
Lev Walkin5fda7d52012-01-09 03:20:19 -0800606 ASN_DEBUG("m=%.10f, scF=%d, bF=%d, expval=%d, ldexp()=%f, ldexp()=%f\n",
Lev Walkin41ba1f22004-09-14 12:46:35 +0000607 m, scaleF, baseF, expval,
608 ldexp(m, expval * baseF + scaleF),
Lev Walkin77aa90f2005-04-28 22:56:36 +0000609 ldexp(m, scaleF) * pow(pow(2, baseF), expval)
Lev Walkin41ba1f22004-09-14 12:46:35 +0000610 );
611
612 /*
613 * (S * N * 2^F) * B^E
614 * Essentially:
Lev Walkin77aa90f2005-04-28 22:56:36 +0000615 m = ldexp(m, scaleF) * pow(pow(2, base), expval);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000616 */
617 m = ldexp(m, expval * baseF + scaleF);
Lev Walkindaeb2162014-01-13 23:08:35 -0800618 if(_asn_isfinite(m)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000619 *dbl_value = sign ? -m : m;
620 } else {
621 errno = ERANGE;
622 return -1;
623 }
624
625 } /* if(binary_format) */
626
627 return 0;
628}
629
630/*
631 * Assume IEEE 754 floating point: standard 64 bit double.
632 * [1 bit sign] [11 bits exponent] [52 bits mantissa]
633 */
634int
Lev Walkin5e033762004-09-29 13:26:15 +0000635asn_double2REAL(REAL_t *st, double dbl_value) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000636#ifdef WORDS_BIGENDIAN /* Known to be big-endian */
637 int littleEndian = 0;
638#else /* need to test: have no explicit information */
639 unsigned int LE = 1;
640 int littleEndian = *(unsigned char *)&LE;
641#endif
642 uint8_t buf[16]; /* More than enough for 8-byte dbl_value */
643 uint8_t dscr[sizeof(dbl_value)]; /* double value scratch pad */
644 /* Assertion guards: won't even compile, if unexpected double size */
Lev Walkin50b0f6c2006-10-16 12:01:36 +0000645 char assertion_buffer1[9 - sizeof(dbl_value)] GCC_NOTUSED;
646 char assertion_buffer2[sizeof(dbl_value) - 7] GCC_NOTUSED;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000647 uint8_t *ptr = buf;
648 uint8_t *mstop; /* Last byte of mantissa */
649 unsigned int mval; /* Value of the last byte of mantissa */
650 unsigned int bmsign; /* binary mask with sign */
651 unsigned int buflen;
652 unsigned int accum;
653 int expval;
654
655 if(!st) {
656 errno = EINVAL;
657 return -1;
658 }
659
Lev Walkin057fb732004-09-14 13:58:10 +0000660 /*
661 * ilogb(+-0) returns -INT_MAX or INT_MIN (platform-dependent)
Lev Walkin2f505022005-07-01 08:28:18 +0000662 * ilogb(+-inf) returns INT_MAX, logb(+-inf) returns +inf
Lev Walkin2a789d92004-09-27 21:36:59 +0000663 * ilogb(NaN) returns INT_MIN or INT_MAX (platform-dependent)
Lev Walkin057fb732004-09-14 13:58:10 +0000664 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000665 expval = ilogb(dbl_value);
Lev Walkin2a789d92004-09-27 21:36:59 +0000666 if(expval <= -INT_MAX /* Also catches +-0 and maybe isnan() */
667 || expval == INT_MAX /* catches isfin() and maybe isnan() */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000668 ) {
669 if(!st->buf || st->size < 2) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000670 ptr = (uint8_t *)MALLOC(2);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000671 if(!ptr) return -1;
672 st->buf = ptr;
673 }
674 /* fpclassify(3) is not portable yet */
Lev Walkin2a789d92004-09-27 21:36:59 +0000675 if(isnan(dbl_value)) {
676 st->buf[0] = 0x42; /* NaN */
677 st->buf[1] = 0;
678 st->size = 1;
Lev Walkindaeb2162014-01-13 23:08:35 -0800679 } else if(!_asn_isfinite(dbl_value)) {
Lev Walkin7e03db92004-09-14 13:50:21 +0000680 if(copysign(1.0, dbl_value) < 0.0) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000681 st->buf[0] = 0x41; /* MINUS-INFINITY */
682 } else {
683 st->buf[0] = 0x40; /* PLUS-INFINITY */
684 }
685 st->buf[1] = 0;
686 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000687 } else {
Lev Walkin5fe72e92012-01-07 17:00:29 -0800688 if(copysign(1.0, dbl_value) >= 0.0) {
Lev Walkinc51e7d62004-09-27 22:16:18 +0000689 /* no content octets: positive zero */
690 st->buf[0] = 0; /* JIC */
691 st->size = 0;
Lev Walkin5fe72e92012-01-07 17:00:29 -0800692 } else {
693 /* Negative zero. #8.5.3, 8.5.9 */
694 st->buf[0] = 0x43;
695 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000696 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000697 }
698 return 0;
699 }
700
701 if(littleEndian) {
702 uint8_t *s = ((uint8_t *)&dbl_value) + sizeof(dbl_value) - 2;
Lev Walkin057fb732004-09-14 13:58:10 +0000703 uint8_t *start = ((uint8_t *)&dbl_value);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000704 uint8_t *d;
705
706 bmsign = 0x80 | ((s[1] >> 1) & 0x40); /* binary mask & - */
Lev Walkin057fb732004-09-14 13:58:10 +0000707 for(mstop = d = dscr; s >= start; d++, s--) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000708 *d = *s;
709 if(*d) mstop = d;
710 }
711 } else {
712 uint8_t *s = ((uint8_t *)&dbl_value) + 1;
713 uint8_t *end = ((uint8_t *)&dbl_value) + sizeof(double);
714 uint8_t *d;
715
716 bmsign = 0x80 | ((s[-1] >> 1) & 0x40); /* binary mask & - */
717 for(mstop = d = dscr; s < end; d++, s++) {
718 *d = *s;
719 if(*d) mstop = d;
720 }
721 }
722
723 /* Remove parts of the exponent, leave mantissa and explicit 1. */
724 dscr[0] = 0x10 | (dscr[0] & 0x0f);
725
726 /* Adjust exponent in a very unobvious way */
727 expval -= 8 * ((mstop - dscr) + 1) - 4;
728
729 /* This loop ensures DER conformance by forcing mantissa odd: 11.3.1 */
730 mval = *mstop;
731 if(mval && !(mval & 1)) {
732 unsigned int shift_count = 1;
733 unsigned int ishift;
734 uint8_t *mptr;
735
736 /*
737 * Figure out what needs to be done to make mantissa odd.
738 */
739 if(!(mval & 0x0f)) /* Speed-up a little */
740 shift_count = 4;
741 while(((mval >> shift_count) & 1) == 0)
742 shift_count++;
743
744 ishift = 8 - shift_count;
745 accum = 0;
746
747 /* Go over the buffer, shifting it shift_count bits right. */
748 for(mptr = dscr; mptr <= mstop; mptr++) {
749 mval = *mptr;
750 *mptr = accum | (mval >> shift_count);
751 accum = mval << ishift;
752 }
753
Lev Walkinb89b3402012-01-09 18:26:30 -0800754 /* Adjust exponent appropriately. */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000755 expval += shift_count;
756 }
757
758 if(expval < 0) {
759 if((expval >> 7) == -1) {
760 *ptr++ = bmsign | 0x00;
761 *ptr++ = expval;
762 } else if((expval >> 15) == -1) {
763 *ptr++ = bmsign | 0x01;
764 *ptr++ = expval >> 8;
765 *ptr++ = expval;
766 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000767 *ptr++ = bmsign | 0x02;
768 *ptr++ = expval >> 16;
769 *ptr++ = expval >> 8;
770 *ptr++ = expval;
771 }
772 } else if(expval <= 0x7f) {
773 *ptr++ = bmsign | 0x00;
774 *ptr++ = expval;
775 } else if(expval <= 0x7fff) {
776 *ptr++ = bmsign | 0x01;
777 *ptr++ = expval >> 8;
778 *ptr++ = expval;
779 } else {
780 assert(expval <= 0x7fffff);
781 *ptr++ = bmsign | 0x02;
782 *ptr++ = expval >> 16;
783 *ptr++ = expval >> 8;
784 *ptr++ = expval;
785 }
786
787 buflen = (mstop - dscr) + 1;
788 memcpy(ptr, dscr, buflen);
789 ptr += buflen;
790 buflen = ptr - buf;
791
Lev Walkinc17d90f2005-01-17 14:32:45 +0000792 ptr = (uint8_t *)MALLOC(buflen + 1);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000793 if(!ptr) return -1;
794
795 memcpy(ptr, buf, buflen);
796 buf[buflen] = 0; /* JIC */
797
798 if(st->buf) FREEMEM(st->buf);
799 st->buf = ptr;
800 st->size = buflen;
801
802 return 0;
803}
Lev Walkin41d972b2017-08-23 23:30:59 -0700804
805#ifndef ASN_DISABLE_PER_SUPPORT
806
807asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700808REAL_decode_uper(const asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
Lev Walkin41d972b2017-08-23 23:30:59 -0700809 const asn_per_constraints_t *constraints, void **sptr,
810 asn_per_data_t *pd) {
811 (void)constraints; /* No PER visible constraints */
812 return OCTET_STRING_decode_uper(opt_codec_ctx, td, 0, sptr, pd);
813}
814
815asn_enc_rval_t
816REAL_encode_uper(asn_TYPE_descriptor_t *td,
817 const asn_per_constraints_t *constraints, void *sptr,
818 asn_per_outp_t *po) {
819 (void)constraints; /* No PER visible constraints */
820 return OCTET_STRING_encode_uper(td, 0, sptr, po);
821}
822
823#endif /* ASN_DISABLE_PER_SUPPORT */