/src/glib/gio/thumbnail-verify.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright © 2013 Canonical Limited |
2 | | * |
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. |
7 | | * |
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. |
12 | | * |
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/>. |
15 | | * |
16 | | * Author: Ryan Lortie <desrt@desrt.ca> |
17 | | */ |
18 | | |
19 | | #include "config.h" |
20 | | |
21 | | #include "thumbnail-verify.h" |
22 | | |
23 | | #include <string.h> |
24 | | |
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 |
28 | | * is relatively easy. |
29 | | */ |
30 | | typedef struct |
31 | | { |
32 | | const gchar *uri; |
33 | | guint64 mtime; |
34 | | guint64 size; |
35 | | } ExpectedInfo; |
36 | | |
37 | | /* We *require* matches on URI and MTime, but the Size field is optional |
38 | | * (as per the spec). |
39 | | * |
40 | | * http://specifications.freedesktop.org/thumbnail-spec/thumbnail-spec-latest.html |
41 | | */ |
42 | 0 | #define MATCHED_URI (1u << 0) |
43 | 0 | #define MATCHED_MTIME (1u << 1) |
44 | 0 | #define MATCHED_ALL (MATCHED_URI | MATCHED_MTIME) |
45 | | |
46 | | static gboolean |
47 | | check_integer_match (guint64 expected, |
48 | | const gchar *value, |
49 | | guint32 value_size) |
50 | 0 | { |
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. |
53 | | * |
54 | | * It's easy enough to do it ourselves... |
55 | | */ |
56 | 0 | if (expected == 0) /* special case: "0" */ |
57 | 0 | return value_size == 1 && value[0] == '0'; |
58 | | |
59 | | /* Check each digit, as long as we have data from both */ |
60 | 0 | while (expected && value_size) |
61 | 0 | { |
62 | | /* Check the low-order digit */ |
63 | 0 | if (value[value_size - 1] != (gchar) ((expected % 10) + '0')) |
64 | 0 | return FALSE; |
65 | | |
66 | | /* Move on... */ |
67 | 0 | expected /= 10; |
68 | 0 | value_size--; |
69 | 0 | } |
70 | | |
71 | | /* Make sure nothing is left over, on either side */ |
72 | 0 | return !expected && !value_size; |
73 | 0 | } |
74 | | |
75 | | static gboolean |
76 | | check_png_info_chunk (ExpectedInfo *expected_info, |
77 | | const gchar *key, |
78 | | guint32 key_size, |
79 | | const gchar *value, |
80 | | guint32 value_size, |
81 | | guint *required_matches) |
82 | 0 | { |
83 | 0 | if (key_size == 10 && memcmp (key, "Thumb::URI", 10) == 0) |
84 | 0 | { |
85 | 0 | gsize expected_size; |
86 | |
|
87 | 0 | expected_size = strlen (expected_info->uri); |
88 | |
|
89 | 0 | if (expected_size != value_size) |
90 | 0 | return FALSE; |
91 | | |
92 | 0 | if (memcmp (expected_info->uri, value, value_size) != 0) |
93 | 0 | return FALSE; |
94 | | |
95 | 0 | *required_matches |= MATCHED_URI; |
96 | 0 | } |
97 | | |
98 | 0 | else if (key_size == 12 && memcmp (key, "Thumb::MTime", 12) == 0) |
99 | 0 | { |
100 | 0 | if (!check_integer_match (expected_info->mtime, value, value_size)) |
101 | 0 | return FALSE; |
102 | | |
103 | 0 | *required_matches |= MATCHED_MTIME; |
104 | 0 | } |
105 | | |
106 | 0 | else if (key_size == 11 && memcmp (key, "Thumb::Size", 11) == 0) |
107 | 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 |
110 | | * thumbnail. |
111 | | */ |
112 | 0 | if (!check_integer_match (expected_info->size, value, value_size)) |
113 | 0 | return FALSE; |
114 | 0 | } |
115 | | |
116 | 0 | return TRUE; |
117 | 0 | } |
118 | | |
119 | | static gboolean |
120 | | check_thumbnail_validity (ExpectedInfo *expected_info, |
121 | | const gchar *contents, |
122 | | gsize size) |
123 | 0 | { |
124 | 0 | guint required_matches = 0; |
125 | | |
126 | | /* Reference: http://www.w3.org/TR/PNG/ */ |
127 | 0 | if (size < 8) |
128 | 0 | return FALSE; |
129 | | |
130 | 0 | if (memcmp (contents, "\x89PNG\r\n\x1a\n", 8) != 0) |
131 | 0 | return FALSE; |
132 | | |
133 | 0 | contents += 8, size -= 8; |
134 | | |
135 | | /* We need at least 12 bytes to have a chunk... */ |
136 | 0 | while (size >= 12) |
137 | 0 | { |
138 | 0 | guint32 chunk_size_be; |
139 | 0 | guint32 chunk_size; |
140 | | |
141 | | /* PNG is not an aligned file format so we have to be careful |
142 | | * about reading integers... |
143 | | */ |
144 | 0 | memcpy (&chunk_size_be, contents, 4); |
145 | 0 | chunk_size = GUINT32_FROM_BE (chunk_size_be); |
146 | |
|
147 | 0 | contents += 4, size -= 4; |
148 | | |
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) |
152 | | * |
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. |
155 | | */ |
156 | 0 | if (G_MAXUINT32 - chunk_size < 8 || size < chunk_size + 8) |
157 | 0 | goto out; |
158 | | |
159 | | /* We are only interested in tEXt fields */ |
160 | 0 | if (memcmp (contents, "tEXt", 4) == 0) |
161 | 0 | { |
162 | 0 | const gchar *key = contents + 4; |
163 | 0 | guint32 key_size; |
164 | | |
165 | | /* We need to find the nul separator character that splits the |
166 | | * key/value. The value is not terminated. |
167 | | * |
168 | | * If we find no nul then we just ignore the field. |
169 | | * |
170 | | * value may contain extra nuls, but check_png_info_chunk() |
171 | | * can handle that. |
172 | | */ |
173 | 0 | for (key_size = 0; key_size < chunk_size; key_size++) |
174 | 0 | { |
175 | 0 | if (key[key_size] == '\0') |
176 | 0 | { |
177 | 0 | const gchar *value; |
178 | 0 | guint32 value_size; |
179 | | |
180 | | /* Since key_size < chunk_size, value_size is |
181 | | * definitely non-negative. |
182 | | */ |
183 | 0 | value_size = chunk_size - key_size - 1; |
184 | 0 | value = key + key_size + 1; |
185 | | |
186 | | /* We found the separator character. */ |
187 | 0 | if (!check_png_info_chunk (expected_info, |
188 | 0 | key, key_size, |
189 | 0 | value, value_size, |
190 | 0 | &required_matches)) |
191 | 0 | return FALSE; |
192 | 0 | } |
193 | 0 | } |
194 | 0 | } |
195 | 0 | else |
196 | 0 | { |
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 |
200 | | * are done. |
201 | | * |
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 |
206 | | * in size. |
207 | | */ |
208 | 0 | if (required_matches == MATCHED_ALL) |
209 | 0 | goto out; |
210 | 0 | } |
211 | | |
212 | | /* skip to the next chunk, ignoring CRC. */ |
213 | 0 | contents += 4, size -= 4; /* type field */ |
214 | 0 | contents += chunk_size, size -= chunk_size; /* data */ |
215 | 0 | contents += 4, size -= 4; /* CRC */ |
216 | 0 | } |
217 | | |
218 | 0 | out: |
219 | 0 | return required_matches == MATCHED_ALL; |
220 | 0 | } |
221 | | |
222 | | gboolean |
223 | | thumbnail_verify (const char *thumbnail_path, |
224 | | const gchar *file_uri, |
225 | | const GLocalFileStat *file_stat_buf) |
226 | 0 | { |
227 | 0 | gboolean thumbnail_is_valid = FALSE; |
228 | 0 | ExpectedInfo expected_info; |
229 | 0 | GMappedFile *file; |
230 | |
|
231 | 0 | if (file_stat_buf == NULL) |
232 | 0 | return FALSE; |
233 | | |
234 | 0 | expected_info.uri = file_uri; |
235 | | #ifdef G_OS_WIN32 |
236 | | expected_info.mtime = (guint64) file_stat_buf->st_mtim.tv_sec; |
237 | | #else |
238 | 0 | expected_info.mtime = _g_stat_mtime (file_stat_buf); |
239 | 0 | #endif |
240 | 0 | expected_info.size = _g_stat_size (file_stat_buf); |
241 | |
|
242 | 0 | file = g_mapped_file_new (thumbnail_path, FALSE, NULL); |
243 | 0 | if (file) |
244 | 0 | { |
245 | 0 | thumbnail_is_valid = check_thumbnail_validity (&expected_info, |
246 | 0 | g_mapped_file_get_contents (file), |
247 | 0 | g_mapped_file_get_length (file)); |
248 | 0 | g_mapped_file_unref (file); |
249 | 0 | } |
250 | |
|
251 | 0 | return thumbnail_is_valid; |
252 | 0 | } |