home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / gfx / misc / imagefx_sdk / sas / scanlib / macbin.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-15  |  7.0 KB  |  317 lines

  1. /*
  2.  * Functions for stripping MacBinary headers from files.
  3.  *
  4.  * Based on information provided by John Foust.  Thanks!
  5.  *
  6.  */
  7.  
  8. #include <scan/modall.h>
  9. #include <clib/dos_protos.h>
  10.  
  11. /* macbinary.h - MacBinary routines */
  12. /* Copyright 1990 Syndesis Corporation */
  13. /* September 17, 1991, 2:28 pm */
  14.  
  15. typedef struct {
  16.     char filename[64];
  17.     ULONG fileType;
  18.     ULONG creator;
  19.     UBYTE finderFlags;
  20.     ULONG dataForkLen;
  21.     ULONG rsrcForkLen;
  22.     ULONG createDate;
  23.     ULONG modifyDate;
  24.     ULONG totalLen;
  25.     UWORD secLen;
  26. } macBinaryHeader;
  27.  
  28.  
  29. BOOL IsMacBinary (BPTR InFile)
  30. {
  31.     LONG oldPos;
  32.     UBYTE junk[ 0x80 + 5 ];
  33.     BOOL retval;
  34.     short each;
  35.  
  36.  
  37.     retval = FALSE;
  38.  
  39.     oldPos = -1;
  40.     oldPos = Seek( InFile, 0L, (LONG) OFFSET_CURRENT );
  41.     if (oldPos == -1) {
  42.         goto out;
  43.     }
  44.  
  45.     /* Read the header: expect at least 0x80 (128) bytes */
  46.     if (Read( InFile, (char *) &junk[0], 128L ) != 128L) {
  47.         goto out;
  48.     }
  49.  
  50.     /* 000: Byte zero should be zero */
  51.     if (junk[0] != 0) {
  52.         goto out;
  53.     }
  54.  
  55.     /* 001: length of filename, 0..63 as a Pascal string */
  56.     if (junk[1] > 63) {
  57.         goto out;
  58.     }
  59.  
  60.     /* 0x0002: 002: Filename 1..63 chars */
  61.     /* None of the filename should be zero */
  62.     for (each=0; each<junk[1]; each++) {
  63.         if (junk[ 2+each ] == 0) {
  64.             goto out;
  65.         }
  66.     }
  67.  
  68. /* need to fix this so it recognizes alpha and numerics */
  69.  
  70.     /* 0x0049: 073: Finder flags, 1 byte */
  71.  
  72.     /* 0x004A: 074: Zero */
  73.     if (junk[74] != 0) {
  74.         goto out;
  75.     }
  76.  
  77.     /* 0x004B: 075: vertical position of icon, 2 bytes */
  78.     /* 0x004D: 077: horizontal position of icon, 2 bytes */
  79.  
  80.     /* 079: file window or folder ID */
  81.     /* 081: protected flag */
  82.  
  83.     /* 0x0052: 082: Zero */
  84.     if (junk[82] != 0) {
  85.         goto out;
  86.     }
  87.  
  88.     /* 083: data fork length, 4 bytes */
  89.  
  90.     /* 087: resource fork length, 4 bytes */
  91.  
  92.     /* If both the resource fork length and data fork length are zero, */
  93.     /* this file could hardly contain more than a MacBinary header */
  94.     if (junk[83]==0 && junk[84]==0 && junk[85]==0 && junk[86]==0
  95.      && junk[87]==0 && junk[87]==0 && junk[88]==0 && junk[89]==0) {
  96.         goto out;
  97.     }
  98.  
  99.     /* 091: file creation date, 4 bytes */
  100.     /* 095: file last modified date */
  101.     /* 099: length of Get Info comment */
  102.     /* 101: Finder flags */
  103.  
  104.     /* 116: total length of all files, 4 bytes */
  105.  
  106.     /* 120: secLen */
  107.  
  108. #ifdef UNUSED
  109.     /* 0x007A: 122: MacBinary II version */
  110.     if (junk[122] != 0) {
  111.         goto out;
  112.     }
  113.  
  114.     /* 0x007B: 123: MacBinary II minimum version */
  115.     if (junk[123] != 0) {
  116.         goto out;
  117.     }
  118. #endif
  119.  
  120.     /* 0x007C: 124: CRC of previous 124 bytes */
  121.  
  122.     /* Junk to pad out to 128? */
  123.  
  124.     retval = TRUE;
  125. out:
  126.     /* Return to where we started */
  127.     Seek( InFile, oldPos, (LONG) OFFSET_BEGINNING );
  128.  
  129.     return(retval);
  130. }
  131.  
  132.  
  133. BOOL ReadMacBinary (BPTR InFile, macBinaryHeader *macBin)
  134. {
  135.     USHORT lerr;
  136.     char len;
  137.     UWORD pos;
  138.     UBYTE zero;
  139.     UBYTE junk[16];
  140.  
  141.  
  142.     /* 001: length of filename 0..63 */
  143.     if (Read( InFile, (char *) &len, 1L ) != 1L) {
  144.         lerr = ERR_Read;
  145.         goto out;
  146.     }
  147.  
  148.     /* 002: Filename 1..63 chars */
  149.     if (Read( InFile, (char *) &macBin->filename[0], 63L ) != 63L) {
  150.         lerr = ERR_Read;
  151.         goto out;
  152.     }
  153.     macBin->filename[ len ] = 0;
  154.  
  155.     /* 065: file type */
  156.     if (Read( InFile, (char *) &macBin->fileType, 4L ) != 4L) {
  157.         lerr = ERR_Read;
  158.         goto out;
  159.     }
  160.  
  161.     /* 069: file creator */
  162.     if (Read( InFile, (char *) &macBin->creator, 4L ) != 4L) {
  163.         lerr = ERR_Read;
  164.         goto out;
  165.     }
  166.  
  167.     /* 073: Finder flags */
  168.     if (Read( InFile, (char *) &macBin->finderFlags, 1L ) != 1L) {
  169.         lerr = ERR_Read;
  170.         goto out;
  171.     }
  172.  
  173.     /* 074: Zero */
  174.     if (Read( InFile, (char *) &zero, 1L ) != 1L) {
  175.         lerr = ERR_Read;
  176.         goto out;
  177.     }
  178.  
  179.     /* 075: vertical position of icon */
  180.     if (Read( InFile, (char *) &pos, 2L ) != 2L) {
  181.         lerr = ERR_Read;
  182.         goto out;
  183.     }
  184.  
  185.     /* 077: horizontal position of icon */
  186.     if (Read( InFile, (char *) &pos, 2L ) != 2L) {
  187.         lerr = ERR_Read;
  188.         goto out;
  189.     }
  190.  
  191.     /* 079: file window or folder ID */
  192.     if (Read( InFile, (char *) &pos, 2L ) != 2L) {
  193.         lerr = ERR_Read;
  194.         goto out;
  195.     }
  196.  
  197.     /* 081: protected flag */
  198.     if (Read( InFile, (char *) &zero, 1L ) != 1L) {
  199.         lerr = ERR_Read;
  200.         goto out;
  201.     }
  202.     /* 082: Zero */
  203.     if (Read( InFile, (char *) &zero, 1L ) != 1L) {
  204.         lerr = ERR_Read;
  205.         goto out;
  206.     }
  207.  
  208.     /* 083: data fork length */
  209.     if (Read( InFile, (char *) &macBin->dataForkLen, 4L ) != 4L) {
  210.         lerr = ERR_Read;
  211.         goto out;
  212.     }
  213.  
  214.     /* 087: resource fork length */
  215.     if (Read( InFile, (char *) &macBin->rsrcForkLen, 4L ) != 4L) {
  216.         lerr = ERR_Read;
  217.         goto out;
  218.     }
  219.  
  220.     /* 091: file creation date */
  221.     if (Read( InFile, (char *) &macBin->createDate, 4L ) != 4L) {
  222.         lerr = ERR_Read;
  223.         goto out;
  224.     }
  225.  
  226.     /* 095: file last modified date */
  227.     if (Read( InFile, (char *) &macBin->modifyDate, 4L ) != 4L) {
  228.         lerr = ERR_Read;
  229.         goto out;
  230.     }
  231.  
  232.     /* 099: length of Get Info comment */
  233.     if (Read( InFile, (char *) &pos, 2L ) != 2L) {
  234.         lerr = ERR_Read;
  235.         goto out;
  236.     }
  237.  
  238.     /* 101: Finder flags */
  239.     if (Read( InFile, (char *) &len, 1L ) != 1L) {
  240.         lerr = ERR_Read;
  241.         goto out;
  242.     }
  243.  
  244.     /* 116: total length of all files */
  245.     if (Read( InFile, (char *) &macBin->totalLen, 4L ) != 4L) {
  246.         lerr = ERR_Read;
  247.         goto out;
  248.     }
  249.  
  250.     /* 120: */
  251.     if (Read( InFile, (char *) &macBin->secLen, 2L ) != 2L) {
  252.         lerr = ERR_Read;
  253.         goto out;
  254.     }
  255.  
  256.     /* 122: MacBinary II version */
  257.     if (Read( InFile, (char *) &len, 1L ) != 1L) {
  258.         lerr = ERR_Read;
  259.         goto out;
  260.     }
  261.  
  262.     /* 123: MacBinary II minimum version */
  263.     if (Read( InFile, (char *) &len, 1L ) != 1L) {
  264.         lerr = ERR_Read;
  265.         goto out;
  266.     }
  267.  
  268.     /* 124: CRC of previous 124 bytes */
  269.     if (Read( InFile, (char *) &macBin->secLen, 2L ) != 2L) {
  270.         lerr = ERR_Read;
  271.         goto out;
  272.     }
  273.  
  274.     /* Junk to pad out to 128? */
  275.     if (Read( InFile, (char *) &junk[0], 16L ) != 16L) {
  276.         lerr = ERR_Read;
  277.         goto out;
  278.     }
  279.  
  280.     lerr = 0;
  281. out:
  282.     SetError(lerr);
  283.     if (lerr) return(FALSE);
  284.     else      return(TRUE);
  285. }
  286.  
  287.  
  288. BOOL SkipMacBinary (BPTR handle)
  289. {
  290.     macBinaryHeader macbin;
  291.  
  292.     if (IsMacBinary(handle)) {
  293.         Seek(handle, 128, OFFSET_BEGINNING);
  294. //      ReadMacBinary(handle, &macbin));
  295.     }
  296.     return(TRUE);
  297. }
  298.  
  299. /* risky at best, but it isn't too likely to change anytime soon */
  300.  
  301. struct BIO {
  302.    BPTR     DosHandle;        /* dos handle */
  303.    ULONG    Mode;             /* open mode (OLDFILE or NEWFILE) */
  304.    ULONG    StructSize;       /* size of this structure */
  305.    ULONG    BufSize;          /* size of just the buffer */
  306.    ULONG    Count;            /* remaining bytes in buffer */
  307.    UBYTE   *Ptr;              /* pointer to current buffer location */
  308.    UBYTE    Buffer[1];
  309. };
  310.  
  311. BOOL BSkipMacBinary (struct BIO *bio)
  312. {
  313.     BFlush(bio);
  314.     return(SkipMacBinary(bio->DosHandle));
  315. }
  316.  
  317.