home *** CD-ROM | disk | FTP | other *** search
- /*
- * Functions for stripping MacBinary headers from files.
- *
- * Based on information provided by John Foust. Thanks!
- *
- */
-
- #include <scan/modall.h>
- #include <clib/dos_protos.h>
-
- /* macbinary.h - MacBinary routines */
- /* Copyright 1990 Syndesis Corporation */
- /* September 17, 1991, 2:28 pm */
-
- typedef struct {
- char filename[64];
- ULONG fileType;
- ULONG creator;
- UBYTE finderFlags;
- ULONG dataForkLen;
- ULONG rsrcForkLen;
- ULONG createDate;
- ULONG modifyDate;
- ULONG totalLen;
- UWORD secLen;
- } macBinaryHeader;
-
-
- BOOL IsMacBinary (BPTR InFile)
- {
- LONG oldPos;
- UBYTE junk[ 0x80 + 5 ];
- BOOL retval;
- short each;
-
-
- retval = FALSE;
-
- oldPos = -1;
- oldPos = Seek( InFile, 0L, (LONG) OFFSET_CURRENT );
- if (oldPos == -1) {
- goto out;
- }
-
- /* Read the header: expect at least 0x80 (128) bytes */
- if (Read( InFile, (char *) &junk[0], 128L ) != 128L) {
- goto out;
- }
-
- /* 000: Byte zero should be zero */
- if (junk[0] != 0) {
- goto out;
- }
-
- /* 001: length of filename, 0..63 as a Pascal string */
- if (junk[1] > 63) {
- goto out;
- }
-
- /* 0x0002: 002: Filename 1..63 chars */
- /* None of the filename should be zero */
- for (each=0; each<junk[1]; each++) {
- if (junk[ 2+each ] == 0) {
- goto out;
- }
- }
-
- /* need to fix this so it recognizes alpha and numerics */
-
- /* 0x0049: 073: Finder flags, 1 byte */
-
- /* 0x004A: 074: Zero */
- if (junk[74] != 0) {
- goto out;
- }
-
- /* 0x004B: 075: vertical position of icon, 2 bytes */
- /* 0x004D: 077: horizontal position of icon, 2 bytes */
-
- /* 079: file window or folder ID */
- /* 081: protected flag */
-
- /* 0x0052: 082: Zero */
- if (junk[82] != 0) {
- goto out;
- }
-
- /* 083: data fork length, 4 bytes */
-
- /* 087: resource fork length, 4 bytes */
-
- /* If both the resource fork length and data fork length are zero, */
- /* this file could hardly contain more than a MacBinary header */
- if (junk[83]==0 && junk[84]==0 && junk[85]==0 && junk[86]==0
- && junk[87]==0 && junk[87]==0 && junk[88]==0 && junk[89]==0) {
- goto out;
- }
-
- /* 091: file creation date, 4 bytes */
- /* 095: file last modified date */
- /* 099: length of Get Info comment */
- /* 101: Finder flags */
-
- /* 116: total length of all files, 4 bytes */
-
- /* 120: secLen */
-
- #ifdef UNUSED
- /* 0x007A: 122: MacBinary II version */
- if (junk[122] != 0) {
- goto out;
- }
-
- /* 0x007B: 123: MacBinary II minimum version */
- if (junk[123] != 0) {
- goto out;
- }
- #endif
-
- /* 0x007C: 124: CRC of previous 124 bytes */
-
- /* Junk to pad out to 128? */
-
- retval = TRUE;
- out:
- /* Return to where we started */
- Seek( InFile, oldPos, (LONG) OFFSET_BEGINNING );
-
- return(retval);
- }
-
-
- BOOL ReadMacBinary (BPTR InFile, macBinaryHeader *macBin)
- {
- USHORT lerr;
- char len;
- UWORD pos;
- UBYTE zero;
- UBYTE junk[16];
-
-
- /* 001: length of filename 0..63 */
- if (Read( InFile, (char *) &len, 1L ) != 1L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* 002: Filename 1..63 chars */
- if (Read( InFile, (char *) &macBin->filename[0], 63L ) != 63L) {
- lerr = ERR_Read;
- goto out;
- }
- macBin->filename[ len ] = 0;
-
- /* 065: file type */
- if (Read( InFile, (char *) &macBin->fileType, 4L ) != 4L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* 069: file creator */
- if (Read( InFile, (char *) &macBin->creator, 4L ) != 4L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* 073: Finder flags */
- if (Read( InFile, (char *) &macBin->finderFlags, 1L ) != 1L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* 074: Zero */
- if (Read( InFile, (char *) &zero, 1L ) != 1L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* 075: vertical position of icon */
- if (Read( InFile, (char *) &pos, 2L ) != 2L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* 077: horizontal position of icon */
- if (Read( InFile, (char *) &pos, 2L ) != 2L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* 079: file window or folder ID */
- if (Read( InFile, (char *) &pos, 2L ) != 2L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* 081: protected flag */
- if (Read( InFile, (char *) &zero, 1L ) != 1L) {
- lerr = ERR_Read;
- goto out;
- }
- /* 082: Zero */
- if (Read( InFile, (char *) &zero, 1L ) != 1L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* 083: data fork length */
- if (Read( InFile, (char *) &macBin->dataForkLen, 4L ) != 4L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* 087: resource fork length */
- if (Read( InFile, (char *) &macBin->rsrcForkLen, 4L ) != 4L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* 091: file creation date */
- if (Read( InFile, (char *) &macBin->createDate, 4L ) != 4L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* 095: file last modified date */
- if (Read( InFile, (char *) &macBin->modifyDate, 4L ) != 4L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* 099: length of Get Info comment */
- if (Read( InFile, (char *) &pos, 2L ) != 2L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* 101: Finder flags */
- if (Read( InFile, (char *) &len, 1L ) != 1L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* 116: total length of all files */
- if (Read( InFile, (char *) &macBin->totalLen, 4L ) != 4L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* 120: */
- if (Read( InFile, (char *) &macBin->secLen, 2L ) != 2L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* 122: MacBinary II version */
- if (Read( InFile, (char *) &len, 1L ) != 1L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* 123: MacBinary II minimum version */
- if (Read( InFile, (char *) &len, 1L ) != 1L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* 124: CRC of previous 124 bytes */
- if (Read( InFile, (char *) &macBin->secLen, 2L ) != 2L) {
- lerr = ERR_Read;
- goto out;
- }
-
- /* Junk to pad out to 128? */
- if (Read( InFile, (char *) &junk[0], 16L ) != 16L) {
- lerr = ERR_Read;
- goto out;
- }
-
- lerr = 0;
- out:
- SetError(lerr);
- if (lerr) return(FALSE);
- else return(TRUE);
- }
-
-
- BOOL SkipMacBinary (BPTR handle)
- {
- macBinaryHeader macbin;
-
- if (IsMacBinary(handle)) {
- Seek(handle, 128, OFFSET_BEGINNING);
- // ReadMacBinary(handle, &macbin));
- }
- return(TRUE);
- }
-
- /* risky at best, but it isn't too likely to change anytime soon */
-
- struct BIO {
- BPTR DosHandle; /* dos handle */
- ULONG Mode; /* open mode (OLDFILE or NEWFILE) */
- ULONG StructSize; /* size of this structure */
- ULONG BufSize; /* size of just the buffer */
- ULONG Count; /* remaining bytes in buffer */
- UBYTE *Ptr; /* pointer to current buffer location */
- UBYTE Buffer[1];
- };
-
- BOOL BSkipMacBinary (struct BIO *bio)
- {
- BFlush(bio);
- return(SkipMacBinary(bio->DosHandle));
- }
-
-