1
/* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2
/* cairo - a vector graphics library with display and print output
3
 *
4
 * Copyright © 2008 Adrian Johnson
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it either under the terms of the GNU Lesser General Public
8
 * License version 2.1 as published by the Free Software Foundation
9
 * (the "LGPL") or, at your option, under the terms of the Mozilla
10
 * Public License Version 1.1 (the "MPL"). If you do not alter this
11
 * notice, a recipient may use your version of this file under either
12
 * the MPL or the LGPL.
13
 *
14
 * You should have received a copy of the LGPL along with this library
15
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17
 * You should have received a copy of the MPL along with this library
18
 * in the file COPYING-MPL-1.1
19
 *
20
 * The contents of this file are subject to the Mozilla Public License
21
 * Version 1.1 (the "License"); you may not use this file except in
22
 * compliance with the License. You may obtain a copy of the License at
23
 * http://www.mozilla.org/MPL/
24
 *
25
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27
 * the specific language governing rights and limitations.
28
 *
29
 * The Original Code is the cairo graphics library.
30
 *
31
 * The Initial Developer of the Original Code is Adrian Johnson.
32
 *
33
 * Contributor(s):
34
 *	Adrian Johnson <ajohnson@redneon.com>
35
 */
36

            
37
#include "cairoint.h"
38

            
39
#include "cairo-error-private.h"
40
#include "cairo-image-info-private.h"
41

            
42
/* JPEG (image/jpeg)
43
 *
44
 * http://www.w3.org/Graphics/JPEG/itu-t81.pdf
45
 */
46

            
47
/* Markers with no parameters. All other markers are followed by a two
48
 * byte length of the parameters. */
49
#define TEM       0x01
50
#define RST_begin 0xd0
51
#define RST_end   0xd7
52
#define SOI       0xd8
53
#define EOI       0xd9
54

            
55
/* Start of frame markers. */
56
#define SOF0  0xc0
57
#define SOF1  0xc1
58
#define SOF2  0xc2
59
#define SOF3  0xc3
60
#define SOF5  0xc5
61
#define SOF6  0xc6
62
#define SOF7  0xc7
63
#define SOF9  0xc9
64
#define SOF10 0xca
65
#define SOF11 0xcb
66
#define SOF13 0xcd
67
#define SOF14 0xce
68
#define SOF15 0xcf
69

            
70
static const unsigned char *
71
_jpeg_skip_segment (const unsigned char *p, const unsigned char *end)
72
{
73
    int len;
74

            
75
    p++;
76
    len = (p[0] << 8) | p[1];
77

            
78
    if (len > end - p)
79
	return end;
80

            
81
    return p + len;
82
}
83

            
84
static void
85
_jpeg_extract_info (cairo_image_info_t *info, const unsigned char *p)
86
{
87
    info->width = (p[6] << 8) + p[7];
88
    info->height = (p[4] << 8) + p[5];
89
    info->num_components = p[8];
90
    info->bits_per_component = p[3];
91
}
92

            
93
cairo_int_status_t
94
_cairo_image_info_get_jpeg_info (cairo_image_info_t	*info,
95
				 const unsigned char	*data,
96
				 unsigned long		 length)
