REAL type support, including -fnative-types

diff --git a/skeletons/NativeReal.c b/skeletons/NativeReal.c
new file mode 100644
index 0000000..e7cc28f
--- /dev/null
+++ b/skeletons/NativeReal.c
@@ -0,0 +1,191 @@
+/*-
+ * Copyright (c) 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
+ * Redistribution and modifications are permitted subject to BSD license.
+ */
+/*
+ * Please read the NativeReal.h for the explanation wrt. differences between
+ * REAL and NativeReal.
+ * Basically, both are decoders and encoders of ASN.1 REAL type, but this
+ * implementation deals with the standard (machine-specific) representation
+ * of them instead of using the platform-independent buffer.
+ */
+#include <NativeReal.h>
+#include <INTEGER.h>
+#include <REAL.h>
+#include <assert.h>
+
+/*
+ * NativeReal basic type description.
+ */
+static ber_tlv_tag_t asn1_DEF_NativeReal_tags[] = {
+	(ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
+};
+asn1_TYPE_descriptor_t asn1_DEF_NativeReal = {
+	"REAL",			/* The ASN.1 type is still REAL */
+	asn_generic_no_constraint,
+	NativeReal_decode_ber,
+	NativeReal_encode_der,
+	NativeReal_print,
+	NativeReal_free,
+	0, /* Use generic outmost tag fetcher */
+	asn1_DEF_NativeReal_tags,
+	sizeof(asn1_DEF_NativeReal_tags) / sizeof(asn1_DEF_NativeReal_tags[0]),
+	asn1_DEF_NativeReal_tags,	/* Same as above */
+	sizeof(asn1_DEF_NativeReal_tags) / sizeof(asn1_DEF_NativeReal_tags[0]),
+	0,	/* Always in primitive form */
+	0, 0,	/* No members */
+	0	/* No specifics */
+};
+
+/*
+ * Decode REAL type.
+ */
+ber_dec_rval_t
+NativeReal_decode_ber(asn1_TYPE_descriptor_t *td,
+	void **dbl_ptr, void *buf_ptr, size_t size, int tag_mode) {
+	double *Dbl = (double *)*dbl_ptr;
+	ber_dec_rval_t rval;
+	ber_dec_ctx_t ctx = { 0, 0, 0, 0 };
+	ber_tlv_len_t length;
+
+	/*
+	 * If the structure is not there, allocate it.
+	 */
+	if(Dbl == NULL) {
+		(void *)Dbl = *dbl_ptr = CALLOC(1, sizeof(*Dbl));
+		if(Dbl == NULL) {
+			rval.code = RC_FAIL;
+			rval.consumed = 0;
+			return rval;
+		}
+	}
+
+	ASN_DEBUG("Decoding %s as REAL (tm=%d)",
+		td->name, tag_mode);
+
+	/*
+	 * Check tags.
+	 */
+	rval = ber_check_tags(td, &ctx,
+		buf_ptr, size, tag_mode, &length, 0);
+	if(rval.code != RC_OK)
+		return rval;
+
+	ASN_DEBUG("%s length is %d bytes", td->name, (int)length);
+
+	/*
+	 * Make sure we have this length.
+	 */
+	buf_ptr = ((char *)buf_ptr) + rval.consumed;
+	size -= rval.consumed;
+	if(length > (ber_tlv_len_t)size) {
+		rval.code = RC_WMORE;
+		rval.consumed = 0;
+		return rval;
+	}
+
+	/*
+	 * ASN.1 encoded REAL: buf_ptr, length
+	 * Fill the Dbl, at the same time checking for overflow.
+	 * If overflow occured, return with RC_FAIL.
+	 */
+	{
+		REAL_t tmp;
+		double d;
+		tmp.buf = (uint8_t *)buf_ptr;
+		tmp.size = length;
+
+		if(asn1_REAL2double(&tmp, &d)) {
+			rval.code = RC_FAIL;
+			rval.consumed = 0;
+			return rval;
+		}
+
+		*Dbl = d;
+	}
+
+	rval.code = RC_OK;
+	rval.consumed += length;
+
+	ASN_DEBUG("Took %ld/%ld bytes to encode %s (%d)",
+		(long)rval.consumed, (long)length, td->name, *Dbl);
+
+	return rval;
+}
+
+/*
+ * Encode the NativeReal using the standard REAL type DER encoder.
+ */
+der_enc_rval_t
+NativeReal_encode_der(asn1_TYPE_descriptor_t *td, void *ptr,
+	int tag_mode, ber_tlv_tag_t tag,
+	asn_app_consume_bytes_f *cb, void *app_key) {
+	double Dbl = *(const double *)ptr;
+	der_enc_rval_t erval;
+	REAL_t tmp;
+
+	if(asn1_double2REAL(&tmp, Dbl)) {
+		erval.encoded = -1;
+		erval.failed_type = td;
+		erval.structure_ptr = ptr;
+		return erval;
+	}
+	
+	/* Encode fake REAL */
+	erval = INTEGER_encode_der(td, &tmp, tag_mode, tag, cb, app_key);
+	if(erval.encoded == -1) {
+		assert(erval.structure_ptr == &tmp);
+		erval.structure_ptr = ptr;
+	}
+	return erval;
+}
+
+/*
+ * REAL specific human-readable output.
+ */
+int
+NativeReal_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
+	asn_app_consume_bytes_f *cb, void *app_key) {
+	const double *Dbl = (const double *)sptr;
+	char scratch[64];
+	int ret;
+
+	(void)td;	/* Unused argument */
+	(void)ilevel;	/* Unused argument */
+
+	if(Dbl) {
+		char *p = scratch;
+		int buf_size = sizeof(scratch);
+	    for(;;) {
+		ret = snprintf(p, buf_size, "%f", *Dbl);
+		if(ret >= 0 && ret < buf_size) {
+			ret = cb(p, ret, app_key);
+			if(p != scratch) free(p);
+			return ret;
+		} else {
+			if(p != scratch) free(p);
+		}
+		if(ret < 0) buf_size <<= 2;	/* Old libc. */
+		else buf_size = ret + 1;
+		(void *)p = MALLOC(ret);
+		if(!p) return -1;
+	    }
+	} else {
+		return cb("<absent>", 8, app_key);
+	}
+}
+
+void
+NativeReal_free(asn1_TYPE_descriptor_t *td, void *ptr, int contents_only) {
+
+	if(!td || !ptr)
+		return;
+
+	ASN_DEBUG("Freeing %s as REAL (%d, %p, Native)",
+		td->name, contents_only, ptr);
+
+	if(!contents_only) {
+		FREEMEM(ptr);
+	}
+}
+
diff --git a/skeletons/NativeReal.h b/skeletons/NativeReal.h
new file mode 100644
index 0000000..1a5436d
--- /dev/null
+++ b/skeletons/NativeReal.h
@@ -0,0 +1,23 @@
+/*-
+ * Copyright (c) 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
+ * Redistribution and modifications are permitted subject to BSD license.
+ */
+/*
+ * This type differs from the standard REAL in that it is modelled using
+ * the fixed machine type (double), so it can hold only values of
+ * limited precision. There is no explicit type (i.e., NativeReal_t).
+ * Use of this type is normally enabled by -fnative-integers.
+ */
+#ifndef	ASN_TYPE_NativeReal_H
+#define	ASN_TYPE_NativeReal_H
+
+#include <constr_TYPE.h>
+
+extern asn1_TYPE_descriptor_t asn1_DEF_NativeReal;
+
+ber_type_decoder_f NativeReal_decode_ber;
+der_type_encoder_f NativeReal_encode_der;
+asn_struct_print_f NativeReal_print;
+asn_struct_free_f  NativeReal_free;
+
+#endif	/* ASN_TYPE_NativeReal_H */
diff --git a/skeletons/REAL.c b/skeletons/REAL.c
new file mode 100644
index 0000000..79b5173
--- /dev/null
+++ b/skeletons/REAL.c
@@ -0,0 +1,377 @@
+/*-
+ * Copyright (c) 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
+ * Redistribution and modifications are permitted subject to BSD license.
+ */
+#include <REAL.h>
+#include <INTEGER.h>
+#include <stdlib.h>	/* for strtod(3) */
+#include <math.h>
+#include <errno.h>
+#include <assert.h>
+
+#undef	INT_MAX
+#define	INT_MAX	((int)(((unsigned int)-1) >> 1))
+
+/*
+ * REAL basic type description.
+ */
+static ber_tlv_tag_t asn1_DEF_REAL_tags[] = {
+	(ASN_TAG_CLASS_UNIVERSAL | (9 << 2))
+};
+asn1_TYPE_descriptor_t asn1_DEF_REAL = {
+	"REAL",
+	asn_generic_no_constraint,
+	INTEGER_decode_ber,	/* Implemented in terms of INTEGER type */
+	INTEGER_encode_der,
+	REAL_print,
+	INTEGER_free,
+	0, /* Use generic outmost tag fetcher */
+	asn1_DEF_REAL_tags,
+	sizeof(asn1_DEF_REAL_tags) / sizeof(asn1_DEF_REAL_tags[0]),
+	asn1_DEF_REAL_tags,	/* Same as above */
+	sizeof(asn1_DEF_REAL_tags) / sizeof(asn1_DEF_REAL_tags[0]),
+	0,	/* Always in primitive form */
+	0, 0,	/* No members */
+	0	/* No specifics */
+};
+
+int
+REAL_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
+	asn_app_consume_bytes_f *cb, void *app_key) {
+	const REAL_t *st = (const REAL_t *)sptr;
+	char buf[128];
+	double d;
+	int ret;
+
+	(void)td;	/* Unused argument */
+	(void)ilevel;	/* Unused argument */
+
+	if(!st)
+		return cb("<absent>", 8, app_key);
+
+	if(asn1_REAL2double(st, &d))
+		return cb("<error>", 7, app_key);
+
+	ret = snprintf(buf, sizeof(buf), "%f", d);
+	if(ret < 0 || ret >= sizeof(buf))
+		return cb("<error>", 7, app_key);
+
+	return cb(buf, ret, app_key);
+}
+
+int
+asn1_REAL2double(const REAL_t *st, double *dbl_value) {
+	unsigned long octv;
+
+	if(!st || !st->buf) {
+		errno = EINVAL;
+		return -1;
+	}
+
+	if(st->size == 0) {
+		*dbl_value = 0;
+		return 0;
+	}
+
+	octv = st->buf[0];	/* unsigned byte */
+
+	switch(octv & 0xC0) {
+	case 0x40:	/* X.690: 8.5.8 */
+		/* "SpecialRealValue" */
+
+		/* Be liberal in what you accept...
+		if(st->size != 1) ...
+		*/
+
+		switch(st->buf[0]) {
+		case 0x40:	/* 01000000: PLUS-INFINITY */
+			*dbl_value = INFINITY;
+			return 0;
+		case 0x41:	/* 01000001: MINUS-INFINITY */
+			*dbl_value = -INFINITY;
+			return 0;
+			/*
+			 * The following cases are defined by
+			 * X.690 Amendment 1 (10/03)
+			 */
+		case 0x42:	/* 01000010: NOT-A-NUMBER */
+			*dbl_value = NAN;
+			return 0;
+		case 0x43:	/* 01000011: minus zero */
+			*dbl_value = NAN;
+			return 0;
+		}
+
+		errno = EINVAL;
+		return -1;
+	case 0x00: {	/* X.690: 8.5.6 */
+		/*
+		 * Decimal. NR{1,2,3} format.
+		 */
+		double d;
+
+		assert(st->buf[st->size - 1] == 0); /* Security, vashu mat' */
+
+		d = strtod((char *)st->buf, 0);
+		if(finite(d)) {
+			*dbl_value = d;
+			return 0;
+		} else {
+			errno = ERANGE;
+			return 0;
+		}
+	  }
+	}
+
+	/*
+	 * Binary representation.
+	 */
+    {
+	double m;
+	int expval;		/* exponent value */
+	unsigned int elen;	/* exponent value length, in octets */
+	unsigned int scaleF;
+	unsigned int baseF;
+	uint8_t *ptr;
+	uint8_t *end;
+	int sign;
+
+	switch((octv & 0x30) >> 4) {
+	case 0x00: baseF = 1; break;	/* base 2 */
+	case 0x01: baseF = 3; break;	/* base 8 */
+	case 0x02: baseF = 4; break;	/* base 16 */
+	default:
+		/* Reserved field, can't parse now. */
+		errno = EINVAL;
+		return -1;
+	}
+
+	sign = (octv & 0x40);	/* bit 7 */
+	scaleF = (octv & 0x0C) >> 2;	/* bits 4 to 3 */
+
+	if(st->size <= (1 + (octv & 0x03))) {
+		errno = EINVAL;
+		return -1;
+	}
+
+	if((octv & 0x03) == 0x11) {
+		/* 8.5.6.4, case d) */
+		elen = st->buf[1];	/* unsigned binary number */
+		if(elen == 0 || st->size <= (2 + elen)) {
+			errno = EINVAL;
+			return -1;
+		}
+		ptr = &st->buf[2];
+	} else {
+		elen = (octv & 0x03);
+		ptr = &st->buf[1];
+	}
+
+	/* Fetch the multibyte exponent */
+	expval = (int)(*(int8_t *)ptr);
+	end = ptr + elen + 1;
+	for(ptr++; ptr < end; ptr++)
+		expval = (expval * 256) + *ptr;
+
+	m = 0.0;	/* Initial mantissa value */
+
+	/* Okay, the exponent is here. Now, what about mantissa? */
+	end = st->buf + st->size;
+	if(ptr < end) {
+		for(; ptr < end; ptr++)
+			m = scalbn(m, 8) + *ptr;
+	}
+
+	ASN_DEBUG("m=%.10f, scF=%d, bF=%d, expval=%d, ldexp()=%f, scalbn()=%f",
+		m, scaleF, baseF, expval,
+		ldexp(m, expval * baseF + scaleF),
+		scalbn(m, scaleF) * pow(pow(2, baseF), expval)
+	);
+
+	/*
+	 * (S * N * 2^F) * B^E
+	 * Essentially:
+	m = scalbn(m, scaleF) * pow(pow(2, base), expval);
+	 */
+	m = ldexp(m, expval * baseF + scaleF);
+	if(finite(m)) {
+		*dbl_value = sign ? -m : m;
+	} else {
+		errno = ERANGE;
+		return -1;
+	}
+
+    } /* if(binary_format) */
+
+	return 0;
+}
+
+/*
+ * Assume IEEE 754 floating point: standard 64 bit double.
+ * [1 bit sign]  [11 bits exponent]  [52 bits mantissa]
+ */
+int
+asn1_double2REAL(REAL_t *st, double dbl_value) {
+#ifdef	WORDS_BIGENDIAN		/* Known to be big-endian */
+	int littleEndian = 0;
+#else				/* need to test: have no explicit information */
+	unsigned int LE = 1;
+	int littleEndian = *(unsigned char *)&LE;
+#endif
+	uint8_t buf[16];	/* More than enough for 8-byte dbl_value */
+	uint8_t dscr[sizeof(dbl_value)];	/* double value scratch pad */
+	/* Assertion guards: won't even compile, if unexpected double size */
+	char assertion_buffer1[9 - sizeof(dbl_value)] __attribute__((unused));
+	char assertion_buffer2[sizeof(dbl_value) - 7] __attribute__((unused));
+	uint8_t *ptr = buf;
+	uint8_t *mstop;		/* Last byte of mantissa */
+	unsigned int mval;	/* Value of the last byte of mantissa */
+	unsigned int bmsign;	/* binary mask with sign */
+	unsigned int buflen;
+	unsigned int accum;
+	int expval;
+
+	if(!st) {
+		errno = EINVAL;
+		return -1;
+	}
+
+	expval = ilogb(dbl_value);
+
+	if(expval == -INT_MAX	/* Also catches (dbl_value == 0) */
+	|| expval == INT_MAX	/* catches finite() which catches isnan() */
+	) {
+		if(!st->buf || st->size < 2) {
+			(void *)ptr = MALLOC(2);
+			if(!ptr) return -1;
+			st->buf = ptr;
+		}
+		/* fpclassify(3) is not portable yet */
+		if(expval == -INT_MAX) {
+			if(signbit(dbl_value)) {
+				st->buf[0] = 0x80 | 0x40;
+				st->buf[1] = 0;
+				st->size = 2;
+			} else {
+				st->buf[0] = 0;	/* JIC */
+				st->size = 0;
+			}
+		} else if(isinf(dbl_value)) {
+			if(signbit(dbl_value)) {
+				st->buf[0] = 0x41;	/* MINUS-INFINITY */
+			} else {
+				st->buf[0] = 0x40;	/* PLUS-INFINITY */
+			}
+			st->buf[1] = 0;
+			st->size = 1;
+		} else {
+			st->buf[0] = 0x42;	/* NaN */
+			st->buf[1] = 0;
+			st->size = 1;
+		}
+		return 0;
+	}
+
+	if(littleEndian) {
+		uint8_t *s = ((uint8_t *)&dbl_value) + sizeof(dbl_value) - 2;
+		uint8_t *d;
+
+		bmsign = 0x80 | ((s[1] >> 1) & 0x40);	/* binary mask & - */
+		for(mstop = d = dscr; s >= (uint8_t *)&dbl_value; d++, s--) {
+			*d = *s;
+			if(*d) mstop = d;
+		}
+	} else {
+		uint8_t *s = ((uint8_t *)&dbl_value) + 1;
+		uint8_t *end = ((uint8_t *)&dbl_value) + sizeof(double);
+		uint8_t *d;
+
+		bmsign = 0x80 | ((s[-1] >> 1) & 0x40);	/* binary mask & - */
+		for(mstop = d = dscr; s < end; d++, s++) {
+			*d = *s;
+			if(*d) mstop = d;
+		}
+	}
+
+	/* Remove parts of the exponent, leave mantissa and explicit 1. */
+	dscr[0] = 0x10 | (dscr[0] & 0x0f);
+
+	/* Adjust exponent in a very unobvious way */
+	expval -= 8 * ((mstop - dscr) + 1) - 4;
+
+	/* This loop ensures DER conformance by forcing mantissa odd: 11.3.1 */
+	mval = *mstop;
+	if(mval && !(mval & 1)) {
+		unsigned int shift_count = 1;
+		unsigned int ishift;
+		uint8_t *mptr;
+
+		/*
+		 * Figure out what needs to be done to make mantissa odd.
+		 */
+		if(!(mval & 0x0f))	/* Speed-up a little */
+			shift_count = 4;
+		while(((mval >> shift_count) & 1) == 0)
+			shift_count++;
+
+		ishift = 8 - shift_count;
+		accum = 0;
+
+		/* Go over the buffer, shifting it shift_count bits right. */
+		for(mptr = dscr; mptr <= mstop; mptr++) {
+			mval = *mptr;
+			*mptr = accum | (mval >> shift_count);
+			accum = mval << ishift;
+		}
+
+		/* Adjust mantissa appropriately. */
+		expval += shift_count;
+	}
+
+	if(expval < 0) {
+		if((expval >> 7) == -1) {
+			*ptr++ = bmsign | 0x00;
+			*ptr++ = expval;
+		} else if((expval >> 15) == -1) {
+			*ptr++ = bmsign | 0x01;
+			*ptr++ = expval >> 8;
+			*ptr++ = expval;
+		} else {
+			assert((expval >> 23) == -1);
+			*ptr++ = bmsign | 0x02;
+			*ptr++ = expval >> 16;
+			*ptr++ = expval >> 8;
+			*ptr++ = expval;
+		}
+	} else if(expval <= 0x7f) {
+		*ptr++ = bmsign | 0x00;
+		*ptr++ = expval;
+	} else if(expval <= 0x7fff) {
+		*ptr++ = bmsign | 0x01;
+		*ptr++ = expval >> 8;
+		*ptr++ = expval;
+	} else {
+		assert(expval <= 0x7fffff);
+		*ptr++ = bmsign | 0x02;
+		*ptr++ = expval >> 16;
+		*ptr++ = expval >> 8;
+		*ptr++ = expval;
+	}
+
+	buflen = (mstop - dscr) + 1;
+	memcpy(ptr, dscr, buflen);
+	ptr += buflen;
+	buflen = ptr - buf;
+
+	(void *)ptr = MALLOC(buflen + 1);
+	if(!ptr) return -1;
+
+	memcpy(ptr, buf, buflen);
+	buf[buflen] = 0;	/* JIC */
+
+	if(st->buf) FREEMEM(st->buf);
+	st->buf = ptr;
+	st->size = buflen;
+
+	return 0;
+}
diff --git a/skeletons/REAL.h b/skeletons/REAL.h
new file mode 100644
index 0000000..751000b
--- /dev/null
+++ b/skeletons/REAL.h
@@ -0,0 +1,32 @@
+/*-
+ * Copyright (c) 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
+ * Redistribution and modifications are permitted subject to BSD license.
+ */
+#ifndef	ASN_TYPE_REAL_H
+#define	ASN_TYPE_REAL_H
+
+#include <constr_TYPE.h>
+
+typedef struct REAL {
+	uint8_t *buf;	/* Buffer with REAL type encoding */
+	int size;	/* Size of the buffer */
+} REAL_t;
+
+extern asn1_TYPE_descriptor_t asn1_DEF_REAL;
+
+asn_struct_print_f REAL_print;
+
+/***********************************
+ * Some handy conversion routines. *
+ ***********************************/
+
+/*
+ * Convert between native double type and REAL representation (DER).
+ * RETURN VALUES:
+ *  0: Value converted successfully
+ * -1: An error occured while converting the value: invalid format.
+ */
+int asn1_REAL2double(const REAL_t *real_ptr, double *d);
+int asn1_double2REAL(REAL_t *real_ptr, double d);
+
+#endif	/* ASN_TYPE_REAL_H */
diff --git a/skeletons/file-dependencies b/skeletons/file-dependencies
index 128bda2..f401844 100644
--- a/skeletons/file-dependencies
+++ b/skeletons/file-dependencies
@@ -20,10 +20,12 @@
 NULL.h NULL.c BOOLEAN.h
 NativeEnumerated.h NativeEnumerated.c NativeInteger.h
 NativeInteger.h NativeInteger.c INTEGER.h
