gtkiconcachevalidator.c 9.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* gtkiconcachevalidator.c
 * Copyright (C) 2007 Red Hat, Inc
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23
 */
#include "config.h"
#include "gtkiconcachevalidator.h"

#include <glib.h>
#include <gdk-pixbuf/gdk-pixdata.h>


Matthias Clasen's avatar
Matthias Clasen committed
24
#define VERBOSE(x)
25 26 27 28

#define check(name,condition) \
  if (!(condition)) \
    { \
29
      VERBOSE(g_message ("bad %s", (name))); \
30
      return FALSE; \
Matthias Clasen's avatar
Matthias Clasen committed
31
    }
32

Matthias Clasen's avatar
Matthias Clasen committed
33 34 35
static inline gboolean
get_uint16 (CacheInfo *info,
            guint32    offset,
36 37
            guint16   *value)
{
Matthias Clasen's avatar
Matthias Clasen committed
38 39 40
  if (offset < info->cache_size)
    {
      *value = GUINT16_FROM_BE(*(guint16*)(info->cache + offset));
41 42
      return TRUE;
    }
Matthias Clasen's avatar
Matthias Clasen committed
43 44
  else
    {
45
      *value = 0;
Matthias Clasen's avatar
Matthias Clasen committed
46 47
      return FALSE;
    }
48 49
}

Matthias Clasen's avatar
Matthias Clasen committed
50 51 52
static inline gboolean
get_uint32 (CacheInfo *info,
            guint32    offset,
53 54
            guint32   *value)
{
Matthias Clasen's avatar
Matthias Clasen committed
55 56 57
  if (offset < info->cache_size)
    {
      *value = GUINT32_FROM_BE(*(guint32*)(info->cache + offset));
58 59
      return TRUE;
    }
Matthias Clasen's avatar
Matthias Clasen committed
60 61
  else
    {
62
      *value = 0;
Matthias Clasen's avatar
Matthias Clasen committed
63 64
      return FALSE;
    }
65 66
}

