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