blob: 5bc95b053639cd4aec60410ec44fd65e2f15847c [file] [log] [blame]
vlm785435b2004-09-14 12:46:35 +00001/*-
2 * Copyright (c) 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5/*
6 * Please read the NativeReal.h for the explanation wrt. differences between
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 */
vlm39ba4c42004-09-22 16:06:28 +000012#include <asn_internal.h>
vlm785435b2004-09-14 12:46:35 +000013#include <NativeReal.h>
vlm785435b2004-09-14 12:46:35 +000014#include <REAL.h>
15#include <assert.h>
16
17/*
18 * NativeReal basic type description.
19 */
vlmef6355b2004-09-29 13:26:15 +000020static ber_tlv_tag_t asn_DEF_NativeReal_tags[] = {
vlm785435b2004-09-14 12:46:35 +000021 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
22};
vlmef6355b2004-09-29 13:26:15 +000023asn_TYPE_descriptor_t asn_DEF_NativeReal = {
vlm785435b2004-09-14 12:46:35 +000024 "REAL", /* The ASN.1 type is still REAL */
vlm9de248e2004-10-20 15:50:55 +000025 "REAL",
vlm39ba4c42004-09-22 16:06:28 +000026 NativeReal_free,
27 NativeReal_print,
vlm785435b2004-09-14 12:46:35 +000028 asn_generic_no_constraint,
29 NativeReal_decode_ber,
30 NativeReal_encode_der,
vlmb848b2a2004-10-21 14:02:19 +000031 NativeReal_decode_xer,
vlm39ba4c42004-09-22 16:06:28 +000032 NativeReal_encode_xer,
vlm785435b2004-09-14 12:46:35 +000033 0, /* Use generic outmost tag fetcher */
vlmef6355b2004-09-29 13:26:15 +000034 asn_DEF_NativeReal_tags,
35 sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]),
36 asn_DEF_NativeReal_tags, /* Same as above */
37 sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]),
vlm785435b2004-09-14 12:46:35 +000038 0, 0, /* No members */
39 0 /* No specifics */
40};
41
42/*
43 * Decode REAL type.
44 */
vlm9de248e2004-10-20 15:50:55 +000045asn_dec_rval_t
vlmef6355b2004-09-29 13:26:15 +000046NativeReal_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
47 asn_TYPE_descriptor_t *td,
vlmb02dcc62005-03-10 18:52:02 +000048 void **dbl_ptr, const void *buf_ptr, size_t size, int tag_mode) {
vlm785435b2004-09-14 12:46:35 +000049 double *Dbl = (double *)*dbl_ptr;
vlm9de248e2004-10-20 15:50:55 +000050 asn_dec_rval_t rval;
vlm785435b2004-09-14 12:46:35 +000051 ber_tlv_len_t length;
52
53 /*
54 * If the structure is not there, allocate it.
55 */
56 if(Dbl == NULL) {
vlma8f4a4c2005-01-17 14:32:45 +000057 *dbl_ptr = CALLOC(1, sizeof(*Dbl));
58 Dbl = (double *)*dbl_ptr;
vlm785435b2004-09-14 12:46:35 +000059 if(Dbl == NULL) {
60 rval.code = RC_FAIL;
61 rval.consumed = 0;
62 return rval;
63 }
64 }
65
66 ASN_DEBUG("Decoding %s as REAL (tm=%d)",
67 td->name, tag_mode);
68
69 /*
70 * Check tags.
71 */
vlmef6355b2004-09-29 13:26:15 +000072 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
73 tag_mode, 0, &length, 0);
vlm785435b2004-09-14 12:46:35 +000074 if(rval.code != RC_OK)
75 return rval;
76
77 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
78
79 /*
80 * Make sure we have this length.
81 */
vlmb02dcc62005-03-10 18:52:02 +000082 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
vlm785435b2004-09-14 12:46:35 +000083 size -= rval.consumed;
84 if(length > (ber_tlv_len_t)size) {
85 rval.code = RC_WMORE;
86 rval.consumed = 0;
87 return rval;
88 }
89
90 /*
91 * ASN.1 encoded REAL: buf_ptr, length
92 * Fill the Dbl, at the same time checking for overflow.
93 * If overflow occured, return with RC_FAIL.
94 */
95 {
96 REAL_t tmp;
97 double d;
vlmb02dcc62005-03-10 18:52:02 +000098 (const uint8_t *)tmp.buf = (const uint8_t *)buf_ptr;
vlm785435b2004-09-14 12:46:35 +000099 tmp.size = length;
100
vlmef6355b2004-09-29 13:26:15 +0000101 if(asn_REAL2double(&tmp, &d)) {
vlm785435b2004-09-14 12:46:35 +0000102 rval.code = RC_FAIL;
103 rval.consumed = 0;
104 return rval;
105 }
106
107 *Dbl = d;
108 }
109
110 rval.code = RC_OK;
111 rval.consumed += length;
112
vlm13318c12004-10-26 10:56:10 +0000113 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%f)",
vlm785435b2004-09-14 12:46:35 +0000114 (long)rval.consumed, (long)length, td->name, *Dbl);
115
116 return rval;
117}
118
119/*
120 * Encode the NativeReal using the standard REAL type DER encoder.
121 */
vlm39ba4c42004-09-22 16:06:28 +0000122asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000123NativeReal_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
vlm785435b2004-09-14 12:46:35 +0000124 int tag_mode, ber_tlv_tag_t tag,
125 asn_app_consume_bytes_f *cb, void *app_key) {
126 double Dbl = *(const double *)ptr;
vlm39ba4c42004-09-22 16:06:28 +0000127 asn_enc_rval_t erval;
vlm785435b2004-09-14 12:46:35 +0000128 REAL_t tmp;
129
vlm83386ef2005-02-06 04:29:03 +0000130 /* Prepare a temporary clean structure */
131 memset(&tmp, 0, sizeof(tmp));
132
vlmef6355b2004-09-29 13:26:15 +0000133 if(asn_double2REAL(&tmp, Dbl)) {
vlm785435b2004-09-14 12:46:35 +0000134 erval.encoded = -1;
135 erval.failed_type = td;
136 erval.structure_ptr = ptr;
137 return erval;
138 }
139
vlm6678cb12004-09-26 13:10:40 +0000140 /* Encode a fake REAL */
141 erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key);
vlm785435b2004-09-14 12:46:35 +0000142 if(erval.encoded == -1) {
143 assert(erval.structure_ptr == &tmp);
144 erval.structure_ptr = ptr;
145 }
vlm83386ef2005-02-06 04:29:03 +0000146
147 /* Free possibly allocated members of the temporary structure */
148 asn_DEF_REAL.free_struct(&asn_DEF_REAL, &tmp, 1);
149
vlm785435b2004-09-14 12:46:35 +0000150 return erval;
151}
152
vlm39ba4c42004-09-22 16:06:28 +0000153
vlmb848b2a2004-10-21 14:02:19 +0000154
155/*
156 * Decode the chunk of XML text encoding REAL.
157 */
158asn_dec_rval_t
159NativeReal_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
160 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
vlmb02dcc62005-03-10 18:52:02 +0000161 const void *buf_ptr, size_t size) {
vlmb848b2a2004-10-21 14:02:19 +0000162 asn_dec_rval_t rval;
163 REAL_t *st = 0;
164 double *Dbl = (double *)*sptr;
165
166 if(!Dbl) {
vlma8f4a4c2005-01-17 14:32:45 +0000167 *sptr = CALLOC(1, sizeof(double));
168 Dbl = (double *)*sptr;
vlmb848b2a2004-10-21 14:02:19 +0000169 if(!Dbl) {
170 rval.code = RC_FAIL;
171 rval.consumed = 0;
172 return rval;
173 }
174 }
175
176 rval = REAL_decode_xer(opt_codec_ctx, td, (void **)&st, opt_mname,
177 buf_ptr, size);
178 if(rval.code == RC_OK) {
179 if(asn_REAL2double(st, Dbl)) {
180 rval.code = RC_FAIL;
181 rval.consumed = 0;
182 }
183 } else {
184 rval.consumed = 0;
185 }
186 asn_DEF_REAL.free_struct(&asn_DEF_REAL, st, 0);
187 return rval;
188}
189
vlm39ba4c42004-09-22 16:06:28 +0000190asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000191NativeReal_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000192 int ilevel, enum xer_encoder_flags_e flags,
193 asn_app_consume_bytes_f *cb, void *app_key) {
194 const double *Dbl = (const double *)sptr;
195 asn_enc_rval_t er;
vlm39ba4c42004-09-22 16:06:28 +0000196
197 (void)ilevel;
198
199 if(!Dbl) _ASN_ENCODE_FAILED;
200
vlm6678cb12004-09-26 13:10:40 +0000201 er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key);
vlm39ba4c42004-09-22 16:06:28 +0000202 if(er.encoded < 0) _ASN_ENCODE_FAILED;
203
204 return er;
205}
206
vlm785435b2004-09-14 12:46:35 +0000207/*
208 * REAL specific human-readable output.
209 */
210int
vlmef6355b2004-09-29 13:26:15 +0000211NativeReal_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlm785435b2004-09-14 12:46:35 +0000212 asn_app_consume_bytes_f *cb, void *app_key) {
213 const double *Dbl = (const double *)sptr;
vlm785435b2004-09-14 12:46:35 +0000214
215 (void)td; /* Unused argument */
216 (void)ilevel; /* Unused argument */
217
vlm6678cb12004-09-26 13:10:40 +0000218 if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlm39ba4c42004-09-22 16:06:28 +0000219
220 return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0;
vlm785435b2004-09-14 12:46:35 +0000221}
222
223void
vlmef6355b2004-09-29 13:26:15 +0000224NativeReal_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
vlm785435b2004-09-14 12:46:35 +0000225
226 if(!td || !ptr)
227 return;
228
229 ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)",
230 td->name, contents_only, ptr);
231
232 if(!contents_only) {
233 FREEMEM(ptr);
234 }
235}
236