home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Tools / splat / splat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  4.9 KB  |  301 lines

  1. /* splat.c: produces a ASN.1 dump */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Tools/splat/RCS/splat.c,v 6.0 1991/12/18 20:32:50 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Tools/splat/RCS/splat.c,v 6.0 1991/12/18 20:32:50 jpo Rel $
  9.  *
  10.  * $Log: splat.c,v $
  11.  * Revision 6.0  1991/12/18  20:32:50  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16.  
  17.  
  18. #include "util.h"
  19. #include <varargs.h>
  20. void advise();
  21.  
  22.  
  23.  
  24.  
  25. /* ---------------------  Begin  Routines  -------------------------------- */
  26. char    *myname;
  27. int    process1 (), process2 ();
  28.  
  29.  
  30. main (argc, argv, envp)
  31. int     argc;
  32. char  **argv,
  33.       **envp;
  34. {
  35.     extern char    *optarg;
  36.     extern int    optind;
  37.     int    opt;
  38.     register char  *cp;
  39.     register    FILE * fp;
  40.     int         status = 0;
  41.     IFP        fnx = process1;
  42.  
  43.     myname = argv[0];
  44.     while((opt = getopt(argc, argv, "d")) != EOF)
  45.     switch (opt) {
  46.     case 'd':
  47.         fnx = process2;
  48.         break;
  49.     default:
  50.         fprintf (stderr, "Usage: %s", myname);
  51.         break;
  52.     }
  53.     argc -= optind;
  54.     argv += optind;
  55.  
  56.     if (argc == 0)
  57.     status = (*fnx) ("(stdin)", stdin);
  58.     else
  59.     while (cp = *argv++) {
  60.         if ((fp = fopen (cp, "r")) == NULL) {
  61.         advise (cp, "unable to read");
  62.         status++;
  63.         continue;
  64.         }
  65.         status += (*fnx) (cp, fp);
  66.         (void) fclose (fp);
  67.     }
  68.  
  69.     exit (status);
  70. }
  71.  
  72.  
  73.  
  74. /* ---------------------  Static  Routines  ------------------------------- */
  75.  
  76.  
  77.  
  78. static int  process1 (file, fp)
  79. register char *file;
  80. register FILE *fp;
  81. {
  82.     register PE     pe;
  83.     register PS     ps;
  84.     PS              ps_out;
  85.  
  86.     if ((ps = ps_alloc (std_open)) == NULLPS) {
  87.     advise ("process", "ps_alloc");
  88.     return 1;
  89.     }
  90.     if (std_setup (ps, fp) == NOTOK) {
  91.     advise (NULLCP, "%s: std_setup loses", file);
  92.     return 1;
  93.     }
  94.  
  95.     if ((ps_out = ps_alloc (std_open)) == NULLPS)
  96.     printf ("failed on ps_alloc\n");
  97.     if (std_setup (ps_out, stdout) != OK)
  98.     printf ("failed to asssign stdout\n");
  99.  
  100.     for (;;) {
  101.     if ((pe = ps2pe (ps)) == NULLPE)
  102.         if (ps -> ps_errno) {
  103.         advise (NULLCP, "ps2pe: %s", ps_error(ps -> ps_errno));
  104.         ps_free (ps);
  105.         return 1;
  106.         }
  107.         else {
  108.         ps_free (ps);
  109.         return 0;
  110.         }
  111.  
  112.  
  113.     pe2pl (ps_out, pe);
  114.  
  115.     pe_free (pe);
  116.     }
  117. }
  118.  
  119. #define ps_advise(ps, str) advise (NULLCP, "%s: %s", str, ps_error(ps -> ps_errno));
  120.  
  121. int process2 (file, fp)
  122. char    *file;
  123. FILE    *fp;
  124. {
  125.     register PE     pe;
  126.     register PS     ps;
  127.     PS              ps_out;
  128.  
  129.     if ((ps = ps_alloc (std_open)) == NULLPS) {
  130.     advise ("process", "ps_alloc");
  131.     return 1;
  132.     }
  133.     if (std_setup (ps, fp) == NOTOK) {
  134.     advise (NULLCP, "%s: std_setup loses", file);
  135.     return 1;
  136.     }
  137.  
  138.     for (;;) {
  139.     if (read_stuff (ps, 0) == NOTOK)
  140.         return 1;
  141.     }
  142. }
  143.  
  144.  
  145. read_stuff (ps, depth)
  146. PS    ps;
  147. int    depth;
  148. {
  149.     PElementClass class;
  150.     PElementForm form;
  151.     PElementID id;
  152.     int len;
  153.     
  154.     if (ps_read_id (ps, 1, &class, &form, &id) == NOTOK) {
  155.     ps_advise (ps, "ps_read_id");
  156.     return NOTOK;
  157.     }
  158.     printf ("%*sClass %s, form %s, id %d ", depth * 2, "",
  159.         pe_classlist[class],
  160.         form == PE_FORM_PRIM ? "primitive" : "constructor",
  161.         id);
  162.     if (ps_read_len (ps, &len) == NOTOK) {
  163.     ps_advise (ps, "ps_read_len");
  164.     return NOTOK;
  165.     }
  166.     printf (len == PE_LEN_INDF ? "length indefinite\n" : "length %d\n", len);
  167.     if (form == PE_FORM_PRIM) {
  168.     if (len == 0) {
  169.         if (class == PE_CLASS_UNIV && id == 0)
  170.         return DONE;
  171.         else return OK;
  172.     }
  173.     else {
  174.         char *str = malloc (len);
  175.         if (ps_read (ps, str, len) == NOTOK) {
  176.         ps_advise (ps, "ps_read");
  177.         return NOTOK;
  178.         }
  179.         hexdump (str, len, depth);
  180.         free (str);
  181.     }
  182.     }
  183.     else {
  184.     if (len == PE_LEN_INDF) {
  185.         int res;
  186.  
  187.         for (;;) {
  188.         res = read_stuff (ps, depth + 1);
  189.         if (res == NOTOK)
  190.             return res;
  191.         if (res == DONE)
  192.             break;
  193.         }
  194.     }
  195.     else {
  196.         int cc = ps -> ps_byteno + len;
  197.  
  198.         for (;;) {
  199.         if (read_stuff (ps, depth + 1) == NOTOK)
  200.             return NOTOK;
  201.         if (ps -> ps_byteno >= cc)
  202.             break;
  203.         }
  204.     }
  205.             
  206.     }
  207.         
  208.     return OK;
  209.     
  210. }
  211.  
  212. hexdump (s, n, depth)
  213. char    *s;
  214. int n;
  215. {
  216.     int        i;
  217.     char    *hexstr = "0123456789ABCDEF";
  218.  
  219.     printf ("%*s", depth * 2, "");
  220.     for (i = depth * 2; n > 0; n--, i+=2, s++) {
  221.     putchar (hexstr[(*s>>4) & 0xf]);
  222.     putchar (hexstr[(*s)&0xf]);
  223.     if (i > 70) {
  224.         putchar ('\n');
  225.         i = depth;
  226.         printf ("%*s", depth * 2, "");
  227.     }
  228.     }
  229.     putchar ('\n');
  230. }
  231.  
  232. #ifndef    lint
  233. void    _advise ();
  234.  
  235.  
  236. void    adios (va_alist)
  237. va_dcl
  238. {
  239.     va_list ap;
  240.  
  241.     va_start (ap);
  242.  
  243.     _advise (ap);
  244.  
  245.     va_end (ap);
  246.  
  247.     _exit (1);
  248. }
  249. #else
  250. /* VARARGS */
  251.  
  252. void    adios (what, fmt)
  253. char   *what,
  254.        *fmt;
  255. {
  256.     adios (what, fmt);
  257. }
  258. #endif
  259.  
  260.  
  261. #ifndef    lint
  262. void    advise (va_alist)
  263. va_dcl
  264. {
  265.     va_list ap;
  266.  
  267.     va_start (ap);
  268.  
  269.     _advise (ap);
  270.  
  271.     va_end (ap);
  272. }
  273.  
  274.  
  275. static void  _advise (ap)
  276. va_list    ap;
  277. {
  278.     char    buffer[BUFSIZ];
  279.  
  280.     asprintf (buffer, ap);
  281.  
  282.     (void) fflush (stdout);
  283.  
  284.     fprintf (stderr, "%s: ", myname);
  285.     (void) fputs (buffer, stderr);
  286.     (void) fputc ('\n', stderr);
  287.  
  288.     (void) fflush (stderr);
  289. }
  290. #else
  291. /* VARARGS */
  292.  
  293. void    advise (what, fmt)
  294. char   *what,
  295.        *fmt;
  296. {
  297.     advise (what, fmt);
  298. }
  299. #endif
  300.  
  301.