1 /* Copyright © 2013 Canonical Limited
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General
14 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
16 * Author: Ryan Lortie <desrt@desrt.ca>
21 #include "thumbnail-verify.h"
25 /* Begin code to check the validity of thumbnail files. In order to do
26 * that we need to parse enough PNG in order to get the Thumb::URI,
27 * Thumb::MTime and Thumb::Size tags out of the file. Fortunately this
37 /* We *require* matches on URI and MTime, but the Size field is optional
40 * http://specifications.freedesktop.org/thumbnail-spec/thumbnail-spec-latest.html
42 #define MATCHED_URI (1u << 0)
43 #define MATCHED_MTIME (1u << 1)
44 #define MATCHED_ALL (MATCHED_URI | MATCHED_MTIME)
47 check_integer_match (guint64 expected
,
51 /* Would be nice to g_ascii_strtoll here, but we don't have a variant
52 * that works on strings that are not nul-terminated.
54 * It's easy enough to do it ourselves...
56 if (expected
== 0) /* special case: "0" */
57 return value_size
== 1 && value
[0] == '0';
59 /* Check each digit, as long as we have data from both */
60 while (expected
&& value_size
)
62 /* Check the low-order digit */
63 if (value
[value_size
- 1] != (gchar
) ((expected
% 10) + '0'))
71 /* Make sure nothing is left over, on either side */
72 return !expected
&& !value_size
;
76 check_png_info_chunk (ExpectedInfo
*expected_info
,
81 guint
*required_matches
)
83 if (key_size
== 10 && memcmp (key
, "Thumb::URI", 10) == 0)
87 expected_size
= strlen (expected_info
->uri
);
89 if (expected_size
!= value_size
)
92 if (memcmp (expected_info
->uri
, value
, value_size
) != 0)
95 *required_matches
|= MATCHED_URI
;
98 else if (key_size
== 12 && memcmp (key
, "Thumb::MTime", 12) == 0)
100 if (!check_integer_match (expected_info
->mtime
, value
, value_size
))
103 *required_matches
|= MATCHED_MTIME
;
106 else if (key_size
== 11 && memcmp (key
, "Thumb::Size", 11) == 0)
108 /* A match on Thumb::Size is not required for success, but if we
109 * find this optional field and it's wrong, we should reject the
112 if (!check_integer_match (expected_info
->size
, value
, value_size
))
120 check_thumbnail_validity (ExpectedInfo
*expected_info
,
121 const gchar
*contents
,
124 guint required_matches
= 0;
126 /* Reference: http://www.w3.org/TR/PNG/ */
130 if (memcmp (contents
, "\x89PNG\r\n\x1a\n", 8) != 0)
133 contents
+= 8, size
-= 8;
135 /* We need at least 12 bytes to have a chunk... */
138 guint32 chunk_size_be
;
141 /* PNG is not an aligned file format so we have to be careful
142 * about reading integers...
144 memcpy (&chunk_size_be
, contents
, 4);
145 chunk_size
= GUINT32_FROM_BE (chunk_size_be
);
147 contents
+= 4, size
-= 4;
149 /* After consuming the size field, we need to have enough bytes
150 * for 4 bytes type field, chunk_size bytes for data, then 4 byte
151 * for CRC (which we ignore)
153 * We just read chunk_size from the file, so it may be very large.
154 * Make sure it won't wrap when we add 8 to it.
156 if (G_MAXUINT32
- chunk_size
< 8 || size
< chunk_size
+ 8)
159 /* We are only interested in tEXt fields */
160 if (memcmp (contents
, "tEXt", 4) == 0)
162 const gchar
*key
= contents
+ 4;
165 /* We need to find the nul separator character that splits the
166 * key/value. The value is not terminated.
168 * If we find no nul then we just ignore the field.
170 * value may contain extra nuls, but check_png_info_chunk()
173 for (key_size
= 0; key_size
< chunk_size
; key_size
++)
175 if (key
[key_size
] == '\0')
180 /* Since key_size < chunk_size, value_size is
181 * definitely non-negative.
183 value_size
= chunk_size
- key_size
- 1;
184 value
= key
+ key_size
+ 1;
186 /* We found the separator character. */
187 if (!check_png_info_chunk (expected_info
,
197 /* A bit of a hack: assume that all tEXt chunks will appear
198 * together. Therefore, if we have already seen both required
199 * fields and then see a non-tEXt chunk then we can assume we
202 * The common case is that the tEXt chunks come at the start
203 * of the file before any of the image data. This trick means
204 * that we will only fault in a single page (4k) whereas many
205 * thumbnails (particularly the large ones) can approach 100k
208 if (required_matches
== MATCHED_ALL
)
212 /* skip to the next chunk, ignoring CRC. */
213 contents
+= 4, size
-= 4; /* type field */
214 contents
+= chunk_size
, size
-= chunk_size
; /* data */
215 contents
+= 4, size
-= 4; /* CRC */
219 return required_matches
== MATCHED_ALL
;
223 thumbnail_verify (const char *thumbnail_path
,
224 const gchar
*file_uri
,
225 const GLocalFileStat
*file_stat_buf
)
227 gboolean thumbnail_is_valid
= FALSE
;
228 ExpectedInfo expected_info
;
231 if (file_stat_buf
== NULL
)
234 expected_info
.uri
= file_uri
;
235 expected_info
.mtime
= file_stat_buf
->st_mtime
;
236 expected_info
.size
= file_stat_buf
->st_size
;
238 file
= g_mapped_file_new (thumbnail_path
, FALSE
, NULL
);
241 thumbnail_is_valid
= check_thumbnail_validity (&expected_info
,
242 g_mapped_file_get_contents (file
),
243 g_mapped_file_get_length (file
));
244 g_mapped_file_unref (file
);
247 return thumbnail_is_valid
;