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