Пример #1
0
        public bool Identify(IFilter imageFilter)
        {
            Stream stream = imageFilter.GetDataForkStream();

            stream.Seek(0, SeekOrigin.Begin);
            // Even if comment is supposedly ASCII, I'm pretty sure most emulators allow Shift-JIS to be used :p
            Encoding shiftjis = Encoding.GetEncoding("shift_jis");

            nhdhdr = new Nhdr0Header();

            if (stream.Length < Marshal.SizeOf(nhdhdr))
            {
                return(false);
            }

            byte[] hdrB = new byte[Marshal.SizeOf(nhdhdr)];
            stream.Read(hdrB, 0, hdrB.Length);

            GCHandle handle = GCHandle.Alloc(hdrB, GCHandleType.Pinned);

            nhdhdr = (Nhdr0Header)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(Nhdr0Header));
            handle.Free();

            if (!nhdhdr.szFileID.SequenceEqual(signature))
            {
                return(false);
            }

            DicConsole.DebugWriteLine("NHDr0 plugin", "nhdhdr.szFileID = \"{0}\"",
                                      StringHandlers.CToString(nhdhdr.szFileID, shiftjis));
            DicConsole.DebugWriteLine("NHDr0 plugin", "nhdhdr.reserved1 = {0}", nhdhdr.reserved1);
            DicConsole.DebugWriteLine("NHDr0 plugin", "nhdhdr.szComment = \"{0}\"",
                                      StringHandlers.CToString(nhdhdr.szComment, shiftjis));
            DicConsole.DebugWriteLine("NHDr0 plugin", "nhdhdr.dwHeadSize = {0}", nhdhdr.dwHeadSize);
            DicConsole.DebugWriteLine("NHDr0 plugin", "nhdhdr.dwCylinder = {0}", nhdhdr.dwCylinder);
            DicConsole.DebugWriteLine("NHDr0 plugin", "nhdhdr.wHead = {0}", nhdhdr.wHead);
            DicConsole.DebugWriteLine("NHDr0 plugin", "nhdhdr.wSect = {0}", nhdhdr.wSect);
            DicConsole.DebugWriteLine("NHDr0 plugin", "nhdhdr.wSectLen = {0}", nhdhdr.wSectLen);

            return(true);
        }
Пример #2
0
        public bool Open(IFilter imageFilter)
        {
            Stream stream = imageFilter.GetDataForkStream();

            stream.Seek(0, SeekOrigin.Begin);
            // Even if comment is supposedly ASCII, I'm pretty sure most emulators allow Shift-JIS to be used :p
            Encoding shiftjis = Encoding.GetEncoding("shift_jis");

            nhdhdr = new Nhdr0Header();

            if (stream.Length < Marshal.SizeOf(nhdhdr))
            {
                return(false);
            }

            byte[] hdrB = new byte[Marshal.SizeOf(nhdhdr)];
            stream.Read(hdrB, 0, hdrB.Length);

            GCHandle handle = GCHandle.Alloc(hdrB, GCHandleType.Pinned);

            nhdhdr = (Nhdr0Header)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(Nhdr0Header));
            handle.Free();

            imageInfo.MediaType = MediaType.GENERIC_HDD;

            imageInfo.ImageSize            = (ulong)(stream.Length - nhdhdr.dwHeadSize);
            imageInfo.CreationTime         = imageFilter.GetCreationTime();
            imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
            imageInfo.MediaTitle           = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
            imageInfo.Sectors         = (ulong)(nhdhdr.dwCylinder * nhdhdr.wHead * nhdhdr.wSect);
            imageInfo.XmlMediaType    = XmlMediaType.BlockMedia;
            imageInfo.SectorSize      = (uint)nhdhdr.wSectLen;
            imageInfo.Cylinders       = (uint)nhdhdr.dwCylinder;
            imageInfo.Heads           = (uint)nhdhdr.wHead;
            imageInfo.SectorsPerTrack = (uint)nhdhdr.wSect;
            imageInfo.Comments        = StringHandlers.CToString(nhdhdr.szComment, shiftjis);

            nhdImageFilter = imageFilter;

            return(true);
        }
Пример #3
0
        public bool Close()
        {
            if (!IsWriting)
            {
                ErrorMessage = "Image is not opened for writing";

                return(false);
            }

            if (_imageInfo.Cylinders == 0)
            {
                _imageInfo.Cylinders       = (uint)(_imageInfo.Sectors / 8 / 17);
                _imageInfo.Heads           = 8;
                _imageInfo.SectorsPerTrack = 17;

                while (_imageInfo.Cylinders == 0)
                {
                    _imageInfo.Heads--;

                    if (_imageInfo.Heads == 0)
                    {
                        _imageInfo.SectorsPerTrack--;
                        _imageInfo.Heads = 16;
                    }

                    _imageInfo.Cylinders = (uint)(_imageInfo.Sectors / _imageInfo.Heads / _imageInfo.SectorsPerTrack);

                    if (_imageInfo.Cylinders == 0 &&
                        _imageInfo.Heads == 0 &&
                        _imageInfo.SectorsPerTrack == 0)
                    {
                        break;
                    }
                }
            }

            var header = new Nhdr0Header
            {
                szFileID   = _signature,
                szComment  = new byte[0x100],
                dwHeadSize = Marshal.SizeOf <Nhdr0Header>(),
                dwCylinder = (byte)_imageInfo.Cylinders,
                wHead      = (byte)_imageInfo.Heads,
                wSect      = (byte)_imageInfo.SectorsPerTrack,
                wSectLen   = (byte)_imageInfo.SectorSize,
                reserved2  = new byte[2],
                reserved3  = new byte[0xE0]
            };

            if (!string.IsNullOrEmpty(_imageInfo.Comments))
            {
                byte[] commentBytes = Encoding.GetEncoding("shift_jis").GetBytes(_imageInfo.Comments);

                Array.Copy(commentBytes, 0, header.szComment, 0,
                           commentBytes.Length >= 0x100 ? 0x100 : commentBytes.Length);
            }

            byte[] hdr    = new byte[Marshal.SizeOf <Nhdr0Header>()];
            IntPtr hdrPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(Marshal.SizeOf <Nhdr0Header>());

            System.Runtime.InteropServices.Marshal.StructureToPtr(header, hdrPtr, true);
            System.Runtime.InteropServices.Marshal.Copy(hdrPtr, hdr, 0, hdr.Length);
            System.Runtime.InteropServices.Marshal.FreeHGlobal(hdrPtr);

            _writingStream.Seek(0, SeekOrigin.Begin);
            _writingStream.Write(hdr, 0, hdr.Length);

            _writingStream.Flush();
            _writingStream.Close();
            IsWriting = false;

            return(true);
        }