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 (!Write(&kZero
, 4)) 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 void ResetChecksum() {
137 assert((Tell() & 3) == 0);
141 uint32_t chksum() const {
150 #define MSGFUNC_FMT_ATTR __attribute__((format(printf, 2, 3)))
152 #define MSGFUNC_FMT_ATTR
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
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
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
; }
193 #endif // OPENTYPE_SANITISER_H_