Home | History | Annotate | Download | only in opentype
      1 /*
      2  * Copyright (C) 2012 Koji Ishii <kojiishi (at) 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 "core/platform/SharedBuffer.h"
     29 
     30 namespace WebCore {
     31 namespace OpenType {
     32 
     33 struct BigEndianShort {
     34     operator short() const { return (v & 0x00ff) << 8 | v >> 8; }
     35     BigEndianShort(short u) : v((u & 0x00ff) << 8 | u >> 8) { }
     36     unsigned short v;
     37 };
     38 
     39 struct BigEndianUShort {
     40     operator unsigned short() const { return (v & 0x00ff) << 8 | v >> 8; }
     41     BigEndianUShort(unsigned short u) : v((u & 0x00ff) << 8 | u >> 8) { }
     42     unsigned short v;
     43 };
     44 
     45 struct BigEndianLong {
     46     operator int() const { return (v & 0xff) << 24 | (v & 0xff00) << 8 | (v & 0xff0000) >> 8 | v >> 24; }
     47     BigEndianLong(int u) : v((u & 0xff) << 24 | (u & 0xff00) << 8 | (u & 0xff0000) >> 8 | u >> 24) { }
     48     unsigned v;
     49 };
     50 
     51 struct BigEndianULong {
     52     operator unsigned() const { return (v & 0xff) << 24 | (v & 0xff00) << 8 | (v & 0xff0000) >> 8 | v >> 24; }
     53     BigEndianULong(unsigned u) : v((u & 0xff) << 24 | (u & 0xff00) << 8 | (u & 0xff0000) >> 8 | u >> 24) { }
     54     unsigned v;
     55 };
     56 
     57 typedef BigEndianShort Int16;
     58 typedef BigEndianUShort UInt16;
     59 typedef BigEndianLong Int32;
     60 typedef BigEndianULong UInt32;
     61 
     62 typedef UInt32 Fixed;
     63 typedef UInt16 Offset;
     64 typedef UInt16 GlyphID;
     65 
     66 // OTTag is native because it's only compared against constants, so we don't
     67 // do endian conversion here but make sure constants are in big-endian order.
     68 // Note that multi-character literal is implementation-defined in C++0x.
     69 typedef uint32_t Tag;
     70 #define OT_MAKE_TAG(ch1, ch2, ch3, ch4) ((((uint32_t)(ch4)) << 24) | (((uint32_t)(ch3)) << 16) | (((uint32_t)(ch2)) << 8) | ((uint32_t)(ch1)))
     71 
     72 template <typename T> static const T* validateTable(const RefPtr<SharedBuffer>& buffer, size_t count = 1)
     73 {
     74     if (!buffer || buffer->size() < sizeof(T) * count)
     75         return 0;
     76     return reinterpret_cast<const T*>(buffer->data());
     77 }
     78 
     79 struct TableBase {
     80 protected:
     81     static bool isValidEnd(const SharedBuffer& buffer, const void* position)
     82     {
     83         if (position < buffer.data())
     84             return false;
     85         size_t offset = reinterpret_cast<const char*>(position) - buffer.data();
     86         return offset <= buffer.size(); // "<=" because end is included as valid
     87     }
     88 
     89     template <typename T> static const T* validatePtr(const SharedBuffer& buffer, const void* position)
     90     {
     91         const T* casted = reinterpret_cast<const T*>(position);
     92         if (!isValidEnd(buffer, &casted[1]))
     93             return 0;
     94         return casted;
     95     }
     96 
     97     template <typename T> const T* validateOffset(const SharedBuffer& buffer, uint16_t offset) const
     98     {
     99         return validatePtr<T>(buffer, reinterpret_cast<const int8_t*>(this) + offset);
    100     }
    101 };
    102 
    103 } // namespace OpenType
    104 } // namespace WebCore
    105 #endif // OpenTypeTypes_h
    106