blob: 7db8bae1e8299181286f61cc195628ea1b4341f8 [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 Walkin41ba1f22004-09-14 12:46:35 +000016
17/*
18 * NativeReal basic type description.
19 */
Wim Lewis18c2ec92014-07-29 11:30:10 -070020static const ber_tlv_tag_t asn_DEF_NativeReal_tags[] = {
Lev Walkin41ba1f22004-09-14 12:46:35 +000021 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
22};
Lev Walkin5e033762004-09-29 13:26:15 +000023asn_TYPE_descriptor_t asn_DEF_NativeReal = {
Lev Walkin41ba1f22004-09-14 12:46:35 +000024 "REAL", /* The ASN.1 type is still REAL */
Lev Walkindc06f6b2004-10-20 15:50:55 +000025 "REAL",
Lev Walkina9cc46e2004-09-22 16:06:28 +000026 NativeReal_free,
27 NativeReal_print,
Lev Walkin41ba1f22004-09-14 12:46:35 +000028 asn_generic_no_constraint,
29 NativeReal_decode_ber,
30 NativeReal_encode_der,
Lev Walkin8471cec2004-10-21 14:02:19 +000031 NativeReal_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000032 NativeReal_encode_xer,
Lev Walkin725883b2006-10-09 12:07:58 +000033 NativeReal_decode_uper,
34 NativeReal_encode_uper,
Harald Welte0b57b082015-08-30 16:33:07 +020035 NativeReal_decode_aper,
36 NativeReal_encode_aper,
Lev Walkin41ba1f22004-09-14 12:46:35 +000037 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000038 asn_DEF_NativeReal_tags,
39 sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]),
40 asn_DEF_NativeReal_tags, /* Same as above */
41 sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]),
Lev Walkin59b176e2005-11-26 11:25:14 +000042 0, /* No PER visible constraints */
Lev Walkin41ba1f22004-09-14 12:46:35 +000043 0, 0, /* No members */
44 0 /* No specifics */
45};
46
47/*
48 * Decode REAL type.
49 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000050asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000051NativeReal_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
52 asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +000053 void **dbl_ptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkin41ba1f22004-09-14 12:46:35 +000054 double *Dbl = (double *)*dbl_ptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +000055 asn_dec_rval_t rval;
Lev Walkin41ba1f22004-09-14 12:46:35 +000056 ber_tlv_len_t length;
57
58 /*
59 * If the structure is not there, allocate it.
60 */
61 if(Dbl == NULL) {
Lev Walkinc17d90f2005-01-17 14:32:45 +000062 *dbl_ptr = CALLOC(1, sizeof(*Dbl));
63 Dbl = (double *)*dbl_ptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +000064 if(Dbl == NULL) {
65 rval.code = RC_FAIL;
66 rval.consumed = 0;
67 return rval;
68 }
69 }
70
71 ASN_DEBUG("Decoding %s as REAL (tm=%d)",
72 td->name, tag_mode);
73
74 /*
75 * Check tags.
76 */
Lev Walkin5e033762004-09-29 13:26:15 +000077 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
78 tag_mode, 0, &length, 0);
Lev Walkin41ba1f22004-09-14 12:46:35 +000079 if(rval.code != RC_OK)
80 return rval;
81
82 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
83
84 /*
85 * Make sure we have this length.
86 */
Lev Walkin8c3b8542005-03-10 18:52:02 +000087 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
Lev Walkin41ba1f22004-09-14 12:46:35 +000088 size -= rval.consumed;
89 if(length > (ber_tlv_len_t)size) {
90 rval.code = RC_WMORE;
91 rval.consumed = 0;
92 return rval;
93 }
94
95 /*
96 * ASN.1 encoded REAL: buf_ptr, length
97 * Fill the Dbl, at the same time checking for overflow.
98 * If overflow occured, return with RC_FAIL.
99 */
100 {
101 REAL_t tmp;
Lev Walkin7e033b52005-07-02 08:19:17 +0000102 union {
103 const void *constbuf;
104 void *nonconstbuf;
105 } unconst_buf;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000106 double d;
Lev Walkin7e033b52005-07-02 08:19:17 +0000107
108 unconst_buf.constbuf = buf_ptr;
109 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000110 tmp.size = length;
111
Lev Walkin0959ffb2011-06-25 15:48:52 -0700112 if(length < (ber_tlv_len_t)size) {
113 int ret;
114 uint8_t saved_byte = tmp.buf[tmp.size];
115 tmp.buf[tmp.size] = '\0';
116 ret = asn_REAL2double(&tmp, &d);
117 tmp.buf[tmp.size] = saved_byte;
118 if(ret) {
119 rval.code = RC_FAIL;
120 rval.consumed = 0;
121 return rval;
122 }
123 } else if(length < 48 /* Enough for longish %f value. */) {
124 tmp.buf = alloca(length + 1);
125 tmp.size = length;
126 memcpy(tmp.buf, buf_ptr, length);
127 tmp.buf[tmp.size] = '\0';
128 if(asn_REAL2double(&tmp, &d)) {
129 rval.code = RC_FAIL;
130 rval.consumed = 0;
131 return rval;
132 }
133 } else {
134 /* This should probably never happen: impractically long value */
135 tmp.buf = CALLOC(1, length + 1);
136 tmp.size = length;
137 if(tmp.buf) memcpy(tmp.buf, buf_ptr, length);
138 if(!tmp.buf || asn_REAL2double(&tmp, &d)) {
139 FREEMEM(tmp.buf);
140 rval.code = RC_FAIL;
141 rval.consumed = 0;
142 return rval;
143 }
144 FREEMEM(tmp.buf);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000145 }
146
147 *Dbl = d;
148 }
149
150 rval.code = RC_OK;
151 rval.consumed += length;
152
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000153 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%f)",
Lev Walkin41ba1f22004-09-14 12:46:35 +0000154 (long)rval.consumed, (long)length, td->name, *Dbl);
155
156 return rval;
157}
158
159/*
160 * Encode the NativeReal using the standard REAL type DER encoder.
161 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000162asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000163NativeReal_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000164 int tag_mode, ber_tlv_tag_t tag,
165 asn_app_consume_bytes_f *cb, void *app_key) {
166 double Dbl = *(const double *)ptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000167 asn_enc_rval_t erval;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000168 REAL_t tmp;
169
Lev Walkina8bbbda2005-02-06 04:29:03 +0000170 /* Prepare a temporary clean structure */
171 memset(&tmp, 0, sizeof(tmp));
172
Lev Walkin5e033762004-09-29 13:26:15 +0000173 if(asn_double2REAL(&tmp, Dbl)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000174 erval.encoded = -1;
175 erval.failed_type = td;
176 erval.structure_ptr = ptr;
177 return erval;
178 }
179
Lev Walkin8e8078a2004-09-26 13:10:40 +0000180 /* Encode a fake REAL */
181 erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000182 if(erval.encoded == -1) {
183 assert(erval.structure_ptr == &tmp);
184 erval.structure_ptr = ptr;
185 }
Lev Walkina8bbbda2005-02-06 04:29:03 +0000186
187 /* Free possibly allocated members of the temporary structure */
Lev Walkinadcb5862006-03-17 02:11:12 +0000188 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
Lev Walkina8bbbda2005-02-06 04:29:03 +0000189
Lev Walkin41ba1f22004-09-14 12:46:35 +0000190 return erval;
191}
192
Lev Walkin725883b2006-10-09 12:07:58 +0000193/*
194 * Decode REAL type using PER.
195 */
196asn_dec_rval_t
197NativeReal_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
198 asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints,
199 void **dbl_ptr, asn_per_data_t *pd) {
200 double *Dbl = (double *)*dbl_ptr;
201 asn_dec_rval_t rval;
202 REAL_t tmp;
203 void *ptmp = &tmp;
204 int ret;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000205
Lev Walkin725883b2006-10-09 12:07:58 +0000206 (void)constraints;
207
208 /*
209 * If the structure is not there, allocate it.
210 */
211 if(Dbl == NULL) {
212 *dbl_ptr = CALLOC(1, sizeof(*Dbl));
213 Dbl = (double *)*dbl_ptr;
214 if(Dbl == NULL)
Lev Walkin7c1dc052016-03-14 03:08:15 -0700215 ASN__DECODE_FAILED;
Lev Walkin725883b2006-10-09 12:07:58 +0000216 }
217
218 memset(&tmp, 0, sizeof(tmp));
219 rval = OCTET_STRING_decode_uper(opt_codec_ctx, td, NULL,
220 &ptmp, pd);
221 if(rval.code != RC_OK) {
222 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
223 return rval;
224 }
225
226 ret = asn_REAL2double(&tmp, Dbl);
227 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700228 if(ret) ASN__DECODE_FAILED;
Lev Walkin725883b2006-10-09 12:07:58 +0000229
230 return rval;
231}
232
Harald Welte0b57b082015-08-30 16:33:07 +0200233asn_dec_rval_t
234NativeReal_decode_aper(asn_codec_ctx_t *opt_codec_ctx,
235 asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints,
236 void **dbl_ptr, asn_per_data_t *pd) {
237 double *Dbl = (double *)*dbl_ptr;
238 asn_dec_rval_t rval;
239 REAL_t tmp;
240 void *ptmp = &tmp;
241 int ret;
242
243 (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)
252 _ASN_DECODE_FAILED;
253 }
254
255 memset(&tmp, 0, sizeof(tmp));
256 rval = OCTET_STRING_decode_aper(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);
265 if(ret) _ASN_DECODE_FAILED;
266
267 return rval;
268}
269
Lev Walkin725883b2006-10-09 12:07:58 +0000270/*
271 * Encode the NativeReal using the OCTET STRING PER encoder.
272 */
273asn_enc_rval_t
274NativeReal_encode_uper(asn_TYPE_descriptor_t *td,
275 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
276 double Dbl = *(const double *)sptr;
277 asn_enc_rval_t erval;
278 REAL_t tmp;
279
280 (void)constraints;
281
282 /* Prepare a temporary clean structure */
283 memset(&tmp, 0, sizeof(tmp));
284
285 if(asn_double2REAL(&tmp, Dbl))
Lev Walkin7c1dc052016-03-14 03:08:15 -0700286 ASN__ENCODE_FAILED;
Lev Walkin725883b2006-10-09 12:07:58 +0000287
288 /* Encode a DER REAL */
289 erval = OCTET_STRING_encode_uper(td, NULL, &tmp, po);
290 if(erval.encoded == -1)
291 erval.structure_ptr = sptr;
292
293 /* Free possibly allocated members of the temporary structure */
294 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
295
296 return erval;
297}
Lev Walkin8471cec2004-10-21 14:02:19 +0000298
Harald Welte0b57b082015-08-30 16:33:07 +0200299asn_enc_rval_t
300NativeReal_encode_aper(asn_TYPE_descriptor_t *td,
301 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
302 double Dbl = *(const double *)sptr;
303 asn_enc_rval_t erval;
304 REAL_t tmp;
305
306 (void)constraints;
307
308 /* Prepare a temporary clean structure */
309 memset(&tmp, 0, sizeof(tmp));
310
311 if(asn_double2REAL(&tmp, Dbl))
312 _ASN_ENCODE_FAILED;
313
314 /* Encode a DER REAL */
315 erval = OCTET_STRING_encode_aper(td, NULL, &tmp, po);
316 if(erval.encoded == -1)
317 erval.structure_ptr = sptr;
318
319 /* Free possibly allocated members of the temporary structure */
320 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
321
322 return erval;
323}
324
Lev Walkin8471cec2004-10-21 14:02:19 +0000325/*
326 * Decode the chunk of XML text encoding REAL.
327 */
328asn_dec_rval_t
329NativeReal_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
330 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000331 const void *buf_ptr, size_t size) {
Lev Walkin8471cec2004-10-21 14:02:19 +0000332 asn_dec_rval_t rval;
333 REAL_t *st = 0;
Lev Walkin1eded352006-07-13 11:19:01 +0000334 REAL_t **stp = &st;
Lev Walkin8471cec2004-10-21 14:02:19 +0000335 double *Dbl = (double *)*sptr;
336
337 if(!Dbl) {
Lev Walkinc17d90f2005-01-17 14:32:45 +0000338 *sptr = CALLOC(1, sizeof(double));
339 Dbl = (double *)*sptr;
Lev Walkin8471cec2004-10-21 14:02:19 +0000340 if(!Dbl) {
341 rval.code = RC_FAIL;
342 rval.consumed = 0;
343 return rval;
344 }
345 }
346
Lev Walkin1eded352006-07-13 11:19:01 +0000347 rval = REAL_decode_xer(opt_codec_ctx, td, (void **)stp, opt_mname,
Lev Walkin8471cec2004-10-21 14:02:19 +0000348 buf_ptr, size);
349 if(rval.code == RC_OK) {
350 if(asn_REAL2double(st, Dbl)) {
351 rval.code = RC_FAIL;
352 rval.consumed = 0;
353 }
354 } else {
355 rval.consumed = 0;
356 }
Lev Walkinadcb5862006-03-17 02:11:12 +0000357 ASN_STRUCT_FREE(asn_DEF_REAL, st);
Lev Walkin8471cec2004-10-21 14:02:19 +0000358 return rval;
359}
360
Lev Walkina9cc46e2004-09-22 16:06:28 +0000361asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000362NativeReal_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000363 int ilevel, enum xer_encoder_flags_e flags,
364 asn_app_consume_bytes_f *cb, void *app_key) {
365 const double *Dbl = (const double *)sptr;
366 asn_enc_rval_t er;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000367
368 (void)ilevel;
369
Lev Walkin7c1dc052016-03-14 03:08:15 -0700370 if(!Dbl) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000371
Lev Walkin8e8078a2004-09-26 13:10:40 +0000372 er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key);
Lev Walkin7c1dc052016-03-14 03:08:15 -0700373 if(er.encoded < 0) ASN__ENCODE_FAILED;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000374
Lev Walkin7c1dc052016-03-14 03:08:15 -0700375 ASN__ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000376}
377
Lev Walkin41ba1f22004-09-14 12:46:35 +0000378/*
379 * REAL specific human-readable output.
380 */
381int
Lev Walkin5e033762004-09-29 13:26:15 +0000382NativeReal_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000383 asn_app_consume_bytes_f *cb, void *app_key) {
384 const double *Dbl = (const double *)sptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000385
386 (void)td; /* Unused argument */
387 (void)ilevel; /* Unused argument */
388
Lev Walkin8e8078a2004-09-26 13:10:40 +0000389 if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000390
391 return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000392}
393
394void
Lev Walkin5e033762004-09-29 13:26:15 +0000395NativeReal_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000396
397 if(!td || !ptr)
398 return;
399
400 ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)",
401 td->name, contents_only, ptr);
402
403 if(!contents_only) {
404 FREEMEM(ptr);
405 }
406}
407