using System;
using System.Text;

namespace SharpQuake
{
	internal class WavHelper
	{
		public int FindChunk(string name, int startFromChunk)
		{
			int num = startFromChunk;
			int num2;
			do
			{
				num2 = num;
				if (num2 >= this._Wav.Length)
				{
					return -1;
				}
				int littleLong = this.GetLittleLong(num2 + 4);
				if (littleLong < 0)
				{
					return -1;
				}
				num = num2 + 8 + (littleLong + 1 & -2);
			}
			while (!(Encoding.ASCII.GetString(this._Wav, num2, 4) == name));
			return num2;
		}

		public short GetLittleShort(int index)
		{
			return (short)this._Wav[index] + (short)(this._Wav[index + 1] << 8);
		}

		public int GetLittleLong(int index)
		{
			return (int)this._Wav[index] + ((int)this._Wav[index + 1] << 8) + ((int)this._Wav[index + 2] << 16) + ((int)this._Wav[index + 3] << 24);
		}

		public WavHelper(byte[] wav)
		{
			this._Wav = wav;
		}

		private byte[] _Wav;
	}
}
