home *** CD-ROM | disk | FTP | other *** search
/ AMIGA PD 1 / AMIGA-PD-1.iso / Programme_zum_Heft / Programmieren / Kurztests / Barfly / Utilities / Showhunk.lha / showhunk.c < prev    next >
C/C++ Source or Header  |  1994-07-05  |  13KB  |  595 lines

  1. /************************************************************************/
  2. /*                                                                      */
  3. /* ShowHunk.c       ---     shows the Hunkformat of files               */
  4. /*                                                                      */
  5. /*                          All Hunks up to V40 are supported           */
  6. /*                          are recognized.                             */
  7. /*                                                                      */
  8. /* Sorry for the bad C-Style....it's not my mother language:-)          */
  9. /* BTW...if you find some bugs or do some enhancements i would          */
  10. /* like to get the modified source. This program is FREEWARE and        */
  11. /* should never be distributed without the source.                      */
  12. /* Not for commercial use!                                              */
  13. /*                                                                      */
  14. /* I want to thank Randell Jesup for his new hunk definitions!          */
  15. /*                                                                      */
  16. /*----------------------------------------------------------------------*/
  17. /*                                                                      */
  18. /* V1.00 - 06/3/92     "Small C-Hack" (V1.00) by Ralph Schmidt          */
  19. /* V1.01 - 13/3/92     "1.Bug Fix"    (V1.01) by Ralph Schmidt          */
  20. /* V1.02 - 09/3/93     "2.Bug Fix"    (V1.02) by Ralph Schmidt          */
  21. /* V1.03 - 24/11/93    "Enhanced"     (V1.03) by Ralph Schmidt          */
  22. /*                     "Fixed Libparsing problem"                       */
  23. /*                                                                      */
  24. /************************************************************************/
  25.  
  26. #define HUNK_UNIT    999
  27. #define HUNK_NAME    1000
  28. #define HUNK_CODE    1001
  29. #define HUNK_DATA    1002
  30. #define HUNK_BSS    1003
  31. #define HUNK_RELOC32    1004
  32. #define HUNK_RELOC16    1005
  33. #define HUNK_RELOC8    1006
  34. #define HUNK_ABSRELOC32    HUNK_RELOC32
  35. #define HUNK_ABSRELOC16    1022
  36. #define HUNK_RELRELOC32    1021
  37. #define HUNK_RELRELOC16    HUNK_RELOC16
  38. #define HUNK_RELRELOC8    HUNK_RELOC8
  39. #define HUNK_EXT    1007
  40. #define HUNK_SYMBOL    1008
  41. #define HUNK_DEBUG    1009
  42. #define HUNK_END    1010
  43. #define HUNK_HEADER    1011
  44. #define HUNK_OVERLAY    1013
  45. #define HUNK_BREAK    1014
  46. #define HUNK_DREL32    1015
  47. #define HUNK_DREL16    1016
  48. #define HUNK_DREL8    1017
  49. #define HUNK_LIB    1018
  50. #define HUNK_INDEX    1019
  51. #define HUNK_RELOC32SHORT 1020
  52. #define HUNK_RELOC32SHORT_V37 1015
  53.  
  54.  
  55. #define EXT_SYMB    0
  56. #define EXT_DEF        1
  57. #define EXT_ABS        2
  58. #define EXT_RES        3
  59. #define EXT_REF32    129
  60. #define EXT_ABSREF32    EXT_REF32
  61. #define EXT_COMMON    130
  62. #define EXT_ABSCOMMON    EXT_COMMON
  63. #define EXT_RELCOMMON    137
  64. #define EXT_REF16    131
  65. #define EXT_REF8    132
  66. #define EXT_ABSREF16    138
  67. #define EXT_ABSREF8    139
  68. #define EXT_RELREF32    136
  69. #define EXT_RELREF16    EXT_REF16
  70. #define EXT_RELREF8    EXT_REF8
  71. #define EXT_DEXT32    133
  72. #define EXT_DEXT16    134
  73. #define EXT_DEXT8    135
  74.  
  75.  
  76. #include <dos/dos.h>
  77. #include <proto/dos.h>
  78. #include <proto/exec.h>
  79. #include <string.h>
  80.  
  81. const UBYTE Version[] = "$VER: ShowHunk 1.03 ("__DATE__")";
  82.  
  83. #define BUFFERSIZE      256
  84. #define READLONG    (Status=Read(FileHandle,Buffer,4))
  85. #define READWORD    (Status=Read(FileHandle,Buffer,2))
  86.  
  87. ULONG    Buffer[BUFFERSIZE+1];
  88. ULONG    StringBuffer[BUFFERSIZE];
  89. char    AttrString[15];
  90. LONG    Status;
  91. BPTR    FileHandle;
  92. BPTR    MyFile;
  93. BOOL    Symbol;
  94.     struct    RDArgs *rdargs=NULL;
  95.  
  96. void    exit(int);
  97.  
  98. void EXITUS(void)
  99. {
  100.         if (rdargs)
  101.            FreeArgs(rdargs);
  102.     if (FileHandle)
  103.            Close(FileHandle);
  104.     exit(20);
  105. }
  106.  
  107. void EXITUS_CTRL(void)
  108. {
  109.         FPuts(MyFile,"CTRL-C...Break\n");
  110.     if (FileHandle)
  111.            Close(FileHandle);
  112.     exit(20);
  113. }
  114.  
  115. void ReadLong(void)
  116. {
  117.     if (SetSignal(0L,SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C)
  118.            EXITUS_CTRL();
  119.     if ( READLONG !=4 )
  120.     {
  121.         if (Status == -1) FPuts(MyFile,"Error: Read Error\n");
  122.         else FPuts(MyFile,"Error: Unexpected File End\n");
  123.         EXITUS();
  124.     }
  125. }
  126.  
  127. void ReadWord(void)
  128. {
  129.     if (SetSignal(0L,SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C)
  130.            EXITUS_CTRL();
  131.     if ( READWORD !=2 )
  132.     {
  133.         if (Status == -1) FPuts(MyFile,"Error: Read Error\n");
  134.         else FPuts(MyFile,"Error: Unexpected File End\n");
  135.         EXITUS();
  136.     }
  137. }
  138.  
  139. void SeekLong(void)
  140. {
  141.     ReadLong();
  142.     if ( (Status=Seek(FileHandle,Buffer[0]<<2,OFFSET_CURRENT)) == -1)
  143.     {
  144.         FPuts(MyFile,"Error: Read Error\n");
  145.         EXITUS();
  146.     }
  147.  
  148. }    
  149.  
  150. void SeekReloc(char * RELOC_Type)
  151. {
  152.     for(;;)
  153.     {
  154.         ReadLong();
  155.         if ( !Buffer[0]) break;
  156.         Buffer[1]=Buffer[0];
  157.         ReadLong();
  158.         Buffer[2]=Buffer[0];
  159.         if ( (Status=Seek(FileHandle,Buffer[1]<<2,OFFSET_CURRENT)) == -1)
  160.         {
  161.             FPuts(MyFile,"Error: Read Error\n");
  162.             EXITUS();
  163.         }
  164.         FPrintf(MyFile,"%s (Hunk %ld) %ld Bytes\n",RELOC_Type,Buffer[2],Buffer[1]<<2);
  165.     }
  166. }
  167.  
  168. void SeekRelocShort(char * RELOC_Type)
  169. {
  170. int    Bytes;
  171.     Bytes=0;
  172.     for(;;)
  173.     {
  174.         ReadWord();
  175.         Buffer[1]=Buffer[0] >> 16;
  176.         if ( Buffer[1] == NULL ) break;
  177.         ReadWord();
  178.         Buffer[2]=Buffer[0] >> 16;
  179.         if ( (Status=Seek(FileHandle,(Buffer[1]<<1) ,OFFSET_CURRENT)) == -1)
  180.         {
  181.             FPuts(MyFile,"Error: Read Error\n");
  182.             EXITUS();
  183.         }
  184.         FPrintf(MyFile,"%s (Hunk %ld) %ld Bytes\n",RELOC_Type,Buffer[2],Buffer[1]<<2);
  185.  
  186.                 Bytes+=Buffer[2]*2;
  187.     }
  188.         Bytes+=2;    /*ReadWord();==0 */
  189.         if (Bytes & 2)
  190.            ReadWord();
  191. }
  192.  
  193. void ReadString(void)
  194. {
  195. ULONG    length;
  196.  
  197.     ReadLong();
  198.     length=Buffer[0]<<2;
  199.     if ( length > BUFFERSIZE)
  200.     {
  201.         FPuts(MyFile,"Error: Name too long\n");
  202.         EXITUS();
  203.     }
  204.     if (Status=Read(FileHandle,StringBuffer,length) != length)
  205.     {
  206.         if (Status == -1) FPuts(MyFile,"Error: Read Error\n");
  207.         else FPuts(MyFile,"Error: Unexpected File End\n");
  208.         EXITUS();
  209.     }
  210.     ((char *) StringBuffer)[length]='\0';
  211. }
  212.  
  213. void ShowSymbolUnit_Def(char * EXT_Type)
  214. {
  215.     ReadLong();
  216.     FPrintf(MyFile,"  EXT_%s\n",EXT_Type);
  217.     FPrintf(MyFile,"      %s = 0x%lx\n",(char *) StringBuffer,Buffer[0]);
  218. }
  219.  
  220. void ShowSymbolUnit_Ref(char * EXT_Type)
  221. {
  222.     ULONG    i,j;
  223.     ReadLong();
  224.     i=Buffer[0];
  225.     for (j=0;j<i;j++)
  226.     {
  227.         ReadLong();
  228.         FPrintf(MyFile,"  EXT_%s\n",EXT_Type);
  229.         FPrintf(MyFile,"      %s = 0x%lx\n",(char *) StringBuffer,Buffer[0]);
  230.     }
  231. }
  232.  
  233. void ShowSymbolUnit_Common(char * EXT_Type)
  234. {
  235.     ULONG    Size,i,j;
  236.     ReadLong();
  237.     Size=Buffer[0];
  238.     ReadLong();
  239.     i=Buffer[0];
  240.     for (j=0;j<i;j++)
  241.     {
  242.         ReadLong();
  243.         FPrintf(MyFile,"  EXT_%s 0x%ld Bytes\n",EXT_Type,Size);
  244.         FPrintf(MyFile,"      %s = 0x%lx\n",(char *) StringBuffer,Buffer[0]);
  245.     }
  246. }
  247.  
  248.  
  249.  
  250. int ReadSymbolDataUnit(void)
  251. {
  252. ULONG    length,type;
  253.  
  254.     ReadLong();
  255.     if ( ! Buffer[0]) return 0;
  256.     length = (Buffer[0] & 0xffffff) << 2;
  257.     type = (Buffer[0] & 0xff000000) >> 24;
  258.     if ( length > BUFFERSIZE)
  259.     {
  260.         FPuts(MyFile,"Error: Name too long\n");
  261.         EXITUS();
  262.     }
  263.     Buffer[length >> 2]=0;
  264.     if (Status=Read(FileHandle,StringBuffer,length) != length)
  265.     {
  266.         if (Status == -1) FPuts(MyFile,"Error: Read Error\n");
  267.         else FPuts(MyFile,"Error: Unexpected File End\n");
  268.         EXITUS();
  269.     }
  270.     ((char *) StringBuffer)[length]='\0';
  271.     switch ( type )
  272.     {
  273.     case    EXT_DEF:
  274.         ShowSymbolUnit_Def("DEF");
  275.         break;
  276.     case    EXT_ABS:
  277.         ShowSymbolUnit_Def("ABS");
  278.         break;
  279.     case    EXT_RES:
  280.         ShowSymbolUnit_Def("RES");
  281.         break;
  282.     case    EXT_ABSREF32:
  283.         ShowSymbolUnit_Ref("ABSREF32");
  284.         break;
  285.     case    EXT_ABSREF16:
  286.         ShowSymbolUnit_Ref("ABSREF16");
  287.         break;
  288.     case    EXT_ABSREF8:
  289.         ShowSymbolUnit_Ref("ABSREF8");
  290.         break;
  291.     case    EXT_RELREF32:
  292.         ShowSymbolUnit_Ref("RELREF32");
  293.         break;
  294.     case    EXT_RELREF16:
  295.         ShowSymbolUnit_Ref("RELREF16");
  296.         break;
  297.     case    EXT_RELREF8:
  298.         ShowSymbolUnit_Ref("RELREF8");
  299.         break;
  300.     case    EXT_DEXT32:
  301.         ShowSymbolUnit_Ref("DEXT32");
  302.         break;
  303.     case    EXT_DEXT16:
  304.         ShowSymbolUnit_Ref("DEXT16");
  305.         break;
  306.     case    EXT_DEXT8:
  307.         ShowSymbolUnit_Ref("DEXT8");
  308.         break;
  309.     case    EXT_ABSCOMMON:
  310.         ShowSymbolUnit_Common("ABSCOMMON");
  311.         break;
  312.     case    EXT_RELCOMMON:
  313.         ShowSymbolUnit_Common("RELCOMMON");
  314.         break;
  315.     default:
  316.         FPrintf(MyFile,"Error: Unknow EXT Type 0x%lx\n",type);
  317.         EXITUS();
  318.         break;
  319.     }
  320.     return    1;
  321.  
  322.  
  323. }
  324.  
  325. char*    GetMemType(ULONG MemType)
  326.  
  327. {
  328.     switch ( MemType )
  329.     {
  330.     case    0x00000000:
  331.         return ("MEMF_PUBLIC");
  332.         break;
  333.  
  334.     case    0x20000000:
  335.         return ("ADVISORY");
  336.         break;
  337.  
  338.     case    0x40000000:
  339.         return ("MEMF_CHIP");
  340.         break;
  341.  
  342.     case    0x80000000:
  343.         return ("MEMF_FAST");
  344.         break;
  345.  
  346.     case    0xc0000000:
  347.         Buffer[1]=Buffer[0];
  348.         ReadLong();
  349.         FPrintf(MyFile,AttrString,"0x%lx\0",Buffer[0]);
  350.         Buffer[0]=Buffer[1];
  351.         return (AttrString);
  352.         break;
  353.     }
  354. }
  355.  
  356.  
  357.  
  358. int    ReadModule(ULONG    FileType,
  359.                    BOOL        HunkEndFinished)
  360. {
  361.     BYTE    Flag;
  362.     char    *MemString;
  363.         BOOL    LastHunkIsHUNKEND=FALSE;
  364.  
  365.     Flag=-1;
  366.     while ( Flag )
  367.     {
  368.         if ( READLONG !=4 )
  369.         {
  370.                   if (LastHunkIsHUNKEND)
  371.                      return(0);
  372.  
  373.                   FPuts(MyFile,"Error: Read Error\n");
  374.                   EXITUS();
  375.         }
  376.                 LastHunkIsHUNKEND    =    FALSE;
  377.         switch ( Buffer[0] & 0x3fffffff)
  378.         {
  379.     
  380.             case    HUNK_END:
  381.                                 LastHunkIsHUNKEND    =    TRUE;
  382.                 FPuts(MyFile,"HUNK_END\n");
  383.                                 if (HunkEndFinished)
  384.                                    return(TRUE);
  385.                 break;
  386.  
  387.             case    HUNK_NAME:
  388.                 ReadString();
  389.                 FPrintf(MyFile,"HUNK_NAME `%s'\n",(char *) StringBuffer);
  390.                 break;
  391.  
  392.             case    HUNK_CODE:
  393.                 MemString=GetMemType( Buffer[0] & 0xe0000000);
  394.                 SeekLong();
  395.                 FPrintf(MyFile,"HUNK_CODE  %ld Bytes (Memory Type = %s)\n",(Buffer[0] & 0x1fffffff) <<2 ,MemString);
  396.                 break;
  397.  
  398.             case    HUNK_DATA:
  399.                 MemString=GetMemType( Buffer[0] & 0xe0000000);
  400.                 SeekLong();
  401.                 FPrintf(MyFile,"HUNK_DATA  %ld Bytes (Memory Type = %s)\n",(Buffer[0] & 0x1fffffff) <<2 ,MemString);
  402.                 break;
  403.  
  404.             case    HUNK_BSS:
  405.                 MemString=GetMemType( Buffer[0] & 0xe0000000);
  406.                 ReadLong();
  407.                 FPrintf(MyFile,"HUNK_BSS  %ld Bytes (Memory Type = %s)\n",(Buffer[0] & 0x1fffffff) <<2 ,MemString);
  408.                 break;
  409.     
  410.             case    HUNK_DEBUG:
  411.                 SeekLong();
  412.                 FPrintf(MyFile,"HUNK_DEBUG   %ld Bytes\n",(Buffer[0] & 0x1fffffff) <<2 );
  413.                 break;
  414.     
  415.     
  416.             case    HUNK_SYMBOL:
  417.                 FPuts(MyFile,"HUNK_SYMBOL\n");
  418.                 for(;;)
  419.                 {
  420.                     ReadString();
  421.                     if (Buffer[0] == 0) break;
  422.                     ReadLong();
  423.                                         if (Symbol)
  424.                        FPrintf(MyFile,"  %s = 0x%lx\n",StringBuffer,Buffer[0]);
  425.                 }
  426.                 break;
  427.  
  428.             case    HUNK_EXT:
  429.                 FPuts(MyFile,"HUNK_EXT\n");
  430.                 for(;;)
  431.                 {
  432.                     Status=ReadSymbolDataUnit();
  433.                     if (!Status) break;
  434.                 }
  435.                 break;
  436.  
  437.  
  438.             case    HUNK_RELOC32SHORT:
  439.                 SeekRelocShort("HUNK_RELOC32SHORT");
  440.                 break;
  441.  
  442.             case    HUNK_ABSRELOC32:
  443.                 SeekReloc("HUNK_ABSRELOC32");
  444.                 break;
  445.  
  446.             case    HUNK_ABSRELOC16:
  447.                 SeekReloc("HUNK_ABSRELOC16");
  448.                 break;
  449.     
  450.             case    HUNK_RELRELOC32:
  451.                 SeekReloc("HUNK_RELRELOC32");
  452.                 break;
  453.  
  454.             case    HUNK_RELRELOC16:
  455.                 SeekReloc("HUNK_RELRELOC16");
  456.                 break;
  457.  
  458.             case    HUNK_RELRELOC8:
  459.                 SeekReloc("HUNK_RELRELOC8");
  460.                 break;
  461.     
  462. /*            case    HUNK_RELOC32SHORT_V37:*/
  463.             case    HUNK_DREL32:
  464.                 if ( FileType == HUNK_HEADER )
  465.                                    SeekRelocShort("HUNK_RELOC32SHORT");
  466.                 else
  467.                                    SeekReloc("HUNK_DREL32");
  468.                 break;
  469.  
  470.             case    HUNK_DREL16:
  471.                 SeekReloc("HUNK_DREL16");
  472.                 break;
  473.             case    HUNK_DREL8:
  474.                 SeekReloc("HUNK_DREL8");
  475.                 break;
  476.  
  477.             default:
  478.                 Flag=0;
  479.                 if ( (Status=Seek(FileHandle,-4,OFFSET_CURRENT)) == -1)
  480.                 {
  481.                     FPuts(MyFile,"Error: Read Error\n");
  482.                     EXITUS();
  483.                 }
  484.  
  485. /*
  486.                 FPrintf(MyFile,"Error: Unknow Hunk 0x%lx\n",Buffer[0]);
  487.                 EXITUS();
  488. */
  489.                 break;
  490.         }
  491.     
  492.     }
  493.     return -1;
  494. }
  495.  
  496. LONG    *MyArgs[5];
  497.  
  498. char    *ArgumentString = "File,Symbols/S";
  499.  
  500.  
  501. void main(int argc, char **argv)
  502. {
  503.     ULONG    Start,End,Count,i,Condition;
  504.     char    *MemString;
  505.  
  506.         MyFile    =    Output();
  507.     
  508.     FPuts(MyFile,"\x9b""0;33;40m""ShowHunk V1.01 \x9b""0;31;40m""© Ralph Schmidt 1992\n");
  509.     FPuts(MyFile,"Email: laire@uni-paderborn.de\n");
  510.  
  511.         if (!(rdargs=ReadArgs(ArgumentString,(LONG*) &MyArgs[0],NULL)))
  512.         {
  513.           puts("Commandline Error\n");
  514.           return;
  515.         }
  516.  
  517.         Symbol=(BOOL) MyArgs[1];
  518.  
  519.         FileHandle  = Open((STRPTR) MyArgs[0], MODE_OLDFILE);
  520.         if ( ! FileHandle )
  521.     {
  522.         FPrintf(MyFile,"Cannot open %s\n",argv[1]);
  523.                 EXITUS();
  524.         }
  525.  
  526.     while( READLONG )
  527.     {
  528.         switch ( Buffer[0] & 0x3fffffff )
  529.         {
  530.         case    HUNK_HEADER:
  531.                     FPuts(MyFile,"HUNK_HEADER\n");
  532.             while( READLONG && Buffer[0])
  533.             {
  534.                 ReadString();
  535.                 FPrintf(MyFile,"Resident Library: %s\n",(char *) StringBuffer);
  536.             }
  537.             ReadLong();
  538.             Count=Buffer[0];
  539.             ReadLong();
  540.             Start=Buffer[0];
  541.             ReadLong();
  542.             End=Buffer[0];
  543.             FPrintf(MyFile,"%ld Hunks from %ld to %ld\n",Count,Start,End);
  544.             for(i=Start;i<=End;i++)
  545.             {
  546.                 ReadLong();
  547.                 MemString=GetMemType( Buffer[0] & 0xe0000000);
  548.                 FPrintf(MyFile,"Hunk %ld = %ld Bytes (Memory Type = %s)\n",i,(Buffer[0] & 0x1fffffff) <<2 ,(char *) MemString);
  549.             }
  550.             FPuts(MyFile,"\n");
  551.             for(i=Start;i<=End;i++)
  552.             {
  553.                 ReadModule(HUNK_HEADER,TRUE);
  554.                 FPuts(MyFile,"\n");
  555.             }
  556.             break;
  557.  
  558.         case    HUNK_UNIT:
  559.             ReadString();
  560.             FPrintf(MyFile,"HUNK_UNIT `%s'\n",(char *) StringBuffer);
  561.             Condition=-1;
  562. /*            while(Condition)*/
  563.             {
  564.                 Condition=ReadModule(HUNK_UNIT,FALSE);
  565.             }
  566.             break;
  567.  
  568.         case    HUNK_LIB:
  569.             SeekLong();
  570.             FPrintf(MyFile,"HUNK_LIB   %ld Bytes\n",Buffer[0]<<2);
  571.             ReadLong();
  572.             if (Buffer[0] != HUNK_INDEX)
  573.             {
  574.                 FPrintf(MyFile,"Error: Unknow Hunk 0x%lx\n",Buffer[0]);
  575.                 EXITUS();
  576.                 break;
  577.             }
  578.             else
  579.             {
  580.             SeekLong();
  581.             FPrintf(MyFile,"HUNK_INDEX   %ld Bytes\n",Buffer[0]<<2);
  582.             break;
  583.             }
  584.  
  585.         default:
  586.             FPrintf(MyFile,"Error: Unknow Hunk 0x%lx\n",Buffer[0]);
  587.             EXITUS();
  588.             break;
  589.  
  590.         }
  591.         }
  592.         FreeArgs(rdargs);
  593.     Close(FileHandle);
  594. }
  595.