Blame gio/thumbnail-verify.c

Packit ae235b
/* Copyright © 2013 Canonical Limited
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Ryan Lortie <desrt@desrt.ca>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "thumbnail-verify.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
/* Begin code to check the validity of thumbnail files.  In order to do
Packit ae235b
 * that we need to parse enough PNG in order to get the Thumb::URI,
Packit ae235b
 * Thumb::MTime and Thumb::Size tags out of the file.  Fortunately this
Packit ae235b
 * is relatively easy.
Packit ae235b
 */
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  const gchar *uri;
Packit ae235b
  guint64      mtime;
Packit ae235b
  guint64      size;
Packit ae235b
} ExpectedInfo;
Packit ae235b
Packit ae235b
/* We *require* matches on URI and MTime, but the Size field is optional
Packit ae235b
 * (as per the spec).
Packit ae235b
 *
Packit ae235b
 * http://specifications.freedesktop.org/thumbnail-spec/thumbnail-spec-latest.html
Packit ae235b
 */
Packit ae235b
#define MATCHED_URI    (1u << 0)
Packit ae235b
#define MATCHED_MTIME  (1u << 1)
Packit ae235b
#define MATCHED_ALL    (MATCHED_URI | MATCHED_MTIME)
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
check_integer_match (guint64      expected,
Packit ae235b
                     const gchar *value,
Packit ae235b
                     guint32      value_size)
