/* automatically generated by pepy 8.0 #1 (big-screw), do not edit! */

#include "psap.h"

#define	advise	PY_advise

void	advise ();

/* Generated from module UNIV */

#include <stdio.h>
#include "UNIV-types.h"

#ifndef PEPYPARM
#define PEPYPARM char *
#endif /* PEPYPARM */
extern PEPYPARM NullParm;

/* ARGSUSED */

int	decode_UNIV_EXTERNAL (pe, explicit, len, buffer, parm)
register PE	pe;
int	explicit;
int    *len;
char  **buffer;
struct type_UNIV_EXTERNAL ** parm;
{
# line 214 "UNIV-types.py"
    
	struct choice_UNIV_0 **choice_UNIV_0;
    
    register PE p24;

#ifdef DEBUG
    (void) testdebug (pe, "UNIV.EXTERNAL");
#endif

    if (explicit
            && PE_ID (pe -> pe_class, pe -> pe_id)
                    != PE_ID (PE_CLASS_UNIV, 8)) {
        advise (NULLCP, "EXTERNAL %s%s/0x%x", PEPY_ERR_BAD_CLASS,
                pe_classlist[pe -> pe_class], pe -> pe_id);
        return NOTOK;
    }
    {
# line 218 "UNIV-types.py"

        if ((*(parm) = (struct type_UNIV_EXTERNAL *)
                calloc (1, sizeof **(parm))) == ((struct type_UNIV_EXTERNAL *) 0)) {
            advise (NULLCP, "%s", PEPY_ERR_NOMEM);
            return NOTOK;
        }
        choice_UNIV_0 = &((*parm) -> encoding);
    
    }
    {
# line 228 "UNIV-types.py"

                (*parm) -> indirect__reference = 0;
                choice_UNIV_0 = &((*parm) -> encoding);
            
    }
    if ((p24 = prim2seq (pe)) == NULLPE) {
        advise (NULLCP, "EXTERNAL %s%s", PEPY_ERR_BAD_SEQ,
                pe_error (pe -> pe_errno));
        return NOTOK;
    }
    pe = p24;

    {
        register PE p25;

        if ((p25 = first_member (pe)) != NULLPE
                && PE_ID (p25 -> pe_class, p25 -> pe_id)
                        != PE_ID (PE_CLASS_UNIV, 6))
            p25 = NULLPE;
        if (p25 != NULLPE) {
            p24 = p25;

            {	/* direct-reference */
                register OID p26;

#ifdef DEBUG
                (void) testdebug (p25, "direct-reference");
#endif

                if (p25 -> pe_class != PE_CLASS_UNIV
                        || p25 -> pe_form != PE_FORM_PRIM
                        || p25 -> pe_id != PE_PRIM_OID) {
                    advise (NULLCP, "direct-reference bad class/form/id: %s/%d/0x%x",
                            pe_classlist[p25 -> pe_class], p25 -> pe_form, p25 -> pe_id);
                    return NOTOK;
                }

                if ((p26 = prim2oid (p25)) == NULLOID) {
                    advise (NULLCP, "direct-reference %s%s", PEPY_ERR_BAD_OID,
                            pe_error (p25 -> pe_errno));
                    return NOTOK;
                }
                (*parm) -> direct__reference  = oid_cpy (p26);
            }
        }
    }

    {
        register PE p27;

        if ((p27 = (pe != p24 ? next_member (pe, p24) : first_member (pe))) 
                    != NULLPE
                && PE_ID (p27 -> pe_class, p27 -> pe_id)
                        != PE_ID (PE_CLASS_UNIV, 2))
            p27 = NULLPE;
        if (p27 != NULLPE) {
            p24 = p27;

            {	/* indirect-reference */
                register integer p28;

#ifdef DEBUG
                (void) testdebug (p27, "indirect-reference");
#endif

                if (p27 -> pe_class != PE_CLASS_UNIV
                        || p27 -> pe_form != PE_FORM_PRIM
                        || p27 -> pe_id != PE_PRIM_INT) {
                    advise (NULLCP, "indirect-reference bad class/form/id: %s/%d/0x%x",
                            pe_classlist[p27 -> pe_class], p27 -> pe_form, p27 -> pe_id);
                    return NOTOK;
                }

                {
# line 239 "UNIV-types.py"
 (*parm) -> indirect__reference = 0; 
                }
                if ((p28 = prim2num (p27)) == NOTOK
                        && p27 -> pe_errno != PE_ERR_NONE) {
                    advise (NULLCP, "indirect-reference %s%s", PEPY_ERR_BAD_INTEGER,
                            pe_error (p27 -> pe_errno));
                    return NOTOK;
                }
                (*parm) -> indirect__reference  = p28;
            }
        }
        else {
            /* set default here using yp -> yp_default */
        }

    }

