blob: 1566a7fb31e989f4910b32736fc6d3c734b798de [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>
14#include <INTEGER.h>
15#include <REAL.h>
16#include <assert.h>
17
18/*
19 * NativeReal basic type description.
20 */
21static ber_tlv_tag_t asn1_DEF_NativeReal_tags[] = {
22 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
23};
24asn1_TYPE_descriptor_t asn1_DEF_NativeReal = {
25 "REAL", /* The ASN.1 type is still 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 Walkina9cc46e2004-09-22 16:06:28 +000031 0, /* Not implemented yet */
32 NativeReal_encode_xer,
Lev Walkin41ba1f22004-09-14 12:46:35 +000033 0, /* Use generic outmost tag fetcher */
34 asn1_DEF_NativeReal_tags,
35 sizeof(asn1_DEF_NativeReal_tags) / sizeof(asn1_DEF_NativeReal_tags[0]),
36 asn1_DEF_NativeReal_tags, /* Same as above */
37 sizeof(asn1_DEF_NativeReal_tags) / sizeof(asn1_DEF_NativeReal_tags[0]),
38 0, /* Always in primitive form */
39 0, 0, /* No members */
40 0 /* No specifics */
41};
42
43/*
44 * Decode REAL type.
45 */
46ber_dec_rval_t
47NativeReal_decode_ber(asn1_TYPE_descriptor_t *td,
48 void **dbl_ptr, void *buf_ptr, size_t size, int tag_mode) {
49 double *Dbl = (double *)*dbl_ptr;
50 ber_dec_rval_t rval;
51 ber_dec_ctx_t ctx = { 0, 0, 0, 0 };
52 ber_tlv_len_t length;
53
54 /*
55 * If the structure is not there, allocate it.
56 */
57 if(Dbl == NULL) {
58 (void *)Dbl = *dbl_ptr = CALLOC(1, sizeof(*Dbl));
59 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 */
72 rval = ber_check_tags(td, &ctx,
73 buf_ptr, size, tag_mode, &length, 0);
74 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
101 if(asn1_REAL2double(&tmp, &d)) {
102 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
113 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%d)",
114 (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 Walkin41ba1f22004-09-14 12:46:35 +0000123NativeReal_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
124 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
130 if(asn1_double2REAL(&tmp, Dbl)) {
131 erval.encoded = -1;
132 erval.failed_type = td;
133 erval.structure_ptr = ptr;
134 return erval;
135 }
136
137 /* Encode fake REAL */
138 erval = INTEGER_encode_der(td, &tmp, tag_mode, tag, cb, app_key);
139 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
147asn_enc_rval_t
148NativeReal_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
149 int ilevel, enum xer_encoder_flags_e flags,
150 asn_app_consume_bytes_f *cb, void *app_key) {
151 const double *Dbl = (const double *)sptr;
152 asn_enc_rval_t er;
153 double d;
154
155 (void)ilevel;
156
157 if(!Dbl) _ASN_ENCODE_FAILED;
158
159 er.encoded = REAL__dump(d, flags & XER_F_CANONICAL, cb, app_key);
160 if(er.encoded < 0) _ASN_ENCODE_FAILED;
161
162 return er;
163}
164
Lev Walkin41ba1f22004-09-14 12:46:35 +0000165/*
166 * REAL specific human-readable output.
167 */
168int
169NativeReal_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
170 asn_app_consume_bytes_f *cb, void *app_key) {
171 const double *Dbl = (const double *)sptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000172
173 (void)td; /* Unused argument */
174 (void)ilevel; /* Unused argument */
175
Lev Walkina9cc46e2004-09-22 16:06:28 +0000176 if(!Dbl) return cb("<absent>", 8, app_key);
177
178 return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000179}
180
181void
182NativeReal_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) {
183
184 if(!td || !ptr)
185 return;
186
187 ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)",
188 td->name, contents_only, ptr);
189
190 if(!contents_only) {
191 FREEMEM(ptr);
192 }
193}
194