home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / WHICHARC.CPP < prev    next >
C/C++ Source or Header  |  1997-07-05  |  7KB  |  274 lines

  1. // +++Date last modified: 05-Jul-1997
  2.  
  3. /*
  4. ** WhichArc.CPP
  5. **
  6. **   Written by Jari Laaksonen (2:221/360.20), 2 Sep 1994
  7. **   Based on the code by Heinz Ozwirk and David Gersic.
  8. **
  9. **   Free for all participants of the C_ECHO & other conferences where
  10. **   this code is posted. Released to SNIPPETS by the authors.
  11. **
  12. **   See the file WhichArc.DOC about archive types and file offsets
  13. **   for self extracting archives.
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <string.h>
  18.  
  19. #include "archive.hpp"
  20. #include "whicharc.h"
  21.  
  22.  
  23. /*
  24. **  Definitions of normal archives, excluding LHA archives
  25. */
  26.  
  27. #define ARCS (sizeof(Arcs)/sizeof(Arcs[0]))
  28.  
  29. Archive Arcs[] = {
  30.       Archive (0,  "PK\x03\x04",    ZIP),
  31.       Archive (0,  "\x60\xEA",      ARJ),
  32.       Archive (0,  "ZOO",           ZOO),
  33.       Archive (0,  "HPAK",          HPK),
  34.       Archive (0,  "Rar!\x1a\x7\0", RAR),
  35.       Archive (0,  "UC2\x1a",       UC2),
  36.       Archive (0,  "HLSQZ",         SQZ)
  37. };
  38.  
  39. /*
  40. **  Definitions of LHA archives
  41. */
  42.  
  43. #define LHARCS (sizeof(LhArcs)/sizeof(LhArcs[0]))
  44.  
  45. LhaArchive LhArcs[] = {
  46.       LhaArchive (0, "-lz-"),
  47.       LhaArchive (0, "-lh-")
  48. };
  49.  
  50. /*
  51. **  Definitions of self-extracting archives, excluding LHA & PAK archives
  52. */
  53.  
  54. #define SFXARCS (sizeof(SfxArcs)/sizeof(SfxArcs[0]))
  55.  
  56. Archive SfxArcs[] = {
  57.       Archive (0x31F0, "PK\x03\x04\x0A", ZIP,   SFXZIP),
  58.       Archive (0x31F0, "PK\x03\x04\x14", ZIP2,  SFXZIP2),
  59.       Archive (0x3CCB, "PK\x03\x04",     ZIP2,  SFXZIP2),
  60.       Archive (0x3D9A, "PK\x03\x04",     ZIP2,  SFXZIP2),
  61.  
  62.       // PkZip 1.02 for OS/2 family mode
  63.       Archive (0x6ED6, "PK\x03\x04\x0A", ZIP,   SFXZIP),
  64.  
  65.       // PkZip 1.02 for OS/2 native mode
  66.       Archive (0x5901, "PK\x03\x04\x0A", ZIP,   SFXZIP),
  67.  
  68.       Archive (0xBBA,  "PK\x03\x04\x0A", ZIP,   SFXZIP),
  69.       Archive (0xBBA,  "PK\x03\x04\x14", ZIP2,  SFXZIP2),
  70.  
  71.       // Zip 2.0.1 for OS/2
  72.       Archive (0x3CE4, "PK\x03\x04\x14", ZIP2,  SFXZIP2),
  73.  
  74.       Archive (0x39BA, "\x60\xEA",       ARJ,   SFXARJ),  // arj 2.22
  75.       Archive (0x1511, "\x60\xEA",       ARJ,   SFXARJ),  // arj 2.22 sfxjr
  76.       Archive (0x3A0A, "\x60\xEA",       ARJ,   SFXARJ),  // arj 2.30
  77.       Archive (0x14D1, "\x60\xEA",       ARJ,   SFXARJ),  // arj 2.30 sfxjr
  78.       Archive (0x3B02, "\x60\xEA",       ARJ,   SFXARJ),  // arj 2.41
  79.       Archive (0x4F4,  "\x60\xEA",       ARJ,   SFXARJ)   // arj 2.41 sfxjr
  80. };
  81.  
  82. /*
  83. **  Definitions of self-extracting LHA archives
  84. */
  85.  
  86. #define SFXLHARCS (sizeof(SfxLhArcs)/sizeof(SfxLhArcs[0]))
  87.  
  88. LhaArchive SfxLhArcs[] = {
  89.       LhaArchive (0x653,  "-lh-"),
  90.       LhaArchive (0x799,  "-lh-"),
  91.       LhaArchive (0x665,  "-lh-"),
  92.       LhaArchive (0x664,  "-lh-"),
  93.       LhaArchive (0x59B1, "-lh-")   // LH for OS/2 2.22
  94. };
  95.  
  96. /*
  97. **  Definitions of self-extracting PAK archives
  98. */
  99.  
  100. #define SFXPAKS (sizeof(SfxPaks)/sizeof(SfxPaks[0]))
  101.  
  102. PakArchive SfxPaks[] = {
  103.       PakArchive (0x261E),
  104.       PakArchive (0x1AD3)
  105. };
  106.  
  107.  
  108. /*
  109. **  Functions begin here
  110. */
  111.  
  112. #ifdef __cplusplus
  113.    extern "C" {
  114. #endif
  115.  
  116. int SeekFile (FILE *fp, char *header, long pos)
  117. {
  118.       memset (header, 0, BUFFSIZE);
  119.       fseek (fp, pos, SEEK_SET);
  120.       return fread (header, 1, BUFFSIZE - 1, fp);
  121. } // SeekFile
  122.  
  123.  
  124. int WhichArc (char *szFileName)
  125. {
  126.       char  header[BUFFSIZE];
  127.  
  128.       memset (header, 0, sizeof (header));
  129.       FILE *fp = fopen (szFileName, "rb");
  130.       if (fp == NULL)
  131.             return ERROR;                    // error opening file
  132.       int n = fread (header, 1, BUFFSIZE - 1, fp);
  133.  
  134.       if (n <= 0)                        // error reading from file
  135.       {
  136.             fclose (fp);
  137.             return ERROR;
  138.       }
  139.  
  140.       int i, rc;
  141.  
  142.       if (header[0]=='M' && header[1]=='Z')   // some sort of .EXE file
  143.       {
  144.             fseek (fp, 0, SEEK_END);
  145.             long flen = ftell (fp);
  146.  
  147.             for (i = 0; i < SFXARCS; i++)
  148.             {
  149.                   rc = SfxArcs[i].Scan (fp, header, flen);
  150.                   if (rc)
  151.                   {
  152.                         fclose (fp);
  153.                         return rc;
  154.                   }
  155.             }
  156.  
  157.             for (i = 0; i < SFXLHARCS; i++)
  158.             {
  159.                   rc = SfxLhArcs[i].Scan (fp, header, flen);
  160.                   if (rc)
  161.                   {
  162.                         fclose (fp);
  163.                         return rc;
  164.                   }
  165.             }
  166.  
  167.             for (i = 0; i < SFXPAKS; i++)
  168.             {
  169.                   rc = SfxPaks[i].Scan (fp, header, flen);
  170.                   if (rc)
  171.                   {
  172.                         fclose (fp);
  173.                         return rc;
  174.                   }
  175.             }
  176.  
  177.             fclose (fp);
  178.             return EXE;
  179.       }
  180.  
  181.       // if file is not .EXE file, define just general types
  182.       // for different archives. Offset parameter == 0 because
  183.       // we do not scan files, just read header from the file start
  184.  
  185.       for (i = 0; i < ARCS; i++)
  186.       {
  187.             rc = Arcs[i].Check (header);
  188.             if (rc)
  189.             {
  190.                   fclose (fp);
  191.                   return rc;
  192.             }
  193.       }
  194.  
  195.       for (i = 0; i < LHARCS; i++)
  196.       {
  197.             rc = LhArcs[i].Check (header);
  198.             if (rc)
  199.             {
  200.                   fclose (fp);
  201.                   return rc;
  202.             }
  203.       }
  204.  
  205.       PakArchive Pak;
  206.  
  207.       rc = Pak.Check (header);
  208.  
  209.       fclose (fp);
  210.  
  211.       return rc;
  212. }
  213.  
  214. #ifdef TEST
  215.  
  216. char *Arctypes[] = {
  217.   "UNKNOWN"    ,
  218.   "ARC"        ,
  219.   "ARC6"       ,
  220.   "PAK"        ,
  221.   "ZOO"        ,
  222.   "HPACK"      ,
  223.   "RAR"        ,
  224.   "UC2"        ,
  225.   "SQZ"        ,
  226.   "ARJ"        ,
  227.   "LARC"       ,
  228.   "LHARC"      ,
  229.   "LHA"        ,
  230.   "ZIP 1.x"    ,
  231.   "ZIP 2.x"    ,
  232.   "SFXARC"     ,
  233.   "SFXARC6"    ,
  234.   "SFXPAK"     ,
  235.   "SFXARJ"     ,
  236.   "SFXLHARC"   ,
  237.   "SFXLHA"     ,
  238.   "SFXZIP 1.x" ,
  239.   "SFXZIP 2.x" ,
  240.   "EXE"        ,
  241. };
  242.  
  243. int main (int argc, char *argv[])
  244. {
  245.       int arc_type;
  246.  
  247.       if (argc != 2)
  248.       {
  249.             printf ("USAGE: WHICHARC arcfile\n");
  250.             return 100;
  251.       }
  252.  
  253.       arc_type = WhichArc (argv[1]);
  254.  
  255.       if (arc_type == -1)
  256.             printf ("File %s not found!\n", argv[1]);
  257.       else
  258.       {
  259.             printf ("%s:\tArchive type is %s\t(return value = %d)\n",
  260.                     argv[1],
  261.                     Arctypes[arc_type],
  262.                     arc_type
  263.                    );
  264.       }
  265.  
  266.       return arc_type;
  267. }
  268.  
  269. #endif
  270.  
  271. #ifdef __cplusplus
  272.    }
  273. #endif
  274.