blob: 1151087fab497bf796f6d058dde1270b859db5ba [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;
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) {
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 */
Lev Walkin5ccf1eb2004-09-24 20:58:47 +000071 rval = ber_check_tags(td, 0, buf_ptr, size, tag_mode, &length, 0);
Lev Walkin41ba1f22004-09-14 12:46:35 +000072 if(rval.code != RC_OK)
73 return rval;
74
75 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
76
77 /*
78 * Make sure we have this length.
79 */
80 buf_ptr = ((char *)buf_ptr) + rval.consumed;
81 size -= rval.consumed;
82 if(length > (ber_tlv_len_t)size) {
83 rval.code = RC_WMORE;
84 rval.consumed = 0;
85 return rval;
86 }
87
88 /*
89 * ASN.1 encoded REAL: buf_ptr, length
90 * Fill the Dbl, at the same time checking for overflow.
91 * If overflow occured, return with RC_FAIL.
92 */
93 {
94 REAL_t tmp;
95 double d;
96 tmp.buf = (uint8_t *)buf_ptr;
97 tmp.size = length;
98
99 if(asn1_REAL2double(&tmp, &d)) {
100 rval.code = RC_FAIL;
101 rval.consumed = 0;
102 return rval;
103 }
104
105 *Dbl = d;
106 }
107
108 rval.code = RC_OK;
109 rval.consumed += length;
110
111 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%d)",
112 (long)rval.consumed, (long)length, td->name, *Dbl);
113
114 return rval;
115}
116
117/*
118 * Encode the NativeReal using the standard REAL type DER encoder.
119 */
Lev Walkina9cc46e2004-09-22 16:06:28 +0000120asn_enc_rval_t
Lev Walkin41ba1f22004-09-14 12:46:35 +0000121NativeReal_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
122 int tag_mode, ber_tlv_tag_t tag,
123 asn_app_consume_bytes_f *cb, void *app_key) {
124 double Dbl = *(const double *)ptr;
Lev Walkina9cc46e2004-09-22 16:06:28 +0000125 asn_enc_rval_t erval;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000126 REAL_t tmp;
127
128 if(asn1_double2REAL(&tmp, Dbl)) {
129 erval.encoded = -1;
130 erval.failed_type = td;
131 erval.structure_ptr = ptr;
132 return erval;
133 }
134
135 /* Encode fake REAL */
136 erval = INTEGER_encode_der(td, &tmp, tag_mode, tag, cb, app_key);
137 if(erval.encoded == -1) {
138 assert(erval.structure_ptr == &tmp);
139 erval.structure_ptr = ptr;
140 }
141 return erval;
142}
143
Lev Walkina9cc46e2004-09-22 16:06:28 +0000144
145asn_enc_rval_t
146NativeReal_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
147 int ilevel, enum xer_encoder_flags_e flags,
148 asn_app_consume_bytes_f *cb, void *app_key) {
149 const double *Dbl = (const double *)sptr;
150 asn_enc_rval_t er;
151 double d;
152
153 (void)ilevel;
154
155 if(!Dbl) _ASN_ENCODE_FAILED;
156
157 er.encoded = REAL__dump(d, flags & XER_F_CANONICAL, cb, app_key);
158 if(er.encoded < 0) _ASN_ENCODE_FAILED;
159
160 return er;
161}
162
Lev Walkin41ba1f22004-09-14 12:46:35 +0000163/*
164 * REAL specific human-readable output.
165 */
166int
167NativeReal_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
168 asn_app_consume_bytes_f *cb, void *app_key) {
169 const double *Dbl = (const double *)sptr;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000170
171 (void)td; /* Unused argument */
172 (void)ilevel; /* Unused argument */
173
Lev Walkina9cc46e2004-09-22 16:06:28 +0000174 if(!Dbl) return cb("<absent>", 8, app_key);
175
176 return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0;
Lev Walkin41ba1f22004-09-14 12:46:35 +0000177}
178
179void
180NativeReal_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) {
181
182 if(!td || !ptr)
183 return;
184
185 ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)",
186 td->name, contents_only, ptr);
187
188 if(!contents_only) {
189 FREEMEM(ptr);
190 }
191}
192