97
{
98
    const unsigned char *p = data;
99

            
100
    while (p + 1 < data + length) {
101
	if (*p != 0xff)
102
	    return CAIRO_INT_STATUS_UNSUPPORTED;
103
	p++;
104

            
105
	switch (*p) {
106
	    /* skip fill bytes */
107
	case 0xff:
108
	    p++;
109
	    break;
110

            
111
	case TEM:
112
	case SOI:
113
	case EOI:
114
	    p++;
115
	    break;
116

            
117
	case SOF0:
118
	case SOF1:
119
	case SOF2:
120
	case SOF3:
121
	case SOF5:
122
	case SOF6:
123
	case SOF7:
124
	case SOF9:
125
	case SOF10:
126
	case SOF11:
127
	case SOF13:
128
	case SOF14:
129
	case SOF15:
130
	    /* Start of frame found. Extract the image parameters. */
131
	    if (p + 9 > data + length)
132
		return CAIRO_INT_STATUS_UNSUPPORTED;
133

            
134
	    _jpeg_extract_info (info, p);
135
	    return CAIRO_STATUS_SUCCESS;
136

            
137
	default:
138
	    if (*p >= RST_begin && *p <= RST_end) {
139
		p++;
140
		break;
141
	    }
142

            
143
	    if (p + 3 > data + length)
144
		return CAIRO_INT_STATUS_UNSUPPORTED;
145

            
146
	    p = _jpeg_skip_segment (p, data + length);
147
	    break;
148
	}
149
    }
150

            
151
    /* No start of frame found. */
152
    return CAIRO_INT_STATUS_UNSUPPORTED;
153
}
154

            
155
/* JPEG 2000 (image/jp2)
156
 *
157
 * http://www.jpeg.org/public/15444-1annexi.pdf
158
 */
159

            
160
#define JPX_FILETYPE 0x66747970
161
#define JPX_JP2_HEADER 0x6A703268
162
#define JPX_IMAGE_HEADER 0x69686472
163

            
164
static const unsigned char _jpx_signature[] = {
165
    0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a
166
};
167

            
168
/* Returns the length of the box starting at p, or 0 if the box is
169
 * invalid or does not fit in the data. */
170
static size_t
171
_jpx_box_length (const unsigned char *p, const unsigned char *end)
172
{
173
    uint32_t length;
174

            
175
    if (p + 8 > end)
176
	return 0;
177

            
178
    length = get_unaligned_be32 (p);
179

            
180
    /* A length of 0 means that the box extends to the end of the data. */
181
    if (length == 0)
182
	return end - p;
183

            
184
    /* A length of 1 means that a 64-bit length follows the box type.
185
     * Such boxes are not supported, nor are the reserved lengths 2
186
     * to 7. */
187
    if (length < 8 || length > (size_t) (end - p))
188
	return 0;
189

            
190
    return length;
191
}
192

            
193
static const unsigned char *
194
_jpx_next_box (const unsigned char *p, const unsigned char *end)
195
{
196
    size_t length = _jpx_box_length (p, end);
197

            
198
    if (length == 0)
199
	return end;
200

            
201
    return p + length;
202
}
203

            
204
static const unsigned char *
205
_jpx_get_box_contents (const unsigned char *p)
206
{
207
    return p + 8;
208
}
209

            
210
static cairo_bool_t
211
_jpx_match_box (const unsigned char *p, const unsigned char *end, uint32_t type)
212
{
213
    return _jpx_box_length (p, end) != 0 && get_unaligned_be32 (p + 4) == type;
214
}
215

            
216
static const unsigned char *
217
_jpx_find_box (const unsigned char *p, const unsigned char *end, uint32_t type)
218
{
219
    while (p < end) {
220
	if (_jpx_match_box (p, end, type))
221
	    return p;
222
	p = _jpx_next_box (p, end);
223
    }
224

            
225
    return NULL;
226
}
227

            
228
static cairo_int_status_t
229
_jpx_extract_info (const unsigned char *p, cairo_image_info_t *info, const unsigned char *end)
230
{
231
    if (p + 11 >= end) {
232
	return CAIRO_INT_STATUS_UNSUPPORTED;
233
    }
234

            
235
    info->height = get_unaligned_be32 (p);
236
    info->width = get_unaligned_be32 (p + 4);
237
    info->num_components = (p[8] << 8) + p[9];
238
    info->bits_per_component = p[10];
239

            
240
    return CAIRO_STATUS_SUCCESS;
241
}
242

            
243
cairo_int_status_t
244
_cairo_image_info_get_jpx_info (cairo_image_info_t	*info,
245
				const unsigned char	*data,
246
				unsigned long		 length)
