blob: f7755db16e42241f854155aeff681b2cafb75585 [file] [log] [blame]
Lev Walkin41ba1f22004-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 */
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 Walkin41ba1f22004-09-14 12:46:35 +000015
16/*
17 * NativeReal basic type description.
18 */
Lev Walkin5e033762004-09-29 13:26:15 +000019static ber_tlv_tag_t asn_DEF_NativeReal_tags[] = {
Lev Walkin41ba1f22004-09-14 12:46:35 +000020 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
21};
Lev Walkin5e033762004-09-29 13:26:15 +000022asn_TYPE_descriptor_t asn_DEF_NativeReal = {
Lev Walkin41ba1f22004-09-14 12:46:35 +000023 "REAL", /* The ASN.1 type is still REAL */
Lev Walkindc06f6b2004-10-20 15:50:55 +000024 "REAL",
Lev Walkina9cc46e2004-09-22 16:06:28 +000025 NativeReal_free,
26 NativeReal_print,
Lev Walkin41ba1f22004-09-14 12:46:35 +000027 asn_generic_no_constraint,
28 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 Walkin41ba1f22004-09-14 12:46:35 +000032 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-09-29 13:26:15 +000033 asn_DEF_NativeReal_tags,
34 sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]),
35 asn_DEF_NativeReal_tags, /* Same as above */
36 sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]),
Lev Walkin41ba1f22004-09-14 12:46:35 +000037 0, 0, /* No members */
38 0 /* No specifics */
39};
40
41/*
42 * Decode REAL type.
43 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000044asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000045NativeReal_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
46 asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +000047 void **dbl_ptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkin41ba1f22004-09-14 12:46:35 +000048 double *Dbl = (double *)*dbl_ptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +000049 asn_dec_rval_t rval;
Lev Walkin41ba1f22004-09-14 12:46:35 +000050 ber_tlv_len_t length;
51
52 /*
53 * If the structure is not there, allocate it.
54 */
55 if(Dbl == NULL) {
Lev Walkinc17d90f2005-01-17 14:32:45 +000056 *dbl_ptr = CALLOC(1, sizeof(*Dbl));
57 Dbl = (double *)*dbl_ptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +000058 if(Dbl == NULL) {
59 rval.code = RC_FAIL;
60 rval.consumed = 0;
61 return rval;
62 }
63 }
64
65 ASN_DEBUG("Decoding %s as REAL (tm=%d)",
66 td->name, tag_mode);
67
68 /*
69 * Check tags.
70 */
Lev Walkin5e033762004-09-29 13:26:15 +000071 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
72 tag_mode, 0, &length, 0);
Lev Walkin41ba1f22004-09-14 12:46:35 +000073 if(rval.code != RC_OK)
74 return rval;
75
76 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
77
78 /*
79 * Make sure we have this length.
80 */
Lev Walkin8c3b8542005-03-10 18:52:02 +000081 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
Lev Walkin41ba1f22004-09-14 12:46:35 +000082 size -= rval.consumed;
83 if(length > (ber_tlv_len_t)size) {
84 rval.code = RC_WMORE;
85 rval.consumed = 0;
86 return rval;
87 }
88
89 /*
90 * ASN.1 encoded REAL: buf_ptr, length
91 * Fill the Dbl, at the same time checking for overflow.
92 * If overflow occured, return with RC_FAIL.
93 */
94 {
95 REAL_t tmp;
Lev Walkin7e033b52005-07-02 08:19:17 +000096 union {
97 const void *constbuf;
98 void *nonconstbuf;
99 } unconst_buf;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000100 double d;
Lev Walkin7e033b52005-07-02 08:19:17 +0000101
102 unconst_buf.constbuf = buf_ptr;
103 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000104 tmp.size = length;
105
Lev Walkin5e033762004-09-29 13:26:15 +0000106 if(asn_REAL2double(&tmp, &d)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000107 rval.code = RC_FAIL;
108 rval.consumed = 0;
109 return rval;
110 }
111
112 *Dbl = d;
113 }
114
115 rval.code = RC_OK;
116 rval.consumed += length;
117
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000118 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%f)",
Lev Walkin41ba1f22004-09-14 12:46:35 +0000119 (long)rval.consumed, (long)length, td->name, *Dbl);
120
121 return rval;
122}
123
124/*
125 * Encode the NativeReal using the standard REAL type DER encoder.
126 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000127asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000128NativeReal_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000129 int tag_mode, ber_tlv_tag_t tag,
130 asn_app_consume_bytes_f *cb, void *app_key) {
131 double Dbl = *(const double *)ptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000132 asn_enc_rval_t erval;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000133 REAL_t tmp;
134
Lev Walkina8bbbda2005-02-06 04:29:03 +0000135 /* Prepare a temporary clean structure */
136 memset(&tmp, 0, sizeof(tmp));
137
Lev Walkin5e033762004-09-29 13:26:15 +0000138 if(asn_double2REAL(&tmp, Dbl)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000139 erval.encoded = -1;
140 erval.failed_type = td;
141 erval.structure_ptr = ptr;
142 return erval;
143 }
144
Lev Walkin8e8078a2004-09-26 13:10:40 +0000145 /* Encode a fake REAL */
146 erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000147 if(erval.encoded == -1) {
148 assert(erval.structure_ptr == &tmp);
149 erval.structure_ptr = ptr;
150 }
Lev Walkina8bbbda2005-02-06 04:29:03 +0000151
152 /* Free possibly allocated members of the temporary structure */
153 asn_DEF_REAL.free_struct(&asn_DEF_REAL, &tmp, 1);
154
Lev Walkin41ba1f22004-09-14 12:46:35 +0000155 return erval;
156}
157
Lev Walkina9cc46e2004-09-22 16:06:28 +0000158
Lev Walkin8471cec2004-10-21 14:02:19 +0000159
160/*
161 * Decode the chunk of XML text encoding REAL.
162 */
163asn_dec_rval_t
164NativeReal_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
165 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000166 const void *buf_ptr, size_t size) {
Lev Walkin8471cec2004-10-21 14:02:19 +0000167 asn_dec_rval_t rval;
168 REAL_t *st = 0;
169 double *Dbl = (double *)*sptr;
170
171 if(!Dbl) {
Lev Walkinc17d90f2005-01-17 14:32:45 +0000172 *sptr = CALLOC(1, sizeof(double));
173 Dbl = (double *)*sptr;
Lev Walkin8471cec2004-10-21 14:02:19 +0000174 if(!Dbl) {
175 rval.code = RC_FAIL;
176 rval.consumed = 0;
177 return rval;
178 }
179 }
180
181 rval = REAL_decode_xer(opt_codec_ctx, td, (void **)&st, opt_mname,
182 buf_ptr, size);
183 if(rval.code == RC_OK) {
184 if(asn_REAL2double(st, Dbl)) {
185 rval.code = RC_FAIL;
186 rval.consumed = 0;
187 }
188 } else {
189 rval.consumed = 0;
190 }
191 asn_DEF_REAL.free_struct(&asn_DEF_REAL, st, 0);
192 return rval;
193}
194
Lev Walkina9cc46e2004-09-22 16:06:28 +0000195asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000196NativeReal_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000197 int ilevel, enum xer_encoder_flags_e flags,
198 asn_app_consume_bytes_f *cb, void *app_key) {
199 const double *Dbl = (const double *)sptr;
200 asn_enc_rval_t er;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000201
202 (void)ilevel;
203
204 if(!Dbl) _ASN_ENCODE_FAILED;
205
Lev Walkin8e8078a2004-09-26 13:10:40 +0000206 er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000207 if(er.encoded < 0) _ASN_ENCODE_FAILED;
208
209 return er;
210}
211
Lev Walkin41ba1f22004-09-14 12:46:35 +0000212/*
213 * REAL specific human-readable output.
214 */
215int
Lev Walkin5e033762004-09-29 13:26:15 +0000216NativeReal_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000217 asn_app_consume_bytes_f *cb, void *app_key) {
218 const double *Dbl = (const double *)sptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000219
220 (void)td; /* Unused argument */
221 (void)ilevel; /* Unused argument */
222
Lev Walkin8e8078a2004-09-26 13:10:40 +0000223 if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000224
225 return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000226}
227
228void
Lev Walkin5e033762004-09-29 13:26:15 +0000229NativeReal_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000230
231 if(!td || !ptr)
232 return;
233
234 ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)",
235 td->name, contents_only, ptr);
236
237 if(!contents_only) {
238 FREEMEM(ptr);
239 }
240}
241