Beispiel #1
0
        /// <summary>
        /// Read the next RIFF element invoking the correct delegate.
        /// Returns FALSE if there are no more elements to read.
        /// </summary>
        /// <param name="chunk">Method to invoke if a CHUNK element is found</param>
        /// <param name="list">Method to invoke if a LIST element is found</param>
        /// <returns></returns>
        public bool ReadNext(ProcessChunkElement chunkCallback, ProcessListElement listCallback = null, ProcessRiffElement riffCallback = null)
        {
            // Done?
            if (reader.BytesLeft (nextElementOffset) < 8)
                return false;

            // We have enough bytes, read
            uint fourCC = reader.ReadUInt32 (ref nextElementOffset);
            int size = reader.ReadInt32 (ref nextElementOffset);

            // Do we have enough bytes?
            if (reader.BytesLeft (nextElementOffset) < size) {
                // Skip over the bad data and throw an exception
                nextElementOffset = size;
                throw new RiffParserException ("Element size mismatch for element " +
                    FromFourCC (fourCC) + " need " + size.ToString ());
            }

            // Examine the element, is it a list or a chunk
            if (fourCC == RIFF4CC || fourCC == RIFX4CC) {
                // we have RIFF head element
                fourCC = reader.ReadUInt32 (ref nextElementOffset);

                // Truncated?
                if (reader.StreamLength < nextElementOffset + size - 4) {
                    throw new RiffParserException ("Error. Truncated stream");
                }

                if (riffCallback != null) {
                    bool processRiffContents = riffCallback (this, fourCC, size - 4);
                    if (!processRiffContents) {
                        nextElementOffset += size - 4;
                    }
                }
            } else if (fourCC == LIST4CC) {
                // We have a list
                fourCC = reader.ReadUInt32 (ref nextElementOffset);

                if (listCallback != null) {
                    bool processListContents = listCallback (this, fourCC, size - 4);
                    if (!processListContents) {
                        nextElementOffset += size - 4;
                    }
                }
            } else {
                // Calculated padded size - padded to WORD boundary
                int paddedSize = size;
                if ((size & 1) != 0)
                    paddedSize++;

                if (chunkCallback != null) {
                    chunkCallback (this, fourCC, size, paddedSize);
                }

                nextElementOffset += paddedSize;
            }
            return true;
        }
Beispiel #2
0
        /// <summary>
        /// Read the next RIFF element invoking the correct delegate.
        /// Returns FALSE if there are no more elements to read.
        /// </summary>
        /// <param name="chunk">Method to invoke if a CHUNK element is found</param>
        /// <param name="list">Method to invoke if a LIST element is found</param>
        /// <returns></returns>
        public bool ReadNext(ProcessChunkElement chunkCallback, ProcessListElement listCallback = null, ProcessRiffElement riffCallback = null)
        {
            // Done?
            if (reader.BytesLeft(nextElementOffset) < 8)
            {
                return(false);
            }

            // We have enough bytes, read
            uint fourCC = reader.ReadUInt32(ref nextElementOffset);
            int  size   = reader.ReadInt32(ref nextElementOffset);

            // Do we have enough bytes?
            if (reader.BytesLeft(nextElementOffset) < size)
            {
                // Skip over the bad data and throw an exception
                nextElementOffset = size;
                throw new RiffParserException("Element size mismatch for element " +
                                              FromFourCC(fourCC) + " need " + size.ToString());
            }

            // Examine the element, is it a list or a chunk
            if (fourCC == RIFF4CC || fourCC == RIFX4CC)
            {
                // we have RIFF head element
                fourCC = reader.ReadUInt32(ref nextElementOffset);

                // Truncated?
                if (reader.StreamLength < nextElementOffset + size - 4)
                {
                    throw new RiffParserException("Error. Truncated stream");
                }

                if (riffCallback != null)
                {
                    bool processRiffContents = riffCallback(this, fourCC, size - 4);
                    if (!processRiffContents)
                    {
                        nextElementOffset += size - 4;
                    }
                }
            }
            else if (fourCC == LIST4CC)
            {
                // We have a list
                fourCC = reader.ReadUInt32(ref nextElementOffset);

                if (listCallback != null)
                {
                    bool processListContents = listCallback(this, fourCC, size - 4);
                    if (!processListContents)
                    {
                        nextElementOffset += size - 4;
                    }
                }
            }
            else
            {
                // Calculated padded size - padded to WORD boundary
                int paddedSize = size;
                if ((size & 1) != 0)
                {
                    paddedSize++;
                }

                if (chunkCallback != null)
                {
                    chunkCallback(this, fourCC, size, paddedSize);
                }

                nextElementOffset += paddedSize;
            }
            return(true);
        }