1
/*
2
 * Copyright © 2026 Ana Bobillo
3
 *
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use, copy,
8
 * modify, merge, publish, distribute, sublicense, and/or sell copies
9
 * of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 *
24
 * Author: Ana Bobillo <anabobillo3@gmail.com>
25
 */
26

            
27
#include "cairo-test.h"
28

            
29
#include <stdlib.h>
30
#include <string.h>
31
#include <cairo.h>
32
#include <cairo-pdf.h>
33

            
34
/* Check that the PDF surface does not embed JPEG, JPEG 2000 or JBIG2
35
 * mime data whose header is invalid.
36
 *
37
 * Run under AddressSanitizer, this also checks that the header
38
 * parsers stay within the data.
39
 */
40

            
41
typedef struct _mime_test {
42
    const char *name;
43
    const char *mime_type;
44
    const char *filter;		/* PDF filter of the embedded data */
45
    cairo_bool_t embedded;	/* whether the data is embedded */
46
    const unsigned char *data;
47
    unsigned long length;	/* bytes attached as mime data */
48
} mime_test_t;
49

            
50
/* A start of frame segment. jpeg-truncated-sof attaches it without
51
 * its last byte, the number of components, which stays in the buffer:
52
 * reading past the end of the mime data embeds the image. */
53
static const unsigned char jpeg_sof[] = {
54
    0xff, 0xd8,				/* SOI */
55
    0xff, 0xc0, 0x00, 0x11, 0x08,	/* SOF0, length, precision */
56
    0x00, 0x01, 0x00, 0x01,		/* height, width */
57
    0x03				/* number of components */
58
};
59

            
60
/* No start of frame segment. */
61
static const unsigned char jpeg_no_sof[] = {
62
    0xff, 0xd8,				/* SOI */
63
    0xff, 0xd9				/* EOI */
64
};
65

            
66
/* A segment longer than the data, so no start of frame segment. */
67
static const unsigned char jpeg_long_segment[] = {
68
    0xff, 0xd8,				/* SOI */
69
    0xff, 0xe0, 0xff, 0xff,		/* APP0, length */
70
    0x00, 0x00
71
};
72

            
73
/* A box with a length of 0 before the JP2 header box. */
74
static const unsigned char jpx_zero_length_box[] = {
75
    0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20,	/* signature box */
76
    0x0d, 0x0a, 0x87, 0x0a,
77
    0x00, 0x00, 0x00, 0x14, 0x66, 0x74, 0x79, 0x70,	/* file type box */
78
    0x6a, 0x70, 0x32, 0x20, 0x00, 0x00, 0x00, 0x00,
79
    0x6a, 0x70, 0x32, 0x20,
80
    0x00, 0x00, 0x00, 0x00, 0x78, 0x6d, 0x6c, 0x20,	/* XML box, length 0 */
81
    0x00, 0x00, 0x00, 0x00
82
};
83

            
84
/* A generic region segment of unknown length whose data ends with
85
 * the first byte of the end sequence. */
86
static const unsigned char jbig2_unterminated_region[] = {
87
    0x00, 0x00, 0x00, 0x00,		/* segment number */
88
    0x26,				/* immediate generic region */
89
    0x00,				/* no referred-to segments */
90
    0x01,				/* page association */
91
    0xff, 0xff, 0xff, 0xff,		/* data length unknown */
92
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* region segment information */
93
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
94
    0x00, 0x00, 0x00, 0x00, 0x00,
95
    0x00,				/* generic region flags */
96
    0xff
97
};
98

            
99
#define MIME_TEST(name, type, filter, embedded, data, length) \
100
    { name, type, filter, embedded, data, length }
