LCOV - code coverage report
Current view: top level - gfx/ots/include - opentype-sanitiser.h (source / functions) Hit Total Coverage
Test: output.info Lines: 0 64 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 19 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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.
       4             : 
       5             : #ifndef OPENTYPE_SANITISER_H_
       6             : #define OPENTYPE_SANITISER_H_
       7             : 
       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
      17             : 
      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
      27             : 
      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
      46             : 
      47             : #include <sys/types.h>
      48             : 
      49             : #include <algorithm>
      50             : #include <cassert>
      51             : #include <cstddef>
      52             : #include <cstring>
      53             : 
      54             : #define OTS_TAG(c1,c2,c3,c4) ((uint32_t)((((uint8_t)(c1))<<24)|(((uint8_t)(c2))<<16)|(((uint8_t)(c3))<<8)|((uint8_t)(c4))))
      55             : #define OTS_UNTAG(tag)       ((uint8_t)((tag)>>24)), ((uint8_t)((tag)>>16)), ((uint8_t)((tag)>>8)), ((uint8_t)(tag))
      56             : 
      57             : namespace ots {
      58             : 
      59             : // -----------------------------------------------------------------------------
      60             : // This is an interface for an abstract stream class which is used for writing
      61             : // the serialised results out.
      62             : // -----------------------------------------------------------------------------
      63             : class OTSStream {
      64             :  public:
      65           0 :   OTSStream() : chksum_(0) {}
      66             : 
      67           0 :   virtual ~OTSStream() {}
      68             : 
      69             :   // This should be implemented to perform the actual write.
      70             :   virtual bool WriteRaw(const void *data, size_t length) = 0;
      71             : 
      72           0 :   bool Write(const void *data, size_t length) {
      73           0 :     if (!length) return false;
      74             : 
      75           0 :     const size_t orig_length = length;
      76           0 :     size_t offset = 0;
      77             : 
      78           0 :     size_t chksum_offset = Tell() & 3;
      79           0 :     if (chksum_offset) {
      80           0 :       const size_t l = std::min(length, static_cast<size_t>(4) - chksum_offset);
      81           0 :       uint32_t tmp = 0;
      82           0 :       std::memcpy(reinterpret_cast<uint8_t *>(&tmp) + chksum_offset, data, l);
      83           0 :       chksum_ += ntohl(tmp);
      84           0 :       length -= l;
      85           0 :       offset += l;
      86             :     }
      87             : 
      88           0 :     while (length >= 4) {
      89             :       uint32_t tmp;
      90           0 :       std::memcpy(&tmp, reinterpret_cast<const uint8_t *>(data) + offset,
      91           0 :         sizeof(uint32_t));
      92           0 :       chksum_ += ntohl(tmp);
      93           0 :       length -= 4;
      94           0 :       offset += 4;
      95             :     }
      96             : 
      97           0 :     if (length) {
      98           0 :       if (length > 4) return false;  // not reached
      99           0 :       uint32_t tmp = 0;
     100           0 :       std::memcpy(&tmp,
     101           0 :                   reinterpret_cast<const uint8_t*>(data) + offset, length);
     102           0 :       chksum_ += ntohl(tmp);
     103             :     }
     104             : 
     105           0 :     return WriteRaw(data, orig_length);
     106             :   }
     107             : 
     108             :   virtual bool Seek(off_t position) = 0;
     109             :   virtual off_t Tell() const = 0;
     110             : 
     111           0 :   virtual bool Pad(size_t bytes) {
     112             :     static const uint32_t kZero = 0;
     113           0 :     while (bytes >= 4) {
     114           0 :       if (!Write(&kZero, 4)) return false;
     115           0 :       bytes -= 4;
     116             :     }
     117           0 :     while (bytes) {
     118             :       static const uint8_t kZerob = 0;
     119           0 :       if (!Write(&kZerob, 1)) return false;
     120           0 :       bytes--;
     121             :     }
     122           0 :     return true;
     123             :   }
     124             : 
     125           0 :   bool WriteU8(uint8_t v) {
     126           0 :     return Write(&v, sizeof(v));
     127             :   }
     128             : 
     129           0 :   bool WriteU16(uint16_t v) {
     130           0 :     v = htons(v);
     131           0 :     return Write(&v, sizeof(v));
     132             :   }
     133             : 
     134           0 :   bool WriteS16(int16_t v) {
     135           0 :     v = htons(v);
     136           0 :     return Write(&v, sizeof(v));
     137             :   }
     138             : 
     139           0 :   bool WriteU24(uint32_t v) {
     140           0 :     v = htonl(v);
     141           0 :     return Write(reinterpret_cast<uint8_t*>(&v)+1, 3);
     142             :   }
     143             : 
     144           0 :   bool WriteU32(uint32_t v) {
     145           0 :     v = htonl(v);
     146           0 :     return Write(&v, sizeof(v));
     147             :   }
     148             : 
     149           0 :   bool WriteS32(int32_t v) {
     150           0 :     v = htonl(v);
     151           0 :     return Write(&v, sizeof(v));
     152             :   }
     153             : 
     154           0 :   bool WriteR64(uint64_t v) {
     155           0 :     return Write(&v, sizeof(v));
     156             :   }
     157             : 
     158           0 :   void ResetChecksum() {
     159           0 :     assert((Tell() & 3) == 0);
     160           0 :     chksum_ = 0;
     161           0 :   }
     162             : 
     163           0 :   uint32_t chksum() const {
     164           0 :     return chksum_;
     165             :   }
     166             : 
     167             :  protected:
     168             :   uint32_t chksum_;
     169             : };
     170             : 
     171             : #ifdef __GCC__
     172             : #define MSGFUNC_FMT_ATTR __attribute__((format(printf, 2, 3)))
     173             : #else
     174             : #define MSGFUNC_FMT_ATTR
     175             : #endif
     176             : 
     177             : enum TableAction {
     178             :   TABLE_ACTION_DEFAULT,  // Use OTS's default action for that table
     179             :   TABLE_ACTION_SANITIZE, // Sanitize the table, potentially droping it
     180             :   TABLE_ACTION_PASSTHRU, // Serialize the table unchanged
     181             :   TABLE_ACTION_DROP      // Drop the table
     182             : };
     183             : 
     184             : class OTS_API OTSContext {
     185             :   public:
     186           0 :     OTSContext() {}
     187           0 :     virtual ~OTSContext() {}
     188             : 
     189             :     // Process a given OpenType file and write out a sanitised version
     190             :     //   output: a pointer to an object implementing the OTSStream interface. The
     191             :     //     sanitisied output will be written to this. In the even of a failure,
     192             :     //     partial output may have been written.
     193             :     //   input: the OpenType file
     194             :     //   length: the size, in bytes, of |input|
     195             :     //   index: if the input is a font collection and index is specified, then
     196             :     //     the corresponding font will be returned, otherwise the whole
     197             :     //     collection. Ignored for non-collection fonts.
     198             :     bool Process(OTSStream *output, const uint8_t *input, size_t length, uint32_t index = -1);
     199             : 
     200             :     // This function will be called when OTS is reporting an error.
     201             :     //   level: the severity of the generated message:
     202             :     //     0: error messages in case OTS fails to sanitize the font.
     203             :     //     1: warning messages about issue OTS fixed in the sanitized font.
     204           0 :     virtual void Message(int level, const char *format, ...) MSGFUNC_FMT_ATTR {}
     205             : 
     206             :     // This function will be called when OTS needs to decide what to do for a
     207             :     // font table.
     208             :     //   tag: table tag formed with OTS_TAG() macro
     209           0 :     virtual TableAction GetTableAction(uint32_t tag) { return ots::TABLE_ACTION_DEFAULT; }
     210             : };
     211             : 
     212             : }  // namespace ots
     213             : 
     214             : #endif  // OPENTYPE_SANITISER_H_

Generated by: LCOV version 1.13