blob: 4761b6da614b5273575266e96ff5e3455fe62b05 [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 Walkin41ba1f22004-09-14 12:46:35 +000045 "REAL",
Lev Walkindc06f6b2004-10-20 15:50:55 +000046 "REAL",
Lev Walkin8e8078a2004-09-26 13:10:40 +000047 ASN__PRIMITIVE_TYPE_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000048 REAL_print,
Lev Walkin41ba1f22004-09-14 12:46:35 +000049 asn_generic_no_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000050 ber_decode_primitive,
51 der_encode_primitive,
Lev Walkin5f560912004-10-21 13:37:57 +000052 REAL_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000053 REAL_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070054#ifdef ASN_DISABLE_OER_SUPPORT
55 0,
56 0,
57#else
58 0,
59 0,
60#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040061#ifdef ASN_DISABLE_PER_SUPPORT
62 0,
63 0,
64#else
65 REAL_decode_uper,
66 REAL_encode_uper,
67#endif /* ASN_DISABLE_PER_SUPPORT */
Lev Walkin41ba1f22004-09-14 12:46:35 +000068 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000069 asn_DEF_REAL_tags,
70 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
71 asn_DEF_REAL_tags, /* Same as above */
72 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
Lev Walkin76780762017-07-07 10:07:30 -070073 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000074 0, /* No PER visible constraints */
Lev Walkin41ba1f22004-09-14 12:46:35 +000075 0, 0, /* No members */
76 0 /* No specifics */
77};
78
Lev Walkin5f560912004-10-21 13:37:57 +000079typedef enum specialRealValue {
80 SRV__NOT_A_NUMBER,
81 SRV__MINUS_INFINITY,
82 SRV__PLUS_INFINITY
83} specialRealValue_e;
84static struct specialRealValue_s {
85 char *string;
Lev Walkin8471cec2004-10-21 14:02:19 +000086 size_t length;
Lev Walkin1aea6982004-10-26 09:35:25 +000087 long dv;
Lev Walkin5f560912004-10-21 13:37:57 +000088} specialRealValue[] = {
89#define SRV_SET(foo, val) { foo, sizeof(foo) - 1, val }
Lev Walkin1aea6982004-10-26 09:35:25 +000090 SRV_SET("<NOT-A-NUMBER/>", 0),
91 SRV_SET("<MINUS-INFINITY/>", -1),
92 SRV_SET("<PLUS-INFINITY/>", 1),
Lev Walkin5f560912004-10-21 13:37:57 +000093#undef SRV_SET
94};
95
Lev Walkina9cc46e2004-09-22 16:06:28 +000096ssize_t
97REAL__dump(double d, int canonical, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin92b35d22004-09-27 20:52:18 +000098 char local_buf[64];
Lev Walkina9cc46e2004-09-22 16:06:28 +000099 char *buf = local_buf;
100 ssize_t buflen = sizeof(local_buf);
Lev Walkin92b35d22004-09-27 20:52:18 +0000101 const char *fmt = canonical?"%.15E":"%.15f";
Lev Walkina9cc46e2004-09-22 16:06:28 +0000102 ssize_t ret;
103
Lev Walkin8e8078a2004-09-26 13:10:40 +0000104 /*
105 * Check whether it is a special value.
106 */
Lev Walkinc51e7d62004-09-27 22:16:18 +0000107 /* fpclassify(3) is not portable yet */
108 if(isnan(d)) {
Lev Walkin5f560912004-10-21 13:37:57 +0000109 buf = specialRealValue[SRV__NOT_A_NUMBER].string;
110 buflen = specialRealValue[SRV__NOT_A_NUMBER].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000111 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
Lev Walkindaeb2162014-01-13 23:08:35 -0800112 } else if(!_asn_isfinite(d)) {
Lev Walkinc51e7d62004-09-27 22:16:18 +0000113 if(copysign(1.0, d) < 0.0) {
Lev Walkin5f560912004-10-21 13:37:57 +0000114 buf = specialRealValue[SRV__MINUS_INFINITY].string;
115 buflen = specialRealValue[SRV__MINUS_INFINITY].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000116 } else {
Lev Walkin5f560912004-10-21 13:37:57 +0000117 buf = specialRealValue[SRV__PLUS_INFINITY].string;
118 buflen = specialRealValue[SRV__PLUS_INFINITY].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000119 }
120 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
121 } else if(ilogb(d) <= -INT_MAX) {
122 if(copysign(1.0, d) < 0.0) {
123 buf = "-0";
124 buflen = 2;
125 } else {
126 buf = "0";
127 buflen = 1;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000128 }
129 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
130 }
131
132 /*
133 * Use the libc's double printing, hopefully they got it right.
134 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000135 do {
136 ret = snprintf(buf, buflen, fmt, d);
137 if(ret < 0) {
138 buflen <<= 1;
139 } else if(ret >= buflen) {
140 buflen = ret + 1;
141 } else {
142 buflen = ret;
143 break;
144 }
Lev Walkin419f6752006-09-13 04:02:00 +0000145 if(buf != local_buf) FREEMEM(buf);
Lev Walkin8e8078a2004-09-26 13:10:40 +0000146 buf = (char *)MALLOC(buflen);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000147 if(!buf) return -1;
148 } while(1);
149
Lev Walkina9cc46e2004-09-22 16:06:28 +0000150 if(canonical) {
Lev Walkin92b35d22004-09-27 20:52:18 +0000151 /*
152 * Transform the "[-]d.dddE+-dd" output into "[-]d.dddE[-]d"
Lev Walkin5f560912004-10-21 13:37:57 +0000153 * Check that snprintf() constructed the output correctly.
Lev Walkin92b35d22004-09-27 20:52:18 +0000154 */
Lev Walkin8ca13c82016-01-24 22:40:00 -0800155 char *dot;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000156 char *end = buf + buflen;
Lev Walkin92b35d22004-09-27 20:52:18 +0000157 char *last_zero;
Lev Walkin8ca13c82016-01-24 22:40:00 -0800158 char *prev_zero;
159 char *s;
160
161 enum {
162 LZSTATE_NOTHING,
163 LZSTATE_SEEN_ZERO
164 } lz_state = LZSTATE_NOTHING;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000165
Lev Walkin5f560912004-10-21 13:37:57 +0000166 dot = (buf[0] == 0x2d /* '-' */) ? (buf + 2) : (buf + 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000167 if(*dot >= 0x30) {
Lev Walkin78d917d2012-01-07 15:29:08 -0800168 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000169 errno = EINVAL;
170 return -1; /* Not a dot, really */
171 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000172 *dot = 0x2e; /* Replace possible comma */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000173
Lev Walkin8ca13c82016-01-24 22:40:00 -0800174 for(prev_zero = last_zero = s = dot + 2; s < end; s++) {
175 switch(*s) {
176 case 0x45: /* 'E' */
177 if(lz_state == LZSTATE_SEEN_ZERO)
178 last_zero = prev_zero;
179 break;
180 case 0x30: /* '0' */
181 if(lz_state == LZSTATE_NOTHING)
182 prev_zero = s;
183 lz_state = LZSTATE_SEEN_ZERO;
184 continue;
185 default:
186 lz_state = LZSTATE_NOTHING;
187 continue;
188 }
189 break;
190 }
191
192 if(s == end) {
Lev Walkin78d917d2012-01-07 15:29:08 -0800193 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000194 errno = EINVAL;
195 return -1; /* No promised E */
196 }
Lev Walkin8ca13c82016-01-24 22:40:00 -0800197
198 assert(*s == 0x45);
199 {
200 char *E = s;
201 char *expptr = ++E;
202 char *s = expptr;
203 int sign;
204
205 if(*expptr == 0x2b /* '+' */) {
206 /* Skip the "+" */
207 buflen -= 1;
208 sign = 0;
209 } else {
210 sign = 1;
211 s++;
212 }
213 expptr++;
214 if(expptr > end) {
215 if(buf != local_buf) FREEMEM(buf);
216 errno = EINVAL;
217 return -1;
218 }
219 if(*expptr == 0x30) {
220 buflen--;
221 expptr++;
222 }
223 if(*last_zero == 0x30) {
224 *last_zero = 0x45; /* E */
225 buflen -= s - (last_zero + 1);
226 s = last_zero + 1;
227 if(sign) {
228 *s++ = 0x2d /* '-' */;
229 buflen++;
230 }
231 }
232 for(; expptr <= end; s++, expptr++)
233 *s = *expptr;
234 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000235 } else {
236 /*
237 * Remove trailing zeros.
238 */
239 char *end = buf + buflen;
240 char *last_zero = end;
Lev Walkinf0b808d2005-04-25 21:08:25 +0000241 int stoplooking = 0;
Lev Walkin92b35d22004-09-27 20:52:18 +0000242 char *z;
243 for(z = end - 1; z > buf; z--) {
244 switch(*z) {
Lev Walkinf0b808d2005-04-25 21:08:25 +0000245 case 0x30:
246 if(!stoplooking)
247 last_zero = z;
248 continue;
Lev Walkin92b35d22004-09-27 20:52:18 +0000249 case 0x31: case 0x32: case 0x33: case 0x34:
250 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinf0b808d2005-04-25 21:08:25 +0000251 stoplooking = 1;
Lev Walkin92b35d22004-09-27 20:52:18 +0000252 continue;
253 default: /* Catch dot and other separators */
Lev Walkinf0b808d2005-04-25 21:08:25 +0000254 /*
255 * Replace possible comma (which may even
256 * be not a comma at all: locale-defined).
257 */
258 *z = 0x2e;
Lev Walkin92b35d22004-09-27 20:52:18 +0000259 if(last_zero == z + 1) { /* leave x.0 */
260 last_zero++;
261 }
262 buflen = last_zero - buf;
263 *last_zero = '\0';
264 break;
265 }
266 break;
267 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000268 }
269
270 ret = cb(buf, buflen, app_key);
Lev Walkin419f6752006-09-13 04:02:00 +0000271 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000272 return (ret < 0) ? -1 : buflen;
273}
274
Lev Walkin41ba1f22004-09-14 12:46:35 +0000275int
Lev Walkin5e033762004-09-29 13:26:15 +0000276REAL_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000277 asn_app_consume_bytes_f *cb, void *app_key) {
278 const REAL_t *st = (const REAL_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000279 ssize_t ret;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000280 double d;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000281
282 (void)td; /* Unused argument */
283 (void)ilevel; /* Unused argument */
284
Lev Walkina9cc46e2004-09-22 16:06:28 +0000285 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000286 ret = cb("<absent>", 8, app_key);
Lev Walkin5e033762004-09-29 13:26:15 +0000287 else if(asn_REAL2double(st, &d))
Lev Walkin8e8078a2004-09-26 13:10:40 +0000288 ret = cb("<error>", 7, app_key);
289 else
290 ret = REAL__dump(d, 0, cb, app_key);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000291
Lev Walkin8e8078a2004-09-26 13:10:40 +0000292 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000293}
Lev Walkin41ba1f22004-09-14 12:46:35 +0000294
Lev Walkina9cc46e2004-09-22 16:06:28 +0000295asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000296REAL_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000297 int ilevel, enum xer_encoder_flags_e flags,
298 asn_app_consume_bytes_f *cb, void *app_key) {
299 REAL_t *st = (REAL_t *)sptr;
300 asn_enc_rval_t er;
301 double d;
302
303 (void)ilevel;
304
Lev Walkin5e033762004-09-29 13:26:15 +0000305 if(!st || !st->buf || asn_REAL2double(st, &d))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700306 ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000307
308 er.encoded = REAL__dump(d, flags & XER_F_CANONICAL, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700309 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000310
Lev Walkin7c1dc052016-03-14 03:08:15 -0700311 ASN__ENCODED_OK(er);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000312}
313
Lev Walkin5f560912004-10-21 13:37:57 +0000314
315/*
316 * Decode the chunk of XML text encoding REAL.
317 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000318static enum xer_pbd_rval
319REAL__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 +0000320 REAL_t *st = (REAL_t *)sptr;
Lev Walkin5f560912004-10-21 13:37:57 +0000321 double value;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000322 const char *xerdata = (const char *)chunk_buf;
Lev Walkin5f560912004-10-21 13:37:57 +0000323 char *endptr = 0;
324 char *b;
325
Lev Walkine0b56e02005-02-25 12:10:27 +0000326 (void)td;
327
Lev Walkin0fab1a62005-03-09 22:19:25 +0000328 if(!chunk_size) return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000329
330 /*
331 * Decode an XMLSpecialRealValue: <MINUS-INFINITY>, etc.
332 */
333 if(xerdata[0] == 0x3c /* '<' */) {
334 size_t i;
335 for(i = 0; i < sizeof(specialRealValue)
336 / sizeof(specialRealValue[0]); i++) {
337 struct specialRealValue_s *srv = &specialRealValue[i];
Lev Walkin1aea6982004-10-26 09:35:25 +0000338 double dv;
339
Lev Walkin5f560912004-10-21 13:37:57 +0000340 if(srv->length != chunk_size
341 || memcmp(srv->string, chunk_buf, chunk_size))
342 continue;
343
Lev Walkin1aea6982004-10-26 09:35:25 +0000344 /*
345 * It could've been done using
346 * (double)srv->dv / real_zero,
347 * but it summons fp exception on some platforms.
348 */
349 switch(srv->dv) {
350 case -1: dv = - INFINITY; break;
351 case 0: dv = NAN; break;
352 case 1: dv = INFINITY; break;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000353 default: return XPBD_SYSTEM_FAILURE;
Lev Walkin1aea6982004-10-26 09:35:25 +0000354 }
355
Lev Walkin0fab1a62005-03-09 22:19:25 +0000356 if(asn_double2REAL(st, dv))
357 return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000358
Lev Walkin0fab1a62005-03-09 22:19:25 +0000359 return XPBD_BODY_CONSUMED;
Lev Walkin5f560912004-10-21 13:37:57 +0000360 }
361 ASN_DEBUG("Unknown XMLSpecialRealValue");
Lev Walkin0fab1a62005-03-09 22:19:25 +0000362 return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000363 }
364
365 /*
366 * Copy chunk into the nul-terminated string, and run strtod.
367 */
Lev Walkin8471cec2004-10-21 14:02:19 +0000368 b = (char *)MALLOC(chunk_size + 1);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000369 if(!b) return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000370 memcpy(b, chunk_buf, chunk_size);
Lev Walkin8471cec2004-10-21 14:02:19 +0000371 b[chunk_size] = 0; /* nul-terminate */
Lev Walkin5f560912004-10-21 13:37:57 +0000372
373 value = strtod(b, &endptr);
Lev Walkin419f6752006-09-13 04:02:00 +0000374 FREEMEM(b);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000375 if(endptr == b) return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000376
377 if(asn_double2REAL(st, value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000378 return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000379
Lev Walkin0fab1a62005-03-09 22:19:25 +0000380 return XPBD_BODY_CONSUMED;
Lev Walkin5f560912004-10-21 13:37:57 +0000381}
382
383asn_dec_rval_t
384REAL_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
385 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000386 const void *buf_ptr, size_t size) {
Lev Walkin5f560912004-10-21 13:37:57 +0000387
388 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000389 sptr, sizeof(REAL_t), opt_mname,
Lev Walkin5f560912004-10-21 13:37:57 +0000390 buf_ptr, size, REAL__xer_body_decode);
391}
392
Lev Walkin725883b2006-10-09 12:07:58 +0000393asn_dec_rval_t
Lev Walkin494fb702017-08-07 20:07:00 -0700394REAL_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,
395 const asn_per_constraints_t *constraints, void **sptr,
396 asn_per_data_t *pd) {
397 (void)constraints; /* No PER visible constraints */
Lev Walkin725883b2006-10-09 12:07:58 +0000398 return OCTET_STRING_decode_uper(opt_codec_ctx, td, 0, sptr, pd);
399}
400
401asn_enc_rval_t
402REAL_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700403 const asn_per_constraints_t *constraints, void *sptr,
404 asn_per_outp_t *po) {
405 (void)constraints; /* No PER visible constraints */
Lev Walkin725883b2006-10-09 12:07:58 +0000406 return OCTET_STRING_encode_uper(td, 0, sptr, po);
407}
Lev Walkin5f560912004-10-21 13:37:57 +0000408
Lev Walkin41ba1f22004-09-14 12:46:35 +0000409int
Lev Walkin5e033762004-09-29 13:26:15 +0000410asn_REAL2double(const REAL_t *st, double *dbl_value) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000411 unsigned int octv;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000412
413 if(!st || !st->buf) {
414 errno = EINVAL;
415 return -1;
416 }
417
418 if(st->size == 0) {
419 *dbl_value = 0;
420 return 0;
421 }
422
423 octv = st->buf[0]; /* unsigned byte */
424
425 switch(octv & 0xC0) {
Lev Walkin5fda7d52012-01-09 03:20:19 -0800426 case 0x40: /* X.690: 8.5.6 a) => 8.5.9 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000427 /* "SpecialRealValue" */
428
429 /* Be liberal in what you accept...
Lev Walkin749916f2012-01-07 17:03:39 -0800430 * http://en.wikipedia.org/wiki/Robustness_principle
Lev Walkin41ba1f22004-09-14 12:46:35 +0000431 if(st->size != 1) ...
432 */
433
434 switch(st->buf[0]) {
435 case 0x40: /* 01000000: PLUS-INFINITY */
Lev Walkin1aea6982004-10-26 09:35:25 +0000436 *dbl_value = INFINITY;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000437 return 0;
438 case 0x41: /* 01000001: MINUS-INFINITY */
Lev Walkin1aea6982004-10-26 09:35:25 +0000439 *dbl_value = - INFINITY;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000440 return 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000441 case 0x42: /* 01000010: NOT-A-NUMBER */
442 *dbl_value = NAN;
443 return 0;
444 case 0x43: /* 01000011: minus zero */
Lev Walkin2a789d92004-09-27 21:36:59 +0000445 *dbl_value = -0.0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000446 return 0;
447 }
448
449 errno = EINVAL;
450 return -1;
Lev Walkin0959ffb2011-06-25 15:48:52 -0700451 case 0x00: { /* X.690: 8.5.7 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000452 /*
Lev Walkin5fda7d52012-01-09 03:20:19 -0800453 * Decimal. NR{1,2,3} format from ISO 6093.
454 * NR1: [ ]*[+-]?[0-9]+
455 * NR2: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)
456 * NR3: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)[Ee][+-]?[0-9]+
Lev Walkin41ba1f22004-09-14 12:46:35 +0000457 */
458 double d;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800459 char *buf;
460 char *endptr;
461 int used_malloc = 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000462
Lev Walkin5fda7d52012-01-09 03:20:19 -0800463 if(octv == 0 || (octv & 0x3C)) {
Lev Walkin0959ffb2011-06-25 15:48:52 -0700464 /* Remaining values of bits 6 to 1 are Reserved. */
465 errno = EINVAL;
466 return -1;
467 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000468
Lev Walkin5fda7d52012-01-09 03:20:19 -0800469
470 /* 1. By contract, an input buffer should be null-terminated.
471 * OCTET STRING decoder ensures that, as is asn_double2REAL().
472 * 2. ISO 6093 specifies COMMA as a possible decimal separator.
473 * However, strtod() can't always deal with COMMA.
474 * So her we fix both by reallocating, copying and fixing.
475 */
476 if(st->buf[st->size] || memchr(st->buf, ',', st->size)) {
477 uint8_t *p, *end;
478 char *b;
479 if(st->size > 100) {
480 /* Avoid malicious stack overflow in alloca() */
481 buf = (char *)MALLOC(st->size);
482 if(!buf) return -1;
483 used_malloc = 1;
484 } else {
485 buf = alloca(st->size);
486 }
487 b = buf;
488 /* Copy without the first byte and with 0-termination */
489 for(p = st->buf + 1, end = st->buf + st->size;
490 p < end; b++, p++)
491 *b = (*p == ',') ? '.' : *p;
492 *b = '\0';
493 } else {
494 buf = (char *)&st->buf[1];
495 }
496
497 endptr = buf;
498 d = strtod(buf, &endptr);
499 if(*endptr != '\0') {
500 /* Format is not consistent with ISO 6093 */
501 if(used_malloc) FREEMEM(buf);
Lev Walkin0959ffb2011-06-25 15:48:52 -0700502 errno = EINVAL;
503 return -1;
504 }
Lev Walkin5fda7d52012-01-09 03:20:19 -0800505 if(used_malloc) FREEMEM(buf);
Lev Walkindaeb2162014-01-13 23:08:35 -0800506 if(_asn_isfinite(d)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000507 *dbl_value = d;
508 return 0;
509 } else {
510 errno = ERANGE;
Lev Walkin0959ffb2011-06-25 15:48:52 -0700511 return -1;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000512 }
513 }
514 }
515
516 /*
517 * Binary representation.
518 */
519 {
520 double m;
521 int expval; /* exponent value */
522 unsigned int elen; /* exponent value length, in octets */
523 unsigned int scaleF;
524 unsigned int baseF;
525 uint8_t *ptr;
526 uint8_t *end;
527 int sign;
528
529 switch((octv & 0x30) >> 4) {
530 case 0x00: baseF = 1; break; /* base 2 */
531 case 0x01: baseF = 3; break; /* base 8 */
532 case 0x02: baseF = 4; break; /* base 16 */
533 default:
534 /* Reserved field, can't parse now. */
535 errno = EINVAL;
536 return -1;
537 }
538
539 sign = (octv & 0x40); /* bit 7 */
540 scaleF = (octv & 0x0C) >> 2; /* bits 4 to 3 */
541
Lev Walkin494fb702017-08-07 20:07:00 -0700542 if(st->size <= 1 + (octv & 0x03)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000543 errno = EINVAL;
544 return -1;
545 }
546
Lev Walkine78753d2007-06-23 17:02:00 +0000547 elen = (octv & 0x03); /* bits 2 to 1; 8.5.6.4 */
548 if(elen == 0x03) { /* bits 2 to 1 = 11; 8.5.6.4, case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000549 elen = st->buf[1]; /* unsigned binary number */
Lev Walkin494fb702017-08-07 20:07:00 -0700550 if(elen == 0 || st->size <= (2 + elen)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000551 errno = EINVAL;
552 return -1;
553 }
Lev Walkine78753d2007-06-23 17:02:00 +0000554 /* FIXME: verify constraints of case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000555 ptr = &st->buf[2];
556 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000557 ptr = &st->buf[1];
558 }
559
560 /* Fetch the multibyte exponent */
561 expval = (int)(*(int8_t *)ptr);
562 end = ptr + elen + 1;
563 for(ptr++; ptr < end; ptr++)
564 expval = (expval * 256) + *ptr;
565
566 m = 0.0; /* Initial mantissa value */
567
568 /* Okay, the exponent is here. Now, what about mantissa? */
569 end = st->buf + st->size;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800570 for(; ptr < end; ptr++)
571 m = ldexp(m, 8) + *ptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000572
Lev Walkin5f560912004-10-21 13:37:57 +0000573 if(0)
Lev Walkin5fda7d52012-01-09 03:20:19 -0800574 ASN_DEBUG("m=%.10f, scF=%d, bF=%d, expval=%d, ldexp()=%f, ldexp()=%f\n",
Lev Walkin41ba1f22004-09-14 12:46:35 +0000575 m, scaleF, baseF, expval,
576 ldexp(m, expval * baseF + scaleF),
Lev Walkin77aa90f2005-04-28 22:56:36 +0000577 ldexp(m, scaleF) * pow(pow(2, baseF), expval)
Lev Walkin41ba1f22004-09-14 12:46:35 +0000578 );
579
580 /*
581 * (S * N * 2^F) * B^E
582 * Essentially:
Lev Walkin77aa90f2005-04-28 22:56:36 +0000583 m = ldexp(m, scaleF) * pow(pow(2, base), expval);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000584 */
585 m = ldexp(m, expval * baseF + scaleF);
Lev Walkindaeb2162014-01-13 23:08:35 -0800586 if(_asn_isfinite(m)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000587 *dbl_value = sign ? -m : m;
588 } else {
589 errno = ERANGE;
590 return -1;
591 }
592
593 } /* if(binary_format) */
594
595 return 0;
596}
597
598/*
599 * Assume IEEE 754 floating point: standard 64 bit double.
600 * [1 bit sign] [11 bits exponent] [52 bits mantissa]
601 */
602int
Lev Walkin5e033762004-09-29 13:26:15 +0000603asn_double2REAL(REAL_t *st, double dbl_value) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000604#ifdef WORDS_BIGENDIAN /* Known to be big-endian */
605 int littleEndian = 0;
606#else /* need to test: have no explicit information */
607 unsigned int LE = 1;
608 int littleEndian = *(unsigned char *)&LE;
609#endif
610 uint8_t buf[16]; /* More than enough for 8-byte dbl_value */
611 uint8_t dscr[sizeof(dbl_value)]; /* double value scratch pad */
612 /* Assertion guards: won't even compile, if unexpected double size */
Lev Walkin50b0f6c2006-10-16 12:01:36 +0000613 char assertion_buffer1[9 - sizeof(dbl_value)] GCC_NOTUSED;
614 char assertion_buffer2[sizeof(dbl_value) - 7] GCC_NOTUSED;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000615 uint8_t *ptr = buf;
616 uint8_t *mstop; /* Last byte of mantissa */
617 unsigned int mval; /* Value of the last byte of mantissa */
618 unsigned int bmsign; /* binary mask with sign */
619 unsigned int buflen;
620 unsigned int accum;
621 int expval;
622
623 if(!st) {
624 errno = EINVAL;
625 return -1;
626 }
627
Lev Walkin057fb732004-09-14 13:58:10 +0000628 /*
629 * ilogb(+-0) returns -INT_MAX or INT_MIN (platform-dependent)
Lev Walkin2f505022005-07-01 08:28:18 +0000630 * ilogb(+-inf) returns INT_MAX, logb(+-inf) returns +inf
Lev Walkin2a789d92004-09-27 21:36:59 +0000631 * ilogb(NaN) returns INT_MIN or INT_MAX (platform-dependent)
Lev Walkin057fb732004-09-14 13:58:10 +0000632 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000633 expval = ilogb(dbl_value);
Lev Walkin2a789d92004-09-27 21:36:59 +0000634 if(expval <= -INT_MAX /* Also catches +-0 and maybe isnan() */
635 || expval == INT_MAX /* catches isfin() and maybe isnan() */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000636 ) {
637 if(!st->buf || st->size < 2) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000638 ptr = (uint8_t *)MALLOC(2);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000639 if(!ptr) return -1;
640 st->buf = ptr;
641 }
642 /* fpclassify(3) is not portable yet */
Lev Walkin2a789d92004-09-27 21:36:59 +0000643 if(isnan(dbl_value)) {
644 st->buf[0] = 0x42; /* NaN */
645 st->buf[1] = 0;
646 st->size = 1;
Lev Walkindaeb2162014-01-13 23:08:35 -0800647 } else if(!_asn_isfinite(dbl_value)) {
Lev Walkin7e03db92004-09-14 13:50:21 +0000648 if(copysign(1.0, dbl_value) < 0.0) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000649 st->buf[0] = 0x41; /* MINUS-INFINITY */
650 } else {
651 st->buf[0] = 0x40; /* PLUS-INFINITY */
652 }
653 st->buf[1] = 0;
654 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000655 } else {
Lev Walkin5fe72e92012-01-07 17:00:29 -0800656 if(copysign(1.0, dbl_value) >= 0.0) {
Lev Walkinc51e7d62004-09-27 22:16:18 +0000657 /* no content octets: positive zero */
658 st->buf[0] = 0; /* JIC */
659 st->size = 0;
Lev Walkin5fe72e92012-01-07 17:00:29 -0800660 } else {
661 /* Negative zero. #8.5.3, 8.5.9 */
662 st->buf[0] = 0x43;
663 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000664 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000665 }
666 return 0;
667 }
668
669 if(littleEndian) {
670 uint8_t *s = ((uint8_t *)&dbl_value) + sizeof(dbl_value) - 2;
Lev Walkin057fb732004-09-14 13:58:10 +0000671 uint8_t *start = ((uint8_t *)&dbl_value);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000672 uint8_t *d;
673
674 bmsign = 0x80 | ((s[1] >> 1) & 0x40); /* binary mask & - */
Lev Walkin057fb732004-09-14 13:58:10 +0000675 for(mstop = d = dscr; s >= start; d++, s--) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000676 *d = *s;
677 if(*d) mstop = d;
678 }
679 } else {
680 uint8_t *s = ((uint8_t *)&dbl_value) + 1;
681 uint8_t *end = ((uint8_t *)&dbl_value) + sizeof(double);
682 uint8_t *d;
683
684 bmsign = 0x80 | ((s[-1] >> 1) & 0x40); /* binary mask & - */
685 for(mstop = d = dscr; s < end; d++, s++) {
686 *d = *s;
687 if(*d) mstop = d;
688 }
689 }
690
691 /* Remove parts of the exponent, leave mantissa and explicit 1. */
692 dscr[0] = 0x10 | (dscr[0] & 0x0f);
693
694 /* Adjust exponent in a very unobvious way */
695 expval -= 8 * ((mstop - dscr) + 1) - 4;
696
697 /* This loop ensures DER conformance by forcing mantissa odd: 11.3.1 */
698 mval = *mstop;
699 if(mval && !(mval & 1)) {
700 unsigned int shift_count = 1;
701 unsigned int ishift;
702 uint8_t *mptr;
703
704 /*
705 * Figure out what needs to be done to make mantissa odd.
706 */
707 if(!(mval & 0x0f)) /* Speed-up a little */
708 shift_count = 4;
709 while(((mval >> shift_count) & 1) == 0)
710 shift_count++;
711
712 ishift = 8 - shift_count;
713 accum = 0;
714
715 /* Go over the buffer, shifting it shift_count bits right. */
716 for(mptr = dscr; mptr <= mstop; mptr++) {
717 mval = *mptr;
718 *mptr = accum | (mval >> shift_count);
719 accum = mval << ishift;
720 }
721
Lev Walkinb89b3402012-01-09 18:26:30 -0800722 /* Adjust exponent appropriately. */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000723 expval += shift_count;
724 }
725
726 if(expval < 0) {
727 if((expval >> 7) == -1) {
728 *ptr++ = bmsign | 0x00;
729 *ptr++ = expval;
730 } else if((expval >> 15) == -1) {
731 *ptr++ = bmsign | 0x01;
732 *ptr++ = expval >> 8;
733 *ptr++ = expval;
734 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000735 *ptr++ = bmsign | 0x02;
736 *ptr++ = expval >> 16;
737 *ptr++ = expval >> 8;
738 *ptr++ = expval;
739 }
740 } else if(expval <= 0x7f) {
741 *ptr++ = bmsign | 0x00;
742 *ptr++ = expval;
743 } else if(expval <= 0x7fff) {
744 *ptr++ = bmsign | 0x01;
745 *ptr++ = expval >> 8;
746 *ptr++ = expval;
747 } else {
748 assert(expval <= 0x7fffff);
749 *ptr++ = bmsign | 0x02;
750 *ptr++ = expval >> 16;
751 *ptr++ = expval >> 8;
752 *ptr++ = expval;
753 }
754
755 buflen = (mstop - dscr) + 1;
756 memcpy(ptr, dscr, buflen);
757 ptr += buflen;
758 buflen = ptr - buf;
759
Lev Walkinc17d90f2005-01-17 14:32:45 +0000760 ptr = (uint8_t *)MALLOC(buflen + 1);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000761 if(!ptr) return -1;
762
763 memcpy(ptr, buf, buflen);
764 buf[buflen] = 0; /* JIC */
765
766 if(st->buf) FREEMEM(st->buf);
767 st->buf = ptr;
768 st->size = buflen;
769
770 return 0;
771}