+NativeReal.h NativeReal.c REAL.h
 NumericString.h NumericString.c
 OBJECT_IDENTIFIER.h OBJECT_IDENTIFIER.c INTEGER.h
 ObjectDescriptor.h ObjectDescriptor.c GraphicString.h
 PrintableString.h PrintableString.c
+REAL.h REAL.c INTEGER.h
 RELATIVE-OID.h RELATIVE-OID.c OBJECT-IDENTIFIER.h
 T61String.h T61String.c
 TeletexString.h TeletexString.c	
diff --git a/skeletons/tests/Makefile.am b/skeletons/tests/Makefile.am
index 0c93ef9..7af5843 100644
--- a/skeletons/tests/Makefile.am
+++ b/skeletons/tests/Makefile.am
@@ -6,6 +6,7 @@
 	check-OIDs		\
 	check-GeneralizedTime	\
 	check-UTCTime		\
-	check-INTEGER
+	check-INTEGER		\
+	check-REAL
 
 TESTS = $(check_PROGRAMS)
diff --git a/skeletons/tests/Makefile.in b/skeletons/tests/Makefile.in
index 2f84112..ea0acc0 100644
--- a/skeletons/tests/Makefile.in
+++ b/skeletons/tests/Makefile.in
@@ -13,7 +13,7 @@
 # PARTICULAR PURPOSE.
 
 @SET_MAKE@
