#ifndef OpenTypeTypes_h
#define OpenTypeTypes_h
#include "platform/SharedBuffer.h"
#include "wtf/ByteOrder.h"
namespace WebCore {
namespace OpenType {
struct Int16 {
Int16(int16_t u) : v(htons(static_cast<uint16_t>(u))) { }
operator int16_t() const { return static_cast<int16_t>(ntohs(v)); }
uint16_t v;
};
struct UInt16 {
UInt16(uint16_t u) : v(htons(u)) { }
operator uint16_t() const { return ntohs(v); }
uint16_t v;
};
struct Int32 {
Int32(int32_t u) : v(htonl(static_cast<uint32_t>(u))) { }
operator int32_t() const { return static_cast<int32_t>(ntohl(v)); }
uint32_t v;
};
struct UInt32 {
UInt32(uint32_t u) : v(htonl(u)) { }
operator uint32_t() const { return ntohl(v); }
uint32_t v;
};
typedef UInt32 Fixed;
typedef UInt16 Offset;
typedef UInt16 GlyphID;
typedef uint32_t Tag;
#define OT_MAKE_TAG(ch1, ch2, ch3, ch4) ((((uint32_t)(ch4)) << 24) | (((uint32_t)(ch3)) << 16) | (((uint32_t)(ch2)) << 8) | ((uint32_t)(ch1)))
template <typename T> static const T* validateTable(const RefPtr<SharedBuffer>& buffer, size_t count = 1)
{
if (!buffer || buffer->size() < sizeof(T) * count)
return 0;
return reinterpret_cast<const T*>(buffer->data());
}
struct TableBase {
protected:
static bool isValidEnd(const SharedBuffer& buffer, const void* position)
{
if (position < buffer.data())
return false;
size_t offset = reinterpret_cast<const char*>(position) - buffer.data();
return offset <= buffer.size();
}
template <typename T> static const T* validatePtr(const SharedBuffer& buffer, const void* position)
{
const T* casted = reinterpret_cast<const T*>(position);
if (!isValidEnd(buffer, &casted[1]))
return 0;
return casted;
}
template <typename T> const T* validateOffset(const SharedBuffer& buffer, uint16_t offset) const
{
return validatePtr<T>(buffer, reinterpret_cast<const int8_t*>(this) + offset);
}
};
}
}
#endif