1 /* gtkiconcachevalidator.c
2 * Copyright (C) 2007 Red Hat, Inc
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 #include "gtkiconcachevalidator.h"
21 #include <gdk-pixbuf/gdk-pixdata.h>
26 #define check(name,condition) \
29 VERBOSE(g_print ("bad %s\n", (name))); \
33 static inline gboolean
34 get_uint16 (CacheInfo *info,
38 if (offset < info->cache_size)
40 *value = GUINT16_FROM_BE(*(guint16*)(info->cache + offset));
50 static inline gboolean
51 get_uint32 (CacheInfo *info,
55 if (offset < info->cache_size)
57 *value = GUINT32_FROM_BE(*(guint32*)(info->cache + offset));
68 check_version (CacheInfo *info)
72 check ("major version", get_uint16 (info, 0, &major) && major == 1);
73 check ("minor version", get_uint16 (info, 2, &minor) && minor == 0);
79 check_string (CacheInfo *info,
82 check ("string offset", offset < info->cache_size);
84 if (info->flags & CHECK_STRINGS)
89 /* assume no string is longer than 1k */
90 for (i = 0; i < 1024; i++)
92 check ("string offset", offset + i < info->cache_size)
93 c = *(info->cache + offset + i);
96 check ("string content", g_ascii_isgraph (c));
98 check ("string length", i < 1024);
105 check_string_utf8 (CacheInfo *info,
108 check ("string offset", offset < info->cache_size);
110 if (info->flags & CHECK_STRINGS)
115 /* assume no string is longer than 1k */
116 for (i = 0; i < 1024; i++)
118 check ("string offset", offset + i < info->cache_size)
119 c = *(info->cache + offset + i);
123 check ("string length", i < 1024);
124 check ("string utf8 data", g_utf8_validate((char *)(info->cache + offset), -1, NULL));
131 check_directory_list (CacheInfo *info,
134 guint32 directory_offset;
137 check ("offset, directory list", get_uint32 (info, offset, &info->n_directories));
139 for (i = 0; i < info->n_directories; i++)
141 check ("offset, directory", get_uint32 (info, offset + 4 + 4 * i, &directory_offset));
142 if (!check_string (info, directory_offset))
150 check_pixel_data (CacheInfo *info,
156 check ("offset, pixel data type", get_uint32 (info, offset, &type));
157 check ("offset, pixel data length", get_uint32 (info, offset + 4, &length));
159 check ("pixel data type", type == 0);
160 check ("pixel data length", offset + 8 + length < info->cache_size);
162 if (info->flags & CHECK_PIXBUFS)
166 check ("pixel data", gdk_pixdata_deserialize (&data, length,
167 (const guint8*)info->cache + offset + 8,
175 check_embedded_rect (CacheInfo *info,
178 check ("embedded rect", offset + 4 < info->cache_size);
184 check_attach_point_list (CacheInfo *info,
187 guint32 n_attach_points;
189 check ("offset, attach point list", get_uint32 (info, offset, &n_attach_points));
190 check ("attach points", offset + 4 + 4 * n_attach_points < info->cache_size);
196 check_display_name_list (CacheInfo *info,
199 guint32 n_display_names, ofs;
202 check ("offset, display name list",
203 get_uint32 (info, offset, &n_display_names));
204 for (i = 0; i < n_display_names; i++)
206 get_uint32(info, offset + 4 + 8 * i, &ofs);
207 if (!check_string (info, ofs))
209 get_uint32(info, offset + 4 + 8 * i + 4, &ofs);
210 if (!check_string_utf8 (info, ofs))
218 check_meta_data (CacheInfo *info,
221 guint32 embedded_rect_offset;
222 guint32 attach_point_list_offset;
223 guint32 display_name_list_offset;
225 check ("offset, embedded rect",
226 get_uint32 (info, offset, &embedded_rect_offset));
227 check ("offset, attach point list",
228 get_uint32 (info, offset + 4, &attach_point_list_offset));
229 check ("offset, display name list",
230 get_uint32 (info, offset + 8, &display_name_list_offset));
232 if (embedded_rect_offset != 0)
234 if (!check_embedded_rect (info, embedded_rect_offset))
238 if (attach_point_list_offset != 0)
240 if (!check_attach_point_list (info, attach_point_list_offset))
244 if (display_name_list_offset != 0)
246 if (!check_display_name_list (info, display_name_list_offset))
254 check_image_data (CacheInfo *info,
257 guint32 pixel_data_offset;
258 guint32 meta_data_offset;
260 check ("offset, pixel data", get_uint32 (info, offset, &pixel_data_offset));
261 check ("offset, meta data", get_uint32 (info, offset + 4, &meta_data_offset));
263 if (pixel_data_offset != 0)
265 if (!check_pixel_data (info, pixel_data_offset))
268 if (meta_data_offset != 0)
270 if (!check_meta_data (info, meta_data_offset))
278 check_image (CacheInfo *info,
283 guint32 image_data_offset;
285 check ("offset, image index", get_uint16 (info, offset, &index));
286 check ("offset, image flags", get_uint16 (info, offset + 2, &flags));
287 check ("offset, image data offset",
288 get_uint32 (info, offset + 4, &image_data_offset));
290 check ("image index", index < info->n_directories);
291 check ("image flags", flags < 16);
293 if (image_data_offset != 0)
295 if (!check_image_data (info, image_data_offset))
303 check_image_list (CacheInfo *info,
309 check ("offset, image list", get_uint32 (info, offset, &n_images));
311 for (i = 0; i < n_images; i++)
313 if (!check_image (info, offset + 4 + 8 * i))
321 check_icon (CacheInfo *info,
324 guint32 chain_offset;
326 guint32 image_list_offset;
328 check ("offset, icon chain", get_uint32 (info, offset, &chain_offset));
329 check ("offset, icon name", get_uint32 (info, offset + 4, &name_offset));
330 check ("offset, icon image list", get_uint32 (info, offset + 8,
331 &image_list_offset));
333 if (!check_string (info, name_offset))
335 if (!check_image_list (info, image_list_offset))
337 if (chain_offset != 0xffffffff)
339 if (!check_icon (info, chain_offset))
347 check_hash (CacheInfo *info,
350 guint32 n_buckets, icon_offset;
353 check ("offset, hash size", get_uint32 (info, offset, &n_buckets));
355 for (i = 0; i < n_buckets; i++)
357 check ("offset, hash chain",
358 get_uint32 (info, offset + 4 + 4 * i, &icon_offset));
359 if (icon_offset != 0xffffffff)
361 if (!check_icon (info, icon_offset))
370 * _gtk_icon_cache_validate:
371 * @info: a CacheInfo structure
373 * Validates the icon cache passed in the @cache and
374 * @cache_size fields of the @info structure. The
375 * validator checks that offsets specified in the
376 * cache do not point outside the mapped area, that
377 * strings look reasonable, and that pixbufs can
378 * be deserialized. The amount of validation can
379 * be controlled with the @flags field.
381 * Return value: %TRUE if the cache is valid
384 _gtk_icon_cache_validate (CacheInfo *info)
387 guint32 directory_list_offset;
389 if (!check_version (info))
391 check ("header, hash offset", get_uint32 (info, 4, &hash_offset));
392 check ("header, directory list offset", get_uint32 (info, 8, &directory_list_offset));
393 if (!check_directory_list (info, directory_list_offset))
396 if (!check_hash (info, hash_offset))