mirror of
https://github.com/x-tools-author/x-tools.git
synced 2025-09-15 15:28:40 +08:00
chore: remove useless files
This commit is contained in:
parent
613b5823b2
commit
afe8e6c358
@ -1,298 +0,0 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright 2018-2023 Qsaker(qsaker@foxmail.com). All rights reserved.
|
||||
*
|
||||
* The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
|
||||
*
|
||||
* QtSwissArmyKnife is licensed according to the terms in the file LICENCE in the root of the source
|
||||
* code directory.
|
||||
**************************************************************************************************/
|
||||
#include <QMetaEnum>
|
||||
#ifndef SAK_IMPORT_MODULE_TESTLIB
|
||||
#include <QStandardItemModel>
|
||||
#endif
|
||||
#include "sakcommoncrcinterface.h"
|
||||
|
||||
SAKCommonCrcInterface::SAKCommonCrcInterface(QObject* parent)
|
||||
: QObject(parent)
|
||||
{}
|
||||
|
||||
QStringList SAKCommonCrcInterface::supportedParameterModels()
|
||||
{
|
||||
modelStrings.clear();
|
||||
QMetaEnum models = QMetaEnum::fromType<SAKEnumCrcModel>();
|
||||
|
||||
const char* ch = Q_NULLPTR;
|
||||
for (int i = 0; i < models.keyCount(); i++) {
|
||||
ch = models.valueToKey(i);
|
||||
if (ch) {
|
||||
modelStrings.append(QString(ch));
|
||||
}
|
||||
}
|
||||
|
||||
return modelStrings;
|
||||
}
|
||||
|
||||
uint32_t SAKCommonCrcInterface::poly(SAKCommonCrcInterface::SAKEnumCrcModel model)
|
||||
{
|
||||
uint32_t poly = 0;
|
||||
|
||||
switch (model) {
|
||||
case CRC_8:
|
||||
case CRC_8_ITU:
|
||||
case CRC_8_ROHC:
|
||||
poly = 0x07;
|
||||
break;
|
||||
case CRC_8_MAXIM:
|
||||
poly = 0x31;
|
||||
break;
|
||||
case CRC_16_IBM:
|
||||
case CRC_16_MAXIM:
|
||||
case CRC_16_USB:
|
||||
case CRC_16_MODBUS:
|
||||
poly = 0x8005;
|
||||
break;
|
||||
case CRC_16_CCITT:
|
||||
case CRC_16_XMODEM:
|
||||
case CRC_16_CCITT_FALSE:
|
||||
case CRC_16_x25:
|
||||
poly = 0x1021;
|
||||
break;
|
||||
case CRC_16_DNP:
|
||||
poly = 0x3d65;
|
||||
break;
|
||||
case CRC_32:
|
||||
case CRC_32_MPEG2:
|
||||
poly = 0x04c11db7;
|
||||
break;
|
||||
}
|
||||
|
||||
return poly;
|
||||
}
|
||||
|
||||
uint32_t SAKCommonCrcInterface::xorValue(SAKCommonCrcInterface::SAKEnumCrcModel model)
|
||||
{
|
||||
uint32_t value = 0;
|
||||
|
||||
switch (model) {
|
||||
case CRC_8:
|
||||
case CRC_8_ROHC:
|
||||
case CRC_8_MAXIM:
|
||||
value = 0x00;
|
||||
break;
|
||||
case CRC_8_ITU:
|
||||
value = 0x55;
|
||||
break;
|
||||
case CRC_16_IBM:
|
||||
|
||||
case CRC_16_MODBUS:
|
||||
case CRC_16_CCITT:
|
||||
case CRC_16_CCITT_FALSE:
|
||||
case CRC_16_XMODEM:
|
||||
value = 0x0000;
|
||||
break;
|
||||
case CRC_16_MAXIM:
|
||||
case CRC_16_USB:
|
||||
case CRC_16_x25:
|
||||
case CRC_16_DNP:
|
||||
value = 0xffff;
|
||||
break;
|
||||
case CRC_32:
|
||||
value = 0xffffffff;
|
||||
break;
|
||||
case CRC_32_MPEG2:
|
||||
value = 0x00000000;
|
||||
break;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
uint32_t SAKCommonCrcInterface::initialValue(SAKCommonCrcInterface::SAKEnumCrcModel model)
|
||||
{
|
||||
uint32_t init = 0;
|
||||
|
||||
switch (model) {
|
||||
case CRC_8:
|
||||
case CRC_8_ITU:
|
||||
case CRC_8_MAXIM:
|
||||
init = 0x00;
|
||||
break;
|
||||
case CRC_8_ROHC:
|
||||
init = 0xff;
|
||||
break;
|
||||
case CRC_16_IBM:
|
||||
case CRC_16_MAXIM:
|
||||
case CRC_16_CCITT:
|
||||
case CRC_16_XMODEM:
|
||||
case CRC_16_DNP:
|
||||
init = 0x0000;
|
||||
break;
|
||||
case CRC_16_USB:
|
||||
case CRC_16_MODBUS:
|
||||
case CRC_16_CCITT_FALSE:
|
||||
case CRC_16_x25:
|
||||
init = 0xffff;
|
||||
break;
|
||||
case CRC_32:
|
||||
case CRC_32_MPEG2:
|
||||
init = 0xffffffff;
|
||||
break;
|
||||
}
|
||||
|
||||
return init;
|
||||
}
|
||||
|
||||
QString SAKCommonCrcInterface::friendlyPoly(SAKCommonCrcInterface::SAKEnumCrcModel model)
|
||||
{
|
||||
QString formula = QString("Error: Formula not found");
|
||||
|
||||
switch (model) {
|
||||
case CRC_8:
|
||||
case CRC_8_ITU:
|
||||
case CRC_8_ROHC:
|
||||
formula = QString("x8 + x2 + x + 1");
|
||||
break;
|
||||
case CRC_8_MAXIM:
|
||||
formula = QString("x8 + x5 + x4 + 1");
|
||||
break;
|
||||
case CRC_16_IBM:
|
||||
case CRC_16_MAXIM:
|
||||
case CRC_16_USB:
|
||||
case CRC_16_MODBUS:
|
||||
case CRC_16_CCITT:
|
||||
case CRC_16_CCITT_FALSE:
|
||||
case CRC_16_x25:
|
||||
case CRC_16_XMODEM:
|
||||
formula = QString("x6 + x2 + x5 + 1");
|
||||
break;
|
||||
case CRC_16_DNP:
|
||||
formula = QString("x6+x3+x2+x1+x0+x8+x6+x5+x2+1");
|
||||
break;
|
||||
case CRC_32:
|
||||
case CRC_32_MPEG2:
|
||||
formula = QString("x32+x6+x3+x2+x6+x2+x1+x0+x8+x7+x5+x4+x2+x+1");
|
||||
break;
|
||||
}
|
||||
|
||||
return formula;
|
||||
}
|
||||
|
||||
bool SAKCommonCrcInterface::isInputReversal(SAKCommonCrcInterface::SAKEnumCrcModel model)
|
||||
{
|
||||
bool reversal = true;
|
||||
|
||||
switch (model) {
|
||||
case CRC_8:
|
||||
case CRC_8_ITU:
|
||||
case CRC_16_CCITT_FALSE:
|
||||
case CRC_16_XMODEM:
|
||||
case CRC_32_MPEG2:
|
||||
reversal = false;
|
||||
break;
|
||||
|
||||
case CRC_8_ROHC:
|
||||
case CRC_8_MAXIM:
|
||||
case CRC_16_IBM:
|
||||
case CRC_16_MAXIM:
|
||||
case CRC_16_USB:
|
||||
case CRC_16_MODBUS:
|
||||
case CRC_16_CCITT:
|
||||
case CRC_16_x25:
|
||||
case CRC_16_DNP:
|
||||
case CRC_32:
|
||||
reversal = true;
|
||||
break;
|
||||
}
|
||||
|
||||
return reversal;
|
||||
}
|
||||
|
||||
bool SAKCommonCrcInterface::isOutputReversal(SAKCommonCrcInterface::SAKEnumCrcModel model)
|
||||
{
|
||||
bool reversal = true;
|
||||
|
||||
switch (model) {
|
||||
case CRC_8:
|
||||
case CRC_8_ITU:
|
||||
case CRC_16_CCITT_FALSE:
|
||||
case CRC_16_XMODEM:
|
||||
case CRC_32_MPEG2:
|
||||
reversal = false;
|
||||
break;
|
||||
|
||||
case CRC_8_ROHC:
|
||||
case CRC_8_MAXIM:
|
||||
case CRC_16_IBM:
|
||||
case CRC_16_MAXIM:
|
||||
case CRC_16_USB:
|
||||
case CRC_16_MODBUS:
|
||||
case CRC_16_CCITT:
|
||||
case CRC_16_x25:
|
||||
case CRC_16_DNP:
|
||||
case CRC_32:
|
||||
reversal = true;
|
||||
break;
|
||||
}
|
||||
|
||||
return reversal;
|
||||
}
|
||||
|
||||
int SAKCommonCrcInterface::bitsWidth(SAKCommonCrcInterface::SAKEnumCrcModel model)
|
||||
{
|
||||
int ret = -1;
|
||||
switch (model) {
|
||||
case CRC_8:
|
||||
case CRC_8_ITU:
|
||||
case CRC_8_ROHC:
|
||||
case CRC_8_MAXIM:
|
||||
ret = 8;
|
||||
break;
|
||||
case CRC_16_IBM:
|
||||
case CRC_16_MAXIM:
|
||||
case CRC_16_USB:
|
||||
case CRC_16_MODBUS:
|
||||
case CRC_16_CCITT:
|
||||
case CRC_16_CCITT_FALSE:
|
||||
case CRC_16_x25:
|
||||
case CRC_16_XMODEM:
|
||||
case CRC_16_DNP:
|
||||
ret = 16;
|
||||
break;
|
||||
case CRC_32:
|
||||
case CRC_32_MPEG2:
|
||||
ret = 32;
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifndef SAK_IMPORT_MODULE_TESTLIB
|
||||
void SAKCommonCrcInterface::addCrcModelItemsToComboBox(QComboBox* comboBox)
|
||||
{
|
||||
if (comboBox) {
|
||||
comboBox->clear();
|
||||
QMetaEnum enums = QMetaEnum::fromType<SAKCommonCrcInterface::SAKEnumCrcModel>();
|
||||
QStandardItemModel* itemModel = new QStandardItemModel(comboBox);
|
||||
for (int i = 0; i < enums.keyCount(); i++) {
|
||||
const QString key = enums.key(i);
|
||||
// There may be a bug, I do not know whether will the itemModel
|
||||
// take ownership of the item
|
||||
// if not, a memory leak will occur after comboBox is destroyed.
|
||||
QStandardItem* item = new QStandardItem(key);
|
||||
item->setToolTip(key);
|
||||
itemModel->appendRow(item);
|
||||
}
|
||||
comboBox->setModel(itemModel);
|
||||
|
||||
// add item data
|
||||
for (int i = 0; i < comboBox->count(); i++) {
|
||||
for (int j = 0; j < enums.keyCount(); j++) {
|
||||
if (comboBox->itemText(i) == QString(enums.key(j))) {
|
||||
comboBox->setItemData(i, enums.value(j));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -1,111 +0,0 @@
|
||||
/***************************************************************************************************
|
||||
* Copyright 2018-2023 Qsaker(qsaker@foxmail.com). All rights reserved.
|
||||
*
|
||||
* The file is encoded using "utf8 with bom", it is a part of QtSwissArmyKnife project.
|
||||
*
|
||||
* QtSwissArmyKnife is licensed according to the terms in the file LICENCE in the root of the source
|
||||
* code directory.
|
||||
**************************************************************************************************/
|
||||
#ifndef SAKCOMMONCRCINTERFACE_H
|
||||
#define SAKCOMMONCRCINTERFACE_H
|
||||
|
||||
#include <QObject>
|
||||
#ifndef SAK_IMPORT_MODULE_TESTLIB
|
||||
#include <QComboBox>
|
||||
#endif
|
||||
#include <QStringList>
|
||||
|
||||
class SAKCommonCrcInterface : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum SAKEnumCrcModel {
|
||||
CRC_8,
|
||||
CRC_8_ITU,
|
||||
CRC_8_ROHC,
|
||||
CRC_8_MAXIM,
|
||||
|
||||
CRC_16_IBM,
|
||||
CRC_16_MAXIM,
|
||||
CRC_16_USB,
|
||||
CRC_16_MODBUS,
|
||||
CRC_16_CCITT,
|
||||
CRC_16_CCITT_FALSE,
|
||||
CRC_16_x25,
|
||||
CRC_16_XMODEM,
|
||||
CRC_16_DNP,
|
||||
|
||||
CRC_32,
|
||||
CRC_32_MPEG2
|
||||
};
|
||||
Q_ENUM(SAKEnumCrcModel);
|
||||
|
||||
public:
|
||||
SAKCommonCrcInterface(QObject *parent = Q_NULLPTR);
|
||||
QStringList supportedParameterModels();
|
||||
uint32_t poly(SAKCommonCrcInterface::SAKEnumCrcModel model);
|
||||
uint32_t xorValue(SAKCommonCrcInterface::SAKEnumCrcModel model);
|
||||
uint32_t initialValue(SAKCommonCrcInterface::SAKEnumCrcModel model);
|
||||
QString friendlyPoly(SAKCommonCrcInterface::SAKEnumCrcModel model);
|
||||
bool isInputReversal(SAKCommonCrcInterface::SAKEnumCrcModel model);
|
||||
bool isOutputReversal(SAKCommonCrcInterface::SAKEnumCrcModel model);
|
||||
int bitsWidth(SAKCommonCrcInterface::SAKEnumCrcModel model);
|
||||
#ifndef SAK_IMPORT_MODULE_TESTLIB
|
||||
static void addCrcModelItemsToComboBox(QComboBox *comboBox);
|
||||
#endif
|
||||
|
||||
public:
|
||||
template<typename T>
|
||||
T crcCalculate(uint8_t *input, uint64_t length, SAKCommonCrcInterface::SAKEnumCrcModel model)
|
||||
{
|
||||
T crcReg = static_cast<T>(initialValue(model));
|
||||
T rawPoly = static_cast<T>(poly(model));
|
||||
uint8_t byte = 0;
|
||||
|
||||
T temp = 1;
|
||||
while (length--) {
|
||||
byte = *(input++);
|
||||
if (isInputReversal(model)) {
|
||||
reverseInt(byte, byte);
|
||||
}
|
||||
|
||||
crcReg ^= static_cast<T>((byte << 8 * (sizeof(T) - 1)));
|
||||
for (int i = 0; i < 8; i++) {
|
||||
if (crcReg & (temp << (sizeof(T) * 8 - 1))) {
|
||||
crcReg = static_cast<T>((crcReg << 1) ^ rawPoly);
|
||||
} else {
|
||||
crcReg = static_cast<T>(crcReg << 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isOutputReversal(model)) {
|
||||
reverseInt(crcReg, crcReg);
|
||||
}
|
||||
|
||||
T crc = (crcReg ^ static_cast<T>(xorValue(model)));
|
||||
return crc;
|
||||
}
|
||||
|
||||
private:
|
||||
QStringList modelStrings;
|
||||
|
||||
private:
|
||||
template<typename T>
|
||||
bool reverseInt(const T &input, T &output)
|
||||
{
|
||||
int bitsWidth = sizeof(input) * 8;
|
||||
QString inputStr = QString("%1").arg(QString::number(input, 2), bitsWidth, '0');
|
||||
QString outputStr;
|
||||
outputStr.resize(bitsWidth);
|
||||
for (int i = 0; i < bitsWidth; i++) {
|
||||
outputStr.replace(i, 1, inputStr.at(bitsWidth - 1 - i));
|
||||
}
|
||||
|
||||
bool ok;
|
||||
output = static_cast<T>(outputStr.toULongLong(&ok, 2));
|
||||
return ok;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
Loading…
Reference in New Issue
Block a user