ZaStoGram_desktop/Telegram/SourceFiles/e2e_cloud/content/protected_message_body.h
2026-08-03 17:04:44 +03:00

65 lines
1.8 KiB
C++

/*
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 "e2e_cloud/core/types.h"
#include <QtCore/QByteArray>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <type_traits>
namespace E2ECloud {
inline constexpr auto kMaximumProtectedMessageTextSize = 256 * 1024;
enum class ProtectedMessageAction : std::uint8_t {
Create = 0,
Edit = 1,
Delete = 2,
};
struct ProtectedMessageBody {
ProtectedMessageAction action = ProtectedMessageAction::Create;
std::uint64_t unixTime = 0;
ObjectId targetEventObjectId;
QByteArray textUtf8;
friend inline bool operator==(
const ProtectedMessageBody &,
const ProtectedMessageBody &) = default;
};
static_assert(std::is_standard_layout_v<ProtectedMessageBody>);
// Keep the decoded layout in the linker symbol: a stale caller must fail to
// link instead of interpreting a newly built body with old field offsets.
template <std::size_t... Layout>
struct ProtectedMessageBodyLayoutTag final {
};
using ProtectedMessageBodyLayout = ProtectedMessageBodyLayoutTag<
sizeof(ProtectedMessageBody),
alignof(ProtectedMessageBody),
offsetof(ProtectedMessageBody, action),
offsetof(ProtectedMessageBody, unixTime),
offsetof(ProtectedMessageBody, targetEventObjectId),
offsetof(ProtectedMessageBody, textUtf8)>;
class ProtectedMessageBodyCodecV1 final {
public:
[[nodiscard]] std::optional<QByteArray> encodePlaintext(
const ProtectedMessageBody &body) const;
[[nodiscard]] std::optional<ProtectedMessageBody> decodePlaintext(
const QByteArray &bytes,
ProtectedMessageBodyLayout = {}) const;
};
} // namespace E2ECloud