blob: 89cd005729097dce42475f67057d4119735e4ca8 [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
18/*
19 * NativeReal basic type description.
20 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070021static const ber_tlv_tag_t asn_DEF_NativeReal_tags[] = {
Lev Walkin41ba1f22004-09-14 12:46:35 +000022 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
23};
Bi-Ruei, Chiu1f87ac02017-08-20 01:25:45 +080024asn_TYPE_operation_t asn_OP_NativeReal = {
Lev Walkina9cc46e2004-09-22 16:06:28 +000025 NativeReal_free,
26 NativeReal_print,
Lev Walkincd2f48e2017-08-10 02:14:59 -070027 NativeReal_compare,
Lev Walkin41ba1f22004-09-14 12:46:35 +000028 NativeReal_decode_ber,
29 NativeReal_encode_der,
Lev Walkin8471cec2004-10-21 14:02:19 +000030 NativeReal_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000031 NativeReal_encode_xer,
Lev Walkincc159472017-07-06 08:26:36 -070032#ifdef ASN_DISABLE_OER_SUPPORT
33 0,
34 0,
35#else
36 0,
37 0,
38#endif /* ASN_DISABLE_OER_SUPPORT */
Lev Walkinb33425f2017-07-14 14:59:52 +040039#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, Chiu1f87ac02017-08-20 01:25:45 +080046 0 /* Use generic outmost tag fetcher */
47};
48asn_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 Walkin5e033762004-09-29 13:26:15 +000053 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 Walkin76780762017-07-07 10:07:30 -070057 0, /* No OER visible constraints */
Lev Walkin59b176e2005-11-26 11:25:14 +000058 0, /* No PER visible constraints */
Lev Walkin41ba1f22004-09-14 12:46:35 +000059 0, 0, /* No members */
60 0 /* No specifics */
61};
62
63/*
64 * Decode REAL type.
65 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000066asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -070067NativeReal_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin5e033762004-09-29 13:26:15 +000068 asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +000069 void **dbl_ptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkin41ba1f22004-09-14 12:46:35 +000070 double *Dbl = (double *)*dbl_ptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +000071 asn_dec_rval_t rval;
Lev Walkin41ba1f22004-09-14 12:46:35 +000072 ber_tlv_len_t length;
73
74 /*
75 * If the structure is not there, allocate it.
76 */
77 if(Dbl == NULL) {
Lev Walkinc17d90f2005-01-17 14:32:45 +000078 *dbl_ptr = CALLOC(1, sizeof(*Dbl));
79 Dbl = (double *)*dbl_ptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +000080 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 Walkin5e033762004-09-29 13:26:15 +000093 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
94 tag_mode, 0, &length, 0);
Lev Walkin41ba1f22004-09-14 12:46:35 +000095 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 Walkin8c3b8542005-03-10 18:52:02 +0000103 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000104 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 Walkin7e033b52005-07-02 08:19:17 +0000118 union {
119 const void *constbuf;
120 void *nonconstbuf;
121 } unconst_buf;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000122 double d;
Lev Walkin7e033b52005-07-02 08:19:17 +0000123
124 unconst_buf.constbuf = buf_ptr;
125 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000126 tmp.size = length;
127
Lev Walkin0959ffb2011-06-25 15:48:52 -0700128 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 Walkin41ba1f22004-09-14 12:46:35 +0000161 }
162
163 *Dbl = d;
164 }
165
166 rval.code = RC_OK;
167 rval.consumed += length;
168
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000169 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%f)",
Lev Walkin41ba1f22004-09-14 12:46:35 +0000170 (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 Walkina9cc46e2004-09-22 16:06:28 +0000178asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000179NativeReal_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000180 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 Walkina9cc46e2004-09-22 16:06:28 +0000183 asn_enc_rval_t erval;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000184 REAL_t tmp;
185
Lev Walkina8bbbda2005-02-06 04:29:03 +0000186 /* Prepare a temporary clean structure */
187 memset(&tmp, 0, sizeof(tmp));
188
Lev Walkin5e033762004-09-29 13:26:15 +0000189 if(asn_double2REAL(&tmp, Dbl)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000190 erval.encoded = -1;
191 erval.failed_type = td;
192 erval.structure_ptr = ptr;
193 return erval;
194 }
195
Lev Walkin8e8078a2004-09-26 13:10:40 +0000196 /* Encode a fake REAL */
197 erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000198 if(erval.encoded == -1) {
199 assert(erval.structure_ptr == &tmp);
200 erval.structure_ptr = ptr;
201 }
Lev Walkina8bbbda2005-02-06 04:29:03 +0000202
203 /* Free possibly allocated members of the temporary structure */
Lev Walkinadcb5862006-03-17 02:11:12 +0000204 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
Lev Walkina8bbbda2005-02-06 04:29:03 +0000205
Lev Walkin41ba1f22004-09-14 12:46:35 +0000206 return erval;
207}
208
Lev Walkin41d972b2017-08-23 23:30:59 -0700209#ifndef ASN_DISABLE_PER_SUPPORT
210
Lev Walkin725883b2006-10-09 12:07:58 +0000211/*
212 * Decode REAL type using PER.
213 */
214asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700215NativeReal_decode_uper(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin494fb702017-08-07 20:07:00 -0700216 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 Walkin725883b2006-10-09 12:07:58 +0000220 asn_dec_rval_t rval;
221 REAL_t tmp;
222 void *ptmp = &tmp;
223 int ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000224
Lev Walkin725883b2006-10-09 12:07:58 +0000225 (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 Walkin7c1dc052016-03-14 03:08:15 -0700234 ASN__DECODE_FAILED;
Lev Walkin725883b2006-10-09 12:07:58 +0000235 }
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 Walkin7c1dc052016-03-14 03:08:15 -0700247 if(ret) ASN__DECODE_FAILED;
Lev Walkin725883b2006-10-09 12:07:58 +0000248
249 return rval;
250}
251
252/*
253 * Encode the NativeReal using the OCTET STRING PER encoder.
254 */
255asn_enc_rval_t
256NativeReal_encode_uper(asn_TYPE_descriptor_t *td,
Lev Walkin494fb702017-08-07 20:07:00 -0700257 const asn_per_constraints_t *constraints, void *sptr,
258 asn_per_outp_t *po) {
259 double Dbl = *(const double *)sptr;
Lev Walkin725883b2006-10-09 12:07:58 +0000260 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 Walkin7c1dc052016-03-14 03:08:15 -0700269 ASN__ENCODE_FAILED;
Lev Walkin725883b2006-10-09 12:07:58 +0000270
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 Walkin8471cec2004-10-21 14:02:19 +0000281
Lev Walkin41d972b2017-08-23 23:30:59 -0700282#endif /* ASN_DISABLE_PER_SUPPORT */
283
Lev Walkin8471cec2004-10-21 14:02:19 +0000284/*
285 * Decode the chunk of XML text encoding REAL.
286 */
287asn_dec_rval_t
Lev Walkinafbf2a92017-09-12 23:30:27 -0700288NativeReal_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
Lev Walkin8471cec2004-10-21 14:02:19 +0000289 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000290 const void *buf_ptr, size_t size) {
Lev Walkin8471cec2004-10-21 14:02:19 +0000291 asn_dec_rval_t rval;
292 REAL_t *st = 0;
Lev Walkin1eded352006-07-13 11:19:01 +0000293 REAL_t **stp = &st;
Lev Walkin8471cec2004-10-21 14:02:19 +0000294 double *Dbl = (double *)*sptr;
295
296 if(!Dbl) {
Lev Walkinc17d90f2005-01-17 14:32:45 +0000297 *sptr = CALLOC(1, sizeof(double));
298 Dbl = (double *)*sptr;
Lev Walkin8471cec2004-10-21 14:02:19 +0000299 if(!Dbl) {
300 rval.code = RC_FAIL;
301 rval.consumed = 0;
302 return rval;
303 }
304 }
305
Lev Walkin1eded352006-07-13 11:19:01 +0000306 rval = REAL_decode_xer(opt_codec_ctx, td, (void **)stp, opt_mname,
Lev Walkin8471cec2004-10-21 14:02:19 +0000307 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 Walkinadcb5862006-03-17 02:11:12 +0000316 ASN_STRUCT_FREE(asn_DEF_REAL, st);
Lev Walkin8471cec2004-10-21 14:02:19 +0000317 return rval;
318}
319
Lev Walkina9cc46e2004-09-22 16:06:28 +0000320asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000321NativeReal_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000322 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 Walkina9cc46e2004-09-22 16:06:28 +0000326
327 (void)ilevel;
328
Lev Walkin7c1dc052016-03-14 03:08:15 -0700329 if(!Dbl) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000330
Lev Walkin8e8078a2004-09-26 13:10:40 +0000331 er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700332 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000333
Lev Walkin7c1dc052016-03-14 03:08:15 -0700334 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000335}
336
Lev Walkin41ba1f22004-09-14 12:46:35 +0000337/*
338 * REAL specific human-readable output.
339 */
340int
Lev Walkin5e033762004-09-29 13:26:15 +0000341NativeReal_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000342 asn_app_consume_bytes_f *cb, void *app_key) {
343 const double *Dbl = (const double *)sptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000344
345 (void)td; /* Unused argument */
346 (void)ilevel; /* Unused argument */
347
Lev Walkin8e8078a2004-09-26 13:10:40 +0000348 if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000349
350 return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000351}
352
Lev Walkincd2f48e2017-08-10 02:14:59 -0700353int
354NativeReal_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 Walkin41ba1f22004-09-14 12:46:35 +0000386void
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700387NativeReal_free(const asn_TYPE_descriptor_t *td, void *ptr,
388 enum asn_struct_free_method method) {
Lev Walkinf6853ce2017-08-11 00:50:27 -0700389 if(!td || !ptr)
Lev Walkin41ba1f22004-09-14 12:46:35 +0000390 return;
391
392 ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)",
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700393 td->name, method, ptr);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000394
Lev Walkin8d99d7b2017-08-25 01:06:00 -0700395 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 Walkin41ba1f22004-09-14 12:46:35 +0000405}
406