blob: 2b8ec1666539ed08d2d1a2dbd5556f744087abe3 [file] [log] [blame]
Harald Welte92c45f32010-06-12 18:59:38 +02001/*-
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 * 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
16/*
17 * NativeReal basic type description.
18 */
19static ber_tlv_tag_t asn_DEF_NativeReal_tags[] = {
20 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
21};
22asn_TYPE_descriptor_t asn_DEF_NativeReal = {
23 "REAL", /* The ASN.1 type is still REAL */
24 "REAL",
25 NativeReal_free,
26 NativeReal_print,
27 asn_generic_no_constraint,
28 NativeReal_decode_ber,
29 NativeReal_encode_der,
30 NativeReal_decode_xer,
31 NativeReal_encode_xer,
32 0, 0,
33 0, /* Use generic outmost tag fetcher */
34 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]),
38 0, /* No PER visible constraints */
39 0, 0, /* No members */
40 0 /* No specifics */
41};
42
43/*
44 * Decode REAL type.
45 */
46asn_dec_rval_t
47NativeReal_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
48 asn_TYPE_descriptor_t *td,
49 void **dbl_ptr, const void *buf_ptr, size_t size, int tag_mode) {
50 double *Dbl = (double *)*dbl_ptr;
51 asn_dec_rval_t rval;
52 ber_tlv_len_t length;
53
54 /*
55 * If the structure is not there, allocate it.
56 */
57 if(Dbl == NULL) {
58 *dbl_ptr = CALLOC(1, sizeof(*Dbl));
59 Dbl = (double *)*dbl_ptr;
60 if(Dbl == NULL) {
61 rval.code = RC_FAIL;
62 rval.consumed = 0;
63 return rval;
64 }
65 }
66
67 ASN_DEBUG("Decoding %s as REAL (tm=%d)",
68 td->name, tag_mode);
69
70 /*
71 * Check tags.
72 */
73 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
74 tag_mode, 0, &length, 0);
75 if(rval.code != RC_OK)
76 return rval;
77
78 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
79
80 /*
81 * Make sure we have this length.
82 */
83 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
84 size -= rval.consumed;
85 if(length > (ber_tlv_len_t)size) {
86 rval.code = RC_WMORE;
87 rval.consumed = 0;
88 return rval;
89 }
90
91 /*
92 * ASN.1 encoded REAL: buf_ptr, length
93 * Fill the Dbl, at the same time checking for overflow.
94 * If overflow occured, return with RC_FAIL.
95 */
96 {
97 REAL_t tmp;
98 union {
99 const void *constbuf;
100 void *nonconstbuf;
101 } unconst_buf;
102 double d;
103
104 unconst_buf.constbuf = buf_ptr;
105 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
106 tmp.size = length;
107
108 if(asn_REAL2double(&tmp, &d)) {
109 rval.code = RC_FAIL;
110 rval.consumed = 0;
111 return rval;
112 }
113
114 *Dbl = d;
115 }
116
117 rval.code = RC_OK;
118 rval.consumed += length;
119
120 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%f)",
121 (long)rval.consumed, (long)length, td->name, *Dbl);
122
123 return rval;
124}
125
126/*
127 * Encode the NativeReal using the standard REAL type DER encoder.
128 */
129asn_enc_rval_t
130NativeReal_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
131 int tag_mode, ber_tlv_tag_t tag,
132 asn_app_consume_bytes_f *cb, void *app_key) {
133 double Dbl = *(const double *)ptr;
134 asn_enc_rval_t erval;
135 REAL_t tmp;
136
137 /* Prepare a temporary clean structure */
138 memset(&tmp, 0, sizeof(tmp));
139
140 if(asn_double2REAL(&tmp, Dbl)) {
141 erval.encoded = -1;
142 erval.failed_type = td;
143 erval.structure_ptr = ptr;
144 return erval;
145 }
146
147 /* Encode a fake REAL */
148 erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key);
149 if(erval.encoded == -1) {
150 assert(erval.structure_ptr == &tmp);
151 erval.structure_ptr = ptr;
152 }
153
154 /* Free possibly allocated members of the temporary structure */
155 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
156
157 return erval;
158}
159
160
161
162/*
163 * Decode the chunk of XML text encoding REAL.
164 */
165asn_dec_rval_t
166NativeReal_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
167 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
168 const void *buf_ptr, size_t size) {
169 asn_dec_rval_t rval;
170 REAL_t *st = 0;
171 REAL_t **stp = &st;
172 double *Dbl = (double *)*sptr;
173
174 if(!Dbl) {
175 *sptr = CALLOC(1, sizeof(double));
176 Dbl = (double *)*sptr;
177 if(!Dbl) {
178 rval.code = RC_FAIL;
179 rval.consumed = 0;
180 return rval;
181 }
182 }
183
184 rval = REAL_decode_xer(opt_codec_ctx, td, (void **)stp, opt_mname,
185 buf_ptr, size);
186 if(rval.code == RC_OK) {
187 if(asn_REAL2double(st, Dbl)) {
188 rval.code = RC_FAIL;
189 rval.consumed = 0;
190 }
191 } else {
192 rval.consumed = 0;
193 }
194 ASN_STRUCT_FREE(asn_DEF_REAL, st);
195 return rval;
196}
197
198asn_enc_rval_t
199NativeReal_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
200 int ilevel, enum xer_encoder_flags_e flags,
201 asn_app_consume_bytes_f *cb, void *app_key) {
202 const double *Dbl = (const double *)sptr;
203 asn_enc_rval_t er;
204
205 (void)ilevel;
206
207 if(!Dbl) _ASN_ENCODE_FAILED;
208
209 er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key);
210 if(er.encoded < 0) _ASN_ENCODE_FAILED;
211
212 _ASN_ENCODED_OK(er);
213}
214
215/*
216 * REAL specific human-readable output.
217 */
218int
219NativeReal_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
220 asn_app_consume_bytes_f *cb, void *app_key) {
221 const double *Dbl = (const double *)sptr;
222
223 (void)td; /* Unused argument */
224 (void)ilevel; /* Unused argument */
225
226 if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
227
228 return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0;
229}
230
231void
232NativeReal_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
233
234 if(!td || !ptr)
235 return;
236
237 ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)",
238 td->name, contents_only, ptr);
239
240 if(!contents_only) {
241 FREEMEM(ptr);
242 }
243}
244