Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / gvdb / gvdb-builder.c
1 /*
2  * Copyright © 2010 Codethink Limited
3  *
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.
8  *
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.
13  *
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.
18  *
19  * Author: Ryan Lortie <desrt@desrt.ca>
20  */
21
22 #include "gvdb-builder.h"
23 #include "gvdb-format.h"
24
25 #include <glib.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <string.h>
29
30
31 struct _GvdbItem
32 {
33   gchar *key;
34   guint32 hash_value;
35   guint32_le assigned_index;
36   GvdbItem *parent;
37   GvdbItem *sibling;
38   GvdbItem *next;
39
40   /* one of:
41    * this:
42    */
43   GVariant *value;
44
45   /* this: */
46   GHashTable *table;
47
48   /* or this: */
49   GvdbItem *child;
50 };
51
52 static void
53 gvdb_item_free (gpointer data)
54 {
55   GvdbItem *item = data;
56
57   g_free (item->key);
58
59   if (item->value)
60     g_variant_unref (item->value);
61
62   if (item->table)
63     g_hash_table_unref (item->table);
64
65   g_slice_free (GvdbItem, item);
66 }
67
68 GHashTable *
69 gvdb_hash_table_new (GHashTable  *parent,
70                      const gchar *name_in_parent)
71 {
72   GHashTable *table;
73
74   table = g_hash_table_new_full (g_str_hash, g_str_equal,
75                                  g_free, gvdb_item_free);
76
77   if (parent)
78     {
79       GvdbItem *item;
80
81       item = gvdb_hash_table_insert (parent, name_in_parent);
82       gvdb_item_set_hash_table (item, table);
83     }
84
85   return table;
86 }
87
88 static guint32
89 djb_hash (const gchar *key)
90 {
91   guint32 hash_value = 5381;
92
93   while (*key)
94     hash_value = hash_value * 33 + *key++;
95
96   return hash_value;
97 }
98
99 GvdbItem *
100 gvdb_hash_table_insert (GHashTable  *table,
101                         const gchar *key)
102 {
103   GvdbItem *item;
104
105   item = g_slice_new0 (GvdbItem);
106   item->key = g_strdup (key);
107   item->hash_value = djb_hash (key);
108
109   g_hash_table_insert (table, g_strdup (key), item);
110
111   return item;
112 }
113
114 void
115 gvdb_hash_table_insert_string (GHashTable  *table,
116                                const gchar *key,
117                                const gchar *value)
118 {
119   GvdbItem *item;
120
121   item = gvdb_hash_table_insert (table, key);
122   gvdb_item_set_value (item, g_variant_new_string (value));
123 }
124
125 void
126 gvdb_item_set_value (GvdbItem *item,
127                      GVariant *value)
128 {
129   g_return_if_fail (!item->value && !item->table && !item->child);
130
131   item->value = g_variant_ref_sink (value);
132 }
133
134 void
135 gvdb_item_set_hash_table (GvdbItem   *item,
136                           GHashTable *table)
137 {
138   g_return_if_fail (!item->value && !item->table && !item->child);
139
140   item->table = g_hash_table_ref (table);
141 }
142
143 void
144 gvdb_item_set_parent (GvdbItem *item,
145                       GvdbItem *parent)
146 {
147   GvdbItem **node;
148
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);
152
153   for (node = &parent->child; *node; node = &(*node)->sibling)
154     if (strcmp ((*node)->key, item->key) > 0)
155       break;
156
157   item->parent = parent;
158   item->sibling = *node;
159   *node = item;
160 }
161
162 typedef struct
163 {
164   GvdbItem **buckets;
165   gint n_buckets;
166 } HashTable;
167
168 static HashTable *
169 hash_table_new (gint n_buckets)
170 {
171   HashTable *table;
172
173   table = g_slice_new (HashTable);
174   table->buckets = g_new0 (GvdbItem *, n_buckets);
175   table->n_buckets = n_buckets;
176
177   return table;
178 }
179
180 static void
181 hash_table_insert (gpointer key,
182                    gpointer value,
183                    gpointer data)
184 {
185   guint32 hash_value, bucket;
186   HashTable *table = data;
187   GvdbItem *item = value;
188
189   hash_value = djb_hash (key);
190   bucket = hash_value % table->n_buckets;
191   item->next = table->buckets[bucket];
192   table->buckets[bucket] = item;
193 }
194
195 static guint32_le
196 item_to_index (GvdbItem *item)
197 {
198   if (item != NULL)
199     return item->assigned_index;
200
201   return guint32_to_le (-1u);
202 }
203
204 typedef struct
205 {
206   GQueue *chunks;
207   guint64 offset;
208   gboolean byteswap;
209 } FileBuilder;
210
211 typedef struct
212 {
213   gsize offset;
214   gsize size;
215   gpointer data;
216 } FileChunk;
217
218 static gpointer
219 file_builder_allocate (FileBuilder         *fb,
220                        guint                alignment,
221                        gsize                size,
222                        struct gvdb_pointer *pointer)
223 {
224   FileChunk *chunk;
225
226   if (size == 0)
227     return NULL;
228
229   fb->offset += (-fb->offset) & (alignment - 1);
230   chunk = g_slice_new (FileChunk);
231   chunk->offset = fb->offset;
232   chunk->size = size;
233   chunk->data = g_malloc (size);
234
235   pointer->start = guint32_to_le (fb->offset);
236   fb->offset += size;
237   pointer->end = guint32_to_le (fb->offset);
238
239   g_queue_push_tail (fb->chunks, chunk);
240
241   return chunk->data;
242 }
243
244 static void
245 file_builder_add_value (FileBuilder         *fb,
246                         GVariant            *value,
247                         struct gvdb_pointer *pointer)
248 {
249   GVariant *variant, *normal;
250   gpointer data;
251   gsize size;
252
253   if (fb->byteswap)
254     {
255       value = g_variant_byteswap (value);
256       variant = g_variant_new_variant (value);
257       g_variant_unref (value);
258     }
259   else
260     variant = g_variant_new_variant (value);
261
262   normal = g_variant_get_normal_form (variant);
263   g_variant_unref (variant);
264
265   size = g_variant_get_size (normal);
266   data = file_builder_allocate (fb, 8, size, pointer);
267   g_variant_store (normal, data);
268   g_variant_unref (normal);
269 }
270
271 static void
272 file_builder_add_string (FileBuilder *fb,
273                          const gchar *string,
274                          guint32_le  *start,
275                          guint16_le  *size)
276 {
277   FileChunk *chunk;
278   gsize length;
279
280   length = strlen (string);
281
282   chunk = g_slice_new (FileChunk);
283   chunk->offset = fb->offset;
284   chunk->size = length;
285   chunk->data = g_malloc (length);
286   memcpy (chunk->data, string, length);
287
288   *start = guint32_to_le (fb->offset);
289   *size = guint16_to_le (length);
290   fb->offset += length;
291
292   g_queue_push_tail (fb->chunks, chunk);
293 }
294
295 static void
296 file_builder_allocate_for_hash (FileBuilder            *fb,
297                                 gsize                   n_buckets,
298                                 gsize                   n_items,
299                                 guint                   bloom_shift,
300                                 gsize                   n_bloom_words,
301                                 guint32_le            **bloom_filter,
302                                 guint32_le            **hash_buckets,
303                                 struct gvdb_hash_item **hash_items,
304                                 struct gvdb_pointer    *pointer)
305 {
306   guint32_le bloom_hdr, table_hdr;
307   guchar *data;
308   gsize size;
309
310   g_assert (n_bloom_words < (1u << 27));
311
312   bloom_hdr = guint32_to_le (bloom_shift << 27 | n_bloom_words);
313   table_hdr = guint32_to_le (n_buckets);
314
315   size = sizeof bloom_hdr + sizeof table_hdr +
316          n_bloom_words * sizeof (guint32_le) +
317          n_buckets     * sizeof (guint32_le) +
318          n_items       * sizeof (struct gvdb_hash_item);
319
320   data = file_builder_allocate (fb, 4, size, pointer);
321
322 #define chunk(s) (size -= (s), data += (s), data - (s))
323   memcpy (chunk (sizeof bloom_hdr), &bloom_hdr, sizeof bloom_hdr);
324   memcpy (chunk (sizeof table_hdr), &table_hdr, sizeof table_hdr);
325   *bloom_filter = (guint32_le *) chunk (n_bloom_words * sizeof (guint32_le));
326   *hash_buckets = (guint32_le *) chunk (n_buckets * sizeof (guint32_le));
327   *hash_items = (struct gvdb_hash_item *) chunk (n_items *
328                   sizeof (struct gvdb_hash_item));
329   g_assert (size == 0);
330 #undef chunk
331
332   memset (*bloom_filter, 0, n_bloom_words * sizeof (guint32_le));
333 }
334
335 static void
336 file_builder_add_hash (FileBuilder         *fb,
337                        GHashTable          *table,
338                        struct gvdb_pointer *pointer)
339 {
340   guint32_le *buckets, *bloom_filter;
341   struct gvdb_hash_item *items;
342   HashTable *mytable;
343   GvdbItem *item;
344   guint32 index;
345   gint bucket;
346
347   mytable = hash_table_new (g_hash_table_size (table));
348   g_hash_table_foreach (table, hash_table_insert, mytable);
349   index = 0;
350
351   for (bucket = 0; bucket < mytable->n_buckets; bucket++)
352     for (item = mytable->buckets[bucket]; item; item = item->next)
353       item->assigned_index = guint32_to_le (index++);
354
355   file_builder_allocate_for_hash (fb, mytable->n_buckets, index, 5, 0,
356                                   &bloom_filter, &buckets, &items, pointer);
357
358   index = 0;
359   for (bucket = 0; bucket < mytable->n_buckets; bucket++)
360     {
361       buckets[bucket] = guint32_to_le (index);
362
363       for (item = mytable->buckets[bucket]; item; item = item->next)
364         {
365           struct gvdb_hash_item *entry = items++;
366           const gchar *basename;
367
368           g_assert (index == guint32_from_le (item->assigned_index));
369           entry->hash_value = guint32_to_le (item->hash_value);
370           entry->parent = item_to_index (item->parent);
371           entry->unused = 0;
372
373           if (item->parent != NULL)
374             basename = item->key + strlen (item->parent->key);
375           else
376             basename = item->key;
377
378           file_builder_add_string (fb, basename,
379                                    &entry->key_start,
380                                    &entry->key_size);
381
382           if (item->value != NULL)
383             {
384               g_assert (item->child == NULL && item->table == NULL);
385
386               file_builder_add_value (fb, item->value, &entry->value.pointer);
387               entry->type = 'v';
388             }
389
390           if (item->child != NULL)
391             {
392               guint32 children = 0, i = 0;
393               guint32_le *offsets;
394               GvdbItem *child;
395
396               g_assert (item->table == NULL);
397
398               for (child = item->child; child; child = child->sibling)
399                 children++;
400
401               offsets = file_builder_allocate (fb, 4, 4 * children,
402                                                &entry->value.pointer);
403               entry->type = 'L';
404
405               for (child = item->child; child; child = child->sibling)
406                 offsets[i++] = child->assigned_index;
407
408               g_assert (children == i);
409             }
410
411           if (item->table != NULL)
412             {
413               entry->type = 'H';
414               file_builder_add_hash (fb, item->table, &entry->value.pointer);
415             }
416
417           index++;
418         }
419     }
420 }
421
422 static FileBuilder *
423 file_builder_new (gboolean byteswap)
424 {
425   FileBuilder *builder;
426
427   builder = g_slice_new (FileBuilder);
428   builder->chunks = g_queue_new ();
429   builder->offset = sizeof (struct gvdb_header);
430   builder->byteswap = byteswap;
431
432   return builder;
433 }
434
435 static GString *
436 file_builder_serialise (FileBuilder          *fb,
437                         struct gvdb_pointer   root)
438 {
439   struct gvdb_header header = { { 0, }, };
440   GString *result;
441
442   if (fb->byteswap)
443     {
444       header.signature[0] = GVDB_SWAPPED_SIGNATURE0;
445       header.signature[1] = GVDB_SWAPPED_SIGNATURE1;
446     }
447   else
448     {
449       header.signature[0] = GVDB_SIGNATURE0;
450       header.signature[1] = GVDB_SIGNATURE1;
451     }
452
453   result = g_string_new (NULL);
454
455   header.root = root;
456   g_string_append_len (result, (gpointer) &header, sizeof header);
457
458   while (!g_queue_is_empty (fb->chunks))
459     {
460       FileChunk *chunk = g_queue_pop_head (fb->chunks);
461
462       if (result->len != chunk->offset)
463         {
464           gchar zero[8] = { 0, };
465
466           g_assert (chunk->offset > result->len);
467           g_assert (chunk->offset - result->len < 8);
468
469           g_string_append_len (result, zero, chunk->offset - result->len);
470           g_assert (result->len == chunk->offset);
471         }
472
473       g_string_append_len (result, chunk->data, chunk->size);
474       g_free (chunk->data);
475     }
476
477   g_queue_free (fb->chunks);
478   g_slice_free (FileBuilder, fb);
479
480   return result;
481 }
482
483 gboolean
484 gvdb_table_write_contents (GHashTable   *table,
485                            const gchar  *filename,
486                            gboolean      byteswap,
487                            GError      **error)
488 {
489   struct gvdb_pointer root;
490   gboolean status;
491   FileBuilder *fb;
492   GString *str;
493
494   fb = file_builder_new (byteswap);
495   file_builder_add_hash (fb, table, &root);
496   str = file_builder_serialise (fb, root);
497
498   status = g_file_set_contents (filename, str->str, str->len, error);
499   g_string_free (str, TRUE);
500
501   return status;
502 }