Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: third_party/ots/src/woff2.cc

Issue 1433383003: Remove //third_party/brotli and //third_party/ots. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/ots/src/woff2.h ('k') | third_party/ots/test/BLACKLIST.txt » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 // This is the implementation of decompression of the proposed WOFF Ultra
6 // Condensed file format.
7
8 #include <cassert>
9 #include <cstdlib>
10 #include <vector>
11
12 #include "third_party/brotli/dec/decode.h"
13
14 #include "opentype-sanitiser.h"
15 #include "ots-memory-stream.h"
16 #include "ots.h"
17 #include "woff2.h"
18
19 #define TABLE_NAME "WOFF2"
20
21 namespace {
22
23 // simple glyph flags
24 const uint8_t kGlyfOnCurve = 1 << 0;
25 const uint8_t kGlyfXShort = 1 << 1;
26 const uint8_t kGlyfYShort = 1 << 2;
27 const uint8_t kGlyfRepeat = 1 << 3;
28 const uint8_t kGlyfThisXIsSame = 1 << 4;
29 const uint8_t kGlyfThisYIsSame = 1 << 5;
30
31 // composite glyph flags
32 const int FLAG_ARG_1_AND_2_ARE_WORDS = 1 << 0;
33 const int FLAG_WE_HAVE_A_SCALE = 1 << 3;
34 const int FLAG_MORE_COMPONENTS = 1 << 5;
35 const int FLAG_WE_HAVE_AN_X_AND_Y_SCALE = 1 << 6;
36 const int FLAG_WE_HAVE_A_TWO_BY_TWO = 1 << 7;
37 const int FLAG_WE_HAVE_INSTRUCTIONS = 1 << 8;
38
39 const size_t kSfntHeaderSize = 12;
40 const size_t kSfntEntrySize = 16;
41 const size_t kCheckSumAdjustmentOffset = 8;
42
43 const size_t kEndPtsOfContoursOffset = 10;
44 const size_t kCompositeGlyphBegin = 10;
45
46 // Note that the byte order is big-endian, not the same as ots.cc
47 #define TAG(a, b, c, d) ((a << 24) | (b << 16) | (c << 8) | d)
48 #define CHR(t) (t >> 24), (t >> 16), (t >> 8), (t >> 0)
49
50 const unsigned int kWoff2FlagsTransform = 1 << 5;
51
52 const uint32_t kKnownTags[] = {
53 TAG('c', 'm', 'a', 'p'), // 0
54 TAG('h', 'e', 'a', 'd'), // 1
55 TAG('h', 'h', 'e', 'a'), // 2
56 TAG('h', 'm', 't', 'x'), // 3
57 TAG('m', 'a', 'x', 'p'), // 4
58 TAG('n', 'a', 'm', 'e'), // 5
59 TAG('O', 'S', '/', '2'), // 6
60 TAG('p', 'o', 's', 't'), // 7
61 TAG('c', 'v', 't', ' '), // 8
62 TAG('f', 'p', 'g', 'm'), // 9
63 TAG('g', 'l', 'y', 'f'), // 10
64 TAG('l', 'o', 'c', 'a'), // 11
65 TAG('p', 'r', 'e', 'p'), // 12
66 TAG('C', 'F', 'F', ' '), // 13
67 TAG('V', 'O', 'R', 'G'), // 14
68 TAG('E', 'B', 'D', 'T'), // 15
69 TAG('E', 'B', 'L', 'C'), // 16
70 TAG('g', 'a', 's', 'p'), // 17
71 TAG('h', 'd', 'm', 'x'), // 18
72 TAG('k', 'e', 'r', 'n'), // 19
73 TAG('L', 'T', 'S', 'H'), // 20
74 TAG('P', 'C', 'L', 'T'), // 21
75 TAG('V', 'D', 'M', 'X'), // 22
76 TAG('v', 'h', 'e', 'a'), // 23
77 TAG('v', 'm', 't', 'x'), // 24
78 TAG('B', 'A', 'S', 'E'), // 25
79 TAG('G', 'D', 'E', 'F'), // 26
80 TAG('G', 'P', 'O', 'S'), // 27
81 TAG('G', 'S', 'U', 'B'), // 28
82 TAG('E', 'B', 'S', 'C'), // 29
83 TAG('J', 'S', 'T', 'F'), // 30
84 TAG('M', 'A', 'T', 'H'), // 31
85 TAG('C', 'B', 'D', 'T'), // 32
86 TAG('C', 'B', 'L', 'C'), // 33
87 TAG('C', 'O', 'L', 'R'), // 34
88 TAG('C', 'P', 'A', 'L'), // 35
89 TAG('S', 'V', 'G', ' '), // 36
90 TAG('s', 'b', 'i', 'x'), // 37
91 TAG('a', 'c', 'n', 't'), // 38
92 TAG('a', 'v', 'a', 'r'), // 39
93 TAG('b', 'd', 'a', 't'), // 40
94 TAG('b', 'l', 'o', 'c'), // 41
95 TAG('b', 's', 'l', 'n'), // 42
96 TAG('c', 'v', 'a', 'r'), // 43
97 TAG('f', 'd', 's', 'c'), // 44
98 TAG('f', 'e', 'a', 't'), // 45
99 TAG('f', 'm', 't', 'x'), // 46
100 TAG('f', 'v', 'a', 'r'), // 47
101 TAG('g', 'v', 'a', 'r'), // 48
102 TAG('h', 's', 't', 'y'), // 49
103 TAG('j', 'u', 's', 't'), // 50
104 TAG('l', 'c', 'a', 'r'), // 51
105 TAG('m', 'o', 'r', 't'), // 52
106 TAG('m', 'o', 'r', 'x'), // 53
107 TAG('o', 'p', 'b', 'd'), // 54
108 TAG('p', 'r', 'o', 'p'), // 55
109 TAG('t', 'r', 'a', 'k'), // 56
110 TAG('Z', 'a', 'p', 'f'), // 57
111 TAG('S', 'i', 'l', 'f'), // 58
112 TAG('G', 'l', 'a', 't'), // 59
113 TAG('G', 'l', 'o', 'c'), // 60
114 TAG('F', 'e', 'a', 't'), // 61
115 TAG('S', 'i', 'l', 'l'), // 62
116 };
117
118 struct Point {
119 int16_t x;
120 int16_t y;
121 bool on_curve;
122 };
123
124 struct Table {
125 uint32_t tag;
126 uint32_t flags;
127
128 uint32_t transform_length;
129
130 uint32_t dst_offset;
131 uint32_t dst_length;
132
133 Table()
134 : tag(0),
135 flags(0),
136 transform_length(0),
137 dst_offset(0),
138 dst_length(0) {}
139
140 bool operator<(const Table& other) const {
141 return tag < other.tag;
142 }
143 };
144
145 // Based on section 6.1.1 of MicroType Express draft spec
146 bool Read255UShort(ots::Buffer* buf, uint16_t* value) {
147 static const uint8_t kWordCode = 253;
148 static const uint8_t kOneMoreByteCode2 = 254;
149 static const uint8_t kOneMoreByteCode1 = 255;
150 static const uint8_t kLowestUCode = 253;
151 uint8_t code = 0;
152 if (!buf->ReadU8(&code)) {
153 return OTS_FAILURE();
154 }
155 if (code == kWordCode) {
156 uint16_t result = 0;
157 if (!buf->ReadU16(&result)) {
158 return OTS_FAILURE();
159 }
160 *value = result;
161 return true;
162 } else if (code == kOneMoreByteCode1) {
163 uint8_t result = 0;
164 if (!buf->ReadU8(&result)) {
165 return OTS_FAILURE();
166 }
167 *value = result + kLowestUCode;
168 return true;
169 } else if (code == kOneMoreByteCode2) {
170 uint8_t result = 0;
171 if (!buf->ReadU8(&result)) {
172 return OTS_FAILURE();
173 }
174 *value = result + kLowestUCode * 2;
175 return true;
176 } else {
177 *value = code;
178 return true;
179 }
180 }
181
182 bool ReadBase128(ots::Buffer* buf, uint32_t* value) {
183 uint32_t result = 0;
184 for (size_t i = 0; i < 5; ++i) {
185 uint8_t code = 0;
186 if (!buf->ReadU8(&code)) {
187 return OTS_FAILURE();
188 }
189 // If any of the top seven bits are set then we're about to overflow.
190 if (result & 0xfe000000U) {
191 return OTS_FAILURE();
192 }
193 result = (result << 7) | (code & 0x7f);
194 if ((code & 0x80) == 0) {
195 *value = result;
196 return true;
197 }
198 }
199 // Make sure not to exceed the size bound
200 return OTS_FAILURE();
201 }
202
203 // Caller must ensure that buffer overrun won't happen.
204 // TODO(ksakamaoto): Consider creating 'writer' version of the Buffer class
205 // and use it across the code.
206 size_t StoreU32(uint8_t* dst, size_t offset, uint32_t x) {
207 dst[offset] = x >> 24;
208 dst[offset + 1] = (x >> 16) & 0xff;
209 dst[offset + 2] = (x >> 8) & 0xff;
210 dst[offset + 3] = x & 0xff;
211 return offset + 4;
212 }
213
214 size_t StoreU16(uint8_t* dst, size_t offset, uint16_t x) {
215 dst[offset] = x >> 8;
216 dst[offset + 1] = x & 0xff;
217 return offset + 2;
218 }
219
220 int WithSign(int flag, int baseval) {
221 assert(0 <= baseval && baseval < 65536);
222 return (flag & 1) ? baseval : -baseval;
223 }
224
225 bool TripletDecode(const uint8_t* flags_in, const uint8_t* in, size_t in_size,
226 unsigned int n_points, std::vector<Point>* result,
227 size_t* in_bytes_consumed) {
228 int x = 0;
229 int y = 0;
230
231 // Early return if |in| buffer is too small. Each point consumes 1-4 bytes.
232 if (n_points > in_size) {
233 return OTS_FAILURE();
234 }
235 unsigned int triplet_index = 0;
236
237 for (unsigned int i = 0; i < n_points; ++i) {
238 uint8_t flag = flags_in[i];
239 bool on_curve = !(flag >> 7);
240 flag &= 0x7f;
241 unsigned int n_data_bytes;
242 if (flag < 84) {
243 n_data_bytes = 1;
244 } else if (flag < 120) {
245 n_data_bytes = 2;
246 } else if (flag < 124) {
247 n_data_bytes = 3;
248 } else {
249 n_data_bytes = 4;
250 }
251 if (triplet_index + n_data_bytes > in_size ||
252 triplet_index + n_data_bytes < triplet_index) {
253 return OTS_FAILURE();
254 }
255 int dx, dy;
256 if (flag < 10) {
257 dx = 0;
258 dy = WithSign(flag, ((flag & 14) << 7) + in[triplet_index]);
259 } else if (flag < 20) {
260 dx = WithSign(flag, (((flag - 10) & 14) << 7) + in[triplet_index]);
261 dy = 0;
262 } else if (flag < 84) {
263 int b0 = flag - 20;
264 int b1 = in[triplet_index];
265 dx = WithSign(flag, 1 + (b0 & 0x30) + (b1 >> 4));
266 dy = WithSign(flag >> 1, 1 + ((b0 & 0x0c) << 2) + (b1 & 0x0f));
267 } else if (flag < 120) {
268 int b0 = flag - 84;
269 dx = WithSign(flag, 1 + ((b0 / 12) << 8) + in[triplet_index]);
270 dy = WithSign(flag >> 1,
271 1 + (((b0 % 12) >> 2) << 8) + in[triplet_index + 1]);
272 } else if (flag < 124) {
273 int b2 = in[triplet_index + 1];
274 dx = WithSign(flag, (in[triplet_index] << 4) + (b2 >> 4));
275 dy = WithSign(flag >> 1, ((b2 & 0x0f) << 8) + in[triplet_index + 2]);
276 } else {
277 dx = WithSign(flag, (in[triplet_index] << 8) + in[triplet_index + 1]);
278 dy = WithSign(flag >> 1,
279 (in[triplet_index + 2] << 8) + in[triplet_index + 3]);
280 }
281 triplet_index += n_data_bytes;
282 // Possible overflow but coordinate values are not security sensitive
283 x += dx;
284 y += dy;
285 result->push_back(Point());
286 Point& back = result->back();
287 back.x = static_cast<int16_t>(x);
288 back.y = static_cast<int16_t>(y);
289 back.on_curve = on_curve;
290 }
291 *in_bytes_consumed = triplet_index;
292 return true;
293 }
294
295 // This function stores just the point data. On entry, dst points to the
296 // beginning of a simple glyph. Returns true on success.
297 bool StorePoints(const std::vector<Point>& points,
298 unsigned int n_contours, unsigned int instruction_length,
299 uint8_t* dst, size_t dst_size, size_t* glyph_size) {
300 // I believe that n_contours < 65536, in which case this is safe. However, a
301 // comment and/or an assert would be good.
302 unsigned int flag_offset = kEndPtsOfContoursOffset + 2 * n_contours + 2 +
303 instruction_length;
304 uint8_t last_flag = 0xff;
305 uint8_t repeat_count = 0;
306 int last_x = 0;
307 int last_y = 0;
308 unsigned int x_bytes = 0;
309 unsigned int y_bytes = 0;
310
311 for (size_t i = 0; i < points.size(); ++i) {
312 const Point& point = points.at(i);
313 uint8_t flag = point.on_curve ? kGlyfOnCurve : 0;
314 int dx = point.x - last_x;
315 int dy = point.y - last_y;
316 if (dx == 0) {
317 flag |= kGlyfThisXIsSame;
318 } else if (dx > -256 && dx < 256) {
319 flag |= kGlyfXShort | (dx > 0 ? kGlyfThisXIsSame : 0);
320 x_bytes += 1;
321 } else {
322 x_bytes += 2;
323 }
324 if (dy == 0) {
325 flag |= kGlyfThisYIsSame;
326 } else if (dy > -256 && dy < 256) {
327 flag |= kGlyfYShort | (dy > 0 ? kGlyfThisYIsSame : 0);
328 y_bytes += 1;
329 } else {
330 y_bytes += 2;
331 }
332
333 if (flag == last_flag && repeat_count != 255) {
334 dst[flag_offset - 1] |= kGlyfRepeat;
335 repeat_count++;
336 } else {
337 if (repeat_count != 0) {
338 if (flag_offset >= dst_size) {
339 return OTS_FAILURE();
340 }
341 dst[flag_offset++] = repeat_count;
342 }
343 if (flag_offset >= dst_size) {
344 return OTS_FAILURE();
345 }
346 dst[flag_offset++] = flag;
347 repeat_count = 0;
348 }
349 last_x = point.x;
350 last_y = point.y;
351 last_flag = flag;
352 }
353
354 if (repeat_count != 0) {
355 if (flag_offset >= dst_size) {
356 return OTS_FAILURE();
357 }
358 dst[flag_offset++] = repeat_count;
359 }
360 unsigned int xy_bytes = x_bytes + y_bytes;
361 if (xy_bytes < x_bytes ||
362 flag_offset + xy_bytes < flag_offset ||
363 flag_offset + xy_bytes > dst_size) {
364 return OTS_FAILURE();
365 }
366
367 int x_offset = flag_offset;
368 int y_offset = flag_offset + x_bytes;
369 last_x = 0;
370 last_y = 0;
371 for (size_t i = 0; i < points.size(); ++i) {
372 int dx = points.at(i).x - last_x;
373 if (dx == 0) {
374 // pass
375 } else if (dx > -256 && dx < 256) {
376 dst[x_offset++] = static_cast<uint8_t>(std::abs(dx));
377 } else {
378 // will always fit for valid input, but overflow is harmless
379 x_offset = StoreU16(dst, x_offset, static_cast<uint16_t>(dx));
380 }
381 last_x += dx;
382 int dy = points.at(i).y - last_y;
383 if (dy == 0) {
384 // pass
385 } else if (dy > -256 && dy < 256) {
386 dst[y_offset++] = static_cast<uint8_t>(std::abs(dy));
387 } else {
388 y_offset = StoreU16(dst, y_offset, static_cast<uint16_t>(dy));
389 }
390 last_y += dy;
391 }
392 *glyph_size = y_offset;
393 return true;
394 }
395
396 // Compute the bounding box of the coordinates, and store into a glyf buffer.
397 // A precondition is that there are at least 10 bytes available.
398 void ComputeBbox(const std::vector<Point>& points, uint8_t* dst) {
399 int16_t x_min = 0;
400 int16_t y_min = 0;
401 int16_t x_max = 0;
402 int16_t y_max = 0;
403
404 for (size_t i = 0; i < points.size(); ++i) {
405 int16_t x = points.at(i).x;
406 int16_t y = points.at(i).y;
407 if (i == 0 || x < x_min) x_min = x;
408 if (i == 0 || x > x_max) x_max = x;
409 if (i == 0 || y < y_min) y_min = y;
410 if (i == 0 || y > y_max) y_max = y;
411 }
412 size_t offset = 2;
413 offset = StoreU16(dst, offset, x_min);
414 offset = StoreU16(dst, offset, y_min);
415 offset = StoreU16(dst, offset, x_max);
416 offset = StoreU16(dst, offset, y_max);
417 }
418
419 // Process entire bbox stream. This is done as a separate pass to allow for
420 // composite bbox computations (an optional more aggressive transform).
421 bool ProcessBboxStream(ots::Buffer* bbox_stream, unsigned int n_glyphs,
422 const std::vector<uint32_t>& loca_values, uint8_t* glyf_buf,
423 size_t glyf_buf_length) {
424 const uint8_t* buf = bbox_stream->buffer();
425 if (n_glyphs >= 65536 || loca_values.size() != n_glyphs + 1) {
426 return OTS_FAILURE();
427 }
428 // Safe because n_glyphs is bounded
429 unsigned int bitmap_length = ((n_glyphs + 31) >> 5) << 2;
430 if (!bbox_stream->Skip(bitmap_length)) {
431 return OTS_FAILURE();
432 }
433 for (unsigned int i = 0; i < n_glyphs; ++i) {
434 if (buf[i >> 3] & (0x80 >> (i & 7))) {
435 uint32_t loca_offset = loca_values.at(i);
436 if (loca_values.at(i + 1) - loca_offset < kEndPtsOfContoursOffset) {
437 return OTS_FAILURE();
438 }
439 if (glyf_buf_length < 2 + 10 ||
440 loca_offset > glyf_buf_length - 2 - 10) {
441 return OTS_FAILURE();
442 }
443 if (!bbox_stream->Read(glyf_buf + loca_offset + 2, 8)) {
444 return OTS_FAILURE();
445 }
446 }
447 }
448 return true;
449 }
450
451 bool ProcessComposite(ots::Buffer* composite_stream, uint8_t* dst,
452 size_t dst_size, size_t* glyph_size, bool* have_instructions) {
453 size_t start_offset = composite_stream->offset();
454 bool we_have_instructions = false;
455
456 uint16_t flags = FLAG_MORE_COMPONENTS;
457 while (flags & FLAG_MORE_COMPONENTS) {
458 if (!composite_stream->ReadU16(&flags)) {
459 return OTS_FAILURE();
460 }
461 we_have_instructions |= (flags & FLAG_WE_HAVE_INSTRUCTIONS) != 0;
462 size_t arg_size = 2; // glyph index
463 if (flags & FLAG_ARG_1_AND_2_ARE_WORDS) {
464 arg_size += 4;
465 } else {
466 arg_size += 2;
467 }
468 if (flags & FLAG_WE_HAVE_A_SCALE) {
469 arg_size += 2;
470 } else if (flags & FLAG_WE_HAVE_AN_X_AND_Y_SCALE) {
471 arg_size += 4;
472 } else if (flags & FLAG_WE_HAVE_A_TWO_BY_TWO) {
473 arg_size += 8;
474 }
475 if (!composite_stream->Skip(arg_size)) {
476 return OTS_FAILURE();
477 }
478 }
479 size_t composite_glyph_size = composite_stream->offset() - start_offset;
480 if (composite_glyph_size + kCompositeGlyphBegin > dst_size) {
481 return OTS_FAILURE();
482 }
483 StoreU16(dst, 0, 0xffff); // nContours = -1 for composite glyph
484 std::memcpy(dst + kCompositeGlyphBegin,
485 composite_stream->buffer() + start_offset,
486 composite_glyph_size);
487 *glyph_size = kCompositeGlyphBegin + composite_glyph_size;
488 *have_instructions = we_have_instructions;
489 return true;
490 }
491
492 // Build TrueType loca table
493 bool StoreLoca(const std::vector<uint32_t>& loca_values, int index_format,
494 uint8_t* dst, size_t dst_size) {
495 const uint64_t loca_size = loca_values.size();
496 const uint64_t offset_size = index_format ? 4 : 2;
497 if ((loca_size << 2) >> 2 != loca_size) {
498 return OTS_FAILURE();
499 }
500 // No integer overflow here (loca_size <= 2^16).
501 if (offset_size * loca_size > dst_size) {
502 return OTS_FAILURE();
503 }
504 size_t offset = 0;
505 for (size_t i = 0; i < loca_values.size(); ++i) {
506 uint32_t value = loca_values.at(i);
507 if (index_format) {
508 offset = StoreU32(dst, offset, value);
509 } else {
510 offset = StoreU16(dst, offset, static_cast<uint16_t>(value >> 1));
511 }
512 }
513 return true;
514 }
515
516 // Reconstruct entire glyf table based on transformed original
517 bool ReconstructGlyf(ots::OpenTypeFile* file,
518 const uint8_t* data, size_t data_size,
519 uint8_t* dst, size_t dst_size,
520 uint8_t* loca_buf, size_t loca_size) {
521 static const int kNumSubStreams = 7;
522 ots::Buffer buffer(data, data_size);
523 uint32_t version;
524 std::vector<std::pair<const uint8_t*, size_t> > substreams(kNumSubStreams);
525
526 if (!buffer.ReadU32(&version)) {
527 return OTS_FAILURE_MSG("Failed to read 'version' of transformed 'glyf' table ");
528 }
529 uint16_t num_glyphs;
530 if (!buffer.ReadU16(&num_glyphs)) {
531 return OTS_FAILURE_MSG("Failed to read 'numGlyphs' from transformed 'glyf' t able");
532 }
533 uint16_t index_format;
534 if (!buffer.ReadU16(&index_format)) {
535 return OTS_FAILURE_MSG("Failed to read 'indexFormat' from transformed 'glyf' table");
536 }
537 unsigned int offset = (2 + kNumSubStreams) * 4;
538 if (offset > data_size) {
539 return OTS_FAILURE_MSG("Size of transformed 'glyf' table is too small to fit its data");
540 }
541 // Invariant from here on: data_size >= offset
542 for (int i = 0; i < kNumSubStreams; ++i) {
543 uint32_t substream_size;
544 if (!buffer.ReadU32(&substream_size)) {
545 return OTS_FAILURE_MSG("Failed to read substream size %d of transformed 'g lyf' table", i);
546 }
547 if (substream_size > data_size - offset) {
548 return OTS_FAILURE_MSG("Size of substream %d of transformed 'glyf' table d oes not fit in table size");
549 }
550 substreams.at(i) = std::make_pair(data + offset, substream_size);
551 offset += substream_size;
552 }
553 ots::Buffer n_contour_stream(substreams.at(0).first, substreams.at(0).second);
554 ots::Buffer n_points_stream(substreams.at(1).first, substreams.at(1).second);
555 ots::Buffer flag_stream(substreams.at(2).first, substreams.at(2).second);
556 ots::Buffer glyph_stream(substreams.at(3).first, substreams.at(3).second);
557 ots::Buffer composite_stream(substreams.at(4).first, substreams.at(4).second);
558 ots::Buffer bbox_stream(substreams.at(5).first, substreams.at(5).second);
559 ots::Buffer instruction_stream(substreams.at(6).first,
560 substreams.at(6).second);
561
562 std::vector<uint32_t> loca_values;
563 loca_values.reserve(num_glyphs + 1);
564 std::vector<uint16_t> n_points_vec;
565 std::vector<Point> points;
566 uint32_t loca_offset = 0;
567 for (unsigned int i = 0; i < num_glyphs; ++i) {
568 size_t glyph_size = 0;
569 uint16_t n_contours = 0;
570 if (!n_contour_stream.ReadU16(&n_contours)) {
571 return OTS_FAILURE_MSG("Filed to read 'numberOfContours' of glyph %d from transformed 'glyf' table", i);
572 }
573 uint8_t* glyf_dst = dst + loca_offset;
574 size_t glyf_dst_size = dst_size - loca_offset;
575 if (n_contours == 0xffff) {
576 // composite glyph
577 bool have_instructions = false;
578 uint16_t instruction_size = 0;
579 if (!ProcessComposite(&composite_stream, glyf_dst, glyf_dst_size,
580 &glyph_size, &have_instructions)) {
581 return OTS_FAILURE_MSG("Filed to process composite glyph %d from transfo rmed 'glyf' table", i);
582 }
583 if (have_instructions) {
584 if (!Read255UShort(&glyph_stream, &instruction_size)) {
585 return OTS_FAILURE_MSG("Failed to read 'instructionLength' of glyph %d from transformed 'glyf' table", i);
586 }
587 // No integer overflow here (instruction_size < 2^16).
588 if (instruction_size + 2U > glyf_dst_size - glyph_size) {
589 return OTS_FAILURE_MSG("'instructionLength' of glyph %d from transform ed 'glyf' table does not fit in the destination glyph size", i);
590 }
591 StoreU16(glyf_dst, glyph_size, instruction_size);
592 if (!instruction_stream.Read(glyf_dst + glyph_size + 2,
593 instruction_size)) {
594 return OTS_FAILURE_MSG("Filed to read instructions of glyph %d from tr ansformed 'glyf' table", i);
595 }
596 glyph_size += instruction_size + 2;
597 }
598 } else if (n_contours > 0) {
599 // simple glyph
600 n_points_vec.clear();
601 points.clear();
602 uint32_t total_n_points = 0;
603 uint16_t n_points_contour;
604 for (uint32_t j = 0; j < n_contours; ++j) {
605 if (!Read255UShort(&n_points_stream, &n_points_contour)) {
606 return OTS_FAILURE_MSG("Filed to read number of points of contour %d o f glyph %d from transformed 'glyf' table", j, i);
607 }
608 n_points_vec.push_back(n_points_contour);
609 if (total_n_points + n_points_contour < total_n_points) {
610 return OTS_FAILURE_MSG("Negative number of points of contour %d of gly ph %d from transformed 'glyf' table", j, i);
611 }
612 total_n_points += n_points_contour;
613 }
614 uint32_t flag_size = total_n_points;
615 if (flag_size > flag_stream.length() - flag_stream.offset()) {
616 return OTS_FAILURE();
617 }
618 const uint8_t* flags_buf = flag_stream.buffer() + flag_stream.offset();
619 const uint8_t* triplet_buf = glyph_stream.buffer() +
620 glyph_stream.offset();
621 size_t triplet_size = glyph_stream.length() - glyph_stream.offset();
622 size_t triplet_bytes_consumed = 0;
623 if (!TripletDecode(flags_buf, triplet_buf, triplet_size, total_n_points,
624 &points, &triplet_bytes_consumed)) {
625 return OTS_FAILURE();
626 }
627 const uint32_t header_and_endpts_contours_size =
628 kEndPtsOfContoursOffset + 2 * n_contours;
629 if (glyf_dst_size < header_and_endpts_contours_size) {
630 return OTS_FAILURE();
631 }
632 StoreU16(glyf_dst, 0, n_contours);
633 ComputeBbox(points, glyf_dst);
634 size_t endpts_offset = kEndPtsOfContoursOffset;
635 int end_point = -1;
636 for (unsigned int contour_ix = 0; contour_ix < n_contours; ++contour_ix) {
637 end_point += n_points_vec.at(contour_ix);
638 if (end_point >= 65536) {
639 return OTS_FAILURE();
640 }
641 endpts_offset = StoreU16(glyf_dst, endpts_offset, static_cast<uint16_t>( end_point));
642 }
643 if (!flag_stream.Skip(flag_size)) {
644 return OTS_FAILURE();
645 }
646 if (!glyph_stream.Skip(triplet_bytes_consumed)) {
647 return OTS_FAILURE();
648 }
649 uint16_t instruction_size;
650 if (!Read255UShort(&glyph_stream, &instruction_size)) {
651 return OTS_FAILURE();
652 }
653 // No integer overflow here (instruction_size < 2^16).
654 if (glyf_dst_size - header_and_endpts_contours_size <
655 instruction_size + 2U) {
656 return OTS_FAILURE();
657 }
658 uint8_t* instruction_dst = glyf_dst + header_and_endpts_contours_size;
659 StoreU16(instruction_dst, 0, instruction_size);
660 if (!instruction_stream.Read(instruction_dst + 2, instruction_size)) {
661 return OTS_FAILURE();
662 }
663 if (!StorePoints(points, n_contours, instruction_size,
664 glyf_dst, glyf_dst_size, &glyph_size)) {
665 return OTS_FAILURE_MSG("Failed to store points of glyph %d from the tran sformed 'glyf' table", i);
666 }
667 } else {
668 glyph_size = 0;
669 }
670 loca_values.push_back(loca_offset);
671 if (glyph_size + 3 < glyph_size) {
672 return OTS_FAILURE();
673 }
674 glyph_size = ots::Round2(glyph_size);
675 if (glyph_size > dst_size - loca_offset) {
676 // This shouldn't happen, but this test defensively maintains the
677 // invariant that loca_offset <= dst_size.
678 return OTS_FAILURE();
679 }
680 loca_offset += glyph_size;
681 }
682 loca_values.push_back(loca_offset);
683 assert(loca_values.size() == static_cast<size_t>(num_glyphs + 1));
684 if (!ProcessBboxStream(&bbox_stream, num_glyphs, loca_values,
685 dst, dst_size)) {
686 return OTS_FAILURE_MSG("Filed to process 'bboxStream' from the transformed ' glyf' table");
687 }
688 return StoreLoca(loca_values, index_format, loca_buf, loca_size);
689 }
690
691 // This is linear search, but could be changed to binary because we
692 // do have a guarantee that the tables are sorted by tag. But the total
693 // cpu time is expected to be very small in any case.
694 const Table* FindTable(const std::vector<Table>& tables, uint32_t tag) {
695 size_t n_tables = tables.size();
696 for (size_t i = 0; i < n_tables; ++i) {
697 if (tables.at(i).tag == tag) {
698 return &tables.at(i);
699 }
700 }
701 return NULL;
702 }
703
704 bool ReconstructTransformed(ots::OpenTypeFile* file,
705 const std::vector<Table>& tables, uint32_t tag,
706 const uint8_t* transformed_buf, size_t transformed_size,
707 uint8_t* dst, size_t dst_length) {
708 if (tag == TAG('g', 'l', 'y', 'f')) {
709 const Table* glyf_table = FindTable(tables, tag);
710 const Table* loca_table = FindTable(tables, TAG('l', 'o', 'c', 'a'));
711 if (glyf_table == NULL || loca_table == NULL) {
712 return OTS_FAILURE();
713 }
714 if (static_cast<uint64_t>(glyf_table->dst_offset) + glyf_table->dst_length >
715 dst_length) {
716 return OTS_FAILURE();
717 }
718 if (static_cast<uint64_t>(loca_table->dst_offset) + loca_table->dst_length >
719 dst_length) {
720 return OTS_FAILURE();
721 }
722 return ReconstructGlyf(file, transformed_buf, transformed_size,
723 dst + glyf_table->dst_offset, glyf_table->dst_length,
724 dst + loca_table->dst_offset, loca_table->dst_length);
725 } else if (tag == TAG('l', 'o', 'c', 'a')) {
726 // processing was already done by glyf table, but validate
727 if (!FindTable(tables, TAG('g', 'l', 'y', 'f'))) {
728 return OTS_FAILURE();
729 }
730 } else {
731 // transform for the tag is not known
732 return OTS_FAILURE();
733 }
734 return true;
735 }
736
737 uint32_t ComputeChecksum(const uint8_t* buf, size_t size) {
738 uint32_t checksum = 0;
739 for (size_t i = 0; i < size; i += 4) {
740 // We assume the addition is mod 2^32, which is valid because unsigned
741 checksum += (buf[i] << 24) | (buf[i + 1] << 16) |
742 (buf[i + 2] << 8) | buf[i + 3];
743 }
744 return checksum;
745 }
746
747 bool FixChecksums(const std::vector<Table>& tables, uint8_t* dst) {
748 const Table* head_table = FindTable(tables, TAG('h', 'e', 'a', 'd'));
749 if (head_table == NULL ||
750 head_table->dst_length < kCheckSumAdjustmentOffset + 4) {
751 return OTS_FAILURE();
752 }
753 size_t adjustment_offset = head_table->dst_offset + kCheckSumAdjustmentOffset;
754 if (adjustment_offset < head_table->dst_offset) {
755 return OTS_FAILURE();
756 }
757 StoreU32(dst, adjustment_offset, 0);
758 size_t n_tables = tables.size();
759 uint32_t file_checksum = 0;
760 for (size_t i = 0; i < n_tables; ++i) {
761 const Table* table = &tables.at(i);
762 size_t table_length = table->dst_length;
763 uint8_t* table_data = dst + table->dst_offset;
764 uint32_t checksum = ComputeChecksum(table_data, table_length);
765 StoreU32(dst, kSfntHeaderSize + i * kSfntEntrySize + 4, checksum);
766 file_checksum += checksum; // The addition is mod 2^32
767 }
768 file_checksum += ComputeChecksum(dst,
769 kSfntHeaderSize + kSfntEntrySize * n_tables);
770 uint32_t checksum_adjustment = 0xb1b0afba - file_checksum;
771 StoreU32(dst, adjustment_offset, checksum_adjustment);
772 return true;
773 }
774
775 bool Woff2Uncompress(uint8_t* dst_buf, size_t dst_size,
776 const uint8_t* src_buf, size_t src_size) {
777 size_t uncompressed_size = dst_size;
778 int ok = BrotliDecompressBuffer(src_size, src_buf,
779 &uncompressed_size, dst_buf);
780 if (!ok || uncompressed_size != dst_size) {
781 return OTS_FAILURE();
782 }
783 return true;
784 }
785
786 bool ReadTableDirectory(ots::OpenTypeFile* file,
787 ots::Buffer* buffer, std::vector<Table>* tables,
788 size_t num_tables) {
789 for (size_t i = 0; i < num_tables; ++i) {
790 Table* table = &tables->at(i);
791 uint8_t flag_byte;
792 if (!buffer->ReadU8(&flag_byte)) {
793 return OTS_FAILURE_MSG("Failed to read the flags of table directory entry %d", i);
794 }
795 uint32_t tag;
796 if ((flag_byte & 0x3f) == 0x3f) {
797 if (!buffer->ReadU32(&tag)) {
798 return OTS_FAILURE_MSG("Failed to read the tag of table directory entry %d", i);
799 }
800 } else {
801 tag = kKnownTags[flag_byte & 0x3f];
802 }
803 // Bits 6 and 7 are reserved and must be 0.
804 if ((flag_byte & 0xc0) != 0) {
805 return OTS_FAILURE_MSG("Bits 6 and 7 are not 0 for table directory entry % d", i);
806 }
807 uint32_t flags = 0;
808 // Always transform the glyf and loca tables
809 if (tag == TAG('g', 'l', 'y', 'f') ||
810 tag == TAG('l', 'o', 'c', 'a')) {
811 flags |= kWoff2FlagsTransform;
812 }
813 uint32_t dst_length;
814 if (!ReadBase128(buffer, &dst_length)) {
815 return OTS_FAILURE_MSG("Failed to read 'origLength' for table '%c%c%c%c'", CHR(tag));
816 }
817 uint32_t transform_length = dst_length;
818 if ((flags & kWoff2FlagsTransform) != 0) {
819 if (!ReadBase128(buffer, &transform_length)) {
820 return OTS_FAILURE_MSG("Failed to read 'transformLength' for table '%c%c %c%c'", CHR(tag));
821 }
822 }
823 // Disallow huge numbers (> 1GB) for sanity.
824 if (transform_length > 1024 * 1024 * 1024 ||
825 dst_length > 1024 * 1024 * 1024) {
826 return OTS_FAILURE_MSG("'origLength' or 'transformLength' > 1GB");
827 }
828 table->tag = tag;
829 table->flags = flags;
830 table->transform_length = transform_length;
831 table->dst_length = dst_length;
832 }
833 return true;
834 }
835
836 } // namespace
837
838 namespace ots {
839
840 size_t ComputeWOFF2FinalSize(const uint8_t* data, size_t length) {
841 ots::Buffer file(data, length);
842 uint32_t total_length;
843
844 if (!file.Skip(16) ||
845 !file.ReadU32(&total_length)) {
846 return 0;
847 }
848 return total_length;
849 }
850
851 bool ConvertWOFF2ToSFNT(ots::OpenTypeFile* file,
852 uint8_t* result, size_t result_length,
853 const uint8_t* data, size_t length) {
854 static const uint32_t kWoff2Signature = 0x774f4632; // "wOF2"
855 ots::Buffer buffer(data, length);
856
857 uint32_t signature;
858 uint32_t flavor = 0;
859 if (!buffer.ReadU32(&signature) || signature != kWoff2Signature ||
860 !buffer.ReadU32(&flavor)) {
861 return OTS_FAILURE_MSG("Failed to read 'signature' or 'flavor', or not WOFF2 signature");
862 }
863
864 if (!IsValidVersionTag(ntohl(flavor))) {
865 return OTS_FAILURE_MSG("Invalid 'flavor'");
866 }
867
868 uint32_t reported_length;
869 if (!buffer.ReadU32(&reported_length) || length != reported_length) {
870 return OTS_FAILURE_MSG("Failed to read 'length' or it does not match the act ual file size");
871 }
872 uint16_t num_tables;
873 if (!buffer.ReadU16(&num_tables) || !num_tables) {
874 return OTS_FAILURE_MSG("Failed to read 'numTables'");
875 }
876
877 uint16_t reserved_value;
878 if (!buffer.ReadU16(&reserved_value)) {
879 return OTS_FAILURE_MSG("Failed to read 'reserved' field");
880 }
881
882 // We don't care about these fields of the header:
883 // uint32_t total_sfnt_size, the caller already passes it as result_length
884 if (!buffer.Skip(4)) {
885 return OTS_FAILURE_MSG("Failed to read 'totalSfntSize'");
886 }
887 uint32_t compressed_length;
888 if (!buffer.ReadU32(&compressed_length)) {
889 return OTS_FAILURE_MSG("Failed to read 'totalCompressedSize'");
890 }
891 if (compressed_length > std::numeric_limits<uint32_t>::max()) {
892 return OTS_FAILURE();
893 }
894
895 // We don't care about these fields of the header:
896 // uint16_t major_version, minor_version
897 if (!buffer.Skip(2 * 2)) {
898 return OTS_FAILURE_MSG("Failed to read 'majorVersion' or 'minorVersion'");
899 }
900
901 // Checks metadata block size.
902 uint32_t meta_offset;
903 uint32_t meta_length;
904 uint32_t meta_length_orig;
905 if (!buffer.ReadU32(&meta_offset) ||
906 !buffer.ReadU32(&meta_length) ||
907 !buffer.ReadU32(&meta_length_orig)) {
908 return OTS_FAILURE_MSG("Failed to read header metadata block fields");
909 }
910 if (meta_offset) {
911 if (meta_offset >= length || length - meta_offset < meta_length) {
912 return OTS_FAILURE_MSG("Invalid metadata block offset or length");
913 }
914 }
915
916 // Checks private data block size.
917 uint32_t priv_offset;
918 uint32_t priv_length;
919 if (!buffer.ReadU32(&priv_offset) ||
920 !buffer.ReadU32(&priv_length)) {
921 return OTS_FAILURE_MSG("Failed to read header private block fields");
922 }
923 if (priv_offset) {
924 if (priv_offset >= length || length - priv_offset < priv_length) {
925 return OTS_FAILURE_MSG("Invalid private block offset or length");
926 }
927 }
928
929 std::vector<Table> tables(num_tables);
930 if (!ReadTableDirectory(file, &buffer, &tables, num_tables)) {
931 return OTS_FAILURE_MSG("Failed to read table directory");
932 }
933 uint64_t compressed_offset = buffer.offset();
934 if (compressed_offset > std::numeric_limits<uint32_t>::max()) {
935 return OTS_FAILURE();
936 }
937 uint64_t dst_offset = kSfntHeaderSize +
938 kSfntEntrySize * static_cast<uint64_t>(num_tables);
939 for (uint16_t i = 0; i < num_tables; ++i) {
940 Table* table = &tables.at(i);
941 table->dst_offset = static_cast<uint32_t>(dst_offset);
942 dst_offset += table->dst_length;
943 if (dst_offset > std::numeric_limits<uint32_t>::max()) {
944 return OTS_FAILURE();
945 }
946 dst_offset = ots::Round4(dst_offset);
947 }
948
949 uint64_t block_end = ots::Round4(compressed_offset + compressed_length);
950 if (block_end > length || dst_offset != result_length) {
951 return OTS_FAILURE_MSG("Uncompressed sfnt size mismatch");
952 }
953
954 const uint32_t sfnt_header_and_table_directory_size = 12 + 16 * num_tables;
955 if (sfnt_header_and_table_directory_size > result_length) {
956 return OTS_FAILURE();
957 }
958
959 if (meta_offset) {
960 if (block_end != meta_offset) {
961 return OTS_FAILURE_MSG("Invalid metadata block offset");
962 }
963 block_end = ots::Round4(static_cast<uint64_t>(meta_offset) +
964 static_cast<uint64_t>(meta_length));
965 if (block_end > std::numeric_limits<uint32_t>::max()) {
966 return OTS_FAILURE_MSG("Invalid metadata block length");
967 }
968 }
969
970 if (priv_offset) {
971 if (block_end != priv_offset) {
972 return OTS_FAILURE_MSG("Invalid private block offset");
973 }
974 block_end = ots::Round4(static_cast<uint64_t>(priv_offset) +
975 static_cast<uint64_t>(priv_length));
976 if (block_end > std::numeric_limits<uint32_t>::max()) {
977 return OTS_FAILURE_MSG("Invalid private block length");
978 }
979 }
980
981 if (block_end != ots::Round4(length)) {
982 return OTS_FAILURE_MSG("File length mismatch (trailing junk?)");
983 }
984
985 // Start building the font
986 size_t offset = 0;
987 offset = StoreU32(result, offset, flavor);
988 offset = StoreU16(result, offset, num_tables);
989 uint8_t max_pow2 = 0;
990 while (1u << (max_pow2 + 1) <= num_tables) {
991 max_pow2++;
992 }
993 const uint16_t output_search_range = (1u << max_pow2) << 4;
994 offset = StoreU16(result, offset, output_search_range);
995 offset = StoreU16(result, offset, max_pow2);
996 offset = StoreU16(result, offset, (num_tables << 4) - output_search_range);
997
998 // sort tags in the table directory in ascending alphabetical order
999 std::vector<Table> sorted_tables(tables);
1000 std::sort(sorted_tables.begin(), sorted_tables.end());
1001
1002 for (uint16_t i = 0; i < num_tables; ++i) {
1003 const Table* table = &sorted_tables.at(i);
1004 offset = StoreU32(result, offset, table->tag);
1005 offset = StoreU32(result, offset, 0); // checksum, to fill in later
1006 offset = StoreU32(result, offset, table->dst_offset);
1007 offset = StoreU32(result, offset, table->dst_length);
1008 }
1009 std::vector<uint8_t> uncompressed_buf;
1010 const uint8_t* transform_buf = NULL;
1011 uint64_t total_size = 0;
1012
1013 for (uint16_t i = 0; i < num_tables; ++i) {
1014 total_size += tables.at(i).transform_length;
1015 if (total_size > std::numeric_limits<uint32_t>::max()) {
1016 return OTS_FAILURE();
1017 }
1018 }
1019 // Enforce same 30M limit on uncompressed tables as OTS
1020 if (total_size > 30 * 1024 * 1024) {
1021 return OTS_FAILURE();
1022 }
1023 const size_t total_size_size_t = static_cast<size_t>(total_size);
1024 uncompressed_buf.resize(total_size_size_t);
1025 const uint8_t* src_buf = data + compressed_offset;
1026 if (!Woff2Uncompress(&uncompressed_buf[0], total_size_size_t,
1027 src_buf, compressed_length)) {
1028 return OTS_FAILURE_MSG("Failed to uncompress font data");
1029 }
1030 transform_buf = &uncompressed_buf[0];
1031
1032 for (uint16_t i = 0; i < num_tables; ++i) {
1033 const Table* table = &tables.at(i);
1034 uint32_t flags = table->flags;
1035 size_t transform_length = table->transform_length;
1036
1037 if ((flags & kWoff2FlagsTransform) == 0) {
1038 if (transform_length != table->dst_length) {
1039 return OTS_FAILURE();
1040 }
1041 if (static_cast<uint64_t>(table->dst_offset) + transform_length >
1042 result_length) {
1043 return OTS_FAILURE();
1044 }
1045 std::memcpy(result + table->dst_offset, transform_buf,
1046 transform_length);
1047 } else {
1048 if (!ReconstructTransformed(file, tables, table->tag,
1049 transform_buf, transform_length, result, result_length)) {
1050 return OTS_FAILURE_MSG("Failed to reconstruct '%c%c%c%c' table", CHR(tab le->tag));
1051 }
1052 }
1053
1054 transform_buf += transform_length;
1055 if (transform_buf > &uncompressed_buf[0] + uncompressed_buf.size()) {
1056 return OTS_FAILURE();
1057 }
1058 }
1059
1060 return FixChecksums(sorted_tables, result);
1061 }
1062
1063 } // namespace ots
1064
1065 #undef TABLE_NAME
OLDNEW
« no previous file with comments | « third_party/ots/src/woff2.h ('k') | third_party/ots/test/BLACKLIST.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698