blob: 2994b741c608f0c493bdbb157992fb89a551094e [file] [log] [blame]
Harald Welte92c45f32010-06-12 18:59:38 +02001/*-
Harald Welte41b85d52015-08-31 08:56:53 +02002 * Copyright (c) 2004-2013 Lev Walkin <vlm@lionet.info>. All rights reserved.
Harald Welte92c45f32010-06-12 18:59:38 +02003 * Redistribution and modifications are permitted subject to BSD license.
4 */
Harald Welte41b85d52015-08-31 08:56:53 +02005#define _ISOC99_SOURCE /* For ilogb() and quiet NAN */
Harald Welte8d9147a2017-10-27 22:05:31 +02006#define _DEFAULT_SOURCE /* To reintroduce finite(3) */
Harald Welte41b85d52015-08-31 08:56:53 +02007#if defined(__alpha)
Harald Welte92c45f32010-06-12 18:59:38 +02008#include <sys/resource.h> /* For INFINITY */
9#endif
10#include <asn_internal.h>
11#include <stdlib.h> /* for strtod(3) */
12#include <math.h>
13#include <errno.h>
14#include <REAL.h>
Harald Welteec0e2172010-07-20 00:03:44 +020015#include <OCTET_STRING.h>
Harald Welte92c45f32010-06-12 18:59:38 +020016
17#undef INT_MAX
18#define INT_MAX ((int)(((unsigned int)-1) >> 1))
19
20#if !(defined(NAN) || defined(INFINITY))
Harald Welteec0e2172010-07-20 00:03:44 +020021static volatile double real_zero GCC_NOTUSED = 0.0;
Harald Welte92c45f32010-06-12 18:59:38 +020022#endif
23#ifndef NAN
24#define NAN (real_zero/real_zero)
25#endif
26#ifndef INFINITY
27#define INFINITY (1.0/real_zero)
28#endif
29
Harald Welte41b85d52015-08-31 08:56:53 +020030#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
Harald Welte92c45f32010-06-12 18:59:38 +020036/*
37 * REAL basic type description.
38 */
Harald Welte41b85d52015-08-31 08:56:53 +020039static const ber_tlv_tag_t asn_DEF_REAL_tags[] = {
Harald Welte92c45f32010-06-12 18:59:38 +020040 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
41};
42asn_TYPE_descriptor_t asn_DEF_REAL = {
43 "REAL",
44 "REAL",
45 ASN__PRIMITIVE_TYPE_free,
46 REAL_print,
47 asn_generic_no_constraint,
48 ber_decode_primitive,
49 der_encode_primitive,
50 REAL_decode_xer,
51 REAL_encode_xer,
Harald Welteec0e2172010-07-20 00:03:44 +020052 REAL_decode_uper,
53 REAL_encode_uper,
Harald Welte41b85d52015-08-31 08:56:53 +020054 REAL_decode_aper,
55 REAL_encode_aper,
Harald Welte92c45f32010-06-12 18:59:38 +020056 0, /* Use generic outmost tag fetcher */
57 asn_DEF_REAL_tags,
58 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
59 asn_DEF_REAL_tags, /* Same as above */
60 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
61 0, /* No PER visible constraints */
62 0, 0, /* No members */
63 0 /* No specifics */
64};
65
66typedef enum specialRealValue {
67 SRV__NOT_A_NUMBER,
68 SRV__MINUS_INFINITY,
69 SRV__PLUS_INFINITY
70} specialRealValue_e;
71static struct specialRealValue_s {
72 char *string;
73 size_t length;
74 long dv;
75} specialRealValue[] = {
76#define SRV_SET(foo, val) { foo, sizeof(foo) - 1, val }
77 SRV_SET("<NOT-A-NUMBER/>", 0),
78 SRV_SET("<MINUS-INFINITY/>", -1),
79 SRV_SET("<PLUS-INFINITY/>", 1),
80#undef SRV_SET
81};
82
83ssize_t
84REAL__dump(double d, int canonical, asn_app_consume_bytes_f *cb, void *app_key) {
85 char local_buf[64];
86 char *buf = local_buf;
87 ssize_t buflen = sizeof(local_buf);
88 const char *fmt = canonical?"%.15E":"%.15f";
89 ssize_t ret;
90
91 /*
92 * Check whether it is a special value.
93 */
94 /* fpclassify(3) is not portable yet */
95 if(isnan(d)) {
96 buf = specialRealValue[SRV__NOT_A_NUMBER].string;
97 buflen = specialRealValue[SRV__NOT_A_NUMBER].length;
98 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
Harald Welte41b85d52015-08-31 08:56:53 +020099 } else if(!_asn_isfinite(d)) {
Harald Welte92c45f32010-06-12 18:59:38 +0200100 if(copysign(1.0, d) < 0.0) {
101 buf = specialRealValue[SRV__MINUS_INFINITY].string;
102 buflen = specialRealValue[SRV__MINUS_INFINITY].length;
103 } else {
104 buf = specialRealValue[SRV__PLUS_INFINITY].string;
105 buflen = specialRealValue[SRV__PLUS_INFINITY].length;
106 }
107 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
108 } else if(ilogb(d) <= -INT_MAX) {
109 if(copysign(1.0, d) < 0.0) {
110 buf = "-0";
111 buflen = 2;
112 } else {
113 buf = "0";
114 buflen = 1;
115 }
116 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
117 }
118
119 /*
120 * Use the libc's double printing, hopefully they got it right.
121 */
122 do {
123 ret = snprintf(buf, buflen, fmt, d);
124 if(ret < 0) {
125 buflen <<= 1;
126 } else if(ret >= buflen) {
127 buflen = ret + 1;
128 } else {
129 buflen = ret;
130 break;
131 }
132 if(buf != local_buf) FREEMEM(buf);
133 buf = (char *)MALLOC(buflen);
134 if(!buf) return -1;
135 } while(1);
136
137 if(canonical) {
138 /*
139 * Transform the "[-]d.dddE+-dd" output into "[-]d.dddE[-]d"
140 * Check that snprintf() constructed the output correctly.
141 */
142 char *dot, *E;
143 char *end = buf + buflen;
144 char *last_zero;
145
146 dot = (buf[0] == 0x2d /* '-' */) ? (buf + 2) : (buf + 1);
147 if(*dot >= 0x30) {
Harald Welte41b85d52015-08-31 08:56:53 +0200148 if(buf != local_buf) FREEMEM(buf);
Harald Welte92c45f32010-06-12 18:59:38 +0200149 errno = EINVAL;
150 return -1; /* Not a dot, really */
151 }
152 *dot = 0x2e; /* Replace possible comma */
153
154 for(last_zero = dot + 2, E = dot; dot < end; E++) {
155 if(*E == 0x45) {
156 char *expptr = ++E;
157 char *s = expptr;
158 int sign;
159 if(*expptr == 0x2b /* '+' */) {
160 /* Skip the "+" */
161 buflen -= 1;
162 sign = 0;
163 } else {
164 sign = 1;
165 s++;
166 }
167 expptr++;
168 if(expptr > end) {
Harald Welte41b85d52015-08-31 08:56:53 +0200169 if(buf != local_buf) FREEMEM(buf);
Harald Welte92c45f32010-06-12 18:59:38 +0200170 errno = EINVAL;
171 return -1;
172 }
173 if(*expptr == 0x30) {
174 buflen--;
175 expptr++;
176 }
177 if(*last_zero == 0x30) {
178 *last_zero = 0x45; /* E */
179 buflen -= s - (last_zero + 1);
180 s = last_zero + 1;
181 if(sign) {
182 *s++ = 0x2d /* '-' */;
183 buflen++;
184 }
185 }
186 for(; expptr <= end; s++, expptr++)
187 *s = *expptr;
188 break;
189 } else if(*E == 0x30) {
190 if(*last_zero != 0x30)
191 last_zero = E;
192 }
193 }
194 if(E == end) {
Harald Welte41b85d52015-08-31 08:56:53 +0200195 if(buf != local_buf) FREEMEM(buf);
Harald Welte92c45f32010-06-12 18:59:38 +0200196 errno = EINVAL;
197 return -1; /* No promised E */
198 }
199 } else {
200 /*
201 * Remove trailing zeros.
202 */
203 char *end = buf + buflen;
204 char *last_zero = end;
205 int stoplooking = 0;
206 char *z;
207 for(z = end - 1; z > buf; z--) {
208 switch(*z) {
209 case 0x30:
210 if(!stoplooking)
211 last_zero = z;
212 continue;
213 case 0x31: case 0x32: case 0x33: case 0x34:
214 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
215 stoplooking = 1;
216 continue;
217 default: /* Catch dot and other separators */
218 /*
219 * Replace possible comma (which may even
220 * be not a comma at all: locale-defined).
221 */
222 *z = 0x2e;
223 if(last_zero == z + 1) { /* leave x.0 */
224 last_zero++;
225 }
226 buflen = last_zero - buf;
227 *last_zero = '\0';
228 break;
229 }
230 break;
231 }
232 }
233
234 ret = cb(buf, buflen, app_key);
235 if(buf != local_buf) FREEMEM(buf);
236 return (ret < 0) ? -1 : buflen;
237}
238
239int
240REAL_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
241 asn_app_consume_bytes_f *cb, void *app_key) {
242 const REAL_t *st = (const REAL_t *)sptr;
243 ssize_t ret;
244 double d;
245
246 (void)td; /* Unused argument */
247 (void)ilevel; /* Unused argument */
248
249 if(!st || !st->buf)
250 ret = cb("<absent>", 8, app_key);
251 else if(asn_REAL2double(st, &d))
252 ret = cb("<error>", 7, app_key);
253 else
254 ret = REAL__dump(d, 0, cb, app_key);
255
256 return (ret < 0) ? -1 : 0;
257}
258
259asn_enc_rval_t
260REAL_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
261 int ilevel, enum xer_encoder_flags_e flags,
262 asn_app_consume_bytes_f *cb, void *app_key) {
263 REAL_t *st = (REAL_t *)sptr;
264 asn_enc_rval_t er;
265 double d;
266
267 (void)ilevel;
268
269 if(!st || !st->buf || asn_REAL2double(st, &d))
270 _ASN_ENCODE_FAILED;
271
272 er.encoded = REAL__dump(d, flags & XER_F_CANONICAL, cb, app_key);
273 if(er.encoded < 0) _ASN_ENCODE_FAILED;
274
275 _ASN_ENCODED_OK(er);
276}
277
278
279/*
280 * Decode the chunk of XML text encoding REAL.
281 */
282static enum xer_pbd_rval
283REAL__xer_body_decode(asn_TYPE_descriptor_t *td, void *sptr, const void *chunk_buf, size_t chunk_size) {
284 REAL_t *st = (REAL_t *)sptr;
285 double value;
286 const char *xerdata = (const char *)chunk_buf;
287 char *endptr = 0;
288 char *b;
289
290 (void)td;
291
292 if(!chunk_size) return XPBD_BROKEN_ENCODING;
293
294 /*
295 * Decode an XMLSpecialRealValue: <MINUS-INFINITY>, etc.
296 */
297 if(xerdata[0] == 0x3c /* '<' */) {
298 size_t i;
299 for(i = 0; i < sizeof(specialRealValue)
300 / sizeof(specialRealValue[0]); i++) {
301 struct specialRealValue_s *srv = &specialRealValue[i];
302 double dv;
303
304 if(srv->length != chunk_size
305 || memcmp(srv->string, chunk_buf, chunk_size))
306 continue;
307
308 /*
309 * It could've been done using
310 * (double)srv->dv / real_zero,
311 * but it summons fp exception on some platforms.
312 */
313 switch(srv->dv) {
314 case -1: dv = - INFINITY; break;
315 case 0: dv = NAN; break;
316 case 1: dv = INFINITY; break;
317 default: return XPBD_SYSTEM_FAILURE;
318 }
319
320 if(asn_double2REAL(st, dv))
321 return XPBD_SYSTEM_FAILURE;
322
323 return XPBD_BODY_CONSUMED;
324 }
325 ASN_DEBUG("Unknown XMLSpecialRealValue");
326 return XPBD_BROKEN_ENCODING;
327 }
328
329 /*
330 * Copy chunk into the nul-terminated string, and run strtod.
331 */
332 b = (char *)MALLOC(chunk_size + 1);
333 if(!b) return XPBD_SYSTEM_FAILURE;
334 memcpy(b, chunk_buf, chunk_size);
335 b[chunk_size] = 0; /* nul-terminate */
336
337 value = strtod(b, &endptr);
338 FREEMEM(b);
339 if(endptr == b) return XPBD_BROKEN_ENCODING;
340
341 if(asn_double2REAL(st, value))
342 return XPBD_SYSTEM_FAILURE;
343
344 return XPBD_BODY_CONSUMED;
345}
346
347asn_dec_rval_t
348REAL_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
349 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
350 const void *buf_ptr, size_t size) {
351
352 return xer_decode_primitive(opt_codec_ctx, td,
353 sptr, sizeof(REAL_t), opt_mname,
354 buf_ptr, size, REAL__xer_body_decode);
355}
356
Harald Welteec0e2172010-07-20 00:03:44 +0200357asn_dec_rval_t
358REAL_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
359 asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints,
360 void **sptr, asn_per_data_t *pd) {
361 (void)constraints; /* No PER visible constraints */
362 return OCTET_STRING_decode_uper(opt_codec_ctx, td, 0, sptr, pd);
363}
364
365asn_enc_rval_t
366REAL_encode_uper(asn_TYPE_descriptor_t *td,
367 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
368 (void)constraints; /* No PER visible constraints */
369 return OCTET_STRING_encode_uper(td, 0, sptr, po);
370}
Harald Welte92c45f32010-06-12 18:59:38 +0200371
Harald Welte41b85d52015-08-31 08:56:53 +0200372asn_dec_rval_t
373REAL_decode_aper(asn_codec_ctx_t *opt_codec_ctx,
374 asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints,
375 void **sptr, asn_per_data_t *pd) {
376 (void)constraints; /* No PER visible constraints */
377 return OCTET_STRING_decode_aper(opt_codec_ctx, td, 0, sptr, pd);
378}
379
380asn_enc_rval_t
381REAL_encode_aper(asn_TYPE_descriptor_t *td,
382 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
383 (void)constraints; /* No PER visible constraints */
384 return OCTET_STRING_encode_aper(td, 0, sptr, po);
385}
386
Harald Welte92c45f32010-06-12 18:59:38 +0200387int
388asn_REAL2double(const REAL_t *st, double *dbl_value) {
389 unsigned int octv;
390
391 if(!st || !st->buf) {
392 errno = EINVAL;
393 return -1;
394 }
395
396 if(st->size == 0) {
397 *dbl_value = 0;
398 return 0;
399 }
400
401 octv = st->buf[0]; /* unsigned byte */
402
403 switch(octv & 0xC0) {
Harald Welte41b85d52015-08-31 08:56:53 +0200404 case 0x40: /* X.690: 8.5.6 a) => 8.5.9 */
Harald Welte92c45f32010-06-12 18:59:38 +0200405 /* "SpecialRealValue" */
406
407 /* Be liberal in what you accept...
Harald Welte41b85d52015-08-31 08:56:53 +0200408 * http://en.wikipedia.org/wiki/Robustness_principle
Harald Welte92c45f32010-06-12 18:59:38 +0200409 if(st->size != 1) ...
410 */
411
412 switch(st->buf[0]) {
413 case 0x40: /* 01000000: PLUS-INFINITY */
414 *dbl_value = INFINITY;
415 return 0;
416 case 0x41: /* 01000001: MINUS-INFINITY */
417 *dbl_value = - INFINITY;
418 return 0;
Harald Welte92c45f32010-06-12 18:59:38 +0200419 case 0x42: /* 01000010: NOT-A-NUMBER */
420 *dbl_value = NAN;
421 return 0;
422 case 0x43: /* 01000011: minus zero */
423 *dbl_value = -0.0;
424 return 0;
425 }
426
427 errno = EINVAL;
428 return -1;
Harald Welte41b85d52015-08-31 08:56:53 +0200429 case 0x00: { /* X.690: 8.5.7 */
Harald Welte92c45f32010-06-12 18:59:38 +0200430 /*
Harald Welte41b85d52015-08-31 08:56:53 +0200431 * Decimal. NR{1,2,3} format from ISO 6093.
432 * NR1: [ ]*[+-]?[0-9]+
433 * NR2: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)
434 * NR3: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)[Ee][+-]?[0-9]+
Harald Welte92c45f32010-06-12 18:59:38 +0200435 */
436 double d;
Harald Welte41b85d52015-08-31 08:56:53 +0200437 char *buf;
438 char *endptr;
439 int used_malloc = 0;
Harald Welte92c45f32010-06-12 18:59:38 +0200440
Harald Welte41b85d52015-08-31 08:56:53 +0200441 if(octv == 0 || (octv & 0x3C)) {
442 /* Remaining values of bits 6 to 1 are Reserved. */
443 errno = EINVAL;
444 return -1;
445 }
Harald Welte92c45f32010-06-12 18:59:38 +0200446
Harald Welte41b85d52015-08-31 08:56:53 +0200447
448 /* 1. By contract, an input buffer should be null-terminated.
449 * OCTET STRING decoder ensures that, as is asn_double2REAL().
450 * 2. ISO 6093 specifies COMMA as a possible decimal separator.
451 * However, strtod() can't always deal with COMMA.
452 * So her we fix both by reallocating, copying and fixing.
453 */
454 if(st->buf[st->size] || memchr(st->buf, ',', st->size)) {
455 uint8_t *p, *end;
456 char *b;
457 if(st->size > 100) {
458 /* Avoid malicious stack overflow in alloca() */
459 buf = (char *)MALLOC(st->size);
460 if(!buf) return -1;
461 used_malloc = 1;
462 } else {
463 buf = alloca(st->size);
464 }
465 b = buf;
466 /* Copy without the first byte and with 0-termination */
467 for(p = st->buf + 1, end = st->buf + st->size;
468 p < end; b++, p++)
469 *b = (*p == ',') ? '.' : *p;
470 *b = '\0';
471 } else {
472 buf = (char *)&st->buf[1];
473 }
474
475 endptr = buf;
476 d = strtod(buf, &endptr);
477 if(*endptr != '\0') {
478 /* Format is not consistent with ISO 6093 */
479 if(used_malloc) FREEMEM(buf);
480 errno = EINVAL;
481 return -1;
482 }
483 if(used_malloc) FREEMEM(buf);
484 if(_asn_isfinite(d)) {
Harald Welte92c45f32010-06-12 18:59:38 +0200485 *dbl_value = d;
486 return 0;
487 } else {
488 errno = ERANGE;
Harald Welte41b85d52015-08-31 08:56:53 +0200489 return -1;
Harald Welte92c45f32010-06-12 18:59:38 +0200490 }
491 }
492 }
493
494 /*
495 * Binary representation.
496 */
497 {
498 double m;
499 int expval; /* exponent value */
500 unsigned int elen; /* exponent value length, in octets */
501 unsigned int scaleF;
502 unsigned int baseF;
503 uint8_t *ptr;
504 uint8_t *end;
505 int sign;
506
507 switch((octv & 0x30) >> 4) {
508 case 0x00: baseF = 1; break; /* base 2 */
509 case 0x01: baseF = 3; break; /* base 8 */
510 case 0x02: baseF = 4; break; /* base 16 */
511 default:
512 /* Reserved field, can't parse now. */
513 errno = EINVAL;
514 return -1;
515 }
516
517 sign = (octv & 0x40); /* bit 7 */
518 scaleF = (octv & 0x0C) >> 2; /* bits 4 to 3 */
519
520 if(st->size <= (int)(1 + (octv & 0x03))) {
521 errno = EINVAL;
522 return -1;
523 }
524
Harald Welteec0e2172010-07-20 00:03:44 +0200525 elen = (octv & 0x03); /* bits 2 to 1; 8.5.6.4 */
526 if(elen == 0x03) { /* bits 2 to 1 = 11; 8.5.6.4, case d) */
Harald Welte92c45f32010-06-12 18:59:38 +0200527 elen = st->buf[1]; /* unsigned binary number */
528 if(elen == 0 || st->size <= (int)(2 + elen)) {
529 errno = EINVAL;
530 return -1;
531 }
Harald Welteec0e2172010-07-20 00:03:44 +0200532 /* FIXME: verify constraints of case d) */
Harald Welte92c45f32010-06-12 18:59:38 +0200533 ptr = &st->buf[2];
534 } else {
Harald Welte92c45f32010-06-12 18:59:38 +0200535 ptr = &st->buf[1];
536 }
537
538 /* Fetch the multibyte exponent */
539 expval = (int)(*(int8_t *)ptr);
540 end = ptr + elen + 1;
541 for(ptr++; ptr < end; ptr++)
542 expval = (expval * 256) + *ptr;
543
544 m = 0.0; /* Initial mantissa value */
545
546 /* Okay, the exponent is here. Now, what about mantissa? */
547 end = st->buf + st->size;
Harald Welte41b85d52015-08-31 08:56:53 +0200548 for(; ptr < end; ptr++)
549 m = ldexp(m, 8) + *ptr;
Harald Welte92c45f32010-06-12 18:59:38 +0200550
551 if(0)
Harald Welte41b85d52015-08-31 08:56:53 +0200552 ASN_DEBUG("m=%.10f, scF=%d, bF=%d, expval=%d, ldexp()=%f, ldexp()=%f\n",
Harald Welte92c45f32010-06-12 18:59:38 +0200553 m, scaleF, baseF, expval,
554 ldexp(m, expval * baseF + scaleF),
555 ldexp(m, scaleF) * pow(pow(2, baseF), expval)
556 );
557
558 /*
559 * (S * N * 2^F) * B^E
560 * Essentially:
561 m = ldexp(m, scaleF) * pow(pow(2, base), expval);
562 */
563 m = ldexp(m, expval * baseF + scaleF);
Harald Welte41b85d52015-08-31 08:56:53 +0200564 if(_asn_isfinite(m)) {
Harald Welte92c45f32010-06-12 18:59:38 +0200565 *dbl_value = sign ? -m : m;
566 } else {
567 errno = ERANGE;
568 return -1;
569 }
570
571 } /* if(binary_format) */
572
573 return 0;
574}
575
576/*
577 * Assume IEEE 754 floating point: standard 64 bit double.
578 * [1 bit sign] [11 bits exponent] [52 bits mantissa]
579 */
580int
581asn_double2REAL(REAL_t *st, double dbl_value) {
582#ifdef WORDS_BIGENDIAN /* Known to be big-endian */
583 int littleEndian = 0;
584#else /* need to test: have no explicit information */
585 unsigned int LE = 1;
586 int littleEndian = *(unsigned char *)&LE;
587#endif
588 uint8_t buf[16]; /* More than enough for 8-byte dbl_value */
589 uint8_t dscr[sizeof(dbl_value)]; /* double value scratch pad */
590 /* Assertion guards: won't even compile, if unexpected double size */
Harald Welteec0e2172010-07-20 00:03:44 +0200591 char assertion_buffer1[9 - sizeof(dbl_value)] GCC_NOTUSED;
592 char assertion_buffer2[sizeof(dbl_value) - 7] GCC_NOTUSED;
Harald Welte92c45f32010-06-12 18:59:38 +0200593 uint8_t *ptr = buf;
594 uint8_t *mstop; /* Last byte of mantissa */
595 unsigned int mval; /* Value of the last byte of mantissa */
596 unsigned int bmsign; /* binary mask with sign */
597 unsigned int buflen;
598 unsigned int accum;
599 int expval;
600
601 if(!st) {
602 errno = EINVAL;
603 return -1;
604 }
605
606 /*
607 * ilogb(+-0) returns -INT_MAX or INT_MIN (platform-dependent)
608 * ilogb(+-inf) returns INT_MAX, logb(+-inf) returns +inf
609 * ilogb(NaN) returns INT_MIN or INT_MAX (platform-dependent)
610 */
611 expval = ilogb(dbl_value);
612 if(expval <= -INT_MAX /* Also catches +-0 and maybe isnan() */
613 || expval == INT_MAX /* catches isfin() and maybe isnan() */
614 ) {
615 if(!st->buf || st->size < 2) {
616 ptr = (uint8_t *)MALLOC(2);
617 if(!ptr) return -1;
618 st->buf = ptr;
619 }
620 /* fpclassify(3) is not portable yet */
621 if(isnan(dbl_value)) {
622 st->buf[0] = 0x42; /* NaN */
623 st->buf[1] = 0;
624 st->size = 1;
Harald Welte41b85d52015-08-31 08:56:53 +0200625 } else if(!_asn_isfinite(dbl_value)) {
Harald Welte92c45f32010-06-12 18:59:38 +0200626 if(copysign(1.0, dbl_value) < 0.0) {
627 st->buf[0] = 0x41; /* MINUS-INFINITY */
628 } else {
629 st->buf[0] = 0x40; /* PLUS-INFINITY */
630 }
631 st->buf[1] = 0;
632 st->size = 1;
633 } else {
Harald Welte41b85d52015-08-31 08:56:53 +0200634 if(copysign(1.0, dbl_value) >= 0.0) {
Harald Welte92c45f32010-06-12 18:59:38 +0200635 /* no content octets: positive zero */
636 st->buf[0] = 0; /* JIC */
637 st->size = 0;
Harald Welte41b85d52015-08-31 08:56:53 +0200638 } else {
639 /* Negative zero. #8.5.3, 8.5.9 */
640 st->buf[0] = 0x43;
641 st->size = 1;
Harald Welte92c45f32010-06-12 18:59:38 +0200642 }
643 }
644 return 0;
645 }
646
647 if(littleEndian) {
648 uint8_t *s = ((uint8_t *)&dbl_value) + sizeof(dbl_value) - 2;
649 uint8_t *start = ((uint8_t *)&dbl_value);
650 uint8_t *d;
651
652 bmsign = 0x80 | ((s[1] >> 1) & 0x40); /* binary mask & - */
653 for(mstop = d = dscr; s >= start; d++, s--) {
654 *d = *s;
655 if(*d) mstop = d;
656 }
657 } else {
658 uint8_t *s = ((uint8_t *)&dbl_value) + 1;
659 uint8_t *end = ((uint8_t *)&dbl_value) + sizeof(double);
660 uint8_t *d;
661
662 bmsign = 0x80 | ((s[-1] >> 1) & 0x40); /* binary mask & - */
663 for(mstop = d = dscr; s < end; d++, s++) {
664 *d = *s;
665 if(*d) mstop = d;
666 }
667 }
668
669 /* Remove parts of the exponent, leave mantissa and explicit 1. */
670 dscr[0] = 0x10 | (dscr[0] & 0x0f);
671
672 /* Adjust exponent in a very unobvious way */
673 expval -= 8 * ((mstop - dscr) + 1) - 4;
674
675 /* This loop ensures DER conformance by forcing mantissa odd: 11.3.1 */
676 mval = *mstop;
677 if(mval && !(mval & 1)) {
678 unsigned int shift_count = 1;
679 unsigned int ishift;
680 uint8_t *mptr;
681
682 /*
683 * Figure out what needs to be done to make mantissa odd.
684 */
685 if(!(mval & 0x0f)) /* Speed-up a little */
686 shift_count = 4;
687 while(((mval >> shift_count) & 1) == 0)
688 shift_count++;
689
690 ishift = 8 - shift_count;
691 accum = 0;
692
693 /* Go over the buffer, shifting it shift_count bits right. */
694 for(mptr = dscr; mptr <= mstop; mptr++) {
695 mval = *mptr;
696 *mptr = accum | (mval >> shift_count);
697 accum = mval << ishift;
698 }
699
Harald Welte41b85d52015-08-31 08:56:53 +0200700 /* Adjust exponent appropriately. */
Harald Welte92c45f32010-06-12 18:59:38 +0200701 expval += shift_count;
702 }
703
704 if(expval < 0) {
705 if((expval >> 7) == -1) {
706 *ptr++ = bmsign | 0x00;
707 *ptr++ = expval;
708 } else if((expval >> 15) == -1) {
709 *ptr++ = bmsign | 0x01;
710 *ptr++ = expval >> 8;
711 *ptr++ = expval;
712 } else {
713 *ptr++ = bmsign | 0x02;
714 *ptr++ = expval >> 16;
715 *ptr++ = expval >> 8;
716 *ptr++ = expval;
717 }
718 } else if(expval <= 0x7f) {
719 *ptr++ = bmsign | 0x00;
720 *ptr++ = expval;
721 } else if(expval <= 0x7fff) {
722 *ptr++ = bmsign | 0x01;
723 *ptr++ = expval >> 8;
724 *ptr++ = expval;
725 } else {
726 assert(expval <= 0x7fffff);
727 *ptr++ = bmsign | 0x02;
728 *ptr++ = expval >> 16;
729 *ptr++ = expval >> 8;
730 *ptr++ = expval;
731 }
732
733 buflen = (mstop - dscr) + 1;
734 memcpy(ptr, dscr, buflen);
735 ptr += buflen;
736 buflen = ptr - buf;
737
738 ptr = (uint8_t *)MALLOC(buflen + 1);
739 if(!ptr) return -1;
740
741 memcpy(ptr, buf, buflen);
742 buf[buflen] = 0; /* JIC */
743
744 if(st->buf) FREEMEM(st->buf);
745 st->buf = ptr;
746 st->size = buflen;
747
748 return 0;
749}