blob: 3316ee5a96dd12efc349b6a50e436710d0f6e3a4 [file] [log] [blame]
Lev Walkin41ba1f22004-09-14 12:46:35 +00001/*-
2 * Copyright (c) 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
Lev Walkin33700162004-10-26 09:03:31 +00005#if defined(__alpha)
6#define _ISOC99_SOURCE /* For quiet NAN, indirectly through bits/nan.h */
7#define _BSD_SOURCE /* To reintroduce finite(3) */
8#endif
Lev Walkina9cc46e2004-09-22 16:06:28 +00009#include <asn_internal.h>
Lev Walkin41ba1f22004-09-14 12:46:35 +000010#include <stdlib.h> /* for strtod(3) */
11#include <math.h>
12#include <errno.h>
13#include <assert.h>
Lev Walkin33700162004-10-26 09:03:31 +000014#include <REAL.h>
Lev Walkin41ba1f22004-09-14 12:46:35 +000015
16#undef INT_MAX
17#define INT_MAX ((int)(((unsigned int)-1) >> 1))
18
Lev Walkin942fd082004-10-03 09:13:02 +000019static volatile double real_zero = 0.0;
Lev Walkinae1bce92004-09-27 22:18:34 +000020#ifndef NAN
Lev Walkinc51e7d62004-09-27 22:16:18 +000021#define NAN (real_zero/real_zero)
Lev Walkin40319a12004-09-14 13:40:42 +000022#endif
23
Lev Walkin41ba1f22004-09-14 12:46:35 +000024/*
25 * REAL basic type description.
26 */
Lev Walkin5e033762004-09-29 13:26:15 +000027static ber_tlv_tag_t asn_DEF_REAL_tags[] = {
Lev Walkin41ba1f22004-09-14 12:46:35 +000028 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
29};
Lev Walkin5e033762004-09-29 13:26:15 +000030asn_TYPE_descriptor_t asn_DEF_REAL = {
Lev Walkin41ba1f22004-09-14 12:46:35 +000031 "REAL",
Lev Walkindc06f6b2004-10-20 15:50:55 +000032 "REAL",
Lev Walkin8e8078a2004-09-26 13:10:40 +000033 ASN__PRIMITIVE_TYPE_free,
Lev Walkina9cc46e2004-09-22 16:06:28 +000034 REAL_print,
Lev Walkin41ba1f22004-09-14 12:46:35 +000035 asn_generic_no_constraint,
Lev Walkin8e8078a2004-09-26 13:10:40 +000036 ber_decode_primitive,
37 der_encode_primitive,
Lev Walkin5f560912004-10-21 13:37:57 +000038 REAL_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000039 REAL_encode_xer,
Lev Walkin41ba1f22004-09-14 12:46:35 +000040 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000041 asn_DEF_REAL_tags,
42 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
43 asn_DEF_REAL_tags, /* Same as above */
44 sizeof(asn_DEF_REAL_tags) / sizeof(asn_DEF_REAL_tags[0]),
Lev Walkin41ba1f22004-09-14 12:46:35 +000045 0, 0, /* No members */
46 0 /* No specifics */
47};
48
Lev Walkin5f560912004-10-21 13:37:57 +000049typedef enum specialRealValue {
50 SRV__NOT_A_NUMBER,
51 SRV__MINUS_INFINITY,
52 SRV__PLUS_INFINITY
53} specialRealValue_e;
54static struct specialRealValue_s {
55 char *string;
Lev Walkin8471cec2004-10-21 14:02:19 +000056 size_t length;
Lev Walkin5f560912004-10-21 13:37:57 +000057 double dv;
58} specialRealValue[] = {
59#define SRV_SET(foo, val) { foo, sizeof(foo) - 1, val }
60 SRV_SET("<NOT-A-NUMBER/>", 0.0),
61 SRV_SET("<MINUS-INFINITY/>", -1.0),
62 SRV_SET("<PLUS-INFINITY/>", 1.0),
63#undef SRV_SET
64};
65
Lev Walkina9cc46e2004-09-22 16:06:28 +000066ssize_t
67REAL__dump(double d, int canonical, asn_app_consume_bytes_f *cb, void *app_key) {
Lev Walkin92b35d22004-09-27 20:52:18 +000068 char local_buf[64];
Lev Walkina9cc46e2004-09-22 16:06:28 +000069 char *buf = local_buf;
70 ssize_t buflen = sizeof(local_buf);
Lev Walkin92b35d22004-09-27 20:52:18 +000071 const char *fmt = canonical?"%.15E":"%.15f";
Lev Walkina9cc46e2004-09-22 16:06:28 +000072 ssize_t ret;
73
Lev Walkin8e8078a2004-09-26 13:10:40 +000074 /*
75 * Check whether it is a special value.
76 */
Lev Walkinc51e7d62004-09-27 22:16:18 +000077 /* fpclassify(3) is not portable yet */
78 if(isnan(d)) {
Lev Walkin5f560912004-10-21 13:37:57 +000079 buf = specialRealValue[SRV__NOT_A_NUMBER].string;
80 buflen = specialRealValue[SRV__NOT_A_NUMBER].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +000081 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
Lev Walkina460ba32004-10-20 15:40:04 +000082 } else if(!finite(d)) {
Lev Walkinc51e7d62004-09-27 22:16:18 +000083 if(copysign(1.0, d) < 0.0) {
Lev Walkin5f560912004-10-21 13:37:57 +000084 buf = specialRealValue[SRV__MINUS_INFINITY].string;
85 buflen = specialRealValue[SRV__MINUS_INFINITY].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +000086 } else {
Lev Walkin5f560912004-10-21 13:37:57 +000087 buf = specialRealValue[SRV__PLUS_INFINITY].string;
88 buflen = specialRealValue[SRV__PLUS_INFINITY].length;
Lev Walkinc51e7d62004-09-27 22:16:18 +000089 }
90 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
91 } else if(ilogb(d) <= -INT_MAX) {
92 if(copysign(1.0, d) < 0.0) {
93 buf = "-0";
94 buflen = 2;
95 } else {
96 buf = "0";
97 buflen = 1;
Lev Walkin8e8078a2004-09-26 13:10:40 +000098 }
99 return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
100 }
101
102 /*
103 * Use the libc's double printing, hopefully they got it right.
104 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000105 do {
106 ret = snprintf(buf, buflen, fmt, d);
107 if(ret < 0) {
108 buflen <<= 1;
109 } else if(ret >= buflen) {
110 buflen = ret + 1;
111 } else {
112 buflen = ret;
113 break;
114 }
115 if(buf != local_buf) free(buf);
Lev Walkin8e8078a2004-09-26 13:10:40 +0000116 buf = (char *)MALLOC(buflen);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000117 if(!buf) return -1;
118 } while(1);
119
Lev Walkina9cc46e2004-09-22 16:06:28 +0000120 if(canonical) {
Lev Walkin92b35d22004-09-27 20:52:18 +0000121 /*
122 * Transform the "[-]d.dddE+-dd" output into "[-]d.dddE[-]d"
Lev Walkin5f560912004-10-21 13:37:57 +0000123 * Check that snprintf() constructed the output correctly.
Lev Walkin92b35d22004-09-27 20:52:18 +0000124 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000125 char *dot, *E;
126 char *end = buf + buflen;
Lev Walkin92b35d22004-09-27 20:52:18 +0000127 char *last_zero;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000128
Lev Walkin5f560912004-10-21 13:37:57 +0000129 dot = (buf[0] == 0x2d /* '-' */) ? (buf + 2) : (buf + 1);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000130 if(*dot >= 0x30) {
131 errno = EINVAL;
132 return -1; /* Not a dot, really */
133 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000134 *dot = 0x2e; /* Replace possible comma */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000135
Lev Walkin92b35d22004-09-27 20:52:18 +0000136 for(last_zero = dot + 2, E = dot; dot < end; E++) {
137 if(*E == 0x45) {
138 char *expptr = ++E;
139 char *s = expptr;
140 int sign;
Lev Walkin5f560912004-10-21 13:37:57 +0000141 if(*expptr == 0x2b /* '+' */) {
Lev Walkin92b35d22004-09-27 20:52:18 +0000142 /* Skip the "+" */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000143 buflen -= 1;
Lev Walkin92b35d22004-09-27 20:52:18 +0000144 sign = 0;
145 } else {
146 sign = 1;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000147 s++;
148 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000149 expptr++;
150 if(expptr > end) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000151 errno = EINVAL;
152 return -1;
153 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000154 if(*expptr == 0x30) {
155 buflen--;
156 expptr++;
157 }
158 if(*last_zero == 0x30) {
159 *last_zero = 0x45; /* E */
Lev Walkin5f560912004-10-21 13:37:57 +0000160 buflen -= s - (last_zero + 1);
Lev Walkin92b35d22004-09-27 20:52:18 +0000161 s = last_zero + 1;
Lev Walkin5f560912004-10-21 13:37:57 +0000162 if(sign) {
163 *s++ = 0x2d /* '-' */;
164 buflen++;
165 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000166 }
167 for(; expptr <= end; s++, expptr++)
168 *s = *expptr;
169 break;
170 } else if(*E == 0x30) {
171 if(*last_zero != 0x30)
172 last_zero = E;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000173 }
174 }
175 if(E == end) {
176 errno = EINVAL;
177 return -1; /* No promised E */
178 }
Lev Walkin92b35d22004-09-27 20:52:18 +0000179 } else {
180 /*
181 * Remove trailing zeros.
182 */
183 char *end = buf + buflen;
184 char *last_zero = end;
185 char *z;
186 for(z = end - 1; z > buf; z--) {
187 switch(*z) {
188 case 0x030:
189 last_zero = z;
190 case 0x31: case 0x32: case 0x33: case 0x34:
191 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
192 continue;
193 default: /* Catch dot and other separators */
194 *z = 0x2e; /* Replace possible comma */
195 if(last_zero == z + 1) { /* leave x.0 */
196 last_zero++;
197 }
198 buflen = last_zero - buf;
199 *last_zero = '\0';
200 break;
201 }
202 break;
203 }
Lev Walkina9cc46e2004-09-22 16:06:28 +0000204 }
205
206 ret = cb(buf, buflen, app_key);
207 if(buf != local_buf) free(buf);
208 return (ret < 0) ? -1 : buflen;
209}
210
Lev Walkin41ba1f22004-09-14 12:46:35 +0000211int
Lev Walkin5e033762004-09-29 13:26:15 +0000212REAL_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000213 asn_app_consume_bytes_f *cb, void *app_key) {
214 const REAL_t *st = (const REAL_t *)sptr;
Lev Walkin8e8078a2004-09-26 13:10:40 +0000215 ssize_t ret;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000216 double d;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000217
218 (void)td; /* Unused argument */
219 (void)ilevel; /* Unused argument */
220
Lev Walkina9cc46e2004-09-22 16:06:28 +0000221 if(!st || !st->buf)
Lev Walkin8e8078a2004-09-26 13:10:40 +0000222 ret = cb("<absent>", 8, app_key);
Lev Walkin5e033762004-09-29 13:26:15 +0000223 else if(asn_REAL2double(st, &d))
Lev Walkin8e8078a2004-09-26 13:10:40 +0000224 ret = cb("<error>", 7, app_key);
225 else
226 ret = REAL__dump(d, 0, cb, app_key);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000227
Lev Walkin8e8078a2004-09-26 13:10:40 +0000228 return (ret < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000229}
Lev Walkin41ba1f22004-09-14 12:46:35 +0000230
Lev Walkina9cc46e2004-09-22 16:06:28 +0000231asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000232REAL_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000233 int ilevel, enum xer_encoder_flags_e flags,
234 asn_app_consume_bytes_f *cb, void *app_key) {
235 REAL_t *st = (REAL_t *)sptr;
236 asn_enc_rval_t er;
237 double d;
238
239 (void)ilevel;
240
Lev Walkin5e033762004-09-29 13:26:15 +0000241 if(!st || !st->buf || asn_REAL2double(st, &d))
Lev Walkina9cc46e2004-09-22 16:06:28 +0000242 _ASN_ENCODE_FAILED;
243
244 er.encoded = REAL__dump(d, flags & XER_F_CANONICAL, cb, app_key);
245 if(er.encoded < 0) _ASN_ENCODE_FAILED;
246
247 return er;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000248}
249
Lev Walkin5f560912004-10-21 13:37:57 +0000250
251/*
252 * Decode the chunk of XML text encoding REAL.
253 */
254static ssize_t
Lev Walkin8471cec2004-10-21 14:02:19 +0000255REAL__xer_body_decode(void *sptr, void *chunk_buf, size_t chunk_size) {
256 REAL_t *st = (REAL_t *)sptr;
Lev Walkin5f560912004-10-21 13:37:57 +0000257 double value;
258 char *xerdata = (char *)chunk_buf;
259 char *endptr = 0;
260 char *b;
261
262 if(!chunk_size) return -1;
263
264 /*
265 * Decode an XMLSpecialRealValue: <MINUS-INFINITY>, etc.
266 */
267 if(xerdata[0] == 0x3c /* '<' */) {
268 size_t i;
269 for(i = 0; i < sizeof(specialRealValue)
270 / sizeof(specialRealValue[0]); i++) {
271 struct specialRealValue_s *srv = &specialRealValue[i];
272 if(srv->length != chunk_size
273 || memcmp(srv->string, chunk_buf, chunk_size))
274 continue;
275
276 if(asn_double2REAL(st, srv->dv / real_zero))
277 return -1;
278
279 return chunk_size;
280 }
281 ASN_DEBUG("Unknown XMLSpecialRealValue");
282 return -1;
283 }
284
285 /*
286 * Copy chunk into the nul-terminated string, and run strtod.
287 */
Lev Walkin8471cec2004-10-21 14:02:19 +0000288 b = (char *)MALLOC(chunk_size + 1);
Lev Walkin5f560912004-10-21 13:37:57 +0000289 if(!b) return -1;
290 memcpy(b, chunk_buf, chunk_size);
Lev Walkin8471cec2004-10-21 14:02:19 +0000291 b[chunk_size] = 0; /* nul-terminate */
Lev Walkin5f560912004-10-21 13:37:57 +0000292
293 value = strtod(b, &endptr);
294 free(b);
295 if(endptr == b) return -1;
296
297 if(asn_double2REAL(st, value))
298 return -1;
299
300 return endptr - b;
301}
302
303asn_dec_rval_t
304REAL_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
305 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
306 void *buf_ptr, size_t size) {
307
308 return xer_decode_primitive(opt_codec_ctx, td,
Lev Walkin8471cec2004-10-21 14:02:19 +0000309 sptr, sizeof(REAL_t), opt_mname,
Lev Walkin5f560912004-10-21 13:37:57 +0000310 buf_ptr, size, REAL__xer_body_decode);
311}
312
313
Lev Walkin41ba1f22004-09-14 12:46:35 +0000314int
Lev Walkin5e033762004-09-29 13:26:15 +0000315asn_REAL2double(const REAL_t *st, double *dbl_value) {
Lev Walkina9cc46e2004-09-22 16:06:28 +0000316 unsigned int octv;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000317
318 if(!st || !st->buf) {
319 errno = EINVAL;
320 return -1;
321 }
322
323 if(st->size == 0) {
324 *dbl_value = 0;
325 return 0;
326 }
327
328 octv = st->buf[0]; /* unsigned byte */
329
330 switch(octv & 0xC0) {
331 case 0x40: /* X.690: 8.5.8 */
332 /* "SpecialRealValue" */
333
334 /* Be liberal in what you accept...
335 if(st->size != 1) ...
336 */
337
338 switch(st->buf[0]) {
339 case 0x40: /* 01000000: PLUS-INFINITY */
Lev Walkinc51e7d62004-09-27 22:16:18 +0000340 *dbl_value = 1.0/real_zero;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000341 return 0;
342 case 0x41: /* 01000001: MINUS-INFINITY */
Lev Walkinc51e7d62004-09-27 22:16:18 +0000343 *dbl_value = -1.0/real_zero;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000344 return 0;
345 /*
346 * The following cases are defined by
347 * X.690 Amendment 1 (10/03)
348 */
349 case 0x42: /* 01000010: NOT-A-NUMBER */
350 *dbl_value = NAN;
351 return 0;
352 case 0x43: /* 01000011: minus zero */
Lev Walkin2a789d92004-09-27 21:36:59 +0000353 *dbl_value = -0.0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000354 return 0;
355 }
356
357 errno = EINVAL;
358 return -1;
359 case 0x00: { /* X.690: 8.5.6 */
360 /*
361 * Decimal. NR{1,2,3} format.
362 */
363 double d;
364
365 assert(st->buf[st->size - 1] == 0); /* Security, vashu mat' */
366
367 d = strtod((char *)st->buf, 0);
368 if(finite(d)) {
369 *dbl_value = d;
370 return 0;
371 } else {
372 errno = ERANGE;
373 return 0;
374 }
375 }
376 }
377
378 /*
379 * Binary representation.
380 */
381 {
382 double m;
383 int expval; /* exponent value */
384 unsigned int elen; /* exponent value length, in octets */
385 unsigned int scaleF;
386 unsigned int baseF;
387 uint8_t *ptr;
388 uint8_t *end;
389 int sign;
390
391 switch((octv & 0x30) >> 4) {
392 case 0x00: baseF = 1; break; /* base 2 */
393 case 0x01: baseF = 3; break; /* base 8 */
394 case 0x02: baseF = 4; break; /* base 16 */
395 default:
396 /* Reserved field, can't parse now. */
397 errno = EINVAL;
398 return -1;
399 }
400
401 sign = (octv & 0x40); /* bit 7 */
402 scaleF = (octv & 0x0C) >> 2; /* bits 4 to 3 */
403
Lev Walkina9cc46e2004-09-22 16:06:28 +0000404 if(st->size <= (int)(1 + (octv & 0x03))) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000405 errno = EINVAL;
406 return -1;
407 }
408
409 if((octv & 0x03) == 0x11) {
410 /* 8.5.6.4, case d) */
411 elen = st->buf[1]; /* unsigned binary number */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000412 if(elen == 0 || st->size <= (int)(2 + elen)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000413 errno = EINVAL;
414 return -1;
415 }
416 ptr = &st->buf[2];
417 } else {
418 elen = (octv & 0x03);
419 ptr = &st->buf[1];
420 }
421
422 /* Fetch the multibyte exponent */
423 expval = (int)(*(int8_t *)ptr);
424 end = ptr + elen + 1;
425 for(ptr++; ptr < end; ptr++)
426 expval = (expval * 256) + *ptr;
427
428 m = 0.0; /* Initial mantissa value */
429
430 /* Okay, the exponent is here. Now, what about mantissa? */
431 end = st->buf + st->size;
432 if(ptr < end) {
433 for(; ptr < end; ptr++)
434 m = scalbn(m, 8) + *ptr;
435 }
436
Lev Walkin5f560912004-10-21 13:37:57 +0000437 if(0)
Lev Walkin41ba1f22004-09-14 12:46:35 +0000438 ASN_DEBUG("m=%.10f, scF=%d, bF=%d, expval=%d, ldexp()=%f, scalbn()=%f",
439 m, scaleF, baseF, expval,
440 ldexp(m, expval * baseF + scaleF),
441 scalbn(m, scaleF) * pow(pow(2, baseF), expval)
442 );
443
444 /*
445 * (S * N * 2^F) * B^E
446 * Essentially:
447 m = scalbn(m, scaleF) * pow(pow(2, base), expval);
448 */
449 m = ldexp(m, expval * baseF + scaleF);
450 if(finite(m)) {
451 *dbl_value = sign ? -m : m;
452 } else {
453 errno = ERANGE;
454 return -1;
455 }
456
457 } /* if(binary_format) */
458
459 return 0;
460}
461
462/*
463 * Assume IEEE 754 floating point: standard 64 bit double.
464 * [1 bit sign] [11 bits exponent] [52 bits mantissa]
465 */
466int
Lev Walkin5e033762004-09-29 13:26:15 +0000467asn_double2REAL(REAL_t *st, double dbl_value) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000468#ifdef WORDS_BIGENDIAN /* Known to be big-endian */
469 int littleEndian = 0;
470#else /* need to test: have no explicit information */
471 unsigned int LE = 1;
472 int littleEndian = *(unsigned char *)&LE;
473#endif
474 uint8_t buf[16]; /* More than enough for 8-byte dbl_value */
475 uint8_t dscr[sizeof(dbl_value)]; /* double value scratch pad */
476 /* Assertion guards: won't even compile, if unexpected double size */
477 char assertion_buffer1[9 - sizeof(dbl_value)] __attribute__((unused));
478 char assertion_buffer2[sizeof(dbl_value) - 7] __attribute__((unused));
479 uint8_t *ptr = buf;
480 uint8_t *mstop; /* Last byte of mantissa */
481 unsigned int mval; /* Value of the last byte of mantissa */
482 unsigned int bmsign; /* binary mask with sign */
483 unsigned int buflen;
484 unsigned int accum;
485 int expval;
486
487 if(!st) {
488 errno = EINVAL;
489 return -1;
490 }
491
Lev Walkin057fb732004-09-14 13:58:10 +0000492 /*
493 * ilogb(+-0) returns -INT_MAX or INT_MIN (platform-dependent)
494 * ilogb(+-inf) returns INT_MAX
Lev Walkin2a789d92004-09-27 21:36:59 +0000495 * ilogb(NaN) returns INT_MIN or INT_MAX (platform-dependent)
Lev Walkin057fb732004-09-14 13:58:10 +0000496 */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000497 expval = ilogb(dbl_value);
Lev Walkin2a789d92004-09-27 21:36:59 +0000498 if(expval <= -INT_MAX /* Also catches +-0 and maybe isnan() */
499 || expval == INT_MAX /* catches isfin() and maybe isnan() */
Lev Walkin41ba1f22004-09-14 12:46:35 +0000500 ) {
501 if(!st->buf || st->size < 2) {
Lev Walkin8e8078a2004-09-26 13:10:40 +0000502 ptr = (uint8_t *)MALLOC(2);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000503 if(!ptr) return -1;
504 st->buf = ptr;
505 }
506 /* fpclassify(3) is not portable yet */
Lev Walkin2a789d92004-09-27 21:36:59 +0000507 if(isnan(dbl_value)) {
508 st->buf[0] = 0x42; /* NaN */
509 st->buf[1] = 0;
510 st->size = 1;
Lev Walkina460ba32004-10-20 15:40:04 +0000511 } else if(!finite(dbl_value)) {
Lev Walkin7e03db92004-09-14 13:50:21 +0000512 if(copysign(1.0, dbl_value) < 0.0) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000513 st->buf[0] = 0x41; /* MINUS-INFINITY */
514 } else {
515 st->buf[0] = 0x40; /* PLUS-INFINITY */
516 }
517 st->buf[1] = 0;
518 st->size = 1;
Lev Walkinc51e7d62004-09-27 22:16:18 +0000519 } else {
520 if(copysign(1.0, dbl_value) < 0.0) {
521 st->buf[0] = 0x80 | 0x40;
522 st->buf[1] = 0;
523 st->size = 2;
524 } else {
525 /* no content octets: positive zero */
526 st->buf[0] = 0; /* JIC */
527 st->size = 0;
528 }
Lev Walkin41ba1f22004-09-14 12:46:35 +0000529 }
530 return 0;
531 }
532
533 if(littleEndian) {
534 uint8_t *s = ((uint8_t *)&dbl_value) + sizeof(dbl_value) - 2;
Lev Walkin057fb732004-09-14 13:58:10 +0000535 uint8_t *start = ((uint8_t *)&dbl_value);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000536 uint8_t *d;
537
538 bmsign = 0x80 | ((s[1] >> 1) & 0x40); /* binary mask & - */
Lev Walkin057fb732004-09-14 13:58:10 +0000539 for(mstop = d = dscr; s >= start; d++, s--) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000540 *d = *s;
541 if(*d) mstop = d;
542 }
543 } else {
544 uint8_t *s = ((uint8_t *)&dbl_value) + 1;
545 uint8_t *end = ((uint8_t *)&dbl_value) + sizeof(double);
546 uint8_t *d;
547
548 bmsign = 0x80 | ((s[-1] >> 1) & 0x40); /* binary mask & - */
549 for(mstop = d = dscr; s < end; d++, s++) {
550 *d = *s;
551 if(*d) mstop = d;
552 }
553 }
554
555 /* Remove parts of the exponent, leave mantissa and explicit 1. */
556 dscr[0] = 0x10 | (dscr[0] & 0x0f);
557
558 /* Adjust exponent in a very unobvious way */
559 expval -= 8 * ((mstop - dscr) + 1) - 4;
560
561 /* This loop ensures DER conformance by forcing mantissa odd: 11.3.1 */
562 mval = *mstop;
563 if(mval && !(mval & 1)) {
564 unsigned int shift_count = 1;
565 unsigned int ishift;
566 uint8_t *mptr;
567
568 /*
569 * Figure out what needs to be done to make mantissa odd.
570 */
571 if(!(mval & 0x0f)) /* Speed-up a little */
572 shift_count = 4;
573 while(((mval >> shift_count) & 1) == 0)
574 shift_count++;
575
576 ishift = 8 - shift_count;
577 accum = 0;
578
579 /* Go over the buffer, shifting it shift_count bits right. */
580 for(mptr = dscr; mptr <= mstop; mptr++) {
581 mval = *mptr;
582 *mptr = accum | (mval >> shift_count);
583 accum = mval << ishift;
584 }
585
586 /* Adjust mantissa appropriately. */
587 expval += shift_count;
588 }
589
590 if(expval < 0) {
591 if((expval >> 7) == -1) {
592 *ptr++ = bmsign | 0x00;
593 *ptr++ = expval;
594 } else if((expval >> 15) == -1) {
595 *ptr++ = bmsign | 0x01;
596 *ptr++ = expval >> 8;
597 *ptr++ = expval;
598 } else {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000599 *ptr++ = bmsign | 0x02;
600 *ptr++ = expval >> 16;
601 *ptr++ = expval >> 8;
602 *ptr++ = expval;
603 }
604 } else if(expval <= 0x7f) {
605 *ptr++ = bmsign | 0x00;
606 *ptr++ = expval;
607 } else if(expval <= 0x7fff) {
608 *ptr++ = bmsign | 0x01;
609 *ptr++ = expval >> 8;
610 *ptr++ = expval;
611 } else {
612 assert(expval <= 0x7fffff);
613 *ptr++ = bmsign | 0x02;
614 *ptr++ = expval >> 16;
615 *ptr++ = expval >> 8;
616 *ptr++ = expval;
617 }
618
619 buflen = (mstop - dscr) + 1;
620 memcpy(ptr, dscr, buflen);
621 ptr += buflen;
622 buflen = ptr - buf;
623
624 (void *)ptr = MALLOC(buflen + 1);
625 if(!ptr) return -1;
626
627 memcpy(ptr, buf, buflen);
628 buf[buflen] = 0; /* JIC */
629
630 if(st->buf) FREEMEM(st->buf);
631 st->buf = ptr;
632 st->size = buflen;
633
634 return 0;
635}