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

/* Execute this file to compile with SAS C 
 * Ex. "1.Ram:>execute org_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 *pictdata;
  UBYTE *buffer;
  BPTR fh;

  /* Make sure we have a filehandle */
  if (fh = dthc->dthc_FileHandle) {
    /* Allocate a buffer */
    if(buffer = AllocVec(0x80, MEMF_PUBLIC)) {

      /* Read first $80 bytes */
      if(Read(fh, buffer, 0x80) == 0x80) {

        if((buffer[0] || buffer[0x4b]) == 0) {

          /* Check for comment */
          if(buffer[1] >= 0 && buffer[1] < 0x40) {

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

            /* This will be harder */
            else {
              chk = (ULONG *) buffer;
              /* Check filesize > $200 bytes (header size) and version*/
              if((dthc->dthc_FIB->fib_Size > 0x200) && (*chk <= 4UL))

                /* Position at byte $135 in file */
                if(n = Seek(fh, 0x135, OFFSET_BEGINNING) >= 0L)

                  /* Get $80 bytes */
                  if(Read(fh, buffer, 0x80) == 0x80) {
                    retval = TRUE;

                    /* All bytes must be zero */
                    for(n = 0; (n < 0x80) && (retval==TRUE); n++)
                      if(buffer[n] != 0) retval = FALSE;
                  }
            }
          }
        }
      }
      /* Do not steal memory */
      FreeVec(buffer);
    }
  }    
  return retval;
}


