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