Otclient  14/8/2020
outputmessage.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 
24 #include <framework/util/crypt.h>
25 
27 {
28  reset();
29 }
30 
32 {
33  m_writePos = MAX_HEADER_SIZE;
34  m_headerPos = MAX_HEADER_SIZE;
35  m_messageSize = 0;
36 }
37 
38 void OutputMessage::setBuffer(const std::string& buffer)
39 {
40  int len = buffer.size();
41  reset();
42  checkWrite(len);
43  memcpy((char*)(m_buffer + m_writePos), buffer.c_str(), len);
44  m_writePos += len;
45  m_messageSize += len;
46 }
47 
49 {
50  checkWrite(1);
51  m_buffer[m_writePos] = value;
52  m_writePos += 1;
53  m_messageSize += 1;
54 }
55 
57 {
58  checkWrite(2);
59  stdext::writeULE16(m_buffer + m_writePos, value);
60  m_writePos += 2;
61  m_messageSize += 2;
62 }
63 
65 {
66  checkWrite(4);
67  stdext::writeULE32(m_buffer + m_writePos, value);
68  m_writePos += 4;
69  m_messageSize += 4;
70 }
71 
73 {
74  checkWrite(8);
75  stdext::writeULE64(m_buffer + m_writePos, value);
76  m_writePos += 8;
77  m_messageSize += 8;
78 }
79 
80 void OutputMessage::addString(const std::string& buffer)
81 {
82  int len = buffer.length();
83  if(len > MAX_STRING_LENGTH)
84  throw stdext::exception(stdext::format("string length > %d", MAX_STRING_LENGTH));
85  checkWrite(len + 2);
86  addU16(len);
87  memcpy((char*)(m_buffer + m_writePos), buffer.c_str(), len);
88  m_writePos += len;
89  m_messageSize += len;
90 }
91 
93 {
94  if(bytes <= 0)
95  return;
96  checkWrite(bytes);
97  memset((void*)&m_buffer[m_writePos], byte, bytes);
98  m_writePos += bytes;
99  m_messageSize += bytes;
100 }
101 
103 {
104  int size = g_crypt.rsaGetSize();
105  if(m_messageSize < size)
106  throw stdext::exception("insufficient bytes in buffer to encrypt");
107 
108  if(!g_crypt.rsaEncrypt((unsigned char*)m_buffer + m_writePos - size, size))
109  throw stdext::exception("rsa encryption failed");
110 }
111 
113 {
114  uint32 checksum = stdext::adler32(m_buffer + m_headerPos, m_messageSize);
115  assert(m_headerPos - 4 >= 0);
116  m_headerPos -= 4;
117  stdext::writeULE32(m_buffer + m_headerPos, checksum);
118  m_messageSize += 4;
119 }
120 
122 {
123  assert(m_headerPos - 2 >= 0);
124  m_headerPos -= 2;
125  stdext::writeULE16(m_buffer + m_headerPos, m_messageSize);
126  m_messageSize += 2;
127 }
128 
129 bool OutputMessage::canWrite(int bytes)
130 {
131  if(m_writePos + bytes > BUFFER_MAXSIZE)
132  return false;
133  return true;
134 }
135 
136 void OutputMessage::checkWrite(int bytes)
137 {
138  if(!canWrite(bytes))
139  throw stdext::exception("OutputMessage max buffer size reached");
140 }
outputmessage.h
Crypt::rsaEncrypt
bool rsaEncrypt(unsigned char *msg, int size)
Definition: crypt.cpp:296
g_crypt
Crypt g_crypt
Definition: crypt.cpp:44
uint32
uint32_t uint32
Definition: types.h:35
OutputMessage::setBuffer
void setBuffer(const std::string &buffer)
Definition: outputmessage.cpp:38
Crypt::rsaGetSize
int rsaGetSize()
Definition: crypt.cpp:350
OutputMessage::writeMessageSize
void writeMessageSize()
Definition: outputmessage.cpp:121
OutputMessage::reset
void reset()
Definition: outputmessage.cpp:31
stdext::writeULE64
void writeULE64(uchar *addr, uint64_t value)
Definition: math.h:40
stdext::format
std::string format()
Definition: format.h:82
stdext::writeULE16
void writeULE16(uchar *addr, uint16_t value)
Definition: math.h:38
OutputMessage::addPaddingBytes
void addPaddingBytes(int bytes, uint8 byte=0)
Definition: outputmessage.cpp:92
crypt.h
uint16
uint16_t uint16
Definition: types.h:36
stdext::writeULE32
void writeULE32(uchar *addr, uint32_t value)
Definition: math.h:39
OutputMessage::addString
void addString(const std::string &buffer)
Definition: outputmessage.cpp:80
OutputMessage::addU64
void addU64(uint64 value)
Definition: outputmessage.cpp:72
stdext::adler32
uint32_t adler32(const uint8_t *buffer, size_t size)
Definition: math.cpp:32
uint64
uint64_t uint64
Definition: types.h:34
OutputMessage::MAX_STRING_LENGTH
@ MAX_STRING_LENGTH
Definition: outputmessage.h:35
OutputMessage::addU8
void addU8(uint8 value)
Definition: outputmessage.cpp:48
OutputMessage::writeChecksum
void writeChecksum()
Definition: outputmessage.cpp:112
OutputMessage::addU32
void addU32(uint32 value)
Definition: outputmessage.cpp:64
OutputMessage::MAX_HEADER_SIZE
@ MAX_HEADER_SIZE
Definition: outputmessage.h:36
OutputMessage::encryptRsa
void encryptRsa()
Definition: outputmessage.cpp:102
OutputMessage::BUFFER_MAXSIZE
@ BUFFER_MAXSIZE
Definition: outputmessage.h:34
OutputMessage::addU16
void addU16(uint16 value)
Definition: outputmessage.cpp:56
uint8
uint8_t uint8
Definition: types.h:37
stdext::exception
Definition: exception.h:31
OutputMessage::OutputMessage
OutputMessage()
Definition: outputmessage.cpp:26