Coverage Report

Created: 2026-01-17 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ots/src/gdef.cc
Line
Count
Source
1
// Copyright (c) 2011-2017 The OTS Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file.
4
5
#include "gdef.h"
6
7
#include <limits>
8
#include <vector>
9
10
#include "gpos.h"
11
#include "gsub.h"
12
#include "layout.h"
13
#include "maxp.h"
14
#include "variations.h"
15
16
// GDEF - The Glyph Definition Table
17
// http://www.microsoft.com/typography/otspec/gdef.htm
18
19
namespace {
20
21
// The maximum class value in the glyph class definision table.
22
const uint16_t kMaxGlyphClassDefValue = 4;
23
// The maximum format number of caret value tables.
24
const uint16_t kMaxCaretValueFormat = 3;
25
26
}  // namespace
27
28
namespace ots {
29
30
690
bool OpenTypeGDEF::ParseAttachListTable(const uint8_t *data, size_t length) {
31
690
  ots::Buffer subtable(data, length);
32
33
690
  uint16_t offset_coverage = 0;
34
690
  uint16_t glyph_count = 0;
35
690
  if (!subtable.ReadU16(&offset_coverage) ||
36
666
      !subtable.ReadU16(&glyph_count)) {
37
45
    return Error("Failed to read gdef header");
38
45
  }
39
645
  const unsigned attach_points_end =
40
645
      2 * static_cast<unsigned>(glyph_count) + 4;
41
645
  if (attach_points_end > std::numeric_limits<uint16_t>::max()) {
42
39
    return Error("Bad glyph count in gdef");
43
39
  }
44
606
  if (offset_coverage == 0 || offset_coverage >= length ||
45
517
      offset_coverage < attach_points_end) {
46
131
    return Error("Bad coverage offset %d", offset_coverage);
47
131
  }
48
475
  if (glyph_count > this->m_num_glyphs) {
49
69
    return Error("Bad glyph count %u", glyph_count);
50
69
  }
51
52
406
  std::vector<uint16_t> attach_points;
53
406
  attach_points.resize(glyph_count);
54
1.08k
  for (unsigned i = 0; i < glyph_count; ++i) {
55
762
    if (!subtable.ReadU16(&attach_points[i])) {
56
0
      return Error("Can't read attachment point %d", i);
57
0
    }
58
762
    if (attach_points[i] >= length ||
59
722
        attach_points[i] < attach_points_end) {
60
83
      return Error("Bad attachment point %d of %d", i, attach_points[i]);
61
83
    }
62
762
  }
63
64
  // Parse coverage table
65
323
  if (!ots::ParseCoverageTable(GetFont(), data + offset_coverage,
66
323
                               length - offset_coverage, this->m_num_glyphs)) {
67
80
    return Error("Bad coverage table");
68
80
  }
69
70
  // Parse attach point table
71
695
  for (unsigned i = 0; i < attach_points.size(); ++i) {
72
521
    subtable.set_offset(attach_points[i]);
73
521
    uint16_t point_count = 0;
74
521
    if (!subtable.ReadU16(&point_count)) {
75
1
      return Error("Can't read point count %d", i);
76
1
    }
77
520
    if (point_count == 0) {
78
21
      return Error("zero point count %d", i);
79
21
    }
80
499
    uint16_t last_point_index = 0;
81
499
    uint16_t point_index = 0;
82
1.46k
    for (unsigned j = 0; j < point_count; ++j) {
83
1.00k
      if (!subtable.ReadU16(&point_index)) {
84
26
        return Error("Can't read point index %d in point %d", j, i);
85
26
      }
86
      // Contour point indices are in increasing numerical order
87
983
      if (last_point_index != 0 && last_point_index >= point_index) {
88
21
        return Error("bad contour indices: %u >= %u",
89
21
                    last_point_index, point_index);
90
21
      }
91
962
      last_point_index = point_index;
92
962
    }
93
499
  }
94
174
  return true;
95
243
}
96
97
870
bool OpenTypeGDEF::ParseLigCaretListTable(const uint8_t *data, size_t length) {
98
870
  ots::Buffer subtable(data, length);
99
870
  uint16_t offset_coverage = 0;
100
870
  uint16_t lig_glyph_count = 0;
101
870
  if (!subtable.ReadU16(&offset_coverage) ||
102
849
      !subtable.ReadU16(&lig_glyph_count)) {
103
44
    return Error("Can't read caret structure");
104
44
  }
105
826
  const unsigned lig_glyphs_end =
106
826
      2 * static_cast<unsigned>(lig_glyph_count) + 4;
107
826
  if (lig_glyphs_end > std::numeric_limits<uint16_t>::max()) {
108
39
    return Error("Bad caret structure");
109
39
  }
110
787
  if (offset_coverage == 0 || offset_coverage >= length ||
111
677
      offset_coverage < lig_glyphs_end) {
112
169
    return Error("Bad caret coverate offset %d", offset_coverage);
113
169
  }
114
618
  if (lig_glyph_count > this->m_num_glyphs) {
115
52
    return Error("bad ligature glyph count: %u", lig_glyph_count);
116
52
  }
117
118
566
  std::vector<uint16_t> lig_glyphs;
119
566
  lig_glyphs.resize(lig_glyph_count);
120
1.47k
  for (unsigned i = 0; i < lig_glyph_count; ++i) {
121
991
    if (!subtable.ReadU16(&lig_glyphs[i])) {
122
0
      return Error("Can't read ligature glyph location %d", i);
123
0
    }
124
991
    if (lig_glyphs[i] >= length || lig_glyphs[i] < lig_glyphs_end) {
125
84
      return Error("Bad ligature glyph location %d in glyph %d", lig_glyphs[i], i);
126
84
    }
127
991
  }
128
129
  // Parse coverage table
130
482
  if (!ots::ParseCoverageTable(GetFont(), data + offset_coverage,
131
482
                               length - offset_coverage, this->m_num_glyphs)) {
132
57
    return Error("Can't parse caret coverage table");
133
57
  }
134
135
  // Parse ligature glyph table
136
731
  for (unsigned i = 0; i < lig_glyphs.size(); ++i) {
137
602
    subtable.set_offset(lig_glyphs[i]);
138
602
    uint16_t caret_count = 0;
139
602
    if (!subtable.ReadU16(&caret_count)) {
140
1
      return Error("Can't read caret count for glyph %d", i);
141
1
    }
142
601
    if (caret_count == 0) {
143
40
      return Error("bad caret value count: %u", caret_count);
144
40
    }
145
146
561
    std::vector<uint16_t> caret_value_offsets;
147
561
    caret_value_offsets.resize(caret_count);
148
561
    unsigned caret_value_offsets_end = 2 * static_cast<unsigned>(caret_count) + 2;
149
1.80k
    for (unsigned j = 0; j < caret_count; ++j) {
150
1.33k
      if (!subtable.ReadU16(&caret_value_offsets[j])) {
151
1
        return Error("Can't read caret offset %d for glyph %d", j, i);
152
1
      }
153
1.33k
      if (caret_value_offsets[j] >= length || caret_value_offsets[j] < caret_value_offsets_end) {
154
96
        return Error("Bad caret offset %d for caret %d glyph %d", caret_value_offsets[j], j, i);
155
96
      }
156
1.33k
    }
157
158
    // Parse caret values table
159
885
    for (unsigned j = 0; j < caret_count; ++j) {
160
579
      subtable.set_offset(lig_glyphs[i] + caret_value_offsets[j]);
161
579
      uint16_t caret_format = 0;
162
579
      if (!subtable.ReadU16(&caret_format)) {
163
24
        return Error("Can't read caret values table %d in glyph %d", j, i);
164
24
      }
165
555
      if (caret_format == 0 || caret_format > kMaxCaretValueFormat) {
166
82
        return Error("bad caret value format: %u", caret_format);
167
82
      }
168
      // CaretValueFormats contain a 2-byte field which could be
169
      // arbitrary value.
170
473
      if (!subtable.Skip(2)) {
171
0
        return Error("Bad caret value table structure %d in glyph %d", j, i);
172
0
      }
173
473
      if (caret_format == 3) {
174
138
        uint16_t offset_device = 0;
175
138
        if (!subtable.ReadU16(&offset_device)) {
176
0
          return Error("Can't read device offset for caret value %d "
177
0
                       "in glyph %d", j, i);
178
0
        }
179
138
        size_t absolute_offset = lig_glyphs[i] + caret_value_offsets[j]
180
138
                                 + offset_device;
181
138
        if (offset_device == 0 || absolute_offset >= length) {
182
29
          return Error("Bad device offset for caret value %d in glyph %d: %d",
183
29
                       j, i, offset_device);
184
29
        }
185
109
        if (!ots::ParseDeviceTable(GetFont(), data + absolute_offset,
186
109
                                   length - absolute_offset)) {
187
23
          return Error("Bad device table for caret value %d in glyph %d",
188
23
                       j, i, offset_device);
189
23
        }
190
109
      }
191
473
    }
192
464
  }
193
129
  return true;
194
425
}
195
196
565
bool OpenTypeGDEF::ParseMarkGlyphSetsDefTable(const uint8_t *data, size_t length) {
197
565
  ots::Buffer subtable(data, length);
198
565
  uint16_t format = 0;
199
565
  uint16_t mark_set_count = 0;
200
565
  if (!subtable.ReadU16(&format) ||
201
564
      !subtable.ReadU16(&mark_set_count)) {
202
24
    return Error("Can' read mark glyph table structure");
203
24
  }
204
541
  if (format != 1) {
205
48
    return Error("bad mark glyph set table format: %u", format);
206
48
  }
207
208
493
  const unsigned mark_sets_end = 2 * static_cast<unsigned>(mark_set_count) + 4;
209
493
  if (mark_sets_end > std::numeric_limits<uint16_t>::max()) {
210
43
    return Error("Bad mark_set %d", mark_sets_end);
211
43
  }
212
1.98k
  for (unsigned i = 0; i < mark_set_count; ++i) {
213
1.66k
    uint32_t offset_coverage = 0;
214
1.66k
    if (!subtable.ReadU32(&offset_coverage)) {
215
4
      return Error("Can't read covrage location for mark set %d", i);
216
4
    }
217
1.65k
    if (offset_coverage >= length ||
218
1.59k
        offset_coverage < mark_sets_end) {
219
89
      return Error("Bad coverage location %d for mark set %d", offset_coverage, i);
220
89
    }
221
1.57k
    if (!ots::ParseCoverageTable(GetFont(), data + offset_coverage,
222
1.57k
                                 length - offset_coverage, this->m_num_glyphs)) {
223
39
      return Error("Failed to parse coverage table for mark set %d", i);
224
39
    }
225
1.57k
  }
226
318
  this->num_mark_glyph_sets = mark_set_count;
227
318
  return true;
228
450
}
229
230
4.93k
bool OpenTypeGDEF::Parse(const uint8_t *data, size_t length) {
231
4.93k
  OpenTypeMAXP *maxp = static_cast<OpenTypeMAXP*>(
232
4.93k
      GetFont()->GetTypedTable(OTS_TAG_MAXP));
233
234
  // Grab the number of glyphs in the font from the maxp table to check
235
  // GlyphIDs in GDEF table.
236
4.93k
  if (!maxp) {
237
0
    return Error("No maxp table in font, needed by GDEF");
238
0
  }
239
4.93k
  this->m_num_glyphs = maxp->num_glyphs;
240
241
4.93k
  Buffer table(data, length);
242
243
4.93k
  uint16_t version_major = 0, version_minor = 0;
244
4.93k
  if (!table.ReadU16(&version_major) ||
245
4.91k
      !table.ReadU16(&version_minor)) {
246
18
    return Error("Incomplete table");
247
18
  }
248
4.91k
  if (version_major != 1 || version_minor == 1) { // there is no v1.1
249
102
    return Error("Bad version");
250
102
  }
251
252
4.81k
  uint16_t offset_glyph_class_def = 0;
253
4.81k
  uint16_t offset_attach_list = 0;
254
4.81k
  uint16_t offset_lig_caret_list = 0;
255
4.81k
  uint16_t offset_mark_attach_class_def = 0;
256
4.81k
  if (!table.ReadU16(&offset_glyph_class_def) ||
257
4.80k
      !table.ReadU16(&offset_attach_list) ||
258
4.80k
      !table.ReadU16(&offset_lig_caret_list) ||
259
4.80k
      !table.ReadU16(&offset_mark_attach_class_def)) {
260
6
    return Error("Incomplete table");
261
6
  }
262
4.80k
  uint16_t offset_mark_glyph_sets_def = 0;
263
4.80k
  if (version_minor >= 2) {
264
3.28k
    if (!table.ReadU16(&offset_mark_glyph_sets_def)) {
265
2
      return Error("Incomplete table");
266
2
    }
267
3.28k
  }
268
4.80k
  uint32_t item_var_store_offset = 0;
269
4.80k
  if (version_minor >= 3) {
270
3.11k
    if (!table.ReadU32(&item_var_store_offset)) {
271
21
      return Error("Incomplete table");
272
21
    }
273
3.11k
  }
274
275
4.78k
  unsigned gdef_header_end = 4 + 4 * 2;
276
4.78k
  if (version_minor >= 2)
277
3.26k
    gdef_header_end += 2;
278
4.78k
  if (version_minor >= 3)
279
3.09k
    gdef_header_end += 4;
280
281
  // Parse subtables
282
4.78k
  if (offset_glyph_class_def) {
283
1.38k
    if (offset_glyph_class_def >= length ||
284
1.33k
        offset_glyph_class_def < gdef_header_end) {
285
100
      return Error("Invalid offset to glyph classes");
286
100
    }
287
1.28k
    if (!ots::ParseClassDefTable(GetFont(), data + offset_glyph_class_def,
288
1.28k
                                 length - offset_glyph_class_def,
289
1.28k
                                 this->m_num_glyphs, kMaxGlyphClassDefValue)) {
290
218
      return Error("Invalid glyph classes");
291
218
    }
292
1.28k
  }
293
294
4.46k
  if (offset_attach_list) {
295
789
    if (offset_attach_list >= length ||
296
729
        offset_attach_list < gdef_header_end) {
297
99
      return Error("Invalid offset to attachment list");
298
99
    }
299
690
    if (!ParseAttachListTable(data + offset_attach_list,
300
690
                              length - offset_attach_list)) {
301
516
      return Error("Invalid attachment list");
302
516
    }
303
690
  }
304
305
3.85k
  if (offset_lig_caret_list) {
306
989
    if (offset_lig_caret_list >= length ||
307
914
        offset_lig_caret_list < gdef_header_end) {
308
119
      return Error("Invalid offset to ligature caret list");
309
119
    }
310
870
    if (!ParseLigCaretListTable(data + offset_lig_caret_list,
311
870
                                length - offset_lig_caret_list)) {
312
741
      return Error("Invalid ligature caret list");
313
741
    }
314
870
  }
315
316
2.99k
  if (offset_mark_attach_class_def) {
317
574
    if (offset_mark_attach_class_def >= length ||
318
526
        offset_mark_attach_class_def < gdef_header_end) {
319
92
      return Error("Invalid offset to mark attachment list");
320
92
    }
321
482
    if (!ots::ParseClassDefTable(GetFont(),
322
482
                                 data + offset_mark_attach_class_def,
323
482
                                 length - offset_mark_attach_class_def,
324
482
                                 this->m_num_glyphs, kMaxClassDefValue)) {
325
53
      return Error("Invalid mark attachment list");
326
53
    }
327
482
  }
328
329
2.84k
  if (offset_mark_glyph_sets_def) {
330
659
    if (offset_mark_glyph_sets_def >= length ||
331
607
        offset_mark_glyph_sets_def < gdef_header_end) {
332
94
      return Error("invalid offset to mark glyph sets");
333
94
    }
334
565
    if (!ParseMarkGlyphSetsDefTable(data + offset_mark_glyph_sets_def,
335
565
                                    length - offset_mark_glyph_sets_def)) {
336
247
      return Error("Invalid mark glyph sets");
337
247
    }
338
565
  }
339
340
2.50k
  if (item_var_store_offset) {
341
326
    if (item_var_store_offset >= length ||
342
266
        item_var_store_offset < gdef_header_end) {
343
95
      return Error("invalid offset to item variation store");
344
95
    }
345
231
    if (!ParseItemVariationStore(GetFont(), data + item_var_store_offset,
346
231
                                 length - item_var_store_offset)) {
347
179
      return Error("Invalid item variation store");
348
179
    }
349
231
  }
350
351
2.23k
  this->m_data = data;
352
2.23k
  this->m_length = length;
353
2.23k
  return true;
354
2.50k
}
355
356
1.65k
bool OpenTypeGDEF::Serialize(OTSStream *out) {
357
1.65k
  if (!out->Write(this->m_data, this->m_length)) {
358
0
    return Error("Failed to write table");
359
0
  }
360
361
1.65k
  return true;
362
1.65k
}
363
364
}  // namespace ots