提交 be707384 编写于 作者: B Behdad Esfahbod

Move sanitizer code around a bit

上级 db5d430e
...@@ -161,7 +161,7 @@ struct LookupFormat0 ...@@ -161,7 +161,7 @@ struct LookupFormat0
inline bool sanitize (hb_sanitize_context_t *c) const inline bool sanitize (hb_sanitize_context_t *c) const
{ {
TRACE_SANITIZE (this); TRACE_SANITIZE (this);
return_trace (arrayZ.sanitize (c, c->num_glyphs)); return_trace (arrayZ.sanitize (c, c->get_num_glyphs ()));
} }
protected: protected:
...@@ -625,7 +625,7 @@ struct hb_aat_apply_context_t : ...@@ -625,7 +625,7 @@ struct hb_aat_apply_context_t :
sanitizer (), lookup_index (0), debug_depth (0) sanitizer (), lookup_index (0), debug_depth (0)
{ {
sanitizer.init (table); sanitizer.init (table);
sanitizer.num_glyphs = face->get_num_glyphs (); sanitizer.set_num_glyphs (face->get_num_glyphs ());
sanitizer.start_processing (); sanitizer.start_processing ();
} }
......
...@@ -182,6 +182,9 @@ struct hb_sanitize_context_t : ...@@ -182,6 +182,9 @@ struct hb_sanitize_context_t :
this->writable = false; this->writable = false;
} }
inline void set_num_glyphs (unsigned int num_glyphs_) { num_glyphs = num_glyphs_; }
inline unsigned int get_num_glyphs (void) { return num_glyphs; }
inline void start_processing (void) inline void start_processing (void)
{ {
this->start = hb_blob_get_data (this->blob, nullptr); this->start = hb_blob_get_data (this->blob, nullptr);
...@@ -275,74 +278,63 @@ struct hb_sanitize_context_t : ...@@ -275,74 +278,63 @@ struct hb_sanitize_context_t :
return false; return false;
} }
mutable unsigned int debug_depth; template <typename Type>
const char *start, *end;
bool writable;
unsigned int edit_count;
mutable int max_ops;
hb_blob_t *blob;
unsigned int num_glyphs;
};
/* Template to sanitize an object. */
template <typename Type>
struct Sanitizer
{
inline Sanitizer (unsigned int num_glyphs = 0) { c->num_glyphs = num_glyphs; }
inline hb_blob_t *sanitize (hb_blob_t *blob) inline hb_blob_t *sanitize (hb_blob_t *blob)
{ {
bool sane; bool sane;
/* TODO is_sane() stuff */ /* TODO is_sane() stuff */
c->init (blob); init (blob);
retry: retry:
DEBUG_MSG_FUNC (SANITIZE, c->start, "start"); DEBUG_MSG_FUNC (SANITIZE, start, "start");
c->start_processing (); start_processing ();
if (unlikely (!c->start)) { if (unlikely (!start))
c->end_processing (); {
end_processing ();
return blob; return blob;
} }
Type *t = CastP<Type> (const_cast<char *> (c->start)); Type *t = CastP<Type> (const_cast<char *> (start));
sane = t->sanitize (c); sane = t->sanitize (this);
if (sane) { if (sane)
if (c->edit_count) { {
DEBUG_MSG_FUNC (SANITIZE, c->start, "passed first round with %d edits; going for second round", c->edit_count); if (edit_count)
{
DEBUG_MSG_FUNC (SANITIZE, start, "passed first round with %d edits; going for second round", edit_count);
/* sanitize again to ensure no toe-stepping */ /* sanitize again to ensure no toe-stepping */
c->edit_count = 0; edit_count = 0;
sane = t->sanitize (c); sane = t->sanitize (this);
if (c->edit_count) { if (edit_count) {
DEBUG_MSG_FUNC (SANITIZE, c->start, "requested %d edits in second round; FAILLING", c->edit_count); DEBUG_MSG_FUNC (SANITIZE, start, "requested %d edits in second round; FAILLING", edit_count);
sane = false; sane = false;
} }
} }
} else { }
unsigned int edit_count = c->edit_count; else
if (edit_count && !c->writable) { {
c->start = hb_blob_get_data_writable (blob, nullptr); if (edit_count && !writable) {
c->end = c->start + blob->length; start = hb_blob_get_data_writable (blob, nullptr);
end = start + blob->length;
if (c->start) {
c->writable = true; if (start)
{
writable = true;
/* ok, we made it writable by relocating. try again */ /* ok, we made it writable by relocating. try again */
DEBUG_MSG_FUNC (SANITIZE, c->start, "retry"); DEBUG_MSG_FUNC (SANITIZE, start, "retry");
goto retry; goto retry;
} }
} }
} }
c->end_processing (); end_processing ();
DEBUG_MSG_FUNC (SANITIZE, c->start, sane ? "PASSED" : "FAILED"); DEBUG_MSG_FUNC (SANITIZE, start, sane ? "PASSED" : "FAILED");
if (sane) if (sane)
{ {
blob->lock (); blob->lock ();
...@@ -355,7 +347,24 @@ struct Sanitizer ...@@ -355,7 +347,24 @@ struct Sanitizer
} }
} }
inline void set_num_glyphs (unsigned int num_glyphs) { c->num_glyphs = num_glyphs; } mutable unsigned int debug_depth;
const char *start, *end;
private:
bool writable;
unsigned int edit_count;
mutable int max_ops;
hb_blob_t *blob;
unsigned int num_glyphs;
};
/* Template to sanitize an object. */
template <typename Type>
struct Sanitizer
{
inline Sanitizer (unsigned int num_glyphs = 0) { c->set_num_glyphs (num_glyphs); }
inline hb_blob_t *sanitize (hb_blob_t *blob) { return c->sanitize<Type> (blob); }
private: private:
hb_sanitize_context_t c[1]; hb_sanitize_context_t c[1];
......
...@@ -68,7 +68,7 @@ struct SBIXStrike ...@@ -68,7 +68,7 @@ struct SBIXStrike
{ {
TRACE_SANITIZE (this); TRACE_SANITIZE (this);
return_trace (c->check_struct (this) && return_trace (c->check_struct (this) &&
imageOffsetsZ.sanitize_shallow (c, c->num_glyphs + 1)); imageOffsetsZ.sanitize_shallow (c, c->get_num_glyphs () + 1));
} }
protected: protected:
...@@ -96,14 +96,11 @@ struct sbix ...@@ -96,14 +96,11 @@ struct sbix
{ {
inline void init (hb_face_t *face) inline void init (hb_face_t *face)
{ {
num_glyphs = hb_face_get_glyph_count (face); /* XXX Using public API instead of private method to avoid link problem in dump_emoji.
* Kill that! */
OT::Sanitizer<OT::sbix> sanitizer; sbix_blob = OT::Sanitizer<OT::sbix>(hb_face_get_glyph_count (face)/*face->get_num_glyphs ()*/).sanitize (face->reference_table (HB_OT_TAG_sbix));
sanitizer.set_num_glyphs (num_glyphs);
sbix_blob = sanitizer.sanitize (face->reference_table (HB_OT_TAG_sbix));
sbix_len = hb_blob_get_length (sbix_blob); sbix_len = hb_blob_get_length (sbix_blob);
sbix_table = sbix_blob->as<OT::sbix> (); sbix_table = sbix_blob->as<OT::sbix> ();
} }
inline void fini (void) inline void fini (void)
...@@ -134,7 +131,6 @@ struct sbix ...@@ -134,7 +131,6 @@ struct sbix
unsigned int sbix_len; unsigned int sbix_len;
unsigned int num_glyphs; unsigned int num_glyphs;
}; };
protected: protected:
......
...@@ -540,9 +540,6 @@ struct Feature ...@@ -540,9 +540,6 @@ struct Feature
c->try_set (&featureParams, new_offset) && c->try_set (&featureParams, new_offset) &&
!featureParams.sanitize (c, this, closure ? closure->tag : HB_TAG_NONE)) !featureParams.sanitize (c, this, closure ? closure->tag : HB_TAG_NONE))
return_trace (false); return_trace (false);
if (c->edit_count > 1)
c->edit_count--; /* This was a "legitimate" edit; don't contribute to error count. */
} }
return_trace (true); return_trace (true);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册