| 1 | #include "advancedsearchdialog.h" |
| 2 | #include "ui_advancedsearchdialog.h" |
| 3 | #include <QDebug> |
| 4 | #include <QMessageBox> |
| 5 | #include "resultType.h" |
| 6 | #include "worditemdelegate.h" |
| 7 | #include <QProgressDialog> |
| 8 | #include <QMessageBox> |
| 9 | |
| 10 | AdvancedSearchDialog::AdvancedSearchDialog(QHexEdit *hexEdit, QWidget *parent) : |
| 11 | QDialog(parent), |
| 12 | ui(new Ui::AdvancedSearchDialog) |
| 13 | { |
| 14 | ui->setupUi(this); |
| 15 | _hexEdit = hexEdit; |
| 16 | //model = new TableModel(this); |
| 17 | |
| 18 | |
| 19 | } |
| 20 | |
| 21 | AdvancedSearchDialog::~AdvancedSearchDialog() |
| 22 | { |
| 23 | delete ui; |
| 24 | } |
| 25 | |
| 26 | qint64 AdvancedSearchDialog::findNext() |
| 27 | { |
| 28 | qint64 from = _hexEdit->cursorPosition() / 2; |
| 29 | _findBa = getContent(ui->cbFindFormat->currentIndex(), ui->cbFind->currentText()); |
| 30 | qint64 idx = -1; |
| 31 | |
| 32 | if (_findBa.length() > 0) |
| 33 | { |
| 34 | if (ui->cbBackwards->isChecked()) |
| 35 | idx = _hexEdit->lastIndexOf(_findBa, from); |
| 36 | else |
| 37 | idx = _hexEdit->indexOf(_findBa, from,ui->cbRegex->isChecked(),ui->cbCase->isChecked()); |
| 38 | } |
| 39 | return idx; |
| 40 | } |
| 41 | |
| 42 | void AdvancedSearchDialog::on_pbFind_clicked() |
| 43 | { |
| 44 | if(ui->cbBegin->isChecked() == true) |
| 45 | { |
| 46 | _hexEdit->setCursorPosition(0); |
| 47 | ui->cbBegin->setChecked(false); |
| 48 | } |
| 49 | findNext(); |
| 50 | |
| 51 | } |
| 52 | |
| 53 | QByteArray AdvancedSearchDialog::getContent(int comboIndex, const QString &input) |
| 54 | { |
| 55 | QByteArray findBa; |
| 56 | switch (comboIndex) |
| 57 | { |
| 58 | case 2: // hex |
| 59 | findBa = QByteArray::fromHex(input.toLatin1()); |
| 60 | break; |
| 61 | case 0: // text |
| 62 | findBa = input.toUtf8(); |
| 63 | break; |
| 64 | case 1: |
| 65 | |
| 66 | findBa = QByteArray::fromRawData((char*)(input.utf16()),input.length()*2); |
| 67 | } |
| 68 | return findBa; |
| 69 | } |
| 70 | QString AdvancedSearchDialog::binToStr(QByteArray bin) |
| 71 | { |
| 72 | QString outString; |
| 73 | for(int i=0;i<bin.length();i++) |
| 74 | { |
| 75 | if(bin.at(i)<32 || uchar(bin.at(i))>127) |
| 76 | { |
| 77 | outString += QString("."); |
| 78 | } |
| 79 | else if(bin.at(i) == '<') |
| 80 | { |
| 81 | outString += QString("<"); |
| 82 | } |
| 83 | else if(bin.at(i) == '>') |
| 84 | { |
| 85 | outString += QString(">"); |
| 86 | } |
| 87 | else if(bin.at(i) == '&') |
| 88 | { |
| 89 | outString += QString("&"); |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | outString += QString(bin.at(i)); |
| 94 | } |
| 95 | } |
| 96 | return outString; |
| 97 | } |
| 98 | void AdvancedSearchDialog::findAll() |
| 99 | { |
| 100 | progrDialog = new QProgressDialog("Task in progress...","Cancel",0,100,this); |
| 101 | progrDialog->setValue(0); |
| 102 | progrDialog->setModal(true); |
| 103 | progrDialog->show(); |
| 104 | |
| 105 | |
| 106 | |
| 107 | resultslist.clear(); |
| 108 | int ret = 1; |
| 109 | Result_S result; |
| 110 | int count=0; |
| 111 | |
| 112 | while(ret > 0) |
| 113 | { |
| 114 | ret = findNext(); |
| 115 | |
| 116 | if(progrDialog->wasCanceled() == true) |
| 117 | { |
| 118 | break; |
| 119 | } |
| 120 | progrDialog->setValue(int(100.0*double(_hexEdit->cursorPosition()/2)/double(_hexEdit->getSize()))); |
| 121 | QCoreApplication::processEvents(); |
| 122 | |
| 123 | if(ret > -1) |
| 124 | { |
| 125 | QByteArray preData; |
| 126 | QByteArray postData; |
| 127 | |
| 128 | result.cursor=ret; |
| 129 | QByteArray inData = _hexEdit->selectedDataBa(); |
| 130 | if(_hexEdit->cursorPosition()/2 > 8) |
| 131 | { |
| 132 | preData = _hexEdit->dataAt(ret-8,8); |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | preData = _hexEdit->dataAt(ret-(_hexEdit->cursorPosition()/2),_hexEdit->cursorPosition()/2); |
| 137 | } |
| 138 | qint64 remainingBytes = _hexEdit->getSize() - _hexEdit->cursorPosition()/2; |
| 139 | if(remainingBytes > 8) |
| 140 | { |
| 141 | postData = _hexEdit->dataAt(ret+inData.length(),8); |
| 142 | } |
| 143 | else { |
| 144 | postData = _hexEdit->dataAt(ret+inData.length(),remainingBytes); |
| 145 | } |
| 146 | if(resultslist.length() > 0) |
| 147 | { |
| 148 | if(resultslist.last().cursor == result.cursor) |
| 149 | { |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | result.datastr = binToStr(preData) + QString("<b style='color:red'>")+ binToStr(inData) + QString("</b>")+ binToStr(postData); |
| 155 | result.hexdatastr = preData.toHex()+ QString("<b style='color:red'>") + inData.toHex() + QString("</b>")+ postData.toHex(); |
| 156 | resultslist.append(result); |
| 157 | count++; |
| 158 | } |
| 159 | |
| 160 | if(count > 65535) |
| 161 | { |
| 162 | |
| 163 | QMessageBox::warning(this, tr("HexWalk"),tr("Too much occurrencies found, stopping search.")); |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | QString message = QString("%1 occurrencies found").arg(count); |
| 169 | QMessageBox::information(this, tr("HexWalk"),message); |
| 170 | |
| 171 | |
| 172 | setData(); |
| 173 | ui->resultsTableView->resizeColumnsToContents(); |
| 174 | ui->resultsTableView->verticalHeader()->show(); |
| 175 | ui->resultsTableView->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 176 | QCoreApplication::processEvents(); |
| 177 | |
| 178 | } |
| 179 | void AdvancedSearchDialog::on_pbFindAll_clicked() |
| 180 | { |
| 181 | if(ui->cbBegin->isChecked() == true) |
| 182 | { |
| 183 | _hexEdit->setCursorPosition(0); |
| 184 | } |
| 185 | findAll(); |
| 186 | } |
| 187 | |
| 188 | TableModel::TableModel(QObject *parent) : QAbstractTableModel(parent) |
| 189 | { |
| 190 | } |
| 191 | |
| 192 | |
| 193 | void TableModel::populateData(const QList<Result_S> &resultlist) |
| 194 | { |
| 195 | tm_cursor.clear(); |
| 196 | tm_content.clear(); |
| 197 | tm_contenthex.clear(); |
| 198 | |
| 199 | for(int i=0;i<resultlist.count();i++) |
| 200 | { |
| 201 | QString cursor; |
| 202 | cursor = QString("%1").arg(resultlist.at(i).cursor,4,16); |
| 203 | tm_cursor.append(cursor); |
| 204 | tm_content.append(resultlist.at(i).datastr); |
| 205 | tm_contenthex.append(resultlist.at(i).hexdatastr); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | int TableModel::rowCount(const QModelIndex &parent) const |
| 216 | { |
| 217 | Q_UNUSED(parent); |
| 218 | return tm_cursor.length(); |
| 219 | } |
| 220 | |
| 221 | int TableModel::columnCount(const QModelIndex &parent) const |
| 222 | { |
| 223 | Q_UNUSED(parent); |
| 224 | return 3; |
| 225 | } |
| 226 | |
| 227 | QVariant TableModel::data(const QModelIndex &index, int role) const |
| 228 | { |
| 229 | if (!index.isValid() || role != Qt::DisplayRole) { |
| 230 | return QVariant(); |
| 231 | } |
| 232 | if (index.column() == 0) { |
| 233 | return tm_cursor[index.row()]; |
| 234 | } else if (index.column() == 1) { |
| 235 | return tm_content[index.row()]; |
| 236 | }else if (index.column() == 2) { |
| 237 | return tm_contenthex[index.row()]; |
| 238 | } |
| 239 | |
| 240 | return QVariant(); |
| 241 | } |
| 242 | |
| 243 | QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 244 | { |
| 245 | if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { |
| 246 | if (section == 0) { |
| 247 | return QString("Cursor"); |
| 248 | } else if (section == 2) { |
| 249 | return QString("Content(Hex)"); |
| 250 | }else if (section == 1) { |
| 251 | return QString("Content"); |
| 252 | } |
| 253 | } |
| 254 | return QVariant(); |
| 255 | } |
| 256 | |
| 257 | void AdvancedSearchDialog::setData() |
| 258 | { |
| 259 | progrDialog->cancel(); |
| 260 | |
| 261 | try{ |
| 262 | if(model) |
| 263 | { |
| 264 | delete model; |
| 265 | } |
| 266 | model = new TableModel(this); |
| 267 | |
| 268 | model->populateData(resultslist); |
| 269 | ui->resultsTableView->clearSpans(); |
| 270 | ui->resultsTableView->setModel(model); |
| 271 | auto wordItemDelegate = new WordItemDelegate(this); |
| 272 | ui->resultsTableView->setItemDelegate(wordItemDelegate); |
| 273 | } |
| 274 | catch(QException &) |
| 275 | { |
| 276 | qInfo() <<"Exception!!\n"; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | void AdvancedSearchDialog::on_resultsTableView_clicked(const QModelIndex &index) |
| 281 | { |
| 282 | //_hexEdit->indexOf("",resultslist.at(index.row()).cursor,ui->cbRegex->isChecked(),ui->cbCase->isChecked(),ui->cbFindFormat->currentIndex()==1); |
| 283 | _hexEdit->setCursorPosition(resultslist.at(index.row()).cursor); |
| 284 | _hexEdit->indexOf(_findBa, resultslist.at(index.row()).cursor - 1,ui->cbRegex->isChecked(),ui->cbCase->isChecked()); |
| 285 | _hexEdit->update(); |
| 286 | |
| 287 | } |
| 288 | void TableModel::clearData() |
| 289 | { |
| 290 | tm_cursor.clear(); |
| 291 | tm_content.clear(); |
| 292 | tm_contenthex.clear(); |
| 293 | } |
| 294 | void AdvancedSearchDialog::on_pbCancel_clicked() |
| 295 | { |
| 296 | |
| 297 | resultslist.clear(); |
| 298 | |
| 299 | |
| 300 | } |
| 301 | |
| 302 | |
| 303 | void AdvancedSearchDialog::on_cbCase_clicked() |
| 304 | { |
| 305 | if(ui->cbCase->isChecked()) |
| 306 | { |
| 307 | ui->cbRegex->setChecked(true); |
| 308 | ui->cbBackwards->setChecked(false); |
| 309 | } |
| 310 | |
| 311 | } |
| 312 | |
| 313 | |
| 314 | void AdvancedSearchDialog::on_cbFindFormat_currentIndexChanged(int index) |
| 315 | { |
| 316 | if(!ui->cbFindFormat->currentText().compare("UTF-8")) |
| 317 | { |
| 318 | ui->cbRegex->setEnabled(true); |
| 319 | ui->cbCase->setEnabled(true); |
| 320 | } |
| 321 | else |
| 322 | { |
| 323 | ui->cbRegex->setChecked(false); |
| 324 | ui->cbCase->setChecked(false); |
| 325 | ui->cbRegex->setEnabled(false); |
| 326 | ui->cbCase->setEnabled(false); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | |
| 331 | void AdvancedSearchDialog::on_cbBackwards_clicked() |
| 332 | { |
| 333 | if(ui->cbBackwards->isChecked()) |
| 334 | { |
| 335 | ui->cbRegex->setChecked(false); |
| 336 | ui->cbCase->setChecked(false); |
| 337 | ui->cbBegin->setChecked(false); |
| 338 | } |
| 339 | |
| 340 | } |
| 341 | |
| 342 | |
| 343 | void AdvancedSearchDialog::on_cbBegin_clicked() |
| 344 | { |
| 345 | if(ui->cbBegin->isChecked()) |
| 346 | { |
| 347 | ui->cbBackwards->setChecked(false); |
| 348 | } |
| 349 | |
| 350 | } |
| 351 | |
| 352 | |
| 353 | void AdvancedSearchDialog::on_cbRegex_clicked() |
| 354 | { |
| 355 | if(ui->cbRegex->isChecked()) |
| 356 | { |
| 357 | ui->cbBackwards->setChecked(false); |
| 358 | } |
| 359 | else |
| 360 | { |
| 361 | ui->cbCase->setChecked(false); |
| 362 | } |
| 363 | } |
| 364 |