blob: 594bdb5f55ac81bb11ddde3a6b960670af6e3bf0 [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>
15#include <assert.h>
16
17/*
18 * NativeReal basic type description.
19 */
Lev Walkin5e033762004-09-29 13:26:15 +000020static ber_tlv_tag_t asn_DEF_NativeReal_tags[] = {
Lev Walkin41ba1f22004-09-14 12:46:35 +000021 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
22};
Lev Walkin5e033762004-09-29 13:26:15 +000023asn_TYPE_descriptor_t asn_DEF_NativeReal = {
Lev Walkin41ba1f22004-09-14 12:46:35 +000024 "REAL", /* The ASN.1 type is still REAL */
Lev Walkindc06f6b2004-10-20 15:50:55 +000025 "REAL",
Lev Walkina9cc46e2004-09-22 16:06:28 +000026 NativeReal_free,
27 NativeReal_print,
Lev Walkin41ba1f22004-09-14 12:46:35 +000028 asn_generic_no_constraint,
29 NativeReal_decode_ber,
30 NativeReal_encode_der,
Lev Walkin8471cec2004-10-21 14:02:19 +000031 NativeReal_decode_xer,
Lev Walkina9cc46e2004-09-22 16:06:28 +000032 NativeReal_encode_xer,
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 Walkin41ba1f22004-09-14 12:46:35 +000038 0, 0, /* No members */
39 0 /* No specifics */
40};
41
42/*
43 * Decode REAL type.
44 */
Lev Walkindc06f6b2004-10-20 15:50:55 +000045asn_dec_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +000046NativeReal_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
47 asn_TYPE_descriptor_t *td,
Lev Walkin41ba1f22004-09-14 12:46:35 +000048 void **dbl_ptr, void *buf_ptr, size_t size, int tag_mode) {
49 double *Dbl = (double *)*dbl_ptr;
Lev Walkindc06f6b2004-10-20 15:50:55 +000050 asn_dec_rval_t rval;
Lev Walkin41ba1f22004-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) {
Lev Walkinc17d90f2005-01-17 14:32:45 +000057 *dbl_ptr = CALLOC(1, sizeof(*Dbl));
58 Dbl = (double *)*dbl_ptr;
Lev Walkin41ba1f22004-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 */
Lev Walkin5e033762004-09-29 13:26:15 +000072 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
73 tag_mode, 0, &length, 0);
Lev Walkin41ba1f22004-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 */
82 buf_ptr = ((char *)buf_ptr) + rval.consumed;
83 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;
98 tmp.buf = (uint8_t *)buf_ptr;
99 tmp.size = length;
100
Lev Walkin5e033762004-09-29 13:26:15 +0000101 if(asn_REAL2double(&tmp, &d)) {
Lev Walkin41ba1f22004-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
Lev Walkin1e3ccbb2004-10-26 10:56:10 +0000113 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%f)",
Lev Walkin41ba1f22004-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 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000122asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000123NativeReal_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
Lev Walkin41ba1f22004-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;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000127 asn_enc_rval_t erval;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000128 REAL_t tmp;
129
Lev Walkin5e033762004-09-29 13:26:15 +0000130 if(asn_double2REAL(&tmp, Dbl)) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000131 erval.encoded = -1;
132 erval.failed_type = td;
133 erval.structure_ptr = ptr;
134 return erval;
135 }
136
Lev Walkin8e8078a2004-09-26 13:10:40 +0000137 /* Encode a fake REAL */
138 erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key);
Lev Walkin41ba1f22004-09-14 12:46:35 +0000139 if(erval.encoded == -1) {
140 assert(erval.structure_ptr == &tmp);
141 erval.structure_ptr = ptr;
142 }
143 return erval;
144}
145
Lev Walkina9cc46e2004-09-22 16:06:28 +0000146
Lev Walkin8471cec2004-10-21 14:02:19 +0000147
148/*
149 * Decode the chunk of XML text encoding REAL.
150 */
151asn_dec_rval_t
152NativeReal_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
153 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
154 void *buf_ptr, size_t size) {
155 asn_dec_rval_t rval;
156 REAL_t *st = 0;
157 double *Dbl = (double *)*sptr;
158
159 if(!Dbl) {
Lev Walkinc17d90f2005-01-17 14:32:45 +0000160 *sptr = CALLOC(1, sizeof(double));
161 Dbl = (double *)*sptr;
Lev Walkin8471cec2004-10-21 14:02:19 +0000162 if(!Dbl) {
163 rval.code = RC_FAIL;
164 rval.consumed = 0;
165 return rval;
166 }
167 }
168
169 rval = REAL_decode_xer(opt_codec_ctx, td, (void **)&st, opt_mname,
170 buf_ptr, size);
171 if(rval.code == RC_OK) {
172 if(asn_REAL2double(st, Dbl)) {
173 rval.code = RC_FAIL;
174 rval.consumed = 0;
175 }
176 } else {
177 rval.consumed = 0;
178 }
179 asn_DEF_REAL.free_struct(&asn_DEF_REAL, st, 0);
180 return rval;
181}
182
Lev Walkina9cc46e2004-09-22 16:06:28 +0000183asn_enc_rval_t
Lev Walkin5e033762004-09-29 13:26:15 +0000184NativeReal_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
Lev Walkina9cc46e2004-09-22 16:06:28 +0000185 int ilevel, enum xer_encoder_flags_e flags,
186 asn_app_consume_bytes_f *cb, void *app_key) {
187 const double *Dbl = (const double *)sptr;
188 asn_enc_rval_t er;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000189
190 (void)ilevel;
191
192 if(!Dbl) _ASN_ENCODE_FAILED;
193
Lev Walkin8e8078a2004-09-26 13:10:40 +0000194 er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key);
Lev Walkina9cc46e2004-09-22 16:06:28 +0000195 if(er.encoded < 0) _ASN_ENCODE_FAILED;
196
197 return er;
198}
199
Lev Walkin41ba1f22004-09-14 12:46:35 +0000200/*
201 * REAL specific human-readable output.
202 */
203int
Lev Walkin5e033762004-09-29 13:26:15 +0000204NativeReal_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
Lev Walkin41ba1f22004-09-14 12:46:35 +0000205 asn_app_consume_bytes_f *cb, void *app_key) {
206 const double *Dbl = (const double *)sptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000207
208 (void)td; /* Unused argument */
209 (void)ilevel; /* Unused argument */
210
Lev Walkin8e8078a2004-09-26 13:10:40 +0000211 if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000212
213 return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000214}
215
216void
Lev Walkin5e033762004-09-29 13:26:15 +0000217NativeReal_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
Lev Walkin41ba1f22004-09-14 12:46:35 +0000218
219 if(!td || !ptr)
220 return;
221
222 ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)",
223 td->name, contents_only, ptr);
224
225 if(!contents_only) {
226 FREEMEM(ptr);
227 }
228}
229