1/*
2 * Copyright (C) 2012 Koji Ishii <kojiishi@gmail.com>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1.  Redistributions of source code must retain the above copyright
8 *     notice, this list of conditions and the following disclaimer.
9 * 2.  Redistributions in binary form must reproduce the above copyright
10 *     notice, this list of conditions and the following disclaimer in the
11 *     documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#ifndef OpenTypeTypes_h
26#define OpenTypeTypes_h
27
28#include "platform/SharedBuffer.h"
29#include "wtf/ByteOrder.h"
30
31namespace blink {
32namespace OpenType {
33
34struct Int16 {
35    Int16(int16_t u) : v(htons(static_cast<uint16_t>(u))) { }
36    operator int16_t() const { return static_cast<int16_t>(ntohs(v)); }
37    uint16_t v; // in BigEndian
38};
39
40struct UInt16 {
41    UInt16(uint16_t u) : v(htons(u)) { }
42    operator uint16_t() const { return ntohs(v); }
43    uint16_t v; // in BigEndian
44};
45
46struct Int32 {
47    Int32(int32_t u) : v(htonl(static_cast<uint32_t>(u))) { }
48    operator int32_t() const { return static_cast<int32_t>(ntohl(v)); }
49    uint32_t v; // in BigEndian
50};
51
52struct UInt32 {
53    UInt32(uint32_t u) : v(htonl(u)) { }
54    operator uint32_t() const { return ntohl(v); }
55    uint32_t v; // in BigEndian
56};
57
58typedef UInt32 Fixed;
59typedef UInt16 Offset;
60typedef UInt16 GlyphID;
61
62// OTTag is native because it's only compared against constants, so we don't
63// do endian conversion here but make sure constants are in big-endian order.
64// Note that multi-character literal is implementation-defined in C++0x.
65typedef uint32_t Tag;
66#define OT_MAKE_TAG(ch1, ch2, ch3, ch4) ((((uint32_t)(ch4)) << 24) | (((uint32_t)(ch3)) << 16) | (((uint32_t)(ch2)) << 8) | ((uint32_t)(ch1)))
67
68template <typename T> static const T* validateTable(const RefPtr<SharedBuffer>& buffer, size_t count = 1)
69{
70    if (!buffer || buffer->size() < sizeof(T) * count)
71        return 0;
72    return reinterpret_cast<const T*>(buffer->data());
73}
74
75struct TableBase {
76protected:
77    static bool isValidEnd(const SharedBuffer& buffer, const void* position)
78    {
79        if (position < buffer.data())
80            return false;
81        size_t offset = reinterpret_cast<const char*>(position) - buffer.data();
82        return offset <= buffer.size(); // "<=" because end is included as valid
83    }
84
85    template <typename T> static const T* validatePtr(const SharedBuffer& buffer, const void* position)
86    {
87        const T* casted = reinterpret_cast<const T*>(position);
88        if (!isValidEnd(buffer, &casted[1]))
89            return 0;
90        return casted;
91    }
92
93    template <typename T> const T* validateOffset(const SharedBuffer& buffer, uint16_t offset) const
94    {
95        return validatePtr<T>(buffer, reinterpret_cast<const int8_t*>(this) + offset);
96    }
97};
98
99} // namespace OpenType
100} // namespace blink
101#endif // OpenTypeTypes_h
102