/* 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	encode_UNIV_EXTERNAL (pe, explicit, len, buffer, parm)
register PE     *pe;
int	explicit;
int	len;
char   *buffer;
struct type_UNIV_EXTERNAL * parm;
{
# line 89 "UNIV-types.py"
    
	struct choice_UNIV_0 *choice_UNIV_0;
	char *bit_parm;
    
    PE	p7_z = NULLPE;
    register PE *p7 = &p7_z;

    if (((*pe) = pe_alloc (PE_CLASS_UNIV, PE_FORM_CONS, 8)) == NULLPE) {
        advise (NULLCP, "EXTERNAL: %s", PEPY_ERR_NOMEM);
        return NOTOK;
    }
    {
# line 96 "UNIV-types.py"

                choice_UNIV_0 = parm -> encoding;
            
    }
    (*p7) = NULLPE;

    if (parm -> direct__reference) {	/* direct-reference */
        register OID p8;

        p8 = parm -> direct__reference ;
        if (p8 == NULLOID) {
            advise (NULLCP, "direct-reference %s", PEPY_ERR_INIT_FAILED);
            return NOTOK;
        }
        if (((*p7) = obj2prim (p8, PE_CLASS_UNIV, PE_PRIM_OID)) == NULLPE) {
            advise (NULLCP, "direct-reference: %s", PEPY_ERR_NOMEM);
            return NOTOK;
        }

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

    }

    if ((*p7) != NULLPE)
        if (seq_add ((*pe), (*p7), -1) == NOTOK) {
            advise (NULLCP, "EXTERNAL %s%s", PEPY_ERR_BAD_SEQ,
                    pe_error ((*pe) -> pe_errno));
            return NOTOK;
        }
    (*p7) = NULLPE;

    if (parm -> indirect__reference != 0) {	/* indirect-reference */
        register integer p9 = parm -> indirect__reference ;

        if (((*p7) = num2prim (p9, PE_CLASS_UNIV, PE_PRIM_INT)) == NULLPE) {
            advise (NULLCP, "indirect-reference: %s", PEPY_ERR_NOMEM);
            return NOTOK;
        }

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

    }

    if ((*p7) != NULLPE)
        if (seq_add ((*pe), (*p7), -1) == NOTOK) {
            advise (NULLCP, "EXTERNAL %s%s", PEPY_ERR_BAD_SEQ,
                    pe_error ((*pe) -> pe_errno));
            return NOTOK;
        }
    (*p7) = NULLPE;

    if (parm -> data__value__descriptor) {	/* data-value-descriptor */
        if (encode_UNIV_ObjectDescriptor (p7, 0, NULL, NULLCP, parm -> data__value__descriptor ) == NOTOK)
            return NOTOK;

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

    }

    if ((*p7) != NULLPE)
        if (seq_add ((*pe), (*p7), -1) == NOTOK) {
            advise (NULLCP, "EXTERNAL %s%s", PEPY_ERR_BAD_SEQ,
                    pe_error ((*pe) -> pe_errno));
            return NOTOK;
        }
    (*p7) = NULLPE;

    {	/* encoding */
        int	p10;

        switch (p10 = (choice_UNIV_0 -> offset)) {
            case 1:	/* single-ASN1-type */
                {
                    PE	p11 = choice_UNIV_0 -> un.single__ASN1__type ;

                    if (p11 == NULLPE) {
                        advise (NULLCP, "single-ASN1-type %s", PEPY_ERR_INIT_FAILED);
                        return NOTOK;
                    }
                    ((*p7) = p11) -> pe_refcnt++;

#ifdef DEBUG
                    (void) testdebug ((*p7), "single-ASN1-type");
#endif

                    {	/* single-ASN1-type TAG PUSHDOWN */
                        PE p12_z;
                        register PE *p12 = &p12_z;

                        if ((*p12 = pe_alloc (PE_CLASS_CONT, PE_FORM_CONS, 0)) == NULLPE) {
                            advise (NULLCP, "single-ASN1-type: %s", PEPY_ERR_NOMEM);
                            return NOTOK;
                        }
                        (*p12) -> pe_cons = (*p7);
                        (*p7) = *p12;
                    }
                }
                break;
            case 2:	/* octet-aligned */
                {
                    register struct qbuf *p13;

                    p13 = choice_UNIV_0 -> un.octet__aligned ;
                    if (p13 == ((struct qbuf *) 0)) {
                        advise (NULLCP, "octet-aligned %s", PEPY_ERR_INIT_FAILED);
                        return NOTOK;
                    }
                    if (((*p7) = qb2prim (p13, PE_CLASS_CONT, 1)) == NULLPE) {
                        advise (NULLCP, "octet-aligned: %s", PEPY_ERR_NOMEM);
                        return NOTOK;
                    }

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

                }
                break;
            case 3:	/* arbitrary */
                {
                    PE	p14_z = NULLPE;
                    register PE *p14 = &p14_z;

                    char *p15;
                    int p16;
                    p15 = bit_parm = bitstr2strb (choice_UNIV_0 -> un.arbitrary, &len) ;
                    p16 =  len;
                    (*p14) = p15 ? strb2bitstr (p15, p16, PE_CLASS_CONT, 2) : NULLPE;
                    if ((*p14) == NULLPE) {
                        advise (NULLCP, "arbitrary %s", PEPY_ERR_INIT_FAILED);
                        return NOTOK;
                    }
                    if (((*p7) = bit2prim ((*p14))) == NULLPE) {
                        advise (NULLCP, "arbitrary: %s", PEPY_ERR_NOMEM);
                        return NOTOK;
                    }

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

                    {
# line 128 "UNIV-types.py"

                            free (bit_parm);
                        
                    }
                }
                break;

            default:
                advise (NULLCP, "encoding %s%d", PEPY_ERR_INVALID_CHOICE, 
                        p10);
                return NOTOK;
        }

#ifdef DEBUG
        (void) testdebug ((*p7), "encoding");
#endif

    }

    if ((*p7) != NULLPE)
        if (seq_add ((*pe), (*p7), -1) == NOTOK) {
            advise (NULLCP, "EXTERNAL %s%s", PEPY_ERR_BAD_SEQ,
                    pe_error ((*pe) -> pe_errno));
            return NOTOK;
        }

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


    return OK;
}
