portability


git-svn-id: https://asn1c.svn.sourceforge.net/svnroot/asn1c/trunk@89 59561ff5-6e30-0410-9f3c-9617f08c8826
diff --git a/skeletons/BIT_STRING.c b/skeletons/BIT_STRING.c
index 7727d78..1646929 100644
--- a/skeletons/BIT_STRING.c
+++ b/skeletons/BIT_STRING.c
@@ -59,7 +59,7 @@
 int
 BIT_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
 		asn_app_consume_bytes_f *cb, void *app_key) {
-	static char h2c[16] = "0123456789ABCDEF";
+	static const char *h2c = "0123456789ABCDEF";
 	char scratch[64];
 	const BIT_STRING_t *st = sptr;
 	uint8_t *buf;
diff --git a/skeletons/BOOLEAN.c b/skeletons/BOOLEAN.c
index 9391316..209235d 100644
--- a/skeletons/BOOLEAN.c
+++ b/skeletons/BOOLEAN.c
@@ -60,7 +60,7 @@
 
 	ASN_DEBUG("Boolean length is %d bytes", (int)length);
 
-	buf_ptr += rval.consumed;
+	(char *)buf_ptr += rval.consumed;
 	size -= rval.consumed;
 	if(length > (ber_tlv_len_t)size) {
 		rval.code = RC_WMORE;
diff --git a/skeletons/GeneralizedTime.c b/skeletons/GeneralizedTime.c
index 5e3b9b0..99f2e72 100644
--- a/skeletons/GeneralizedTime.c
+++ b/skeletons/GeneralizedTime.c
@@ -437,7 +437,7 @@
 		if(!opt_gt) { free(buf); return 0; }
 	}
 
-	opt_gt->buf = buf;
+	opt_gt->buf = (unsigned char *)buf;
 	opt_gt->size = size;
 
 	return opt_gt;
diff --git a/skeletons/INTEGER.c b/skeletons/INTEGER.c
index 120b94f..1af06c7 100644
--- a/skeletons/INTEGER.c
+++ b/skeletons/INTEGER.c
@@ -66,7 +66,7 @@
 	/*
 	 * Make sure we have this length.
 	 */
-	buf_ptr += rval.consumed;
+	(char *)buf_ptr += rval.consumed;
 	size -= rval.consumed;
 	if(length > (ber_tlv_len_t)size) {
 		rval.code = RC_WMORE;
@@ -230,7 +230,7 @@
 	/* Output in the long xx:yy:zz... format */
 	/* TODO: replace with generic algorithm (Knuth TAOCP Vol 2, 4.3.1) */
 	for(p = scratch; buf < buf_end; buf++) {
-		static char h2c[16] = "0123456789ABCDEF";
+		static const char *h2c = "0123456789ABCDEF";
 		if((p - scratch) >= (ssize_t)(sizeof(scratch) - 4)) {
 			/* Flush buffer */
 			if(cb(scratch, p - scratch, app_key))
diff --git a/skeletons/NativeInteger.c b/skeletons/NativeInteger.c
index 8f13a59..3dce02f 100644
--- a/skeletons/NativeInteger.c
+++ b/skeletons/NativeInteger.c
@@ -73,7 +73,7 @@
 	/*
 	 * Make sure we have this length.
 	 */
-	buf_ptr += rval.consumed;
+	(char *)buf_ptr += rval.consumed;
 	size -= rval.consumed;
 	if(length > (ber_tlv_len_t)size) {
 		rval.code = RC_WMORE;
diff --git a/skeletons/OBJECT_IDENTIFIER.c b/skeletons/OBJECT_IDENTIFIER.c
index 216aed8..20de2f6 100644
--- a/skeletons/OBJECT_IDENTIFIER.c
+++ b/skeletons/OBJECT_IDENTIFIER.c
@@ -154,7 +154,7 @@
 #ifndef	WORDS_BIGENDIAN
 	if(*(unsigned char *)&LE) {	/* Little endian (x86) */
 		/* "Convert" to big endian */
-		rvbuf += rvsize / CHAR_BIT - 1;
+		(unsigned char *)rvbuf += rvsize / CHAR_BIT - 1;
 		((unsigned char *)rvstart)--;
 		inc = -1;	/* Descending */
 	} else
@@ -167,7 +167,7 @@
 		/* Clear the high unused bits */
 		for(bits = rvsize - arclen;
 			bits > CHAR_BIT;
-				rvbuf += inc, bits -= CHAR_BIT)
+				(unsigned char *)rvbuf += inc, bits -= CHAR_BIT)
 				*(unsigned char *)rvbuf = 0;
 
 		/* Fill the body of a value */
@@ -177,17 +177,17 @@
 			if(bits >= CHAR_BIT) {
 				bits -= CHAR_BIT;
 				*(unsigned char *)rvbuf = (cache >> bits);
-				rvbuf += inc;
+				(unsigned char *)rvbuf += inc;
 			}
 		}
 		if(bits) {
 			*(unsigned char *)rvbuf = cache;
-			rvbuf += inc;
+			(unsigned char *)rvbuf += inc;
 		}
 	}
 
 	if(add) {
-		for(rvbuf -= inc; rvbuf != rvstart; rvbuf -= inc) {
+		for((unsigned char *)rvbuf -= inc; rvbuf != rvstart; (unsigned char *)rvbuf -= inc) {
 			int v = add + *(unsigned char *)rvbuf;
 			if(v & (-1 << CHAR_BIT)) {
 				*(unsigned char *)rvbuf
@@ -287,7 +287,7 @@
 int
 OBJECT_IDENTIFIER_get_arcs(OBJECT_IDENTIFIER_t *oid, void *arcs,
 		unsigned int arc_type_size, unsigned int arc_slots) {
-	void *arcs_end = arcs + (arc_type_size * arc_slots);
+	void *arcs_end = (char *)arcs + (arc_type_size * arc_slots);
 	int num_arcs = 0;
 	int startn = 0;
 	int add = 0;
@@ -319,10 +319,10 @@
 
 			add = -40 * first_arc;
 			memset(arcs, 0, arc_type_size);
-			*(unsigned char *)(arcs
+			*(unsigned char *)((char *)arcs
 				+ ((*(char *)&LE)?0:(arc_type_size - 1)))
 					= first_arc;
-			arcs += arc_type_size;
+			(char *)arcs += arc_type_size;
 		}
 
 		/* Decode, if has space */
@@ -332,7 +332,7 @@
 					arcs, arc_type_size))
 				return -1;
 			startn = i + 1;
-			arcs += arc_type_size;
+			(char *)arcs += arc_type_size;
 			add = 0;
 		}
 		num_arcs++;
@@ -371,7 +371,7 @@
 #endif
 
 	if(isLittleEndian && !prepared_order) {
-		uint8_t *a = arcval + arcval_size - 1;
+		uint8_t *a = (unsigned char *)arcval + arcval_size - 1;
 		uint8_t *aend = arcval;
 		uint8_t *msb = buffer + arcval_size - 1;
 		for(tp = buffer; a >= aend; tp++, a--)
@@ -381,7 +381,7 @@
 		tp = msb;	/* Most significant non-zero byte */
 	} else {
 		/* Look for most significant non-zero byte */
-		tend = arcval + arcval_size;
+		tend = (unsigned char *)arcval + arcval_size;
 		for(tp = arcval; tp < tend - 1; tp++)
 			if(*tp) break;
 	}
@@ -451,7 +451,8 @@
 			unsigned char *ps, *pe;
 			/* If more significant bytes are present,
 			 * make them > 255 quick */
-			for(ps = arcs + 1, pe = ps+arc_type_size; ps < pe; ps++)
+			for(ps = (unsigned char *)arcs + 1, pe = ps+arc_type_size;
+					ps < pe; ps++)
 				arc0 |= *ps, arc1 |= *(ps + arc_type_size);
 			arc0 <<= CHAR_BIT, arc1 <<= CHAR_BIT;
 			arc0 = *((unsigned char *)arcs + 0);
@@ -512,10 +513,15 @@
 		uint8_t *tp;
 #ifdef	__GNUC__
 		uint8_t first_value[1 + arc_type_size];	/* of two arcs */
+		uint8_t *fv = first_value;
 #else
 		uint8_t *first_value = alloca(1 + arc_type_size);
-#endif
 		uint8_t *fv = first_value;
+		if(!first_value) {
+			errno = ENOMEM;
+			return -1;
+		}
+#endif
 
 		/*
 		 * Simulate first_value = arc0 * 40 + arc1;
@@ -524,8 +530,8 @@
 		*fv++ = 0;
 		(char *)arcs += arc_type_size;
 		if(isLittleEndian) {
-			uint8_t *aend = arcs - 1;
-			uint8_t *a1 = arcs + arc_type_size - 1;
+			uint8_t *aend = (unsigned char *)arcs - 1;
+			uint8_t *a1 = (unsigned char *)arcs + arc_type_size - 1;
 			for(; a1 > aend; fv++, a1--) *fv = *a1;
 		} else {
 			uint8_t *a1 = arcs;
diff --git a/skeletons/OCTET_STRING.c b/skeletons/OCTET_STRING.c
index 53a1a0d..d5b28c3 100644
--- a/skeletons/OCTET_STRING.c
+++ b/skeletons/OCTET_STRING.c
@@ -38,7 +38,7 @@
 
 #define	ADVANCE(num_bytes)	do {	\
 		size_t num = num_bytes;	\
-		buf_ptr += num;		\
+		(char *)buf_ptr += num;	\
 		size -= num;		\
 		consumed_myself += num;	\
 	} while(0)
@@ -239,7 +239,7 @@
 		tlv_constr = BER_TLV_CONSTRUCTED(buf_ptr);
 
 		ll = ber_fetch_length(tlv_constr,
-				buf_ptr + tl, size - tl, &tlv_len);
+				(char *)buf_ptr + tl, size - tl, &tlv_len);
 		ASN_DEBUG("Got tag=%s, tl=%d, len=%d, ll=%d, {%d, %d}",
 			ber_tlv_tag_string(tlv_tag), tl, tlv_len, ll,
 			((uint8_t *)buf_ptr)[0],
@@ -325,7 +325,7 @@
 				 */
 				st->buf[st->size-1] &= 0xff << st->buf[0];
 
-				APPEND((buf_ptr+1), (len - 1));
+				APPEND(((char *)buf_ptr+1), (len - 1));
 				st->buf[0] = *(uint8_t *)buf_ptr;
 				sel->bits_chopped = 1;
 			} else {
@@ -457,7 +457,7 @@
 int
 OCTET_STRING_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel,
 	asn_app_consume_bytes_f *cb, void *app_key) {
-	static char h2c[16] = "0123456789ABCDEF";
+	static const char *h2c = "0123456789ABCDEF";
 	const OCTET_STRING_t *st = sptr;
 	char scratch[16 * 3 + 4];
 	char *p = scratch;
diff --git a/skeletons/RELATIVE-OID.c b/skeletons/RELATIVE-OID.c
index ab4c366..de0a7c3 100644
--- a/skeletons/RELATIVE-OID.c
+++ b/skeletons/RELATIVE-OID.c
@@ -65,7 +65,7 @@
 int
 RELATIVE_OID_get_arcs(RELATIVE_OID_t *roid,
 	void *arcs, unsigned int arc_type_size, unsigned int arc_slots) {
-	void *arcs_end = arcs + (arc_slots * arc_type_size);
+	void *arcs_end = (char *)arcs + (arc_slots * arc_type_size);
 	int num_arcs = 0;
 	int startn = 0;
 	int i;
@@ -86,7 +86,7 @@
 					i - startn + 1, 0,
 					arcs, arc_type_size))
 				return -1;
-			arcs += arc_type_size;
+			(char *)arcs += arc_type_size;
 			num_arcs++;
 		}
 
diff --git a/skeletons/UTCTime.c b/skeletons/UTCTime.c
index 2ebf433..c02e9f7 100644
--- a/skeletons/UTCTime.c
+++ b/skeletons/UTCTime.c
@@ -93,7 +93,7 @@
 		return -1;
 	}
 
-	gt.buf = buf;
+	gt.buf = (unsigned char *)buf;
 	gt.size = st->size + 2;
 	memcpy(gt.buf + 2, st->buf, st->size);
 	if(st->buf[0] > 0x35) {
diff --git a/skeletons/ber_decoder.c b/skeletons/ber_decoder.c
index 3ae678e..0fd9b26 100644
--- a/skeletons/ber_decoder.c
+++ b/skeletons/ber_decoder.c
@@ -7,7 +7,7 @@
 
 #define	ADVANCE(num_bytes)	do {			\
 		size_t num = num_bytes;			\
-		ptr += num;				\
+		(char *)ptr += num;			\
 		size -= num;				\
 		consumed_myself += num;			\
 	} while(0)
@@ -162,7 +162,7 @@
 		 * Fetch and process L from TLV.
 		 */
 		len_len = ber_fetch_length(tlv_constr,
-			ptr + tag_len, size - tag_len, &tlv_len);
+			(char *)ptr + tag_len, size - tag_len, &tlv_len);
 		switch(len_len) {
 		case -1: RETURN(RC_FAIL);
 		case 0: RETURN(RC_WMORE);
diff --git a/skeletons/ber_decoder.h b/skeletons/ber_decoder.h
index 8240270..4842adb 100644
--- a/skeletons/ber_decoder.h
+++ b/skeletons/ber_decoder.h
@@ -18,14 +18,14 @@
  * decoded bytes, hence provide a possibility, to fail with more diagnostics
  * (i.e., print the offending remainder of the buffer).
  */
+enum ber_dec_rval_code_e {
+	RC_OK,		/* Decoded successfully */
+	RC_WMORE,	/* More data expected, call again */
+	RC_FAIL,	/* Failure to decode data */
+};
 typedef struct ber_dec_rval_s {
-	enum {
-		RC_OK,		/* Decoded successfully */
-		RC_WMORE,	/* More data expected, call again */
-		RC_FAIL,	/* Failure to decode data */
-	} code;
-
-	size_t consumed;	/* Number of bytes consumed */
+	enum ber_dec_rval_code_e code;	/* Result code */
+	size_t consumed;		/* Number of bytes consumed */
 } ber_dec_rval_t;
 
 /*
diff --git a/skeletons/ber_tlv_length.c b/skeletons/ber_tlv_length.c
index 71850f0..af87236 100644
--- a/skeletons/ber_tlv_length.c
+++ b/skeletons/ber_tlv_length.c
@@ -87,7 +87,7 @@
 	 * Indefinite length!
 	 */
 	ASN_DEBUG("Skipping indefinite length");
-	for(skip = ll, ptr += ll, size -= ll;;) {
+	for(skip = ll, (char *)ptr += ll, size -= ll;;) {
 		ber_tlv_tag_t tag;
 
 		/* Fetch the tag */
@@ -95,7 +95,7 @@
 		if(tl <= 0) return tl;
 
 		ll = ber_skip_length(BER_TLV_CONSTRUCTED(ptr),
-			ptr + tl, size - tl);
+			(char *)ptr + tl, size - tl);
 		if(ll <= 0) return ll;
 
 		skip += tl + ll;
@@ -109,7 +109,7 @@
 		&& ((uint8_t *)ptr)[1] == 0)
 			return skip;
 
-		ptr  += tl + ll;
+		(char *)ptr  += tl + ll;
 		size -= tl + ll;
  	}
 
