/// <summary> /// Read the next RIFF element invoking the correct delegate. /// Returns true if an element can be read /// </summary> /// <param name="bytesleft">Reference to number of bytes left in the current list</param> /// <param name="chunk">Method to invoke if a chunk is found</param> /// <param name="list">Method to invoke if a list is found</param> /// <returns></returns> public bool ReadElement(ref int bytesleft, ProcessChunkElement chunk, ProcessListElement list) { // Are we done? if (TwoDWordSize > bytesleft) { return(false); } // We have enough bytes, read int fourCc; int size; ReadTwoInts(out fourCc, out size); // Reduce bytes left bytesleft -= TwoDWordSize; // Do we have enough bytes? if (bytesleft < size) { // Skip the bad data and throw an exception SkipData(bytesleft); bytesleft = 0; throw new RiffParserException("Element size mismatch for element " + FromFourCc(fourCc) + " need " + size + " but have only " + bytesleft); } // Examine the element, is it a list or a chunk string type = FromFourCc(fourCc); if (type == List4Cc) { // We have a list ReadOneInt(out fourCc); if (null == list) { SkipData(size - 4); } else { // Invoke the list method list(this, fourCc, size - 4); } // Adjust size bytesleft -= size; } else { // Calculated padded size - padded to WORD boundary int paddedSize = size; if (0 != (size & 1)) { ++paddedSize; } if (null == chunk) { SkipData(paddedSize); } else { chunk(this, fourCc, size, paddedSize); } // Adjust size bytesleft -= paddedSize; } return(true); }
/// <summary> /// Read the next RIFF element invoking the correct delegate. /// Returns true if an element can be read /// </summary> /// <param name="bytesleft">Reference to number of bytes left in the current list</param> /// <param name="chunk">Method to invoke if a chunk is found</param> /// <param name="list">Method to invoke if a list is found</param> /// <returns></returns> public bool ReadElement(ref int bytesleft, ProcessChunkElement chunk, ProcessListElement list) { // Are we done? if (TWODWORDSSIZE > bytesleft) { return false; } //Console.WriteLine(m_stream.Position.ToString() + ", " + bytesleft.ToString()); // We have enough bytes, read int FourCC; int size; ReadTwoInts(out FourCC, out size); // Reduce bytes left bytesleft -= TWODWORDSSIZE; // Do we have enough bytes? if (bytesleft < size) { // Skip the bad data and throw an exception SkipData(bytesleft); bytesleft = 0; throw new RiffParserException("Element size mismatch for element " + FromFourCC(FourCC) + " need " + size.ToString() + " but have only " + bytesleft.ToString()); } // Examine the element, is it a list or a chunk string type = FromFourCC(FourCC); if (0 == String.Compare(type, LIST4CC)) { // We have a list ReadOneInt(out FourCC); if (null == list) { SkipData(size - 4); } else { // Invoke the list method list(this, FourCC, size - 4); } // Adjust size bytesleft -= size; } else { // Calculated padded size - padded to WORD boundary int paddedSize = size; if (0 != (size & 1)) ++paddedSize; if (null == chunk) { SkipData(paddedSize); } else { chunk(this, FourCC, size, paddedSize); } // Adjust size bytesleft -= paddedSize; } return true; }
/// <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; }
/// <summary> /// Read the next RIFF element invoking the correct delegate. /// Returns true if an element can be read /// </summary> /// <param name="bytesleft">Reference to number of bytes left in the current list</param> /// <param name="chunk">Method to invoke if a chunk is found</param> /// <param name="list">Method to invoke if a list is found</param> /// <returns></returns> public bool ReadElement(ref int bytesleft, ProcessChunkElement chunk, ProcessListElement list) { // Are we done? if (TWODWORDSSIZE > bytesleft) { return(false); } //Console.WriteLine(m_stream.Position.ToString() + ", " + bytesleft.ToString()); // We have enough bytes, read int FourCC; int size; ReadTwoInts(out FourCC, out size); // Reduce bytes left bytesleft -= TWODWORDSSIZE; // Do we have enough bytes? if (bytesleft < size) { // Skip the bad data and throw an exception SkipData(bytesleft); bytesleft = 0; throw new RiffParserException("Element size mismatch for element " + FromFourCC(FourCC) + " need " + size.ToString() + " but have only " + bytesleft.ToString()); } // Examine the element, is it a list or a chunk string type = FromFourCC(FourCC); if (0 == String.Compare(type, LIST4CC)) { // We have a list ReadOneInt(out FourCC); if (null == list) { SkipData(size - 4); } else { // Invoke the list method list(this, FourCC, size - 4); } // Adjust size bytesleft -= size; } else { // Calculated padded size - padded to WORD boundary int paddedSize = size; if (0 != (size & 1)) { ++paddedSize; } if (null == chunk) { SkipData(paddedSize); } else { chunk(this, FourCC, size, paddedSize); } // Adjust size bytesleft -= paddedSize; } return(true); }
/// <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); }