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, see <http://www.gnu.org/licenses/>.
17 * Author: Ryan Lortie <desrt@desrt.ca>
20 #include "gvdb-builder.h"
21 #include "gvdb-format.h"
25 #if !defined(G_OS_WIN32) || !defined(_MSC_VER)
35 guint32_le assigned_index;
53 gvdb_item_free (gpointer data)
55 GvdbItem *item = data;
60 g_variant_unref (item->value);
63 g_hash_table_unref (item->table);
65 g_slice_free (GvdbItem, item);
69 gvdb_hash_table_new (GHashTable *parent,
70 const gchar *name_in_parent)
74 table = g_hash_table_new_full (g_str_hash, g_str_equal,
75 g_free, gvdb_item_free);
81 item = gvdb_hash_table_insert (parent, name_in_parent);
82 gvdb_item_set_hash_table (item, table);
89 djb_hash (const gchar *key)
91 guint32 hash_value = 5381;
94 hash_value = hash_value * 33 + *(signed char *)key++;
100 gvdb_hash_table_insert (GHashTable *table,
105 item = g_slice_new0 (GvdbItem);
106 item->key = g_strdup (key);
107 item->hash_value = djb_hash (key);
109 g_hash_table_insert (table, g_strdup (key), item);
115 gvdb_hash_table_insert_string (GHashTable *table,
121 item = gvdb_hash_table_insert (table, key);
122 gvdb_item_set_value (item, g_variant_new_string (value));
126 gvdb_item_set_value (GvdbItem *item,
129 g_return_if_fail (!item->value && !item->table && !item->child);
131 item->value = g_variant_ref_sink (value);
135 gvdb_item_set_hash_table (GvdbItem *item,
138 g_return_if_fail (!item->value && !item->table && !item->child);
140 item->table = g_hash_table_ref (table);
144 gvdb_item_set_parent (GvdbItem *item,
149 g_return_if_fail (g_str_has_prefix (item->key, parent->key));
150 g_return_if_fail (!parent->value && !parent->table);
151 g_return_if_fail (!item->parent && !item->sibling);
153 for (node = &parent->child; *node; node = &(*node)->sibling)
154 if (strcmp ((*node)->key, item->key) > 0)
157 item->parent = parent;
158 item->sibling = *node;
169 hash_table_new (gint n_buckets)
173 table = g_slice_new (HashTable);
174 table->buckets = g_new0 (GvdbItem *, n_buckets);
175 table->n_buckets = n_buckets;
181 hash_table_free (HashTable *table)
183 g_free (table->buckets);
185 g_slice_free (HashTable, table);
189 hash_table_insert (gpointer key,
193 guint32 hash_value, bucket;
194 HashTable *table = data;
195 GvdbItem *item = value;
197 hash_value = djb_hash (key);
198 bucket = hash_value % table->n_buckets;
199 item->next = table->buckets[bucket];
200 table->buckets[bucket] = item;
204 item_to_index (GvdbItem *item)
207 return item->assigned_index;
209 return guint32_to_le (-1u);
227 file_builder_allocate (FileBuilder *fb,
230 struct gvdb_pointer *pointer)
237 fb->offset += (-fb->offset) & (alignment - 1);
238 chunk = g_slice_new (FileChunk);
239 chunk->offset = fb->offset;
241 chunk->data = g_malloc (size);
243 pointer->start = guint32_to_le (fb->offset);
245 pointer->end = guint32_to_le (fb->offset);
247 g_queue_push_tail (fb->chunks, chunk);
253 file_builder_add_value (FileBuilder *fb,
255 struct gvdb_pointer *pointer)
257 GVariant *variant, *normal;
263 value = g_variant_byteswap (value);
264 variant = g_variant_new_variant (value);
265 g_variant_unref (value);
268 variant = g_variant_new_variant (value);
270 normal = g_variant_get_normal_form (variant);
271 g_variant_unref (variant);
273 size = g_variant_get_size (normal);
274 data = file_builder_allocate (fb, 8, size, pointer);
275 g_variant_store (normal, data);
276 g_variant_unref (normal);
280 file_builder_add_string (FileBuilder *fb,
288 length = strlen (string);
290 chunk = g_slice_new (FileChunk);
291 chunk->offset = fb->offset;
292 chunk->size = length;
293 chunk->data = g_malloc (length);
294 memcpy (chunk->data, string, length);
296 *start = guint32_to_le (fb->offset);
297 *size = guint16_to_le (length);
298 fb->offset += length;
300 g_queue_push_tail (fb->chunks, chunk);
304 file_builder_allocate_for_hash (FileBuilder *fb,
309 guint32_le **bloom_filter,
310 guint32_le **hash_buckets,
311 struct gvdb_hash_item **hash_items,
312 struct gvdb_pointer *pointer)
314 guint32_le bloom_hdr, table_hdr;
318 g_assert (n_bloom_words < (1u << 27));
320 bloom_hdr = guint32_to_le (bloom_shift << 27 | n_bloom_words);
321 table_hdr = guint32_to_le (n_buckets);
323 size = sizeof bloom_hdr + sizeof table_hdr +
324 n_bloom_words * sizeof (guint32_le) +
325 n_buckets * sizeof (guint32_le) +
326 n_items * sizeof (struct gvdb_hash_item);
328 data = file_builder_allocate (fb, 4, size, pointer);
330 #define chunk(s) (size -= (s), data += (s), data - (s))
331 memcpy (chunk (sizeof bloom_hdr), &bloom_hdr, sizeof bloom_hdr);
332 memcpy (chunk (sizeof table_hdr), &table_hdr, sizeof table_hdr);
333 *bloom_filter = (guint32_le *) chunk (n_bloom_words * sizeof (guint32_le));
334 *hash_buckets = (guint32_le *) chunk (n_buckets * sizeof (guint32_le));
335 *hash_items = (struct gvdb_hash_item *) chunk (n_items *
336 sizeof (struct gvdb_hash_item));
337 g_assert (size == 0);
340 memset (*bloom_filter, 0, n_bloom_words * sizeof (guint32_le));
344 file_builder_add_hash (FileBuilder *fb,
346 struct gvdb_pointer *pointer)
348 guint32_le *buckets, *bloom_filter;
349 struct gvdb_hash_item *items;
355 mytable = hash_table_new (g_hash_table_size (table));
356 g_hash_table_foreach (table, hash_table_insert, mytable);
359 for (bucket = 0; bucket < mytable->n_buckets; bucket++)
360 for (item = mytable->buckets[bucket]; item; item = item->next)
361 item->assigned_index = guint32_to_le (index++);
363 file_builder_allocate_for_hash (fb, mytable->n_buckets, index, 5, 0,
364 &bloom_filter, &buckets, &items, pointer);
367 for (bucket = 0; bucket < mytable->n_buckets; bucket++)
369 buckets[bucket] = guint32_to_le (index);
371 for (item = mytable->buckets[bucket]; item; item = item->next)
373 struct gvdb_hash_item *entry = items++;
374 const gchar *basename;
376 g_assert (index == guint32_from_le (item->assigned_index));
377 entry->hash_value = guint32_to_le (item->hash_value);
378 entry->parent = item_to_index (item->parent);
381 if (item->parent != NULL)
382 basename = item->key + strlen (item->parent->key);
384 basename = item->key;
386 file_builder_add_string (fb, basename,
390 if (item->value != NULL)
392 g_assert (item->child == NULL && item->table == NULL);
394 file_builder_add_value (fb, item->value, &entry->value.pointer);
398 if (item->child != NULL)
400 guint32 children = 0, i = 0;
404 g_assert (item->table == NULL);
406 for (child = item->child; child; child = child->sibling)
409 offsets = file_builder_allocate (fb, 4, 4 * children,
410 &entry->value.pointer);
413 for (child = item->child; child; child = child->sibling)
414 offsets[i++] = child->assigned_index;
416 g_assert (children == i);
419 if (item->table != NULL)
422 file_builder_add_hash (fb, item->table, &entry->value.pointer);
429 hash_table_free (mytable);
433 file_builder_new (gboolean byteswap)
435 FileBuilder *builder;
437 builder = g_slice_new (FileBuilder);
438 builder->chunks = g_queue_new ();
439 builder->offset = sizeof (struct gvdb_header);
440 builder->byteswap = byteswap;
446 file_builder_serialise (FileBuilder *fb,
447 struct gvdb_pointer root)
449 struct gvdb_header header = { { 0, }, };
454 header.signature[0] = GVDB_SWAPPED_SIGNATURE0;
455 header.signature[1] = GVDB_SWAPPED_SIGNATURE1;
459 header.signature[0] = GVDB_SIGNATURE0;
460 header.signature[1] = GVDB_SIGNATURE1;
463 result = g_string_new (NULL);
466 g_string_append_len (result, (gpointer) &header, sizeof header);
468 while (!g_queue_is_empty (fb->chunks))
470 FileChunk *chunk = g_queue_pop_head (fb->chunks);
472 if (result->len != chunk->offset)
474 gchar zero[8] = { 0, };
476 g_assert (chunk->offset > result->len);
477 g_assert (chunk->offset - result->len < 8);
479 g_string_append_len (result, zero, chunk->offset - result->len);
480 g_assert (result->len == chunk->offset);
483 g_string_append_len (result, chunk->data, chunk->size);
484 g_free (chunk->data);
486 g_slice_free (FileChunk, chunk);
489 g_queue_free (fb->chunks);
490 g_slice_free (FileBuilder, fb);
496 gvdb_table_write_contents (GHashTable *table,
497 const gchar *filename,
501 struct gvdb_pointer root;
506 fb = file_builder_new (byteswap);
507 file_builder_add_hash (fb, table, &root);
508 str = file_builder_serialise (fb, root);
510 status = g_file_set_contents (filename, str->str, str->len, error);
511 g_string_free (str, TRUE);