2011-08-28 15:17:58 +02:00
|
|
|
/*
|
2012-01-02 17:58:37 +01:00
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
2011-08-28 15:17:58 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2011-07-28 01:01:33 +02:00
|
|
|
#include "inputmessage.h"
|
2012-08-04 15:54:35 +02:00
|
|
|
#include <framework/util/crypt.h>
|
2011-05-21 21:02:37 +02:00
|
|
|
|
|
|
|
InputMessage::InputMessage()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputMessage::reset()
|
|
|
|
{
|
2012-05-15 02:04:04 +02:00
|
|
|
m_messageSize = 0;
|
2012-05-11 20:02:57 +02:00
|
|
|
m_readPos = MAX_HEADER_SIZE;
|
|
|
|
m_headerPos = MAX_HEADER_SIZE;
|
|
|
|
}
|
|
|
|
|
2012-05-15 01:13:48 +02:00
|
|
|
void InputMessage::setBuffer(const std::string& buffer)
|
2012-05-11 20:02:57 +02:00
|
|
|
{
|
2012-06-06 16:10:35 +02:00
|
|
|
int len = buffer.size();
|
|
|
|
checkWrite(MAX_HEADER_SIZE + len);
|
|
|
|
memcpy(m_buffer + MAX_HEADER_SIZE, buffer.c_str(), len);
|
2012-05-15 01:13:48 +02:00
|
|
|
m_readPos = MAX_HEADER_SIZE;
|
|
|
|
m_headerPos = MAX_HEADER_SIZE;
|
2012-06-06 16:10:35 +02:00
|
|
|
m_messageSize = len;
|
2011-05-21 21:02:37 +02:00
|
|
|
}
|
|
|
|
|
2012-05-15 02:04:04 +02:00
|
|
|
uint8 InputMessage::getU8()
|
2011-05-21 21:02:37 +02:00
|
|
|
{
|
2011-12-01 23:25:32 +01:00
|
|
|
checkRead(1);
|
2011-08-01 06:28:41 +02:00
|
|
|
uint8 v = m_buffer[m_readPos];
|
2012-05-15 02:04:04 +02:00
|
|
|
m_readPos += 1;
|
2011-08-01 06:28:41 +02:00
|
|
|
return v;
|
2011-05-21 21:02:37 +02:00
|
|
|
}
|
|
|
|
|
2012-05-15 02:04:04 +02:00
|
|
|
uint16 InputMessage::getU16()
|
2011-05-21 21:02:37 +02:00
|
|
|
{
|
2011-12-01 23:25:32 +01:00
|
|
|
checkRead(2);
|
2012-05-28 15:06:26 +02:00
|
|
|
uint16 v = stdext::readLE16(m_buffer + m_readPos);
|
2012-05-15 02:04:04 +02:00
|
|
|
m_readPos += 2;
|
2011-05-21 21:02:37 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2012-05-15 02:04:04 +02:00
|
|
|
uint32 InputMessage::getU32()
|
2011-05-21 21:02:37 +02:00
|
|
|
{
|
2011-12-01 23:25:32 +01:00
|
|
|
checkRead(4);
|
2012-05-28 15:06:26 +02:00
|
|
|
uint32 v = stdext::readLE32(m_buffer + m_readPos);
|
2012-05-15 02:04:04 +02:00
|
|
|
m_readPos += 4;
|
2011-05-21 21:02:37 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2012-05-15 02:04:04 +02:00
|
|
|
uint64 InputMessage::getU64()
|
2011-05-21 21:02:37 +02:00
|
|
|
{
|
2011-12-01 23:25:32 +01:00
|
|
|
checkRead(8);
|
2012-05-28 15:06:26 +02:00
|
|
|
uint64 v = stdext::readLE64(m_buffer + m_readPos);
|
2012-05-15 02:04:04 +02:00
|
|
|
m_readPos += 8;
|
2011-05-21 21:02:37 +02:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string InputMessage::getString()
|
|
|
|
{
|
|
|
|
uint16 stringLength = getU16();
|
2011-12-01 23:25:32 +01:00
|
|
|
checkRead(stringLength);
|
2011-05-21 21:02:37 +02:00
|
|
|
char* v = (char*)(m_buffer + m_readPos);
|
|
|
|
m_readPos += stringLength;
|
|
|
|
return std::string(v, stringLength);
|
|
|
|
}
|
|
|
|
|
2012-12-26 14:56:06 +01:00
|
|
|
double InputMessage::getDouble()
|
|
|
|
{
|
|
|
|
uint8 precision = getU8();
|
2012-12-28 12:05:45 +01:00
|
|
|
int32 v = getU32() - INT_MAX;
|
2012-12-26 14:56:06 +01:00
|
|
|
return (v / std::pow((float)10, precision));
|
|
|
|
}
|
|
|
|
|
2012-08-04 15:54:35 +02:00
|
|
|
bool InputMessage::decryptRsa(int size)
|
2012-05-14 23:36:54 +02:00
|
|
|
{
|
2012-05-29 00:04:44 +02:00
|
|
|
checkRead(size);
|
2012-08-04 15:54:35 +02:00
|
|
|
g_crypt.rsaDecrypt((unsigned char*)m_buffer + m_readPos, size);
|
2012-08-01 20:34:42 +02:00
|
|
|
return (getU8() == 0x00);
|
2012-05-14 23:36:54 +02:00
|
|
|
}
|
|
|
|
|
2012-05-15 01:13:48 +02:00
|
|
|
void InputMessage::fillBuffer(uint8 *buffer, uint16 size)
|
|
|
|
{
|
2012-06-06 16:10:35 +02:00
|
|
|
checkWrite(m_readPos + size);
|
2012-05-15 01:13:48 +02:00
|
|
|
memcpy(m_buffer + m_readPos, buffer, size);
|
|
|
|
m_messageSize += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputMessage::setHeaderSize(uint16 size)
|
|
|
|
{
|
2012-06-06 16:10:35 +02:00
|
|
|
assert(MAX_HEADER_SIZE - size >= 0);
|
2012-05-15 01:13:48 +02:00
|
|
|
m_headerPos = MAX_HEADER_SIZE - size;
|
|
|
|
m_readPos = m_headerPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InputMessage::readChecksum()
|
|
|
|
{
|
2012-06-06 16:10:35 +02:00
|
|
|
uint32 receivedCheck = getU32();
|
2012-06-04 14:38:15 +02:00
|
|
|
uint32 checksum = stdext::adler32(m_buffer + m_readPos, getUnreadSize());
|
2012-05-15 01:13:48 +02:00
|
|
|
return receivedCheck == checksum;
|
|
|
|
}
|
|
|
|
|
2011-05-21 21:02:37 +02:00
|
|
|
bool InputMessage::canRead(int bytes)
|
|
|
|
{
|
2012-05-11 20:02:57 +02:00
|
|
|
if((m_readPos - m_headerPos + bytes > m_messageSize) || (m_readPos + bytes > BUFFER_MAXSIZE))
|
2011-07-28 01:01:33 +02:00
|
|
|
return false;
|
2011-05-31 07:24:30 +02:00
|
|
|
return true;
|
2011-05-21 21:02:37 +02:00
|
|
|
}
|
2011-12-01 23:25:32 +01:00
|
|
|
void InputMessage::checkRead(int bytes)
|
|
|
|
{
|
|
|
|
if(!canRead(bytes))
|
2012-06-26 00:42:38 +02:00
|
|
|
throw stdext::exception("InputMessage eof reached");
|
2011-12-01 23:25:32 +01:00
|
|
|
}
|
|
|
|
|
2012-06-06 16:10:35 +02:00
|
|
|
void InputMessage::checkWrite(int bytes)
|
|
|
|
{
|
|
|
|
if(bytes > BUFFER_MAXSIZE)
|
2012-06-26 00:42:38 +02:00
|
|
|
throw stdext::exception("InputMessage max buffer size reached");
|
2012-06-06 16:10:35 +02:00
|
|
|
}
|