gio/gvdb/: LGPLv2+ -> LGPLv2.1+
[dconf.git] / gvdb-reader.c
blobeb7a11c8354aaea35a52700f0a215e4dc3ab25fc
1 /*
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.1 of the License, 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"
25 #include <string.h>
27 struct _GvdbTable {
28 GBytes *bytes;
30 const gchar *data;
31 gsize size;
33 gboolean byteswapped;
34 gboolean trusted;
36 const guint32_le *bloom_words;
37 guint32 n_bloom_words;
38 guint bloom_shift;
40 const guint32_le *hash_buckets;
41 guint32 n_buckets;
43 struct gvdb_hash_item *hash_items;
44 guint32 n_hash_items;
47 static const gchar *
48 gvdb_table_item_get_key (GvdbTable *file,
49 const struct gvdb_hash_item *item,
50 gsize *size)
52 guint32 start, end;
54 start = guint32_from_le (item->key_start);
55 *size = guint16_from_le (item->key_size);
56 end = start + *size;
58 if G_UNLIKELY (start > end || end > file->size)
59 return NULL;
61 return file->data + start;
64 static gconstpointer
65 gvdb_table_dereference (GvdbTable *file,
66 const struct gvdb_pointer *pointer,
67 gint alignment,
68 gsize *size)
70 guint32 start, end;
72 start = guint32_from_le (pointer->start);
73 end = guint32_from_le (pointer->end);
75 if G_UNLIKELY (start > end || end > file->size || start & (alignment - 1))
76 return NULL;
78 *size = end - start;
80 return file->data + start;
83 static void
84 gvdb_table_setup_root (GvdbTable *file,
85 const struct gvdb_pointer *pointer)
87 const struct gvdb_hash_header *header;
88 guint32 n_bloom_words;
89 guint32 n_buckets;
90 gsize size;
92 header = gvdb_table_dereference (file, pointer, 4, &size);
94 if G_UNLIKELY (header == NULL || size < sizeof *header)
95 return;
97 size -= sizeof *header;
99 n_bloom_words = guint32_from_le (header->n_bloom_words);
100 n_buckets = guint32_from_le (header->n_buckets);
101 n_bloom_words &= (1u << 27) - 1;
103 if G_UNLIKELY (n_bloom_words * sizeof (guint32_le) > size)
104 return;
106 file->bloom_words = (gpointer) (header + 1);
107 size -= n_bloom_words * sizeof (guint32_le);
108 file->n_bloom_words = n_bloom_words;
110 if G_UNLIKELY (n_buckets > G_MAXUINT / sizeof (guint32_le) ||
111 n_buckets * sizeof (guint32_le) > size)
112 return;
114 file->hash_buckets = file->bloom_words + file->n_bloom_words;
115 size -= n_buckets * sizeof (guint32_le);
116 file->n_buckets = n_buckets;
118 if G_UNLIKELY (size % sizeof (struct gvdb_hash_item))
119 return;
121 file->hash_items = (gpointer) (file->hash_buckets + n_buckets);
122 file->n_hash_items = size / sizeof (struct gvdb_hash_item);
126 * gvdb_table_new_from_bytes:
127 * @bytes: the #GBytes with the data
128 * @trusted: if the contents of @bytes are trusted
129 * @error: %NULL, or a pointer to a %NULL #GError
130 * @returns: a new #GvdbTable
132 * Creates a new #GvdbTable from the contents of @bytes.
134 * This call can fail if the header contained in @bytes is invalid.
136 * You should call gvdb_table_free() on the return result when you no
137 * longer require it.
139 GvdbTable *
140 gvdb_table_new_from_bytes (GBytes *bytes,
141 gboolean trusted,
142 GError **error)
144 const struct gvdb_header *header;
145 GvdbTable *file;
147 file = g_slice_new0 (GvdbTable);
148 file->bytes = g_bytes_ref (bytes);
149 file->data = g_bytes_get_data (bytes, &file->size);
150 file->trusted = trusted;
152 if (file->size < sizeof (struct gvdb_header))
153 goto invalid;
155 header = (gpointer) file->data;
157 if (header->signature[0] == GVDB_SIGNATURE0 &&
158 header->signature[1] == GVDB_SIGNATURE1 &&
159 guint32_from_le (header->version) == 0)
160 file->byteswapped = FALSE;
162 else if (header->signature[0] == GVDB_SWAPPED_SIGNATURE0 &&
163 header->signature[1] == GVDB_SWAPPED_SIGNATURE1 &&
164 guint32_from_le (header->version) == 0)
165 file->byteswapped = TRUE;
167 else
168 goto invalid;
170 gvdb_table_setup_root (file, &header->root);
172 return file;
174 invalid:
175 g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, "invalid gvdb header");
177 g_bytes_unref (file->bytes);
179 g_slice_free (GvdbTable, file);
181 return NULL;
185 * gvdb_table_new:
186 * @filename: a filename
187 * @trusted: if the contents of @bytes are trusted
188 * @error: %NULL, or a pointer to a %NULL #GError
189 * @returns: a new #GvdbTable
191 * Creates a new #GvdbTable using the #GMappedFile for @filename as the
192 * #GBytes.
194 GvdbTable *
195 gvdb_table_new (const gchar *filename,
196 gboolean trusted,
197 GError **error)
199 GMappedFile *mapped;
200 GvdbTable *table;
201 GBytes *bytes;
203 mapped = g_mapped_file_new (filename, FALSE, error);
204 if (!mapped)
205 return NULL;
207 bytes = g_mapped_file_get_bytes (mapped);
208 table = gvdb_table_new_from_bytes (bytes, trusted, error);
209 g_mapped_file_unref (mapped);
210 g_bytes_unref (bytes);
212 g_prefix_error (error, "%s: ", filename);
214 return table;
217 static gboolean
218 gvdb_table_bloom_filter (GvdbTable *file,
219 guint32 hash_value)
221 guint32 word, mask;
223 if (file->n_bloom_words == 0)
224 return TRUE;
226 word = (hash_value / 32) % file->n_bloom_words;
227 mask = 1 << (hash_value & 31);
228 mask |= 1 << ((hash_value >> file->bloom_shift) & 31);
230 return (guint32_from_le (file->bloom_words[word]) & mask) == mask;
233 static gboolean
234 gvdb_table_check_name (GvdbTable *file,
235 struct gvdb_hash_item *item,
236 const gchar *key,
237 guint key_length)
239 const gchar *this_key;
240 gsize this_size;
241 guint32 parent;
243 this_key = gvdb_table_item_get_key (file, item, &this_size);
245 if G_UNLIKELY (this_key == NULL || this_size > key_length)
246 return FALSE;
248 key_length -= this_size;
250 if G_UNLIKELY (memcmp (this_key, key + key_length, this_size) != 0)
251 return FALSE;
253 parent = guint32_from_le (item->parent);
254 if (key_length == 0 && parent == 0xffffffffu)
255 return TRUE;
257 if G_LIKELY (parent < file->n_hash_items && this_size > 0)
258 return gvdb_table_check_name (file,
259 &file->hash_items[parent],
260 key, key_length);
262 return FALSE;
265 static const struct gvdb_hash_item *
266 gvdb_table_lookup (GvdbTable *file,
267 const gchar *key,
268 gchar type)
270 guint32 hash_value = 5381;
271 guint key_length;
272 guint32 bucket;
273 guint32 lastno;
274 guint32 itemno;
276 if G_UNLIKELY (file->n_buckets == 0 || file->n_hash_items == 0)
277 return NULL;
279 for (key_length = 0; key[key_length]; key_length++)
280 hash_value = (hash_value * 33) + ((signed char *) key)[key_length];
282 if (!gvdb_table_bloom_filter (file, hash_value))
283 return NULL;
285 bucket = hash_value % file->n_buckets;
286 itemno = guint32_from_le (file->hash_buckets[bucket]);
288 if (bucket == file->n_buckets - 1 ||
289 (lastno = guint32_from_le(file->hash_buckets[bucket + 1])) > file->n_hash_items)
290 lastno = file->n_hash_items;
292 while G_LIKELY (itemno < lastno)
294 struct gvdb_hash_item *item = &file->hash_items[itemno];
296 if (hash_value == guint32_from_le (item->hash_value))
297 if G_LIKELY (gvdb_table_check_name (file, item, key, key_length))
298 if G_LIKELY (item->type == type)
299 return item;
301 itemno++;
304 return NULL;
307 static gboolean
308 gvdb_table_list_from_item (GvdbTable *table,
309 const struct gvdb_hash_item *item,
310 const guint32_le **list,
311 guint *length)
313 gsize size;
315 *list = gvdb_table_dereference (table, &item->value.pointer, 4, &size);
317 if G_LIKELY (*list == NULL || size % 4)
318 return FALSE;
320 *length = size / 4;
322 return TRUE;
326 * gvdb_table_get_names:
327 * @table: a #GvdbTable
328 * @length: the number of items returned, or %NULL
330 * Gets a list of all names contained in @table.
332 * No call to gvdb_table_get_table(), gvdb_table_list() or
333 * gvdb_table_get_value() will succeed unless it is for one of the
334 * names returned by this function.
336 * Note that some names that are returned may still fail for all of the
337 * above calls in the case of the corrupted file. Note also that the
338 * returned strings may not be utf8.
340 * Returns: a %NULL-terminated list of strings, of length @length
342 gchar **
343 gvdb_table_get_names (GvdbTable *table,
344 gint *length)
346 gchar **names;
347 gint n_names;
348 gint filled;
349 gint total;
350 gint i;
352 /* We generally proceed by iterating over the list of items in the
353 * hash table (in order of appearance) recording them into an array.
355 * Each item has a parent item (except root items). The parent item
356 * forms part of the name of the item. We could go fetching the
357 * parent item chain at the point that we encounter each item but then
358 * we would need to implement some sort of recursion along with checks
359 * for self-referential items.
361 * Instead, we do a number of passes. Each pass will build up one
362 * level of names (starting from the root). We continue to do passes
363 * until no more items are left. The first pass will only add root
364 * items and each further pass will only add items whose direct parent
365 * is an item added in the immediately previous pass. It's also
366 * possible that items get filled if they follow their parent within a
367 * particular pass.
369 * At most we will have a number of passes equal to the depth of the
370 * tree. Self-referential items will never be filled in (since their
371 * parent will have never been filled in). We continue until we have
372 * a pass that fills in no additional items.
374 * This takes an O(n) algorithm and turns it into O(n*m) where m is
375 * the depth of the tree, but in all sane cases the tree won't be very
376 * deep and the constant factor of this algorithm is lower (and the
377 * complexity of coding it, as well).
380 n_names = table->n_hash_items;
381 names = g_new0 (gchar *, n_names + 1);
383 /* 'names' starts out all-NULL. On each pass we record the number
384 * of items changed from NULL to non-NULL in 'filled' so we know if we
385 * should repeat the loop. 'total' counts the total number of items
386 * filled. If 'total' ends up equal to 'n_names' then we know that
387 * 'names' has been completely filled.
390 total = 0;
393 /* Loop until we have filled no more entries */
394 filled = 0;
396 for (i = 0; i < n_names; i++)
398 const struct gvdb_hash_item *item = &table->hash_items[i];
399 const gchar *name;
400 gsize name_length;
401 guint32 parent;
403 /* already got it on a previous pass */
404 if (names[i] != NULL)
405 continue;
407 parent = guint32_from_le (item->parent);
409 if (parent == 0xffffffffu)
411 /* it's a root item */
412 name = gvdb_table_item_get_key (table, item, &name_length);
414 if (name != NULL)
416 names[i] = g_strndup (name, name_length);
417 filled++;
421 else if (parent < n_names && names[parent] != NULL)
423 /* It's a non-root item whose parent was filled in already.
425 * Calculate the name of this item by combining it with
426 * its parent name.
428 name = gvdb_table_item_get_key (table, item, &name_length);
430 if (name != NULL)
432 const gchar *parent_name = names[parent];
433 gsize parent_length;
434 gchar *fullname;
436 parent_length = strlen (parent_name);
437 fullname = g_malloc (parent_length + name_length + 1);
438 memcpy (fullname, parent_name, parent_length);
439 memcpy (fullname + parent_length, name, name_length);
440 fullname[parent_length + name_length] = '\0';
441 names[i] = fullname;
442 filled++;
447 total += filled;
449 while (filled && total < n_names);
451 /* If the table was corrupted then 'names' may have holes in it.
452 * Collapse those.
454 if G_UNLIKELY (total != n_names)
456 GPtrArray *fixed_names;
458 fixed_names = g_ptr_array_new ();
459 for (i = 0; i < n_names; i++)
460 if (names[i] != NULL)
461 g_ptr_array_add (fixed_names, names[i]);
463 g_free (names);
464 n_names = fixed_names->len;
465 g_ptr_array_add (fixed_names, NULL);
466 names = (gchar **) g_ptr_array_free (fixed_names, FALSE);
469 if (length)
470 *length = n_names;
472 return names;
476 * gvdb_table_list:
477 * @file: a #GvdbTable
478 * @key: a string
479 * @returns: a %NULL-terminated string array
481 * List all of the keys that appear below @key. The nesting of keys
482 * within the hash file is defined by the program that created the hash
483 * file. One thing is constant: each item in the returned array can be
484 * concatenated to @key to obtain the full name of that key.
486 * It is not possible to tell from this function if a given key is
487 * itself a path, a value, or another hash table; you are expected to
488 * know this for yourself.
490 * You should call g_strfreev() on the return result when you no longer
491 * require it.
493 gchar **
494 gvdb_table_list (GvdbTable *file,
495 const gchar *key)
497 const struct gvdb_hash_item *item;
498 const guint32_le *list;
499 gchar **strv;
500 guint length;
501 guint i;
503 if ((item = gvdb_table_lookup (file, key, 'L')) == NULL)
504 return NULL;
506 if (!gvdb_table_list_from_item (file, item, &list, &length))
507 return NULL;
509 strv = g_new (gchar *, length + 1);
510 for (i = 0; i < length; i++)
512 guint32 itemno = guint32_from_le (list[i]);
514 if (itemno < file->n_hash_items)
516 const struct gvdb_hash_item *item;
517 const gchar *string;
518 gsize strsize;
520 item = file->hash_items + itemno;
522 string = gvdb_table_item_get_key (file, item, &strsize);
524 if (string != NULL)
525 strv[i] = g_strndup (string, strsize);
526 else
527 strv[i] = g_malloc0 (1);
529 else
530 strv[i] = g_malloc0 (1);
533 strv[i] = NULL;
535 return strv;
539 * gvdb_table_has_value:
540 * @file: a #GvdbTable
541 * @key: a string
542 * @returns: %TRUE if @key is in the table
544 * Checks for a value named @key in @file.
546 * Note: this function does not consider non-value nodes (other hash
547 * tables, for example).
549 gboolean
550 gvdb_table_has_value (GvdbTable *file,
551 const gchar *key)
553 static const struct gvdb_hash_item *item;
554 gsize size;
556 item = gvdb_table_lookup (file, key, 'v');
558 if (item == NULL)
559 return FALSE;
561 return gvdb_table_dereference (file, &item->value.pointer, 8, &size) != NULL;
564 static GVariant *
565 gvdb_table_value_from_item (GvdbTable *table,
566 const struct gvdb_hash_item *item)
568 GVariant *variant, *value;
569 gconstpointer data;
570 GBytes *bytes;
571 gsize size;
573 data = gvdb_table_dereference (table, &item->value.pointer, 8, &size);
575 if G_UNLIKELY (data == NULL)
576 return NULL;
578 bytes = g_bytes_new_from_bytes (table->bytes, ((gchar *) data) - table->data, size);
579 variant = g_variant_new_from_bytes (G_VARIANT_TYPE_VARIANT, bytes, table->trusted);
580 value = g_variant_get_variant (variant);
581 g_variant_unref (variant);
582 g_bytes_unref (bytes);
584 return value;
588 * gvdb_table_get_value:
589 * @file: a #GvdbTable
590 * @key: a string
591 * @returns: a #GVariant, or %NULL
593 * Looks up a value named @key in @file.
595 * If the value is not found then %NULL is returned. Otherwise, a new
596 * #GVariant instance is returned. The #GVariant does not depend on the
597 * continued existence of @file.
599 * You should call g_variant_unref() on the return result when you no
600 * longer require it.
602 GVariant *
603 gvdb_table_get_value (GvdbTable *file,
604 const gchar *key)
606 const struct gvdb_hash_item *item;
607 GVariant *value;
609 if ((item = gvdb_table_lookup (file, key, 'v')) == NULL)
610 return NULL;
612 value = gvdb_table_value_from_item (file, item);
614 if (value && file->byteswapped)
616 GVariant *tmp;
618 tmp = g_variant_byteswap (value);
619 g_variant_unref (value);
620 value = tmp;
623 return value;
627 * gvdb_table_get_raw_value:
628 * @table: a #GvdbTable
629 * @key: a string
630 * @returns: a #GVariant, or %NULL
632 * Looks up a value named @key in @file.
634 * This call is equivalent to gvdb_table_get_value() except that it
635 * never byteswaps the value.
637 GVariant *
638 gvdb_table_get_raw_value (GvdbTable *table,
639 const gchar *key)
641 const struct gvdb_hash_item *item;
643 if ((item = gvdb_table_lookup (table, key, 'v')) == NULL)
644 return NULL;
646 return gvdb_table_value_from_item (table, item);
650 * gvdb_table_get_table:
651 * @file: a #GvdbTable
652 * @key: a string
653 * @returns: a new #GvdbTable, or %NULL
655 * Looks up the hash table named @key in @file.
657 * The toplevel hash table in a #GvdbTable can contain reference to
658 * child hash tables (and those can contain further references...).
660 * If @key is not found in @file then %NULL is returned. Otherwise, a
661 * new #GvdbTable is returned, referring to the child hashtable as
662 * contained in the file. This newly-created #GvdbTable does not depend
663 * on the continued existence of @file.
665 * You should call gvdb_table_free() on the return result when you no
666 * longer require it.
668 GvdbTable *
669 gvdb_table_get_table (GvdbTable *file,
670 const gchar *key)
672 const struct gvdb_hash_item *item;
673 GvdbTable *new;
675 item = gvdb_table_lookup (file, key, 'H');
677 if (item == NULL)
678 return NULL;
680 new = g_slice_new0 (GvdbTable);
681 new->bytes = g_bytes_ref (file->bytes);
682 new->byteswapped = file->byteswapped;
683 new->trusted = file->trusted;
684 new->data = file->data;
685 new->size = file->size;
687 gvdb_table_setup_root (new, &item->value.pointer);
689 return new;
693 * gvdb_table_free:
694 * @file: a #GvdbTable
696 * Frees @file.
698 void
699 gvdb_table_free (GvdbTable *file)
701 g_bytes_unref (file->bytes);
702 g_slice_free (GvdbTable, file);
706 * gvdb_table_is_valid:
707 * @table: a #GvdbTable
708 * @returns: %TRUE if @table is still valid
710 * Checks if the table is still valid.
712 * An on-disk GVDB can be marked as invalid. This happens when the file
713 * has been replaced. The appropriate action is typically to reopen the
714 * file.
716 gboolean
717 gvdb_table_is_valid (GvdbTable *table)
719 return !!*table->data;