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