
#ifndef VOB_READER_H_
#define VOB_READER_H_

//SVE 03.06.2001
#include <windows.h>
#include "runstate.h"

#include <stdio.h>
#include <list>
#include <vector>
#include "AwaString.h"

class VobReader
{
public:
	enum FrameType
	{
		AC3,
		VID,
		AUD
	};

	int IgnoreAudio;
//SVE 03.06.2001
	runstate* m_pRunState;
//SVE 03.06.2001	double m_FileSize;
//SVE 03.06.2001	double m_FilePos;
//SVE 03.06.2001	double m_FilePosRead;
	unsigned long BufferSize;

	typedef void (*messageCallback)(const char* message, int priority);

	VobReader(messageCallback msgCb = NULL);
	~VobReader();

	bool addFile(const char* fileName);

	unsigned char* getFrame();
	unsigned char* getInterpretedFrame(FrameType& type, long& stream, unsigned long& size);

	unsigned char* interpreteFrame(FrameType& type, long& stream, unsigned long& size);

	unsigned long getM2VBytes(unsigned char* buffer, long requestedSize, const char* outputDirectory);
	unsigned long getLastLBA();

	AwaString getStringKey();

	static void initDecode();

	void SetReadBuffer( unsigned long size ) { BufferSize = size; }

private:
	void message(const char* msg, int priority);

	messageCallback _messageCallback;
	unsigned char* _buffer;
	//bool _angles;
	bool _skippingLbasForAngles;
	int _skipVobId;
	unsigned long _lba, _nextLba, _lastLba;

//SVE 03.06.2001	std::list<AwaString> _files;
	FILE* _currentFile;
//SVE 03.06.2001	AwaString _currentFileName;

	//getM2VBytes stuff
	unsigned char* _ptReadStart;
	unsigned char* _ptReadEnd;
	std::vector<FILE*> _ac3Files;
	std::vector<FILE*> _audFile;

	// VobDec stuff

	bool _keyComputed;
	int _lfsr0;
	int _lfsr1;

	static unsigned char _reverse[];
	static unsigned char _table[];

	static inline void _ClockLfsr0Forward(int &lfsr0);
	static inline void _ClockLfsr1Forward(int &lfsr1);
	static inline void _ClockBackward(int &lfsr0, int &lfsr1);
	static void _Salt(int &lfsr0, int &lfsr1, const unsigned char salt[5]);
	static int _FindLfsr(const unsigned char *crypt, int offset, const unsigned char *plain, int &result0, int &result1);
	bool _FindKey(const char *filename);
	static void _Decrypt(unsigned char *buffer, int lfsr0, int lfsr1);
};

#endif // VOB_READER_H_