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

Side by Side Diff: src/woff2.cc

Issue 13918002: [OTS] Integrate WOFF 2.0 algorithm into OTS (Closed) Base URL: http://ots.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 8 months 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 | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
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 <vector>
Yusuke Sato 2013/04/17 18:38:46 space between 8&9
Kunihiko Sakamoto 2013/04/23 06:36:26 Done.
9 #include <zlib.h>
Yusuke Sato 2013/04/17 18:38:46 space between 9&10
Kunihiko Sakamoto 2013/04/23 06:36:26 Done.
10 #include "third_party/lzma_sdk/LzmaLib.h"
11
12 #include "opentype-sanitiser.h"
13 #include "ots-memory-stream.h"
14 #include "ots.h"
15 #include "woff2.h"
16
17 namespace {
18
19 // simple glyph flags
20 const int kGlyfOnCurve = 1 << 0;
21 const int kGlyfXShort = 1 << 1;
22 const int kGlyfYShort = 1 << 2;
23 const int kGlyfRepeat = 1 << 3;
24 const int kGlyfThisXIsSame = 1 << 4;
25 const int kGlyfThisYIsSame = 1 << 5;
26
27 // composite glyph flags
28 const int FLAG_ARG_1_AND_2_ARE_WORDS = 1 << 0;
29 const int FLAG_ARGS_ARE_XY_VALUES = 1 << 1;
30 const int FLAG_ROUND_XY_TO_GRID = 1 << 2;
31 const int FLAG_WE_HAVE_A_SCALE = 1 << 3;
32 const int FLAG_RESERVED = 1 << 4;
33 const int FLAG_MORE_COMPONENTS = 1 << 5;
34 const int FLAG_WE_HAVE_AN_X_AND_Y_SCALE = 1 << 6;
35 const int FLAG_WE_HAVE_A_TWO_BY_TWO = 1 << 7;
36 const int FLAG_WE_HAVE_INSTRUCTIONS = 1 << 8;
37 const int FLAG_USE_MY_METRICS = 1 << 9;
38 const int FLAG_OVERLAP_COMPOUND = 1 << 10;
39 const int FLAG_SCALED_COMPONENT_OFFSET = 1 << 11;
40 const int FLAG_UNSCALED_COMPONENT_OFFSET = 1 << 12;
41
42 const size_t kSfntHeaderSize = 12;
43 const size_t kSfntEntrySize = 16;
44 const size_t kCheckSumAdjustmentOffset = 8;
45
46 const size_t kEndPtsOfContoursOffset = 10;
47 const size_t kCompositeGlyphBegin = 10;
48
49 // Note that the byte order is big-endian, not the same as ots.cc
50 #define TAG(a, b, c, d) ((a << 24) | (b << 16) | (c << 8) | d)
51
52 const unsigned int kWoff2FlagsContinueStream = 1 << 4;
53 const unsigned int kWoff2FlagsTransform = 1 << 5;
54
55 const size_t kWoff2HeaderSize = 44;
56 const size_t kWoff2EntrySize = 20;
57
58 const size_t kLzmaHeaderSize = 13;
59
60 // Compression type values common to both short and long formats
61 const uint32_t kCompressionTypeMask = 0xf;
62 const uint32_t kCompressionTypeNone = 0;
63 const uint32_t kCompressionTypeGzip = 1;
64 const uint32_t kCompressionTypeLzma = 2;
65
66 // This is a special value for the short format only, as described in
67 // "Design for compressed header format" in draft doc.
68 const uint32_t kShortFlagsContinue = 3;
69
70 struct Point {
71 int x;
72 int y;
73 bool on_curve;
74 };
75
76 struct Table {
77 uint32_t tag;
78 uint32_t flags;
79 uint32_t src_offset;
80 uint32_t src_length;
81
82 uint32_t transform_length;
83
84 uint32_t dst_offset;
85 uint32_t dst_length;
86 };
87
88 // TODO: copied from ots.cc, probably shouldn't be duplicated.
Yusuke Sato 2013/04/17 18:38:46 I guess we can fix this now.
Kunihiko Sakamoto 2013/04/23 06:36:26 Moved to ots.h.
89 // Round a value up to the nearest multiple of 4. Don't round the value in the
90 // case that rounding up overflows.
91 template<typename T> T Round4(T value) {
92 if (std::numeric_limits<T>::max() - value < 3) {
93 return value;
94 }
95 return (value + 3) & ~3;
96 }
97
98 // Based on section 6.1.1 of MicroType Express draft spec
99 bool Read255UShort(ots::Buffer* buf, unsigned int* value) {
100 static const int kWordCode = 253;
101 static const int kOneMoreByteCode2 = 254;
102 static const int kOneMoreByteCode1 = 255;
103 static const int kLowestUCode = 253;
104 uint8_t code = 0;
105 if (!buf->ReadU8(&code)) {
106 return OTS_FAILURE();
107 }
108 if (code == kWordCode) {
109 uint16_t result = 0;
110 if (!buf->ReadU16(&result)) {
111 return OTS_FAILURE();
112 }
113 *value = result;
114 return true;
115 } else if (code == kOneMoreByteCode1) {
116 uint8_t result = 0;
117 if (!buf->ReadU8(&result)) {
118 return OTS_FAILURE();
119 }
120 *value = result + kLowestUCode;
121 return true;
122 } else if (code == kOneMoreByteCode2) {
123 uint8_t result = 0;
124 if (!buf->ReadU8(&result)) {
125 return OTS_FAILURE();
126 }
127 *value = result + kLowestUCode * 2;
128 return true;
129 } else {
130 *value = code;
131 return true;
132 }
133 }
134
135 bool ReadBase128(ots::Buffer* buf, uint32_t* value) {
136 uint32_t result = 0;
137 for (size_t i = 0; i < 5; ++i) {
138 uint8_t code = 0;
139 if (!buf->ReadU8(&code)) {
140 return OTS_FAILURE();
141 }
142 // If any of the top seven bits are set then we're about to overflow.
143 if (result & 0xe0000000) {
Yusuke Sato 2013/04/17 18:38:46 nit: 0xe0000000U is slightly easier to read.
Kunihiko Sakamoto 2013/04/23 06:36:26 Done.
144 return OTS_FAILURE();
145 }
146 result = (result << 7) | (code & 0x7f);
147 if ((code & 0x80) == 0) {
148 *value = result;
149 return true;
150 }
151 }
152 // Make sure not to exceed the size bound
153 return OTS_FAILURE();
154 }
155
156 size_t StoreU32(uint8_t* dst, size_t offset, uint32_t x) {
157 dst[offset] = x >> 24;
Yusuke Sato 2013/04/17 18:38:46 is there any reason not to check the buffer length
Kunihiko Sakamoto 2013/04/23 06:36:26 I don't think so. Probably I should create 'writer
Yusuke Sato 2013/05/07 06:11:27 Please add a TODO. I guess you have to do this any
Kunihiko Sakamoto 2013/05/07 10:17:23 Done.
158 dst[offset + 1] = x >> 16;
159 dst[offset + 2] = x >> 8;
160 dst[offset + 3] = x;
161 return offset + 4;
162 }
163
164 size_t Store16(uint8_t* dst, size_t offset, int x) {
165 dst[offset] = x >> 8;
Yusuke Sato 2013/04/17 18:38:46 the same.
166 dst[offset + 1] = x;
167 return offset + 2;
168 }
169
170 int WithSign(int flag, int baseval) {
171 // Precondition: 0 <= baseval < 65536 (to avoid integer overflow)
Yusuke Sato 2013/04/17 18:38:46 Use assert() then?
Kunihiko Sakamoto 2013/04/23 06:36:26 Done.
172 return (flag & 1) ? baseval : -baseval;
173 }
174
175 bool TripletDecode(const uint8_t* flags_in, const uint8_t* in, size_t in_size,
176 unsigned int n_points, std::vector<Point>* result,
177 size_t* in_bytes_consumed) {
178 int x = 0;
179 int y = 0;
180
181 if (n_points > in_size) {
Yusuke Sato 2013/04/17 18:38:46 Is this correct? I guess this might be >=.
Kunihiko Sakamoto 2013/04/23 06:36:26 Correct, I think. Note that each point consumes 1-
Yusuke Sato 2013/05/07 06:11:27 Please add a code comment then.
Kunihiko Sakamoto 2013/05/07 10:17:23 Done.
182 return OTS_FAILURE();
183 }
184 unsigned int triplet_index = 0;
185
186 for (unsigned int i = 0; i < n_points; ++i) {
187 uint8_t flag = flags_in[i];
188 bool on_curve = !(flag >> 7);
189 flag &= 0x7f;
190 unsigned int n_data_bytes;
191 if (flag < 84) {
192 n_data_bytes = 1;
193 } else if (flag < 120) {
194 n_data_bytes = 2;
195 } else if (flag < 124) {
196 n_data_bytes = 3;
197 } else {
198 n_data_bytes = 4;
199 }
200 if (triplet_index + n_data_bytes > in_size ||
201 triplet_index + n_data_bytes < triplet_index) {
202 return OTS_FAILURE();
203 }
204 int dx, dy;
205 if (flag < 10) {
206 dx = 0;
207 dy = WithSign(flag, ((flag & 14) << 7) + in[triplet_index]);
208 } else if (flag < 20) {
209 dx = WithSign(flag, (((flag - 10) & 14) << 7) + in[triplet_index]);
210 dy = 0;
211 } else if (flag < 84) {
212 int b0 = flag - 20;
213 int b1 = in[triplet_index];
214 dx = WithSign(flag, 1 + (b0 & 0x30) + (b1 >> 4));
215 dy = WithSign(flag >> 1, 1 + ((b0 & 0x0c) << 2) + (b1 & 0x0f));
216 } else if (flag < 120) {
217 int b0 = flag - 84;
218 dx = WithSign(flag, 1 + ((b0 / 12) << 8) + in[triplet_index]);
219 dy = WithSign(flag >> 1,
220 1 + (((b0 % 12) >> 2) << 8) + in[triplet_index + 1]);
221 } else if (flag < 124) {
222 int b2 = in[triplet_index + 1];
223 dx = WithSign(flag, (in[triplet_index] << 4) + (b2 >> 4));
224 dy = WithSign(flag >> 1, ((b2 & 0x0f) << 8) + in[triplet_index + 2]);
225 } else {
226 dx = WithSign(flag, (in[triplet_index] << 8) + in[triplet_index + 1]);
227 dy = WithSign(flag >> 1,
228 (in[triplet_index + 2] << 8) + in[triplet_index + 3]);
229 }
230 triplet_index += n_data_bytes;
231 // Possible overflow but coordinate values are not security sensitive
232 x += dx;
233 y += dy;
234 result->push_back(Point());
235 Point& back = result->back();
236 back.x = x;
237 back.y = y;
238 back.on_curve = on_curve;
239 }
240 *in_bytes_consumed = triplet_index;
241 return true;
242 }
243
244 // This function stores just the point data. On entry, dst points to the
245 // beginning of a simple glyph. Returns true on success.
246 bool StorePoints(const std::vector<Point>& points,
247 unsigned int n_contours, unsigned int instruction_length,
248 uint8_t* dst, size_t dst_size, size_t* glyph_size) {
249 // I believe that n_contours < 65536, in which case this is safe. However, a
250 // comment and/or an assert would be good.
251 unsigned int flag_offset = kEndPtsOfContoursOffset + 2 * n_contours + 2 +
252 instruction_length;
253 int last_flag = -1;
254 int repeat_count = 0;
255 int last_x = 0;
256 int last_y = 0;
257 unsigned int x_bytes = 0;
258 unsigned int y_bytes = 0;
259
260 for (unsigned int i = 0; i < points.size(); ++i) {
Yusuke Sato 2013/04/17 18:38:46 size_t
Kunihiko Sakamoto 2013/04/23 06:36:26 Done.
261 const Point& point = points[i];
262 int flag = point.on_curve ? kGlyfOnCurve : 0;
263 int dx = point.x - last_x;
264 int dy = point.y - last_y;
265 if (dx == 0) {
266 flag |= kGlyfThisXIsSame;
267 } else if (dx > -256 && dx < 256) {
268 flag |= kGlyfXShort | (dx > 0 ? kGlyfThisXIsSame : 0);
269 x_bytes += 1;
270 } else {
271 x_bytes += 2;
272 }
273 if (dy == 0) {
274 flag |= kGlyfThisYIsSame;
275 } else if (dy > -256 && dy < 256) {
276 flag |= kGlyfYShort | (dy > 0 ? kGlyfThisYIsSame : 0);
277 y_bytes += 1;
278 } else {
279 y_bytes += 2;
280 }
281
282 if (flag == last_flag && repeat_count != 255) {
283 dst[flag_offset - 1] |= kGlyfRepeat;
284 repeat_count++;
285 } else {
286 if (repeat_count != 0) {
287 if (flag_offset >= dst_size) {
288 return OTS_FAILURE();
289 }
290 dst[flag_offset++] = repeat_count;
291 }
292 if (flag_offset >= dst_size) {
293 return OTS_FAILURE();
294 }
295 dst[flag_offset++] = flag;
296 repeat_count = 0;
297 }
298 last_x = point.x;
299 last_y = point.y;
300 last_flag = flag;
301 }
302
303 if (repeat_count != 0) {
304 if (flag_offset >= dst_size) {
305 return OTS_FAILURE();
306 }
307 dst[flag_offset++] = repeat_count;
308 }
309 unsigned int xy_bytes = x_bytes + y_bytes;
310 if (xy_bytes < x_bytes ||
311 flag_offset + xy_bytes < flag_offset ||
312 flag_offset + xy_bytes > dst_size) {
313 return OTS_FAILURE();
314 }
315
316 int x_offset = flag_offset;
317 int y_offset = flag_offset + x_bytes;
318 last_x = 0;
319 last_y = 0;
320 for (unsigned int i = 0; i < points.size(); ++i) {
Yusuke Sato 2013/04/17 18:38:46 size_t
Kunihiko Sakamoto 2013/04/23 06:36:26 Done.
321 int dx = points[i].x - last_x;
322 if (dx == 0) {
323 // pass
324 } else if (dx > -256 && dx < 256) {
325 dst[x_offset++] = std::abs(dx);
Yusuke Sato 2013/04/17 18:38:46 nit: #include <cstdlib>
Kunihiko Sakamoto 2013/04/23 06:36:26 Done.
326 } else {
327 // will always fit for valid input, but overflow is harmless
328 x_offset = Store16(dst, x_offset, dx);
329 }
330 last_x += dx;
331 int dy = points[i].y - last_y;
332 if (dy == 0) {
333 // pass
334 } else if (dy > -256 && dy < 256) {
335 dst[y_offset++] = std::abs(dy);
336 } else {
337 y_offset = Store16(dst, y_offset, dy);
338 }
339 last_y += dy;
340 }
341 *glyph_size = y_offset;
342 return true;
343 }
344
345 // Compute the bounding box of the coordinates, and store into a glyf buffer.
346 // A precondition is that there are at least 10 bytes available.
347 void ComputeBbox(const std::vector<Point>& points, uint8_t* dst) {
348 int x_min = 0;
349 int y_min = 0;
350 int x_max = 0;
351 int y_max = 0;
352
353 for (unsigned int i = 0; i < points.size(); ++i) {
Yusuke Sato 2013/04/17 18:38:46 size_t
Kunihiko Sakamoto 2013/04/23 06:36:26 Done.
354 int x = points[i].x;
355 int y = points[i].y;
356 if (i == 0 || x < x_min) x_min = x;
357 if (i == 0 || x > x_max) x_max = x;
358 if (i == 0 || y < y_min) y_min = y;
359 if (i == 0 || y > y_max) y_max = y;
360 }
361 size_t offset = 2;
362 offset = Store16(dst, offset, x_min);
363 offset = Store16(dst, offset, y_min);
364 offset = Store16(dst, offset, x_max);
365 offset = Store16(dst, offset, y_max);
366 }
367
368 // Process entire bbox stream. This is done as a separate pass to allow for
369 // composite bbox computations (an optional more aggressive transform).
370 bool ProcessBboxStream(ots::Buffer* bbox_stream, unsigned int n_glyphs,
371 const std::vector<uint32_t>& loca_values, uint8_t* glyf_buf,
372 size_t glyf_buf_length) {
373 const uint8_t* buf = bbox_stream->buffer();
374 if (n_glyphs >= 65536 || loca_values.size() != n_glyphs + 1) {
375 return OTS_FAILURE();
376 }
377 // Safe because n_glyphs is bounded
378 unsigned int bitmap_length = ((n_glyphs + 31) >> 5) << 2;
379 if (!bbox_stream->Skip(bitmap_length)) {
380 return OTS_FAILURE();
381 }
382 for (unsigned int i = 0; i < n_glyphs; ++i) {
383 if (buf[i >> 3] & (0x80 >> (i & 7))) {
384 uint32_t loca_offset = loca_values[i];
385 if (loca_values[i + 1] - loca_offset < kEndPtsOfContoursOffset) {
386 return OTS_FAILURE();
387 }
388 if (glyf_buf_length < 2 + 10 ||
389 loca_offset > glyf_buf_length - 2 - 10) {
390 return OTS_FAILURE();
391 }
392 if (!bbox_stream->Read(glyf_buf + loca_offset + 2, 8)) {
393 return OTS_FAILURE();
394 }
395 }
396 }
397 return true;
398 }
399
400 bool ProcessComposite(ots::Buffer* composite_stream, uint8_t* dst,
401 size_t dst_size, size_t* glyph_size, bool* have_instructions) {
402 size_t start_offset = composite_stream->offset();
403 bool we_have_instructions = false;
404
405 uint16_t flags = FLAG_MORE_COMPONENTS;
406 while (flags & FLAG_MORE_COMPONENTS) {
407 if (!composite_stream->ReadU16(&flags)) {
408 return OTS_FAILURE();
409 }
410 we_have_instructions |= (flags & FLAG_WE_HAVE_INSTRUCTIONS) != 0;
411 size_t arg_size = 2; // glyph index
412 if (flags & FLAG_ARG_1_AND_2_ARE_WORDS) {
413 arg_size += 4;
414 } else {
415 arg_size += 2;
416 }
417 if (flags & FLAG_WE_HAVE_A_SCALE) {
418 arg_size += 2;
419 } else if (flags & FLAG_WE_HAVE_AN_X_AND_Y_SCALE) {
420 arg_size += 4;
421 } else if (flags & FLAG_WE_HAVE_A_TWO_BY_TWO) {
422 arg_size += 8;
423 }
424 if (!composite_stream->Skip(arg_size)) {
425 return OTS_FAILURE();
426 }
427 }
428 size_t composite_glyph_size = composite_stream->offset() - start_offset;
429 if (composite_glyph_size + kCompositeGlyphBegin > dst_size) {
430 return OTS_FAILURE();
431 }
432 Store16(dst, 0, 0xffff); // nContours = -1 for composite glyph
433 std::memcpy(dst + kCompositeGlyphBegin,
434 composite_stream->buffer() + start_offset,
435 composite_glyph_size);
436 *glyph_size = kCompositeGlyphBegin + composite_glyph_size;
437 *have_instructions = we_have_instructions;
438 return true;
439 }
440
441 // Build TrueType loca table
442 bool StoreLoca(const std::vector<uint32_t>& loca_values, int index_format,
443 uint8_t* dst, size_t dst_size) {
444 const uint64_t loca_size = loca_values.size();
445 const uint64_t offset_size = index_format ? 4 : 2;
446 if ((loca_size << 2) >> 2 != loca_size) {
447 return OTS_FAILURE();
448 }
449 if (offset_size * loca_size > dst_size) {
Yusuke Sato 2013/04/17 18:38:46 please add a comment about an integer overflow (wh
Kunihiko Sakamoto 2013/04/23 06:36:26 Done.
450 return OTS_FAILURE();
451 }
452 size_t offset = 0;
453 for (size_t i = 0; i < loca_values.size(); ++i) {
454 uint32_t value = loca_values[i];
455 if (index_format) {
456 offset = StoreU32(dst, offset, value);
457 } else {
458 offset = Store16(dst, offset, value >> 1);
459 }
460 }
461 return true;
462 }
463
464 // Reconstruct entire glyf table based on transformed original
465 bool ReconstructGlyf(const uint8_t* data, size_t data_size,
466 uint8_t* dst, size_t dst_size,
467 uint8_t* loca_buf, size_t loca_size) {
468 static const int kNumSubStreams = 7;
469 ots::Buffer file(data, data_size);
470 uint32_t version;
471 std::vector<std::pair<const uint8_t*, size_t> > substreams(kNumSubStreams);
472
473 if (!file.ReadU32(&version)) {
474 return OTS_FAILURE();
475 }
476 uint16_t num_glyphs;
477 uint16_t index_format;
478 if (!file.ReadU16(&num_glyphs) ||
479 !file.ReadU16(&index_format)) {
480 return OTS_FAILURE();
481 }
482 unsigned int offset = (2 + kNumSubStreams) * 4;
483 if (offset > data_size) {
484 return OTS_FAILURE();
485 }
486 // Invariant from here on: data_size >= offset
487 for (int i = 0; i < kNumSubStreams; ++i) {
488 uint32_t substream_size;
489 if (!file.ReadU32(&substream_size)) {
490 return OTS_FAILURE();
491 }
492 if (substream_size > data_size - offset) {
493 return OTS_FAILURE();
494 }
495 substreams[i] = std::make_pair(data + offset, substream_size);
496 offset += substream_size;
497 }
498 ots::Buffer n_contour_stream(substreams[0].first, substreams[0].second);
499 ots::Buffer n_points_stream(substreams[1].first, substreams[1].second);
500 ots::Buffer flag_stream(substreams[2].first, substreams[2].second);
501 ots::Buffer glyph_stream(substreams[3].first, substreams[3].second);
502 ots::Buffer composite_stream(substreams[4].first, substreams[4].second);
503 ots::Buffer bbox_stream(substreams[5].first, substreams[5].second);
504 ots::Buffer instruction_stream(substreams[6].first, substreams[6].second);
505
506 std::vector<uint32_t> loca_values(num_glyphs + 1);
Yusuke Sato 2013/04/17 18:38:46 Is zero-fill required?
Kunihiko Sakamoto 2013/04/23 06:36:26 No - all element should be written.
Yusuke Sato 2013/05/07 06:11:27 Please fix. it's still zero-filled.
Kunihiko Sakamoto 2013/05/07 10:17:23 Changed to use vector::reserve for allocation and
507 std::vector<unsigned int> n_points_vec;
508 std::vector<Point> points;
509 uint32_t loca_offset = 0;
510 for (unsigned int i = 0; i < num_glyphs; ++i) {
511 size_t glyph_size = 0;
512 uint16_t n_contours = 0;
513 if (!n_contour_stream.ReadU16(&n_contours)) {
514 return OTS_FAILURE();
515 }
516 uint8_t* glyf_dst = dst + loca_offset;
517 size_t glyf_dst_size = dst_size - loca_offset;
518 if (n_contours == 0xffff) {
519 // composite glyph
520 bool have_instructions = false;
521 unsigned int instruction_size = 0;
522 if (!ProcessComposite(&composite_stream, glyf_dst, glyf_dst_size,
523 &glyph_size, &have_instructions)) {
524 return OTS_FAILURE();
525 }
526 if (have_instructions) {
527 if (!Read255UShort(&glyph_stream, &instruction_size)) {
528 return OTS_FAILURE();
529 }
530 if (instruction_size + 2 > glyf_dst_size - glyph_size) {
Yusuke Sato 2013/04/17 18:38:46 integer overflow not checked?
Kunihiko Sakamoto 2013/04/23 06:36:26 |instruction_size| is set by Read255UShort, so 0 <
Yusuke Sato 2013/05/07 06:11:27 add comment please.
Kunihiko Sakamoto 2013/05/07 10:17:23 Done.
531 return OTS_FAILURE();
532 }
533 Store16(glyf_dst, glyph_size, instruction_size);
534 if (!instruction_stream.Read(glyf_dst + glyph_size + 2,
535 instruction_size)) {
536 return OTS_FAILURE();
537 }
538 glyph_size += instruction_size + 2;
539 }
540 } else if (n_contours > 0) {
541 // simple glyph
542 n_points_vec.clear();
543 points.clear();
544 unsigned int total_n_points = 0;
545 unsigned int n_points_contour;
546 for (unsigned int j = 0; j < n_contours; ++j) {
547 if (!Read255UShort(&n_points_stream, &n_points_contour)) {
548 return OTS_FAILURE();
549 }
550 n_points_vec.push_back(n_points_contour);
551 if (total_n_points + n_points_contour < total_n_points) {
552 return OTS_FAILURE();
553 }
554 total_n_points += n_points_contour;
555 }
556 unsigned int flag_size = total_n_points;
557 if (flag_size > flag_stream.length() - flag_stream.offset()) {
558 return OTS_FAILURE();
559 }
560 const uint8_t* flags_buf = flag_stream.buffer() + flag_stream.offset();
561 const uint8_t* triplet_buf = glyph_stream.buffer() +
562 glyph_stream.offset();
563 size_t triplet_size = glyph_stream.length() - glyph_stream.offset();
564 size_t triplet_bytes_consumed = 0;
565 if (!TripletDecode(flags_buf, triplet_buf, triplet_size, total_n_points,
566 &points, &triplet_bytes_consumed)) {
567 return OTS_FAILURE();
568 }
569 const uint32_t header_and_endpts_contours_size =
570 kEndPtsOfContoursOffset + 2 * n_contours;
571 if (glyf_dst_size < header_and_endpts_contours_size) {
572 return OTS_FAILURE();
573 }
574 Store16(glyf_dst, 0, n_contours);
575 ComputeBbox(points, glyf_dst);
576 size_t offset = kEndPtsOfContoursOffset;
577 int end_point = -1;
578 for (unsigned int contour_ix = 0; contour_ix < n_contours; ++contour_ix) {
579 end_point += n_points_vec[contour_ix];
580 if (end_point >= 65536) {
581 return OTS_FAILURE();
582 }
583 offset = Store16(glyf_dst, offset, end_point);
584 }
585 if (!flag_stream.Skip(flag_size)) {
586 return OTS_FAILURE();
587 }
588 if (!glyph_stream.Skip(triplet_bytes_consumed)) {
589 return OTS_FAILURE();
590 }
591 unsigned int instruction_size;
592 if (!Read255UShort(&glyph_stream, &instruction_size)) {
593 return OTS_FAILURE();
594 }
595 if (glyf_dst_size - header_and_endpts_contours_size <
596 instruction_size + 2) {
Yusuke Sato 2013/04/17 18:38:46 integer overflow not checked.
Kunihiko Sakamoto 2013/04/23 06:36:26 Same as above.
Yusuke Sato 2013/05/07 06:11:27 add comment please.
Kunihiko Sakamoto 2013/05/07 10:17:23 Done.
597 return OTS_FAILURE();
598 }
599 uint8_t* instruction_dst = glyf_dst + header_and_endpts_contours_size;
600 Store16(instruction_dst, 0, instruction_size);
601 if (!instruction_stream.Read(instruction_dst + 2, instruction_size)) {
602 return OTS_FAILURE();
603 }
604 if (!StorePoints(points, n_contours, instruction_size,
605 glyf_dst, glyf_dst_size, &glyph_size)) {
606 return OTS_FAILURE();
607 }
608 } else {
609 glyph_size = 0;
610 }
611 loca_values[i] = loca_offset;
612 if (glyph_size + 3 < glyph_size) {
613 return OTS_FAILURE();
614 }
615 glyph_size = Round4(glyph_size);
616 if (glyph_size > dst_size - loca_offset) {
617 // This shouldn't happen, but this test defensively maintains the
618 // invariant that loca_offset <= dst_size.
619 return OTS_FAILURE();
620 }
621 loca_offset += glyph_size;
622 }
623 loca_values[num_glyphs] = loca_offset;
624 if (!ProcessBboxStream(&bbox_stream, num_glyphs, loca_values,
625 dst, dst_size)) {
626 return OTS_FAILURE();
627 }
628 return StoreLoca(loca_values, index_format, loca_buf, loca_size);
629 }
630
631 // This is linear search, but could be changed to binary because we
632 // do have a guarantee that the tables are sorted by tag. But the total
633 // cpu time is expected to be very small in any case.
634 const Table* FindTable(const std::vector<Table>& tables, uint32_t tag) {
635 size_t n_tables = tables.size();
636 for (size_t i = 0; i < n_tables; ++i) {
637 if (tables[i].tag == tag) {
Yusuke Sato 2013/04/17 18:38:46 qq: is this endian neutral? (not reading the code
Kunihiko Sakamoto 2013/04/23 06:36:26 Yes. Tag values are either read by ReadU32 or crea
638 return &tables[i];
639 }
640 }
641 return NULL;
642 }
643
644 bool ReconstructTransformed(const std::vector<Table>& tables, uint32_t tag,
645 const uint8_t* transformed_buf, size_t transformed_size,
646 uint8_t* dst, size_t dst_length) {
647 if (tag == TAG('g', 'l', 'y', 'f')) {
648 const Table* glyf_table = FindTable(tables, tag);
649 const Table* loca_table = FindTable(tables, TAG('l', 'o', 'c', 'a'));
650 if (glyf_table == NULL || loca_table == NULL) {
651 return OTS_FAILURE();
652 }
653 if (static_cast<uint64_t>(glyf_table->dst_offset + glyf_table->dst_length) >
Yusuke Sato 2013/04/17 18:38:46 what is the case for? Looks no-op to me.
Kunihiko Sakamoto 2013/04/23 06:36:26 You mean the static_cast is no-op?
Yusuke Sato 2013/05/07 06:11:27 yes. s/case/cast/. cast<64b>(32b+32b) does not mak
Kunihiko Sakamoto 2013/05/07 10:17:23 Thanks for the explanation. Changed to cast<64b>(3
654 dst_length) {
655 return OTS_FAILURE();
656 }
657 if (static_cast<uint64_t>(loca_table->dst_offset + loca_table->dst_length) >
Yusuke Sato 2013/04/17 18:38:46 the same.
Kunihiko Sakamoto 2013/05/07 10:17:23 Done.
658 dst_length) {
659 return OTS_FAILURE();
660 }
661 return ReconstructGlyf(transformed_buf, transformed_size,
662 dst + glyf_table->dst_offset, glyf_table->dst_length,
663 dst + loca_table->dst_offset, loca_table->dst_length);
664 } else if (tag == TAG('l', 'o', 'c', 'a')) {
665 // processing was already done by glyf table, but validate
666 if (!FindTable(tables, TAG('g', 'l', 'y', 'f'))) {
667 return OTS_FAILURE();
668 }
669 } else {
670 // transform for the tag is not known
671 return OTS_FAILURE();
672 }
673 return true;
674 }
675
676 uint32_t ComputeChecksum(const uint8_t* buf, size_t size) {
677 uint32_t checksum = 0;
678 for (size_t i = 0; i < size; i += 4) {
679 // We assume the addition is mod 2^32, which is valid because unsigned
680 checksum += (buf[i] << 24) | (buf[i + 1] << 16) |
Yusuke Sato 2013/04/17 18:38:46 endian? (same as above. just asking)
Kunihiko Sakamoto 2013/04/23 06:36:26 Looks OK to me.
681 (buf[i + 2] << 8) | buf[i + 3];
682 }
683 return checksum;
684 }
685
686 bool FixChecksums(const std::vector<Table>& tables, uint8_t* dst) {
687 const Table* head_table = FindTable(tables, TAG('h', 'e', 'a', 'd'));
688 if (head_table == NULL ||
689 head_table->dst_length < kCheckSumAdjustmentOffset + 4) {
690 return OTS_FAILURE();
691 }
692 size_t adjustment_offset = head_table->dst_offset + kCheckSumAdjustmentOffset;
Yusuke Sato 2013/04/17 18:38:46 not sure, but maybe change the type to uint64_t an
Kunihiko Sakamoto 2013/04/23 06:36:26 Overflow check added.
693 StoreU32(dst, adjustment_offset, 0);
694 size_t n_tables = tables.size();
695 uint32_t file_checksum = 0;
696 for (size_t i = 0; i < n_tables; ++i) {
697 const Table* table = &tables[i];
698 size_t table_length = table->dst_length;
699 uint8_t* table_data = dst + table->dst_offset;
700 uint32_t checksum = ComputeChecksum(table_data, table_length);
701 StoreU32(dst, kSfntHeaderSize + i * kSfntEntrySize + 4, checksum);
702 file_checksum += checksum;
Yusuke Sato 2013/04/17 18:38:46 integer overflow is explicitly allowed for |file_c
Kunihiko Sakamoto 2013/04/23 06:36:26 Done.
703 }
704 file_checksum += ComputeChecksum(dst,
705 kSfntHeaderSize + kSfntEntrySize * n_tables);
706 uint32_t checksum_adjustment = 0xb1b0afba - file_checksum;
707 StoreU32(dst, adjustment_offset, checksum_adjustment);
708 return true;
709 }
710
711 bool Woff2Uncompress(uint8_t* dst_buf, size_t dst_size,
712 const uint8_t* src_buf, size_t src_size, uint32_t compression_type) {
713 if (compression_type == kCompressionTypeGzip) {
714 uLongf uncompressed_length = dst_size;
715 int r = uncompress(reinterpret_cast<Bytef *>(dst_buf), &uncompressed_length,
716 src_buf, src_size);
717 if (r != Z_OK || uncompressed_length != dst_size) {
718 return OTS_FAILURE();
719 }
720 return true;
721 } else if (compression_type == kCompressionTypeLzma) {
722 if (src_size < kLzmaHeaderSize) {
723 // Make sure we have at least a full Lzma header
724 return OTS_FAILURE();
725 }
726 // TODO: check that size matches (or elide size?)
727 size_t uncompressed_size = dst_size;
728 size_t compressed_size = src_size;
729 int result = LzmaUncompress(dst_buf, &dst_size,
730 src_buf + kLzmaHeaderSize, &compressed_size,
731 src_buf, LZMA_PROPS_SIZE);
732 if (result != SZ_OK || uncompressed_size != dst_size) {
733 return OTS_FAILURE();
734 }
735 return true;
736 }
737 // Unknown compression type
738 return OTS_FAILURE();
739 }
740
741 const uint32_t known_tags[29] = {
Yusuke Sato 2013/04/17 18:38:46 kKnownTags. Move this to around line 69. I guess y
Kunihiko Sakamoto 2013/04/23 06:36:26 Done.
Yusuke Sato 2013/05/07 06:11:27 "Move this to around line 69." is not yet done.
Kunihiko Sakamoto 2013/05/07 10:17:23 Oops, sorry. Done.
742 TAG('c', 'm', 'a', 'p'), // 0
743 TAG('h', 'e', 'a', 'd'), // 1
744 TAG('h', 'h', 'e', 'a'), // 2
745 TAG('h', 'm', 't', 'x'), // 3
746 TAG('m', 'a', 'x', 'p'), // 4
747 TAG('n', 'a', 'm', 'e'), // 5
748 TAG('O', 'S', '/', '2'), // 6
749 TAG('p', 'o', 's', 't'), // 7
750 TAG('c', 'v', 't', ' '), // 8
751 TAG('f', 'p', 'g', 'm'), // 9
752 TAG('g', 'l', 'y', 'f'), // 10
753 TAG('l', 'o', 'c', 'a'), // 11
754 TAG('p', 'r', 'e', 'p'), // 12
755 TAG('C', 'F', 'F', ' '), // 13
756 TAG('V', 'O', 'R', 'G'), // 14
757 TAG('E', 'B', 'D', 'T'), // 15
758 TAG('E', 'B', 'L', 'C'), // 16
759 TAG('g', 'a', 's', 'p'), // 17
760 TAG('h', 'd', 'm', 'x'), // 18
761 TAG('k', 'e', 'r', 'n'), // 19
762 TAG('L', 'T', 'S', 'H'), // 20
763 TAG('P', 'C', 'L', 'T'), // 21
764 TAG('V', 'D', 'M', 'X'), // 22
765 TAG('v', 'h', 'e', 'a'), // 23
766 TAG('v', 'm', 't', 'x'), // 24
767 TAG('B', 'A', 'S', 'E'), // 25
768 TAG('G', 'D', 'E', 'F'), // 26
769 TAG('G', 'P', 'O', 'S'), // 27
770 TAG('G', 'S', 'U', 'B'), // 28
771 };
772
773 bool ReadShortDirectory(ots::Buffer* file, std::vector<Table>* tables,
774 size_t num_tables) {
775 uint32_t last_compression_type = 0;
776 for (size_t i = 0; i < num_tables; ++i) {
777 Table* table = &(*tables)[i];
778 uint8_t flag_byte;
779 if (!file->ReadU8(&flag_byte)) {
780 return OTS_FAILURE();
781 }
782 uint32_t tag;
783 if ((flag_byte & 0x1f) == 0x1f) {
784 if (!file->ReadU32(&tag)) {
785 return OTS_FAILURE();
786 }
787 } else {
788 if ((flag_byte & 0x1f) >= (sizeof(known_tags) / sizeof(known_tags[0]))) {
Yusuke Sato 2013/04/17 18:38:46 optional: time to add the arraysize template funct
Kunihiko Sakamoto 2013/04/23 06:36:26 Done.
789 return OTS_FAILURE();
790 }
791 tag = known_tags[flag_byte & 0x1f];
792 }
793 uint32_t flags = flag_byte >> 6;
794 if (flags == kShortFlagsContinue) {
795 flags = last_compression_type | kWoff2FlagsContinueStream;
796 } else {
797 if (flags == kCompressionTypeNone ||
798 flags == kCompressionTypeGzip ||
799 flags == kCompressionTypeLzma) {
800 last_compression_type = flags;
801 } else {
802 return OTS_FAILURE();
803 }
804 }
805 if ((flag_byte & 0x20) != 0) {
806 flags |= kWoff2FlagsTransform;
807 }
808 uint32_t dst_length;
809 if (!ReadBase128(file, &dst_length)) {
810 return OTS_FAILURE();
811 }
812 uint32_t transform_length = dst_length;
813 if ((flags & kWoff2FlagsTransform) != 0) {
814 if (!ReadBase128(file, &transform_length)) {
815 return OTS_FAILURE();
816 }
817 }
818 uint32_t src_length = transform_length;
819 if ((flag_byte >> 6) == 1 || (flag_byte >> 6) == 2) {
820 if (!ReadBase128(file, &src_length)) {
821 return OTS_FAILURE();
822 }
823 }
824 table->tag = tag;
825 table->flags = flags;
826 table->src_length = src_length;
Yusuke Sato 2013/04/17 18:38:46 To be on safer side, can we reject huge src_length
Kunihiko Sakamoto 2013/04/23 06:36:26 Done.
827 table->transform_length = transform_length;
828 table->dst_length = dst_length;
Yusuke Sato 2013/04/17 18:38:46 could you assign zero to src_offset and dst_offset
Kunihiko Sakamoto 2013/04/23 06:36:26 Constructor added.
829 }
830 return true;
831 }
832
833 } // namespace
834
835 namespace ots {
836
837 size_t ComputeWOFF2FinalSize(const uint8_t* data, size_t length) {
838 ots::Buffer file(data, length);
839 uint32_t total_length;
Yusuke Sato 2013/04/17 18:38:46 IIRC, always adding '= 0;' would be better to avoi
Kunihiko Sakamoto 2013/04/23 06:36:26 Hmm gcc -Wall does not generate any warnings on th
Yusuke Sato 2013/05/07 06:11:27 On Chrome OS build environment (gcc 4.7, soon 4.8)
Kunihiko Sakamoto 2013/05/07 10:17:23 Gcc 4.7 didn't warn for me... I would like to leav
Yusuke Sato 2013/05/07 16:56:44 Please run 'cros_amd64' trybot when you update Chr
840
841 if (!file.Skip(16) ||
842 !file.ReadU32(&total_length)) {
843 return 0;
844 }
845 return total_length;
846 }
847
848 bool ConvertWOFF2ToTTF(uint8_t* result, size_t result_length,
849 const uint8_t* data, size_t length) {
850 static const uint32_t kWoff2Signature = 0x774f4632; // "wOF2"
851 ots::Buffer file(data, length);
852
853 uint32_t signature;
Yusuke Sato 2013/04/17 18:38:46 =0
854 uint32_t flavor;
Yusuke Sato 2013/04/17 18:38:46 =0
855 if (!file.ReadU32(&signature) || signature != kWoff2Signature ||
Yusuke Sato 2013/04/17 18:38:46 is this endian neutral? if not, better not to assu
Kunihiko Sakamoto 2013/04/23 06:36:26 ReadU32 reads big endian so this is endian neutral
856 !file.ReadU32(&flavor)) {
857 return OTS_FAILURE();
858 }
859
860 // TODO(bashi): Should call IsValidVersionTag() here.
Yusuke Sato 2013/04/17 18:38:46 File a bug?
Kunihiko Sakamoto 2013/04/23 06:36:26 Now it's easy, as this is a change to ots reposito
Yusuke Sato 2013/05/07 06:11:27 nice.
861
862 uint32_t reported_length;
863 if (!file.ReadU32(&reported_length) || length != reported_length) {
864 return OTS_FAILURE();
865 }
866 uint16_t num_tables;
867 if (!file.ReadU16(&num_tables) || !num_tables) {
868 return OTS_FAILURE();
869 }
870 // We don't care about these fields of the header:
871 // uint16_t reserved
872 // uint32_t total_sfnt_size
873 // uint16_t major_version, minor_version
874 // uint32_t meta_offset, meta_length, meta_orig_length
875 // uint32_t priv_offset, priv_length
876 if (!file.Skip(30)) {
877 return OTS_FAILURE();
878 }
879 std::vector<Table> tables(num_tables);
880 // Note: change below to ReadLongDirectory to enable long format.
Yusuke Sato 2013/04/17 18:38:46 what's "long format"? Don't we have to support it?
Kunihiko Sakamoto 2013/04/23 06:36:26 I guess we don't. Checking with David.
Kunihiko Sakamoto 2013/05/07 10:17:23 Confirmed that the long format is no longer used.
881 if (!ReadShortDirectory(&file, &tables, num_tables)) {
882 return OTS_FAILURE();
883 }
884 uint64_t src_offset = file.offset();
885 uint64_t dst_offset = kSfntHeaderSize +
886 kSfntEntrySize * static_cast<uint64_t>(num_tables);
887 uint64_t uncompressed_sum = 0;
888 for (uint16_t i = 0; i < num_tables; ++i) {
889 Table* table = &tables[i];
Yusuke Sato 2013/04/17 18:38:46 replacing all operator[] calls with at() which che
Kunihiko Sakamoto 2013/04/23 06:36:26 Done.
890 table->src_offset = src_offset;
891 src_offset += table->src_length;
892 if (src_offset > std::numeric_limits<uint32_t>::max()) {
893 return OTS_FAILURE();
894 }
895 src_offset = Round4(src_offset); // TODO: reconsider
Yusuke Sato 2013/04/17 18:38:46 more descriptive comment, please.
Kunihiko Sakamoto 2013/04/23 06:36:26 I think this comment is obsolete. Removed.
896 table->dst_offset = dst_offset;
897 dst_offset += table->dst_length;
898 if (dst_offset > std::numeric_limits<uint32_t>::max()) {
899 return OTS_FAILURE();
900 }
901 dst_offset = Round4(dst_offset);
902 if ((table->flags & kCompressionTypeMask) != kCompressionTypeNone) {
903 uncompressed_sum += table->src_length;
904 if (uncompressed_sum > std::numeric_limits<uint32_t>::max()) {
905 return OTS_FAILURE();
906 }
907 }
908 }
909 // Enforce same 30M limit on uncompressed tables as OTS
910 if (uncompressed_sum > 30 * 1024 * 1024) {
911 return OTS_FAILURE();
912 }
913 if (src_offset > length || dst_offset > result_length) {
914 return OTS_FAILURE();
915 }
916
917 const uint32_t sfnt_header_and_table_directory_size = 12 + 16 * num_tables;
918 if (sfnt_header_and_table_directory_size > result_length) {
919 return OTS_FAILURE();
920 }
921
922 // Start building the font
923 size_t offset = 0;
924 offset = StoreU32(result, offset, flavor);
925 offset = Store16(result, offset, num_tables);
926 unsigned max_pow2 = 0;
927 while (1u << (max_pow2 + 1) <= num_tables) {
928 max_pow2++;
929 }
930 const uint16_t output_search_range = (1u << max_pow2) << 4;
931 offset = Store16(result, offset, output_search_range);
932 offset = Store16(result, offset, max_pow2);
933 offset = Store16(result, offset, (num_tables << 4) - output_search_range);
934 for (uint16_t i = 0; i < num_tables; ++i) {
935 const Table* table = &tables[i];
936 offset = StoreU32(result, offset, table->tag);
937 offset = StoreU32(result, offset, 0); // checksum, to fill in later
938 offset = StoreU32(result, offset, table->dst_offset);
939 offset = StoreU32(result, offset, table->dst_length);
940 }
941 std::vector<uint8_t> uncompressed_buf;
942 bool continue_valid = false;
943 for (uint16_t i = 0; i < num_tables; ++i) {
944 const Table* table = &tables[i];
945 uint32_t flags = table->flags;
946 const uint8_t* src_buf = data + table->src_offset;
947 uint32_t compression_type = flags & kCompressionTypeMask;
948 const uint8_t* transform_buf = NULL;
949 size_t transform_length = table->transform_length;
950 if ((flags & kWoff2FlagsContinueStream) != 0) {
951 if (!continue_valid) {
952 return OTS_FAILURE();
953 }
954 } else if (compression_type == kCompressionTypeNone) {
955 if (transform_length != table->src_length) {
956 return OTS_FAILURE();
957 }
958 transform_buf = src_buf;
959 continue_valid = false;
960 } else if ((flags & kWoff2FlagsContinueStream) == 0) {
961 uint64_t total_size = transform_length;
962 for (uint16_t j = i + 1; j < num_tables; ++j) {
963 if ((tables[j].flags & kWoff2FlagsContinueStream) == 0) {
964 break;
965 }
966 total_size += tables[j].transform_length;
967 if (total_size > std::numeric_limits<uint32_t>::max()) {
968 return OTS_FAILURE();
969 }
970 }
971 uncompressed_buf.resize(total_size);
972 if (!Woff2Uncompress(&uncompressed_buf[0], total_size,
973 src_buf, table->src_length, compression_type)) {
974 return OTS_FAILURE();
975 }
976 transform_buf = &uncompressed_buf[0];
977 continue_valid = true;
978 } else {
979 return OTS_FAILURE();
980 }
981
982 if ((flags & kWoff2FlagsTransform) == 0) {
983 if (transform_length != table->dst_length) {
984 return OTS_FAILURE();
985 }
986 if (static_cast<uint64_t>(table->dst_offset + transform_length) >
Yusuke Sato 2013/04/17 18:38:46 no-op?
Kunihiko Sakamoto 2013/05/07 10:17:23 Fixed.
987 result_length) {
988 return OTS_FAILURE();
989 }
990 std::memcpy(result + table->dst_offset, transform_buf,
991 transform_length);
992 } else {
993 if (!ReconstructTransformed(tables, table->tag,
994 transform_buf, transform_length, result, result_length)) {
995 return OTS_FAILURE();
996 }
997 }
998 if (continue_valid) {
999 transform_buf += transform_length;
1000 if (transform_buf > &uncompressed_buf[uncompressed_buf.size()]) {
1001 return OTS_FAILURE();
1002 }
1003 }
1004 }
1005
1006 return FixChecksums(tables, result);
1007 }
1008
1009 } // namespace ots
OLDNEW
« src/ots.cc ('K') | « src/woff2.h ('k') | test/ot-sanitise.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698