Blame gio/gvdb/gvdb-reader.c

Packit ae235b
/*
Packit ae235b
 * Copyright © 2010 Codethink 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 Public
Packit ae235b
 * 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 "gvdb-reader.h"
Packit ae235b
#include "gvdb-format.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
struct _GvdbTable {
Packit ae235b
  gint ref_count;
Packit ae235b
Packit ae235b
  const gchar *data;
Packit ae235b
  gsize size;
Packit ae235b
Packit ae235b
  gpointer user_data;
Packit ae235b
  GvdbRefFunc ref_user_data;
Packit ae235b
  GDestroyNotify unref_user_data;
Packit ae235b
Packit ae235b
  gboolean byteswapped;
Packit ae235b
  gboolean trusted;
Packit ae235b
Packit ae235b
  const guint32_le *bloom_words;
Packit ae235b
  guint32 n_bloom_words;
Packit ae235b
  guint bloom_shift;
Packit ae235b
Packit ae235b
  const guint32_le *hash_buckets;
Packit ae235b
  guint32 n_buckets;
Packit ae235b
Packit ae235b
  struct gvdb_hash_item *hash_items;
Packit ae235b
  guint32 n_hash_items;
Packit ae235b
};
Packit ae235b
Packit ae235b
static const gchar *
Packit ae235b
gvdb_table_item_get_key (GvdbTable                   *file,
Packit ae235b
                         const struct gvdb_hash_item *item,
Packit ae235b
                         gsize                       *size)
Packit ae235b
{
Packit ae235b
  guint32 start, end;
Packit ae235b
Packit ae235b
  start = guint32_from_le (item->key_start);
Packit ae235b
  *size = guint16_from_le (item->key_size);
Packit ae235b
  end = start + *size;
Packit ae235b
Packit ae235b
  if G_UNLIKELY (start > end || end > file->size)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  return file->data + start;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gconstpointer
Packit ae235b
gvdb_table_dereference (GvdbTable                 *file,
Packit ae235b
                        const struct gvdb_pointer *pointer,
Packit ae235b
                        gint                       alignment,
Packit ae235b
                        gsize                     *size)
Packit ae235b
{
Packit ae235b
  guint32 start, end;
Packit ae235b
Packit ae235b
  start = guint32_from_le (pointer->start);
Packit ae235b
  end = guint32_from_le (pointer->end);
Packit ae235b
Packit ae235b
  if G_UNLIKELY (start > end || end > file->size || start & (alignment - 1))
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  *size = end - start;
Packit ae235b
Packit ae235b
  return file->data + start;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
gvdb_table_setup_root (GvdbTable                 *file,
Packit ae235b
                       const struct gvdb_pointer *pointer)
Packit ae235b
{
Packit ae235b
  const struct gvdb_hash_header *header;
Packit ae235b
  guint32 n_bloom_words;
Packit ae235b
  guint32 n_buckets;
Packit ae235b
  gsize size;
Packit ae235b
Packit ae235b
  header = gvdb_table_dereference (file, pointer, 4, &size);
Packit ae235b
Packit ae235b
  if G_UNLIKELY (header == NULL || size < sizeof *header)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  size -= sizeof *header;
Packit ae235b
Packit ae235b
  n_bloom_words = guint32_from_le (header->n_bloom_words);
Packit ae235b
  n_buckets = guint32_from_le (header->n_buckets);
Packit ae235b
  n_bloom_words &= (1u << 27) - 1;
Packit ae235b
Packit ae235b
  if G_UNLIKELY (n_bloom_words * sizeof (guint32_le) > size)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  file->bloom_words = (gpointer) (header + 1);
Packit ae235b
  size -= n_bloom_words * sizeof (guint32_le);
Packit ae235b
  file->n_bloom_words = n_bloom_words;
Packit ae235b
Packit ae235b
  if G_UNLIKELY (n_buckets > G_MAXUINT / sizeof (guint32_le) ||
Packit ae235b
                 n_buckets * sizeof (guint32_le) > size)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  file->hash_buckets = file->bloom_words + file->n_bloom_words;
Packit ae235b
  size -= n_buckets * sizeof (guint32_le);
Packit ae235b
  file->n_buckets = n_buckets;
Packit ae235b
Packit ae235b
  if G_UNLIKELY (size % sizeof (struct gvdb_hash_item))
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  file->hash_items = (gpointer) (file->hash_buckets + n_buckets);
Packit ae235b
  file->n_hash_items = size / sizeof (struct gvdb_hash_item);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GvdbTable *
Packit ae235b
new_from_data (const void    *data,
Packit ae235b
	       gsize          data_len,
Packit ae235b
	       gboolean       trusted,
Packit ae235b
	       gpointer       user_data,
Packit ae235b
	       GvdbRefFunc    ref,
Packit ae235b
	       GDestroyNotify unref,
Packit ae235b
	       const char    *filename,
Packit ae235b
	       GError       **error)
Packit ae235b
{
Packit ae235b
  GvdbTable *file;
Packit ae235b
Packit ae235b
  file = g_slice_new0 (GvdbTable);
Packit ae235b
  file->data = data;
Packit ae235b
  file->size = data_len;
Packit ae235b
  file->trusted = trusted;
Packit ae235b
  file->ref_count = 1;
Packit ae235b
  file->ref_user_data = ref;
Packit ae235b
  file->unref_user_data = unref;
Packit ae235b
  file->user_data = user_data;
Packit ae235b
Packit ae235b
  if (sizeof (struct gvdb_header) <= file->size)
Packit ae235b
    {
Packit ae235b
      const struct gvdb_header *header = (gpointer) file->data;
Packit ae235b
Packit ae235b
      if (header->signature[0] == GVDB_SIGNATURE0 &&
Packit ae235b
          header->signature[1] == GVDB_SIGNATURE1 &&
Packit ae235b
          guint32_from_le (header->version) == 0)
Packit ae235b
        file->byteswapped = FALSE;
Packit ae235b
Packit ae235b
      else if (header->signature[0] == GVDB_SWAPPED_SIGNATURE0 &&
Packit ae235b
               header->signature[1] == GVDB_SWAPPED_SIGNATURE1 &&
Packit ae235b
               guint32_from_le (header->version) == 0)
Packit ae235b
        file->byteswapped = TRUE;
Packit ae235b
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
	  if (filename)
Packit ae235b
	    g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL,
Packit ae235b
			 "%s: invalid header", filename);
Packit ae235b
	  else
Packit ae235b
	    g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL,
Packit ae235b
			 "invalid gvdb header");
Packit ae235b
          g_slice_free (GvdbTable, file);
Packit ae235b
	  if (unref)
Packit ae235b
	    unref (user_data);
Packit ae235b
Packit ae235b
          return NULL;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      gvdb_table_setup_root (file, &header->root);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return file;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * gvdb_table_new:
Packit ae235b
 * @filename: the path to the hash file
Packit ae235b
 * @trusted: if the contents of @filename are trusted
Packit ae235b
 * @error: %NULL, or a pointer to a %NULL #GError
Packit ae235b
 *
Packit ae235b
 * Creates a new #GvdbTable from the contents of the file found at
Packit ae235b
 * @filename.
Packit ae235b
 *
Packit ae235b
 * The only time this function fails is if the file cannot be opened.
Packit ae235b
 * In that case, the #GError that is returned will be an error from
Packit ae235b
 * g_mapped_file_new().
Packit ae235b
 *
Packit ae235b
 * An empty or otherwise corrupted file is considered to be a valid
Packit ae235b
 * #GvdbTable with no entries.
Packit ae235b
 *
Packit ae235b
 * You should call gvdb_table_unref() on the return result when you no
Packit ae235b
 * longer require it.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GvdbTable
Packit ae235b
 **/