-SOURCES = check-GeneralizedTime.c check-INTEGER.c check-OIDs.c check-UTCTime.c check-ber_tlv_tag.c check-length.c
+SOURCES = check-GeneralizedTime.c check-INTEGER.c check-OIDs.c check-REAL.c check-UTCTime.c check-ber_tlv_tag.c check-length.c
 
 srcdir = @srcdir@
 top_srcdir = @top_srcdir@
@@ -38,7 +38,8 @@
 host_triplet = @host@
 check_PROGRAMS = check-ber_tlv_tag$(EXEEXT) check-length$(EXEEXT) \
 	check-OIDs$(EXEEXT) check-GeneralizedTime$(EXEEXT) \
-	check-UTCTime$(EXEEXT) check-INTEGER$(EXEEXT)
+	check-UTCTime$(EXEEXT) check-INTEGER$(EXEEXT) \
+	check-REAL$(EXEEXT)
 subdir = skeletons/tests
 DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
@@ -57,6 +58,9 @@
 check_OIDs_SOURCES = check-OIDs.c
 check_OIDs_OBJECTS = check-OIDs.$(OBJEXT)
 check_OIDs_LDADD = $(LDADD)
+check_REAL_SOURCES = check-REAL.c
+check_REAL_OBJECTS = check-REAL.$(OBJEXT)
+check_REAL_LDADD = $(LDADD)
 check_UTCTime_SOURCES = check-UTCTime.c
 check_UTCTime_OBJECTS = check-UTCTime.$(OBJEXT)
 check_UTCTime_LDADD = $(LDADD)