diff --git a/skeletons/ber_tlv_tag.c b/skeletons/ber_tlv_tag.c
index a6cccec..26d7122 100644
--- a/skeletons/ber_tlv_tag.c
+++ b/skeletons/ber_tlv_tag.c
@@ -30,7 +30,8 @@
 	 * Each octet contains 7 bits of useful information.
 	 * The MSB is 0 if it is the last octet of the tag.
 	 */
-	for(val = 0, ptr++, skipped = 2; skipped < size; ptr++, skipped++) {
+	for(val = 0, ((char *)ptr)++, skipped = 2;
+			skipped < size; ((char *)ptr)++, skipped++) {
 		unsigned oct = *(uint8_t *)ptr;
 		if(oct & 0x80) {
 			val = (val << 7) | (oct & 0x7F);
diff --git a/skeletons/constr_CHOICE.c b/skeletons/constr_CHOICE.c
index 7870a4c..a620b0c 100644
--- a/skeletons/constr_CHOICE.c
+++ b/skeletons/constr_CHOICE.c
@@ -31,7 +31,7 @@
  */
 #define	ADVANCE(num_bytes)	do {		\
 		size_t num = num_bytes;		\
-		ptr += num;			\
+		(char *)ptr += num;		\
 		size -= num;			\
 		if(ctx->left >= 0)		\
 			ctx->left -= num;	\
@@ -128,7 +128,7 @@
 	/*
 	 * Restore parsing context.
 	 */
-	ctx = (st + specs->ctx_offset);
+	ctx = (ber_dec_ctx_t *)((char *)st + specs->ctx_offset);
 	
 	/*
 	 * Start to parse where left previously
@@ -206,7 +206,7 @@
 
 				skip = ber_skip_length(
 					BER_TLV_CONSTRUCTED(ptr),
-					ptr + tag_len, LEFT - tag_len);
+					(char *)ptr + tag_len, LEFT - tag_len);
 
 				switch(skip) {
 				case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
@@ -468,7 +468,7 @@
 		return asn1_TYPE_outmost_tag(elm->type, memb_ptr,
 			elm->tag_mode, elm->tag);
 	} else {
-		return -1;
+		return (ber_tlv_tag_t)-1;
 	}
 }
 
diff --git a/skeletons/constr_SEQUENCE.c b/skeletons/constr_SEQUENCE.c
index 2a5bbd4..f273681 100644
--- a/skeletons/constr_SEQUENCE.c
+++ b/skeletons/constr_SEQUENCE.c
@@ -31,7 +31,7 @@
  */
 #define	ADVANCE(num_bytes)	do {		\
 		size_t num = num_bytes;		\
-		ptr += num;			\
+		(char *)ptr += num;		\
 		size -= num;			\
 		if(ctx->left >= 0)		\
 			ctx->left -= num;	\
@@ -138,7 +138,7 @@
 	/*
 	 * Restore parsing context.
 	 */
-	ctx = (st + specs->ctx_offset);
+	ctx = (ber_dec_ctx_t *)((char *)st + specs->ctx_offset);
 	
 	/*
 	 * Start to parse where left previously
@@ -336,7 +336,7 @@
 
 				skip = ber_skip_length(
 					BER_TLV_CONSTRUCTED(ptr),
-					ptr + tag_len, LEFT - tag_len);
+					(char *)ptr + tag_len, LEFT - tag_len);
 				ASN_DEBUG("Skip length %d in %s",
 					(int)skip, sd->name);
 				switch(skip) {
@@ -455,7 +455,7 @@
 
 			ll = ber_skip_length(
 				BER_TLV_CONSTRUCTED(ptr),
-				ptr + tl, LEFT - tl);
+				(char *)ptr + tl, LEFT - tl);
 			switch(ll) {
 			case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
 				/* Fall through */
diff --git a/skeletons/constr_SET.c b/skeletons/constr_SET.c
index dc699f5..2477a38 100644
--- a/skeletons/constr_SET.c
+++ b/skeletons/constr_SET.c
@@ -37,7 +37,7 @@
  */
 #define	ADVANCE(num_bytes)	do {		\
 		size_t num = num_bytes;		\
-		ptr += num;			\
+		(char *)ptr += num;		\
 		size -= num;			\
 		if(ctx->left >= 0)		\
 			ctx->left -= num;	\
@@ -128,7 +128,7 @@
 	/*
 	 * Restore parsing context.
 	 */
-	ctx = (st + specs->ctx_offset);
+	ctx = (ber_dec_ctx_t *)((char *)st + specs->ctx_offset);
 	
 	/*
 	 * Start to parse where left previously
@@ -256,7 +256,7 @@
 
 				skip = ber_skip_length(
 					BER_TLV_CONSTRUCTED(ptr),
-					ptr + tag_len, LEFT - tag_len);
+					(char *)ptr + tag_len, LEFT - tag_len);
 
 				switch(skip) {
 				case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
@@ -281,7 +281,7 @@
 		 * Check for duplications: must not overwrite
 		 * already decoded elements.
 		 */
-		if(ASN_SET_ISPRESENT2(st + specs->pres_offset, edx)) {
+		if(ASN_SET_ISPRESENT2((char *)st + specs->pres_offset, edx)) {
 			ASN_DEBUG("SET %s: Duplicate element %s (%d)",
 				sd->name, elements[edx].name, edx);
 			RETURN(RC_FAIL);
@@ -312,7 +312,7 @@
 				elements[edx].tag_mode);
 		switch(rval.code) {
 		case RC_OK:
-			ASN_SET_MKPRESENT(st + specs->pres_offset, edx);
+			ASN_SET_MKPRESENT((char *)st + specs->pres_offset, edx);
 			break;
 		case RC_WMORE: /* More data expected */
 			if(!SIZE_VIOLATION) {
@@ -378,7 +378,7 @@
 
 			ll = ber_skip_length(
 				BER_TLV_CONSTRUCTED(ptr),
-				ptr + tl, LEFT - tl);
+				(char *)ptr + tl, LEFT - tl);
 			switch(ll) {
 			case 0: if(!SIZE_VIOLATION) RETURN(RC_WMORE);
 				/* Fall through */
@@ -398,7 +398,7 @@
 			unsigned int midx, pres, must;
 
 			midx = edx/(8 * sizeof(specs->_mandatory_elements[0]));
-			pres = ((unsigned int *)(st+specs->pres_offset))[midx];
+			pres = ((unsigned int *)((char *)st+specs->pres_offset))[midx];
 			must = ntohl(specs->_mandatory_elements[midx]);
 
 			if((pres & must) == must) {
diff --git a/skeletons/constr_SET_OF.c b/skeletons/constr_SET_OF.c
index ff628d2..84fb895 100644
--- a/skeletons/constr_SET_OF.c
+++ b/skeletons/constr_SET_OF.c
@@ -31,7 +31,7 @@
  */
 #define	ADVANCE(num_bytes)	do {		\
 		size_t num = num_bytes;		\
-		ptr += num;			\
+		(char *)ptr += num;		\
 		size -= num;			\
 		if(ctx->left >= 0)		\
 			ctx->left -= num;	\
@@ -95,7 +95,7 @@
 	/*
 	 * Restore parsing context.
 	 */
-	ctx = (st + specs->ctx_offset);
+	ctx = (ber_dec_ctx_t *)((char *)st + specs->ctx_offset);
 	
 	/*
 	 * Start to parse where left previously
diff --git a/skeletons/der_encoder.c b/skeletons/der_encoder.c
index dc547a2..9ed91ce 100644
--- a/skeletons/der_encoder.c
+++ b/skeletons/der_encoder.c
@@ -4,6 +4,7 @@
  */
 #include <constr_TYPE.h>
 #include <assert.h>
+#include <errno.h>
 
 static ssize_t der_write_TL(ber_tlv_tag_t tag, ber_tlv_len_t len,
 	asn_app_consume_bytes_f *cb, void *app_key, int constructed);