vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 1 | /*- |
| 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 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 12 | #include <asn_internal.h> |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 13 | #include <NativeReal.h> |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 14 | #include <REAL.h> |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 15 | |
| 16 | /* |
| 17 | * NativeReal basic type description. |
| 18 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 19 | static ber_tlv_tag_t asn_DEF_NativeReal_tags[] = { |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 20 | (ASN_TAG_CLASS_UNIVERSAL | (9 << 2)) |
| 21 | }; |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 22 | asn_TYPE_descriptor_t asn_DEF_NativeReal = { |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 23 | "REAL", /* The ASN.1 type is still REAL */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 24 | "REAL", |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 25 | NativeReal_free, |
| 26 | NativeReal_print, |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 27 | asn_generic_no_constraint, |
| 28 | NativeReal_decode_ber, |
| 29 | NativeReal_encode_der, |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 30 | NativeReal_decode_xer, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 31 | NativeReal_encode_xer, |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 32 | 0, |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 33 | 0, /* Use generic outmost tag fetcher */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 34 | 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]), |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 38 | 0, /* No PER visible constraints */ |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 39 | 0, 0, /* No members */ |
| 40 | 0 /* No specifics */ |
| 41 | }; |
| 42 | |
| 43 | /* |
| 44 | * Decode REAL type. |
| 45 | */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 46 | asn_dec_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 47 | NativeReal_decode_ber(asn_codec_ctx_t *opt_codec_ctx, |
| 48 | asn_TYPE_descriptor_t *td, |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 49 | void **dbl_ptr, const void *buf_ptr, size_t size, int tag_mode) { |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 50 | double *Dbl = (double *)*dbl_ptr; |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 51 | asn_dec_rval_t rval; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 52 | ber_tlv_len_t length; |
| 53 | |
| 54 | /* |
| 55 | * If the structure is not there, allocate it. |
| 56 | */ |
| 57 | if(Dbl == NULL) { |
vlm | a8f4a4c | 2005-01-17 14:32:45 +0000 | [diff] [blame] | 58 | *dbl_ptr = CALLOC(1, sizeof(*Dbl)); |
| 59 | Dbl = (double *)*dbl_ptr; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 60 | if(Dbl == NULL) { |
| 61 | rval.code = RC_FAIL; |
| 62 | rval.consumed = 0; |
| 63 | return rval; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | ASN_DEBUG("Decoding %s as REAL (tm=%d)", |
| 68 | td->name, tag_mode); |
| 69 | |
| 70 | /* |
| 71 | * Check tags. |
| 72 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 73 | rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size, |
| 74 | tag_mode, 0, &length, 0); |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 75 | if(rval.code != RC_OK) |
| 76 | return rval; |
| 77 | |
| 78 | ASN_DEBUG("%s length is %d bytes", td->name, (int)length); |
| 79 | |
| 80 | /* |
| 81 | * Make sure we have this length. |
| 82 | */ |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 83 | buf_ptr = ((const char *)buf_ptr) + rval.consumed; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 84 | size -= rval.consumed; |
| 85 | if(length > (ber_tlv_len_t)size) { |
| 86 | rval.code = RC_WMORE; |
| 87 | rval.consumed = 0; |
| 88 | return rval; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * ASN.1 encoded REAL: buf_ptr, length |
| 93 | * Fill the Dbl, at the same time checking for overflow. |
| 94 | * If overflow occured, return with RC_FAIL. |
| 95 | */ |
| 96 | { |
| 97 | REAL_t tmp; |
vlm | 41dee12 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 98 | union { |
| 99 | const void *constbuf; |
| 100 | void *nonconstbuf; |
| 101 | } unconst_buf; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 102 | double d; |
vlm | 41dee12 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 103 | |
| 104 | unconst_buf.constbuf = buf_ptr; |
| 105 | tmp.buf = (uint8_t *)unconst_buf.nonconstbuf; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 106 | tmp.size = length; |
| 107 | |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 108 | if(asn_REAL2double(&tmp, &d)) { |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 109 | rval.code = RC_FAIL; |
| 110 | rval.consumed = 0; |
| 111 | return rval; |
| 112 | } |
| 113 | |
| 114 | *Dbl = d; |
| 115 | } |
| 116 | |
| 117 | rval.code = RC_OK; |
| 118 | rval.consumed += length; |
| 119 | |
vlm | 13318c1 | 2004-10-26 10:56:10 +0000 | [diff] [blame] | 120 | ASN_DEBUG("Took %ld/%ld bytes to encode %s (%f)", |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 121 | (long)rval.consumed, (long)length, td->name, *Dbl); |
| 122 | |
| 123 | return rval; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Encode the NativeReal using the standard REAL type DER encoder. |
| 128 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 129 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 130 | NativeReal_encode_der(asn_TYPE_descriptor_t *td, void *ptr, |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 131 | int tag_mode, ber_tlv_tag_t tag, |
| 132 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 133 | double Dbl = *(const double *)ptr; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 134 | asn_enc_rval_t erval; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 135 | REAL_t tmp; |
| 136 | |
vlm | 83386ef | 2005-02-06 04:29:03 +0000 | [diff] [blame] | 137 | /* Prepare a temporary clean structure */ |
| 138 | memset(&tmp, 0, sizeof(tmp)); |
| 139 | |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 140 | if(asn_double2REAL(&tmp, Dbl)) { |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 141 | erval.encoded = -1; |
| 142 | erval.failed_type = td; |
| 143 | erval.structure_ptr = ptr; |
| 144 | return erval; |
| 145 | } |
| 146 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 147 | /* Encode a fake REAL */ |
| 148 | erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key); |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 149 | if(erval.encoded == -1) { |
| 150 | assert(erval.structure_ptr == &tmp); |
| 151 | erval.structure_ptr = ptr; |
| 152 | } |
vlm | 83386ef | 2005-02-06 04:29:03 +0000 | [diff] [blame] | 153 | |
| 154 | /* Free possibly allocated members of the temporary structure */ |
| 155 | asn_DEF_REAL.free_struct(&asn_DEF_REAL, &tmp, 1); |
| 156 | |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 157 | return erval; |
| 158 | } |
| 159 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 160 | |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 161 | |
| 162 | /* |
| 163 | * Decode the chunk of XML text encoding REAL. |
| 164 | */ |
| 165 | asn_dec_rval_t |
| 166 | NativeReal_decode_xer(asn_codec_ctx_t *opt_codec_ctx, |
| 167 | asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 168 | const void *buf_ptr, size_t size) { |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 169 | asn_dec_rval_t rval; |
| 170 | REAL_t *st = 0; |
| 171 | double *Dbl = (double *)*sptr; |
| 172 | |
| 173 | if(!Dbl) { |
vlm | a8f4a4c | 2005-01-17 14:32:45 +0000 | [diff] [blame] | 174 | *sptr = CALLOC(1, sizeof(double)); |
| 175 | Dbl = (double *)*sptr; |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 176 | if(!Dbl) { |
| 177 | rval.code = RC_FAIL; |
| 178 | rval.consumed = 0; |
| 179 | return rval; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | rval = REAL_decode_xer(opt_codec_ctx, td, (void **)&st, opt_mname, |
| 184 | buf_ptr, size); |
| 185 | if(rval.code == RC_OK) { |
| 186 | if(asn_REAL2double(st, Dbl)) { |
| 187 | rval.code = RC_FAIL; |
| 188 | rval.consumed = 0; |
| 189 | } |
| 190 | } else { |
| 191 | rval.consumed = 0; |
| 192 | } |
| 193 | asn_DEF_REAL.free_struct(&asn_DEF_REAL, st, 0); |
| 194 | return rval; |
| 195 | } |
| 196 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 197 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 198 | NativeReal_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 199 | int ilevel, enum xer_encoder_flags_e flags, |
| 200 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 201 | const double *Dbl = (const double *)sptr; |
| 202 | asn_enc_rval_t er; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 203 | |
| 204 | (void)ilevel; |
| 205 | |
| 206 | if(!Dbl) _ASN_ENCODE_FAILED; |
| 207 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 208 | er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 209 | if(er.encoded < 0) _ASN_ENCODE_FAILED; |
| 210 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 211 | _ASN_ENCODED_OK(er); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 212 | } |
| 213 | |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 214 | /* |
| 215 | * REAL specific human-readable output. |
| 216 | */ |
| 217 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 218 | NativeReal_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 219 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 220 | const double *Dbl = (const double *)sptr; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 221 | |
| 222 | (void)td; /* Unused argument */ |
| 223 | (void)ilevel; /* Unused argument */ |
| 224 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 225 | if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 226 | |
| 227 | return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 231 | NativeReal_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 232 | |
| 233 | if(!td || !ptr) |
| 234 | return; |
| 235 | |
| 236 | ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)", |
| 237 | td->name, contents_only, ptr); |
| 238 | |
| 239 | if(!contents_only) { |
| 240 | FREEMEM(ptr); |
| 241 | } |
| 242 | } |
| 243 | |