dataurisrc: fix string leak in property getter
[platform/upstream/gstreamer.git] / plugins / elements / gstdataurisrc.c
1 /* GStreamer
2  *
3  * Copyright (C) 2009 Igalia S.L
4  * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 /**
23  * SECTION:element-dataurisrc
24  *
25  * dataurisrc handles data: URIs, see <ulink url="http://tools.ietf.org/html/rfc2397">RFC 2397</ulink> for more information.
26  *
27  * <refsect2>
28  * <title>Example launch line</title>
29  * |[
30  * gst-launch-1.0 -v dataurisrc uri="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAfElEQVQ4je2MwQnAIAxFgziA4EnczIsO4MEROo/gzZWc4xdTbe1R6LGRR74heYS7iKElzfcMiRnt4hf8gk8EayB6luefue/HzlJfCA50XsNjYRxprZmenXNIKSGEsC+QUqK1hhgj521BzhnWWiilUGvdF5RS4L2HMQZCCJy8sHMm2TYdJAAAAABJRU5ErkJggg==" ! pngdec ! videoconvert ! imagefreeze ! videoconvert ! autovideosink
31  * ]| This pipeline displays a small 16x16 PNG image from the data URI.
32  * </refsect2>
33  */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include "gstdataurisrc.h"
40
41 #include <string.h>
42 #include <gst/base/gsttypefindhelper.h>
43
44 GST_DEBUG_CATEGORY (data_uri_src_debug);
45 #define GST_CAT_DEFAULT (data_uri_src_debug)
46
47 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
48     GST_PAD_SRC,
49     GST_PAD_ALWAYS,
50     GST_STATIC_CAPS_ANY);
51
52 enum
53 {
54   PROP_0,
55   PROP_URI,
56 };
57
58 static void gst_data_uri_src_finalize (GObject * object);
59 static void gst_data_uri_src_set_property (GObject * object,
60     guint prop_id, const GValue * value, GParamSpec * pspec);
61 static void gst_data_uri_src_get_property (GObject * object,
62     guint prop_id, GValue * value, GParamSpec * pspec);
63
64 static GstCaps *gst_data_uri_src_get_caps (GstBaseSrc * src, GstCaps * filter);
65 static gboolean gst_data_uri_src_get_size (GstBaseSrc * src, guint64 * size);
66 static gboolean gst_data_uri_src_is_seekable (GstBaseSrc * src);
67 static GstFlowReturn gst_data_uri_src_create (GstBaseSrc * src, guint64 offset,
68     guint size, GstBuffer ** buf);
69 static gboolean gst_data_uri_src_start (GstBaseSrc * src);
70
71 static void gst_data_uri_src_handler_init (gpointer g_iface,
72     gpointer iface_data);
73 static GstURIType gst_data_uri_src_get_uri_type (GType type);
74 static const gchar *const *gst_data_uri_src_get_protocols (GType type);
75 static gchar *gst_data_uri_src_get_uri (GstURIHandler * handler);
76 static gboolean gst_data_uri_src_set_uri (GstURIHandler * handler,
77     const gchar * uri, GError ** error);
78
79
80 #define gst_data_uri_src_parent_class parent_class
81 G_DEFINE_TYPE_WITH_CODE (GstDataURISrc, gst_data_uri_src, GST_TYPE_BASE_SRC,
82     G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER,
83         gst_data_uri_src_handler_init));
84
85 static void
86 gst_data_uri_src_class_init (GstDataURISrcClass * klass)
87 {
88   GObjectClass *gobject_class = (GObjectClass *) klass;
89   GstElementClass *element_class = (GstElementClass *) klass;
90   GstBaseSrcClass *basesrc_class = (GstBaseSrcClass *) klass;
91
92   gobject_class->finalize = gst_data_uri_src_finalize;
93   gobject_class->set_property = gst_data_uri_src_set_property;
94   gobject_class->get_property = gst_data_uri_src_get_property;
95
96   g_object_class_install_property (gobject_class, PROP_URI,
97       g_param_spec_string ("uri",
98           "URI",
99           "URI that should be used",
100           NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
101
102   gst_element_class_add_static_pad_template (element_class, &src_template);
103   gst_element_class_set_static_metadata (element_class,
104       "data: URI source element", "Source", "Handles data: uris",
105       "Philippe Normand <pnormand@igalia.com>, "
106       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
107
108   GST_DEBUG_CATEGORY_INIT (data_uri_src_debug, "dataurisrc", 0,
109       "data: URI source");
110
111   basesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_data_uri_src_get_caps);
112   basesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_data_uri_src_get_size);
113   basesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_data_uri_src_is_seekable);
114   basesrc_class->create = GST_DEBUG_FUNCPTR (gst_data_uri_src_create);
115   basesrc_class->start = GST_DEBUG_FUNCPTR (gst_data_uri_src_start);
116 }
117
118 static void
119 gst_data_uri_src_init (GstDataURISrc * src)
120 {
121 }
122
123 static void
124 gst_data_uri_src_finalize (GObject * object)
125 {
126   GstDataURISrc *src = GST_DATA_URI_SRC (object);
127
128   g_free (src->uri);
129   src->uri = NULL;
130
131   if (src->buffer)
132     gst_buffer_unref (src->buffer);
133   src->buffer = NULL;
134
135   G_OBJECT_CLASS (parent_class)->finalize (object);
136 }
137
138 static void
139 gst_data_uri_src_set_property (GObject * object, guint prop_id,
140     const GValue * value, GParamSpec * pspec)
141 {
142   GstDataURISrc *src = GST_DATA_URI_SRC (object);
143
144   switch (prop_id) {
145     case PROP_URI:
146       gst_data_uri_src_set_uri (GST_URI_HANDLER (src),
147           g_value_get_string (value), NULL);
148       break;
149     default:
150       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
151       break;
152   }
153 }
154
155 static void
156 gst_data_uri_src_get_property (GObject * object,
157     guint prop_id, GValue * value, GParamSpec * pspec)
158 {
159   GstDataURISrc *src = GST_DATA_URI_SRC (object);
160
161   switch (prop_id) {
162     case PROP_URI:
163       g_value_take_string (value,
164           gst_data_uri_src_get_uri (GST_URI_HANDLER (src)));
165       break;
166     default:
167       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
168       break;
169   }
170 }
171
172 static GstCaps *
173 gst_data_uri_src_get_caps (GstBaseSrc * basesrc, GstCaps * filter)
174 {
175   GstDataURISrc *src = GST_DATA_URI_SRC (basesrc);
176   GstCaps *caps;
177
178   GST_OBJECT_LOCK (src);
179   caps = gst_pad_get_current_caps (GST_BASE_SRC_PAD (basesrc));
180   if (!caps)
181     caps = gst_caps_new_any ();
182   GST_OBJECT_UNLOCK (src);
183
184   return caps;
185 }
186
187 static gboolean
188 gst_data_uri_src_get_size (GstBaseSrc * basesrc, guint64 * size)
189 {
190   GstDataURISrc *src = GST_DATA_URI_SRC (basesrc);
191   gboolean ret;
192
193   GST_OBJECT_LOCK (src);
194   if (!src->buffer) {
195     ret = FALSE;
196     *size = -1;
197   } else {
198     ret = TRUE;
199     *size = gst_buffer_get_size (src->buffer);
200   }
201   GST_OBJECT_UNLOCK (src);
202
203   return ret;
204 }
205
206 static gboolean
207 gst_data_uri_src_is_seekable (GstBaseSrc * basesrc)
208 {
209   return TRUE;
210 }
211
212 static GstFlowReturn
213 gst_data_uri_src_create (GstBaseSrc * basesrc, guint64 offset, guint size,
214     GstBuffer ** buf)
215 {
216   GstDataURISrc *src = GST_DATA_URI_SRC (basesrc);
217   GstFlowReturn ret;
218
219   GST_OBJECT_LOCK (src);
220
221   if (!src->buffer)
222     goto no_buffer;
223
224   /* This is only correct because GstBaseSrc already clips size for us to be no
225    * larger than the max. available size if a segment at the end is requested */
226   if (offset + size > gst_buffer_get_size (src->buffer)) {
227     ret = GST_FLOW_EOS;
228   } else if (*buf != NULL) {
229     GstMapInfo src_info;
230     GstMapInfo dest_info;
231     gsize fill_size;
232
233     gst_buffer_map (src->buffer, &src_info, GST_MAP_READ);
234     gst_buffer_map (*buf, &dest_info, GST_MAP_WRITE);
235
236     fill_size = gst_buffer_fill (*buf, 0, src_info.data + offset, size);
237
238     gst_buffer_unmap (*buf, &dest_info);
239     gst_buffer_unmap (src->buffer, &src_info);
240     gst_buffer_set_size (*buf, fill_size);
241     ret = GST_FLOW_OK;
242   } else {
243     *buf =
244         gst_buffer_copy_region (src->buffer, GST_BUFFER_COPY_ALL, offset, size);
245     ret = GST_FLOW_OK;
246   }
247   GST_OBJECT_UNLOCK (src);
248
249   return ret;
250
251 /* ERRORS */
252 no_buffer:
253   {
254     GST_OBJECT_UNLOCK (src);
255     GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), (NULL));
256     return GST_FLOW_NOT_NEGOTIATED;
257   }
258 }
259
260 static gboolean
261 gst_data_uri_src_start (GstBaseSrc * basesrc)
262 {
263   GstDataURISrc *src = GST_DATA_URI_SRC (basesrc);
264
265   GST_OBJECT_LOCK (src);
266
267   if (src->uri == NULL || *src->uri == '\0' || src->buffer == NULL)
268     goto no_uri;
269
270   GST_OBJECT_UNLOCK (src);
271
272   return TRUE;
273
274 /* ERRORS */
275 no_uri:
276   {
277     GST_OBJECT_UNLOCK (src);
278     GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ,
279         ("No valid data URI specified, or the data URI could not be parsed."),
280         ("%s", src->uri));
281     return FALSE;
282   }
283 }
284
285 static GstURIType
286 gst_data_uri_src_get_uri_type (GType type)
287 {
288   return GST_URI_SRC;
289 }
290
291 static const gchar *const *
292 gst_data_uri_src_get_protocols (GType type)
293 {
294   static const gchar *protocols[] = { "data", 0 };
295
296   return protocols;
297 }
298
299 static gchar *
300 gst_data_uri_src_get_uri (GstURIHandler * handler)
301 {
302   GstDataURISrc *src = GST_DATA_URI_SRC (handler);
303   gchar *src_uri = NULL;
304
305   GST_OBJECT_LOCK (src);
306   src_uri = g_strdup (src->uri);
307   GST_OBJECT_UNLOCK (src);
308   return src_uri;
309 }
310
311 static gboolean
312 gst_data_uri_src_set_uri (GstURIHandler * handler, const gchar * uri,
313     GError ** error)
314 {
315   GstDataURISrc *src = GST_DATA_URI_SRC (handler);
316   gboolean ret = FALSE;
317   gchar *mimetype = NULL;
318   const gchar *parameters_start;
319   const gchar *data_start;
320   const gchar *orig_uri = uri;
321   GstCaps *caps;
322   GstBuffer *buffer;
323   gboolean base64 = FALSE;
324   gchar *charset = NULL;
325   gpointer bdata;
326   gsize bsize;
327
328   GST_OBJECT_LOCK (src);
329   if (GST_STATE (src) >= GST_STATE_PAUSED)
330     goto wrong_state;
331   GST_OBJECT_UNLOCK (src);
332
333   /* uri must be an URI as defined in RFC 2397
334    * data:[<mediatype>][;base64],<data>
335    */
336   if (strncmp ("data:", uri, 5) != 0)
337     goto invalid_uri;
338
339   uri += 5;
340
341   parameters_start = strchr (uri, ';');
342   data_start = strchr (uri, ',');
343   if (data_start == NULL)
344     goto invalid_uri;
345
346   if (data_start != uri && parameters_start != uri)
347     mimetype =
348         g_strndup (uri,
349         (parameters_start ? parameters_start : data_start) - uri);
350   else
351     mimetype = g_strdup ("text/plain");
352
353   GST_DEBUG_OBJECT (src, "Mimetype: %s", mimetype);
354
355   if (parameters_start != NULL) {
356     gchar **walk;
357     gchar *parameters =
358         g_strndup (parameters_start + 1, data_start - parameters_start - 1);
359     gchar **parameters_strv;
360
361     parameters_strv = g_strsplit (parameters, ";", -1);
362
363     GST_DEBUG_OBJECT (src, "Parameters: ");
364     walk = parameters_strv;
365     while (*walk) {
366       GST_DEBUG_OBJECT (src, "\t %s", *walk);
367       if (strcmp ("base64", *walk) == 0) {
368         base64 = TRUE;
369       } else if (strncmp ("charset=", *walk, 8) == 0) {
370         charset = g_strdup (*walk + 8);
371       }
372       walk++;
373     }
374     g_free (parameters);
375     g_strfreev (parameters_strv);
376   }
377
378   /* Skip comma */
379   data_start += 1;
380   if (base64) {
381     bdata = g_base64_decode (data_start, &bsize);
382   } else {
383     /* URI encoded, i.e. "percent" encoding */
384     bdata = g_uri_unescape_string (data_start, NULL);
385     if (bdata == NULL)
386       goto invalid_uri_encoded_data;
387     bsize = strlen (bdata) + 1;
388   }
389   /* Convert to UTF8 */
390   if (strcmp ("text/plain", mimetype) == 0 &&
391       charset && g_ascii_strcasecmp ("US-ASCII", charset) != 0
392       && g_ascii_strcasecmp ("UTF-8", charset) != 0) {
393     gsize read;
394     gsize written;
395     gpointer data;
396
397     data =
398         g_convert_with_fallback (bdata, -1, "UTF-8", charset, (char *) "*",
399         &read, &written, NULL);
400     g_free (bdata);
401
402     bdata = data;
403     bsize = written;
404   }
405   buffer = gst_buffer_new_wrapped (bdata, bsize);
406
407   caps = gst_type_find_helper_for_buffer (GST_OBJECT (src), buffer, NULL);
408   if (!caps)
409     caps = gst_caps_new_empty_simple (mimetype);
410   gst_base_src_set_caps (GST_BASE_SRC_CAST (src), caps);
411   gst_caps_unref (caps);
412
413   GST_OBJECT_LOCK (src);
414   gst_buffer_replace (&src->buffer, buffer);
415   gst_buffer_unref (buffer);
416   g_free (src->uri);
417   src->uri = g_strdup (orig_uri);
418   GST_OBJECT_UNLOCK (src);
419
420   ret = TRUE;
421
422 out:
423
424   g_free (mimetype);
425   g_free (charset);
426
427   return ret;
428
429 wrong_state:
430   {
431     GST_WARNING_OBJECT (src, "Can't set URI in %s state",
432         gst_element_state_get_name (GST_STATE (src)));
433     GST_OBJECT_UNLOCK (src);
434     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_STATE,
435         "Changing the 'uri' property on dataurisrc while it is running "
436         "is not supported");
437     goto out;
438   }
439 invalid_uri:
440   {
441     GST_WARNING_OBJECT (src, "invalid URI '%s'", uri);
442     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
443         "Invalid data URI");
444     goto out;
445   }
446 invalid_uri_encoded_data:
447   {
448     GST_WARNING_OBJECT (src, "Failed to parse data encoded in URI '%s'", uri);
449     g_set_error (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
450         "Could not parse data encoded in data URI");
451     goto out;
452   }
453 }
454
455 static void
456 gst_data_uri_src_handler_init (gpointer g_iface, gpointer iface_data)
457 {
458   GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
459
460   iface->get_type = gst_data_uri_src_get_uri_type;
461   iface->get_protocols = gst_data_uri_src_get_protocols;
462   iface->get_uri = gst_data_uri_src_get_uri;
463   iface->set_uri = gst_data_uri_src_set_uri;
464 }
465
466 static gboolean
467 plugin_init (GstPlugin * plugin)
468 {
469   return gst_element_register (plugin, "dataurisrc",
470       GST_RANK_PRIMARY, GST_TYPE_DATA_URI_SRC);
471 }
472
473 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
474     GST_VERSION_MINOR,
475     dataurisrc,
476     "data: URI source",
477     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);