blob: 2b8ec1666539ed08d2d1a2dbd5556f744087abe3 [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/*
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 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 Walkin523de9e2006-08-18 01:34:18 +000032 0, 0,
Lev Walkin41ba1f22004-09-14 12:46:35 +000033 0, /* Use generic outmost tag fetcher */
Lev Walkin5e033762004-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]),
Lev Walkin59b176e2005-11-26 11:25:14 +000038 0, /* No PER visible constraints */
Lev Walkin41ba1f22004-09-14 12:46:35 +000039 0, 0, /* No members */
40 0 /* No specifics */
41};
42
43/*
44 * Decode REAL type.
45 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000046asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000047NativeReal_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
48 asn_TYPE_descriptor_t *td,
Lev Walkin8c3b8542005-03-10 18:52:02 +000049 void **dbl_ptr, const void *buf_ptr, size_t size, int tag_mode) {
Lev Walkin41ba1f22004-09-14 12:46:35 +000050 double *Dbl = (double *)*dbl_ptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +000051 asn_dec_rval_t rval;
Lev Walkin41ba1f22004-09-14 12:46:35 +000052 ber_tlv_len_t length;
53
54 /*
55 * If the structure is not there, allocate it.
56 */
57 if(Dbl == NULL) {
Lev Walkinc17d90f2005-01-17 14:32:45 +000058 *dbl_ptr = CALLOC(1, sizeof(*Dbl));
59 Dbl = (double *)*dbl_ptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +000060 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 */
Lev Walkin5e033762004-09-29 13:26:15 +000073 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
74 tag_mode, 0, &length, 0);
Lev Walkin41ba1f22004-09-14 12:46:35 +000075 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 */
Lev Walkin8c3b8542005-03-10 18:52:02 +000083 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
Lev Walkin41ba1f22004-09-14 12:46:35 +000084 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;
Lev Walkin7e033b52005-07-02 08:19:17 +000098 union {
99 const void *constbuf;
100 void *nonconstbuf;
101 } unconst_buf;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000102 double d;
Lev Walkin7e033b52005-07-02 08:19:17 +0000103
104 unconst_buf.constbuf = buf_ptr;
105 tmp.buf = (uint8_t *)unconst_buf.nonconstbuf;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000106 tmp.size = length;
107
Lev Walkin5e033762004-09-29 13:26:15 +0000108 if(asn_REAL2double(&tmp, &d)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000109 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
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000120 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%f)",
Lev Walkin41ba1f22004-09-14 12:46:35 +0000121 (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 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000129asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000130NativeReal_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000131 int tag_mode, ber_tlv_tag_t tag,
132 asn_app_consume_bytes_f *cb, void *app_key) {
133 double Dbl = *(const double *)ptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000134 asn_enc_rval_t erval;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000135 REAL_t tmp;
136
Lev Walkina8bbbda2005-02-06 04:29:03 +0000137 /* Prepare a temporary clean structure */
138 memset(&tmp, 0, sizeof(tmp));
139
Lev Walkin5e033762004-09-29 13:26:15 +0000140 if(asn_double2REAL(&tmp, Dbl)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000141 erval.encoded = -1;
142 erval.failed_type = td;
143 erval.structure_ptr = ptr;
144 return erval;
145 }
146
Lev Walkin8e8078a2004-09-26 13:10:40 +0000147 /* Encode a fake REAL */
148 erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000149 if(erval.encoded == -1) {
150 assert(erval.structure_ptr == &tmp);
151 erval.structure_ptr = ptr;
152 }
Lev Walkina8bbbda2005-02-06 04:29:03 +0000153
154 /* Free possibly allocated members of the temporary structure */
Lev Walkinadcb5862006-03-17 02:11:12 +0000155 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);
Lev Walkina8bbbda2005-02-06 04:29:03 +0000156
Lev Walkin41ba1f22004-09-14 12:46:35 +0000157 return erval;
158}
159
Lev Walkina9cc46e2004-09-22 16:06:28 +0000160
Lev Walkin8471cec2004-10-21 14:02:19 +0000161
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,
Lev Walkin8c3b8542005-03-10 18:52:02 +0000168 const void *buf_ptr, size_t size) {
Lev Walkin8471cec2004-10-21 14:02:19 +0000169 asn_dec_rval_t rval;
170 REAL_t *st = 0;
Lev Walkin1eded352006-07-13 11:19:01 +0000171 REAL_t **stp = &st;
Lev Walkin8471cec2004-10-21 14:02:19 +0000172 double *Dbl = (double *)*sptr;
173
174 if(!Dbl) {
Lev Walkinc17d90f2005-01-17 14:32:45 +0000175 *sptr = CALLOC(1, sizeof(double));
176 Dbl = (double *)*sptr;
Lev Walkin8471cec2004-10-21 14:02:19 +0000177 if(!Dbl) {
178 rval.code = RC_FAIL;
179 rval.consumed = 0;
180 return rval;
181 }
182 }
183
Lev Walkin1eded352006-07-13 11:19:01 +0000184 rval = REAL_decode_xer(opt_codec_ctx, td, (void **)stp, opt_mname,
Lev Walkin8471cec2004-10-21 14:02:19 +0000185 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 }
Lev Walkinadcb5862006-03-17 02:11:12 +0000194 ASN_STRUCT_FREE(asn_DEF_REAL, st);
Lev Walkin8471cec2004-10-21 14:02:19 +0000195 return rval;
196}
197
Lev Walkina9cc46e2004-09-22 16:06:28 +0000198asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000199NativeReal_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000200 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;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000204
205 (void)ilevel;
206
207 if(!Dbl) _ASN_ENCODE_FAILED;
208
Lev Walkin8e8078a2004-09-26 13:10:40 +0000209 er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000210 if(er.encoded < 0) _ASN_ENCODE_FAILED;
211
Lev Walkin59b176e2005-11-26 11:25:14 +0000212 _ASN_ENCODED_OK(er);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000213}
214
Lev Walkin41ba1f22004-09-14 12:46:35 +0000215/*
216 * REAL specific human-readable output.
217 */
218int
Lev Walkin5e033762004-09-29 13:26:15 +0000219NativeReal_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000220 asn_app_consume_bytes_f *cb, void *app_key) {
221 const double *Dbl = (const double *)sptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000222
223 (void)td; /* Unused argument */
224 (void)ilevel; /* Unused argument */
225
Lev Walkin8e8078a2004-09-26 13:10:40 +0000226 if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000227
228 return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000229}
230
231void
Lev Walkin5e033762004-09-29 13:26:15 +0000232NativeReal_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000233
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