@@ -10,10 +10,21 @@ namespace SixLabors.Fonts.Tables.AdvancedTypographic
10
10
{
11
11
internal static class AdvancedTypographicUtils
12
12
{
13
- /// <summary>
14
- /// The maximum length of a context. Taken from HarfBuzz - hb-ot-layout-common.hh
15
- /// </summary>
13
+ // The following properties are used to prevent overflows caused
14
+ // by maliciously crafted fonts.
15
+ // Based on HarfBuzz hb-buffer.hh
16
16
public const int MaxContextLength = 64 ;
17
+ private const int MaxLengthFactor = 64 ;
18
+ private const int MaxLengthMinimum = 16384 ;
19
+ private const int MaxOperationsFactor = 1024 ;
20
+ private const int MaxOperationsMinimum = 16384 ;
21
+ private const int MaxShapingCharsLength = 0x3FFFFFFF ; // Half int max.
22
+
23
+ public static int GetMaxAllowableShapingCollectionCount ( int length )
24
+ => ( int ) Math . Min ( Math . Max ( ( long ) length * MaxLengthFactor , MaxLengthMinimum ) , MaxShapingCharsLength ) ;
25
+
26
+ public static int GetMaxAllowableShapingOperationsCount ( int length )
27
+ => ( int ) Math . Min ( Math . Max ( ( long ) length * MaxOperationsFactor , MaxOperationsMinimum ) , MaxShapingCharsLength ) ;
17
28
18
29
public static bool ApplyLookupList (
19
30
FontMetrics fontMetrics ,
@@ -22,7 +33,7 @@ public static bool ApplyLookupList(
22
33
LookupFlags lookupFlags ,
23
34
SequenceLookupRecord [ ] records ,
24
35
GlyphSubstitutionCollection collection ,
25
- ushort index ,
36
+ int index ,
26
37
int count )
27
38
{
28
39
bool hasChanged = false ;
@@ -56,7 +67,7 @@ public static bool ApplyLookupList(
56
67
LookupFlags lookupFlags ,
57
68
SequenceLookupRecord [ ] records ,
58
69
GlyphPositioningCollection collection ,
59
- ushort index ,
70
+ int index ,
60
71
int count )
61
72
{
62
73
bool hasChanged = false ;
@@ -189,7 +200,7 @@ public static bool CheckAllCoverages(
189
200
FontMetrics fontMetrics ,
190
201
LookupFlags lookupFlags ,
191
202
IGlyphShapingCollection collection ,
192
- ushort index ,
203
+ int index ,
193
204
int count ,
194
205
CoverageTable [ ] input ,
195
206
CoverageTable [ ] backtrack ,
@@ -224,7 +235,7 @@ public static bool CheckAllCoverages(
224
235
public static void ApplyAnchor (
225
236
FontMetrics fontMetrics ,
226
237
GlyphPositioningCollection collection ,
227
- ushort index ,
238
+ int index ,
228
239
AnchorTable baseAnchor ,
229
240
MarkRecord markRecord ,
230
241
int baseGlyphIndex )
@@ -242,7 +253,7 @@ public static void ApplyAnchor(
242
253
243
254
public static void ApplyPosition (
244
255
GlyphPositioningCollection collection ,
245
- ushort index ,
256
+ int index ,
246
257
ValueRecord record )
247
258
{
248
259
GlyphShapingData current = collection . GetGlyphShapingData ( index ) ;
@@ -302,8 +313,8 @@ private static bool Match<T>(
302
313
Func < T , GlyphShapingData , bool > condition ,
303
314
Span < int > matches )
304
315
{
305
- ushort position = iterator . Index ;
306
- ushort offset = iterator . Increment ( increment ) ;
316
+ int position = iterator . Index ;
317
+ int offset = iterator . Increment ( increment ) ;
307
318
IGlyphShapingCollection collection = iterator . Collection ;
308
319
309
320
int i = 0 ;
0 commit comments