247
{
248
    const unsigned char *p = data;
249
    const unsigned char *end = data + length;
250

            
251
    /* First 12 bytes must be the JPEG 2000 signature box. */
252
    if (length < ARRAY_LENGTH(_jpx_signature) ||
253
	memcmp(p, _jpx_signature, ARRAY_LENGTH(_jpx_signature)) != 0)
254
	return CAIRO_INT_STATUS_UNSUPPORTED;
255

            
256
    p += ARRAY_LENGTH(_jpx_signature);
257

            
258
    /* Next box must be a File Type Box */
259
    if (! _jpx_match_box (p, end, JPX_FILETYPE))
260
	return CAIRO_INT_STATUS_UNSUPPORTED;
261

            
262
    p = _jpx_next_box (p, end);
263

            
264
    /* Locate the JP2 header box. */
265
    p = _jpx_find_box (p, end, JPX_JP2_HEADER);
266
    if (!p)
267
	return CAIRO_INT_STATUS_UNSUPPORTED;
268

            
269
    /* Step into the JP2 header box. First box must be the Image
270
     * Header */
271
    p = _jpx_get_box_contents (p);
272
    if (! _jpx_match_box (p, end, JPX_IMAGE_HEADER))
273
	return CAIRO_INT_STATUS_UNSUPPORTED;
274

            
275
    /* Get the image info */
276
    p = _jpx_get_box_contents (p);
277
    return _jpx_extract_info (p, info, end);
278
}
279

            
280
/* PNG (image/png)
281
 *
282
 * http://www.w3.org/TR/2003/REC-PNG-20031110/
283
 */
