-
Notifications
You must be signed in to change notification settings - Fork 7
/
mainwindow.cpp
315 lines (300 loc) · 10.4 KB
/
mainwindow.cpp
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ids<<0; //ESN
ids<<32; //MIN1
ids<<33; //MIN2
ids<<85; //SPC
ids<<550; //IMEI
ids<<1943;//MEID
ids<<71; //banner
ui->statusbar->addWidget(&label);
label.setText(HDR_MSG.arg("Simple Qcn Parser"));
label.show();
label.setOpenExternalLinks(true);
connect(ui->btn_LoadFile,SIGNAL(clicked()),this,SLOT(UserBtns()));
connect(ui->btnWriteFile,SIGNAL(clicked()),this,SLOT(UserBtns()));
RmOptions(0);
UpdateTitle();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::RmOptions(bool show)
{
ui->ch_ESN->setVisible(show);
ui->ch_MEID->setVisible(show);
ui->ch_SPC->setVisible(show);
ui->ch_IMEI->setVisible(show);
}
bool MainWindow::ReadFile(const QString &file, QByteArray &buffer)
{
QFile _read(file);
if(_read.open(QIODevice::ReadOnly))
buffer=_read.readAll();
else{
showMsg(_read.errorString());
}
return !buffer.isEmpty();
}
bool MainWindow::WriteFile(const QString &file, const QByteArray &buffer)
{
QFile _write(file);
if(_write.open(QIODevice::WriteOnly)){
_write.write(buffer);
_write.close();
return true;
}else{
showMsg(_write.errorString());
}
return false;
}
QByteArray MainWindow::GetHandle(const uint32_t &item)
{
QByteArray qcHandle;
qcHandle=QByteArray::fromRawData(reinterpret_cast<const char*>(&item),sizeof(item));
qcHandle.prepend(QByteArray::fromHex(handle.toLatin1()));
return qcHandle;
}
bool MainWindow::ParserBuffer(QByteArray &buffer)
{
nvItems.clear();
currentLen=0;
ui->lineESN->clear();
ui->lineSPC->clear();
ui->lineIMEI->clear();
ui->lineMEID->clear();
ui->lineQcFile->clear();
stringMin.clear();
Banner.clear();
label.setText(HDR_MSG.arg("Simple Qcn Parser"));
ui->btnWriteFile->setEnabled(0);
QByteArray qcHandle,nvData;
uint32_t min1,min2;
int offset=0;
foreach(uint32_t idNV,ids){
// qcHandle=QByteArray::fromRawData(reinterpret_cast<const char*>(&idNV),sizeof(idNV));
// qcHandle.prepend(QByteArray::fromHex(handle.toLatin1()));
qcHandle=GetHandle(idNV);
qDebug() <<idNV<<qcHandle<<qcHandle.toHex();
offset=buffer.indexOf(qcHandle);
qDebug() <<offset;
if(offset>0){
nvItems.append(nvItem(idNV,offset,buffer.mid(offset+8,128)));
}
offset=0;
}
if(nvItems.isEmpty()){
showMsg("Can't Find Items ESN/MEID/IMEI/SPC");
return false;
}
foreach(nvItem item,nvItems){
QString value;
qDebug() <<item.id<<item.buffer.toHex();
if(!item.id and QC_Calculator::ReadableESN(item.buffer.toHex(),value))
ui->lineESN->setText(value);
else if(item.id==85){
ui->lineSPC->setText(item.buffer.mid(0,6));
}else if(item.id==550 and QC_Calculator::ReadableIMEI(value,item.buffer))
ui->lineIMEI->setText(value);
else if(item.id==1943 and QC_Calculator::ReadableMEID(value,reinterpret_cast<BYTE*>(item.buffer.mid(0,7).data())))
ui->lineMEID->setText(value);
else if(item.id==32)
min1=QC_Calculator::ConvertMin1((BYTE*)item.buffer.data());
else if(item.id==32)
min2=QC_Calculator::ConvertMin2((BYTE*)item.buffer.data());
else if(item.id==71)
Banner=item.buffer;
}
stringMin=QC_Calculator::DecodeMIN(min1,min2);
qDebug() <<"stringMin"<<min1<<Banner;
if(ui->lineESN->text().isEmpty() && ui->lineIMEI->text().isEmpty() &&
ui->lineMEID->text().isEmpty() && ui->lineSPC->text().isEmpty())
{
showMsg("Fail Parser QCN File!");
}else{
QString msg="Parser QCN Success";
if(stringMin.length() and Banner.length())
msg+=QString(" | MIN:%1 | BANNER:%2").arg(stringMin).arg(Banner);
else if(stringMin.length())
msg+=QString(" | MIN:%1").arg(stringMin);
else if(Banner.length())
msg+=QString(" | BANNER:%1").arg(Banner);
showMsg(msg);
ui->btnWriteFile->setEnabled(1);
currentLen=buffer.length();
}
UpdateTitle();
return currentLen;
}
bool MainWindow::PatchBuffer(QByteArray &buffer)
{
bool state=false;
rmItems=0;
qDebug() <<"bufferLen"<<buffer.length();
QString error;
QList<int> sensitiveItems=QList<int>() <<0<<85<<550<<1943;
foreach(nvItem item,nvItems){
QByteArray patch,currentItem;
QString value;
qDebug() <<item.id<<item.buffer.toHex();
if(ui->ch_SensitiveItems->isChecked()){
if(!ui->ch_ESN->isChecked() && item.id==0) // ESN
continue;
else if(!ui->ch_IMEI->isChecked() && item.id==550) // IMEI
continue;
else if(!ui->ch_MEID->isChecked() && item.id==1943) // MEID
continue;
else if(!ui->ch_SPC->isChecked() && item.id==85){ // SPC
continue;
}
if(!sensitiveItems.contains(item.id))
continue;
qDebug() <<"Remove Item"<<item.id;
patch.prepend(GetHandle((60000)+item.id));
patch.append(QByteArray().fill(0x00,(136-patch.length())));
currentItem=GetHandle(item.id);
currentItem.append(item.buffer);
if(patch==currentItem){
qDebug() <<"Skip "<<item.id;
continue;
}
qDebug() <<patch.length()<<"patch"<<patch.toHex();
qDebug() <<currentItem.length()<<"currentItem"<<currentItem.toHex();
buffer.replace(currentItem,patch);
rmItems+=1;
continue;
}
if(!item.id){
value=ui->lineESN->text().trimmed();
if(!value.isEmpty() && value.length()!=8)
{
error="Length ESN Must be 8";
break;
}
QC_Calculator::WritebleESN(value,patch);
}else if(item.id==85){
value=ui->lineSPC->text().trimmed();
if(!value.isEmpty() && value.length()!=6)
{
error="Length SPC Must be 6";
break;
}
if(value.length()==6)
patch=value.toLatin1();
}else if(item.id==550 and QC_Calculator::ReadableIMEI(value,item.buffer)){
value=ui->lineIMEI->text().trimmed();
if(!value.isEmpty() && value.length()!=15)
{
error="Length IMEI Must Be 15";
break;
}
QC_Calculator::WritableIMEI(value,patch,item.buffer.startsWith(0x08));
}else if(item.id==1943 and QC_Calculator::ReadableMEID(value,reinterpret_cast<BYTE*>(item.buffer.mid(0,7).data()))){
value=ui->lineMEID->text().trimmed();
if(!value.isEmpty() && value.length()!=14)
{
error="Length MEID Must be 14";
break;
}
QC_Calculator::WritableMEID(value,patch);
}
if(patch.length()){
patch.prepend(GetHandle(item.id));
patch.append(QByteArray().fill(0x00,(136-patch.length())));
currentItem=GetHandle(item.id);
currentItem.append(item.buffer);
if(patch==currentItem){
qDebug() <<"Skip "<<item.id;
continue;
}
qDebug() <<patch.length()<<"patch"<<patch.toHex();
qDebug() <<currentItem.length()<<"currentItem"<<currentItem.toHex();
buffer.replace(currentItem,patch);
state=true;
}
}
qDebug() <<"bufferLen"<<buffer.length();
if(!error.isEmpty())
{
showMsg(error);
return false;
}
if(!state and !rmItems){
showMsg("Fail Patch data");
return false;
}
return true;
}
void MainWindow::showMsg(QString Msg)
{
QApplication::beep();
label.setText(HDR_MSG.arg(Msg));
}
void MainWindow::UpdateTitle()
{
QString title="Simple QCN Patcher v1.2";
setWindowTitle(title);
}
void MainWindow::UserBtns()
{
setEnabled(false);
QPushButton *pButton = qobject_cast<QPushButton *>(sender());
qDebug() <<"sender"<<sender();
if(pButton==ui->btn_LoadFile){
QString file=QFileDialog::getOpenFileName(this,"Select QCN File","","Qcn File(*.qcn)");
if(!file.isEmpty()){
if(ReadFile(file,buffer)){
if(ParserBuffer(buffer))
ui->lineQcFile->setText(file);
}
}
}else if(pButton==ui->btnWriteFile){
QString file;
if(ui->ch_SensitiveItems->isChecked()){
if(!(ui->ch_ESN->isChecked() or ui->ch_SPC->isChecked() or ui->ch_IMEI->isChecked() or ui->ch_MEID->isChecked())){
showMsg("! Please Select Item(s) to remove");
setEnabled(true);
return;
}
}
file=QFileDialog::getSaveFileName(this,"Select Save File Name","","Qcn File(*.qcn)");
if(!file.isEmpty()){
if(!buffer.isEmpty()){
if(PatchBuffer(buffer))
{
if(currentLen!=buffer.length()/* and !ui->ch_SensitiveItems->isChecked()*/){
qDebug() <<currentLen<<buffer.length();
showMsg("Fail Detect Current Length File");
}else if(WriteFile(file,buffer)){
QString msg1="Success Write Patch Data";
if(rmItems){
msg1.append(QString(" && Remove Items:"+QString::number(rmItems)));
}
showMsg(msg1);
}
}
}else{
showMsg("Load QCN File First !");
}
}
}
setEnabled(true);
}
void MainWindow::on_ch_SensitiveItems_clicked(bool checked)
{
RmOptions(checked);
}