]> Pileus Git - ~andy/gtk/blob - gtk/gtkiconcachevalidator.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkiconcachevalidator.c
1 /* gtkiconcachevalidator.c
2  * Copyright (C) 2007 Red Hat, Inc
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 #include "config.h"
18 #include "gtkiconcachevalidator.h"
19
20 #include <glib.h>
21 #include <gdk-pixbuf/gdk-pixdata.h>
22
23
24 #define VERBOSE(x) 
25
26 #define check(name,condition) \
27   if (!(condition)) \
28     { \
29       VERBOSE(g_print ("bad %s\n", (name))); \
30       return FALSE; \
31     } 
32
33 static inline gboolean 
34 get_uint16 (CacheInfo *info, 
35             guint32    offset, 
36             guint16   *value)
37 {
38   if (offset < info->cache_size) 
39     { 
40       *value = GUINT16_FROM_BE(*(guint16*)(info->cache + offset)); 
41       return TRUE;
42     }
43   else 
44     { 
45       *value = 0;
46       return FALSE; 
47     } 
48 }
49
50 static inline gboolean 
51 get_uint32 (CacheInfo *info, 
52             guint32    offset, 
53             guint32   *value)
54 {
55   if (offset < info->cache_size) 
56     { 
57       *value = GUINT32_FROM_BE(*(guint32*)(info->cache + offset)); 
58       return TRUE;
59     }
60   else 
61     { 
62       *value = 0;
63       return FALSE; 
64     } 
65 }
66
67 static gboolean 
68 check_version (CacheInfo *info)
69 {
70   guint16 major, minor;
71
72   check ("major version", get_uint16 (info, 0, &major) && major == 1);
73   check ("minor version", get_uint16 (info, 2, &minor) && minor == 0);
74
75   return TRUE;
76 }
77
78 static gboolean 
79 check_string (CacheInfo *info, 
80               guint32    offset)
81 {
82   check ("string offset", offset < info->cache_size);
83
84   if (info->flags & CHECK_STRINGS) 
85     {
86       gint i;
87       gchar c;
88
89       /* assume no string is longer than 1k */
90       for (i = 0; i < 1024; i++) 
91         { 
92           check ("string offset", offset + i < info->cache_size)
93           c = *(info->cache + offset + i);
94           if (c == '\0')
95             break;
96           check ("string content", g_ascii_isgraph (c));
97         }
98       check ("string length", i < 1024);
99     }
100
101   return TRUE;
102 }
103
104 static gboolean 
105 check_string_utf8 (CacheInfo *info, 
106                    guint32    offset)
107 {
108   check ("string offset", offset < info->cache_size);
109
110   if (info->flags & CHECK_STRINGS) 
111     {
112       gint i;
113       gchar c;
114
115       /* assume no string is longer than 1k */
116       for (i = 0; i < 1024; i++) 
117         { 
118           check ("string offset", offset + i < info->cache_size)
119             c = *(info->cache + offset + i);
120           if (c == '\0')
121             break;
122         }
123       check ("string length", i < 1024);
124       check ("string utf8 data", g_utf8_validate((char *)(info->cache + offset), -1, NULL));
125     }
126
127   return TRUE;
128 }
129
130 static gboolean 
131 check_directory_list (CacheInfo *info, 
132                       guint32    offset)
133 {
134   guint32 directory_offset;
135   gint i;
136         
137   check ("offset, directory list", get_uint32 (info, offset, &info->n_directories));
138         
139   for (i = 0; i < info->n_directories; i++) 
140     {
141       check ("offset, directory", get_uint32 (info, offset + 4 + 4 * i, &directory_offset));
142       if (!check_string (info, directory_offset))
143         return FALSE;
144     }
145
146   return TRUE;
147 }
148
149 static gboolean 
150 check_pixel_data (CacheInfo *info, 
151                   guint32    offset)
152 {
153   guint32 type;
154   guint32 length;
155
156   check ("offset, pixel data type", get_uint32 (info, offset, &type));
157   check ("offset, pixel data length", get_uint32 (info, offset + 4, &length));
158
159   check ("pixel data type", type == 0);
160   check ("pixel data length", offset + 8 + length < info->cache_size);
161
162   if (info->flags & CHECK_PIXBUFS) 
163     {
164       GdkPixdata data; 
165  
166       check ("pixel data", gdk_pixdata_deserialize (&data, length,
167                                                     (const guint8*)info->cache + offset + 8, 
168                                                     NULL));
169     }
170         
171   return TRUE;
172 }
173
174 static gboolean 
175 check_embedded_rect (CacheInfo *info, 
176                      guint32    offset)
177 {
178   check ("embedded rect", offset + 4 < info->cache_size);
179
180   return TRUE;
181 }
182
183 static gboolean 
184 check_attach_point_list (CacheInfo *info, 
185                          guint32    offset)
186 {
187   guint32 n_attach_points;
188
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); 
191
192   return TRUE;
193 }
194
195 static gboolean 
196 check_display_name_list (CacheInfo *info, 
197                          guint32    offset)
198 {
199   guint32 n_display_names, ofs;
200   gint i;
201
202   check ("offset, display name list", 
203          get_uint32 (info, offset, &n_display_names));
204   for (i = 0; i < n_display_names; i++) 
205     {
206       get_uint32(info, offset + 4 + 8 * i, &ofs);
207       if (!check_string (info, ofs))
208         return FALSE;
209       get_uint32(info, offset + 4 + 8 * i + 4, &ofs);
210       if (!check_string_utf8 (info, ofs))
211         return FALSE;
212     }
213         
214   return TRUE;  
215 }
216
217 static gboolean 
218 check_meta_data (CacheInfo *info, 
219                  guint32    offset)
220 {
221   guint32 embedded_rect_offset;
222   guint32 attach_point_list_offset;
223   guint32 display_name_list_offset;
224
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));
231
232   if (embedded_rect_offset != 0) 
233     {
234       if (!check_embedded_rect (info, embedded_rect_offset))
235         return FALSE;
236     }
237
238   if (attach_point_list_offset != 0) 
239     {
240       if (!check_attach_point_list (info, attach_point_list_offset))
241         return FALSE;
242     }
243
244   if (display_name_list_offset != 0) 
245     {
246       if (!check_display_name_list (info, display_name_list_offset))
247         return FALSE;
248     }
249
250   return TRUE;
251 }
252
253 static gboolean 
254 check_image_data (CacheInfo *info, 
255                   guint32    offset)
256 {
257   guint32 pixel_data_offset;
258   guint32 meta_data_offset;
259
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));
262
263   if (pixel_data_offset != 0) 
264     {
265       if (!check_pixel_data (info, pixel_data_offset))
266         return FALSE;
267     }
268   if (meta_data_offset != 0) 
269     {
270       if (!check_meta_data (info, meta_data_offset))
271         return FALSE;
272     }
273         
274   return TRUE;
275 }
276
277 static gboolean 
278 check_image (CacheInfo *info, 
279              guint32    offset)
280 {
281   guint16 index;
282   guint16 flags;
283   guint32 image_data_offset;
284
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));
289
290   check ("image index", index < info->n_directories);
291   check ("image flags", flags < 16);
292
293   if (image_data_offset != 0) 
294     {
295       if (!check_image_data (info, image_data_offset))
296         return FALSE;
297     }
298
299   return TRUE;
300 }
301
302 static gboolean 
303 check_image_list (CacheInfo *info, 
304                   guint32    offset)
305 {
306   guint32 n_images;
307   gint i;
308
309   check ("offset, image list", get_uint32 (info, offset, &n_images));
310         
311   for (i = 0; i < n_images; i++) 
312     {
313       if (!check_image (info, offset + 4 + 8 * i))
314         return FALSE;
315     }
316
317   return TRUE;
318 }
319
320 static gboolean 
321 check_icon (CacheInfo *info, 
322             guint32    offset)
323 {
324   guint32 chain_offset;
325   guint32 name_offset;
326   guint32 image_list_offset;
327
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));
332
333   if (!check_string (info, name_offset))
334     return FALSE;
335   if (!check_image_list (info, image_list_offset))
336     return FALSE;
337   if (chain_offset != 0xffffffff) 
338     {
339       if (!check_icon (info, chain_offset))
340         return FALSE;
341     }
342
343   return TRUE;
344 }
345
346 static gboolean 
347 check_hash (CacheInfo *info, 
348             guint32    offset)
349 {
350   guint32 n_buckets, icon_offset;
351   gint i;
352
353   check ("offset, hash size", get_uint32 (info, offset, &n_buckets));
354
355   for (i = 0; i < n_buckets; i++) 
356     {
357       check ("offset, hash chain", 
358              get_uint32 (info, offset + 4 + 4 * i, &icon_offset));
359       if (icon_offset != 0xffffffff) 
360         {
361           if (!check_icon (info, icon_offset))
362             return FALSE;
363         }
364     }
365
366   return TRUE;
367 }
368
369 /**
370  * _gtk_icon_cache_validate:
371  * @info: a CacheInfo structure 
372  *
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.  
380  *
381  * Return value: %TRUE if the cache is valid
382  */
383 gboolean 
384 _gtk_icon_cache_validate (CacheInfo *info)
385 {
386   guint32 hash_offset;
387   guint32 directory_list_offset;
388
389   if (!check_version (info))
390     return FALSE;
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))
394     return FALSE;
395
396   if (!check_hash (info, hash_offset))
397     return FALSE;
398
399   return TRUE;
400 }
401