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