SIM-115: update simulator to use latest E2SM KPM version 3
[sim/e2-interface.git] / e2sim / asn1c / REAL.c
1 /*-
2  * Copyright (c) 2004-2017 Lev Walkin <vlm@lionet.info>. All rights reserved.
3  * Redistribution and modifications are permitted subject to BSD license.
4  */
5 #include <asn_internal.h>
6 #include <stdlib.h>     /* for strtod(3) */
7 #include <errno.h>
8 #include <REAL.h>
9
10 #undef  INT_MAX
11 #define INT_MAX ((int)(((unsigned int)-1) >> 1))
12
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),
18 #undef SRV_SET
19 };
20
21 #if defined(__clang__)
22 /*
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.
28  */
29 #pragma clang diagnostic push
30 #pragma clang diagnostic ignored "-Wc11-extensions"
31 static int asn_isnan(double d) {
32     return isnan(d);
33 }
34 static int asn_isfinite(double d) {
35 #ifdef isfinite
36     return isfinite(d);  /* ISO C99 */
37 #else
38     return finite(d);    /* Deprecated on Mac OS X 10.9 */
39 #endif
40 }
41 #pragma clang diagnostic pop
42 #else   /* !clang */
43 #define asn_isnan(v)    isnan(v)
44 #ifdef isfinite
45 #define asn_isfinite(d)   isfinite(d)  /* ISO C99 */
46 #else
47 #define asn_isfinite(d)   finite(d)    /* Deprecated on Mac OS X 10.9 */
48 #endif
49 #endif  /* clang */
50
51 /*
52  * REAL basic type description.
53  */
54 static const ber_tlv_tag_t asn_DEF_REAL_tags[] = {
55     (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
56 };
57 asn_TYPE_operation_t asn_OP_REAL = {
58     ASN__PRIMITIVE_TYPE_free,
59 #if !defined(ASN_DISABLE_PRINT_SUPPORT)
60     REAL_print,
61 #else
62     0,
63 #endif  /* !defined(ASN_DISABLE_PRINT_SUPPORT) */
64     REAL_compare,
65 #if !defined(ASN_DISABLE_BER_SUPPORT)
66     ber_decode_primitive,
67     der_encode_primitive,
68 #else
69     0,
70     0,
71 #endif  /* !defined(ASN_DISABLE_BER_SUPPORT) */
72 #if !defined(ASN_DISABLE_XER_SUPPORT)
73     REAL_decode_xer,
74     REAL_encode_xer,
75 #else
76     0,
77     0,
78 #endif  /* !defined(ASN_DISABLE_XER_SUPPORT) */
79 #if !defined(ASN_DISABLE_JER_SUPPORT)
80     REAL_encode_jer,
81 #else
82     0,
83 #endif  /* !defined(ASN_DISABLE_JER_SUPPORT) */
84 #if !defined(ASN_DISABLE_OER_SUPPORT)
85     REAL_decode_oer,
86     REAL_encode_oer,
87 #else
88     0,
89     0,
90 #endif  /* !defined(ASN_DISABLE_OER_SUPPORT) */
91 #if !defined(ASN_DISABLE_UPER_SUPPORT)
92     REAL_decode_uper,
93     REAL_encode_uper,
94 #else
95     0,
96     0,
97 #endif  /* !defined(ASN_DISABLE_UPER_SUPPORT) */
98 #if !defined(ASN_DISABLE_APER_SUPPORT)
99     REAL_decode_aper,
100     REAL_encode_aper,
101 #else
102     0,
103     0,
104 #endif  /* !defined(ASN_DISABLE_APER_SUPPORT) */
105 #if !defined(ASN_DISABLE_RFILL_SUPPORT)
106     REAL_random_fill,
107 #else
108     0,
109 #endif  /* !defined(ASN_DISABLE_RFILL_SUPPORT) */
110     0  /* Use generic outmost tag fetcher */
111 };
112 asn_TYPE_descriptor_t asn_DEF_REAL = {
113     "REAL",
114     "REAL",
115     &asn_OP_REAL,
116     asn_DEF_REAL_tags,
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]),
120     {
121 #if !defined(ASN_DISABLE_OER_SUPPORT)
122         0,
123 #endif  /* !defined(ASN_DISABLE_OER_SUPPORT) */
124 #if !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT)
125         0,
126 #endif  /* !defined(ASN_DISABLE_UPER_SUPPORT) || !defined(ASN_DISABLE_APER_SUPPORT) */
127         asn_generic_no_constraint
128     },
129     0, 0,  /* No members */
130     0  /* No specifics */
131 };
132
133 ssize_t
134 REAL__dump(double d, int canonical, asn_app_consume_bytes_f *cb, void *app_key) {
135         char local_buf[64];
136         char *buf = local_buf;
137         ssize_t buflen = sizeof(local_buf);
138         ssize_t ret;
139
140         /*
141          * Check whether it is a special value.
142          */
143         /* fpclassify(3) is not portable yet */
144         if(asn_isnan(d)) {
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;
152                 } else {
153                         buf = specialRealValue[SRV__PLUS_INFINITY].string;
154                         buflen = specialRealValue[SRV__PLUS_INFINITY].length;
155                 }
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) {
159                         buf = "-0";
160                         buflen = 2;
161                 } else {
162                         buf = "0";
163                         buflen = 1;
164                 }
165                 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
166         }
167
168         /*
169          * Use the libc's double printing, hopefully they got it right.
170          */
171         do {
172         ret = snprintf(buf,
173                        buflen,
174                        canonical ? "%.17E" /* Precise */ : "%.15f" /* Pleasant*/,
175                        d);
176                 if(ret < 0) {
177                         /* There are some old broken APIs. */
178                         buflen <<= 1;
179                         if(buflen > 4096) {
180                                 /* Should be plenty. */
181                                 if(buf != local_buf) FREEMEM(buf);
182                                 return -1;
183                         }
184                 } else if(ret >= buflen) {
185                         buflen = ret + 1;
186                 } else {
187                         buflen = ret;
188                         break;
189                 }
190                 if(buf != local_buf) FREEMEM(buf);
191                 buf = (char *)MALLOC(buflen);
192                 if(!buf) return -1;
193         } while(1);
194
195         if(canonical) {
196                 /*
197                  * Transform the "[-]d.dddE+-dd" output into "[-]d.dddE[-]d"
198                  * Check that snprintf() constructed the output correctly.
199                  */
200                 char *dot;
201                 char *end = buf + buflen;
202                 char *last_zero;
203                 char *first_zero_in_run;
204         char *s;
205
206         enum {
207             LZSTATE_NOTHING,
208             LZSTATE_ZEROES
209         } lz_state = LZSTATE_NOTHING;
210
211                 dot = (buf[0] == 0x2d /* '-' */) ? (buf + 2) : (buf + 1);
212                 if(*dot >= 0x30) {
213                         if(buf != local_buf) FREEMEM(buf);
214                         errno = EINVAL;
215                         return -1;      /* Not a dot, really */
216                 }
217                 *dot = 0x2e;            /* Replace possible comma */
218
219         for(first_zero_in_run = last_zero = s = dot + 2; s < end; s++) {
220             switch(*s) {
221             case 0x45: /* 'E' */
222                 if(lz_state == LZSTATE_ZEROES) last_zero = first_zero_in_run;
223                 break;
224             case 0x30: /* '0' */
225                 if(lz_state == LZSTATE_NOTHING) first_zero_in_run = s;
226                 lz_state = LZSTATE_ZEROES;
227                 continue;
228             default:
229                 lz_state = LZSTATE_NOTHING;
230                 continue;
231             }
232             break;
233         }
234
235                 if(s == end) {
236                         if(buf != local_buf) FREEMEM(buf);
237                         errno = EINVAL;
238                         return -1;              /* No promised E */
239                 }
240
241         assert(*s == 0x45);
242         {
243             int sign;
244             char *E = s;
245             char *expptr = ++E;
246
247             s = expptr;
248
249             if(*expptr == 0x2b /* '+' */) {
250                 /* Skip the "+" */
251                 buflen -= 1;
252                 sign = 0;
253             } else {
254                 sign = 1;
255                 s++;
256             }
257             expptr++;
258             if(expptr > end) {
259                 if(buf != local_buf) FREEMEM(buf);
260                 errno = EINVAL;
261                 return -1;
262             }
263             if(*expptr == 0x30) {
264                 buflen--;
265                 expptr++;
266             }
267             if(lz_state == LZSTATE_ZEROES) {
268                 *last_zero = 0x45;      /* E */
269                 buflen -= s - (last_zero + 1);
270                 s = last_zero + 1;
271                 if(sign) {
272                     *s++ = 0x2d /* '-' */;
273                     buflen++;
274                 }
275             }
276             for(; expptr <= end; s++, expptr++)
277                 *s = *expptr;
278         }
279         } else {
280                 /*
281                  * Remove trailing zeros.
282                  */
283                 char *end = buf + buflen;
284                 char *last_zero = end;
285                 int stoplooking = 0;
286                 char *z;
287                 for(z = end - 1; z > buf; z--) {
288                         switch(*z) {
289                         case 0x30:
290                                 if(!stoplooking)
291                                         last_zero = z;
292                                 continue;
293                         case 0x31: case 0x32: case 0x33: case 0x34:
294                         case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
295                                 stoplooking = 1;
296                                 continue;
297                         default:        /* Catch dot and other separators */
298                                 /*
299                                  * Replace possible comma (which may even
300                                  * be not a comma at all: locale-defined).
301                                  */
302                                 *z = 0x2e;
303                                 if(last_zero == z + 1) {        /* leave x.0 */
304                                         last_zero++;
305                                 }
306                                 buflen = last_zero - buf;
307                                 *last_zero = '\0';
308                                 break;
309                         }
310                         break;
311                 }
312         }
313
314         ret = cb(buf, buflen, app_key);
315         if(buf != local_buf) FREEMEM(buf);
316         return (ret < 0) ? -1 : buflen;
317 }
318
319 int
320 REAL_compare(const asn_TYPE_descriptor_t *td, const void *aptr,
321              const void *bptr) {
322     const REAL_t *a = aptr;
323     const REAL_t *b = bptr;
324
325     (void)td;
326
327     if(a && b) {
328         double adbl, bdbl;
329         int ra, rb;
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)) {
335                     return 0;
336                 } else {
337                     return -1;
338                 }
339             } else if(asn_isnan(bdbl)) {
340                 return 1;
341             }
342             /* Value comparison. */
343             if(adbl < bdbl) {
344                 return -1;
345             } else if(adbl > bdbl) {
346                 return 1;
347             } else {
348                 return 0;
349             }
350         } else if(ra) {
351             return -1;
352         } else {
353             return 1;
354         }
355     } else if(!a) {
356         return -1;
357     } else {
358         return 1;
359     }
360 }
361
362 int
363 asn_REAL2double(const REAL_t *st, double *dbl_value) {
364         unsigned int octv;
365
366         if(!st || !st->buf) {
367                 errno = EINVAL;
368                 return -1;
369         }
370
371         if(st->size == 0) {
372                 *dbl_value = 0;
373                 return 0;
374         }
375
376         octv = st->buf[0];      /* unsigned byte */
377
378         switch(octv & 0xC0) {
379         case 0x40:      /* X.690: 8.5.6 a) => 8.5.9 */
380                 /* "SpecialRealValue" */
381
382                 /* Be liberal in what you accept...
383                  * http://en.wikipedia.org/wiki/Robustness_principle
384                 if(st->size != 1) ...
385                 */
386
387                 switch(st->buf[0]) {
388                 case 0x40:      /* 01000000: PLUS-INFINITY */
389                         *dbl_value = INFINITY;
390                         return 0;
391                 case 0x41:      /* 01000001: MINUS-INFINITY */
392                         *dbl_value = - INFINITY;
393                         return 0;
394                 case 0x42:      /* 01000010: NOT-A-NUMBER */
395                         *dbl_value = NAN;
396                         return 0;
397                 case 0x43:      /* 01000011: minus zero */
398                         *dbl_value = -0.0;
399                         return 0;
400                 }
401
402                 errno = EINVAL;
403                 return -1;
404         case 0x00: {    /* X.690: 8.5.7 */
405                 /*
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]+
410                  */
411                 double d;
412                 char *source = 0;
413                 char *endptr;
414                 int used_malloc = 0;
415
416                 if(octv == 0 || (octv & 0x3C)) {
417                         /* Remaining values of bits 6 to 1 are Reserved. */
418                         errno = EINVAL;
419                         return -1;
420                 }
421
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.
427                  */
428                 if(st->buf[st->size] != '\0' || memchr(st->buf, ',', st->size)) {
429                         const uint8_t *p, *end;
430                         char *b;
431
432             b = source = (char *)MALLOC(st->size + 1);
433             if(!source) return -1;
434             used_malloc = 1;
435
436                         /* Copy without the first byte and with 0-termination */
437                         for(p = st->buf + 1, end = st->buf + st->size;
438                                         p < end; b++, p++)
439                                 *b = (*p == ',') ? '.' : *p;
440                         *b = '\0';
441                 } else {
442                         source = (char *)&st->buf[1];
443                 }
444
445                 endptr = source;
446                 d = strtod(source, &endptr);
447                 if(*endptr != '\0') {
448                         /* Format is not consistent with ISO 6093 */
449                         if(used_malloc) FREEMEM(source);
450                         errno = EINVAL;
451                         return -1;
452                 }
453                 if(used_malloc) FREEMEM(source);
454                 if(asn_isfinite(d)) {
455                         *dbl_value = d;
456                         return 0;
457                 } else {
458                         errno = ERANGE;
459                         return -1;
460                 }
461           }
462         }
463
464         /*
465          * Binary representation.
466          */
467     {
468         double m;
469         int32_t expval;         /* exponent value */
470         unsigned int elen;      /* exponent value length, in octets */
471         int scaleF;
472         int baseF;
473         uint8_t *ptr;
474         uint8_t *end;
475         int sign;
476
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 */
481         default:
482                 /* Reserved field, can't parse now. */
483                 errno = EINVAL;
484                 return -1;
485         }
486
487         sign = (octv & 0x40);   /* bit 7 */
488         scaleF = (octv & 0x0C) >> 2;    /* bits 4 to 3 */
489
490         if(st->size <= 1 + (octv & 0x03)) {
491                 errno = EINVAL;
492                 return -1;
493         }
494
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)) {
499                         errno = EINVAL;
500                         return -1;
501                 }
502                 /* FIXME: verify constraints of case d) */
503                 ptr = &st->buf[2];
504         } else {
505                 ptr = &st->buf[1];
506         }
507
508         /* Fetch the multibyte exponent */
509         expval = (int)(*(int8_t *)ptr);
510         if(elen >= sizeof(expval)-1) {
511                 errno = ERANGE;
512                 return -1;
513         }
514         end = ptr + elen + 1;
515         for(ptr++; ptr < end; ptr++)
516                 expval = (expval * 256) + *ptr;
517
518         m = 0.0;        /* Initial mantissa value */
519
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;
524
525         if(0)
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)
530         );
531
532         /*
533          * (S * N * 2^F) * B^E
534          * Essentially:
535         m = ldexp(m, scaleF) * pow(pow(2, baseF), expval);
536          */
537         m = ldexp(m, expval * baseF + scaleF);
538         if(asn_isfinite(m)) {
539                 *dbl_value = sign ? -m : m;
540         } else {
541                 errno = ERANGE;
542                 return -1;
543         }
544
545     } /* if(binary_format) */
546
547         return 0;
548 }
549
550 /*
551  * Assume IEEE 754 floating point: standard 64 bit double.
552  * [1 bit sign]  [11 bits exponent]  [52 bits mantissa]
553  */
554 int
555 asn_double2REAL(REAL_t *st, double dbl_value) {
556     double test = -0.0;
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;
563         uint8_t *ptr = buf;
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 */
567         unsigned int buflen;
568         unsigned int accum;
569         int expval;
570
571         if(!st) {
572                 errno = EINVAL;
573                 return -1;
574         }
575
576         /*
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)
580          */
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() */
584         ) {
585                 if(!st->buf || st->size < 2) {
586                         ptr = (uint8_t *)MALLOC(2);
587                         if(!ptr) return -1;
588                         if(st->buf) FREEMEM(st->buf);
589                         st->buf = ptr;
590                 }
591                 /* fpclassify(3) is not portable yet */
592                 if(asn_isnan(dbl_value)) {
593                         st->buf[0] = 0x42;      /* NaN */
594                         st->buf[1] = 0;
595                         st->size = 1;
596                 } else if(!asn_isfinite(dbl_value)) {
597                         if(copysign(1.0, dbl_value) < 0.0) {
598                                 st->buf[0] = 0x41;      /* MINUS-INFINITY */
599                         } else {
600                                 st->buf[0] = 0x40;      /* PLUS-INFINITY */
601                         }
602                         st->buf[1] = 0;
603                         st->size = 1;
604                 } else {
605                         if(copysign(1.0, dbl_value) >= 0.0) {
606                                 /* no content octets: positive zero */
607                                 st->buf[0] = 0; /* JIC */
608                                 st->size = 0;
609                         } else {
610                                 /* Negative zero. #8.5.3, 8.5.9 */
611                                 st->buf[0] = 0x43;
612                                 st->buf[1] = 0;
613                                 st->size = 1;
614                         }
615                 }
616                 return 0;
617         }
618
619         if(float_big_endian) {
620                 uint8_t *s = ((uint8_t *)&dbl_value) + 1;
621                 uint8_t *end = ((uint8_t *)&dbl_value) + sizeof(double);
622                 uint8_t *d;
623
624                 bmsign = 0x80 | ((s[-1] >> 1) & 0x40);  /* binary mask & - */
625                 for(mstop = d = dscr; s < end; d++, s++) {
626                         *d = *s;
627                         if(*d) mstop = d;
628                 }
629     } else {
630                 uint8_t *s = ((uint8_t *)&dbl_value) + sizeof(dbl_value) - 2;
631                 uint8_t *start = ((uint8_t *)&dbl_value);
632                 uint8_t *d;
633
634                 bmsign = 0x80 | ((s[1] >> 1) & 0x40);   /* binary mask & - */
635                 for(mstop = d = dscr; s >= start; d++, s--) {
636                         *d = *s;
637                         if(*d) mstop = d;
638                 }
639     }
640
641         /* Remove parts of the exponent, leave mantissa and explicit 1. */
642         dscr[0] = 0x10 | (dscr[0] & 0x0f);
643
644         /* Adjust exponent in a very unobvious way */
645         expval -= 8 * ((mstop - dscr) + 1) - 4;
646
647         /* This loop ensures DER conformance by forcing mantissa odd: 11.3.1 */
648         mval = *mstop;
649         if(mval && !(mval & 1)) {
650                 int shift_count = 1;
651                 int ishift;
652                 uint8_t *mptr;
653
654                 /*
655                  * Figure out what needs to be done to make mantissa odd.
656                  */
657                 if(!(mval & 0x0f))      /* Speed-up a little */
658                         shift_count = 4;
659                 while(((mval >> shift_count) & 1) == 0)
660                         shift_count++;
661
662                 ishift = 8 - shift_count;
663                 accum = 0;
664
665                 /* Go over the buffer, shifting it shift_count bits right. */
666                 for(mptr = dscr; mptr <= mstop; mptr++) {
667                         mval = *mptr;
668                         *mptr = accum | (mval >> shift_count);
669                         accum = mval << ishift;
670                 }
671
672                 /* Adjust exponent appropriately. */
673                 expval += shift_count;
674         }
675
676         if(expval < 0) {
677                 if((expval >> 7) == -1) {
678                         *ptr++ = bmsign | 0x00;
679                         *ptr++ = expval;
680                 } else if((expval >> 15) == -1) {
681                         *ptr++ = bmsign | 0x01;
682                         *ptr++ = expval >> 8;
683                         *ptr++ = expval;
684                 } else {
685                         *ptr++ = bmsign | 0x02;
686                         *ptr++ = expval >> 16;
687                         *ptr++ = expval >> 8;
688                         *ptr++ = expval;
689                 }
690         } else if(expval <= 0x7f) {
691                 *ptr++ = bmsign | 0x00;
692                 *ptr++ = expval;
693         } else if(expval <= 0x7fff) {
694                 *ptr++ = bmsign | 0x01;
695                 *ptr++ = expval >> 8;
696                 *ptr++ = expval;
697         } else {
698                 assert(expval <= 0x7fffff);
699                 *ptr++ = bmsign | 0x02;
700                 *ptr++ = expval >> 16;
701                 *ptr++ = expval >> 8;
702                 *ptr++ = expval;
703         }
704
705         buflen = (mstop - dscr) + 1;
706         memcpy(ptr, dscr, buflen);
707         ptr += buflen;
708         buflen = ptr - buf;
709
710         ptr = (uint8_t *)MALLOC(buflen + 1);
711         if(!ptr) return -1;
712
713         memcpy(ptr, buf, buflen);
714         buf[buflen] = 0;        /* JIC */
715
716         if(st->buf) FREEMEM(st->buf);
717         st->buf = ptr;
718         st->size = buflen;
719
720         return 0;
721 }
722
723 int CC_ATTR_NO_SANITIZE("float-cast-overflow")
724 asn_double2float(double d, float *outcome) {
725     float f = d;
726
727     *outcome = f;
728
729     if(asn_isfinite(d) == asn_isfinite(f)) {
730         return 0;
731     } else {
732         return -1;
733     }
734 }