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;
93 header = gvdb_table_dereference (file, pointer, 4, &size);
95 if G_UNLIKELY (header == NULL || size < sizeof *header)
98 size -= sizeof *header;
100 n_bloom_words = guint32_from_le (header->n_bloom_words);
101 n_buckets = guint32_from_le (header->n_buckets);
102 n_bloom_words &= (1u << 27) - 1;
104 if G_UNLIKELY (n_bloom_words * sizeof (guint32_le) > size)
107 file->bloom_words = (gpointer) (header + 1);
108 size -= n_bloom_words * sizeof (guint32_le);
109 file->n_bloom_words = n_bloom_words;
111 if G_UNLIKELY (n_buckets > G_MAXUINT / sizeof (guint32_le) ||
112 n_buckets * sizeof (guint32_le) > size)
115 file->hash_buckets = file->bloom_words + file->n_bloom_words;
116 size -= n_buckets * sizeof (guint32_le);
117 file->n_buckets = n_buckets;
119 if G_UNLIKELY (size % sizeof (struct gvdb_hash_item))
122 file->hash_items = (gpointer) (file->hash_buckets + n_buckets);
123 file->n_hash_items = size / sizeof (struct gvdb_hash_item);
128 * @filename: the path to the hash file
129 * @trusted: if the contents of @filename are trusted
130 * @error: %NULL, or a pointer to a %NULL #GError
131 * @returns: a new #GvdbTable
133 * Creates a new #GvdbTable from the contents of the file found at
136 * The only time this function fails is if the file cannot be opened.
137 * In that case, the #GError that is returned will be an error from
138 * g_mapped_file_new().
140 * An empty or otherwise corrupted file is considered to be a valid
141 * #GvdbTable with no entries.
143 * You should call gvdb_table_unref() on the return result when you no
147 gvdb_table_new (const gchar *filename,
154 if ((mapped = g_mapped_file_new (filename, FALSE, error)) == NULL)
157 file = g_slice_new0 (GvdbTable);
158 file->data = g_mapped_file_get_contents (mapped);
159 file->size = g_mapped_file_get_length (mapped);
160 file->trusted = trusted;
161 file->mapped = mapped;
164 if (sizeof (struct gvdb_header) <= file->size)
166 const struct gvdb_header *header = (gpointer) file->data;
168 if (header->signature[0] == GVDB_SIGNATURE0 &&
169 header->signature[1] == GVDB_SIGNATURE1 &&
170 guint32_from_le (header->version) == 0)
171 file->byteswapped = FALSE;
173 else if (header->signature[0] == GVDB_SWAPPED_SIGNATURE0 &&
174 header->signature[1] == GVDB_SWAPPED_SIGNATURE1 &&
175 guint32_from_le (header->version) == 0)
176 file->byteswapped = TRUE;
180 g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL,
181 "%s: invalid header", filename);
182 g_slice_free (GvdbTable, file);
183 g_mapped_file_unref (mapped);
188 gvdb_table_setup_root (file, &header->root);
195 gvdb_table_bloom_filter (GvdbTable *file,
200 if (file->n_bloom_words == 0)
203 word = (hash_value / 32) % file->n_bloom_words;
204 mask = 1 << (hash_value & 31);
205 mask |= 1 << ((hash_value >> file->bloom_shift) & 31);
207 return (guint32_from_le (file->bloom_words[word]) & mask) == mask;
211 gvdb_table_check_name (GvdbTable *file,
212 struct gvdb_hash_item *item,
216 const gchar *this_key;
220 this_key = gvdb_table_item_get_key (file, item, &this_size);
222 if G_UNLIKELY (this_key == NULL || this_size > key_length)
225 key_length -= this_size;
227 if G_UNLIKELY (memcmp (this_key, key + key_length, this_size) != 0)
230 parent = guint32_from_le (item->parent);
231 if (key_length == 0 && parent == 0xffffffffu)
234 if G_LIKELY (parent < file->n_hash_items && this_size > 0)
235 return gvdb_table_check_name (file,
236 &file->hash_items[parent],
242 static const struct gvdb_hash_item *
243 gvdb_table_lookup (GvdbTable *file,
247 guint32 hash_value = 5381;
253 if G_UNLIKELY (file->n_buckets == 0 || file->n_hash_items == 0)
256 for (key_length = 0; key[key_length]; key_length++)
257 hash_value = (hash_value * 33) + key[key_length];
259 if (!gvdb_table_bloom_filter (file, hash_value))
262 bucket = hash_value % file->n_buckets;
263 itemno = guint32_from_le (file->hash_buckets[bucket]);
265 if (bucket == file->n_buckets - 1 ||
266 (lastno = guint32_from_le(file->hash_buckets[bucket + 1])) > file->n_hash_items)
267 lastno = file->n_hash_items;
269 while G_LIKELY (itemno < lastno)
271 struct gvdb_hash_item *item = &file->hash_items[itemno];
273 if (hash_value == guint32_from_le (item->hash_value))
274 if G_LIKELY (gvdb_table_check_name (file, item, key, key_length))
275 if G_LIKELY (item->type == type)
284 static const struct gvdb_hash_item *
285 gvdb_table_get_item (GvdbTable *table,
288 guint32 item_no_native = guint32_from_le (item_no);
290 if G_LIKELY (item_no_native < table->n_hash_items)
291 return table->hash_items + item_no_native;
297 gvdb_table_list_from_item (GvdbTable *table,
298 const struct gvdb_hash_item *item,
299 const guint32_le **list,
304 *list = gvdb_table_dereference (table, &item->value.pointer, 4, &size);
306 if G_LIKELY (*list == NULL || size % 4)
317 * @file: a #GvdbTable
319 * @returns: a %NULL-terminated string array
321 * List all of the keys that appear below @key. The nesting of keys
322 * within the hash file is defined by the program that created the hash
323 * file. One thing is constant: each item in the returned array can be
324 * concatenated to @key to obtain the full name of that key.
326 * It is not possible to tell from this function if a given key is
327 * itself a path, a value, or another hash table; you are expected to
328 * know this for yourself.
330 * You should call g_strfreev() on the return result when you no longer
334 gvdb_table_list (GvdbTable *file,
337 const struct gvdb_hash_item *item;
338 const guint32_le *list;
343 if ((item = gvdb_table_lookup (file, key, 'L')) == NULL)
346 if (!gvdb_table_list_from_item (file, item, &list, &length))
349 strv = g_new (gchar *, length + 1);
350 for (i = 0; i < length; i++)
352 guint32 itemno = guint32_from_le (list[i]);
354 if (itemno < file->n_hash_items)
356 const struct gvdb_hash_item *item;
360 item = file->hash_items + itemno;
362 string = gvdb_table_item_get_key (file, item, &strsize);
365 strv[i] = g_strndup (string, strsize);
367 strv[i] = g_malloc0 (1);
370 strv[i] = g_malloc0 (1);
379 * gvdb_table_has_value:
380 * @file: a #GvdbTable
382 * @returns: %TRUE if @key is in the table
384 * Checks for a value named @key in @file.
386 * Note: this function does not consider non-value nodes (other hash
387 * tables, for example).
390 gvdb_table_has_value (GvdbTable *file,
393 return gvdb_table_lookup (file, key, 'v') != NULL;
397 gvdb_table_value_from_item (GvdbTable *table,
398 const struct gvdb_hash_item *item)
400 GVariant *variant, *value;
404 data = gvdb_table_dereference (table, &item->value.pointer, 8, &size);
406 if G_UNLIKELY (data == NULL)
409 variant = g_variant_new_from_data (G_VARIANT_TYPE_VARIANT,
410 data, size, table->trusted,
411 (GDestroyNotify) g_mapped_file_unref,
412 g_mapped_file_ref (table->mapped));
413 value = g_variant_get_variant (variant);
414 g_variant_unref (variant);
420 * gvdb_table_get_value:
421 * @file: a #GvdbTable
423 * @returns: a #GVariant, or %NULL
425 * Looks up a value named @key in @file.
427 * If the value is not found then %NULL is returned. Otherwise, a new
428 * #GVariant instance is returned. The #GVariant does not depend on the
429 * continued existence of @file.
431 * You should call g_variant_unref() on the return result when you no
435 gvdb_table_get_value (GvdbTable *file,
438 const struct gvdb_hash_item *item;
441 if ((item = gvdb_table_lookup (file, key, 'v')) == NULL)
444 value = gvdb_table_value_from_item (file, item);
446 if (value && file->byteswapped)
450 tmp = g_variant_byteswap (value);
451 g_variant_unref (value);
459 * gvdb_table_get_raw_value:
460 * @table: a #GvdbTable
462 * @returns: a #GVariant, or %NULL
464 * Looks up a value named @key in @file.
466 * This call is equivalent to gvdb_table_get_value() except that it
467 * never byteswaps the value.
470 gvdb_table_get_raw_value (GvdbTable *table,
473 const struct gvdb_hash_item *item;
475 if ((item = gvdb_table_lookup (table, key, 'v')) == NULL)
478 return gvdb_table_value_from_item (table, item);
482 * gvdb_table_get_table:
483 * @file: a #GvdbTable
485 * @returns: a new #GvdbTable, or %NULL
487 * Looks up the hash table named @key in @file.
489 * The toplevel hash table in a #GvdbTable can contain reference to
490 * child hash tables (and those can contain further references...).
492 * If @key is not found in @file then %NULL is returned. Otherwise, a
493 * new #GvdbTable is returned, referring to the child hashtable as
494 * contained in the file. This newly-created #GvdbTable does not depend
495 * on the continued existence of @file.
497 * You should call gvdb_table_unref() on the return result when you no
501 gvdb_table_get_table (GvdbTable *file,
504 const struct gvdb_hash_item *item;
507 item = gvdb_table_lookup (file, key, 'H');
512 new = g_slice_new0 (GvdbTable);
513 new->mapped = g_mapped_file_ref (file->mapped);
514 new->byteswapped = file->byteswapped;
515 new->trusted = file->trusted;
516 new->data = file->data;
517 new->size = file->size;
520 gvdb_table_setup_root (new, &item->value.pointer);
527 * @file: a #GvdbTable
528 * @returns: a new reference on @file
530 * Increases the reference count on @file.
533 gvdb_table_ref (GvdbTable *file)
535 g_atomic_int_inc (&file->ref_count);
542 * @file: a #GvdbTable
544 * Decreases the reference count on @file, possibly freeing it.
549 gvdb_table_unref (GvdbTable *file)
551 if (g_atomic_int_dec_and_test (&file->ref_count))
553 g_mapped_file_unref (file->mapped);
554 g_slice_free (GvdbTable, file);
559 * gvdb_table_is_valid:
560 * @table: a #GvdbTable
561 * @returns: %TRUE if @table is still valid
563 * Checks if the table is still valid.
565 * An on-disk GVDB can be marked as invalid. This happens when the file
566 * has been replaced. The appropriate action is typically to reopen the
570 gvdb_table_is_valid (GvdbTable *table)
572 return !!*table->data;
577 * @table: a #GvdbTable
578 * @key: a key corresponding to a list
579 * @open_func: the #GvdbWalkOpenFunc
580 * @value_func: the #GvdbWalkValueFunc
581 * @close_func: the #GvdbWalkCloseFunc
582 * @user_data: data to pass to the callbacks
584 * Looks up the list at @key and iterate over the items in it.
586 * First, @open_func is called to signal that we are starting to iterate over
587 * the list. Then the list is iterated. When all items in the list have been
588 * iterated over, the @close_func is called.
590 * When iterating, if a given item in the list is a value then @value_func is
593 * If a given item in the list is itself a list then @open_func is called. If
594 * that function returns %TRUE then the walk begins iterating the items in the
595 * sublist, until there are no more items, at which point a matching
596 * @close_func call is made. If @open_func returns %FALSE then no iteration of
597 * the sublist occurs and no corresponding @close_func call is made.
600 gvdb_table_walk (GvdbTable *table,
602 GvdbWalkOpenFunc open_func,
603 GvdbWalkValueFunc value_func,
604 GvdbWalkCloseFunc close_func,
607 const struct gvdb_hash_item *item;
608 const guint32_le *pointers[64];
609 const guint32_le *enders[64];
610 gsize name_lengths[64];
613 item = gvdb_table_lookup (table, key, 'L');
621 close_func (name_lengths[index], user_data);
624 while (pointers[index] < enders[index])
629 item = gvdb_table_get_item (table, *pointers[index]++);
633 (name = gvdb_table_item_get_key (table, item, &name_len)))
635 if (item->type == 'L')
637 if (open_func (name, name_len, user_data))
642 g_assert (index < 64);
644 gvdb_table_list_from_item (table, item,
647 enders[index] = pointers[index] + length;
648 name_lengths[index] = name_len;
651 else if (item->type == 'v')
655 value = gvdb_table_value_from_item (table, item);
659 if (table->byteswapped)
663 tmp = g_variant_byteswap (value);
664 g_variant_unref (value);
668 value_func (name, name_len, value, user_data);
669 g_variant_unref (value);