Merge remote-tracking branch 'origin/master' into 0.11
[platform/upstream/gstreamer.git] / gst / gstregistrychunks.c
1 /* GStreamer
2  * Copyright (C) 2006 Josep Torra <josep@fluendo.com>
3  *               2006 Mathieu Garcia <matthieu@fluendo.com>
4  *               2006,2007 Stefan Kost <ensonic@users.sf.net>
5  *               2008 Sebastian Dröge <slomo@circular-chaos.org>
6  *               2008 Jan Schmidt <jan.schmidt@sun.com>
7  *
8  * gstregistrychunks.c: GstRegistryChunk helper for serialising/deserialising
9  * plugin entries and features.
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #  include "config.h"
29 #endif
30
31 #include <gst/gst_private.h>
32 #include <gst/gstconfig.h>
33 #include <gst/gstelement.h>
34 #include <gst/gsttypefind.h>
35 #include <gst/gsttypefindfactory.h>
36 #include <gst/gsturi.h>
37 #include <gst/gstinfo.h>
38 #include <gst/gstenumtypes.h>
39 #include <gst/gstpadtemplate.h>
40
41 #include <gst/gstregistrychunks.h>
42
43 #define GST_CAT_DEFAULT GST_CAT_REGISTRY
44
45 /* count string length, but return -1 if we hit the eof */
46 static gint
47 _strnlen (const gchar * str, gint maxlen)
48 {
49   gint len = 0;
50
51   while (G_LIKELY (len < maxlen)) {
52     if (G_UNLIKELY (str[len] == '\0'))
53       return len;
54     len++;
55   }
56   return -1;
57 }
58
59 /* Macros */
60 #define unpack_element(inptr, outptr, element, endptr, error_label) G_STMT_START{ \
61   if (inptr + sizeof(element) > endptr) { \
62     GST_ERROR ("Failed reading element " G_STRINGIFY (element)  \
63         ". Have %d bytes need %" G_GSSIZE_FORMAT, \
64         (int) (endptr - inptr), sizeof(element)); \
65     goto error_label; \
66   } \
67   outptr = (element *) inptr; \
68   inptr += sizeof (element); \
69 }G_STMT_END
70
71 #define unpack_const_string(inptr, outptr, endptr, error_label) G_STMT_START{\
72   gint _len = _strnlen (inptr, (endptr-inptr)); \
73   if (_len == -1) \
74     goto error_label; \
75   outptr = g_intern_string ((const gchar *)inptr); \
76   inptr += _len + 1; \
77 }G_STMT_END
78
79 #define unpack_string(inptr, outptr, endptr, error_label)  G_STMT_START{\
80   gint _len = _strnlen (inptr, (endptr-inptr)); \
81   if (_len == -1) \
82     goto error_label; \
83   outptr = g_memdup ((gconstpointer)inptr, _len + 1); \
84   inptr += _len + 1; \
85 }G_STMT_END
86
87 #define unpack_string_nocopy(inptr, outptr, endptr, error_label)  G_STMT_START{\
88   gint _len = _strnlen (inptr, (endptr-inptr)); \
89   if (_len == -1) \
90     goto error_label; \
91   outptr = (const gchar *)inptr; \
92   inptr += _len + 1; \
93 }G_STMT_END
94
95 #define ALIGNMENT            (sizeof (void *))
96 #define alignment(_address)  (gsize)_address%ALIGNMENT
97 #define align(_ptr)          _ptr += (( alignment(_ptr) == 0) ? 0 : ALIGNMENT-alignment(_ptr))
98
99 void
100 _priv_gst_registry_chunk_free (GstRegistryChunk * chunk)
101 {
102   if (!(chunk->flags & GST_REGISTRY_CHUNK_FLAG_CONST)) {
103     if ((chunk->flags & GST_REGISTRY_CHUNK_FLAG_MALLOC))
104       g_free (chunk->data);
105     else
106       g_slice_free1 (chunk->size, chunk->data);
107   }
108   g_slice_free (GstRegistryChunk, chunk);
109 }
110
111 /*
112  * gst_registry_chunks_save_const_string:
113  *
114  * Store a const string in a binary chunk.
115  *
116  * Returns: %TRUE for success
117  */
118 inline static gboolean
119 gst_registry_chunks_save_const_string (GList ** list, const gchar * str)
120 {
121   GstRegistryChunk *chunk;
122
123   if (G_UNLIKELY (str == NULL)) {
124     GST_ERROR ("unexpected NULL string in plugin or plugin feature data");
125     str = "";
126   }
127
128   chunk = g_slice_new (GstRegistryChunk);
129   chunk->data = (gpointer) str;
130   chunk->size = strlen ((gchar *) chunk->data) + 1;
131   chunk->flags = GST_REGISTRY_CHUNK_FLAG_CONST;
132   chunk->align = FALSE;
133   *list = g_list_prepend (*list, chunk);
134   return TRUE;
135 }
136
137 /*
138  * gst_registry_chunks_save_string:
139  *
140  * Store a string in a binary chunk.
141  *
142  * Returns: %TRUE for success
143  */
144 inline static gboolean
145 gst_registry_chunks_save_string (GList ** list, gchar * str)
146 {
147   GstRegistryChunk *chunk;
148
149   chunk = g_slice_new (GstRegistryChunk);
150   chunk->data = str;
151   chunk->size = strlen ((gchar *) chunk->data) + 1;
152   chunk->flags = GST_REGISTRY_CHUNK_FLAG_MALLOC;
153   chunk->align = FALSE;
154   *list = g_list_prepend (*list, chunk);
155   return TRUE;
156 }
157
158 /*
159  * gst_registry_chunks_save_data:
160  *
161  * Store some data in a binary chunk.
162  *
163  * Returns: the initialized chunk
164  */
165 inline static GstRegistryChunk *
166 gst_registry_chunks_make_data (gpointer data, gulong size)
167 {
168   GstRegistryChunk *chunk;
169
170   chunk = g_slice_new (GstRegistryChunk);
171   chunk->data = data;
172   chunk->size = size;
173   chunk->flags = GST_REGISTRY_CHUNK_FLAG_NONE;
174   chunk->align = TRUE;
175   return chunk;
176 }
177
178
179 /*
180  * gst_registry_chunks_save_pad_template:
181  *
182  * Store pad_templates in binary chunks.
183  *
184  * Returns: %TRUE for success
185  */
186 static gboolean
187 gst_registry_chunks_save_pad_template (GList ** list,
188     GstStaticPadTemplate * template)
189 {
190   GstRegistryChunkPadTemplate *pt;
191   GstRegistryChunk *chk;
192
193   pt = g_slice_new (GstRegistryChunkPadTemplate);
194   chk =
195       gst_registry_chunks_make_data (pt, sizeof (GstRegistryChunkPadTemplate));
196
197   pt->presence = template->presence;
198   pt->direction = template->direction;
199
200   /* pack pad template strings */
201   gst_registry_chunks_save_const_string (list,
202       (gchar *) (template->static_caps.string));
203   gst_registry_chunks_save_const_string (list, template->name_template);
204
205   *list = g_list_prepend (*list, chk);
206
207   return TRUE;
208 }
209
210 #define VALIDATE_UTF8(__details, __entry)                                   \
211 G_STMT_START {                                                              \
212   if (!g_utf8_validate (__details->__entry, -1, NULL)) {                    \
213     g_warning ("Invalid UTF-8 in " G_STRINGIFY (__entry) ": %s",            \
214         __details->__entry);                                                \
215     g_free (__details->__entry);                                            \
216     __details->__entry = g_strdup ("[ERROR: invalid UTF-8]");               \
217   }                                                                         \
218 } G_STMT_END
219
220 /*
221  * gst_registry_chunks_save_feature:
222  *
223  * Store features in binary chunks.
224  *
225  * Returns: %TRUE for success
226  */
227 static gboolean
228 gst_registry_chunks_save_feature (GList ** list, GstPluginFeature * feature)
229 {
230   const gchar *type_name = g_type_name (G_OBJECT_TYPE (feature));
231   GstRegistryChunkPluginFeature *pf = NULL;
232   GstRegistryChunk *chk = NULL;
233   GList *walk;
234
235   if (!type_name) {
236     GST_ERROR ("NULL feature type_name, aborting.");
237     return FALSE;
238   }
239
240   if (GST_IS_ELEMENT_FACTORY (feature)) {
241     GstRegistryChunkElementFactory *ef;
242     GstElementFactory *factory = GST_ELEMENT_FACTORY (feature);
243
244     /* Initialize with zeroes because of struct padding and
245      * valgrind complaining about copying unitialized memory
246      */
247     ef = g_slice_new0 (GstRegistryChunkElementFactory);
248     chk =
249         gst_registry_chunks_make_data (ef,
250         sizeof (GstRegistryChunkElementFactory));
251     ef->npadtemplates = ef->ninterfaces = ef->nuriprotocols = 0;
252     pf = (GstRegistryChunkPluginFeature *) ef;
253
254     /* save interfaces */
255     for (walk = factory->interfaces; walk;
256         walk = g_list_next (walk), ef->ninterfaces++) {
257       gst_registry_chunks_save_const_string (list, (gchar *) walk->data);
258     }
259     GST_DEBUG ("Feature %s: saved %d interfaces %d pad templates",
260         GST_OBJECT_NAME (feature), ef->ninterfaces, ef->npadtemplates);
261
262     /* save uritypes */
263     if (GST_URI_TYPE_IS_VALID (factory->uri_type)) {
264       if (factory->uri_protocols && *factory->uri_protocols) {
265         GstRegistryChunk *subchk;
266         gchar **protocol;
267
268         subchk =
269             gst_registry_chunks_make_data (&factory->uri_type,
270             sizeof (factory->uri_type));
271         subchk->flags = GST_REGISTRY_CHUNK_FLAG_CONST;
272
273         protocol = factory->uri_protocols;
274         while (*protocol) {
275           gst_registry_chunks_save_const_string (list, *protocol++);
276           ef->nuriprotocols++;
277         }
278         *list = g_list_prepend (*list, subchk);
279         GST_DEBUG ("Saved %d UriTypes", ef->nuriprotocols);
280       } else {
281         g_warning ("GStreamer feature '%s' is URI handler but does not provide"
282             " any protocols it can handle", GST_OBJECT_NAME (feature));
283       }
284     }
285
286     /* save pad-templates */
287     for (walk = factory->staticpadtemplates; walk;
288         walk = g_list_next (walk), ef->npadtemplates++) {
289       GstStaticPadTemplate *template = walk->data;
290
291       if (!gst_registry_chunks_save_pad_template (list, template)) {
292         GST_ERROR ("Can't fill pad template, aborting.");
293         goto fail;
294       }
295     }
296
297     /* pack element metadata strings */
298     gst_registry_chunks_save_string (list,
299         gst_structure_to_string (factory->metadata));
300   } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
301     GstRegistryChunkTypeFindFactory *tff;
302     GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
303     gchar *str;
304
305     /* Initialize with zeroes because of struct padding and
306      * valgrind complaining about copying unitialized memory
307      */
308     tff = g_slice_new0 (GstRegistryChunkTypeFindFactory);
309     chk =
310         gst_registry_chunks_make_data (tff,
311         sizeof (GstRegistryChunkTypeFindFactory));
312     tff->nextensions = 0;
313     pf = (GstRegistryChunkPluginFeature *) tff;
314
315     /* save extensions */
316     if (factory->extensions) {
317       while (factory->extensions[tff->nextensions]) {
318         gst_registry_chunks_save_const_string (list,
319             factory->extensions[tff->nextensions++]);
320       }
321     }
322     /* save caps */
323     if (factory->caps) {
324       /* we copy the caps here so we can simplify them before saving. This
325        * is a lot faster when loading them later on */
326       GstCaps *copy = gst_caps_copy (factory->caps);
327
328       gst_caps_do_simplify (copy);
329       str = gst_caps_to_string (copy);
330       gst_caps_unref (copy);
331       gst_registry_chunks_save_string (list, str);
332     } else {
333       gst_registry_chunks_save_const_string (list, "");
334     }
335   } else if (GST_IS_INDEX_FACTORY (feature)) {
336     GstIndexFactory *factory = GST_INDEX_FACTORY (feature);
337
338     pf = g_slice_new (GstRegistryChunkPluginFeature);
339     chk =
340         gst_registry_chunks_make_data (pf,
341         sizeof (GstRegistryChunkPluginFeature));
342
343     /* pack element factory strings */
344     gst_registry_chunks_save_const_string (list, factory->longdesc);
345   } else {
346     GST_WARNING ("unhandled feature type '%s'", type_name);
347   }
348
349   if (pf) {
350     pf->rank = feature->rank;
351     *list = g_list_prepend (*list, chk);
352
353     /* pack plugin feature strings */
354     gst_registry_chunks_save_const_string (list, GST_OBJECT_NAME (feature));
355     gst_registry_chunks_save_const_string (list, (gchar *) type_name);
356
357     return TRUE;
358   }
359
360   /* Errors */
361 fail:
362   g_free (chk);
363   g_free (pf);
364   return FALSE;
365 }
366
367 static gboolean
368 gst_registry_chunks_save_plugin_dep (GList ** list, GstPluginDep * dep)
369 {
370   GstRegistryChunkDep *ed;
371   GstRegistryChunk *chk;
372   gchar **s;
373
374   ed = g_slice_new (GstRegistryChunkDep);
375   chk = gst_registry_chunks_make_data (ed, sizeof (GstRegistryChunkDep));
376
377   ed->flags = dep->flags;
378   ed->n_env_vars = 0;
379   ed->n_paths = 0;
380   ed->n_names = 0;
381
382   ed->env_hash = dep->env_hash;
383   ed->stat_hash = dep->stat_hash;
384
385   for (s = dep->env_vars; s != NULL && *s != NULL; ++s, ++ed->n_env_vars)
386     gst_registry_chunks_save_string (list, g_strdup (*s));
387
388   for (s = dep->paths; s != NULL && *s != NULL; ++s, ++ed->n_paths)
389     gst_registry_chunks_save_string (list, g_strdup (*s));
390
391   for (s = dep->names; s != NULL && *s != NULL; ++s, ++ed->n_names)
392     gst_registry_chunks_save_string (list, g_strdup (*s));
393
394   *list = g_list_prepend (*list, chk);
395
396   GST_LOG ("Saved external plugin dependency");
397   return TRUE;
398 }
399
400 /*
401  * _priv_gst_registry_chunks_save_plugin:
402  *
403  * Adapt a GstPlugin to our GstRegistryChunkPluginElement structure, and
404  * prepend it as a GstRegistryChunk in the provided list.
405  *
406  */
407 gboolean
408 _priv_gst_registry_chunks_save_plugin (GList ** list, GstRegistry * registry,
409     GstPlugin * plugin)
410 {
411   GstRegistryChunkPluginElement *pe;
412   GstRegistryChunk *chk;
413   GList *plugin_features = NULL;
414   GList *walk;
415
416   pe = g_slice_new (GstRegistryChunkPluginElement);
417   chk =
418       gst_registry_chunks_make_data (pe,
419       sizeof (GstRegistryChunkPluginElement));
420
421   pe->file_size = plugin->file_size;
422   pe->file_mtime = plugin->file_mtime;
423   pe->nfeatures = 0;
424   pe->n_deps = 0;
425
426   /* pack external deps */
427   for (walk = plugin->priv->deps; walk != NULL; walk = walk->next) {
428     if (!gst_registry_chunks_save_plugin_dep (list, walk->data)) {
429       GST_ERROR ("Could not save external plugin dependency, aborting.");
430       goto fail;
431     }
432     ++pe->n_deps;
433   }
434
435   /* pack plugin features */
436   plugin_features =
437       gst_registry_get_feature_list_by_plugin (registry, plugin->desc.name);
438   for (walk = plugin_features; walk; walk = g_list_next (walk), pe->nfeatures++) {
439     GstPluginFeature *feature = GST_PLUGIN_FEATURE (walk->data);
440
441     if (!gst_registry_chunks_save_feature (list, feature)) {
442       GST_ERROR ("Can't fill plugin feature, aborting.");
443       goto fail;
444     }
445   }
446
447   gst_plugin_feature_list_free (plugin_features);
448
449   /* pack cache data */
450   if (plugin->priv->cache_data) {
451     gchar *cache_str = gst_structure_to_string (plugin->priv->cache_data);
452     gst_registry_chunks_save_string (list, cache_str);
453   } else {
454     gst_registry_chunks_save_const_string (list, "");
455   }
456
457   /* pack plugin element strings */
458   gst_registry_chunks_save_const_string (list,
459       (plugin->desc.release_datetime) ? plugin->desc.release_datetime : "");
460   gst_registry_chunks_save_const_string (list, plugin->desc.origin);
461   gst_registry_chunks_save_const_string (list, plugin->desc.package);
462   gst_registry_chunks_save_const_string (list, plugin->desc.source);
463   gst_registry_chunks_save_const_string (list, plugin->desc.license);
464   gst_registry_chunks_save_const_string (list, plugin->desc.version);
465   gst_registry_chunks_save_const_string (list, plugin->filename);
466   gst_registry_chunks_save_const_string (list, plugin->desc.description);
467   gst_registry_chunks_save_const_string (list, plugin->desc.name);
468
469   *list = g_list_prepend (*list, chk);
470
471   GST_DEBUG ("Found %d features in plugin \"%s\"", pe->nfeatures,
472       plugin->desc.name);
473   return TRUE;
474
475   /* Errors */
476 fail:
477   gst_plugin_feature_list_free (plugin_features);
478   g_free (chk);
479   g_free (pe);
480   return FALSE;
481 }
482
483 /*
484  * gst_registry_chunks_load_pad_template:
485  *
486  * Make a new GstStaticPadTemplate from current GstRegistryChunkPadTemplate
487  * structure.
488  *
489  * Returns: new GstStaticPadTemplate
490  */
491 static gboolean
492 gst_registry_chunks_load_pad_template (GstElementFactory * factory, gchar ** in,
493     gchar * end)
494 {
495   GstRegistryChunkPadTemplate *pt;
496   GstStaticPadTemplate *template = NULL;
497
498   align (*in);
499   GST_DEBUG ("Reading/casting for GstRegistryChunkPadTemplate at address %p",
500       *in);
501   unpack_element (*in, pt, GstRegistryChunkPadTemplate, end, fail);
502
503   template = g_slice_new (GstStaticPadTemplate);
504   template->presence = pt->presence;
505   template->direction = (GstPadDirection) pt->direction;
506   template->static_caps.caps.mini_object.refcount = 0;
507
508   /* unpack pad template strings */
509   unpack_const_string (*in, template->name_template, end, fail);
510   unpack_const_string (*in, template->static_caps.string, end, fail);
511
512   __gst_element_factory_add_static_pad_template (factory, template);
513   GST_DEBUG ("Added pad_template %s", template->name_template);
514
515   return TRUE;
516 fail:
517   GST_INFO ("Reading pad template failed");
518   if (template)
519     g_slice_free (GstStaticPadTemplate, template);
520   return FALSE;
521 }
522
523 /*
524  * gst_registry_chunks_load_feature:
525  *
526  * Make a new GstPluginFeature from current binary plugin feature structure
527  *
528  * Returns: new GstPluginFeature
529  */
530 static gboolean
531 gst_registry_chunks_load_feature (GstRegistry * registry, gchar ** in,
532     gchar * end, GstPlugin * plugin)
533 {
534   GstRegistryChunkPluginFeature *pf = NULL;
535   GstPluginFeature *feature = NULL;
536   const gchar *const_str, *type_name;
537   const gchar *feature_name;
538   const gchar *plugin_name;
539   gchar *str;
540   GType type;
541   guint i;
542
543   plugin_name = plugin->desc.name;
544
545   /* unpack plugin feature strings */
546   unpack_string_nocopy (*in, type_name, end, fail);
547
548   if (G_UNLIKELY (!type_name)) {
549     GST_ERROR ("No feature type name");
550     return FALSE;
551   }
552
553   /* unpack more plugin feature strings */
554   unpack_string_nocopy (*in, feature_name, end, fail);
555
556   GST_DEBUG ("Plugin '%s' feature '%s' typename : '%s'", plugin_name,
557       feature_name, type_name);
558
559   if (G_UNLIKELY (!(type = g_type_from_name (type_name)))) {
560     GST_ERROR ("Unknown type from typename '%s' for plugin '%s'", type_name,
561         plugin_name);
562     return FALSE;
563   }
564   if (G_UNLIKELY ((feature = g_object_newv (type, 0, NULL)) == NULL)) {
565     GST_ERROR ("Can't create feature from type");
566     return FALSE;
567   }
568   gst_plugin_feature_set_name (feature, feature_name);
569
570   if (G_UNLIKELY (!GST_IS_PLUGIN_FEATURE (feature))) {
571     GST_ERROR ("typename : '%s' is not a plugin feature", type_name);
572     goto fail;
573   }
574
575   if (GST_IS_ELEMENT_FACTORY (feature)) {
576     GstRegistryChunkElementFactory *ef;
577     guint n;
578     GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (feature);
579     gchar *str;
580     const gchar *meta_data_str;
581
582     align (*in);
583     GST_LOG ("Reading/casting for GstRegistryChunkElementFactory at address %p",
584         *in);
585     unpack_element (*in, ef, GstRegistryChunkElementFactory, end, fail);
586     pf = (GstRegistryChunkPluginFeature *) ef;
587
588     /* unpack element factory strings */
589     unpack_string_nocopy (*in, meta_data_str, end, fail);
590     if (meta_data_str && *meta_data_str) {
591       factory->metadata = gst_structure_from_string (meta_data_str, NULL);
592       if (!factory->metadata) {
593         GST_ERROR
594             ("Error when trying to deserialize structure for metadata '%s'",
595             meta_data_str);
596         goto fail;
597       }
598     }
599     n = ef->npadtemplates;
600     GST_DEBUG ("Element factory : npadtemplates=%d", n);
601
602     /* load pad templates */
603     for (i = 0; i < n; i++) {
604       if (G_UNLIKELY (!gst_registry_chunks_load_pad_template (factory, in,
605                   end))) {
606         GST_ERROR ("Error while loading binary pad template");
607         goto fail;
608       }
609     }
610
611     /* load uritypes */
612     if (G_UNLIKELY ((n = ef->nuriprotocols))) {
613       GST_DEBUG ("Reading %d UriTypes at address %p", n, *in);
614
615       align (*in);
616       factory->uri_type = *((guint *) * in);
617       *in += sizeof (factory->uri_type);
618       /*unpack_element(*in, &factory->uri_type, factory->uri_type, end, fail); */
619
620       factory->uri_protocols = g_new0 (gchar *, n + 1);
621       for (i = 0; i < n; i++) {
622         unpack_string (*in, str, end, fail);
623         factory->uri_protocols[i] = str;
624       }
625     }
626     /* load interfaces */
627     if (G_UNLIKELY ((n = ef->ninterfaces))) {
628       GST_DEBUG ("Reading %d Interfaces at address %p", n, *in);
629       for (i = 0; i < n; i++) {
630         unpack_string_nocopy (*in, const_str, end, fail);
631         __gst_element_factory_add_interface (factory, const_str);
632       }
633     }
634   } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
635     GstRegistryChunkTypeFindFactory *tff;
636     GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
637
638     align (*in);
639     GST_DEBUG
640         ("Reading/casting for GstRegistryChunkPluginFeature at address %p",
641         *in);
642     unpack_element (*in, tff, GstRegistryChunkTypeFindFactory, end, fail);
643     pf = (GstRegistryChunkPluginFeature *) tff;
644
645     /* load typefinder caps */
646     unpack_string_nocopy (*in, const_str, end, fail);
647     if (const_str != NULL && *const_str != '\0')
648       factory->caps = gst_caps_from_string (const_str);
649     else
650       factory->caps = NULL;
651
652     /* load extensions */
653     if (tff->nextensions) {
654       GST_DEBUG ("Reading %d Typefind extensions at address %p",
655           tff->nextensions, *in);
656       factory->extensions = g_new0 (gchar *, tff->nextensions + 1);
657       /* unpack in reverse order to maintain the correct order */
658       for (i = tff->nextensions; i > 0; i--) {
659         unpack_string (*in, str, end, fail);
660         factory->extensions[i - 1] = str;
661       }
662     }
663   } else if (GST_IS_INDEX_FACTORY (feature)) {
664     GstIndexFactory *factory = GST_INDEX_FACTORY (feature);
665
666     align (*in);
667     GST_DEBUG
668         ("Reading/casting for GstRegistryChunkPluginFeature at address %p",
669         *in);
670     unpack_element (*in, pf, GstRegistryChunkPluginFeature, end, fail);
671
672     /* unpack index factory strings */
673     unpack_string (*in, factory->longdesc, end, fail);
674   } else {
675     GST_WARNING ("unhandled factory type : %s", G_OBJECT_TYPE_NAME (feature));
676     goto fail;
677   }
678
679   feature->rank = pf->rank;
680
681   feature->plugin_name = plugin_name;
682   feature->plugin = plugin;
683   g_object_add_weak_pointer ((GObject *) plugin,
684       (gpointer *) & feature->plugin);
685
686   gst_registry_add_feature (registry, feature);
687   GST_DEBUG ("Added feature %s, plugin %p %s", GST_OBJECT_NAME (feature),
688       plugin, plugin_name);
689
690   return TRUE;
691
692   /* Errors */
693 fail:
694   GST_INFO ("Reading plugin feature failed");
695   if (feature) {
696     if (GST_IS_OBJECT (feature))
697       gst_object_unref (feature);
698     else
699       g_object_unref (feature);
700   }
701   return FALSE;
702 }
703
704 static gchar **
705 gst_registry_chunks_load_plugin_dep_strv (gchar ** in, gchar * end, guint n)
706 {
707   gchar **arr;
708
709   if (n == 0)
710     return NULL;
711
712   arr = g_new0 (gchar *, n + 1);
713   while (n > 0) {
714     unpack_string (*in, arr[n - 1], end, fail);
715     --n;
716   }
717   return arr;
718 fail:
719   GST_INFO ("Reading plugin dependency strings failed");
720   return NULL;
721 }
722
723 static gboolean
724 gst_registry_chunks_load_plugin_dep (GstPlugin * plugin, gchar ** in,
725     gchar * end)
726 {
727   GstPluginDep *dep;
728   GstRegistryChunkDep *d;
729   gchar **s;
730
731   align (*in);
732   GST_LOG_OBJECT (plugin, "Unpacking GstRegistryChunkDep from %p", *in);
733   unpack_element (*in, d, GstRegistryChunkDep, end, fail);
734
735   dep = g_slice_new (GstPluginDep);
736
737   dep->env_hash = d->env_hash;
738   dep->stat_hash = d->stat_hash;
739
740   dep->flags = (GstPluginDependencyFlags) d->flags;
741
742   dep->names = gst_registry_chunks_load_plugin_dep_strv (in, end, d->n_names);
743   dep->paths = gst_registry_chunks_load_plugin_dep_strv (in, end, d->n_paths);
744   dep->env_vars =
745       gst_registry_chunks_load_plugin_dep_strv (in, end, d->n_env_vars);
746
747   plugin->priv->deps = g_list_append (plugin->priv->deps, dep);
748
749   GST_DEBUG_OBJECT (plugin, "Loaded external plugin dependency from registry: "
750       "env_hash: %08x, stat_hash: %08x", dep->env_hash, dep->stat_hash);
751   for (s = dep->env_vars; s != NULL && *s != NULL; ++s)
752     GST_LOG_OBJECT (plugin, " evar: %s", *s);
753   for (s = dep->paths; s != NULL && *s != NULL; ++s)
754     GST_LOG_OBJECT (plugin, " path: %s", *s);
755   for (s = dep->names; s != NULL && *s != NULL; ++s)
756     GST_LOG_OBJECT (plugin, " name: %s", *s);
757
758   return TRUE;
759 fail:
760   GST_INFO ("Reading plugin dependency failed");
761   return FALSE;
762 }
763
764
765 /*
766  * _priv_gst_registry_chunks_load_plugin:
767  *
768  * Make a new GstPlugin from current GstRegistryChunkPluginElement structure
769  * and add it to the GstRegistry. Return an offset to the next
770  * GstRegistryChunkPluginElement structure.
771  */
772 gboolean
773 _priv_gst_registry_chunks_load_plugin (GstRegistry * registry, gchar ** in,
774     gchar * end, GstPlugin ** out_plugin)
775 {
776 #ifndef GST_DISABLE_GST_DEBUG
777   gchar *start = *in;
778 #endif
779   GstRegistryChunkPluginElement *pe;
780   const gchar *cache_str = NULL;
781   GstPlugin *plugin = NULL;
782   guint i, n;
783
784   align (*in);
785   GST_LOG ("Reading/casting for GstRegistryChunkPluginElement at address %p",
786       *in);
787   unpack_element (*in, pe, GstRegistryChunkPluginElement, end, fail);
788
789   plugin = g_object_newv (GST_TYPE_PLUGIN, 0, NULL);
790
791   /* TODO: also set GST_PLUGIN_FLAG_CONST */
792   plugin->flags |= GST_PLUGIN_FLAG_CACHED;
793   plugin->file_mtime = pe->file_mtime;
794   plugin->file_size = pe->file_size;
795
796   /* unpack plugin element strings */
797   unpack_const_string (*in, plugin->desc.name, end, fail);
798   unpack_const_string (*in, plugin->desc.description, end, fail);
799   unpack_string (*in, plugin->filename, end, fail);
800   unpack_const_string (*in, plugin->desc.version, end, fail);
801   unpack_const_string (*in, plugin->desc.license, end, fail);
802   unpack_const_string (*in, plugin->desc.source, end, fail);
803   unpack_const_string (*in, plugin->desc.package, end, fail);
804   unpack_const_string (*in, plugin->desc.origin, end, fail);
805   unpack_const_string (*in, plugin->desc.release_datetime, end, fail);
806
807   GST_LOG ("read strings for name='%s'", plugin->desc.name);
808   GST_LOG ("  desc.description='%s'", plugin->desc.description);
809   GST_LOG ("  filename='%s'", plugin->filename);
810   GST_LOG ("  desc.version='%s'", plugin->desc.version);
811   GST_LOG ("  desc.license='%s'", plugin->desc.license);
812   GST_LOG ("  desc.source='%s'", plugin->desc.source);
813   GST_LOG ("  desc.package='%s'", plugin->desc.package);
814   GST_LOG ("  desc.origin='%s'", plugin->desc.origin);
815   GST_LOG ("  desc.datetime=%s", plugin->desc.release_datetime);
816
817   if (plugin->desc.release_datetime[0] == '\0')
818     plugin->desc.release_datetime = NULL;
819
820   /* unpack cache data */
821   unpack_string_nocopy (*in, cache_str, end, fail);
822   if (cache_str != NULL && *cache_str != '\0')
823     plugin->priv->cache_data = gst_structure_from_string (cache_str, NULL);
824
825   /* If the license string is 'BLACKLIST', mark this as a blacklisted
826    * plugin */
827   if (strcmp (plugin->desc.license, "BLACKLIST") == 0)
828     plugin->flags |= GST_PLUGIN_FLAG_BLACKLISTED;
829
830   plugin->basename = g_path_get_basename (plugin->filename);
831
832   /* Takes ownership of plugin */
833   gst_registry_add_plugin (registry, plugin);
834   n = pe->nfeatures;
835   GST_DEBUG ("Added plugin '%s' plugin with %d features from binary registry",
836       plugin->desc.name, n);
837
838   /* Load plugin features */
839   for (i = 0; i < n; i++) {
840     if (G_UNLIKELY (!gst_registry_chunks_load_feature (registry, in, end,
841                 plugin))) {
842       GST_ERROR ("Error while loading binary feature for plugin '%s'",
843           GST_STR_NULL (plugin->desc.name));
844       gst_registry_remove_plugin (registry, plugin);
845       goto fail;
846     }
847   }
848
849   /* Load external plugin dependencies */
850   for (i = 0; i < pe->n_deps; ++i) {
851     if (G_UNLIKELY (!gst_registry_chunks_load_plugin_dep (plugin, in, end))) {
852       GST_ERROR_OBJECT (plugin, "Could not read external plugin dependency "
853           "for plugin '%s'", GST_STR_NULL (plugin->desc.name));
854       gst_registry_remove_plugin (registry, plugin);
855       goto fail;
856     }
857   }
858
859   if (out_plugin)
860     *out_plugin = plugin;
861
862   return TRUE;
863
864   /* Errors */
865 fail:
866   GST_INFO ("Reading plugin failed after %u bytes", (guint) (end - start));
867   return FALSE;
868 }
869
870 void
871 _priv_gst_registry_chunks_save_global_header (GList ** list,
872     GstRegistry * registry, guint32 filter_env_hash)
873 {
874   GstRegistryChunkGlobalHeader *hdr;
875   GstRegistryChunk *chk;
876
877   hdr = g_slice_new (GstRegistryChunkGlobalHeader);
878   chk = gst_registry_chunks_make_data (hdr,
879       sizeof (GstRegistryChunkGlobalHeader));
880
881   hdr->filter_env_hash = filter_env_hash;
882
883   *list = g_list_prepend (*list, chk);
884
885   GST_LOG ("Saved global header (filter_env_hash=0x%08x)", filter_env_hash);
886 }
887
888 gboolean
889 _priv_gst_registry_chunks_load_global_header (GstRegistry * registry,
890     gchar ** in, gchar * end, guint32 * filter_env_hash)
891 {
892   GstRegistryChunkGlobalHeader *hdr;
893
894   align (*in);
895   GST_LOG ("Reading/casting for GstRegistryChunkGlobalHeader at %p", *in);
896   unpack_element (*in, hdr, GstRegistryChunkGlobalHeader, end, fail);
897   *filter_env_hash = hdr->filter_env_hash;
898   return TRUE;
899
900   /* Errors */
901 fail:
902   GST_WARNING ("Reading global header failed");
903   return FALSE;
904 }