1/*
2 * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#ifndef INPUTMESSAGE_H
24#define INPUTMESSAGE_H
25
26#include "declarations.h"
27#include <framework/luaengine/luaobject.h>
28
29// @bindclass
30class InputMessage : public LuaObject
31{
32public:
33 enum {
34 BUFFER_MAXSIZE = 327680,
35 MAX_HEADER_SIZE = 12
36 };
37
38 InputMessage();
39
40 void setBuffer(const std::string& buffer);
41 std::string getBuffer() { return std::string((char*)m_buffer + m_headerPos, m_messageSize); }
42 std::string getBodyBuffer() { return std::string((char*)m_buffer + MAX_HEADER_SIZE, m_messageSize - getHeaderSize()); }
43
44 void skipBytes(uint32 bytes) { m_readPos += bytes; }
45 void setReadPos(uint32 readPos) { m_readPos = readPos; }
46 uint8 getU8();
47 uint16 getU16();
48 uint32 getU32();
49 uint64 getU64();
50 std::string getString();
51 double getDouble();
52
53 uint8 peekU8() { uint8 v = getU8(); m_readPos-=1; return v; }
54 uint16 peekU16() { uint16 v = getU16(); m_readPos-=2; return v; }
55 uint32 peekU32() { uint32 v = getU32(); m_readPos-=4; return v; }
56 uint64 peekU64() { uint64 v = getU64(); m_readPos-=8; return v; }
57
58 bool decryptRsa(int size);
59
60 uint32 getHeaderPos() { return m_headerPos; }
61 uint32 getHeaderSize() { return (MAX_HEADER_SIZE - m_headerPos); }
62 int getReadSize() { return m_readPos - m_headerPos; }
63 int getReadPos() { return m_readPos; }
64 int getUnreadSize() { return m_messageSize - (m_readPos - m_headerPos); }
65 uint32 getMessageSize() { return m_messageSize; }
66
67 bool eof() { return (m_readPos - m_headerPos) >= m_messageSize; }
68
69protected:
70 void reset();
71 void fillBuffer(uint8 *buffer, uint32 size);
72
73 void setHeaderSize(uint32 size);
74 void setMessageSize(uint32 size) { m_messageSize = size; }
75
76 uint8* getReadBuffer() { return m_buffer + m_readPos; }
77 uint8* getHeaderBuffer() { return m_buffer + m_headerPos; }
78 uint8* getDataBuffer() { return m_buffer + MAX_HEADER_SIZE; }
79
80 uint32 readSize(bool bigSize) { return bigSize ? getU32() : getU16(); }
81 bool readChecksum();
82
83 void addZlibFooter();
84
85 friend class Protocol;
86
87private:
88 bool canRead(int bytes);
89 void checkRead(int bytes);
90 void checkWrite(int bytes);
91
92 uint32 m_headerPos;
93 uint32 m_readPos;
94 uint32 m_messageSize;
95 uint8 m_buffer[BUFFER_MAXSIZE];
96};
97
98#endif
99