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 | */ |
| 20 | static ber_tlv_tag_t asn1_DEF_NativeReal_tags[] = { |
| 21 | (ASN_TAG_CLASS_UNIVERSAL | (9 << 2)) |
| 22 | }; |
| 23 | asn1_TYPE_descriptor_t asn1_DEF_NativeReal = { |
| 24 | "REAL", /* The ASN.1 type is still 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 | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 30 | 0, /* Not implemented yet */ |
| 31 | NativeReal_encode_xer, |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 32 | 0, /* Use generic outmost tag fetcher */ |
| 33 | asn1_DEF_NativeReal_tags, |
| 34 | sizeof(asn1_DEF_NativeReal_tags) / sizeof(asn1_DEF_NativeReal_tags[0]), |
| 35 | asn1_DEF_NativeReal_tags, /* Same as above */ |
| 36 | sizeof(asn1_DEF_NativeReal_tags) / sizeof(asn1_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 | */ |
| 44 | ber_dec_rval_t |
| 45 | NativeReal_decode_ber(asn1_TYPE_descriptor_t *td, |
| 46 | void **dbl_ptr, void *buf_ptr, size_t size, int tag_mode) { |
| 47 | double *Dbl = (double *)*dbl_ptr; |
| 48 | ber_dec_rval_t rval; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 49 | ber_tlv_len_t length; |
| 50 | |
| 51 | /* |
| 52 | * If the structure is not there, allocate it. |
| 53 | */ |
| 54 | if(Dbl == NULL) { |
| 55 | (void *)Dbl = *dbl_ptr = CALLOC(1, sizeof(*Dbl)); |
| 56 | if(Dbl == NULL) { |
| 57 | rval.code = RC_FAIL; |
| 58 | rval.consumed = 0; |
| 59 | return rval; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | ASN_DEBUG("Decoding %s as REAL (tm=%d)", |
| 64 | td->name, tag_mode); |
| 65 | |
| 66 | /* |
| 67 | * Check tags. |
| 68 | */ |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 69 | rval = ber_check_tags(td, 0, buf_ptr, size, tag_mode, 0, &length, 0); |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 70 | if(rval.code != RC_OK) |
| 71 | return rval; |
| 72 | |
| 73 | ASN_DEBUG("%s length is %d bytes", td->name, (int)length); |
| 74 | |
| 75 | /* |
| 76 | * Make sure we have this length. |
| 77 | */ |
| 78 | buf_ptr = ((char *)buf_ptr) + rval.consumed; |
| 79 | size -= rval.consumed; |
| 80 | if(length > (ber_tlv_len_t)size) { |
| 81 | rval.code = RC_WMORE; |
| 82 | rval.consumed = 0; |
| 83 | return rval; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * ASN.1 encoded REAL: buf_ptr, length |
| 88 | * Fill the Dbl, at the same time checking for overflow. |
| 89 | * If overflow occured, return with RC_FAIL. |
| 90 | */ |
| 91 | { |
| 92 | REAL_t tmp; |
| 93 | double d; |
| 94 | tmp.buf = (uint8_t *)buf_ptr; |
| 95 | tmp.size = length; |
| 96 | |
| 97 | if(asn1_REAL2double(&tmp, &d)) { |
| 98 | rval.code = RC_FAIL; |
| 99 | rval.consumed = 0; |
| 100 | return rval; |
| 101 | } |
| 102 | |
| 103 | *Dbl = d; |
| 104 | } |
| 105 | |
| 106 | rval.code = RC_OK; |
| 107 | rval.consumed += length; |
| 108 | |
| 109 | ASN_DEBUG("Took %ld/%ld bytes to encode %s (%d)", |
| 110 | (long)rval.consumed, (long)length, td->name, *Dbl); |
| 111 | |
| 112 | return rval; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Encode the NativeReal using the standard REAL type DER encoder. |
| 117 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 118 | asn_enc_rval_t |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 119 | NativeReal_encode_der(asn1_TYPE_descriptor_t *td, void *ptr, |
| 120 | int tag_mode, ber_tlv_tag_t tag, |
| 121 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 122 | double Dbl = *(const double *)ptr; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 123 | asn_enc_rval_t erval; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 124 | REAL_t tmp; |
| 125 | |
| 126 | if(asn1_double2REAL(&tmp, Dbl)) { |
| 127 | erval.encoded = -1; |
| 128 | erval.failed_type = td; |
| 129 | erval.structure_ptr = ptr; |
| 130 | return erval; |
| 131 | } |
| 132 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 133 | /* Encode a fake REAL */ |
| 134 | erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key); |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 135 | if(erval.encoded == -1) { |
| 136 | assert(erval.structure_ptr == &tmp); |
| 137 | erval.structure_ptr = ptr; |
| 138 | } |
| 139 | return erval; |
| 140 | } |
| 141 | |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 142 | |
| 143 | asn_enc_rval_t |
| 144 | NativeReal_encode_xer(asn1_TYPE_descriptor_t *td, void *sptr, |
| 145 | int ilevel, enum xer_encoder_flags_e flags, |
| 146 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 147 | const double *Dbl = (const double *)sptr; |
| 148 | asn_enc_rval_t er; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 149 | |
| 150 | (void)ilevel; |
| 151 | |
| 152 | if(!Dbl) _ASN_ENCODE_FAILED; |
| 153 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 154 | er.encoded = REAL__dump(*Dbl, flags & XER_F_CANONICAL, cb, app_key); |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 155 | if(er.encoded < 0) _ASN_ENCODE_FAILED; |
| 156 | |
| 157 | return er; |
| 158 | } |
| 159 | |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 160 | /* |
| 161 | * REAL specific human-readable output. |
| 162 | */ |
| 163 | int |
| 164 | NativeReal_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 165 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 166 | const double *Dbl = (const double *)sptr; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 167 | |
| 168 | (void)td; /* Unused argument */ |
| 169 | (void)ilevel; /* Unused argument */ |
| 170 | |
vlm | 6678cb1 | 2004-09-26 13:10:40 +0000 | [diff] [blame] | 171 | if(!Dbl) return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 172 | |
| 173 | return (REAL__dump(*Dbl, 0, cb, app_key) < 0) ? -1 : 0; |
vlm | 785435b | 2004-09-14 12:46:35 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void |
| 177 | NativeReal_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
| 178 | |
| 179 | if(!td || !ptr) |
| 180 | return; |
| 181 | |
| 182 | ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)", |
| 183 | td->name, contents_only, ptr); |
| 184 | |
| 185 | if(!contents_only) { |
| 186 | FREEMEM(ptr); |
| 187 | } |
| 188 | } |
| 189 | |