typefindhelper: Fix signed integer overflow
[platform/upstream/gstreamer.git] / libs / gst / base / gsttypefindhelper.c
index 41f66bf..67efeac 100644 (file)
  *
  * You should have received a copy of the GNU Library General Public
  * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 
 /**
  * SECTION:gsttypefindhelper
- * @short_description: Utility functions for typefinding 
+ * @title: GstTypeFindHelper
+ * @short_description: Utility functions for typefinding
  *
  * Utility functions for elements doing typefinding:
  * gst_type_find_helper() does typefinding in pull mode, while
@@ -74,8 +75,8 @@ typedef struct
  * for performance reasons, but mostly because pointers returned by us need
  * to stay valid until typefinding has finished)
  *
- * Returns: address of the data or %NULL if buffer does not cover the
- * requested range.
+ * Returns: (nullable): address of the data or %NULL if buffer does not cover
+ * the requested range.
  */
 static const guint8 *
 helper_find_peek (gpointer data, gint64 offset, guint size)
@@ -113,11 +114,12 @@ helper_find_peek (gpointer data, gint64 offset, guint size)
     for (walk = helper->buffers; walk; walk = walk->next) {
       GstMappedBuffer *bmp = (GstMappedBuffer *) walk->data;
       GstBuffer *buf = GST_BUFFER_CAST (bmp->buffer);
-      guint64 buf_offset = GST_BUFFER_OFFSET (buf);
-      guint buf_size = gst_buffer_get_size (buf);
+
+      buf_offset = GST_BUFFER_OFFSET (buf);
+      buf_size = bmp->map.size;
 
       /* buffers are kept sorted by end offset (highest first) in the list, so
-       * at this point we save the current position and stop searching if 
+       * at this point we save the current position and stop searching if
        * we're after the searched end offset */
       if (buf_offset <= offset) {
         if ((offset + size) < (buf_offset + buf_size)) {
@@ -161,7 +163,7 @@ helper_find_peek (gpointer data, gint64 offset, guint size)
 #endif
 
   /* getrange might silently return shortened buffers at the end of a file,
-   * we must, however, always return either the full requested data or NULL */
+   * we must, however, always return either the full requested data or %NULL */
   buf_offset = GST_BUFFER_OFFSET (buffer);
   buf_size = gst_buffer_get_size (buffer);
 
@@ -174,7 +176,12 @@ helper_find_peek (gpointer data, gint64 offset, guint size)
   }
 
   bmap = g_slice_new0 (GstMappedBuffer);
+
+  if (!gst_buffer_map (buffer, &bmap->map, GST_MAP_READ))
+    goto map_failed;
+
   bmap->buffer = buffer;
+
   if (insert_pos) {
     helper->buffers = g_slist_insert_before (helper->buffers, insert_pos, bmap);
   } else {
@@ -185,8 +192,6 @@ helper_find_peek (gpointer data, gint64 offset, guint size)
     helper->buffers = g_slist_prepend (helper->buffers, bmap);
   }
 
-  gst_buffer_map (buffer, &bmap->map, GST_MAP_READ);
-
   return bmap->map.data;
 
 error:
@@ -194,6 +199,13 @@ error:
     GST_INFO ("typefind function returned: %s", gst_flow_get_name (ret));
     return NULL;
   }
+map_failed:
+  {
+    GST_ERROR ("map failed");
+    gst_buffer_unref (buffer);
+    g_slice_free (GstMappedBuffer, bmap);
+    return NULL;
+  }
 }
 
 /*
@@ -205,8 +217,7 @@ error:
  * If given @probability is higher, replace previously store caps.
  */
 static void
