blob: 1983a3b70d62a3eda8b9b7e2f329de1c33282a22 [file] [log] [blame]
Lev Walkin41ba1f22004-09-14 12:46:35 +00001/*-
Lev Walkin725883b2006-10-09 12:07:58 +00002 * Copyright (c) 2004, 2006 Lev Walkin <vlm@lionet.info>. All rights reserved.
Lev Walkin41ba1f22004-09-14 12:46:35 +00003 * Redistribution and modifications are permitted subject to BSD license.
4 */
5/*
Lev Walkin41635d32006-03-18 05:06:57 +00006 * Read the NativeReal.h for the explanation wrt. differences between
Lev Walkin41ba1f22004-09-14 12:46:35 +00007 * 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 Walkina9cc46e2004-09-22 16:06:28 +000012#include <asn_internal.h>
Lev Walkin41ba1f22004-09-14 12:46:35 +000013#include <NativeReal.h>
Lev Walkin41ba1f22004-09-14 12:46:35 +000014#include <REAL.h>
Lev Walkin725883b2006-10-09 12:07:58 +000015#include <OCTET_STRING.h>
Lev Walkincd2f48e2017-08-10 02:14:59 -070016#include <math.h>
Lev Walkin41ba1f22004-09-14 12:46:35 +000017
Lev Walkineff98a52017-09-13 22:24:35 +000018#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"
28static 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 Walkin41ba1f22004-09-14 12:46:35 +000036/*
37 * NativeReal basic type description.
38 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070039static const ber_tlv_tag_t asn_DEF_NativeReal_tags[] = {
Lev Walkin41ba1f22004-09-14 12:46:35 +000040 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
41};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080042asn_TYPE_operation_t asn_OP_NativeReal = {
Lev Walkina9cc46e2004-09-22 16:06:28 +000043 NativeReal_free,
44 NativeReal_print,
Lev Walkincd2f48e2017-08-10 02:14:59 -070045 NativeReal_compare,
Lev Walkin41ba1f22004-09-14 12:46:35 +000046 NativeReal_decode_ber,
47 NativeReal_encode_der,
Lev Walkin8471cec2004-10-21 14:02:19 +000048 NativeReal_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000049 NativeReal_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070050#ifdef ASN_DISABLE_OER_SUPPORT
51 0,
52 0,
53#else
54 0,
55 0,
56#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040057#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, Chiu1f87ac02017-08-20 01:25:45 +080064 0 /* Use generic outmost tag fetcher */
65};
66asn_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 Walkin5e033762004-09-29 13:26:15 +000071 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 Walkin76780762017-07-07 10:07:30 -070075 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000076 0, /* No PER visible constraints */
Lev Walkin41ba1f22004-09-14 12:46:35 +000077 0, 0, /* No members */
78 0 /* No specifics */
79};
80
81/*
82 * Decode REAL type.
83 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000084asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -070085NativeReal_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin5e033762004-09-29 13:26:15 +000086 asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +000087 void **dbl_ptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkin41ba1f22004-09-14 12:46:35 +000088 double *Dbl = (double *)*dbl_ptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +000089 asn_dec_rval_t rval;
Lev Walkin41ba1f22004-09-14 12:46:35 +000090 ber_tlv_len_t length;
91
92 /*
93 * If the structure is not there, allocate it.
94 */
95 if(Dbl == NULL) {
Lev Walkinc17d90f2005-01-17 14:32:45 +000096 *dbl_ptr = CALLOC(1, sizeof(*Dbl));
97 Dbl = (double *)*dbl_ptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +000098 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 Walkin5e033762004-09-29 13:26:15 +0000111 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
112 tag_mode, 0, &length, 0);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000113 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 Walkin8c3b8542005-03-10 18:52:02 +0000121 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000122 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 Walkin7e033b52005-07-02 08:19:17 +0000136 union {
137 const void *constbuf;
138 void *nonconstbuf;
139 } unconst_buf;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000140 double d;
Lev Walkin7e033b52005-07-02 08:19:17 +0000141
142 unconst_buf.constbuf = buf_ptr;
143 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000144 tmp.size = length;
145
Lev Walkin0959ffb2011-06-25 15:48:52 -0700146 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 Walkin41ba1f22004-09-14 12:46:35 +0000179 }
180
181 *Dbl = d;
182 }
183
184 rval.code = RC_OK;
185 rval.consumed += length;
186
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000187 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%f)",
Lev Walkin41ba1f22004-09-14 12:46:35 +0000188 (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 Walkina9cc46e2004-09-22 16:06:28 +0000196asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000197NativeReal_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000198 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 Walkina9cc46e2004-09-22 16:06:28 +0000201 asn_enc_rval_t erval;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000202 REAL_t tmp;
203
Lev Walkina8bbbda2005-02-06 04:29:03 +0000204 /* Prepare a temporary clean structure */
205 memset(&tmp, 0, sizeof(tmp));
206
Lev Walkin5e033762004-09-29 13:26:15 +0000207 if(asn_double2REAL(&tmp, Dbl)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000208 erval.encoded = -1;
209 erval.failed_type = td;
210 erval.structure_ptr = ptr;
211 return erval;
212 }
213
Lev Walkin8e8078a2004-09-26 13:10:40 +0000214 /* Encode a fake REAL */
215 erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000216 if(erval.encoded == -1) {
217 assert(erval.structure_ptr == &tmp);
218 erval.structure_ptr = ptr;
219 }
Lev Walkina8bbbda2005-02-06 04:29:03 +0000220
221 /* Free possibly allocated members of the temporary structure */
Lev Walkinadcb5862006-03-17 02:11:12 +0000222 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
Lev Walkina8bbbda2005-02-06 04:29:03 +0000223
Lev Walkin41ba1f22004-09-14 12:46:35 +0000224 return erval;
225}
226
Lev Walkin41d972b2017-08-23 23:30:59 -0700227#ifndef ASN_DISABLE_PER_SUPPORT
228
Lev Walkin725883b2006-10-09 12:07:58 +0000229/*
230 * Decode REAL type using PER.
231 */
232asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700233NativeReal_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin494fb702017-08-07 20:07:00 -0700234 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 Walkin725883b2006-10-09 12:07:58 +0000238 asn_dec_rval_t rval;
239 REAL_t tmp;
240 void *ptmp = &tmp;
241 int ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000242
Lev Walkin725883b2006-10-09 12:07:58 +0000243 (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 Walkin7c1dc052016-03-14 03:08:15 -0700252 ASN__DECODE_FAILED;
Lev Walkin725883b2006-10-09 12:07:58 +0000253 }
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 Walkin7c1dc052016-03-14 03:08:15 -0700265 if(ret) ASN__DECODE_FAILED;
Lev Walkin725883b2006-10-09 12:07:58 +0000266
267 return rval;
268}
269
270/*
271 * Encode the NativeReal using the OCTET STRING PER encoder.
272 */
273asn_enc_rval_t
274NativeReal_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700275 const asn_per_constraints_t *constraints, void *sptr,
276 asn_per_outp_t *po) {
277 double Dbl = *(const double *)sptr;
Lev Walkin725883b2006-10-09 12:07:58 +0000278 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 Walkin7c1dc052016-03-14 03:08:15 -0700287 ASN__ENCODE_FAILED;
Lev Walkin725883b2006-10-09 12:07:58 +0000288
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 Walkin8471cec2004-10-21 14:02:19 +0000299
Lev Walkin41d972b2017-08-23 23:30:59 -0700300#endif /* ASN_DISABLE_PER_SUPPORT */
301
Lev Walkin8471cec2004-10-21 14:02:19 +0000302/*
303 * Decode the chunk of XML text encoding REAL.
304 */
305asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700306NativeReal_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin8471cec2004-10-21 14:02:19 +0000307 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000308 const void *buf_ptr, size_t size) {
Lev Walkin8471cec2004-10-21 14:02:19 +0000309 asn_dec_rval_t rval;
310 REAL_t *st = 0;
Lev Walkin1eded352006-07-13 11:19:01 +0000311 REAL_t **stp = &st;
Lev Walkin8471cec2004-10-21 14:02:19 +0000312 double *Dbl = (double *)*sptr;
313
314 if(!Dbl) {
Lev Walkinc17d90f2005-01-17 14:32:45 +0000315 *sptr = CALLOC(1, sizeof(double));
316 Dbl = (double *)*sptr;
Lev Walkin8471cec2004-10-21 14:02:19 +0000317 if(!Dbl) {
318 rval.code = RC_FAIL;
319 rval.consumed = 0;
320 return rval;
321 }
322 }
323
Lev Walkin1eded352006-07-13 11:19:01 +0000324 rval = REAL_decode_xer(opt_codec_ctx, td, (void **)stp, opt_mname,
Lev Walkin8471cec2004-10-21 14:02:19 +0000325 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 Walkinadcb5862006-03-17 02:11:12 +0000334 ASN_STRUCT_FREE(asn_DEF_REAL, st);
Lev Walkin8471cec2004-10-21 14:02:19 +0000335 return rval;
336}
337
Lev Walkina9cc46e2004-09-22 16:06:28 +0000338asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000339NativeReal_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000340 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 Walkina9cc46e2004-09-22 16:06:28 +0000344
345 (void)ilevel;
346
Lev Walkin7c1dc052016-03-14 03:08:15 -0700347 if(!Dbl) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000348
Lev Walkin8e8078a2004-09-26 13:10:40 +0000349 er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700350 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000351
Lev Walkin7c1dc052016-03-14 03:08:15 -0700352 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000353}
354
Lev Walkin41ba1f22004-09-14 12:46:35 +0000355/*
356 * REAL specific human-readable output.
357 */
358int
Lev Walkin5e033762004-09-29 13:26:15 +0000359NativeReal_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000360 asn_app_consume_bytes_f *cb, void *app_key) {
361 const double *Dbl = (const double *)sptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000362
363 (void)td; /* Unused argument */
364 (void)ilevel; /* Unused argument */
365
Lev Walkin8e8078a2004-09-26 13:10:40 +0000366 if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000367
368 return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000369}
370
Lev Walkincd2f48e2017-08-10 02:14:59 -0700371int
372NativeReal_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 Walkineff98a52017-09-13 22:24:35 +0000380 if(asn_isnan(*a)) {
381 if(asn_isnan(*b)) {
Lev Walkincd2f48e2017-08-10 02:14:59 -0700382 return 0;
383 } else {
384 return -1;
385 }
Lev Walkineff98a52017-09-13 22:24:35 +0000386 } else if(asn_isnan(*b)) {
Lev Walkincd2f48e2017-08-10 02:14:59 -0700387 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 Walkin41ba1f22004-09-14 12:46:35 +0000404void
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700405NativeReal_free(const asn_TYPE_descriptor_t *td, void *ptr,
406 enum asn_struct_free_method method) {
Lev Walkinf6853ce2017-08-11 00:50:27 -0700407 if(!td || !ptr)
Lev Walkin41ba1f22004-09-14 12:46:35 +0000408 return;
409
410 ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)",
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700411 td->name, method, ptr);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000412
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700413 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 Walkin41ba1f22004-09-14 12:46:35 +0000423}
424