ArgumentDecoder.cpp [plain text]
#include "config.h"
#include "ArgumentDecoder.h"
#include "DataReference.h"
#include <stdio.h>
namespace IPC {
ArgumentDecoder::ArgumentDecoder(const uint8_t* buffer, size_t bufferSize)
{
initialize(buffer, bufferSize);
}
ArgumentDecoder::ArgumentDecoder(const uint8_t* buffer, size_t bufferSize, Vector<Attachment> attachments)
{
initialize(buffer, bufferSize);
m_attachments = WTF::move(attachments);
}
ArgumentDecoder::~ArgumentDecoder()
{
ASSERT(m_buffer);
free(m_buffer);
#if !USE(UNIX_DOMAIN_SOCKETS)
#else
Vector<Attachment>::iterator end = m_attachments.end();
for (Vector<Attachment>::iterator it = m_attachments.begin(); it != end; ++it)
it->dispose();
#endif
}
static inline uint8_t* roundUpToAlignment(uint8_t* ptr, unsigned alignment)
{
ASSERT(alignment && !(alignment & (alignment - 1)));
uintptr_t alignmentMask = alignment - 1;
return reinterpret_cast<uint8_t*>((reinterpret_cast<uintptr_t>(ptr) + alignmentMask) & ~alignmentMask);
}
void ArgumentDecoder::initialize(const uint8_t* buffer, size_t bufferSize)
{
m_buffer = static_cast<uint8_t*>(malloc(bufferSize));
ASSERT(!(reinterpret_cast<uintptr_t>(m_buffer) % alignof(uint64_t)));
m_bufferPos = m_buffer;
m_bufferEnd = m_buffer + bufferSize;
memcpy(m_buffer, buffer, bufferSize);
}
static inline bool alignedBufferIsLargeEnoughToContain(const uint8_t* alignedPosition, const uint8_t* bufferEnd, size_t size)
{
return bufferEnd >= alignedPosition && static_cast<size_t>(bufferEnd - alignedPosition) >= size;
}
bool ArgumentDecoder::alignBufferPosition(unsigned alignment, size_t size)
{
uint8_t* alignedPosition = roundUpToAlignment(m_bufferPos, alignment);
if (!alignedBufferIsLargeEnoughToContain(alignedPosition, m_bufferEnd, size)) {
markInvalid();
return false;
}
m_bufferPos = alignedPosition;
return true;
}
bool ArgumentDecoder::bufferIsLargeEnoughToContain(unsigned alignment, size_t size) const
{
return alignedBufferIsLargeEnoughToContain(roundUpToAlignment(m_bufferPos, alignment), m_bufferEnd, size);
}
bool ArgumentDecoder::decodeFixedLengthData(uint8_t* data, size_t size, unsigned alignment)
{
if (!alignBufferPosition(alignment, size))
return false;
memcpy(data, m_bufferPos, size);
m_bufferPos += size;
return true;
}
bool ArgumentDecoder::decodeVariableLengthByteArray(DataReference& dataReference)
{
uint64_t size;
if (!decode(size))
return false;
if (!alignBufferPosition(1, size))
return false;
uint8_t* data = m_bufferPos;
m_bufferPos += size;
dataReference = DataReference(data, size);
return true;
}
template<typename Type>
static void decodeValueFromBuffer(Type& value, uint8_t*& bufferPosition)
{
memcpy(&value, bufferPosition, sizeof(value));
bufferPosition += sizeof(Type);
}
bool ArgumentDecoder::decode(bool& result)
{
if (!alignBufferPosition(sizeof(result), sizeof(result)))
return false;
decodeValueFromBuffer(result, m_bufferPos);
return true;
}
bool ArgumentDecoder::decode(uint8_t& result)
{
if (!alignBufferPosition(sizeof(result), sizeof(result)))
return false;
decodeValueFromBuffer(result, m_bufferPos);
return true;
}
bool ArgumentDecoder::decode(uint16_t& result)
{
if (!alignBufferPosition(sizeof(result), sizeof(result)))
return false;
decodeValueFromBuffer(result, m_bufferPos);
return true;
}
bool ArgumentDecoder::decode(uint32_t& result)
{
if (!alignBufferPosition(sizeof(result), sizeof(result)))
return false;
decodeValueFromBuffer(result, m_bufferPos);
return true;
}
bool ArgumentDecoder::decode(uint64_t& result)
{
if (!alignBufferPosition(sizeof(result), sizeof(result)))
return false;
decodeValueFromBuffer(result, m_bufferPos);
return true;
}
bool ArgumentDecoder::decode(int32_t& result)
{
if (!alignBufferPosition(sizeof(result), sizeof(result)))
return false;
decodeValueFromBuffer(result, m_bufferPos);
return true;
}
bool ArgumentDecoder::decode(int64_t& result)
{
if (!alignBufferPosition(sizeof(result), sizeof(result)))
return false;
decodeValueFromBuffer(result, m_bufferPos);
return true;
}
bool ArgumentDecoder::decode(float& result)
{
if (!alignBufferPosition(sizeof(result), sizeof(result)))
return false;
decodeValueFromBuffer(result, m_bufferPos);
return true;
}
bool ArgumentDecoder::decode(double& result)
{
if (!alignBufferPosition(sizeof(result), sizeof(result)))
return false;
decodeValueFromBuffer(result, m_bufferPos);
return true;
}
bool ArgumentDecoder::removeAttachment(Attachment& attachment)
{
if (m_attachments.isEmpty())
return false;
attachment = m_attachments.last();
m_attachments.removeLast();
return true;
}
}