tibia-client/src/framework/net/inputmessage.cpp

125 lines
3.3 KiB
C++
Raw Normal View History

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"
#include "rsa.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;
m_readPos = MAX_HEADER_SIZE;
m_headerPos = MAX_HEADER_SIZE;
}
void InputMessage::setBuffer(const std::string& buffer)
{
memcpy(m_buffer + MAX_HEADER_SIZE, buffer.c_str(), buffer.size());
m_readPos = MAX_HEADER_SIZE;
m_headerPos = MAX_HEADER_SIZE;
m_messageSize = buffer.size();
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
{
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
{
checkRead(2);
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
{
checkRead(4);
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
{
checkRead(8);
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();
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);
}
void InputMessage::decryptRSA(int size, const std::string& p, const std::string& q, const std::string& d)
{
checkRead(size);
RSA::decrypt((char*)m_buffer + m_readPos, size, p.c_str(), q.c_str(), d.c_str());
}
void InputMessage::fillBuffer(uint8 *buffer, uint16 size)
{
memcpy(m_buffer + m_readPos, buffer, size);
m_messageSize += size;
}
void InputMessage::setHeaderSize(uint16 size)
{
m_headerPos = MAX_HEADER_SIZE - size;
m_readPos = m_headerPos;
}
bool InputMessage::readChecksum()
{
uint32_t receivedCheck = getU32();
uint32 checksum = stdext::generate_adler_checksum(m_buffer + m_readPos, getUnreadSize());
return receivedCheck == checksum;
}
2011-05-21 21:02:37 +02:00
bool InputMessage::canRead(int bytes)
{
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
}
void InputMessage::checkRead(int bytes)
{
if(!canRead(bytes))
throw NetworkException("InputMessage eof reached");
}