OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 "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.h" | |
18 | |
19 // The OpenType Font File | |
20 // http://www.microsoft.com/typography/otspec/cmap.htm | |
21 | |
22 namespace { | |
23 | |
24 // Generate a message with or without a table tag, when 'header' is the OpenType
File pointer | |
25 #define OTS_FAILURE_MSG_TAG(msg_,tag_) OTS_FAILURE_MSG_TAG_(header, msg_, tag_) | |
26 #define OTS_FAILURE_MSG_HDR(msg_) OTS_FAILURE_MSG_(header, msg_) | |
27 | |
28 | |
29 struct OpenTypeTable { | |
30 uint32_t tag; | |
31 uint32_t chksum; | |
32 uint32_t offset; | |
33 uint32_t length; | |
34 uint32_t uncompressed_length; | |
35 }; | |
36 | |
37 bool CheckTag(uint32_t tag_value) { | |
38 for (unsigned i = 0; i < 4; ++i) { | |
39 const uint32_t check = tag_value & 0xff; | |
40 if (check < 32 || check > 126) { | |
41 return false; // non-ASCII character found. | |
42 } | |
43 tag_value >>= 8; | |
44 } | |
45 return true; | |
46 } | |
47 | |
48 uint32_t Tag(const char *tag_str) { | |
49 uint32_t ret; | |
50 std::memcpy(&ret, tag_str, 4); | |
51 return ret; | |
52 } | |
53 | |
54 struct OutputTable { | |
55 uint32_t tag; | |
56 size_t offset; | |
57 size_t length; | |
58 uint32_t chksum; | |
59 | |
60 static bool SortByTag(const OutputTable& a, const OutputTable& b) { | |
61 const uint32_t atag = ntohl(a.tag); | |
62 const uint32_t btag = ntohl(b.tag); | |
63 return atag < btag; | |
64 } | |
65 }; | |
66 | |
67 struct Arena { | |
68 public: | |
69 ~Arena() { | |
70 for (std::vector<uint8_t*>::iterator | |
71 i = hunks_.begin(); i != hunks_.end(); ++i) { | |
72 delete[] *i; | |
73 } | |
74 } | |
75 | |
76 uint8_t* Allocate(size_t length) { | |
77 uint8_t* p = new uint8_t[length]; | |
78 hunks_.push_back(p); | |
79 return p; | |
80 } | |
81 | |
82 private: | |
83 std::vector<uint8_t*> hunks_; | |
84 }; | |
85 | |
86 const struct { | |
87 const char* tag; | |
88 bool (*parse)(ots::OpenTypeFile *otf, const uint8_t *data, size_t length); | |
89 bool (*serialise)(ots::OTSStream *out, ots::OpenTypeFile *file); | |
90 bool (*should_serialise)(ots::OpenTypeFile *file); | |
91 void (*free)(ots::OpenTypeFile *file); | |
92 bool required; | |
93 } table_parsers[] = { | |
94 { "maxp", ots::ots_maxp_parse, ots::ots_maxp_serialise, | |
95 ots::ots_maxp_should_serialise, ots::ots_maxp_free, true }, | |
96 { "head", ots::ots_head_parse, ots::ots_head_serialise, | |
97 ots::ots_head_should_serialise, ots::ots_head_free, true }, | |
98 { "OS/2", ots::ots_os2_parse, ots::ots_os2_serialise, | |
99 ots::ots_os2_should_serialise, ots::ots_os2_free, true }, | |
100 { "cmap", ots::ots_cmap_parse, ots::ots_cmap_serialise, | |
101 ots::ots_cmap_should_serialise, ots::ots_cmap_free, true }, | |
102 { "hhea", ots::ots_hhea_parse, ots::ots_hhea_serialise, | |
103 ots::ots_hhea_should_serialise, ots::ots_hhea_free, true }, | |
104 { "hmtx", ots::ots_hmtx_parse, ots::ots_hmtx_serialise, | |
105 ots::ots_hmtx_should_serialise, ots::ots_hmtx_free, true }, | |
106 { "name", ots::ots_name_parse, ots::ots_name_serialise, | |
107 ots::ots_name_should_serialise, ots::ots_name_free, true }, | |
108 { "post", ots::ots_post_parse, ots::ots_post_serialise, | |
109 ots::ots_post_should_serialise, ots::ots_post_free, true }, | |
110 { "loca", ots::ots_loca_parse, ots::ots_loca_serialise, | |
111 ots::ots_loca_should_serialise, ots::ots_loca_free, false }, | |
112 { "glyf", ots::ots_glyf_parse, ots::ots_glyf_serialise, | |
113 ots::ots_glyf_should_serialise, ots::ots_glyf_free, false }, | |
114 { "CFF ", ots::ots_cff_parse, ots::ots_cff_serialise, | |
115 ots::ots_cff_should_serialise, ots::ots_cff_free, false }, | |
116 { "VDMX", ots::ots_vdmx_parse, ots::ots_vdmx_serialise, | |
117 ots::ots_vdmx_should_serialise, ots::ots_vdmx_free, false }, | |
118 { "hdmx", ots::ots_hdmx_parse, ots::ots_hdmx_serialise, | |
119 ots::ots_hdmx_should_serialise, ots::ots_hdmx_free, false }, | |
120 { "gasp", ots::ots_gasp_parse, ots::ots_gasp_serialise, | |
121 ots::ots_gasp_should_serialise, ots::ots_gasp_free, false }, | |
122 { "cvt ", ots::ots_cvt_parse, ots::ots_cvt_serialise, | |
123 ots::ots_cvt_should_serialise, ots::ots_cvt_free, false }, | |
124 { "fpgm", ots::ots_fpgm_parse, ots::ots_fpgm_serialise, | |
125 ots::ots_fpgm_should_serialise, ots::ots_fpgm_free, false }, | |
126 { "prep", ots::ots_prep_parse, ots::ots_prep_serialise, | |
127 ots::ots_prep_should_serialise, ots::ots_prep_free, false }, | |
128 { "LTSH", ots::ots_ltsh_parse, ots::ots_ltsh_serialise, | |
129 ots::ots_ltsh_should_serialise, ots::ots_ltsh_free, false }, | |
130 { "VORG", ots::ots_vorg_parse, ots::ots_vorg_serialise, | |
131 ots::ots_vorg_should_serialise, ots::ots_vorg_free, false }, | |
132 { "kern", ots::ots_kern_parse, ots::ots_kern_serialise, | |
133 ots::ots_kern_should_serialise, ots::ots_kern_free, false }, | |
134 // We need to parse GDEF table in advance of parsing GSUB/GPOS tables | |
135 // because they could refer GDEF table. | |
136 { "GDEF", ots::ots_gdef_parse, ots::ots_gdef_serialise, | |
137 ots::ots_gdef_should_serialise, ots::ots_gdef_free, false }, | |
138 { "GPOS", ots::ots_gpos_parse, ots::ots_gpos_serialise, | |
139 ots::ots_gpos_should_serialise, ots::ots_gpos_free, false }, | |
140 { "GSUB", ots::ots_gsub_parse, ots::ots_gsub_serialise, | |
141 ots::ots_gsub_should_serialise, ots::ots_gsub_free, false }, | |
142 { "vhea", ots::ots_vhea_parse, ots::ots_vhea_serialise, | |
143 ots::ots_vhea_should_serialise, ots::ots_vhea_free, false }, | |
144 { "vmtx", ots::ots_vmtx_parse, ots::ots_vmtx_serialise, | |
145 ots::ots_vmtx_should_serialise, ots::ots_vmtx_free, false }, | |
146 { "MATH", ots::ots_math_parse, ots::ots_math_serialise, | |
147 ots::ots_math_should_serialise, ots::ots_math_free, false }, | |
148 // TODO(bashi): Support mort, base, and jstf tables. | |
149 { 0, NULL, NULL, NULL, NULL, false }, | |
150 }; | |
151 | |
152 bool ProcessGeneric(ots::OpenTypeFile *header, | |
153 uint32_t signature, | |
154 ots::OTSStream *output, | |
155 const uint8_t *data, size_t length, | |
156 const std::vector<OpenTypeTable>& tables, | |
157 ots::Buffer& file); | |
158 | |
159 bool ProcessTTF(ots::OpenTypeFile *header, | |
160 ots::OTSStream *output, const uint8_t *data, size_t length) { | |
161 ots::Buffer file(data, length); | |
162 | |
163 // we disallow all files > 1GB in size for sanity. | |
164 if (length > 1024 * 1024 * 1024) { | |
165 return OTS_FAILURE_MSG_HDR("file exceeds 1GB"); | |
166 } | |
167 | |
168 if (!file.ReadTag(&header->version)) { | |
169 return OTS_FAILURE_MSG_HDR("error reading version tag"); | |
170 } | |
171 if (!ots::IsValidVersionTag(header->version)) { | |
172 return OTS_FAILURE_MSG_HDR("invalid version tag"); | |
173 } | |
174 | |
175 if (!file.ReadU16(&header->num_tables) || | |
176 !file.ReadU16(&header->search_range) || | |
177 !file.ReadU16(&header->entry_selector) || | |
178 !file.ReadU16(&header->range_shift)) { | |
179 return OTS_FAILURE_MSG_HDR("error reading table directory search header"); | |
180 } | |
181 | |
182 // search_range is (Maximum power of 2 <= numTables) x 16. Thus, to avoid | |
183 // overflow num_tables is, at most, 2^16 / 16 = 2^12 | |
184 if (header->num_tables >= 4096 || header->num_tables < 1) { | |
185 return OTS_FAILURE_MSG_HDR("excessive (or zero) number of tables"); | |
186 } | |
187 | |
188 unsigned max_pow2 = 0; | |
189 while (1u << (max_pow2 + 1) <= header->num_tables) { | |
190 max_pow2++; | |
191 } | |
192 const uint16_t expected_search_range = (1u << max_pow2) << 4; | |
193 | |
194 // Don't call ots_failure() here since ~25% of fonts (250+ fonts) in | |
195 // http://www.princexml.com/fonts/ have unexpected search_range value. | |
196 if (header->search_range != expected_search_range) { | |
197 OTS_FAILURE_MSG_HDR("bad search range"); | |
198 header->search_range = expected_search_range; // Fix the value. | |
199 } | |
200 | |
201 // entry_selector is Log2(maximum power of 2 <= numTables) | |
202 if (header->entry_selector != max_pow2) { | |
203 return OTS_FAILURE_MSG_HDR("incorrect entrySelector for table directory"); | |
204 } | |
205 | |
206 // range_shift is NumTables x 16-searchRange. We know that 16*num_tables | |
207 // doesn't over flow because we range checked it above. Also, we know that | |
208 // it's > header->search_range by construction of search_range. | |
209 const uint16_t expected_range_shift = | |
210 16 * header->num_tables - header->search_range; | |
211 if (header->range_shift != expected_range_shift) { | |
212 OTS_FAILURE_MSG_HDR("bad range shift"); | |
213 header->range_shift = expected_range_shift; // the same as above. | |
214 } | |
215 | |
216 // Next up is the list of tables. | |
217 std::vector<OpenTypeTable> tables; | |
218 | |
219 for (unsigned i = 0; i < header->num_tables; ++i) { | |
220 OpenTypeTable table; | |
221 if (!file.ReadTag(&table.tag) || | |
222 !file.ReadU32(&table.chksum) || | |
223 !file.ReadU32(&table.offset) || | |
224 !file.ReadU32(&table.length)) { | |
225 return OTS_FAILURE_MSG_HDR("error reading table directory"); | |
226 } | |
227 | |
228 table.uncompressed_length = table.length; | |
229 tables.push_back(table); | |
230 } | |
231 | |
232 return ProcessGeneric(header, header->version, output, data, length, | |
233 tables, file); | |
234 } | |
235 | |
236 bool ProcessWOFF(ots::OpenTypeFile *header, | |
237 ots::OTSStream *output, const uint8_t *data, size_t length) { | |
238 ots::Buffer file(data, length); | |
239 | |
240 // we disallow all files > 1GB in size for sanity. | |
241 if (length > 1024 * 1024 * 1024) { | |
242 return OTS_FAILURE_MSG_HDR("file exceeds 1GB"); | |
243 } | |
244 | |
245 uint32_t woff_tag; | |
246 if (!file.ReadTag(&woff_tag)) { | |
247 return OTS_FAILURE_MSG_HDR("error reading WOFF marker"); | |
248 } | |
249 | |
250 if (woff_tag != Tag("wOFF")) { | |
251 return OTS_FAILURE_MSG_HDR("invalid WOFF marker"); | |
252 } | |
253 | |
254 if (!file.ReadTag(&header->version)) { | |
255 return OTS_FAILURE_MSG_HDR("error reading version tag"); | |
256 } | |
257 if (!ots::IsValidVersionTag(header->version)) { | |
258 return OTS_FAILURE_MSG_HDR("invalid version tag"); | |
259 } | |
260 | |
261 header->search_range = 0; | |
262 header->entry_selector = 0; | |
263 header->range_shift = 0; | |
264 | |
265 uint32_t reported_length; | |
266 if (!file.ReadU32(&reported_length) || length != reported_length) { | |
267 return OTS_FAILURE_MSG_HDR("incorrect file size in WOFF header"); | |
268 } | |
269 | |
270 if (!file.ReadU16(&header->num_tables) || !header->num_tables) { | |
271 return OTS_FAILURE_MSG_HDR("error reading number of tables"); | |
272 } | |
273 | |
274 uint16_t reserved_value; | |
275 if (!file.ReadU16(&reserved_value) || reserved_value) { | |
276 return OTS_FAILURE_MSG_HDR("error in reserved field of WOFF header"); | |
277 } | |
278 | |
279 uint32_t reported_total_sfnt_size; | |
280 if (!file.ReadU32(&reported_total_sfnt_size)) { | |
281 return OTS_FAILURE_MSG_HDR("error reading total sfnt size"); | |
282 } | |
283 | |
284 // We don't care about these fields of the header: | |
285 // uint16_t major_version, minor_version | |
286 if (!file.Skip(2 * 2)) { | |
287 return OTS_FAILURE_MSG_HDR("error skipping WOFF header fields"); | |
288 } | |
289 | |
290 // Checks metadata block size. | |
291 uint32_t meta_offset; | |
292 uint32_t meta_length; | |
293 uint32_t meta_length_orig; | |
294 if (!file.ReadU32(&meta_offset) || | |
295 !file.ReadU32(&meta_length) || | |
296 !file.ReadU32(&meta_length_orig)) { | |
297 return OTS_FAILURE_MSG_HDR("error reading WOFF header fields"); | |
298 } | |
299 if (meta_offset) { | |
300 if (meta_offset >= length || length - meta_offset < meta_length) { | |
301 return OTS_FAILURE_MSG_HDR("invalid metadata block location/size"); | |
302 } | |
303 } | |
304 | |
305 // Checks private data block size. | |
306 uint32_t priv_offset; | |
307 uint32_t priv_length; | |
308 if (!file.ReadU32(&priv_offset) || | |
309 !file.ReadU32(&priv_length)) { | |
310 return OTS_FAILURE_MSG_HDR("error reading WOFF header fields"); | |
311 } | |
312 if (priv_offset) { | |
313 if (priv_offset >= length || length - priv_offset < priv_length) { | |
314 return OTS_FAILURE_MSG_HDR("invalid private block location/size"); | |
315 } | |
316 } | |
317 | |
318 // Next up is the list of tables. | |
319 std::vector<OpenTypeTable> tables; | |
320 | |
321 uint32_t first_index = 0; | |
322 uint32_t last_index = 0; | |
323 // Size of sfnt header plus size of table records. | |
324 uint64_t total_sfnt_size = 12 + 16 * header->num_tables; | |
325 for (unsigned i = 0; i < header->num_tables; ++i) { | |
326 OpenTypeTable table; | |
327 if (!file.ReadTag(&table.tag) || | |
328 !file.ReadU32(&table.offset) || | |
329 !file.ReadU32(&table.length) || | |
330 !file.ReadU32(&table.uncompressed_length) || | |
331 !file.ReadU32(&table.chksum)) { | |
332 return OTS_FAILURE_MSG_HDR("error reading table directory"); | |
333 } | |
334 | |
335 total_sfnt_size += ots::Round4(table.uncompressed_length); | |
336 if (total_sfnt_size > std::numeric_limits<uint32_t>::max()) { | |
337 return OTS_FAILURE_MSG_HDR("sfnt size overflow"); | |
338 } | |
339 tables.push_back(table); | |
340 if (i == 0 || tables[first_index].offset > table.offset) | |
341 first_index = i; | |
342 if (i == 0 || tables[last_index].offset < table.offset) | |
343 last_index = i; | |
344 } | |
345 | |
346 if (reported_total_sfnt_size != total_sfnt_size) { | |
347 return OTS_FAILURE_MSG_HDR("uncompressed sfnt size mismatch"); | |
348 } | |
349 | |
350 // Table data must follow immediately after the header. | |
351 if (tables[first_index].offset != ots::Round4(file.offset())) { | |
352 return OTS_FAILURE_MSG_HDR("junk before tables in WOFF file"); | |
353 } | |
354 | |
355 if (tables[last_index].offset >= length || | |
356 length - tables[last_index].offset < tables[last_index].length) { | |
357 return OTS_FAILURE_MSG_HDR("invalid table location/size"); | |
358 } | |
359 // Blocks must follow immediately after the previous block. | |
360 // (Except for padding with a maximum of three null bytes) | |
361 uint64_t block_end = ots::Round4( | |
362 static_cast<uint64_t>(tables[last_index].offset) + | |
363 static_cast<uint64_t>(tables[last_index].length)); | |
364 if (block_end > std::numeric_limits<uint32_t>::max()) { | |
365 return OTS_FAILURE_MSG_HDR("invalid table location/size"); | |
366 } | |
367 if (meta_offset) { | |
368 if (block_end != meta_offset) { | |
369 return OTS_FAILURE_MSG_HDR("invalid metadata block location"); | |
370 } | |
371 block_end = ots::Round4(static_cast<uint64_t>(meta_offset) + | |
372 static_cast<uint64_t>(meta_length)); | |
373 if (block_end > std::numeric_limits<uint32_t>::max()) { | |
374 return OTS_FAILURE_MSG_HDR("invalid metadata block size"); | |
375 } | |
376 } | |
377 if (priv_offset) { | |
378 if (block_end != priv_offset) { | |
379 return OTS_FAILURE_MSG_HDR("invalid private block location"); | |
380 } | |
381 block_end = ots::Round4(static_cast<uint64_t>(priv_offset) + | |
382 static_cast<uint64_t>(priv_length)); | |
383 if (block_end > std::numeric_limits<uint32_t>::max()) { | |
384 return OTS_FAILURE_MSG_HDR("invalid private block size"); | |
385 } | |
386 } | |
387 if (block_end != ots::Round4(length)) { | |
388 return OTS_FAILURE_MSG_HDR("file length mismatch (trailing junk?)"); | |
389 } | |
390 | |
391 return ProcessGeneric(header, woff_tag, output, data, length, tables, file); | |
392 } | |
393 | |
394 bool ProcessWOFF2(ots::OpenTypeFile *header, | |
395 ots::OTSStream *output, const uint8_t *data, size_t length) { | |
396 size_t decompressed_size = ots::ComputeWOFF2FinalSize(data, length); | |
397 if (decompressed_size == 0) { | |
398 return OTS_FAILURE(); | |
399 } | |
400 // decompressed font must be <= 30MB | |
401 if (decompressed_size > 30 * 1024 * 1024) { | |
402 return OTS_FAILURE(); | |
403 } | |
404 | |
405 std::vector<uint8_t> decompressed_buffer(decompressed_size); | |
406 if (!ots::ConvertWOFF2ToTTF(header, &decompressed_buffer[0], decompressed_size
, | |
407 data, length)) { | |
408 return OTS_FAILURE(); | |
409 } | |
410 return ProcessTTF(header, output, &decompressed_buffer[0], decompressed_size); | |
411 } | |
412 | |
413 ots::TableAction GetTableAction(ots::OpenTypeFile *header, uint32_t tag) { | |
414 ots::TableAction action = ots::TABLE_ACTION_DEFAULT; | |
415 | |
416 action = header->context->GetTableAction(htonl(tag)); | |
417 | |
418 if (action == ots::TABLE_ACTION_DEFAULT) { | |
419 action = ots::TABLE_ACTION_DROP; | |
420 | |
421 for (unsigned i = 0; ; ++i) { | |
422 if (table_parsers[i].parse == NULL) break; | |
423 | |
424 if (Tag(table_parsers[i].tag) == tag) { | |
425 action = ots::TABLE_ACTION_SANITIZE; | |
426 break; | |
427 } | |
428 } | |
429 } | |
430 | |
431 assert(action != ots::TABLE_ACTION_DEFAULT); // Should never return this. | |
432 return action; | |
433 } | |
434 | |
435 bool GetTableData(const uint8_t *data, | |
436 const OpenTypeTable table, | |
437 Arena *arena, | |
438 size_t *table_length, | |
439 const uint8_t **table_data) { | |
440 if (table.uncompressed_length != table.length) { | |
441 // Compressed table. Need to uncompress into memory first. | |
442 *table_length = table.uncompressed_length; | |
443 *table_data = (*arena).Allocate(*table_length); | |
444 uLongf dest_len = *table_length; | |
445 int r = uncompress((Bytef*) *table_data, &dest_len, | |
446 data + table.offset, table.length); | |
447 if (r != Z_OK || dest_len != *table_length) { | |
448 return false; | |
449 } | |
450 } else { | |
451 // Uncompressed table. We can process directly from memory. | |
452 *table_data = data + table.offset; | |
453 *table_length = table.length; | |
454 } | |
455 | |
456 return true; | |
457 } | |
458 | |
459 bool ProcessGeneric(ots::OpenTypeFile *header, uint32_t signature, | |
460 ots::OTSStream *output, | |
461 const uint8_t *data, size_t length, | |
462 const std::vector<OpenTypeTable>& tables, | |
463 ots::Buffer& file) { | |
464 const size_t data_offset = file.offset(); | |
465 | |
466 uint32_t uncompressed_sum = 0; | |
467 | |
468 for (unsigned i = 0; i < header->num_tables; ++i) { | |
469 // the tables must be sorted by tag (when taken as big-endian numbers). | |
470 // This also remove the possibility of duplicate tables. | |
471 if (i) { | |
472 const uint32_t this_tag = ntohl(tables[i].tag); | |
473 const uint32_t prev_tag = ntohl(tables[i - 1].tag); | |
474 if (this_tag <= prev_tag) { | |
475 return OTS_FAILURE_MSG_HDR("table directory not correctly ordered"); | |
476 } | |
477 } | |
478 | |
479 // all tag names must be built from printable ASCII characters | |
480 if (!CheckTag(tables[i].tag)) { | |
481 return OTS_FAILURE_MSG_TAG("invalid table tag", &tables[i].tag); | |
482 } | |
483 | |
484 // tables must be 4-byte aligned | |
485 if (tables[i].offset & 3) { | |
486 return OTS_FAILURE_MSG_TAG("misaligned table", &tables[i].tag); | |
487 } | |
488 | |
489 // and must be within the file | |
490 if (tables[i].offset < data_offset || tables[i].offset >= length) { | |
491 return OTS_FAILURE_MSG_TAG("invalid table offset", &tables[i].tag); | |
492 } | |
493 // disallow all tables with a zero length | |
494 if (tables[i].length < 1) { | |
495 // Note: malayalam.ttf has zero length CVT table... | |
496 return OTS_FAILURE_MSG_TAG("zero-length table", &tables[i].tag); | |
497 } | |
498 // disallow all tables with a length > 1GB | |
499 if (tables[i].length > 1024 * 1024 * 1024) { | |
500 return OTS_FAILURE_MSG_TAG("table length exceeds 1GB", &tables[i].tag); | |
501 } | |
502 // disallow tables where the uncompressed size is < the compressed size. | |
503 if (tables[i].uncompressed_length < tables[i].length) { | |
504 return OTS_FAILURE_MSG_TAG("invalid compressed table", &tables[i].tag); | |
505 } | |
506 if (tables[i].uncompressed_length > tables[i].length) { | |
507 // We'll probably be decompressing this table. | |
508 | |
509 // disallow all tables which uncompress to > 30 MB | |
510 if (tables[i].uncompressed_length > 30 * 1024 * 1024) { | |
511 return OTS_FAILURE_MSG_TAG("uncompressed length exceeds 30MB", &tables[i
].tag); | |
512 } | |
513 if (uncompressed_sum + tables[i].uncompressed_length < uncompressed_sum) { | |
514 return OTS_FAILURE_MSG_TAG("overflow of uncompressed sum", &tables[i].ta
g); | |
515 } | |
516 | |
517 uncompressed_sum += tables[i].uncompressed_length; | |
518 } | |
519 // since we required that the file be < 1GB in length, and that the table | |
520 // length is < 1GB, the following addtion doesn't overflow | |
521 uint32_t end_byte = tables[i].offset + tables[i].length; | |
522 // Tables in the WOFF file must be aligned 4-byte boundary. | |
523 if (signature == Tag("wOFF")) { | |
524 end_byte = ots::Round4(end_byte); | |
525 } | |
526 if (!end_byte || end_byte > length) { | |
527 return OTS_FAILURE_MSG_TAG("table overruns end of file", &tables[i].tag); | |
528 } | |
529 } | |
530 | |
531 // All decompressed tables uncompressed must be <= 30MB. | |
532 if (uncompressed_sum > 30 * 1024 * 1024) { | |
533 return OTS_FAILURE_MSG_HDR("uncompressed sum exceeds 30MB"); | |
534 } | |
535 | |
536 std::map<uint32_t, OpenTypeTable> table_map; | |
537 for (unsigned i = 0; i < header->num_tables; ++i) { | |
538 table_map[tables[i].tag] = tables[i]; | |
539 } | |
540 | |
541 // check that the tables are not overlapping. | |
542 std::vector<std::pair<uint32_t, uint8_t> > overlap_checker; | |
543 for (unsigned i = 0; i < header->num_tables; ++i) { | |
544 overlap_checker.push_back( | |
545 std::make_pair(tables[i].offset, static_cast<uint8_t>(1) /* start */)); | |
546 overlap_checker.push_back( | |
547 std::make_pair(tables[i].offset + tables[i].length, | |
548 static_cast<uint8_t>(0) /* end */)); | |
549 } | |
550 std::sort(overlap_checker.begin(), overlap_checker.end()); | |
551 int overlap_count = 0; | |
552 for (unsigned i = 0; i < overlap_checker.size(); ++i) { | |
553 overlap_count += (overlap_checker[i].second ? 1 : -1); | |
554 if (overlap_count > 1) { | |
555 return OTS_FAILURE_MSG_HDR("overlapping tables"); | |
556 } | |
557 } | |
558 | |
559 Arena arena; | |
560 | |
561 for (unsigned i = 0; ; ++i) { | |
562 if (table_parsers[i].parse == NULL) break; | |
563 | |
564 uint32_t tag = Tag(table_parsers[i].tag); | |
565 const std::map<uint32_t, OpenTypeTable>::const_iterator it = table_map.find(
tag); | |
566 | |
567 ots::TableAction action = GetTableAction(header, tag); | |
568 if (it == table_map.end()) { | |
569 if (table_parsers[i].required && action == ots::TABLE_ACTION_SANITIZE) { | |
570 return OTS_FAILURE_MSG_TAG("missing required table", table_parsers[i].ta
g); | |
571 } | |
572 continue; | |
573 } | |
574 | |
575 const uint8_t* table_data; | |
576 size_t table_length; | |
577 | |
578 if (!GetTableData(data, it->second, &arena, &table_length, &table_data)) { | |
579 return OTS_FAILURE_MSG_TAG("uncompress failed", table_parsers[i].tag); | |
580 } | |
581 | |
582 if (action == ots::TABLE_ACTION_SANITIZE && | |
583 !table_parsers[i].parse(header, table_data, table_length)) { | |
584 // TODO: parsers should generate specific messages detailing the failure; | |
585 // once those are all added, we won't need a generic failure message here | |
586 return OTS_FAILURE_MSG_TAG("failed to parse table", table_parsers[i].tag); | |
587 } | |
588 } | |
589 | |
590 if (header->cff) { | |
591 // font with PostScript glyph | |
592 if (header->version != Tag("OTTO")) { | |
593 return OTS_FAILURE_MSG_HDR("wrong font version for PostScript glyph data")
; | |
594 } | |
595 if (header->glyf || header->loca) { | |
596 // mixing outline formats is not recommended | |
597 return OTS_FAILURE_MSG_HDR("font contains both PS and TT glyphs"); | |
598 } | |
599 } else { | |
600 if (!header->glyf || !header->loca) { | |
601 // No TrueType glyph found. | |
602 // Note: bitmap-only fonts are not supported. | |
603 return OTS_FAILURE_MSG_HDR("neither PS nor TT glyphs present"); | |
604 } | |
605 } | |
606 | |
607 uint16_t num_output_tables = 0; | |
608 for (unsigned i = 0; ; ++i) { | |
609 if (table_parsers[i].parse == NULL) { | |
610 break; | |
611 } | |
612 | |
613 if (table_parsers[i].should_serialise(header)) { | |
614 num_output_tables++; | |
615 } | |
616 } | |
617 | |
618 for (std::map<uint32_t, OpenTypeTable>::const_iterator it = table_map.begin(); | |
619 it != table_map.end(); ++it) { | |
620 ots::TableAction action = GetTableAction(header, it->first); | |
621 if (action == ots::TABLE_ACTION_PASSTHRU) { | |
622 num_output_tables++; | |
623 } | |
624 } | |
625 | |
626 uint16_t max_pow2 = 0; | |
627 while (1u << (max_pow2 + 1) <= num_output_tables) { | |
628 max_pow2++; | |
629 } | |
630 const uint16_t output_search_range = (1u << max_pow2) << 4; | |
631 | |
632 // most of the errors here are highly unlikely - they'd only occur if the | |
633 // output stream returns a failure, e.g. lack of space to write | |
634 output->ResetChecksum(); | |
635 if (!output->WriteTag(header->version) || | |
636 !output->WriteU16(num_output_tables) || | |
637 !output->WriteU16(output_search_range) || | |
638 !output->WriteU16(max_pow2) || | |
639 !output->WriteU16((num_output_tables << 4) - output_search_range)) { | |
640 return OTS_FAILURE_MSG_HDR("error writing output"); | |
641 } | |
642 const uint32_t offset_table_chksum = output->chksum(); | |
643 | |
644 const size_t table_record_offset = output->Tell(); | |
645 if (!output->Pad(16 * num_output_tables)) { | |
646 return OTS_FAILURE_MSG_HDR("error writing output"); | |
647 } | |
648 | |
649 std::vector<OutputTable> out_tables; | |
650 | |
651 size_t head_table_offset = 0; | |
652 for (unsigned i = 0; ; ++i) { | |
653 if (table_parsers[i].parse == NULL) { | |
654 break; | |
655 } | |
656 | |
657 if (!table_parsers[i].should_serialise(header)) { | |
658 continue; | |
659 } | |
660 | |
661 OutputTable out; | |
662 uint32_t tag = Tag(table_parsers[i].tag); | |
663 out.tag = tag; | |
664 out.offset = output->Tell(); | |
665 | |
666 output->ResetChecksum(); | |
667 if (tag == Tag("head")) { | |
668 head_table_offset = out.offset; | |
669 } | |
670 if (!table_parsers[i].serialise(output, header)) { | |
671 return OTS_FAILURE_MSG_TAG("failed to serialize table", table_parsers[i].t
ag); | |
672 } | |
673 | |
674 const size_t end_offset = output->Tell(); | |
675 if (end_offset <= out.offset) { | |
676 // paranoid check. |end_offset| is supposed to be greater than the offset, | |
677 // as long as the Tell() interface is implemented correctly. | |
678 return OTS_FAILURE_MSG_HDR("error writing output"); | |
679 } | |
680 out.length = end_offset - out.offset; | |
681 | |
682 // align tables to four bytes | |
683 if (!output->Pad((4 - (end_offset & 3)) % 4)) { | |
684 return OTS_FAILURE_MSG_HDR("error writing output"); | |
685 } | |
686 out.chksum = output->chksum(); | |
687 out_tables.push_back(out); | |
688 } | |
689 | |
690 for (std::map<uint32_t, OpenTypeTable>::const_iterator it = table_map.begin(); | |
691 it != table_map.end(); ++it) { | |
692 ots::TableAction action = GetTableAction(header, it->first); | |
693 if (action == ots::TABLE_ACTION_PASSTHRU) { | |
694 OutputTable out; | |
695 out.tag = it->second.tag; | |
696 out.offset = output->Tell(); | |
697 | |
698 output->ResetChecksum(); | |
699 if (it->second.tag == Tag("head")) { | |
700 head_table_offset = out.offset; | |
701 } | |
702 | |
703 const uint8_t* table_data; | |
704 size_t table_length; | |
705 | |
706 if (!GetTableData(data, it->second, &arena, &table_length, &table_data)) { | |
707 return OTS_FAILURE_MSG_HDR("Failed to uncompress table"); | |
708 } | |
709 | |
710 if (!output->Write(table_data, table_length)) { | |
711 return OTS_FAILURE_MSG_HDR("Failed to serialize table"); | |
712 } | |
713 | |
714 const size_t end_offset = output->Tell(); | |
715 if (end_offset <= out.offset) { | |
716 // paranoid check. |end_offset| is supposed to be greater than the offse
t, | |
717 // as long as the Tell() interface is implemented correctly. | |
718 return OTS_FAILURE_MSG_HDR("error writing output"); | |
719 } | |
720 out.length = end_offset - out.offset; | |
721 | |
722 // align tables to four bytes | |
723 if (!output->Pad((4 - (end_offset & 3)) % 4)) { | |
724 return OTS_FAILURE_MSG_HDR("error writing output"); | |
725 } | |
726 out.chksum = output->chksum(); | |
727 out_tables.push_back(out); | |
728 } | |
729 } | |
730 | |
731 const size_t end_of_file = output->Tell(); | |
732 | |
733 // Need to sort the output tables for inclusion in the file | |
734 std::sort(out_tables.begin(), out_tables.end(), OutputTable::SortByTag); | |
735 if (!output->Seek(table_record_offset)) { | |
736 return OTS_FAILURE_MSG_HDR("error writing output"); | |
737 } | |
738 | |
739 output->ResetChecksum(); | |
740 uint32_t tables_chksum = 0; | |
741 for (unsigned i = 0; i < out_tables.size(); ++i) { | |
742 if (!output->WriteTag(out_tables[i].tag) || | |
743 !output->WriteU32(out_tables[i].chksum) || | |
744 !output->WriteU32(out_tables[i].offset) || | |
745 !output->WriteU32(out_tables[i].length)) { | |
746 return OTS_FAILURE_MSG_HDR("error writing output"); | |
747 } | |
748 tables_chksum += out_tables[i].chksum; | |
749 } | |
750 const uint32_t table_record_chksum = output->chksum(); | |
751 | |
752 // http://www.microsoft.com/typography/otspec/otff.htm | |
753 const uint32_t file_chksum | |
754 = offset_table_chksum + tables_chksum + table_record_chksum; | |
755 const uint32_t chksum_magic = static_cast<uint32_t>(0xb1b0afba) - file_chksum; | |
756 | |
757 // seek into the 'head' table and write in the checksum magic value | |
758 if (!head_table_offset) { | |
759 return OTS_FAILURE_MSG_HDR("internal error!"); | |
760 } | |
761 if (!output->Seek(head_table_offset + 8)) { | |
762 return OTS_FAILURE_MSG_HDR("error writing output"); | |
763 } | |
764 if (!output->WriteU32(chksum_magic)) { | |
765 return OTS_FAILURE_MSG_HDR("error writing output"); | |
766 } | |
767 | |
768 if (!output->Seek(end_of_file)) { | |
769 return OTS_FAILURE_MSG_HDR("error writing output"); | |
770 } | |
771 | |
772 return true; | |
773 } | |
774 | |
775 } // namespace | |
776 | |
777 namespace ots { | |
778 | |
779 void EnableWOFF2() { | |
780 } | |
781 | |
782 bool IsValidVersionTag(uint32_t tag) { | |
783 return tag == Tag("\x00\x01\x00\x00") || | |
784 // OpenType fonts with CFF data have 'OTTO' tag. | |
785 tag == Tag("OTTO") || | |
786 // Older Mac fonts might have 'true' or 'typ1' tag. | |
787 tag == Tag("true") || | |
788 tag == Tag("typ1"); | |
789 } | |
790 | |
791 bool OTSContext::Process(OTSStream *output, | |
792 const uint8_t *data, | |
793 size_t length) { | |
794 OpenTypeFile header; | |
795 | |
796 header.context = this; | |
797 | |
798 if (length < 4) { | |
799 return OTS_FAILURE_MSG_(&header, "file less than 4 bytes"); | |
800 } | |
801 | |
802 bool result; | |
803 if (data[0] == 'w' && data[1] == 'O' && data[2] == 'F' && data[3] == 'F') { | |
804 result = ProcessWOFF(&header, output, data, length); | |
805 } else if (data[0] == 'w' && data[1] == 'O' && data[2] == 'F' && data[3] == '2
') { | |
806 result = ProcessWOFF2(&header, output, data, length); | |
807 } else { | |
808 result = ProcessTTF(&header, output, data, length); | |
809 } | |
810 | |
811 for (unsigned i = 0; ; ++i) { | |
812 if (table_parsers[i].parse == NULL) break; | |
813 table_parsers[i].free(&header); | |
814 } | |
815 return result; | |
816 } | |
817 | |
818 // For backward compatibility | |
819 bool Process(OTSStream *output, const uint8_t *data, size_t length) { | |
820 static OTSContext context; | |
821 return context.Process(output, data, length); | |
822 } | |
823 | |
824 } // namespace ots | |
OLD | NEW |