2014-11-10 06:24:23 +08:00
|
|
|
/*
|
2016-11-28 21:33:38 +08:00
|
|
|
Copyright © 2014 by The qTox Project Contributors
|
2014-11-10 06:24:23 +08:00
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
2015-09-19 16:15:04 +08:00
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
2014-11-10 06:24:23 +08:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2015-09-19 16:15:04 +08:00
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
2014-11-10 06:24:23 +08:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-09-19 16:15:04 +08:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2014-11-10 06:24:23 +08:00
|
|
|
|
2015-09-19 16:15:04 +08:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>
|
2014-11-10 06:24:23 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "serialize.h"
|
|
|
|
|
|
|
|
QByteArray doubleToData(double num)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
char tab[8];
|
|
|
|
double n;
|
|
|
|
} castUnion;
|
2017-02-26 19:52:45 +08:00
|
|
|
// char n[8];
|
2014-11-10 06:24:23 +08:00
|
|
|
//*((double*) n) = num;
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
castUnion.n = num;
|
|
|
|
return QByteArray(castUnion.tab, 8);
|
2014-11-10 06:24:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray floatToData(float num)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
char tab[4];
|
|
|
|
float n;
|
|
|
|
} castUnion;
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
castUnion.n = num;
|
|
|
|
return QByteArray(castUnion.tab, 4);
|
2014-11-10 06:24:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float dataToFloat(QByteArray data)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
char tab[4];
|
|
|
|
float n;
|
|
|
|
} castUnion;
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
castUnion.tab[0] = data.data()[0];
|
|
|
|
castUnion.tab[1] = data.data()[1];
|
|
|
|
castUnion.tab[2] = data.data()[2];
|
|
|
|
castUnion.tab[3] = data.data()[3];
|
2014-11-10 06:24:23 +08:00
|
|
|
return castUnion.n;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts a string into PNet string data
|
|
|
|
QByteArray stringToData(QString str)
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
QByteArray data(4, 0);
|
2014-11-10 06:24:23 +08:00
|
|
|
// Write the size in a Uint of variable lenght (8-32 bits)
|
2017-02-26 19:52:45 +08:00
|
|
|
int i = 0;
|
2014-11-10 06:24:23 +08:00
|
|
|
uint num1 = (uint)str.toUtf8().size();
|
2017-02-26 19:52:45 +08:00
|
|
|
while (num1 >= 0x80) {
|
|
|
|
data[i] = (unsigned char)(num1 | 0x80);
|
|
|
|
i++;
|
2014-11-10 06:24:23 +08:00
|
|
|
num1 = num1 >> 7;
|
|
|
|
}
|
2017-02-26 19:52:45 +08:00
|
|
|
data[i] = num1;
|
|
|
|
data.resize(i + 1);
|
|
|
|
data += str.toUtf8();
|
2014-11-10 06:24:23 +08:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString dataToString(QByteArray data)
|
|
|
|
{
|
|
|
|
// Variable UInt32
|
|
|
|
unsigned char num3;
|
|
|
|
int num = 0;
|
|
|
|
int num2 = 0;
|
2017-02-26 19:52:45 +08:00
|
|
|
int i = 0;
|
|
|
|
do {
|
|
|
|
num3 = data[i];
|
|
|
|
i++;
|
2014-11-10 06:24:23 +08:00
|
|
|
num |= (num3 & 0x7f) << num2;
|
|
|
|
num2 += 7;
|
|
|
|
} while ((num3 & 0x80) != 0);
|
2017-02-26 19:52:45 +08:00
|
|
|
unsigned int strlen = (uint)num;
|
2014-11-10 06:24:23 +08:00
|
|
|
|
|
|
|
if (!strlen)
|
|
|
|
return QString();
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
data = data.right(data.size() - i); // Remove the strlen
|
2014-11-10 06:24:23 +08:00
|
|
|
data.truncate(strlen);
|
|
|
|
return QString(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
float dataToRangedSingle(float min, float max, int numberOfBits, QByteArray data)
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
uint endvalue = 0;
|
|
|
|
uint value = 0;
|
|
|
|
if (numberOfBits <= 8) {
|
2014-11-10 06:24:23 +08:00
|
|
|
endvalue = (uchar)data[0];
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
value = (uchar)data[0];
|
|
|
|
numberOfBits -= 8;
|
2017-02-26 19:52:45 +08:00
|
|
|
if (numberOfBits <= 8) {
|
|
|
|
endvalue = (value | ((uint)((uchar)data[1]) << 8));
|
2014-11-10 06:24:23 +08:00
|
|
|
goto done;
|
|
|
|
}
|
2017-02-26 19:52:45 +08:00
|
|
|
value |= (uint)(((uchar)data[1]) << 8);
|
2014-11-10 06:24:23 +08:00
|
|
|
numberOfBits -= 8;
|
2017-02-26 19:52:45 +08:00
|
|
|
if (numberOfBits <= 8) {
|
|
|
|
uint num2 = (uint)(((uchar)data[2]) << 0x10);
|
2014-11-10 06:24:23 +08:00
|
|
|
endvalue = (value | num2);
|
|
|
|
goto done;
|
|
|
|
}
|
2017-02-26 19:52:45 +08:00
|
|
|
value |= (uint)(((uchar)data[2]) << 0x10);
|
2014-11-10 06:24:23 +08:00
|
|
|
numberOfBits -= 8;
|
2017-02-26 19:52:45 +08:00
|
|
|
endvalue = (value | ((uint)(((uchar)data[3]) << 0x18)));
|
2014-11-10 06:24:23 +08:00
|
|
|
goto done;
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
done:
|
2014-11-10 06:24:23 +08:00
|
|
|
|
|
|
|
float num = max - min;
|
2017-02-26 19:52:45 +08:00
|
|
|
int num2 = (((int)1) << numberOfBits) - 1;
|
2014-11-10 06:24:23 +08:00
|
|
|
float num3 = endvalue;
|
2017-02-26 19:52:45 +08:00
|
|
|
float num4 = num3 / ((float)num2);
|
2014-11-10 06:24:23 +08:00
|
|
|
return (min + (num4 * num));
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray rangedSingleToData(float value, float min, float max, int numberOfBits)
|
|
|
|
{
|
|
|
|
QByteArray data;
|
|
|
|
float num = max - min;
|
|
|
|
float num2 = (value - min) / num;
|
2017-02-26 19:52:45 +08:00
|
|
|
int num3 = (((int)1) << numberOfBits) - 1;
|
2014-11-10 06:24:23 +08:00
|
|
|
uint source = num3 * num2;
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
if (numberOfBits <= 8) {
|
2014-11-10 06:24:23 +08:00
|
|
|
data += (unsigned char)source;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
data += (unsigned char)source;
|
|
|
|
numberOfBits -= 8;
|
2017-02-26 19:52:45 +08:00
|
|
|
if (numberOfBits <= 8) {
|
|
|
|
data += (unsigned char)(source >> 8);
|
2014-11-10 06:24:23 +08:00
|
|
|
return data;
|
|
|
|
}
|
2017-02-26 19:52:45 +08:00
|
|
|
data += (unsigned char)(source >> 8);
|
2014-11-10 06:24:23 +08:00
|
|
|
numberOfBits -= 8;
|
2017-02-26 19:52:45 +08:00
|
|
|
if (numberOfBits <= 8) {
|
|
|
|
data += (unsigned char)(source >> 16);
|
2014-11-10 06:24:23 +08:00
|
|
|
return data;
|
|
|
|
}
|
2017-02-26 19:52:45 +08:00
|
|
|
data += (unsigned char)(source >> 16);
|
|
|
|
data += (unsigned char)(source >> 24);
|
2014-11-10 06:24:23 +08:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t dataToUint8(QByteArray data)
|
|
|
|
{
|
|
|
|
return (uint8_t)data[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t dataToUint16(QByteArray data)
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
return ((uint16_t)(uint8_t)data[0]) + (((uint16_t)(uint8_t)data[1]) << 8);
|
2014-11-10 06:24:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t dataToUint32(QByteArray data)
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
return ((uint32_t)(uint8_t)data[0]) + (((uint32_t)(uint8_t)data[1]) << 8)
|
|
|
|
+ (((uint32_t)(uint8_t)data[2]) << 16) + (((uint32_t)(uint8_t)data[3]) << 24);
|
2014-11-10 06:24:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t dataToUint64(QByteArray data)
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
return ((uint64_t)(uint8_t)data[0]) + (((uint64_t)(uint8_t)data[1]) << 8)
|
|
|
|
+ (((uint64_t)(uint8_t)data[2]) << 16) + (((uint64_t)(uint8_t)data[3]) << 24)
|
|
|
|
+ (((uint64_t)(uint8_t)data[4]) << 32) + (((uint64_t)(uint8_t)data[5]) << 40)
|
|
|
|
+ (((uint64_t)(uint8_t)data[6]) << 48) + (((uint64_t)(uint8_t)data[7]) << 56);
|
2014-11-10 06:24:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned getVUint32Size(QByteArray data)
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
unsigned lensize = 0;
|
2014-11-10 06:24:23 +08:00
|
|
|
{
|
|
|
|
unsigned char num3;
|
|
|
|
do {
|
|
|
|
num3 = data[lensize];
|
|
|
|
lensize++;
|
|
|
|
} while ((num3 & 0x80) != 0);
|
|
|
|
}
|
|
|
|
return lensize;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray uint8ToData(uint8_t num)
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
QByteArray data(1, 0);
|
2014-11-10 06:24:23 +08:00
|
|
|
data[0] = (uint8_t)num;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray uint16ToData(uint16_t num)
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
QByteArray data(2, 0);
|
2014-11-10 06:24:23 +08:00
|
|
|
data[0] = (uint8_t)(num & 0xFF);
|
2017-02-26 19:52:45 +08:00
|
|
|
data[1] = (uint8_t)((num >> 8) & 0xFF);
|
2014-11-10 06:24:23 +08:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray uint32ToData(uint32_t num)
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
QByteArray data(4, 0);
|
2014-11-10 06:24:23 +08:00
|
|
|
data[0] = (uint8_t)(num & 0xFF);
|
2017-02-26 19:52:45 +08:00
|
|
|
data[1] = (uint8_t)((num >> 8) & 0xFF);
|
|
|
|
data[2] = (uint8_t)((num >> 16) & 0xFF);
|
|
|
|
data[3] = (uint8_t)((num >> 24) & 0xFF);
|
2014-11-10 06:24:23 +08:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray uint64ToData(uint64_t num)
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
QByteArray data(8, 0);
|
2014-11-10 06:24:23 +08:00
|
|
|
data[0] = (uint8_t)(num & 0xFF);
|
2017-02-26 19:52:45 +08:00
|
|
|
data[1] = (uint8_t)((num >> 8) & 0xFF);
|
|
|
|
data[2] = (uint8_t)((num >> 16) & 0xFF);
|
|
|
|
data[3] = (uint8_t)((num >> 24) & 0xFF);
|
|
|
|
data[4] = (uint8_t)((num >> 32) & 0xFF);
|
|
|
|
data[5] = (uint8_t)((num >> 40) & 0xFF);
|
|
|
|
data[6] = (uint8_t)((num >> 48) & 0xFF);
|
|
|
|
data[7] = (uint8_t)((num >> 56) & 0xFF);
|
2014-11-10 06:24:23 +08:00
|
|
|
return data;
|
|
|
|
}
|