blob: 83b1676e1d0e8c8ef0a590b140cf27efaf72219e [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 */
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 */
vlmef6355b2004-09-29 13:26:15 +000033 asn_DEF_NativeReal_tags,
34 sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]),
35 asn_DEF_NativeReal_tags, /* Same as above */
36 sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_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
vlmef6355b2004-09-29 13:26:15 +000045NativeReal_decode_ber(asn_codec_ctx_t *opt_codec_ctx,
46 asn_TYPE_descriptor_t *td,
vlm785435b2004-09-14 12:46:35 +000047 void **dbl_ptr, void *buf_ptr, size_t size, int tag_mode) {
48 double *Dbl = (double *)*dbl_ptr;
49 ber_dec_rval_t rval;
vlm785435b2004-09-14 12:46:35 +000050 ber_tlv_len_t length;
51
52 /*
53 * If the structure is not there, allocate it.
54 */
55 if(Dbl == NULL) {
56 (void *)Dbl = *dbl_ptr = CALLOC(1, sizeof(*Dbl));
57 if(Dbl == NULL) {
58 rval.code = RC_FAIL;
59 rval.consumed = 0;
60 return rval;
61 }
62 }
63
64 ASN_DEBUG("Decoding %s as REAL (tm=%d)",
65 td->name, tag_mode);
66
67 /*
68 * Check tags.
69 */
vlmef6355b2004-09-29 13:26:15 +000070 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size,
71 tag_mode, 0, &length, 0);
vlm785435b2004-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
vlmef6355b2004-09-29 13:26:15 +000099 if(asn_REAL2double(&tmp, &d)) {
vlm785435b2004-09-14 12:46:35 +0000100 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 */
vlm39ba4c42004-09-22 16:06:28 +0000120asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000121NativeReal_encode_der(asn_TYPE_descriptor_t *td, void *ptr,
vlm785435b2004-09-14 12:46:35 +0000122 int tag_mode, ber_tlv_tag_t tag,
123 asn_app_consume_bytes_f *cb, void *app_key) {
124 double Dbl = *(const double *)ptr;
vlm39ba4c42004-09-22 16:06:28 +0000125 asn_enc_rval_t erval;
vlm785435b2004-09-14 12:46:35 +0000126 REAL_t tmp;
127
vlmef6355b2004-09-29 13:26:15 +0000128 if(asn_double2REAL(&tmp, Dbl)) {
vlm785435b2004-09-14 12:46:35 +0000129 erval.encoded = -1;
130 erval.failed_type = td;
131 erval.structure_ptr = ptr;
132 return erval;
133 }
134
vlm6678cb12004-09-26 13:10:40 +0000135 /* Encode a fake REAL */
136 erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key);
vlm785435b2004-09-14 12:46:35 +0000137 if(erval.encoded == -1) {
138 assert(erval.structure_ptr == &tmp);
139 erval.structure_ptr = ptr;
140 }
141 return erval;
142}
143
vlm39ba4c42004-09-22 16:06:28 +0000144
145asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000146NativeReal_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000147 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;
vlm39ba4c42004-09-22 16:06:28 +0000151
152 (void)ilevel;
153
154 if(!Dbl) _ASN_ENCODE_FAILED;
155
vlm6678cb12004-09-26 13:10:40 +0000156 er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key);
vlm39ba4c42004-09-22 16:06:28 +0000157 if(er.encoded < 0) _ASN_ENCODE_FAILED;
158
159 return er;
160}
161
vlm785435b2004-09-14 12:46:35 +0000162/*
163 * REAL specific human-readable output.
164 */
165int
vlmef6355b2004-09-29 13:26:15 +0000166NativeReal_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlm785435b2004-09-14 12:46:35 +0000167 asn_app_consume_bytes_f *cb, void *app_key) {
168 const double *Dbl = (const double *)sptr;
vlm785435b2004-09-14 12:46:35 +0000169
170 (void)td; /* Unused argument */
171 (void)ilevel; /* Unused argument */
172
vlm6678cb12004-09-26 13:10:40 +0000173 if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlm39ba4c42004-09-22 16:06:28 +0000174
175 return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0;
vlm785435b2004-09-14 12:46:35 +0000176}
177
178void
vlmef6355b2004-09-29 13:26:15 +0000179NativeReal_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) {
vlm785435b2004-09-14 12:46:35 +0000180
181 if(!td || !ptr)
182 return;
183
184 ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)",
185 td->name, contents_only, ptr);
186
187 if(!contents_only) {
188 FREEMEM(ptr);
189 }
190}
191