284

            
285
#define PNG_IHDR 0x49484452
286

            
287
static const unsigned char _png_magic[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
288

            
289
cairo_int_status_t
290
_cairo_image_info_get_png_info (cairo_image_info_t     *info,
291
                               const unsigned char     *data,
292
                               unsigned long            length)
293
{
294
    const unsigned char *p = data;
295
    const unsigned char *end = data + length;
296

            
297
    if (length < 8 || memcmp (data, _png_magic, 8) != 0)
298
       return CAIRO_INT_STATUS_UNSUPPORTED;
299

            
300
    p += 8;
301

            
302
    /* The first chunk must be IDHR. IDHR has 13 bytes of data plus
303
     * the 12 bytes of overhead for the chunk. */
304
    if (p + 13 + 12 > end)
305
       return CAIRO_INT_STATUS_UNSUPPORTED;
306

            
307
    p += 4;
308
    if (get_unaligned_be32 (p) != PNG_IHDR)
309
       return CAIRO_INT_STATUS_UNSUPPORTED;
310

            
311
    p += 4;
312
    info->width = get_unaligned_be32 (p);
313
    p += 4;
314
    info->height = get_unaligned_be32 (p);
315

            
316
    return CAIRO_STATUS_SUCCESS;
317
}
318

            
319
static const unsigned char *
320
_jbig2_find_data_end (const unsigned char *p,
321
		      const unsigned char *end,
322
		      int                  type)
323
{
324
    unsigned char end_seq[2];
325
    int mmr;
326

            
327
    /* Segments of type "Immediate generic region" may have an
328
     * unspecified data length.  The JBIG2 specification specifies the
329
     * method to find the end of the data for these segments. */
330
    if (type == 36 || type == 38 || type == 39) {
331
	if (p + 18 < end) {
332
	    mmr = p[17] & 0x01;
333
	    if (mmr) {
334
		/* MMR encoding ends with 0x00, 0x00 */
335
		end_seq[0] = 0x00;
336
		end_seq[1] = 0x00;
337
	    } else {
338
		/* Template encoding ends with 0xff, 0xac */
339
		end_seq[0] = 0xff;
340
		end_seq[1] = 0xac;
341
	    }
342
	    p += 18;
343
	    while (p + 1 < end) {
344
		if (p[0] == end_seq[0] && p[1] == end_seq[1]) {
345
		    /* Skip the 2 terminating bytes and the 4 byte row count that follows. */
346
		    p += 6;
347
		    if (p < end)
348
			return p;
349
		}
350
		p++;
351
	    }
352
	}
353
    }
354

            
355
    return NULL;
356
}
357

            
358
static const unsigned char *
359
_jbig2_get_next_segment (const unsigned char  *p,
360
			 const unsigned char  *end,
361
			 int                  *type,
362
			 const unsigned char **data,
363
			 unsigned long        *data_len)
364
{
365
    unsigned long seg_num;
366
    cairo_bool_t big_page_size;
367
    uint32_t num_segs;
368
    size_t ref_seg_bytes;
369
    size_t referred_size;
370
    size_t header_len;
371

            
372
    if (p + 6 >= end)
373
	return NULL;
374

            
375
    seg_num = get_unaligned_be32 (p);
376
    *type = p[4] & 0x3f;
377
    big_page_size = (p[4] & 0x40) != 0;
378
    p += 5;
379

            
380
    num_segs = p[0] >> 5;
381
    if (num_segs == 7) {
382
	if (p + 4 >= end)
383
	    return NULL;
384
	num_segs = get_unaligned_be32 (p) & 0x1fffffff;
385
	ref_seg_bytes = 4 + ((num_segs + 1)/8);
386
    } else {
387
	ref_seg_bytes = 1;
388
    }
389

            
390
    if (seg_num <= 256)
391
	referred_size = 1;
392
    else if (seg_num <= 65536)
393
	referred_size = 2;
394
    else
395
	referred_size = 4;
396

            
397
    /* Skip the referred-to segment count and retention flags, the
398
     * referred-to segment numbers and the page association. num_segs
399
     * is less than 2^29, so header_len is at most 0x84000004 and
400
     * header_len + 4 does not overflow, even with a 32-bit size_t. */
401
    header_len = ref_seg_bytes + num_segs * referred_size +
402
		 (big_page_size ? 4 : 1);
403
    if (header_len + 4 >= (size_t) (end - p))
404
	return NULL;
405

            
406
    p += header_len;
407
    *data_len = get_unaligned_be32 (p);
408
    p += 4;
409
    *data = p;
410

            
411
    if (*data_len == 0xffffffff) {
412
	/* if data length is -1 we have to scan through the data to find the end */
413
	p = _jbig2_find_data_end (*data, end, *type);
414
	if (!p || p >= end)
415
	    return NULL;
416

            
417
	*data_len = p - *data;
418
    } else {
419
	if (*data_len >= (size_t) (end - p))
420
	    return NULL;
421

            
422
	p += *data_len;
423
    }
424

            
425
    return p;
426
}
427

            
428
static void
429
_jbig2_extract_info (cairo_image_info_t *info, const unsigned char *p)
430
{
431
    info->width = get_unaligned_be32 (p);
432
    info->height = get_unaligned_be32 (p + 4);
433
    info->num_components = 1;
434
    info->bits_per_component = 1;
435
}
436

            
437
cairo_int_status_t
438
_cairo_image_info_get_jbig2_info (cairo_image_info_t	*info,
439
				  const unsigned char	*data,
440
				  unsigned long		 length)
441
{
442
    const unsigned char *p = data;
443
    const unsigned char *end = data + length;
444
    int seg_type;
445
    const unsigned char *seg_data;
446
    unsigned long seg_data_len;
447

            
448
    while (p && p < end) {
449
	p = _jbig2_get_next_segment (p, end, &seg_type, &seg_data, &seg_data_len);
450
	if (p && seg_type == 48 && seg_data_len > 8) {
451
	    /* page information segment */
452
	    _jbig2_extract_info (info, seg_data);
453
	    return CAIRO_STATUS_SUCCESS;
454
	}
455
    }
456

            
457
    return CAIRO_INT_STATUS_UNSUPPORTED;
458
}