summaryrefslogtreecommitdiffstats
path: root/gfx/ots/src/ots.h
diff options
context:
space:
mode:
authorwolfbeast <mcwerewolf@wolfbeast.com>2019-11-14 10:07:01 +0100
committerwolfbeast <mcwerewolf@wolfbeast.com>2019-11-14 10:07:01 +0100
commit0f8691a48869932cd3de5195f5211c25e4691b21 (patch)
tree829675d64c457be0b447dfbcf9534cc22f83d392 /gfx/ots/src/ots.h
parent36975f3865948f3faa959fe386e58b22783bd379 (diff)
downloadUXP-0f8691a48869932cd3de5195f5211c25e4691b21.tar
UXP-0f8691a48869932cd3de5195f5211c25e4691b21.tar.gz
UXP-0f8691a48869932cd3de5195f5211c25e4691b21.tar.lz
UXP-0f8691a48869932cd3de5195f5211c25e4691b21.tar.xz
UXP-0f8691a48869932cd3de5195f5211c25e4691b21.zip
Issue #1288 - Part 4: Update the OpenType Sanitizer component to 8.0.0
Diffstat (limited to 'gfx/ots/src/ots.h')
-rw-r--r--gfx/ots/src/ots.h220
1 files changed, 142 insertions, 78 deletions
diff --git a/gfx/ots/src/ots.h b/gfx/ots/src/ots.h
index 2d13f8d6d..4d2be1689 100644
--- a/gfx/ots/src/ots.h
+++ b/gfx/ots/src/ots.h
@@ -1,10 +1,14 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2009-2017 The OTS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef OTS_H_
#define OTS_H_
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
#include <stddef.h>
#include <cstdarg>
#include <cstddef>
@@ -54,13 +58,9 @@ namespace ots {
#define OTS_WARNING_MSG_(otf_,...) \
OTS_MESSAGE_(1,otf_,__VA_ARGS__)
-// Generate a message with an associated table tag
-#define OTS_FAILURE_MSG_TAG_(otf_,msg_,tag_) \
- (OTS_MESSAGE_(0,otf_,"%c%c%c%c: %s", OTS_UNTAG(tag_), msg_), false)
-
// Convenience macros for use in files that only handle a single table tag,
// defined as TABLE_NAME at the top of the file; the 'file' variable is
-// expected to be the current OpenTypeFile pointer.
+// expected to be the current FontFile pointer.
#define OTS_FAILURE_MSG(...) OTS_FAILURE_MSG_(font->file, TABLE_NAME ": " __VA_ARGS__)
#define OTS_WARNING(...) OTS_WARNING_MSG_(font->file, TABLE_NAME ": " __VA_ARGS__)
@@ -112,7 +112,7 @@ class Buffer {
return OTS_FAILURE();
}
std::memcpy(value, buffer_ + offset_, sizeof(uint16_t));
- *value = ntohs(*value);
+ *value = ots_ntohs(*value);
offset_ += 2;
return true;
}
@@ -137,7 +137,7 @@ class Buffer {
return OTS_FAILURE();
}
std::memcpy(value, buffer_ + offset_, sizeof(uint32_t));
- *value = ntohl(*value);
+ *value = ots_ntohl(*value);
offset_ += 4;
return true;
}
@@ -184,111 +184,175 @@ template<typename T> T Round2(T value) {
return (value + 1) & ~1;
}
+// Check that a tag consists entirely of printable ASCII characters
+bool CheckTag(uint32_t tag_value);
+
bool IsValidVersionTag(uint32_t tag);
-#define FOR_EACH_TABLE_TYPE \
- F(cff, CFF) \
- F(cmap, CMAP) \
- F(cvt, CVT) \
- F(fpgm, FPGM) \
- F(gasp, GASP) \
- F(gdef, GDEF) \
- F(glyf, GLYF) \
- F(gpos, GPOS) \
- F(gsub, GSUB) \
- F(hdmx, HDMX) \
- F(head, HEAD) \
- F(hhea, HHEA) \
- F(hmtx, HMTX) \
- F(kern, KERN) \
- F(loca, LOCA) \
- F(ltsh, LTSH) \
- F(math, MATH) \
- F(maxp, MAXP) \
- F(name, NAME) \
- F(os2, OS2) \
- F(post, POST) \
- F(prep, PREP) \
- F(vdmx, VDMX) \
- F(vorg, VORG) \
- F(vhea, VHEA) \
- F(vmtx, VMTX)
-
-#define F(name, capname) struct OpenType##capname;
-FOR_EACH_TABLE_TYPE
-#undef F
+#define OTS_TAG_CFF OTS_TAG('C','F','F',' ')
+#define OTS_TAG_CFF2 OTS_TAG('C','F','F','2')
+#define OTS_TAG_CMAP OTS_TAG('c','m','a','p')
+#define OTS_TAG_CVT OTS_TAG('c','v','t',' ')
+#define OTS_TAG_FEAT OTS_TAG('F','e','a','t')
+#define OTS_TAG_FPGM OTS_TAG('f','p','g','m')
+#define OTS_TAG_GASP OTS_TAG('g','a','s','p')
+#define OTS_TAG_GDEF OTS_TAG('G','D','E','F')
+#define OTS_TAG_GLAT OTS_TAG('G','l','a','t')
+#define OTS_TAG_GLOC OTS_TAG('G','l','o','c')
+#define OTS_TAG_GLYF OTS_TAG('g','l','y','f')
+#define OTS_TAG_GPOS OTS_TAG('G','P','O','S')
+#define OTS_TAG_GSUB OTS_TAG('G','S','U','B')
+#define OTS_TAG_HDMX OTS_TAG('h','d','m','x')
+#define OTS_TAG_HEAD OTS_TAG('h','e','a','d')
+#define OTS_TAG_HHEA OTS_TAG('h','h','e','a')
+#define OTS_TAG_HMTX OTS_TAG('h','m','t','x')
+#define OTS_TAG_KERN OTS_TAG('k','e','r','n')
+#define OTS_TAG_LOCA OTS_TAG('l','o','c','a')
+#define OTS_TAG_LTSH OTS_TAG('L','T','S','H')
+#define OTS_TAG_MATH OTS_TAG('M','A','T','H')
+#define OTS_TAG_MAXP OTS_TAG('m','a','x','p')
+#define OTS_TAG_NAME OTS_TAG('n','a','m','e')
+#define OTS_TAG_OS2 OTS_TAG('O','S','/','2')
+#define OTS_TAG_POST OTS_TAG('p','o','s','t')
+#define OTS_TAG_PREP OTS_TAG('p','r','e','p')
+#define OTS_TAG_SILE OTS_TAG('S','i','l','e')
+#define OTS_TAG_SILF OTS_TAG('S','i','l','f')
+#define OTS_TAG_SILL OTS_TAG('S','i','l','l')
+#define OTS_TAG_VDMX OTS_TAG('V','D','M','X')
+#define OTS_TAG_VHEA OTS_TAG('v','h','e','a')
+#define OTS_TAG_VMTX OTS_TAG('v','m','t','x')
+#define OTS_TAG_VORG OTS_TAG('V','O','R','G')
+
+#define OTS_TAG_AVAR OTS_TAG('a','v','a','r')
+#define OTS_TAG_CVAR OTS_TAG('c','v','a','r')
+#define OTS_TAG_FVAR OTS_TAG('f','v','a','r')
+#define OTS_TAG_GVAR OTS_TAG('g','v','a','r')
+#define OTS_TAG_HVAR OTS_TAG('H','V','A','R')
+#define OTS_TAG_MVAR OTS_TAG('M','V','A','R')
+#define OTS_TAG_VVAR OTS_TAG('V','V','A','R')
+#define OTS_TAG_STAT OTS_TAG('S','T','A','T')
struct Font;
-struct OpenTypeFile;
+struct FontFile;
+struct TableEntry;
+struct Arena;
+
+class Table {
+ public:
+ explicit Table(Font *font, uint32_t tag, uint32_t type)
+ : m_tag(tag),
+ m_type(type),
+ m_font(font),
+ m_shouldSerialize(true) {
+ }
+
+ virtual ~Table() { }
-#define F(name, capname) \
-bool ots_##name##_parse(Font *f, const uint8_t *d, size_t l); \
-bool ots_##name##_should_serialise(Font *f); \
-bool ots_##name##_serialise(OTSStream *s, Font *f); \
-void ots_##name##_reuse(Font *f, Font *o);\
-void ots_##name##_free(Font *f);
-FOR_EACH_TABLE_TYPE
-#undef F
+ virtual bool Parse(const uint8_t *data, size_t length) = 0;
+ virtual bool Serialize(OTSStream *out) = 0;
+ virtual bool ShouldSerialize();
+
+ // Return the tag (table type) this Table was parsed as, to support
+ // "poor man's RTTI" so that we know if we can safely down-cast to
+ // a specific Table subclass. The m_type field is initialized to the
+ // appropriate tag when a subclass is constructed, or to zero for
+ // TablePassthru (indicating unparsed data).
+ uint32_t Type() { return m_type; }
+
+ Font* GetFont() { return m_font; }
+
+ bool Error(const char *format, ...);
+ bool Warning(const char *format, ...);
+ bool Drop(const char *format, ...);
+ bool DropGraphite(const char *format, ...);
+ bool DropVariations(const char *format, ...);
+
+ private:
+ void Message(int level, const char *format, va_list va);
+
+ uint32_t m_tag;
+ uint32_t m_type;
+ Font *m_font;
+ bool m_shouldSerialize;
+};
+
+class TablePassthru : public Table {
+ public:
+ explicit TablePassthru(Font *font, uint32_t tag)
+ : Table(font, tag, 0),
+ m_data(NULL),
+ m_length(0) {
+ }
+
+ bool Parse(const uint8_t *data, size_t length);
+ bool Serialize(OTSStream *out);
+
+ private:
+ const uint8_t *m_data;
+ size_t m_length;
+};
struct Font {
- explicit Font(const OpenTypeFile *f)
+ explicit Font(FontFile *f)
: file(f),
version(0),
num_tables(0),
search_range(0),
entry_selector(0),
- range_shift(0) {
-#define F(name, capname) \
- name = NULL; \
- name##_reused = false;
- FOR_EACH_TABLE_TYPE
-#undef F
+ range_shift(0),
+ dropped_graphite(false),
+ dropped_variations(false) {
}
- ~Font() {
-#define F(name, capname) \
- if (!name##_reused) {\
- ots_##name##_free(this); \
- }
- FOR_EACH_TABLE_TYPE
-#undef F
- }
+ bool ParseTable(const TableEntry& tableinfo, const uint8_t* data,
+ Arena &arena);
+ Table* GetTable(uint32_t tag) const;
+
+ // This checks that the returned Table is actually of the correct subclass
+ // for |tag|, so it can safely be downcast to the corresponding OpenTypeXXXX;
+ // if not (i.e. if the table was treated as Passthru), it will return NULL.
+ Table* GetTypedTable(uint32_t tag) const;
+
+ // Drop all Graphite tables and don't parse new ones.
+ void DropGraphite();
- const OpenTypeFile *file;
+ // Drop all Variations tables and don't parse new ones.
+ void DropVariations();
+
+ FontFile *file;
uint32_t version;
uint16_t num_tables;
uint16_t search_range;
uint16_t entry_selector;
uint16_t range_shift;
+ bool dropped_graphite;
+ bool dropped_variations;
-#define F(name, capname) \
- OpenType##capname *name; \
- bool name##_reused;
-FOR_EACH_TABLE_TYPE
-#undef F
+ private:
+ std::map<uint32_t, Table*> m_tables;
};
-struct OutputTable {
+struct TableEntry {
uint32_t tag;
- size_t offset;
- size_t length;
+ uint32_t offset;
+ uint32_t length;
+ uint32_t uncompressed_length;
uint32_t chksum;
- bool operator<(const OutputTable& other) const {
+ bool operator<(const TableEntry& other) const {
return tag < other.tag;
}
};
-typedef std::map<uint32_t, std::pair<Font*, OutputTable> > TableMap;
+struct FontFile {
+ ~FontFile();
-struct OpenTypeFile {
OTSContext *context;
- TableMap tables;
+ std::map<TableEntry, Table*> tables;
+ std::map<uint32_t, TableEntry> table_entries;
};
} // namespace ots
-#undef FOR_EACH_TABLE_TYPE
-
#endif // OTS_H_