Roll src/third_party/WebKit a3b4a2e:7441784 (svn 202551:202552)
[chromium-blink-merge.git] / third_party / ots / include / opentype-sanitiser.h
blob0343cdcc9c7731b7c0aa04d6c7d2583a8eddd925
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)
9 #include <stdlib.h>
10 typedef signed char int8_t;
11 typedef unsigned char uint8_t;
12 typedef short int16_t;
13 typedef unsigned short uint16_t;
14 typedef int int32_t;
15 typedef unsigned int uint32_t;
16 typedef __int64 int64_t;
17 typedef unsigned __int64 uint64_t;
18 #define ntohl(x) _byteswap_ulong (x)
19 #define ntohs(x) _byteswap_ushort (x)
20 #define htonl(x) _byteswap_ulong (x)
21 #define htons(x) _byteswap_ushort (x)
22 #else
23 #include <arpa/inet.h>
24 #include <stdint.h>
25 #endif
27 #include <algorithm>
28 #include <cassert>
29 #include <cstddef>
30 #include <cstring>
32 #define OTS_TAG(c1,c2,c3,c4) ((uint32_t)((((uint8_t)(c1))<<24)|(((uint8_t)(c2))<<16)|(((uint8_t)(c3))<<8)|((uint8_t)(c4))))
33 #define OTS_UNTAG(tag) ((uint8_t)((tag)>>24)), ((uint8_t)((tag)>>16)), ((uint8_t)((tag)>>8)), ((uint8_t)(tag))
35 namespace ots {
37 // -----------------------------------------------------------------------------
38 // This is an interface for an abstract stream class which is used for writing
39 // the serialised results out.
40 // -----------------------------------------------------------------------------
41 class OTSStream {
42 public:
43 OTSStream() : chksum_(0) {}
45 virtual ~OTSStream() {}
47 // This should be implemented to perform the actual write.
48 virtual bool WriteRaw(const void *data, size_t length) = 0;
50 bool Write(const void *data, size_t length) {
51 if (!length) return false;
53 const size_t orig_length = length;
54 size_t offset = 0;
56 size_t chksum_offset = Tell() & 3;
57 if (chksum_offset) {
58 const size_t l = std::min(length, static_cast<size_t>(4) - chksum_offset);
59 uint32_t tmp = 0;
60 std::memcpy(reinterpret_cast<uint8_t *>(&tmp) + chksum_offset, data, l);
61 chksum_ += ntohl(tmp);
62 length -= l;
63 offset += l;
66 while (length >= 4) {
67 uint32_t tmp;
68 std::memcpy(&tmp, reinterpret_cast<const uint8_t *>(data) + offset,
69 sizeof(uint32_t));
70 chksum_ += ntohl(tmp);
71 length -= 4;
72 offset += 4;
75 if (length) {
76 if (length > 4) return false; // not reached
77 uint32_t tmp = 0;
78 std::memcpy(&tmp,
79 reinterpret_cast<const uint8_t*>(data) + offset, length);
80 chksum_ += ntohl(tmp);
83 return WriteRaw(data, orig_length);
86 virtual bool Seek(off_t position) = 0;
87 virtual off_t Tell() const = 0;
89 virtual bool Pad(size_t bytes) {
90 static const uint32_t kZero = 0;
91 while (bytes >= 4) {
92 if (!Write(&kZero, 4)) return false;
93 bytes -= 4;
95 while (bytes) {
96 static const uint8_t kZerob = 0;
97 if (!Write(&kZerob, 1)) return false;
98 bytes--;
100 return true;
103 bool WriteU8(uint8_t v) {
104 return Write(&v, sizeof(v));
107 bool WriteU16(uint16_t v) {
108 v = htons(v);
109 return Write(&v, sizeof(v));
112 bool WriteS16(int16_t v) {
113 v = htons(v);
114 return Write(&v, sizeof(v));
117 bool WriteU24(uint32_t v) {
118 v = htonl(v);
119 return Write(reinterpret_cast<uint8_t*>(&v)+1, 3);
122 bool WriteU32(uint32_t v) {
123 v = htonl(v);
124 return Write(&v, sizeof(v));
127 bool WriteS32(int32_t v) {
128 v = htonl(v);
129 return Write(&v, sizeof(v));
132 bool WriteR64(uint64_t v) {
133 return Write(&v, sizeof(v));
136 void ResetChecksum() {
137 assert((Tell() & 3) == 0);
138 chksum_ = 0;
141 uint32_t chksum() const {
142 return chksum_;
145 protected:
146 uint32_t chksum_;
149 #ifdef __GCC__
150 #define MSGFUNC_FMT_ATTR __attribute__((format(printf, 2, 3)))
151 #else
152 #define MSGFUNC_FMT_ATTR
153 #endif
155 enum TableAction {
156 TABLE_ACTION_DEFAULT, // Use OTS's default action for that table
157 TABLE_ACTION_SANITIZE, // Sanitize the table, potentially droping it
158 TABLE_ACTION_PASSTHRU, // Serialize the table unchanged
159 TABLE_ACTION_DROP // Drop the table
162 class OTSContext {
163 public:
164 OTSContext() {}
165 virtual ~OTSContext() {}
167 // Process a given OpenType file and write out a sanitised version
168 // output: a pointer to an object implementing the OTSStream interface. The
169 // sanitisied output will be written to this. In the even of a failure,
170 // partial output may have been written.
171 // input: the OpenType file
172 // length: the size, in bytes, of |input|
173 // index: if the input is a font collection and index is specified, then
174 // the corresponding font will be returned, otherwise the whole
175 // collection. Ignored for non-collection fonts.
176 bool Process(OTSStream *output, const uint8_t *input, size_t length, uint32_t index = -1);
178 // This function will be called when OTS is reporting an error.
179 // level: the severity of the generated message:
180 // 0: error messages in case OTS fails to sanitize the font.
181 // 1: warning messages about issue OTS fixed in the sanitized font.
182 virtual void Message(int level, const char *format, ...) MSGFUNC_FMT_ATTR {}
184 // This function will be called when OTS needs to decide what to do for a
185 // font table.
186 // tag: table tag as an integer in big-endian byte order, independent of
187 // platform endianness
188 virtual TableAction GetTableAction(uint32_t tag) { return ots::TABLE_ACTION_DEFAULT; }
191 } // namespace ots
193 #endif // OPENTYPE_SANITISER_H_