different place for termination check


git-svn-id: https://asn1c.svn.sourceforge.net/svnroot/asn1c/trunk@490 59561ff5-6e30-0410-9f3c-9617f08c8826
diff --git a/skeletons/constr_SEQUENCE.c b/skeletons/constr_SEQUENCE.c
index 83da765..693ddd2 100644
--- a/skeletons/constr_SEQUENCE.c
+++ b/skeletons/constr_SEQUENCE.c
@@ -161,8 +161,7 @@
 		if(rval.code != RC_OK) {
 			ASN_DEBUG("%s tagging check failed: %d",
 				td->name, rval.code);
-			consumed_myself += rval.consumed;
-			RETURN(rval.code);
+			return rval;
 		}
 
 		if(ctx->left >= 0)
@@ -240,6 +239,35 @@
 		case -1: RETURN(RC_FAIL);
 		}
 
+		if(ctx->left < 0 && ((uint8_t *)ptr)[0] == 0) {
+			if(LEFT < 2) {
+				if(SIZE_VIOLATION)
+					RETURN(RC_FAIL);
+				else
+					RETURN(RC_WMORE);
+			} else if(((uint8_t *)ptr)[1] == 0) {
+			ASN_DEBUG("edx = %d, opt = %d, ec=%d",
+				edx, elements[edx].optional,
+				td->elements_count);
+				if((edx + elements[edx].optional
+					== td->elements_count)
+				|| (IN_EXTENSION_GROUP(specs, edx)
+					&& specs->ext_before
+						> td->elements_count)) {
+					/*
+					 * Yeah, baby! Found the terminator
+					 * of the indefinite length structure.
+					 */
+					/*
+					 * Proceed to the canonical
+					 * finalization function.
+					 * No advancing is necessary.
+					 */
+					goto phase3;
+				}
+			}
+		}
+
 		/*
 		 * Find the next available type with this tag.
 		 */
@@ -319,35 +347,14 @@
 			 * or an end of the indefinite-length structure.
 			 */
 			if(!IN_EXTENSION_GROUP(specs, edx)) {
-				ASN_DEBUG("Unexpected tag %s",
-					ber_tlv_tag_string(tlv_tag));
+				ASN_DEBUG("Unexpected tag %s (at %d)",
+					ber_tlv_tag_string(tlv_tag), edx);
 				ASN_DEBUG("Expected tag %s (%s)%s",
 					ber_tlv_tag_string(elements[edx].tag),
 					elements[edx].name,
 					elements[edx].optional
 						?" or alternatives":"");
 				RETURN(RC_FAIL);
-			}
-
-			if(ctx->left < 0
-				&& ((uint8_t *)ptr)[0] == 0) {
-				if(LEFT < 2) {
-					if(SIZE_VIOLATION)
-						RETURN(RC_FAIL);
-					else
-						RETURN(RC_WMORE);
-				} else if(((uint8_t *)ptr)[1] == 0) {
-					/*
-					 * Yeah, baby! Found the terminator
-					 * of the indefinite length structure.
-					 */
-					/*
-					 * Proceed to the canonical
-					 * finalization function.
-					 * No advancing is necessary.
-					 */
-					goto phase3;
-				}
 			} else {
 				/* Skip this tag */
 				ssize_t skip;