blob: 8fc9ca63b4e9cdba84bb8b4eec54ef8146e20bf6 [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 */
20static ber_tlv_tag_t asn1_DEF_NativeReal_tags[] = {
21 (ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
22};
23asn1_TYPE_descriptor_t asn1_DEF_NativeReal = {
24 "REAL", /* The ASN.1 type is still REAL */
vlm39ba4c42004-09-22 16:06:28 +000025 NativeReal_free,
26 NativeReal_print,
vlm785435b2004-09-14 12:46:35 +000027 asn_generic_no_constraint,
28 NativeReal_decode_ber,
29 NativeReal_encode_der,
vlm39ba4c42004-09-22 16:06:28 +000030 0, /* Not implemented yet */
31 NativeReal_encode_xer,
vlm785435b2004-09-14 12:46:35 +000032 0, /* Use generic outmost tag fetcher */
33 asn1_DEF_NativeReal_tags,
34 sizeof(asn1_DEF_NativeReal_tags) / sizeof(asn1_DEF_NativeReal_tags[0]),
35 asn1_DEF_NativeReal_tags, /* Same as above */
36 sizeof(asn1_DEF_NativeReal_tags) / sizeof(asn1_DEF_NativeReal_tags[0]),
vlm785435b2004-09-14 12:46:35 +000037 0, 0, /* No members */
38 0 /* No specifics */
39};
40
41/*
42 * Decode REAL type.
43 */
44ber_dec_rval_t
45NativeReal_decode_ber(asn1_TYPE_descriptor_t *td,
46 void **dbl_ptr, void *buf_ptr, size_t size, int tag_mode) {
47 double *Dbl = (double *)*dbl_ptr;
48 ber_dec_rval_t rval;
vlm785435b2004-09-14 12:46:35 +000049 ber_tlv_len_t length;
50
51 /*
52 * If the structure is not there, allocate it.
53 */
54 if(Dbl == NULL) {
55 (void *)Dbl = *dbl_ptr = CALLOC(1, sizeof(*Dbl));
56 if(Dbl == NULL) {
57 rval.code = RC_FAIL;
58 rval.consumed = 0;
59 return rval;
60 }
61 }
62
63 ASN_DEBUG("Decoding %s as REAL (tm=%d)",
64 td->name, tag_mode);
65
66 /*
67 * Check tags.
68 */
vlm6678cb12004-09-26 13:10:40 +000069 rval = ber_check_tags(td, 0, buf_ptr, size, tag_mode, 0, &length, 0);
vlm785435b2004-09-14 12:46:35 +000070 if(rval.code != RC_OK)
71 return rval;
72
73 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
74
75 /*
76 * Make sure we have this length.
77 */
78 buf_ptr = ((char *)buf_ptr) + rval.consumed;
79 size -= rval.consumed;
80 if(length > (ber_tlv_len_t)size) {
81 rval.code = RC_WMORE;
82 rval.consumed = 0;
83 return rval;
84 }
85
86 /*
87 * ASN.1 encoded REAL: buf_ptr, length
88 * Fill the Dbl, at the same time checking for overflow.
89 * If overflow occured, return with RC_FAIL.
90 */
91 {
92 REAL_t tmp;
93 double d;
94 tmp.buf = (uint8_t *)buf_ptr;
95 tmp.size = length;
96
97 if(asn1_REAL2double(&tmp, &d)) {
98 rval.code = RC_FAIL;
99 rval.consumed = 0;
100 return rval;
101 }
102
103 *Dbl = d;
104 }
105
106 rval.code = RC_OK;
107 rval.consumed += length;
108
109 ASN_DEBUG("Took %ld/%ld bytes to encode %s (%d)",
110 (long)rval.consumed, (long)length, td->name, *Dbl);
111
112 return rval;
113}
114
115/*
116 * Encode the NativeReal using the standard REAL type DER encoder.
117 */
vlm39ba4c42004-09-22 16:06:28 +0000118asn_enc_rval_t
vlm785435b2004-09-14 12:46:35 +0000119NativeReal_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
120 int tag_mode, ber_tlv_tag_t tag,
121 asn_app_consume_bytes_f *cb, void *app_key) {
122 double Dbl = *(const double *)ptr;
vlm39ba4c42004-09-22 16:06:28 +0000123 asn_enc_rval_t erval;
vlm785435b2004-09-14 12:46:35 +0000124 REAL_t tmp;
125
126 if(asn1_double2REAL(&tmp, Dbl)) {
127 erval.encoded = -1;
128 erval.failed_type = td;
129 erval.structure_ptr = ptr;
130 return erval;
131 }
132
vlm6678cb12004-09-26 13:10:40 +0000133 /* Encode a fake REAL */
134 erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key);
vlm785435b2004-09-14 12:46:35 +0000135 if(erval.encoded == -1) {
136 assert(erval.structure_ptr == &tmp);
137 erval.structure_ptr = ptr;
138 }
139 return erval;
140}
141
vlm39ba4c42004-09-22 16:06:28 +0000142
143asn_enc_rval_t
144NativeReal_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr,
145 int ilevel, enum xer_encoder_flags_e flags,
146 asn_app_consume_bytes_f *cb, void *app_key) {
147 const double *Dbl = (const double *)sptr;
148 asn_enc_rval_t er;
vlm39ba4c42004-09-22 16:06:28 +0000149
150 (void)ilevel;
151
152 if(!Dbl) _ASN_ENCODE_FAILED;
153
vlm6678cb12004-09-26 13:10:40 +0000154 er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key);
vlm39ba4c42004-09-22 16:06:28 +0000155 if(er.encoded < 0) _ASN_ENCODE_FAILED;
156
157 return er;
158}
159
vlm785435b2004-09-14 12:46:35 +0000160/*
161 * REAL specific human-readable output.
162 */
163int
164NativeReal_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
165 asn_app_consume_bytes_f *cb, void *app_key) {
166 const double *Dbl = (const double *)sptr;
vlm785435b2004-09-14 12:46:35 +0000167
168 (void)td; /* Unused argument */
169 (void)ilevel; /* Unused argument */
170
vlm6678cb12004-09-26 13:10:40 +0000171 if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlm39ba4c42004-09-22 16:06:28 +0000172
173 return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0;
vlm785435b2004-09-14 12:46:35 +0000174}
175
176void
177NativeReal_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) {
178
179 if(!td || !ptr)
180 return;
181
182 ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)",
183 td->name, contents_only, ptr);
184
185 if(!contents_only) {
186 FREEMEM(ptr);
187 }
188}
189