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

  1. // +++Date last modified: 05-Jul-1997
  2.  
  3. /*
  4. **  ARCHIVE.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 "archive.hpp"
  17. #include "whicharc.h"
  18.  
  19. int Archive::Scan (FILE *fp, char *szBuff, long flen)
  20. {
  21.       if (offset > flen)
  22.             return 0;
  23.  
  24.       int rc;
  25.  
  26.       rc = SeekFile (fp, szBuff, offset);
  27.       if (rc > 0)
  28.             rc = Check (szBuff);
  29.  
  30.       return (rc > UNKNOWN ? sfxtype : rc);
  31. }
  32.  
  33. // Generic fingerprint check.
  34. // LHA and PAK/ARC needs more complicated checking
  35. // If some new archiver needs different check algorithm, derive a new
  36. // class from class Archive and write a new Check() function.
  37.  
  38. int Archive::Check (char *szBuff)
  39. {
  40.       char *p;
  41.  
  42.       p = fingerprint;
  43.       while (*p)
  44.       {
  45.             if (*p != *szBuff)
  46.                   return UNKNOWN;
  47.             p++;
  48.             szBuff++;
  49.       }
  50.  
  51.       return type;
  52. }
  53.  
  54. int LhaArchive::Check (char *szBuff)
  55. {
  56.       int i, c;
  57.  
  58.       for (c = 0, i = szBuff[0]; i--; c += (szBuff + 2)[i])
  59.             ;
  60.       if (
  61.           ((unsigned char) (c & 0x00FF)) == szBuff[1]
  62.           && szBuff[2] == fingerprint[0]
  63.           && szBuff[3] == fingerprint[1]
  64.           && szBuff[4] == fingerprint[2]
  65.           && szBuff[6] == fingerprint[3]
  66.          )
  67.       {
  68.             if (szBuff[5] > '1')
  69.             {
  70.                   sfxtype = SFXLHA;
  71.                   return LHA;
  72.             }
  73.             else
  74.             {
  75.                   sfxtype = SFXLHARC;
  76.                   return LHARC;
  77.             }
  78.       }
  79.       else  return UNKNOWN;
  80. }
  81.  
  82. int PakArchive::Check (char *szBuff)
  83. {
  84.       if (szBuff[0] == 0x1A)
  85.       {
  86.             if (szBuff[1] == 0x0A || szBuff[1] == 0x0B)
  87.             {
  88.                   sfxtype = SFXPAK;
  89.                   return PAK;
  90.             }
  91.             else if (szBuff[1] > 0x14)
  92.             {
  93.                   sfxtype = SFXARC6;
  94.                   return ARC6;
  95.             }
  96.             else
  97.             {
  98.                   sfxtype = SFXARC;
  99.                   return ARC;
  100.             }
  101.       }
  102.       else  return UNKNOWN;
  103. }
  104.