Example #1
0
        public static DiskCopyImage ReadFrom(byte[] dataFork)
        {
            if (null == dataFork)
            {
                throw new ArgumentNullException(nameof(dataFork));
            }

            // Based on: https://www.bigmessowires.com/2013/12/16/macintosh-diskcopy-4-2-floppy-image-converter/

            // Process header

            byte[] header = new byte[HeaderLength];
            Array.Copy(dataFork, header, HeaderLength);

            if (header[0x52] != 0x01)
            {
                throw new Exception("The input does not appear to have a DiskCopy 4.2 header. Missing one byte at offset 0x52.");
            }

            if (header[0x53] != 0x00)
            {
                throw new Exception("The input does not appear to have a DiskCopy 4.2 header. Missing zero byte at offset 0x53.");
            }

            int dataLength = ByteListExtensions.ReadInt32(header, DataLengthOffset);

            // Extract embedded data

            byte[] data = new byte[dataLength];
            Array.Copy(dataFork, HeaderLength, data, 0, dataLength);

            return(new DiskCopyImage()
            {
                Header = header,
                Data = data,
            });
        }
Example #2
0
        public static MacBinaryFile ReadFrom(Stream inputStream)
        {
            if (null == inputStream)
            {
                throw new ArgumentNullException(nameof(inputStream));
            }

            // Based on spec: https://github.com/mietek/theunarchiver/wiki/MacBinarySpecs

            using BinaryReader binaryReader = new BinaryReader(inputStream);

            byte[] header = binaryReader.ReadBytes(HeaderLength);

            if (header[0x00] != 0)
            {
                throw new Exception("The input does not appear to have a MacBinary header. Missing zero byte at offset 0x00.");
            }

            int filenameLength = header[FileNameLengthOffset];

            if (filenameLength < MinFileNameLength || filenameLength > MaxFileNameLength)
            {
                throw new Exception("The input does not appear to have a MacBinary header. Filename length is out of range.");
            }

            string fileName = ByteListExtensions.ReadString(header, FileNameOffset, filenameLength);

            string fileType    = ByteListExtensions.ReadString(header, FileTypeOffest, FileTypeLength);
            string fileCreator = ByteListExtensions.ReadString(header, FileCreatorOffest, FileCreatorLength);

            ushort finderFlags = (ushort)((header[FinderFlagsHighOffset] << 8) + header[FinderFlagsLowOffset]);

            if (header[0x4A] != 0)
            {
                throw new Exception("The input does not appear to have a MacBinary header. Missing zero byte at offset 0x4A.");
            }

            if (header[0x52] != 0)
            {
                throw new Exception("The input does not appear to have a MacBinary header. Missing zero byte at offset 0x52.");
            }

            int dataForkLength = ByteListExtensions.ReadInt32(header, DataForkLengthOffset);

            if (dataForkLength < MinForkLength || dataForkLength > MaxForkLength)
            {
                throw new Exception("The input does not appear to have a MacBinary header. Data fork length is out of range.");
            }

            int resourceForkLength = ByteListExtensions.ReadInt32(header, ResourceForkLengthOffset);

            if (resourceForkLength < MinForkLength || resourceForkLength > MaxForkLength)
            {
                throw new Exception("The input does not appear to have a MacBinary header. Resource fork length is out of range.");
            }

            byte[] dataFork = dataForkLength > 0 ? binaryReader.ReadBytes(dataForkLength) : new byte[0];

            binaryReader.ReadBytes(dataForkLength % 128);

            byte[] resourceFork = resourceForkLength > 0 ? binaryReader.ReadBytes(resourceForkLength) : new byte[0];

            return(new MacBinaryFile()
            {
                FileName = fileName,
                FileType = fileType,
                FileCreator = fileCreator,
                FinderFlags = finderFlags,
                DataFork = dataFork,
                ResourceFork = resourceFork,
            });
        }