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