Debian Codegraph @

skeletons/NativeReal.c

raw symbols SCIP
1 /*-
2 * Copyright (c) 2004, 2006 Lev Walkin <[email protected]>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5 /*
6 * 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 */
12 #include <asn_internal.h>
13 #include <NativeReal.h>
14 #include <REAL.h>
15 #include <OCTET_STRING.h>
16
17 /*
18 * NativeReal basic type description.
19 */
20 static const ber_tlv_tag_t asn_DEF_NativeReal_tags[] = {
21 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
22 };
23 asn_TYPE_descriptor_t asn_DEF_NativeReal = {
24 "REAL", /* The ASN.1 type is still REAL */
25 "REAL",
26 NativeReal_free,
27 NativeReal_print,
28 asn_generic_no_constraint,
29 NativeReal_decode_ber,
30 NativeReal_encode_der,
31 NativeReal_decode_xer,
32 NativeReal_encode_xer,
33 NativeReal_decode_uper,
34 NativeReal_encode_uper,
35 0, /* Use generic outmost tag fetcher */
36 asn_DEF_NativeReal_tags,
37 sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]),
38 asn_DEF_NativeReal_tags, /* Same as above */
39 sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]),
40 0, /* No PER visible constraints */
41 0, 0, /* No members */
42 0 /* No specifics */
43 };
44
45 /*
46 * Decode REAL type.
47 */
48 asn_dec_rval_t
49 NativeReal_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
50 asn_TYPE_descriptor_t *td,
51 void **dbl_ptr, const void *buf_ptr, size_t size, int tag_mode) {
52 double *Dbl = (double *)*dbl_ptr;
53 asn_dec_rval_t rval;
54 ber_tlv_len_t length;
55
56 /*
57 * If the structure is not there, allocate it.
58 */
59 if(Dbl == NULL) {
60 *dbl_ptr = CALLOC(1, sizeof(*Dbl));
61 Dbl = (double *)*dbl_ptr;
62 if(Dbl == NULL) {
63 rval.code = RC_FAIL;
64 rval.consumed = 0;
65 return rval;
66 }
67 }
68
69 ASN_DEBUG("Decoding %s as REAL (tm=%d)",
70 td->name, tag_mode);
71
72 /*
73 * Check tags.
74 */
75 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
76 tag_mode, 0, &length, 0);
77 if(rval.code != RC_OK)
78 return rval;
79
80 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
81
82 /*
83 * Make sure we have this length.
84 */
85 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
86 size -= rval.consumed;
87 if(length > (ber_tlv_len_t)size) {
88 rval.code = RC_WMORE;
89 rval.consumed = 0;
90 return rval;
91 }
92
93 /*
94 * ASN.1 encoded REAL: buf_ptr, length
95 * Fill the Dbl, at the same time checking for overflow.
96 * If overflow occured, return with RC_FAIL.
97 */
98 {
99 REAL_t tmp;
100 union {
101 const void *constbuf;
102 void *nonconstbuf;
103 } unconst_buf;
104 double d;
105
106 unconst_buf.constbuf = buf_ptr;
107 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
108 tmp.size = length;
109
110 if(length < (ber_tlv_len_t)size) {
111 int ret;
112 uint8_t saved_byte = tmp.buf[tmp.size];
113 tmp.buf[tmp.size] = '\0';
114 ret = asn_REAL2double(&tmp, &d);
115 tmp.buf[tmp.size] = saved_byte;
116 if(ret) {
117 rval.code = RC_FAIL;
118 rval.consumed = 0;
119 return rval;
120 }
121 } else if(length < 48 /* Enough for longish %f value. */) {
122 tmp.buf = alloca(length + 1);
123 tmp.size = length;
124 memcpy(tmp.buf, buf_ptr, length);
125 tmp.buf[tmp.size] = '\0';
126 if(asn_REAL2double(&tmp, &d)) {
127 rval.code = RC_FAIL;
128 rval.consumed = 0;
129 return rval;
130 }
131 } else {
132 /* This should probably never happen: impractically long value */
133 tmp.buf = CALLOC(1, length + 1);
134 tmp.size = length;
135 if(tmp.buf) memcpy(tmp.buf, buf_ptr, length);
136 if(!tmp.buf || asn_REAL2double(&tmp, &d)) {
137 FREEMEM(tmp.buf);
138 rval.code = RC_FAIL;
139 rval.consumed = 0;
140 return rval;
141 }
142 FREEMEM(tmp.buf);
143 }
144
145 *Dbl = d;
146 }
147
148 rval.code = RC_OK;
149 rval.consumed += length;
150
151 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%f)",
152 (long)rval.consumed, (long)length, td->name, *Dbl);
153
154 return rval;
155 }
156
157 /*
158 * Encode the NativeReal using the standard REAL type DER encoder.
159 */
160 asn_enc_rval_t
161 NativeReal_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
162 int tag_mode, ber_tlv_tag_t tag,
163 asn_app_consume_bytes_f *cb, void *app_key) {
164 double Dbl = *(const double *)ptr;
165 asn_enc_rval_t erval;
166 REAL_t tmp;
167
168 /* Prepare a temporary clean structure */
169 memset(&tmp, 0, sizeof(tmp));
170
171 if(asn_double2REAL(&tmp, Dbl)) {
172 erval.encoded = -1;
173 erval.failed_type = td;
174 erval.structure_ptr = ptr;
175 return erval;
176 }
177
178 /* Encode a fake REAL */
179 erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key);
180 if(erval.encoded == -1) {
181 assert(erval.structure_ptr == &tmp);
182 erval.structure_ptr = ptr;
183 }
184
185 /* Free possibly allocated members of the temporary structure */
186 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
187
188 return erval;
189 }
190
191 /*
192 * Decode REAL type using PER.
193 */
194 asn_dec_rval_t
195 NativeReal_decode_uper(asn_codec_ctx_t *opt_codec_ctx,
196 asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints,
197 void **dbl_ptr, asn_per_data_t *pd) {
198 double *Dbl = (double *)*dbl_ptr;
199 asn_dec_rval_t rval;
200 REAL_t tmp;
201 void *ptmp = &tmp;
202 int ret;
203
204 (void)constraints;
205
206 /*
207 * If the structure is not there, allocate it.
208 */
209 if(Dbl == NULL) {
210 *dbl_ptr = CALLOC(1, sizeof(*Dbl));
211 Dbl = (double *)*dbl_ptr;
212 if(Dbl == NULL)
213 ASN__DECODE_FAILED;
214 }
215
216 memset(&tmp, 0, sizeof(tmp));
217 rval = OCTET_STRING_decode_uper(opt_codec_ctx, td, NULL,
218 &ptmp, pd);
219 if(rval.code != RC_OK) {
220 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
221 return rval;
222 }
223
224 ret = asn_REAL2double(&tmp, Dbl);
225 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
226 if(ret) ASN__DECODE_FAILED;
227
228 return rval;
229 }
230
231 /*
232 * Encode the NativeReal using the OCTET STRING PER encoder.
233 */
234 asn_enc_rval_t
235 NativeReal_encode_uper(asn_TYPE_descriptor_t *td,
236 asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) {
237 double Dbl = *(const double *)sptr;
238 asn_enc_rval_t erval;
239 REAL_t tmp;
240
241 (void)constraints;
242
243 /* Prepare a temporary clean structure */
244 memset(&tmp, 0, sizeof(tmp));
245
246 if(asn_double2REAL(&tmp, Dbl))
247 ASN__ENCODE_FAILED;
248
249 /* Encode a DER REAL */
250 erval = OCTET_STRING_encode_uper(td, NULL, &tmp, po);
251 if(erval.encoded == -1)
252 erval.structure_ptr = sptr;
253
254 /* Free possibly allocated members of the temporary structure */
255 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
256
257 return erval;
258 }
259
260 /*
261 * Decode the chunk of XML text encoding REAL.
262 */
263 asn_dec_rval_t
264 NativeReal_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
265 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
266 const void *buf_ptr, size_t size) {
267 asn_dec_rval_t rval;
268 REAL_t *st = 0;
269 REAL_t **stp = &st;
270 double *Dbl = (double *)*sptr;
271
272 if(!Dbl) {
273 *sptr = CALLOC(1, sizeof(double));
274 Dbl = (double *)*sptr;
275 if(!Dbl) {
276 rval.code = RC_FAIL;
277 rval.consumed = 0;
278 return rval;
279 }
280 }
281
282 rval = REAL_decode_xer(opt_codec_ctx, td, (void **)stp, opt_mname,
283 buf_ptr, size);
284 if(rval.code == RC_OK) {
285 if(asn_REAL2double(st, Dbl)) {
286 rval.code = RC_FAIL;
287 rval.consumed = 0;
288 }
289 } else {
290 rval.consumed = 0;
291 }
292 ASN_STRUCT_FREE(asn_DEF_REAL, st);
293 return rval;
294 }
295
296 asn_enc_rval_t
297 NativeReal_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
298 int ilevel, enum xer_encoder_flags_e flags,
299 asn_app_consume_bytes_f *cb, void *app_key) {
300 const double *Dbl = (const double *)sptr;
301 asn_enc_rval_t er;
302
303 (void)ilevel;
304
305 if(!Dbl) ASN__ENCODE_FAILED;
306
307 er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key);
308 if(er.encoded < 0) ASN__ENCODE_FAILED;
309
310 ASN__ENCODED_OK(er);
311 }
312
313 /*
314 * REAL specific human-readable output.
315 */
316 int
317 NativeReal_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
318 asn_app_consume_bytes_f *cb, void *app_key) {
319 const double *Dbl = (const double *)sptr;
320
321 (void)td; /* Unused argument */
322 (void)ilevel; /* Unused argument */
323
324 if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
325
326 return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0;
327 }
328
329 void
330 NativeReal_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
331
332 if(!td || !ptr)
333 return;
334
335 ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)",
336 td->name, contents_only, ptr);
337
338 if(!contents_only) {
339 FREEMEM(ptr);
340 }
341 }
342