Files

74 lines
2.8 KiB
C++

#pragma once
#include <array>
#include <cstdint>
#include <span>
#include <stdexcept>
#include <string>
#include <vector>
namespace gateway {
using Bytes = std::vector<uint8_t>;
struct Error : std::runtime_error { using std::runtime_error::runtime_error; };
/* Raw server rejection only; remains catchable as the existing Error. */
struct SourceAuthRejected : Error {
uint8_t phase, code;
SourceAuthRejected(uint8_t p,uint8_t c,const std::string& message):Error(message),phase(p),code(c){}
};
class Reader {
std::span<const uint8_t> b_; size_t pos_ = 0;
public:
explicit Reader(std::span<const uint8_t> b): b_(b) {}
Reader(Bytes&&) = delete;
size_t left() const { return b_.size()-pos_; }
Bytes take(size_t n);
uint64_t le(size_t n);
float f32();
std::string cstring(size_t max = 64);
// Consume the whole field, returning a display prefix on a UTF-8 boundary.
std::string utf8_cstring_prefix(size_t max_bytes);
void end() const;
};
void append(Bytes&, std::span<const uint8_t>);
void le(Bytes&, uint64_t, size_t);
void cstring(Bytes&, const std::string&);
std::string normalize(std::string);
Bytes sha1(std::span<const uint8_t>);
Bytes random_bytes(size_t);
Bytes unhex(const std::string&);
Bytes natural(Bytes);
Bytes legacy_challenge(const std::string&, uint16_t build = 7272);
Bytes world_proof(const std::string&, uint32_t client, uint32_t server, const Bytes& key);
struct Header { uint32_t opcode; size_t payload; };
Bytes frame(uint32_t opcode, std::span<const uint8_t> body, bool client);
Header header(std::span<const uint8_t>, bool client);
// Source legacy world headers use the numeric BigNumber key width. Other
// callers retain the original fixed40-byte cipher contract explicitly.
enum class HeaderKeyWidth { Fixed40, SourceBigNumber };
class Cipher {
Bytes key_; size_t index_ = 0; uint8_t previous_ = 0;
public:
explicit Cipher(Bytes key);
Cipher(Bytes key,HeaderKeyWidth width);
~Cipher();
Cipher(const Cipher&) = delete;
Cipher& operator=(const Cipher&) = delete;
void encrypt(std::span<uint8_t>);
void decrypt(std::span<uint8_t>);
};
using Guid128 = std::array<uint8_t,16>;
Bytes pack_guid(std::span<const uint8_t>);
Bytes unpack_guid(Reader&, size_t width);
struct Visual { uint32_t display; uint8_t type; };
struct Character {
uint64_t guid; std::string name;
uint8_t race, cls, gender; std::array<uint8_t,5> appearance;
uint8_t level; uint32_t zone, map; std::array<float,3> position;
uint32_t guild, flags; uint8_t first_login;
std::array<uint32_t,3> pet;
std::array<Visual,20> visuals;
};
std::vector<Character> characters(std::span<const uint8_t>);
struct SrpResult { Bytes A, key, m1, m2; };
SrpResult srp(const std::string& user, const std::string& password,
const Bytes& B, const Bytes& salt, const Bytes& secret);
}