summaryrefslogtreecommitdiff
path: root/compareDlg.cpp
blob: f8621f377ad48215764c8e4cee1c91b20c889b9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* $Id: compareDlg.cpp,v 1.8 2006/11/05 04:42:43 ganzhorn Exp $
 * This file is part of lfhex.
 * Copyright (C) 2006 Salem Ganzhorn <eyekode@yahoo.com>
 *
 * 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 version 2.
 *
 * 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 <QLabel>
#include <QLineEdit>
#include <QCheckBox>
#include <qlayout.h>
#include <qpushbutton.h>
#include <QToolBar>
#include <qtoolbutton.h>
#include <QFileDialog>
#include <qwidget.h>
#include <qapplication.h>
#include <qmessagebox.h>
#include <qcombobox.h>
#include <qtabwidget.h>
#include <QBitmap>
#include <QVBoxLayout>

#include "compareDlg.hpp"
#include "offsetConstraint.hpp"
#include "hexGui.hpp"
#include "reader.hpp"
#include "box.hpp"

// icon images:
#include "img/prev.xbm"
#include "img/next.xbm"
#include "img/last.xbm"
#include "img/first.xbm"
#include "img/exit.xbm"

//extern QApplication * qApp;

CompareDlg::CompareDlg(const char *file0,
		       const char *file1,
		       QWidget*parent)
    : QMainWindow(parent)
{
  tabLayout = new QTabWidget(this);
  setCentralWidget(tabLayout);

  QWidget *options = new QWidget(tabLayout);
  QVBoxLayout *optionLayout = new QVBoxLayout(options);

  optionLayout->setMargin(3);
  optionLayout->setSpacing(2);

  // setup alignment entries:
  offsetConstraint = new OffsetConstraint(options);
  offsetConstraint->setTitle( "Constrain offset to X*n+Y" );
  optionLayout->addWidget(offsetConstraint);
  // setup cursor offsets
  diffOffsets = new QCheckBox("Allow different file offsets",options);
  optionLayout->addWidget(diffOffsets);
  // add a dummy strechable widget for alignment.
  optionLayout->addStretch(1);
  connect(diffOffsets,SIGNAL(stateChanged(int)),
	  this,SLOT(setAllowDiffOffsets(int)));
  diffOffsets->setChecked(false);
  setAllowDiffOffsets(false);

  // setup toolbar 
  QToolBar *tools = new QToolBar(this);
/*  FIXME new QToolButton(QBitmap::fromData(QSize(exit_width,exit_height),exit_bits),
		  "Exit","",qApp,SLOT(quit()),tools);
  tools->addSeparator();
  new QToolButton(QBitmap(first_width,first_height,first_bits,true),
		  "First Difference","",this,SLOT(first()),tools);
  new QToolButton(QBitmap(prev_width,prev_height,prev_bits,true),
		  "Previous Difference","?",
		  this,SLOT(prev()),tools);
  new QToolButton(QBitmap(next_width,next_height,next_bits,true),
		  "Next Difference","foo",
		  this,SLOT(next()),tools);
  new QToolButton(QBitmap(last_width,last_height,last_bits,true),
		  "Last Difference","",this,SLOT(last()),tools);
*/
  // setup compare section
  vbox* view = new vbox(tabLayout);

  hexGui[0] = new HexGui(view);
  hexGui[1] = new HexGui(view);
  reader[0] = hexGui[0]->reader();
  reader[1] = hexGui[1]->reader();
  
  if( file0 )
    hexGui[0]->open(file0);
  if( file1 )
    hexGui[1]->open(file1);

  tabLayout->addTab(view,"&Compare");
  tabLayout->addTab(options,"&Options");
  addToolBar(tools);
  // request a size large enough for both hexEditors
  resize(500,500);
  setWindowTitle("Compare dialog");
}

bool CompareDlg::enabled()
{
  if( reader[0]->is_open() && reader[1]->is_open() ) {
    // check to see if modified!!!
    if( hexGui[0]->isModified() || hexGui[1]->isModified() ) {
      QMessageBox::critical(this,PROGRAM_STRING,
		  "Error, cannot use comparison"
		  " functions on modified buffers.");
      return false;
    }
    return true;
  }

  return false;
}
// public slots
void CompareDlg::setAllowDiffOffsets(int state)
{
    //   diffOffsets->setChecked(state);
}

bool CompareDlg::first()
{
  if( !enabled() )
    return false;
  
  // see Compare::last() for description of concept
  off_t offset = offsetConstraint->next(0);
  hexGui[0]->gotoOffset(offset);
  hexGui[1]->gotoOffset(offset);
  if( prev() )
    return true;
  hexGui[0]->gotoOffset(0);
  hexGui[1]->gotoOffset(0);
  return next();
}

bool CompareDlg::last()
{
  if( !enabled() ) 
    return false;

  // look at lastidx 's previous's next range
  // prev(x) is garranteed to not contain x, and next(prev(x)) will contain x
  off_t min_size_decr = min( reader[0]->size()-1,
			     reader[1]->size()-1 );
  off_t offset = offsetConstraint->prev( min_size_decr );
  hexGui[0]->gotoOffset(offset);
  hexGui[1]->gotoOffset(offset);
  if( next() )
    return true;
  hexGui[0]->gotoOffset( min_size_decr );
  hexGui[1]->gotoOffset( min_size_decr );
  return prev();
}

