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 | */ |
| 12 | #include <NativeReal.h> |
| 13 | #include <INTEGER.h> |
| 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 */ |
| 25 | asn_generic_no_constraint, |
| 26 | NativeReal_decode_ber, |
| 27 | NativeReal_encode_der, |
| 28 | NativeReal_print, |
| 29 | NativeReal_free, |
| 30 | 0, /* Use generic outmost tag fetcher */ |
| 31 | asn1_DEF_NativeReal_tags, |
| 32 | sizeof(asn1_DEF_NativeReal_tags) / sizeof(asn1_DEF_NativeReal_tags[0]), |
| 33 | asn1_DEF_NativeReal_tags, /* Same as above */ |
| 34 | sizeof(asn1_DEF_NativeReal_tags) / sizeof(asn1_DEF_NativeReal_tags[0]), |
| 35 | 0, /* Always in primitive form */ |
| 36 | 0, 0, /* No members */ |
| 37 | 0 /* No specifics */ |
| 38 | }; |
| 39 | |
| 40 | /* |
| 41 | * Decode REAL type. |
| 42 | */ |
| 43 | ber_dec_rval_t |
| 44 | NativeReal_decode_ber(asn1_TYPE_descriptor_t *td, |
| 45 | void **dbl_ptr, void *buf_ptr, size_t size, int tag_mode) { |
| 46 | double *Dbl = (double *)*dbl_ptr; |
| 47 | ber_dec_rval_t rval; |
| 48 | ber_dec_ctx_t ctx = { 0, 0, 0, 0 }; |
| 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 | */ |
| 69 | rval = ber_check_tags(td, &ctx, |
| 70 | buf_ptr, size, tag_mode, &length, 0); |
| 71 | if(rval.code != RC_OK) |
| 72 | return rval; |
| 73 | |
| 74 | ASN_DEBUG("%s length is %d bytes", td->name, (int)length); |
| 75 | |
| 76 | /* |
| 77 | * Make sure we have this length. |
| 78 | */ |
| 79 | buf_ptr = ((char *)buf_ptr) + rval.consumed; |
| 80 | size -= rval.consumed; |
| 81 | if(length > (ber_tlv_len_t)size) { |
| 82 | rval.code = RC_WMORE; |
| 83 | rval.consumed = 0; |
| 84 | return rval; |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * ASN.1 encoded REAL: buf_ptr, length |
| 89 | * Fill the Dbl, at the same time checking for overflow. |
| 90 | * If overflow occured, return with RC_FAIL. |
| 91 | */ |
| 92 | { |
| 93 | REAL_t tmp; |
| 94 | double d; |
| 95 | tmp.buf = (uint8_t *)buf_ptr; |
| 96 | tmp.size = length; |
| 97 | |
| 98 | if(asn1_REAL2double(&tmp, &d)) { |
| 99 | rval.code = RC_FAIL; |
| 100 | rval.consumed = 0; |
| 101 | return rval; |
| 102 | } |
| 103 | |
| 104 | *Dbl = d; |
| 105 | } |
| 106 | |
| 107 | rval.code = RC_OK; |
| 108 | rval.consumed += length; |
| 109 | |
| 110 | ASN_DEBUG("Took %ld/%ld bytes to encode %s (%d)", |
| 111 | (long)rval.consumed, (long)length, td->name, *Dbl); |
| 112 | |
| 113 | return rval; |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Encode the NativeReal using the standard REAL type DER encoder. |
| 118 | */ |
| 119 | der_enc_rval_t |
| 120 | NativeReal_encode_der(asn1_TYPE_descriptor_t *td, void *ptr, |
| 121 | int tag_mode, ber_tlv_tag_t tag, |
| 122 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 123 | double Dbl = *(const double *)ptr; |
| 124 | der_enc_rval_t erval; |
| 125 | REAL_t tmp; |
| 126 | |
| 127 | if(asn1_double2REAL(&tmp, Dbl)) { |
| 128 | erval.encoded = -1; |
| 129 | erval.failed_type = td; |
| 130 | erval.structure_ptr = ptr; |
| 131 | return erval; |
| 132 | } |
| 133 | |
| 134 | /* Encode fake REAL */ |
| 135 | erval = INTEGER_encode_der(td, &tmp, tag_mode, tag, cb, app_key); |
| 136 | if(erval.encoded == -1) { |
| 137 | assert(erval.structure_ptr == &tmp); |
| 138 | erval.structure_ptr = ptr; |
| 139 | } |
| 140 | return erval; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * REAL specific human-readable output. |
| 145 | */ |
| 146 | int |
| 147 | NativeReal_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 148 | asn_app_consume_bytes_f *cb, void *app_key) { |
| 149 | const double *Dbl = (const double *)sptr; |
| 150 | char scratch[64]; |
| 151 | int ret; |
| 152 | |
| 153 | (void)td; /* Unused argument */ |
| 154 | (void)ilevel; /* Unused argument */ |
| 155 | |
| 156 | if(Dbl) { |
| 157 | char *p = scratch; |
| 158 | int buf_size = sizeof(scratch); |
| 159 | for(;;) { |
| 160 | ret = snprintf(p, buf_size, "%f", *Dbl); |
| 161 | if(ret >= 0 && ret < buf_size) { |
| 162 | ret = cb(p, ret, app_key); |
| 163 | if(p != scratch) free(p); |
| 164 | return ret; |
| 165 | } else { |
| 166 | if(p != scratch) free(p); |
| 167 | } |
| 168 | if(ret < 0) buf_size <<= 2; /* Old libc. */ |
| 169 | else buf_size = ret + 1; |
| 170 | (void *)p = MALLOC(ret); |
| 171 | if(!p) return -1; |
| 172 | } |
| 173 | } else { |
| 174 | return cb("<absent>", 8, app_key); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | NativeReal_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) { |
| 180 | |
| 181 | if(!td || !ptr) |
| 182 | return; |
| 183 | |
| 184 | ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)", |
| 185 | td->name, contents_only, ptr); |
| 186 | |
| 187 | if(!contents_only) { |
| 188 | FREEMEM(ptr); |
| 189 | } |
| 190 | } |
| 191 | |