home *** CD-ROM | disk | FTP | other *** search
/ Amiga GigaPD 3 / Amiga_GigaPD_v3_3of3.iso / fred_fish / fish_651-700_unpacked / 662 / ninfo / ninfo.c < prev    next >
C/C++ Source or Header  |  1993-06-25  |  9KB  |  358 lines

  1. /*
  2. *
  3. * Object information utility  Version 1.0 16-apr-1990
  4. * Arguments are:
  5. * Oinfo <options> <input file> ... <input file>.
  6. * Description:
  7. * This utility will read an object (from the Lattice Compiler) and
  8. * decode the Hunks and informations comtained in the Hunks.
  9. *
  10. * Options:
  11. *   -m   : Decode a block of memory
  12. *   -v   : Decode boot block dump (check virus code)
  13. *   -s   : symbols supressed
  14. *   -d   : data words supressed
  15. *   -c   : code words supressed
  16. *
  17. */
  18. #include <libraries/dos.h>
  19. #include "hunk.h"
  20. #include <ctype.h>
  21. int Flgs[26];          /* user options */
  22. void main(int, char **);
  23. void main(argc, argv)
  24. int   argc;                      /*number of arguments            */
  25. char  *argv[];                   /*array of ptrs to arg strings   */
  26.   {
  27.   BPTR  file1;                    /* input File pointer                 */
  28.   char *name1;
  29.   int  i;
  30.   long addrs;
  31.   short More_Stuff;
  32.   int Header[3];     /* Hunk type and size in words */
  33.   /* get the file names and open the files */
  34.   /* initialize input buffers and variables*/
  35.   addrs = 0;
  36.   Do_Arguments(argc, argv,&addrs);
  37.   fprintf(stderr," \tObject/Executable/Library Disassembler\n");
  38.   fprintf(stderr," \tVersion 2.0 ⌐ 1991 Custom Services\n");
  39.   fprintf(stderr," \t\t***All Rights Reserved***\n");
  40.   fprintf(stderr," \t\tShareware donation:$5.00\n");
  41.   fprintf(stderr," \t\tCustom Services\n");
  42.   fprintf(stderr," \t\tP. O. Box 254\n");
  43.   fprintf(stderr," \t\tMoorestown, NJ 08057\n");
  44.   file1 = NULL;
  45.   if ( argc < 2 )
  46.     {
  47.     printf(" Format is:\n%s [-c -d -s -v -m<address>] <file>... <file>\n",argv[0]);
  48.     exit(10);
  49.  
  50.     };
  51.   /* collect the data */
  52.   while (--argc > 0 )       /* while files to process */
  53.     {
  54.     if( file1 != NULL)
  55.       {
  56.       Close( file1);
  57.       file1 = NULL;
  58.  
  59.       };
  60.     name1      = *++argv;         /*   get a file name?      */
  61.     if ( *name1 == '-' )continue;  /*   skip options          */
  62.     printf("\n      Processing: %s\n",name1);
  63.     file1      = Open(name1,MODE_OLDFILE);  /* open file for reading */
  64.     if ( file1 == NULL )
  65.       {
  66.       printf(" Cannot Open Object file: %s\n",name1);
  67.       break;
  68.  
  69.       };
  70.     /*  read the entries and decode data  */
  71.     More_Stuff = TRUE;
  72.     if( ! Flgs[21] ) /* regular if no -V or -M<address> */
  73.       {
  74.       while ( More_Stuff )
  75.         {
  76.         Header[0] = ( GrabLong(file1) & 0x0000FFFF );
  77.         switch ( Header[0] )
  78.           {
  79.           case  0 :                   /* end of file */
  80.           More_Stuff = FALSE;
  81.           break;
  82.           case  Hunk_End   :          /* end of a Hunk */
  83.           printf("Hunk_End(3F2)\n");
  84.           break;
  85.           case  Hunk_Code :  /* 3e9 */
  86.           Header[0] = GrabLong(file1);  /* get length of code */
  87.           printf("Hunk_Code(3E9) :%d words\n",Header[0]);
  88.           Dump_Code(file1, Header[0]);
  89.           break;
  90.           case  Hunk_Data :
  91.           case  Hunk_Bss  :
  92.           Header[1] = GrabLong(file1);
  93.           switch ( Header[0] )
  94.             {
  95.             case  Hunk_Data : printf("Hunk_Data(3EA) :"); break;
  96.             case  Hunk_Bss  : printf("Hunk_Bss (3EB) :"); break;
  97.  
  98.             };
  99.           printf("%d words\n",Header[1]);
  100.           if( Header[0] != Hunk_Bss )Dump_Raw(file1,Header[0],Header[1]);
  101.           break;
  102.           case  Hunk_Debug:                /* debug hunks */
  103.           Header[1] = GrabLong(file1);
  104.           printf(" Hunk_Debug(3F1):(%d words)",Header[1]);
  105.           Header[0] = GrabLong(file1);
  106.           Header[1] = Header[1] - Header[0] - 1;
  107.           Do_Name(file1,Header[0]);
  108.           More_Stuff = Dump_Raw(file1,Header[0],Header[1]);
  109.           break;
  110.           case  Hunk_Name :
  111.           case  Hunk_Unit :
  112.           Header[1] = GrabLong(file1);
  113.           switch ( Header[0] )
  114.             {
  115.             case Hunk_Unit :printf("Hunk_Unit(3E7"); break;
  116.             case Hunk_Name :printf("Hunk_Name(3E8"); break;
  117.  
  118.             };
  119.           Do_Name(file1,Header[1]);
  120.           break;
  121.           case  Hunk_Dreloc32:
  122.           case  Hunk_Dreloc16:
  123.           case  Hunk_Dreloc8 :
  124.           case  Hunk_Reloc32 :
  125.           case  Hunk_Reloc16 :
  126.           case  Hunk_Reloc8  :
  127.           Header[1] = GrabLong(file1);
  128.           while( Header[1] != 0 && More_Stuff)
  129.             {
  130.             (void) Read(file1,(char *)&i,4);
  131.             switch ( Header[0] )
  132.               {
  133.               case  Hunk_Dreloc32 : printf("Hunk_Dreloc32");break;
  134.               case  Hunk_Dreloc16 : printf("Hunk_Dreloc16");break;
  135.               case  Hunk_Dreloc8  : printf("Hunk_Dreloc8 ");break;
  136.               case  Hunk_Reloc32  : printf("Hunk_Reloc32 ");break;
  137.               case  Hunk_Reloc16  : printf("Hunk_Reloc16 ");break;
  138.               case  Hunk_Reloc8   : printf("Hunk_Reloc8  ");break;
  139.  
  140.               };
  141.             printf(" Hunk %d, %d words\n",i,Header[1]);
  142.             More_Stuff = Dump_Raw(file1,Hunk_Code,Header[1]);
  143.             Header[1] = GrabLong(file1);
  144.  
  145.             };
  146.           break;
  147.           case Hunk_Ext:
  148.           printf("Hunk_Ext(3EF):\n");
  149.           DoSymbolData(file1);
  150.           break;
  151.           case Hunk_Symbol:
  152.           printf("Hunk_Symbol(3F0):\n");
  153.           DoSymbolData(file1);
  154.           break;
  155.           case Hunk_Library:
  156.           printf("Hunk_Library(3FA)\n");
  157.           printf("   Value = %x\n",GrabLong(file1));
  158.           break;
  159.           case Hunk_Break:
  160.           printf("Hunk_Break(3F6)\n");
  161.           break;
  162.           case Hunk_Overlay:
  163.           printf("Hunk_Overlay(3F5)\n");
  164.           Header[1] = GrabLong(file1);
  165.           printf("%d words\n",Header[1]);
  166.           Dump_Raw(file1,Header[0],Header[1]);
  167.           break;
  168.           case Hunk_Index:
  169.           printf("Hunk_Index(3FB)\n");
  170.           Header[1] = GrabLong(file1);
  171.           printf("%d words\n",Header[1]);
  172.           Dump_Raw(file1,Header[0],Header[1]);
  173.           break;
  174.           case Hunk_Header:
  175.           printf("Hunk_Header(3F3)\n");
  176.           Header[1] = GrabLong(file1);
  177.           if( Header[1] == 0)
  178.             {
  179.             printf(" No Resident Libraries to open\n");
  180.  
  181.             }
  182.           else
  183.           while( Header[1] != 0 && More_Stuff)
  184.             {
  185.             Do_Name(file1,Header[1]);
  186.             Header[1] = GrabLong(file1);
  187.  
  188.             };
  189.           for( i=0; i<3; i++) Header[i] = GrabLong(file1);
  190.           printf(" %d Hunks, Number %d thru %d\n",
  191.           Header[0],   Header[1],  Header[2]);
  192.           Dump_Raw(file1,Hunk_Code,Header[0]);
  193.           break;
  194.           default:
  195.           printf( "Hunk type = %x\n",Header[0]);
  196.  
  197.           };
  198.  
  199.         };
  200.  
  201.       }
  202.     else if( Flgs[21] )
  203.       {
  204.       Dump_Virus(file1);   /* if not regular then virus */
  205.  
  206.       };
  207.  
  208.     };
  209.   if( file1 != NULL) Close( file1);
  210.   if( Flgs[12] )
  211.     {
  212.     printf("Memory Dump as Code Hunk:60 bytess\n");
  213.     (void)dumpcode(addrs,(char *)addrs,60); /* dump the code */
  214.  
  215.     };
  216.  
  217.   }
  218. void Dump_Text(tptr,count)
  219. char *tptr;
  220. int count;
  221.   {
  222.   short t;
  223.   for( t=0; t < count; t++)
  224.     {
  225.     tptr[t] = 0x7f & tptr[t];
  226.     if( tptr[t] > 20 )
  227.       {
  228.       printf("%c",tptr[t]);
  229.  
  230.       }
  231.     else  printf(".");
  232.  
  233.     };
  234.   printf("\n");
  235.  
  236.   }
  237. Dump_Raw(file,Type,Words)
  238. BPTR file;
  239. int Type,Words;
  240.   {
  241.   char *Data;
  242.   char *tptr;
  243.   int *k;
  244.   short More_Stuff,i,j;
  245.   if( Words == 0)return(0);
  246.   Data = malloc(Words*4); /*get a large enough buffer */
  247.   if( Data == 0 )
  248.     {
  249.     printf(" Memory not allocated for buffer:%d\n");
  250.     exit(0);
  251.  
  252.     };
  253.   More_Stuff = Read(file,Data,Words*4) == Words*4 ;
  254.   i = j = 0;
  255.   if( ( Flgs[2] && Type == Hunk_Code ) ||
  256.   ( Flgs[3] && (Type == Hunk_Data || Type == Hunk_Debug) ) )
  257.     {
  258.     free(Data);
  259.     return((int)More_Stuff);
  260.  
  261.     };
  262.   while ( i < Words )
  263.     {
  264.     k = (int *)&Data[i*4];
  265.     if ( i++ < Words)printf("%08.08X ",*k);
  266.     if( ++j % 5 == 0)
  267.       {
  268.       tptr = &Data[(i-5)*4];
  269.       Dump_Text(tptr,20);
  270.  
  271.       };
  272.  
  273.     };
  274.   if( j % 5 != 0)
  275.     {
  276.     tptr = &Data[(i-5)*4];
  277.     Dump_Text(tptr,20);
  278.  
  279.     };
  280.   free(Data);
  281.   return ((int)More_Stuff);
  282.  
  283.   }
  284. Do_Name(file,Words)
  285. int  Words;
  286. BPTR file;
  287.   {
  288.   char *Data;
  289.   printf("[%d words])",Words);
  290.   if( Words > 100 || Words < 1)
  291.     {
  292.     printf("\n");
  293.     return ( 0 );
  294.  
  295.     };
  296.   Data = malloc(Words*4);
  297.   if( Data == 0)
  298.     {
  299.     printf(" Memory not allocated for name:%d\n",Words);
  300.     exit(10);
  301.  
  302.     };
  303.   (void)Read(file,Data,Words*4);
  304.   printf("%s\n",Data);
  305.   free(Data);
  306.   return(0);
  307.  
  308.   }
  309. Dump_Code(file,Words)
  310. BPTR file;
  311. int Words;
  312.   {
  313.   char *Data; /* pointer to data */
  314.   short More_Stuff;
  315.   if( Words == 0)return(0);
  316.   Data = malloc(Words*4); /*get a large enough buffer */
  317.   if( Data == 0 )
  318.     {
  319.     printf(" Memory not allocated for buffer:%d\n");
  320.     exit(10);
  321.  
  322.     };
  323.   More_Stuff = Read(file,Data,Words*4) == Words*4 ;
  324.   if( ! Flgs[2] )
  325.     {
  326.     (void)dumpcode((int)Data,Data,Words*4);
  327.  
  328.     };
  329.   free(Data);
  330.   return ((int)More_Stuff);
  331.  
  332.   }
  333. Dump_Virus(file)  /* dump virus block code */
  334. BPTR file;
  335.   {
  336.   char *Data;                /* pointer to data */
  337.   short More_Stuff;
  338.   int Words = 253;          /* preset the size */
  339.   Data = malloc(Words*4);   /*get a large enough buffer to hold the virus */
  340.   if( Data == 0 )
  341.     {
  342.     printf(" Memory not allocated for buffer:%d\n");
  343.     exit(10);
  344.  
  345.     };
  346.   printf(" Boot Block Header:\n");
  347.   Dump_Raw(file,Hunk_Data,3);
  348.   More_Stuff = Read(file,Data,Words*4) == Words*4 ;
  349.   if( ! Flgs[2] )
  350.     {
  351.     (void)dumpcode((int)Data,Data,Words*4); /* dump the code */
  352.  
  353.     };
  354.   free(Data);
  355.   return ((int)More_Stuff);
  356.  
  357.   }
  358.