Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/gfx/ots/src/ots.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2009-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 "ots.h"
6
7
#include <sys/types.h>
8
#include <zlib.h>
9
10
#include <algorithm>
11
#include <cstdlib>
12
#include <cstring>
13
#include <limits>
14
#include <map>
15
#include <vector>
16
17
#include <woff2/decode.h>
18
19
// The OpenType Font File
20
// http://www.microsoft.com/typography/otspec/otff.htm
21
22
#include "cff.h"
23
#include "cmap.h"
24
#include "cvt.h"
25
#include "fpgm.h"
26
#include "gasp.h"
27
#include "gdef.h"
28
#include "glyf.h"
29
#include "gpos.h"
30
#include "gsub.h"
31
#include "hdmx.h"
32
#include "head.h"
33
#include "hhea.h"
34
#include "hmtx.h"
35
#include "kern.h"
36
#include "loca.h"
37
#include "ltsh.h"
38
#include "math_.h"
39
#include "maxp.h"
40
#include "name.h"
41
#include "os2.h"
42
#include "ots.h"
43
#include "post.h"
44
#include "prep.h"
45
#include "vdmx.h"
46
#include "vhea.h"
47
#include "vmtx.h"
48
#include "vorg.h"
49
50
// Graphite tables
51
#ifdef OTS_GRAPHITE
52
#include "feat.h"
53
#include "glat.h"
54
#include "gloc.h"
55
#include "sile.h"
56
#include "silf.h"
57
#include "sill.h"
58
#endif
59
60
#ifdef OTS_VARIATIONS
61
#include "avar.h"
62
#include "cvar.h"
63
#include "fvar.h"
64
#include "gvar.h"
65
#include "hvar.h"
66
#include "mvar.h"
67
#include "stat.h"
68
#include "vvar.h"
69
#endif
70
71
namespace ots {
72
73
struct Arena {
74
 public:
75
0
  ~Arena() {
76
0
    for (std::vector<uint8_t*>::iterator
77
0
         i = hunks_.begin(); i != hunks_.end(); ++i) {
78
0
      delete[] *i;
79
0
    }
80
0
  }
81
82
0
  uint8_t* Allocate(size_t length) {
83
0
    uint8_t* p = new uint8_t[length];
84
0
    hunks_.push_back(p);
85
0
    return p;
86
0
  }
87
88
 private:
89
  std::vector<uint8_t*> hunks_;
90
};
91
92
0
bool CheckTag(uint32_t tag_value) {
93
0
  for (unsigned i = 0; i < 4; ++i) {
94
0
    const uint32_t check = tag_value & 0xff;
95
0
    if (check < 32 || check > 126) {
96
0
      return false;  // non-ASCII character found.
97
0
    }
98
0
    tag_value >>= 8;
99
0
  }
100
0
  return true;
101
0
}
102
103
}; // namespace ots
104
105
namespace {
106
107
#define OTS_MSG_TAG_(level,otf_,msg_,tag_) \
108
0
  (OTS_MESSAGE_(level,otf_,"%c%c%c%c: %s", OTS_UNTAG(tag_), msg_), false)
109
110
// Generate a message with or without a table tag, when 'header' is the FontFile pointer
111
0
#define OTS_FAILURE_MSG_TAG(msg_,tag_) OTS_MSG_TAG_(0, header, msg_, tag_)
112
0
#define OTS_FAILURE_MSG_HDR(...)       OTS_FAILURE_MSG_(header, __VA_ARGS__)
113
0
#define OTS_WARNING_MSG_HDR(...)       OTS_WARNING_MSG_(header, __VA_ARGS__)
114
115
116
const struct {
117
  uint32_t tag;
118
  bool required;
119
} supported_tables[] = {
120
  { OTS_TAG_MAXP, true },
121
  { OTS_TAG_HEAD, true },
122
  { OTS_TAG_OS2,  true },
123
  { OTS_TAG_CMAP, true },
124
  { OTS_TAG_HHEA, true },
125
  { OTS_TAG_HMTX, true },
126
  { OTS_TAG_NAME, true },
127
  { OTS_TAG_POST, true },
128
  { OTS_TAG_LOCA, false },
129
  { OTS_TAG_GLYF, false },
130
  { OTS_TAG_CFF,  false },
131
  { OTS_TAG_VDMX, false },
132
  { OTS_TAG_HDMX, false },
133
  { OTS_TAG_GASP, false },
134
  { OTS_TAG_CVT,  false },
135
  { OTS_TAG_FPGM, false },
136
  { OTS_TAG_PREP, false },
137
  { OTS_TAG_LTSH, false },
138
  { OTS_TAG_VORG, false },
139
  { OTS_TAG_KERN, false },
140
  // We need to parse fvar table before other tables that may need to know
141
  // the number of variation axes (if any)
142
#ifdef OTS_VARIATIONS
143
  { OTS_TAG_FVAR, false },
144
  { OTS_TAG_AVAR, false },
145
  { OTS_TAG_CVAR, false },
146
  { OTS_TAG_GVAR, false },
147
  { OTS_TAG_HVAR, false },
148
  { OTS_TAG_MVAR, false },
149
  { OTS_TAG_STAT, false },
150
  { OTS_TAG_VVAR, false },
151
#endif
152
  // We need to parse GDEF table in advance of parsing GSUB/GPOS tables
153
  // because they could refer GDEF table.
154
  { OTS_TAG_GDEF, false },
155
  { OTS_TAG_GPOS, false },
156
  { OTS_TAG_GSUB, false },
157
  { OTS_TAG_VHEA, false },
158
  { OTS_TAG_VMTX, false },
159
  { OTS_TAG_MATH, false },
160
  // Graphite tables
161
#ifdef OTS_GRAPHITE
162
  { OTS_TAG_GLOC, false },
163
  { OTS_TAG_GLAT, false },
164
  { OTS_TAG_FEAT, false },
165
  { OTS_TAG_SILF, false },
166
  { OTS_TAG_SILE, false },
167
  { OTS_TAG_SILL, false },
168
#endif
169
  { 0, false },
170
};
171
172
bool ProcessGeneric(ots::FontFile *header,
173
                    ots::Font *font,
174
                    uint32_t signature,
175
                    ots::OTSStream *output,
176
                    const uint8_t *data, size_t length,
177
                    const std::vector<ots::TableEntry>& tables,
178
                    ots::Buffer& file);
179
180
bool ProcessTTF(ots::FontFile *header,
181
                ots::Font *font,
182
                ots::OTSStream *output, const uint8_t *data, size_t length,
183
0
                uint32_t offset = 0) {
184
0
  ots::Buffer file(data + offset, length - offset);
185
0
186
0
  if (offset > length) {
187
0
    return OTS_FAILURE_MSG_HDR("offset beyond end of file");
188
0
  }
189
0
190
0
  // we disallow all files > 1GB in size for sanity.
191
0
  if (length > 1024 * 1024 * 1024) {
192
0
    return OTS_FAILURE_MSG_HDR("file exceeds 1GB");
193
0
  }
194
0
195
0
  if (!file.ReadU32(&font->version)) {
196
0
    return OTS_FAILURE_MSG_HDR("error reading version tag");
197
0
  }
198
0
  if (!ots::IsValidVersionTag(font->version)) {
199
0
      return OTS_FAILURE_MSG_HDR("invalid version tag");
200
0
  }
201
0
202
0
  if (!file.ReadU16(&font->num_tables) ||
203
0
      !file.ReadU16(&font->search_range) ||
204
0
      !file.ReadU16(&font->entry_selector) ||
205
0
      !file.ReadU16(&font->range_shift)) {
206
0
    return OTS_FAILURE_MSG_HDR("error reading table directory search header");
207
0
  }
208
0
209
0
  // search_range is (Maximum power of 2 <= numTables) x 16. Thus, to avoid
210
0
  // overflow num_tables is, at most, 2^16 / 16 = 2^12
211
0
  if (font->num_tables >= 4096 || font->num_tables < 1) {
212
0
    return OTS_FAILURE_MSG_HDR("excessive (or zero) number of tables");
213
0
  }
214
0
215
0
  unsigned max_pow2 = 0;
216
0
  while (1u << (max_pow2 + 1) <= font->num_tables) {
217
0
    max_pow2++;
218
0
  }
219
0
  const uint16_t expected_search_range = (1u << max_pow2) << 4;
220
0
221
0
  // Don't call ots_failure() here since ~25% of fonts (250+ fonts) in
222
0
  // http://www.princexml.com/fonts/ have unexpected search_range value.
223
0
  if (font->search_range != expected_search_range) {
224
0
    OTS_WARNING_MSG_HDR("bad search range");
225
0
    font->search_range = expected_search_range;  // Fix the value.
226
0
  }
227
0
228
0
  // entry_selector is Log2(maximum power of 2 <= numTables)
229
0
  if (font->entry_selector != max_pow2) {
230
0
    return OTS_FAILURE_MSG_HDR("incorrect entrySelector for table directory");
231
0
  }
232
0
233
0
  // range_shift is NumTables x 16-searchRange. We know that 16*num_tables
234
0
  // doesn't over flow because we range checked it above. Also, we know that
235
0
  // it's > font->search_range by construction of search_range.
236
0
  const uint16_t expected_range_shift =
237
0
      16 * font->num_tables - font->search_range;
238
0
  if (font->range_shift != expected_range_shift) {
239
0
    OTS_WARNING_MSG_HDR("bad range shift");
240
0
    font->range_shift = expected_range_shift;  // the same as above.
241
0
  }
242
0
243
0
  // Next up is the list of tables.
244
0
  std::vector<ots::TableEntry> tables;
245
0
246
0
  for (unsigned i = 0; i < font->num_tables; ++i) {
247
0
    ots::TableEntry table;
248
0
    if (!file.ReadU32(&table.tag) ||
249
0
        !file.ReadU32(&table.chksum) ||
250
0
        !file.ReadU32(&table.offset) ||
251
0
        !file.ReadU32(&table.length)) {
252
0
      return OTS_FAILURE_MSG_HDR("error reading table directory");
253
0
    }
254
0
255
0
    table.uncompressed_length = table.length;
256
0
    tables.push_back(table);
257
0
  }
258
0
259
0
  return ProcessGeneric(header, font, font->version, output, data, length,
260
0
                        tables, file);
261
0
}
262
263
bool ProcessTTC(ots::FontFile *header,
264
                ots::OTSStream *output,
265
                const uint8_t *data,
266
                size_t length,
267
0
                uint32_t index) {
268
0
  ots::Buffer file(data, length);
269
0
270
0
  // we disallow all files > 1GB in size for sanity.
271
0
  if (length > 1024 * 1024 * 1024) {
272
0
    return OTS_FAILURE_MSG_HDR("file exceeds 1GB");
273
0
  }
274
0
275
0
  uint32_t ttc_tag;
276
0
  if (!file.ReadU32(&ttc_tag)) {
277
0
    return OTS_FAILURE_MSG_HDR("Error reading TTC tag");
278
0
  }
279
0
  if (ttc_tag != OTS_TAG('t','t','c','f')) {
280
0
    return OTS_FAILURE_MSG_HDR("Invalid TTC tag");
281
0
  }
282
0
283
0
  uint32_t ttc_version;
284
0
  if (!file.ReadU32(&ttc_version)) {
285
0
    return OTS_FAILURE_MSG_HDR("Error reading TTC version");
286
0
  }
287
0
  if (ttc_version != 0x00010000 && ttc_version != 0x00020000) {
288
0
    return OTS_FAILURE_MSG_HDR("Invalid TTC version");
289
0
  }
290
0
291
0
  uint32_t num_fonts;
292
0
  if (!file.ReadU32(&num_fonts)) {
293
0
    return OTS_FAILURE_MSG_HDR("Error reading number of TTC fonts");
294
0
  }
295
0
  // Limit the allowed number of subfonts to have same memory allocation.
296
0
  if (num_fonts > 0x10000) {
297
0
    return OTS_FAILURE_MSG_HDR("Too many fonts in TTC");
298
0
  }
299
0
300
0
  std::vector<uint32_t> offsets(num_fonts);
301
0
  for (unsigned i = 0; i < num_fonts; i++) {
302
0
    if (!file.ReadU32(&offsets[i])) {
303
0
      return OTS_FAILURE_MSG_HDR("Error reading offset to OffsetTable");
304
0
    }
305
0
  }
306
0
307
0
  if (ttc_version == 0x00020000) {
308
0
    // We don't care about these fields of the header:
309
0
    // uint32_t dsig_tag, dsig_length, dsig_offset
310
0
    if (!file.Skip(3 * 4)) {
311
0
      return OTS_FAILURE_MSG_HDR("Error reading DSIG offset and length in TTC font");
312
0
    }
313
0
  }
314
0
315
0
  if (index == static_cast<uint32_t>(-1)) {
316
0
    if (!output->WriteU32(ttc_tag) ||
317
0
        !output->WriteU32(0x00010000) ||
318
0
        !output->WriteU32(num_fonts) ||
319
0
        !output->Seek((3 + num_fonts) * 4)) {
320
0
      return OTS_FAILURE_MSG_HDR("Error writing output");
321
0
    }
322
0
323
0
    // Keep references to the fonts processed in the loop below, as we need
324
0
    // them for reused tables.
325
0
    std::vector<ots::Font> fonts(num_fonts, ots::Font(header));
326
0
327
0
    for (unsigned i = 0; i < num_fonts; i++) {
328
0
      uint32_t out_offset = output->Tell();
329
0
      if (!output->Seek((3 + i) * 4) ||
330
0
          !output->WriteU32(out_offset) ||
331
0
          !output->Seek(out_offset)) {
332
0
        return OTS_FAILURE_MSG_HDR("Error writing output");
333
0
      }
334
0
      if (!ProcessTTF(header, &fonts[i], output, data, length, offsets[i])) {
335
0
        return false;
336
0
      }
337
0
    }
338
0
339
0
    return true;
340
0
  } else {
341
0
    if (index >= num_fonts) {
342
0
      return OTS_FAILURE_MSG_HDR("Requested font index is bigger than the number of fonts in the TTC file");
343
0
    }
344
0
345
0
    ots::Font font(header);
346
0
    return ProcessTTF(header, &font, output, data, length, offsets[index]);
347
0
  }
348
0
}
349
350
bool ProcessWOFF(ots::FontFile *header,
351
                 ots::Font *font,
352
0
                 ots::OTSStream *output, const uint8_t *data, size_t length) {
353
0
  ots::Buffer file(data, length);
354
0
355
0
  // we disallow all files > 1GB in size for sanity.
356
0
  if (length > 1024 * 1024 * 1024) {
357
0
    return OTS_FAILURE_MSG_HDR("file exceeds 1GB");
358
0
  }
359
0
360
0
  uint32_t woff_tag;
361
0
  if (!file.ReadU32(&woff_tag)) {
362
0
    return OTS_FAILURE_MSG_HDR("error reading WOFF marker");
363
0
  }
364
0
365
0
  if (woff_tag != OTS_TAG('w','O','F','F')) {
366
0
    return OTS_FAILURE_MSG_HDR("invalid WOFF marker");
367
0
  }
368
0
369
0
  if (!file.ReadU32(&font->version)) {
370
0
    return OTS_FAILURE_MSG_HDR("error reading version tag");
371
0
  }
372
0
  if (!ots::IsValidVersionTag(font->version)) {
373
0
    return OTS_FAILURE_MSG_HDR("invalid version tag");
374
0
  }
375
0
376
0
  uint32_t reported_length;
377
0
  if (!file.ReadU32(&reported_length) || length != reported_length) {
378
0
    return OTS_FAILURE_MSG_HDR("incorrect file size in WOFF header");
379
0
  }
380
0
381
0
  if (!file.ReadU16(&font->num_tables) || !font->num_tables) {
382
0
    return OTS_FAILURE_MSG_HDR("error reading number of tables");
383
0
  }
384
0
385
0
  uint16_t reserved_value;
386
0
  if (!file.ReadU16(&reserved_value) || reserved_value) {
387
0
    return OTS_FAILURE_MSG_HDR("error in reserved field of WOFF header");
388
0
  }
389
0
390
0
  uint32_t reported_total_sfnt_size;
391
0
  if (!file.ReadU32(&reported_total_sfnt_size)) {
392
0
    return OTS_FAILURE_MSG_HDR("error reading total sfnt size");
393
0
  }
394
0
395
0
  // We don't care about these fields of the header:
396
0
  //   uint16_t major_version, minor_version
397
0
  if (!file.Skip(2 * 2)) {
398
0
    return OTS_FAILURE_MSG_HDR("Failed to read 'majorVersion' or 'minorVersion'");
399
0
  }
400
0
401
0
  // Checks metadata block size.
402
0
  uint32_t meta_offset;
403
0
  uint32_t meta_length;
404
0
  uint32_t meta_length_orig;
405
0
  if (!file.ReadU32(&meta_offset) ||
406
0
      !file.ReadU32(&meta_length) ||
407
0
      !file.ReadU32(&meta_length_orig)) {
408
0
    return OTS_FAILURE_MSG_HDR("Failed to read header metadata block fields");
409
0
  }
410
0
  if (meta_offset) {
411
0
    if (meta_offset >= length || length - meta_offset < meta_length) {
412
0
      return OTS_FAILURE_MSG_HDR("Invalid metadata block offset or length");
413
0
    }
414
0
  }
415
0
416
0
  // Checks private data block size.
417
0
  uint32_t priv_offset;
418
0
  uint32_t priv_length;
419
0
  if (!file.ReadU32(&priv_offset) ||
420
0
      !file.ReadU32(&priv_length)) {
421
0
    return OTS_FAILURE_MSG_HDR("Failed to read header private block fields");
422
0
  }
423
0
  if (priv_offset) {
424
0
    if (priv_offset >= length || length - priv_offset < priv_length) {
425
0
      return OTS_FAILURE_MSG_HDR("Invalid private block offset or length");
426
0
    }
427
0
  }
428
0
429
0
  // Next up is the list of tables.
430
0
  std::vector<ots::TableEntry> tables;
431
0
432
0
  uint32_t first_index = 0;
433
0
  uint32_t last_index = 0;
434
0
  // Size of sfnt header plus size of table records.
435
0
  uint64_t total_sfnt_size = 12 + 16 * font->num_tables;
436
0
  for (unsigned i = 0; i < font->num_tables; ++i) {
437
0
    ots::TableEntry table;
438
0
    if (!file.ReadU32(&table.tag) ||
439
0
        !file.ReadU32(&table.offset) ||
440
0
        !file.ReadU32(&table.length) ||
441
0
        !file.ReadU32(&table.uncompressed_length) ||
442
0
        !file.ReadU32(&table.chksum)) {
443
0
      return OTS_FAILURE_MSG_HDR("error reading table directory");
444
0
    }
445
0
446
0
    total_sfnt_size += ots::Round4(table.uncompressed_length);
447
0
    if (total_sfnt_size > std::numeric_limits<uint32_t>::max()) {
448
0
      return OTS_FAILURE_MSG_HDR("sfnt size overflow");
449
0
    }
450
0
    tables.push_back(table);
451
0
    if (i == 0 || tables[first_index].offset > table.offset)
452
0
      first_index = i;
453
0
    if (i == 0 || tables[last_index].offset < table.offset)
454
0
      last_index = i;
455
0
  }
456
0
457
0
  if (reported_total_sfnt_size != total_sfnt_size) {
458
0
    return OTS_FAILURE_MSG_HDR("uncompressed sfnt size mismatch");
459
0
  }
460
0
461
0
  // Table data must follow immediately after the header.
462
0
  if (tables[first_index].offset != ots::Round4(file.offset())) {
463
0
    return OTS_FAILURE_MSG_HDR("junk before tables in WOFF file");
464
0
  }
465
0
466
0
  if (tables[last_index].offset >= length ||
467
0
      length - tables[last_index].offset < tables[last_index].length) {
468
0
    return OTS_FAILURE_MSG_HDR("invalid table location/size");
469
0
  }
470
0
  // Blocks must follow immediately after the previous block.
471
0
  // (Except for padding with a maximum of three null bytes)
472
0
  uint64_t block_end = ots::Round4(
473
0
      static_cast<uint64_t>(tables[last_index].offset) +
474
0
      static_cast<uint64_t>(tables[last_index].length));
475
0
  if (block_end > std::numeric_limits<uint32_t>::max()) {
476
0
    return OTS_FAILURE_MSG_HDR("invalid table location/size");
477
0
  }
478
0
  if (meta_offset) {
479
0
    if (block_end != meta_offset) {
480
0
      return OTS_FAILURE_MSG_HDR("Invalid metadata block offset");
481
0
    }
482
0
    block_end = ots::Round4(static_cast<uint64_t>(meta_offset) +
483
0
                            static_cast<uint64_t>(meta_length));
484
0
    if (block_end > std::numeric_limits<uint32_t>::max()) {
485
0
      return OTS_FAILURE_MSG_HDR("Invalid metadata block length");
486
0
    }
487
0
  }
488
0
  if (priv_offset) {
489
0
    if (block_end != priv_offset) {
490
0
      return OTS_FAILURE_MSG_HDR("Invalid private block offset");
491
0
    }
492
0
    block_end = ots::Round4(static_cast<uint64_t>(priv_offset) +
493
0
                            static_cast<uint64_t>(priv_length));
494
0
    if (block_end > std::numeric_limits<uint32_t>::max()) {
495
0
      return OTS_FAILURE_MSG_HDR("Invalid private block length");
496
0
    }
497
0
  }
498
0
  if (block_end != ots::Round4(length)) {
499
0
    return OTS_FAILURE_MSG_HDR("File length mismatch (trailing junk?)");
500
0
  }
501
0
502
0
  return ProcessGeneric(header, font, woff_tag, output, data, length, tables, file);
503
0
}
504
505
bool ProcessWOFF2(ots::FontFile *header,
506
                  ots::OTSStream *output,
507
                  const uint8_t *data,
508
                  size_t length,
509
0
                  uint32_t index) {
510
0
  size_t decompressed_size = woff2::ComputeWOFF2FinalSize(data, length);
511
0
512
0
  if (decompressed_size < length) {
513
0
    return OTS_FAILURE_MSG_HDR("Size of decompressed WOFF 2.0 is less than compressed size");
514
0
  }
515
0
516
0
  if (decompressed_size == 0) {
517
0
    return OTS_FAILURE_MSG_HDR("Size of decompressed WOFF 2.0 is set to 0");
518
0
  }
519
0
  // decompressed font must be <= 30MB
520
0
  if (decompressed_size > 30 * 1024 * 1024) {
521
0
    return OTS_FAILURE_MSG_HDR("Size of decompressed WOFF 2.0 font exceeds 30MB");
522
0
  }
523
0
524
0
  std::string buf(decompressed_size, 0);
525
0
  woff2::WOFF2StringOut out(&buf);
526
0
  if (!woff2::ConvertWOFF2ToTTF(data, length, &out)) {
527
0
    return OTS_FAILURE_MSG_HDR("Failed to convert WOFF 2.0 font to SFNT");
528
0
  }
529
0
  const uint8_t *decompressed = reinterpret_cast<const uint8_t*>(buf.data());
530
0
531
0
  if (data[4] == 't' && data[5] == 't' && data[6] == 'c' && data[7] == 'f') {
532
0
    return ProcessTTC(header, output, decompressed, out.Size(), index);
533
0
  } else {
534
0
    ots::Font font(header);
535
0
    return ProcessTTF(header, &font, output, decompressed, out.Size());
536
0
  }
537
0
}
538
539
0
ots::TableAction GetTableAction(const ots::FontFile *header, uint32_t tag) {
540
0
  ots::TableAction action = header->context->GetTableAction(tag);
541
0
542
0
  if (action == ots::TABLE_ACTION_DEFAULT) {
543
0
    action = ots::TABLE_ACTION_DROP;
544
0
545
0
    for (unsigned i = 0; ; ++i) {
546
0
      if (supported_tables[i].tag == 0) break;
547
0
548
0
      if (supported_tables[i].tag == tag) {
549
0
        action = ots::TABLE_ACTION_SANITIZE;
550
0
        break;
551
0
      }
552
0
    }
553
0
  }
554
0
555
0
  assert(action != ots::TABLE_ACTION_DEFAULT); // Should never return this.
556
0
  return action;
557
0
}
558
559
bool GetTableData(const uint8_t *data,
560
                  const ots::TableEntry& table,
561
                  ots::Arena &arena,
562
                  size_t *table_length,
563
0
                  const uint8_t **table_data) {
564
0
  if (table.uncompressed_length != table.length) {
565
0
    // Compressed table. Need to uncompress into memory first.
566
0
    *table_length = table.uncompressed_length;
567
0
    *table_data = arena.Allocate(*table_length);
568
0
    uLongf dest_len = *table_length;
569
0
    int r = uncompress((Bytef*) *table_data, &dest_len,
570
0
                       data + table.offset, table.length);
571
0
    if (r != Z_OK || dest_len != *table_length) {
572
0
      return false;
573
0
    }
574
0
  } else {
575
0
    // Uncompressed table. We can process directly from memory.
576
0
    *table_data = data + table.offset;
577
0
    *table_length = table.length;
578
0
  }
579
0
580
0
  return true;
581
0
}
582
583
bool ProcessGeneric(ots::FontFile *header,
584
                    ots::Font *font,
585
                    uint32_t signature,
586
                    ots::OTSStream *output,
587
                    const uint8_t *data, size_t length,
588
                    const std::vector<ots::TableEntry>& tables,
589
0
                    ots::Buffer& file) {
590
0
  const size_t data_offset = file.offset();
591
0
592
0
  uint32_t uncompressed_sum = 0;
593
0
594
0
  for (unsigned i = 0; i < font->num_tables; ++i) {
595
0
    // the tables must be sorted by tag (when taken as big-endian numbers).
596
0
    // This also remove the possibility of duplicate tables.
597
0
    if (i) {
598
0
      const uint32_t this_tag = tables[i].tag;
599
0
      const uint32_t prev_tag = tables[i - 1].tag;
600
0
      if (this_tag <= prev_tag) {
601
0
        OTS_WARNING_MSG_HDR("Table directory is not correctly ordered");
602
0
      }
603
0
    }
604
0
605
0
    // all tag names must be built from printable ASCII characters
606
0
    if (!ots::CheckTag(tables[i].tag)) {
607
0
      OTS_WARNING_MSG_HDR("Invalid table tag: 0x%X", tables[i].tag);
608
0
    }
609
0
610
0
    // tables must be 4-byte aligned
611
0
    if (tables[i].offset & 3) {
612
0
      return OTS_FAILURE_MSG_TAG("misaligned table", tables[i].tag);
613
0
    }
614
0
615
0
    // and must be within the file
616
0
    if (tables[i].offset < data_offset || tables[i].offset >= length) {
617
0
      return OTS_FAILURE_MSG_TAG("invalid table offset", tables[i].tag);
618
0
    }
619
0
    // disallow all tables with a zero length
620
0
    if (tables[i].length < 1) {
621
0
      // Note: malayalam.ttf has zero length CVT table...
622
0
      return OTS_FAILURE_MSG_TAG("zero-length table", tables[i].tag);
623
0
    }
624
0
    // disallow all tables with a length > 1GB
625
0
    if (tables[i].length > 1024 * 1024 * 1024) {
626
0
      return OTS_FAILURE_MSG_TAG("table length exceeds 1GB", tables[i].tag);
627
0
    }
628
0
    // disallow tables where the uncompressed size is < the compressed size.
629
0
    if (tables[i].uncompressed_length < tables[i].length) {
630
0
      return OTS_FAILURE_MSG_TAG("invalid compressed table", tables[i].tag);
631
0
    }
632
0
    if (tables[i].uncompressed_length > tables[i].length) {
633
0
      // We'll probably be decompressing this table.
634
0
635
0
      // disallow all tables which uncompress to > 30 MB
636
0
      if (tables[i].uncompressed_length > 30 * 1024 * 1024) {
637
0
        return OTS_FAILURE_MSG_TAG("uncompressed length exceeds 30MB", tables[i].tag);
638
0
      }
639
0
      if (uncompressed_sum + tables[i].uncompressed_length < uncompressed_sum) {
640
0
        return OTS_FAILURE_MSG_TAG("overflow of uncompressed sum", tables[i].tag);
641
0
      }
642
0
643
0
      uncompressed_sum += tables[i].uncompressed_length;
644
0
    }
645
0
    // since we required that the file be < 1GB in length, and that the table
646
0
    // length is < 1GB, the following addtion doesn't overflow
647
0
    uint32_t end_byte = tables[i].offset + tables[i].length;
648
0
    // Tables in the WOFF file must be aligned 4-byte boundary.
649
0
    if (signature == OTS_TAG('w','O','F','F')) {
650
0
        end_byte = ots::Round4(end_byte);
651
0
    }
652
0
    if (!end_byte || end_byte > length) {
653
0
      return OTS_FAILURE_MSG_TAG("table overruns end of file", tables[i].tag);
654
0
    }
655
0
  }
656
0
657
0
  // All decompressed tables uncompressed must be <= 30MB.
658
0
  if (uncompressed_sum > 30 * 1024 * 1024) {
659
0
    return OTS_FAILURE_MSG_HDR("uncompressed sum exceeds 30MB");
660
0
  }
661
0
662
0
  // check that the tables are not overlapping.
663
0
  std::vector<std::pair<uint32_t, uint8_t> > overlap_checker;
664
0
  for (unsigned i = 0; i < font->num_tables; ++i) {
665
0
    overlap_checker.push_back(
666
0
        std::make_pair(tables[i].offset, static_cast<uint8_t>(1) /* start */));
667
0
    overlap_checker.push_back(
668
0
        std::make_pair(tables[i].offset + tables[i].length,
669
0
                       static_cast<uint8_t>(0) /* end */));
670
0
  }
671
0
  std::sort(overlap_checker.begin(), overlap_checker.end());
672
0
  int overlap_count = 0;
673
0
  for (unsigned i = 0; i < overlap_checker.size(); ++i) {
674
0
    overlap_count += (overlap_checker[i].second ? 1 : -1);
675
0
    if (overlap_count > 1) {
676
0
      return OTS_FAILURE_MSG_HDR("overlapping tables");
677
0
    }
678
0
  }
679
0
680
0
  std::map<uint32_t, ots::TableEntry> table_map;
681
0
  for (unsigned i = 0; i < font->num_tables; ++i) {
682
0
    table_map[tables[i].tag] = tables[i];
683
0
  }
684
0
685
0
  ots::Arena arena;
686
0
  // Parse known tables first as we need to parse them in specific order.
687
0
  for (unsigned i = 0; ; ++i) {
688
0
    if (supported_tables[i].tag == 0) break;
689
0
690
0
    uint32_t tag = supported_tables[i].tag;
691
0
    const auto &it = table_map.find(tag);
692
0
    if (it == table_map.cend()) {
693
0
      if (supported_tables[i].required) {
694
0
        return OTS_FAILURE_MSG_TAG("missing required table", tag);
695
0
      }
696
0
    } else {
697
0
      if (!font->ParseTable(it->second, data, arena)) {
698
0
        return OTS_FAILURE_MSG_TAG("Failed to parse table", tag);
699
0
      }
700
0
    }
701
0
  }
702
0
703
0
  // Then parse any tables left.
704
0
  for (const auto &table_entry : tables) {
705
0
    if (!font->GetTable(table_entry.tag)) {
706
0
      if (!font->ParseTable(table_entry, data, arena)) {
707
0
        return OTS_FAILURE_MSG_TAG("Failed to parse table", table_entry.tag);
708
0
      }
709
0
    }
710
0
  }
711
0
712
0
  if (font->GetTable(OTS_TAG_CFF) || font->GetTable(OTS_TAG('C', 'F', 'F', '2'))) {
713
0
    // font with PostScript glyph
714
0
    if (font->version != OTS_TAG('O','T','T','O')) {
715
0
      return OTS_FAILURE_MSG_HDR("wrong font version for PostScript glyph data");
716
0
    }
717
0
    if (font->GetTable(OTS_TAG_GLYF) || font->GetTable(OTS_TAG_LOCA)) {
718
0
      // mixing outline formats is not recommended
719
0
      return OTS_FAILURE_MSG_HDR("font contains both PS and TT glyphs");
720
0
    }
721
0
  } else {
722
0
    if (!font->GetTable(OTS_TAG_GLYF) || !font->GetTable(OTS_TAG_LOCA)) {
723
0
      // No TrueType glyph found.
724
0
      //
725
0
      // We don't sanitize bitmap tables, but don’t reject bitmap-only fonts if
726
0
      // we are asked to pass them thru.
727
0
      // Also don’t reject if we are asked to pass glyf/loca thru.
728
0
      if (!font->GetTable(OTS_TAG('C','B','D','T')) &&
729
0
          !font->GetTable(OTS_TAG('C','B','L','C'))) {
730
0
        return OTS_FAILURE_MSG_HDR("no supported glyph shapes table(s) present");
731
0
      }
732
0
    }
733
0
  }
734
0
735
0
  uint16_t num_output_tables = 0;
736
0
  for (const auto &it : table_map) {
737
0
    ots::Table *table = font->GetTable(it.first);
738
0
    if (table != NULL && table->ShouldSerialize())
739
0
      num_output_tables++;
740
0
  }
741
0
742
0
  uint16_t max_pow2 = 0;
743
0
  while (1u << (max_pow2 + 1) <= num_output_tables) {
744
0
    max_pow2++;
745
0
  }
746
0
  const uint16_t output_search_range = (1u << max_pow2) << 4;
747
0
748
0
  // most of the errors here are highly unlikely - they'd only occur if the
749
0
  // output stream returns a failure, e.g. lack of space to write
750
0
  output->ResetChecksum();
751
0
  if (!output->WriteU32(font->version) ||
752
0
      !output->WriteU16(num_output_tables) ||
753
0
      !output->WriteU16(output_search_range) ||
754
0
      !output->WriteU16(max_pow2) ||
755
0
      !output->WriteU16((num_output_tables << 4) - output_search_range)) {
756
0
    return OTS_FAILURE_MSG_HDR("error writing output");
757
0
  }
758
0
  const uint32_t offset_table_chksum = output->chksum();
759
0
760
0
  const size_t table_record_offset = output->Tell();
761
0
  if (!output->Pad(16 * num_output_tables)) {
762
0
    return OTS_FAILURE_MSG_HDR("error writing output");
763
0
  }
764
0
765
0
  std::vector<ots::TableEntry> out_tables;
766
0
767
0
  size_t head_table_offset = 0;
768
0
  for (const auto &it : table_map) {
769
0
    uint32_t input_offset = it.second.offset;
770
0
    const auto &ot = header->table_entries.find(input_offset);
771
0
    if (ot != header->table_entries.end()) {
772
0
      ots::TableEntry out = ot->second;
773
0
      if (out.tag == OTS_TAG('h','e','a','d')) {
774
0
        head_table_offset = out.offset;
775
0
      }
776
0
      out_tables.push_back(out);
777
0
    } else {
778
0
      ots::TableEntry out;
779
0
      out.tag = it.first;
780
0
      out.offset = output->Tell();
781
0
782
0
      if (out.tag == OTS_TAG('h','e','a','d')) {
783
0
        head_table_offset = out.offset;
784
0
      }
785
0
786
0
      ots::Table *table = font->GetTable(out.tag);
787
0
      if (table != NULL && table->ShouldSerialize()) {
788
0
        output->ResetChecksum();
789
0
        if (!table->Serialize(output)) {
790
0
          return OTS_FAILURE_MSG_TAG("Failed to serialize table", out.tag);
791
0
        }
792
0
793
0
        const size_t end_offset = output->Tell();
794
0
        if (end_offset <= out.offset) {
795
0
          // paranoid check. |end_offset| is supposed to be greater than the offset,
796
0
          // as long as the Tell() interface is implemented correctly.
797
0
          return OTS_FAILURE_MSG_TAG("Table is empty or have -ve size", out.tag);
798
0
        }
799
0
        out.length = end_offset - out.offset;
800
0
801
0
        // align tables to four bytes
802
0
        if (!output->Pad((4 - (end_offset & 3)) % 4)) {
803
0
          return OTS_FAILURE_MSG_TAG("Failed to pad table to 4 bytes", out.tag);
804
0
        }
805
0
        out.chksum = output->chksum();
806
0
        out_tables.push_back(out);
807
0
        header->table_entries[input_offset] = out;
808
0
      }
809
0
    }
810
0
  }
811
0
812
0
  const size_t end_of_file = output->Tell();
813
0
814
0
  // Need to sort the output tables for inclusion in the file
815
0
  std::sort(out_tables.begin(), out_tables.end());
816
0
  if (!output->Seek(table_record_offset)) {
817
0
    return OTS_FAILURE_MSG_HDR("error writing output");
818
0
  }
819
0
820
0
  output->ResetChecksum();
821
0
  uint32_t tables_chksum = 0;
822
0
  for (unsigned i = 0; i < out_tables.size(); ++i) {
823
0
    if (!output->WriteU32(out_tables[i].tag) ||
824
0
        !output->WriteU32(out_tables[i].chksum) ||
825
0
        !output->WriteU32(out_tables[i].offset) ||
826
0
        !output->WriteU32(out_tables[i].length)) {
827
0
      return OTS_FAILURE_MSG_HDR("error writing output");
828
0
    }
829
0
    tables_chksum += out_tables[i].chksum;
830
0
  }
831
0
  const uint32_t table_record_chksum = output->chksum();
832
0
833
0
  // http://www.microsoft.com/typography/otspec/otff.htm
834
0
  const uint32_t file_chksum
835
0
      = offset_table_chksum + tables_chksum + table_record_chksum;
836
0
  const uint32_t chksum_magic = static_cast<uint32_t>(0xb1b0afba) - file_chksum;
837
0
838
0
  // seek into the 'head' table and write in the checksum magic value
839
0
  if (!head_table_offset) {
840
0
    return OTS_FAILURE_MSG_HDR("internal error!");
841
0
  }
842
0
  if (!output->Seek(head_table_offset + 8)) {
843
0
    return OTS_FAILURE_MSG_HDR("error writing output");
844
0
  }
845
0
  if (!output->WriteU32(chksum_magic)) {
846
0
    return OTS_FAILURE_MSG_HDR("error writing output");
847
0
  }
848
0
849
0
  if (!output->Seek(end_of_file)) {
850
0
    return OTS_FAILURE_MSG_HDR("error writing output");
851
0
  }
852
0
853
0
  return true;
854
0
}
855
856
}  // namespace
857
858
namespace ots {
859
860
0
FontFile::~FontFile() {
861
0
  for (const auto& it : tables) {
862
0
    delete it.second;
863
0
  }
864
0
  tables.clear();
865
0
}
866
867
bool Font::ParseTable(const TableEntry& table_entry, const uint8_t* data,
868
0
                      Arena &arena) {
869
0
  uint32_t tag = table_entry.tag;
870
0
  TableAction action = GetTableAction(file, tag);
871
0
  if (action == TABLE_ACTION_DROP) {
872
0
    return true;
873
0
  }
874
0
875
0
  const auto &it = file->tables.find(table_entry);
876
0
  if (it != file->tables.end()) {
877
0
    m_tables[tag] = it->second;
878
0
    return true;
879
0
  }
880
0
881
0
  Table *table = NULL;
882
0
  bool ret = false;
883
0
884
0
  if (action == TABLE_ACTION_PASSTHRU) {
885
0
    table = new TablePassthru(this, tag);
886
0
  } else {
887
0
    switch (tag) {
888
0
      case OTS_TAG_CFF:  table = new OpenTypeCFF(this,  tag); break;
889
0
      case OTS_TAG_CMAP: table = new OpenTypeCMAP(this, tag); break;
890
0
      case OTS_TAG_CVT:  table = new OpenTypeCVT(this,  tag); break;
891
0
      case OTS_TAG_FPGM: table = new OpenTypeFPGM(this, tag); break;
892
0
      case OTS_TAG_GASP: table = new OpenTypeGASP(this, tag); break;
893
0
      case OTS_TAG_GDEF: table = new OpenTypeGDEF(this, tag); break;
894
0
      case OTS_TAG_GLYF: table = new OpenTypeGLYF(this, tag); break;
895
0
      case OTS_TAG_GPOS: table = new OpenTypeGPOS(this, tag); break;
896
0
      case OTS_TAG_GSUB: table = new OpenTypeGSUB(this, tag); break;
897
0
      case OTS_TAG_HDMX: table = new OpenTypeHDMX(this, tag); break;
898
0
      case OTS_TAG_HEAD: table = new OpenTypeHEAD(this, tag); break;
899
0
      case OTS_TAG_HHEA: table = new OpenTypeHHEA(this, tag); break;
900
0
      case OTS_TAG_HMTX: table = new OpenTypeHMTX(this, tag); break;
901
0
      case OTS_TAG_KERN: table = new OpenTypeKERN(this, tag); break;
902
0
      case OTS_TAG_LOCA: table = new OpenTypeLOCA(this, tag); break;
903
0
      case OTS_TAG_LTSH: table = new OpenTypeLTSH(this, tag); break;
904
0
      case OTS_TAG_MATH: table = new OpenTypeMATH(this, tag); break;
905
0
      case OTS_TAG_MAXP: table = new OpenTypeMAXP(this, tag); break;
906
0
      case OTS_TAG_NAME: table = new OpenTypeNAME(this, tag); break;
907
0
      case OTS_TAG_OS2:  table = new OpenTypeOS2(this,  tag); break;
908
0
      case OTS_TAG_POST: table = new OpenTypePOST(this, tag); break;
909
0
      case OTS_TAG_PREP: table = new OpenTypePREP(this, tag); break;
910
0
      case OTS_TAG_VDMX: table = new OpenTypeVDMX(this, tag); break;
911
0
      case OTS_TAG_VORG: table = new OpenTypeVORG(this, tag); break;
912
0
      case OTS_TAG_VHEA: table = new OpenTypeVHEA(this, tag); break;
913
0
      case OTS_TAG_VMTX: table = new OpenTypeVMTX(this, tag); break;
914
0
      // Graphite tables
915
0
#ifdef OTS_GRAPHITE
916
0
      case OTS_TAG_FEAT: table = new OpenTypeFEAT(this, tag); break;
917
0
      case OTS_TAG_GLAT: table = new OpenTypeGLAT(this, tag); break;
918
0
      case OTS_TAG_GLOC: table = new OpenTypeGLOC(this, tag); break;
919
0
      case OTS_TAG_SILE: table = new OpenTypeSILE(this, tag); break;
920
0
      case OTS_TAG_SILF: table = new OpenTypeSILF(this, tag); break;
921
0
      case OTS_TAG_SILL: table = new OpenTypeSILL(this, tag); break;
922
0
#endif
923
0
#ifdef OTS_VARIATIONS
924
0
      case OTS_TAG_AVAR: table = new OpenTypeAVAR(this, tag); break;
925
0
      case OTS_TAG_CVAR: table = new OpenTypeCVAR(this, tag); break;
926
0
      case OTS_TAG_FVAR: table = new OpenTypeFVAR(this, tag); break;
927
0
      case OTS_TAG_GVAR: table = new OpenTypeGVAR(this, tag); break;
928
0
      case OTS_TAG_HVAR: table = new OpenTypeHVAR(this, tag); break;
929
0
      case OTS_TAG_MVAR: table = new OpenTypeMVAR(this, tag); break;
930
0
      case OTS_TAG_STAT: table = new OpenTypeSTAT(this, tag); break;
931
0
      case OTS_TAG_VVAR: table = new OpenTypeVVAR(this, tag); break;
932
0
#endif
933
0
      default: break;
934
0
    }
935
0
  }
936
0
937
0
  if (table) {
938
0
    const uint8_t* table_data;
939
0
    size_t table_length;
940
0
941
0
    ret = GetTableData(data, table_entry, arena, &table_length, &table_data);
942
0
    if (ret) {
943
0
      // FIXME: Parsing some tables will fail if the table is not added to
944
0
      // m_tables first.
945
0
      m_tables[tag] = table;
946
0
      ret = table->Parse(table_data, table_length);
947
0
      if (ret)
948
0
        file->tables[table_entry] = table;
949
0
      else
950
0
        m_tables.erase(tag);
951
0
    }
952
0
  }
953
0
954
0
  if (!ret)
955
0
    delete table;
956
0
957
0
  return ret;
958
0
}
959
960
0
Table* Font::GetTable(uint32_t tag) const {
961
0
  const auto &it = m_tables.find(tag);
962
0
  if (it != m_tables.end())
963
0
    return it->second;
964
0
  return NULL;
965
0
}
966
967
0
Table* Font::GetTypedTable(uint32_t tag) const {
968
0
  Table* t = GetTable(tag);
969
0
  if (t && t->Type() == tag)
970
0
    return t;
971
0
  return NULL;
972
0
}
973
974
0
void Font::DropGraphite() {
975
0
  file->context->Message(0, "Dropping all Graphite tables");
976
0
  for (const std::pair<uint32_t, Table*> entry : m_tables) {
977
0
    if (entry.first == OTS_TAG_FEAT ||
978
0
        entry.first == OTS_TAG_GLAT ||
979
0
        entry.first == OTS_TAG_GLOC ||
980
0
        entry.first == OTS_TAG_SILE ||
981
0
        entry.first == OTS_TAG_SILF ||
982
0
        entry.first == OTS_TAG_SILL) {
983
0
      entry.second->Drop("Discarding Graphite table");
984
0
    }
985
0
  }
986
0
  dropped_graphite = true;
987
0
}
988
989
0
void Font::DropVariations() {
990
0
  file->context->Message(0, "Dropping all Variation tables");
991
0
  for (const std::pair<uint32_t, Table*> entry : m_tables) {
992
0
    if (entry.first == OTS_TAG_AVAR ||
993
0
        entry.first == OTS_TAG_CVAR ||
994
0
        entry.first == OTS_TAG_FVAR ||
995
0
        entry.first == OTS_TAG_GVAR ||
996
0
        entry.first == OTS_TAG_HVAR ||
997
0
        entry.first == OTS_TAG_MVAR ||
998
0
        entry.first == OTS_TAG_STAT ||
999
0
        entry.first == OTS_TAG_VVAR) {
1000
0
      entry.second->Drop("Discarding Variations table");
1001
0
    }
1002
0
  }
1003
0
  dropped_variations = true;
1004
0
}
1005
1006
0
bool Table::ShouldSerialize() {
1007
0
  return m_shouldSerialize;
1008
0
}
1009
1010
0
void Table::Message(int level, const char *format, va_list va) {
1011
0
  char msg[206] = { OTS_UNTAG(m_tag), ':', ' ' };
1012
0
  std::vsnprintf(msg + 6, 200, format, va);
1013
0
  m_font->file->context->Message(level, msg);
1014
0
}
1015
1016
0
bool Table::Error(const char *format, ...) {
1017
0
  va_list va;
1018
0
  va_start(va, format);
1019
0
  Message(0, format, va);
1020
0
  va_end(va);
1021
0
1022
0
  return false;
1023
0
}
1024
1025
0
bool Table::Warning(const char *format, ...) {
1026
0
  va_list va;
1027
0
  va_start(va, format);
1028
0
  Message(1, format, va);
1029
0
  va_end(va);
1030
0
1031
0
  return true;
1032
0
}
1033
1034
0
bool Table::Drop(const char *format, ...) {
1035
0
  m_shouldSerialize = false;
1036
0
1037
0
  va_list va;
1038
0
  va_start(va, format);
1039
0
  Message(0, format, va);
1040
0
  m_font->file->context->Message(0, "Table discarded");
1041
0
  va_end(va);
1042
0
1043
0
  return true;
1044
0
}
1045
1046
0
bool Table::DropGraphite(const char *format, ...) {
1047
0
  va_list va;
1048
0
  va_start(va, format);
1049
0
  Message(0, format, va);
1050
0
  va_end(va);
1051
0
1052
0
  m_font->DropGraphite();
1053
0
  return true;
1054
0
}
1055
1056
0
bool Table::DropVariations(const char *format, ...) {
1057
0
  va_list va;
1058
0
  va_start(va, format);
1059
0
  Message(0, format, va);
1060
0
  va_end(va);
1061
0
1062
0
  m_font->DropVariations();
1063
0
  return true;
1064
0
}
1065
1066
0
bool TablePassthru::Parse(const uint8_t *data, size_t length) {
1067
0
  m_data = data;
1068
0
  m_length = length;
1069
0
  return true;
1070
0
}
1071
1072
0
bool TablePassthru::Serialize(OTSStream *out) {
1073
0
    if (!out->Write(m_data, m_length)) {
1074
0
    return Error("Failed to write table");
1075
0
  }
1076
0
1077
0
  return true;
1078
0
}
1079
1080
0
bool IsValidVersionTag(uint32_t tag) {
1081
0
  return tag == 0x000010000 ||
1082
0
         // OpenType fonts with CFF data have 'OTTO' tag.
1083
0
         tag == OTS_TAG('O','T','T','O') ||
1084
0
         // Older Mac fonts might have 'true' or 'typ1' tag.
1085
0
         tag == OTS_TAG('t','r','u','e') ||
1086
0
         tag == OTS_TAG('t','y','p','1');
1087
0
}
1088
1089
bool OTSContext::Process(OTSStream *output,
1090
                         const uint8_t *data,
1091
                         size_t length,
1092
0
                         uint32_t index) {
1093
0
  FontFile header;
1094
0
  Font font(&header);
1095
0
  header.context = this;
1096
0
1097
0
  if (length < 4) {
1098
0
    return OTS_FAILURE_MSG_(&header, "file less than 4 bytes");
1099
0
  }
1100
0
1101
0
  bool result;
1102
0
  if (data[0] == 'w' && data[1] == 'O' && data[2] == 'F' && data[3] == 'F') {
1103
0
    result = ProcessWOFF(&header, &font, output, data, length);
1104
0
  } else if (data[0] == 'w' && data[1] == 'O' && data[2] == 'F' && data[3] == '2') {
1105
0
    result = ProcessWOFF2(&header, output, data, length, index);
1106
0
  } else if (data[0] == 't' && data[1] == 't' && data[2] == 'c' && data[3] == 'f') {
1107
0
    result = ProcessTTC(&header, output, data, length, index);
1108
0
  } else {
1109
0
    result = ProcessTTF(&header, &font, output, data, length);
1110
0
  }
1111
0
1112
0
  return result;
1113
0
}
1114
1115
}  // namespace ots