blob: 1b8e30cfd6734a3b1f5161b1ecccf4e82725b7bd [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 */
Lev Walkin1aea6982004-10-26 09:35:25 +00006#define _BSD_SOURCE /* To reintroduce finite(3) */
Lev Walkin73823e12013-03-28 00:24:48 -07007#if defined(__alpha)
Lev Walkin1aea6982004-10-26 09:35:25 +00008#include <sys/resource.h> /* For INFINITY */
Lev Walkin33700162004-10-26 09:03:31 +00009#endif
Lev Walkina9cc46e2004-09-22 16:06:28 +000010#include <asn_internal.h>
Lev Walkin41ba1f22004-09-14 12:46:35 +000011#include <stdlib.h> /* for strtod(3) */
12#include <math.h>
13#include <errno.h>
Lev Walkin33700162004-10-26 09:03:31 +000014#include <REAL.h>
Lev Walkin725883b2006-10-09 12:07:58 +000015#include <OCTET_STRING.h>
Lev Walkin41ba1f22004-09-14 12:46:35 +000016
17#undef INT_MAX
18#define INT_MAX ((int)(((unsigned int)-1) >> 1))
19
Lev Walkin1aea6982004-10-26 09:35:25 +000020#if !(defined(NAN) || defined(INFINITY))
Lev Walkin50b0f6c2006-10-16 12:01:36 +000021static volatile double real_zero GCC_NOTUSED = 0.0;
Lev Walkin1aea6982004-10-26 09:35:25 +000022#endif
Lev Walkinae1bce92004-09-27 22:18:34 +000023#ifndef NAN
Lev Walkinc51e7d62004-09-27 22:16:18 +000024#define NAN (real_zero/real_zero)
Lev Walkin40319a12004-09-14 13:40:42 +000025#endif
Lev Walkin1aea6982004-10-26 09:35:25 +000026#ifndef INFINITY
27#define INFINITY (1.0/real_zero)
28#endif
Lev Walkin40319a12004-09-14 13:40:42 +000029
Lev Walkindaeb2162014-01-13 23:08:35 -080030#ifdef isfinite
31#define _asn_isfinite(d) isfinite(d) /* ISO C99 */
32#else
33#define _asn_isfinite(d) finite(d) /* Deprecated on Mac OS X 10.9 */
34#endif
35
Lev Walkin41ba1f22004-09-14 12:46:35 +000036/*
37 * REAL basic type description.
38 */
Lev Walkin5e033762004-09-29 13:26:15 +000039static ber_tlv_tag_t asn_DEF_REAL_tags[] = {
Lev Walkin41ba1f22004-09-14 12:46:35 +000040 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
41};
Lev Walkin5e033762004-09-29 13:26:15 +000042asn_TYPE_descriptor_t asn_DEF_REAL = {
Lev Walkin41ba1f22004-09-14 12:46:35 +000043 "REAL",
Lev Walkindc06f6b2004-10-20 15:50:55 +000044 "REAL",
Lev Walkin8e8078a2004-09-26 13:10:40 +000045 ASN__PRIMITIVE_TYPE_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000046 REAL_print,
Lev Walkin41ba1f22004-09-14 12:46:35 +000047 asn_generic_no_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000048 ber_decode_primitive,
49 der_encode_primitive,
Lev Walkin5f560912004-10-21 13:37:57 +000050 REAL_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000051 REAL_encode_xer,
Lev Walkin725883b2006-10-09 12:07:58 +000052 REAL_decode_uper,
53 REAL_encode_uper,
Lev Walkin41ba1f22004-09-14 12:46:35 +000054 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000055 asn_DEF_REAL_tags,
56 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
57 asn_DEF_REAL_tags, /* Same as above */
58 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000059 0, /* No PER visible constraints */
Lev Walkin41ba1f22004-09-14 12:46:35 +000060 0, 0, /* No members */
61 0 /* No specifics */
62};
63
Lev Walkin5f560912004-10-21 13:37:57 +000064typedef enum specialRealValue {
65 SRV__NOT_A_NUMBER,
66 SRV__MINUS_INFINITY,
67 SRV__PLUS_INFINITY
68} specialRealValue_e;
69static struct specialRealValue_s {
70 char *string;
Lev Walkin8471cec2004-10-21 14:02:19 +000071 size_t length;
Lev Walkin1aea6982004-10-26 09:35:25 +000072 long dv;
Lev Walkin5f560912004-10-21 13:37:57 +000073} specialRealValue[] = {
74#define SRV_SET(foo, val) { foo, sizeof(foo) - 1, val }
Lev Walkin1aea6982004-10-26 09:35:25 +000075 SRV_SET("<NOT-A-NUMBER/>", 0),
76 SRV_SET("<MINUS-INFINITY/>", -1),
77 SRV_SET("<PLUS-INFINITY/>", 1),
Lev Walkin5f560912004-10-21 13:37:57 +000078#undef SRV_SET
79};
80
Lev Walkina9cc46e2004-09-22 16:06:28 +000081ssize_t
82REAL__dump(double d, int canonical, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin92b35d22004-09-27 20:52:18 +000083 char local_buf[64];
Lev Walkina9cc46e2004-09-22 16:06:28 +000084 char *buf = local_buf;
85 ssize_t buflen = sizeof(local_buf);
Lev Walkin92b35d22004-09-27 20:52:18 +000086 const char *fmt = canonical?"%.15E":"%.15f";
Lev Walkina9cc46e2004-09-22 16:06:28 +000087 ssize_t ret;
88
Lev Walkin8e8078a2004-09-26 13:10:40 +000089 /*
90 * Check whether it is a special value.
91 */
Lev Walkinc51e7d62004-09-27 22:16:18 +000092 /* fpclassify(3) is not portable yet */
93 if(isnan(d)) {
Lev Walkin5f560912004-10-21 13:37:57 +000094 buf = specialRealValue[SRV__NOT_A_NUMBER].string;
95 buflen = specialRealValue[SRV__NOT_A_NUMBER].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +000096 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
Lev Walkindaeb2162014-01-13 23:08:35 -080097 } else if(!_asn_isfinite(d)) {
Lev Walkinc51e7d62004-09-27 22:16:18 +000098 if(copysign(1.0, d) < 0.0) {
Lev Walkin5f560912004-10-21 13:37:57 +000099 buf = specialRealValue[SRV__MINUS_INFINITY].string;
100 buflen = specialRealValue[SRV__MINUS_INFINITY].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000101 } else {
Lev Walkin5f560912004-10-21 13:37:57 +0000102 buf = specialRealValue[SRV__PLUS_INFINITY].string;
103 buflen = specialRealValue[SRV__PLUS_INFINITY].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000104 }
105 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
106 } else if(ilogb(d) <= -INT_MAX) {
107 if(copysign(1.0, d) < 0.0) {
108 buf = "-0";
109 buflen = 2;
110 } else {
111 buf = "0";
112 buflen = 1;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000113 }
114 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
115 }
116
117 /*
118 * Use the libc's double printing, hopefully they got it right.
119 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000120 do {
121 ret = snprintf(buf, buflen, fmt, d);
122 if(ret < 0) {
123 buflen <<= 1;
124 } else if(ret >= buflen) {
125 buflen = ret + 1;
126 } else {
127 buflen = ret;
128 break;
129 }
Lev Walkin419f6752006-09-13 04:02:00 +0000130 if(buf != local_buf) FREEMEM(buf);
Lev Walkin8e8078a2004-09-26 13:10:40 +0000131 buf = (char *)MALLOC(buflen);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000132 if(!buf) return -1;
133 } while(1);
134
Lev Walkina9cc46e2004-09-22 16:06:28 +0000135 if(canonical) {
Lev Walkin92b35d22004-09-27 20:52:18 +0000136 /*
137 * Transform the "[-]d.dddE+-dd" output into "[-]d.dddE[-]d"
Lev Walkin5f560912004-10-21 13:37:57 +0000138 * Check that snprintf() constructed the output correctly.
Lev Walkin92b35d22004-09-27 20:52:18 +0000139 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000140 char *dot, *E;
141 char *end = buf + buflen;
Lev Walkin92b35d22004-09-27 20:52:18 +0000142 char *last_zero;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000143
Lev Walkin5f560912004-10-21 13:37:57 +0000144 dot = (buf[0] == 0x2d /* '-' */) ? (buf + 2) : (buf + 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000145 if(*dot >= 0x30) {
Lev Walkin78d917d2012-01-07 15:29:08 -0800146 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000147 errno = EINVAL;
148 return -1; /* Not a dot, really */
149 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000150 *dot = 0x2e; /* Replace possible comma */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000151
Lev Walkin92b35d22004-09-27 20:52:18 +0000152 for(last_zero = dot + 2, E = dot; dot < end; E++) {
153 if(*E == 0x45) {
154 char *expptr = ++E;
155 char *s = expptr;
156 int sign;
Lev Walkin5f560912004-10-21 13:37:57 +0000157 if(*expptr == 0x2b /* '+' */) {
Lev Walkin92b35d22004-09-27 20:52:18 +0000158 /* Skip the "+" */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000159 buflen -= 1;
Lev Walkin92b35d22004-09-27 20:52:18 +0000160 sign = 0;
161 } else {
162 sign = 1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000163 s++;
164 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000165 expptr++;
166 if(expptr > end) {
Lev Walkin78d917d2012-01-07 15:29:08 -0800167 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000168 errno = EINVAL;
169 return -1;
170 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000171 if(*expptr == 0x30) {
172 buflen--;
173 expptr++;
174 }
175 if(*last_zero == 0x30) {
176 *last_zero = 0x45; /* E */
Lev Walkin5f560912004-10-21 13:37:57 +0000177 buflen -= s - (last_zero + 1);
Lev Walkin92b35d22004-09-27 20:52:18 +0000178 s = last_zero + 1;
Lev Walkin5f560912004-10-21 13:37:57 +0000179 if(sign) {
180 *s++ = 0x2d /* '-' */;
181 buflen++;
182 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000183 }
184 for(; expptr <= end; s++, expptr++)
185 *s = *expptr;
186 break;
187 } else if(*E == 0x30) {
188 if(*last_zero != 0x30)
189 last_zero = E;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000190 }
191 }
192 if(E == 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 Walkin92b35d22004-09-27 20:52:18 +0000197 } else {
198 /*
199 * Remove trailing zeros.
200 */
201 char *end = buf + buflen;
202 char *last_zero = end;
Lev Walkinf0b808d2005-04-25 21:08:25 +0000203 int stoplooking = 0;
Lev Walkin92b35d22004-09-27 20:52:18 +0000204 char *z;
205 for(z = end - 1; z > buf; z--) {
206 switch(*z) {
Lev Walkinf0b808d2005-04-25 21:08:25 +0000207 case 0x30:
208 if(!stoplooking)
209 last_zero = z;
210 continue;
Lev Walkin92b35d22004-09-27 20:52:18 +0000211 case 0x31: case 0x32: case 0x33: case 0x34:
212 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
Lev Walkinf0b808d2005-04-25 21:08:25 +0000213 stoplooking = 1;
Lev Walkin92b35d22004-09-27 20:52:18 +0000214 continue;
215 default: /* Catch dot and other separators */
Lev Walkinf0b808d2005-04-25 21:08:25 +0000216 /*
217 * Replace possible comma (which may even
218 * be not a comma at all: locale-defined).
219 */
220 *z = 0x2e;
Lev Walkin92b35d22004-09-27 20:52:18 +0000221 if(last_zero == z + 1) { /* leave x.0 */
222 last_zero++;
223 }
224 buflen = last_zero - buf;
225 *last_zero = '\0';
226 break;
227 }
228 break;
229 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000230 }
231
232 ret = cb(buf, buflen, app_key);
Lev Walkin419f6752006-09-13 04:02:00 +0000233 if(buf != local_buf) FREEMEM(buf);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000234 return (ret < 0) ? -1 : buflen;
235}
236
Lev Walkin41ba1f22004-09-14 12:46:35 +0000237int
Lev Walkin5e033762004-09-29 13:26:15 +0000238REAL_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000239 asn_app_consume_bytes_f *cb, void *app_key) {
240 const REAL_t *st = (const REAL_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000241 ssize_t ret;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000242 double d;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000243
244 (void)td; /* Unused argument */
245 (void)ilevel; /* Unused argument */
246
Lev Walkina9cc46e2004-09-22 16:06:28 +0000247 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000248 ret = cb("<absent>", 8, app_key);
Lev Walkin5e033762004-09-29 13:26:15 +0000249 else if(asn_REAL2double(st, &d))
Lev Walkin8e8078a2004-09-26 13:10:40 +0000250 ret = cb("<error>", 7, app_key);
251 else
252 ret = REAL__dump(d, 0, cb, app_key);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000253
Lev Walkin8e8078a2004-09-26 13:10:40 +0000254 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000255}
Lev Walkin41ba1f22004-09-14 12:46:35 +0000256
Lev Walkina9cc46e2004-09-22 16:06:28 +0000257asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000258REAL_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000259 int ilevel, enum xer_encoder_flags_e flags,
260 asn_app_consume_bytes_f *cb, void *app_key) {
261 REAL_t *st = (REAL_t *)sptr;
262 asn_enc_rval_t er;
263 double d;
264
265 (void)ilevel;
266
Lev Walkin5e033762004-09-29 13:26:15 +0000267 if(!st || !st->buf || asn_REAL2double(st, &d))
Lev Walkina9cc46e2004-09-22 16:06:28 +0000268 _ASN_ENCODE_FAILED;
269
270 er.encoded = REAL__dump(d, flags & XER_F_CANONICAL, cb, app_key);
271 if(er.encoded < 0) _ASN_ENCODE_FAILED;
272
Lev Walkin59b176e2005-11-26 11:25:14 +0000273 _ASN_ENCODED_OK(er);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000274}
275
Lev Walkin5f560912004-10-21 13:37:57 +0000276
277/*
278 * Decode the chunk of XML text encoding REAL.
279 */
Lev Walkin0fab1a62005-03-09 22:19:25 +0000280static enum xer_pbd_rval
281REAL__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 +0000282 REAL_t *st = (REAL_t *)sptr;
Lev Walkin5f560912004-10-21 13:37:57 +0000283 double value;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000284 const char *xerdata = (const char *)chunk_buf;
Lev Walkin5f560912004-10-21 13:37:57 +0000285 char *endptr = 0;
286 char *b;
287
Lev Walkine0b56e02005-02-25 12:10:27 +0000288 (void)td;
289
Lev Walkin0fab1a62005-03-09 22:19:25 +0000290 if(!chunk_size) return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000291
292 /*
293 * Decode an XMLSpecialRealValue: <MINUS-INFINITY>, etc.
294 */
295 if(xerdata[0] == 0x3c /* '<' */) {
296 size_t i;
297 for(i = 0; i < sizeof(specialRealValue)
298 / sizeof(specialRealValue[0]); i++) {
299 struct specialRealValue_s *srv = &specialRealValue[i];
Lev Walkin1aea6982004-10-26 09:35:25 +0000300 double dv;
301
Lev Walkin5f560912004-10-21 13:37:57 +0000302 if(srv->length != chunk_size
303 || memcmp(srv->string, chunk_buf, chunk_size))
304 continue;
305
Lev Walkin1aea6982004-10-26 09:35:25 +0000306 /*
307 * It could've been done using
308 * (double)srv->dv / real_zero,
309 * but it summons fp exception on some platforms.
310 */
311 switch(srv->dv) {
312 case -1: dv = - INFINITY; break;
313 case 0: dv = NAN; break;
314 case 1: dv = INFINITY; break;
Lev Walkin0fab1a62005-03-09 22:19:25 +0000315 default: return XPBD_SYSTEM_FAILURE;
Lev Walkin1aea6982004-10-26 09:35:25 +0000316 }
317
Lev Walkin0fab1a62005-03-09 22:19:25 +0000318 if(asn_double2REAL(st, dv))
319 return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000320
Lev Walkin0fab1a62005-03-09 22:19:25 +0000321 return XPBD_BODY_CONSUMED;
Lev Walkin5f560912004-10-21 13:37:57 +0000322 }
323 ASN_DEBUG("Unknown XMLSpecialRealValue");
Lev Walkin0fab1a62005-03-09 22:19:25 +0000324 return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000325 }
326
327 /*
328 * Copy chunk into the nul-terminated string, and run strtod.
329 */
Lev Walkin8471cec2004-10-21 14:02:19 +0000330 b = (char *)MALLOC(chunk_size + 1);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000331 if(!b) return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000332 memcpy(b, chunk_buf, chunk_size);
Lev Walkin8471cec2004-10-21 14:02:19 +0000333 b[chunk_size] = 0; /* nul-terminate */
Lev Walkin5f560912004-10-21 13:37:57 +0000334
335 value = strtod(b, &endptr);
Lev Walkin419f6752006-09-13 04:02:00 +0000336 FREEMEM(b);
Lev Walkin0fab1a62005-03-09 22:19:25 +0000337 if(endptr == b) return XPBD_BROKEN_ENCODING;
Lev Walkin5f560912004-10-21 13:37:57 +0000338
339 if(asn_double2REAL(st, value))
Lev Walkin0fab1a62005-03-09 22:19:25 +0000340 return XPBD_SYSTEM_FAILURE;
Lev Walkin5f560912004-10-21 13:37:57 +0000341
Lev Walkin0fab1a62005-03-09 22:19:25 +0000342 return XPBD_BODY_CONSUMED;
Lev Walkin5f560912004-10-21 13:37:57 +0000343}
344
345asn_dec_rval_t
346REAL_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
347 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000348 const void *buf_ptr, size_t size) {
Lev Walkin5f560912004-10-21 13:37:57 +0000349
350 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000351 sptr, sizeof(REAL_t), opt_mname,
Lev Walkin5f560912004-10-21 13:37:57 +0000352 buf_ptr, size, REAL__xer_body_decode);
353}
354
Lev Walkin725883b2006-10-09 12:07:58 +0000355asn_dec_rval_t
356REAL_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
357 asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints,
358 void **sptr, asn_per_data_t *pd) {
359 (void)constraints; /* No PER visible constraints */
360 return OCTET_STRING_decode_uper(opt_codec_ctx, td, 0, sptr, pd);
361}
362
363asn_enc_rval_t
364REAL_encode_uper(asn_TYPE_descriptor_t *td,
365 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
366 (void)constraints; /* No PER visible constraints */
367 return OCTET_STRING_encode_uper(td, 0, sptr, po);
368}
Lev Walkin5f560912004-10-21 13:37:57 +0000369
Lev Walkin41ba1f22004-09-14 12:46:35 +0000370int
Lev Walkin5e033762004-09-29 13:26:15 +0000371asn_REAL2double(const REAL_t *st, double *dbl_value) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000372 unsigned int octv;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000373
374 if(!st || !st->buf) {
375 errno = EINVAL;
376 return -1;
377 }
378
379 if(st->size == 0) {
380 *dbl_value = 0;
381 return 0;
382 }
383
384 octv = st->buf[0]; /* unsigned byte */
385
386 switch(octv & 0xC0) {
Lev Walkin5fda7d52012-01-09 03:20:19 -0800387 case 0x40: /* X.690: 8.5.6 a) => 8.5.9 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000388 /* "SpecialRealValue" */
389
390 /* Be liberal in what you accept...
Lev Walkin749916f2012-01-07 17:03:39 -0800391 * http://en.wikipedia.org/wiki/Robustness_principle
Lev Walkin41ba1f22004-09-14 12:46:35 +0000392 if(st->size != 1) ...
393 */
394
395 switch(st->buf[0]) {
396 case 0x40: /* 01000000: PLUS-INFINITY */
Lev Walkin1aea6982004-10-26 09:35:25 +0000397 *dbl_value = INFINITY;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000398 return 0;
399 case 0x41: /* 01000001: MINUS-INFINITY */
Lev Walkin1aea6982004-10-26 09:35:25 +0000400 *dbl_value = - INFINITY;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000401 return 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000402 case 0x42: /* 01000010: NOT-A-NUMBER */
403 *dbl_value = NAN;
404 return 0;
405 case 0x43: /* 01000011: minus zero */
Lev Walkin2a789d92004-09-27 21:36:59 +0000406 *dbl_value = -0.0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000407 return 0;
408 }
409
410 errno = EINVAL;
411 return -1;
Lev Walkin0959ffb2011-06-25 15:48:52 -0700412 case 0x00: { /* X.690: 8.5.7 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000413 /*
Lev Walkin5fda7d52012-01-09 03:20:19 -0800414 * Decimal. NR{1,2,3} format from ISO 6093.
415 * NR1: [ ]*[+-]?[0-9]+
416 * NR2: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)
417 * NR3: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)[Ee][+-]?[0-9]+
Lev Walkin41ba1f22004-09-14 12:46:35 +0000418 */
419 double d;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800420 char *buf;
421 char *endptr;
422 int used_malloc = 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000423
Lev Walkin5fda7d52012-01-09 03:20:19 -0800424 if(octv == 0 || (octv & 0x3C)) {
Lev Walkin0959ffb2011-06-25 15:48:52 -0700425 /* Remaining values of bits 6 to 1 are Reserved. */
426 errno = EINVAL;
427 return -1;
428 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000429
Lev Walkin5fda7d52012-01-09 03:20:19 -0800430
431 /* 1. By contract, an input buffer should be null-terminated.
432 * OCTET STRING decoder ensures that, as is asn_double2REAL().
433 * 2. ISO 6093 specifies COMMA as a possible decimal separator.
434 * However, strtod() can't always deal with COMMA.
435 * So her we fix both by reallocating, copying and fixing.
436 */
437 if(st->buf[st->size] || memchr(st->buf, ',', st->size)) {
438 uint8_t *p, *end;
439 char *b;
440 if(st->size > 100) {
441 /* Avoid malicious stack overflow in alloca() */
442 buf = (char *)MALLOC(st->size);
443 if(!buf) return -1;
444 used_malloc = 1;
445 } else {
446 buf = alloca(st->size);
447 }
448 b = buf;
449 /* Copy without the first byte and with 0-termination */
450 for(p = st->buf + 1, end = st->buf + st->size;
451 p < end; b++, p++)
452 *b = (*p == ',') ? '.' : *p;
453 *b = '\0';
454 } else {
455 buf = (char *)&st->buf[1];
456 }
457
458 endptr = buf;
459 d = strtod(buf, &endptr);
460 if(*endptr != '\0') {
461 /* Format is not consistent with ISO 6093 */
462 if(used_malloc) FREEMEM(buf);
Lev Walkin0959ffb2011-06-25 15:48:52 -0700463 errno = EINVAL;
464 return -1;
465 }
Lev Walkin5fda7d52012-01-09 03:20:19 -0800466 if(used_malloc) FREEMEM(buf);
Lev Walkindaeb2162014-01-13 23:08:35 -0800467 if(_asn_isfinite(d)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000468 *dbl_value = d;
469 return 0;
470 } else {
471 errno = ERANGE;
Lev Walkin0959ffb2011-06-25 15:48:52 -0700472 return -1;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000473 }
474 }
475 }
476
477 /*
478 * Binary representation.
479 */
480 {
481 double m;
482 int expval; /* exponent value */
483 unsigned int elen; /* exponent value length, in octets */
484 unsigned int scaleF;
485 unsigned int baseF;
486 uint8_t *ptr;
487 uint8_t *end;
488 int sign;
489
490 switch((octv & 0x30) >> 4) {
491 case 0x00: baseF = 1; break; /* base 2 */
492 case 0x01: baseF = 3; break; /* base 8 */
493 case 0x02: baseF = 4; break; /* base 16 */
494 default:
495 /* Reserved field, can't parse now. */
496 errno = EINVAL;
497 return -1;
498 }
499
500 sign = (octv & 0x40); /* bit 7 */
501 scaleF = (octv & 0x0C) >> 2; /* bits 4 to 3 */
502
Lev Walkina9cc46e2004-09-22 16:06:28 +0000503 if(st->size <= (int)(1 + (octv & 0x03))) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000504 errno = EINVAL;
505 return -1;
506 }
507
Lev Walkine78753d2007-06-23 17:02:00 +0000508 elen = (octv & 0x03); /* bits 2 to 1; 8.5.6.4 */
509 if(elen == 0x03) { /* bits 2 to 1 = 11; 8.5.6.4, case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000510 elen = st->buf[1]; /* unsigned binary number */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000511 if(elen == 0 || st->size <= (int)(2 + elen)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000512 errno = EINVAL;
513 return -1;
514 }
Lev Walkine78753d2007-06-23 17:02:00 +0000515 /* FIXME: verify constraints of case d) */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000516 ptr = &st->buf[2];
517 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000518 ptr = &st->buf[1];
519 }
520
521 /* Fetch the multibyte exponent */
522 expval = (int)(*(int8_t *)ptr);
523 end = ptr + elen + 1;
524 for(ptr++; ptr < end; ptr++)
525 expval = (expval * 256) + *ptr;
526
527 m = 0.0; /* Initial mantissa value */
528
529 /* Okay, the exponent is here. Now, what about mantissa? */
530 end = st->buf + st->size;
Lev Walkin5fda7d52012-01-09 03:20:19 -0800531 for(; ptr < end; ptr++)
532 m = ldexp(m, 8) + *ptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000533
Lev Walkin5f560912004-10-21 13:37:57 +0000534 if(0)
Lev Walkin5fda7d52012-01-09 03:20:19 -0800535 ASN_DEBUG("m=%.10f, scF=%d, bF=%d, expval=%d, ldexp()=%f, ldexp()=%f\n",
Lev Walkin41ba1f22004-09-14 12:46:35 +0000536 m, scaleF, baseF, expval,
537 ldexp(m, expval * baseF + scaleF),
Lev Walkin77aa90f2005-04-28 22:56:36 +0000538 ldexp(m, scaleF) * pow(pow(2, baseF), expval)
Lev Walkin41ba1f22004-09-14 12:46:35 +0000539 );
540
541 /*
542 * (S * N * 2^F) * B^E
543 * Essentially:
Lev Walkin77aa90f2005-04-28 22:56:36 +0000544 m = ldexp(m, scaleF) * pow(pow(2, base), expval);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000545 */
546 m = ldexp(m, expval * baseF + scaleF);
Lev Walkindaeb2162014-01-13 23:08:35 -0800547 if(_asn_isfinite(m)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000548 *dbl_value = sign ? -m : m;
549 } else {
550 errno = ERANGE;
551 return -1;
552 }
553
554 } /* if(binary_format) */
555
556 return 0;
557}
558
559/*
560 * Assume IEEE 754 floating point: standard 64 bit double.
561 * [1 bit sign] [11 bits exponent] [52 bits mantissa]
562 */
563int
Lev Walkin5e033762004-09-29 13:26:15 +0000564asn_double2REAL(REAL_t *st, double dbl_value) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000565#ifdef WORDS_BIGENDIAN /* Known to be big-endian */
566 int littleEndian = 0;
567#else /* need to test: have no explicit information */
568 unsigned int LE = 1;
569 int littleEndian = *(unsigned char *)&LE;
570#endif
571 uint8_t buf[16]; /* More than enough for 8-byte dbl_value */
572 uint8_t dscr[sizeof(dbl_value)]; /* double value scratch pad */
573 /* Assertion guards: won't even compile, if unexpected double size */
Lev Walkin50b0f6c2006-10-16 12:01:36 +0000574 char assertion_buffer1[9 - sizeof(dbl_value)] GCC_NOTUSED;
575 char assertion_buffer2[sizeof(dbl_value) - 7] GCC_NOTUSED;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000576 uint8_t *ptr = buf;
577 uint8_t *mstop; /* Last byte of mantissa */
578 unsigned int mval; /* Value of the last byte of mantissa */
579 unsigned int bmsign; /* binary mask with sign */
580 unsigned int buflen;
581 unsigned int accum;
582 int expval;
583
584 if(!st) {
585 errno = EINVAL;
586 return -1;
587 }
588
Lev Walkin057fb732004-09-14 13:58:10 +0000589 /*
590 * ilogb(+-0) returns -INT_MAX or INT_MIN (platform-dependent)
Lev Walkin2f505022005-07-01 08:28:18 +0000591 * ilogb(+-inf) returns INT_MAX, logb(+-inf) returns +inf
Lev Walkin2a789d92004-09-27 21:36:59 +0000592 * ilogb(NaN) returns INT_MIN or INT_MAX (platform-dependent)
Lev Walkin057fb732004-09-14 13:58:10 +0000593 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000594 expval = ilogb(dbl_value);
Lev Walkin2a789d92004-09-27 21:36:59 +0000595 if(expval <= -INT_MAX /* Also catches +-0 and maybe isnan() */
596 || expval == INT_MAX /* catches isfin() and maybe isnan() */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000597 ) {
598 if(!st->buf || st->size < 2) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000599 ptr = (uint8_t *)MALLOC(2);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000600 if(!ptr) return -1;
601 st->buf = ptr;
602 }
603 /* fpclassify(3) is not portable yet */
Lev Walkin2a789d92004-09-27 21:36:59 +0000604 if(isnan(dbl_value)) {
605 st->buf[0] = 0x42; /* NaN */
606 st->buf[1] = 0;
607 st->size = 1;
Lev Walkindaeb2162014-01-13 23:08:35 -0800608 } else if(!_asn_isfinite(dbl_value)) {
Lev Walkin7e03db92004-09-14 13:50:21 +0000609 if(copysign(1.0, dbl_value) < 0.0) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000610 st->buf[0] = 0x41; /* MINUS-INFINITY */
611 } else {
612 st->buf[0] = 0x40; /* PLUS-INFINITY */
613 }
614 st->buf[1] = 0;
615 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000616 } else {
Lev Walkin5fe72e92012-01-07 17:00:29 -0800617 if(copysign(1.0, dbl_value) >= 0.0) {
Lev Walkinc51e7d62004-09-27 22:16:18 +0000618 /* no content octets: positive zero */
619 st->buf[0] = 0; /* JIC */
620 st->size = 0;
Lev Walkin5fe72e92012-01-07 17:00:29 -0800621 } else {
622 /* Negative zero. #8.5.3, 8.5.9 */
623 st->buf[0] = 0x43;
624 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000625 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000626 }
627 return 0;
628 }
629
630 if(littleEndian) {
631 uint8_t *s = ((uint8_t *)&dbl_value) + sizeof(dbl_value) - 2;
Lev Walkin057fb732004-09-14 13:58:10 +0000632 uint8_t *start = ((uint8_t *)&dbl_value);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000633 uint8_t *d;
634
635 bmsign = 0x80 | ((s[1] >> 1) & 0x40); /* binary mask & - */
Lev Walkin057fb732004-09-14 13:58:10 +0000636 for(mstop = d = dscr; s >= start; d++, s--) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000637 *d = *s;
638 if(*d) mstop = d;
639 }
640 } else {
641 uint8_t *s = ((uint8_t *)&dbl_value) + 1;
642 uint8_t *end = ((uint8_t *)&dbl_value) + sizeof(double);
643 uint8_t *d;
644
645 bmsign = 0x80 | ((s[-1] >> 1) & 0x40); /* binary mask & - */
646 for(mstop = d = dscr; s < end; d++, s++) {
647 *d = *s;
648 if(*d) mstop = d;
649 }
650 }
651
652 /* Remove parts of the exponent, leave mantissa and explicit 1. */
653 dscr[0] = 0x10 | (dscr[0] & 0x0f);
654
655 /* Adjust exponent in a very unobvious way */
656 expval -= 8 * ((mstop - dscr) + 1) - 4;
657
658 /* This loop ensures DER conformance by forcing mantissa odd: 11.3.1 */
659 mval = *mstop;
660 if(mval && !(mval & 1)) {
661 unsigned int shift_count = 1;
662 unsigned int ishift;
663 uint8_t *mptr;
664
665 /*
666 * Figure out what needs to be done to make mantissa odd.
667 */
668 if(!(mval & 0x0f)) /* Speed-up a little */
669 shift_count = 4;
670 while(((mval >> shift_count) & 1) == 0)
671 shift_count++;
672
673 ishift = 8 - shift_count;
674 accum = 0;
675
676 /* Go over the buffer, shifting it shift_count bits right. */
677 for(mptr = dscr; mptr <= mstop; mptr++) {
678 mval = *mptr;
679 *mptr = accum | (mval >> shift_count);
680 accum = mval << ishift;
681 }
682
Lev Walkinb89b3402012-01-09 18:26:30 -0800683 /* Adjust exponent appropriately. */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000684 expval += shift_count;
685 }
686
687 if(expval < 0) {
688 if((expval >> 7) == -1) {
689 *ptr++ = bmsign | 0x00;
690 *ptr++ = expval;
691 } else if((expval >> 15) == -1) {
692 *ptr++ = bmsign | 0x01;
693 *ptr++ = expval >> 8;
694 *ptr++ = expval;
695 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000696 *ptr++ = bmsign | 0x02;
697 *ptr++ = expval >> 16;
698 *ptr++ = expval >> 8;
699 *ptr++ = expval;
700 }
701 } else if(expval <= 0x7f) {
702 *ptr++ = bmsign | 0x00;
703 *ptr++ = expval;
704 } else if(expval <= 0x7fff) {
705 *ptr++ = bmsign | 0x01;
706 *ptr++ = expval >> 8;
707 *ptr++ = expval;
708 } else {
709 assert(expval <= 0x7fffff);
710 *ptr++ = bmsign | 0x02;
711 *ptr++ = expval >> 16;
712 *ptr++ = expval >> 8;
713 *ptr++ = expval;
714 }
715
716 buflen = (mstop - dscr) + 1;
717 memcpy(ptr, dscr, buflen);
718 ptr += buflen;
719 buflen = ptr - buf;
720
Lev Walkinc17d90f2005-01-17 14:32:45 +0000721 ptr = (uint8_t *)MALLOC(buflen + 1);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000722 if(!ptr) return -1;
723
724 memcpy(ptr, buf, buflen);
725 buf[buflen] = 0; /* JIC */
726
727 if(st->buf) FREEMEM(st->buf);
728 st->buf = ptr;
729 st->size = buflen;
730
731 return 0;
732}