blob: 654d82fd40c1b62032823b1bccfce685c1823f37 [file] [log] [blame]
bashi@google.com00b790a2011-01-27 06:35:42 +00001// Copyright (c) 2011 The Chromium 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 "layout.h"
6
bashi@google.com78a8baa2011-02-07 06:21:25 +00007#include <limits>
8#include <vector>
9
10#include "gdef.h"
11
bashi@google.com00b790a2011-01-27 06:35:42 +000012// OpenType Layout Common Table Formats
13// http://www.microsoft.com/typography/otspec/chapter2.htm
14
15namespace {
16
bashi@google.com78a8baa2011-02-07 06:21:25 +000017// The 'DFLT' tag of script table.
18const uint32_t kScriptTableTagDflt = 0x44464c54;
19// The value which represents there is no required feature index.
20const uint16_t kNoRequiredFeatureIndexDefined = 0xFFFF;
21// The lookup flag bit which indicates existence of MarkFilteringSet.
22const uint16_t kUseMarkFilteringSetBit = 0x0010;
23// The mask for MarkAttachmentType.
24const uint16_t kMarkAttachmentTypeMask = 0xFF00;
25// The maximum type number of format for device tables.
26const uint16_t kMaxDeltaFormatType = 3;
bashi@chromium.orgced71122011-02-17 10:23:47 +000027// The maximum number of class value.
28const uint16_t kMaxClassDefValue = 0xFFFF;
bashi@google.com78a8baa2011-02-07 06:21:25 +000029
30struct ScriptRecord {
31 uint32_t tag;
32 uint16_t offset;
33};
34
35struct LangSysRecord {
36 uint32_t tag;
37 uint16_t offset;
38};
39
40struct FeatureRecord {
41 uint32_t tag;
42 uint16_t offset;
43};
44
45bool ParseLangSysTable(ots::Buffer *subtable, const uint32_t tag,
46 const uint16_t num_features) {
47 uint16_t offset_lookup_order = 0;
48 uint16_t req_feature_index = 0;
49 uint16_t feature_count = 0;
50 if (!subtable->ReadU16(&offset_lookup_order) ||
51 !subtable->ReadU16(&req_feature_index) ||
52 !subtable->ReadU16(&feature_count)) {
53 return OTS_FAILURE();
54 }
55 // |offset_lookup_order| is reserved and should be NULL.
56 if (offset_lookup_order != 0) {
57 return OTS_FAILURE();
58 }
59 if (req_feature_index != kNoRequiredFeatureIndexDefined &&
60 req_feature_index >= num_features) {
61 return OTS_FAILURE();
62 }
63 if (feature_count > num_features) {
64 return OTS_FAILURE();
65 }
66
67 for (unsigned i = 0; i < feature_count; ++i) {
68 uint16_t feature_index = 0;
69 if (!subtable->ReadU16(&feature_index)) {
70 return OTS_FAILURE();
71 }
72 if (feature_index >= num_features) {
73 return OTS_FAILURE();
74 }
75 }
76 return true;
77}
78
79bool ParseScriptTable(const uint8_t *data, const size_t length,
80 const uint32_t tag, const uint16_t num_features) {
81 ots::Buffer subtable(data, length);
82
83 uint16_t offset_default_lang_sys = 0;
84 uint16_t lang_sys_count = 0;
85 if (!subtable.ReadU16(&offset_default_lang_sys) ||
86 !subtable.ReadU16(&lang_sys_count)) {
87 return OTS_FAILURE();
88 }
89
90 // The spec requires a script table for 'DFLT' tag must contain non-NULL
91 // |offset_default_lang_sys| and |lang_sys_count| == 0
92 if (tag == kScriptTableTagDflt &&
93 (offset_default_lang_sys == 0 || lang_sys_count != 0)) {
94 OTS_WARNING("DFLT table doesn't satisfy the spec.");
95 return OTS_FAILURE();
96 }
97
98 const unsigned lang_sys_record_end = static_cast<unsigned>(4) +
99 lang_sys_count * 6;
100 if (lang_sys_record_end > std::numeric_limits<uint16_t>::max()) {
101 return OTS_FAILURE();
102 }
103
104 std::vector<LangSysRecord> lang_sys_records;
105 lang_sys_records.resize(lang_sys_count);
106 uint32_t last_tag = 0;
107 for (unsigned i = 0; i < lang_sys_count; ++i) {
108 if (!subtable.ReadU32(&lang_sys_records[i].tag) ||
109 !subtable.ReadU16(&lang_sys_records[i].offset)) {
110 return OTS_FAILURE();
111 }
112 // The record array must store the records alphabetically by tag
113 if (last_tag != 0 && last_tag > lang_sys_records[i].tag) {
114 return OTS_FAILURE();
115 }
116 if (lang_sys_records[i].offset < lang_sys_record_end ||
117 lang_sys_records[i].offset >= length) {
118 return OTS_FAILURE();
119 }
120 last_tag = lang_sys_records[i].tag;
121 }
122
123 // Check lang sys tables
124 for (unsigned i = 0; i < lang_sys_count; ++i) {
125 subtable.set_offset(lang_sys_records[i].offset);
126 if (!ParseLangSysTable(&subtable, lang_sys_records[i].tag, num_features)) {
127 return OTS_FAILURE();
128 }
129 }
130
131 return true;
132}
133
134bool ParseFeatureTable(const uint8_t *data, const size_t length,
135 const uint16_t num_lookups) {
136 ots::Buffer subtable(data, length);
137
138 uint16_t offset_feature_params = 0;
139 uint16_t lookup_count = 0;
140 if (!subtable.ReadU16(&offset_feature_params) ||
141 !subtable.ReadU16(&lookup_count)) {
142 return OTS_FAILURE();
143 }
144
145 const unsigned feature_table_end = static_cast<unsigned>(4) +
146 num_lookups * 2;
147 if (feature_table_end > std::numeric_limits<uint16_t>::max()) {
148 return OTS_FAILURE();
149 }
150 // |offset_feature_params| is generally set to NULL.
151 if (offset_feature_params != 0 &&
152 (offset_feature_params < feature_table_end ||
153 offset_feature_params >= length)) {
154 return OTS_FAILURE();
155 }
156
157 for (unsigned i = 0; i < lookup_count; ++i) {
158 uint16_t lookup_index = 0;
159 if (!subtable.ReadU16(&lookup_index)) {
160 return OTS_FAILURE();
161 }
162 // lookup index starts with 0.
163 if (lookup_index >= num_lookups) {
164 return OTS_FAILURE();
165 }
166 }
167 return true;
168}
169
bashi@google.com78a8baa2011-02-07 06:21:25 +0000170bool ParseLookupTable(ots::OpenTypeFile *file, const uint8_t *data,
bashi@chromium.orgced71122011-02-17 10:23:47 +0000171 const size_t length,
172 const ots::LookupSubtableParser* parser) {
bashi@google.com78a8baa2011-02-07 06:21:25 +0000173 ots::Buffer subtable(data, length);
174
175 uint16_t lookup_type = 0;
176 uint16_t lookup_flag = 0;
177 uint16_t subtable_count = 0;
178 if (!subtable.ReadU16(&lookup_type) ||
179 !subtable.ReadU16(&lookup_flag) ||
180 !subtable.ReadU16(&subtable_count)) {
181 return OTS_FAILURE();
182 }
183
bashi@chromium.orgced71122011-02-17 10:23:47 +0000184 if (lookup_type == 0 || lookup_type > parser->num_types) {
bashi@google.com78a8baa2011-02-07 06:21:25 +0000185 return OTS_FAILURE();
186 }
187
188 // Check lookup flags.
189 if ((lookup_flag & kMarkAttachmentTypeMask) &&
190 (!file->gdef || !file->gdef->has_mark_attachment_class_def)) {
191 return OTS_FAILURE();
192 }
193 bool use_mark_filtering_set = false;
194 if (lookup_flag & kUseMarkFilteringSetBit) {
195 if (!file->gdef || !file->gdef->has_mark_glyph_sets_def) {
196 return OTS_FAILURE();
197 }
198 use_mark_filtering_set = true;
199 }
200
201 std::vector<uint16_t> subtables;
202 subtables.reserve(subtable_count);
203 // If the |kUseMarkFilteringSetBit| of |lookup_flag| is set,
204 // extra 2 bytes will follow after subtable offset array.
205 const unsigned lookup_table_end =
206 static_cast<unsigned>(use_mark_filtering_set ? 8 : 6) +
207 subtable_count * 2;
208 if (lookup_table_end > std::numeric_limits<uint16_t>::max()) {
209 return OTS_FAILURE();
210 }
211 for (unsigned i = 0; i < subtable_count; ++i) {
212 if (!subtable.ReadU16(&subtables[i])) {
213 return OTS_FAILURE();
214 }
215 if (subtables[i] < lookup_table_end || subtables[i] >= length) {
216 return OTS_FAILURE();
217 }
218 }
219
220 if (use_mark_filtering_set) {
221 uint16_t mark_filtering_set = 0;
222 if (!subtable.ReadU16(&mark_filtering_set)) {
223 return OTS_FAILURE();
224 }
225 if (file->gdef->num_mark_glyph_sets == 0 ||
226 mark_filtering_set >= file->gdef->num_mark_glyph_sets) {
227 return OTS_FAILURE();
228 }
229 }
230
231 // Parse lookup subtables for this lookup type.
232 for (unsigned i = 0; i < subtable_count; ++i) {
bashi@chromium.orgced71122011-02-17 10:23:47 +0000233 if (!parser->Parse(file, data + subtables[i], length - subtables[i],
234 lookup_type)) {
bashi@google.com78a8baa2011-02-07 06:21:25 +0000235 return OTS_FAILURE();
236 }
237 }
238 return true;
239}
240
bashi@google.com00b790a2011-01-27 06:35:42 +0000241bool ParseClassDefFormat1(const uint8_t *data, size_t length,
242 const uint16_t num_glyphs,
243 const uint16_t num_classes) {
244 ots::Buffer subtable(data, length);
245
246 // Skip format field.
247 if (!subtable.Skip(2)) {
248 return OTS_FAILURE();
249 }
250
251 uint16_t start_glyph = 0;
252 if (!subtable.ReadU16(&start_glyph)) {
253 return OTS_FAILURE();
254 }
255 if (start_glyph > num_glyphs) {
256 OTS_WARNING("bad start glyph ID: %u", start_glyph);
257 return OTS_FAILURE();
258 }
259
260 uint16_t glyph_count = 0;
261 if (!subtable.ReadU16(&glyph_count)) {
262 return OTS_FAILURE();
263 }
264 if (glyph_count > num_glyphs) {
265 OTS_WARNING("bad glyph count: %u", glyph_count);
266 return OTS_FAILURE();
267 }
268 for (unsigned i = 0; i < glyph_count; ++i) {
269 uint16_t class_value = 0;
270 if (!subtable.ReadU16(&class_value)) {
271 return OTS_FAILURE();
272 }
bashi@chromium.orgced71122011-02-17 10:23:47 +0000273 if (class_value > num_classes) {
bashi@google.com00b790a2011-01-27 06:35:42 +0000274 OTS_WARNING("bad class value: %u", class_value);
275 return OTS_FAILURE();
276 }
277 }
278
279 return true;
280}
281
282bool ParseClassDefFormat2(const uint8_t *data, size_t length,
283 const uint16_t num_glyphs,
284 const uint16_t num_classes) {
285 ots::Buffer subtable(data, length);
286
287 // Skip format field.
288 if (!subtable.Skip(2)) {
289 return OTS_FAILURE();
290 }
291
292 uint16_t range_count = 0;
293 if (!subtable.ReadU16(&range_count)) {
294 return OTS_FAILURE();
295 }
296 if (range_count > num_glyphs) {
297 OTS_WARNING("bad range count: %u", range_count);
298 return OTS_FAILURE();
299 }
300
301 uint16_t last_end = 0;
302 for (unsigned i = 0; i < range_count; ++i) {
303 uint16_t start = 0;
304 uint16_t end = 0;
305 uint16_t class_value = 0;
306 if (!subtable.ReadU16(&start) ||
307 !subtable.ReadU16(&end) ||
308 !subtable.ReadU16(&class_value)) {
309 return OTS_FAILURE();
310 }
311 if (start > end || (last_end && start <= last_end)) {
312 OTS_WARNING("glyph range is overlapping.");
313 return OTS_FAILURE();
314 }
bashi@chromium.orgced71122011-02-17 10:23:47 +0000315 if (class_value > num_classes) {
bashi@google.com00b790a2011-01-27 06:35:42 +0000316 OTS_WARNING("bad class value: %u", class_value);
317 return OTS_FAILURE();
318 }
bashi@google.com78a8baa2011-02-07 06:21:25 +0000319 last_end = end;
bashi@google.com00b790a2011-01-27 06:35:42 +0000320 }
321
322 return true;
323}
324
325bool ParseCoverageFormat1(const uint8_t *data, size_t length,
326 const uint16_t num_glyphs) {
327 ots::Buffer subtable(data, length);
328
329 // Skip format field.
330 if (!subtable.Skip(2)) {
331 return OTS_FAILURE();
332 }
333
334 uint16_t glyph_count = 0;
335 if (!subtable.ReadU16(&glyph_count)) {
336 return OTS_FAILURE();
337 }
338 if (glyph_count > num_glyphs) {
339 OTS_WARNING("bad glyph count: %u", glyph_count);
340 return OTS_FAILURE();
341 }
342 for (unsigned i = 0; i < glyph_count; ++i) {
343 uint16_t glyph = 0;
344 if (!subtable.ReadU16(&glyph)) {
345 return OTS_FAILURE();
346 }
347 if (glyph > num_glyphs) {
348 OTS_WARNING("bad glyph ID: %u", glyph);
349 return OTS_FAILURE();
350 }
351 }
352
353 return true;
354}
355
356bool ParseCoverageFormat2(const uint8_t *data, size_t length,
357 const uint16_t num_glyphs) {
358 ots::Buffer subtable(data, length);
359
360 // Skip format field.
361 if (!subtable.Skip(2)) {
362 return OTS_FAILURE();
363 }
364
365 uint16_t range_count = 0;
366 if (!subtable.ReadU16(&range_count)) {
367 return OTS_FAILURE();
368 }
bashi@google.com78a8baa2011-02-07 06:21:25 +0000369 if (range_count > num_glyphs) {
bashi@google.com00b790a2011-01-27 06:35:42 +0000370 OTS_WARNING("bad range count: %u", range_count);
371 return OTS_FAILURE();
372 }
373 uint16_t last_end = 0;
bashi@google.com78a8baa2011-02-07 06:21:25 +0000374 uint16_t last_start_coverage_index = 0;
bashi@google.com00b790a2011-01-27 06:35:42 +0000375 for (unsigned i = 0; i < range_count; ++i) {
376 uint16_t start = 0;
377 uint16_t end = 0;
378 uint16_t start_coverage_index = 0;
379 if (!subtable.ReadU16(&start) ||
380 !subtable.ReadU16(&end) ||
381 !subtable.ReadU16(&start_coverage_index)) {
382 return OTS_FAILURE();
383 }
384 if (start > end || (last_end && start <= last_end)) {
385 OTS_WARNING("glyph range is overlapping.");
386 return OTS_FAILURE();
387 }
bashi@google.com78a8baa2011-02-07 06:21:25 +0000388 if (start_coverage_index != last_start_coverage_index) {
389 OTS_WARNING("bad start coverage index.");
390 return OTS_FAILURE();
391 }
392 last_end = end;
393 last_start_coverage_index += end - start + 1;
bashi@google.com00b790a2011-01-27 06:35:42 +0000394 }
395
396 return true;
397}
398
bashi@chromium.orgced71122011-02-17 10:23:47 +0000399// Parsers for Contextual subtables in GSUB/GPOS tables.
400
401bool ParseLookupRecord(ots::Buffer *subtable, const uint16_t num_glyphs,
402 const uint16_t num_lookups) {
403 uint16_t sequence_index = 0;
404 uint16_t lookup_list_index = 0;
405 if (!subtable->ReadU16(&sequence_index) ||
406 !subtable->ReadU16(&lookup_list_index)) {
407 return OTS_FAILURE();
408 }
409 if (sequence_index >= num_glyphs) {
410 return OTS_FAILURE();
411 }
412 if (lookup_list_index >= num_lookups) {
413 return OTS_FAILURE();
414 }
415 return true;
416}
417
418bool ParseRuleSubtable(const uint8_t *data, const size_t length,
419 const uint16_t num_glyphs,
420 const uint16_t num_lookups) {
421 ots::Buffer subtable(data, length);
422
423 uint16_t glyph_count = 0;
424 uint16_t lookup_count = 0;
425 if (!subtable.ReadU16(&glyph_count) ||
426 !subtable.ReadU16(&lookup_count)) {
427 return OTS_FAILURE();
428 }
429
430 if (glyph_count == 0 || glyph_count >= num_glyphs) {
431 return OTS_FAILURE();
432 }
433 for (unsigned i = 0; i < glyph_count - static_cast<unsigned>(1); ++i) {
434 uint16_t glyph_id = 0;
435 if (!subtable.ReadU16(&glyph_id)) {
436 return OTS_FAILURE();
437 }
438 if (glyph_id > num_glyphs) {
439 return OTS_FAILURE();
440 }
441 }
442
443 for (unsigned i = 0; i < lookup_count; ++i) {
444 if (!ParseLookupRecord(&subtable, num_glyphs, num_lookups)) {
445 return OTS_FAILURE();
446 }
447 }
448 return true;
449}
450
451bool ParseRuleSetTable(const uint8_t *data, const size_t length,
452 const uint16_t num_glyphs,
453 const uint16_t num_lookups) {
454 ots::Buffer subtable(data, length);
455
456 uint16_t rule_count = 0;
457 if (!subtable.ReadU16(&rule_count)) {
458 return OTS_FAILURE();
459 }
460 const unsigned rule_end = static_cast<unsigned>(2) +
461 rule_count * 2;
462 if (rule_end > std::numeric_limits<uint16_t>::max()) {
463 return OTS_FAILURE();
464 }
465
466 for (unsigned i = 0; i < rule_count; ++i) {
467 uint16_t offset_rule = 0;
468 if (!subtable.ReadU16(&offset_rule)) {
469 return OTS_FAILURE();
470 }
471 if (offset_rule < rule_end || offset_rule >= length) {
472 return OTS_FAILURE();
473 }
474 if (!ParseRuleSubtable(data + offset_rule, length - offset_rule,
475 num_glyphs, num_lookups)) {
476 return OTS_FAILURE();
477 }
478 }
479
480 return true;
481}
482
483bool ParseContextFormat1(const uint8_t *data, const size_t length,
484 const uint16_t num_glyphs,
485 const uint16_t num_lookups) {
486 ots::Buffer subtable(data, length);
487
488 uint16_t offset_coverage = 0;
489 uint16_t rule_set_count = 0;
490 // Skip format field.
491 if (!subtable.Skip(2) ||
492 !subtable.ReadU16(&offset_coverage) ||
493 !subtable.ReadU16(&rule_set_count)) {
494 return OTS_FAILURE();
495 }
496
497 const unsigned rule_set_end = static_cast<unsigned>(6) +
498 rule_set_count * 2;
499 if (rule_set_end > std::numeric_limits<uint16_t>::max()) {
500 return OTS_FAILURE();
501 }
502 if (offset_coverage < rule_set_end || offset_coverage >= length) {
503 return OTS_FAILURE();
504 }
505 if (!ots::ParseCoverageTable(data + offset_coverage,
506 length - offset_coverage, num_glyphs)) {
507 return OTS_FAILURE();
508 }
509
510 for (unsigned i = 0; i < rule_set_count; ++i) {
511 uint16_t offset_rule = 0;
512 if (!subtable.ReadU16(&offset_rule)) {
513 return OTS_FAILURE();
514 }
515 if (offset_rule < rule_set_end || offset_rule >= length) {
516 return OTS_FAILURE();
517 }
518 if (!ParseRuleSetTable(data + offset_rule, length - offset_rule,
519 num_glyphs, num_lookups)) {
520 return OTS_FAILURE();
521 }
522 }
523
524 return true;
525}
526
527bool ParseClassRuleTable(const uint8_t *data, const size_t length,
528 const uint16_t num_glyphs,
529 const uint16_t num_lookups) {
530 ots::Buffer subtable(data, length);
531
532 uint16_t glyph_count = 0;
533 uint16_t lookup_count = 0;
534 if (!subtable.ReadU16(&glyph_count) ||
535 !subtable.ReadU16(&lookup_count)) {
536 return OTS_FAILURE();
537 }
538
539 if (glyph_count == 0 || glyph_count >= num_glyphs) {
540 return OTS_FAILURE();
541 }
542
543 // ClassRule table contains an array of classes. Each value of classes
544 // could take arbitrary values including zero so we don't check these value.
545 const unsigned num_classes = glyph_count - static_cast<unsigned>(1);
546 if (!subtable.Skip(2 * num_classes)) {
547 return OTS_FAILURE();
548 }
549
550 for (unsigned i = 0; i < lookup_count; ++i) {
551 if (!ParseLookupRecord(&subtable, num_glyphs, num_lookups)) {
552 return OTS_FAILURE();
553 }
554 }
555 return true;
556}
557
558bool ParseClassSetTable(const uint8_t *data, const size_t length,
559 const uint16_t num_glyphs,
560 const uint16_t num_lookups) {
561 ots::Buffer subtable(data, length);
562
563 uint16_t class_rule_count = 0;
564 if (!subtable.ReadU16(&class_rule_count)) {
565 return OTS_FAILURE();
566 }
567 const unsigned class_rule_end = static_cast<unsigned>(2) +
568 class_rule_count * 2;
569 if (class_rule_end > std::numeric_limits<uint16_t>::max()) {
570 return OTS_FAILURE();
571 }
572 for (unsigned i = 0; i < class_rule_count; ++i) {
573 uint16_t offset_class_rule = 0;
574 if (!subtable.ReadU16(&offset_class_rule)) {
575 return OTS_FAILURE();
576 }
577 if (offset_class_rule < class_rule_end || offset_class_rule >= length) {
578 return OTS_FAILURE();
579 }
580 if (!ParseClassRuleTable(data + offset_class_rule,
581 length - offset_class_rule, num_glyphs,
582 num_lookups)) {
583 return OTS_FAILURE();
584 }
585 }
586
587 return true;
588}
589
590bool ParseContextFormat2(const uint8_t *data, const size_t length,
591 const uint16_t num_glyphs,
592 const uint16_t num_lookups) {
593 ots::Buffer subtable(data, length);
594
595 uint16_t offset_coverage = 0;
596 uint16_t offset_class_def = 0;
597 uint16_t class_set_cnt = 0;
598 // Skip format field.
599 if (!subtable.Skip(2) ||
600 !subtable.ReadU16(&offset_coverage) ||
601 !subtable.ReadU16(&offset_class_def) ||
602 !subtable.ReadU16(&class_set_cnt)) {
603 return OTS_FAILURE();
604 }
605
606 const unsigned class_set_end = static_cast<unsigned>(8) +
607 class_set_cnt * 2;
608 if (class_set_end > std::numeric_limits<uint16_t>::max()) {
609 return OTS_FAILURE();
610 }
611 if (offset_coverage < class_set_end || offset_coverage >= length) {
612 return OTS_FAILURE();
613 }
614 if (!ots::ParseCoverageTable(data + offset_coverage,
615 length - offset_coverage, num_glyphs)) {
616 return OTS_FAILURE();
617 }
618
619 if (offset_class_def < class_set_end || offset_class_def >= length) {
620 return OTS_FAILURE();
621 }
622 if (!ots::ParseClassDefTable(data + offset_class_def,
623 length - offset_class_def,
624 num_glyphs, kMaxClassDefValue)) {
625 return OTS_FAILURE();
626 }
627
628 for (unsigned i = 0; i < class_set_cnt; ++i) {
629 uint16_t offset_class_rule = 0;
630 if (!subtable.ReadU16(&offset_class_rule)) {
631 return OTS_FAILURE();
632 }
633 if (offset_class_rule) {
634 if (offset_class_rule < class_set_end || offset_class_rule >= length) {
635 return OTS_FAILURE();
636 }
637 if (!ParseClassSetTable(data + offset_class_rule,
638 length - offset_class_rule, num_glyphs,
639 num_lookups)) {
640 return OTS_FAILURE();
641 }
642 }
643 }
644
645 return true;
646}
647
648bool ParseContextFormat3(const uint8_t *data, const size_t length,
649 const uint16_t num_glyphs,
650 const uint16_t num_lookups) {
651 ots::Buffer subtable(data, length);
652
653 uint16_t glyph_count = 0;
654 uint16_t lookup_count = 0;
655 // Skip format field.
656 if (!subtable.Skip(2) ||
657 !subtable.ReadU16(&glyph_count) ||
658 !subtable.ReadU16(&lookup_count)) {
659 return OTS_FAILURE();
660 }
661
662 if (glyph_count >= num_glyphs) {
663 return OTS_FAILURE();
664 }
665 const unsigned lookup_record_end = static_cast<unsigned>(6) +
666 glyph_count * 2 + lookup_count * 4;
667 if (lookup_record_end > std::numeric_limits<uint16_t>::max()) {
668 return OTS_FAILURE();
669 }
670 for (unsigned i = 0; i < glyph_count; ++i) {
671 uint16_t offset_coverage = 0;
672 if (!subtable.ReadU16(&offset_coverage)) {
673 return OTS_FAILURE();
674 }
675 if (offset_coverage < lookup_record_end || offset_coverage >= length) {
676 return OTS_FAILURE();
677 }
678 if (!ots::ParseCoverageTable(data + offset_coverage,
679 length - offset_coverage, num_glyphs)) {
680 return OTS_FAILURE();
681 }
682 }
683
684 for (unsigned i = 0; i < lookup_count; ++i) {
685 if (!ParseLookupRecord(&subtable, num_glyphs, num_lookups)) {
686 return OTS_FAILURE();
687 }
688 }
689
690 return true;
691}
692
693// Parsers for Chaning Contextual subtables in GSUB/GPOS tables.
694
695bool ParseChainRuleSubtable(const uint8_t *data, const size_t length,
696 const uint16_t num_glyphs,
697 const uint16_t num_lookups) {
698 ots::Buffer subtable(data, length);
699
700 uint16_t backtrack_count = 0;
701 if (!subtable.ReadU16(&backtrack_count)) {
702 return OTS_FAILURE();
703 }
704 if (backtrack_count >= num_glyphs) {
705 return OTS_FAILURE();
706 }
707 for (unsigned i = 0; i < backtrack_count; ++i) {
708 uint16_t glyph_id = 0;
709 if (!subtable.ReadU16(&glyph_id)) {
710 return OTS_FAILURE();
711 }
712 if (glyph_id > num_glyphs) {
713 return OTS_FAILURE();
714 }
715 }
716
717 uint16_t input_count = 0;
718 if (!subtable.ReadU16(&input_count)) {
719 return OTS_FAILURE();
720 }
721 if (input_count == 0 || input_count >= num_glyphs) {
722 return OTS_FAILURE();
723 }
724 for (unsigned i = 0; i < input_count - static_cast<unsigned>(1); ++i) {
725 uint16_t glyph_id = 0;
726 if (!subtable.ReadU16(&glyph_id)) {
727 return OTS_FAILURE();
728 }
729 if (glyph_id > num_glyphs) {
730 return OTS_FAILURE();
731 }
732 }
733
734 uint16_t lookahead_count = 0;
735 if (!subtable.ReadU16(&lookahead_count)) {
736 return OTS_FAILURE();
737 }
738 if (lookahead_count >= num_glyphs) {
739 return OTS_FAILURE();
740 }
741 for (unsigned i = 0; i < lookahead_count; ++i) {
742 uint16_t glyph_id = 0;
743 if (!subtable.ReadU16(&glyph_id)) {
744 return OTS_FAILURE();
745 }
746 if (glyph_id > num_glyphs) {
747 return OTS_FAILURE();
748 }
749 }
750
751 uint16_t lookup_count = 0;
752 if (!subtable.ReadU16(&lookup_count)) {
753 return OTS_FAILURE();
754 }
755 for (unsigned i = 0; i < lookup_count; ++i) {
756 if (!ParseLookupRecord(&subtable, num_glyphs, num_lookups)) {
757 return OTS_FAILURE();
758 }
759 }
760
761 return true;
762}
763
764bool ParseChainRuleSetTable(const uint8_t *data, const size_t length,
765 const uint16_t num_glyphs,
766 const uint16_t num_lookups) {
767 ots::Buffer subtable(data, length);
768
769 uint16_t chain_rule_count = 0;
770 if (!subtable.ReadU16(&chain_rule_count)) {
771 return OTS_FAILURE();
772 }
773 const unsigned chain_rule_end = static_cast<unsigned>(2) +
774 chain_rule_count * 2;
775 if (chain_rule_end > std::numeric_limits<uint16_t>::max()) {
776 return OTS_FAILURE();
777 }
778 for (unsigned i = 0; i < chain_rule_count; ++i) {
779 uint16_t offset_chain_rule = 0;
780 if (!subtable.ReadU16(&offset_chain_rule)) {
781 return OTS_FAILURE();
782 }
783 if (offset_chain_rule < chain_rule_end || offset_chain_rule >= length) {
784 return OTS_FAILURE();
785 }
786 if (!ParseChainRuleSubtable(data + offset_chain_rule,
787 length - offset_chain_rule,
788 num_glyphs, num_lookups)) {
789 return OTS_FAILURE();
790 }
791 }
792
793 return true;
794}
795
796bool ParseChainContextFormat1(const uint8_t *data, const size_t length,
797 const uint16_t num_glyphs,
798 const uint16_t num_lookups) {
799 ots::Buffer subtable(data, length);
800
801 uint16_t offset_coverage = 0;
802 uint16_t chain_rule_set_count = 0;
803 // Skip format field.
804 if (!subtable.Skip(2) ||
805 !subtable.ReadU16(&offset_coverage) ||
806 !subtable.ReadU16(&chain_rule_set_count)) {
807 return OTS_FAILURE();
808 }
809
810 const unsigned chain_rule_set_end = static_cast<unsigned>(6) +
811 chain_rule_set_count * 2;
812 if (chain_rule_set_end > std::numeric_limits<uint16_t>::max()) {
813 return OTS_FAILURE();
814 }
815 if (offset_coverage < chain_rule_set_end || offset_coverage >= length) {
816 return OTS_FAILURE();
817 }
818 if (!ots::ParseCoverageTable(data + offset_coverage,
819 length - offset_coverage, num_glyphs)) {
820 return OTS_FAILURE();
821 }
822
823 for (unsigned i = 0; i < chain_rule_set_count; ++i) {
824 uint16_t offset_chain_rule_set = 0;
825 if (!subtable.ReadU16(&offset_chain_rule_set)) {
826 return OTS_FAILURE();
827 }
828 if (offset_chain_rule_set < chain_rule_set_end ||
829 offset_chain_rule_set >= length) {
830 return OTS_FAILURE();
831 }
832 if (!ParseChainRuleSetTable(data + offset_chain_rule_set,
833 length - offset_chain_rule_set,
834 num_glyphs, num_lookups)) {
835 return OTS_FAILURE();
836 }
837 }
838
839 return true;
840}
841
842bool ParseChainClassRuleSubtable(const uint8_t *data, const size_t length,
843 const uint16_t num_glyphs,
844 const uint16_t num_lookups) {
845 ots::Buffer subtable(data, length);
846
847 // In this subtable, we don't check the value of classes for now since
848 // these could take arbitrary values.
849
850 uint16_t backtrack_count = 0;
851 if (!subtable.ReadU16(&backtrack_count)) {
852 return OTS_FAILURE();
853 }
854 if (backtrack_count >= num_glyphs) {
855 return OTS_FAILURE();
856 }
857 if (!subtable.Skip(2 * backtrack_count)) {
858 return OTS_FAILURE();
859 }
860
861 uint16_t input_count = 0;
862 if (!subtable.ReadU16(&input_count)) {
863 return OTS_FAILURE();
864 }
865 if (input_count == 0 || input_count >= num_glyphs) {
866 return OTS_FAILURE();
867 }
868 if (!subtable.Skip(2 * (input_count - 1))) {
869 return OTS_FAILURE();
870 }
871
872 uint16_t lookahead_count = 0;
873 if (!subtable.ReadU16(&lookahead_count)) {
874 return OTS_FAILURE();
875 }
876 if (lookahead_count >= num_glyphs) {
877 return OTS_FAILURE();
878 }
879 if (!subtable.Skip(2 * lookahead_count)) {
880 return OTS_FAILURE();
881 }
882
883 uint16_t lookup_count = 0;
884 if (!subtable.ReadU16(&lookup_count)) {
885 return OTS_FAILURE();
886 }
887 for (unsigned i = 0; i < lookup_count; ++i) {
888 if (!ParseLookupRecord(&subtable, num_glyphs, num_lookups)) {
889 return OTS_FAILURE();
890 }
891 }
892
893 return true;
894}
895
896bool ParseChainClassSetTable(const uint8_t *data, const size_t length,
897 const uint16_t num_glyphs,
898 const uint16_t num_lookups) {
899 ots::Buffer subtable(data, length);
900
901 uint16_t chain_class_rule_count = 0;
902 if (!subtable.ReadU16(&chain_class_rule_count)) {
903 return OTS_FAILURE();
904 }
905 const unsigned chain_class_rule_end = static_cast<unsigned>(2) +
906 chain_class_rule_count * 2;
907 if (chain_class_rule_end > std::numeric_limits<uint16_t>::max()) {
908 return OTS_FAILURE();
909 }
910 for (unsigned i = 0; i < chain_class_rule_count; ++i) {
911 uint16_t offset_chain_class_rule = 0;
912 if (!subtable.ReadU16(&offset_chain_class_rule)) {
913 return OTS_FAILURE();
914 }
915 if (offset_chain_class_rule < chain_class_rule_end ||
916 offset_chain_class_rule >= length) {
917 return OTS_FAILURE();
918 }
919 if (!ParseChainClassRuleSubtable(data + offset_chain_class_rule,
920 length - offset_chain_class_rule,
921 num_glyphs, num_lookups)) {
922 return OTS_FAILURE();
923 }
924 }
925
926 return true;
927}
928
929bool ParseChainContextFormat2(const uint8_t *data, const size_t length,
930 const uint16_t num_glyphs,
931 const uint16_t num_lookups) {
932 ots::Buffer subtable(data, length);
933
934 uint16_t offset_coverage = 0;
935 uint16_t offset_backtrack_class_def = 0;
936 uint16_t offset_input_class_def = 0;
937 uint16_t offset_lookahead_class_def = 0;
938 uint16_t chain_class_set_count = 0;
939 // Skip format field.
940 if (!subtable.Skip(2) ||
941 !subtable.ReadU16(&offset_coverage) ||
942 !subtable.ReadU16(&offset_backtrack_class_def) ||
943 !subtable.ReadU16(&offset_input_class_def) ||
944 !subtable.ReadU16(&offset_lookahead_class_def) ||
945 !subtable.ReadU16(&chain_class_set_count)) {
946 return OTS_FAILURE();
947 }
948
949 const unsigned chain_class_set_end = static_cast<unsigned>(12) +
950 chain_class_set_count * 2;
951 if (chain_class_set_end > std::numeric_limits<uint16_t>::max()) {
952 return OTS_FAILURE();
953 }
954 if (offset_coverage < chain_class_set_end || offset_coverage >= length) {
955 return OTS_FAILURE();
956 }
957 if (!ots::ParseCoverageTable(data + offset_coverage,
958 length - offset_coverage, num_glyphs)) {
959 return OTS_FAILURE();
960 }
961
962 // Classes for backtrack/lookahead sequences might not be defined.
963 if (offset_backtrack_class_def) {
964 if (offset_backtrack_class_def < chain_class_set_end ||
965 offset_backtrack_class_def >= length) {
966 return OTS_FAILURE();
967 }
968 if (!ots::ParseClassDefTable(data + offset_backtrack_class_def,
969 length - offset_backtrack_class_def,
970 num_glyphs, kMaxClassDefValue)) {
971 return OTS_FAILURE();
972 }
973 }
974
975 if (offset_input_class_def < chain_class_set_end ||
976 offset_input_class_def >= length) {
977 return OTS_FAILURE();
978 }
979 if (!ots::ParseClassDefTable(data + offset_input_class_def,
980 length - offset_input_class_def,
981 num_glyphs, kMaxClassDefValue)) {
982 return OTS_FAILURE();
983 }
984
985 if (offset_lookahead_class_def) {
986 if (offset_lookahead_class_def < chain_class_set_end ||
987 offset_lookahead_class_def >= length) {
988 return OTS_FAILURE();
989 }
990 if (!ots::ParseClassDefTable(data + offset_lookahead_class_def,
991 length - offset_lookahead_class_def,
992 num_glyphs, kMaxClassDefValue)) {
993 return OTS_FAILURE();
994 }
995 }
996
997 for (unsigned i = 0; i < chain_class_set_count; ++i) {
998 uint16_t offset_chain_class_set = 0;
999 if (!subtable.ReadU16(&offset_chain_class_set)) {
1000 return OTS_FAILURE();
1001 }
1002 // |offset_chain_class_set| could be NULL.
1003 if (offset_chain_class_set) {
1004 if (offset_chain_class_set < chain_class_set_end ||
1005 offset_chain_class_set >= length) {
1006 return OTS_FAILURE();
1007 }
1008 if (!ParseChainClassSetTable(data + offset_chain_class_set,
1009 length - offset_chain_class_set,
1010 num_glyphs, num_lookups)) {
1011 return OTS_FAILURE();
1012 }
1013 }
1014 }
1015
1016 return true;
1017}
1018
1019bool ParseChainContextFormat3(const uint8_t *data, const size_t length,
1020 const uint16_t num_glyphs,
1021 const uint16_t num_lookups) {
1022 ots::Buffer subtable(data, length);
1023
1024 uint16_t backtrack_count = 0;
1025 // Skip format field.
1026 if (!subtable.Skip(2) ||
1027 !subtable.ReadU16(&backtrack_count)) {
1028 return OTS_FAILURE();
1029 }
1030
1031 if (backtrack_count >= num_glyphs) {
1032 return OTS_FAILURE();
1033 }
1034 std::vector<uint16_t> offsets_backtrack;
1035 offsets_backtrack.reserve(backtrack_count);
1036 for (unsigned i = 0; i < backtrack_count; ++i) {
1037 if (!subtable.ReadU16(&offsets_backtrack[i])) {
1038 return OTS_FAILURE();
1039 }
1040 }
1041
1042 uint16_t input_count = 0;
1043 if (!subtable.ReadU16(&input_count)) {
1044 return OTS_FAILURE();
1045 }
1046 if (input_count >= num_glyphs) {
1047 return OTS_FAILURE();
1048 }
1049 std::vector<uint16_t> offsets_input;
1050 offsets_input.reserve(input_count);
1051 for (unsigned i = 0; i < input_count; ++i) {
1052 if (!subtable.ReadU16(&offsets_input[i])) {
1053 return OTS_FAILURE();
1054 }
1055 }
1056
1057 uint16_t lookahead_count = 0;
1058 if (!subtable.ReadU16(&lookahead_count)) {
1059 return OTS_FAILURE();
1060 }
1061 if (lookahead_count >= num_glyphs) {
1062 return OTS_FAILURE();
1063 }
1064 std::vector<uint16_t> offsets_lookahead;
1065 offsets_lookahead.reserve(lookahead_count);
1066 for (unsigned i = 0; i < lookahead_count; ++i) {
1067 if (!subtable.ReadU16(&offsets_lookahead[i])) {
1068 return OTS_FAILURE();
1069 }
1070 }
1071
1072 uint16_t lookup_count = 0;
1073 if (!subtable.ReadU16(&lookup_count)) {
1074 return OTS_FAILURE();
1075 }
1076 for (unsigned i = 0; i < lookup_count; ++i) {
1077 if (!ParseLookupRecord(&subtable, num_glyphs, num_lookups)) {
1078 return OTS_FAILURE();
1079 }
1080 }
1081
1082 const unsigned lookup_record_end = static_cast<unsigned>(10) +
1083 (backtrack_count + input_count + lookahead_count) * 2 + lookup_count * 4;
1084 if (lookup_record_end > std::numeric_limits<uint16_t>::max()) {
1085 return OTS_FAILURE();
1086 }
1087 for (unsigned i = 0; i < backtrack_count; ++i) {
1088 if (offsets_backtrack[i] < lookup_record_end ||
1089 offsets_backtrack[i] >= length) {
1090 return OTS_FAILURE();
1091 }
1092 if (!ots::ParseCoverageTable(data + offsets_backtrack[i],
1093 length - offsets_backtrack[i], num_glyphs)) {
1094 return OTS_FAILURE();
1095 }
1096 }
1097 for (unsigned i = 0; i < input_count; ++i) {
1098 if (offsets_input[i] < lookup_record_end || offsets_input[i] >= length) {
1099 return OTS_FAILURE();
1100 }
1101 if (!ots::ParseCoverageTable(data + offsets_input[i],
1102 length - offsets_input[i], num_glyphs)) {
1103 return OTS_FAILURE();
1104 }
1105 }
1106 for (unsigned i = 0; i < lookahead_count; ++i) {
1107 if (offsets_lookahead[i] < lookup_record_end ||
1108 offsets_lookahead[i] >= length) {
1109 return OTS_FAILURE();
1110 }
1111 if (!ots::ParseCoverageTable(data + offsets_lookahead[i],
1112 length - offsets_lookahead[i], num_glyphs)) {
1113 return OTS_FAILURE();
1114 }
1115 }
1116
1117 return true;
1118}
1119
bashi@google.com00b790a2011-01-27 06:35:42 +00001120} // namespace
1121
1122namespace ots {
1123
bashi@chromium.orgced71122011-02-17 10:23:47 +00001124bool LookupSubtableParser::Parse(const OpenTypeFile *file, const uint8_t *data,
1125 const size_t length,
1126 const uint16_t lookup_type) const {
1127 for (unsigned i = 0; i < num_types; ++i) {
1128 if (parsers[i].type == lookup_type && parsers[i].parse) {
1129 if (!parsers[i].parse(file, data, length)) {
1130 return OTS_FAILURE();
1131 }
1132 return true;
1133 }
1134 }
1135 return OTS_FAILURE();
1136}
1137
bashi@google.com78a8baa2011-02-07 06:21:25 +00001138// Parsing ScriptListTable requires number of features so we need to
1139// parse FeatureListTable before calling this function.
1140bool ParseScriptListTable(const uint8_t *data, const size_t length,
1141 const uint16_t num_features) {
1142 Buffer subtable(data, length);
1143
1144 uint16_t script_count = 0;
1145 if (!subtable.ReadU16(&script_count)) {
1146 return OTS_FAILURE();
1147 }
1148
1149 const unsigned script_record_end = static_cast<unsigned>(2) +
1150 script_count * 6;
1151 if (script_record_end > std::numeric_limits<uint16_t>::max()) {
1152 return OTS_FAILURE();
1153 }
1154 std::vector<ScriptRecord> script_list;
1155 script_list.reserve(script_count);
1156 uint32_t last_tag = 0;
1157 for (unsigned i = 0; i < script_count; ++i) {
1158 if (!subtable.ReadU32(&script_list[i].tag) ||
1159 !subtable.ReadU16(&script_list[i].offset)) {
1160 return OTS_FAILURE();
1161 }
1162 // Script tags should be arranged alphabetically by tag
1163 if (last_tag != 0 && last_tag > script_list[i].tag) {
1164 return OTS_FAILURE();
1165 }
1166 last_tag = script_list[i].tag;
1167 if (script_list[i].offset < script_record_end ||
1168 script_list[i].offset >= length) {
1169 return OTS_FAILURE();
1170 }
1171 }
1172
1173 // Check script records.
1174 for (unsigned i = 0; i < script_count; ++i) {
1175 if (!ParseScriptTable(data + script_list[i].offset,
1176 length - script_list[i].offset,
1177 script_list[i].tag, num_features)) {
1178 return OTS_FAILURE();
1179 }
1180 }
1181
1182 return true;
1183}
1184
1185// Parsing FeatureListTable requires number of lookups so we need to parse
1186// LookupListTable before calling this function.
1187bool ParseFeatureListTable(const uint8_t *data, const size_t length,
1188 const uint16_t num_lookups,
1189 uint16_t* num_features) {
1190 Buffer subtable(data, length);
1191
1192 uint16_t feature_count = 0;
1193 if (!subtable.ReadU16(&feature_count)) {
1194 return OTS_FAILURE();
1195 }
1196
1197 std::vector<FeatureRecord> feature_records;
1198 feature_records.resize(feature_count);
1199 const unsigned feature_record_end = static_cast<unsigned>(2) +
1200 feature_count * 6;
1201 if (feature_record_end > std::numeric_limits<uint16_t>::max()) {
1202 return OTS_FAILURE();
1203 }
1204 uint32_t last_tag = 0;
1205 for (unsigned i = 0; i < feature_count; ++i) {
1206 if (!subtable.ReadU32(&feature_records[i].tag) ||
1207 !subtable.ReadU16(&feature_records[i].offset)) {
1208 return OTS_FAILURE();
1209 }
1210 // Feature record array should be arranged alphabetically by tag
1211 if (last_tag != 0 && last_tag > feature_records[i].tag) {
1212 return OTS_FAILURE();
1213 }
1214 last_tag = feature_records[i].tag;
1215 if (feature_records[i].offset < feature_record_end ||
1216 feature_records[i].offset >= length) {
1217 return OTS_FAILURE();
1218 }
1219 }
1220
1221 for (unsigned i = 0; i < feature_count; ++i) {
1222 if (!ParseFeatureTable(data + feature_records[i].offset,
1223 length - feature_records[i].offset, num_lookups)) {
1224 return OTS_FAILURE();
1225 }
1226 }
1227 *num_features = feature_count;
1228 return true;
1229}
1230
1231// For parsing GPOS/GSUB tables, this function should be called at first to
1232// obtain the number of lookups because parsing FeatureTableList requires
1233// the number.
1234bool ParseLookupListTable(OpenTypeFile *file, const uint8_t *data,
bashi@chromium.orgced71122011-02-17 10:23:47 +00001235 const size_t length,
1236 const LookupSubtableParser* parser,
bashi@google.com78a8baa2011-02-07 06:21:25 +00001237 uint16_t *num_lookups) {
1238 Buffer subtable(data, length);
1239
1240 if (!subtable.ReadU16(num_lookups)) {
1241 return OTS_FAILURE();
1242 }
1243
1244 std::vector<uint16_t> lookups;
1245 lookups.reserve(*num_lookups);
1246 const unsigned lookup_end = static_cast<unsigned>(2) +
1247 (*num_lookups) * 2;
1248 if (lookup_end > std::numeric_limits<uint16_t>::max()) {
1249 return OTS_FAILURE();
1250 }
1251 for (unsigned i = 0; i < *num_lookups; ++i) {
1252 if (!subtable.ReadU16(&lookups[i])) {
1253 return OTS_FAILURE();
1254 }
1255 if (lookups[i] < lookup_end || lookups[i] >= length) {
1256 return OTS_FAILURE();
1257 }
1258 }
1259
1260 for (unsigned i = 0; i < *num_lookups; ++i) {
1261 if (!ParseLookupTable(file, data + lookups[i], length - lookups[i],
bashi@chromium.orgced71122011-02-17 10:23:47 +00001262 parser)) {
bashi@google.com78a8baa2011-02-07 06:21:25 +00001263 return OTS_FAILURE();
1264 }
1265 }
1266
1267 return true;
1268}
1269
bashi@google.com00b790a2011-01-27 06:35:42 +00001270bool ParseClassDefTable(const uint8_t *data, size_t length,
1271 const uint16_t num_glyphs,
1272 const uint16_t num_classes) {
1273 Buffer subtable(data, length);
1274
1275 uint16_t format = 0;
1276 if (!subtable.ReadU16(&format)) {
1277 return OTS_FAILURE();
1278 }
1279 if (format == 1) {
1280 return ParseClassDefFormat1(data, length, num_glyphs, num_classes);
1281 } else if (format == 2) {
1282 return ParseClassDefFormat2(data, length, num_glyphs, num_classes);
1283 }
1284
1285 return OTS_FAILURE();
1286}
1287
1288bool ParseCoverageTable(const uint8_t *data, size_t length,
1289 const uint16_t num_glyphs) {
1290 Buffer subtable(data, length);
1291
1292 uint16_t format = 0;
1293 if (!subtable.ReadU16(&format)) {
1294 return OTS_FAILURE();
1295 }
1296 if (format == 1) {
1297 return ParseCoverageFormat1(data, length, num_glyphs);
1298 } else if (format == 2) {
1299 return ParseCoverageFormat2(data, length, num_glyphs);
1300 }
1301
1302 return OTS_FAILURE();
1303}
1304
bashi@google.com78a8baa2011-02-07 06:21:25 +00001305bool ParseDeviceTable(const uint8_t *data, size_t length) {
1306 Buffer subtable(data, length);
1307
1308 uint16_t start_size = 0;
1309 uint16_t end_size = 0;
1310 uint16_t delta_format = 0;
1311 if (!subtable.ReadU16(&start_size) ||
1312 !subtable.ReadU16(&end_size) ||
1313 !subtable.ReadU16(&delta_format)) {
1314 return OTS_FAILURE();
1315 }
1316 if (start_size > end_size) {
1317 OTS_WARNING("bad size range: %u > %u", start_size, end_size);
1318 return OTS_FAILURE();
1319 }
1320 if (delta_format == 0 || delta_format > kMaxDeltaFormatType) {
1321 OTS_WARNING("bad delta format: %u", delta_format);
1322 return OTS_FAILURE();
1323 }
1324 // The number of delta values per uint16. The device table should contain
1325 // at least |num_units| * 2 bytes compressed data.
1326 const unsigned num_units = (end_size - start_size) /
1327 (1 << (4 - delta_format)) + 1;
1328 // Just skip |num_units| * 2 bytes since the compressed data could take
1329 // arbitrary values.
1330 if (!subtable.Skip(num_units * 2)) {
1331 return OTS_FAILURE();
1332 }
1333 return true;
1334}
1335
bashi@chromium.orgced71122011-02-17 10:23:47 +00001336bool ParseContextSubtable(const uint8_t *data, const size_t length,
1337 const uint16_t num_glyphs,
1338 const uint16_t num_lookups) {
1339 Buffer subtable(data, length);
1340
1341 uint16_t format = 0;
1342 if (!subtable.ReadU16(&format)) {
1343 return OTS_FAILURE();
1344 }
1345
1346 if (format == 1) {
1347 if (!ParseContextFormat1(data, length, num_glyphs, num_lookups)) {
1348 return OTS_FAILURE();
1349 }
1350 } else if (format == 2) {
1351 if (!ParseContextFormat2(data, length, num_glyphs, num_lookups)) {
1352 return OTS_FAILURE();
1353 }
1354 } else if (format == 3) {
1355 if (!ParseContextFormat3(data, length, num_glyphs, num_lookups)) {
1356 return OTS_FAILURE();
1357 }
1358 } else {
1359 return OTS_FAILURE();
1360 }
1361
1362 return true;
1363}
1364
1365bool ParseChainingContextSubtable(const uint8_t *data, const size_t length,
1366 const uint16_t num_glyphs,
1367 const uint16_t num_lookups) {
1368 Buffer subtable(data, length);
1369
1370 uint16_t format = 0;
1371 if (!subtable.ReadU16(&format)) {
1372 return OTS_FAILURE();
1373 }
1374
1375 if (format == 1) {
1376 if (!ParseChainContextFormat1(data, length, num_glyphs, num_lookups)) {
1377 return OTS_FAILURE();
1378 }
1379 } else if (format == 2) {
1380 if (!ParseChainContextFormat2(data, length, num_glyphs, num_lookups)) {
1381 return OTS_FAILURE();
1382 }
1383 } else if (format == 3) {
1384 if (!ParseChainContextFormat3(data, length, num_glyphs, num_lookups)) {
1385 return OTS_FAILURE();
1386 }
1387 } else {
1388 return OTS_FAILURE();
1389 }
1390
1391 return true;
1392}
1393
1394bool ParseExtensionSubtable(const OpenTypeFile *file,
1395 const uint8_t *data, const size_t length,
1396 const LookupSubtableParser* parser) {
1397 Buffer subtable(data, length);
1398
1399 uint16_t format = 0;
1400 uint16_t lookup_type = 0;
1401 uint32_t offset_extension = 0;
1402 if (!subtable.ReadU16(&format) ||
1403 !subtable.ReadU16(&lookup_type) ||
1404 !subtable.ReadU32(&offset_extension)) {
1405 return OTS_FAILURE();
1406 }
1407
1408 if (format != 1) {
1409 return OTS_FAILURE();
1410 }
1411 // |lookup_type| should be other than |parser->extension_type|.
1412 if (lookup_type < 1 || lookup_type > parser->num_types ||
1413 lookup_type == parser->extension_type) {
1414 return OTS_FAILURE();
1415 }
1416
1417 const unsigned format_end = static_cast<unsigned>(8);
1418 if (offset_extension < format_end ||
1419 offset_extension >= length) {
1420 return OTS_FAILURE();
1421 }
1422
1423 // Parse the extension subtable of |lookup_type|.
1424 if (!parser->Parse(file, data + offset_extension, length - offset_extension,
1425 lookup_type)) {
1426 return OTS_FAILURE();
1427 }
1428
1429 return true;
1430}
1431
bashi@google.com00b790a2011-01-27 06:35:42 +00001432} // namespace ots
1433