/*
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.
*/

#include "DialogBinaryString.h"
#include "DebuggerCoreInterface.h"
#include "MemoryRegions.h"
#include "PluginAPI.h"
#include "ByteShiftArray.h"
#include "Util.h"
#include "Core.h"

#include <QVector>
#include <cstring>
#include <boost/smart_ptr.hpp>

//------------------------------------------------------------------------------
// Name: DialogBinaryString(PluginAPI &api, QWidget *parent)
// Desc: constructor
//------------------------------------------------------------------------------
DialogBinaryString::DialogBinaryString(PluginAPI &api, QWidget *parent) : QDialog(parent), m_API(api) {
	ui.setupUi(this);
	ui.progressBar->setValue(0);
	ui.listWidget->clear();
}

//------------------------------------------------------------------------------
// Name: doFind()
// Desc: 
//------------------------------------------------------------------------------
void DialogBinaryString::doFind() {

	const QByteArray b = ui.binaryString->value();
	ui.listWidget->clear();

	const int sz = b.size();
	if(sz != 0) {
		ByteShiftArray bsa(sz);

		m_API.memoryRegions().sync();
		const QVector<MemRegion> regions	= m_API.memoryRegions().regions();
		const edb::address_t pageSize		= m_API.debuggerCore->pageSize();

		int i = 0;
		foreach(const MemRegion &region, regions) {

			bsa.clear();
			
			// a short circut for speading things up
			if(ui.chkSkipNoAccess->isChecked() && !region.accessible()) {
				ui.progressBar->setValue(CalculatePercentage(++i, regions.size()));
				continue;
			}
			
			const edb::address_t size_in_pages = region.size() / pageSize;
			boost::scoped_array<quint8> pages(new quint8[size_in_pages * pageSize]);
			const quint8 *const pages_end = &pages[0] + region.size();
			
			if(m_API.debuggerCore->readPages(region.start, &pages[0], size_in_pages)) {
				const quint8 *p = &pages[0];
				QString temp;
				while(p != pages_end) {
					// shift in the next byte
					bsa << *p;
									
					// compare values..
					if(std::memcmp(bsa.data(), b.constData(), sz) == 0) {
						const edb::address_t addr = (p - &pages[0] + region.start) - sz + 1;
						const edb::address_t align = 1 << (ui.cmbAlignment->currentIndex() + 1);
						
						if(!ui.chkAlignment->isChecked() || (addr % align) == 0) {
							ui.listWidget->addItem(QString("%1").arg(addr, EDB_MAX_HEX, 16, QChar('0')));
						}
					}
					
					ui.progressBar->setValue(CalculatePercentage(i, regions.size(), p - &pages[0], region.size()));

					++p;
				}
			}			
			++i;
		}
	}
}

//------------------------------------------------------------------------------
// Name: on_btnFind_clicked()
// Desc: find button event handler
//------------------------------------------------------------------------------
void DialogBinaryString::on_btnFind_clicked() {

	ui.btnFind->setEnabled(false);
	ui.progressBar->setValue(0);
	doFind();
	ui.progressBar->setValue(100);
	ui.btnFind->setEnabled(true);
}

//------------------------------------------------------------------------------
// Name: on_listWidget_itemDoubleClicked(QListWidgetItem *item)
// Desc: follows the found item in the data view
//------------------------------------------------------------------------------
void DialogBinaryString::on_listWidget_itemDoubleClicked(QListWidgetItem *item) {
	bool ok;
	const edb::address_t addr = edb::core::stringToAddress(item->text(), ok);
	if(ok) {
		m_API.dumpData(addr, false);
	}
}