@@ -72,6 +76,7 @@
 @AMDEP_TRUE@DEP_FILES = ./$(DEPDIR)/check-GeneralizedTime.Po \
 @AMDEP_TRUE@	./$(DEPDIR)/check-INTEGER.Po \
 @AMDEP_TRUE@	./$(DEPDIR)/check-OIDs.Po \
+@AMDEP_TRUE@	./$(DEPDIR)/check-REAL.Po \
 @AMDEP_TRUE@	./$(DEPDIR)/check-UTCTime.Po \
 @AMDEP_TRUE@	./$(DEPDIR)/check-ber_tlv_tag.Po \
 @AMDEP_TRUE@	./$(DEPDIR)/check-length.Po
@@ -84,9 +89,11 @@
 LINK = $(LIBTOOL) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \
 	$(AM_LDFLAGS) $(LDFLAGS) -o $@
 SOURCES = check-GeneralizedTime.c check-INTEGER.c check-OIDs.c \
-	check-UTCTime.c check-ber_tlv_tag.c check-length.c
+	check-REAL.c check-UTCTime.c check-ber_tlv_tag.c \
+	check-length.c
 DIST_SOURCES = check-GeneralizedTime.c check-INTEGER.c check-OIDs.c \
-	check-UTCTime.c check-ber_tlv_tag.c check-length.c
+	check-REAL.c check-UTCTime.c check-ber_tlv_tag.c \
+	check-length.c
 ETAGS = etags
 CTAGS = ctags
 DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
