Otclient  14/8/2020
inputmessage.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 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 #include "inputmessage.h"
24 #include <framework/util/crypt.h>
25 
27 {
28  reset();
29 }
30 
32 {
33  m_messageSize = 0;
34  m_readPos = MAX_HEADER_SIZE;
35  m_headerPos = MAX_HEADER_SIZE;
36 }
37 
38 void InputMessage::setBuffer(const std::string& buffer)
39 {
40  int len = buffer.size();
41  reset();
42  checkWrite(len);
43  memcpy((char*)(m_buffer + m_readPos), buffer.c_str(), len);
44  m_readPos += len;
45  m_messageSize += len;
46 }
47 
49 {
50  checkRead(1);
51  uint8 v = m_buffer[m_readPos];
52  m_readPos += 1;
53  return v;
54 }
55 
57 {
58  checkRead(2);
59  uint16 v = stdext::readULE16(m_buffer + m_readPos);
60  m_readPos += 2;
61  return v;
62 }
63 
65 {
66  checkRead(4);
67  uint32 v = stdext::readULE32(m_buffer + m_readPos);
68  m_readPos += 4;
69  return v;
70 }
71 
73 {
74  checkRead(8);
75  uint64 v = stdext::readULE64(m_buffer + m_readPos);
76  m_readPos += 8;
77  return v;
78 }
79 
81 {
82  uint16 stringLength = getU16();
83  checkRead(stringLength);
84  char* v = (char*)(m_buffer + m_readPos);
85  m_readPos += stringLength;
86  return std::string(v, stringLength);
87 }
88 
90 {
91  uint8 precision = getU8();
92  int32 v = getU32() - INT_MAX;
93  return (v / std::pow((float)10, precision));
94 }
95 
97 {
98  checkRead(size);
99  g_crypt.rsaDecrypt((unsigned char*)m_buffer + m_readPos, size);
100  return (getU8() == 0x00);
101 }
102 
104 {
105  checkWrite(m_readPos + size);
106  memcpy(m_buffer + m_readPos, buffer, size);
107  m_messageSize += size;
108 }
109 
111 {
112  assert(MAX_HEADER_SIZE - size >= 0);
113  m_headerPos = MAX_HEADER_SIZE - size;
114  m_readPos = m_headerPos;
115 }
116 
118 {
119  uint32 receivedCheck = getU32();
120  uint32 checksum = stdext::adler32(m_buffer + m_readPos, getUnreadSize());
121  return receivedCheck == checksum;
122 }
123 
124 bool InputMessage::canRead(int bytes)
125 {
126  if((m_readPos - m_headerPos + bytes > m_messageSize) || (m_readPos + bytes > BUFFER_MAXSIZE))
127  return false;
128  return true;
129 }
130 void InputMessage::checkRead(int bytes)
131 {
132  if(!canRead(bytes))
133  throw stdext::exception("InputMessage eof reached");
134 }
135 
136 void InputMessage::checkWrite(int bytes)
137 {
138  if(bytes > BUFFER_MAXSIZE)
139  throw stdext::exception("InputMessage max buffer size reached");
140 }
InputMessage::setBuffer
void setBuffer(const std::string &buffer)
Definition: inputmessage.cpp:38
stdext::readULE16
uint16_t readULE16(const uchar *addr)
Definition: math.h:34
InputMessage::MAX_HEADER_SIZE
@ MAX_HEADER_SIZE
Definition: inputmessage.h:35
InputMessage::getDouble
double getDouble()
Definition: inputmessage.cpp:89
InputMessage::setHeaderSize
void setHeaderSize(uint16 size)
Definition: inputmessage.cpp:110
InputMessage::getU16
uint16 getU16()
Definition: inputmessage.cpp:56
g_crypt
Crypt g_crypt
Definition: crypt.cpp:44
uint32
uint32_t uint32
Definition: types.h:35
InputMessage::getU8
uint8 getU8()
Definition: inputmessage.cpp:48
InputMessage::InputMessage
InputMessage()
Definition: inputmessage.cpp:26
InputMessage::getUnreadSize
int getUnreadSize()
Definition: inputmessage.h:61
crypt.h
uint16
uint16_t uint16
Definition: types.h:36
InputMessage::decryptRsa
bool decryptRsa(int size)
Definition: inputmessage.cpp:96
int32
int32_t int32
Definition: types.h:39
InputMessage::getString
std::string getString()
Definition: inputmessage.cpp:80
InputMessage::getU64
uint64 getU64()
Definition: inputmessage.cpp:72
InputMessage::getU32
uint32 getU32()
Definition: inputmessage.cpp:64
stdext::adler32
uint32_t adler32(const uint8_t *buffer, size_t size)
Definition: math.cpp:32
uint64
uint64_t uint64
Definition: types.h:34
stdext::readULE64
uint64_t readULE64(const uchar *addr)
Definition: math.h:36
Crypt::rsaDecrypt
bool rsaDecrypt(unsigned char *msg, int size)
Definition: crypt.cpp:323
InputMessage::reset
void reset()
Definition: inputmessage.cpp:31
inputmessage.h
InputMessage::fillBuffer
void fillBuffer(uint8 *buffer, uint16 size)
Definition: inputmessage.cpp:103
InputMessage::readChecksum
bool readChecksum()
Definition: inputmessage.cpp:117
stdext::readULE32
uint32_t readULE32(const uchar *addr)
Definition: math.h:35
InputMessage::BUFFER_MAXSIZE
@ BUFFER_MAXSIZE
Definition: inputmessage.h:34
uint8
uint8_t uint8
Definition: types.h:37
stdext::exception
Definition: exception.h:31