;/*
SC 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.
 *
 */
#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, *chk;
  UWORD *chkpict;
  UBYTE *buffer;
  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) {
        if(buffer[0] == 0) {

          /* Check for PNTG mark */
          chk = (ULONG *) &buffer[0x41];
          if((*chk == 0x504E5447) && (buffer[0x4b] == 0)) 
            /* Now we are sure it is a macpaint picture */
            retval = TRUE;

          /* This will be harder */
          else {
            chk = (ULONG *) buffer; /* This will normally be 0x00000002 */
            if((n = Seek(fh, 0x135, OFFSET_BEGINNING) >= 0L) && (*chk <= 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 */
                    chkpict = (UWORD *) &buffer[0xcb];
                    if((chkpict[5]==0x0011) && (chkpict[6]==0x02ff))
                      retval = FALSE;
              }
          }
        }
      }
	  /* Do not steal memory */
      FreeVec(buffer);
    }
  }    
  return retval;
}


