/*
Copyright (C) 2006 Evan Teran
                   eteran@alum.rit.edu

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#ifndef DIALOGOPCODES_20061101_H_
#define DIALOGOPCODES_20061101_H_

#include "ui_dialogopcodes.h"
#include "Types.h"
#include "Instruction.h"
struct PluginAPI;

class QSortFilterProxyModel;

class DialogOpcodes : public QDialog {
	Q_OBJECT
	
public:
	DialogOpcodes(PluginAPI &api, QWidget *parent = 0);

public Q_SLOTS:
	void on_btnFind_clicked();
	void on_listWidget_itemDoubleClicked(QListWidgetItem *);
	
private:
	// we currently only support opcodes sequences up to 8 bytes big
	union OpcodeData {
		quint32	dword;
		quint64	qword;
		quint8	data[sizeof(quint64)];
	};

	void testESP_ADD_0(const OpcodeData &data, edb::address_t start_address);
	void testESP_ADD_4(const OpcodeData &data, edb::address_t start_address);
	void testESP_ADD_8(const OpcodeData &data, edb::address_t start_address);
	void testESP_SUB_4(const OpcodeData &data, edb::address_t start_address);
	void doFind();
	
	void addOpcodeResult(edb::Instruction &insn, edb::address_t rva);
	void addOpcodeResult(edb::Instruction &insn, edb::Instruction &insn2, edb::address_t rva);
	void addOpcodeResult(edb::Instruction &insn, edb::Instruction &insn2, edb::Instruction &insn3, edb::address_t rva);
	
	
	template<edb::Operand::Register REG>
	void testRegToIP(const DialogOpcodes::OpcodeData &data, edb::address_t start_address);
	
private:
	virtual void showEvent(QShowEvent *event);

private:
	Ui::DialogOpcodes		ui;
	QSortFilterProxyModel	*m_FilterModel;
	PluginAPI				&m_API;
};

#endif
