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