Lev Walkin | 41ba1f2 | 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 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 12 | #include <asn_internal.h> |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 13 | #include <NativeReal.h> |
Lev Walkin | 41ba1f2 | 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 | */ |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 20 | static ber_tlv_tag_t asn_DEF_NativeReal_tags[] = { |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 21 | (ASN_TAG_CLASS_UNIVERSAL | (9 << 2)) |
| 22 | }; |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 23 | asn_TYPE_descriptor_t asn_DEF_NativeReal = { |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 24 | "REAL", /* The ASN.1 type is still REAL */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 25 | "REAL", |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 26 | NativeReal_free, |
| 27 | NativeReal_print, |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 28 | asn_generic_no_constraint, |
| 29 | NativeReal_decode_ber, |
| 30 | NativeReal_encode_der, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 31 | 0, /* Not implemented yet */ |
| 32 | NativeReal_encode_xer, |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 33 | 0, /* Use generic outmost tag fetcher */ |
Lev Walkin | 5e03376 | 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]), |
Lev Walkin | 41ba1f2 | 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 | */ |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 45 | asn_dec_rval_t |
Lev Walkin | 5e03376 | 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, |
Lev Walkin | 41ba1f2 | 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; |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 50 | asn_dec_rval_t rval; |
Lev Walkin | 41ba1f2 | 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 | */ |
Lev Walkin | 5e03376 | 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); |
Lev Walkin | 41ba1f2 | 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 | |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 100 | if(asn_REAL2double(&tmp, &d)) { |
Lev Walkin | 41ba1f2 | 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 | |
| 112 | ASN_DEBUG("Took %ld/%ld bytes to encode %s (%d)", |
| 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 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 121 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 122 | NativeReal_encode_der(asn_TYPE_descriptor_t *td, void *ptr, |
Lev Walkin | 41ba1f2 | 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; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 126 | asn_enc_rval_t erval; |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 127 | REAL_t tmp; |
| 128 | |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 129 | if(asn_double2REAL(&tmp, Dbl)) { |
Lev Walkin | 41ba1f2 | 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 | |
Lev Walkin | 8e8078a | 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); |
Lev Walkin | 41ba1f2 | 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 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 145 | |
| 146 | asn_enc_rval_t |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 147 | NativeReal_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 148 | int ilevel, enum xer_encoder_flags_e flags, |
| 149 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 150 | const double *Dbl = (const double *)sptr; |
| 151 | asn_enc_rval_t er; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 152 | |
| 153 | (void)ilevel; |
| 154 | |
| 155 | if(!Dbl) _ASN_ENCODE_FAILED; |
| 156 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 157 | er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 158 | if(er.encoded < 0) _ASN_ENCODE_FAILED; |
| 159 | |
| 160 | return er; |
| 161 | } |
| 162 | |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 163 | /* |
| 164 | * REAL specific human-readable output. |
| 165 | */ |
| 166 | int |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 167 | NativeReal_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 168 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 169 | const double *Dbl = (const double *)sptr; |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 170 | |
| 171 | (void)td; /* Unused argument */ |
| 172 | (void)ilevel; /* Unused argument */ |
| 173 | |
Lev Walkin | 8e8078a | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 174 | if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 175 | |
| 176 | return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0; |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | void |
Lev Walkin | 5e03376 | 2004-09-29 13:26:15 +0000 | [diff] [blame] | 180 | NativeReal_free(asn_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
Lev Walkin | 41ba1f2 | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 181 | |
| 182 | if(!td || !ptr) |
| 183 | return; |
| 184 | |
| 185 | ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)", |
| 186 | td->name, contents_only, ptr); |
| 187 | |
| 188 | if(!contents_only) { |
| 189 | FREEMEM(ptr); |
| 190 | } |
| 191 | } |
| 192 | |