Debian Codegraph @

skeletons/REAL.c

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