2 * Copyright © 2010 Codethink Limited
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser 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.
19 * Author: Ryan Lortie <desrt@desrt.ca>
22 #include "gvdb-reader.h"
23 #include "gvdb-format.h"
37 const guint32_le *bloom_words;
38 guint32 n_bloom_words;
41 const guint32_le *hash_buckets;
44 struct gvdb_hash_item *hash_items;
49 gvdb_table_item_get_key (GvdbTable *file,
50 const struct gvdb_hash_item *item,
55 start = guint32_from_le (item->key_start);
56 *size = guint16_from_le (item->key_size);
59 if G_UNLIKELY (start > end || end > file->size)
62 return file->data + start;
66 gvdb_table_dereference (GvdbTable *file,
67 const struct gvdb_pointer *pointer,
73 start = guint32_from_le (pointer->start);
74 end = guint32_from_le (pointer->end);
76 if G_UNLIKELY (start > end || end > file->size || start & (alignment - 1))
81 return file->data + start;
85 gvdb_table_setup_root (GvdbTable *file,
86 const struct gvdb_pointer *pointer)
88 const struct gvdb_hash_header *header;
89 guint32 n_bloom_words;
94 header = gvdb_table_dereference (file, pointer, 4, &size);
96 if G_UNLIKELY (header == NULL || size < sizeof *header)
99 size -= sizeof *header;
101 n_bloom_words = guint32_from_le (header->n_bloom_words);
102 n_buckets = guint32_from_le (header->n_buckets);
103 bloom_shift = n_bloom_words >> 27;
104 n_bloom_words &= (1u << 27) - 1;
106 if G_UNLIKELY (n_bloom_words * sizeof (guint32_le) > size)
109 file->bloom_words = (gpointer) (header + 1);
110 size -= n_bloom_words * sizeof (guint32_le);
111 file->n_bloom_words = n_bloom_words;
113 if G_UNLIKELY (n_buckets > G_MAXUINT / sizeof (guint32_le) ||
114 n_buckets * sizeof (guint32_le) > size)
117 file->hash_buckets = file->bloom_words + file->n_bloom_words;
118 size -= n_buckets * sizeof (guint32_le);
119 file->n_buckets = n_buckets;
121 if G_UNLIKELY (size % sizeof (struct gvdb_hash_item))
124 file->hash_items = (gpointer) (file->hash_buckets + n_buckets);
125 file->n_hash_items = size / sizeof (struct gvdb_hash_item);
130 * @filename: the path to the hash file
131 * @trusted: if the contents of @filename are trusted
132 * @error: %NULL, or a pointer to a %NULL #GError
133 * @returns: a new #GvdbTable
135 * Creates a new #GvdbTable from the contents of the file found at
138 * The only time this function fails is if the file cannot be opened.
139 * In that case, the #GError that is returned will be an error from
140 * g_mapped_file_new().
142 * An empty or otherwise corrupted file is considered to be a valid
143 * #GvdbTable with no entries.
145 * You should call gvdb_table_unref() on the return result when you no
149 gvdb_table_new (const gchar *filename,
156 if ((mapped = g_mapped_file_new (filename, FALSE, error)) == NULL)
159 file = g_slice_new0 (GvdbTable);
160 file->data = g_mapped_file_get_contents (mapped);
161 file->size = g_mapped_file_get_length (mapped);
162 file->trusted = trusted;
163 file->mapped = mapped;
166 if (sizeof (struct gvdb_header) <= file->size)
168 const struct gvdb_header *header = (gpointer) file->data;
170 if (header->signature[0] == GVDB_SIGNATURE0 &&
171 header->signature[1] == GVDB_SIGNATURE1 &&
172 guint32_from_le (header->version) == 0)
173 file->byteswapped = FALSE;
175 else if (header->signature[0] == GVDB_SWAPPED_SIGNATURE0 &&
176 header->signature[1] == GVDB_SWAPPED_SIGNATURE1 &&
177 guint32_from_le (header->version) == 0)
178 file->byteswapped = TRUE;
182 g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL,
183 "%s: invalid header", filename);
184 g_slice_free (GvdbTable, file);
185 g_mapped_file_unref (mapped);
190 gvdb_table_setup_root (file, &header->root);
197 gvdb_table_bloom_filter (GvdbTable *file,
202 if (file->n_bloom_words == 0)
205 word = (hash_value / 32) % file->n_bloom_words;
206 mask = 1 << (hash_value & 31);
207 mask |= 1 << ((hash_value >> file->bloom_shift) & 31);
209 return (guint32_from_le (file->bloom_words[word]) & mask) == mask;
213 gvdb_table_check_name (GvdbTable *file,
214 struct gvdb_hash_item *item,
218 const gchar *this_key;
222 this_key = gvdb_table_item_get_key (file, item, &this_size);
224 if G_UNLIKELY (this_key == NULL || this_size > key_length)
227 key_length -= this_size;
229 if G_UNLIKELY (memcmp (this_key, key + key_length, this_size) != 0)
232 parent = guint32_from_le (item->parent);
233 if (key_length == 0 && parent == 0xffffffffu)
236 if G_LIKELY (parent < file->n_hash_items && this_size > 0)
237 return gvdb_table_check_name (file,
238 &file->hash_items[parent],
244 static const struct gvdb_hash_item *
245 gvdb_table_lookup (GvdbTable *file,
249 guint32 hash_value = 5381;
255 if G_UNLIKELY (file->n_buckets == 0 || file->n_hash_items == 0)
258 for (key_length = 0; key[key_length]; key_length++)
259 hash_value = (hash_value * 33) + key[key_length];
261 if (!gvdb_table_bloom_filter (file, hash_value))
264 bucket = hash_value % file->n_buckets;
265 itemno = guint32_from_le (file->hash_buckets[bucket]);
267 if (bucket == file->n_buckets - 1 ||
268 (lastno = guint32_from_le(file->hash_buckets[bucket + 1])) > file->n_hash_items)
269 lastno = file->n_hash_items;
271 while G_LIKELY (itemno < lastno)
273 struct gvdb_hash_item *item = &file->hash_items[itemno];
275 if (hash_value == guint32_from_le (item->hash_value))
276 if G_LIKELY (gvdb_table_check_name (file, item, key, key_length))
277 if G_LIKELY (item->type == type)
286 static const struct gvdb_hash_item *
287 gvdb_table_get_item (GvdbTable *table,
290 guint32 item_no_native = guint32_from_le (item_no);
292 if G_LIKELY (item_no_native < table->n_hash_items)
293 return table->hash_items + item_no_native;
299 gvdb_table_list_from_item (GvdbTable *table,
300 const struct gvdb_hash_item *item,
301 const guint32_le **list,
306 *list = gvdb_table_dereference (table, &item->value.pointer, 4, &size);
308 if G_LIKELY (*list == NULL || size % 4)
319 * @file: a #GvdbTable
321 * @returns: a %NULL-terminated string array
323 * List all of the keys that appear below @key. The nesting of keys
324 * within the hash file is defined by the program that created the hash
325 * file. One thing is constant: each item in the returned array can be
326 * concatenated to @key to obtain the full name of that key.
328 * It is not possible to tell from this function if a given key is
329 * itself a path, a value, or another hash table; you are expected to
330 * know this for yourself.
332 * You should call g_strfreev() on the return result when you no longer
336 gvdb_table_list (GvdbTable *file,
339 const struct gvdb_hash_item *item;
340 const guint32_le *list;
345 if ((item = gvdb_table_lookup (file, key, 'L')) == NULL)
348 if (!gvdb_table_list_from_item (file, item, &list, &length))
351 strv = g_new (gchar *, length + 1);
352 for (i = 0; i < length; i++)
354 guint32 itemno = guint32_from_le (list[i]);
356 if (itemno < file->n_hash_items)
358 const struct gvdb_hash_item *item;
362 item = file->hash_items + itemno;
364 string = gvdb_table_item_get_key (file, item, &strsize);
367 strv[i] = g_strndup (string, strsize);
369 strv[i] = g_malloc0 (1);
372 strv[i] = g_malloc0 (1);
381 * gvdb_table_has_value:
382 * @file: a #GvdbTable
384 * @returns: %TRUE if @key is in the table
386 * Checks for a value named @key in @file.
388 * Note: this function does not consider non-value nodes (other hash
389 * tables, for example).
392 gvdb_table_has_value (GvdbTable *file,
395 return gvdb_table_lookup (file, key, 'v') != NULL;
399 gvdb_table_value_from_item (GvdbTable *table,
400 const struct gvdb_hash_item *item)
402 GVariant *variant, *value;
406 data = gvdb_table_dereference (table, &item->value.pointer, 8, &size);
408 if G_UNLIKELY (data == NULL)
411 variant = g_variant_new_from_data (G_VARIANT_TYPE_VARIANT,
412 data, size, table->trusted,
413 (GDestroyNotify) g_mapped_file_unref,
414 g_mapped_file_ref (table->mapped));
415 value = g_variant_get_variant (variant);
416 g_variant_unref (variant);
422 * gvdb_table_get_value:
423 * @file: a #GvdbTable
425 * @returns: a #GVariant, or %NULL
427 * Looks up a value named @key in @file.
429 * If the value is not found then %NULL is returned. Otherwise, a new
430 * #GVariant instance is returned. The #GVariant does not depend on the
431 * continued existence of @file.
433 * You should call g_variant_unref() on the return result when you no
437 gvdb_table_get_value (GvdbTable *file,
440 const struct gvdb_hash_item *item;
443 if ((item = gvdb_table_lookup (file, key, 'v')) == NULL)
446 value = gvdb_table_value_from_item (file, item);
448 if (value && file->byteswapped)
452 tmp = g_variant_byteswap (value);
453 g_variant_unref (value);
461 * gvdb_table_get_raw_value:
462 * @table: a #GvdbTable
464 * @returns: a #GVariant, or %NULL
466 * Looks up a value named @key in @file.
468 * This call is equivalent to gvdb_table_get_value() except that it
469 * never byteswaps the value.
472 gvdb_table_get_raw_value (GvdbTable *table,
475 const struct gvdb_hash_item *item;
477 if ((item = gvdb_table_lookup (table, key, 'v')) == NULL)
480 return gvdb_table_value_from_item (table, item);
484 * gvdb_table_get_table:
485 * @file: a #GvdbTable
487 * @returns: a new #GvdbTable, or %NULL
489 * Looks up the hash table named @key in @file.
491 * The toplevel hash table in a #GvdbTable can contain reference to
492 * child hash tables (and those can contain further references...).
494 * If @key is not found in @file then %NULL is returned. Otherwise, a
495 * new #GvdbTable is returned, referring to the child hashtable as
496 * contained in the file. This newly-created #GvdbTable does not depend
497 * on the continued existence of @file.
499 * You should call gvdb_table_unref() on the return result when you no
503 gvdb_table_get_table (GvdbTable *file,
506 const struct gvdb_hash_item *item;
509 item = gvdb_table_lookup (file, key, 'H');
514 new = g_slice_new0 (GvdbTable);
515 new->mapped = g_mapped_file_ref (file->mapped);
516 new->byteswapped = file->byteswapped;
517 new->trusted = file->trusted;
518 new->data = file->data;
519 new->size = file->size;
522 gvdb_table_setup_root (new, &item->value.pointer);
529 * @file: a #GvdbTable
530 * @returns: a new reference on @file
532 * Increases the reference count on @file.
535 gvdb_table_ref (GvdbTable *file)
537 g_atomic_int_inc (&file->ref_count);
544 * @file: a #GvdbTable
546 * Decreases the reference count on @file, possibly freeing it.
551 gvdb_table_unref (GvdbTable *file)
553 if (g_atomic_int_dec_and_test (&file->ref_count))
555 g_mapped_file_unref (file->mapped);
556 g_slice_free (GvdbTable, file);
561 * gvdb_table_is_valid:
562 * @table: a #GvdbTable
563 * @returns: %TRUE if @table is still valid
565 * Checks if the table is still valid.
567 * An on-disk GVDB can be marked as invalid. This happens when the file
568 * has been replaced. The appropriate action is typically to reopen the
572 gvdb_table_is_valid (GvdbTable *table)
574 return !!*table->data;
579 * @table: a #GvdbTable
580 * @key: a key corresponding to a list
581 * @open_func: the #GvdbWalkOpenFunc
582 * @value_func: the #GvdbWalkValueFunc
583 * @close_func: the #GvdbWalkCloseFunc
584 * @user_data: data to pass to the callbacks
586 * Looks up the list at @key and iterate over the items in it.
588 * First, @open_func is called to signal that we are starting to iterate over
589 * the list. Then the list is iterated. When all items in the list have been
590 * iterated over, the @close_func is called.
592 * When iterating, if a given item in the list is a value then @value_func is
595 * If a given item in the list is itself a list then @open_func is called. If
596 * that function returns %TRUE then the walk begins iterating the items in the
597 * sublist, until there are no more items, at which point a matching
598 * @close_func call is made. If @open_func returns %FALSE then no iteration of
599 * the sublist occurs and no corresponding @close_func call is made.
602 gvdb_table_walk (GvdbTable *table,
604 GvdbWalkOpenFunc open_func,
605 GvdbWalkValueFunc value_func,
606 GvdbWalkCloseFunc close_func,
609 const struct gvdb_hash_item *item;
610 const guint32_le *pointers[64];
611 const guint32_le *enders[64];
612 gsize name_lengths[64];
615 item = gvdb_table_lookup (table, key, 'L');
623 close_func (name_lengths[index], user_data);
626 while (pointers[index] < enders[index])
631 item = gvdb_table_get_item (table, *pointers[index]++);
635 (name = gvdb_table_item_get_key (table, item, &name_len)))
637 if (item->type == 'L')
639 if (open_func (name, name_len, user_data))
644 g_assert (index < 64);
646 gvdb_table_list_from_item (table, item,
649 enders[index] = pointers[index] + length;
650 name_lengths[index] = name_len;
653 else if (item->type == 'v')
657 value = gvdb_table_value_from_item (table, item);
661 if (table->byteswapped)
665 tmp = g_variant_byteswap (value);
666 g_variant_unref (value);
670 value_func (name, name_len, value, user_data);
671 g_variant_unref (value);