1 // Copyright 2020 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 #include <cairo.h>
16 #include "fuzzer_temp_file.h"
17
18 const int glyph_range = 9;
19
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)20 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
21 if (size < glyph_range) {
22 return 0;
23 }
24 cairo_t *cr;
25 cairo_surface_t *surface;
26 cairo_status_t status;
27 cairo_text_extents_t extents;
28 cairo_text_cluster_t cluster;
29
30 char *tmpfile = fuzzer_get_tmpfile(data, size);
31 surface = cairo_image_surface_create_from_png(tmpfile);
32 status = cairo_surface_status(surface);
33 if (status != CAIRO_STATUS_SUCCESS) {
34 fuzzer_release_tmpfile(tmpfile);
35 return 0;
36 }
37
38 char *buf = (char *) calloc(size + 1, sizeof(char));
39 memcpy(buf, data, size);
40 buf[size] = '\0';
41
42 cr = cairo_create(surface);
43 cairo_text_extents(cr, buf, &extents);
44 cluster.num_bytes = size;
45 cluster.num_glyphs = 1;
46 for (int i = 0; i < glyph_range; i++) {
47 // Taken from test/text-glyph-range.c
48 cairo_glyph_t glyph = {
49 (long int)data[i], 10 * i, 25
50 };
51 cairo_show_text_glyphs(cr, buf, size, &glyph, 1, &cluster, 1, 0);
52 }
53
54 cairo_destroy(cr);
55 cairo_surface_destroy(surface);
56 free(buf);
57 fuzzer_release_tmpfile(tmpfile);
58 return 0;
59 }
60