home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / utilities / ha.lha / ha.c next >
Encoding:
C/C++ Source or Header  |  1992-12-23  |  3.8 KB  |  189 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <exec/types.h>
  5. #include <dos.h>
  6. #include <proto/exec.h>
  7. #include <proto/dos.h>
  8.  
  9. BPTR ZeFile,OutFile;
  10. LONG LastErr,LastRead;
  11. char FileName[80];
  12. LONG HunkCount,FirstHunk,LastHunk;
  13. char BufName[80];
  14. struct HunkInfo {
  15.     int pos_data;
  16.     int size_data;
  17.     LONG type;
  18.     BOOL HaveReloc;
  19.     };
  20. struct HunkInfo *ExecInfo;
  21. BOOL NoReloc;
  22.  
  23. LONG GetLong(BPTR);
  24. BOOL ValidType(LONG);
  25. void SkipHunk(BPTR);
  26.  
  27. LONG GetLong(fi)
  28. BPTR fi;
  29. {
  30.     static LONG res;
  31.  
  32.     Read(fi,&res,sizeof(LONG));
  33.     return(res);
  34. }
  35.  
  36. BOOL ValidType(typ)
  37. LONG typ;
  38. {
  39.     register BOOL res;
  40.     register LONG inttyp;
  41.  
  42.     inttyp=typ & 0xfff;
  43.     if ((inttyp==0x3e9)||(inttyp==0x3ea)||(inttyp==0x3eb))
  44.         res=TRUE;
  45.     else
  46.         res=FALSE;
  47.     return(res);
  48. }
  49.  
  50. void SkipHunk(fi)
  51. BPTR(fi);
  52. {
  53.     register int length;
  54.  
  55.     length=GetLong(fi);
  56.     if (Seek(fi,(length*4),OFFSET_CURRENT)==-1)
  57.     {
  58.         printf("Unexpected end of file... terminating\n");
  59.         exit(0);
  60.     };
  61. }
  62.  
  63. main(argc,argv)
  64. int argc;
  65. char *argv[];
  66. {
  67.     register int i,j;
  68.  
  69.     printf("\033[1;31;40\155HunkAnalyzer\033[0;31;40\155\n\
  70. Analyze hunk structure of an executable\n\
  71. Save out a stripped version of the executable if possible\n\
  72. By TLC from CHRYSEIS in December '92\n");
  73.     if (argc!=2)
  74.     {
  75.         printf("Usage: HunkAnalyze executable_file\n");
  76.         exit(0);
  77.     };
  78.  
  79.     strcpy(FileName,argv[1]);
  80.  
  81.     if ((ZeFile=(BPTR)Open(FileName,MODE_OLDFILE))==NULL)
  82.     {
  83.         LastErr=IoErr();
  84.         printf("Unable to open %s : DOS error %ld\n",FileName,LastErr);
  85.         exit(0);
  86.     };
  87.  
  88.     if (GetLong(ZeFile)!=0x000003f3)
  89.     {
  90.         printf("%s is not an executable\n",FileName);
  91.         Close(ZeFile);
  92.         exit(0);
  93.     };
  94.  
  95.     LastRead=GetLong(ZeFile);
  96.     HunkCount=0;
  97.     while (LastRead!=0)
  98.     {
  99.         Read(ZeFile,BufName,(LastRead*4));
  100.         BufName[(LastRead*4)+1]=0;
  101.         printf("Hunk %d : %s\n",HunkCount,BufName);
  102.         LastRead=GetLong(ZeFile);
  103.         HunkCount++;
  104.     };
  105.  
  106.     HunkCount=GetLong(ZeFile);
  107.     FirstHunk=GetLong(ZeFile);
  108.     LastHunk=GetLong(ZeFile);
  109.     printf("\nNombre de Hunks : %ld (%ld->%ld)\n"\
  110.                          ,HunkCount,FirstHunk,LastHunk);
  111.  
  112.     ExecInfo=(struct HunkInfo *)calloc(HunkCount,sizeof(struct HunkInfo));
  113.  
  114.     for (i=0;i<HunkCount;i++)
  115.         ExecInfo[i].size_data=GetLong(ZeFile);
  116.  
  117.     NoReloc=TRUE;
  118.  
  119.     j=GetLong(ZeFile);
  120.     for (i=0;i<HunkCount;i++)
  121.     {
  122.         while (j==0x000003f1)
  123.         {
  124.             SkipHunk(ZeFile);
  125.             j=GetLong(ZeFile);
  126.         };
  127.         if (ValidType(j)==FALSE)
  128.         {
  129.             printf("Error : hunk %d : hunk is not valid (should be CODE, DATA or BSS)\n",i);
  130.             Close(ZeFile);
  131.             exit(0);
  132.         };
  133.         ExecInfo[i].type=j;
  134.         ExecInfo[i].size_data=GetLong(ZeFile);
  135.         ExecInfo[i].pos_data=Seek(ZeFile,0,OFFSET_CURRENT);
  136.         ExecInfo[i].HaveReloc=FALSE;
  137.         if ((j & 0xfff)!=0x3eb)
  138.             Seek(ZeFile,(ExecInfo[i].size_data)*4,OFFSET_CURRENT);
  139.         j=GetLong(ZeFile);
  140.         while (j!=0x000003f2)
  141.         {
  142.             if ((j==0x000003ec)||(j==0x000003ed)||(j==0x000003ee))
  143.             {
  144.                 NoReloc=FALSE;
  145.                 ExecInfo[i].HaveReloc=TRUE;
  146.             };
  147.             SkipHunk(ZeFile);
  148.             j=GetLong(ZeFile);
  149.         };
  150.         j=GetLong(ZeFile);
  151.     };
  152.  
  153.     printf("Hunk # |  size  | position |   type   | position independant\n");
  154.     printf("-------+--------+----------+----------+---------------------\n");
  155.     for (i=0;i<HunkCount;i++)
  156.         printf("  %3d  | %6d | %8d | %08X |       %s\n",\
  157.             i,\
  158.             ExecInfo[i].size_data*4,\
  159.             ExecInfo[i].pos_data,\
  160.             ExecInfo[i].type,\
  161.             (ExecInfo[i].HaveReloc==TRUE?"No":"Yes"));
  162.  
  163.     if (NoReloc==FALSE)
  164.         printf("Unable to create raw datas : some hunks are relocatable\n");
  165.  
  166.     if (HunkCount>1)
  167.         printf("Unable to create raw datas : there is more than one hunk\n");
  168.  
  169.     if ((NoReloc==TRUE) && (HunkCount==1))
  170.     {
  171.         strcat(FileName,".RawCode");
  172.         printf("Saving raw 68000 code as %s\n",FileName);
  173.         if ((OutFile=Open(FileName,MODE_NEWFILE))!=NULL)
  174.         {
  175.             Seek(ZeFile,ExecInfo[0].pos_data,OFFSET_BEGINNING);
  176.             for (i=0;i<ExecInfo[0].size_data;i++)
  177.             {
  178.                 LastRead=GetLong(ZeFile);
  179.                 Write(OutFile,&LastRead,sizeof(LONG));
  180.             };
  181.             Close(OutFile);
  182.         }
  183.         else
  184.             printf("Unable to open the output file\n");
  185.     };
  186.  
  187.     Close(ZeFile);
  188.     return(0);
  189. }