mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-23 16:01:07 +01:00
[cmap/format4] Try to ignore wrong subtable length (#5740)
* WIP [cmap/format4] Try to ignore wrong subtable length Fixes https://github.com/harfbuzz/harfbuzz/issues/5739 See comments. * [cmap/format4] Followup fix for unreliable subtable length field * [cmap/format4] Try to fix previous commit Let's see.. Let's see.. * [cmap/format4] Another try at fixing recent changes * [cmap/format4] Still try to fix * [cmap/format4] Refactor duplicated code and disable unsafe path
This commit is contained in:
1 parent
7ed158f930
commit
2deabfbc90
1 file changed
+118
-38
+118
-38
@@ -237,8 +237,6 @@ struct CmapSubtableFormat0
|
||||
|
||||
struct CmapSubtableFormat4
|
||||
{
|
||||
|
||||
|
||||
template<typename Iterator,
|
||||
typename Writer,
|
||||
hb_requires (hb_is_iterator (Iterator))>
|
||||
@@ -521,9 +519,10 @@ struct CmapSubtableFormat4
|
||||
struct accelerator_t
|
||||
{
|
||||
accelerator_t () {}
|
||||
accelerator_t (const CmapSubtableFormat4 *subtable) { init (subtable); }
|
||||
accelerator_t (const CmapSubtableFormat4 *subtable) = delete;
|
||||
|
||||
void init (const CmapSubtableFormat4 *subtable)
|
||||
void init (const CmapSubtableFormat4 *subtable,
|
||||
unsigned int subtable_data_size)
|
||||
{
|
||||
segCount = subtable->segCountX2 / 2;
|
||||
endCount = subtable->values.arrayZ;
|
||||
@@ -531,7 +530,11 @@ struct CmapSubtableFormat4
|
||||
idDelta = startCount + segCount;
|
||||
idRangeOffset = idDelta + segCount;
|
||||
glyphIdArray = idRangeOffset + segCount;
|
||||
glyphIdArrayLength = (subtable->length - 16 - 8 * segCount) / 2;
|
||||
|
||||
unsigned int values_offset = 16 + 8 * segCount;
|
||||
glyphIdArrayLength = subtable_data_size > values_offset
|
||||
? (subtable_data_size - values_offset) / 2
|
||||
: 0;
|
||||
}
|
||||
|
||||
bool get_glyph (hb_codepoint_t codepoint, hb_codepoint_t *glyph) const
|
||||
@@ -671,23 +674,36 @@ struct CmapSubtableFormat4
|
||||
unsigned int glyphIdArrayLength;
|
||||
};
|
||||
|
||||
bool get_glyph (hb_codepoint_t codepoint, hb_codepoint_t *glyph) const
|
||||
bool get_glyph (hb_codepoint_t codepoint, hb_codepoint_t *glyph,
|
||||
unsigned int subtable_data_size) const
|
||||
{
|
||||
accelerator_t accel (this);
|
||||
accelerator_t accel;
|
||||
accel.init (this, subtable_data_size);
|
||||
return accel.get_glyph_func (&accel, codepoint, glyph);
|
||||
}
|
||||
void collect_unicodes (hb_set_t *out) const
|
||||
bool get_glyph (hb_codepoint_t codepoint, hb_codepoint_t *glyph) const
|
||||
{ return false; }
|
||||
|
||||
void collect_unicodes (hb_set_t *out, unsigned int subtable_data_size) const
|
||||
{
|
||||
accelerator_t accel (this);
|
||||
accelerator_t accel;
|
||||
accel.init (this, subtable_data_size);
|
||||
accel.collect_unicodes (out);
|
||||
}
|
||||
void collect_unicodes (hb_set_t *out) const
|
||||
{ collect_unicodes (out, length); }
|
||||
|
||||
void collect_mapping (hb_set_t *unicodes, /* OUT */
|
||||
hb_map_t *mapping /* OUT */) const
|
||||
hb_map_t *mapping, /* OUT */
|
||||
unsigned int subtable_data_size) const
|
||||
{
|
||||
accelerator_t accel (this);
|
||||
accelerator_t accel;
|
||||
accel.init (this, subtable_data_size);
|
||||
accel.collect_mapping (unicodes, mapping);
|
||||
}
|
||||
void collect_mapping (hb_set_t *unicodes, /* OUT */
|
||||
hb_map_t *mapping /* OUT */) const
|
||||
{ collect_mapping (unicodes, mapping, length); }
|
||||
|
||||
bool sanitize (hb_sanitize_context_t *c) const
|
||||
{
|
||||
@@ -696,14 +712,9 @@ struct CmapSubtableFormat4
|
||||
return_trace (false);
|
||||
hb_barrier ();
|
||||
|
||||
if (unlikely (!c->check_range (this, length)))
|
||||
return_trace (false);
|
||||
|
||||
return_trace (16 + 4 * (unsigned int) segCountX2 <= length);
|
||||
return_trace (c->check_range (values, 2 + 4 * segCountX2));
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
HBUINT16 format; /* Format number is set to 4. */
|
||||
HBUINT16 length; /* This is the length in bytes of the
|
||||
@@ -1485,11 +1496,14 @@ struct CmapSubtable
|
||||
/* Note: We intentionally do NOT implement subtable formats 2 and 8. */
|
||||
|
||||
bool get_glyph (hb_codepoint_t codepoint,
|
||||
hb_codepoint_t *glyph) const
|
||||
hb_codepoint_t *glyph,
|
||||
unsigned int subtable_data_size = 0) const
|
||||
{
|
||||
switch (u.format.v) {
|
||||
case 0: hb_barrier (); return u.format0 .get_glyph (codepoint, glyph);
|
||||
case 4: hb_barrier (); return u.format4 .get_glyph (codepoint, glyph);
|
||||
case 4: hb_barrier (); return subtable_data_size
|
||||
? u.format4.get_glyph (codepoint, glyph, subtable_data_size)
|
||||
: false;
|
||||
case 6: hb_barrier (); return u.format6 .get_glyph (codepoint, glyph);
|
||||
case 10: hb_barrier (); return u.format10.get_glyph (codepoint, glyph);
|
||||
case 12: hb_barrier (); return u.format12.get_glyph (codepoint, glyph);
|
||||
@@ -1498,11 +1512,17 @@ struct CmapSubtable
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
void collect_unicodes (hb_set_t *out, unsigned int num_glyphs = UINT_MAX) const
|
||||
void collect_unicodes (hb_set_t *out,
|
||||
unsigned int num_glyphs = UINT_MAX,
|
||||
unsigned int subtable_data_size = 0) const
|
||||
{
|
||||
switch (u.format.v) {
|
||||
case 0: hb_barrier (); u.format0 .collect_unicodes (out); return;
|
||||
case 4: hb_barrier (); u.format4 .collect_unicodes (out); return;
|
||||
case 4: hb_barrier (); if (subtable_data_size)
|
||||
u.format4.collect_unicodes (out, subtable_data_size);
|
||||
else
|
||||
u.format4.collect_unicodes (out);
|
||||
return;
|
||||
case 6: hb_barrier (); u.format6 .collect_unicodes (out); return;
|
||||
case 10: hb_barrier (); u.format10.collect_unicodes (out); return;
|
||||
case 12: hb_barrier (); u.format12.collect_unicodes (out, num_glyphs); return;
|
||||
@@ -1514,11 +1534,16 @@ struct CmapSubtable
|
||||
|
||||
void collect_mapping (hb_set_t *unicodes, /* OUT */
|
||||
hb_map_t *mapping, /* OUT */
|
||||
unsigned num_glyphs = UINT_MAX) const
|
||||
unsigned num_glyphs = UINT_MAX,
|
||||
unsigned int subtable_data_size = 0) const
|
||||
{
|
||||
switch (u.format.v) {
|
||||
case 0: hb_barrier (); u.format0 .collect_mapping (unicodes, mapping); return;
|
||||
case 4: hb_barrier (); u.format4 .collect_mapping (unicodes, mapping); return;
|
||||
case 4: hb_barrier (); if (subtable_data_size)
|
||||
u.format4.collect_mapping (unicodes, mapping, subtable_data_size);
|
||||
else
|
||||
u.format4.collect_mapping (unicodes, mapping);
|
||||
return;
|
||||
case 6: hb_barrier (); u.format6 .collect_mapping (unicodes, mapping); return;
|
||||
case 10: hb_barrier (); u.format10.collect_mapping (unicodes, mapping); return;
|
||||
case 12: hb_barrier (); u.format12.collect_mapping (unicodes, mapping, num_glyphs); return;
|
||||
@@ -1662,6 +1687,7 @@ struct SubtableUnicodesCache {
|
||||
private:
|
||||
hb_blob_ptr_t<cmap> base_blob;
|
||||
const char* base;
|
||||
unsigned int table_length;
|
||||
hb_hashmap_t<unsigned, hb::unique_ptr<hb_set_t>> cached_unicodes;
|
||||
|
||||
public:
|
||||
@@ -1686,15 +1712,18 @@ struct SubtableUnicodesCache {
|
||||
hb_free (cache);
|
||||
}
|
||||
|
||||
SubtableUnicodesCache(const void* cmap_base)
|
||||
SubtableUnicodesCache(const void* cmap_base,
|
||||
unsigned int table_length_ = 0)
|
||||
: base_blob(),
|
||||
base ((const char*) cmap_base),
|
||||
table_length (table_length_),
|
||||
cached_unicodes ()
|
||||
{}
|
||||
|
||||
SubtableUnicodesCache(hb_blob_ptr_t<cmap> base_blob_)
|
||||
: base_blob(base_blob_),
|
||||
base ((const char *) base_blob.get()),
|
||||
table_length (base_blob.get_length ()),
|
||||
cached_unicodes ()
|
||||
{}
|
||||
|
||||
@@ -1725,7 +1754,10 @@ struct SubtableUnicodesCache {
|
||||
if (unlikely (s->in_error ()))
|
||||
return hb_set_get_empty ();
|
||||
|
||||
(base+record->subtable).collect_unicodes (s);
|
||||
unsigned int subtable_data_size = record->subtable < table_length
|
||||
? table_length - (unsigned int) record->subtable
|
||||
: 0;
|
||||
(base+record->subtable).collect_unicodes (s, UINT_MAX, subtable_data_size);
|
||||
|
||||
if (unlikely (!cached_unicodes.set ((unsigned) ((const char *) record - base), hb::unique_ptr<hb_set_t> {s})))
|
||||
return hb_set_get_empty ();
|
||||
@@ -1784,7 +1816,8 @@ struct cmap
|
||||
EncodingRecIter encodingrec_iter,
|
||||
const void *base,
|
||||
hb_subset_plan_t *plan,
|
||||
bool drop_format_4 = false)
|
||||
bool drop_format_4 = false,
|
||||
unsigned int source_table_length = 0)
|
||||
{
|
||||
if (unlikely (!c->extend_min ((*this)))) return false;
|
||||
this->version = 0;
|
||||
@@ -1792,7 +1825,7 @@ struct cmap
|
||||
unsigned format4objidx = 0, format12objidx = 0, format14objidx = 0;
|
||||
auto snap = c->snapshot ();
|
||||
|
||||
SubtableUnicodesCache local_unicodes_cache (base);
|
||||
SubtableUnicodesCache local_unicodes_cache (base, source_table_length);
|
||||
const SubtableUnicodesCache* unicodes_cache = &local_unicodes_cache;
|
||||
|
||||
if (plan->accelerator &&
|
||||
@@ -1821,7 +1854,8 @@ struct cmap
|
||||
encodingrec_iter,
|
||||
base,
|
||||
plan,
|
||||
true);
|
||||
true,
|
||||
source_table_length);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1955,7 +1989,9 @@ struct cmap
|
||||
it,
|
||||
encodingrec_iter,
|
||||
this,
|
||||
c->plan));
|
||||
c->plan,
|
||||
false,
|
||||
c->source_blob->length));
|
||||
}
|
||||
|
||||
const CmapSubtable *find_best_subtable (bool *symbol = nullptr,
|
||||
@@ -2034,32 +2070,60 @@ struct cmap
|
||||
|
||||
this->get_glyph_data = subtable;
|
||||
#ifndef HB_NO_CMAP_LEGACY_SUBTABLES
|
||||
bool is_format4 = subtable->u.format.v == 4;
|
||||
auto set_format4_getter = [this] (bool (*func) (const void *,
|
||||
hb_codepoint_t,
|
||||
hb_codepoint_t *))
|
||||
{
|
||||
this->format4_accel.init (&this->subtable->u.format4,
|
||||
get_subtable_data_size (this->subtable));
|
||||
this->get_glyph_data = &this->format4_accel;
|
||||
this->get_glyph_funcZ = func;
|
||||
};
|
||||
if (unlikely (symbol))
|
||||
{
|
||||
switch ((unsigned) face->table.OS2->get_font_page ()) {
|
||||
case OS2::font_page_t::FONT_PAGE_NONE:
|
||||
this->get_glyph_funcZ = get_glyph_from_symbol<CmapSubtable, _hb_symbol_pua_map>;
|
||||
if (is_format4)
|
||||
set_format4_getter (get_glyph_from_symbol<CmapSubtableFormat4::accelerator_t, _hb_symbol_pua_map>);
|
||||
else
|
||||
this->get_glyph_funcZ = get_glyph_from_symbol<CmapSubtable, _hb_symbol_pua_map>;
|
||||
break;
|
||||
#ifndef HB_NO_OT_SHAPER_ARABIC_FALLBACK
|
||||
case OS2::font_page_t::FONT_PAGE_SIMP_ARABIC:
|
||||
this->get_glyph_funcZ = get_glyph_from_symbol<CmapSubtable, _hb_arabic_pua_simp_map>;
|
||||
if (is_format4)
|
||||
set_format4_getter (get_glyph_from_symbol<CmapSubtableFormat4::accelerator_t, _hb_arabic_pua_simp_map>);
|
||||
else
|
||||
this->get_glyph_funcZ = get_glyph_from_symbol<CmapSubtable, _hb_arabic_pua_simp_map>;
|
||||
break;
|
||||
case OS2::font_page_t::FONT_PAGE_TRAD_ARABIC:
|
||||
this->get_glyph_funcZ = get_glyph_from_symbol<CmapSubtable, _hb_arabic_pua_trad_map>;
|
||||
if (is_format4)
|
||||
set_format4_getter (get_glyph_from_symbol<CmapSubtableFormat4::accelerator_t, _hb_arabic_pua_trad_map>);
|
||||
else
|
||||
this->get_glyph_funcZ = get_glyph_from_symbol<CmapSubtable, _hb_arabic_pua_trad_map>;
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
this->get_glyph_funcZ = get_glyph_from<CmapSubtable>;
|
||||
if (is_format4)
|
||||
set_format4_getter (get_glyph_from<CmapSubtableFormat4::accelerator_t>);
|
||||
else
|
||||
this->get_glyph_funcZ = get_glyph_from<CmapSubtable>;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (unlikely (macroman))
|
||||
{
|
||||
this->get_glyph_funcZ = get_glyph_from_macroman<CmapSubtable>;
|
||||
if (is_format4)
|
||||
set_format4_getter (get_glyph_from_macroman<CmapSubtableFormat4::accelerator_t>);
|
||||
else
|
||||
this->get_glyph_funcZ = get_glyph_from_macroman<CmapSubtable>;
|
||||
}
|
||||
else if (unlikely (mac))
|
||||
{
|
||||
this->get_glyph_funcZ = get_glyph_from_ascii<CmapSubtable>;
|
||||
if (is_format4)
|
||||
set_format4_getter (get_glyph_from_ascii<CmapSubtableFormat4::accelerator_t>);
|
||||
else
|
||||
this->get_glyph_funcZ = get_glyph_from_ascii<CmapSubtable>;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
@@ -2074,7 +2138,8 @@ struct cmap
|
||||
break;
|
||||
case 4:
|
||||
{
|
||||
this->format4_accel.init (&subtable->u.format4);
|
||||
this->format4_accel.init (&subtable->u.format4,
|
||||
get_subtable_data_size (subtable));
|
||||
this->get_glyph_data = &this->format4_accel;
|
||||
this->get_glyph_funcZ = this->format4_accel.get_glyph_func;
|
||||
break;
|
||||
@@ -2155,10 +2220,10 @@ struct cmap
|
||||
}
|
||||
|
||||
void collect_unicodes (hb_set_t *out, unsigned int num_glyphs) const
|
||||
{ subtable->collect_unicodes (out, num_glyphs); }
|
||||
{ subtable->collect_unicodes (out, num_glyphs, get_subtable_data_size (subtable)); }
|
||||
void collect_mapping (hb_set_t *unicodes, hb_map_t *mapping,
|
||||
unsigned num_glyphs = UINT_MAX) const
|
||||
{ subtable->collect_mapping (unicodes, mapping, num_glyphs); }
|
||||
{ subtable->collect_mapping (unicodes, mapping, num_glyphs, get_subtable_data_size (subtable)); }
|
||||
void collect_variation_selectors (hb_set_t *out) const
|
||||
{ subtable_uvs->collect_variation_selectors (out); }
|
||||
void collect_variation_unicodes (hb_codepoint_t variation_selector,
|
||||
@@ -2217,6 +2282,21 @@ struct cmap
|
||||
return c && typed_obj->get_glyph (c, glyph);
|
||||
}
|
||||
|
||||
unsigned int get_subtable_data_size (const CmapSubtable *subtable) const
|
||||
{
|
||||
unsigned int table_length = this->table.get_length ();
|
||||
uintptr_t table_start = (uintptr_t) (const void *) this->table.get ();
|
||||
uintptr_t subtable_addr = (uintptr_t) (const void *) subtable;
|
||||
if (unlikely (subtable_addr < table_start))
|
||||
return 0;
|
||||
|
||||
uintptr_t subtable_offset = subtable_addr - table_start;
|
||||
if (unlikely (subtable_offset >= table_length))
|
||||
return 0;
|
||||
|
||||
return table_length - (unsigned int) subtable_offset;
|
||||
}
|
||||
|
||||
private:
|
||||
hb_nonnull_ptr_t<const CmapSubtable> subtable;
|
||||
hb_nonnull_ptr_t<const CmapSubtableFormat14> subtable_uvs;
|
||||
|
||||
Reference in new issue
Block a user