2 * Copyright (c) 2004-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
5 #include <asn_internal.h>
6 #include <stdlib.h> /* for strtod(3) */
11 #define INT_MAX ((int)(((unsigned int)-1) >> 1))
13 struct specialRealValue_s specialRealValue[] = {
14 #define SRV_SET(foo, val) { (char *)foo, sizeof(foo) - 1, val }
15 SRV_SET("<NOT-A-NUMBER/>", 0),
16 SRV_SET("<MINUS-INFINITY/>", -1),
17 SRV_SET("<PLUS-INFINITY/>", 1),
21 #if defined(__clang__)
23 * isnan() is defined using generic selections and won't compile in
24 * strict C89 mode because of too fancy system's standard library.
25 * However, prior to C11 the math had a perfectly working isnan()
26 * in the math library.
27 * Disable generic selection warning so we can test C89 mode with newer libc.
29 #pragma clang diagnostic push
30 #pragma clang diagnostic ignored "-Wc11-extensions"
31 static int asn_isnan(double d) {
34 static int asn_isfinite(double d) {
36 return isfinite(d); /* ISO C99 */
38 return finite(d); /* Deprecated on Mac OS X 10.9 */
41 #pragma clang diagnostic pop
43 #define asn_isnan(v) isnan(v)
45 #define asn_isfinite(d) isfinite(d) /* ISO C99 */
47 #define asn_isfinite(d) finite(d) /* Deprecated on Mac OS X 10.9 */
52 * REAL basic type description.
54 static const ber_tlv_tag_t asn_DEF_REAL_tags[] = {
55 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
57 asn_TYPE_operation_t asn_OP_REAL = {
58 ASN__PRIMITIVE_TYPE_free,
59 #if !defined(ASN_DISABLE_PRINT_SUPPORT)
63 #endif /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
65 #if !defined(ASN_DISABLE_BER_SUPPORT)
71 #endif /* !defined(ASN_DISABLE_BER_SUPPORT) */
72 #if !defined(ASN_DISABLE_XER_SUPPORT)
78 #endif /* !defined(ASN_DISABLE_XER_SUPPORT) */
79 #if !defined(ASN_DISABLE_JER_SUPPORT)
83 #endif /* !defined(ASN_DISABLE_JER_SUPPORT) */
84 #if !defined(ASN_DISABLE_OER_SUPPORT)
90 #endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
91 #if !defined(ASN_DISABLE_UPER_SUPPORT)
97 #endif /* !defined(ASN_DISABLE_UPER_SUPPORT) */
98 #if !defined(ASN_DISABLE_APER_SUPPORT)
104 #endif /* !defined(ASN_DISABLE_APER_SUPPORT) */
105 #if !defined(ASN_DISABLE_RFILL_SUPPORT)
109 #endif /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
110 0 /* Use generic outmost tag fetcher */
112 asn_TYPE_descriptor_t asn_DEF_REAL = {
117 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
118 asn_DEF_REAL_tags, /* Same as above */
119 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
121 #if !defined(ASN_DISABLE_OER_SUPPORT)
123 #endif /* !defined(ASN_DISABLE_OER_SUPPORT) */
124 #if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
126 #endif /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
127 asn_generic_no_constraint
129 0, 0, /* No members */
134 REAL__dump(double d, int canonical, asn_app_consume_bytes_f *cb, void *app_key) {
136 char *buf = local_buf;
137 ssize_t buflen = sizeof(local_buf);
141 * Check whether it is a special value.
143 /* fpclassify(3) is not portable yet */
145 buf = specialRealValue[SRV__NOT_A_NUMBER].string;
146 buflen = specialRealValue[SRV__NOT_A_NUMBER].length;
147 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
148 } else if(!asn_isfinite(d)) {
149 if(copysign(1.0, d) < 0.0) {
150 buf = specialRealValue[SRV__MINUS_INFINITY].string;
151 buflen = specialRealValue[SRV__MINUS_INFINITY].length;
153 buf = specialRealValue[SRV__PLUS_INFINITY].string;
154 buflen = specialRealValue[SRV__PLUS_INFINITY].length;
156 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
157 } else if(ilogb(d) <= -INT_MAX) {
158 if(copysign(1.0, d) < 0.0) {
165 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
169 * Use the libc's double printing, hopefully they got it right.
174 canonical ? "%.17E" /* Precise */ : "%.15f" /* Pleasant*/,
177 /* There are some old broken APIs. */
180 /* Should be plenty. */
181 if(buf != local_buf) FREEMEM(buf);
184 } else if(ret >= buflen) {
190 if(buf != local_buf) FREEMEM(buf);
191 buf = (char *)MALLOC(buflen);
197 * Transform the "[-]d.dddE+-dd" output into "[-]d.dddE[-]d"
198 * Check that snprintf() constructed the output correctly.
201 char *end = buf + buflen;
203 char *first_zero_in_run;
209 } lz_state = LZSTATE_NOTHING;
211 dot = (buf[0] == 0x2d /* '-' */) ? (buf + 2) : (buf + 1);
213 if(buf != local_buf) FREEMEM(buf);
215 return -1; /* Not a dot, really */
217 *dot = 0x2e; /* Replace possible comma */
219 for(first_zero_in_run = last_zero = s = dot + 2; s < end; s++) {
222 if(lz_state == LZSTATE_ZEROES) last_zero = first_zero_in_run;
225 if(lz_state == LZSTATE_NOTHING) first_zero_in_run = s;
226 lz_state = LZSTATE_ZEROES;
229 lz_state = LZSTATE_NOTHING;
236 if(buf != local_buf) FREEMEM(buf);
238 return -1; /* No promised E */
249 if(*expptr == 0x2b /* '+' */) {
259 if(buf != local_buf) FREEMEM(buf);
263 if(*expptr == 0x30) {
267 if(lz_state == LZSTATE_ZEROES) {
268 *last_zero = 0x45; /* E */
269 buflen -= s - (last_zero + 1);
272 *s++ = 0x2d /* '-' */;
276 for(; expptr <= end; s++, expptr++)
281 * Remove trailing zeros.
283 char *end = buf + buflen;
284 char *last_zero = end;
287 for(z = end - 1; z > buf; z--) {
293 case 0x31: case 0x32: case 0x33: case 0x34:
294 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
297 default: /* Catch dot and other separators */
299 * Replace possible comma (which may even
300 * be not a comma at all: locale-defined).
303 if(last_zero == z + 1) { /* leave x.0 */
306 buflen = last_zero - buf;
314 ret = cb(buf, buflen, app_key);
315 if(buf != local_buf) FREEMEM(buf);
316 return (ret < 0) ? -1 : buflen;
320 REAL_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
322 const REAL_t *a = aptr;
323 const REAL_t *b = bptr;
330 ra = asn_REAL2double(a, &adbl);
331 rb = asn_REAL2double(b, &bdbl);
332 if(ra == 0 && rb == 0) {
333 if(asn_isnan(adbl)) {
334 if(asn_isnan(bdbl)) {
339 } else if(asn_isnan(bdbl)) {
342 /* Value comparison. */
345 } else if(adbl > bdbl) {
363 asn_REAL2double(const REAL_t *st, double *dbl_value) {
366 if(!st || !st->buf) {
376 octv = st->buf[0]; /* unsigned byte */
378 switch(octv & 0xC0) {
379 case 0x40: /* X.690: 8.5.6 a) => 8.5.9 */
380 /* "SpecialRealValue" */
382 /* Be liberal in what you accept...
383 * http://en.wikipedia.org/wiki/Robustness_principle
384 if(st->size != 1) ...
388 case 0x40: /* 01000000: PLUS-INFINITY */
389 *dbl_value = INFINITY;
391 case 0x41: /* 01000001: MINUS-INFINITY */
392 *dbl_value = - INFINITY;
394 case 0x42: /* 01000010: NOT-A-NUMBER */
397 case 0x43: /* 01000011: minus zero */
404 case 0x00: { /* X.690: 8.5.7 */
406 * Decimal. NR{1,2,3} format from ISO 6093.
407 * NR1: [ ]*[+-]?[0-9]+
408 * NR2: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)
409 * NR3: [ ]*[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)[Ee][+-]?[0-9]+
416 if(octv == 0 || (octv & 0x3C)) {
417 /* Remaining values of bits 6 to 1 are Reserved. */
422 /* 1. By contract, an input buffer should be '\0'-terminated.
423 * OCTET STRING decoder ensures that, as is asn_double2REAL().
424 * 2. ISO 6093 specifies COMMA as a possible decimal separator.
425 * However, strtod() can't always deal with COMMA.
426 * So her we fix both by reallocating, copying and fixing.
428 if(st->buf[st->size] != '\0' || memchr(st->buf, ',', st->size)) {
429 const uint8_t *p, *end;
432 b = source = (char *)MALLOC(st->size + 1);
433 if(!source) return -1;
436 /* Copy without the first byte and with 0-termination */
437 for(p = st->buf + 1, end = st->buf + st->size;
439 *b = (*p == ',') ? '.' : *p;
442 source = (char *)&st->buf[1];
446 d = strtod(source, &endptr);
447 if(*endptr != '\0') {
448 /* Format is not consistent with ISO 6093 */
449 if(used_malloc) FREEMEM(source);
453 if(used_malloc) FREEMEM(source);
454 if(asn_isfinite(d)) {
465 * Binary representation.
469 int32_t expval; /* exponent value */
470 unsigned int elen; /* exponent value length, in octets */
477 switch((octv & 0x30) >> 4) {
478 case 0x00: baseF = 1; break; /* base 2 */
479 case 0x01: baseF = 3; break; /* base 8 */
480 case 0x02: baseF = 4; break; /* base 16 */
482 /* Reserved field, can't parse now. */
487 sign = (octv & 0x40); /* bit 7 */
488 scaleF = (octv & 0x0C) >> 2; /* bits 4 to 3 */
490 if(st->size <= 1 + (octv & 0x03)) {
495 elen = (octv & 0x03); /* bits 2 to 1; 8.5.6.4 */
496 if(elen == 0x03) { /* bits 2 to 1 = 11; 8.5.6.4, case d) */
497 elen = st->buf[1]; /* unsigned binary number */
498 if(elen == 0 || st->size <= (2 + elen)) {
502 /* FIXME: verify constraints of case d) */
508 /* Fetch the multibyte exponent */
509 expval = (int)(*(int8_t *)ptr);
510 if(elen >= sizeof(expval)-1) {
514 end = ptr + elen + 1;
515 for(ptr++; ptr < end; ptr++)
516 expval = (expval * 256) + *ptr;
518 m = 0.0; /* Initial mantissa value */
520 /* Okay, the exponent is here. Now, what about mantissa? */
521 end = st->buf + st->size;
522 for(; ptr < end; ptr++)
523 m = ldexp(m, 8) + *ptr;
526 ASN_DEBUG("m=%.10f, scF=%d, bF=%d, expval=%d, ldexp()=%f, ldexp()=%f\n",
527 m, scaleF, baseF, expval,
528 ldexp(m, expval * baseF + scaleF),
529 ldexp(m, scaleF) * pow(pow(2, baseF), expval)
533 * (S * N * 2^F) * B^E
535 m = ldexp(m, scaleF) * pow(pow(2, baseF), expval);
537 m = ldexp(m, expval * baseF + scaleF);
538 if(asn_isfinite(m)) {
539 *dbl_value = sign ? -m : m;
545 } /* if(binary_format) */
551 * Assume IEEE 754 floating point: standard 64 bit double.
552 * [1 bit sign] [11 bits exponent] [52 bits mantissa]
555 asn_double2REAL(REAL_t *st, double dbl_value) {
557 int float_big_endian = *(const char *)&test != 0;
558 uint8_t buf[16]; /* More than enough for 8-byte dbl_value */
559 uint8_t dscr[sizeof(dbl_value)]; /* double value scratch pad */
560 /* Assertion guards: won't even compile, if unexpected double size */
561 char assertion_buffer1[9 - sizeof(dbl_value)] CC_NOTUSED;
562 char assertion_buffer2[sizeof(dbl_value) - 7] CC_NOTUSED;
564 uint8_t *mstop; /* Last byte of mantissa */
565 unsigned int mval; /* Value of the last byte of mantissa */
566 unsigned int bmsign; /* binary mask with sign */
577 * ilogb(+-0) returns -INT_MAX or INT_MIN (platform-dependent)
578 * ilogb(+-inf) returns INT_MAX, logb(+-inf) returns +inf
579 * ilogb(NaN) returns INT_MIN or INT_MAX (platform-dependent)
581 expval = ilogb(dbl_value);
582 if(expval <= -INT_MAX /* Also catches +-0 and maybe isnan() */
583 || expval == INT_MAX /* catches isfin() and maybe isnan() */
585 if(!st->buf || st->size < 2) {
586 ptr = (uint8_t *)MALLOC(2);
588 if(st->buf) FREEMEM(st->buf);
591 /* fpclassify(3) is not portable yet */
592 if(asn_isnan(dbl_value)) {
593 st->buf[0] = 0x42; /* NaN */
596 } else if(!asn_isfinite(dbl_value)) {
597 if(copysign(1.0, dbl_value) < 0.0) {
598 st->buf[0] = 0x41; /* MINUS-INFINITY */
600 st->buf[0] = 0x40; /* PLUS-INFINITY */
605 if(copysign(1.0, dbl_value) >= 0.0) {
606 /* no content octets: positive zero */
607 st->buf[0] = 0; /* JIC */
610 /* Negative zero. #8.5.3, 8.5.9 */
619 if(float_big_endian) {
620 uint8_t *s = ((uint8_t *)&dbl_value) + 1;
621 uint8_t *end = ((uint8_t *)&dbl_value) + sizeof(double);
624 bmsign = 0x80 | ((s[-1] >> 1) & 0x40); /* binary mask & - */
625 for(mstop = d = dscr; s < end; d++, s++) {
630 uint8_t *s = ((uint8_t *)&dbl_value) + sizeof(dbl_value) - 2;
631 uint8_t *start = ((uint8_t *)&dbl_value);
634 bmsign = 0x80 | ((s[1] >> 1) & 0x40); /* binary mask & - */
635 for(mstop = d = dscr; s >= start; d++, s--) {
641 /* Remove parts of the exponent, leave mantissa and explicit 1. */
642 dscr[0] = 0x10 | (dscr[0] & 0x0f);
644 /* Adjust exponent in a very unobvious way */
645 expval -= 8 * ((mstop - dscr) + 1) - 4;
647 /* This loop ensures DER conformance by forcing mantissa odd: 11.3.1 */
649 if(mval && !(mval & 1)) {
655 * Figure out what needs to be done to make mantissa odd.
657 if(!(mval & 0x0f)) /* Speed-up a little */
659 while(((mval >> shift_count) & 1) == 0)
662 ishift = 8 - shift_count;
665 /* Go over the buffer, shifting it shift_count bits right. */
666 for(mptr = dscr; mptr <= mstop; mptr++) {
668 *mptr = accum | (mval >> shift_count);
669 accum = mval << ishift;
672 /* Adjust exponent appropriately. */
673 expval += shift_count;
677 if((expval >> 7) == -1) {
678 *ptr++ = bmsign | 0x00;
680 } else if((expval >> 15) == -1) {
681 *ptr++ = bmsign | 0x01;
682 *ptr++ = expval >> 8;
685 *ptr++ = bmsign | 0x02;
686 *ptr++ = expval >> 16;
687 *ptr++ = expval >> 8;
690 } else if(expval <= 0x7f) {
691 *ptr++ = bmsign | 0x00;
693 } else if(expval <= 0x7fff) {
694 *ptr++ = bmsign | 0x01;
695 *ptr++ = expval >> 8;
698 assert(expval <= 0x7fffff);
699 *ptr++ = bmsign | 0x02;
700 *ptr++ = expval >> 16;
701 *ptr++ = expval >> 8;
705 buflen = (mstop - dscr) + 1;
706 memcpy(ptr, dscr, buflen);
710 ptr = (uint8_t *)MALLOC(buflen + 1);
713 memcpy(ptr, buf, buflen);
714 buf[buflen] = 0; /* JIC */
716 if(st->buf) FREEMEM(st->buf);
723 int CC_ATTR_NO_SANITIZE("float-cast-overflow")
724 asn_double2float(double d, float *outcome) {
729 if(asn_isfinite(d) == asn_isfinite(f)) {