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