generated from QuardCRT-platform/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charactercode.cpp
448 lines (432 loc) · 18.3 KB
/
charactercode.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "charactercode.h"
#include <QApplication>
#include <QTranslator>
#include <QClipboard>
#include <QMap>
#include <QMessageBox>
#include <QDialog>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QRadioButton>
#include <QDialogButtonBox>
#include <QLocale>
#include <QGroupBox>
#include <QTimer>
#include <QFile>
#include <QFileInfo>
#include <QFileDialog>
#include <QDebug>
int CharacterCode::init(QMap<QString, QString> params, QWidget *parent)
{
m_action = new QAction(tr("Character Code"), parent);
connect(m_action, &QAction::triggered, [&,parent](){
QDialog dialog(parent);
dialog.setWindowTitle(tr("Character Code"));
QVBoxLayout *layout = new QVBoxLayout(&dialog);
QGroupBox *bytesOrdergroupBox = new QGroupBox(tr("UTF-8 Bytes Order"), &dialog);
QRadioButton *msbRadio = new QRadioButton(tr("Most Significant Byte First"), &dialog);
QRadioButton *lsbRadio = new QRadioButton(tr("Least Significant Byte First"), &dialog);
if(utf8BytesOrderMsB) {
msbRadio->setChecked(true);
} else {
lsbRadio->setChecked(true);
}
QHBoxLayout *bytesOrderLayout = new QHBoxLayout;
bytesOrderLayout->addWidget(msbRadio);
bytesOrderLayout->addWidget(lsbRadio);
bytesOrdergroupBox->setLayout(bytesOrderLayout);
layout->addWidget(bytesOrdergroupBox);
QGroupBox *base64groupBox = new QGroupBox(tr("Base64 Encoding"), &dialog);
QRadioButton *base64Radio = new QRadioButton(tr("Base64"), &dialog);
QRadioButton *base64urlRadio = new QRadioButton(tr("Base64 URL"), &dialog);
if(base64) {
base64Radio->setChecked(true);
} else {
base64urlRadio->setChecked(true);
}
QHBoxLayout *base64Layout = new QHBoxLayout;
base64Layout->addWidget(base64Radio);
base64Layout->addWidget(base64urlRadio);
base64groupBox->setLayout(base64Layout);
layout->addWidget(base64groupBox);
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, &dialog);
layout->addWidget(buttonBox);
connect(buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
connect(buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);
if(dialog.exec() == QDialog::Accepted) {
utf8BytesOrderMsB = msbRadio->isChecked();
base64 = base64Radio->isChecked();
emit writeSettings("CharacterCode","BytesOrderMsB", QVariant::fromValue(utf8BytesOrderMsB));
emit writeSettings("CharacterCode","Base64", QVariant::fromValue(base64));
}
});
QTimer::singleShot(10,this,[&](){
QVariant bytesOrderMsBVariant;
emit readSettings("CharacterCode","BytesOrderMsB", bytesOrderMsBVariant);
if(bytesOrderMsBVariant.isValid()) {
utf8BytesOrderMsB = bytesOrderMsBVariant.toBool();
} else {
utf8BytesOrderMsB = true;
emit writeSettings("CharacterCode","BytesOrderMsB", QVariant::fromValue(utf8BytesOrderMsB));
}
QVariant base64Variant;
emit readSettings("CharacterCode","Base64", bytesOrderMsBVariant);
if(base64Variant.isValid()) {
base64 = base64Variant.toBool();
} else {
base64 = true;
emit writeSettings("CharacterCode","Base64", QVariant::fromValue(base64));
}
});
Q_UNUSED(params);
return 0;
}
QList<QAction *> CharacterCode::terminalContextAction(QString selectedText, QString workingDirectory, QMenu *parentMenu)
{
Q_UNUSED(workingDirectory);
if(selectedText.isEmpty()) {
return QList<QAction *>();
}
QList<QAction *> actions;
QAction *copyToASCIIAction = new QAction(tr("Copy to ASCII"), parentMenu);
actions.append(copyToASCIIAction);
connect(copyToASCIIAction, &QAction::triggered, [=](){
QString asciiText;
bool isAllASCII = true;
for(int i = 0; i < selectedText.length(); i++)
{
uint32_t c = selectedText.at(i).unicode();
if(c<=127) {
asciiText += QString("0x%1 ").arg(c, 0, 16);
} else {
isAllASCII = false;
}
}
if(!asciiText.isEmpty()) {
QApplication::clipboard()->setText(asciiText);
if(!isAllASCII) {
QMessageBox::information(parentMenu, tr("Copy to ASCII"), tr("Some characters are not ASCII"));
}
} else {
QMessageBox::information(parentMenu, tr("Copy to ASCII"), tr("No ASCII character found"));
}
});
QAction *copyToUnicodeAction = new QAction(tr("Copy to Unicode"), parentMenu);
actions.append(copyToUnicodeAction);
connect(copyToUnicodeAction, &QAction::triggered, [=](){
QString unicodeText;
for(int i = 0; i < selectedText.length(); i++)
{
uint32_t c = selectedText.at(i).unicode();
unicodeText += QString("\\u%1 ").arg(c, 0, 16);
}
if(!unicodeText.isEmpty()) {
QApplication::clipboard()->setText(unicodeText);
} else {
QMessageBox::information(parentMenu, tr("Copy to Unicode"), tr("No character found"));
}
});
QAction *copyToUTF8Action = new QAction(tr("Copy to UTF-8"), parentMenu);
actions.append(copyToUTF8Action);
connect(copyToUTF8Action, &QAction::triggered, [=](){
QString utf8Text;
for(int i = 0; i < selectedText.length(); i++)
{
QString utf8(selectedText.at(i));
QByteArray utf8Bytes = utf8.toUtf8();
QString utf8BytesStr;
if(utf8BytesOrderMsB) {
for(int j = 0; j < utf8Bytes.length(); j++) {
utf8BytesStr += QString("%1").arg((uint8_t)utf8Bytes.at(j), 0, 16);
}
} else {
for(int j = utf8Bytes.length()-1; j >= 0; j--) {
utf8BytesStr += QString("%1").arg((uint8_t)utf8Bytes.at(j), 0, 16);
}
}
utf8Text += QString("0x%1 ").arg(utf8BytesStr);
}
if(!utf8Text.isEmpty()) {
QApplication::clipboard()->setText(utf8Text);
} else {
QMessageBox::information(parentMenu, tr("Copy to UTF-8"), tr("No character found"));
}
});
QByteArray::Base64Options base64Type = base64 ? QByteArray::Base64Encoding : QByteArray::Base64UrlEncoding;
QAction *copyToBase64Action = new QAction(tr("Copy to Base64"), parentMenu);
actions.append(copyToBase64Action);
connect(copyToBase64Action, &QAction::triggered, [=](){
QApplication::clipboard()->setText(selectedText.toUtf8().toBase64(base64Type|QByteArray::KeepTrailingEquals));
});
QFileInfo fileInfo(selectedText);
if(fileInfo.exists()) {
QAction *copyToBase64FileAction = new QAction(tr("Copy to Base64 (File)"), parentMenu);
actions.append(copyToBase64FileAction);
connect(copyToBase64FileAction, &QAction::triggered, [=](){
QFile file(selectedText);
QFileInfo fileInfo(file);
if(fileInfo.size() > 8*1024) {
int ret = QMessageBox::question(parentMenu, tr("Copy to Base64 (File)"), tr("The file is too large, continue?"), QMessageBox::Yes|QMessageBox::No);
if(ret != QMessageBox::Yes) {
return;
}
}
if(file.open(QIODevice::ReadOnly)) {
QByteArray fileData = file.readAll();
file.close();
QByteArray fileBase64Data = fileData.toBase64(base64Type|QByteArray::KeepTrailingEquals);
QApplication::clipboard()->setText(fileBase64Data);
}
});
}
QByteArray base64Bytes = QByteArray::fromBase64(selectedText.toUtf8(),base64Type|QByteArray::KeepTrailingEquals|QByteArray::AbortOnBase64DecodingErrors);
if(!base64Bytes.isEmpty()) {
QAction *copyFromBase64Action = new QAction(tr("Copy from Base64"), parentMenu);
actions.append(copyFromBase64Action);
connect(copyFromBase64Action, &QAction::triggered, [=](){
QApplication::clipboard()->setText(QString::fromUtf8(base64Bytes));
});
QAction *saveFileFromBase64Action = new QAction(tr("Save File from Base64"), parentMenu);
actions.append(saveFileFromBase64Action);
connect(saveFileFromBase64Action, &QAction::triggered, [=](){
QString saveFileName = QFileDialog::getSaveFileName(parentMenu, tr("Save File from Base64"), QString(), tr("All Files (*)"));
if(!saveFileName.isEmpty()) {
QFile file(saveFileName);
if(file.open(QIODevice::WriteOnly)) {
file.write(base64Bytes);
file.close();
}
}
});
}
auto checkStringNum = [](const QString &text, bool *isNumber) -> uint64_t {
QLocale locale;
if(text.startsWith("0x")) { // hex 0xhh
QString testText = text.mid(2);
return testText.toULongLong(isNumber,16);
}else if(text.startsWith("\\u")) { // unicode \uhhhh
QString testText = text.mid(2);
return testText.toULongLong(isNumber,16);
} else if(text.startsWith("\\")) { // dec \nnn
QString testText = text.mid(1);
return testText.toULongLong(isNumber,10);
} else if(text.startsWith("u")) { // unicode uhhhh
QString testText = text.mid(1);
return testText.toULongLong(isNumber,16);
} else { // dec nnn
return text.toULongLong(isNumber,10);
if(!isNumber) { // dec(locale) nnn
return locale.toULongLong(text,isNumber);
}
}
*isNumber = false;
return 0;
};
// single character
bool isUInt64Number = false;
bool isFloatNumber = false;
bool isDoubleNumber = false;
uint64_t number = checkStringNum(selectedText, &isUInt64Number);
if(isUInt64Number) {
if(number<=127) {
QAction *showASCIIAction = new QAction(tr("Show ASCII"), parentMenu);
actions.append(showASCIIAction);
connect(showASCIIAction, &QAction::triggered, [=](){
QMessageBox::information(parentMenu, tr("Show ASCII"), QString("%1").arg(QChar((uint8_t)number).toLatin1()));
});
}
if(number<=0xffff) {
QAction *showUnicodeAction = new QAction(tr("Show Unicode"), parentMenu);
actions.append(showUnicodeAction);
connect(showUnicodeAction, &QAction::triggered, [=](){
QMessageBox::information(parentMenu, tr("Show Unicode"), QString("%1").arg(QChar((uint16_t)number)));
});
}
QAction *showUTF8Action = new QAction(tr("Show UTF-8"), parentMenu);
actions.append(showUTF8Action);
connect(showUTF8Action, &QAction::triggered, [=](){
QByteArray utf8Bytes;
if(utf8BytesOrderMsB) {
for(int i = 0; i < 8; i++) {
uint8_t byte = (uint8_t)(number>>(8*(7-i)));
if(byte == 0) {
continue;
}
utf8Bytes.append(byte);
}
} else {
for(int i = 0; i < 8; i++) {
uint8_t byte = (uint8_t)(number>>(8*i));
if(byte == 0) {
continue;
}
utf8Bytes.append(byte);
}
}
QMessageBox::information(parentMenu, tr("Show UTF-8"), QString::fromUtf8(utf8Bytes));
});
} else {
float fnumber = selectedText.toFloat(&isFloatNumber);
if(!isFloatNumber) {
QLocale locale;
fnumber = locale.toFloat(selectedText,&isFloatNumber);
}
if(isFloatNumber) {
QAction *showFloatHexAction = new QAction(tr("Show Float Hex"), parentMenu);
actions.append(showFloatHexAction);
connect(showFloatHexAction, &QAction::triggered, [=](){
uint32_t *p = (uint32_t *)&fnumber;
QMessageBox::information(parentMenu, tr("Show Float Hex"), QString("0x%1").arg(*p, 0, 16));
});
}
double dnumber = selectedText.toDouble(&isDoubleNumber);
if(!isDoubleNumber) {
QLocale locale;
dnumber = locale.toFloat(selectedText,&isDoubleNumber);
}
if(isDoubleNumber) {
QAction *showDoubleHexAction = new QAction(tr("Show Double Hex"), parentMenu);
actions.append(showDoubleHexAction);
connect(showDoubleHexAction, &QAction::triggered, [=](){
uint64_t *p = (uint64_t *)&dnumber;
QMessageBox::information(parentMenu, tr("Show Double Hex"), QString("0x%1").arg(*p, 0, 16));
});
}
}
auto tryParseNumberList = [checkStringNum](const QString &text, QList<uint64_t> &numberList) -> bool {
bool isNumber = false;
QList<QString> strList = text.split(" ",Qt::SkipEmptyParts);
if(strList.isEmpty()) {
return false;
}
if(strList.size() == 1) {
return false;
}
foreach(QString str, strList) {
uint64_t number = checkStringNum(str, &isNumber);
if(isNumber) {
numberList.append(number);
} else {
break;
}
}
return isNumber && numberList.size() == strList.size();
};
// multiple characters
if(!(isUInt64Number || isFloatNumber || isDoubleNumber)) {
QList<uint64_t> unmberList;
bool isNumber = tryParseNumberList(selectedText, unmberList);
if(!isNumber) {
unmberList.clear();
QString selectedTextUpdate = selectedText;
selectedTextUpdate.replace("0x"," 0x");
isNumber = tryParseNumberList(selectedTextUpdate, unmberList);
if(!isNumber) {
unmberList.clear();
selectedTextUpdate = selectedText;
selectedTextUpdate.replace("\\"," \\");
isNumber = tryParseNumberList(selectedTextUpdate, unmberList);
if(!isNumber) {
unmberList.clear();
selectedTextUpdate = selectedText;
selectedTextUpdate.replace("u"," u");
isNumber = tryParseNumberList(selectedTextUpdate, unmberList);
if(!isNumber) {
unmberList.clear();
selectedTextUpdate = selectedText;
selectedTextUpdate.replace("\\u"," \\u");
isNumber = tryParseNumberList(selectedTextUpdate, unmberList);
}
}
}
}
if(isNumber){
bool isAllASCII = true;
bool isAllUnicode = true;
foreach(uint64_t number, unmberList) {
if(number>127) {
isAllASCII = false;
}
if(number>0xffff) {
isAllUnicode = false;
}
}
if(isAllASCII) {
QAction *showASCIIAction = new QAction(tr("Show ASCII"), parentMenu);
actions.append(showASCIIAction);
connect(showASCIIAction, &QAction::triggered, [=](){
QString asciiText;
foreach(uint64_t number, unmberList) {
asciiText += QString("%1").arg(QChar((uint8_t)number).toLatin1());
}
QMessageBox::information(parentMenu, tr("Show ASCII"), asciiText);
});
}
if(isAllUnicode) {
QAction *showUnicodeAction = new QAction(tr("Show Unicode"), parentMenu);
actions.append(showUnicodeAction);
connect(showUnicodeAction, &QAction::triggered, [=](){
QString unicodeText;
foreach(uint64_t number, unmberList) {
unicodeText += QString("%1").arg(QChar((uint16_t)number));
}
QMessageBox::information(parentMenu, tr("Show Unicode"), unicodeText);
});
}
QAction *showUTF8Action = new QAction(tr("Show UTF-8"), parentMenu);
actions.append(showUTF8Action);
connect(showUTF8Action, &QAction::triggered, [=](){
QByteArray utf8Bytes;
foreach(uint64_t number, unmberList) {
if(number == 0) {
utf8Bytes.append('0');
continue;
}
if(utf8BytesOrderMsB) {
for(int i = 0; i < 8; i++) {
uint8_t byte = (uint8_t)(number>>(8*(7-i)));
if(byte == 0) {
continue;
}
utf8Bytes.append(byte);
}
} else {
for(int i = 0; i < 8; i++) {
uint8_t byte = (uint8_t)(number>>(8*i));
if(byte == 0) {
continue;
}
utf8Bytes.append(byte);
}
}
}
QMessageBox::information(parentMenu, tr("Show UTF-8"), QString::fromUtf8(utf8Bytes));
});
}
}
return actions;
}
void CharacterCode::setLanguage(const QLocale &language,QApplication *app) {
static QTranslator *qtTranslator = nullptr;
if(qtTranslator == nullptr) {
qtTranslator = new QTranslator(app);
} else {
app->removeTranslator(qtTranslator);
delete qtTranslator;
qtTranslator = new QTranslator(app);
}
switch(language.language()) {
case QLocale::Chinese:
if(qtTranslator->load(":/lang/charactercode_zh_CN.qm"))
app->installTranslator(qtTranslator);
break;
default:
case QLocale::English:
if(qtTranslator->load(":/lang/charactercode_en_US.qm"))
app->installTranslator(qtTranslator);
break;
}
}
void CharacterCode::retranslateUi() {
m_action->setText(tr("Character Code"));
}