/* This file is part of Telegram Desktop, the official desktop application for the Telegram messaging service. For license and copyright information please follow this link: https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once #include "ui/images.h" #include "mtproto/auth_key.h" namespace Serialize { inline int stringSize(const QString &str) { return sizeof(quint32) + str.size() * sizeof(ushort); } inline int bytearraySize(const QByteArray &arr) { return sizeof(quint32) + arr.size(); } inline int bytesSize(base::const_byte_span bytes) { return sizeof(quint32) + bytes.size(); } struct ReadBytesVectorWrap { base::byte_vector &bytes; }; inline ReadBytesVectorWrap bytes(base::byte_vector &bytes) { 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(bytes.data()) + allocated, blockSize) != blockSize) { bytes.clear(); stream.setStatus(QDataStream::ReadPastEnd); return stream; } allocated += blockSize; } return stream; } struct WriteBytesWrap { base::const_byte_span bytes; }; inline WriteBytesWrap bytes(base::const_byte_span bytes) { 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(bytes.data()), size); } return stream; } inline QDataStream &operator<<(QDataStream &stream, ReadBytesVectorWrap data) { return stream << WriteBytesWrap { data.bytes }; } inline int dateTimeSize() { return (sizeof(qint64) + sizeof(quint32) + sizeof(qint8)); } void writeStorageImageLocation(QDataStream &stream, const StorageImageLocation &loc); StorageImageLocation readStorageImageLocation(QDataStream &stream); int storageImageLocationSize(); template inline T read(QDataStream &stream) { auto result = T(); stream >> result; return result; } template <> inline MTP::AuthKey::Data read(QDataStream &stream) { auto result = MTP::AuthKey::Data(); stream.readRawData(reinterpret_cast(result.data()), result.size()); return result; } } // namespace Serialize