inner structures tagging fix. see 0.9.5 comment in ChangeLog


git-svn-id: https://asn1c.svn.sourceforge.net/svnroot/asn1c/trunk@311 59561ff5-6e30-0410-9f3c-9617f08c8826
diff --git a/skeletons/BIT_STRING.c b/skeletons/BIT_STRING.c
index 7f1aa09..a43a22a 100644
--- a/skeletons/BIT_STRING.c
+++ b/skeletons/BIT_STRING.c
@@ -21,6 +21,9 @@
 	asn1_DEF_BIT_STRING_tags,
 	sizeof(asn1_DEF_BIT_STRING_tags)
 	  / sizeof(asn1_DEF_BIT_STRING_tags[0]),
+	asn1_DEF_BIT_STRING_tags,	/* Same as above */
+	sizeof(asn1_DEF_BIT_STRING_tags)
+	  / sizeof(asn1_DEF_BIT_STRING_tags[0]),
 	-1,	/* Both ways are fine */
 	0, 0,	/* No members */
 	(void *)-1	/* Special indicator that this is a BIT STRING */
diff --git a/skeletons/BMPString.c b/skeletons/BMPString.c
index 87aacea..3e779cc 100644
--- a/skeletons/BMPString.c
+++ b/skeletons/BMPString.c
@@ -8,7 +8,8 @@
  * BMPString basic type description.
  */
 static ber_tlv_tag_t asn1_DEF_BMPString_tags[] = {
-	(ASN_TAG_CLASS_UNIVERSAL | (30 << 2))
+	(ASN_TAG_CLASS_UNIVERSAL | (30 << 2)),	/* [UNIVERSAL 30] IMPLICIT ...*/
+	(ASN_TAG_CLASS_UNIVERSAL | (4 << 2))	/* ... OCTET STRING */
 };
 asn1_TYPE_descriptor_t asn1_DEF_BMPString = {
 	"BMPString",
@@ -20,6 +21,9 @@
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_BMPString_tags,
 	sizeof(asn1_DEF_BMPString_tags)
+	  / sizeof(asn1_DEF_BMPString_tags[0]) - 1,
+	asn1_DEF_BMPString_tags,
+	sizeof(asn1_DEF_BMPString_tags)
 	  / sizeof(asn1_DEF_BMPString_tags[0]),
 	-1,	/* Both ways are fine */
 	0, 0,	/* No members */
diff --git a/skeletons/BOOLEAN.c b/skeletons/BOOLEAN.c
index b7b4795..5ba2e4f 100644
--- a/skeletons/BOOLEAN.c
+++ b/skeletons/BOOLEAN.c
@@ -19,7 +19,9 @@
 	BOOLEAN_free,
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_BOOLEAN_tags,
-	sizeof(asn1_DEF_BOOLEAN_tags)/sizeof(asn1_DEF_BOOLEAN_tags[0]),
+	sizeof(asn1_DEF_BOOLEAN_tags) / sizeof(asn1_DEF_BOOLEAN_tags[0]),
+	asn1_DEF_BOOLEAN_tags,	/* Same as above */
+	sizeof(asn1_DEF_BOOLEAN_tags) / sizeof(asn1_DEF_BOOLEAN_tags[0]),
 	0,	/* Always in primitive form */
 	0, 0,	/* No members */
 	0	/* No specifics */
diff --git a/skeletons/ENUMERATED.c b/skeletons/ENUMERATED.c
index 9c8db29..ed908b5 100644
--- a/skeletons/ENUMERATED.c
+++ b/skeletons/ENUMERATED.c
@@ -19,7 +19,9 @@
 	INTEGER_free,			/* Implemented in terms of INTEGER */
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_ENUMERATED_tags,
-	sizeof(asn1_DEF_ENUMERATED_tags)/sizeof(asn1_DEF_ENUMERATED_tags[0]),
+	sizeof(asn1_DEF_ENUMERATED_tags) / sizeof(asn1_DEF_ENUMERATED_tags[0]),
+	asn1_DEF_ENUMERATED_tags,	/* Same as above */
+	sizeof(asn1_DEF_ENUMERATED_tags) / sizeof(asn1_DEF_ENUMERATED_tags[0]),
 	0,	/* Primitive */
 	0, 0,	/* No members */
 	0	/* No specifics */
diff --git a/skeletons/GeneralString.c b/skeletons/GeneralString.c
index feef7cc..dfca2ca 100644
--- a/skeletons/GeneralString.c
+++ b/skeletons/GeneralString.c
@@ -8,7 +8,8 @@
  * GeneralString basic type description.
  */
 static ber_tlv_tag_t asn1_DEF_GeneralString_tags[] = {
-	(ASN_TAG_CLASS_UNIVERSAL | (27 << 2))
+	(ASN_TAG_CLASS_UNIVERSAL | (27 << 2)),	/* [UNIVERSAL 27] IMPLICIT ...*/
+	(ASN_TAG_CLASS_UNIVERSAL | (4 << 2))	/* ... OCTET STRING */
 };
 asn1_TYPE_descriptor_t asn1_DEF_GeneralString = {
 	"GeneralString",
@@ -20,6 +21,9 @@
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_GeneralString_tags,
 	sizeof(asn1_DEF_GeneralString_tags)
+	  / sizeof(asn1_DEF_GeneralString_tags[0]) - 1,
+	asn1_DEF_GeneralString_tags,
+	sizeof(asn1_DEF_GeneralString_tags)
 	  / sizeof(asn1_DEF_GeneralString_tags[0]),
 	-1,	/* Both ways are fine */
 	0, 0,	/* No members */
diff --git a/skeletons/GeneralizedTime.c b/skeletons/GeneralizedTime.c
index c66c314..6a89b2d 100644
--- a/skeletons/GeneralizedTime.c
+++ b/skeletons/GeneralizedTime.c
@@ -85,6 +85,9 @@
 	asn1_DEF_GeneralizedTime_tags,
 	sizeof(asn1_DEF_GeneralizedTime_tags)
 	  / sizeof(asn1_DEF_GeneralizedTime_tags[0]),
+	asn1_DEF_GeneralizedTime_tags,	/* Same as above */
+	sizeof(asn1_DEF_GeneralizedTime_tags)
+	  / sizeof(asn1_DEF_GeneralizedTime_tags[0]),
 	-1,	/* Both ways are fine */
 	0, 0,	/* No members */
 	0	/* No specifics */
diff --git a/skeletons/GraphicString.c b/skeletons/GraphicString.c
index f6d0ae9..ceb247d 100644
--- a/skeletons/GraphicString.c
+++ b/skeletons/GraphicString.c
@@ -8,7 +8,8 @@
  * GraphicString basic type description.
  */
 static ber_tlv_tag_t asn1_DEF_GraphicString_tags[] = {
-	(ASN_TAG_CLASS_UNIVERSAL | (25 << 2))
+	(ASN_TAG_CLASS_UNIVERSAL | (25 << 2)),	/* [UNIVERSAL 25] IMPLICIT ...*/
+	(ASN_TAG_CLASS_UNIVERSAL | (4 << 2))	/* ... OCTET STRING */
 };
 asn1_TYPE_descriptor_t asn1_DEF_GraphicString = {
 	"GraphicString",
@@ -20,6 +21,9 @@
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_GraphicString_tags,
 	sizeof(asn1_DEF_GraphicString_tags)
+	  / sizeof(asn1_DEF_GraphicString_tags[0]) - 1,
+	asn1_DEF_GraphicString_tags,
+	sizeof(asn1_DEF_GraphicString_tags)
 	  / sizeof(asn1_DEF_GraphicString_tags[0]),
 	-1,	/* Both ways are fine */
 	0, 0,	/* No members */
diff --git a/skeletons/IA5String.c b/skeletons/IA5String.c
index 33f38a6..9fc308b 100644
--- a/skeletons/IA5String.c
+++ b/skeletons/IA5String.c
@@ -8,7 +8,8 @@
  * IA5String basic type description.
  */
 static ber_tlv_tag_t asn1_DEF_IA5String_tags[] = {
-	(ASN_TAG_CLASS_UNIVERSAL | (22 << 2))
+	(ASN_TAG_CLASS_UNIVERSAL | (22 << 2)),	/* [UNIVERSAL 22] IMPLICIT ...*/
+	(ASN_TAG_CLASS_UNIVERSAL | (4 << 2))	/* ... OCTET STRING */
 };
 asn1_TYPE_descriptor_t asn1_DEF_IA5String = {
 	"IA5String",
@@ -20,6 +21,9 @@
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_IA5String_tags,
 	sizeof(asn1_DEF_IA5String_tags)
+	  / sizeof(asn1_DEF_IA5String_tags[0]) - 1,
+	asn1_DEF_IA5String_tags,
+	sizeof(asn1_DEF_IA5String_tags)
 	  / sizeof(asn1_DEF_IA5String_tags[0]),
 	-1,	/* Both ways are fine */
 	0, 0,	/* No members */
diff --git a/skeletons/INTEGER.c b/skeletons/INTEGER.c
index 14e91c6..5bfcf1f 100644
--- a/skeletons/INTEGER.c
+++ b/skeletons/INTEGER.c
@@ -21,7 +21,9 @@
 	INTEGER_free,
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_INTEGER_tags,
-	sizeof(asn1_DEF_INTEGER_tags)/sizeof(asn1_DEF_INTEGER_tags[0]),
+	sizeof(asn1_DEF_INTEGER_tags) / sizeof(asn1_DEF_INTEGER_tags[0]),
+	asn1_DEF_INTEGER_tags,	/* Same as above */
+	sizeof(asn1_DEF_INTEGER_tags) / sizeof(asn1_DEF_INTEGER_tags[0]),
 	0,	/* Always in primitive form */
 	0, 0,	/* No members */
 	0	/* No specifics */
diff --git a/skeletons/ISO646String.c b/skeletons/ISO646String.c
index 7ce5496..8a99cbc 100644
--- a/skeletons/ISO646String.c
+++ b/skeletons/ISO646String.c
@@ -8,7 +8,8 @@
  * ISO646String basic type description.
  */
 static ber_tlv_tag_t asn1_DEF_ISO646String_tags[] = {
-	(ASN_TAG_CLASS_UNIVERSAL | (26 << 2))
+	(ASN_TAG_CLASS_UNIVERSAL | (26 << 2)),	/* [UNIVERSAL 26] IMPLICIT ...*/
+	(ASN_TAG_CLASS_UNIVERSAL | (4 << 2))	/* ... OCTET STRING */
 };
 asn1_TYPE_descriptor_t asn1_DEF_ISO646String = {
 	"ISO646String",
@@ -20,6 +21,9 @@
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_ISO646String_tags,
 	sizeof(asn1_DEF_ISO646String_tags)
+	  / sizeof(asn1_DEF_ISO646String_tags[0]) - 1,
+	asn1_DEF_ISO646String_tags,
+	sizeof(asn1_DEF_ISO646String_tags)
 	  / sizeof(asn1_DEF_ISO646String_tags[0]),
 	-1,	/* Both ways are fine */
 	0, 0,	/* No members */
diff --git a/skeletons/NULL.c b/skeletons/NULL.c
index 42afa7e..36796b8 100644
--- a/skeletons/NULL.c
+++ b/skeletons/NULL.c
@@ -20,7 +20,9 @@
 	BOOLEAN_free,
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_NULL_tags,
-	sizeof(asn1_DEF_NULL_tags)/sizeof(asn1_DEF_NULL_tags[0]),
+	sizeof(asn1_DEF_NULL_tags) / sizeof(asn1_DEF_NULL_tags[0]),
+	asn1_DEF_NULL_tags,	/* Same as above */
+	sizeof(asn1_DEF_NULL_tags) / sizeof(asn1_DEF_NULL_tags[0]),
 	0,	/* Always in primitive form */
 	0, 0,	/* No members */
 	0	/* No specifics */
diff --git a/skeletons/NativeEnumerated.c b/skeletons/NativeEnumerated.c
index 9c4fd8f..53ce501 100644
--- a/skeletons/NativeEnumerated.c
+++ b/skeletons/NativeEnumerated.c
@@ -26,7 +26,9 @@
 	NativeInteger_free,
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_NativeEnumerated_tags,
-	sizeof(asn1_DEF_NativeEnumerated_tags)/sizeof(asn1_DEF_NativeEnumerated_tags[0]),
+	sizeof(asn1_DEF_NativeEnumerated_tags) / sizeof(asn1_DEF_NativeEnumerated_tags[0]),
+	asn1_DEF_NativeEnumerated_tags,	/* Same as above */
+	sizeof(asn1_DEF_NativeEnumerated_tags) / sizeof(asn1_DEF_NativeEnumerated_tags[0]),
 	0,	/* Always in primitive form */
 	0, 0,	/* No members */
 	0	/* No specifics */
diff --git a/skeletons/NativeInteger.c b/skeletons/NativeInteger.c
index 60321cc..98fee96 100644
--- a/skeletons/NativeInteger.c
+++ b/skeletons/NativeInteger.c
@@ -28,7 +28,9 @@
 	NativeInteger_free,
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_NativeInteger_tags,
-	sizeof(asn1_DEF_NativeInteger_tags)/sizeof(asn1_DEF_NativeInteger_tags[0]),
+	sizeof(asn1_DEF_NativeInteger_tags) / sizeof(asn1_DEF_NativeInteger_tags[0]),
+	asn1_DEF_NativeInteger_tags,	/* Same as above */
+	sizeof(asn1_DEF_NativeInteger_tags) / sizeof(asn1_DEF_NativeInteger_tags[0]),
 	0,	/* Always in primitive form */
 	0, 0,	/* No members */
 	0	/* No specifics */
diff --git a/skeletons/NumericString.c b/skeletons/NumericString.c
index a70bb5d..a23e39f 100644
--- a/skeletons/NumericString.c
+++ b/skeletons/NumericString.c
@@ -8,7 +8,8 @@
  * NumericString basic type description.
  */
 static ber_tlv_tag_t asn1_DEF_NumericString_tags[] = {
-	(ASN_TAG_CLASS_UNIVERSAL | (18 << 2))
+	(ASN_TAG_CLASS_UNIVERSAL | (18 << 2)),	/* [UNIVERSAL 18] IMPLICIT ...*/
+	(ASN_TAG_CLASS_UNIVERSAL | (4 << 2))	/* ... OCTET STRING */
 };
 asn1_TYPE_descriptor_t asn1_DEF_NumericString = {
 	"NumericString",
@@ -20,6 +21,9 @@
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_NumericString_tags,
 	sizeof(asn1_DEF_NumericString_tags)
+	  / sizeof(asn1_DEF_NumericString_tags[0]) - 1,
+	asn1_DEF_NumericString_tags,
+	sizeof(asn1_DEF_NumericString_tags)
 	  / sizeof(asn1_DEF_NumericString_tags[0]),
 	-1,	/* Both ways are fine */
 	0, 0,	/* No members */
diff --git a/skeletons/OBJECT_IDENTIFIER.c b/skeletons/OBJECT_IDENTIFIER.c
index a44349e..c55410d 100644
--- a/skeletons/OBJECT_IDENTIFIER.c
+++ b/skeletons/OBJECT_IDENTIFIER.c
@@ -24,6 +24,9 @@
 	asn1_DEF_OBJECT_IDENTIFIER_tags,
 	sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags)
 	    / sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags[0]),
+	asn1_DEF_OBJECT_IDENTIFIER_tags,	/* Same as above */
+	sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags)
+	    / sizeof(asn1_DEF_OBJECT_IDENTIFIER_tags[0]),
 	0,	/* Always in primitive form */
 	0, 0,	/* No members */
 	0	/* No specifics */
@@ -98,7 +101,7 @@
 
 int
 OBJECT_IDENTIFIER_get_single_arc(uint8_t *arcbuf, unsigned int arclen, signed int add, void *rvbuf, unsigned int rvsize) {
-	unsigned LE = 1;	/* Little endian (x86) */
+	unsigned LE __attribute__ ((unused)) = 1; /* Little endian (x86) */
 	uint8_t *arcend = arcbuf + arclen;	/* End of arc */
 	void *rvstart = rvbuf;	/* Original start of the value buffer */
 	unsigned int cache = 0;	/* No more than 14 significant bits */
diff --git a/skeletons/OCTET_STRING.c b/skeletons/OCTET_STRING.c
index 6589334..e0af730 100644
--- a/skeletons/OCTET_STRING.c
+++ b/skeletons/OCTET_STRING.c
@@ -1,5 +1,5 @@
 /*-
- * Copyright (c) 2003 Lev Walkin <vlm@lionet.info>. All rights reserved.
+ * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
  * Redistribution and modifications are permitted subject to BSD license.
  */
 #include <OCTET_STRING.h>
@@ -23,6 +23,9 @@
 	asn1_DEF_OCTET_STRING_tags,
 	sizeof(asn1_DEF_OCTET_STRING_tags)
 	  / sizeof(asn1_DEF_OCTET_STRING_tags[0]),
+	asn1_DEF_OCTET_STRING_tags,	/* Same as above */
+	sizeof(asn1_DEF_OCTET_STRING_tags)
+	  / sizeof(asn1_DEF_OCTET_STRING_tags[0]),
 	-1,	/* Both ways are fine (primitive and constructed) */
 	0, 0,	/* No members */
 	0	/* No specifics */
@@ -54,6 +57,7 @@
 		size_t _ns = ctx->step;	/* Allocated */			\
 		if(_ns <= (size_t)(st->size + _bs)) {			\
 			void *ptr;					\
+			/* Be nice and round to the memory allocator */	\
 			do { _ns = _ns ? _ns<<2 : 16; }			\
 			    while(_ns <= (size_t)(st->size + _bs));	\
 			ptr = REALLOC(st->buf, _ns);			\
@@ -81,6 +85,7 @@
  */
 struct _stack_el {
 	ber_tlv_len_t	left;	/* What's left to read */
+	int	cont_level;	/* Depth of subcontainment */
 	int	want_nulls;	/* Want null "end of content" octets? */
 	int	bits_chopped;	/* Flag in BIT STRING mode */
 	struct _stack_el *prev;
@@ -95,17 +100,23 @@
 _add_stack_el(struct _stack *st) {
 	struct _stack_el *nel;
 
+	/*
+	 * Reuse the old stack frame or allocate a new one.
+	 */
 	if(st->cur_ptr && st->cur_ptr->next) {
 		nel = st->cur_ptr->next;
 		nel->left = 0;
 		nel->want_nulls = 0;
 		nel->bits_chopped = 0;
+		/* Retain nel->cont_level, it's correct. */
 	} else {
 		(void *)nel = CALLOC(1, sizeof(struct _stack_el));
 		if(nel == NULL)
 			return NULL;
 	
 		if(st->tail) {
+			/* Increase a subcontainment depth */
+			nel->cont_level = st->tail->cont_level + 1;
 			st->tail->next = nel;
 		}
 		nel->prev = st->tail;
@@ -136,13 +147,16 @@
 	OCTET_STRING_t *st = (OCTET_STRING_t *)*os_structure;
 	ber_dec_rval_t rval;
 	ber_dec_ctx_t *ctx;
-	ber_tlv_tag_t terminal_tag;	/* Inner tag for constructed types */
 	ssize_t consumed_myself = 0;
 	struct _stack *stck;	/* A stack structure */
 	struct _stack_el *sel;	/* Stack element */
 	int tlv_constr;
-	int is_bit_str = 0;	/* See below under switch(td->specifics) */
-	int is_ANY_type = 0;	/* See below under switch(td->specifics) */
+	enum type_type_e {
+		_TT_GENERIC	= 0,	/* Just a random OCTET STRING */
+		_TT_BIT_STRING	= -1,	/* BIT STRING type, a special case */
+		_TT_ANY		= 1,	/* ANY type, a special case too */
+	} type_type
+		= (enum type_type_e)(int)td->specifics;	/* An ugly hack */
 
 	ASN_DEBUG("Decoding %s as %s (%ld)",
 		td->name, "OCTET STRING", (long)size);
@@ -159,25 +173,6 @@
 	/* Restore parsing context */
 	ctx = &st->_ber_dec_ctx;
 
-	switch((int)td->specifics) {
-	case 0:
-		terminal_tag = asn1_DEF_OCTET_STRING_tags[0];	/* [U4] */
-		break;
-	case -1:	/* BIT STRING */
-		/*
-		 * This is some sort of a hack.
-		 * The OCTET STRING decoder is being used in BIT STRING mode.
-		 */
-		is_bit_str = 1;
-		terminal_tag = ASN_TAG_CLASS_UNIVERSAL | (3 << 2);
-		break;
-	default:	/* Just in case; fall through */
-	case  1:	/* ANY type */
-		is_ANY_type = 1;
-		terminal_tag = -1;
-		break;
-	}
-
 	switch(ctx->phase) {
 	case 0:
 		/*
@@ -212,7 +207,7 @@
 					stck->cur_ptr->left,
 					stck->cur_ptr->want_nulls);
 #endif
-				if(is_bit_str) {
+				if(type_type == _TT_BIT_STRING) {
 					/* Number of meaningless tail bits */
 					APPEND("\0", 1);
 				}
@@ -224,7 +219,8 @@
 			 * Jump into stackless primitive decoding.
 			 */
 			_CH_PHASE(ctx, 3);
-			if(is_ANY_type) APPEND(buf_ptr, rval.consumed);
+			if(type_type == _TT_ANY)
+				APPEND(buf_ptr, rval.consumed);
 			ADVANCE(rval.consumed);
 			goto phase3;
 		}
@@ -241,6 +237,7 @@
 	  do {
 		ber_tlv_tag_t tlv_tag;
 		ber_tlv_len_t tlv_len;
+		ber_tlv_tag_t expected_tag;
 		ssize_t tl, ll;
 
 		ASN_DEBUG("fetch tag(size=%d), %sstack, left=%d, want0=%d",
@@ -271,7 +268,7 @@
 			&& ((uint8_t *)buf_ptr)[1] == 0)
 		{
 			ADVANCE(2);
-			if(is_ANY_type) APPEND("\0\0", 2);
+			if(type_type == _TT_ANY) APPEND("\0\0", 2);
 
 			ASN_DEBUG("Eat EOC; wn=%d--", sel->want_nulls);
 
@@ -294,8 +291,38 @@
 			}
 
 			continue;
-		} else if(tlv_tag != terminal_tag
-				&& terminal_tag != (ber_tlv_tag_t)-1) {
+		}
+
+		/*
+		 * Set up expected tags,
+		 * depending on ASN.1 type being decoded.
+		 */
+		switch(type_type) {
+		case _TT_BIT_STRING:
+			/* X.690: 8.6.4.1, NOTE 2 */
+			/* Fall through */
+		case _TT_GENERIC:
+		default:
+			if(sel) {
+				int level = sel->cont_level;
+				if(level < td->all_tags_count) {
+					expected_tag = td->all_tags[level];
+					break;
+				} else if(td->all_tags_count) {
+					expected_tag = td->all_tags
+						[td->all_tags_count - 1];
+					break;
+				}
+				/* else, Fall through */
+			}
+			/* Fall through */
+		case _TT_ANY:
+			expected_tag = tlv_tag;
+			break;
+		}
+
+
+		if(tlv_tag != expected_tag) {
 			char buf[2][32];
 			ber_tlv_tag_snprint(tlv_tag,
 				buf[0], sizeof(buf[0]));
@@ -313,17 +340,18 @@
 		if(sel) {
 			sel->want_nulls = (tlv_len==-1);
 			sel->left = tlv_len;
-			ASN_DEBUG("+EXPECT2 left=%d wn=%d",
-				sel->left, sel->want_nulls);
+			ASN_DEBUG("+EXPECT2 left=%d wn=%d, clvl=%d",
+				sel->left, sel->want_nulls, sel->cont_level);
 		} else {
 			RETURN(RC_FAIL);
 		}
 
-		if(is_ANY_type) APPEND(buf_ptr, tl + ll);
+		if(type_type == _TT_ANY) APPEND(buf_ptr, tl + ll);
 		ADVANCE(tl+ll);
 	  } while(tlv_constr);
 		if(sel == NULL) {
 			/* Finished operation, "phase out" */
+			ASN_DEBUG("Phase out");
 			_CH_PHASE(ctx, +3);
 			break;
 		}
@@ -343,7 +371,8 @@
 		len = ((ber_tlv_len_t)size < sel->left)
 				? (ber_tlv_len_t)size : sel->left;
 		if(len > 0) {
-			if(is_bit_str && sel->bits_chopped == 0) {
+			if(type_type == _TT_BIT_STRING
+			&& sel->bits_chopped == 0) {
 				/*
 				 * Finalize the previous chunk:
 				 * strip down unused bits.
@@ -394,7 +423,7 @@
 	/*
 	 * BIT STRING-specific processing.
 	 */
-	if(is_bit_str && st->size >= 2) {
+	if(type_type == _TT_BIT_STRING && st->size >= 2) {
 		/* Finalize BIT STRING: zero out unused bits. */
 		st->buf[st->size-1] &= 0xff << st->buf[0];
 	}
diff --git a/skeletons/ObjectDescriptor.c b/skeletons/ObjectDescriptor.c
index 1f82e2b..5017918 100644
--- a/skeletons/ObjectDescriptor.c
+++ b/skeletons/ObjectDescriptor.c
@@ -8,7 +8,8 @@
  * ObjectDescriptor basic type description.
  */
 static ber_tlv_tag_t asn1_DEF_ObjectDescriptor_tags[] = {
-	(ASN_TAG_CLASS_UNIVERSAL | (7 << 2))
+	(ASN_TAG_CLASS_UNIVERSAL | (7 << 2)),	/* [UNIVERSAL 7] IMPLICIT ... */
+	(ASN_TAG_CLASS_UNIVERSAL | (4 << 2))	/* ... OCTET STRING */
 };
 asn1_TYPE_descriptor_t asn1_DEF_ObjectDescriptor = {
 	"ObjectDescriptor",
@@ -20,6 +21,9 @@
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_ObjectDescriptor_tags,
 	sizeof(asn1_DEF_ObjectDescriptor_tags)
+	  / sizeof(asn1_DEF_ObjectDescriptor_tags[0]) - 1,
+	asn1_DEF_ObjectDescriptor_tags,
+	sizeof(asn1_DEF_ObjectDescriptor_tags)
 	  / sizeof(asn1_DEF_ObjectDescriptor_tags[0]),
 	-1,	/* Both ways are fine */
 	0, 0,	/* No members */
diff --git a/skeletons/PrintableString.c b/skeletons/PrintableString.c
index 89cbf8d..c37de49 100644
--- a/skeletons/PrintableString.c
+++ b/skeletons/PrintableString.c
@@ -8,7 +8,8 @@
  * PrintableString basic type description.
  */
 static ber_tlv_tag_t asn1_DEF_PrintableString_tags[] = {
-	(ASN_TAG_CLASS_UNIVERSAL | (19 << 2))
+	(ASN_TAG_CLASS_UNIVERSAL | (19 << 2)),	/* [UNIVERSAL 19] IMPLICIT ...*/
+	(ASN_TAG_CLASS_UNIVERSAL | (4 << 2))	/* ... OCTET STRING */
 };
 asn1_TYPE_descriptor_t asn1_DEF_PrintableString = {
 	"PrintableString",
@@ -20,6 +21,9 @@
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_PrintableString_tags,
 	sizeof(asn1_DEF_PrintableString_tags)
+	  / sizeof(asn1_DEF_PrintableString_tags[0]) - 1,
+	asn1_DEF_PrintableString_tags,
+	sizeof(asn1_DEF_PrintableString_tags)
 	  / sizeof(asn1_DEF_PrintableString_tags[0]),
 	-1,	/* Both ways are fine */
 	0, 0,	/* No members */
diff --git a/skeletons/RELATIVE-OID.c b/skeletons/RELATIVE-OID.c
index f1948f8..bee795c 100644
--- a/skeletons/RELATIVE-OID.c
+++ b/skeletons/RELATIVE-OID.c
@@ -24,6 +24,9 @@
 	asn1_DEF_RELATIVE_OID_tags,
 	sizeof(asn1_DEF_RELATIVE_OID_tags)
 	    / sizeof(asn1_DEF_RELATIVE_OID_tags[0]),
+	asn1_DEF_RELATIVE_OID_tags,	/* Same as above */
+	sizeof(asn1_DEF_RELATIVE_OID_tags)
+	    / sizeof(asn1_DEF_RELATIVE_OID_tags[0]),
 	0,	/* Always in primitive form */
 	0, 0,	/* No members */
 	0	/* No specifics */
diff --git a/skeletons/T61String.c b/skeletons/T61String.c
index 5226374..b306a5b 100644
--- a/skeletons/T61String.c
+++ b/skeletons/T61String.c
@@ -8,7 +8,8 @@
  * T61String basic type description.
  */
 static ber_tlv_tag_t asn1_DEF_T61String_tags[] = {
-	(ASN_TAG_CLASS_UNIVERSAL | (20 << 2))
+	(ASN_TAG_CLASS_UNIVERSAL | (20 << 2)),	/* [UNIVERSAL 20] IMPLICIT ...*/
+	(ASN_TAG_CLASS_UNIVERSAL | (4 << 2))	/* ... OCTET STRING */
 };
 asn1_TYPE_descriptor_t asn1_DEF_T61String = {
 	"T61String",
@@ -20,6 +21,9 @@
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_T61String_tags,
 	sizeof(asn1_DEF_T61String_tags)
+	  / sizeof(asn1_DEF_T61String_tags[0]) - 1,
+	asn1_DEF_T61String_tags,
+	sizeof(asn1_DEF_T61String_tags)
 	  / sizeof(asn1_DEF_T61String_tags[0]),
 	-1,	/* Both ways are fine */
 	0, 0,	/* No members */
diff --git a/skeletons/TeletexString.c b/skeletons/TeletexString.c
index b46caed..09f3eb6 100644
--- a/skeletons/TeletexString.c
+++ b/skeletons/TeletexString.c
@@ -8,7 +8,8 @@
  * TeletexString basic type description.
  */
 static ber_tlv_tag_t asn1_DEF_TeletexString_tags[] = {
-	(ASN_TAG_CLASS_UNIVERSAL | (20 << 2))
+	(ASN_TAG_CLASS_UNIVERSAL | (20 << 2)),	/* [UNIVERSAL 20] IMPLICIT ...*/
+	(ASN_TAG_CLASS_UNIVERSAL | (4 << 2)),	/* ... OCTET STRING */
 };
 asn1_TYPE_descriptor_t asn1_DEF_TeletexString = {
 	"TeletexString",
@@ -20,6 +21,9 @@
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_TeletexString_tags,
 	sizeof(asn1_DEF_TeletexString_tags)
+	  / sizeof(asn1_DEF_TeletexString_tags[0]) - 1,
+	asn1_DEF_TeletexString_tags,
+	sizeof(asn1_DEF_TeletexString_tags)
 	  / sizeof(asn1_DEF_TeletexString_tags[0]),
 	-1,	/* Both ways are fine */
 	0, 0,	/* No members */
diff --git a/skeletons/UTCTime.c b/skeletons/UTCTime.c
index c574e4e..029a43e 100644
--- a/skeletons/UTCTime.c
+++ b/skeletons/UTCTime.c
@@ -27,6 +27,9 @@
 	asn1_DEF_UTCTime_tags,
 	sizeof(asn1_DEF_UTCTime_tags)
 	  / sizeof(asn1_DEF_UTCTime_tags[0]),
+	asn1_DEF_UTCTime_tags,	/* Same as above */
+	sizeof(asn1_DEF_UTCTime_tags)
+	  / sizeof(asn1_DEF_UTCTime_tags[0]),
 	-1,	/* Both ways are fine */
 	0, 0,	/* No members */
 	0	/* No specifics */
diff --git a/skeletons/UTF8String.c b/skeletons/UTF8String.c
index 384600a..40fef39 100644
--- a/skeletons/UTF8String.c
+++ b/skeletons/UTF8String.c
@@ -8,7 +8,8 @@
  * UTF8String basic type description.
  */
 static ber_tlv_tag_t asn1_DEF_UTF8String_tags[] = {
-	(ASN_TAG_CLASS_UNIVERSAL | (12 << 2))
+	(ASN_TAG_CLASS_UNIVERSAL | (12 << 2)),	/* [UNIVERSAL 12] IMPLICIT ...*/
+	(ASN_TAG_CLASS_UNIVERSAL | (4 << 2)),	/* ... OCTET STRING */
 };
 asn1_TYPE_descriptor_t asn1_DEF_UTF8String = {
 	"UTF8String",
@@ -20,6 +21,9 @@
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_UTF8String_tags,
 	sizeof(asn1_DEF_UTF8String_tags)
+	  / sizeof(asn1_DEF_UTF8String_tags[0]) - 1,
+	asn1_DEF_UTF8String_tags,
+	sizeof(asn1_DEF_UTF8String_tags)
 	  / sizeof(asn1_DEF_UTF8String_tags[0]),
 	-1,	/* Both ways are fine */
 	0, 0,	/* No members */
diff --git a/skeletons/UniversalString.c b/skeletons/UniversalString.c
index 35e7bea..1498896 100644
--- a/skeletons/UniversalString.c
+++ b/skeletons/UniversalString.c
@@ -8,7 +8,8 @@
  * UniversalString basic type description.
  */
 static ber_tlv_tag_t asn1_DEF_UniversalString_tags[] = {
-	(ASN_TAG_CLASS_UNIVERSAL | (28 << 2))
+	(ASN_TAG_CLASS_UNIVERSAL | (28 << 2)),	/* [UNIVERSAL 28] IMPLICIT ...*/
+	(ASN_TAG_CLASS_UNIVERSAL | (4 << 2))	/* ... OCTET STRING */
 };
 asn1_TYPE_descriptor_t asn1_DEF_UniversalString = {
 	"UniversalString",
@@ -20,6 +21,9 @@
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_UniversalString_tags,
 	sizeof(asn1_DEF_UniversalString_tags)
+	  / sizeof(asn1_DEF_UniversalString_tags[0]) - 1,
+	asn1_DEF_UniversalString_tags,
+	sizeof(asn1_DEF_UniversalString_tags)
 	  / sizeof(asn1_DEF_UniversalString_tags[0]),
 	-1,	/* Both ways are fine */
 	0, 0,	/* No members */
diff --git a/skeletons/VideotexString.c b/skeletons/VideotexString.c
index 772c66c..6d2eaeb 100644
--- a/skeletons/VideotexString.c
+++ b/skeletons/VideotexString.c
@@ -8,7 +8,8 @@
  * VideotexString basic type description.
  */
 static ber_tlv_tag_t asn1_DEF_VideotexString_tags[] = {
-	(ASN_TAG_CLASS_UNIVERSAL | (21 << 2))
+	(ASN_TAG_CLASS_UNIVERSAL | (21 << 2)),	/* [UNIVERSAL 21] IMPLICIT */
+	(ASN_TAG_CLASS_UNIVERSAL | (4 << 2))	/* ... OCTET STRING */
 };
 asn1_TYPE_descriptor_t asn1_DEF_VideotexString = {
 	"VideotexString",
@@ -20,6 +21,9 @@
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_VideotexString_tags,
 	sizeof(asn1_DEF_VideotexString_tags)
+	  / sizeof(asn1_DEF_VideotexString_tags[0]) - 1,
+	asn1_DEF_VideotexString_tags,
+	sizeof(asn1_DEF_VideotexString_tags)
 	  / sizeof(asn1_DEF_VideotexString_tags[0]),
 	-1,	/* Both ways are fine */
 	0, 0,	/* No members */
diff --git a/skeletons/VisibleString.c b/skeletons/VisibleString.c
index 18a27fa..9ddb0f1 100644
--- a/skeletons/VisibleString.c
+++ b/skeletons/VisibleString.c
@@ -8,7 +8,8 @@
  * VisibleString basic type description.
  */
 static ber_tlv_tag_t asn1_DEF_VisibleString_tags[] = {
-	(ASN_TAG_CLASS_UNIVERSAL | (26 << 2))
+	(ASN_TAG_CLASS_UNIVERSAL | (26 << 2)),	/* [UNIVERSAL 26] IMPLICIT ...*/
+	(ASN_TAG_CLASS_UNIVERSAL | (4 << 2))	/* ... OCTET STRING */
 };
 asn1_TYPE_descriptor_t asn1_DEF_VisibleString = {
 	"VisibleString",
@@ -20,6 +21,9 @@
 	0, /* Use generic outmost tag fetcher */
 	asn1_DEF_VisibleString_tags,
 	sizeof(asn1_DEF_VisibleString_tags)
+	  / sizeof(asn1_DEF_VisibleString_tags[0]) - 1,
+	asn1_DEF_VisibleString_tags,
+	sizeof(asn1_DEF_VisibleString_tags)
 	  / sizeof(asn1_DEF_VisibleString_tags[0]),
 	-1,	/* Both ways are fine */
 	0, 0,	/* No members */
diff --git a/skeletons/constr_TYPE.h b/skeletons/constr_TYPE.h
index 1918b5c..15a6726 100644
--- a/skeletons/constr_TYPE.h
+++ b/skeletons/constr_TYPE.h
@@ -70,10 +70,13 @@
 	asn_outmost_tag_f  *outmost_tag;	/* <optional, internal> */
 
 	/*
-	 * Tags that are expected, with some of their vital properties.
+	 * Tags that are expected to occur.
 	 */
-	ber_tlv_tag_t *tags;	/* At least one tag must be specified */
+	ber_tlv_tag_t *tags;	/* Effective tags sequence for this type */
 	int tags_count;		/* Number of tags which are expected */
+	ber_tlv_tag_t *all_tags;/* Every tag for BER/containment */
+	int all_tags_count;	/* Number of tags */
+
 	int last_tag_form;	/* Acceptable form of the tag (prim, constr) */
 
 	/*
@@ -92,12 +95,13 @@
 /*
  * An element of the constructed type, i.e. SEQUENCE, SET, CHOICE.
  */
+  enum asn1_TYPE_flags_e {
+	ATF_NOFLAGS,
+	ATF_POINTER	= 0x01,	/* Represented by the pointer */
+	ATF_OPEN_TYPE	= 0x02,	/* ANY type, without meaningful tag */
+  };
 typedef struct asn1_TYPE_member_s {
-	enum asn1_TYPE_flags_e {
-		ATF_NOFLAGS,
-		ATF_POINTER	= 0x01,	/* Represented by the pointer */
-		ATF_OPEN_TYPE	= 0x02,	/* ANY type, without meaningful tag */
-	} flags;			/* Element's presentation flags */
+	enum asn1_TYPE_flags_e flags;	/* Element's presentation flags */
 	int optional;	/* Following optional members, including current */
 	int memb_offset;		/* Offset of the element */
 	ber_tlv_tag_t tag;		/* Outmost (most immediate) tag */