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