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