;/*
SC resopt desc_macpaint.c nodebug opt optsize link nostrt nostkext nostkchk
quit 
*/
/* This description function replaces the original MacPaint datatype
   recognition found in "pictdt_42_1.lha" archive (on NDU3.1 disks) */

/* Execute this file to compile with SAS C 
 * Ex. "1.Ram:>execute desc_macpaint.c"
 *
 * Otherwise it must be compiled with no stack checking. 
 * It must not be linked with any startup code so that DTHook is
 * the entry point for the executable.
 *
 * This source is free for any usage.  (Erik Engdahl 1999)
 *
 */
#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <datatypes/datatypes.h>
#include <datatypes/pictureclass.h>

#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/utility_protos.h>

#include <pragmas/exec_pragmas.h>
#include <pragmas/dos_pragmas.h>
#include <pragmas/utility_pragmas.h>

#define SysBase          dthc->dthc_SysBase
#define DOSBase          dthc->dthc_DOSBase
#define UtilityBase      dthc->dthc_UtilityBase

BOOL __asm DTHook (register __a0 struct DTHookContext * dthc)
{
  BOOL retval = FALSE;
  ULONG n;
  UBYTE *buffer, *chk;
  BPTR fh;

  /* Make sure we have a filehandle */
  if (fh = dthc->dthc_FileHandle) {

    /* Allocate a buffer */
    if(buffer = AllocVec(0x100, MEMF_PUBLIC)) {

      /* Read first $100 bytes */
      if(Read(fh, buffer, 0x100) == 0x100) {

        /* Quick initial check */
        if(buffer[0] == 0) {

          /* Check for PNTG mark */
          chk = &buffer[0x41];
          if((chk[1] == 0x50 && chk[2] == 0x4E && chk[3] == 0x54 &&
              chk[4] == 0x47) && (buffer[0x4b] == 0)) 
            /* Now we are sure it is a macpaint picture */
            retval = TRUE;

          /* This will be harder */
          else {
            chk = buffer; /* First LONG are usually 0x00000002 */
            if((n = Seek(fh, 0x135, OFFSET_BEGINNING) >= 0L) && 
               (chk[0] == 0 && chk[1] == 0 && chk[2] == 0 && chk[3] <= 4UL))
              /* Get next $100 bytes */
              if(Read(fh, buffer, 0x100) == 0x100) {
                retval = TRUE;

                /* First bytes must all be zero */
                for(n = 0; (n < 0x80) && (retval==TRUE); n++)
                  if(buffer[n] != 0) retval = FALSE;

                  /* We do not want a Mac PICT file, check for version number
					at filepos 0x20a */
                    chk = &buffer[0xcb];
                    if((chk[0x20]==0x00 && chk[0x21] == 0x11) && 
                       (chk[0x22]==0x02 && chk[0x23] == 0xff))
                      retval = FALSE;
              }
          }
        }
      }
	  /* Return the buffer memory */
      FreeVec(buffer);
    }
  }    
  return retval;
}


