#include "stdafx.h" #include "InputOutputStream.h" #include "PacketListener.h" #include "AuthSchemePacket.h" AuthSchemePacket::AuthSchemePacket() { } AuthSchemePacket::AuthSchemePacket(const vector& schemes, const wstring& serverId) { this->schemes = schemes; this->serverId = serverId; } void AuthSchemePacket::read(DataInputStream *dis) { int count = dis->readInt(); if (count < 0 || count > 16) count = 0; // sane upper bound for auth schemes schemes.clear(); schemes.reserve(static_cast(count)); for (int i = 0; i < count; i++) { wstring scheme = readUtf(dis, 32); // Only accept known scheme names; discard unknown/empty if (scheme == L"mojang" || scheme == L"offline" || scheme == L"elyby") schemes.push_back(std::move(scheme)); } serverId = readUtf(dis, 64); } void AuthSchemePacket::write(DataOutputStream *dos) { dos->writeInt(static_cast(schemes.size())); for (auto& s : schemes) { writeUtf(s, dos); } writeUtf(serverId, dos); } void AuthSchemePacket::handle(PacketListener *listener) { listener->handleAuthScheme(shared_from_this()); } int AuthSchemePacket::getEstimatedSize() { int size = sizeof(int); for (auto& s : schemes) { size += sizeof(short) + static_cast(s.length()) * sizeof(wchar_t); } size += sizeof(short) + static_cast(serverId.length()) * sizeof(wchar_t); return size; }