Пример #1
0
        //-------------------------------------------------------------------
        // Name: ReadRiffHeader
        // Description:
        // Read the container header section. (The 'RIFF' or 'LIST' header.)
        //
        // This method verifies the header is well-formed and caches the
        // container's FOURCC type.
        //-------------------------------------------------------------------
        private void ReadRiffHeader()
        {
            int hr;
            int iRiffSize = Marshal.SizeOf(typeof(RIFFLIST));

            // Riff chunks must be WORD aligned
            if (!Utils.IsAligned(m_llContainerOffset, 2))
            {
                throw new COMException("bad alignment", E_InvalidArgument);
            }

            // Offset must be positive.
            if (m_llContainerOffset < 0)
            {
                throw new COMException("negative offset", E_InvalidArgument);
            }

            // Offset + the size of header must not overflow.
            if (long.MaxValue - m_llContainerOffset <= iRiffSize)
            {
                throw new COMException("overflow chunk", E_InvalidArgument);
            }

            RIFFLIST header = new RIFFLIST();
            int cbRead = 0;

            // Seek to the start of the container.
            hr = m_pStream.SetCurrentPosition(m_llContainerOffset);
            MFError.ThrowExceptionForHR(hr);

            // Read the header.
            IntPtr ip = Marshal.AllocCoTaskMem(iRiffSize);
            try
            {
                hr = m_pStream.Read(ip, iRiffSize, out cbRead);
                MFError.ThrowExceptionForHR(hr);

                // Make sure we read the number of bytes we expected.
                if (cbRead == iRiffSize)
                {
                    Marshal.PtrToStructure(ip, header);
                }
                else
                {
                    throw new COMException("read riff failure", E_InvalidArgument);
                }
            }
            finally
            {
                Marshal.FreeCoTaskMem(ip);
            }

            // Make sure the header ID matches what the caller expected.
            if (header.fcc != m_fccID)
            {
                throw new COMException("bad header id", E_InvalidArgument);
            }

            // The size given in the RIFF header does not include the 8-byte header.
            // However, our m_llContainerOffset is the offset from the start of the
            // header. Therefore our container size = listed size + size of header.

            m_dwContainerSize = header.cb + Marshal.SizeOf(typeof(RIFFCHUNK));
            m_fccType = header.fccListType;

            // Start of the first chunk = start of container + size of container header
            m_llCurrentChunkOffset = m_llContainerOffset + iRiffSize;

            ReadChunkHeader();
        }
Пример #2
0
        //-------------------------------------------------------------------
        // Name: ReadRiffHeader
        // Description:
        // Read the container header section. (The 'RIFF' or 'LIST' header.)
        //
        // This method verifies the header is well-formed and caches the
        // container's FOURCC type.
        //-------------------------------------------------------------------

        private void ReadRiffHeader()
        {
            HResult hr;
            int     iRiffSize = Marshal.SizeOf(typeof(RIFFLIST));

            // Riff chunks must be WORD aligned
            if (!Utils.IsAligned(m_llContainerOffset, 2))
            {
                throw new COMException("bad alignment", (int)HResult.E_INVALIDARG);
            }

            // Offset must be positive.
            if (m_llContainerOffset < 0)
            {
                throw new COMException("negative offset", (int)HResult.E_INVALIDARG);
            }

            // Offset + the size of header must not overflow.
            if (long.MaxValue - m_llContainerOffset <= iRiffSize)
            {
                throw new COMException("overflow chunk", (int)HResult.E_INVALIDARG);
            }

            RIFFLIST header = new RIFFLIST();
            int      cbRead = 0;

            // Seek to the start of the container.
            hr = m_pStream.SetCurrentPosition(m_llContainerOffset);
            MFError.ThrowExceptionForHR(hr);

            // Read the header.
            IntPtr ip = Marshal.AllocCoTaskMem(iRiffSize);

            try
            {
                hr = m_pStream.Read(ip, iRiffSize, out cbRead);
                MFError.ThrowExceptionForHR(hr);

                // Make sure we read the number of bytes we expected.
                if (cbRead == iRiffSize)
                {
                    Marshal.PtrToStructure(ip, header);
                }
                else
                {
                    throw new COMException("read riff failure", (int)HResult.E_INVALIDARG);
                }
            }
            finally
            {
                Marshal.FreeCoTaskMem(ip);
            }

            // Make sure the header ID matches what the caller expected.
            if (header.fcc != m_fccID)
            {
                throw new COMException("bad header id", (int)HResult.E_INVALIDARG);
            }

            // The size given in the RIFF header does not include the 8-byte header.
            // However, our m_llContainerOffset is the offset from the start of the
            // header. Therefore our container size = listed size + size of header.

            m_dwContainerSize = header.cb + Marshal.SizeOf(typeof(RIFFCHUNK));
            m_fccType         = header.fccListType;

            // Start of the first chunk = start of container + size of container header
            m_llCurrentChunkOffset = m_llContainerOffset + iRiffSize;

            ReadChunkHeader();
        }