/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

Interface sample Noautotag;

    Real(20,5) 			Real20_5type;
    BitString(*) 		BitString_type;
    ObjectIdentifier(*,2) 	ObjectIdentifier_type;
    Boolean(1) 			Boolean1type;
    Integer(4) 			Integer4type;
    OctetString(*)		OctetString_type;
    Enumerated(4) 		Enumerated4type;
    Null	 		Nulltype;
    IA5String(*) 		IA5String_type;

    Union 
    {
       Real(20,5) 		<Context 0 Implicit> real20_5u1;
       BitString(*) 		<Context 1 Implicit> bs_u2;
       ObjectIdentifier(*,2) 	<Context 2 Implicit> oid_u3;
       Boolean(1) 		<Context 3 Implicit> bool1u4;
       Integer(4) 		<Context 4 Implicit> int4u5;
       OctetString(*)		<Context 5 Implicit> os_u6;
       Enumerated(4) 		<Context 6 Implicit> en4u7;
       Null	 		<Context 7 Implicit> nulu8;
       IA5String(*) 		<Context 8 Implicit> ia5_u9;
    } untype;

    Union <Autotag> 
    {
       Real(20,5) 		real20_5u1;
       BitString(*) 		bs_u2;
       ObjectIdentifier(*,2) 	oid_u3;
       Boolean(1) 		bool1u4;
       Integer(4) 		int4u5;
       OctetString(*)		os_u6;
       Enumerated(4) 		en4u7;
       Null	 		nulu8;
       IA5String(*) 		ia5_u9;
    } noauto_untype;

End; /* of interface sample */


main(argc,argv)
   int argc;
   char *argv[];
{
   int i;
   int l1,l2;

   Real20_5type real20_5_s = 4.56;
   static unsigned char bs_s_val[] = { 0xa8, 0xa8, 0x80 };
   static BitString_type bs_s = { 17, bs_s_val };
   static unsigned long oid_s_val[] = { 2, 5, 6 };
   static ObjectIdentifier_type oid_s = {3, oid_s_val};
   Boolean1type bool1_s = 1;
   Integer4type int4_s = 456;
   static unsigned char os_s_val[] = 
                  {0x5a,0x5a, 0x5a,0x5a, 0x5a,0x5a, 0x5a,0x5a, 0x5a,0x5a};
   static OctetString_type os_s = {10, os_s_val};
   Enumerated4type en4_s = 678;
   static IA5String_type ia5_s = {5, "hello"};
   untype 		un_s;
   noauto_untype	un_r;

   Data (sample) inst;

   if (argc != 2)
      exit(1);   

/***************** send it out ************************/

   Bind_STUB(inst,argv[1],"w",Asn_1_ber);
   Encode inst
   {
      for (i=0 ; i<9; i++) {
	 un_s.tag = i;
	 switch (i) {
	 case 0:
	    un_s.un.real20_5u1 = 4.56;
	    break;
	 case 1:
            un_s.un.bs_u2.len=17;
            un_s.un.bs_u2.val = (unsigned char *)malloc(3);
	    un_s.un.bs_u2.val[0] = 0xa8;
	    un_s.un.bs_u2.val[1] = 0xa8;
	    un_s.un.bs_u2.val[2] = 0x80;
	    break;
	 case 2:
	    un_s.un.oid_u3.size = 3;
            un_s.un.oid_u3.comp = (unsigned long *)malloc(3*sizeof(long));
	    un_s.un.oid_u3.comp[0] = 2;
	    un_s.un.oid_u3.comp[1] = 5;
	    un_s.un.oid_u3.comp[2] = 6;
	    break;
	 case 3:
	    un_s.un.bool1u4 = 1;
	    break;
	 case 4:
	    un_s.un.int4u5 = 456;
	    break;
	 case 5:
            un_s.un.os_u6.len=10;
            un_s.un.os_u6.val = (unsigned char *)malloc(10);
	    memset(un_s.un.os_u6.val,0x5A,10);
	    break;
	 case 6:
	    un_s.un.en4u7= 678;
	    break;
	 case 7:
	    /* don't set un_s.un.nulu8 and won't be sent */
	    break;
	 case 8:
            un_s.un.ia5_u9.len = 5;
            un_s.un.ia5_u9.val = (char *)malloc(5);
	    memcpy(un_s.un.ia5_u9.val,"hello",5);
	    break;
	 }
	 un_s => untype;
      }
   }
   Unbind(inst);

/***************** receive it back ********************/

   Bind_STUB(inst,argv[1],"r",Asn_1_ber);

   for (i=0; i<9; i++) {
      if (i == 7) continue;
      Decode inst
	 {
	    On noauto_untype => (un_r);
	 }

/***************** report results *********************/

      switch (un_r.tag) {
	 case 0:
	    if (un_r.un.real20_5u1 != real20_5_s) {
		printf("   un_r.un.real20_5u1 value was bad\n");
		exit(1);
	    }
	    break;
	 case 1:
	     if (!__ROr_cmpP_BitString(&un_r.un.bs_u2,
				       &bs_s,
				       (l1=0,&l1) ,0)) {
		printf("   un_r.bs_u2 value was bad\n");
		exit(1);
	    }
	    break;
	 case 2:
	    if (!__ROr_cmpP_ObjectIdentifier(
				     &(un_r.un.oid_u3),
				     &(oid_s),
				     (l1=0,&l1) ,(l2=2,&l2))) {
	       printf("   un_r.oid_u3 value was bad\n");
	       exit(1);
	    }
	    break;
	 case 3:
	    if (!__ROr_cmpP_Boolean(&un_r.un.bool1u4,
				    &(bool1_s),
				    (l1=1,&l1) ,0)) {
	       printf("   un_r.bool1u4 value was bad\n");
	       exit(1);
	    }
	    break;
	 case 4:
	    if (un_r.un.int4u5 != int4_s) {
	       printf("   un_r.un.int4u5 had bad value\n");
	       exit(1);
	    }
	    break;
	 case 5:
	    if (!__ROr_CMPP_OctetString(un_r.un.os_u6,
					os_s,(l1=0,&l1) ,0)) {
	       printf("   un.os_u6 value was bad\n");
	       exit(1);
	    }
	    break;
	 case 6:
	    if (un_r.un.en4u7 != en4_s) {
	       printf("   un_r.un.en4u7 had bad value\n");
	       exit(1);
	    }
	    break;
	 case 7:
	    /* null was sent correctly */
	    break;
	 case 8:
	    if (!__ROr_CMPP_IA5String(un_r.un.ia5_u9,ia5_s,(l1=0,&l1),0)) {
	       printf("   un_r.un.ia5_u9 had bad value\n");
	       exit(1);
	    }
	    break;
      }
   } /* for */

   Unbind(inst);

   printf("test completed\n");
   return(0);
}
