mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-20 06:21:16 +01:00
SingleSubstFormat2 already picks, per closure visit, between walking its whole Coverage and iterating the (usually small) active glyph set. The Multiple/Alternate/Ligature/ReverseChain closure and depend paths, the format-1 (Chain)Context closure, and Coverage::intersect_set lacked that choice, so a nested lookup visited many times over a small active set re-walked a large Coverage every time, giving quadratic GSUB closure (and thus subsetting) time. Add the same get_population()-based fast path to those paths and to CoverageFormat1/2::intersect_set. Output is byte-identical; only the per-visit work is bounded. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-KNHDK379. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
176 lines
4.9 KiB
C++
176 lines
4.9 KiB
C++
#ifndef OT_LAYOUT_GSUB_MULTIPLESUBSTFORMAT1_HH
|
|
#define OT_LAYOUT_GSUB_MULTIPLESUBSTFORMAT1_HH
|
|
|
|
#include "Common.hh"
|
|
#include "Sequence.hh"
|
|
|
|
namespace OT {
|
|
namespace Layout {
|
|
namespace GSUB_impl {
|
|
|
|
template <typename Types>
|
|
struct MultipleSubstFormat1_2
|
|
{
|
|
protected:
|
|
HBUINT16 format; /* Format identifier--format = 1 */
|
|
typename Types::template LOffsetTo<Coverage>
|
|
coverage; /* Offset to Coverage table--from
|
|
* beginning of Substitution table */
|
|
typename Types::template ArrayOf<typename Types::template OffsetTo<Sequence<Types>>>
|
|
sequence; /* Array of Sequence tables
|
|
* ordered by Coverage Index */
|
|
public:
|
|
DEFINE_SIZE_ARRAY (2 + Types::LOffset::static_size + Types::HBUINT::static_size, sequence);
|
|
|
|
bool sanitize (hb_sanitize_context_t *c) const
|
|
{
|
|
TRACE_SANITIZE (this);
|
|
return_trace (coverage.sanitize (c, this) && sequence.sanitize (c, this));
|
|
}
|
|
|
|
bool intersects (const hb_set_t *glyphs) const
|
|
{ return (this+coverage).intersects (glyphs); }
|
|
|
|
bool may_have_non_1to1 () const
|
|
{ return true; }
|
|
|
|
void closure (hb_closure_context_t *c) const
|
|
{
|
|
auto &cov = this+coverage;
|
|
auto &glyph_set = c->parent_active_glyphs ();
|
|
|
|
if (sequence.len > glyph_set.get_population () * 4)
|
|
{
|
|
for (auto g : glyph_set)
|
|
{
|
|
unsigned i = cov.get_coverage (g);
|
|
if (i == NOT_COVERED || i >= sequence.len)
|
|
continue;
|
|
(this+sequence.arrayZ[i]).closure (c);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
+ hb_zip (cov, sequence)
|
|
| hb_filter (glyph_set, hb_first)
|
|
| hb_map (hb_second)
|
|
| hb_map (hb_add (this))
|
|
| hb_apply ([c] (const Sequence<Types> &_) { _.closure (c); })
|
|
;
|
|
}
|
|
|
|
void depend (hb_depend_context_t *c) const
|
|
{
|
|
auto &cov = this+coverage;
|
|
auto &glyph_set = c->parent_active_glyphs ();
|
|
|
|
// Filter by parent_active_glyphs like closure does
|
|
if (sequence.len > glyph_set.get_population () * 4)
|
|
{
|
|
for (auto g : glyph_set)
|
|
{
|
|
unsigned i = cov.get_coverage (g);
|
|
if (i == NOT_COVERED || i >= sequence.len)
|
|
continue;
|
|
(this+sequence.arrayZ[i]).depend (c, g);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
+ hb_zip (cov, sequence)
|
|
| hb_filter (glyph_set, hb_first)
|
|
| hb_apply ([&] (const hb_pair_t<hb_codepoint_t, const typename Types::template OffsetTo<Sequence<Types>>&> &_)
|
|
{
|
|
const Sequence<Types>& s = this+_.second;
|
|
s.depend (c, _.first);
|
|
})
|
|
;
|
|
}
|
|
|
|
void closure_lookups (hb_closure_lookups_context_t *c) const {}
|
|
|
|
void collect_glyphs (hb_collect_glyphs_context_t *c) const
|
|
{
|
|
if (unlikely (!(this+coverage).collect_coverage (c->input))) return;
|
|
+ hb_zip (this+coverage, sequence)
|
|
| hb_map (hb_second)
|
|
| hb_map (hb_add (this))
|
|
| hb_apply ([c] (const Sequence<Types> &_) { _.collect_glyphs (c); })
|
|
;
|
|
}
|
|
|
|
const Coverage &get_coverage () const { return this+coverage; }
|
|
|
|
bool would_apply (hb_would_apply_context_t *c) const
|
|
{ return c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED; }
|
|
|
|
bool apply (hb_ot_apply_context_t *c) const
|
|
{
|
|
TRACE_APPLY (this);
|
|
|
|
unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
|
|
if (index == NOT_COVERED) return_trace (false);
|
|
|
|
return_trace ((this+sequence[index]).apply (c));
|
|
}
|
|
|
|
template<typename Iterator,
|
|
hb_requires (hb_is_sorted_iterator (Iterator))>
|
|
bool serialize (hb_serialize_context_t *c,
|
|
Iterator it)
|
|
{
|
|
TRACE_SERIALIZE (this);
|
|
auto sequences =
|
|
+ it
|
|
| hb_map (hb_second)
|
|
;
|
|
auto glyphs =
|
|
+ it
|
|
| hb_map_retains_sorting (hb_first)
|
|
;
|
|
if (unlikely (!c->extend_min (this))) return_trace (false);
|
|
|
|
if (unlikely (!sequence.serialize (c, sequences.length))) return_trace (false);
|
|
|
|
for (auto& pair : hb_zip (sequences, sequence))
|
|
{
|
|
if (unlikely (!pair.second
|
|
.serialize_serialize (c, pair.first)))
|
|
return_trace (false);
|
|
}
|
|
|
|
return_trace (coverage.serialize_serialize (c, glyphs));
|
|
}
|
|
|
|
bool subset (hb_subset_context_t *c) const
|
|
{
|
|
TRACE_SUBSET (this);
|
|
const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
|
|
const hb_map_t &glyph_map = *c->plan->glyph_map;
|
|
|
|
auto *out = c->serializer->start_embed (*this);
|
|
if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
|
|
out->format = format;
|
|
|
|
hb_sorted_vector_t<hb_codepoint_t> new_coverage;
|
|
+ hb_zip (this+coverage, sequence)
|
|
| hb_filter (glyphset, hb_first)
|
|
| hb_filter (subset_offset_array (c, out->sequence, this), hb_second)
|
|
| hb_map (hb_first)
|
|
| hb_map (glyph_map)
|
|
| hb_sink (new_coverage)
|
|
;
|
|
out->coverage.serialize_serialize (c->serializer, new_coverage.iter ());
|
|
return_trace (bool (new_coverage));
|
|
}
|
|
};
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
#endif /* OT_LAYOUT_GSUB_MULTIPLESUBSTFORMAT1_HH */
|