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