added stack control to PER


git-svn-id: https://asn1c.svn.sourceforge.net/svnroot/asn1c/trunk@1022 59561ff5-6e30-0410-9f3c-9617f08c8826
diff --git a/skeletons/asn_internal.h b/skeletons/asn_internal.h
index 4a86141..89c9c41 100644
--- a/skeletons/asn_internal.h
+++ b/skeletons/asn_internal.h
@@ -82,6 +82,28 @@
 		if(cb("    ", 4, app_key) < 0) return -1;		\
 } while(0)
 
+/*
+ * Check stack against overflow, if limit is set.
+ */
+#define	_ASN_DEFAULT_STACK_MAX	(30000)
+static inline int
+_ASN_STACK_OVERFLOW_CHECK(asn_codec_ctx_t *ctx) {
+	if(ctx && ctx->max_stack_size) {
+
+		/* ctx MUST be allocated on the stack */
+		ptrdiff_t usedstack = ((char *)ctx - (char *)&ctx);
+		if(usedstack > 0) usedstack = -usedstack; /* grows up! */
+
+		/* double negative required to avoid int wrap-around */
+		if(usedstack < -(ptrdiff_t)ctx->max_stack_size) {
+			ASN_DEBUG("Stack limit %ld reached",
+				(long)ctx->max_stack_size);
+			return -1;
+		}
+	}
+	return 0;
+}
+
 #ifdef	__cplusplus
 }
 #endif