-helper_find_suggest (gpointer data, GstTypeFindProbability probability,
-    GstCaps * caps)
+helper_find_suggest (gpointer data, guint probability, GstCaps * caps)
 {
   GstTypeFindHelper *helper = (GstTypeFindHelper *) data;
 
@@ -234,13 +245,13 @@ helper_find_get_length (gpointer data)
 /**
  * gst_type_find_helper_get_range:
  * @obj: A #GstObject that will be passed as first argument to @func
- * @parent: the parent of @obj or NULL
+ * @parent: (allow-none): the parent of @obj or %NULL
  * @func: (scope call): A generic #GstTypeFindHelperGetRangeFunction that will
  *        be used to access data at random offsets when doing the typefinding
  * @size: The length in bytes
  * @extension: extension of the media
  * @prob: (out) (allow-none): location to store the probability of the found
- *     caps, or #NULL
+ *     caps, or %NULL
  *
  * Utility function to do pull-based typefinding. Unlike gst_type_find_helper()
  * however, this function will use the specified function @func to obtain the
@@ -251,16 +262,14 @@ helper_find_get_length (gpointer data)
  * callback can then call the upstream peer pad with offsets adjusted for the
  * tag size, for example).
  *
- * When @extension is not NULL, this function will first try the typefind
+ * When @extension is not %NULL, this function will first try the typefind
  * functions for the given extension, which might speed up the typefinding
  * in many cases.
  *
  * Free-function: gst_caps_unref
  *
- * Returns: (transfer full): the #GstCaps corresponding to the data stream.
- *     Returns #NULL if no #GstCaps matches the data stream.
- *
- * Since: 0.10.26
+ * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data
+ *     stream.  Returns %NULL if no #GstCaps matches the data stream.
  */
 GstCaps *
 gst_type_find_helper_get_range (GstObject * obj, GstObject * parent,
@@ -308,9 +317,8 @@ gst_type_find_helper_get_range (GstObject * obj, GstObject * parent,
         extension);
 
     for (l = type_list; l; l = next) {
+      const gchar *const *ext;
       GstTypeFindFactory *factory;
-      gint i;
-      gchar **ext;
 
       next = l->next;
 
@@ -323,8 +331,8 @@ gst_type_find_helper_get_range (GstObject * obj, GstObject * parent,
       GST_LOG_OBJECT (obj, "testing factory %s for extension %s",
           GST_OBJECT_NAME (factory), extension);
 
-      for (i = 0; ext[i]; i++) {
-        if (strcmp (ext[i], extension) == 0) {
+      while (*ext != NULL) {
+        if (strcmp (*ext, extension) == 0) {
           /* found extension, move in front */
           GST_LOG_OBJECT (obj, "moving typefind for extension %s to head",
               extension);
@@ -336,6 +344,7 @@ gst_type_find_helper_get_range (GstObject * obj, GstObject * parent,
           pos++;
           break;
         }
+        ++ext;
       }
     }
   }
@@ -378,8 +387,8 @@ gst_type_find_helper_get_range (GstObject * obj, GstObject * parent,
  *
  * Free-function: gst_caps_unref
  *
- * Returns: (transfer full): the #GstCaps corresponding to the data stream.
- *     Returns #NULL if no #GstCaps matches the data stream.
+ * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data
+ *     stream.  Returns %NULL if no #GstCaps matches the data stream.
  */
 
 GstCaps *
@@ -416,8 +425,8 @@ typedef struct
  *
  * Get data pointer within a buffer.
  *
- * Returns: address inside the buffer or %NULL if buffer does not cover the
- * requested range.
+ * Returns: (nullable): address inside the buffer or %NULL if buffer does not
+ * cover the requested range.
  */
 static const guint8 *
 buf_helper_find_peek (gpointer data, gint64 off, guint size)
@@ -437,7 +446,7 @@ buf_helper_find_peek (gpointer data, gint64 off, guint size)
     return NULL;
   }
 
-  if ((off + size) <= helper->size)
+  if (((guint64) off + size) <= helper->size)
     return helper->data + off;
 
   return NULL;
@@ -452,8 +461,7 @@ buf_helper_find_peek (gpointer data, gint64 off, guint size)
  * If given @probability is higher, replace previously store caps.
  */
 static void
-buf_helper_find_suggest (gpointer data, GstTypeFindProbability probability,
-    GstCaps * caps)
+buf_helper_find_suggest (gpointer data, guint probability, GstCaps * caps)
 {
   GstTypeFindBufHelper *helper = (GstTypeFindBufHelper *) data;
 
@@ -470,28 +478,28 @@ buf_helper_find_suggest (gpointer data, GstTypeFindProbability probability,
 
 /**
  * gst_type_find_helper_for_data:
- * @obj: object doing the typefinding, or NULL (used for logging)
+ * @obj: (allow-none): object doing the typefinding, or %NULL (used for logging)
  * @data: (in) (transfer none): a pointer with data to typefind
- * @size: (in) (transfer none): the size of @data
+ * @size: (in): the size of @data
  * @prob: (out) (allow-none): location to store the probability of the found
- *     caps, or #NULL
+ *     caps, or %NULL
  *
  * Tries to find what type of data is contained in the given @data, the
  * assumption being that the data represents the beginning of the stream or
  * file.
  *
  * All available typefinders will be called on the data in order of rank. If
- * a typefinding function returns a probability of #GST_TYPE_FIND_MAXIMUM,
+ * a typefinding function returns a probability of %GST_TYPE_FIND_MAXIMUM,
  * typefinding is stopped immediately and the found caps will be returned
  * right away. Otherwise, all available typefind functions will the tried,
- * and the caps with the highest probability will be returned, or #NULL if
+ * and the caps with the highest probability will be returned, or %NULL if
  * the content of @data could not be identified.
  *
  * Free-function: gst_caps_unref
  *
- * Returns: (transfer full): the #GstCaps corresponding to the data, or #NULL
- *     if no type could be found. The caller should free the caps returned
- *     with gst_caps_unref().
+ * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
+ *     or %NULL if no type could be found. The caller should free the caps
+ *     returned with gst_caps_unref().
  */
 GstCaps *
 gst_type_find_helper_for_data (GstObject * obj, const guint8 * data, gsize size,
@@ -542,27 +550,27 @@ gst_type_find_helper_for_data (GstObject * obj, const guint8 * data, gsize size,
 
 /**
  * gst_type_find_helper_for_buffer:
- * @obj: object doing the typefinding, or NULL (used for logging)
+ * @obj: (allow-none): object doing the typefinding, or %NULL (used for logging)
  * @buf: (in) (transfer none): a #GstBuffer with data to typefind
  * @prob: (out) (allow-none): location to store the probability of the found
- *     caps, or #NULL
+ *     caps, or %NULL
  *
  * Tries to find what type of data is contained in the given #GstBuffer, the
  * assumption being that the buffer represents the beginning of the stream or
  * file.
  *
  * All available typefinders will be called on the data in order of rank. If
- * a typefinding function returns a probability of #GST_TYPE_FIND_MAXIMUM,
+ * a typefinding function returns a probability of %GST_TYPE_FIND_MAXIMUM,
  * typefinding is stopped immediately and the found caps will be returned
  * right away. Otherwise, all available typefind functions will the tried,
- * and the caps with the highest probability will be returned, or #NULL if
+ * and the caps with the highest probability will be returned, or %NULL if
  * the content of the buffer could not be identified.
  *
  * Free-function: gst_caps_unref
  *
- * Returns: (transfer full): the #GstCaps corresponding to the data, or #NULL
- *     if no type could be found. The caller should free the caps returned
- *     with gst_caps_unref().
+ * Returns: (transfer full) (nullable): the #GstCaps corresponding to the data,
+ *     or %NULL if no type could be found. The caller should free the caps
+ *     returned with gst_caps_unref().
  */
 GstCaps *
 gst_type_find_helper_for_buffer (GstObject * obj, GstBuffer * buf,
@@ -586,7 +594,7 @@ gst_type_find_helper_for_buffer (GstObject * obj, GstBuffer * buf,
 
 /**
  * gst_type_find_helper_for_extension:
- * @obj: (allow-none): object doing the typefinding, or NULL (used for logging)
+ * @obj: (allow-none): object doing the typefinding, or %NULL (used for logging)
  * @extension: an extension
  *
  * Tries to find the best #GstCaps associated with @extension.
@@ -597,11 +605,9 @@ gst_type_find_helper_for_buffer (GstObject * obj, GstBuffer * buf,
  *
  * Free-function: gst_caps_unref
  *
- * Returns: (transfer full): the #GstCaps corresponding to @extension, or
- *     #NULL if no type could be found. The caller should free the caps
- *     returned with gst_caps_unref().
- * 
- * Since: 0.10.23
+ * Returns: (transfer full) (nullable): the #GstCaps corresponding to
+ *     @extension, or %NULL if no type could be found. The caller should free
+ *     the caps returned with gst_caps_unref().
  */
 GstCaps *
 gst_type_find_helper_for_extension (GstObject * obj, const gchar * extension)
@@ -617,13 +623,12 @@ gst_type_find_helper_for_extension (GstObject * obj, const gchar * extension)
 
   for (l = type_list; l; l = g_list_next (l)) {
     GstTypeFindFactory *factory;
-    gchar **ext;
-    gint i;
+    const gchar *const *ext;
 
     factory = GST_TYPE_FIND_FACTORY (l->data);
 
     /* we only want to check those factories without a function */
-    if (factory->function != NULL)
+    if (gst_type_find_factory_has_function (factory))
       continue;
 
     /* get the extension that this typefind factory can handle */
@@ -633,14 +638,15 @@ gst_type_find_helper_for_extension (GstObject * obj, const gchar * extension)
 
     /* there are extension, see if one of them matches the requested
      * extension */
-    for (i = 0; ext[i]; i++) {
-      if (strcmp (ext[i], extension) == 0) {
+    while (*ext != NULL) {
+      if (strcmp (*ext, extension) == 0) {
         /* we found a matching extension, take the caps */
         if ((result = gst_type_find_factory_get_caps (factory))) {
           gst_caps_ref (result);
           goto done;
         }
       }
+      ++ext;
     }
   }
 done: