blob: ae33d6428167ea9fb60d7ba5c6586266476a2f8f [file] [log] [blame]
vlm785435b2004-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 */
vlm39ba4c42004-09-22 16:06:28 +000012#include <asn_internal.h>
vlm785435b2004-09-14 12:46:35 +000013#include <NativeReal.h>
vlm785435b2004-09-14 12:46:35 +000014#include <REAL.h>
15#include <assert.h>
16
17/*
18 * NativeReal basic type description.
19 */
vlmef6355b2004-09-29 13:26:15 +000020static ber_tlv_tag_t asn_DEF_NativeReal_tags[] = {
vlm785435b2004-09-14 12:46:35 +000021 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
22};
vlmef6355b2004-09-29 13:26:15 +000023asn_TYPE_descriptor_t asn_DEF_NativeReal = {
vlm785435b2004-09-14 12:46:35 +000024 "REAL", /* The ASN.1 type is still REAL */
vlm9de248e2004-10-20 15:50:55 +000025 "REAL",
vlm39ba4c42004-09-22 16:06:28 +000026 NativeReal_free,
27 NativeReal_print,
vlm785435b2004-09-14 12:46:35 +000028 asn_generic_no_constraint,
29 NativeReal_decode_ber,
30 NativeReal_encode_der,
vlmb848b2a2004-10-21 14:02:19 +000031 NativeReal_decode_xer,
vlm39ba4c42004-09-22 16:06:28 +000032 NativeReal_encode_xer,
vlm785435b2004-09-14 12:46:35 +000033 0, /* Use generic outmost tag fetcher */
vlmef6355b2004-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]),
vlm785435b2004-09-14 12:46:35 +000038 0, 0, /* No members */
39 0 /* No specifics */
40};
41
42/*
43 * Decode REAL type.
44 */
vlm9de248e2004-10-20 15:50:55 +000045asn_dec_rval_t
vlmef6355b2004-09-29 13:26:15 +000046NativeReal_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
47 asn_TYPE_descriptor_t *td,
vlm785435b2004-09-14 12:46:35 +000048 void **dbl_ptr, void *buf_ptr, size_t size, int tag_mode) {
49 double *Dbl = (double *)*dbl_ptr;
vlm9de248e2004-10-20 15:50:55 +000050 asn_dec_rval_t rval;
vlm785435b2004-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) {
57 (void *)Dbl = *dbl_ptr = CALLOC(1, sizeof(*Dbl));
58 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 */
vlmef6355b2004-09-29 13:26:15 +000071 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
72 tag_mode, 0, &length, 0);
vlm785435b2004-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 */
81 buf_ptr = ((char *)buf_ptr) + rval.consumed;
82 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;
96 double d;
97 tmp.buf = (uint8_t *)buf_ptr;
98 tmp.size = length;
99
vlmef6355b2004-09-29 13:26:15 +0000100 if(asn_REAL2double(&tmp, &d)) {
vlm785435b2004-09-14 12:46:35 +0000101 rval.code = RC_FAIL;
102 rval.consumed = 0;
103 return rval;
104 }
105
106 *Dbl = d;
107 }
108
109 rval.code = RC_OK;
110 rval.consumed += length;
111
vlm13318c12004-10-26 10:56:10 +0000112 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%f)",
vlm785435b2004-09-14 12:46:35 +0000113 (long)rval.consumed, (long)length, td->name, *Dbl);
114
115 return rval;
116}
117
118/*
119 * Encode the NativeReal using the standard REAL type DER encoder.
120 */
vlm39ba4c42004-09-22 16:06:28 +0000121asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000122NativeReal_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
vlm785435b2004-09-14 12:46:35 +0000123 int tag_mode, ber_tlv_tag_t tag,
124 asn_app_consume_bytes_f *cb, void *app_key) {
125 double Dbl = *(const double *)ptr;
vlm39ba4c42004-09-22 16:06:28 +0000126 asn_enc_rval_t erval;
vlm785435b2004-09-14 12:46:35 +0000127 REAL_t tmp;
128
vlmef6355b2004-09-29 13:26:15 +0000129 if(asn_double2REAL(&tmp, Dbl)) {
vlm785435b2004-09-14 12:46:35 +0000130 erval.encoded = -1;
131 erval.failed_type = td;
132 erval.structure_ptr = ptr;
133 return erval;
134 }
135
vlm6678cb12004-09-26 13:10:40 +0000136 /* Encode a fake REAL */
137 erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key);
vlm785435b2004-09-14 12:46:35 +0000138 if(erval.encoded == -1) {
139 assert(erval.structure_ptr == &tmp);
140 erval.structure_ptr = ptr;
141 }
142 return erval;
143}
144
vlm39ba4c42004-09-22 16:06:28 +0000145
vlmb848b2a2004-10-21 14:02:19 +0000146
147/*
148 * Decode the chunk of XML text encoding REAL.
149 */
150asn_dec_rval_t
151NativeReal_decode_xer(asn_codec_ctx_t *opt_codec_ctx,
152 asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname,
153 void *buf_ptr, size_t size) {
154 asn_dec_rval_t rval;
155 REAL_t *st = 0;
156 double *Dbl = (double *)*sptr;
157
158 if(!Dbl) {
159 (void *)Dbl = *sptr = CALLOC(1, sizeof(double));
160 if(!Dbl) {
161 rval.code = RC_FAIL;
162 rval.consumed = 0;
163 return rval;
164 }
165 }
166
167 rval = REAL_decode_xer(opt_codec_ctx, td, (void **)&st, opt_mname,
168 buf_ptr, size);
169 if(rval.code == RC_OK) {
170 if(asn_REAL2double(st, Dbl)) {
171 rval.code = RC_FAIL;
172 rval.consumed = 0;
173 }
174 } else {
175 rval.consumed = 0;
176 }
177 asn_DEF_REAL.free_struct(&asn_DEF_REAL, st, 0);
178 return rval;
179}
180
vlm39ba4c42004-09-22 16:06:28 +0000181asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000182NativeReal_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000183 int ilevel, enum xer_encoder_flags_e flags,
184 asn_app_consume_bytes_f *cb, void *app_key) {
185 const double *Dbl = (const double *)sptr;
186 asn_enc_rval_t er;
vlm39ba4c42004-09-22 16:06:28 +0000187
188 (void)ilevel;
189
190 if(!Dbl) _ASN_ENCODE_FAILED;
191
vlm6678cb12004-09-26 13:10:40 +0000192 er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key);
vlm39ba4c42004-09-22 16:06:28 +0000193 if(er.encoded < 0) _ASN_ENCODE_FAILED;
194
195 return er;
196}
197
vlm785435b2004-09-14 12:46:35 +0000198/*
199 * REAL specific human-readable output.
200 */
201int
vlmef6355b2004-09-29 13:26:15 +0000202NativeReal_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlm785435b2004-09-14 12:46:35 +0000203 asn_app_consume_bytes_f *cb, void *app_key) {
204 const double *Dbl = (const double *)sptr;
vlm785435b2004-09-14 12:46:35 +0000205
206 (void)td; /* Unused argument */
207 (void)ilevel; /* Unused argument */
208
vlm6678cb12004-09-26 13:10:40 +0000209 if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlm39ba4c42004-09-22 16:06:28 +0000210
211 return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0;
vlm785435b2004-09-14 12:46:35 +0000212}
213
214void
vlmef6355b2004-09-29 13:26:15 +0000215NativeReal_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
vlm785435b2004-09-14 12:46:35 +0000216
217 if(!td || !ptr)
218 return;
219
220 ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)",
221 td->name, contents_only, ptr);
222
223 if(!contents_only) {
224 FREEMEM(ptr);
225 }
226}
227