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