Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 1 | /*- |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 2 | * Copyright (c) 2004-2017 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> |
Jon Ringle | 35c3f0d | 2017-10-04 21:54:39 -0400 | [diff] [blame] | 17 | #include <float.h> |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 18 | |
Lev Walkin | eff98a5 | 2017-09-13 22:24:35 +0000 | [diff] [blame] | 19 | #if defined(__clang__) |
| 20 | /* |
| 21 | * isnan() is defined using generic selections and won't compile in |
| 22 | * strict C89 mode because of too fancy system's standard library. |
| 23 | * However, prior to C11 the math had a perfectly working isnan() |
| 24 | * in the math library. |
| 25 | * Disable generic selection warning so we can test C89 mode with newer libc. |
| 26 | */ |
| 27 | #pragma clang diagnostic push |
| 28 | #pragma clang diagnostic ignored "-Wc11-extensions" |
| 29 | static int asn_isnan(double d) { |
| 30 | return isnan(d); |
| 31 | } |
| 32 | #pragma clang diagnostic pop |
| 33 | #else |
| 34 | #define asn_isnan(v) isnan(v) |
| 35 | #endif /* generic selections */ |
| 36 | |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 37 | /* |
| 38 | * NativeReal basic type description. |
| 39 | */ |
Wim Lewis | 18c2ec9 | 2014-07-29 11:30:10 -0700 | [diff] [blame] | 40 | static const ber_tlv_tag_t asn_DEF_NativeReal_tags[] = { |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 41 | (ASN_TAG_CLASS_UNIVERSAL | (9 << 2)) |
| 42 | }; |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 43 | asn_TYPE_operation_t asn_OP_NativeReal = { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 44 | NativeReal_free, |
| 45 | NativeReal_print, |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 46 | NativeReal_compare, |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 47 | NativeReal_decode_ber, |
| 48 | NativeReal_encode_der, |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 49 | NativeReal_decode_xer, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 50 | NativeReal_encode_xer, |
Lev Walkin | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 51 | #ifdef ASN_DISABLE_OER_SUPPORT |
| 52 | 0, |
| 53 | 0, |
| 54 | #else |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 55 | NativeReal_decode_oer, |
| 56 | NativeReal_encode_oer, |
Lev Walkin | cc15947 | 2017-07-06 08:26:36 -0700 | [diff] [blame] | 57 | #endif /* ASN_DISABLE_OER_SUPPORT */ |
Lev Walkin | b33425f | 2017-07-14 14:59:52 +0400 | [diff] [blame] | 58 | #ifdef ASN_DISABLE_PER_SUPPORT |
| 59 | 0, |
| 60 | 0, |
| 61 | #else |
| 62 | NativeReal_decode_uper, |
| 63 | NativeReal_encode_uper, |
| 64 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 65 | NativeReal_random_fill, |
Bi-Ruei, Chiu | 1f87ac0 | 2017-08-20 01:25:45 +0800 | [diff] [blame] | 66 | 0 /* Use generic outmost tag fetcher */ |
| 67 | }; |
| 68 | asn_TYPE_descriptor_t asn_DEF_NativeReal = { |
| 69 | "REAL", /* The ASN.1 type is still REAL */ |
| 70 | "REAL", |
| 71 | &asn_OP_NativeReal, |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 72 | asn_DEF_NativeReal_tags, |
| 73 | sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]), |
| 74 | asn_DEF_NativeReal_tags, /* Same as above */ |
| 75 | sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]), |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 76 | { 0, 0, asn_generic_no_constraint }, |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 77 | 0, 0, /* No members */ |
| 78 | 0 /* No specifics */ |
| 79 | }; |
| 80 | |
Lev Walkin | afcc891 | 2017-10-04 23:48:35 -0700 | [diff] [blame^] | 81 | static double NativeReal__get_double(const asn_TYPE_descriptor_t *td, |
| 82 | const void *ptr); |
| 83 | static ssize_t NativeReal__set(const asn_TYPE_descriptor_t *td, void **sptr, |
| 84 | double d); |
| 85 | static void NativeReal__network_swap(size_t float_size, const void *srcp, |
| 86 | uint8_t *dst); |
Lev Walkin | cb5e1c7 | 2017-10-04 22:23:08 -0700 | [diff] [blame] | 87 | |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 88 | /* |
| 89 | * Decode REAL type. |
| 90 | */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 91 | asn_dec_rval_t |
Lev Walkin | afbf2a9 | 2017-09-12 23:30:27 -0700 | [diff] [blame] | 92 | NativeReal_decode_ber(const asn_codec_ctx_t *opt_codec_ctx, |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 93 | asn_TYPE_descriptor_t *td, void **sptr, |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 94 | const void *buf_ptr, size_t size, int tag_mode) { |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 95 | asn_dec_rval_t rval; |
| 96 | ber_tlv_len_t length; |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 97 | |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 98 | ASN_DEBUG("Decoding %s as REAL (tm=%d)", td->name, tag_mode); |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 99 | |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 100 | /* |
| 101 | * Check tags. |
| 102 | */ |
| 103 | rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size, tag_mode, 0, |
| 104 | &length, 0); |
| 105 | if(rval.code != RC_OK) return rval; |
Lev Walkin | 4ca4149 | 2017-10-03 20:29:54 -0700 | [diff] [blame] | 106 | assert(length >= 0); /* Ensured by ber_check_tags */ |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 107 | |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 108 | ASN_DEBUG("%s length is %d bytes", td->name, (int)length); |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 109 | |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 110 | /* |
| 111 | * Make sure we have this length. |
| 112 | */ |
| 113 | buf_ptr = ((const char *)buf_ptr) + rval.consumed; |
| 114 | size -= rval.consumed; |
| 115 | if(length > (ber_tlv_len_t)size) { |
| 116 | rval.code = RC_WMORE; |
| 117 | rval.consumed = 0; |
| 118 | return rval; |
| 119 | } |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 120 | |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 121 | /* |
| 122 | * ASN.1 encoded REAL: buf_ptr, length |
| 123 | * Fill the Dbl, at the same time checking for overflow. |
| 124 | * If overflow occured, return with RC_FAIL. |
| 125 | */ |
| 126 | { |
| 127 | uint8_t scratch[24]; /* Longer than %.16f in decimal */ |
| 128 | REAL_t tmp; |
| 129 | double d; |
| 130 | int ret; |
Lev Walkin | 7e033b5 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 131 | |
Lev Walkin | 4ca4149 | 2017-10-03 20:29:54 -0700 | [diff] [blame] | 132 | if((size_t)length < sizeof(scratch)) { |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 133 | tmp.buf = scratch; |
| 134 | tmp.size = length; |
| 135 | } else { |
| 136 | /* This rarely happens: impractically long value */ |
| 137 | tmp.buf = CALLOC(1, length + 1); |
| 138 | tmp.size = length; |
| 139 | if(!tmp.buf) { |
| 140 | rval.code = RC_FAIL; |
| 141 | rval.consumed = 0; |
| 142 | return rval; |
| 143 | } |
| 144 | } |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 145 | |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 146 | memcpy(tmp.buf, buf_ptr, length); |
| 147 | tmp.buf[length] = '\0'; |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 148 | |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 149 | ret = asn_REAL2double(&tmp, &d); |
| 150 | if(tmp.buf != scratch) FREEMEM(tmp.buf); |
| 151 | if(ret) { |
| 152 | rval.code = RC_FAIL; |
| 153 | rval.consumed = 0; |
| 154 | return rval; |
| 155 | } |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 156 | |
Lev Walkin | cb5e1c7 | 2017-10-04 22:23:08 -0700 | [diff] [blame] | 157 | if(NativeReal__set(td, sptr, d) < 0) |
| 158 | ASN__DECODE_FAILED; |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 159 | } |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 160 | |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 161 | rval.code = RC_OK; |
| 162 | rval.consumed += length; |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 163 | |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 164 | ASN_DEBUG("Took %ld/%ld bytes to encode %s", (long)rval.consumed, |
| 165 | (long)length, td->name); |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 166 | |
| 167 | return rval; |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Encode the NativeReal using the standard REAL type DER encoder. |
| 172 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 173 | asn_enc_rval_t |
Lev Walkin | cb5e1c7 | 2017-10-04 22:23:08 -0700 | [diff] [blame] | 174 | NativeReal_encode_der(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 175 | int tag_mode, ber_tlv_tag_t tag, |
| 176 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | cb5e1c7 | 2017-10-04 22:23:08 -0700 | [diff] [blame] | 177 | double d = NativeReal__get_double(td, sptr); |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 178 | asn_enc_rval_t erval; |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 179 | REAL_t tmp; |
| 180 | |
Lev Walkin | a8bbbda | 2005-02-06 04:29:03 +0000 | [diff] [blame] | 181 | /* Prepare a temporary clean structure */ |
| 182 | memset(&tmp, 0, sizeof(tmp)); |
| 183 | |
Lev Walkin | cb5e1c7 | 2017-10-04 22:23:08 -0700 | [diff] [blame] | 184 | if(asn_double2REAL(&tmp, d)) |
| 185 | ASN__ENCODE_FAILED; |
| 186 | |
| 187 | /* Encode a fake REAL */ |
| 188 | erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key); |
| 189 | if(erval.encoded == -1) { |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 190 | assert(erval.structure_ptr == &tmp); |
Lev Walkin | cb5e1c7 | 2017-10-04 22:23:08 -0700 | [diff] [blame] | 191 | erval.structure_ptr = sptr; |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 192 | } |
Lev Walkin | a8bbbda | 2005-02-06 04:29:03 +0000 | [diff] [blame] | 193 | |
| 194 | /* Free possibly allocated members of the temporary structure */ |
Lev Walkin | cb5e1c7 | 2017-10-04 22:23:08 -0700 | [diff] [blame] | 195 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp); |
Lev Walkin | a8bbbda | 2005-02-06 04:29:03 +0000 | [diff] [blame] | 196 | |
Lev Walkin | cb5e1c7 | 2017-10-04 22:23:08 -0700 | [diff] [blame] | 197 | return erval; |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Lev Walkin | 41d972b | 2017-08-23 23:30:59 -0700 | [diff] [blame] | 200 | #ifndef ASN_DISABLE_PER_SUPPORT |
| 201 | |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 202 | /* |
| 203 | * Decode REAL type using PER. |
| 204 | */ |
| 205 | asn_dec_rval_t |
Lev Walkin | afbf2a9 | 2017-09-12 23:30:27 -0700 | [diff] [blame] | 206 | NativeReal_decode_uper(const asn_codec_ctx_t *opt_codec_ctx, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 207 | asn_TYPE_descriptor_t *td, |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 208 | const asn_per_constraints_t *constraints, void **sptr, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 209 | asn_per_data_t *pd) { |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 210 | asn_dec_rval_t rval; |
Lev Walkin | cb5e1c7 | 2017-10-04 22:23:08 -0700 | [diff] [blame] | 211 | double d; |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 212 | REAL_t tmp; |
| 213 | void *ptmp = &tmp; |
| 214 | int ret; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 215 | |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 216 | (void)constraints; |
| 217 | |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 218 | memset(&tmp, 0, sizeof(tmp)); |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 219 | rval = OCTET_STRING_decode_uper(opt_codec_ctx, &asn_DEF_REAL, |
| 220 | NULL, &ptmp, pd); |
| 221 | if(rval.code != RC_OK) { |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 222 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp); |
| 223 | return rval; |
| 224 | } |
| 225 | |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 226 | ret = asn_REAL2double(&tmp, &d); |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 227 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp); |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 228 | if(ret) ASN__DECODE_FAILED; |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 229 | |
Lev Walkin | cb5e1c7 | 2017-10-04 22:23:08 -0700 | [diff] [blame] | 230 | if(NativeReal__set(td, sptr, d) < 0 ) |
| 231 | ASN__DECODE_FAILED; |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 232 | |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 233 | return rval; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Encode the NativeReal using the OCTET STRING PER encoder. |
| 238 | */ |
| 239 | asn_enc_rval_t |
| 240 | NativeReal_encode_uper(asn_TYPE_descriptor_t *td, |
Lev Walkin | 494fb70 | 2017-08-07 20:07:00 -0700 | [diff] [blame] | 241 | const asn_per_constraints_t *constraints, void *sptr, |
| 242 | asn_per_outp_t *po) { |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 243 | double d = NativeReal__get_double(td, sptr); |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 244 | asn_enc_rval_t erval; |
| 245 | REAL_t tmp; |
| 246 | |
| 247 | (void)constraints; |
| 248 | |
| 249 | /* Prepare a temporary clean structure */ |
| 250 | memset(&tmp, 0, sizeof(tmp)); |
| 251 | |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 252 | if(asn_double2REAL(&tmp, d)) |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 253 | ASN__ENCODE_FAILED; |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 254 | |
| 255 | /* Encode a DER REAL */ |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 256 | erval = OCTET_STRING_encode_uper(&asn_DEF_REAL, NULL, &tmp, po); |
| 257 | if(erval.encoded == -1) |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 258 | erval.structure_ptr = sptr; |
| 259 | |
| 260 | /* Free possibly allocated members of the temporary structure */ |
| 261 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp); |
| 262 | |
| 263 | return erval; |
| 264 | } |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 265 | |
Lev Walkin | 41d972b | 2017-08-23 23:30:59 -0700 | [diff] [blame] | 266 | #endif /* ASN_DISABLE_PER_SUPPORT */ |
| 267 | |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 268 | #ifndef ASN_DISABLE_OER_SUPPORT |
| 269 | |
| 270 | /* |
| 271 | * Encode as Canonical OER. |
| 272 | */ |
| 273 | asn_enc_rval_t |
| 274 | NativeReal_encode_oer(asn_TYPE_descriptor_t *td, |
| 275 | const asn_oer_constraints_t *constraints, void *sptr, |
| 276 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | afcc891 | 2017-10-04 23:48:35 -0700 | [diff] [blame^] | 277 | asn_enc_rval_t er = {0, 0, 0}; |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 278 | |
Lev Walkin | afcc891 | 2017-10-04 23:48:35 -0700 | [diff] [blame^] | 279 | if(!constraints) constraints = td->encoding_constraints.oer_constraints; |
| 280 | if(constraints && constraints->value.width != 0) { |
| 281 | /* X.696 IEEE 754 binary32 and binary64 encoding */ |
| 282 | uint8_t scratch[sizeof(double)]; |
| 283 | const asn_NativeReal_specifics_t *specs = |
| 284 | (const asn_NativeReal_specifics_t *)td->specifics; |
| 285 | size_t wire_size = constraints->value.width; |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 286 | |
Lev Walkin | afcc891 | 2017-10-04 23:48:35 -0700 | [diff] [blame^] | 287 | if(specs ? (wire_size == specs->float_size) |
| 288 | : (wire_size == sizeof(double))) { |
| 289 | /* |
| 290 | * Our representation matches the wire, modulo endianness. |
| 291 | * That was the whole point of compact encoding! |
| 292 | */ |
| 293 | } else { |
| 294 | assert(wire_size == sizeof(double) |
| 295 | || specs && specs->float_size == wire_size); |
| 296 | ASN__ENCODE_FAILED; |
| 297 | } |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 298 | |
Lev Walkin | afcc891 | 2017-10-04 23:48:35 -0700 | [diff] [blame^] | 299 | /* |
| 300 | * The X.696 standard doesn't specify endianness, neither is IEEE 754. |
| 301 | * So we assume the network format is big endian. |
| 302 | */ |
| 303 | NativeReal__network_swap(wire_size, sptr, scratch); |
| 304 | if(cb(scratch, wire_size, app_key) < 0) { |
| 305 | ASN__ENCODE_FAILED; |
| 306 | } else { |
| 307 | er.encoded = wire_size; |
| 308 | ASN__ENCODED_OK(er); |
| 309 | } |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 310 | } else { |
Lev Walkin | afcc891 | 2017-10-04 23:48:35 -0700 | [diff] [blame^] | 311 | double d = NativeReal__get_double(td, sptr); |
| 312 | ssize_t len_len; |
| 313 | REAL_t tmp; |
| 314 | |
| 315 | /* Prepare a temporary clean structure */ |
| 316 | memset(&tmp, 0, sizeof(tmp)); |
| 317 | |
| 318 | if(asn_double2REAL(&tmp, d)) { |
| 319 | ASN__ENCODE_FAILED; |
| 320 | } |
| 321 | |
| 322 | /* Encode a fake REAL */ |
| 323 | len_len = oer_serialize_length(tmp.size, cb, app_key); |
| 324 | if(len_len < 0 || cb(tmp.buf, tmp.size, app_key) < 0) { |
| 325 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp); |
| 326 | ASN__ENCODE_FAILED; |
| 327 | } else { |
| 328 | er.encoded = len_len + tmp.size; |
| 329 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp); |
| 330 | ASN__ENCODED_OK(er); |
| 331 | } |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | asn_dec_rval_t |
| 336 | NativeReal_decode_oer(const asn_codec_ctx_t *opt_codec_ctx, |
| 337 | asn_TYPE_descriptor_t *td, |
| 338 | const asn_oer_constraints_t *constraints, void **sptr, |
| 339 | const void *ptr, size_t size) { |
| 340 | asn_dec_rval_t ok = {RC_OK, 0}; |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 341 | double d; |
| 342 | ssize_t len_len; |
| 343 | size_t real_body_len; |
| 344 | |
| 345 | (void)opt_codec_ctx; |
Lev Walkin | afcc891 | 2017-10-04 23:48:35 -0700 | [diff] [blame^] | 346 | |
| 347 | if(!constraints) constraints = td->encoding_constraints.oer_constraints; |
| 348 | if(constraints && constraints->value.width != 0) { |
| 349 | /* X.696 IEEE 754 binary32 and binary64 encoding */ |
| 350 | uint8_t scratch[sizeof(double)]; |
| 351 | size_t wire_size = constraints->value.width; |
| 352 | |
| 353 | if(size < wire_size) |
| 354 | ASN__DECODE_STARVED; |
| 355 | |
| 356 | /* |
| 357 | * The X.696 standard doesn't specify endianness, neither is IEEE 754. |
| 358 | * So we assume the network format is big endian. |
| 359 | */ |
| 360 | NativeReal__network_swap(wire_size, ptr, scratch); |
| 361 | |
| 362 | switch(wire_size) { |
| 363 | case sizeof(double): |
| 364 | if(NativeReal__set(td, sptr, *(const double *)scratch) < 0) |
| 365 | ASN__DECODE_FAILED; |
| 366 | break; |
| 367 | case sizeof(float): |
| 368 | if(NativeReal__set(td, sptr, *(const float *)scratch) < 0) |
| 369 | ASN__DECODE_FAILED; |
| 370 | break; |
| 371 | default: |
| 372 | ASN__DECODE_FAILED; |
| 373 | } |
| 374 | |
| 375 | ok.consumed = wire_size; |
| 376 | return ok; |
| 377 | } |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 378 | |
| 379 | len_len = oer_fetch_length(ptr, size, &real_body_len); |
| 380 | if(len_len < 0) ASN__DECODE_FAILED; |
| 381 | if(len_len == 0) ASN__DECODE_STARVED; |
| 382 | |
| 383 | ptr = (const char *)ptr + len_len; |
| 384 | size -= len_len; |
| 385 | |
| 386 | if(real_body_len > size) ASN__DECODE_STARVED; |
| 387 | |
| 388 | { |
| 389 | uint8_t scratch[24]; /* Longer than %.16f in decimal */ |
| 390 | REAL_t tmp; |
| 391 | int ret; |
| 392 | |
| 393 | if(real_body_len < sizeof(scratch)) { |
| 394 | tmp.buf = scratch; |
| 395 | tmp.size = real_body_len; |
| 396 | } else { |
| 397 | /* This rarely happens: impractically long value */ |
| 398 | tmp.buf = CALLOC(1, real_body_len + 1); |
| 399 | tmp.size = real_body_len; |
| 400 | if(!tmp.buf) { |
| 401 | ASN__DECODE_FAILED; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | memcpy(tmp.buf, ptr, real_body_len); |
| 406 | tmp.buf[real_body_len] = '\0'; |
| 407 | |
| 408 | ret = asn_REAL2double(&tmp, &d); |
| 409 | if(tmp.buf != scratch) FREEMEM(tmp.buf); |
| 410 | if(ret) { |
| 411 | ASN_DEBUG("REAL decoded in %zu bytes, but can't convert t double", |
| 412 | real_body_len); |
| 413 | ASN__DECODE_FAILED; |
| 414 | } |
| 415 | } |
| 416 | |
Lev Walkin | cb5e1c7 | 2017-10-04 22:23:08 -0700 | [diff] [blame] | 417 | if(NativeReal__set(td, sptr, d) < 0) |
| 418 | ASN__DECODE_FAILED; |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 419 | |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 420 | ok.consumed = len_len + real_body_len; |
| 421 | return ok; |
| 422 | } |
| 423 | |
| 424 | #endif /* ASN_DISABLE_OER_SUPPORT */ |
| 425 | |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 426 | /* |
| 427 | * Decode the chunk of XML text encoding REAL. |
| 428 | */ |
| 429 | asn_dec_rval_t |
Lev Walkin | afbf2a9 | 2017-09-12 23:30:27 -0700 | [diff] [blame] | 430 | NativeReal_decode_xer(const asn_codec_ctx_t *opt_codec_ctx, |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 431 | asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 432 | const void *buf_ptr, size_t size) { |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 433 | asn_dec_rval_t rval; |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 434 | REAL_t st = { 0, 0 }; |
| 435 | REAL_t *stp = &st; |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 436 | |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 437 | rval = REAL_decode_xer(opt_codec_ctx, td, (void **)&stp, opt_mname, |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 438 | buf_ptr, size); |
| 439 | if(rval.code == RC_OK) { |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 440 | double d; |
Lev Walkin | cb5e1c7 | 2017-10-04 22:23:08 -0700 | [diff] [blame] | 441 | if(asn_REAL2double(&st, &d) || NativeReal__set(td, sptr, d) < 0) { |
| 442 | rval.code = RC_FAIL; |
| 443 | rval.consumed = 0; |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 444 | } |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 445 | } else { |
Lev Walkin | cb5e1c7 | 2017-10-04 22:23:08 -0700 | [diff] [blame] | 446 | /* Convert all errors into RC_FAIL */ |
| 447 | rval.consumed = 0; |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 448 | } |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 449 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &st); |
Lev Walkin | 8471cec | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 450 | return rval; |
| 451 | } |
| 452 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 453 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 454 | NativeReal_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 455 | int ilevel, enum xer_encoder_flags_e flags, |
| 456 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 457 | double d = NativeReal__get_double(td, sptr); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 458 | asn_enc_rval_t er; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 459 | |
| 460 | (void)ilevel; |
| 461 | |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 462 | er.encoded = REAL__dump(d, flags & XER_F_CANONICAL, cb, app_key); |
| 463 | if(er.encoded < 0) ASN__ENCODE_FAILED; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 464 | |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 465 | ASN__ENCODED_OK(er); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 468 | /* |
| 469 | * REAL specific human-readable output. |
| 470 | */ |
| 471 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 472 | NativeReal_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 473 | asn_app_consume_bytes_f *cb, void *app_key) { |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 474 | |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 475 | (void)ilevel; /* Unused argument */ |
| 476 | |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 477 | if(sptr) { |
| 478 | double d = NativeReal__get_double(td, sptr); |
| 479 | return (REAL__dump(d, 0, cb, app_key) < 0) ? -1 : 0; |
| 480 | } else { |
| 481 | return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
| 482 | } |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 485 | int |
| 486 | NativeReal_compare(const asn_TYPE_descriptor_t *td, const void *aptr, |
| 487 | const void *bptr) { |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 488 | |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 489 | if(aptr && bptr) { |
| 490 | double a = NativeReal__get_double(td, aptr); |
| 491 | double b = NativeReal__get_double(td, bptr); |
| 492 | |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 493 | /* NaN sorted above everything else */ |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 494 | if(asn_isnan(a)) { |
| 495 | if(asn_isnan(b)) { |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 496 | return 0; |
| 497 | } else { |
| 498 | return -1; |
| 499 | } |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 500 | } else if(asn_isnan(b)) { |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 501 | return 1; |
| 502 | } |
| 503 | /* Value comparison. */ |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 504 | if(a < b) { |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 505 | return -1; |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 506 | } else if(a > b) { |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 507 | return 1; |
| 508 | } else { |
| 509 | return 0; |
| 510 | } |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 511 | } else if(!aptr) { |
Lev Walkin | cd2f48e | 2017-08-10 02:14:59 -0700 | [diff] [blame] | 512 | return -1; |
| 513 | } else { |
| 514 | return 1; |
| 515 | } |
| 516 | } |
| 517 | |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 518 | void |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 519 | NativeReal_free(const asn_TYPE_descriptor_t *td, void *ptr, |
| 520 | enum asn_struct_free_method method) { |
Lev Walkin | f6853ce | 2017-08-11 00:50:27 -0700 | [diff] [blame] | 521 | if(!td || !ptr) |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 522 | return; |
| 523 | |
| 524 | ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)", |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 525 | td->name, method, ptr); |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 526 | |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 527 | switch(method) { |
| 528 | case ASFM_FREE_EVERYTHING: |
| 529 | FREEMEM(ptr); |
| 530 | break; |
| 531 | case ASFM_FREE_UNDERLYING: |
| 532 | break; |
Lev Walkin | cb5e1c7 | 2017-10-04 22:23:08 -0700 | [diff] [blame] | 533 | case ASFM_FREE_UNDERLYING_AND_RESET: { |
| 534 | const asn_NativeReal_specifics_t *specs; |
| 535 | size_t float_size; |
| 536 | specs = (const asn_NativeReal_specifics_t *)td->specifics; |
| 537 | float_size = specs ? specs->float_size : sizeof(double); |
| 538 | memset(ptr, 0, float_size); |
| 539 | } break; |
Lev Walkin | 8d99d7b | 2017-08-25 01:06:00 -0700 | [diff] [blame] | 540 | } |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 543 | |
| 544 | asn_random_fill_result_t |
| 545 | NativeReal_random_fill(const asn_TYPE_descriptor_t *td, void **sptr, |
| 546 | const asn_encoding_constraints_t *constraints, |
| 547 | size_t max_length) { |
Lev Walkin | cb5e1c7 | 2017-10-04 22:23:08 -0700 | [diff] [blame] | 548 | asn_random_fill_result_t result_ok = {ARFILL_OK, 0}; |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 549 | asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0}; |
| 550 | asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0}; |
Lev Walkin | ab1d1e1 | 2017-10-03 18:43:12 -0700 | [diff] [blame] | 551 | static const double values[] = { |
| 552 | 0, -0.0, -1, 1, -M_E, M_E, -3.14, 3.14, -M_PI, M_PI, -255, 255, |
| 553 | /* 2^51 */ |
| 554 | -2251799813685248.0, 2251799813685248.0, |
| 555 | /* 2^52 */ |
| 556 | -4503599627370496.0, 4503599627370496.0, |
| 557 | /* 2^100 */ |
| 558 | -1267650600228229401496703205376.0, 1267650600228229401496703205376.0, |
Lev Walkin | 4ca4149 | 2017-10-03 20:29:54 -0700 | [diff] [blame] | 559 | #if __STDC_VERSION__ >= 199901L |
Jon Ringle | 35c3f0d | 2017-10-04 21:54:39 -0400 | [diff] [blame] | 560 | -FLT_MAX, FLT_MAX, |
Lev Walkin | 4ca4149 | 2017-10-03 20:29:54 -0700 | [diff] [blame] | 561 | #endif |
| 562 | INFINITY, -INFINITY, NAN}; |
Lev Walkin | cb5e1c7 | 2017-10-04 22:23:08 -0700 | [diff] [blame] | 563 | ssize_t float_set_size; |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 564 | double d; |
| 565 | |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 566 | (void)constraints; |
| 567 | |
| 568 | if(max_length == 0) return result_skipped; |
| 569 | |
| 570 | d = values[asn_random_between(0, sizeof(values) / sizeof(values[0]) - 1)]; |
| 571 | |
Lev Walkin | cb5e1c7 | 2017-10-04 22:23:08 -0700 | [diff] [blame] | 572 | float_set_size = NativeReal__set(td, sptr, d); |
| 573 | if(float_set_size < 0) return result_failed; |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 574 | |
Lev Walkin | cb5e1c7 | 2017-10-04 22:23:08 -0700 | [diff] [blame] | 575 | result_ok.length = float_set_size; |
Lev Walkin | a5972be | 2017-09-29 23:15:58 -0700 | [diff] [blame] | 576 | return result_ok; |
| 577 | } |
Lev Walkin | afcc891 | 2017-10-04 23:48:35 -0700 | [diff] [blame^] | 578 | |
| 579 | |
| 580 | /* |
| 581 | * Local helper functions. |
| 582 | */ |
| 583 | |
| 584 | static double |
| 585 | NativeReal__get_double(const asn_TYPE_descriptor_t *td, const void *ptr) { |
| 586 | const asn_NativeReal_specifics_t *specs = |
| 587 | (const asn_NativeReal_specifics_t *)td->specifics; |
| 588 | size_t float_size = specs ? specs->float_size : sizeof(double); |
| 589 | if(float_size == sizeof(float)) { |
| 590 | return *(const float *)ptr; |
| 591 | } else { |
| 592 | return *(const double *)ptr; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | static ssize_t /* Returns -1 or float size. */ |
| 597 | NativeReal__set(const asn_TYPE_descriptor_t *td, void **sptr, double d) { |
| 598 | const asn_NativeReal_specifics_t *specs = |
| 599 | (const asn_NativeReal_specifics_t *)td->specifics; |
| 600 | size_t float_size = specs ? specs->float_size : sizeof(double); |
| 601 | void *native; |
| 602 | |
| 603 | if(!(native = *sptr)) { |
| 604 | native = (*sptr = CALLOC(1, float_size)); |
| 605 | if(!native) { |
| 606 | return -1; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | if(float_size == sizeof(float)) { |
| 611 | *(float *)native = d; |
| 612 | } else { |
| 613 | *(double *)native = d; |
| 614 | } |
| 615 | |
| 616 | return float_size; |
| 617 | } |
| 618 | |
| 619 | /* |
| 620 | * Swap bytes from/to network, if local is little-endian. |
| 621 | * Unused endianness sections are likely removed at compile phase. |
| 622 | */ |
| 623 | static void |
| 624 | NativeReal__network_swap(size_t float_size, const void *srcp, uint8_t *dst) { |
| 625 | const uint8_t *src = srcp; |
| 626 | double test = -0.0; |
| 627 | int float_big_endian = *(const char *)&test != 0; |
| 628 | /* In lieu of static_assert(sizeof(double) == 8) */ |
| 629 | static const char sizeof_double_is_8_a[sizeof(double)-7] CC_NOTUSED; |
| 630 | static const char sizeof_double_is_8_b[9-sizeof(double)] CC_NOTUSED; |
| 631 | /* In lieu of static_assert(sizeof(sizeof) == 4) */ |
| 632 | static const char sizeof_float_is_4_a[sizeof(float)-3] CC_NOTUSED; |
| 633 | static const char sizeof_float_is_4_b[5-sizeof(float)] CC_NOTUSED; |
| 634 | |
| 635 | switch(float_size) { |
| 636 | case sizeof(double): |
| 637 | assert(sizeof(double) == 8); |
| 638 | if(float_big_endian) { |
| 639 | dst[0] = src[0]; |
| 640 | dst[1] = src[1]; |
| 641 | dst[2] = src[2]; |
| 642 | dst[3] = src[3]; |
| 643 | dst[4] = src[4]; |
| 644 | dst[5] = src[5]; |
| 645 | dst[6] = src[6]; |
| 646 | dst[7] = src[7]; |
| 647 | } else { |
| 648 | dst[0] = src[7]; |
| 649 | dst[1] = src[6]; |
| 650 | dst[2] = src[5]; |
| 651 | dst[3] = src[4]; |
| 652 | dst[4] = src[3]; |
| 653 | dst[5] = src[2]; |
| 654 | dst[6] = src[1]; |
| 655 | dst[7] = src[0]; |
| 656 | } |
| 657 | return; |
| 658 | case sizeof(float): |
| 659 | assert(sizeof(float) == 4); |
| 660 | if(float_big_endian) { |
| 661 | dst[0] = src[0]; |
| 662 | dst[1] = src[1]; |
| 663 | dst[2] = src[2]; |
| 664 | dst[3] = src[3]; |
| 665 | } else { |
| 666 | dst[0] = src[3]; |
| 667 | dst[1] = src[2]; |
| 668 | dst[2] = src[1]; |
| 669 | dst[3] = src[0]; |
| 670 | } |
| 671 | return; |
| 672 | } |
| 673 | } |