1 // Copyright (c) 2009-2017 The OTS 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)
13 #define OTS_DLL_IMPORT __attribute__((visibility ("default")))
14 #define OTS_DLL_EXPORT __attribute__((visibility ("default")))
19 #ifdef OTS_DLL_EXPORTS
20 #define OTS_API OTS_DLL_EXPORT
22 #define OTS_API OTS_DLL_IMPORT
30 typedef signed char int8_t;
31 typedef unsigned char uint8_t;
32 typedef short int16_t;
33 typedef unsigned short uint16_t;
35 typedef unsigned int uint32_t;
36 typedef __int64
int64_t;
37 typedef unsigned __int64
uint64_t;
38 #define ots_ntohl(x) _byteswap_ulong (x)
39 #define ots_ntohs(x) _byteswap_ushort (x)
40 #define ots_htonl(x) _byteswap_ulong (x)
41 #define ots_htons(x) _byteswap_ushort (x)
43 #include <arpa/inet.h>
45 #define ots_ntohl(x) ntohl (x)
46 #define ots_ntohs(x) ntohs (x)
47 #define ots_htonl(x) htonl (x)
48 #define ots_htons(x) htons (x)
51 #include <sys/types.h>
58 #define OTS_TAG(c1,c2,c3,c4) ((uint32_t)((((uint8_t)(c1))<<24)|(((uint8_t)(c2))<<16)|(((uint8_t)(c3))<<8)|((uint8_t)(c4))))
59 #define OTS_UNTAG(tag) ((char)((tag)>>24)), ((char)((tag)>>16)), ((char)((tag)>>8)), ((char)(tag))
61 #if defined(__GNUC__) && (__GNUC__ >= 4) || (__clang__)
62 #define OTS_UNUSED __attribute__((unused))
63 #elif defined(_MSC_VER)
64 #define OTS_UNUSED __pragma(warning(suppress: 4100 4101))
71 // -----------------------------------------------------------------------------
72 // This is an interface for an abstract stream class which is used for writing
73 // the serialised results out.
74 // -----------------------------------------------------------------------------
77 OTSStream() : chksum_(0) {}
79 virtual ~OTSStream() {}
81 virtual size_t size() = 0;
83 // This should be implemented to perform the actual write.
84 virtual bool WriteRaw(const void *data
, size_t length
) = 0;
86 bool Write(const void *data
, size_t length
) {
87 if (!length
) return false;
89 const size_t orig_length
= length
;
92 size_t chksum_offset
= Tell() & 3;
94 const size_t l
= std::min(length
, static_cast<size_t>(4) - chksum_offset
);
96 std::memcpy(reinterpret_cast<uint8_t *>(&tmp
) + chksum_offset
, data
, l
);
97 chksum_
+= ots_ntohl(tmp
);
102 while (length
>= 4) {
104 std::memcpy(&tmp
, reinterpret_cast<const uint8_t *>(data
) + offset
,
106 chksum_
+= ots_ntohl(tmp
);
112 if (length
> 4) return false; // not reached
115 reinterpret_cast<const uint8_t*>(data
) + offset
, length
);
116 chksum_
+= ots_ntohl(tmp
);
119 return WriteRaw(data
, orig_length
);
122 virtual bool Seek(off_t position
) = 0;
123 virtual off_t
Tell() const = 0;
125 virtual bool Pad(size_t bytes
) {
126 static const uint32_t kZero
= 0;
128 if (!Write(&kZero
, 4)) return false;
132 static const uint8_t kZerob
= 0;
133 if (!Write(&kZerob
, 1)) return false;
139 bool WriteU8(uint8_t v
) {
140 return Write(&v
, sizeof(v
));
143 bool WriteU16(uint16_t v
) {
145 return Write(&v
, sizeof(v
));
148 bool WriteS16(int16_t v
) {
150 return Write(&v
, sizeof(v
));
153 bool WriteU24(uint32_t v
) {
155 return Write(reinterpret_cast<uint8_t*>(&v
)+1, 3);
158 bool WriteU32(uint32_t v
) {
160 return Write(&v
, sizeof(v
));
163 bool WriteS32(int32_t v
) {
165 return Write(&v
, sizeof(v
));
168 bool WriteR64(uint64_t v
) {
169 return Write(&v
, sizeof(v
));
172 void ResetChecksum() {
173 assert((Tell() & 3) == 0);
177 uint32_t chksum() const {
186 #define MSGFUNC_FMT_ATTR __attribute__((format(printf, 2, 3)))
188 #define MSGFUNC_FMT_ATTR
192 TABLE_ACTION_DEFAULT
, // Use OTS's default action for that table
193 TABLE_ACTION_SANITIZE
, // Sanitize the table, potentially dropping it
194 TABLE_ACTION_PASSTHRU
, // Serialize the table unchanged
195 TABLE_ACTION_DROP
// Drop the table
198 class OTS_API OTSContext
{
201 virtual ~OTSContext() {}
203 // Process a given OpenType file and write out a sanitized 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 // index: if the input is a font collection and index is specified, then
210 // the corresponding font will be returned, otherwise the whole
211 // collection. Ignored for non-collection fonts.
212 bool Process(OTSStream
*output
, const uint8_t *input
, size_t length
, uint32_t index
= -1);
214 // This function will be called when OTS is reporting an error.
215 // level: the severity of the generated message:
216 // 0: error messages in case OTS fails to sanitize the font.
217 // 1: warning messages about issue OTS fixed in the sanitized font.
218 virtual void Message(int level OTS_UNUSED
, const char *format OTS_UNUSED
, ...) MSGFUNC_FMT_ATTR
{}
220 // This function will be called when OTS needs to decide what to do for a
222 // tag: table tag formed with OTS_TAG() macro
223 virtual TableAction
GetTableAction(uint32_t tag OTS_UNUSED
) { return ots::TABLE_ACTION_DEFAULT
; }
228 #endif // OPENTYPE_SANITISER_H_