vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 1 | /*- |
vlm | 86380d3 | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2 | * Copyright (c) 2004, 2006 Lev Walkin <vlm@lionet.info>. All rights reserved. |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
| 5 | /* |
vlm | c03306f | 2006-03-18 05:06:57 +0000 | [diff] [blame] | 6 | * Read the NativeReal.h for the explanation wrt. differences between |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 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 | 86380d3 | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 15 | #include <OCTET_STRING.h> |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * NativeReal basic type description. |
| 19 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 20 | static ber_tlv_tag_t asn_DEF_NativeReal_tags[] = { |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 21 | (ASN_TAG_CLASS_UNIVERSAL | (9 << 2)) |
| 22 | }; |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 23 | asn_TYPE_descriptor_t asn_DEF_NativeReal = { |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 24 | "REAL", /* The ASN.1 type is still REAL */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 25 | "REAL", |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 26 | NativeReal_free, |
| 27 | NativeReal_print, |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 28 | asn_generic_no_constraint, |
| 29 | NativeReal_decode_ber, |
| 30 | NativeReal_encode_der, |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 31 | NativeReal_decode_xer, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 32 | NativeReal_encode_xer, |
vlm | 86380d3 | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 33 | NativeReal_decode_uper, |
| 34 | NativeReal_encode_uper, |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 35 | 0, /* Use generic outmost tag fetcher */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 36 | asn_DEF_NativeReal_tags, |
| 37 | sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]), |
| 38 | asn_DEF_NativeReal_tags, /* Same as above */ |
| 39 | sizeof(asn_DEF_NativeReal_tags) / sizeof(asn_DEF_NativeReal_tags[0]), |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 40 | 0, /* No PER visible constraints */ |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 41 | 0, 0, /* No members */ |
| 42 | 0 /* No specifics */ |
| 43 | }; |
| 44 | |
| 45 | /* |
| 46 | * Decode REAL type. |
| 47 | */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 48 | asn_dec_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 49 | NativeReal_decode_ber(asn_codec_ctx_t *opt_codec_ctx, |
| 50 | asn_TYPE_descriptor_t *td, |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 51 | void **dbl_ptr, const void *buf_ptr, size_t size, int tag_mode) { |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 52 | double *Dbl = (double *)*dbl_ptr; |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 53 | asn_dec_rval_t rval; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 54 | ber_tlv_len_t length; |
| 55 | |
| 56 | /* |
| 57 | * If the structure is not there, allocate it. |
| 58 | */ |
| 59 | if(Dbl == NULL) { |
vlm | a8f4a4c | 2005-01-17 14:32:45 +0000 | [diff] [blame] | 60 | *dbl_ptr = CALLOC(1, sizeof(*Dbl)); |
| 61 | Dbl = (double *)*dbl_ptr; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 62 | if(Dbl == NULL) { |
| 63 | rval.code = RC_FAIL; |
| 64 | rval.consumed = 0; |
| 65 | return rval; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | ASN_DEBUG("Decoding %s as REAL (tm=%d)", |
| 70 | td->name, tag_mode); |
| 71 | |
| 72 | /* |
| 73 | * Check tags. |
| 74 | */ |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 75 | rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size, |
| 76 | tag_mode, 0, &length, 0); |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 77 | if(rval.code != RC_OK) |
| 78 | return rval; |
| 79 | |
| 80 | ASN_DEBUG("%s length is %d bytes", td->name, (int)length); |
| 81 | |
| 82 | /* |
| 83 | * Make sure we have this length. |
| 84 | */ |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 85 | buf_ptr = ((const char *)buf_ptr) + rval.consumed; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 86 | size -= rval.consumed; |
| 87 | if(length > (ber_tlv_len_t)size) { |
| 88 | rval.code = RC_WMORE; |
| 89 | rval.consumed = 0; |
| 90 | return rval; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * ASN.1 encoded REAL: buf_ptr, length |
| 95 | * Fill the Dbl, at the same time checking for overflow. |
| 96 | * If overflow occured, return with RC_FAIL. |
| 97 | */ |
| 98 | { |
| 99 | REAL_t tmp; |
vlm | 41dee12 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 100 | union { |
| 101 | const void *constbuf; |
| 102 | void *nonconstbuf; |
| 103 | } unconst_buf; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 104 | double d; |
vlm | 41dee12 | 2005-07-02 08:19:17 +0000 | [diff] [blame] | 105 | |
| 106 | unconst_buf.constbuf = buf_ptr; |
| 107 | tmp.buf = (uint8_t *)unconst_buf.nonconstbuf; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 108 | tmp.size = length; |
| 109 | |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 110 | if(asn_REAL2double(&tmp, &d)) { |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 111 | rval.code = RC_FAIL; |
| 112 | rval.consumed = 0; |
| 113 | return rval; |
| 114 | } |
| 115 | |
| 116 | *Dbl = d; |
| 117 | } |
| 118 | |
| 119 | rval.code = RC_OK; |
| 120 | rval.consumed += length; |
| 121 | |
vlm | 13318c1 | 2004-10-26 10:56:10 +0000 | [diff] [blame] | 122 | ASN_DEBUG("Took %ld/%ld bytes to encode %s (%f)", |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 123 | (long)rval.consumed, (long)length, td->name, *Dbl); |
| 124 | |
| 125 | return rval; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Encode the NativeReal using the standard REAL type DER encoder. |
| 130 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 131 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 132 | NativeReal_encode_der(asn_TYPE_descriptor_t *td, void *ptr, |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 133 | int tag_mode, ber_tlv_tag_t tag, |
| 134 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 135 | double Dbl = *(const double *)ptr; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 136 | asn_enc_rval_t erval; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 137 | REAL_t tmp; |
| 138 | |
vlm | 83386ef | 2005-02-06 04:29:03 +0000 | [diff] [blame] | 139 | /* Prepare a temporary clean structure */ |
| 140 | memset(&tmp, 0, sizeof(tmp)); |
| 141 | |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 142 | if(asn_double2REAL(&tmp, Dbl)) { |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 143 | erval.encoded = -1; |
| 144 | erval.failed_type = td; |
| 145 | erval.structure_ptr = ptr; |
| 146 | return erval; |
| 147 | } |
| 148 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 149 | /* Encode a fake REAL */ |
| 150 | erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key); |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 151 | if(erval.encoded == -1) { |
| 152 | assert(erval.structure_ptr == &tmp); |
| 153 | erval.structure_ptr = ptr; |
| 154 | } |
vlm | 83386ef | 2005-02-06 04:29:03 +0000 | [diff] [blame] | 155 | |
| 156 | /* Free possibly allocated members of the temporary structure */ |
vlm | be78c80 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 157 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp); |
vlm | 83386ef | 2005-02-06 04:29:03 +0000 | [diff] [blame] | 158 | |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 159 | return erval; |
| 160 | } |
| 161 | |
vlm | 86380d3 | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 162 | /* |
| 163 | * Decode REAL type using PER. |
| 164 | */ |
| 165 | asn_dec_rval_t |
| 166 | NativeReal_decode_uper(asn_codec_ctx_t *opt_codec_ctx, |
| 167 | asn_TYPE_descriptor_t *td, asn_per_constraints_t *constraints, |
| 168 | void **dbl_ptr, asn_per_data_t *pd) { |
| 169 | double *Dbl = (double *)*dbl_ptr; |
| 170 | asn_dec_rval_t rval; |
| 171 | REAL_t tmp; |
| 172 | void *ptmp = &tmp; |
| 173 | int ret; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 174 | |
vlm | 86380d3 | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 175 | (void)constraints; |
| 176 | |
| 177 | /* |
| 178 | * If the structure is not there, allocate it. |
| 179 | */ |
| 180 | if(Dbl == NULL) { |
| 181 | *dbl_ptr = CALLOC(1, sizeof(*Dbl)); |
| 182 | Dbl = (double *)*dbl_ptr; |
| 183 | if(Dbl == NULL) |
| 184 | _ASN_DECODE_FAILED; |
| 185 | } |
| 186 | |
| 187 | memset(&tmp, 0, sizeof(tmp)); |
| 188 | rval = OCTET_STRING_decode_uper(opt_codec_ctx, td, NULL, |
| 189 | &ptmp, pd); |
| 190 | if(rval.code != RC_OK) { |
| 191 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp); |
| 192 | return rval; |
| 193 | } |
| 194 | |
| 195 | ret = asn_REAL2double(&tmp, Dbl); |
| 196 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp); |
| 197 | if(ret) _ASN_DECODE_FAILED; |
| 198 | |
| 199 | return rval; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Encode the NativeReal using the OCTET STRING PER encoder. |
| 204 | */ |
| 205 | asn_enc_rval_t |
| 206 | NativeReal_encode_uper(asn_TYPE_descriptor_t *td, |
| 207 | asn_per_constraints_t *constraints, void *sptr, asn_per_outp_t *po) { |
| 208 | double Dbl = *(const double *)sptr; |
| 209 | asn_enc_rval_t erval; |
| 210 | REAL_t tmp; |
| 211 | |
| 212 | (void)constraints; |
| 213 | |
| 214 | /* Prepare a temporary clean structure */ |
| 215 | memset(&tmp, 0, sizeof(tmp)); |
| 216 | |
| 217 | if(asn_double2REAL(&tmp, Dbl)) |
| 218 | _ASN_ENCODE_FAILED; |
| 219 | |
| 220 | /* Encode a DER REAL */ |
| 221 | erval = OCTET_STRING_encode_uper(td, NULL, &tmp, po); |
| 222 | if(erval.encoded == -1) |
| 223 | erval.structure_ptr = sptr; |
| 224 | |
| 225 | /* Free possibly allocated members of the temporary structure */ |
| 226 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp); |
| 227 | |
| 228 | return erval; |
| 229 | } |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 230 | |
| 231 | /* |
| 232 | * Decode the chunk of XML text encoding REAL. |
| 233 | */ |
| 234 | asn_dec_rval_t |
| 235 | NativeReal_decode_xer(asn_codec_ctx_t *opt_codec_ctx, |
| 236 | asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 237 | const void *buf_ptr, size_t size) { |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 238 | asn_dec_rval_t rval; |
| 239 | REAL_t *st = 0; |
vlm | af68ef5 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 240 | REAL_t **stp = &st; |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 241 | double *Dbl = (double *)*sptr; |
| 242 | |
| 243 | if(!Dbl) { |
vlm | a8f4a4c | 2005-01-17 14:32:45 +0000 | [diff] [blame] | 244 | *sptr = CALLOC(1, sizeof(double)); |
| 245 | Dbl = (double *)*sptr; |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 246 | if(!Dbl) { |
| 247 | rval.code = RC_FAIL; |
| 248 | rval.consumed = 0; |
| 249 | return rval; |
| 250 | } |
| 251 | } |
| 252 | |
vlm | af68ef5 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 253 | rval = REAL_decode_xer(opt_codec_ctx, td, (void **)stp, opt_mname, |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 254 | buf_ptr, size); |
| 255 | if(rval.code == RC_OK) { |
| 256 | if(asn_REAL2double(st, Dbl)) { |
| 257 | rval.code = RC_FAIL; |
| 258 | rval.consumed = 0; |
| 259 | } |
| 260 | } else { |
| 261 | rval.consumed = 0; |
| 262 | } |
vlm | be78c80 | 2006-03-17 02:11:12 +0000 | [diff] [blame] | 263 | ASN_STRUCT_FREE(asn_DEF_REAL, st); |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 264 | return rval; |
| 265 | } |
| 266 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 267 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 268 | NativeReal_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 269 | int ilevel, enum xer_encoder_flags_e flags, |
| 270 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 271 | const double *Dbl = (const double *)sptr; |
| 272 | asn_enc_rval_t er; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 273 | |
| 274 | (void)ilevel; |
| 275 | |
| 276 | if(!Dbl) _ASN_ENCODE_FAILED; |
| 277 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 278 | er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 279 | if(er.encoded < 0) _ASN_ENCODE_FAILED; |
| 280 | |
vlm | 337167e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 281 | _ASN_ENCODED_OK(er); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 282 | } |
| 283 | |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 284 | /* |
| 285 | * REAL specific human-readable output. |
| 286 | */ |
| 287 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 288 | NativeReal_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 289 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 290 | const double *Dbl = (const double *)sptr; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 291 | |
| 292 | (void)td; /* Unused argument */ |
| 293 | (void)ilevel; /* Unused argument */ |
| 294 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 295 | if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 296 | |
| 297 | return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | void |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 301 | NativeReal_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 302 | |
| 303 | if(!td || !ptr) |
| 304 | return; |
| 305 | |
| 306 | ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)", |
| 307 | td->name, contents_only, ptr); |
| 308 | |
| 309 | if(!contents_only) { |
| 310 | FREEMEM(ptr); |
| 311 | } |
| 312 | } |
| 313 | |