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_
10 typedef signed char int8_t;
11 typedef unsigned char uint8_t;
12 typedef short int16_t;
13 typedef unsigned short uint16_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)
23 #include <arpa/inet.h>
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))
37 // -----------------------------------------------------------------------------
38 // This is an interface for an abstract stream class which is used for writing
39 // the serialised results out.
40 // -----------------------------------------------------------------------------
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
;
56 size_t chksum_offset
= Tell() & 3;
58 const size_t l
= std::min(length
, static_cast<size_t>(4) - chksum_offset
);
60 std::memcpy(reinterpret_cast<uint8_t *>(&tmp
) + chksum_offset
, data
, l
);
61 chksum_
+= ntohl(tmp
);
68 std::memcpy(&tmp
, reinterpret_cast<const uint8_t *>(data
) + offset
,
70 chksum_
+= ntohl(tmp
);
76 if (length
> 4) return false; // not reached
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;
92 if (!WriteTag(kZero
)) return false;
96 static const uint8_t kZerob
= 0;
97 if (!Write(&kZerob
, 1)) return false;
103 bool WriteU8(uint8_t v
) {
104 return Write(&v
, sizeof(v
));
107 bool WriteU16(uint16_t v
) {
109 return Write(&v
, sizeof(v
));
112 bool WriteS16(int16_t v
) {
114 return Write(&v
, sizeof(v
));
117 bool WriteU24(uint32_t v
) {
119 return Write(reinterpret_cast<uint8_t*>(&v
)+1, 3);
122 bool WriteU32(uint32_t v
) {
124 return Write(&v
, sizeof(v
));
127 bool WriteS32(int32_t v
) {
129 return Write(&v
, sizeof(v
));
132 bool WriteR64(uint64_t v
) {
133 return Write(&v
, sizeof(v
));
136 bool WriteTag(uint32_t v
) {
137 return Write(&v
, sizeof(v
));
140 void ResetChecksum() {
141 assert((Tell() & 3) == 0);
145 uint32_t chksum() const {
154 #define MSGFUNC_FMT_ATTR __attribute__((format(printf, 2, 3)))
156 #define MSGFUNC_FMT_ATTR
160 TABLE_ACTION_DEFAULT
, // Use OTS's default action for that table
161 TABLE_ACTION_SANITIZE
, // Sanitize the table, potentially droping it
162 TABLE_ACTION_PASSTHRU
, // Serialize the table unchanged
163 TABLE_ACTION_DROP
// Drop the table
169 virtual ~OTSContext() {}
171 // Process a given OpenType file and write out a sanitised version
172 // output: a pointer to an object implementing the OTSStream interface. The
173 // sanitisied output will be written to this. In the even of a failure,
174 // partial output may have been written.
175 // input: the OpenType file
176 // length: the size, in bytes, of |input|
177 bool Process(OTSStream
*output
, const uint8_t *input
, size_t length
);
179 // This function will be called when OTS is reporting an error.
180 // level: the severity of the generated message:
181 // 0: error messages in case OTS fails to sanitize the font.
182 // 1: warning messages about issue OTS fixed in the sanitized font.
183 virtual void Message(int level
, const char *format
, ...) MSGFUNC_FMT_ATTR
{}
185 // This function will be called when OTS needs to decide what to do for a
187 // tag: table tag as an integer in big-endian byte order, independent of
188 // platform endianness
189 virtual TableAction
GetTableAction(uint32_t tag
) { return ots::TABLE_ACTION_DEFAULT
; }
194 #endif // OPENTYPE_SANITISER_H_