2 * Copyright (c) 2017 Lev Walkin <vlm@lionet.info>.
4 * Redistribution and modifications are permitted subject to BSD license.
6 #include <asn_internal.h>
7 #include <NativeReal.h>
14 NativeReal_decode_ber(const asn_codec_ctx_t *opt_codec_ctx,
15 const asn_TYPE_descriptor_t *td, void **sptr,
16 const void *buf_ptr, size_t size, int tag_mode) {
20 ASN_DEBUG("Decoding %s as REAL (tm=%d)", td->name, tag_mode);
25 rval = ber_check_tags(opt_codec_ctx, td, 0, buf_ptr, size, tag_mode, 0,
27 if(rval.code != RC_OK) return rval;
28 assert(length >= 0); /* Ensured by ber_check_tags */
30 ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
33 * Make sure we have this length.
35 buf_ptr = ((const char *)buf_ptr) + rval.consumed;
36 size -= rval.consumed;
37 if(length > (ber_tlv_len_t)size) {
44 * ASN.1 encoded REAL: buf_ptr, length
45 * Fill the Dbl, at the same time checking for overflow.
46 * If overflow occurred, return with RC_FAIL.
49 uint8_t scratch[24]; /* Longer than %.16f in decimal */
54 if((size_t)length < sizeof(scratch)) {
58 /* This rarely happens: impractically long value */
59 tmp.buf = CALLOC(1, length + 1);
68 memcpy(tmp.buf, buf_ptr, length);
69 tmp.buf[length] = '\0';
71 ret = asn_REAL2double(&tmp, &d);
72 if(tmp.buf != scratch) FREEMEM(tmp.buf);
79 if(NativeReal__set(td, sptr, d) < 0)
84 rval.consumed += length;
86 ASN_DEBUG("Took %ld/%ld bytes to encode %s", (long)rval.consumed,
87 (long)length, td->name);
93 * Encode the NativeReal using the standard REAL type DER encoder.
96 NativeReal_encode_der(const asn_TYPE_descriptor_t *td, const void *sptr,
97 int tag_mode, ber_tlv_tag_t tag,
98 asn_app_consume_bytes_f *cb, void *app_key) {
99 double d = NativeReal__get_double(td, sptr);
100 asn_enc_rval_t erval = {0,0,0};
103 /* Prepare a temporary clean structure */
104 memset(&tmp, 0, sizeof(tmp));
106 if(asn_double2REAL(&tmp, d))
109 /* Encode a fake REAL */
110 erval = der_encode_primitive(td, &tmp, tag_mode, tag, cb, app_key);
111 if(erval.encoded == -1) {
112 assert(erval.structure_ptr == &tmp);
113 erval.structure_ptr = sptr;
116 /* Free possibly allocated members of the temporary structure */
117 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_REAL, &tmp);