Exemplo n.º 1
0
        internal static unsafe TFileStream CreateTFileStream(string filePath, FileMode mode = FileMode.Open, FileAccess access = FileAccess.Read, FileShare share = FileShare.Read)
        {
            FileStream     fs     = new FileStream(filePath, mode, access, share);
            SafeFileHandle handle = fs.SafeFileHandle;
            IntPtr         ptr    = handle.DangerousGetHandle();

            _handlesToStreams.Add(ptr, fs);

            TFileStream result = new TFileStream();

            result.RawFilePos    = 0;
            result.hFile         = ptr;
            result.StreamFlags   = StreamFlags.None;
            result.StreamGetPos  = Marshal.GetFunctionPointerForDelegate((STREAM_GETPOS)GetPos);
            result.StreamRead    = Marshal.GetFunctionPointerForDelegate((STREAM_READ)Read);
            result.StreamWrite   = Marshal.GetFunctionPointerForDelegate((STREAM_WRITE)Write);
            result.StreamGetSize = Marshal.GetFunctionPointerForDelegate((STREAM_GETSIZE)GetSize);
            result.StreamSetSize = Marshal.GetFunctionPointerForDelegate((STREAM_SETSIZE)SetSize);

            return(result);
        }
Exemplo n.º 2
0
 private static unsafe bool Write(TFileStream* pStream, ref ulong pByteOffset, void* pvBuffer, uint dwBytesToWrite)
 {
     // Not supported in v1
     return false;
 }
Exemplo n.º 3
0
 private static unsafe bool SetSize(TFileStream* pStream, ulong FileSize)
 {
     // Not supported in v1
     return false;
 }
Exemplo n.º 4
0
        private static unsafe bool Read(TFileStream* pStream, ref ulong byteOffset, void* pwBuffer, uint dwBytesToRead)
        {
            Debug.Assert(dwBytesToRead < int.MaxValue, "Unsupported case; must read < 2GiB worth of data per chunk.");

            if (pStream == null)
                return false;

            FileStream fs;
            if (!_handlesToStreams.TryGetValue(pStream->hFile, out fs))
                return false;

            byte[] tmpBuf = new byte[dwBytesToRead];
            if (byteOffset != 0)
            {
                long pos = unchecked((long)byteOffset);
                fs.Seek(pos, SeekOrigin.Begin);
            }

            fs.Read(tmpBuf, 0, unchecked((int)dwBytesToRead));
            IntPtr dest = new IntPtr(pwBuffer);
            Marshal.Copy(tmpBuf, 0, dest, unchecked((int)dwBytesToRead));
            byteOffset = (ulong)fs.Position;

            return true;
        }
Exemplo n.º 5
0
        private static unsafe bool GetSize(TFileStream* pStream, ref ulong FileSize)
        {
            if (pStream == null)
                return false;

            FileStream fs;
            if (!_handlesToStreams.TryGetValue(pStream->hFile, out fs))
                return false;

            FileSize = (ulong)fs.Length;
            return true;
        }
Exemplo n.º 6
0
        private static unsafe bool GetPos(TFileStream* pStream, ref ulong byteOffset)
        {
            if (pStream == null)
                return false;

            FileStream fs;
            if (!_handlesToStreams.TryGetValue(pStream->hFile, out fs))
                return false;

            long pos = fs.Position;
            byteOffset = (ulong)pos;

            return true;
        }
Exemplo n.º 7
0
        internal static unsafe TFileStream CreateTFileStream(string filePath, FileMode mode = FileMode.Open, FileAccess access = FileAccess.Read, FileShare share = FileShare.Read)
        {
            FileStream fs = new FileStream(filePath, mode, access, share);
            SafeFileHandle handle = fs.SafeFileHandle;
            IntPtr ptr = handle.DangerousGetHandle();
            _handlesToStreams.Add(ptr, fs);

            TFileStream result = new TFileStream();
            result.RawFilePos = 0;
            result.hFile = ptr;
            result.StreamFlags = StreamFlags.None;
            result.StreamGetPos = Marshal.GetFunctionPointerForDelegate((STREAM_GETPOS)GetPos);
            result.StreamRead = Marshal.GetFunctionPointerForDelegate((STREAM_READ)Read);
            result.StreamWrite = Marshal.GetFunctionPointerForDelegate((STREAM_WRITE)Write);
            result.StreamGetSize = Marshal.GetFunctionPointerForDelegate((STREAM_GETSIZE)GetSize);
            result.StreamSetSize = Marshal.GetFunctionPointerForDelegate((STREAM_SETSIZE)SetSize);

            return result;
        }