24 using System.Collections;
25 using System.Collections.Generic;
28 namespace SiliconStudio.
Paradox.Audio.Wave
33 internal class RiffParser : IEnumerator<RiffChunk>,
IEnumerable<RiffChunk>
35 private readonly
Stream input;
36 private readonly
long startPosition;
37 private readonly BinaryReader reader;
38 private readonly Stack<RiffChunk> chunckStack;
39 private bool descendNext;
40 private bool isEndOfRiff;
41 private bool isErrorState;
42 private RiffChunk current;
48 public RiffParser(
Stream input)
51 startPosition = input.Position;
52 reader =
new BinaryReader(input);
53 chunckStack =
new Stack<RiffChunk>();
73 public bool MoveNext()
80 long nextOffset = current.DataPosition;
90 nextOffset += current.Size;
92 if ((nextOffset & 1) != 0)
95 input.Position = nextOffset;
99 var currentChunkContainer = chunckStack.Peek();
100 long endOfOuterChunk = currentChunkContainer.DataPosition + currentChunkContainer.Size;
101 if (input.Position >= endOfOuterChunk)
105 if (chunckStack.Count == 0)
112 var fourCC = ((FourCC)reader.ReadUInt32());
113 bool isList = (fourCC ==
"LIST");
114 bool isHeader = (fourCC ==
"RIFF");
116 if (input.Position == (startPosition + 4) && !isHeader)
119 throw new InvalidOperationException(
"Invalid RIFF file format");
123 var chunkSize = reader.ReadUInt32();
126 if (isList || isHeader)
129 if (isHeader && chunkSize > (input.Length - 8))
132 throw new InvalidOperationException(
"Invalid RIFF file format");
135 fourCC = reader.ReadUInt32();
139 current =
new RiffChunk(input, fourCC, chunkSize, (uint)input.Position, isList, isHeader);
143 private void CheckState()
146 throw new InvalidOperationException(
"End of Riff. Cannot MoveNext");
149 throw new InvalidOperationException(
"The enumerator is in an error state");
155 public Stack<RiffChunk> ChunkStack {
get {
return chunckStack; } }
167 input.Position = startPosition;
176 var outerChunk = chunckStack.Pop();
177 input.Position = outerChunk.DataPosition + outerChunk.Size;
183 public void Descend()
186 chunckStack.Push(current);
194 public IList<RiffChunk> GetAllChunks()
196 var chunks =
new List<RiffChunk>();
197 foreach (var riffChunk
in this)
198 chunks.Add(riffChunk);
208 public RiffChunk Current
228 object IEnumerator.Current
237 IEnumerator IEnumerable.GetEnumerator()
239 return GetEnumerator();
The device failed due to a badly formed command. This is a run-time issue; The application should des...