home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / utilities / checkx / sources / checkx.c next >
Encoding:
C/C++ Source or Header  |  2000-06-26  |  43.7 KB  |  1,499 lines

  1. #define NAME         "CheckX"
  2. #define REVISION     "77"
  3. #define DISTRIBUTION "(Freeware) "
  4. #define DATE         "05.06.2000"
  5.  
  6. /*
  7. This program scans for crunched, linked files and archived files. It
  8. decrunches them and saves the result files to another directory-tree.
  9. The scanning routines are recursiv and thus check really all stuff.
  10.  
  11. The program must be compiled and linked without startup-code. You can set
  12. the pure file protection bit and make it resident, because it is multi-
  13. reentrant (no global variables, except library bases).
  14.  
  15. Crypted archives are not supported yet (e.g. asking for password).
  16. */
  17.  
  18. /* Programmheader
  19.  
  20.     Name:        CheckX
  21.     Author:        SDI
  22.     Distribution:    Freeware
  23.     Description:    scans, decrunches and dearchives files
  24.     Compileropts:    -
  25.     Linkeropts:    -l xpkmaster xadmaster amiga -gsi
  26.  
  27.  1.0   14.12.96 : first Version
  28.  1.1   28.12.96 : moved PassRequest into xpkmaster.library
  29.  1.2   12.02.97 : now also decrunches Exe-Files
  30.  1.3   15.06.97 : added length output as test
  31.  1.4   21.11.97 : renamed from Decrunch, got really new program
  32.  1.5   22.11.97 : bug-fixes
  33.  1.6   29.11.97 : added unarchiving feature
  34.  1.7   30.11.97 : bug-fixes
  35.  1.8   06.12.97 : xpkmaster.library now only required with ASKPWD option
  36.  1.9   07.12.97 : Added archive copy for weird archive names, better error
  37.     codes
  38.  1.10  08.12.97 : fixed error codes a bit
  39.  1.11  11.12.97 : disabled DOS requests, added Zip-Archives, added TaskID
  40.     to temporary filenames
  41.  1.12  12.12.97 : added Arc, ZOO and LhASFX archives
  42.  1.13  13.12.97 : fixed Arc recognition
  43.  1.14  19.12.97 : deletes copied arc before scan
  44.  1.15  22.12.97 : crunched archives are unarchived correctly now
  45.  1.16  02.01.98 : opens dos.library itself, no startup-code required
  46.  1.17  23.01.98 : added disk crunchers
  47.  1.18  24.01.98 : some fixes
  48.  1.19  01.02.98 : little bug-fix in argument-option use
  49.  1.20  06.02.98 : better error output, added automount
  50.  1.21  08.02.98 : little bug-fix
  51.  1.22  10.02.98 : fixed archive copy conditions, added PRINTALL
  52.  1.23  12.02.98 : fixed help text, bug fixes with unlinking and FreeMem
  53.  1.24  13.02.98 : added unstripping
  54.  1.25  04.03.98 : added PRINTEXEC
  55.  1.26  13.03.98 : added high density DMS support
  56.  1.27  19.03.98 : added LOUD keyword
  57.  1.28  23.03.98 : RDx no longer depends on archive depth, but on dddepth
  58.  1.29  10.04.98 : bug fixes
  59.  1.30  26.04.98 : bug fixes
  60.  1.31  09.05.98 : now uses no longer adress 4 for SysBase
  61.  1.32  31.05.98 : better output
  62.  1.33  04.06.98 : added HEADER addition for address files
  63.  1.34  08.08.98 : bug fix with SAVE
  64.  1.35  24.09.98 : added xvs.library virus checks
  65.  1.36  18.10.98 : xvs is opened global and only once
  66.  1.37  30.10.98 : renamed from CheckXFD
  67.  1.38  11.11.98 : fixed format drive bug using a delay and an error report
  68.  1.39  14.11.98 : format error with AUTOMOUNT removed
  69.  1.40  16.11.98 : removed HEADER addition stuff
  70.  1.41  18.11.98 : better RDx: access and mount
  71.  1.42  23.11.98 : fixed bug with hunk stripping
  72.  1.43  29.12.98 : now prints an error, when virus detection is turned off,
  73.     added time calculation and output
  74.  1.44  31.12.98 : little bug fix
  75.  1.45  06.02.99 : bug fixes, added xadmaster.library stuff, removed LOUD
  76.     and internal DMS call
  77.  1.46  09.02.99 : removed internal LZX call
  78.  1.47  11.02.99 : now uses assembler startcode allocating a bigger stack
  79.  1.48  14.02.99 : bug-fixes for nocylinder archivers (PackDev)
  80.  1.49  16.02.99 : fixed strip option (don't know, where the error was :-)
  81.  1.50  21.02.99 : removed internal Zoom call and disk-archiver stuff
  82.  1.51  22.02.99 : fixed archiver call for remaining non-XAD archivers, added
  83.     empty file check
  84.  1.52  23.02.99 : fixed StartCode return value
  85.  1.53  24.02.99 : forgot empty check for archived files
  86.  1.54  08.03.99 : old archiver calling did not work, when file was XAD
  87.     unarchived
  88.  1.55  26.03.99 : prints error, when logfile cannot be created
  89.  1.56  30.03.99 : added bootblock scanning for unarchived disks
  90.  1.57  16.05.99 : bug fix with unlinked file save
  91.  1.58  17.07.99 : removed internal LhA Support
  92.  1.59  30.07.99 : bug fix with file name prints
  93.  1.60  03.08.99 : added DEBUG option
  94.  1.61  05.08.99 : added XVS SelfTest and MemoryTest
  95.  1.62  19.08.99 : added error summary, again fixed name problem
  96.  1.63  14.09.99 : solved big memory loss problem (did not free xadArchiveInfo),
  97.     added logfile comment
  98.  1.64  17.10.99 : removed external archiver calls, added QUIET
  99.  1.65  24.11.99 : now sets nice return values
  100.  1.66  26.11.99 : tries reading again before giving read error
  101.  1.67  03.12.99 : bug fix in name printing
  102.  1.68  04.12.99 : tries again opening files.
  103.  1.69  16.12.99 : added NOSILENT Option
  104.  1.70  15.01.00 : files from disc archives are extracted directly now,
  105.     removed double read tries
  106.  1.71  16.01.00 : fixed error messages for disk unarchiving
  107.  1.72  12.02.00 : added support for ADF images
  108.  1.73  13.02.00 : bug fix for disk archives
  109.  1.74  09.03.00 : now detects XADFIF_NOFILENAME and XADFIF_NOUNCRUNCHSIZE
  110.  1.75  05.04.00 : fixed bug with XADFIF_NOFILENAME
  111.  1.76  26.04.00 : added size checkout for XADFIF_NOUNCRUNCHSIZE
  112.  1.77  05.06.00 : added support for multiple filesystems, removed NODOS and
  113.     diskerr errors, always prints FS-type now.
  114. */
  115.  
  116. #include <proto/exec.h>
  117. #include <proto/dos.h>
  118. #include <proto/xfdmaster.h>
  119. #include <proto/xadmaster.h>
  120. #include <proto/xpkmaster.h>
  121. #include <proto/intuition.h>
  122. #include <proto/xvs.h>
  123. #include <proto/utility.h>
  124. #include <libraries/xfdmaster.h>
  125. #include <dos/dostags.h>
  126. #include <dos/doshunks.h>
  127. #include <dos/filehandler.h>
  128. #include <exec/memory.h>
  129. #include "SDI_version.h" /* make version string */
  130. #include "SDI_compiler.h"
  131. #define SDI_TO_ANSI
  132. #include "SDI_ASM_STD_protos.h"
  133.  
  134. #define PARAM   "FROM,LOG,SAVE/K,ALL/S,ASKPWD/S,PRINTALL/S,PRINTEXEC/S," \
  135.         "NODECRUNCH/S,NOUNLINK/S,NOUNARCHIVE/S,NOUNTRACK/S,"     \
  136.         "NOSILENT/S,NOSTRIP/S,NOVIRUS/S,DEBUG/S,QUIET/S"
  137.  
  138. void RawPutChar(ULONG c);
  139.  
  140. #ifdef __SASC
  141.   #pragma libcall SysBase RawPutChar 204 001
  142. #elif defined(__GNUC__)
  143.   #define RawPutChar(c) LP1NR(204, RawPutChar, ULONG, c, d0, , SysBase)
  144. #else
  145.   #pragma amicall(SysBase,0x204,RawPutChar(d0))
  146. #endif
  147.  
  148. void KPutC(ULONG c)
  149. {
  150.   RawPutChar(c);
  151. }
  152.  
  153. void KPrintf(STRPTR fmt, ...)
  154. {
  155.   RawDoFmt(fmt, &fmt + 1, (void (*)()) KPutC, 0);
  156. }
  157.  
  158. #ifdef __SASC
  159.   #define XpkBase    xpkbase
  160.   #define ASSIGN_XPK
  161.   #define IntuitionBase    intuitionbase
  162.   #define ASSIGN_INT
  163.   #define UtilityBase    utilitybase
  164.   #define ASSIGN_UTIL
  165. #else
  166.   struct Library *    XpkBase        = 0;
  167.   struct IntuitionBase *IntuitionBase    = 0;
  168.   struct UtilityBase *  UtilityBase     = 0;
  169.   #define ASSIGN_XPK    XpkBase = xpkbase;
  170.   #define ASSIGN_INT    IntuitionBase = intuitionbase;
  171.   #define ASSIGN_UTIL    UtilityBase = utilitybase;
  172. #endif
  173. struct xfdMasterBase *    xfdMasterBase    = 0;
  174. struct DosLibrary *    DOSBase        = 0;
  175. struct ExecBase *    SysBase        = 0;
  176. struct xvsBase *    xvsBase        = 0;
  177. struct xadMasterBase *    xadMasterBase    = 0;
  178.  
  179. struct Args {
  180.   STRPTR from;
  181.   STRPTR log;
  182.   STRPTR save;
  183.   ULONG  all;
  184.   ULONG  askpwd;
  185.   ULONG  printall;
  186.   ULONG  printexec;
  187.   ULONG  nodecrunch;
  188.   ULONG  nounlink;
  189.   ULONG  nounarchive;
  190.   ULONG  nountrack;
  191.   ULONG  nosilent;
  192.   ULONG  nostrip;
  193.   ULONG  novirus;
  194.   ULONG  debug;
  195.   ULONG     quiet;
  196. };
  197.  
  198. struct CrunchMemList {
  199.   struct CrunchMemList * cml_Next;
  200.   APTR             cml_MemoryRegion;
  201.   ULONG             cml_MemorySize;
  202. };
  203.  
  204. #define CHECKXFLAG_SAVE            (1<< 0)
  205. #define CHECKXFLAG_ALL            (1<< 1)
  206. #define CHECKXFLAG_ASKPWD        (1<< 2)
  207. #define CHECKXFLAG_PRINTALL        (1<< 3)
  208. #define CHECKXFLAG_PRINTEXEC        (1<< 4)
  209. #define CHECKXFLAG_NODECRUNCH        (1<< 5)
  210. #define CHECKXFLAG_NOUNLINK        (1<< 6)
  211. #define CHECKXFLAG_NOUNARCHIVE        (1<< 7)
  212. #define CHECKXFLAG_NOUNTRACK        (1<< 8)
  213. #define CHECKXFLAG_NOSTRIP        (1<< 9)
  214. #define CHECKXFLAG_DEBUG        (1<<10)
  215. #define CHECKXFLAG_QUIET        (1<<11)
  216.  
  217. #define CHECKXFLAG_XVSLIB        (1<<20)
  218.  
  219. #define CHKXCALLFLAGS    (CHECKXFLAG_SAVE|CHECKXFLAG_ALL|        \
  220.              CHECKXFLAG_ASKPWD|CHECKXFLAG_PRINTALL|        \
  221.              CHECKXFLAG_PRINTEXEC|CHECKXFLAG_NODECRUNCH|    \
  222.              CHECKXFLAG_NOUNLINK|CHECKXFLAG_NOUNARCHIVE|    \
  223.              CHECKXFLAG_NOUNTRACK|CHECKXFLAG_NOSTRIP|    \
  224.              CHECKXFLAG_DEBUG|CHECKXFLAG_QUIET|        \
  225.              CHECKXFLAG_XVSLIB)
  226.  
  227. #define CHKXSAVEFLAGS    (CHECKXFLAG_LINKED|CHECKXFLAG_CRUNCHED|        \
  228.              CHECKXFLAG_STRIPPED)
  229.  
  230. #define CHECKXFLAG_NAMEPRINTED        (1<<26)
  231. #define CHECKXFLAG_CRUNCHED        (1<<27)
  232. #define CHECKXFLAG_LINKED        (1<<28)
  233. #define CHECKXFLAG_ADDRESS        (1<<29)
  234. #define CHECKXFLAG_NOFREEMEM        (1<<30)
  235. #define CHECKXFLAG_STRIPPED        (1<<31)
  236.  
  237. #define CHXWARN_OFFSET        30
  238. #define XADERR_OFFSET        0x100
  239. #define XFDERR_OFFSET        0x200
  240.  
  241. #define CHKXERR_NOMEMORY    1
  242. #define CHKXERR_EXAMINEERR    2
  243. #define CHKXERR_OPENERR        3
  244. #define CHKXERR_READ        4
  245. #define CHKXERR_SCANERR        5
  246. #define CHKXERR_BREAK        6
  247. #define CHKXERR_OPENDIR        7
  248. #define CHKXERR_RESOURCE    8
  249. #define CHKXERR_NOBOOTVIRUS    9
  250. #define CHKXERR_WRITE        10
  251. #define CHKXERR_NOVIRUS        11
  252. #define CHKXERR_EMPTY        12
  253.  
  254. #define CHKXWARN_NOVIRUS    31
  255. #define CHKXWARN_XVSSELFTEST    32
  256. #define    CHKXWARN_MEMVIRUS    33
  257.  
  258. struct FileData {
  259.   struct CrunchMemList * fd_MemList;
  260.   STRPTR         fd_Name;
  261.   ULONG           fd_LogFileFH;
  262.   ULONG           fd_SaveDirL;
  263.   ULONG             fd_Flags;
  264.   ULONG             fd_NumVirus;
  265.   ULONG             fd_CHKXErrors;
  266.   ULONG             fd_XFDErrors;
  267.   ULONG             fd_XADErrors;
  268.   ULONG             fd_CorruptedArchives;
  269.   BYTE           fd_RecurseDepth;
  270.   UBYTE           fd_LinkNum;
  271.   UBYTE             fd_ArchiveDepth;
  272. };
  273.  
  274. static void SetLogComment(STRPTR);
  275. static LONG DoDirectoryScan(STRPTR, STRPTR, struct FileData *);
  276. static LONG DoFileOpen(struct FileData *);
  277. static LONG DoGetVirus(struct FileData *, APTR, ULONG);
  278. ASM(LONG) OutHookCheckX(REG(a0, struct Hook *hook), REG(a1, struct xadHookParam *hp));
  279. static LONG DoFileUnArchive(struct FileData *, APTR, ULONG);
  280. static LONG DoFileUnLink(struct FileData *, APTR, ULONG);
  281. static LONG DoFileUnCrunch(struct FileData *, APTR, ULONG);
  282. static LONG DoFileStrip(struct FileData *, APTR, ULONG);
  283. static void PrintCHKXFile(struct FileData *);
  284. static void PrintCHKXErr(struct FileData *, LONG);
  285. static void PrintCHKXTxt(struct FileData *, STRPTR, ...);
  286. static LONG AddCrunchMemList(struct FileData *, APTR, ULONG);
  287. static void FreeCrunchMemList(struct FileData *, APTR);
  288. static LONG SaveUncrFile(struct FileData *, APTR, ULONG);
  289. static ULONG OpenParentDir(struct FileData *);
  290. static ULONG OpenNewDir(struct FileData *, STRPTR);
  291.  
  292. /* All memory regions must be in mem list. All unneeded memory must be freed
  293.    as fast as possible (after unlinking, decrunching), as well as the
  294.    MemoryList structure.
  295.    
  296.    The program has a loop like scan routine system, which is called for
  297.    every file:
  298.  
  299.    A) Scan files, directories and sub directories and call following for
  300.       every file:
  301.    1) Check for viruses.
  302.    2) Test if it is an archive. When yes decrunch and start for every file
  303.       with point 1.
  304.    3) Test if file is linked. When yes unlink and call point 1 for both
  305.       parts.
  306.    4) Test if file is crunched. When yes, decrunch and start again with
  307.       point 1.
  308.    5) Try stripping useless stuff. When successful start with point 1 again.
  309.    6) Possibly save file (with SAVE option) or end loop here.
  310. */
  311.  
  312. /* main routine, do argument parsing */
  313. LONG start(void)
  314. {
  315.   LONG error = RETURN_FAIL;
  316.   struct DosLibrary *dosbase;
  317.   struct Process *task;
  318.  
  319.   SysBase = (*((struct ExecBase **) 4));
  320.  
  321.   /* test for WB and reply startup-message */
  322.   if(!(task = (struct Process *) FindTask(0))->pr_CLI)
  323.   {
  324.     WaitPort(&task->pr_MsgPort);
  325.     Forbid();
  326.     ReplyMsg(GetMsg(&task->pr_MsgPort));
  327.     return RETURN_FAIL;
  328.   }
  329.  
  330.   if((dosbase = (struct DosLibrary *) OpenLibrary("dos.library", 37)))
  331.   {
  332.     struct xfdMasterBase *xfdmasterbase;
  333.  
  334.     DOSBase = dosbase;
  335.     if((xfdmasterbase = (struct xfdMasterBase *) OpenLibrary("xfdmaster.library", 37)))
  336.     {
  337.       struct xadMasterBase *xadmasterbase;
  338.  
  339.       xfdMasterBase = xfdmasterbase;
  340.       if((xadmasterbase = (struct xadMasterBase *) OpenLibrary("xadmaster.library", 7)))
  341.       {
  342.         struct Args Args;
  343.         struct RDArgs *rda;
  344.  
  345.     xadMasterBase = xadmasterbase;
  346.         memset(&Args, 0, sizeof(struct Args));
  347.  
  348.         if((rda = (struct RDArgs *) AllocDosObject(DOS_RDARGS, 0)))
  349.         {
  350.           rda->RDA_ExtHelp =
  351.           "FROM        source file or directory - may contain patterns\n"
  352.           "LOG         log file name\n"
  353.           "SAVE        directory, where decrunched files are saved\n"
  354.           "ALL         scan deep into directories\n"
  355.           "ASKPWD      ask for password when needed (needs xpkmaster.library)\n"
  356.       "PRINTALL    print all filenames\n"
  357.           "PRINTEXEC   print names of all executable files\n"
  358.           "NODECRUNCH  do not decrunch files with xfdmaster\n"
  359.           "NOUNLINK    do not unlink files with xfdmaster\n"
  360.           "NOUNARCHIVE do not unarchive file archives\n"
  361.           "NOUNTRACK   do not unarchive track archives\n"
  362.           "NOSILENT    do not disable dos requests\n"
  363.           "NOSTRIP     do not strip useless hunks\n"
  364.           "NOVIRUS     do not scan with xvs.library for viruses\n"
  365.           "DEBUG       also output texts to serial debug engine\n"
  366.           "QUIET       do not output texts to console\n";
  367.  
  368.           if(ReadArgs(PARAM, (LONG *) &Args, rda))
  369.           {
  370.             ULONG flags = 0, log = 0;
  371.             struct xvsBase *xvsbase = 0;
  372.  
  373.             if(!Args.from)        Args.from = "#?";
  374.             if(Args.all)        flags |= CHECKXFLAG_ALL;
  375.             if(Args.save)        flags |= CHECKXFLAG_SAVE;
  376.             if(Args.askpwd)        flags |= CHECKXFLAG_ASKPWD;
  377.         if(Args.printall)        flags |= CHECKXFLAG_PRINTALL;
  378.         if(Args.printexec)        flags |= CHECKXFLAG_PRINTEXEC;
  379.             if(Args.nodecrunch)        flags |= CHECKXFLAG_NODECRUNCH;
  380.             if(Args.nounlink)         flags |= CHECKXFLAG_NOUNLINK;
  381.             if(Args.nounarchive)    flags |= CHECKXFLAG_NOUNARCHIVE;
  382.             if(Args.nountrack)        flags |= CHECKXFLAG_NOUNTRACK;
  383.             if(Args.nostrip)        flags |= CHECKXFLAG_NOSTRIP;
  384.             if(Args.debug)        flags |= CHECKXFLAG_DEBUG;
  385.             if(Args.quiet)        flags |= CHECKXFLAG_QUIET;
  386.  
  387.             if(!Args.novirus)
  388.             {
  389.               if((xvsbase = (struct xvsBase *) OpenLibrary("xvs.library", 33)))
  390.               {
  391.                 flags |= CHECKXFLAG_XVSLIB;
  392.                 xvsBase = xvsbase;
  393.               }
  394.         }
  395.  
  396.             if(!Args.log || (log = Open(Args.log, MODE_READWRITE)))
  397.             {
  398.           struct FileData fd;
  399.           APTR win;
  400.           ULONG s1 = 0, s2 = 0, msecs;
  401.           struct IntuitionBase *intuitionbase;
  402.  
  403.           win = task->pr_WindowPtr;
  404.           if(!Args.nosilent)
  405.             task->pr_WindowPtr = (APTR) -1;
  406.           /* prevent dos requests */
  407.  
  408.           if(log)
  409.           {
  410.             SetFileSize(log, 0, OFFSET_BEGINNING);
  411.             SetProtection(Args.log, FIBF_EXECUTE);
  412.             SetLogComment(Args.log);
  413.           }
  414.  
  415.           memset(&fd, 0, sizeof(struct FileData));
  416.               fd.fd_Flags = flags | CHECKXFLAG_NAMEPRINTED; /* for possible warnings display */
  417.               fd.fd_LogFileFH = log;
  418.  
  419.           if(!xvsbase)
  420.             PrintCHKXErr(&fd, CHKXWARN_NOVIRUS);
  421.           else
  422.           {
  423.                 struct xvsMemoryInfo *mi;
  424.  
  425.             if(!xvsSelfTest())
  426.               PrintCHKXErr(&fd, CHKXWARN_XVSSELFTEST);
  427.  
  428.                   if((mi = (struct xvsMemoryInfo *) xvsAllocObject(XVSOBJ_MEMORYINFO)))
  429.                   { 
  430.                     if(xvsSurveyMemory(mi))
  431.                 PrintCHKXErr(&fd, CHKXWARN_MEMVIRUS);
  432.  
  433.                     xvsFreeObject(mi);
  434.                   }
  435.           }
  436.  
  437.           if((intuitionbase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37)))
  438.           {
  439.             ASSIGN_INT
  440.             CurrentTime(&s1, &msecs);
  441.           }    
  442.  
  443.           ++fd.fd_RecurseDepth;
  444.           error = DoDirectoryScan(Args.from, Args.save, &fd) ? RETURN_FAIL : RETURN_OK;
  445.           --fd.fd_RecurseDepth;
  446.           fd.fd_Flags |= CHECKXFLAG_NAMEPRINTED; /* for result texts */
  447.  
  448.           task->pr_WindowPtr = win;
  449.  
  450.           if(intuitionbase)
  451.           {
  452.             CurrentTime(&s2, &msecs);
  453.             s2 -= s1;
  454.             s1 = s2 / 60;
  455.             s2 %= 60;
  456.             msecs = s1 / 60;
  457.             s1 %= 60;
  458.             PrintCHKXTxt(&fd, "\nTime needed for check: %2ld:%02ld:%02ld", msecs, s1, s2);
  459.             CloseLibrary((struct Library *) intuitionbase);
  460.           }
  461.           if(fd.fd_NumVirus)
  462.             PrintCHKXTxt(&fd, "The scan detected %ld virus%s.", fd.fd_NumVirus, fd.fd_NumVirus > 1 ? "es" : "");
  463.           if(fd.fd_CHKXErrors || fd.fd_XADErrors || fd.fd_XFDErrors || fd.fd_CorruptedArchives)
  464.             PrintCHKXTxt(&fd, "There were errors (CheckX/XFD/XAD/corrupted archives): %ld/%ld/%ld/%ld.",
  465.             fd.fd_CHKXErrors, fd.fd_XFDErrors, fd.fd_XADErrors, fd.fd_CorruptedArchives);
  466.           if(!error && (fd.fd_NumVirus || !xvsBase))
  467.           {
  468.             SetIoErr(0);
  469.             error = RETURN_WARN;
  470.           }
  471.  
  472.               if(log)
  473.                 Close(log);
  474.             }
  475.             else if(Args.log && !Args.quiet)
  476.               Printf("Could not create logfile.\n");
  477.  
  478.         if(xvsbase)
  479.           CloseLibrary((struct Library *) xvsbase);
  480.             FreeArgs(rda);
  481.           }
  482.           FreeDosObject(DOS_RDARGS, rda);
  483.         }
  484.     CloseLibrary((struct Library *) xadmasterbase);
  485.       }
  486.       CloseLibrary((struct Library *) xfdmasterbase);
  487.     }
  488.     CloseLibrary((struct Library *) dosbase);
  489.   }
  490.  
  491.   return error;
  492. }
  493.  
  494. static void SetLogComment(STRPTR name)
  495. {
  496.   UBYTE com[30];
  497.   struct MsgPort *TimerMP;
  498.  
  499.   if((TimerMP = CreateMsgPort()))
  500.   {
  501.     struct timerequest *TimerIO;
  502.  
  503.     if((TimerIO = (struct timerequest *) CreateIORequest(TimerMP,
  504.     sizeof(struct timerequest))))
  505.     {
  506.       if(!OpenDevice("timer.device",UNIT_VBLANK,
  507.       (struct IORequest *)TimerIO,0))
  508.       {
  509.         struct UtilityBase *utilitybase;
  510.  
  511.         TimerIO->tr_node.io_Command = TR_GETSYSTIME;
  512.         DoIO((struct IORequest *) TimerIO);
  513.         if((utilitybase = (struct UtilityBase *) OpenLibrary("utility.library",37)))
  514.         {
  515.           struct ClockData dat;
  516.  
  517.       ASSIGN_UTIL
  518.           Amiga2Date(TimerIO->tr_time.tv_secs, &dat);
  519.           sprintf(com, "CheckX " VERSION "." REVISION " - %02ld.%02ld.%ld", dat.mday, dat.month, dat.year);
  520.       CloseLibrary((struct Library *) utilitybase);
  521.       SetComment(name, com);
  522.         }
  523.         CloseDevice((struct IORequest *) TimerIO);
  524.       }
  525.       DeleteIORequest(TimerIO);
  526.     }
  527.     DeleteMsgPort(TimerMP);
  528.   }
  529. }
  530.  
  531. /* This scans a directory and calls DoFileOpen for every file. It
  532.    automatically creates SAVE destination directories when necessary. */
  533. static LONG DoDirectoryScan(STRPTR name, STRPTR sav, struct FileData *fd)
  534. {
  535.   struct AnchorPath *APath;
  536.   LONG error = CHKXERR_SCANERR;
  537.   ULONG retval;
  538.  
  539.   if(!(fd->fd_Flags & CHECKXFLAG_SAVE) || !sav ||
  540.   (fd->fd_SaveDirL = Lock(sav, SHARED_LOCK)))
  541.   {
  542.     if((APath = (struct AnchorPath *) AllocMem(sizeof(struct AnchorPath) +
  543.     512, MEMF_PUBLIC|MEMF_CLEAR)))
  544.     {
  545.       fd->fd_Name = APath->ap_Buf;
  546.       APath->ap_Strlen = 256;
  547.       for(retval = MatchFirst(name, APath); !retval;
  548.       retval = MatchNext(APath))
  549.       {
  550.         if(APath->ap_Flags & APF_DIDDIR)
  551.         {
  552.           OpenParentDir(fd);
  553.           APath->ap_Flags &= ~APF_DIDDIR; /* clear flag */
  554.         }
  555.         else if(APath->ap_Info.fib_DirEntryType > 0)
  556.         {
  557.           if(fd->fd_Flags & CHECKXFLAG_ALL)
  558.           {
  559.             OpenNewDir(fd, APath->ap_Info.fib_FileName);
  560.             APath->ap_Flags |= APF_DODIR;
  561.           }
  562.         }
  563.         else
  564.         {
  565.       fd->fd_Flags &= CHKXCALLFLAGS;
  566.           fd->fd_LinkNum = 0;
  567.           PrintCHKXErr(fd, DoFileOpen(fd));
  568.  
  569.           while(fd->fd_MemList)
  570.             FreeCrunchMemList(fd, fd->fd_MemList->cml_MemoryRegion);
  571.         }
  572.         if((fd->fd_Flags & CHECKXFLAG_SAVE) && !fd->fd_SaveDirL)
  573.         {
  574.           error = CHKXERR_OPENDIR; break;
  575.         }
  576.         if(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)
  577.         {
  578.           error = CHKXERR_BREAK; break;
  579.         }
  580.       }
  581.       MatchEnd(APath);
  582.  
  583.       if(retval == ERROR_NO_MORE_ENTRIES)
  584.         error = 0;
  585.  
  586.       FreeMem(APath, sizeof(struct AnchorPath) + 512);
  587.     }
  588.     else
  589.       error = CHKXERR_NOMEMORY;
  590.  
  591.     if(sav && fd->fd_SaveDirL)
  592.       UnLock(fd->fd_SaveDirL);
  593.   }
  594.   else
  595.     error = CHKXERR_OPENDIR;
  596.  
  597.   if(error)
  598.     PrintCHKXErr(fd, error);
  599.  
  600.   return error;
  601. }
  602.  
  603. /* Open a file and call DoGetVirus to scan */
  604. static LONG DoFileOpen(struct FileData *fd)
  605. {
  606.   struct FileInfoBlock *fib;
  607.   LONG ret = 0;
  608.  
  609.   if((fib = (struct FileInfoBlock *) AllocDosObject(DOS_FIB, 0)))
  610.   {
  611.     ULONG fh;
  612.  
  613.     if((fh = Open(fd->fd_Name, MODE_OLDFILE)))
  614.     {
  615.       if(ExamineFH(fh, fib))
  616.       {
  617.         APTR mem;
  618.  
  619.     if(!fib->fib_Size)
  620.           ret = CHKXERR_EMPTY;
  621.         else if((mem = AllocMem(fib->fib_Size, MEMF_ANY)))
  622.         {
  623.           if(Read(fh, mem, fib->fib_Size) != fib->fib_Size)
  624.             ret = CHKXERR_READ;
  625.  
  626.           if(!ret && !(ret = AddCrunchMemList(fd, mem, fib->fib_Size)))
  627.           {
  628.             if((fd->fd_Flags & CHECKXFLAG_PRINTALL) ||
  629.             ((fd->fd_Flags & CHECKXFLAG_PRINTEXEC) && fib->fib_Size > 4
  630.             && *((ULONG *)mem) == HUNK_HEADER))
  631.               PrintCHKXFile(fd);
  632.             ret = DoGetVirus(fd, mem, fib->fib_Size);
  633.           }
  634.           else
  635.             FreeMem(mem, fib->fib_Size);
  636.         }
  637.         else
  638.           ret = CHKXERR_NOMEMORY;
  639.       }
  640.       else
  641.         ret = CHKXERR_EXAMINEERR;
  642.       Close(fh);
  643.     }
  644.     else
  645.       ret = CHKXERR_OPENERR;
  646.   
  647.     FreeDosObject(DOS_FIB, fib);
  648.   }
  649.   else
  650.     ret = CHKXERR_NOMEMORY;
  651.  
  652.   return ret;
  653. }
  654.  
  655. /* for SAVE option: open parent directory and try to delete the directory
  656.    we leave. This only succeeds, when the directory is empty. */
  657. static ULONG OpenParentDir(struct FileData *fd)
  658. {
  659.   ULONG g;
  660.   UBYTE name[300];
  661.  
  662.   if((g = fd->fd_SaveDirL))
  663.   {
  664.     NameFromLock(g, name, 300);
  665.     fd->fd_SaveDirL = ParentDir(g);
  666.     UnLock(g);
  667.  
  668.     DeleteFile(name);
  669.   }
  670.   else
  671.     return -1;
  672.  
  673.   return fd->fd_SaveDirL;
  674. }
  675.  
  676. /* Open a new subdirectory for SAVE option */
  677. static ULONG OpenNewDir(struct FileData *fd, STRPTR name)
  678. {
  679.   ULONG g;
  680.  
  681.   if(fd->fd_SaveDirL)
  682.   {
  683.     g = CurrentDir(fd->fd_SaveDirL);
  684.     if(!(fd->fd_SaveDirL = Lock(name, SHARED_LOCK)))
  685.       if((fd->fd_SaveDirL = CreateDir(name)))
  686.         ChangeMode(CHANGE_LOCK, fd->fd_SaveDirL, SHARED_LOCK);
  687.     UnLock(CurrentDir(g));
  688.   }
  689.   else
  690.     return -1;
  691.  
  692.   return fd->fd_SaveDirL;
  693. }
  694.  
  695. static LONG DoGetVirus(struct FileData *fd, APTR buffer, ULONG buflength)
  696. {
  697.   if(!buflength) /* restarted with empty file */
  698.     return CHKXERR_EMPTY;
  699.  
  700.   if(fd->fd_Flags & CHECKXFLAG_XVSLIB)
  701.   {
  702.     APTR mem;
  703.       
  704.     if((mem = AllocMem(buflength, MEMF_ANY)))
  705.     {
  706.       struct xvsFileInfo *fi;
  707.       if((fi = (struct xvsFileInfo *) xvsAllocObject(XVSOBJ_FILEINFO)))
  708.       {
  709.         ULONG i;
  710.     /* xvs may modify the buffer! */
  711.         CopyMem(buffer, mem, buflength);
  712.         fi->xvsfi_File = mem;
  713.         fi->xvsfi_FileLen = buflength;
  714.         i = xvsCheckFile(fi);
  715.         if(i == XVSFT_DATAVIRUS)
  716.         {
  717.        PrintCHKXTxt(fd, "Data-Virus '%s'", fi->xvsfi_Name);
  718.        ++fd->fd_NumVirus;
  719.      }
  720.         else if(i == XVSFT_FILEVIRUS)
  721.         {
  722.        PrintCHKXTxt(fd, "File-Virus '%s'", fi->xvsfi_Name);
  723.        ++fd->fd_NumVirus;
  724.      }
  725.         else if(i == XVSFT_LINKVIRUS)
  726.         {
  727.        PrintCHKXTxt(fd, "Link-Virus '%s'", fi->xvsfi_Name);
  728.        ++fd->fd_NumVirus;
  729.      }
  730.         xvsFreeObject(fi);
  731.       }
  732.       else
  733.         PrintCHKXErr(fd, CHKXERR_NOVIRUS);
  734.       FreeMem(mem, buflength);
  735.     }
  736.     else
  737.       PrintCHKXErr(fd, CHKXERR_NOVIRUS);
  738.   }
  739.  
  740.   return DoFileUnArchive(fd, buffer, buflength);
  741. }
  742.  
  743. /* For bootblocks */
  744. ASM(LONG) OutHookCheckX(REG(a0, struct Hook *hook), REG(a1, struct xadHookParam *hp))
  745. {
  746.   ULONG i;
  747.  
  748.   switch(hp->xhp_Command)
  749.   {
  750.   case XADHC_WRITE:
  751.     if((i = 1024-hp->xhp_DataPos) > hp->xhp_BufferSize)
  752.       i = hp->xhp_BufferSize;
  753.     CopyMem(hp->xhp_BufferPtr, ((STRPTR)(hook->h_Data))+hp->xhp_DataPos, i);
  754.     hp->xhp_DataPos += i;
  755.     if(hp->xhp_DataPos == 1024)
  756.       return 10000; /* CheckX own error code for error detection */
  757.   case XADHC_INIT:
  758.   case XADHC_FREE:
  759.   case XADHC_ABORT:
  760.     break;
  761.   default: return XADERR_NOTSUPPORTED;
  762.   }
  763.  
  764.   return 0;
  765. }
  766.  
  767. /* To get real file size */
  768. ASM(LONG) OutHookCheckXSize(REG(a0, struct Hook *hook), REG(a1, struct xadHookParam *hp))
  769. {
  770.   switch(hp->xhp_Command)
  771.   {
  772.   case XADHC_WRITE:
  773.     hp->xhp_DataPos += hp->xhp_BufferSize;
  774.     hook->h_Data = (APTR) hp->xhp_DataPos;
  775.   case XADHC_INIT:
  776.   case XADHC_FREE:
  777.   case XADHC_ABORT:
  778.     break;
  779.   default: return XADERR_NOTSUPPORTED;
  780.   }
  781.  
  782.   return 0;
  783. }
  784.  
  785. static void CheckArcFiles(struct FileData *fd, struct xadArchiveInfo *ai, ULONG disk)
  786. {
  787.   struct xadFileInfo *fi;
  788.   struct TagItem ti[4];
  789.  
  790.   ti[0].ti_Tag = XAD_OUTMEMORY;
  791.   ti[1].ti_Tag = XAD_OUTSIZE;
  792.   ti[2].ti_Tag = XAD_ENTRYNUMBER;
  793.   ti[3].ti_Tag = TAG_DONE;
  794.  
  795.   fi = ai->xai_FileInfo;
  796.   while(!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C) && fi)
  797.   {
  798.     APTR dest;
  799.  
  800.     if(!(fi->xfi_Flags & (XADFIF_DIRECTORY|XADFIF_LINK)))
  801.     {
  802.       LONG err = 0;
  803.       ULONG flags, size;
  804.  
  805.       flags = fd->fd_Flags;
  806.       if(!(fi->xfi_Flags & XADFIF_NOFILENAME))
  807.         fd->fd_Flags &= ~CHECKXFLAG_NAMEPRINTED;
  808.       fd->fd_Name = fi->xfi_FileName;
  809.       fd->fd_RecurseDepth++;
  810.       fd->fd_ArchiveDepth++;
  811.  
  812.       size = fi->xfi_Size;
  813.       if(fi->xfi_Flags & XADFIF_NOUNCRUNCHSIZE)
  814.       {
  815.     struct Hook hook = {{0,0},(ULONG (*)()) OutHookCheckXSize, 0, 0};
  816.       struct TagItem ti[3];
  817.  
  818.     ti[0].ti_Tag = XAD_OUTHOOK;
  819.     ti[0].ti_Data = (ULONG) &hook;
  820.       ti[1].ti_Tag = XAD_ENTRYNUMBER;
  821.     ti[1].ti_Data = fi->xfi_EntryNumber;
  822.       ti[2].ti_Tag = TAG_DONE;
  823.  
  824.         if(!(err = (disk ? xadDiskFileUnArcA(ai, ti) : xadFileUnArcA(ai, ti))))
  825.       size = (ULONG) hook.h_Data;
  826.       }
  827.  
  828.       if(!size)
  829.       {
  830.         if(!err)
  831.           err = CHKXERR_EMPTY;
  832.       }
  833.       else if((dest = AllocMem(size, MEMF_PUBLIC)))
  834.       {
  835.         if(!(err = AddCrunchMemList(fd, dest, size)))
  836.         {
  837.       ti[0].ti_Data = (ULONG) dest;
  838.       ti[1].ti_Data = size;
  839.       ti[2].ti_Data = fi->xfi_EntryNumber;
  840.  
  841.           if((err = disk ? xadDiskFileUnArcA(ai, ti) : xadFileUnArcA(ai, ti)))
  842.       {
  843.         if(disk)
  844.           err = CHKXERR_READ;
  845.         else
  846.               err += XADERR_OFFSET;
  847.           }
  848.           else
  849.           {
  850.             if((fd->fd_Flags & CHECKXFLAG_PRINTALL) ||
  851.         ((fd->fd_Flags & CHECKXFLAG_PRINTEXEC) && size > 4
  852.         && *((ULONG *)dest) == HUNK_HEADER))
  853.               PrintCHKXFile(fd);
  854.             err = DoGetVirus(fd, dest, size);
  855.           }
  856.           FreeCrunchMemList(fd, dest);
  857.         }
  858.         else
  859.           FreeMem(dest, size);
  860.       }
  861.       else
  862.         err = CHKXERR_NOMEMORY;
  863.  
  864.       if(err)
  865.       {
  866.         if(fi->xfi_Flags & XADFIF_NOFILENAME)
  867.           fd->fd_Flags &= ~CHECKXFLAG_NAMEPRINTED;
  868.         PrintCHKXErr(fd, err);
  869.       }
  870.  
  871.       --fd->fd_RecurseDepth;
  872.       --fd->fd_ArchiveDepth;
  873.       fd->fd_Flags = flags;
  874.     }
  875.     fi = fi->xfi_Next;
  876.   }
  877. }
  878.  
  879. /* Tests if a file is an archive. When yes, the archive is unarchived and
  880.    DoDirectoryScan is called for the result files. Else DoFileUnLink is
  881.    called.
  882. */
  883. static LONG DoFileUnArchive(struct FileData *fd, APTR buffer, ULONG buflength)
  884. {
  885.   LONG err = 0;
  886.   struct xadArchiveInfo *ai;
  887.  
  888.   if((ai = (struct xadArchiveInfo *) xadAllocObject(XADOBJ_ARCHIVEINFO, 0)))
  889.   {
  890.     STRPTR arcname;
  891.  
  892.     arcname = fd->fd_Name;
  893.     if(!xadGetInfo(ai, XAD_INMEMORY, buffer, XAD_INSIZE, buflength,
  894.     TAG_DONE))
  895.     {
  896.       if(ai->xai_Flags & XADAIF_FILECORRUPT)
  897.       {
  898.         ++fd->fd_CorruptedArchives;
  899.         PrintCHKXTxt(fd, "%s (ARCHIVE, CORRUPTED)", ai->xai_Client->xc_ArchiverName);
  900.       }
  901.       else
  902.         PrintCHKXTxt(fd, "%s (ARCHIVE)", ai->xai_Client->xc_ArchiverName);
  903.         
  904.       if(!(!(fd->fd_Flags & CHECKXFLAG_NOUNARCHIVE) && ai->xai_FileInfo ||
  905.       !(fd->fd_Flags & CHECKXFLAG_NOUNTRACK) && ai->xai_DiskInfo))
  906.         err = DoFileUnLink(fd, buffer, buflength);
  907.       else if(!OpenNewDir(fd, FilePart(fd->fd_Name)))
  908.         err = CHKXERR_OPENERR;
  909.       else
  910.       {
  911.     if(!(fd->fd_Flags & CHECKXFLAG_NOUNARCHIVE) && ai->xai_FileInfo)
  912.       CheckArcFiles(fd, ai, 0);
  913.     if(!(fd->fd_Flags & CHECKXFLAG_NOUNTRACK) && ai->xai_DiskInfo)
  914.     {
  915.       STRPTR buf;
  916.  
  917.       /* reduce stacksize, as we have a recursive program */
  918.       if((buf = (STRPTR) AllocMem(1024, MEMF_PUBLIC)))
  919.       {
  920.             ULONG flags;
  921.         struct xadDiskInfo *di;
  922.         struct xadArchiveInfo *aid;
  923.         di = ai->xai_DiskInfo;
  924.  
  925.             flags = fd->fd_Flags;
  926.         while(!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C) && di)
  927.         {
  928.           if(di->xdi_SectorSize != 512 || (di->xdi_TrackSectors != 11 &&
  929.           di->xdi_TrackSectors != 22) || (!(di->xdi_Flags & XADDIF_NOHEADS) &&
  930.           di->xdi_Heads != 2) || (!(di->xdi_Flags & XADDIF_NOCYLINDERS) &&
  931.           di->xdi_Cylinders != 80))
  932.             sprintf(buf, "-disk image %ld", di->xdi_EntryNumber);
  933.           else
  934.           {
  935.             if(!(di->xdi_Flags & (XADDIF_NOHIGHCYL|XADDIF_NOLOWCYL)) &&
  936.          (di->xdi_LowCyl || di->xdi_HighCyl != 79))
  937.                   sprintf(buf, "-disk image %ld (%s, %ld to %ld)",
  938.                   di->xdi_EntryNumber, di->xdi_TrackSectors == 22 ?
  939.                   "HD" : "DD", di->xdi_LowCyl, di->xdi_HighCyl);
  940.                 else
  941.                   sprintf(buf, "-disk image %ld (%s)",
  942.                   di->xdi_EntryNumber, di->xdi_TrackSectors == 22 ? "HD" : "DD");
  943.               }
  944.       
  945.               fd->fd_Flags &= ~CHECKXFLAG_NAMEPRINTED;
  946.           fd->fd_Name = buf;
  947.               PrintCHKXFile(fd);
  948.  
  949.           if(di->xdi_TextInfo)
  950.           {
  951.                 struct xadTextInfo *ti;
  952.                 ULONG flags, i = 1;
  953.  
  954.             for(ti = di->xdi_TextInfo; ti; ti = ti->xti_Next)
  955.             {
  956.               if(ti->xti_Size && ti->xti_Text)
  957.               {
  958.                     flags = fd->fd_Flags;
  959.                     fd->fd_Flags &= ~CHECKXFLAG_NAMEPRINTED;
  960.                     fd->fd_Flags |= CHECKXFLAG_NOFREEMEM;
  961.                     fd->fd_RecurseDepth++;
  962.                     sprintf(buf, "--infotext %ld (size %ld)", i, ti->xti_Size);
  963.                     if((fd->fd_Flags & CHECKXFLAG_PRINTALL) ||
  964.                     ((fd->fd_Flags & CHECKXFLAG_PRINTEXEC) && ti->xti_Size > 4
  965.                     && *((ULONG *)(ti->xti_Text)) == HUNK_HEADER))
  966.                       PrintCHKXFile(fd);
  967.                     PrintCHKXErr(fd, DoGetVirus(fd, ti->xti_Text, ti->xti_Size));
  968.                     --fd->fd_RecurseDepth;
  969.                     fd->fd_Flags = flags;
  970.                   }
  971.                   ++i;
  972.                 }
  973.               }
  974.  
  975.           /* check bootblock here - special outhook for bootblock extraction */
  976.           if(!di->xdi_LowCyl) /* boot check only, when first block! */
  977.           {
  978.             struct Hook hook = {{0,0},(ULONG (*)()) OutHookCheckX, 0};
  979.  
  980.             hook.h_Data = buf;
  981.             if(xadDiskUnArc(ai, XAD_ENTRYNUMBER, di->xdi_EntryNumber,
  982.             XAD_OUTHOOK, &hook, TAG_DONE) == 10000) /* special hook return code! */
  983.             {
  984.               struct xvsBootInfo *bi;
  985.  
  986.               if((bi = (struct xvsBootInfo *) xvsAllocObject(XVSOBJ_BOOTINFO)))
  987.                 {
  988.                 bi->xvsbi_Bootblock = buf;
  989.                 if(xvsCheckBootblock(bi) == XVSBT_VIRUS)
  990.                 {
  991.                   PrintCHKXTxt(fd, "Boot-Virus '%s'", bi->xvsbi_Name);
  992.                   ++fd->fd_NumVirus;
  993.                 }
  994.                 xvsFreeObject(bi);
  995.                 }
  996.                 else
  997.                   PrintCHKXErr(fd, CHKXERR_NOBOOTVIRUS);
  998.               }
  999.               else
  1000.                 PrintCHKXErr(fd, CHKXERR_NOBOOTVIRUS);
  1001.           }
  1002.     
  1003.           if((aid = (struct xadArchiveInfo *) xadAllocObject(XADOBJ_ARCHIVEINFO, 0)))
  1004.           {
  1005.             struct TagItem ti[4];
  1006.             struct xadClient *cl;
  1007.       
  1008.           ti[0].ti_Tag = XAD_INMEMORY;
  1009.           ti[0].ti_Data = (ULONG) buffer;
  1010.           ti[1].ti_Tag = XAD_INSIZE;
  1011.           ti[1].ti_Data = buflength;
  1012.           ti[2].ti_Tag = XAD_ENTRYNUMBER;
  1013.           ti[2].ti_Data = di->xdi_EntryNumber;
  1014.           ti[3].ti_Tag = TAG_DONE;
  1015.     
  1016.           if(!(err = xadGetDiskInfo(aid, XAD_INDISKARCHIVE, ti, TAG_DONE)))
  1017.           {
  1018.             if(aid->xai_Flags & XADAIF_FILECORRUPT)
  1019.             {
  1020.                     ++fd->fd_CorruptedArchives;
  1021.                     PrintCHKXTxt(fd, "%s (FILESYSTEM, CORRUPTED)", aid->xai_Client->xc_ArchiverName);
  1022.             }
  1023.                   else
  1024.                     PrintCHKXTxt(fd, "%s (FILESYSTEM)", aid->xai_Client->xc_ArchiverName);
  1025.           CheckArcFiles(fd, aid, 1);
  1026.           cl = aid->xai_Client->xc_Next;
  1027.           xadFreeInfo(aid);
  1028.           while(cl) /* multiple filesystems */
  1029.           {
  1030.               if(!xadGetDiskInfo(aid, XAD_INDISKARCHIVE, ti, XAD_STARTCLIENT, cl, TAG_DONE))
  1031.               {
  1032.                 if(aid->xai_Flags & XADAIF_FILECORRUPT)
  1033.                 {
  1034.                         ++fd->fd_CorruptedArchives;
  1035.                         PrintCHKXTxt(fd, "%s (FILESYSTEM, CORRUPTED)", aid->xai_Client->xc_ArchiverName);
  1036.                 }
  1037.                       else
  1038.                         PrintCHKXTxt(fd, "%s (FILESYSTEM)", aid->xai_Client->xc_ArchiverName);
  1039.               CheckArcFiles(fd, aid, 1);
  1040.               cl = aid->xai_Client->xc_Next;
  1041.               xadFreeInfo(aid);
  1042.             }
  1043.             else
  1044.               cl = 0;
  1045.           }
  1046.           }
  1047.           else
  1048.           {
  1049.             if(err != XADERR_FILESYSTEM)
  1050.               PrintCHKXErr(fd, XADERR_OFFSET + err);
  1051.             err = 0;
  1052.           }
  1053.           xadFreeObjectA(aid, 0);
  1054.           }
  1055.  
  1056.               di = di->xdi_Next;
  1057.             } /* while */
  1058.             fd->fd_Flags = flags;
  1059.             FreeMem(buf, 1024);
  1060.           } /* AllocMem buffer */
  1061.           else
  1062.             err = CHKXERR_NOMEMORY;
  1063.         } /* is there a disk entry? */
  1064.         if(!OpenParentDir(fd))
  1065.           err = CHKXERR_OPENDIR;
  1066.       }
  1067.  
  1068.       xadFreeInfo(ai);
  1069.     } /* xadGetInfo */
  1070.     else if(!xadGetDiskInfo(ai, XAD_INMEMORY, buffer, XAD_INSIZE, buflength, TAG_DONE))
  1071.     {
  1072.       struct xadClient *cl;
  1073.       ULONG isvalid = 1;
  1074.  
  1075.       if(ai->xai_Flags & XADAIF_FILECORRUPT)
  1076.       {
  1077.         ++fd->fd_CorruptedArchives;
  1078.         PrintCHKXTxt(fd, "%s (FILESYSTEM, CORRUPTED)", ai->xai_Client->xc_ArchiverName);
  1079.       }
  1080.       else
  1081.         PrintCHKXTxt(fd, "%s (FILESYSTEM)", ai->xai_Client->xc_ArchiverName);
  1082.         
  1083.       if((fd->fd_Flags & CHECKXFLAG_NOUNTRACK) || !ai->xai_FileInfo)
  1084.         err = DoFileUnLink(fd, buffer, buflength);
  1085.       else if(!OpenNewDir(fd, FilePart(fd->fd_Name)))
  1086.     err = CHKXERR_OPENERR;
  1087.       else
  1088.       {
  1089.     CheckArcFiles(fd, ai, 1);
  1090.     while(isvalid && (cl = ai->xai_Client->xc_Next))
  1091.     {
  1092.           xadFreeInfo(ai);
  1093.           if(!xadGetDiskInfo(ai, XAD_INMEMORY, buffer, XAD_INSIZE, buflength, XAD_STARTCLIENT, cl, TAG_DONE))
  1094.           {
  1095.             if(ai->xai_Flags & XADAIF_FILECORRUPT)
  1096.             {
  1097.               ++fd->fd_CorruptedArchives;
  1098.               PrintCHKXTxt(fd, "%s (FILESYSTEM, CORRUPTED)", ai->xai_Client->xc_ArchiverName);
  1099.             }
  1100.             else
  1101.               PrintCHKXTxt(fd, "%s (FILESYSTEM)", ai->xai_Client->xc_ArchiverName);
  1102.         CheckArcFiles(fd, ai, 1);
  1103.           }
  1104.           else
  1105.             isvalid = 0;
  1106.         } 
  1107.         if(!OpenParentDir(fd))
  1108.           err = CHKXERR_OPENDIR;
  1109.       }
  1110.       if(isvalid)
  1111.         xadFreeInfo(ai);
  1112.     }
  1113.     else
  1114.       err = DoFileUnLink(fd, buffer, buflength);
  1115.  
  1116.     fd->fd_Name = arcname;
  1117.     xadFreeObjectA(ai, 0);
  1118.   }
  1119.   else
  1120.     err = CHKXERR_NOMEMORY;
  1121.  
  1122.   return err;
  1123. }
  1124.  
  1125. /* Tries to unlink a file. When the file was linked, we call DoGetVirus
  1126.    for the two parts to check if they may be archives, else we call
  1127.    DoFileUnCrunch.
  1128. */
  1129. static LONG DoFileUnLink(struct FileData *fd, APTR buffer, ULONG buflength)
  1130. {
  1131.   LONG ret = CHKXERR_NOMEMORY;
  1132.   struct xfdLinkerInfo *xli;
  1133.  
  1134.   if((xli = (struct xfdLinkerInfo *) xfdAllocObject(XFDOBJ_LINKERINFO)))
  1135.   {
  1136.     xli->xfdli_Buffer = buffer;
  1137.     xli->xfdli_BufLen = buflength;
  1138.     if(xfdRecogLinker(xli))
  1139.     {
  1140.       PrintCHKXTxt(fd, xli->xfdli_LinkerName);
  1141.       if(fd->fd_Flags & CHECKXFLAG_NOUNLINK)
  1142.         ret = DoFileUnCrunch(fd, buffer, buflength);
  1143.       else if(xfdUnlink(xli))
  1144.       {
  1145.         ULONG flags;
  1146.     fd->fd_Flags |= CHECKXFLAG_LINKED;
  1147.         ++fd->fd_RecurseDepth;
  1148.         ++fd->fd_LinkNum;
  1149.     flags = fd->fd_Flags;
  1150.     fd->fd_Flags |= CHECKXFLAG_NOFREEMEM;
  1151.         PrintCHKXErr(fd, DoGetVirus(fd, xli->xfdli_Save1,
  1152.         xli->xfdli_SaveLen1));
  1153.         fd->fd_Flags = flags; /* CHECKXFLAG_NOFREEMEM is cleared */
  1154.         PrintCHKXErr(fd, DoGetVirus(fd, xli->xfdli_Save2,
  1155.         xli->xfdli_SaveLen2));
  1156.         ret = 0;
  1157.         --fd->fd_RecurseDepth;
  1158.       }
  1159.       else
  1160.         ret = XFDERR_OFFSET + xli->xfdli_Error;
  1161.     }
  1162.     else
  1163.       ret = DoFileUnCrunch(fd, buffer, buflength);
  1164.  
  1165.     xfdFreeObject(xli);
  1166.   }
  1167.   return ret;
  1168. }
  1169.  
  1170. /* Tries to decrunch a file. When it is crunched, we decrunch it and call
  1171.    DoGetVirus to start the loop again. Else we call unstripping.
  1172. */
  1173. static LONG DoFileUnCrunch(struct FileData *fd, APTR buffer, ULONG buflength)
  1174. {
  1175.   LONG ret = CHKXERR_NOMEMORY;
  1176.   struct xfdBufferInfo *xbi;
  1177.  
  1178.   if((xbi = (struct xfdBufferInfo *) xfdAllocObject(XFDOBJ_BUFFERINFO)))
  1179.   {
  1180.     xbi->xfdbi_SourceBuffer = buffer;
  1181.     xbi->xfdbi_SourceBufLen = buflength;
  1182.     xbi->xfdbi_Flags = XFDFF_RECOGEXTERN;
  1183.     if(xfdRecogBuffer(xbi))
  1184.     {
  1185.       struct Library *xpkbase;
  1186.       STRPTR buf = 0;
  1187.       ULONG buflen = 0;
  1188.  
  1189.       PrintCHKXTxt(fd, xbi->xfdbi_PackerFlags & XFDPFF_ADDR ? "%s (ADDRESS)" :
  1190.       "%s", xbi->xfdbi_PackerName);
  1191.  
  1192.       if(fd->fd_Flags & CHECKXFLAG_ASKPWD && (xpkbase =
  1193.       OpenLibrary(XPKNAME, 4)))
  1194.       {
  1195.     ASSIGN_XPK
  1196.         if(xbi->xfdbi_PackerFlags & XFDPFF_PASSWORD)
  1197.         {
  1198.           buflen = (xbi->xfdbi_MaxSpecialLen == 0xFFFF) ? 256 :
  1199.         xbi->xfdbi_MaxSpecialLen;
  1200.           if((buf = (STRPTR) AllocMem(buflen, MEMF_ANY|MEMF_CLEAR)))
  1201.       {
  1202.             if(!XpkPassRequestTags(XPK_PasswordBuf, buf,
  1203.             XPK_PassBufSize, buflen, TAG_DONE))
  1204.           xbi->xfdbi_Special = buf;
  1205.       }
  1206.         }
  1207.         else if(xbi->xfdbi_PackerFlags & XFDPFF_KEY16)
  1208.         {
  1209.       if(!XpkPassRequestTags(XPK_Key16BitPtr, &buflen, TAG_DONE))
  1210.         xbi->xfdbi_Special = &buflen;
  1211.         }
  1212.         else if(xbi->xfdbi_PackerFlags & XFDPFF_KEY32)
  1213.         {
  1214.       if(!XpkPassRequestTags(XPK_Key32BitPtr, &buflen, TAG_DONE))
  1215.             xbi->xfdbi_Special = &buflen;
  1216.         }
  1217.     CloseLibrary(xpkbase);
  1218.       }
  1219.       if(fd->fd_Flags & CHECKXFLAG_NODECRUNCH)
  1220.         ret = DoFileStrip(fd, buffer, buflength);
  1221.       else if(xfdDecrunchBuffer(xbi))
  1222.       {
  1223.         if((xbi->xfdbi_PackerFlags & XFDPFF_ADDR) &&
  1224.         !(fd->fd_Flags & CHECKXFLAG_ADDRESS))
  1225.         {
  1226.       fd->fd_Flags |= CHECKXFLAG_ADDRESS;
  1227.           PrintCHKXErr(fd, SaveUncrFile(fd, buffer, buflength));
  1228.         }
  1229.     fd->fd_Flags |= CHECKXFLAG_CRUNCHED;
  1230.         FreeCrunchMemList(fd, buffer);
  1231.         ++fd->fd_RecurseDepth;
  1232.         if(!(ret = AddCrunchMemList(fd, xbi->xfdbi_TargetBuffer,
  1233.         xbi->xfdbi_TargetBufLen)))
  1234.         {
  1235.           PrintCHKXErr(fd, DoGetVirus(fd, xbi->xfdbi_TargetBuffer,
  1236.             xbi->xfdbi_TargetBufSaveLen));
  1237.         }
  1238.         else
  1239.           FreeMem(xbi->xfdbi_TargetBuffer, xbi->xfdbi_TargetBufLen);
  1240.         --fd->fd_RecurseDepth;
  1241.       }
  1242.       else
  1243.         ret = XFDERR_OFFSET + xbi->xfdbi_Error;
  1244.  
  1245.       if(buf)
  1246.         FreeMem(buf, buflen);
  1247.     }
  1248.     else
  1249.       ret = DoFileStrip(fd, buffer, buflength);
  1250.  
  1251.     xfdFreeObject(xbi);
  1252.   }
  1253.   return ret;
  1254. }
  1255.  
  1256. /* Tries to strip useless hunks in a file. When there are some, we remove
  1257.    them and call DoGetVirus to start the loop again. Else we finish.
  1258.    When either unlinking or uncrunching happend in before loops, we may
  1259.    save the file when there was SAVE option.
  1260. */
  1261. static LONG DoFileStrip(struct FileData *fd, APTR buffer, ULONG buflength)
  1262. {
  1263.   LONG ret = 0;
  1264.   ULONG reslength = buflength;
  1265.  
  1266.   if(*((ULONG *) buffer) == 0x000003F3 && !(fd->fd_Flags &
  1267.   CHECKXFLAG_NOSTRIP))
  1268.     xfdStripHunks(buffer, buflength, &reslength,
  1269.     XFDSHF_NAME|XFDSHF_SYMBOL|XFDSHF_DEBUG);
  1270.     /* errors are not interpreted */
  1271.  
  1272.   if(buflength > reslength)
  1273.   {
  1274.     fd->fd_Flags |= CHECKXFLAG_STRIPPED;
  1275.     PrintCHKXTxt(fd, "%ld bytes stripped", buflength-reslength);
  1276.     ret = DoGetVirus(fd, buffer, reslength);
  1277.   }
  1278.   else if(!(fd->fd_Flags & CHECKXFLAG_ADDRESS))
  1279.     ret = SaveUncrFile(fd, buffer, buflength);
  1280.  
  1281.   FreeCrunchMemList(fd, buffer);
  1282.  
  1283.   return ret;
  1284. }
  1285.  
  1286. /* Print file name */
  1287. static void PrintCHKXFile(struct FileData *fd)
  1288. {
  1289.   STRPTR name = fd->fd_Name;
  1290.   UBYTE i;
  1291.  
  1292.   if(fd->fd_Flags & CHECKXFLAG_DEBUG)
  1293.   {
  1294.     for(i = 0; i < fd->fd_ArchiveDepth; ++i)
  1295.       KPutC('*');
  1296.     KPrintf("%s\n", name);
  1297.   }
  1298.  
  1299.   if(fd->fd_LogFileFH)
  1300.   {
  1301.     for(i = 0; i < fd->fd_ArchiveDepth; ++i)
  1302.       FPutC(fd->fd_LogFileFH, '*');
  1303.     FPrintf(fd->fd_LogFileFH, "%s\n", name);
  1304.   }
  1305.   if(!(fd->fd_Flags & CHECKXFLAG_QUIET))
  1306.   {
  1307.     for(i = 0; i < fd->fd_ArchiveDepth; ++i)
  1308.       FPutC(Output(), '*');
  1309.     Printf("%s\n", name);
  1310.   }
  1311.   fd->fd_Flags |= CHECKXFLAG_NAMEPRINTED;
  1312. }
  1313.  
  1314. static void PrintCHKXErr(struct FileData *fd, LONG err)
  1315. {
  1316.   if(err)
  1317.   {
  1318.     STRPTR txt = 0, txt2;
  1319.  
  1320.     if(err > XFDERR_OFFSET)
  1321.     {
  1322.       ++fd->fd_XFDErrors;
  1323.       txt2 = "XFD-Error %ld: %s";
  1324.       err -= XFDERR_OFFSET;
  1325.       txt = xfdGetErrorText(err);
  1326.     }
  1327.     else if(err > XADERR_OFFSET)
  1328.     {
  1329.       ++fd->fd_XADErrors;
  1330.       txt2 = "XAD-Error %ld: %s";
  1331.       err -= XADERR_OFFSET;
  1332.       txt = xadGetErrorText(err);
  1333.     }
  1334.     else if(err > CHXWARN_OFFSET)
  1335.     {
  1336.       txt2 = "CheckX-Warning %ld: %s";
  1337.       switch(err)
  1338.       {
  1339.       case CHKXWARN_NOVIRUS:    txt = "Virus-Checking disabled!";                        break;
  1340.       case CHKXWARN_XVSSELFTEST:txt = "The xvs.library is modified, maybe the system is virus infected!";    break;
  1341.       case CHKXWARN_MEMVIRUS:    txt = "Your system memory was virus infected!";                    break;
  1342.       }
  1343.       err -= CHXWARN_OFFSET;
  1344.     }
  1345.     else
  1346.     {
  1347.       ++fd->fd_CHKXErrors;
  1348.       txt2 = "CheckX-Error %ld: %s";
  1349.       switch(err)
  1350.       {
  1351.       case CHKXERR_NOMEMORY:    txt = "not enough memory";            break;
  1352.       case CHKXERR_EXAMINEERR:    txt = "examining failed";            break;
  1353.       case CHKXERR_OPENERR:    txt = "opening file failed";            break;
  1354.       case CHKXERR_READ:    txt = "reading failed";                break;
  1355.       case CHKXERR_SCANERR:    txt = "directory scan failed";            break;
  1356.       case CHKXERR_BREAK:    txt = "user break";                break;
  1357.       case CHKXERR_OPENDIR:    txt = "opening directory failed";        break;
  1358.       case CHKXERR_RESOURCE:    txt = "needed resource not available";        break;
  1359.       case CHKXERR_NOBOOTVIRUS:    txt = "could not check for bootblock virus";    break;
  1360.       case CHKXERR_WRITE:    txt = "writing failed";                break;
  1361.       case CHKXERR_NOVIRUS:    txt = "could not check for virus";        break;
  1362.       case CHKXERR_EMPTY:    txt = "file is empty";                break;
  1363.       }
  1364.     }
  1365.  
  1366.     PrintCHKXTxt(fd, txt2, err, txt);
  1367.   }
  1368. }
  1369.  
  1370. /* Print type text */
  1371. static void PrintCHKXTxt(struct FileData *fd, STRPTR txt, ...)
  1372. {
  1373.   UBYTE i;
  1374.  
  1375.   if(!(fd->fd_Flags & CHECKXFLAG_NAMEPRINTED))
  1376.     PrintCHKXFile(fd);
  1377.  
  1378.   if(fd->fd_Flags & CHECKXFLAG_DEBUG)
  1379.   {
  1380.     for(i = 0; i < fd->fd_RecurseDepth; ++i)
  1381.       KPutC(' ');
  1382.     RawDoFmt(txt, &txt+1, (void (*)()) KPutC, 0);
  1383.     KPutC('\n');
  1384.   }
  1385.  
  1386.   if(fd->fd_LogFileFH)
  1387.   {
  1388.     for(i = 0; i < fd->fd_RecurseDepth; ++i)
  1389.       FPutC(fd->fd_LogFileFH, ' ');
  1390.     VFPrintf(fd->fd_LogFileFH, txt, &txt+1);
  1391.     FPutC(fd->fd_LogFileFH, '\n');
  1392.     Flush(fd->fd_LogFileFH);
  1393.   }
  1394.  
  1395.   if(!(fd->fd_Flags & CHECKXFLAG_QUIET))
  1396.   {
  1397.     for(i = 0; i < fd->fd_RecurseDepth; ++i)
  1398.       FPutC(Output(), ' ');
  1399.     VPrintf(txt, &txt+1);
  1400.     FPutC(Output(), '\n');
  1401.   }
  1402. }
  1403.  
  1404. /* Add memory to the memory list. */
  1405. static LONG AddCrunchMemList(struct FileData *fd, APTR reg, ULONG size)
  1406. {
  1407.   struct CrunchMemList *ml;
  1408.  
  1409.   if((ml = (struct CrunchMemList *) AllocMem(sizeof(struct CrunchMemList),
  1410.   MEMF_ANY)))
  1411.   {
  1412.     ml->cml_Next = fd->fd_MemList;
  1413.     ml->cml_MemoryRegion = reg;
  1414.     ml->cml_MemorySize = size;
  1415.     fd->fd_MemList = ml;
  1416.     return 0;
  1417.   }
  1418.  
  1419.   return CHKXERR_NOMEMORY;
  1420. }
  1421.  
  1422. /* Free memory from the memory list. */
  1423. static void FreeCrunchMemList(struct FileData *fd, APTR reg)
  1424. {
  1425.   struct CrunchMemList mc, *ml = &mc;
  1426.  
  1427.   if(fd->fd_Flags & CHECKXFLAG_NOFREEMEM)
  1428.     return;
  1429.  
  1430.   for(mc.cml_Next = fd->fd_MemList; ml; ml = ml->cml_Next)
  1431.   {
  1432.     if(ml->cml_Next->cml_MemoryRegion == reg)
  1433.     {
  1434.       struct CrunchMemList *m;
  1435.       m = ml->cml_Next;
  1436.       ml->cml_Next = m->cml_Next;
  1437.       FreeMem(m->cml_MemoryRegion, m->cml_MemorySize);
  1438.       FreeMem(m, sizeof(struct CrunchMemList));
  1439.     }
  1440.   }
  1441.  
  1442.   fd->fd_MemList = mc.cml_Next;
  1443. }
  1444.  
  1445. /* Save file, when SAVE option and file was crunched or linked. */
  1446. static LONG SaveUncrFile(struct FileData *fd, APTR buf, ULONG size)
  1447. {
  1448.   BPTR filefh, cd;
  1449.   LONG ret = 0, i = 0;
  1450.   UBYTE name[50];
  1451.   UBYTE nbuf[256];
  1452.  
  1453.   if(!(fd->fd_SaveDirL && (fd->fd_Flags & CHKXSAVEFLAGS)))
  1454.     return 0;
  1455.  
  1456.   sprintf(name, (fd->fd_LinkNum ? "%s.%ld" : "%s"), FilePart(fd->fd_Name),
  1457.   fd->fd_LinkNum++);
  1458.  
  1459.   cd = CurrentDir(fd->fd_SaveDirL);
  1460.  
  1461.   while(!ret && name[i])
  1462.   {
  1463.     for(;name[i] && name[i] != '/'; ++i)
  1464.       nbuf[i] = name[i];
  1465.     if(name[i] == '/')
  1466.     {
  1467.       nbuf[i] = 0;
  1468.       if((filefh = Lock(nbuf, SHARED_LOCK)))
  1469.         UnLock(filefh);
  1470.       else
  1471.       {
  1472.         if((filefh = CreateDir(nbuf)))
  1473.           UnLock(filefh);
  1474.         else
  1475.           ret = CHKXERR_OPENERR;
  1476.       }
  1477.       nbuf[i] = name[i];
  1478.       ++i;
  1479.     }
  1480.   }
  1481.  
  1482.   if(!ret)
  1483.   {
  1484.     if((filefh = Open(name, MODE_NEWFILE)))
  1485.     {
  1486.       if(Write(filefh, buf, size) != size)
  1487.         ret = CHKXERR_WRITE;
  1488.       Close(filefh);
  1489.     }
  1490.     else
  1491.       ret = CHKXERR_OPENERR;
  1492.   }
  1493.  
  1494.   CurrentDir(cd);
  1495.  
  1496.   return ret;
  1497. }
  1498.  
  1499.