/work/workdir/UnpackedTarball/cairo/src/cairo-image-info.c
Line | Count | Source (jump to first uncovered line) |
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 | 0 | #define TEM 0x01 |
50 | 0 | #define RST_begin 0xd0 |
51 | 0 | #define RST_end 0xd7 |
52 | 0 | #define SOI 0xd8 |
53 | 0 | #define EOI 0xd9 |
54 | | |
55 | | /* Start of frame markers. */ |
56 | 0 | #define SOF0 0xc0 |
57 | 0 | #define SOF1 0xc1 |
58 | 0 | #define SOF2 0xc2 |
59 | 0 | #define SOF3 0xc3 |
60 | 0 | #define SOF5 0xc5 |
61 | 0 | #define SOF6 0xc6 |
62 | 0 | #define SOF7 0xc7 |
63 | 0 | #define SOF9 0xc9 |
64 | 0 | #define SOF10 0xca |
65 | 0 | #define SOF11 0xcb |
66 | 0 | #define SOF13 0xcd |
67 | 0 | #define SOF14 0xce |
68 | 0 | #define SOF15 0xcf |
69 | | |
70 | | static const unsigned char * |
71 | | _jpeg_skip_segment (const unsigned char *p) |
72 | 0 | { |
73 | 0 | int len; |
74 | |
|
75 | 0 | p++; |
76 | 0 | len = (p[0] << 8) | p[1]; |
77 | |
|
78 | 0 | return p + len; |
79 | 0 | } |
80 | | |
81 | | static void |
82 | | _jpeg_extract_info (cairo_image_info_t *info, const unsigned char *p) |
83 | 0 | { |
84 | 0 | info->width = (p[6] << 8) + p[7]; |
85 | 0 | info->height = (p[4] << 8) + p[5]; |
86 | 0 | info->num_components = p[8]; |
87 | 0 | info->bits_per_component = p[3]; |
88 | 0 | } |
89 | | |
90 | | cairo_int_status_t |
91 | | _cairo_image_info_get_jpeg_info (cairo_image_info_t *info, |
92 | | const unsigned char *data, |
93 | | unsigned long length) |
94 | 0 | { |
95 | 0 | const unsigned char *p = data; |
96 | |
|
97 | 0 | while (p + 1 < data + length) { |
98 | 0 | if (*p != 0xff) |
99 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
100 | 0 | p++; |
101 | |
|
102 | 0 | switch (*p) { |
103 | | /* skip fill bytes */ |
104 | 0 | case 0xff: |
105 | 0 | p++; |
106 | 0 | break; |
107 | | |
108 | 0 | case TEM: |
109 | 0 | case SOI: |
110 | 0 | case EOI: |
111 | 0 | p++; |
112 | 0 | break; |
113 | | |
114 | 0 | case SOF0: |
115 | 0 | case SOF1: |
116 | 0 | case SOF2: |
117 | 0 | case SOF3: |
118 | 0 | case SOF5: |
119 | 0 | case SOF6: |
120 | 0 | case SOF7: |
121 | 0 | case SOF9: |
122 | 0 | case SOF10: |
123 | 0 | case SOF11: |
124 | 0 | case SOF13: |
125 | 0 | case SOF14: |
126 | 0 | case SOF15: |
127 | | /* Start of frame found. Extract the image parameters. */ |
128 | 0 | if (p + 8 > data + length) |
129 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
130 | | |
131 | 0 | _jpeg_extract_info (info, p); |
132 | 0 | return CAIRO_STATUS_SUCCESS; |
133 | | |
134 | 0 | default: |
135 | 0 | if (*p >= RST_begin && *p <= RST_end) { |
136 | 0 | p++; |
137 | 0 | break; |
138 | 0 | } |
139 | | |
140 | 0 | if (p + 3 > data + length) |
141 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
142 | | |
143 | 0 | p = _jpeg_skip_segment (p); |
144 | 0 | break; |
145 | 0 | } |
146 | 0 | } |
147 | | |
148 | 0 | return CAIRO_STATUS_SUCCESS; |
149 | 0 | } |
150 | | |
151 | | /* JPEG 2000 (image/jp2) |
152 | | * |
153 | | * http://www.jpeg.org/public/15444-1annexi.pdf |
154 | | */ |
155 | | |
156 | 0 | #define JPX_FILETYPE 0x66747970 |
157 | 0 | #define JPX_JP2_HEADER 0x6A703268 |
158 | 0 | #define JPX_IMAGE_HEADER 0x69686472 |
159 | | |
160 | | static const unsigned char _jpx_signature[] = { |
161 | | 0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a |
162 | | }; |
163 | | |
164 | | static const unsigned char * |
165 | | _jpx_next_box (const unsigned char *p) |
166 | 0 | { |
167 | 0 | return p + get_unaligned_be32 (p); |
168 | 0 | } |
169 | | |
170 | | static const unsigned char * |
171 | | _jpx_get_box_contents (const unsigned char *p) |
172 | 0 | { |
173 | 0 | return p + 8; |
174 | 0 | } |
175 | | |
176 | | static cairo_bool_t |
177 | | _jpx_match_box (const unsigned char *p, const unsigned char *end, uint32_t type) |
178 | 0 | { |
179 | 0 | uint32_t length; |
180 | |
|
181 | 0 | if (p + 8 < end) { |
182 | 0 | length = get_unaligned_be32 (p); |
183 | 0 | if (get_unaligned_be32 (p + 4) == type && p + length < end) |
184 | 0 | return TRUE; |
185 | 0 | } |
186 | | |
187 | 0 | return FALSE; |
188 | 0 | } |
189 | | |
190 | | static const unsigned char * |
191 | | _jpx_find_box (const unsigned char *p, const unsigned char *end, uint32_t type) |
192 | 0 | { |
193 | 0 | while (p < end) { |
194 | 0 | if (_jpx_match_box (p, end, type)) |
195 | 0 | return p; |
196 | 0 | p = _jpx_next_box (p); |
197 | 0 | } |
198 | | |
199 | 0 | return NULL; |
200 | 0 | } |
201 | | |
202 | | static void |
203 | | _jpx_extract_info (const unsigned char *p, cairo_image_info_t *info) |
204 | 0 | { |
205 | 0 | info->height = get_unaligned_be32 (p); |
206 | 0 | info->width = get_unaligned_be32 (p + 4); |
207 | 0 | info->num_components = (p[8] << 8) + p[9]; |
208 | 0 | info->bits_per_component = p[10]; |
209 | 0 | } |
210 | | |
211 | | cairo_int_status_t |
212 | | _cairo_image_info_get_jpx_info (cairo_image_info_t *info, |
213 | | const unsigned char *data, |
214 | | unsigned long length) |
215 | 0 | { |
216 | 0 | const unsigned char *p = data; |
217 | 0 | const unsigned char *end = data + length; |
218 | | |
219 | | /* First 12 bytes must be the JPEG 2000 signature box. */ |
220 | 0 | if (length < ARRAY_LENGTH(_jpx_signature) || |
221 | 0 | memcmp(p, _jpx_signature, ARRAY_LENGTH(_jpx_signature)) != 0) |
222 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
223 | | |
224 | 0 | p += ARRAY_LENGTH(_jpx_signature); |
225 | | |
226 | | /* Next box must be a File Type Box */ |
227 | 0 | if (! _jpx_match_box (p, end, JPX_FILETYPE)) |
228 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
229 | | |
230 | 0 | p = _jpx_next_box (p); |
231 | | |
232 | | /* Locate the JP2 header box. */ |
233 | 0 | p = _jpx_find_box (p, end, JPX_JP2_HEADER); |
234 | 0 | if (!p) |
235 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
236 | | |
237 | | /* Step into the JP2 header box. First box must be the Image |
238 | | * Header */ |
239 | 0 | p = _jpx_get_box_contents (p); |
240 | 0 | if (! _jpx_match_box (p, end, JPX_IMAGE_HEADER)) |
241 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
242 | | |
243 | | /* Get the image info */ |
244 | 0 | p = _jpx_get_box_contents (p); |
245 | 0 | _jpx_extract_info (p, info); |
246 | |
|
247 | 0 | return CAIRO_STATUS_SUCCESS; |
248 | 0 | } |
249 | | |
250 | | /* PNG (image/png) |
251 | | * |
252 | | * http://www.w3.org/TR/2003/REC-PNG-20031110/ |
253 | | */ |
254 | | |
255 | 0 | #define PNG_IHDR 0x49484452 |
256 | | |
257 | | static const unsigned char _png_magic[8] = { 137, 80, 78, 71, 13, 10, 26, 10 }; |
258 | | |
259 | | cairo_int_status_t |
260 | | _cairo_image_info_get_png_info (cairo_image_info_t *info, |
261 | | const unsigned char *data, |
262 | | unsigned long length) |
263 | 0 | { |
264 | 0 | const unsigned char *p = data; |
265 | 0 | const unsigned char *end = data + length; |
266 | |
|
267 | 0 | if (length < 8 || memcmp (data, _png_magic, 8) != 0) |
268 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
269 | | |
270 | 0 | p += 8; |
271 | | |
272 | | /* The first chunk must be IDHR. IDHR has 13 bytes of data plus |
273 | | * the 12 bytes of overhead for the chunk. */ |
274 | 0 | if (p + 13 + 12 > end) |
275 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
276 | | |
277 | 0 | p += 4; |
278 | 0 | if (get_unaligned_be32 (p) != PNG_IHDR) |
279 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
280 | | |
281 | 0 | p += 4; |
282 | 0 | info->width = get_unaligned_be32 (p); |
283 | 0 | p += 4; |
284 | 0 | info->height = get_unaligned_be32 (p); |
285 | |
|
286 | 0 | return CAIRO_STATUS_SUCCESS; |
287 | 0 | } |
288 | | |
289 | | static const unsigned char * |
290 | | _jbig2_find_data_end (const unsigned char *p, |
291 | | const unsigned char *end, |
292 | | int type) |
293 | 0 | { |
294 | 0 | unsigned char end_seq[2]; |
295 | 0 | int mmr; |
296 | | |
297 | | /* Segments of type "Immediate generic region" may have an |
298 | | * unspecified data length. The JBIG2 specification specifies the |
299 | | * method to find the end of the data for these segments. */ |
300 | 0 | if (type == 36 || type == 38 || type == 39) { |
301 | 0 | if (p + 18 < end) { |
302 | 0 | mmr = p[17] & 0x01; |
303 | 0 | if (mmr) { |
304 | | /* MMR encoding ends with 0x00, 0x00 */ |
305 | 0 | end_seq[0] = 0x00; |
306 | 0 | end_seq[1] = 0x00; |
307 | 0 | } else { |
308 | | /* Template encoding ends with 0xff, 0xac */ |
309 | 0 | end_seq[0] = 0xff; |
310 | 0 | end_seq[1] = 0xac; |
311 | 0 | } |
312 | 0 | p += 18; |
313 | 0 | while (p < end) { |
314 | 0 | if (p[0] == end_seq[0] && p[1] == end_seq[1]) { |
315 | | /* Skip the 2 terminating bytes and the 4 byte row count that follows. */ |
316 | 0 | p += 6; |
317 | 0 | if (p < end) |
318 | 0 | return p; |
319 | 0 | } |
320 | 0 | p++; |
321 | 0 | } |
322 | 0 | } |
323 | 0 | } |
324 | | |
325 | 0 | return NULL; |
326 | 0 | } |
327 | | |
328 | | static const unsigned char * |
329 | | _jbig2_get_next_segment (const unsigned char *p, |
330 | | const unsigned char *end, |
331 | | int *type, |
332 | | const unsigned char **data, |
333 | | unsigned long *data_len) |
334 | 0 | { |
335 | 0 | unsigned long seg_num; |
336 | 0 | cairo_bool_t big_page_size; |
337 | 0 | int num_segs; |
338 | 0 | int ref_seg_bytes; |
339 | 0 | int referred_size; |
340 | |
|
341 | 0 | if (p + 6 >= end) |
342 | 0 | return NULL; |
343 | | |
344 | 0 | seg_num = get_unaligned_be32 (p); |
345 | 0 | *type = p[4] & 0x3f; |
346 | 0 | big_page_size = (p[4] & 0x40) != 0; |
347 | 0 | p += 5; |
348 | |
|
349 | 0 | num_segs = p[0] >> 5; |
350 | 0 | if (num_segs == 7) { |
351 | 0 | num_segs = get_unaligned_be32 (p) & 0x1fffffff; |
352 | 0 | ref_seg_bytes = 4 + ((num_segs + 1)/8); |
353 | 0 | } else { |
354 | 0 | ref_seg_bytes = 1; |
355 | 0 | } |
356 | 0 | p += ref_seg_bytes; |
357 | |
|
358 | 0 | if (seg_num <= 256) |
359 | 0 | referred_size = 1; |
360 | 0 | else if (seg_num <= 65536) |
361 | 0 | referred_size = 2; |
362 | 0 | else |
363 | 0 | referred_size = 4; |
364 | |
|
365 | 0 | p += num_segs * referred_size; |
366 | 0 | p += big_page_size ? 4 : 1; |
367 | 0 | if (p + 4 >= end) |
368 | 0 | return NULL; |
369 | | |
370 | 0 | *data_len = get_unaligned_be32 (p); |
371 | 0 | p += 4; |
372 | 0 | *data = p; |
373 | |
|
374 | 0 | if (*data_len == 0xffffffff) { |
375 | | /* if data length is -1 we have to scan through the data to find the end */ |
376 | 0 | p = _jbig2_find_data_end (*data, end, *type); |
377 | 0 | if (!p || p >= end) |
378 | 0 | return NULL; |
379 | | |
380 | 0 | *data_len = p - *data; |
381 | 0 | } else { |
382 | 0 | p += *data_len; |
383 | 0 | } |
384 | | |
385 | 0 | if (p < end) |
386 | 0 | return p; |
387 | 0 | else |
388 | 0 | return NULL; |
389 | 0 | } |
390 | | |
391 | | static void |
392 | | _jbig2_extract_info (cairo_image_info_t *info, const unsigned char *p) |
393 | 0 | { |
394 | 0 | info->width = get_unaligned_be32 (p); |
395 | 0 | info->height = get_unaligned_be32 (p + 4); |
396 | 0 | info->num_components = 1; |
397 | 0 | info->bits_per_component = 1; |
398 | 0 | } |
399 | | |
400 | | cairo_int_status_t |
401 | | _cairo_image_info_get_jbig2_info (cairo_image_info_t *info, |
402 | | const unsigned char *data, |
403 | | unsigned long length) |
404 | 0 | { |
405 | 0 | const unsigned char *p = data; |
406 | 0 | const unsigned char *end = data + length; |
407 | 0 | int seg_type; |
408 | 0 | const unsigned char *seg_data; |
409 | 0 | unsigned long seg_data_len; |
410 | |
|
411 | 0 | while (p && p < end) { |
412 | 0 | p = _jbig2_get_next_segment (p, end, &seg_type, &seg_data, &seg_data_len); |
413 | 0 | if (p && seg_type == 48 && seg_data_len > 8) { |
414 | | /* page information segment */ |
415 | 0 | _jbig2_extract_info (info, seg_data); |
416 | 0 | return CAIRO_STATUS_SUCCESS; |
417 | 0 | } |
418 | 0 | } |
419 | | |
420 | 0 | return CAIRO_INT_STATUS_UNSUPPORTED; |
421 | 0 | } |