gtestutils: Fix a typo in a documentation comment
[glib.git] / gio / thumbnail-verify.c
blobbf5c60f7788c56f27697fdf5800e0a27266e06f0
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 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>
19 #include "config.h"
21 #include "thumbnail-verify.h"
23 #include <string.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
28 * is relatively easy.
30 typedef struct
32 const gchar *uri;
33 guint64 mtime;
34 guint64 size;
35 } ExpectedInfo;
37 /* We *require* matches on URI and MTime, but the Size field is optional
38 * (as per the spec).
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)
46 static gboolean
47 check_integer_match (guint64 expected,
48 const gchar *value,
49 guint32 value_size)
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'))
64 return FALSE;
66 /* Move on... */
67 expected /= 10;
68 value_size--;
71 /* Make sure nothing is left over, on either side */
72 return !expected && !value_size;
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)
83 if (key_size == 10 && memcmp (key, "Thumb::URI", 10) == 0)
85 gsize expected_size;
87 expected_size = strlen (expected_info->uri);
89 if (expected_size != value_size)
90 return FALSE;
92 if (memcmp (expected_info->uri, value, value_size) != 0)
93 return FALSE;
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))
101 return FALSE;
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
110 * thumbnail.
112 if (!check_integer_match (expected_info->size, value, value_size))
113 return FALSE;
116 return TRUE;
119 static gboolean
120 check_thumbnail_validity (ExpectedInfo *expected_info,
121 const gchar *contents,
122 gsize size)
124 guint required_matches = 0;
126 /* Reference: http://www.w3.org/TR/PNG/ */
127 if (size < 8)
128 return FALSE;
130 if (memcmp (contents, "\x89PNG\r\n\x1a\n", 8) != 0)
131 return FALSE;
133 contents += 8, size -= 8;
135 /* We need at least 12 bytes to have a chunk... */
136 while (size >= 12)
138 guint32 chunk_size_be;
139 guint32 chunk_size;
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)
157 goto out;
159 /* We are only interested in tEXt fields */
160 if (memcmp (contents, "tEXt", 4) == 0)
162 const gchar *key = contents + 4;
163 guint32 key_size;
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()
171 * can handle that.
173 for (key_size = 0; key_size < chunk_size; key_size++)
175 if (key[key_size] == '\0')
177 const gchar *value;
178 guint32 value_size;
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,
188 key, key_size,
189 value, value_size,
190 &required_matches))
191 return FALSE;
195 else
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.
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.
208 if (required_matches == MATCHED_ALL)
209 goto out;
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 */
218 out:
219 return required_matches == MATCHED_ALL;
222 gboolean
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;
229 GMappedFile *file;
231 if (file_stat_buf == NULL)
232 return FALSE;
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);
239 if (file)
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;