Project Wonder 2.0

er.extensions
Class ERXMacBinarySwissArmyKnife

java.lang.Object
  extended byer.extensions.ERXMacBinarySwissArmyKnife

public class ERXMacBinarySwissArmyKnife
extends Object

Useful for extracting files from binhexed files, ie when a Mac user uploads a file.


Field Summary
static int _AT
          Offsets in header where the fields are located.
static int CRC_AT
          Offsets in header where the fields are located.
static int FILE_CREATOR_AT
          Offsets in header where the fields are located.
static int FILE_TYPE_AT
          Offsets in header where the fields are located.
static int FINDER_FLAGS1_AT
          Offsets in header where the fields are located.
static int FINDER_FLAGS2_AT
          Offsets in header where the fields are located.
static int FINDER_FLAGS3_AT
          Offsets in header where the fields are located.
static int FINDER_HORZ_AT
          Offsets in header where the fields are located.
static int FINDER_VERT_AT
          Offsets in header where the fields are located.
static int FINDER_WINDOW_AT
          Offsets in header where the fields are located.
static int FLAG_LOCKED_AT
          Offsets in header where the fields are located.
static int LEN_COMMENT_AT
          Offsets in header where the fields are located.
static int LEN_DATA_FORK_AT
          Offsets in header where the fields are located.
static int LEN_RES_FORK_AT
          Offsets in header where the fields are located.
static int LEN_SECONDARY_AT
          Offsets in header where the fields are located.
protected static int[] levelMasks
          Bit-masks for different levels.
static long LIMIT_DATAFORK
          Enforce this limit on data-fork length, due either to Mac OS API or field-width.
static int LIMIT_NAME
          Enforce this limit on name-length, regardless of available space in header.
static int LIMIT_RESFORK
          Enforce this limit on resource-fork length, due either to Mac OS API or field-width.
static ERXLogger log
           
static int MACBINARY_HEADER_LEN
          Length of a MacBinary header, always the first component of the file, and the only required element.
static int MB1_VERSION
          Possible values in VERSION_TARGET_AT and VERSION_MIN_AT byte-sized fields.
static int MB2_VERSION
          Possible values in VERSION_TARGET_AT and VERSION_MIN_AT byte-sized fields.
static int MB3_SIGNATURE
          Distinctive int-sized value in MB3_SIGNATURE_AT field, only present for MB-3 format.
static int MB3_SIGNATURE_AT
          Offsets in header where the fields are located.
static int MB3_VERSION
          Possible values in VERSION_TARGET_AT and VERSION_MIN_AT byte-sized fields.
static int NAME_BYTES_AT
          Offsets in header where the fields are located.
static int NAME_LEN_AT
          Offsets in header where the fields are located.
static int NAME_SCRIPT_AT
          Offsets in header where the fields are located.
protected static boolean strict
           
static int VERSION_MIN_AT
          Offsets in header where the fields are located.
static int VERSION_OBSOLETE_AT
          Offsets in header where the fields are located.
static int VERSION_TARGET_AT
          Offsets in header where the fields are located.
static int WHEN_CREATED_AT
          Offsets in header where the fields are located.
static int WHEN_MODIFIED_AT
          Offsets in header where the fields are located.
static int ZERO_1_AT
          Offsets in header where the fields are located.
static int ZERO_2_AT
          Offsets in header where the fields are located.
static int ZERO_3_AT
          Offsets in header where the fields are located.
 
Constructor Summary
ERXMacBinarySwissArmyKnife()
           
ERXMacBinarySwissArmyKnife(NSData fileData)
           
 
Method Summary
 int calcCRC()
          Calculate and return a CRC over the header.
 int calcCRC2()
          Calculate and return an alternative header CRC.