bool CompareDlg::prev() 
{
  if( !enabled() )
    return false;;
  // find previous diff
  off_t offset[2];
  offset[0] = hexGui[0]->offset();
  offset[1] = (diffOffsets->isChecked()) ? hexGui[1]->offset() : offset[0];
  
  if( offsetConstraint->getStride() > 1 ) {
    off_t stop[2];
    off_t pos[2];
    for( pos[0] = offset[0] = offsetConstraint->prev(offset[0]), 
	   pos[1] = offset[1] = offsetConstraint->prev(offset[1]);
	 offset[0] >= 0 && offset[1] >= 0;
	 pos[0] = offset[0] = offsetConstraint->prev(offset[0]),
	   pos[1] = offset[1] = offsetConstraint->prev(offset[1])) {
      
      // calculate stop
      stop[0] = offset[0]+offsetConstraint->getStride();
      stop[1] = offset[1]+offsetConstraint->getStride();
      
      // ignore lookups on less than a full stride length
      // this would only happen if the file size is less than the stride
      if( stop[0] > reader[0]->size() || stop[1] > reader[1]->size() )
	return false;

      while( pos[0] < stop[0] ) {
	if( (*reader[0])[pos[0]++] != (*reader[1])[pos[1]++] ) {
	  // we have a difference
	  hexGui[0]->setSelection(offset[0],stop[0]);
	  hexGui[1]->setSelection(offset[1],stop[1]);
	  hexGui[0]->gotoOffset(offset[0]);
	  hexGui[1]->gotoOffset(offset[1]);
	  return true;
	}
      }
    }
  } else {
    // just go one char at a time

    // make sure we are not past the eof of the second buffer
    if( offset[1] >= reader[1]->size() )
      return false;
    if( offset[0] <= 0 || offset[1] <= 0 )
      return false;
    do {
      offset[0]--;
      offset[1]--;
      
      if( (*reader[0])[offset[0]] != (*reader[1])[offset[1]] ) {
	hexGui[0]->gotoOffset(offset[0]);
	hexGui[1]->gotoOffset(offset[1]);
	hexGui[0]->setSelection(offset[0],offset[0]+1);
	hexGui[1]->setSelection(offset[1],offset[1]+1);
	return true;
      }
    } while( offset[0] > 0 && offset[1] > 0 );
  }
  return false;
}

bool CompareDlg::next() 
{
  // find next diff
  if( !enabled() )
    return false;

  off_t offset[2];
  offset[0] = hexGui[0]->offset();
  offset[1] = (diffOffsets->isChecked()) ? hexGui[1]->offset() : offset[0];
  
  off_t size[2];
  size[0] = reader[0]->size();
  size[1] = reader[1]->size();
  
  if( offsetConstraint->getStride() > 1 ) {
    off_t stop[2];
    off_t pos[2];
    for( pos[0] = offset[0] = offsetConstraint->next(offset[0]),
	   pos[1] = offset[1] = offsetConstraint->next(offset[1]);
	 offset[0] < size[0] && offset[1] < size[1];
	 pos[0] = offset[0] = offsetConstraint->next( offset[0] ),
	   pos[1] = offset[1] = offsetConstraint->next( offset[1] )) {
      // compare from offset to offset+stride()
      stop[0] = offset[0]+offsetConstraint->getStride();
      stop[1] = offset[1]+offsetConstraint->getStride();
      // if this will go past the bounds of the files, then the difference
      // does not matter, ignore it.
      if( stop[0] > size[0] || stop[1] > size[1] ) {
	return false;
      }
      
      while( pos[0] < stop[0] ) {
	if( (*reader[0])[pos[0]++] != (*reader[1])[pos[1]++] ) {
	  // we have a difference
	  hexGui[0]->setSelection(offset[0],stop[0]);
	  hexGui[1]->setSelection(offset[1],stop[1]);
	  hexGui[0]->gotoOffset(offset[0]);
	  hexGui[1]->gotoOffset(offset[1]);
	  return true; // bail out of fn
	}
      }
    }
  } else {
    // just go one at a time
    if( offset[0] >= size[0] || offset[1] >= size[1] ) 
      return false;
    if( offset[0] < 0 || offset[1] < 0 ) 
      return false;
    offset[0]++;
    offset[1]++;
    while( offset[0] < size[0] && offset[1] < size[1] ) {
      // don't compare the one we are on
      if( (*reader[0])[offset[0]] != (*reader[1])[offset[1]] ) {
	hexGui[0]->gotoOffset(offset[0]);
	hexGui[1]->gotoOffset(offset[1]);
	hexGui[0]->setSelection(offset[0],offset[0]+1);
	hexGui[1]->setSelection(offset[1],offset[1]+1);
	return true;
      }
      offset[0]++;
      offset[1]++;
    } 
  }
  return false;
}