fcacd6b6dc00ffe4c447c5286d0d405d70b0a999
[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., 51 Franklin St, Fifth Floor,
24  * Boston, MA 02110-1301, 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/gstdevicemonitorfactory.h>
37 #include <gst/gsturi.h>
38 #include <gst/gstinfo.h>
39 #include <gst/gstenumtypes.h>
40 #include <gst/gstpadtemplate.h>
41
42 #include <gst/gstregistrychunks.h>
43
44 #define GST_CAT_DEFAULT GST_CAT_REGISTRY
45
46 /* count string length, but return -1 if we hit the eof */
47 static gint
48 _strnlen (const gchar * str, gint maxlen)
49 {
50   gint len = 0;
51
52   while (G_LIKELY (len < maxlen)) {
53     if (G_UNLIKELY (str[len] == '\0'))
54       return len;
55     len++;
56   }
57   return -1;
58 }
59
60 /* Macros */
61 #define unpack_element(inptr, outptr, element, endptr, error_label) G_STMT_START{ \
62   if (inptr + sizeof(element) > endptr) { \
63     GST_ERROR ("Failed reading element " G_STRINGIFY (element)  \
64         ". Have %d bytes need %" G_GSIZE_FORMAT, \
65         (int) (endptr - inptr), sizeof(element)); \
66     goto error_label; \
67   } \
68   outptr = (element *) inptr; \
69   inptr += sizeof (element); \
70 }G_STMT_END
71
72 #define unpack_const_string(inptr, outptr, endptr, error_label) G_STMT_START{\
73   gint _len = _strnlen (inptr, (endptr-inptr)); \
74   if (_len == -1) \
75     goto error_label; \
76   outptr = g_intern_string ((const gchar *)inptr); \
77   inptr += _len + 1; \
78 }G_STMT_END
79
80 #define unpack_string(inptr, outptr, endptr, error_label)  G_STMT_START{\
81   gint _len = _strnlen (inptr, (endptr-inptr)); \
82   if (_len == -1) \
83     goto error_label; \
84   outptr = g_memdup ((gconstpointer)inptr, _len + 1); \
85   inptr += _len + 1; \
86 }G_STMT_END
87
88 #define unpack_string_nocopy(inptr, outptr, endptr, error_label)  G_STMT_START{\
89   gint _len = _strnlen (inptr, (endptr-inptr)); \
90   if (_len == -1) \
91     goto error_label; \
92   outptr = (const gchar *)inptr; \
93   inptr += _len + 1; \
94 }G_STMT_END
95
96 #define ALIGNMENT            (sizeof (void *))
97 #define alignment(_address)  (gsize)_address%ALIGNMENT
98 #define align(_ptr)          _ptr += (( alignment(_ptr) == 0) ? 0 : ALIGNMENT-alignment(_ptr))
99
100 void
101 _priv_gst_registry_chunk_free (GstRegistryChunk * chunk)
102 {
103   if (!(chunk->flags & GST_REGISTRY_CHUNK_FLAG_CONST)) {
104     if ((chunk->flags & GST_REGISTRY_CHUNK_FLAG_MALLOC))
105       g_free (chunk->data);
106     else
107       g_slice_free1 (chunk->size, chunk->data);
108   }
109   g_slice_free (GstRegistryChunk, chunk);
110 }
111
112 /*
113  * gst_registry_chunks_save_const_string:
114  *
115  * Store a const string in a binary chunk.
116  *
117  * Returns: %TRUE for success
118  */
119 inline static gboolean
120 gst_registry_chunks_save_const_string (GList ** list, const gchar * str)
121 {
122   GstRegistryChunk *chunk;
123
124   if (G_UNLIKELY (str == NULL)) {
125     GST_ERROR ("unexpected NULL string in plugin or plugin feature data");
126     str = "";
127   }
128
129   chunk = g_slice_new (GstRegistryChunk);
130   chunk->data = (gpointer) str;
131   chunk->size = strlen ((gchar *) chunk->data) + 1;
132   chunk->flags = GST_REGISTRY_CHUNK_FLAG_CONST;
133   chunk->align = FALSE;
134   *list = g_list_prepend (*list, chunk);
135   return TRUE;
136 }
137
138 /*
139  * gst_registry_chunks_save_string:
140  *
141  * Store a string in a binary chunk.
142  *
143  * Returns: %TRUE for success
144  */
145 inline static gboolean
146 gst_registry_chunks_save_string (GList ** list, gchar * str)
147 {
148   GstRegistryChunk *chunk;
149
150   chunk = g_slice_new (GstRegistryChunk);
151   chunk->data = str;
152   chunk->size = strlen ((gchar *) chunk->data) + 1;
153   chunk->flags = GST_REGISTRY_CHUNK_FLAG_MALLOC;
154   chunk->align = FALSE;
155   *list = g_list_prepend (*list, chunk);
156   return TRUE;
157 }
158
159 /*
160  * gst_registry_chunks_save_data:
161  *
162  * Store some data in a binary chunk.
163  *
164  * Returns: the initialized chunk
165  */
166 inline static GstRegistryChunk *
167 gst_registry_chunks_make_data (gpointer data, gulong size)
168 {
169   GstRegistryChunk *chunk;
170
171   chunk = g_slice_new (GstRegistryChunk);
172   chunk->data = data;
173   chunk->size = size;
174   chunk->flags = GST_REGISTRY_CHUNK_FLAG_NONE;
175   chunk->align = TRUE;
176   return chunk;
177 }
178
179
180 /*
181  * gst_registry_chunks_save_pad_template:
182  *
183  * Store pad_templates in binary chunks.
184  *
185  * Returns: %TRUE for success
186  */
187 static gboolean
188 gst_registry_chunks_save_pad_template (GList ** list,
189     GstStaticPadTemplate * template)
190 {
191   GstRegistryChunkPadTemplate *pt;
192   GstRegistryChunk *chk;
193
194   pt = g_slice_new (GstRegistryChunkPadTemplate);
195   chk =
196       gst_registry_chunks_make_data (pt, sizeof (GstRegistryChunkPadTemplate));
197
198   pt->presence = template->presence;
199   pt->direction = template->direction;
200
201   /* pack pad template strings */
202   gst_registry_chunks_save_const_string (list,
203       (gchar *) (template->static_caps.string));
204   gst_registry_chunks_save_const_string (list, template->name_template);
205
206   *list = g_list_prepend (*list, chk);
207
208   return TRUE;
209 }
210
211 #define VALIDATE_UTF8(__details, __entry)                                   \
212 G_STMT_START {                                                              \
213   if (!g_utf8_validate (__details->__entry, -1, NULL)) {                    \
214     g_warning ("Invalid UTF-8 in " G_STRINGIFY (__entry) ": %s",            \
215         __details->__entry);                                                \
216     g_free (__details->__entry);                                            \
217     __details->__entry = g_strdup ("[ERROR: invalid UTF-8]");               \
218   }                                                                         \
219 } G_STMT_END
220
221 /*
222  * gst_registry_chunks_save_feature:
223  *
224  * Store features in binary chunks.
225  *
226  * Returns: %TRUE for success
227  */
228 static gboolean
229 gst_registry_chunks_save_feature (GList ** list, GstPluginFeature * feature)
230 {
231   const gchar *type_name = G_OBJECT_TYPE_NAME (feature);
232   GstRegistryChunkPluginFeature *pf = NULL;
233   GstRegistryChunk *chk = NULL;
234   GList *walk;
235   gsize pf_size = 0;
236
237   if (!type_name) {
238     GST_ERROR ("NULL feature type_name, aborting.");
239     return FALSE;
240   }
241
242   if (GST_IS_ELEMENT_FACTORY (feature)) {
243     GstRegistryChunkElementFactory *ef;
244     GstElementFactory *factory = GST_ELEMENT_FACTORY (feature);
245
246     /* Initialize with zeroes because of struct padding and
247      * valgrind complaining about copying unitialized memory
248      */
249     ef = g_slice_new0 (GstRegistryChunkElementFactory);
250     pf_size = sizeof (GstRegistryChunkElementFactory);
251     chk = gst_registry_chunks_make_data (ef, pf_size);
252     ef->npadtemplates = ef->ninterfaces = ef->nuriprotocols = 0;
253     pf = (GstRegistryChunkPluginFeature *) ef;
254
255     /* save interfaces */
256     for (walk = factory->interfaces; walk;
257         walk = g_list_next (walk), ef->ninterfaces++) {
258       gst_registry_chunks_save_const_string (list, (gchar *) walk->data);
259     }
260     GST_DEBUG_OBJECT (feature, "saved %d interfaces %d pad templates",
261         ef->ninterfaces, ef->npadtemplates);
262
263     /* save uritypes */
264     if (GST_URI_TYPE_IS_VALID (factory->uri_type)) {
265       if (factory->uri_protocols && *factory->uri_protocols) {
266         GstRegistryChunk *subchk;
267         gchar **protocol;
268
269         subchk =
270             gst_registry_chunks_make_data (&factory->uri_type,
271             sizeof (factory->uri_type));
272         subchk->flags = GST_REGISTRY_CHUNK_FLAG_CONST;
273
274         protocol = factory->uri_protocols;
275         while (*protocol) {
276           gst_registry_chunks_save_const_string (list, *protocol++);
277           ef->nuriprotocols++;
278         }
279         *list = g_list_prepend (*list, subchk);
280         GST_DEBUG_OBJECT (feature, "Saved %d UriTypes", ef->nuriprotocols);
281       } else {
282         g_warning ("GStreamer feature '%s' is URI handler but does not provide"
283             " any protocols it can handle", GST_OBJECT_NAME (feature));
284       }
285     }
286
287     /* save pad-templates */
288     for (walk = factory->staticpadtemplates; walk;
289         walk = g_list_next (walk), ef->npadtemplates++) {
290       GstStaticPadTemplate *template = walk->data;
291
292       if (!gst_registry_chunks_save_pad_template (list, template)) {
293         GST_ERROR_OBJECT (feature, "Can't fill pad template, aborting.");
294         goto fail;
295       }
296     }
297
298     /* pack element metadata strings */
299     gst_registry_chunks_save_string (list,
300         gst_structure_to_string (factory->metadata));
301   } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
302     GstRegistryChunkTypeFindFactory *tff;
303     GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
304     gchar *str;
305
306     /* Initialize with zeroes because of struct padding and
307      * valgrind complaining about copying unitialized memory
308      */
309     tff = g_slice_new0 (GstRegistryChunkTypeFindFactory);
310     pf_size = sizeof (GstRegistryChunkTypeFindFactory);
311     chk = gst_registry_chunks_make_data (tff, pf_size);
312     tff->nextensions = 0;
313     pf = (GstRegistryChunkPluginFeature *) tff;
314
315     /* save extensions */
316     if (factory->extensions) {
317       while (factory->extensions[tff->nextensions]) {
318         gst_registry_chunks_save_const_string (list,
319             factory->extensions[tff->nextensions++]);
320       }
321     }
322     GST_DEBUG_OBJECT (feature, "saved %d extensions", tff->nextensions);
323     /* save caps */
324     if (factory->caps) {
325       GstCaps *fcaps = gst_caps_ref (factory->caps);
326       /* we simplify the caps before saving. This is a lot faster
327        * when loading them later on */
328       fcaps = gst_caps_simplify (fcaps);
329       str = gst_caps_to_string (fcaps);
330       gst_caps_unref (fcaps);
331
332       gst_registry_chunks_save_string (list, str);
333     } else {
334       gst_registry_chunks_save_const_string (list, "");
335     }
336   } else if (GST_IS_DEVICE_MONITOR_FACTORY (feature)) {
337     GstRegistryChunkDeviceMonitorFactory *tff;
338     GstDeviceMonitorFactory *factory = GST_DEVICE_MONITOR_FACTORY (feature);
339
340     /* Initialize with zeroes because of struct padding and
341      * valgrind complaining about copying unitialized memory
342      */
343     tff = g_slice_new0 (GstRegistryChunkDeviceMonitorFactory);
344     chk =
345         gst_registry_chunks_make_data (tff,
346         sizeof (GstRegistryChunkDeviceMonitorFactory));
347     pf = (GstRegistryChunkPluginFeature *) tff;
348
349
350     /* pack element metadata strings */
351     gst_registry_chunks_save_string (list,
352         gst_structure_to_string (factory->metadata));
353   } else {
354     GST_WARNING_OBJECT (feature, "unhandled feature type '%s'", type_name);
355   }
356
357   if (pf) {
358     pf->rank = feature->rank;
359     *list = g_list_prepend (*list, chk);
360
361     /* pack plugin feature strings */
362     gst_registry_chunks_save_const_string (list, GST_OBJECT_NAME (feature));
363     gst_registry_chunks_save_const_string (list, (gchar *) type_name);
364
365     return TRUE;
366   }
367
368   /* Errors */
369 fail:
370   g_slice_free (GstRegistryChunk, chk);
371   g_slice_free1 (pf_size, pf);
372   return FALSE;
373 }
374
375 static gboolean
376 gst_registry_chunks_save_plugin_dep (GList ** list, GstPluginDep * dep)
377 {
378   GstRegistryChunkDep *ed;
379   GstRegistryChunk *chk;
380   gchar **s;
381
382   ed = g_slice_new (GstRegistryChunkDep);
383   chk = gst_registry_chunks_make_data (ed, sizeof (GstRegistryChunkDep));
384
385   ed->flags = dep->flags;
386   ed->n_env_vars = 0;
387   ed->n_paths = 0;
388   ed->n_names = 0;
389
390   ed->env_hash = dep->env_hash;
391   ed->stat_hash = dep->stat_hash;
392
393   for (s = dep->env_vars; s != NULL && *s != NULL; ++s, ++ed->n_env_vars)
394     gst_registry_chunks_save_string (list, g_strdup (*s));
395
396   for (s = dep->paths; s != NULL && *s != NULL; ++s, ++ed->n_paths)
397     gst_registry_chunks_save_string (list, g_strdup (*s));
398
399   for (s = dep->names; s != NULL && *s != NULL; ++s, ++ed->n_names)
400     gst_registry_chunks_save_string (list, g_strdup (*s));
401
402   *list = g_list_prepend (*list, chk);
403
404   GST_LOG ("Saved external plugin dependency");
405   return TRUE;
406 }
407
408 /*
409  * _priv_gst_registry_chunks_save_plugin:
410  *
411  * Adapt a GstPlugin to our GstRegistryChunkPluginElement structure, and
412  * prepend it as a GstRegistryChunk in the provided list.
413  *
414  */
415 gboolean
416 _priv_gst_registry_chunks_save_plugin (GList ** list, GstRegistry * registry,
417     GstPlugin * plugin)
418 {
419   GstRegistryChunkPluginElement *pe;
420   GstRegistryChunk *chk;
421   GList *plugin_features = NULL;
422   GList *walk;
423
424   pe = g_slice_new (GstRegistryChunkPluginElement);
425   chk =
426       gst_registry_chunks_make_data (pe,
427       sizeof (GstRegistryChunkPluginElement));
428
429   pe->file_size = plugin->file_size;
430   pe->file_mtime = plugin->file_mtime;
431   pe->nfeatures = 0;
432   pe->n_deps = 0;
433
434   /* pack external deps */
435   for (walk = plugin->priv->deps; walk != NULL; walk = walk->next) {
436     if (!gst_registry_chunks_save_plugin_dep (list, walk->data)) {
437       GST_ERROR ("Could not save external plugin dependency, aborting.");
438       goto fail;
439     }
440     ++pe->n_deps;
441   }
442
443   /* pack plugin features */
444   plugin_features =
445       gst_registry_get_feature_list_by_plugin (registry, plugin->desc.name);
446   for (walk = plugin_features; walk; walk = g_list_next (walk), pe->nfeatures++) {
447     GstPluginFeature *feature = GST_PLUGIN_FEATURE (walk->data);
448
449     if (!gst_registry_chunks_save_feature (list, feature)) {
450       GST_ERROR ("Can't fill plugin feature, aborting.");
451       goto fail;
452     }
453   }
454
455   gst_plugin_feature_list_free (plugin_features);
456
457   /* pack cache data */
458   if (plugin->priv->cache_data) {
459     gchar *cache_str = gst_structure_to_string (plugin->priv->cache_data);
460     gst_registry_chunks_save_string (list, cache_str);
461   } else {
462     gst_registry_chunks_save_const_string (list, "");
463   }
464
465   /* pack plugin element strings */
466   gst_registry_chunks_save_const_string (list,
467       (plugin->desc.release_datetime) ? plugin->desc.release_datetime : "");
468   gst_registry_chunks_save_const_string (list, plugin->desc.origin);
469   gst_registry_chunks_save_const_string (list, plugin->desc.package);
470   gst_registry_chunks_save_const_string (list, plugin->desc.source);
471   gst_registry_chunks_save_const_string (list, plugin->desc.license);
472   gst_registry_chunks_save_const_string (list, plugin->desc.version);
473   gst_registry_chunks_save_const_string (list, plugin->filename);
474   gst_registry_chunks_save_const_string (list, plugin->desc.description);
475   gst_registry_chunks_save_const_string (list, plugin->desc.name);
476
477   *list = g_list_prepend (*list, chk);
478
479   GST_DEBUG ("Found %d features in plugin \"%s\"", pe->nfeatures,
480       plugin->desc.name);
481   return TRUE;
482
483   /* Errors */
484 fail:
485   gst_plugin_feature_list_free (plugin_features);
486   g_slice_free (GstRegistryChunk, chk);
487   g_slice_free (GstRegistryChunkPluginElement, pe);
488   return FALSE;
489 }
490
491 /*
492  * gst_registry_chunks_load_pad_template:
493  *
494  * Make a new GstStaticPadTemplate from current GstRegistryChunkPadTemplate
495  * structure.
496  *
497  * Returns: new GstStaticPadTemplate
498  */
499 static gboolean
500 gst_registry_chunks_load_pad_template (GstElementFactory * factory, gchar ** in,
501     gchar * end)
502 {
503   GstRegistryChunkPadTemplate *pt;
504   GstStaticPadTemplate *template = NULL;
505
506   align (*in);
507   GST_DEBUG ("Reading/casting for GstRegistryChunkPadTemplate at address %p",
508       *in);
509   unpack_element (*in, pt, GstRegistryChunkPadTemplate, end, fail);
510
511   template = g_slice_new (GstStaticPadTemplate);
512   template->presence = pt->presence;
513   template->direction = (GstPadDirection) pt->direction;
514   template->static_caps.caps = NULL;
515
516   /* unpack pad template strings */
517   unpack_const_string (*in, template->name_template, end, fail);
518   unpack_const_string (*in, template->static_caps.string, end, fail);
519
520   __gst_element_factory_add_static_pad_template (factory, template);
521   GST_DEBUG ("Added pad_template %s", template->name_template);
522
523   return TRUE;
524 fail:
525   GST_INFO ("Reading pad template failed");
526   if (template)
527     g_slice_free (GstStaticPadTemplate, template);
528   return FALSE;
529 }
530
531 /*
532  * gst_registry_chunks_load_feature:
533  *
534  * Make a new GstPluginFeature from current binary plugin feature structure
535  *
536  * Returns: new GstPluginFeature
537  */
538 static gboolean
539 gst_registry_chunks_load_feature (GstRegistry * registry, gchar ** in,
540     gchar * end, GstPlugin * plugin)
541 {
542   GstRegistryChunkPluginFeature *pf = NULL;
543   GstPluginFeature *feature = NULL;
544   const gchar *const_str, *type_name;
545   const gchar *feature_name;
546   const gchar *plugin_name;
547   gchar *str;
548   GType type;
549   guint i;
550
551   plugin_name = plugin->desc.name;
552
553   /* unpack plugin feature strings */
554   unpack_string_nocopy (*in, type_name, end, fail);
555
556   if (G_UNLIKELY (!type_name)) {
557     GST_ERROR ("No feature type name");
558     return FALSE;
559   }
560
561   /* unpack more plugin feature strings */
562   unpack_string_nocopy (*in, feature_name, end, fail);
563
564   GST_DEBUG ("Plugin '%s' feature '%s' typename : '%s'", plugin_name,
565       feature_name, type_name);
566
567   if (G_UNLIKELY (!(type = g_type_from_name (type_name)))) {
568     GST_ERROR ("Unknown type from typename '%s' for plugin '%s'", type_name,
569         plugin_name);
570     return FALSE;
571   }
572   if (G_UNLIKELY ((feature = g_object_newv (type, 0, NULL)) == NULL)) {
573     GST_ERROR ("Can't create feature from type");
574     return FALSE;
575   }
576   gst_plugin_feature_set_name (feature, feature_name);
577
578   if (G_UNLIKELY (!GST_IS_PLUGIN_FEATURE (feature))) {
579     GST_ERROR ("typename : '%s' is not a plugin feature", type_name);
580     goto fail;
581   }
582
583   if (GST_IS_ELEMENT_FACTORY (feature)) {
584     GstRegistryChunkElementFactory *ef;
585     guint n;
586     GstElementFactory *factory = GST_ELEMENT_FACTORY_CAST (feature);
587     gchar *str;
588     const gchar *meta_data_str;
589
590     align (*in);
591     GST_LOG ("Reading/casting for GstRegistryChunkElementFactory at address %p",
592         *in);
593     unpack_element (*in, ef, GstRegistryChunkElementFactory, end, fail);
594     pf = (GstRegistryChunkPluginFeature *) ef;
595
596     /* unpack element factory strings */
597     unpack_string_nocopy (*in, meta_data_str, end, fail);
598     if (meta_data_str && *meta_data_str) {
599       factory->metadata = gst_structure_from_string (meta_data_str, NULL);
600       if (!factory->metadata) {
601         GST_ERROR
602             ("Error when trying to deserialize structure for metadata '%s'",
603             meta_data_str);
604         goto fail;
605       }
606     }
607     n = ef->npadtemplates;
608     GST_DEBUG ("Element factory : npadtemplates=%d", n);
609
610     /* load pad templates */
611     for (i = 0; i < n; i++) {
612       if (G_UNLIKELY (!gst_registry_chunks_load_pad_template (factory, in,
613                   end))) {
614         GST_ERROR ("Error while loading binary pad template");
615         goto fail;
616       }
617     }
618
619     /* load uritypes */
620     if (G_UNLIKELY ((n = ef->nuriprotocols))) {
621       GST_DEBUG ("Reading %d UriTypes at address %p", n, *in);
622
623       align (*in);
624       factory->uri_type = *((guint *) * in);
625       *in += sizeof (factory->uri_type);
626       /*unpack_element(*in, &factory->uri_type, factory->uri_type, end, fail); */
627
628       factory->uri_protocols = g_new0 (gchar *, n + 1);
629       for (i = 0; i < n; i++) {
630         unpack_string (*in, str, end, fail);
631         factory->uri_protocols[i] = str;
632       }
633     }
634     /* load interfaces */
635     if (G_UNLIKELY ((n = ef->ninterfaces))) {
636       GST_DEBUG ("Reading %d Interfaces at address %p", n, *in);
637       for (i = 0; i < n; i++) {
638         unpack_string_nocopy (*in, const_str, end, fail);
639         __gst_element_factory_add_interface (factory, const_str);
640       }
641     }
642   } else if (GST_IS_TYPE_FIND_FACTORY (feature)) {
643     GstRegistryChunkTypeFindFactory *tff;
644     GstTypeFindFactory *factory = GST_TYPE_FIND_FACTORY (feature);
645
646     align (*in);
647     GST_DEBUG
648         ("Reading/casting for GstRegistryChunkPluginFeature at address %p",
649         *in);
650     unpack_element (*in, tff, GstRegistryChunkTypeFindFactory, end, fail);
651     pf = (GstRegistryChunkPluginFeature *) tff;
652
653     /* load typefinder caps */
654     unpack_string_nocopy (*in, const_str, end, fail);
655     if (const_str != NULL && *const_str != '\0')
656       factory->caps = gst_caps_from_string (const_str);
657     else
658       factory->caps = NULL;
659
660     /* load extensions */
661     if (tff->nextensions) {
662       GST_DEBUG ("Reading %d Typefind extensions at address %p",
663           tff->nextensions, *in);
664       factory->extensions = g_new0 (gchar *, tff->nextensions + 1);
665       /* unpack in reverse order to maintain the correct order */
666       for (i = tff->nextensions; i > 0; i--) {
667         unpack_string (*in, str, end, fail);
668         factory->extensions[i - 1] = str;
669       }
670     }
671   } else if (GST_IS_DEVICE_MONITOR_FACTORY (feature)) {
672     GstRegistryChunkDeviceMonitorFactory *dmf;
673     GstDeviceMonitorFactory *factory = GST_DEVICE_MONITOR_FACTORY (feature);
674     const gchar *meta_data_str;
675
676     align (*in);
677     GST_DEBUG
678         ("Reading/casting for GstRegistryChunkPluginFeature at address %p",
679         *in);
680     unpack_element (*in, dmf, GstRegistryChunkDeviceMonitorFactory, end, fail);
681
682     pf = (GstRegistryChunkPluginFeature *) dmf;
683
684     /* unpack element factory strings */
685     unpack_string_nocopy (*in, meta_data_str, end, fail);
686     if (meta_data_str && *meta_data_str) {
687       factory->metadata = gst_structure_from_string (meta_data_str, NULL);
688       if (!factory->metadata) {
689         GST_ERROR
690             ("Error when trying to deserialize structure for metadata '%s'",
691             meta_data_str);
692         goto fail;
693       }
694     }
695   } else {
696     GST_WARNING ("unhandled factory type : %s", G_OBJECT_TYPE_NAME (feature));
697     goto fail;
698   }
699
700   feature->rank = pf->rank;
701
702   feature->plugin_name = plugin_name;
703   feature->plugin = plugin;
704   g_object_add_weak_pointer ((GObject *) plugin,
705       (gpointer *) & feature->plugin);
706
707   gst_registry_add_feature (registry, feature);
708   GST_DEBUG ("Added feature %s, plugin %p %s", GST_OBJECT_NAME (feature),
709       plugin, plugin_name);
710
711   return TRUE;
712
713   /* Errors */
714 fail:
715   GST_INFO ("Reading plugin feature failed");
716   if (feature) {
717     if (GST_IS_OBJECT (feature))
718       gst_object_unref (feature);
719     else
720       g_object_unref (feature);
721   }
722   return FALSE;
723 }
724
725 static gchar **
726 gst_registry_chunks_load_plugin_dep_strv (gchar ** in, gchar * end, guint n)
727 {
728   gchar **arr;
729
730   if (n == 0)
731     return NULL;
732
733   arr = g_new0 (gchar *, n + 1);
734   while (n > 0) {
735     unpack_string (*in, arr[n - 1], end, fail);
736     --n;
737   }
738   return arr;
739 fail:
740   GST_INFO ("Reading plugin dependency strings failed");
741   return NULL;
742 }
743
744 static gboolean
745 gst_registry_chunks_load_plugin_dep (GstPlugin * plugin, gchar ** in,
746     gchar * end)
747 {
748   GstPluginDep *dep;
749   GstRegistryChunkDep *d;
750   gchar **s;
751
752   align (*in);
753   GST_LOG_OBJECT (plugin, "Unpacking GstRegistryChunkDep from %p", *in);
754   unpack_element (*in, d, GstRegistryChunkDep, end, fail);
755
756   dep = g_slice_new (GstPluginDep);
757
758   dep->env_hash = d->env_hash;
759   dep->stat_hash = d->stat_hash;
760
761   dep->flags = (GstPluginDependencyFlags) d->flags;
762
763   dep->names = gst_registry_chunks_load_plugin_dep_strv (in, end, d->n_names);
764   dep->paths = gst_registry_chunks_load_plugin_dep_strv (in, end, d->n_paths);
765   dep->env_vars =
766       gst_registry_chunks_load_plugin_dep_strv (in, end, d->n_env_vars);
767
768   plugin->priv->deps = g_list_append (plugin->priv->deps, dep);
769
770   GST_DEBUG_OBJECT (plugin, "Loaded external plugin dependency from registry: "
771       "env_hash: %08x, stat_hash: %08x", dep->env_hash, dep->stat_hash);
772   for (s = dep->env_vars; s != NULL && *s != NULL; ++s)
773     GST_LOG_OBJECT (plugin, " evar: %s", *s);
774   for (s = dep->paths; s != NULL && *s != NULL; ++s)
775     GST_LOG_OBJECT (plugin, " path: %s", *s);
776   for (s = dep->names; s != NULL && *s != NULL; ++s)
777     GST_LOG_OBJECT (plugin, " name: %s", *s);
778
779   return TRUE;
780 fail:
781   GST_INFO ("Reading plugin dependency failed");
782   return FALSE;
783 }
784
785
786 /*
787  * _priv_gst_registry_chunks_load_plugin:
788  *
789  * Make a new GstPlugin from current GstRegistryChunkPluginElement structure
790  * and add it to the GstRegistry. Return an offset to the next
791  * GstRegistryChunkPluginElement structure.
792  */
793 gboolean
794 _priv_gst_registry_chunks_load_plugin (GstRegistry * registry, gchar ** in,
795     gchar * end, GstPlugin ** out_plugin)
796 {
797 #ifndef GST_DISABLE_GST_DEBUG
798   gchar *start = *in;
799 #endif
800   GstRegistryChunkPluginElement *pe;
801   const gchar *cache_str = NULL;
802   GstPlugin *plugin = NULL;
803   guint i, n;
804
805   align (*in);
806   GST_LOG ("Reading/casting for GstRegistryChunkPluginElement at address %p",
807       *in);
808   unpack_element (*in, pe, GstRegistryChunkPluginElement, end, fail);
809
810   plugin = g_object_newv (GST_TYPE_PLUGIN, 0, NULL);
811
812   /* TODO: also set GST_PLUGIN_FLAG_CONST */
813   GST_OBJECT_FLAG_SET (plugin, GST_PLUGIN_FLAG_CACHED);
814   plugin->file_mtime = pe->file_mtime;
815   plugin->file_size = pe->file_size;
816
817   /* unpack plugin element strings */
818   unpack_const_string (*in, plugin->desc.name, end, fail);
819   unpack_const_string (*in, plugin->desc.description, end, fail);
820   unpack_string (*in, plugin->filename, end, fail);
821   unpack_const_string (*in, plugin->desc.version, end, fail);
822   unpack_const_string (*in, plugin->desc.license, end, fail);
823   unpack_const_string (*in, plugin->desc.source, end, fail);
824   unpack_const_string (*in, plugin->desc.package, end, fail);
825   unpack_const_string (*in, plugin->desc.origin, end, fail);
826   unpack_const_string (*in, plugin->desc.release_datetime, end, fail);
827
828   GST_LOG ("read strings for name='%s'", plugin->desc.name);
829   GST_LOG ("  desc.description='%s'", plugin->desc.description);
830   GST_LOG ("  filename='%s'", plugin->filename);
831   GST_LOG ("  desc.version='%s'", plugin->desc.version);
832   GST_LOG ("  desc.license='%s'", plugin->desc.license);
833   GST_LOG ("  desc.source='%s'", plugin->desc.source);
834   GST_LOG ("  desc.package='%s'", plugin->desc.package);
835   GST_LOG ("  desc.origin='%s'", plugin->desc.origin);
836   GST_LOG ("  desc.datetime=%s", plugin->desc.release_datetime);
837
838   if (plugin->desc.release_datetime[0] == '\0')
839     plugin->desc.release_datetime = NULL;
840
841   /* unpack cache data */
842   unpack_string_nocopy (*in, cache_str, end, fail);
843   if (cache_str != NULL && *cache_str != '\0')
844     plugin->priv->cache_data = gst_structure_from_string (cache_str, NULL);
845
846   /* If the license string is 'BLACKLIST', mark this as a blacklisted
847    * plugin */
848   if (strcmp (plugin->desc.license, "BLACKLIST") == 0)
849     GST_OBJECT_FLAG_SET (plugin, GST_PLUGIN_FLAG_BLACKLISTED);
850
851   plugin->basename = g_path_get_basename (plugin->filename);
852
853   /* Takes ownership of plugin */
854   gst_registry_add_plugin (registry, plugin);
855   n = pe->nfeatures;
856   GST_DEBUG ("Added plugin '%s' plugin with %d features from binary registry",
857       plugin->desc.name, n);
858
859   /* Load plugin features */
860   for (i = 0; i < n; i++) {
861     if (G_UNLIKELY (!gst_registry_chunks_load_feature (registry, in, end,
862                 plugin))) {
863       GST_ERROR ("Error while loading binary feature for plugin '%s'",
864           GST_STR_NULL (plugin->desc.name));
865       gst_registry_remove_plugin (registry, plugin);
866       goto fail;
867     }
868   }
869
870   /* Load external plugin dependencies */
871   for (i = 0; i < pe->n_deps; ++i) {
872     if (G_UNLIKELY (!gst_registry_chunks_load_plugin_dep (plugin, in, end))) {
873       GST_ERROR_OBJECT (plugin, "Could not read external plugin dependency "
874           "for plugin '%s'", GST_STR_NULL (plugin->desc.name));
875       gst_registry_remove_plugin (registry, plugin);
876       goto fail;
877     }
878   }
879
880   if (out_plugin)
881     *out_plugin = plugin;
882
883   return TRUE;
884
885   /* Errors */
886 fail:
887   GST_INFO ("Reading plugin failed after %u bytes", (guint) (end - start));
888   return FALSE;
889 }
890
891 void
892 _priv_gst_registry_chunks_save_global_header (GList ** list,
893     GstRegistry * registry, guint32 filter_env_hash)
894 {
895   GstRegistryChunkGlobalHeader *hdr;
896   GstRegistryChunk *chk;
897
898   hdr = g_slice_new (GstRegistryChunkGlobalHeader);
899   chk = gst_registry_chunks_make_data (hdr,
900       sizeof (GstRegistryChunkGlobalHeader));
901
902   hdr->filter_env_hash = filter_env_hash;
903
904   *list = g_list_prepend (*list, chk);
905
906   GST_LOG ("Saved global header (filter_env_hash=0x%08x)", filter_env_hash);
907 }
908
909 gboolean
910 _priv_gst_registry_chunks_load_global_header (GstRegistry * registry,
911     gchar ** in, gchar * end, guint32 * filter_env_hash)
912 {
913   GstRegistryChunkGlobalHeader *hdr;
914
915   align (*in);
916   GST_LOG ("Reading/casting for GstRegistryChunkGlobalHeader at %p", *in);
917   unpack_element (*in, hdr, GstRegistryChunkGlobalHeader, end, fail);
918   *filter_env_hash = hdr->filter_env_hash;
919   return TRUE;
920
921   /* Errors */
922 fail:
923   GST_WARNING ("Reading global header failed");
924   return FALSE;
925 }