2016-04-09 05:57:55 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-04-09 05:57:55 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-04-09 05:57:55 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2017-06-26 17:38:16 +00:00
|
|
|
#include "mtproto/auth_key.h"
|
2016-04-09 05:57:55 +00:00
|
|
|
|
|
|
|
namespace Serialize {
|
|
|
|
|
2016-05-05 16:04:17 +00:00
|
|
|
inline int stringSize(const QString &str) {
|
|
|
|
return sizeof(quint32) + str.size() * sizeof(ushort);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int bytearraySize(const QByteArray &arr) {
|
|
|
|
return sizeof(quint32) + arr.size();
|
|
|
|
}
|
|
|
|
|
2018-03-27 12:16:00 +00:00
|
|
|
inline int bytesSize(bytes::const_span bytes) {
|
2017-04-24 12:16:38 +00:00
|
|
|
return sizeof(quint32) + bytes.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ReadBytesVectorWrap {
|
2018-03-27 12:16:00 +00:00
|
|
|
bytes::vector &bytes;
|
2017-04-24 12:16:38 +00:00
|
|
|
};
|
|
|
|
|
2018-03-27 12:16:00 +00:00
|
|
|
inline ReadBytesVectorWrap bytes(bytes::vector &bytes) {
|
2017-04-24 12:16:38 +00:00
|
|
|
return ReadBytesVectorWrap { bytes };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compatible with QDataStream &operator>>(QDataStream &, QByteArray &);
|
|
|
|
inline QDataStream &operator>>(QDataStream &stream, ReadBytesVectorWrap data) {
|
|
|
|
auto &bytes = data.bytes;
|
|
|
|
bytes.clear();
|
|
|
|
quint32 len;
|
|
|
|
stream >> len;
|
|
|
|
if (stream.status() != QDataStream::Ok || len == 0xFFFFFFFF) {
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr auto kStep = quint32(1024 * 1024);
|
|
|
|
for (auto allocated = quint32(0); allocated < len;) {
|
|
|
|
auto blockSize = qMin(kStep, len - allocated);
|
|
|
|
bytes.resize(allocated + blockSize);
|
|
|
|
if (stream.readRawData(reinterpret_cast<char*>(bytes.data()) + allocated, blockSize) != blockSize) {
|
|
|
|
bytes.clear();
|
|
|
|
stream.setStatus(QDataStream::ReadPastEnd);
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
allocated += blockSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct WriteBytesWrap {
|
2018-03-27 12:16:00 +00:00
|
|
|
bytes::const_span bytes;
|
2017-04-24 12:16:38 +00:00
|
|
|
};
|
|
|
|
|
2018-03-27 12:16:00 +00:00
|
|
|
inline WriteBytesWrap bytes(bytes::const_span bytes) {
|
2017-04-24 12:16:38 +00:00
|
|
|
return WriteBytesWrap { bytes };
|
|
|
|
}
|
|
|
|
|
|
|
|
inline QDataStream &operator<<(QDataStream &stream, WriteBytesWrap data) {
|
|
|
|
auto bytes = data.bytes;
|
|
|
|
if (bytes.empty()) {
|
|
|
|
stream << quint32(0xFFFFFFFF);
|
|
|
|
} else {
|
|
|
|
auto size = quint32(bytes.size());
|
|
|
|
stream << size;
|
|
|
|
stream.writeRawData(reinterpret_cast<const char*>(bytes.data()), size);
|
|
|
|
}
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline QDataStream &operator<<(QDataStream &stream, ReadBytesVectorWrap data) {
|
|
|
|
return stream << WriteBytesWrap { data.bytes };
|
|
|
|
}
|
|
|
|
|
2016-05-05 16:04:17 +00:00
|
|
|
inline int dateTimeSize() {
|
|
|
|
return (sizeof(qint64) + sizeof(quint32) + sizeof(qint8));
|
|
|
|
}
|
2016-04-09 05:57:55 +00:00
|
|
|
|
2019-03-22 14:19:43 +00:00
|
|
|
int storageImageLocationSize(const StorageImageLocation &location);
|
2018-07-13 16:49:46 +00:00
|
|
|
void writeStorageImageLocation(
|
|
|
|
QDataStream &stream,
|
|
|
|
const StorageImageLocation &location);
|
2019-03-22 14:19:43 +00:00
|
|
|
|
|
|
|
// NB! This method can return StorageFileLocation with Type::Generic!
|
|
|
|
// The reader should discard it or convert to one of the valid modern types.
|
|
|
|
std::optional<StorageImageLocation> readStorageImageLocation(
|
2018-07-13 16:49:46 +00:00
|
|
|
int streamAppVersion,
|
|
|
|
QDataStream &stream);
|
2016-04-09 05:57:55 +00:00
|
|
|
|
2017-02-25 16:44:02 +00:00
|
|
|
template <typename T>
|
|
|
|
inline T read(QDataStream &stream) {
|
|
|
|
auto result = T();
|
|
|
|
stream >> result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline MTP::AuthKey::Data read<MTP::AuthKey::Data>(QDataStream &stream) {
|
|
|
|
auto result = MTP::AuthKey::Data();
|
2017-04-24 12:16:38 +00:00
|
|
|
stream.readRawData(reinterpret_cast<char*>(result.data()), result.size());
|
2017-02-25 16:44:02 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-11 14:36:09 +00:00
|
|
|
uint32 peerSize(not_null<PeerData*> peer);
|
|
|
|
void writePeer(QDataStream &stream, PeerData *peer);
|
|
|
|
PeerData *readPeer(int streamAppVersion, QDataStream &stream);
|
2018-10-07 09:02:44 +00:00
|
|
|
QString peekUserPhone(int streamAppVersion, QDataStream &stream);
|
2018-09-11 14:36:09 +00:00
|
|
|
|
2016-04-09 05:57:55 +00:00
|
|
|
} // namespace Serialize
|