Packit ae235b
{
Packit ae235b
  /* Would be nice to g_ascii_strtoll here, but we don't have a variant
Packit ae235b
   * that works on strings that are not nul-terminated.
Packit ae235b
   *
Packit ae235b
   * It's easy enough to do it ourselves...
Packit ae235b
   */
Packit ae235b
  if (expected == 0)  /* special case: "0" */
Packit ae235b
    return value_size == 1 && value[0] == '0';
Packit ae235b
Packit ae235b
  /* Check each digit, as long as we have data from both */
Packit ae235b
  while (expected && value_size)
Packit ae235b
    {
Packit ae235b
      /* Check the low-order digit */
Packit ae235b
      if (value[value_size - 1] != (gchar) ((expected % 10) + '0'))
Packit ae235b
        return FALSE;
Packit ae235b
Packit ae235b
      /* Move on... */
Packit ae235b
      expected /= 10;
Packit ae235b
      value_size--;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Make sure nothing is left over, on either side */
Packit ae235b
  return !expected && !value_size;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
check_png_info_chunk (ExpectedInfo *expected_info,
Packit ae235b
                      const gchar  *key,
Packit ae235b
                      guint32       key_size,
Packit ae235b
                      const gchar  *value,
Packit ae235b
                      guint32       value_size,
Packit ae235b
                      guint        *required_matches)
Packit ae235b
{
Packit ae235b
  if (key_size == 10 && memcmp (key, "Thumb::URI", 10) == 0)
Packit ae235b
    {
Packit ae235b
      gsize expected_size;
Packit ae235b
Packit ae235b
      expected_size = strlen (expected_info->uri);
Packit ae235b
Packit ae235b
      if (expected_size != value_size)
Packit ae235b
        return FALSE;
Packit ae235b
Packit ae235b
      if (memcmp (expected_info->uri, value, value_size) != 0)
Packit ae235b
        return FALSE;
Packit ae235b
Packit ae235b
      *required_matches |= MATCHED_URI;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  else if (key_size == 12 && memcmp (key, "Thumb::MTime", 12) == 0)
Packit ae235b
    {
Packit ae235b
      if (!check_integer_match (expected_info->mtime, value, value_size))
Packit ae235b
        return FALSE;
Packit ae235b
Packit ae235b
      *required_matches |= MATCHED_MTIME;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  else if (key_size == 11 && memcmp (key, "Thumb::Size", 11) == 0)
Packit ae235b
    {
Packit ae235b
      /* A match on Thumb::Size is not required for success, but if we
Packit ae235b
       * find this optional field and it's wrong, we should reject the
Packit ae235b
       * thumbnail.
Packit ae235b
       */
Packit ae235b
      if (!check_integer_match (expected_info->size, value, value_size))
Packit ae235b
        return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
check_thumbnail_validity (ExpectedInfo *expected_info,
Packit ae235b
                          const gchar  *contents,
Packit ae235b
                          gsize         size)
Packit ae235b
{
Packit ae235b
  guint required_matches = 0;
Packit ae235b
Packit ae235b
  /* Reference: http://www.w3.org/TR/PNG/ */
Packit ae235b
  if (size < 8)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  if (memcmp (contents, "\x89PNG\r\n\x1a\n", 8) != 0)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  contents += 8, size -= 8;
Packit ae235b
Packit ae235b
  /* We need at least 12 bytes to have a chunk... */
Packit ae235b
  while (size >= 12)
Packit ae235b
    {
Packit ae235b
      guint32 chunk_size_be;
Packit ae235b
      guint32 chunk_size;
Packit ae235b
Packit ae235b
      /* PNG is not an aligned file format so we have to be careful
Packit ae235b
       * about reading integers...
Packit ae235b
       */
Packit ae235b
      memcpy (&chunk_size_be, contents, 4);
Packit ae235b
      chunk_size = GUINT32_FROM_BE (chunk_size_be);
Packit ae235b
Packit ae235b
      contents += 4, size -= 4;
Packit ae235b
Packit ae235b
      /* After consuming the size field, we need to have enough bytes
Packit ae235b
       * for 4 bytes type field, chunk_size bytes for data, then 4 byte
Packit ae235b
       * for CRC (which we ignore)
Packit ae235b
       *
Packit ae235b
       * We just read chunk_size from the file, so it may be very large.
Packit ae235b
       * Make sure it won't wrap when we add 8 to it.
Packit ae235b
       */
Packit ae235b
      if (G_MAXUINT32 - chunk_size < 8 || size < chunk_size + 8)
Packit ae235b
        goto out;
Packit ae235b
Packit ae235b
      /* We are only interested in tEXt fields */
Packit ae235b
      if (memcmp (contents, "tEXt", 4) == 0)
Packit ae235b
        {
Packit ae235b
          const gchar *key = contents + 4;
Packit ae235b
          guint32 key_size;
Packit ae235b
Packit ae235b
          /* We need to find the nul separator character that splits the
Packit ae235b
           * key/value.  The value is not terminated.
Packit ae235b
           *
Packit ae235b
           * If we find no nul then we just ignore the field.
Packit ae235b
           *
Packit ae235b
           * value may contain extra nuls, but check_png_info_chunk()
Packit ae235b
           * can handle that.
Packit ae235b
           */
Packit ae235b
          for (key_size = 0; key_size < chunk_size; key_size++)
Packit ae235b
            {
Packit ae235b
              if (key[key_size] == '\0')
Packit ae235b
                {
Packit ae235b
                  const gchar *value;
Packit ae235b
                  guint32 value_size;
Packit ae235b
Packit ae235b
                  /* Since key_size < chunk_size, value_size is
Packit ae235b
                   * definitely non-negative.
Packit ae235b
                   */
Packit ae235b
                  value_size = chunk_size - key_size - 1;
Packit ae235b
                  value = key + key_size + 1;
Packit ae235b
Packit ae235b
                  /* We found the separator character. */
Packit ae235b
                  if (!check_png_info_chunk (expected_info,
Packit ae235b
                                             key, key_size,
Packit ae235b
                                             value, value_size,
Packit ae235b
                                             &required_matches))
Packit ae235b
                    return FALSE;
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          /* A bit of a hack: assume that all tEXt chunks will appear
Packit ae235b
           * together.  Therefore, if we have already seen both required
Packit ae235b
           * fields and then see a non-tEXt chunk then we can assume we
Packit ae235b
           * are done.
Packit ae235b
           *
Packit ae235b
           * The common case is that the tEXt chunks come at the start
Packit ae235b
           * of the file before any of the image data.  This trick means
Packit ae235b
           * that we will only fault in a single page (4k) whereas many
Packit ae235b
           * thumbnails (particularly the large ones) can approach 100k
Packit ae235b
           * in size.
Packit ae235b
           */
Packit ae235b
          if (required_matches == MATCHED_ALL)
Packit ae235b
            goto out;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      /* skip to the next chunk, ignoring CRC. */
Packit ae235b
      contents += 4, size -= 4;                         /* type field */
Packit ae235b
      contents += chunk_size, size -= chunk_size;       /* data */
Packit ae235b
      contents += 4, size -= 4;                         /* CRC */
Packit ae235b
    }
Packit ae235b
Packit ae235b
out:
Packit ae235b
  return required_matches == MATCHED_ALL;
Packit ae235b
}
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
thumbnail_verify (const char     *thumbnail_path,
Packit ae235b
                  const gchar    *file_uri,
Packit ae235b
                  const GLocalFileStat *file_stat_buf)
Packit ae235b
{
Packit ae235b
  gboolean thumbnail_is_valid = FALSE;
Packit ae235b
  ExpectedInfo expected_info;
Packit ae235b
  GMappedFile *file;
Packit ae235b
Packit ae235b
  if (file_stat_buf == NULL)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  expected_info.uri = file_uri;
Packit ae235b
  expected_info.mtime = file_stat_buf->st_mtime;
Packit ae235b
  expected_info.size = file_stat_buf->st_size;
Packit ae235b
Packit ae235b
  file = g_mapped_file_new (thumbnail_path, FALSE, NULL);
Packit ae235b
  if (file)
Packit ae235b
    {
Packit ae235b
      thumbnail_is_valid = check_thumbnail_validity (&expected_info,
Packit ae235b
                                                     g_mapped_file_get_contents (file),
Packit ae235b
                                                     g_mapped_file_get_length (file));
Packit ae235b
      g_mapped_file_unref (file);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return thumbnail_is_valid;
Packit ae235b
}