#ifndef OpenTypeTypes_h
#define OpenTypeTypes_h
#include "SharedBuffer.h"
namespace WebCore {
namespace OpenType {
struct BigEndianShort {
operator short() const { return (v & 0x00ff) << 8 | v >> 8; }
BigEndianShort(short u) : v((u & 0x00ff) << 8 | u >> 8) { }
unsigned short v;
};
struct BigEndianUShort {
operator unsigned short() const { return (v & 0x00ff) << 8 | v >> 8; }
BigEndianUShort(unsigned short u) : v((u & 0x00ff) << 8 | u >> 8) { }
unsigned short v;
};
struct BigEndianLong {
operator int() const { return (v & 0xff) << 24 | (v & 0xff00) << 8 | (v & 0xff0000) >> 8 | v >> 24; }
BigEndianLong(int u) : v((u & 0xff) << 24 | (u & 0xff00) << 8 | (u & 0xff0000) >> 8 | u >> 24) { }
unsigned v;
};
struct BigEndianULong {
operator unsigned() const { return (v & 0xff) << 24 | (v & 0xff00) << 8 | (v & 0xff0000) >> 8 | v >> 24; }
BigEndianULong(unsigned u) : v((u & 0xff) << 24 | (u & 0xff00) << 8 | (u & 0xff0000) >> 8 | u >> 24) { }
unsigned v;
};
typedef BigEndianShort Int16;
typedef BigEndianUShort UInt16;
typedef BigEndianLong Int32;
typedef BigEndianULong UInt32;
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 // OpenTypeTypes_h