gfx/ots/include/opentype-sanitiser.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
     2 // Use of this source code is governed by a BSD-style license that can be
     3 // found in the LICENSE file.
     5 #ifndef OPENTYPE_SANITISER_H_
     6 #define OPENTYPE_SANITISER_H_
     8 #if defined(_WIN32) || defined(__CYGWIN__)
     9   #define OTS_DLL_IMPORT __declspec(dllimport)
    10   #define OTS_DLL_EXPORT __declspec(dllexport)
    11 #else
    12   #if __GNUC__ >= 4
    13     #define OTS_DLL_IMPORT __attribute__((visibility ("default")))
    14     #define OTS_DLL_EXPORT __attribute__((visibility ("default")))
    15   #endif
    16 #endif
    18 #ifdef OTS_DLL
    19   #ifdef OTS_DLL_EXPORTS
    20     #define OTS_API OTS_DLL_EXPORT
    21   #else
    22     #define OTS_API OTS_DLL_IMPORT
    23   #endif
    24 #else
    25   #define OTS_API
    26 #endif
    28 #if defined(_WIN32)
    29 #include <stdlib.h>
    30 typedef signed char int8_t;
    31 typedef unsigned char uint8_t;
    32 typedef short int16_t;
    33 typedef unsigned short uint16_t;
    34 typedef int int32_t;
    35 typedef unsigned int uint32_t;
    36 typedef __int64 int64_t;
    37 typedef unsigned __int64 uint64_t;
    38 #define ntohl(x) _byteswap_ulong (x)
    39 #define ntohs(x) _byteswap_ushort (x)
    40 #define htonl(x) _byteswap_ulong (x)
    41 #define htons(x) _byteswap_ushort (x)
    42 #else
    43 #include <arpa/inet.h>
    44 #include <stdint.h>
    45 #endif
    47 #include <algorithm>
    48 #include <cassert>
    49 #include <cstddef>
    50 #include <cstring>
    52 namespace ots {
    54 // -----------------------------------------------------------------------------
    55 // This is an interface for an abstract stream class which is used for writing
    56 // the serialised results out.
    57 // -----------------------------------------------------------------------------
    58 class OTSStream {
    59  public:
    60   OTSStream() {
    61     ResetChecksum();
    62   }
    64   virtual ~OTSStream() {}
    66   // This should be implemented to perform the actual write.
    67   virtual bool WriteRaw(const void *data, size_t length) = 0;
    69   bool Write(const void *data, size_t length) {
    70     if (!length) return false;
    72     const size_t orig_length = length;
    73     size_t offset = 0;
    74     if (chksum_buffer_offset_) {
    75       const size_t l =
    76         std::min(length, static_cast<size_t>(4) - chksum_buffer_offset_);
    77       std::memcpy(chksum_buffer_ + chksum_buffer_offset_, data, l);
    78       chksum_buffer_offset_ += l;
    79       offset += l;
    80       length -= l;
    81     }
    83     if (chksum_buffer_offset_ == 4) {
    84       uint32_t tmp;
    85       std::memcpy(&tmp, chksum_buffer_, 4);
    86       chksum_ += ntohl(tmp);
    87       chksum_buffer_offset_ = 0;
    88     }
    90     while (length >= 4) {
    91       uint32_t tmp;
    92       std::memcpy(&tmp, reinterpret_cast<const uint8_t *>(data) + offset,
    93         sizeof(uint32_t));
    94       chksum_ += ntohl(tmp);
    95       length -= 4;
    96       offset += 4;
    97     }
    99     if (length) {
   100       if (chksum_buffer_offset_ != 0) return false;  // not reached
   101       if (length > 4) return false;  // not reached
   102       std::memcpy(chksum_buffer_,
   103              reinterpret_cast<const uint8_t*>(data) + offset, length);
   104       chksum_buffer_offset_ = length;
   105     }
   107     return WriteRaw(data, orig_length);
   108   }
   110   virtual bool Seek(off_t position) = 0;
   111   virtual off_t Tell() const = 0;
   113   virtual bool Pad(size_t bytes) {
   114     static const uint32_t kZero = 0;
   115     while (bytes >= 4) {
   116       if (!WriteTag(kZero)) return false;
   117       bytes -= 4;
   118     }
   119     while (bytes) {
   120       static const uint8_t kZerob = 0;
   121       if (!Write(&kZerob, 1)) return false;
   122       bytes--;
   123     }
   124     return true;
   125   }
   127   bool WriteU8(uint8_t v) {
   128     return Write(&v, sizeof(v));
   129   }
   131   bool WriteU16(uint16_t v) {
   132     v = htons(v);
   133     return Write(&v, sizeof(v));
   134   }
   136   bool WriteS16(int16_t v) {
   137     v = htons(v);
   138     return Write(&v, sizeof(v));
   139   }
   141   bool WriteU24(uint32_t v) {
   142     v = htonl(v);
   143     return Write(reinterpret_cast<uint8_t*>(&v)+1, 3);
   144   }
   146   bool WriteU32(uint32_t v) {
   147     v = htonl(v);
   148     return Write(&v, sizeof(v));
   149   }
   151   bool WriteS32(int32_t v) {
   152     v = htonl(v);
   153     return Write(&v, sizeof(v));
   154   }
   156   bool WriteR64(uint64_t v) {
   157     return Write(&v, sizeof(v));
   158   }
   160   bool WriteTag(uint32_t v) {
   161     return Write(&v, sizeof(v));
   162   }
   164   void ResetChecksum() {
   165     chksum_ = 0;
   166     chksum_buffer_offset_ = 0;
   167   }
   169   uint32_t chksum() const {
   170     assert(chksum_buffer_offset_ == 0);
   171     return chksum_;
   172   }
   174   struct ChecksumState {
   175     uint32_t chksum;
   176     uint8_t chksum_buffer[4];
   177     unsigned chksum_buffer_offset;
   178   };
   180   ChecksumState SaveChecksumState() const {
   181     ChecksumState s;
   182     s.chksum = chksum_;
   183     s.chksum_buffer_offset = chksum_buffer_offset_;
   184     std::memcpy(s.chksum_buffer, chksum_buffer_, 4);
   186     return s;
   187   }
   189   void RestoreChecksum(const ChecksumState &s) {
   190     assert(chksum_buffer_offset_ == 0);
   191     chksum_ += s.chksum;
   192     chksum_buffer_offset_ = s.chksum_buffer_offset;
   193     std::memcpy(chksum_buffer_, s.chksum_buffer, 4);
   194   }
   196  protected:
   197   uint32_t chksum_;
   198   uint8_t chksum_buffer_[4];
   199   unsigned chksum_buffer_offset_;
   200 };
   202 // -----------------------------------------------------------------------------
   203 // Process a given OpenType file and write out a sanitised version
   204 //   output: a pointer to an object implementing the OTSStream interface. The
   205 //     sanitisied output will be written to this. In the even of a failure,
   206 //     partial output may have been written.
   207 //   input: the OpenType file
   208 //   length: the size, in bytes, of |input|
   209 // -----------------------------------------------------------------------------
   210 bool OTS_API Process(OTSStream *output, const uint8_t *input, size_t length);
   212 // Signature of the function to be provided by the client in order to report errors.
   213 // The return type is a boolean so that it can be used within an expression,
   214 // but the actual value is ignored. (Suggested convention is to always return 'false'.)
   215 #ifdef __GCC__
   216 #define MSGFUNC_FMT_ATTR __attribute__((format(printf, 2, 3)))
   217 #else
   218 #define MSGFUNC_FMT_ATTR
   219 #endif
   220 typedef bool (*MessageFunc)(void *user_data, const char *format, ...)  MSGFUNC_FMT_ATTR;
   222 // Set a callback function that will be called when OTS is reporting an error.
   223 void OTS_API SetMessageCallback(MessageFunc func, void *user_data);
   225 enum TableAction {
   226   TABLE_ACTION_DEFAULT,  // Use OTS's default action for that table
   227   TABLE_ACTION_SANITIZE, // Sanitize the table, potentially droping it
   228   TABLE_ACTION_PASSTHRU, // Serialize the table unchanged
   229   TABLE_ACTION_DROP      // Drop the table
   230 };
   232 // Signature of the function to be provided by the client to decide what action
   233 // to do for a given table.
   234 typedef TableAction (*TableActionFunc)(uint32_t tag, void *user_data);
   236 // Set a callback function that will be called when OTS needs to decide what to
   237 // do for a font table.
   238 void OTS_API SetTableActionCallback(TableActionFunc func, void *user_data);
   240 // Force to disable debug output even when the library is compiled with
   241 // -DOTS_DEBUG.
   242 void DisableDebugOutput();
   244 #ifdef MOZ_OTS_WOFF2
   245 // Enable WOFF2 support(experimental).
   246 void EnableWOFF2();
   247 #endif
   249 }  // namespace ots
   251 #endif  // OPENTYPE_SANITISER_H_

mercurial