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