@@ -251,6 +258,9 @@
 check-OIDs$(EXEEXT): $(check_OIDs_OBJECTS) $(check_OIDs_DEPENDENCIES) 
 	@rm -f check-OIDs$(EXEEXT)
 	$(LINK) $(check_OIDs_LDFLAGS) $(check_OIDs_OBJECTS) $(check_OIDs_LDADD) $(LIBS)
+check-REAL$(EXEEXT): $(check_REAL_OBJECTS) $(check_REAL_DEPENDENCIES) 
+	@rm -f check-REAL$(EXEEXT)
+	$(LINK) $(check_REAL_LDFLAGS) $(check_REAL_OBJECTS) $(check_REAL_LDADD) $(LIBS)
 check-UTCTime$(EXEEXT): $(check_UTCTime_OBJECTS) $(check_UTCTime_DEPENDENCIES) 
 	@rm -f check-UTCTime$(EXEEXT)
 	$(LINK) $(check_UTCTime_LDFLAGS) $(check_UTCTime_OBJECTS) $(check_UTCTime_LDADD) $(LIBS)
@@ -270,6 +280,7 @@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/check-GeneralizedTime.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/check-INTEGER.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/check-OIDs.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/check-REAL.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/check-UTCTime.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/check-ber_tlv_tag.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/check-length.Po@am__quote@
diff --git a/skeletons/tests/check-REAL.c b/skeletons/tests/check-REAL.c
new file mode 100644
index 0000000..12310e3
--- /dev/null
+++ b/skeletons/tests/check-REAL.c
@@ -0,0 +1,139 @@
+#define	EMIT_ASN_DEBUG	1
+#include <INTEGER.c>
+#include <REAL.c>
+#include <ber_decoder.c>
+#include <ber_tlv_length.c>
+#include <ber_tlv_tag.c>
+#include <der_encoder.c>
+#include <constraints.c>
+
+static void
+check(REAL_t *rn, double orig_dbl) {
+	double val;
+	uint8_t *p, *end;
+	int ret;
+
+	printf("double value %.12f [", orig_dbl);
+	for(p = (uint8_t *)&orig_dbl, end = p + sizeof(double); p < end ; p++)
+		printf("%02x", *p);
+	printf("] (ilogb %d)\n", ilogb(orig_dbl));
+
+	val = frexp(orig_dbl, &ret);
+	printf("frexp(%f, %d): [", val, ret);
+	for(p = (uint8_t *)&val, end = p + sizeof(double); p < end ; p++)
+		printf("%02x", *p);
+	printf("]\n");
+
+	ret = asn1_double2REAL(rn, orig_dbl);
+	assert(ret == 0);
+
+	printf("converted into [");
+	for(p = rn->buf, end = p + rn->size; p < end; p++)
+		printf("%02x", *p);
+	printf("]\n");
+
+	ret = asn1_REAL2double(rn, &val);
+	assert(ret == 0);
+
+	printf("and back to double: [");
+	for(p = (uint8_t *)&val, end = p + sizeof(double); p < end ; p++)
+		printf("%02x", *p);
+	printf("] (ilogb %d)\n", ilogb(val));
+
+	printf("%.12f vs %.12f\n", orig_dbl, val);
+
+	assert(orig_dbl == val);
+	printf("OK\n");
+}
+
+uint8_t buf_1_0[]  = { 0x80, 0xcc, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
+uint8_t buf_1_1[]  = { 0x80, 0xcc, 0x11, 0x99, 0x99, 0x99, 0x99, 0x99, 0x9a };
+uint8_t buf_3_14[] = { 0x80, 0xcd, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f };
+/* These ones are very interesting! It checks mantissa overflow! */
+uint8_t buf_mo1[]  = { 0x80, 0xc5, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f,3};
+uint8_t buf_mo2[]  = { 0x80, 0xbd, 0x19, 0x1e, 0xb8, 0x51, 0xeb, 0x85, 0x1f,3,2};
+
+static void
+check_buf(uint8_t *buf, size_t bufsize, double verify) {
+	REAL_t rn;
+	double val;
+	uint8_t *p, *end;
+	int ret;
+
+	printf("verify double value %.12f [", verify);
+	for(p = (uint8_t *)&verify, end = p + sizeof(double); p < end ; p++)
+		printf("%02x", *p);
+	printf("] (ilogb %d)\n", ilogb(verify));
+
+	rn.buf = 0;
+	rn.size = 0;
+
+	ret = asn1_double2REAL(&rn, verify);
+	assert(ret == 0);
+
+	printf("canonical DER: [");
+	for(p = rn.buf, end = p + rn.size; p < end; p++)
+		printf("%02x", *p);
+	printf("]\n");
+
+	rn.buf = buf;
+	rn.size = bufsize;
+
+	printf("received as:   [");
+	for(p = rn.buf, end = p + rn.size; p < end; p++)
+		printf("%02x", *p);
+	printf("]\n");
+
+	ret = asn1_REAL2double(&rn, &val);
+	assert(ret == 0);
+
+	printf("%.12f vs %.12f\n", verify, val);
+
+	assert(val == verify);
+}
+
+int
+main() {
+	REAL_t rn;
+
+	memset(&rn, 0, sizeof(rn));
+
+	check(&rn, 0.0);
+	check(&rn, 1.0);
+	check(&rn, -1.0);
+	check(&rn, 1.5);
+	check(&rn, 0.1);
+	check(&rn, 0.33333);
+	check(&rn, 2);
+	check(&rn, 2.1);
+	check(&rn, 3);
+	check(&rn, 3.1);
+	check(&rn, 3.14);
+	check(&rn, 3.1415);
+	check(&rn, 3.141592);
+	check(&rn, 3.14159265);
+	check(&rn, -3.14159265);
+	check(&rn, 14159265.0);
+	check(&rn, -123456789123456789.0);
+	check(&rn, 0.00000000001);
+	check(&rn, 0.00000000002);
+	check(&rn, 0.00000000009);
+	check(&rn, 0.0000000000000000000001);
+	check(&rn, 0.000000000000000000000000000001); /* proved 2B a problem */
+	check(&rn,-0.000000000000000000000000000001); /* proved 2B a problem */
+	check(&rn, 0.0000000000010000000001000000000001);
+	check(&rn, 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001);
+	check(&rn, 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001);
+	check(&rn,-0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001);
+	check(&rn,-3.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333);
+	check(&rn, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000033333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333);
+	check(&rn, -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001);
+
+	check_buf(buf_1_0, sizeof(buf_1_0),	1.0);
+	check_buf(buf_1_1, sizeof(buf_1_1),	1.1);
+	check_buf(buf_3_14, sizeof(buf_3_14),	3.14);
+	check_buf(buf_mo1, sizeof(buf_mo1),	3.14);
+	check_buf(buf_mo2, sizeof(buf_mo2),	3.14);
+
+	return 0;
+}