Packit ae235b
GvdbTable *
Packit ae235b
gvdb_table_new (const gchar  *filename,
Packit ae235b
                gboolean      trusted,
Packit ae235b
                GError      **error)
Packit ae235b
{
Packit ae235b
  GMappedFile *mapped;
Packit ae235b
Packit ae235b
  if ((mapped = g_mapped_file_new (filename, FALSE, error)) == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  return new_from_data (g_mapped_file_get_contents (mapped),
Packit ae235b
			g_mapped_file_get_length (mapped),
Packit ae235b
			trusted,
Packit ae235b
			mapped,
Packit ae235b
			(GvdbRefFunc)g_mapped_file_ref,
Packit ae235b
			(GDestroyNotify)g_mapped_file_unref,
Packit ae235b
			filename,
Packit ae235b
			error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * gvdb_table_new_from_data:
Packit ae235b
 * @data: the data
Packit ae235b
 * @data_len: the length of @data in bytes
Packit ae235b
 * @trusted: if the contents of @data are trusted
Packit ae235b
 * @user_data: User supplied data that owns @data
Packit ae235b
 * @ref: Ref function for @user_data
Packit ae235b
 * @unref: Unref function for @user_data
Packit ae235b
 *
Packit ae235b
 * Creates a new #GvdbTable from the data in @data.
Packit ae235b
 *
Packit ae235b
 * An empty or otherwise corrupted data is considered to be a valid
Packit ae235b
 * #GvdbTable with no entries.
Packit ae235b
 *
Packit ae235b
 * You should call gvdb_table_unref() on the return result when you no
Packit ae235b
 * longer require it.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GvdbTable
Packit ae235b
 **/
Packit ae235b
GvdbTable *
Packit ae235b
gvdb_table_new_from_data (const void    *data,
Packit ae235b
			  gsize          data_len,
Packit ae235b
			  gboolean       trusted,
Packit ae235b
			  gpointer       user_data,
Packit ae235b
			  GvdbRefFunc    ref,
Packit ae235b
			  GDestroyNotify unref,
Packit ae235b
			  GError        **error)
Packit ae235b
{
Packit ae235b
  return new_from_data (data, data_len,
Packit ae235b
			trusted,
Packit ae235b
			user_data, ref, unref,
Packit ae235b
			NULL,
Packit ae235b
			error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
gvdb_table_bloom_filter (GvdbTable *file,
Packit ae235b
                          guint32    hash_value)
Packit ae235b
{
Packit ae235b
  guint32 word, mask;
Packit ae235b
Packit ae235b
  if (file->n_bloom_words == 0)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  word = (hash_value / 32) % file->n_bloom_words;
Packit ae235b
  mask = 1 << (hash_value & 31);
Packit ae235b
  mask |= 1 << ((hash_value >> file->bloom_shift) & 31);
Packit ae235b
Packit ae235b
  return (guint32_from_le (file->bloom_words[word]) & mask) == mask;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
gvdb_table_check_name (GvdbTable             *file,
Packit ae235b
                       struct gvdb_hash_item *item,
Packit ae235b
                       const gchar           *key,
Packit ae235b
                       guint                  key_length)
Packit ae235b
{
Packit ae235b
  const gchar *this_key;
Packit ae235b
  gsize this_size;
Packit ae235b
  guint32 parent;
Packit ae235b
Packit ae235b
  this_key = gvdb_table_item_get_key (file, item, &this_size);
Packit ae235b
Packit ae235b
  if G_UNLIKELY (this_key == NULL || this_size > key_length)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  key_length -= this_size;
Packit ae235b
Packit ae235b
  if G_UNLIKELY (memcmp (this_key, key + key_length, this_size) != 0)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  parent = guint32_from_le (item->parent);
Packit ae235b
  if (key_length == 0 && parent == 0xffffffffu)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  if G_LIKELY (parent < file->n_hash_items && this_size > 0)
Packit ae235b
    return gvdb_table_check_name (file,
Packit ae235b
                                   &file->hash_items[parent],
Packit ae235b
                                   key, key_length);
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static const struct gvdb_hash_item *
Packit ae235b
gvdb_table_lookup (GvdbTable   *file,
Packit ae235b
                   const gchar *key,
Packit ae235b
                   gchar        type)
Packit ae235b
{
Packit ae235b
  guint32 hash_value = 5381;
Packit ae235b
  guint key_length;
Packit ae235b
  guint32 bucket;
Packit ae235b
  guint32 lastno;
Packit ae235b
  guint32 itemno;
Packit ae235b
Packit ae235b
  if G_UNLIKELY (file->n_buckets == 0 || file->n_hash_items == 0)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  for (key_length = 0; key[key_length]; key_length++)
Packit ae235b
    hash_value = (hash_value * 33) + ((signed char *) key)[key_length];
Packit ae235b
Packit ae235b
  if (!gvdb_table_bloom_filter (file, hash_value))
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  bucket = hash_value % file->n_buckets;
Packit ae235b
  itemno = guint32_from_le (file->hash_buckets[bucket]);
Packit ae235b
Packit ae235b
  if (bucket == file->n_buckets - 1 ||
Packit ae235b
      (lastno = guint32_from_le(file->hash_buckets[bucket + 1])) > file->n_hash_items)
Packit ae235b
    lastno = file->n_hash_items;
Packit ae235b
Packit ae235b
  while G_LIKELY (itemno < lastno)
Packit ae235b
    {
Packit ae235b
      struct gvdb_hash_item *item = &file->hash_items[itemno];
Packit ae235b
Packit ae235b
      if (hash_value == guint32_from_le (item->hash_value))
Packit ae235b
        if G_LIKELY (gvdb_table_check_name (file, item, key, key_length))
Packit ae235b
          if G_LIKELY (item->type == type)
Packit ae235b
            return item;
Packit ae235b
Packit ae235b
      itemno++;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static const struct gvdb_hash_item *
Packit ae235b
gvdb_table_get_item (GvdbTable  *table,
Packit ae235b
                     guint32_le  item_no)
Packit ae235b
{
Packit ae235b
  guint32 item_no_native = guint32_from_le (item_no);
Packit ae235b
Packit ae235b
  if G_LIKELY (item_no_native < table->n_hash_items)
Packit ae235b
    return table->hash_items + item_no_native;
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
gvdb_table_list_from_item (GvdbTable                    *table,
Packit ae235b
                           const struct gvdb_hash_item  *item,
Packit ae235b
                           const guint32_le            **list,
Packit ae235b
                           guint                        *length)
Packit ae235b
{
Packit ae235b
  gsize size;
Packit ae235b
Packit ae235b
  *list = gvdb_table_dereference (table, &item->value.pointer, 4, &size);
Packit ae235b
Packit ae235b
  if G_LIKELY (*list == NULL || size % 4)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  *length = size / 4;
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * gvdb_table_list:
Packit ae235b
 * @file: a #GvdbTable
Packit ae235b
 * @key: a string
Packit ae235b
 *
Packit ae235b
 * List all of the keys that appear below @key.  The nesting of keys
Packit ae235b
 * within the hash file is defined by the program that created the hash
Packit ae235b
 * file.  One thing is constant: each item in the returned array can be
Packit ae235b
 * concatenated to @key to obtain the full name of that key.
Packit ae235b
 *
Packit ae235b
 * It is not possible to tell from this function if a given key is
Packit ae235b
 * itself a path, a value, or another hash table; you are expected to
Packit ae235b
 * know this for yourself.
Packit ae235b
 *
Packit ae235b
 * You should call g_strfreev() on the return result when you no longer
Packit ae235b
 * require it.
Packit ae235b
 *
Packit ae235b
 * Returns: a %NULL-terminated string array
Packit ae235b
 **/
Packit ae235b
gchar **
Packit ae235b
gvdb_table_list (GvdbTable   *file,
Packit ae235b
                 const gchar *key)
Packit ae235b
{
Packit ae235b
  const struct gvdb_hash_item *item;
Packit ae235b
  const guint32_le *list;
Packit ae235b
  gchar **strv;
Packit ae235b
  guint length;
Packit ae235b
  guint i;
Packit ae235b
Packit ae235b
  if ((item = gvdb_table_lookup (file, key, 'L')) == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  if (!gvdb_table_list_from_item (file, item, &list, &length))
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  strv = g_new (gchar *, length + 1);
Packit ae235b
  for (i = 0; i < length; i++)
Packit ae235b
    {
Packit ae235b
      guint32 itemno = guint32_from_le (list[i]);
Packit ae235b
Packit ae235b
      if (itemno < file->n_hash_items)
Packit ae235b
        {
Packit ae235b
          const struct gvdb_hash_item *item;
Packit ae235b
          const gchar *string;
Packit ae235b
          gsize strsize;
Packit ae235b
Packit ae235b
          item = file->hash_items + itemno;
Packit ae235b
Packit ae235b
          string = gvdb_table_item_get_key (file, item, &strsize);
Packit ae235b
Packit ae235b
          if (string != NULL)
Packit ae235b
            strv[i] = g_strndup (string, strsize);
Packit ae235b
          else
Packit ae235b
            strv[i] = g_malloc0 (1);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        strv[i] = g_malloc0 (1);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  strv[i] = NULL;
Packit ae235b
Packit ae235b
  return strv;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * gvdb_table_has_value:
Packit ae235b
 * @file: a #GvdbTable
Packit ae235b
 * @key: a string
Packit ae235b
 *
Packit ae235b
 * Checks for a value named @key in @file.
Packit ae235b
 *
Packit ae235b
 * Note: this function does not consider non-value nodes (other hash
Packit ae235b
 * tables, for example).
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @key is in the table
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
gvdb_table_has_value (GvdbTable    *file,
Packit ae235b
                      const gchar  *key)
Packit ae235b
{
Packit ae235b
  return gvdb_table_lookup (file, key, 'v') != NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GVariant *
Packit ae235b
gvdb_table_value_from_item (GvdbTable                   *table,
Packit ae235b
                            const struct gvdb_hash_item *item)
Packit ae235b
{
Packit ae235b
  GVariant *variant, *value;
Packit ae235b
  gconstpointer data;
Packit ae235b
  gsize size;
Packit ae235b
Packit ae235b
  data = gvdb_table_dereference (table, &item->value.pointer, 8, &size);
Packit ae235b
Packit ae235b
  if G_UNLIKELY (data == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  variant = g_variant_new_from_data (G_VARIANT_TYPE_VARIANT,
Packit ae235b
                                     data, size, table->trusted,
Packit ae235b
                                     table->unref_user_data,
Packit ae235b
                                     table->ref_user_data ? table->ref_user_data (table->user_data) : table->user_data);
Packit ae235b
  value = g_variant_get_variant (variant);
Packit ae235b
  g_variant_unref (variant);
Packit ae235b
Packit ae235b
  return value;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * gvdb_table_get_value:
Packit ae235b
 * @file: a #GvdbTable
Packit ae235b
 * @key: a string
Packit ae235b
 *
Packit ae235b
 * Looks up a value named @key in @file.
Packit ae235b
 *
Packit ae235b
 * If the value is not found then %NULL is returned.  Otherwise, a new
Packit ae235b
 * #GVariant instance is returned.  The #GVariant does not depend on the
Packit ae235b
 * continued existence of @file.
Packit ae235b
 *
Packit ae235b
 * You should call g_variant_unref() on the return result when you no
Packit ae235b
 * longer require it.
Packit ae235b
 *
Packit ae235b
 * Returns: a #GVariant, or %NULL
Packit ae235b
 **/
Packit ae235b
GVariant *
Packit ae235b
gvdb_table_get_value (GvdbTable    *file,
Packit ae235b
                      const gchar  *key)
Packit ae235b
{
Packit ae235b
  const struct gvdb_hash_item *item;
Packit ae235b
  GVariant *value;
Packit ae235b
Packit ae235b
  if ((item = gvdb_table_lookup (file, key, 'v')) == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  value = gvdb_table_value_from_item (file, item);
Packit ae235b
Packit ae235b
  if (value && file->byteswapped)
Packit ae235b
    {
Packit ae235b
      GVariant *tmp;
Packit ae235b
Packit ae235b
      tmp = g_variant_byteswap (value);
Packit ae235b
      g_variant_unref (value);
Packit ae235b
      value = tmp;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return value;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * gvdb_table_get_raw_value:
Packit ae235b
 * @table: a #GvdbTable
Packit ae235b
 * @key: a string
Packit ae235b
 *
Packit ae235b
 * Looks up a value named @key in @file.
Packit ae235b
 *
Packit ae235b
 * This call is equivalent to gvdb_table_get_value() except that it
Packit ae235b
 * never byteswaps the value.
Packit ae235b
 *
Packit ae235b
 * Returns: a #GVariant, or %NULL
Packit ae235b
 **/
Packit ae235b
GVariant *
Packit ae235b
gvdb_table_get_raw_value (GvdbTable   *table,
Packit ae235b
                          const gchar *key)
Packit ae235b
{
Packit ae235b
  const struct gvdb_hash_item *item;
Packit ae235b
Packit ae235b
  if ((item = gvdb_table_lookup (table, key, 'v')) == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  return gvdb_table_value_from_item (table, item);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * gvdb_table_get_table:
Packit ae235b
 * @file: a #GvdbTable
Packit ae235b
 * @key: a string
Packit ae235b
 *
Packit ae235b
 * Looks up the hash table named @key in @file.
Packit ae235b
 *
Packit ae235b
 * The toplevel hash table in a #GvdbTable can contain reference to
Packit ae235b
 * child hash tables (and those can contain further references...).
Packit ae235b
 *
Packit ae235b
 * If @key is not found in @file then %NULL is returned.  Otherwise, a
Packit ae235b
 * new #GvdbTable is returned, referring to the child hashtable as
Packit ae235b
 * contained in the file.  This newly-created #GvdbTable does not depend
Packit ae235b
 * on the continued existence of @file.
Packit ae235b
 *
Packit ae235b
 * You should call gvdb_table_unref() on the return result when you no
Packit ae235b
 * longer require it.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GvdbTable, or %NULL
Packit ae235b
 **/
Packit ae235b
GvdbTable *
Packit ae235b
gvdb_table_get_table (GvdbTable   *file,
Packit ae235b
                      const gchar *key)
Packit ae235b
{
Packit ae235b
  const struct gvdb_hash_item *item;
Packit ae235b
  GvdbTable *new;
Packit ae235b
Packit ae235b
  item = gvdb_table_lookup (file, key, 'H');
Packit ae235b
Packit ae235b
  if (item == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  new = g_slice_new0 (GvdbTable);
Packit ae235b
  new->user_data = file->ref_user_data ? file->ref_user_data (file->user_data) : file->user_data;
Packit ae235b
  new->ref_user_data = file->ref_user_data;
Packit ae235b
  new->unref_user_data = file->unref_user_data;
Packit ae235b
  new->byteswapped = file->byteswapped;
Packit ae235b
  new->trusted = file->trusted;
Packit ae235b
  new->data = file->data;
Packit ae235b
  new->size = file->size;
Packit ae235b
  new->ref_count = 1;
Packit ae235b
Packit ae235b
  gvdb_table_setup_root (new, &item->value.pointer);
Packit ae235b
Packit ae235b
  return new;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * gvdb_table_ref:
Packit ae235b
 * @file: a #GvdbTable
Packit ae235b
 *
Packit ae235b
 * Increases the reference count on @file.
Packit ae235b
 *
Packit ae235b
 * Returns: a new reference on @file
Packit ae235b
 **/
Packit ae235b
GvdbTable *
Packit ae235b
gvdb_table_ref (GvdbTable *file)
Packit ae235b
{
Packit ae235b
  g_atomic_int_inc (&file->ref_count);
Packit ae235b
Packit ae235b
  return file;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * gvdb_table_unref:
Packit ae235b
 * @file: a #GvdbTable
Packit ae235b
 *
Packit ae235b
 * Decreases the reference count on @file, possibly freeing it.
Packit ae235b
 *
Packit ae235b
 * Since: 2.26
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
gvdb_table_unref (GvdbTable *file)
Packit ae235b
{
Packit ae235b
  if (g_atomic_int_dec_and_test (&file->ref_count))
Packit ae235b
    {
Packit ae235b
      if (file->unref_user_data)
Packit ae235b
	file->unref_user_data (file->user_data);
Packit ae235b
      g_slice_free (GvdbTable, file);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * gvdb_table_is_valid:
Packit ae235b
 * @table: a #GvdbTable
Packit ae235b
 *
Packit ae235b
 * Checks if the table is still valid.
Packit ae235b
 *
Packit ae235b
 * An on-disk GVDB can be marked as invalid.  This happens when the file
Packit ae235b
 * has been replaced.  The appropriate action is typically to reopen the
Packit ae235b
 * file.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @table is still valid
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
gvdb_table_is_valid (GvdbTable *table)
Packit ae235b
{
Packit ae235b
  return !!*table->data;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * gvdb_table_walk:
Packit ae235b
 * @table: a #GvdbTable
Packit ae235b
 * @key: a key corresponding to a list
Packit ae235b
 * @open_func: the #GvdbWalkOpenFunc
Packit ae235b
 * @value_func: the #GvdbWalkValueFunc
Packit ae235b
 * @close_func: the #GvdbWalkCloseFunc
Packit ae235b
 * @user_data: data to pass to the callbacks
Packit ae235b
 *
Packit ae235b
 * Looks up the list at @key and iterate over the items in it.
Packit ae235b
 *
Packit ae235b
 * First, @open_func is called to signal that we are starting to iterate over
Packit ae235b
 * the list.  Then the list is iterated.  When all items in the list have been
Packit ae235b
 * iterated over, the @close_func is called.
Packit ae235b
 *
Packit ae235b
 * When iterating, if a given item in the list is a value then @value_func is
Packit ae235b
 * called.
Packit ae235b
 *
Packit ae235b
 * If a given item in the list is itself a list then @open_func is called.  If
Packit ae235b
 * that function returns %TRUE then the walk begins iterating the items in the
Packit ae235b
 * sublist, until there are no more items, at which point a matching
Packit ae235b
 * @close_func call is made.  If @open_func returns %FALSE then no iteration of
Packit ae235b
 * the sublist occurs and no corresponding @close_func call is made.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
gvdb_table_walk (GvdbTable         *table,
Packit ae235b
                 const gchar       *key,
Packit ae235b
                 GvdbWalkOpenFunc   open_func,
Packit ae235b
                 GvdbWalkValueFunc  value_func,
Packit ae235b
                 GvdbWalkCloseFunc  close_func,
Packit ae235b
                 gpointer           user_data)
Packit ae235b
{
Packit ae235b
  const struct gvdb_hash_item *item;
Packit ae235b
  const guint32_le *pointers[64];
Packit ae235b
  const guint32_le *enders[64];
Packit ae235b
  gsize name_lengths[64];
Packit ae235b
  gint index = 0;
Packit ae235b
Packit ae235b
  item = gvdb_table_lookup (table, key, 'L');
Packit ae235b
  name_lengths[0] = 0;
Packit ae235b
  pointers[0] = NULL;
Packit ae235b
  enders[0] = NULL;
Packit ae235b
  goto start_here;
Packit ae235b
Packit ae235b
  while (index)
Packit ae235b
    {
Packit ae235b
      close_func (name_lengths[index], user_data);
Packit ae235b
      index--;
Packit ae235b
Packit ae235b
      while (pointers[index] < enders[index])
Packit ae235b
        {
Packit ae235b
          const gchar *name;
Packit ae235b
          gsize name_len;
Packit ae235b
Packit ae235b
          item = gvdb_table_get_item (table, *pointers[index]++);
Packit ae235b
 start_here:
Packit ae235b
Packit ae235b
          if (item != NULL &&
Packit ae235b
              (name = gvdb_table_item_get_key (table, item, &name_len)))
Packit ae235b
            {
Packit ae235b
              if (item->type == 'L')
Packit ae235b
                {
Packit ae235b
                  if (open_func (name, name_len, user_data))
Packit ae235b
                    {
Packit ae235b
                      guint length = 0;
Packit ae235b
Packit ae235b
                      index++;
Packit ae235b
                      g_assert (index < 64);
Packit ae235b
Packit ae235b
                      gvdb_table_list_from_item (table, item,
Packit ae235b
                                                 &pointers[index],
Packit ae235b
                                                 &length);
Packit ae235b
                      enders[index] = pointers[index] + length;
Packit ae235b
                      name_lengths[index] = name_len;
Packit ae235b
                    }
Packit ae235b
                }
Packit ae235b
              else if (item->type == 'v')
Packit ae235b
                {
Packit ae235b
                  GVariant *value;
Packit ae235b
Packit ae235b
                  value = gvdb_table_value_from_item (table, item);
Packit ae235b
Packit ae235b
                  if (value != NULL)
Packit ae235b
                    {
Packit ae235b
                      if (table->byteswapped)
Packit ae235b
                        {
Packit ae235b
                          GVariant *tmp;
Packit ae235b
Packit ae235b
                          tmp = g_variant_byteswap (value);
Packit ae235b
                          g_variant_unref (value);
Packit ae235b
                          value = tmp;
Packit ae235b
                        }
Packit ae235b
Packit ae235b
                      value_func (name, name_len, value, user_data);
Packit ae235b
                      g_variant_unref (value);
Packit ae235b
                    }
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
}