Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2 | * Copyright (c) 2004, 2006 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 | */ |
| 5 | /* |
Lev Walkin | 41635d3 | 2006-03-18 05:06:57 +0000 | [diff] [blame] | 6 | * Read the NativeReal.h for the explanation wrt. differences between |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 7 | * REAL and NativeReal. |
| 8 | * Basically, both are decoders and encoders of ASN.1 REAL type, but this |
| 9 | * implementation deals with the standard (machine-specific) representation |
| 10 | * of them instead of using the platform-independent buffer. |
| 11 | */ |
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 <NativeReal.h> |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 14 | #include <REAL.h> |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 15 | #include <OCTET_STRING.h> |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 16 | #include <math.h> |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 17 | |
Lev Walkin | eff98a5 | 2017-09-13 22:24:35 +0000 | [diff] [blame] | 18 | #if defined(__clang__) |
| 19 | /* |
| 20 | * isnan() is defined using generic selections and won't compile in |
| 21 | * strict C89 mode because of too fancy system's standard library. |
| 22 | * However, prior to C11 the math had a perfectly working isnan() |
| 23 | * in the math library. |
| 24 | * Disable generic selection warning so we can test C89 mode with newer libc. |
| 25 | */ |
| 26 | #pragma clang diagnostic push |
| 27 | #pragma clang diagnostic ignored "-Wc11-extensions" |
| 28 | static int asn_isnan(double d) { |
| 29 | return isnan(d); |
| 30 | } |
| 31 | #pragma clang diagnostic pop |
| 32 | #else |
| 33 | #define asn_isnan(v) isnan(v) |
| 34 | #endif /* generic selections */ |
| 35 | |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 36 | /* |
| 37 | * NativeReal basic type description. |
| 38 | */ |
Wim Lewis | 18c2ec9 | 2014-07-29 11:30:10 -0700 | [diff] [blame] | 39 | static const ber_tlv_tag_t asn_DEF_NativeReal_tags[] = { |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 40 | (ASN_TAG_CLASS_UNIVERSAL | (9 << 2)) |
| 41 | }; |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 42 | asn_TYPE_operation_t asn_OP_NativeReal = { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 43 | NativeReal_free, |
| 44 | NativeReal_print, |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 45 | NativeReal_compare, |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 46 | NativeReal_decode_ber, |
| 47 | NativeReal_encode_der, |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 48 | NativeReal_decode_xer, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 49 | NativeReal_encode_xer, |
Lev Walkin | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 50 | #ifdef ASN_DISABLE_OER_SUPPORT |
| 51 | 0, |
| 52 | 0, |
| 53 | #else |
| 54 | 0, |
| 55 | 0, |
| 56 | #endif /* ASN_DISABLE_OER_SUPPORT */ |
Lev Walkin | b33425f | 2017-07-14 14:59:52 +0400 | [diff] [blame] | 57 | #ifdef ASN_DISABLE_PER_SUPPORT |
| 58 | 0, |
| 59 | 0, |
| 60 | #else |
| 61 | NativeReal_decode_uper, |
| 62 | NativeReal_encode_uper, |
| 63 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 64 | NativeReal_random_fill, |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 65 | 0 /* Use generic outmost tag fetcher */ |
| 66 | }; |
| 67 | asn_TYPE_descriptor_t asn_DEF_NativeReal = { |
| 68 | "REAL", /* The ASN.1 type is still REAL */ |
| 69 | "REAL", |
| 70 | &asn_OP_NativeReal, |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 71 | asn_DEF_NativeReal_tags, |
| 72 | sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]), |
| 73 | asn_DEF_NativeReal_tags, /* Same as above */ |
| 74 | sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]), |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 75 | { 0, 0, asn_generic_no_constraint }, |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 76 | 0, 0, /* No members */ |
| 77 | 0 /* No specifics */ |
| 78 | }; |
| 79 | |
| 80 | /* |
| 81 | * Decode REAL type. |
| 82 | */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 83 | asn_dec_rval_t |
Lev Walkin | afbf2a9 | 2017-09-12 23:30:27 -0700 | [diff] [blame] | 84 | NativeReal_decode_ber(const asn_codec_ctx_t *opt_codec_ctx, |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 85 | asn_TYPE_descriptor_t *td, |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 86 | void **dbl_ptr, const void *buf_ptr, size_t size, int tag_mode) { |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 87 | double *Dbl = (double *)*dbl_ptr; |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 88 | asn_dec_rval_t rval; |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 89 | ber_tlv_len_t length; |
| 90 | |
| 91 | /* |
| 92 | * If the structure is not there, allocate it. |
| 93 | */ |
| 94 | if(Dbl == NULL) { |
Lev Walkin | c17d90f | 2005-01-17 14:32:45 +0000 | [diff] [blame] | 95 | *dbl_ptr = CALLOC(1, sizeof(*Dbl)); |
| 96 | Dbl = (double *)*dbl_ptr; |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 97 | if(Dbl == NULL) { |
| 98 | rval.code = RC_FAIL; |
| 99 | rval.consumed = 0; |
| 100 | return rval; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | ASN_DEBUG("Decoding %s as REAL (tm=%d)", |
| 105 | td->name, tag_mode); |
| 106 | |
| 107 | /* |
| 108 | * Check tags. |
| 109 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 110 | rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size, |
| 111 | tag_mode, 0, &length, 0); |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 112 | if(rval.code != RC_OK) |
| 113 | return rval; |
| 114 | |
| 115 | ASN_DEBUG("%s length is %d bytes", td->name, (int)length); |
| 116 | |
| 117 | /* |
| 118 | * Make sure we have this length. |
| 119 | */ |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 120 | buf_ptr = ((const char *)buf_ptr) + rval.consumed; |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 121 | size -= rval.consumed; |
| 122 | if(length > (ber_tlv_len_t)size) { |
| 123 | rval.code = RC_WMORE; |
| 124 | rval.consumed = 0; |
| 125 | return rval; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * ASN.1 encoded REAL: buf_ptr, length |
| 130 | * Fill the Dbl, at the same time checking for overflow. |
| 131 | * If overflow occured, return with RC_FAIL. |
| 132 | */ |
| 133 | { |
| 134 | REAL_t tmp; |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 135 | union { |
| 136 | const void *constbuf; |
| 137 | void *nonconstbuf; |
| 138 | } unconst_buf; |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 139 | double d; |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 140 | |
| 141 | unconst_buf.constbuf = buf_ptr; |
| 142 | tmp.buf = (uint8_t *)unconst_buf.nonconstbuf; |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 143 | tmp.size = length; |
| 144 | |
Lev Walkin | 0959ffb | 2011-06-25 15:48:52 -0700 | [diff] [blame] | 145 | if(length < (ber_tlv_len_t)size) { |
| 146 | int ret; |
| 147 | uint8_t saved_byte = tmp.buf[tmp.size]; |
| 148 | tmp.buf[tmp.size] = '\0'; |
| 149 | ret = asn_REAL2double(&tmp, &d); |
| 150 | tmp.buf[tmp.size] = saved_byte; |
| 151 | if(ret) { |
| 152 | rval.code = RC_FAIL; |
| 153 | rval.consumed = 0; |
| 154 | return rval; |
| 155 | } |
| 156 | } else if(length < 48 /* Enough for longish %f value. */) { |
| 157 | tmp.buf = alloca(length + 1); |
| 158 | tmp.size = length; |
| 159 | memcpy(tmp.buf, buf_ptr, length); |
| 160 | tmp.buf[tmp.size] = '\0'; |
| 161 | if(asn_REAL2double(&tmp, &d)) { |
| 162 | rval.code = RC_FAIL; |
| 163 | rval.consumed = 0; |
| 164 | return rval; |
| 165 | } |
| 166 | } else { |
| 167 | /* This should probably never happen: impractically long value */ |
| 168 | tmp.buf = CALLOC(1, length + 1); |
| 169 | tmp.size = length; |
| 170 | if(tmp.buf) memcpy(tmp.buf, buf_ptr, length); |
| 171 | if(!tmp.buf || asn_REAL2double(&tmp, &d)) { |
| 172 | FREEMEM(tmp.buf); |
| 173 | rval.code = RC_FAIL; |
| 174 | rval.consumed = 0; |
| 175 | return rval; |
| 176 | } |
| 177 | FREEMEM(tmp.buf); |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | *Dbl = d; |
| 181 | } |
| 182 | |
| 183 | rval.code = RC_OK; |
| 184 | rval.consumed += length; |
| 185 | |
Lev Walkin | 1e3ccbb | 2004-10-26 10:56:10 +0000 | [diff] [blame] | 186 | ASN_DEBUG("Took %ld/%ld bytes to encode %s (%f)", |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 187 | (long)rval.consumed, (long)length, td->name, *Dbl); |
| 188 | |
| 189 | return rval; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Encode the NativeReal using the standard REAL type DER encoder. |
| 194 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 195 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 196 | NativeReal_encode_der(asn_TYPE_descriptor_t *td, void *ptr, |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 197 | int tag_mode, ber_tlv_tag_t tag, |
| 198 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 199 | double Dbl = *(const double *)ptr; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 200 | asn_enc_rval_t erval; |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 201 | REAL_t tmp; |
| 202 | |
Lev Walkin | a8bbbda | 2005-02-06 04:29:03 +0000 | [diff] [blame] | 203 | /* Prepare a temporary clean structure */ |
| 204 | memset(&tmp, 0, sizeof(tmp)); |
| 205 | |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 206 | if(asn_double2REAL(&tmp, Dbl)) { |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 207 | erval.encoded = -1; |
| 208 | erval.failed_type = td; |
| 209 | erval.structure_ptr = ptr; |
| 210 | return erval; |
| 211 | } |
| 212 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 213 | /* Encode a fake REAL */ |
| 214 | erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key); |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 215 | if(erval.encoded == -1) { |
| 216 | assert(erval.structure_ptr == &tmp); |
| 217 | erval.structure_ptr = ptr; |
| 218 | } |
Lev Walkin | a8bbbda | 2005-02-06 04:29:03 +0000 | [diff] [blame] | 219 | |
| 220 | /* Free possibly allocated members of the temporary structure */ |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 221 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp); |
Lev Walkin | a8bbbda | 2005-02-06 04:29:03 +0000 | [diff] [blame] | 222 | |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 223 | return erval; |
| 224 | } |
| 225 | |
Lev Walkin | 41d972b | 2017-08-23 23:30:59 -0700 | [diff] [blame] | 226 | #ifndef ASN_DISABLE_PER_SUPPORT |
| 227 | |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 228 | /* |
| 229 | * Decode REAL type using PER. |
| 230 | */ |
| 231 | asn_dec_rval_t |
Lev Walkin | afbf2a9 | 2017-09-12 23:30:27 -0700 | [diff] [blame] | 232 | NativeReal_decode_uper(const asn_codec_ctx_t *opt_codec_ctx, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 233 | asn_TYPE_descriptor_t *td, |
| 234 | const asn_per_constraints_t *constraints, void **dbl_ptr, |
| 235 | asn_per_data_t *pd) { |
| 236 | double *Dbl = (double *)*dbl_ptr; |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 237 | asn_dec_rval_t rval; |
| 238 | REAL_t tmp; |
| 239 | void *ptmp = &tmp; |
| 240 | int ret; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 241 | |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 242 | (void)constraints; |
| 243 | |
| 244 | /* |
| 245 | * If the structure is not there, allocate it. |
| 246 | */ |
| 247 | if(Dbl == NULL) { |
| 248 | *dbl_ptr = CALLOC(1, sizeof(*Dbl)); |
| 249 | Dbl = (double *)*dbl_ptr; |
| 250 | if(Dbl == NULL) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 251 | ASN__DECODE_FAILED; |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | memset(&tmp, 0, sizeof(tmp)); |
| 255 | rval = OCTET_STRING_decode_uper(opt_codec_ctx, td, NULL, |
| 256 | &ptmp, pd); |
| 257 | if(rval.code != RC_OK) { |
| 258 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp); |
| 259 | return rval; |
| 260 | } |
| 261 | |
| 262 | ret = asn_REAL2double(&tmp, Dbl); |
| 263 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 264 | if(ret) ASN__DECODE_FAILED; |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 265 | |
| 266 | return rval; |
| 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Encode the NativeReal using the OCTET STRING PER encoder. |
| 271 | */ |
| 272 | asn_enc_rval_t |
| 273 | NativeReal_encode_uper(asn_TYPE_descriptor_t *td, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 274 | const asn_per_constraints_t *constraints, void *sptr, |
| 275 | asn_per_outp_t *po) { |
| 276 | double Dbl = *(const double *)sptr; |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 277 | asn_enc_rval_t erval; |
| 278 | REAL_t tmp; |
| 279 | |
| 280 | (void)constraints; |
| 281 | |
| 282 | /* Prepare a temporary clean structure */ |
| 283 | memset(&tmp, 0, sizeof(tmp)); |
| 284 | |
| 285 | if(asn_double2REAL(&tmp, Dbl)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 286 | ASN__ENCODE_FAILED; |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 287 | |
| 288 | /* Encode a DER REAL */ |
| 289 | erval = OCTET_STRING_encode_uper(td, NULL, &tmp, po); |
| 290 | if(erval.encoded == -1) |
| 291 | erval.structure_ptr = sptr; |
| 292 | |
| 293 | /* Free possibly allocated members of the temporary structure */ |
| 294 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp); |
| 295 | |
| 296 | return erval; |
| 297 | } |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 298 | |
Lev Walkin | 41d972b | 2017-08-23 23:30:59 -0700 | [diff] [blame] | 299 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
| 300 | |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 301 | /* |
| 302 | * Decode the chunk of XML text encoding REAL. |
| 303 | */ |
| 304 | asn_dec_rval_t |
Lev Walkin | afbf2a9 | 2017-09-12 23:30:27 -0700 | [diff] [blame] | 305 | NativeReal_decode_xer(const asn_codec_ctx_t *opt_codec_ctx, |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 306 | asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 307 | const void *buf_ptr, size_t size) { |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 308 | asn_dec_rval_t rval; |
| 309 | REAL_t *st = 0; |
Lev Walkin | 1eded35 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 310 | REAL_t **stp = &st; |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 311 | double *Dbl = (double *)*sptr; |
| 312 | |
| 313 | if(!Dbl) { |
Lev Walkin | c17d90f | 2005-01-17 14:32:45 +0000 | [diff] [blame] | 314 | *sptr = CALLOC(1, sizeof(double)); |
| 315 | Dbl = (double *)*sptr; |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 316 | if(!Dbl) { |
| 317 | rval.code = RC_FAIL; |
| 318 | rval.consumed = 0; |
| 319 | return rval; |
| 320 | } |
| 321 | } |
| 322 | |
Lev Walkin | 1eded35 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 323 | rval = REAL_decode_xer(opt_codec_ctx, td, (void **)stp, opt_mname, |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 324 | buf_ptr, size); |
| 325 | if(rval.code == RC_OK) { |
| 326 | if(asn_REAL2double(st, Dbl)) { |
| 327 | rval.code = RC_FAIL; |
| 328 | rval.consumed = 0; |
| 329 | } |
| 330 | } else { |
| 331 | rval.consumed = 0; |
| 332 | } |
Lev Walkin | adcb586 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 333 | ASN_STRUCT_FREE(asn_DEF_REAL, st); |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 334 | return rval; |
| 335 | } |
| 336 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 337 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 338 | NativeReal_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 339 | int ilevel, enum xer_encoder_flags_e flags, |
| 340 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 341 | const double *Dbl = (const double *)sptr; |
| 342 | asn_enc_rval_t er; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 343 | |
| 344 | (void)ilevel; |
| 345 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 346 | if(!Dbl) ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 347 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 348 | er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 349 | if(er.encoded < 0) ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 350 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 351 | ASN__ENCODED_OK(er); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 354 | /* |
| 355 | * REAL specific human-readable output. |
| 356 | */ |
| 357 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 358 | NativeReal_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 359 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 360 | const double *Dbl = (const double *)sptr; |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 361 | |
| 362 | (void)td; /* Unused argument */ |
| 363 | (void)ilevel; /* Unused argument */ |
| 364 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 365 | if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 366 | |
| 367 | return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0; |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 370 | int |
| 371 | NativeReal_compare(const asn_TYPE_descriptor_t *td, const void *aptr, |
| 372 | const void *bptr) { |
| 373 | const double *a = aptr; |
| 374 | const double *b = bptr; |
| 375 | (void)td; |
| 376 | |
| 377 | if(a && b) { |
| 378 | /* NaN sorted above everything else */ |
Lev Walkin | eff98a5 | 2017-09-13 22:24:35 +0000 | [diff] [blame] | 379 | if(asn_isnan(*a)) { |
| 380 | if(asn_isnan(*b)) { |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 381 | return 0; |
| 382 | } else { |
| 383 | return -1; |
| 384 | } |
Lev Walkin | eff98a5 | 2017-09-13 22:24:35 +0000 | [diff] [blame] | 385 | } else if(asn_isnan(*b)) { |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 386 | return 1; |
| 387 | } |
| 388 | /* Value comparison. */ |
| 389 | if(*a < *b) { |
| 390 | return -1; |
| 391 | } else if(*a > *b) { |
| 392 | return 1; |
| 393 | } else { |
| 394 | return 0; |
| 395 | } |
| 396 | } else if(!a) { |
| 397 | return -1; |
| 398 | } else { |
| 399 | return 1; |
| 400 | } |
| 401 | } |
| 402 | |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 403 | void |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 404 | NativeReal_free(const asn_TYPE_descriptor_t *td, void *ptr, |
| 405 | enum asn_struct_free_method method) { |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 406 | if(!td || !ptr) |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 407 | return; |
| 408 | |
| 409 | ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)", |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 410 | td->name, method, ptr); |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 411 | |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 412 | switch(method) { |
| 413 | case ASFM_FREE_EVERYTHING: |
| 414 | FREEMEM(ptr); |
| 415 | break; |
| 416 | case ASFM_FREE_UNDERLYING: |
| 417 | break; |
| 418 | case ASFM_FREE_UNDERLYING_AND_RESET: |
| 419 | memset(ptr, 0, sizeof(double)); |
| 420 | break; |
| 421 | } |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 424 | |
| 425 | asn_random_fill_result_t |
| 426 | NativeReal_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, |
| 427 | const asn_encoding_constraints_t *constraints, |
| 428 | size_t max_length) { |
| 429 | asn_random_fill_result_t result_ok = {ARFILL_OK, 1}; |
| 430 | asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0}; |
| 431 | asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0}; |
| 432 | static const double values[] = {0, -0.0, -1, 1, INFINITY, -INFINITY, NAN}; |
| 433 | double *st; |
| 434 | double d; |
| 435 | |
| 436 | (void)td; |
| 437 | (void)constraints; |
| 438 | |
| 439 | if(max_length == 0) return result_skipped; |
| 440 | |
| 441 | d = values[asn_random_between(0, sizeof(values) / sizeof(values[0]) - 1)]; |
| 442 | |
| 443 | if(*sptr) { |
| 444 | st = *sptr; |
| 445 | } else { |
| 446 | st = (double *)(*sptr = CALLOC(1, sizeof(double))); |
| 447 | if(!st) { |
| 448 | return result_failed; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | *st = d; |
| 453 | |
| 454 | result_ok.length = sizeof(double); |
| 455 | return result_ok; |
| 456 | } |