/*****************************************************************/
/**                                                             **/
/**             (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           **/
/**                                                             **/
/*****************************************************************/

static char sccsid_dasn_c[] = "@(#)dasn.C	1.5 10/20/92 08:35:50  LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	8/10/90

	PROGRAM:	dasn

PURPOSE:
	Decode a byte stream into corresponding ASN.1 value structure
*/




#include <stdio.h>
#include <stdlib.h>
#include "c_varieties.h"

typedef int boolean;
void decode_asn(FILE *, int);
enum hdr_enum {VALID_HDR,DEF_EOC,INDEF_EOC};
enum hdr_enum decode_header(FILE *, int, int &, int &, int &,int &);
void decode_value(FILE *,int, int, int, int, int);
EXTERN_FUNCTION(int fdcd_tag, (FILE *,int *,int *,int *) );
EXTERN_FUNCTION(int fdcd_len, (FILE *,int *) );
EXTERN_FUNCTION(unsigned char fgethex, (FILE *) );
#define IN(X)	{for(int i=0; i<(X);i++) printf("   "); }

/*
	ROUTINE:	main

PURPOSE:
	Control ASN.1-Decoder. Opens files and gets 1 or more data values

IN:
	1 argument input filename (stdin assumed, if omitted)

OUT:
	outputs decoded form to standard output


CALLED ROUTINES:
	

	
MODIFICATION HISTORY:


*/

main(int argc, char *argv[])

{
   FILE *filein;

   if (argc < 2)	/* no arguments */
      filein = stdin;	/* use STD Input */
   else
   {
      filein = fopen(argv[1],"r");	/* open the user-file */
      if (!filein)			/* complain on error */
      {
         printf("Can't open input file\n");
         exit(1);
      }
   }

   for (; ; )		/* Decode as many values as there are in the file */
      decode_asn(filein,0);
}

/*
	ROUTINE:	decode_asn

PURPOSE:
	Decode an asn.1 value, first a header, then the value

IN:
	filein - input file
	indent - level of indenting

OUT:
	NONE


CALLED ROUTINES:
	

	
MODIFICATION HISTORY:


*/

void decode_asn(FILE *filein, int indent)

{
   int cl,form,tag,len;
   extern int global_exit_flag;

   global_exit_flag = 1;
   decode_header(filein,indent,cl,form,tag,len);
   decode_value(filein,indent,cl,form,tag,len);
   printf("\n");
}

/*
	ROUTINE:	decode_value

PURPOSE:
	Decode an asn.1 value, given information from its header

IN:
	filein - input file
	indent - level of indenting
	cl - class of value tag
	form - form of value tag
	tag - tag number
	len - length of value (-1 is indefinite)
	

OUT:
	NONE


CALLED ROUTINES:
	

	
MODIFICATION HISTORY:


*/

void decode_value(FILE *filein, int indent, int cl, int form, int tag, int len)
{
   int ch,i;

   if (form == 0)
   {
      IN(indent);
      for (i=0; i<len; i++)
      {
         ch = fgethex(filein);
         printf("%2.2x ",ch);
      }
      if (cl==0 && tag==0)
         printf("!!!EXTRA }");
      printf("\n");
   }
   else
   {
      hdr_enum e;

      IN(indent);
      printf("{\n");
      while ((e=decode_header(filein,indent+1,cl,form,tag,len)) == VALID_HDR)
         decode_value(filein,indent+1,cl,form,tag,len);
      IN(indent);
      printf("}\n");
   }
}

/*
	ROUTINE:	decode_header

PURPOSE:
	Decode an asn.1 value, given information from its header

IN:
	filein - input file
	indent - level of indenting
	

OUT:
	cl - class of value tag
	form - form of value tag
	tag - tag number
	len - length of value (-1 is indefinite)


CALLED ROUTINES:
	

	
MODIFICATION HISTORY:


*/
enum hdr_enum decode_header(FILE *filein, int indent, int &c, int &f, int &t, int &l)

{
   extern int global_byte_count;
#define STACKSIZE 1000
   static int stack[STACKSIZE],top = 0;

   if (top && global_byte_count == stack[top-1])
   {
      top--;
      return(DEF_EOC);
   }
   else if (top && global_byte_count > stack[top-1])
   {
      printf("Definite Stack malfunction\n");
      exit(1);
   }

   fdcd_tag(filein,&c,&f,&t);
   fdcd_len(filein,&l);
   if (c==0 && t==0 /* && l==0 */)
   {
      return(INDEF_EOC);
   }
   {
      IN(indent);
      printf("[");
      switch (c)
      {
         case 0: printf("UNIVERSAL"); break;
         case 1: printf("APPLICATION"); break;
         case 2: printf("CONTEXT"); break;
         case 3: printf("PRIVATE"); break;
      }
      printf(" %d ",t);
      printf(" %s]",f?"con":"prim");
      if (l == -1)
         printf(" (...)");
      else
      {
         printf(" (%d)",l);
         if (f==1)
            if (top < STACKSIZE)
               stack[top++] = global_byte_count+l;
            else
            {
               printf("Definite Stack overflow\n");
               exit(1);
            }
      }
   }
   printf("\n");
   return(VALID_HDR);
}



