• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cairo-pdf.h>
17 #include "fuzzer_temp_file.h"
18 
19 const double width_in_inches = 3;
20 const double height_in_inches = 3;
21 const double width_in_points = width_in_inches * 72.0;
22 const double height_in_points = height_in_inches * 72.0;
23 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)24 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
25     cairo_t *cr;
26     cairo_surface_t *surface;
27     cairo_status_t status;
28 
29     if (size == 0) {
30         return 0;
31     }
32 
33     char *tmpfile = fuzzer_get_tmpfile(data, size);
34     surface = cairo_pdf_surface_create(tmpfile, width_in_points, height_in_points);
35     status = cairo_surface_status(surface);
36     if (status != CAIRO_STATUS_SUCCESS) {
37         fuzzer_release_tmpfile(tmpfile);
38         return 0;
39     }
40 
41     char *buf = (char *) calloc(size + 1, sizeof(char));
42     memcpy(buf, data, size);
43     buf[size] = '\0';
44 
45     cairo_pdf_surface_set_metadata(surface, CAIRO_PDF_METADATA_TITLE, buf);
46     cr = cairo_create(surface);
47     cairo_tag_begin(cr, buf, NULL);
48     cairo_tag_end(cr, buf);
49 
50     cairo_destroy(cr);
51     cairo_surface_destroy(surface);
52     free(buf);
53     fuzzer_release_tmpfile(tmpfile);
54     return 0;
55 }
56