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