101

            
102
static const mime_test_t mime_tests[] = {
103
    MIME_TEST ("jpeg-sof",
104
	       CAIRO_MIME_TYPE_JPEG, "/DCTDecode", TRUE,
105
	       jpeg_sof, sizeof (jpeg_sof)),
106
    MIME_TEST ("jpeg-truncated-sof",
107
	       CAIRO_MIME_TYPE_JPEG, "/DCTDecode", FALSE,
108
	       jpeg_sof, sizeof (jpeg_sof) - 1),
109
    MIME_TEST ("jpeg-no-sof",
110
	       CAIRO_MIME_TYPE_JPEG, "/DCTDecode", FALSE,
111
	       jpeg_no_sof, sizeof (jpeg_no_sof)),
112
    MIME_TEST ("jpeg-long-segment",
113
	       CAIRO_MIME_TYPE_JPEG, "/DCTDecode", FALSE,
114
	       jpeg_long_segment, sizeof (jpeg_long_segment)),
115
    MIME_TEST ("jpx-zero-length-box",
116
	       CAIRO_MIME_TYPE_JP2, "/JPXDecode", FALSE,
117
	       jpx_zero_length_box, sizeof (jpx_zero_length_box)),
118
    MIME_TEST ("jbig2-unterminated-region",
119
	       CAIRO_MIME_TYPE_JBIG2, "/JBIG2Decode", FALSE,
120
	       jbig2_unterminated_region, sizeof (jbig2_unterminated_region)),
121
};
122

            
123
typedef struct _buffer {
124
    unsigned char *data;
125
    size_t length;
126
    size_t size;
127
} buffer_t;
128

            
129
static cairo_status_t
130
write_func (void *closure, const unsigned char *data, unsigned int length)
131
{
132
    buffer_t *buffer = closure;
133

            
134
    if (buffer->length + length > buffer->size) {
135
	size_t size = 2 * (buffer->length + length);
136
	unsigned char *new_data = realloc (buffer->data, size);
137

            
138
	if (new_data == NULL)
139
	    return CAIRO_STATUS_NO_MEMORY;
140

            
141
	buffer->data = new_data;
142
	buffer->size = size;
143
    }
144

            
145
    memcpy (buffer->data + buffer->length, data, length);
146
    buffer->length += length;
147

            
148
    return CAIRO_STATUS_SUCCESS;
149
}
150

            
151
static cairo_bool_t
152
buffer_contains (const buffer_t *buffer, const char *str)
153
{
154
    size_t len = strlen (str);
155
    size_t i;
156

            
157
    for (i = 0; i + len <= buffer->length; i++) {
158
	if (memcmp (buffer->data + i, str, len) == 0)
159
	    return TRUE;
160
    }
161

            
162
    return FALSE;
163
}
164

            
165
static cairo_test_status_t
166
test_mime_data (const cairo_test_context_t *ctx, const mime_test_t *test)
167
{
168
    cairo_surface_t *image;
169
    cairo_surface_t *surface;
170
    cairo_t *cr;
171
    cairo_status_t status;
172
    buffer_t buffer = { NULL, 0, 0 };
173
    cairo_test_status_t result = CAIRO_TEST_SUCCESS;
174

            
175
    image = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 1, 1);
176

            
177
    status = cairo_surface_set_mime_data (image, test->mime_type,
178
					  test->data, test->length,
179
					  NULL, NULL);
180
    if (status) {
181
	cairo_surface_destroy (image);
182
	cairo_test_log (ctx, "%s: failed to set mime data: %s\n",
183
			test->name, cairo_status_to_string (status));
184
	return CAIRO_TEST_FAILURE;
185
    }
186

            
187
    surface = cairo_pdf_surface_create_for_stream (write_func, &buffer, 1, 1);
188
    cr = cairo_create (surface);
189
    cairo_set_source_surface (cr, image, 0, 0);
190
    cairo_paint (cr);
191
    status = cairo_status (cr);
192
    cairo_destroy (cr);
193
    cairo_surface_finish (surface);
194
    if (status == CAIRO_STATUS_SUCCESS)
195
	status = cairo_surface_status (surface);
196
    cairo_surface_destroy (surface);
197
    cairo_surface_destroy (image);
198

            
199
    if (status) {
200
	cairo_test_log (ctx, "%s: failed to create pdf: %s\n",
201
			test->name, cairo_status_to_string (status));
202
	result = CAIRO_TEST_FAILURE;
203
    } else if (buffer_contains (&buffer, test->filter) != test->embedded) {
204
	cairo_test_log (ctx, "%s: mime data %s with %s\n",
205
			test->name,
206
			test->embedded ? "not embedded" : "embedded",
207
			test->filter);
208
	result = CAIRO_TEST_FAILURE;
209
    }
210

            
211
    free (buffer.data);
212

            
213
    return result;
214
}
215

            
216
static cairo_test_status_t
217
1
preamble (cairo_test_context_t *ctx)
218
{
219
1
    cairo_test_status_t result = CAIRO_TEST_SUCCESS;
220
    int i;
221

            
222
1
    if (! cairo_test_is_target_enabled (ctx, "pdf"))
223
1
	return CAIRO_TEST_UNTESTED;
224

            
225
    for (i = 0; i < ARRAY_LENGTH (mime_tests); i++) {
226
	if (test_mime_data (ctx, &mime_tests[i]) != CAIRO_TEST_SUCCESS)
227
	    result = CAIRO_TEST_FAILURE;
228
    }
229

            
230
    return result;
231
}
232

            
233
1
CAIRO_TEST (pdf_mime_data_invalid,
234
	    "Check invalid mime data is not embedded by the PDF surface",
235
	    "pdf, mime-data", /* keywords */
236
	    NULL, /* requirements */
237
	    0, 0,
238
	    preamble, NULL)