-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
157 lines (116 loc) · 3.93 KB
/
main.cpp
File metadata and controls
157 lines (116 loc) · 3.93 KB
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
#include <QCoreApplication>
#include <QFile>
#include <QFileInfo>
#include <QString>
#include <QDataStream>
#include <QTextStream>
#include <QByteArray>
#include <QDebug>
#define echo QTextStream(stderr) <<
using namespace std;
/**
* @brief crc16Add
* @param data
*/
uint16_t crc_calc(char *buff, unsigned int len)
{
uint16_t crc = 0xFFFF;
uint16_t crcshr8;
uint16_t crcshl8;
uint16_t c1shl12;
uint16_t c1shl05;
uint16_t c1shl00;
uint8_t c1;
uint8_t byte;
while(len--)
{
byte = (uint8_t) *buff++;
crcshr8 = crc >> 8;
crcshl8 = crc << 8;
c1 = crcshr8 ^ byte;
c1 = c1 ^ (c1 >> 4);
c1shl05 = (uint16_t)(c1 << 5);
c1shl12 = (uint16_t)(c1 << 12);
c1shl00 = (uint16_t)(c1 << 0 );
crc = crcshl8 ^ c1shl12 ^ c1shl05 ^ c1shl00;
}
return crc;
}
int main(int argc, char *argv[])
{
echo "\nSC-Develop bin-crc-cli utility v1.0\n";
echo "Copyright (c) 2021 (MIT) Ing. Salvatore Cerami - dev.salvatore.cerami@gmail.com\n";
echo "https://github.com/sc-develop - git.sc.develop@gmail.com\n";
if (argc < 3)
{
qDebug() << "\n" << "Usage: bin-crc-cli <filename> <size>" << "\n";
exit(0);
}
QFile file(argv[1]);
int crcsize = 2; // 2 bytes crc size
qint64 destsize = QString(argv[2]).toInt(); // filled file size (crc 2 bytes included)
qint64 filesize = file.size(); // file size
qint64 fillbytes = destsize - filesize - crcsize; // byte to fill
if (fillbytes<0)
{
qDebug() << "\n\n" << "Attenzione: la dimensione del file destinazione non può essere inferione a " << QString::number(filesize + crcsize) << " Bytes" << "\n\n";
exit(0);
}
QString path = QFileInfo(file.fileName()).absolutePath() + "/";
QString destfilename = path + QFileInfo(file.fileName()).completeBaseName() + "-crc.bin";
if (QFile::exists(destfilename))
{
if (!file.remove(destfilename))
{
qDebug() << "\n\n" << "Errore: Impossible sovrascrivere il file " + destfilename << "\n\n";
exit(0);
}
}
if (!file.copy(destfilename)) // esegue la copia il file sorgente nel file destinazione (in questo momento sono uguali)
{
qDebug() << "\n\n" << "Errore: Impossible copiare il file " + file.fileName() + " in " + destfilename << "\n\n";
exit(0);
}
file.setFileName(destfilename);
if (file.open(QIODevice::Append))
{
QDataStream out(&file);
while (fillbytes--)
{
out << (uint8_t)0xFF;
}
file.close();
QString hexcrc;
if (file.open(QFile::ReadOnly))
{
QByteArray buff = file.readAll();
// calc file crc *******************************************************************************
int blen = buff.length();
/*crc16Init();
for (int n=0; n<blen; n++)
{
uint8_t c = (uint8_t) buff.at(n);
crc16Add(c);
}
uint16_t crc = crcGet();*/
uint16_t crc = crc_calc(buff.data(),blen);
hexcrc = QString("%1").arg(crc,4,16,QChar('0')).toLocal8Bit().toUpper();
file.close();
// write crc to end of file *********************************************************************
if (file.open(QIODevice::Append))
{
char crch = (char) (crc >> 8);
char crcl = (char) (crc >> 0);
file.write(&crch,1);
file.write(&crcl,1);
file.close();
}
}
qDebug() << "\n\n" << "Completato => File generato: " << QFileInfo(destfilename).fileName() << " - " << file.size() << " Bytes - CRC: " << hexcrc << "\n\n";;
}
else
{
qDebug() << "\n\n" << "Error: Impossibile aprire/creare il file: " + file.fileName() << "\n\n";
}
exit(0);
}