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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "gtkiconcachevalidator.h"
23 #include <gdk-pixbuf/gdk-pixdata.h>
28 #define check(name,condition) \
31 VERBOSE(g_print ("bad %s\n", (name))); \
35 static inline gboolean
36 get_uint16 (CacheInfo *info,
40 if (offset < info->cache_size)
42 *value = GUINT16_FROM_BE(*(guint16*)(info->cache + offset));
52 static inline gboolean
53 get_uint32 (CacheInfo *info,
57 if (offset < info->cache_size)
59 *value = GUINT32_FROM_BE(*(guint32*)(info->cache + offset));
70 check_version (CacheInfo *info)
74 check ("major version", get_uint16 (info, 0, &major) && major == 1);
75 check ("minor version", get_uint16 (info, 2, &minor) && minor == 0);
81 check_string (CacheInfo *info,
84 check ("string offset", offset < info->cache_size);
86 if (info->flags & CHECK_STRINGS)
91 /* assume no string is longer than 1k */
92 for (i = 0; i < 1024; i++)
94 check ("string offset", offset + i < info->cache_size)
95 c = *(info->cache + offset + i);
98 check ("string content", g_ascii_isgraph (c));
100 check ("string length", i < 1024);
107 check_string_utf8 (CacheInfo *info,
110 check ("string offset", offset < info->cache_size);
112 if (info->flags & CHECK_STRINGS)
117 /* assume no string is longer than 1k */
118 for (i = 0; i < 1024; i++)
120 check ("string offset", offset + i < info->cache_size)
121 c = *(info->cache + offset + i);
125 check ("string length", i < 1024);
126 check ("string utf8 data", g_utf8_validate((char *)(info->cache + offset), -1, NULL));
133 check_directory_list (CacheInfo *info,
136 guint32 directory_offset;
139 check ("offset, directory list", get_uint32 (info, offset, &info->n_directories));
141 for (i = 0; i < info->n_directories; i++)
143 check ("offset, directory", get_uint32 (info, offset + 4 + 4 * i, &directory_offset));
144 if (!check_string (info, directory_offset))
152 check_pixel_data (CacheInfo *info,
158 check ("offset, pixel data type", get_uint32 (info, offset, &type));
159 check ("offset, pixel data length", get_uint32 (info, offset + 4, &length));
161 check ("pixel data type", type == 0);
162 check ("pixel data length", offset + 8 + length < info->cache_size);
164 if (info->flags & CHECK_PIXBUFS)
168 check ("pixel data", gdk_pixdata_deserialize (&data, length,
169 (const guint8*)info->cache + offset + 8,
177 check_embedded_rect (CacheInfo *info,
180 check ("embedded rect", offset + 4 < info->cache_size);
186 check_attach_point_list (CacheInfo *info,
189 guint32 n_attach_points;
191 check ("offset, attach point list", get_uint32 (info, offset, &n_attach_points));
192 check ("attach points", offset + 4 + 4 * n_attach_points < info->cache_size);
198 check_display_name_list (CacheInfo *info,
201 guint32 n_display_names, ofs;
204 check ("offset, display name list",
205 get_uint32 (info, offset, &n_display_names));
206 for (i = 0; i < n_display_names; i++)
208 get_uint32(info, offset + 4 + 8 * i, &ofs);
209 if (!check_string (info, ofs))
211 get_uint32(info, offset + 4 + 8 * i + 4, &ofs);
212 if (!check_string_utf8 (info, ofs))
220 check_meta_data (CacheInfo *info,
223 guint32 embedded_rect_offset;
224 guint32 attach_point_list_offset;
225 guint32 display_name_list_offset;
227 check ("offset, embedded rect",
228 get_uint32 (info, offset, &embedded_rect_offset));
229 check ("offset, attach point list",
230 get_uint32 (info, offset + 4, &attach_point_list_offset));
231 check ("offset, display name list",
232 get_uint32 (info, offset + 8, &display_name_list_offset));
234 if (embedded_rect_offset != 0)
236 if (!check_embedded_rect (info, embedded_rect_offset))
240 if (attach_point_list_offset != 0)
242 if (!check_attach_point_list (info, attach_point_list_offset))
246 if (display_name_list_offset != 0)
248 if (!check_display_name_list (info, display_name_list_offset))
256 check_image_data (CacheInfo *info,
259 guint32 pixel_data_offset;
260 guint32 meta_data_offset;
262 check ("offset, pixel data", get_uint32 (info, offset, &pixel_data_offset));
263 check ("offset, meta data", get_uint32 (info, offset + 4, &meta_data_offset));
265 if (pixel_data_offset != 0)
267 if (!check_pixel_data (info, pixel_data_offset))
270 if (meta_data_offset != 0)
272 if (!check_meta_data (info, meta_data_offset))
280 check_image (CacheInfo *info,
285 guint32 image_data_offset;
287 check ("offset, image index", get_uint16 (info, offset, &index));
288 check ("offset, image flags", get_uint16 (info, offset + 2, &flags));
289 check ("offset, image data offset",
290 get_uint32 (info, offset + 4, &image_data_offset));
292 check ("image index", index < info->n_directories);
293 check ("image flags", flags < 16);
295 if (image_data_offset != 0)
297 if (!check_image_data (info, image_data_offset))
305 check_image_list (CacheInfo *info,
311 check ("offset, image list", get_uint32 (info, offset, &n_images));
313 for (i = 0; i < n_images; i++)
315 if (!check_image (info, offset + 4 + 8 * i))
323 check_icon (CacheInfo *info,
326 guint32 chain_offset;
328 guint32 image_list_offset;
330 check ("offset, icon chain", get_uint32 (info, offset, &chain_offset));
331 check ("offset, icon name", get_uint32 (info, offset + 4, &name_offset));
332 check ("offset, icon image list", get_uint32 (info, offset + 8,
333 &image_list_offset));
335 if (!check_string (info, name_offset))
337 if (!check_image_list (info, image_list_offset))
339 if (chain_offset != 0xffffffff)
341 if (!check_icon (info, chain_offset))
349 check_hash (CacheInfo *info,
352 guint32 n_buckets, icon_offset;
355 check ("offset, hash size", get_uint32 (info, offset, &n_buckets));
357 for (i = 0; i < n_buckets; i++)
359 check ("offset, hash chain",
360 get_uint32 (info, offset + 4 + 4 * i, &icon_offset));
361 if (icon_offset != 0xffffffff)
363 if (!check_icon (info, icon_offset))
372 * _gtk_icon_cache_validate:
373 * @info: a CacheInfo structure
375 * Validates the icon cache passed in the @cache and
376 * @cache_size fields of the @info structure. The
377 * validator checks that offsets specified in the
378 * cache do not point outside the mapped area, that
379 * strings look reasonable, and that pixbufs can
380 * be deserialized. The amount of validation can
381 * be controlled with the @flags field.
383 * Return value: %TRUE if the cache is valid
386 _gtk_icon_cache_validate (CacheInfo *info)
389 guint32 directory_list_offset;
391 if (!check_version (info))
393 check ("header, hash offset", get_uint32 (info, 4, &hash_offset));
394 check ("header, directory list offset", get_uint32 (info, 8, &directory_list_offset));
395 if (!check_directory_list (info, directory_list_offset))
398 if (!check_hash (info, hash_offset))