static int calculateCRC(int seed, byte[] bytes, int offset, int count)
          Calculate a MacBinary CRC using the given starting seed, and proceeding over the given range of bytes.
 int checkFormat(boolean strict)
           
 byte[] getByteArray()
          Get the internal byte-array.
 byte getByteAt(int offset)
          Return the signed byte at the given offset.
 int getCRC()
          Return the header CRC, as embedded in the header itself.
 int getDataForkLength()
          Get the data-fork length from the header.
 int getIntAt(int offset)
          Return the signed int at the given offset.
 long getLongAt(int offset)
          Return the signed long at the given offset.
 int getResourceForkLength()
          Get the resource-fork length from the header.
 short getShortAt(int offset)
          Return the signed short at the given offset.
 int getUByteAt(int offset)
          Return an int holding the unsigned byte at the given offset.
 long getUIntAt(int offset)
          Return a long holding the unsigned int at the given offset.
 int getUShortAt(int offset)
          Return an int holding the unsigned short at the given offset.
 boolean isMacBinary(NSData fileData)
           
 boolean isValidCRC()
          Calculate and check the header CRC.
 void putByteAt(byte value, int offset)
          Put the given byte at the supplied offset.
 void putBytesAt(byte[] data, int offset)
          Put all the given bytes at the supplied offset.
 void putBytesAt(byte[] data, int offset, int count)
          Put the given bytes at the supplied offset.
 void putIntAt(int value, int offset)
          Put the given int at the supplied offset.
 void putLongAt(long value, int offset)
          Put the given long at the supplied offset.
 void putShortAt(short value, int offset)
          Put the given short at the supplied offset.
 void setByteArray(byte[] bytes)
          Use the given array to hold bytes.
 void setCRC()
          Calculate and set the CRC over the header bytes previously set, invoking calcCRC() to calculate the 16-bit value to store.
 NSData unwrapMacBinary(NSData fileContents)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

public static final ERXLogger log

ZERO_1_AT

public static final int ZERO_1_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

NAME_LEN_AT

public static final int NAME_LEN_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

NAME_BYTES_AT

public static final int NAME_BYTES_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

FILE_TYPE_AT

public static final int FILE_TYPE_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

FILE_CREATOR_AT

public static final int FILE_CREATOR_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

FINDER_FLAGS1_AT

public static final int FINDER_FLAGS1_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

ZERO_2_AT

public static final int ZERO_2_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

FINDER_VERT_AT

public static final int FINDER_VERT_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

FINDER_HORZ_AT

public static final int FINDER_HORZ_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

FINDER_WINDOW_AT

public static final int FINDER_WINDOW_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

FLAG_LOCKED_AT

public static final int FLAG_LOCKED_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

ZERO_3_AT

public static final int ZERO_3_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

LEN_DATA_FORK_AT

public static final int LEN_DATA_FORK_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

LEN_RES_FORK_AT

public static final int LEN_RES_FORK_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

WHEN_CREATED_AT

public static final int WHEN_CREATED_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

WHEN_MODIFIED_AT

public static final int WHEN_MODIFIED_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

LEN_COMMENT_AT

public static final int LEN_COMMENT_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

FINDER_FLAGS2_AT

public static final int FINDER_FLAGS2_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

MB3_SIGNATURE_AT

public static final int MB3_SIGNATURE_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

NAME_SCRIPT_AT

public static final int NAME_SCRIPT_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

FINDER_FLAGS3_AT

public static final int FINDER_FLAGS3_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

LEN_SECONDARY_AT

public static final int LEN_SECONDARY_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

VERSION_TARGET_AT

public static final int VERSION_TARGET_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

VERSION_MIN_AT

public static final int VERSION_MIN_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

CRC_AT

public static final int CRC_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

VERSION_OBSOLETE_AT

public static final int VERSION_OBSOLETE_AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

_AT

public static final int _AT
Offsets in header where the fields are located.

See Also:
Constant Field Values

MB1_VERSION

public static final int MB1_VERSION
Possible values in VERSION_TARGET_AT and VERSION_MIN_AT byte-sized fields.

See Also:
Constant Field Values

MB2_VERSION

public static final int MB2_VERSION
Possible values in VERSION_TARGET_AT and VERSION_MIN_AT byte-sized fields.

See Also:
Constant Field Values

MB3_VERSION

public static final int MB3_VERSION
Possible values in VERSION_TARGET_AT and VERSION_MIN_AT byte-sized fields.

See Also:
Constant Field Values

MB3_SIGNATURE

public static final int MB3_SIGNATURE
Distinctive int-sized value in MB3_SIGNATURE_AT field, only present for MB-3 format.

See Also:
Constant Field Values

levelMasks

protected static final int[] levelMasks
Bit-masks for different levels. The mask at 0 clears only the "do not restore" flags, but none of the "reserved" bits. "Reserved" bits are cleared to zero in the other masks -- you may not want


MACBINARY_HEADER_LEN

public static final int MACBINARY_HEADER_LEN
Length of a MacBinary header, always the first component of the file, and the only required element. A MacBinary file must be at least this length.

See Also:
Constant Field Values

LIMIT_DATAFORK

public static final long LIMIT_DATAFORK
Enforce this limit on data-fork length, due either to Mac OS API or field-width.

See Also:
Constant Field Values

LIMIT_RESFORK

public static final int LIMIT_RESFORK
Enforce this limit on resource-fork length, due either to Mac OS API or field-width.

See Also:
Constant Field Values

LIMIT_NAME

public static final int LIMIT_NAME
Enforce this limit on name-length, regardless of available space in header.

See Also:
Constant Field Values

strict

protected static boolean strict
Constructor Detail

ERXMacBinarySwissArmyKnife

public ERXMacBinarySwissArmyKnife()

ERXMacBinarySwissArmyKnife

public ERXMacBinarySwissArmyKnife(NSData fileData)
Method Detail

isMacBinary

public boolean isMacBinary(NSData fileData)

unwrapMacBinary

public NSData unwrapMacBinary(NSData fileContents)

checkFormat

public int checkFormat(boolean strict)

getByteAt

public byte getByteAt(int offset)
Return the signed byte at the given offset.


getUByteAt

public int getUByteAt(int offset)
Return an int holding the unsigned byte at the given offset.


getShortAt

public short getShortAt(int offset)
Return the signed short at the given offset.


getUShortAt

public int getUShortAt(int offset)
Return an int holding the unsigned short at the given offset.


getIntAt

public int getIntAt(int offset)
Return the signed int at the given offset.


getUIntAt

public long getUIntAt(int offset)
Return a long holding the unsigned int at the given offset.


getLongAt

public long getLongAt(int offset)
Return the signed long at the given offset.


putByteAt

public void putByteAt(byte value,
                      int offset)
Put the given byte at the supplied offset.


putBytesAt

public void putBytesAt(byte[] data,
                       int offset)
Put all the given bytes at the supplied offset.


putBytesAt

public void putBytesAt(byte[] data,
                       int offset,
                       int count)
Put the given bytes at the supplied offset.


putShortAt

public void putShortAt(short value,
                       int offset)
Put the given short at the supplied offset.


putIntAt

public void putIntAt(int value,
                     int offset)
Put the given int at the supplied offset.


putLongAt

public void putLongAt(long value,
                      int offset)
Put the given long at the supplied offset.


getByteArray

public final byte[] getByteArray()
Get the internal byte-array.


setByteArray

public final void setByteArray(byte[] bytes)
Use the given array to hold bytes. Accepts null and/or zero-length arrays without error, though a subsequent get or put will throw an exception.


getDataForkLength

public int getDataForkLength()
Get the data-fork length from the header.


getResourceForkLength

public int getResourceForkLength()
Get the resource-fork length from the header.


calculateCRC

public static int calculateCRC(int seed,
                               byte[] bytes,
                               int offset,
                               int count)
Calculate a MacBinary CRC using the given starting seed, and proceeding over the given range of bytes. The returned CRC value is a 16-bit result in an int, because it's unsigned.


setCRC

public void setCRC()
Calculate and set the CRC over the header bytes previously set, invoking calcCRC() to calculate the 16-bit value to store. You should only invoke this after setting every other field of interest, such as name and name-encoding, data-fork len, res-fork len, secondary-header len, etc. Since the CRC is calculated internally, there is no parameter to this method, even though it's a "setter" method.

In most cases, you will find finishHeader() more useful since it finishes all the assembly for a particular header-format you select.


isValidCRC

public boolean isValidCRC()
Calculate and check the header CRC.


calcCRC

public int calcCRC()
Calculate and return a CRC over the header. The CRC value is in the low 16-bits of the returned int.


calcCRC2

public int calcCRC2()
Calculate and return an alternative header CRC. Where calcCRC() calculates over the first 124 bytes of the header, this calculates over the first 126 bytes of the header. I'm guessing that with a valid CRC-value in the header, the returned value is zero, but I don't know enough about CRC theory and practice to assert that confidently. This is more of a Greg's-hacky-toy than anything that might actually be useful.


getCRC

public int getCRC()
Return the header CRC, as embedded in the header itself.


Last updated: Do, Dez 9, 2004 • 12:46 PM CET

Copyright © 2002 – 2004 Project Wonder.