Matthias Clasen's avatar
Matthias Clasen committed
67
static gboolean
68 69 70 71 72 73 74 75 76 77
check_version (CacheInfo *info)
{
  guint16 major, minor;

  check ("major version", get_uint16 (info, 0, &major) && major == 1);
  check ("minor version", get_uint16 (info, 2, &minor) && minor == 0);

  return TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
78 79
static gboolean
check_string (CacheInfo *info,
80 81 82 83
              guint32    offset)
{
  check ("string offset", offset < info->cache_size);

Matthias Clasen's avatar
Matthias Clasen committed
84
  if (info->flags & CHECK_STRINGS)
85 86 87 88 89
    {
      gint i;
      gchar c;

      /* assume no string is longer than 1k */
Matthias Clasen's avatar
Matthias Clasen committed
90 91
      for (i = 0; i < 1024; i++)
        {
92 93 94 95 96 97 98 99 100 101 102 103
          check ("string offset", offset + i < info->cache_size)
          c = *(info->cache + offset + i);
          if (c == '\0')
            break;
          check ("string content", g_ascii_isgraph (c));
        }
      check ("string length", i < 1024);
    }

  return TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
104 105
static gboolean
check_string_utf8 (CacheInfo *info,
106 107 108 109
                   guint32    offset)
{
  check ("string offset", offset < info->cache_size);

Matthias Clasen's avatar
Matthias Clasen committed
110
  if (info->flags & CHECK_STRINGS)
111 112 113 114 115
    {
      gint i;
      gchar c;

      /* assume no string is longer than 1k */
Matthias Clasen's avatar
Matthias Clasen committed
116 117
      for (i = 0; i < 1024; i++)
        {
118 119 120 121 122 123 124 125 126 127 128 129
          check ("string offset", offset + i < info->cache_size)
            c = *(info->cache + offset + i);
          if (c == '\0')
            break;
        }
      check ("string length", i < 1024);
      check ("string utf8 data", g_utf8_validate((char *)(info->cache + offset), -1, NULL));
    }

  return TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
130 131
static gboolean
check_directory_list (CacheInfo *info,
132 133 134 135
                      guint32    offset)
{
  guint32 directory_offset;
  gint i;
Matthias Clasen's avatar
Matthias Clasen committed
136

137
  check ("offset, directory list", get_uint32 (info, offset, &info->n_directories));
Matthias Clasen's avatar
Matthias Clasen committed
138 139

  for (i = 0; i < info->n_directories; i++)
140 141 142 143 144 145 146 147 148
    {
      check ("offset, directory", get_uint32 (info, offset + 4 + 4 * i, &directory_offset));
      if (!check_string (info, directory_offset))
        return FALSE;
    }

  return TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
149 150
static gboolean
check_pixel_data (CacheInfo *info,
151 152 153 154 155 156 157 158 159 160 161
                  guint32    offset)
{
  guint32 type;
  guint32 length;

  check ("offset, pixel data type", get_uint32 (info, offset, &type));
  check ("offset, pixel data length", get_uint32 (info, offset + 4, &length));

  check ("pixel data type", type == 0);
  check ("pixel data length", offset + 8 + length < info->cache_size);

Matthias Clasen's avatar
Matthias Clasen committed
162
  if (info->flags & CHECK_PIXBUFS)
163
    {
Matthias Clasen's avatar
Matthias Clasen committed
164 165
      GdkPixdata data;

166
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
167
      check ("pixel data", gdk_pixdata_deserialize (&data, length,
Matthias Clasen's avatar
Matthias Clasen committed
168
                                                    (const guint8*)info->cache + offset + 8,
169
                                                    NULL));
170
G_GNUC_END_IGNORE_DEPRECATIONS;
171
    }
Matthias Clasen's avatar
Matthias Clasen committed
172

173 174 175
  return TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
176 177
static gboolean
check_embedded_rect (CacheInfo *info,
178 179 180 181 182 183 184
                     guint32    offset)
{
  check ("embedded rect", offset + 4 < info->cache_size);

  return TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
185 186
static gboolean
check_attach_point_list (CacheInfo *info,
187 188 189 190 191
                         guint32    offset)
{
  guint32 n_attach_points;

  check ("offset, attach point list", get_uint32 (info, offset, &n_attach_points));
Matthias Clasen's avatar
Matthias Clasen committed
192
  check ("attach points", offset + 4 + 4 * n_attach_points < info->cache_size);
193 194 195 196

  return TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
197 198
static gboolean
check_display_name_list (CacheInfo *info,
199 200
                         guint32    offset)
{
201
  guint32 n_display_names, ofs;
202 203
  gint i;

Matthias Clasen's avatar
Matthias Clasen committed
204
  check ("offset, display name list",
205
         get_uint32 (info, offset, &n_display_names));
Matthias Clasen's avatar
Matthias Clasen committed
206
  for (i = 0; i < n_display_names; i++)
207
    {
208 209
      get_uint32(info, offset + 4 + 8 * i, &ofs);
      if (!check_string (info, ofs))
210
        return FALSE;
211 212
      get_uint32(info, offset + 4 + 8 * i + 4, &ofs);
      if (!check_string_utf8 (info, ofs))
213 214
        return FALSE;
    }
Matthias Clasen's avatar
Matthias Clasen committed
215 216

  return TRUE;
217 218
}

Matthias Clasen's avatar
Matthias Clasen committed
219 220
static gboolean
check_meta_data (CacheInfo *info,
221 222 223 224 225 226
                 guint32    offset)
{
  guint32 embedded_rect_offset;
  guint32 attach_point_list_offset;
  guint32 display_name_list_offset;

Matthias Clasen's avatar
Matthias Clasen committed
227
  check ("offset, embedded rect",
228
         get_uint32 (info, offset, &embedded_rect_offset));
Matthias Clasen's avatar
Matthias Clasen committed
229
  check ("offset, attach point list",
230
         get_uint32 (info, offset + 4, &attach_point_list_offset));
Matthias Clasen's avatar
Matthias Clasen committed
231
  check ("offset, display name list",
232 233
         get_uint32 (info, offset + 8, &display_name_list_offset));

Matthias Clasen's avatar
Matthias Clasen committed
234
  if (embedded_rect_offset != 0)
235 236 237 238 239
    {
      if (!check_embedded_rect (info, embedded_rect_offset))
        return FALSE;
    }

Matthias Clasen's avatar
Matthias Clasen committed
240
  if (attach_point_list_offset != 0)
241 242 243 244 245
    {
      if (!check_attach_point_list (info, attach_point_list_offset))
        return FALSE;
    }

Matthias Clasen's avatar
Matthias Clasen committed
246
  if (display_name_list_offset != 0)
247 248 249 250 251 252 253 254
    {
      if (!check_display_name_list (info, display_name_list_offset))
        return FALSE;
    }

  return TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
255 256
static gboolean
check_image_data (CacheInfo *info,
257 258 259 260 261 262 263 264
                  guint32    offset)
{
  guint32 pixel_data_offset;
  guint32 meta_data_offset;

  check ("offset, pixel data", get_uint32 (info, offset, &pixel_data_offset));
  check ("offset, meta data", get_uint32 (info, offset + 4, &meta_data_offset));

Matthias Clasen's avatar
Matthias Clasen committed
265
  if (pixel_data_offset != 0)
266 267 268 269
    {
      if (!check_pixel_data (info, pixel_data_offset))
        return FALSE;
    }
Matthias Clasen's avatar
Matthias Clasen committed
270
  if (meta_data_offset != 0)
271 272 273 274
    {
      if (!check_meta_data (info, meta_data_offset))
        return FALSE;
    }
Matthias Clasen's avatar
Matthias Clasen committed
275

276 277 278
  return TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
279 280
static gboolean
check_image (CacheInfo *info,
281 282 283 284 285 286 287 288
             guint32    offset)
{
  guint16 index;
  guint16 flags;
  guint32 image_data_offset;

  check ("offset, image index", get_uint16 (info, offset, &index));
  check ("offset, image flags", get_uint16 (info, offset + 2, &flags));	
Matthias Clasen's avatar
Matthias Clasen committed
289
  check ("offset, image data offset",
290 291 292
         get_uint32 (info, offset + 4, &image_data_offset));

  check ("image index", index < info->n_directories);
293
  check ("image flags", flags < 16);
294

Matthias Clasen's avatar
Matthias Clasen committed
295
  if (image_data_offset != 0)
296 297 298 299 300 301 302 303
    {
      if (!check_image_data (info, image_data_offset))
        return FALSE;
    }

  return TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
304 305
static gboolean
check_image_list (CacheInfo *info,
306 307 308 309 310 311
                  guint32    offset)
{
  guint32 n_images;
  gint i;

  check ("offset, image list", get_uint32 (info, offset, &n_images));
Matthias Clasen's avatar
Matthias Clasen committed
312 313

  for (i = 0; i < n_images; i++)
314 315 316 317 318 319 320 321
    {
      if (!check_image (info, offset + 4 + 8 * i))
        return FALSE;
    }

  return TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
322 323
static gboolean
check_icon (CacheInfo *info,
324 325 326 327 328 329 330 331
            guint32    offset)
{
  guint32 chain_offset;
  guint32 name_offset;
  guint32 image_list_offset;

  check ("offset, icon chain", get_uint32 (info, offset, &chain_offset));
  check ("offset, icon name", get_uint32 (info, offset + 4, &name_offset));
Matthias Clasen's avatar
Matthias Clasen committed
332
  check ("offset, icon image list", get_uint32 (info, offset + 8,
333 334 335 336 337 338
         &image_list_offset));

  if (!check_string (info, name_offset))
    return FALSE;
  if (!check_image_list (info, image_list_offset))
    return FALSE;
Matthias Clasen's avatar
Matthias Clasen committed
339
  if (chain_offset != 0xffffffff)
340 341 342 343 344 345 346 347
    {
      if (!check_icon (info, chain_offset))
        return FALSE;
    }

  return TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
348 349
static gboolean
check_hash (CacheInfo *info,
350 351 352 353 354 355 356
            guint32    offset)
{
  guint32 n_buckets, icon_offset;
  gint i;

  check ("offset, hash size", get_uint32 (info, offset, &n_buckets));

Matthias Clasen's avatar
Matthias Clasen committed
357
  for (i = 0; i < n_buckets; i++)
358
    {
Matthias Clasen's avatar
Matthias Clasen committed
359
      check ("offset, hash chain",
360
             get_uint32 (info, offset + 4 + 4 * i, &icon_offset));
Matthias Clasen's avatar
Matthias Clasen committed
361
      if (icon_offset != 0xffffffff)
362 363 364 365 366 367 368 369 370 371 372
        {
          if (!check_icon (info, icon_offset))
            return FALSE;
        }
    }

  return TRUE;
}

/**
 * _gtk_icon_cache_validate:
Matthias Clasen's avatar
Matthias Clasen committed
373
 * @info: a CacheInfo structure
374 375 376 377 378 379 380
 *
 * Validates the icon cache passed in the @cache and
 * @cache_size fields of the @info structure. The
 * validator checks that offsets specified in the
 * cache do not point outside the mapped area, that
 * strings look reasonable, and that pixbufs can
 * be deserialized. The amount of validation can
Matthias Clasen's avatar
Matthias Clasen committed
381
 * be controlled with the @flags field.
382
 *
383
 * Returns: %TRUE if the cache is valid
384
 */
Matthias Clasen's avatar
Matthias Clasen committed
385
gboolean
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
_gtk_icon_cache_validate (CacheInfo *info)
{
  guint32 hash_offset;
  guint32 directory_list_offset;

  if (!check_version (info))
    return FALSE;
  check ("header, hash offset", get_uint32 (info, 4, &hash_offset));
  check ("header, directory list offset", get_uint32 (info, 8, &directory_list_offset));
  if (!check_directory_list (info, directory_list_offset))
    return FALSE;

  if (!check_hash (info, hash_offset))
    return FALSE;

  return TRUE;
}