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> |
| 15 | #include <assert.h> |
| 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 | 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 | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 38 | 0, 0, /* No members */ |
| 39 | 0 /* No specifics */ |
| 40 | }; |
| 41 | |
| 42 | /* |
| 43 | * Decode REAL type. |
| 44 | */ |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 45 | asn_dec_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 46 | NativeReal_decode_ber(asn_codec_ctx_t *opt_codec_ctx, |
| 47 | asn_TYPE_descriptor_t *td, |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 48 | void **dbl_ptr, void *buf_ptr, size_t size, int tag_mode) { |
| 49 | double *Dbl = (double *)*dbl_ptr; |
vlm | 9de248e | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 50 | asn_dec_rval_t rval; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 51 | ber_tlv_len_t length; |
| 52 | |
| 53 | /* |
| 54 | * If the structure is not there, allocate it. |
| 55 | */ |
| 56 | if(Dbl == NULL) { |
| 57 | (void *)Dbl = *dbl_ptr = CALLOC(1, sizeof(*Dbl)); |
| 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 | */ |
| 81 | buf_ptr = ((char *)buf_ptr) + rval.consumed; |
| 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; |
| 97 | tmp.buf = (uint8_t *)buf_ptr; |
| 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 | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 129 | if(asn_double2REAL(&tmp, Dbl)) { |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 130 | erval.encoded = -1; |
| 131 | erval.failed_type = td; |
| 132 | erval.structure_ptr = ptr; |
| 133 | return erval; |
| 134 | } |
| 135 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 136 | /* Encode a fake REAL */ |
| 137 | erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key); |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 138 | if(erval.encoded == -1) { |
| 139 | assert(erval.structure_ptr == &tmp); |
| 140 | erval.structure_ptr = ptr; |
| 141 | } |
| 142 | return erval; |
| 143 | } |
| 144 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 145 | |
vlm | b848b2a | 2004-10-21 14:02:19 +0000 | [diff] [blame] | 146 | |
| 147 | /* |
| 148 | * Decode the chunk of XML text encoding REAL. |
| 149 | */ |
| 150 | asn_dec_rval_t |
| 151 | NativeReal_decode_xer(asn_codec_ctx_t *opt_codec_ctx, |
| 152 | asn_TYPE_descriptor_t *td, void **sptr, const char *opt_mname, |
| 153 | void *buf_ptr, size_t size) { |
| 154 | asn_dec_rval_t rval; |
| 155 | REAL_t *st = 0; |
| 156 | double *Dbl = (double *)*sptr; |
| 157 | |
| 158 | if(!Dbl) { |
| 159 | (void *)Dbl = *sptr = CALLOC(1, sizeof(double)); |
| 160 | if(!Dbl) { |
| 161 | rval.code = RC_FAIL; |
| 162 | rval.consumed = 0; |
| 163 | return rval; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | rval = REAL_decode_xer(opt_codec_ctx, td, (void **)&st, opt_mname, |
| 168 | buf_ptr, size); |
| 169 | if(rval.code == RC_OK) { |
| 170 | if(asn_REAL2double(st, Dbl)) { |
| 171 | rval.code = RC_FAIL; |
| 172 | rval.consumed = 0; |
| 173 | } |
| 174 | } else { |
| 175 | rval.consumed = 0; |
| 176 | } |
| 177 | asn_DEF_REAL.free_struct(&asn_DEF_REAL, st, 0); |
| 178 | return rval; |
| 179 | } |
| 180 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 181 | asn_enc_rval_t |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 182 | NativeReal_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 183 | int ilevel, enum xer_encoder_flags_e flags, |
| 184 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 185 | const double *Dbl = (const double *)sptr; |
| 186 | asn_enc_rval_t er; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 187 | |
| 188 | (void)ilevel; |
| 189 | |
| 190 | if(!Dbl) _ASN_ENCODE_FAILED; |
| 191 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 192 | er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 193 | if(er.encoded < 0) _ASN_ENCODE_FAILED; |
| 194 | |
| 195 | return er; |
| 196 | } |
| 197 | |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 198 | /* |
| 199 | * REAL specific human-readable output. |
| 200 | */ |
| 201 | int |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 202 | NativeReal_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 203 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 204 | const double *Dbl = (const double *)sptr; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 205 | |
| 206 | (void)td; /* Unused argument */ |
| 207 | (void)ilevel; /* Unused argument */ |
| 208 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 209 | if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 210 | |
| 211 | return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void |
vlm | ef6355b | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 215 | NativeReal_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 216 | |
| 217 | if(!td || !ptr) |
| 218 | return; |
| 219 | |
| 220 | ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)", |
| 221 | td->name, contents_only, ptr); |
| 222 | |
| 223 | if(!contents_only) { |
| 224 | FREEMEM(ptr); |
| 225 | } |
| 226 | } |
| 227 | |