/src/ttf-parser/fuzz/fuzz_targets/fuzz-table-with-builder.rs
Line | Count | Source |
1 | | // Copyright 2025 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #![no_main] |
16 | | |
17 | | use libfuzzer_sys::fuzz_target; |
18 | | |
19 | | use ttf_parser::{GlyphId, Face, cff}; |
20 | | use std::fmt::Write; |
21 | | |
22 | | fuzz_target!(|fuzz_data: &[u8]| { |
23 | | // Skip this iteration if data is not enough |
24 | | if fuzz_data.is_empty() { |
25 | | return; |
26 | | } |
27 | | |
28 | | let choice = fuzz_data[0] % 2; |
29 | | let data = &fuzz_data[1..]; |
30 | | |
31 | | // Randomly choose a module to fuzz |
32 | | if (choice) == 0 { |
33 | | // Fuzzing CFF1 module |
34 | | if let Some(table) = cff::Table::parse(data) { |
35 | | let mut builder = Builder(String::new()); |
36 | | let _ = table.outline(GlyphId(0), &mut builder); |
37 | | } |
38 | | } else { |
39 | | // Fuzzing glyf module |
40 | | if let Ok(face) = Face::parse(data, 0) { |
41 | | let mut builder = Builder(String::new()); |
42 | | let _ = face.outline_glyph(GlyphId(0), &mut builder); |
43 | | } |
44 | | } |
45 | | }); |
46 | | |
47 | | // Custom Builder implementation |
48 | | struct Builder(String); |
49 | | |
50 | | impl ttf_parser::OutlineBuilder for Builder { |
51 | 3.03M | fn move_to(&mut self, x: f32, y: f32) { |
52 | 3.03M | let _ = write!(&mut self.0, "M {} {} ", x, y); |
53 | 3.03M | } |
54 | | |
55 | 98.6k | fn line_to(&mut self, x: f32, y: f32) { |
56 | 98.6k | let _ = write!(&mut self.0, "L {} {} ", x, y); |
57 | 98.6k | } |
58 | | |
59 | 0 | fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) { |
60 | 0 | let _ = write!(&mut self.0, "Q {} {} {} {} ", x1, y1, x, y); |
61 | 0 | } |
62 | | |
63 | 13.7M | fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) { |
64 | 13.7M | let _ = write!(&mut self.0, "C {} {} {} {} {} {} ", x1, y1, x2, y2, x, y); |
65 | 13.7M | } |
66 | | |
67 | 3.03M | fn close(&mut self) { |
68 | 3.03M | let _ = write!(&mut self.0, "Z "); |
69 | 3.03M | } |
70 | | } |