1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/ots/include/opentype-sanitiser.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,251 @@ 1.4 +// Copyright (c) 2009 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#ifndef OPENTYPE_SANITISER_H_ 1.9 +#define OPENTYPE_SANITISER_H_ 1.10 + 1.11 +#if defined(_WIN32) || defined(__CYGWIN__) 1.12 + #define OTS_DLL_IMPORT __declspec(dllimport) 1.13 + #define OTS_DLL_EXPORT __declspec(dllexport) 1.14 +#else 1.15 + #if __GNUC__ >= 4 1.16 + #define OTS_DLL_IMPORT __attribute__((visibility ("default"))) 1.17 + #define OTS_DLL_EXPORT __attribute__((visibility ("default"))) 1.18 + #endif 1.19 +#endif 1.20 + 1.21 +#ifdef OTS_DLL 1.22 + #ifdef OTS_DLL_EXPORTS 1.23 + #define OTS_API OTS_DLL_EXPORT 1.24 + #else 1.25 + #define OTS_API OTS_DLL_IMPORT 1.26 + #endif 1.27 +#else 1.28 + #define OTS_API 1.29 +#endif 1.30 + 1.31 +#if defined(_WIN32) 1.32 +#include <stdlib.h> 1.33 +typedef signed char int8_t; 1.34 +typedef unsigned char uint8_t; 1.35 +typedef short int16_t; 1.36 +typedef unsigned short uint16_t; 1.37 +typedef int int32_t; 1.38 +typedef unsigned int uint32_t; 1.39 +typedef __int64 int64_t; 1.40 +typedef unsigned __int64 uint64_t; 1.41 +#define ntohl(x) _byteswap_ulong (x) 1.42 +#define ntohs(x) _byteswap_ushort (x) 1.43 +#define htonl(x) _byteswap_ulong (x) 1.44 +#define htons(x) _byteswap_ushort (x) 1.45 +#else 1.46 +#include <arpa/inet.h> 1.47 +#include <stdint.h> 1.48 +#endif 1.49 + 1.50 +#include <algorithm> 1.51 +#include <cassert> 1.52 +#include <cstddef> 1.53 +#include <cstring> 1.54 + 1.55 +namespace ots { 1.56 + 1.57 +// ----------------------------------------------------------------------------- 1.58 +// This is an interface for an abstract stream class which is used for writing 1.59 +// the serialised results out. 1.60 +// ----------------------------------------------------------------------------- 1.61 +class OTSStream { 1.62 + public: 1.63 + OTSStream() { 1.64 + ResetChecksum(); 1.65 + } 1.66 + 1.67 + virtual ~OTSStream() {} 1.68 + 1.69 + // This should be implemented to perform the actual write. 1.70 + virtual bool WriteRaw(const void *data, size_t length) = 0; 1.71 + 1.72 + bool Write(const void *data, size_t length) { 1.73 + if (!length) return false; 1.74 + 1.75 + const size_t orig_length = length; 1.76 + size_t offset = 0; 1.77 + if (chksum_buffer_offset_) { 1.78 + const size_t l = 1.79 + std::min(length, static_cast<size_t>(4) - chksum_buffer_offset_); 1.80 + std::memcpy(chksum_buffer_ + chksum_buffer_offset_, data, l); 1.81 + chksum_buffer_offset_ += l; 1.82 + offset += l; 1.83 + length -= l; 1.84 + } 1.85 + 1.86 + if (chksum_buffer_offset_ == 4) { 1.87 + uint32_t tmp; 1.88 + std::memcpy(&tmp, chksum_buffer_, 4); 1.89 + chksum_ += ntohl(tmp); 1.90 + chksum_buffer_offset_ = 0; 1.91 + } 1.92 + 1.93 + while (length >= 4) { 1.94 + uint32_t tmp; 1.95 + std::memcpy(&tmp, reinterpret_cast<const uint8_t *>(data) + offset, 1.96 + sizeof(uint32_t)); 1.97 + chksum_ += ntohl(tmp); 1.98 + length -= 4; 1.99 + offset += 4; 1.100 + } 1.101 + 1.102 + if (length) { 1.103 + if (chksum_buffer_offset_ != 0) return false; // not reached 1.104 + if (length > 4) return false; // not reached 1.105 + std::memcpy(chksum_buffer_, 1.106 + reinterpret_cast<const uint8_t*>(data) + offset, length); 1.107 + chksum_buffer_offset_ = length; 1.108 + } 1.109 + 1.110 + return WriteRaw(data, orig_length); 1.111 + } 1.112 + 1.113 + virtual bool Seek(off_t position) = 0; 1.114 + virtual off_t Tell() const = 0; 1.115 + 1.116 + virtual bool Pad(size_t bytes) { 1.117 + static const uint32_t kZero = 0; 1.118 + while (bytes >= 4) { 1.119 + if (!WriteTag(kZero)) return false; 1.120 + bytes -= 4; 1.121 + } 1.122 + while (bytes) { 1.123 + static const uint8_t kZerob = 0; 1.124 + if (!Write(&kZerob, 1)) return false; 1.125 + bytes--; 1.126 + } 1.127 + return true; 1.128 + } 1.129 + 1.130 + bool WriteU8(uint8_t v) { 1.131 + return Write(&v, sizeof(v)); 1.132 + } 1.133 + 1.134 + bool WriteU16(uint16_t v) { 1.135 + v = htons(v); 1.136 + return Write(&v, sizeof(v)); 1.137 + } 1.138 + 1.139 + bool WriteS16(int16_t v) { 1.140 + v = htons(v); 1.141 + return Write(&v, sizeof(v)); 1.142 + } 1.143 + 1.144 + bool WriteU24(uint32_t v) { 1.145 + v = htonl(v); 1.146 + return Write(reinterpret_cast<uint8_t*>(&v)+1, 3); 1.147 + } 1.148 + 1.149 + bool WriteU32(uint32_t v) { 1.150 + v = htonl(v); 1.151 + return Write(&v, sizeof(v)); 1.152 + } 1.153 + 1.154 + bool WriteS32(int32_t v) { 1.155 + v = htonl(v); 1.156 + return Write(&v, sizeof(v)); 1.157 + } 1.158 + 1.159 + bool WriteR64(uint64_t v) { 1.160 + return Write(&v, sizeof(v)); 1.161 + } 1.162 + 1.163 + bool WriteTag(uint32_t v) { 1.164 + return Write(&v, sizeof(v)); 1.165 + } 1.166 + 1.167 + void ResetChecksum() { 1.168 + chksum_ = 0; 1.169 + chksum_buffer_offset_ = 0; 1.170 + } 1.171 + 1.172 + uint32_t chksum() const { 1.173 + assert(chksum_buffer_offset_ == 0); 1.174 + return chksum_; 1.175 + } 1.176 + 1.177 + struct ChecksumState { 1.178 + uint32_t chksum; 1.179 + uint8_t chksum_buffer[4]; 1.180 + unsigned chksum_buffer_offset; 1.181 + }; 1.182 + 1.183 + ChecksumState SaveChecksumState() const { 1.184 + ChecksumState s; 1.185 + s.chksum = chksum_; 1.186 + s.chksum_buffer_offset = chksum_buffer_offset_; 1.187 + std::memcpy(s.chksum_buffer, chksum_buffer_, 4); 1.188 + 1.189 + return s; 1.190 + } 1.191 + 1.192 + void RestoreChecksum(const ChecksumState &s) { 1.193 + assert(chksum_buffer_offset_ == 0); 1.194 + chksum_ += s.chksum; 1.195 + chksum_buffer_offset_ = s.chksum_buffer_offset; 1.196 + std::memcpy(chksum_buffer_, s.chksum_buffer, 4); 1.197 + } 1.198 + 1.199 + protected: 1.200 + uint32_t chksum_; 1.201 + uint8_t chksum_buffer_[4]; 1.202 + unsigned chksum_buffer_offset_; 1.203 +}; 1.204 + 1.205 +// ----------------------------------------------------------------------------- 1.206 +// Process a given OpenType file and write out a sanitised version 1.207 +// output: a pointer to an object implementing the OTSStream interface. The 1.208 +// sanitisied output will be written to this. In the even of a failure, 1.209 +// partial output may have been written. 1.210 +// input: the OpenType file 1.211 +// length: the size, in bytes, of |input| 1.212 +// ----------------------------------------------------------------------------- 1.213 +bool OTS_API Process(OTSStream *output, const uint8_t *input, size_t length); 1.214 + 1.215 +// Signature of the function to be provided by the client in order to report errors. 1.216 +// The return type is a boolean so that it can be used within an expression, 1.217 +// but the actual value is ignored. (Suggested convention is to always return 'false'.) 1.218 +#ifdef __GCC__ 1.219 +#define MSGFUNC_FMT_ATTR __attribute__((format(printf, 2, 3))) 1.220 +#else 1.221 +#define MSGFUNC_FMT_ATTR 1.222 +#endif 1.223 +typedef bool (*MessageFunc)(void *user_data, const char *format, ...) MSGFUNC_FMT_ATTR; 1.224 + 1.225 +// Set a callback function that will be called when OTS is reporting an error. 1.226 +void OTS_API SetMessageCallback(MessageFunc func, void *user_data); 1.227 + 1.228 +enum TableAction { 1.229 + TABLE_ACTION_DEFAULT, // Use OTS's default action for that table 1.230 + TABLE_ACTION_SANITIZE, // Sanitize the table, potentially droping it 1.231 + TABLE_ACTION_PASSTHRU, // Serialize the table unchanged 1.232 + TABLE_ACTION_DROP // Drop the table 1.233 +}; 1.234 + 1.235 +// Signature of the function to be provided by the client to decide what action 1.236 +// to do for a given table. 1.237 +typedef TableAction (*TableActionFunc)(uint32_t tag, void *user_data); 1.238 + 1.239 +// Set a callback function that will be called when OTS needs to decide what to 1.240 +// do for a font table. 1.241 +void OTS_API SetTableActionCallback(TableActionFunc func, void *user_data); 1.242 + 1.243 +// Force to disable debug output even when the library is compiled with 1.244 +// -DOTS_DEBUG. 1.245 +void DisableDebugOutput(); 1.246 + 1.247 +#ifdef MOZ_OTS_WOFF2 1.248 +// Enable WOFF2 support(experimental). 1.249 +void EnableWOFF2(); 1.250 +#endif 1.251 + 1.252 +} // namespace ots 1.253 + 1.254 +#endif // OPENTYPE_SANITISER_H_