#ifndef ArgumentDecoder_h
#define ArgumentDecoder_h
#include "ArgumentCoder.h"
#include "Attachment.h"
#include <wtf/PassOwnPtr.h>
#include <wtf/TypeTraits.h>
#include <wtf/Vector.h>
namespace CoreIPC {
class DataReference;
class ArgumentDecoder {
public:
static PassOwnPtr<ArgumentDecoder> create(const uint8_t* buffer, size_t bufferSize);
virtual ~ArgumentDecoder();
uint64_t destinationID() const { return m_destinationID; }
size_t length() const { return m_bufferEnd - m_buffer; }
bool isInvalid() const { return m_bufferPos > m_bufferEnd; }
void markInvalid() { m_bufferPos = m_bufferEnd + 1; }
bool decodeFixedLengthData(uint8_t*, size_t, unsigned alignment);
bool decodeVariableLengthByteArray(DataReference&);
bool decode(bool&);
bool decode(uint8_t&);
bool decode(uint16_t&);
bool decode(uint32_t&);
bool decode(uint64_t&);
bool decode(int32_t&);
bool decode(int64_t&);
bool decode(float&);
bool decode(double&);
template<typename T> bool decodeEnum(T& result)
{
COMPILE_ASSERT(sizeof(T) <= sizeof(uint64_t), enum_type_must_not_be_larger_than_64_bits);
uint64_t value;
if (!decode(value))
return false;
result = static_cast<T>(value);
return true;
}
template<typename T>
bool bufferIsLargeEnoughToContain(size_t numElements) const
{
COMPILE_ASSERT(WTF::IsArithmetic<T>::value, type_must_have_known_encoded_size);
if (numElements > std::numeric_limits<size_t>::max() / sizeof(T))
return false;
return bufferIsLargeEnoughToContain(__alignof(T), numElements * sizeof(T));
}
template<typename T> bool decode(T& t)
{
return ArgumentCoder<T>::decode(*this, t);
}
bool removeAttachment(Attachment&);
protected:
ArgumentDecoder(const uint8_t* buffer, size_t bufferSize, Vector<Attachment>&);
void initialize(const uint8_t* buffer, size_t bufferSize);
bool alignBufferPosition(unsigned alignment, size_t size);
bool bufferIsLargeEnoughToContain(unsigned alignment, size_t size) const;
protected:
uint64_t m_destinationID;
private:
uint8_t* m_allocatedBase;
uint8_t* m_buffer;
uint8_t* m_bufferPos;
uint8_t* m_bufferEnd;
Vector<Attachment> m_attachments;
};
}
#endif // ArgumentDecoder_h