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