registrychunks: Fix off-by-one error. Improve debug.
[platform/upstream/gstreamer.git] / gst / gstregistrychunks.c
1 /* GStreamer
2  * Copyright (C) 2006 Josep Torra <josep@fluendo.com>
3  *               2006 Mathieu Garcia <matthieu@fluendo.com>
4  *               2006,2007 Stefan Kost <ensonic@users.sf.net>
5  *               2008 Sebastian Dröge <slomo@circular-chaos.org>
6  *               2008 Jan Schmidt <jan.schmidt@sun.com>
7  *
8  * gstregistrychunks.c: GstRegistryChunk helper for serialising/deserialising
9  * plugin entries and features.
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #  include "config.h"
29 #endif
30
31 #include <gst/gst_private.h>
32 #include <gst/gstconfig.h>
33 #include <gst/gstelement.h>
34 #include <gst/gsttypefind.h>
35 #include <gst/gsttypefindfactory.h>
36 #include <gst/gsturi.h>
37 #include <gst/gstinfo.h>
38 #include <gst/gstenumtypes.h>
39 #include <gst/gstpadtemplate.h>
40
41 #include <gst/gstregistrychunks.h>
42
43 #define GST_CAT_DEFAULT GST_CAT_REGISTRY
44
45 /* count string length, but return -1 if we hit the eof */
46 static gint
47 _strnlen (const gchar * str, gint maxlen)
48 {
49   gint len = 0;
50
51   if (G_UNLIKELY (len == maxlen))
52     return -1;
53
54   while (*str++ != '\0') {
55     len++;
56     if (G_UNLIKELY (len == maxlen))
57       return -1;
58   }
59   return len;
60 }
61
62 /* Macros */
63 #define unpack_element(inptr, outptr, element, endptr, error_label) G_STMT_START{ \
64   if (inptr + sizeof(element) > endptr) { \
65     GST_ERROR ("Failed reading element " G_STRINGIFY (element)  \
66         ". Have %d bytes need %d", endptr - inptr, sizeof(element)); \
67     goto error_label; \
68   } \
69   outptr = (element *) inptr; \
70   inptr += sizeof (element); \
71 }G_STMT_END
72
73 #define unpack_const_string(inptr, outptr, endptr, error_label) G_STMT_START{\
74   gint _len = _strnlen (inptr, (endptr-inptr)); \
75   if (_len == -1) \
76     goto error_label; \
77   outptr = g_intern_string ((const gchar *)inptr); \
78   inptr += _len + 1; \
79 }G_STMT_END
80
81 #define unpack_string(inptr, outptr, endptr, error_label)  G_STMT_START{\
82   gint _len = _strnlen (inptr, (endptr-inptr)); \
83   if (_len == -1) \
84     goto error_label; \
85   outptr = g_memdup ((gconstpointer)inptr, _len + 1); \
86   inptr += _len + 1; \
87 }G_STMT_END
88
89 #define ALIGNMENT            (sizeof (void *))
90 #define alignment(_address)  (gsize)_address%ALIGNMENT
91 #define align(_ptr)          _ptr += (( alignment(_ptr) == 0) ? 0 : ALIGNMENT-alignment(_ptr))
92
93 /*
94  * gst_registry_chunks_save_const_string:
95  *
96  * Store a const string in a binary chunk.
97  *
98  * Returns: %TRUE for success
99  */
100 inline static gboolean
101 gst_registry_chunks_save_const_string (GList ** list, const gchar * str)
102 {
103   GstRegistryChunk *chunk;
104
105   if (G_UNLIKELY (str == NULL)) {
106     GST_ERROR ("unexpected NULL string in plugin or plugin feature data");
107     str = "";
108   }
109
110   chunk = g_malloc (sizeof (GstRegistryChunk));
111   chunk->data = (gpointer) str;
112   chunk->size = strlen ((gchar *) chunk->data) + 1;
113   chunk->flags = GST_REGISTRY_CHUNK_FLAG_CONST;
114   chunk->align = FALSE;
115   *list = g_list_prepend (*list, chunk);
116   return TRUE;
117 }
118
119 /*
120  * gst_registry_chunks_save_string:
121  *
122  * Store a string in a binary chunk.
123  *
124  * Returns: %TRUE for success
125  */
126 inline static gboolean
127 gst_registry_chunks_save_string (GList ** list, gchar * str)
128 {
129   GstRegistryChunk *chunk;
130
131   chunk = g_malloc (sizeof (GstRegistryChunk));
132   chunk->data = str;
133   chunk->size = strlen ((gchar *) chunk->data) + 1;
134   chunk->flags = GST_REGISTRY_CHUNK_FLAG_NONE;
135   chunk->align = FALSE;
136   *list = g_list_prepend (*list, chunk);
137   return TRUE;
138 }
139
140 /*
141  * gst_registry_chunks_save_data:
142  *
143  * Store some data in a binary chunk.
144  *
145  * Returns: the initialized chunk
146  */
147 inline static GstRegistryChunk *
148 gst_registry_chunks_make_data (gpointer data, gulong size)
149 {
150   GstRegistryChunk *chunk;
151
152   chunk = g_malloc (sizeof (GstRegistryChunk));
153   chunk->data = data;
154   chunk->size = size;
155   chunk->flags = GST_REGISTRY_CHUNK_FLAG_NONE;
156   chunk->align = TRUE;
157   return chunk;
158 }
159
160
161 /*
162  * gst_registry_chunks_save_pad_template:
163  *
164  * Store pad_templates in binary chunks.
165  *
166  * Returns: %TRUE for success
167  */
168 static gboolean
169 gst_registry_chunks_save_pad_template (GList ** list,
170     GstStaticPadTemplate * template)
171 {
172   GstRegistryChunkPadTemplate *pt;
173   GstRegistryChunk *chk;
174
175   pt = g_malloc0 (sizeof (GstRegistryChunkPadTemplate));
176   chk =
177       gst_registry_chunks_make_data (pt, sizeof (GstRegistryChunkPadTemplate));
178
179   pt->presence = template->presence;
180   pt->direction = template->direction;
181
182   /* pack pad template strings */
183   gst_registry_chunks_save_const_string (list,
184       (gchar *) (template->static_caps.string));
185   gst_registry_chunks_save_const_string (list, template->name_template);
186
187   *list = g_list_prepend (*list, chk);
188
189   return TRUE;
190 }
191
192 /*
193  * gst_registry_chunks_save_feature:
194  *
195  * Store features in binary chunks.
196  *
197  * Returns: %TRUE for success
198  */
199 static gboolean
200 gst_registry_chunks_save_feature (GList ** list, GstPluginFeature * feature)
201 {
202   const gchar *type_name = g_type_name (G_OBJECT_TYPE (feature));
203   GstRegistryChunkPluginFeature *pf = NULL;
204   GstRegistryChunk *chk = NULL;
205   GList *walk;
206
207   if (!type_name) {
208     GST_ERROR ("NULL feature type_name, aborting.");
209     return FALSE;
210   }
211
212   if (GST_IS_ELEMENT_FACTORY (feature)) {
213     GstRegistryChunkElementFactory *ef;
214     GstElementFactory *factory = GST_ELEMENT_FACTORY (feature);
215
216     ef = g_malloc0 (sizeof (GstRegistryChunkElementFactory));
217     chk =
218         gst_registry_chunks_make_data (ef,
219         sizeof (GstRegistryChunkElementFactory));
220     ef->npadtemplates = ef->ninterfaces = ef->nuriprotocols = 0;
221     pf = (GstRegistryChunkPluginFeature *) ef;
222
223     /* save interfaces */
224     for (walk = factory->interfaces; walk;
225         walk = g_list_next (walk), ef->ninterfaces++) {
226       gst_registry_chunks_save_const_string (list, (gchar *) walk->data);
227     }
228     GST_DEBUG ("Saved %d Interfaces", ef->ninterfaces);
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   GType type;
501   guint i;
502
503   /* unpack plugin feature strings */
504   unpack_string (*in, type_name, end, fail);
505
506   if (G_UNLIKELY (!type_name)) {
507     GST_ERROR ("No feature type name");
508     return FALSE;
509   }
510
511   GST_DEBUG ("Plugin '%s' feature typename : '%s'", plugin_name, type_name);
512
513   if (G_UNLIKELY (!(type = g_type_from_name (type_name)))) {
514     GST_ERROR ("Unknown type from typename '%s' for plugin '%s'", type_name,
515         plugin_name);
516     g_free (type_name);
517     return FALSE;
518   }
519   if (G_UNLIKELY ((feature = g_object_new (type, NULL)) == NULL)) {
520     GST_ERROR ("Can't create feature from type");
521     g_free (type_name);
522     return FALSE;
523   }
524
525   if (G_UNLIKELY (!GST_IS_PLUGIN_FEATURE (feature))) {
526     GST_ERROR ("typename : '%s' is not a plugin feature", type_name);
527     goto fail;
528   }
529
530   /* unpack more plugin feature strings */
531   unpack_string (*in, feature->name, end, fail);
532
533   if (GST_IS_ELEMENT_FACTORY (feature)) {
534     GstRegistryChunkElementFactory *ef;
535     guint n;
536     GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (feature);
537
538     align (*in);
539     GST_LOG ("Reading/casting for GstRegistryChunkElementFactory at address %p",
540         *in);
541     unpack_element (*in, ef, GstRegistryChunkElementFactory, end, fail);
542     pf = (GstRegistryChunkPluginFeature *) ef;
543
544     /* unpack element factory strings */
545     unpack_string (*in, factory->details.longname, end, fail);
546     unpack_string (*in, factory->details.klass, end, fail);
547     unpack_string (*in, factory->details.description, end, fail);
548     unpack_string (*in, factory->details.author, end, fail);
549     n = ef->npadtemplates;
550     GST_DEBUG ("Element factory : '%s' with npadtemplates=%d",
551         factory->details.longname, n);
552
553     /* load pad templates */
554     for (i = 0; i < n; i++) {
555       if (G_UNLIKELY (!gst_registry_chunks_load_pad_template (factory, in,
556                   end))) {
557         GST_ERROR ("Error while loading binary pad template");
558         goto fail;
559       }
560     }
561
562     /* load uritypes */
563     if (G_UNLIKELY ((n = ef->nuriprotocols))) {
564       GST_DEBUG ("Reading %d UriTypes at address %p", n, *in);
565
566       align (*in);
567       factory->uri_type = *((guint *) * in);
568       *in += sizeof (factory->uri_type);
569       /*unpack_element(*in, &factory->uri_type, factory->uri_type, end, fail); */
570
571       factory->uri_protocols = g_new0 (gchar *, n + 1);
572       for (i = 0; i < n; i++) {
573         unpack_string (*in, str, end, fail);
574         factory->uri_protocols[i] = str;
575       }
576     }
577     /* load interfaces */
578     if (G_UNLIKELY ((n = ef->ninterfaces))) {
579       GST_DEBUG ("Reading %d Interfaces at address %p", n, *in);
580       for (i = 0; i < n; i++) {
581         unpack_string (*in, str, end, fail);
582         __gst_element_factory_add_interface (factory, str);
583         g_free (str);
584       }
585     }
586   } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
587     GstRegistryChunkTypeFindFactory *tff;
588     GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
589
590     align (*in);
591     GST_DEBUG
592         ("Reading/casting for GstRegistryChunkPluginFeature at address %p",
593         *in);
594     unpack_element (*in, tff, GstRegistryChunkTypeFindFactory, end, fail);
595     pf = (GstRegistryChunkPluginFeature *) tff;
596
597     /* load caps */
598     unpack_string (*in, str, end, fail);
599     factory->caps = (str && *str) ? gst_caps_from_string (str) : NULL;
600     g_free (str);
601     /* load extensions */
602     if (tff->nextensions) {
603       GST_DEBUG ("Reading %d Typefind extensions at address %p",
604           tff->nextensions, *in);
605       factory->extensions = g_new0 (gchar *, tff->nextensions + 1);
606       for (i = 0; i < tff->nextensions; i++) {
607         unpack_string (*in, str, end, fail);
608         factory->extensions[i] = str;
609       }
610     }
611   } else if (GST_IS_INDEX_FACTORY (feature)) {
612     GstIndexFactory *factory = GST_INDEX_FACTORY (feature);
613
614     align (*in);
615     GST_DEBUG
616         ("Reading/casting for GstRegistryChunkPluginFeature at address %p",
617         *in);
618     unpack_element (*in, pf, GstRegistryChunkPluginFeature, end, fail);
619
620     /* unpack index factory strings */
621     unpack_string (*in, factory->longdesc, end, fail);
622   } else {
623     GST_WARNING ("unhandled factory type : %s", G_OBJECT_TYPE_NAME (feature));
624     goto fail;
625   }
626
627   feature->rank = pf->rank;
628
629   /* should already be the interned string, but better make sure */
630   feature->plugin_name = g_intern_string (plugin_name);
631
632   gst_registry_add_feature (registry, feature);
633   GST_DEBUG ("Added feature %s", feature->name);
634
635   g_free (type_name);
636   return TRUE;
637
638   /* Errors */
639 fail:
640   GST_INFO ("Reading plugin feature failed");
641   g_free (type_name);
642   if (feature) {
643     if (GST_IS_OBJECT (feature))
644       gst_object_unref (feature);
645     else
646       g_object_unref (feature);
647   }
648   return FALSE;
649 }
650
651 static gchar **
652 gst_registry_chunks_load_plugin_dep_strv (gchar ** in, gchar * end, guint n)
653 {
654   gchar **arr;
655
656   if (n == 0)
657     return NULL;
658
659   arr = g_new0 (gchar *, n + 1);
660   while (n > 0) {
661     unpack_string (*in, arr[n - 1], end, fail);
662     --n;
663   }
664   return arr;
665 fail:
666   GST_INFO ("Reading plugin dependency strings failed");
667   return NULL;
668 }
669
670 static gboolean
671 gst_registry_chunks_load_plugin_dep (GstPlugin * plugin, gchar ** in,
672     gchar * end)
673 {
674   GstPluginDep *dep;
675   GstRegistryChunkDep *d;
676   gchar **s;
677
678   align (*in);
679   GST_LOG_OBJECT (plugin, "Unpacking GstRegistryChunkDep from %p", *in);
680   unpack_element (*in, d, GstRegistryChunkDep, end, fail);
681
682   dep = g_malloc0 (sizeof (GstPluginDep));
683
684   dep->env_hash = d->env_hash;
685   dep->stat_hash = d->stat_hash;
686
687   dep->flags = d->flags;
688
689   dep->names = gst_registry_chunks_load_plugin_dep_strv (in, end, d->n_names);
690   dep->paths = gst_registry_chunks_load_plugin_dep_strv (in, end, d->n_paths);
691   dep->env_vars =
692       gst_registry_chunks_load_plugin_dep_strv (in, end, d->n_env_vars);
693
694   plugin->priv->deps = g_list_append (plugin->priv->deps, dep);
695
696   GST_DEBUG_OBJECT (plugin, "Loaded external plugin dependency from registry: "
697       "env_hash: %08x, stat_hash: %08x", dep->env_hash, dep->stat_hash);
698   for (s = dep->env_vars; s != NULL && *s != NULL; ++s)
699     GST_LOG_OBJECT (plugin, " evar: %s", *s);
700   for (s = dep->paths; s != NULL && *s != NULL; ++s)
701     GST_LOG_OBJECT (plugin, " path: %s", *s);
702   for (s = dep->names; s != NULL && *s != NULL; ++s)
703     GST_LOG_OBJECT (plugin, " name: %s", *s);
704
705   return TRUE;
706 fail:
707   GST_INFO ("Reading plugin dependency failed");
708   return FALSE;
709 }
710
711
712 /*
713  * _priv_gst_registry_chunks_load_plugin:
714  *
715  * Make a new GstPlugin from current GstRegistryChunkPluginElement structure
716  * and add it to the GstRegistry. Return an offset to the next
717  * GstRegistryChunkPluginElement structure.
718  */
719 gboolean
720 _priv_gst_registry_chunks_load_plugin (GstRegistry * registry, gchar ** in,
721     gchar * end, GstPlugin ** out_plugin)
722 {
723   GstRegistryChunkPluginElement *pe;
724   GstPlugin *plugin = NULL;
725   gchar *cache_str = NULL;
726   guint i, n;
727
728   align (*in);
729   GST_LOG ("Reading/casting for GstRegistryChunkPluginElement at address %p",
730       *in);
731   unpack_element (*in, pe, GstRegistryChunkPluginElement, end, fail);
732
733   plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
734
735   /* TODO: also set GST_PLUGIN_FLAG_CONST */
736   plugin->flags |= GST_PLUGIN_FLAG_CACHED;
737   plugin->file_mtime = pe->file_mtime;
738   plugin->file_size = pe->file_size;
739
740   /* unpack plugin element strings */
741   unpack_const_string (*in, plugin->desc.name, end, fail);
742   unpack_string (*in, plugin->desc.description, end, fail);
743   unpack_string (*in, plugin->filename, end, fail);
744   unpack_const_string (*in, plugin->desc.version, end, fail);
745   unpack_const_string (*in, plugin->desc.license, end, fail);
746   unpack_const_string (*in, plugin->desc.source, end, fail);
747   unpack_const_string (*in, plugin->desc.package, end, fail);
748   unpack_const_string (*in, plugin->desc.origin, end, fail);
749   GST_LOG ("read strings for name='%s'", plugin->desc.name);
750   GST_LOG ("  desc.description='%s'", plugin->desc.description);
751   GST_LOG ("  filename='%s'", plugin->filename);
752   GST_LOG ("  desc.version='%s'", plugin->desc.version);
753   GST_LOG ("  desc.license='%s'", plugin->desc.license);
754   GST_LOG ("  desc.source='%s'", plugin->desc.source);
755   GST_LOG ("  desc.package='%s'", plugin->desc.package);
756   GST_LOG ("  desc.origin='%s'", plugin->desc.origin);
757
758   /* unpack cache data */
759   unpack_string (*in, cache_str, end, fail);
760   if (*cache_str) {
761     plugin->priv->cache_data = gst_structure_from_string (cache_str, NULL);
762   }
763   g_free (cache_str);
764
765   /* If the license string is 'BLACKLIST', mark this as a blacklisted
766    * plugin */
767   if (strcmp (plugin->desc.license, "BLACKLIST") == 0)
768     plugin->flags |= GST_PLUGIN_FLAG_BLACKLISTED;
769
770   plugin->basename = g_path_get_basename (plugin->filename);
771
772   /* Takes ownership of plugin */
773   gst_registry_add_plugin (registry, plugin);
774   n = pe->nfeatures;
775   GST_DEBUG ("Added plugin '%s' plugin with %d features from binary registry",
776       plugin->desc.name, n);
777
778   /* Load plugin features */
779   for (i = 0; i < n; i++) {
780     if (G_UNLIKELY (!gst_registry_chunks_load_feature (registry, in, end,
781                 plugin->desc.name))) {
782       GST_ERROR ("Error while loading binary feature for plugin '%s'",
783           GST_STR_NULL (plugin->desc.name));
784       gst_registry_remove_plugin (registry, plugin);
785       goto fail;
786     }
787   }
788
789   /* Load external plugin dependencies */
790   for (i = 0; i < pe->n_deps; ++i) {
791     if (G_UNLIKELY (!gst_registry_chunks_load_plugin_dep (plugin, in, end))) {
792       GST_ERROR_OBJECT (plugin, "Could not read external plugin dependency "
793           "for plugin '%s'", GST_STR_NULL (plugin->desc.name));
794       gst_registry_remove_plugin (registry, plugin);
795       goto fail;
796     }
797   }
798
799   if (out_plugin)
800     *out_plugin = plugin;
801
802   return TRUE;
803
804   /* Errors */
805 fail:
806   GST_INFO ("Reading plugin failed");
807   return FALSE;
808 }