2014-11-10 00:55:38 +08:00
|
|
|
/*
|
2018-04-13 04:02:28 +08:00
|
|
|
Copyright © 2014-2018 by The qTox Project Contributors
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2014-11-10 00:55:38 +08:00
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
2014-11-10 00:55:38 +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-06-06 09:40:08 +08:00
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
2014-11-10 00:55:38 +08:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-06-06 09:40:08 +08:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2014-11-10 00:55:38 +08:00
|
|
|
|
2015-06-06 09:40:08 +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 00:55:38 +08:00
|
|
|
*/
|
|
|
|
|
2016-12-19 10:26:26 +08:00
|
|
|
#include "src/persistence/serialize.h"
|
2014-11-10 00:55:38 +08:00
|
|
|
|
2016-07-27 06:20:54 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @file serialize.cpp
|
|
|
|
* Most of functions in this file are unsafe unless otherwise specified.
|
|
|
|
* @warning Do not use them on untrusted data (e.g. check a signature first).
|
|
|
|
*/
|
2016-07-27 06:20:54 +08:00
|
|
|
|
2014-11-10 00:55:38 +08:00
|
|
|
QString dataToString(QByteArray data)
|
|
|
|
{
|
2016-08-01 03:24:48 +08:00
|
|
|
char num3;
|
|
|
|
int strlen = 0;
|
2014-11-10 00:55:38 +08:00
|
|
|
int num2 = 0;
|
2017-02-26 19:52:45 +08:00
|
|
|
int i = 0;
|
|
|
|
do {
|
2016-08-01 03:24:48 +08:00
|
|
|
num3 = data[i++];
|
|
|
|
strlen |= (num3 & 0x7f) << num2;
|
2014-11-10 00:55:38 +08:00
|
|
|
num2 += 7;
|
|
|
|
} while ((num3 & 0x80) != 0);
|
|
|
|
|
2016-08-01 03:24:48 +08:00
|
|
|
if (strlen <= 0)
|
2014-11-10 00:55:38 +08:00
|
|
|
return QString();
|
|
|
|
|
2016-08-01 03:24:48 +08:00
|
|
|
// Remove the strlen
|
2016-11-22 19:52:35 +08:00
|
|
|
data.remove(0, i);
|
2014-11-10 00:55:38 +08:00
|
|
|
data.truncate(strlen);
|
2016-08-01 03:24:48 +08:00
|
|
|
|
2014-11-10 00:55:38 +08:00
|
|
|
return QString(data);
|
|
|
|
}
|
|
|
|
|
2016-08-01 03:24:48 +08:00
|
|
|
uint64_t dataToUint64(const QByteArray& data)
|
2014-11-10 00:55:38 +08:00
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
return static_cast<uint64_t>(data[0]) | (static_cast<uint64_t>(data[1]) << 8)
|
|
|
|
| (static_cast<uint64_t>(data[2]) << 16) | (static_cast<uint64_t>(data[3]) << 24)
|
|
|
|
| (static_cast<uint64_t>(data[4]) << 32) | (static_cast<uint64_t>(data[5]) << 40)
|
|
|
|
| (static_cast<uint64_t>(data[6]) << 48) | (static_cast<uint64_t>(data[7]) << 56);
|
2016-08-01 03:24:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int dataToVInt(const QByteArray& data)
|
|
|
|
{
|
|
|
|
char num3;
|
|
|
|
int num = 0;
|
|
|
|
int num2 = 0;
|
2017-02-26 19:52:45 +08:00
|
|
|
int i = 0;
|
|
|
|
do {
|
2016-08-01 03:24:48 +08:00
|
|
|
num3 = data[i++];
|
|
|
|
num |= static_cast<int>(num3 & 0x7f) << num2;
|
|
|
|
num2 += 7;
|
|
|
|
} while ((num3 & 0x80) != 0);
|
|
|
|
return num;
|
2014-11-10 00:55:38 +08:00
|
|
|
}
|
|
|
|
|
2015-06-06 21:54:58 +08:00
|
|
|
size_t dataToVUint(const QByteArray& data)
|
|
|
|
{
|
2016-08-01 03:24:48 +08:00
|
|
|
char num3;
|
2015-06-06 21:54:58 +08:00
|
|
|
size_t num = 0;
|
|
|
|
int num2 = 0;
|
2017-02-26 19:52:45 +08:00
|
|
|
int i = 0;
|
|
|
|
do {
|
2016-08-01 03:24:48 +08:00
|
|
|
num3 = data[i++];
|
|
|
|
num |= static_cast<size_t>(num3 & 0x7f) << num2;
|
2015-06-06 21:54:58 +08:00
|
|
|
num2 += 7;
|
|
|
|
} while ((num3 & 0x80) != 0);
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2014-11-10 00:55:38 +08:00
|
|
|
unsigned getVUint32Size(QByteArray data)
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
unsigned lensize = 0;
|
2016-08-01 03:24:48 +08:00
|
|
|
|
|
|
|
char num3;
|
|
|
|
do {
|
|
|
|
num3 = data[lensize];
|
2016-10-30 06:48:03 +08:00
|
|
|
++lensize;
|
2016-08-01 03:24:48 +08:00
|
|
|
} while ((num3 & 0x80) != 0);
|
|
|
|
|
2014-11-10 00:55:38 +08:00
|
|
|
return lensize;
|
|
|
|
}
|
|
|
|
|
2016-08-01 03:24:48 +08:00
|
|
|
QByteArray vintToData(int num)
|
|
|
|
{
|
|
|
|
QByteArray data(sizeof(int), 0);
|
|
|
|
// Write the size in a Uint of variable lenght (8-32 bits)
|
2017-02-26 19:52:45 +08:00
|
|
|
int i = 0;
|
|
|
|
while (num >= 0x80) {
|
2016-08-01 03:24:48 +08:00
|
|
|
data[i] = static_cast<char>(num | 0x80);
|
2016-10-30 06:48:03 +08:00
|
|
|
++i;
|
2016-08-01 03:24:48 +08:00
|
|
|
num = num >> 7;
|
|
|
|
}
|
|
|
|
data[i] = static_cast<char>(num);
|
2017-02-26 19:52:45 +08:00
|
|
|
data.resize(i + 1);
|
2014-11-10 00:55:38 +08:00
|
|
|
return data;
|
|
|
|
}
|
2015-06-06 21:54:58 +08:00
|
|
|
|
|
|
|
QByteArray vuintToData(size_t num)
|
|
|
|
{
|
|
|
|
QByteArray data(sizeof(size_t), 0);
|
|
|
|
// Write the size in a Uint of variable lenght (8-32 bits)
|
2017-02-26 19:52:45 +08:00
|
|
|
int i = 0;
|
|
|
|
while (num >= 0x80) {
|
2016-08-01 03:24:48 +08:00
|
|
|
data[i] = static_cast<char>(num | 0x80);
|
2016-10-30 06:48:03 +08:00
|
|
|
++i;
|
2015-06-06 21:54:58 +08:00
|
|
|
num = num >> 7;
|
|
|
|
}
|
2016-08-01 03:24:48 +08:00
|
|
|
data[i] = static_cast<char>(num);
|
2017-02-26 19:52:45 +08:00
|
|
|
data.resize(i + 1);
|
2015-06-06 21:54:58 +08:00
|
|
|
return data;
|
|
|
|
}
|