Coverage Report

Created: 2025-06-10 07:07

/src/gs_fuzzlib.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
# Copyright 2019 The Chromium OS Authors.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#      http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
#
16
################################################################################
17
*/
18
19
#include <base/gserrors.h>
20
#include <psi/iapi.h>
21
22
#include <limits.h>
23
#include <stdio.h>
24
#include <string.h>
25
#include <stdint.h>
26
27
static const unsigned char *g_data;
28
static size_t g_size;
29
30
int fuzz_gs_device(
31
  const unsigned char *buf,
32
  size_t size,
33
  int color_scheme,
34
  const char *device_target,
35
  const char *output_file,
36
  int do_interpolation
37
);
38
39
122k
#define min(x, y) ((x) < (y) ? (x) : (y))
40
41
static int gs_stdin(void *inst, char *buf, int len)
42
61.0k
{
43
61.0k
  size_t to_copy = min(len, g_size);
44
61.0k
  to_copy = min(INT_MAX, to_copy);
45
46
61.0k
  memcpy(buf, g_data, to_copy);
47
48
61.0k
  g_data += to_copy;
49
61.0k
  g_size -= to_copy;
50
51
61.0k
  return to_copy;
52
61.0k
}
53
54
static int gs_stdnull(void *inst, const char *buf, int len)
55
66.6k
{
56
  /* Just discard everything. */
57
66.6k
  return len;
58
66.6k
}
59
60
int gs_to_raster_fuzz(
61
  const unsigned char *buf,
62
  size_t size,
63
  int color_scheme
64
)
65
0
{
66
0
  return fuzz_gs_device(buf, size, color_scheme, "cups", "/dev/null", 0);
67
0
}
68
69
int fuzz_gs_device(
70
  const unsigned char *buf,
71
  size_t size,
72
  int color_scheme,
73
  const char *device_target,
74
  const char *output_file,
75
  int do_interpolation
76
)
77
9.05k
{
78
9.05k
  int ret;
79
9.05k
  void *gs = NULL;
80
9.05k
  char color_space[50];
81
9.05k
  char gs_device[50];
82
9.05k
  char gs_o[100];
83
9.05k
  char opt_interpolation[50];
84
  /*
85
   * We are expecting color_scheme to be in the [0:62] interval.
86
   * This corresponds to the color schemes defined here:
87
   * https://github.com/ArtifexSoftware/ghostpdl/blob/8c97d5adce0040ac38a1fb4d7954499c65f582ff/cups/libs/cups/raster.h#L102
88
   */
89
9.05k
  sprintf(color_space, "-dcupsColorSpace=%d", color_scheme);
90
9.05k
  sprintf(gs_device, "-sDEVICE=%s", device_target);
91
9.05k
  sprintf(gs_o, "-sOutputFile=%s", output_file);
92
9.05k
  if (do_interpolation) {
93
5.28k
    sprintf(opt_interpolation, "-dDOINTERPOLATE");
94
5.28k
  }
95
3.76k
  else {
96
3.76k
    sprintf(opt_interpolation, "-dNOINTERPOLATE");
97
3.76k
  }
98
  /* Mostly stolen from cups-filters gstoraster. */
99
9.05k
  char *args[] = {
100
9.05k
    "gs",
101
9.05k
    "-K1048576",
102
9.05k
    "-r200x200",
103
9.05k
    "-sBandListStorage=memory",
104
9.05k
    "-dMaxBitmap=0",
105
9.05k
    "-dBufferSpace=450k",
106
9.05k
    "-dMediaPosition=1",
107
9.05k
    color_space,
108
9.05k
    "-dQUIET",
109
9.05k
    "-dSAFER",
110
9.05k
    "-dNOPAUSE",
111
9.05k
    "-dBATCH",
112
9.05k
    opt_interpolation,
113
9.05k
    "-dNOMEDIAATTRS",
114
9.05k
    "-sstdout=%%stderr",
115
9.05k
    gs_o,
116
9.05k
    gs_device,
117
9.05k
    "-_",
118
9.05k
  };
119
9.05k
  int argc = sizeof(args) / sizeof(args[0]);
120
121
  /* Stash buffers globally, for gs_stdin(). */
122
9.05k
  g_data = buf;
123
9.05k
  g_size = size;
124
125
9.05k
  ret = gsapi_new_instance(&gs, NULL);
126
9.05k
  if (ret < 0) {
127
0
    fprintf(stderr, "gsapi_new_instance: error %d\n", ret);
128
0
    return ret;
129
0
  }
130
131
9.05k
  gsapi_set_stdio(gs, gs_stdin, gs_stdnull, gs_stdnull);
132
9.05k
  ret = gsapi_set_arg_encoding(gs, GS_ARG_ENCODING_UTF8);
133
9.05k
  if (ret < 0) {
134
0
    fprintf(stderr, "gsapi_set_arg_encoding: error %d\n", ret);
135
0
    gsapi_delete_instance(gs);
136
0
    return ret;
137
0
  }
138
139
9.05k
  ret = gsapi_init_with_args(gs, argc, args);
140
9.05k
  if (ret && ret != gs_error_Quit)
141
    /* Just keep going, to cleanup. */
142
3.10k
    fprintf(stderr, "gsapi_init_with_args: error %d\n", ret);
143
144
9.05k
  ret = gsapi_exit(gs);
145
9.05k
  if (ret < 0 && ret != gs_error_Quit) {
146
0
    fprintf(stderr, "gsapi_exit: error %d\n", ret);
147
0
    return ret;
148
0
  }
149
150
9.05k
  gsapi_delete_instance(gs);
151
152
9.05k
  return 0;
153
9.05k
}