registrychunks: Fix format string for debug error message.
[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   if (G_UNLIKELY (len == maxlen))
52     return -1;
53
54   while (*str++ != '\0') {
55     len++;
56     if (G_UNLIKELY (len == maxlen))
57       return -1;
58   }
59   return len;
60 }
61
62 /* Macros */
63 #define unpack_element(inptr, outptr, element, endptr, error_label) G_STMT_START{ \
64   if (inptr + sizeof(element) > endptr) { \
65     GST_ERROR ("Failed reading element " G_STRINGIFY (element)  \
66         ". Have %" G_GSSIZE_FORMAT " bytes need %" G_GSSIZE_FORMAT, \
67         endptr - inptr, sizeof(element)); \
68     goto error_label; \
69   } \
70   outptr = (element *) inptr; \
71   inptr += sizeof (element); \
72 }G_STMT_END
73
74 #define unpack_const_string(inptr, outptr, endptr, error_label) G_STMT_START{\
75   gint _len = _strnlen (inptr, (endptr-inptr)); \
76   if (_len == -1) \
77     goto error_label; \
78   outptr = g_intern_string ((const gchar *)inptr); \
79   inptr += _len + 1; \
80 }G_STMT_END
81
82 #define unpack_string(inptr, outptr, endptr, error_label)  G_STMT_START{\
83   gint _len = _strnlen (inptr, (endptr-inptr)); \
84   if (_len == -1) \
85     goto error_label; \
86   outptr = g_memdup ((gconstpointer)inptr, _len + 1); \
87   inptr += _len + 1; \
88 }G_STMT_END
89
90 #define ALIGNMENT            (sizeof (void *))
91 #define alignment(_address)  (gsize)_address%ALIGNMENT
92 #define align(_ptr)          _ptr += (( alignment(_ptr) == 0) ? 0 : ALIGNMENT-alignment(_ptr))
93
94 /*
95  * gst_registry_chunks_save_const_string:
96  *
97  * Store a const string in a binary chunk.
98  *
99  * Returns: %TRUE for success
100  */
101 inline static gboolean
102 gst_registry_chunks_save_const_string (GList ** list, const gchar * str)
103 {
104   GstRegistryChunk *chunk;
105
106   if (G_UNLIKELY (str == NULL)) {
107     GST_ERROR ("unexpected NULL string in plugin or plugin feature data");
108     str = "";
109   }
110
111   chunk = g_malloc (sizeof (GstRegistryChunk));
112   chunk->data = (gpointer) str;
113   chunk->size = strlen ((gchar *) chunk->data) + 1;
114   chunk->flags = GST_REGISTRY_CHUNK_FLAG_CONST;
115   chunk->align = FALSE;
116   *list = g_list_prepend (*list, chunk);
117   return TRUE;
118 }
119
120 /*
121  * gst_registry_chunks_save_string:
122  *
123  * Store a string in a binary chunk.
124  *
125  * Returns: %TRUE for success
126  */
127 inline static gboolean
128 gst_registry_chunks_save_string (GList ** list, gchar * str)
129 {
130   GstRegistryChunk *chunk;
131
132   chunk = g_malloc (sizeof (GstRegistryChunk));
133   chunk->data = str;
134   chunk->size = strlen ((gchar *) chunk->data) + 1;
135   chunk->flags = GST_REGISTRY_CHUNK_FLAG_NONE;
136   chunk->align = FALSE;
137   *list = g_list_prepend (*list, chunk);
138   return TRUE;
139 }
140
141 /*
142  * gst_registry_chunks_save_data:
143  *
144  * Store some data in a binary chunk.
145  *
146  * Returns: the initialized chunk
147  */
148 inline static GstRegistryChunk *
149 gst_registry_chunks_make_data (gpointer data, gulong size)
150 {
151   GstRegistryChunk *chunk;
152
153   chunk = g_malloc (sizeof (GstRegistryChunk));
154   chunk->data = data;
155   chunk->size = size;
156   chunk->flags = GST_REGISTRY_CHUNK_FLAG_NONE;
157   chunk->align = TRUE;
158   return chunk;
159 }
160
161
162 /*
163  * gst_registry_chunks_save_pad_template:
164  *
165  * Store pad_templates in binary chunks.
166  *
167  * Returns: %TRUE for success
168  */
169 static gboolean
170 gst_registry_chunks_save_pad_template (GList ** list,
171     GstStaticPadTemplate * template)
172 {
173   GstRegistryChunkPadTemplate *pt;
174   GstRegistryChunk *chk;
175
176   pt = g_malloc0 (sizeof (GstRegistryChunkPadTemplate));
177   chk =
178       gst_registry_chunks_make_data (pt, sizeof (GstRegistryChunkPadTemplate));
179
180   pt->presence = template->presence;
181   pt->direction = template->direction;
182
183   /* pack pad template strings */
184   gst_registry_chunks_save_const_string (list,
185       (gchar *) (template->static_caps.string));
186   gst_registry_chunks_save_const_string (list, template->name_template);
187
188   *list = g_list_prepend (*list, chk);
189
190   return TRUE;
191 }
192
193 /*
194  * gst_registry_chunks_save_feature:
195  *
196  * Store features in binary chunks.
197  *
198  * Returns: %TRUE for success
199  */
200 static gboolean
201 gst_registry_chunks_save_feature (GList ** list, GstPluginFeature * feature)
202 {
203   const gchar *type_name = g_type_name (G_OBJECT_TYPE (feature));
204   GstRegistryChunkPluginFeature *pf = NULL;
205   GstRegistryChunk *chk = NULL;
206   GList *walk;
207
208   if (!type_name) {
209     GST_ERROR ("NULL feature type_name, aborting.");
210     return FALSE;
211   }
212
213   if (GST_IS_ELEMENT_FACTORY (feature)) {
214     GstRegistryChunkElementFactory *ef;
215     GstElementFactory *factory = GST_ELEMENT_FACTORY (feature);
216
217     ef = g_malloc0 (sizeof (GstRegistryChunkElementFactory));
218     chk =
219         gst_registry_chunks_make_data (ef,
220         sizeof (GstRegistryChunkElementFactory));
221     ef->npadtemplates = ef->ninterfaces = ef->nuriprotocols = 0;
222     pf = (GstRegistryChunkPluginFeature *) ef;
223
224     /* save interfaces */
225     for (walk = factory->interfaces; walk;
226         walk = g_list_next (walk), ef->ninterfaces++) {
227       gst_registry_chunks_save_const_string (list, (gchar *) walk->data);
228     }
229     GST_DEBUG ("Saved %d Interfaces", ef->ninterfaces);
230     /* save uritypes */
231     if (GST_URI_TYPE_IS_VALID (factory->uri_type)) {
232       if (factory->uri_protocols && *factory->uri_protocols) {
233         GstRegistryChunk *subchk;
234         gchar **protocol;
235
236         subchk =
237             gst_registry_chunks_make_data (&factory->uri_type,
238             sizeof (factory->uri_type));
239         subchk->flags = GST_REGISTRY_CHUNK_FLAG_CONST;
240
241         protocol = factory->uri_protocols;
242         while (*protocol) {
243           gst_registry_chunks_save_const_string (list, *protocol++);
244           ef->nuriprotocols++;
245         }
246         *list = g_list_prepend (*list, subchk);
247         GST_DEBUG ("Saved %d UriTypes", ef->nuriprotocols);
248       } else {
249         g_warning ("GStreamer feature '%s' is URI handler but does not provide"
250             " any protocols it can handle", feature->name);
251       }
252     }
253
254     /* save pad-templates */
255     for (walk = factory->staticpadtemplates; walk;
256         walk = g_list_next (walk), ef->npadtemplates++) {
257       GstStaticPadTemplate *template = walk->data;
258
259       if (!gst_registry_chunks_save_pad_template (list, template)) {
260         GST_ERROR ("Can't fill pad template, aborting.");
261         goto fail;
262       }
263     }
264
265     /* pack element factory strings */
266     gst_registry_chunks_save_const_string (list, factory->details.author);
267     gst_registry_chunks_save_const_string (list, factory->details.description);
268     gst_registry_chunks_save_const_string (list, factory->details.klass);
269     gst_registry_chunks_save_const_string (list, factory->details.longname);
270   } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
271     GstRegistryChunkTypeFindFactory *tff;
272     GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
273     gchar *str;
274
275     tff = g_malloc0 (sizeof (GstRegistryChunkTypeFindFactory));
276     chk =
277         gst_registry_chunks_make_data (tff,
278         sizeof (GstRegistryChunkTypeFindFactory));
279     tff->nextensions = 0;
280     pf = (GstRegistryChunkPluginFeature *) tff;
281
282     /* save extensions */
283     if (factory->extensions) {
284       while (factory->extensions[tff->nextensions]) {
285         gst_registry_chunks_save_const_string (list,
286             factory->extensions[tff->nextensions++]);
287       }
288     }
289     /* save caps */
290     if (factory->caps) {
291       /* we copy the caps here so we can simplify them before saving. This
292        * is a lot faster when loading them later on */
293       GstCaps *copy = gst_caps_copy (factory->caps);
294
295       gst_caps_do_simplify (copy);
296       str = gst_caps_to_string (copy);
297       gst_caps_unref (copy);
298       gst_registry_chunks_save_string (list, str);
299     } else {
300       gst_registry_chunks_save_const_string (list, "");
301     }
302   } else if (GST_IS_INDEX_FACTORY (feature)) {
303     GstIndexFactory *factory = GST_INDEX_FACTORY (feature);
304
305     pf = g_malloc0 (sizeof (GstRegistryChunkPluginFeature));
306     chk =
307         gst_registry_chunks_make_data (pf,
308         sizeof (GstRegistryChunkPluginFeature));
309     pf->rank = feature->rank;
310
311     /* pack element factory strings */
312     gst_registry_chunks_save_const_string (list, factory->longdesc);
313   } else {
314     GST_WARNING ("unhandled feature type '%s'", type_name);
315   }
316
317   if (pf) {
318     pf->rank = feature->rank;
319     *list = g_list_prepend (*list, chk);
320
321     /* pack plugin feature strings */
322     gst_registry_chunks_save_const_string (list, feature->name);
323     gst_registry_chunks_save_const_string (list, (gchar *) type_name);
324
325     return TRUE;
326   }
327
328   /* Errors */
329 fail:
330   g_free (chk);
331   g_free (pf);
332   return FALSE;
333 }
334
335 static gboolean
336 gst_registry_chunks_save_plugin_dep (GList ** list, GstPluginDep * dep)
337 {
338   GstRegistryChunkDep *ed;
339   GstRegistryChunk *chk;
340   gchar **s;
341
342   ed = g_malloc0 (sizeof (GstRegistryChunkDep));
343   chk = gst_registry_chunks_make_data (ed, sizeof (GstRegistryChunkDep));
344
345   ed->flags = dep->flags;
346   ed->n_env_vars = 0;
347   ed->n_paths = 0;
348   ed->n_names = 0;
349
350   ed->env_hash = dep->env_hash;
351   ed->stat_hash = dep->stat_hash;
352
353   for (s = dep->env_vars; s != NULL && *s != NULL; ++s, ++ed->n_env_vars)
354     gst_registry_chunks_save_string (list, g_strdup (*s));
355
356   for (s = dep->paths; s != NULL && *s != NULL; ++s, ++ed->n_paths)
357     gst_registry_chunks_save_string (list, g_strdup (*s));
358
359   for (s = dep->names; s != NULL && *s != NULL; ++s, ++ed->n_names)
360     gst_registry_chunks_save_string (list, g_strdup (*s));
361
362   *list = g_list_prepend (*list, chk);
363
364   GST_LOG ("Saved external plugin dependency");
365   return TRUE;
366 }
367
368 /*
369  * _priv_gst_registry_chunks_save_plugin:
370  *
371  * Adapt a GstPlugin to our GstRegistryChunkPluginElement structure, and
372  * prepend it as a GstRegistryChunk in the provided list.
373  *
374  */
375 gboolean
376 _priv_gst_registry_chunks_save_plugin (GList ** list, GstRegistry * registry,
377     GstPlugin * plugin)
378 {
379   GstRegistryChunkPluginElement *pe;
380   GstRegistryChunk *chk;
381   GList *plugin_features = NULL;
382   GList *walk;
383
384   pe = g_malloc0 (sizeof (GstRegistryChunkPluginElement));
385   chk =
386       gst_registry_chunks_make_data (pe,
387       sizeof (GstRegistryChunkPluginElement));
388
389   pe->file_size = plugin->file_size;
390   pe->file_mtime = plugin->file_mtime;
391   pe->nfeatures = 0;
392   pe->n_deps = 0;
393
394   /* pack external deps */
395   for (walk = plugin->priv->deps; walk != NULL; walk = walk->next) {
396     if (!gst_registry_chunks_save_plugin_dep (list, walk->data)) {
397       GST_ERROR ("Could not save external plugin dependency, aborting.");
398       goto fail;
399     }
400     ++pe->n_deps;
401   }
402
403   /* pack plugin features */
404   plugin_features =
405       gst_registry_get_feature_list_by_plugin (registry, plugin->desc.name);
406   for (walk = plugin_features; walk; walk = g_list_next (walk), pe->nfeatures++) {
407     GstPluginFeature *feature = GST_PLUGIN_FEATURE (walk->data);
408
409     if (!gst_registry_chunks_save_feature (list, feature)) {
410       GST_ERROR ("Can't fill plugin feature, aborting.");
411       goto fail;
412     }
413   }
414
415   gst_plugin_feature_list_free (plugin_features);
416
417   /* pack cache data */
418   if (plugin->priv->cache_data) {
419     gchar *cache_str = gst_structure_to_string (plugin->priv->cache_data);
420     gst_registry_chunks_save_string (list, cache_str);
421   } else {
422     gst_registry_chunks_save_const_string (list, "");
423   }
424
425   /* pack plugin element strings */
426   gst_registry_chunks_save_const_string (list, plugin->desc.origin);
427   gst_registry_chunks_save_const_string (list, plugin->desc.package);
428   gst_registry_chunks_save_const_string (list, plugin->desc.source);
429   gst_registry_chunks_save_const_string (list, plugin->desc.license);
430   gst_registry_chunks_save_const_string (list, plugin->desc.version);
431   gst_registry_chunks_save_const_string (list, plugin->filename);
432   gst_registry_chunks_save_const_string (list, plugin->desc.description);
433   gst_registry_chunks_save_const_string (list, plugin->desc.name);
434
435   *list = g_list_prepend (*list, chk);
436
437   GST_DEBUG ("Found %d features in plugin \"%s\"", pe->nfeatures,
438       plugin->desc.name);
439   return TRUE;
440
441   /* Errors */
442 fail:
443   gst_plugin_feature_list_free (plugin_features);
444   g_free (chk);
445   g_free (pe);
446   return FALSE;
447 }
448
449 /*
450  * gst_registry_chunks_load_pad_template:
451  *
452  * Make a new GstStaticPadTemplate from current GstRegistryChunkPadTemplate
453  * structure.
454  *
455  * Returns: new GstStaticPadTemplate
456  */
457 static gboolean
458 gst_registry_chunks_load_pad_template (GstElementFactory * factory, gchar ** in,
459     gchar * end)
460 {
461   GstRegistryChunkPadTemplate *pt;
462   GstStaticPadTemplate *template = NULL;
463
464   align (*in);
465   GST_DEBUG ("Reading/casting for GstRegistryChunkPadTemplate at address %p",
466       *in);
467   unpack_element (*in, pt, GstRegistryChunkPadTemplate, end, fail);
468
469   template = g_new0 (GstStaticPadTemplate, 1);
470   template->presence = pt->presence;
471   template->direction = pt->direction;
472
473   /* unpack pad template strings */
474   unpack_const_string (*in, template->name_template, end, fail);
475   unpack_string (*in, template->static_caps.string, end, fail);
476
477   __gst_element_factory_add_static_pad_template (factory, template);
478   GST_DEBUG ("Added pad_template %s", template->name_template);
479
480   return TRUE;
481 fail:
482   GST_INFO ("Reading pad template failed");
483   g_free (template);
484   return FALSE;
485 }
486
487 /*
488  * gst_registry_chunks_load_feature:
489  *
490  * Make a new GstPluginFeature from current binary plugin feature structure
491  *
492  * Returns: new GstPluginFeature
493  */
494 static gboolean
495 gst_registry_chunks_load_feature (GstRegistry * registry, gchar ** in,
496     gchar * end, const gchar * plugin_name)
497 {
498   GstRegistryChunkPluginFeature *pf = NULL;
499   GstPluginFeature *feature = NULL;
500   gchar *type_name = NULL, *str;
501   GType type;
502   guint i;
503
504   /* unpack plugin feature strings */
505   unpack_string (*in, type_name, end, fail);
506
507   if (G_UNLIKELY (!type_name)) {
508     GST_ERROR ("No feature type name");
509     return FALSE;
510   }
511
512   GST_DEBUG ("Plugin '%s' feature typename : '%s'", plugin_name, type_name);
513
514   if (G_UNLIKELY (!(type = g_type_from_name (type_name)))) {
515     GST_ERROR ("Unknown type from typename '%s' for plugin '%s'", type_name,
516         plugin_name);
517     g_free (type_name);
518     return FALSE;
519   }
520   if (G_UNLIKELY ((feature = g_object_new (type, NULL)) == NULL)) {
521     GST_ERROR ("Can't create feature from type");
522     g_free (type_name);
523     return FALSE;
524   }
525
526   if (G_UNLIKELY (!GST_IS_PLUGIN_FEATURE (feature))) {
527     GST_ERROR ("typename : '%s' is not a plugin feature", type_name);
528     goto fail;
529   }
530
531   /* unpack more plugin feature strings */
532   unpack_string (*in, feature->name, end, fail);
533
534   if (GST_IS_ELEMENT_FACTORY (feature)) {
535     GstRegistryChunkElementFactory *ef;
536     guint n;
537     GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (feature);
538
539     align (*in);
540     GST_LOG ("Reading/casting for GstRegistryChunkElementFactory at address %p",
541         *in);
542     unpack_element (*in, ef, GstRegistryChunkElementFactory, end, fail);
543     pf = (GstRegistryChunkPluginFeature *) ef;
544
545     /* unpack element factory strings */
546     unpack_string (*in, factory->details.longname, end, fail);
547     unpack_string (*in, factory->details.klass, end, fail);
548     unpack_string (*in, factory->details.description, end, fail);
549     unpack_string (*in, factory->details.author, end, fail);
550     n = ef->npadtemplates;
551     GST_DEBUG ("Element factory : '%s' with npadtemplates=%d",
552         factory->details.longname, n);
553
554     /* load pad templates */
555     for (i = 0; i < n; i++) {
556       if (G_UNLIKELY (!gst_registry_chunks_load_pad_template (factory, in,
557                   end))) {
558         GST_ERROR ("Error while loading binary pad template");
559         goto fail;
560       }
561     }
562
563     /* load uritypes */
564     if (G_UNLIKELY ((n = ef->nuriprotocols))) {
565       GST_DEBUG ("Reading %d UriTypes at address %p", n, *in);
566
567       align (*in);
568       factory->uri_type = *((guint *) * in);
569       *in += sizeof (factory->uri_type);
570       /*unpack_element(*in, &factory->uri_type, factory->uri_type, end, fail); */
571
572       factory->uri_protocols = g_new0 (gchar *, n + 1);
573       for (i = 0; i < n; i++) {
574         unpack_string (*in, str, end, fail);
575         factory->uri_protocols[i] = str;
576       }
577     }
578     /* load interfaces */
579     if (G_UNLIKELY ((n = ef->ninterfaces))) {
580       GST_DEBUG ("Reading %d Interfaces at address %p", n, *in);
581       for (i = 0; i < n; i++) {
582         unpack_string (*in, str, end, fail);
583         __gst_element_factory_add_interface (factory, str);
584         g_free (str);
585       }
586     }
587   } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
588     GstRegistryChunkTypeFindFactory *tff;
589     GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
590
591     align (*in);
592     GST_DEBUG
593         ("Reading/casting for GstRegistryChunkPluginFeature at address %p",
594         *in);
595     unpack_element (*in, tff, GstRegistryChunkTypeFindFactory, end, fail);
596     pf = (GstRegistryChunkPluginFeature *) tff;
597
598     /* load caps */
599     unpack_string (*in, str, end, fail);
600     factory->caps = (str && *str) ? gst_caps_from_string (str) : NULL;
601     g_free (str);
602     /* load extensions */
603     if (tff->nextensions) {
604       GST_DEBUG ("Reading %d Typefind extensions at address %p",
605           tff->nextensions, *in);
606       factory->extensions = g_new0 (gchar *, tff->nextensions + 1);
607       for (i = 0; i < tff->nextensions; i++) {
608         unpack_string (*in, str, end, fail);
609         factory->extensions[i] = str;
610       }
611     }
612   } else if (GST_IS_INDEX_FACTORY (feature)) {
613     GstIndexFactory *factory = GST_INDEX_FACTORY (feature);
614
615     align (*in);
616     GST_DEBUG
617         ("Reading/casting for GstRegistryChunkPluginFeature at address %p",
618         *in);
619     unpack_element (*in, pf, GstRegistryChunkPluginFeature, end, fail);
620
621     /* unpack index factory strings */
622     unpack_string (*in, factory->longdesc, end, fail);
623   } else {
624     GST_WARNING ("unhandled factory type : %s", G_OBJECT_TYPE_NAME (feature));
625     goto fail;
626   }
627
628   feature->rank = pf->rank;
629
630   /* should already be the interned string, but better make sure */
631   feature->plugin_name = g_intern_string (plugin_name);
632
633   gst_registry_add_feature (registry, feature);
634   GST_DEBUG ("Added feature %s", feature->name);
635
636   g_free (type_name);
637   return TRUE;
638
639   /* Errors */
640 fail:
641   GST_INFO ("Reading plugin feature failed");
642   g_free (type_name);
643   if (feature) {
644     if (GST_IS_OBJECT (feature))
645       gst_object_unref (feature);
646     else
647       g_object_unref (feature);
648   }
649   return FALSE;
650 }
651
652 static gchar **
653 gst_registry_chunks_load_plugin_dep_strv (gchar ** in, gchar * end, guint n)
654 {
655   gchar **arr;
656
657   if (n == 0)
658     return NULL;
659
660   arr = g_new0 (gchar *, n + 1);
661   while (n > 0) {
662     unpack_string (*in, arr[n - 1], end, fail);
663     --n;
664   }
665   return arr;
666 fail:
667   GST_INFO ("Reading plugin dependency strings failed");
668   return NULL;
669 }
670
671 static gboolean
672 gst_registry_chunks_load_plugin_dep (GstPlugin * plugin, gchar ** in,
673     gchar * end)
674 {
675   GstPluginDep *dep;
676   GstRegistryChunkDep *d;
677   gchar **s;
678
679   align (*in);
680   GST_LOG_OBJECT (plugin, "Unpacking GstRegistryChunkDep from %p", *in);
681   unpack_element (*in, d, GstRegistryChunkDep, end, fail);
682
683   dep = g_malloc0 (sizeof (GstPluginDep));
684
685   dep->env_hash = d->env_hash;
686   dep->stat_hash = d->stat_hash;
687
688   dep->flags = d->flags;
689
690   dep->names = gst_registry_chunks_load_plugin_dep_strv (in, end, d->n_names);
691   dep->paths = gst_registry_chunks_load_plugin_dep_strv (in, end, d->n_paths);
692   dep->env_vars =
693       gst_registry_chunks_load_plugin_dep_strv (in, end, d->n_env_vars);
694
695   plugin->priv->deps = g_list_append (plugin->priv->deps, dep);
696
697   GST_DEBUG_OBJECT (plugin, "Loaded external plugin dependency from registry: "
698       "env_hash: %08x, stat_hash: %08x", dep->env_hash, dep->stat_hash);
699   for (s = dep->env_vars; s != NULL && *s != NULL; ++s)
700     GST_LOG_OBJECT (plugin, " evar: %s", *s);
701   for (s = dep->paths; s != NULL && *s != NULL; ++s)
702     GST_LOG_OBJECT (plugin, " path: %s", *s);
703   for (s = dep->names; s != NULL && *s != NULL; ++s)
704     GST_LOG_OBJECT (plugin, " name: %s", *s);
705
706   return TRUE;
707 fail:
708   GST_INFO ("Reading plugin dependency failed");
709   return FALSE;
710 }
711
712
713 /*
714  * _priv_gst_registry_chunks_load_plugin:
715  *
716  * Make a new GstPlugin from current GstRegistryChunkPluginElement structure
717  * and add it to the GstRegistry. Return an offset to the next
718  * GstRegistryChunkPluginElement structure.
719  */
720 gboolean
721 _priv_gst_registry_chunks_load_plugin (GstRegistry * registry, gchar ** in,
722     gchar * end, GstPlugin ** out_plugin)
723 {
724   GstRegistryChunkPluginElement *pe;
725   GstPlugin *plugin = NULL;
726   gchar *cache_str = NULL;
727   guint i, n;
728
729   align (*in);
730   GST_LOG ("Reading/casting for GstRegistryChunkPluginElement at address %p",
731       *in);
732   unpack_element (*in, pe, GstRegistryChunkPluginElement, end, fail);
733
734   plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
735
736   /* TODO: also set GST_PLUGIN_FLAG_CONST */
737   plugin->flags |= GST_PLUGIN_FLAG_CACHED;
738   plugin->file_mtime = pe->file_mtime;
739   plugin->file_size = pe->file_size;
740
741   /* unpack plugin element strings */
742   unpack_const_string (*in, plugin->desc.name, end, fail);
743   unpack_string (*in, plugin->desc.description, end, fail);
744   unpack_string (*in, plugin->filename, end, fail);
745   unpack_const_string (*in, plugin->desc.version, end, fail);
746   unpack_const_string (*in, plugin->desc.license, end, fail);
747   unpack_const_string (*in, plugin->desc.source, end, fail);
748   unpack_const_string (*in, plugin->desc.package, end, fail);
749   unpack_const_string (*in, plugin->desc.origin, end, fail);
750   GST_LOG ("read strings for name='%s'", plugin->desc.name);
751   GST_LOG ("  desc.description='%s'", plugin->desc.description);
752   GST_LOG ("  filename='%s'", plugin->filename);
753   GST_LOG ("  desc.version='%s'", plugin->desc.version);
754   GST_LOG ("  desc.license='%s'", plugin->desc.license);
755   GST_LOG ("  desc.source='%s'", plugin->desc.source);
756   GST_LOG ("  desc.package='%s'", plugin->desc.package);
757   GST_LOG ("  desc.origin='%s'", plugin->desc.origin);
758
759   /* unpack cache data */
760   unpack_string (*in, cache_str, end, fail);
761   if (*cache_str) {
762     plugin->priv->cache_data = gst_structure_from_string (cache_str, NULL);
763   }
764   g_free (cache_str);
765
766   /* If the license string is 'BLACKLIST', mark this as a blacklisted
767    * plugin */
768   if (strcmp (plugin->desc.license, "BLACKLIST") == 0)
769     plugin->flags |= GST_PLUGIN_FLAG_BLACKLISTED;
770
771   plugin->basename = g_path_get_basename (plugin->filename);
772
773   /* Takes ownership of plugin */
774   gst_registry_add_plugin (registry, plugin);
775   n = pe->nfeatures;
776   GST_DEBUG ("Added plugin '%s' plugin with %d features from binary registry",
777       plugin->desc.name, n);
778
779   /* Load plugin features */
780   for (i = 0; i < n; i++) {
781     if (G_UNLIKELY (!gst_registry_chunks_load_feature (registry, in, end,
782                 plugin->desc.name))) {
783       GST_ERROR ("Error while loading binary feature for plugin '%s'",
784           GST_STR_NULL (plugin->desc.name));
785       gst_registry_remove_plugin (registry, plugin);
786       goto fail;
787     }
788   }
789
790   /* Load external plugin dependencies */
791   for (i = 0; i < pe->n_deps; ++i) {
792     if (G_UNLIKELY (!gst_registry_chunks_load_plugin_dep (plugin, in, end))) {
793       GST_ERROR_OBJECT (plugin, "Could not read external plugin dependency "
794           "for plugin '%s'", GST_STR_NULL (plugin->desc.name));
795       gst_registry_remove_plugin (registry, plugin);
796       goto fail;
797     }
798   }
799
800   if (out_plugin)
801     *out_plugin = plugin;
802
803   return TRUE;
804
805   /* Errors */
806 fail:
807   GST_INFO ("Reading plugin failed");
808   return FALSE;
809 }