    {
        register PE p29;

        if ((p29 = (pe != p24 ? next_member (pe, p24) : first_member (pe))) 
                    != NULLPE
                && PE_ID (p29 -> pe_class, p29 -> pe_id)
                        != PE_ID (PE_CLASS_UNIV, 7))
            p29 = NULLPE;
        if (p29 != NULLPE) {
            p24 = p29;

            {	/* data-value-descriptor */
#ifdef DEBUG
                (void) testdebug (p29, "data-value-descriptor");
#endif

                if (decode_UNIV_ObjectDescriptor (p29, 1, NULLIP, NULLVP, &((*parm) -> data__value__descriptor)) == NOTOK)
                    return NOTOK;
            }
        }
    }

    {
        register PE p30;

        if ((p30 = (pe != p24 ? next_member (pe, p24) : first_member (pe))) != NULLPE) {
            p24 = p30;

            {	/* encoding */
#ifdef DEBUG
                (void) testdebug (p30, "encoding");
#endif

                {
# line 251 "UNIV-types.py"

                        if ((*(choice_UNIV_0) = (struct choice_UNIV_0 *)
                                calloc (1, sizeof **(choice_UNIV_0))) == ((struct choice_UNIV_0 *) 0)) {
                            advise (NULLCP, "%s", PEPY_ERR_NOMEM);
                            return NOTOK;
                        }
                    
                }
                switch (PE_ID (p30 -> pe_class, p30 -> pe_id)) {
                    case PE_ID (PE_CLASS_CONT, 0):	/* single-ASN1-type */
                        {
                            register PE p31 = p30;

                            {	/* single-ASN1-type TAG PULLUP */
                                register PE p32;

                                if ((p32 = prim2set (p31)) == NULLPE) {
                                    advise (NULLCP, "single-ASN1-type %ssingle-ASN1-type: %s", PEPY_ERR_BAD,
                                            pe_error (p31 -> pe_errno));
                                    return NOTOK;
                                }
                                if (p32 -> pe_cardinal != 1) {
                                    advise (NULLCP, "single-ASN1-type %s single-ASN1-type: %d", PEPY_ERR_TOO_MANY_TAGGED,
                                            p32 -> pe_cardinal);
                                    return NOTOK;
                                }
                                p31 = first_member (p32);
                            }
                            {
#ifdef DEBUG
                                (void) testdebug (p31, "single-ASN1-type");
#endif

                                ((*choice_UNIV_0) -> un.single__ASN1__type  = p31) -> pe_refcnt++;
                                {
# line 262 "UNIV-types.py"
 (*(choice_UNIV_0)) -> offset = choice_UNIV_0_single__ASN1__type; 
                                }
                            }
                        }
                        break;
                    case PE_ID (PE_CLASS_CONT, 1):	/* octet-aligned */
                        {
                            register struct qbuf *p33;

#ifdef DEBUG
                            (void) testdebug (p30, "octet-aligned");
#endif

                            if ((p33 = prim2qb (p30)) == (struct qbuf *)0) {
                                advise (NULLCP, "octet-aligned %s%s", PEPY_ERR_BAD_OCTET,
                                        pe_error (p30 -> pe_errno));
                                return NOTOK;
                            }
                            (*choice_UNIV_0) -> un.octet__aligned  = p33;
                            {
# line 267 "UNIV-types.py"
 (*(choice_UNIV_0)) -> offset = choice_UNIV_0_octet__aligned; 
                            }
                        }
                        break;
                    case PE_ID (PE_CLASS_CONT, 2):	/* arbitrary */
                        {
                            register PE p34;

#ifdef DEBUG
                            (void) testdebug (p30, "arbitrary");
#endif

                            if ((p34 = prim2bit (p30)) == NULLPE) {
                                advise (NULLCP, "arbitrary %s%s", PEPY_ERR_BAD_BITS,
                                        pe_error (p30 -> pe_errno));
                                return NOTOK;
                            }
                            {
# line 271 "UNIV-types.py"

                            if (((*choice_UNIV_0) -> un.arbitrary = prim2bit (pe_cpy (p34))) == NULLPE) {
                                advise (NULLCP, "%s", PEPY_ERR_NOMEM);
                                return NOTOK;
                            }

                            (*(choice_UNIV_0)) -> offset = choice_UNIV_0_arbitrary;
                        
                            }
                        }
                        break;

                    default:
                        advise (NULLCP, "encoding %s%s/%d/0x%x", PEPY_ERR_UNKNOWN_CHOICE,
                                pe_classlist[p30 -> pe_class], p30 -> pe_form, p30 -> pe_id);
                        return NOTOK;
                }
            }
        }
        else {
            advise (NULLCP, "EXTERNAL %sencoding element", PEPY_ERR_MISSING);
            return NOTOK;
        }

    }


    if (pe -> pe_cardinal > 4) {
        advise (NULLCP, "EXTERNAL %s(4): %d", PEPY_ERR_TOO_MANY_ELEMENTS,
                pe -> pe_cardinal);
        return NOTOK;
    }

    return OK;
}
