Blame libs/gst/base/gstbytereader.c

Packit a6ee4b
/* GStreamer byte reader
Packit a6ee4b
 *
Packit a6ee4b
 * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>.
Packit a6ee4b
 * Copyright (C) 2009,2014 Tim-Philipp Müller <tim centricular net>
Packit a6ee4b
 *
Packit a6ee4b
 * This library is free software; you can redistribute it and/or
Packit a6ee4b
 * modify it under the terms of the GNU Library General Public
Packit a6ee4b
 * License as published by the Free Software Foundation; either
Packit a6ee4b
 * version 2 of the License, or (at your option) any later version.
Packit a6ee4b
 *
Packit a6ee4b
 * This library is distributed in the hope that it will be useful,
Packit a6ee4b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit a6ee4b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit a6ee4b
 * Library General Public License for more details.
Packit a6ee4b
 *
Packit a6ee4b
 * You should have received a copy of the GNU Library General Public
Packit a6ee4b
 * License along with this library; if not, write to the
Packit a6ee4b
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit a6ee4b
 * Boston, MA 02110-1301, USA.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
#ifdef HAVE_CONFIG_H
Packit a6ee4b
#include "config.h"
Packit a6ee4b
#endif
Packit a6ee4b
Packit a6ee4b
#define GST_BYTE_READER_DISABLE_INLINES
Packit a6ee4b
#include "gstbytereader.h"
Packit a6ee4b
Packit a6ee4b
#include <string.h>
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * SECTION:gstbytereader
Packit a6ee4b
 * @title: GstByteReader
Packit a6ee4b
 * @short_description: Reads different integer, string and floating point
Packit a6ee4b
 *     types from a memory buffer
Packit a6ee4b
 *
Packit a6ee4b
 * #GstByteReader provides a byte reader that can read different integer and
Packit a6ee4b
 * floating point types from a memory buffer. It provides functions for reading
Packit a6ee4b
 * signed/unsigned, little/big endian integers of 8, 16, 24, 32 and 64 bits
Packit a6ee4b
 * and functions for reading little/big endian floating points numbers of
Packit a6ee4b
 * 32 and 64 bits. It also provides functions to read NUL-terminated strings
Packit a6ee4b
 * in various character encodings.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_new: (skip)
Packit a6ee4b
 * @data: (in) (transfer none) (array length=size): data from which the
Packit a6ee4b
 *     #GstByteReader should read
Packit a6ee4b
 * @size: Size of @data in bytes
Packit a6ee4b
 *
Packit a6ee4b
 * Create a new #GstByteReader instance, which will read from @data.
Packit a6ee4b
 *
Packit a6ee4b
 * Free-function: gst_byte_reader_free
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: (transfer full): a new #GstByteReader instance
Packit a6ee4b
 */
Packit a6ee4b
GstByteReader *
Packit a6ee4b
gst_byte_reader_new (const guint8 * data, guint size)
Packit a6ee4b
{
Packit a6ee4b
  GstByteReader *ret = g_slice_new0 (GstByteReader);
Packit a6ee4b
Packit a6ee4b
  ret->data = data;
Packit a6ee4b
  ret->size = size;
Packit a6ee4b
Packit a6ee4b
  return ret;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_free:
Packit a6ee4b
 * @reader: (in) (transfer full): a #GstByteReader instance
Packit a6ee4b
 *
Packit a6ee4b
 * Frees a #GstByteReader instance, which was previously allocated by
Packit a6ee4b
 * gst_byte_reader_new().
Packit a6ee4b
 */
Packit a6ee4b
void
Packit a6ee4b
gst_byte_reader_free (GstByteReader * reader)
Packit a6ee4b
{
Packit a6ee4b
  g_return_if_fail (reader != NULL);
Packit a6ee4b
Packit a6ee4b
  g_slice_free (GstByteReader, reader);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_init:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @data: (in) (transfer none) (array length=size): data from which
Packit a6ee4b
 *     the #GstByteReader should read
Packit a6ee4b
 * @size: Size of @data in bytes
Packit a6ee4b
 *
Packit a6ee4b
 * Initializes a #GstByteReader instance to read from @data. This function
Packit a6ee4b
 * can be called on already initialized instances.
Packit a6ee4b
 */
Packit a6ee4b
void
Packit a6ee4b
gst_byte_reader_init (GstByteReader * reader, const guint8 * data, guint size)
Packit a6ee4b
{
Packit a6ee4b
  g_return_if_fail (reader != NULL);
Packit a6ee4b
Packit a6ee4b
  reader->data = data;
Packit a6ee4b
  reader->size = size;
Packit a6ee4b
  reader->byte = 0;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_sub_reader: (skip)
Packit a6ee4b
 * @reader: an existing and initialized #GstByteReader instance
Packit a6ee4b
 * @sub_reader: a #GstByteReader instance to initialize as sub-reader
Packit a6ee4b
 * @size: size of @sub_reader in bytes
Packit a6ee4b
 *
Packit a6ee4b
 * Initializes a #GstByteReader sub-reader instance to contain @size bytes of
Packit a6ee4b
 * data from the current position of @reader. This is useful to read chunked
Packit a6ee4b
 * formats and make sure that one doesn't read beyond the size of the sub-chunk.
Packit a6ee4b
 *
Packit a6ee4b
 * Unlike gst_byte_reader_get_sub_reader(), this function does not modify the
Packit a6ee4b
 * current position of @reader.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: FALSE on error or if @reader does not contain @size more bytes from
Packit a6ee4b
 *     the current position, and otherwise TRUE
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.6
Packit a6ee4b
 */
Packit a6ee4b
gboolean
Packit a6ee4b
gst_byte_reader_peek_sub_reader (GstByteReader * reader,
Packit a6ee4b
    GstByteReader * sub_reader, guint size)
Packit a6ee4b
{
Packit a6ee4b
  return _gst_byte_reader_peek_sub_reader_inline (reader, sub_reader, size);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_sub_reader: (skip)
Packit a6ee4b
 * @reader: an existing and initialized #GstByteReader instance
Packit a6ee4b
 * @sub_reader: a #GstByteReader instance to initialize as sub-reader
Packit a6ee4b
 * @size: size of @sub_reader in bytes
Packit a6ee4b
 *
Packit a6ee4b
 * Initializes a #GstByteReader sub-reader instance to contain @size bytes of
Packit a6ee4b
 * data from the current position of @reader. This is useful to read chunked
Packit a6ee4b
 * formats and make sure that one doesn't read beyond the size of the sub-chunk.
Packit a6ee4b
 *
Packit a6ee4b
 * Unlike gst_byte_reader_peek_sub_reader(), this function also modifies the
Packit a6ee4b
 * position of @reader and moves it forward by @size bytes.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: FALSE on error or if @reader does not contain @size more bytes from
Packit a6ee4b
 *     the current position, and otherwise TRUE
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.6
Packit a6ee4b
 */
Packit a6ee4b
gboolean
Packit a6ee4b
gst_byte_reader_get_sub_reader (GstByteReader * reader,
Packit a6ee4b
    GstByteReader * sub_reader, guint size)
Packit a6ee4b
{
Packit a6ee4b
  return _gst_byte_reader_get_sub_reader_inline (reader, sub_reader, size);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_set_pos:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @pos: The new position in bytes
Packit a6ee4b
 *
Packit a6ee4b
 * Sets the new position of a #GstByteReader instance to @pos in bytes.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if the position could be set successfully, %FALSE
Packit a6ee4b
 * otherwise.
Packit a6ee4b
 */
Packit a6ee4b
gboolean
Packit a6ee4b
gst_byte_reader_set_pos (GstByteReader * reader, guint pos)
Packit a6ee4b
{
Packit a6ee4b
  g_return_val_if_fail (reader != NULL, FALSE);
Packit a6ee4b
Packit a6ee4b
  if (pos > reader->size)
Packit a6ee4b
    return FALSE;
Packit a6ee4b
Packit a6ee4b
  reader->byte = pos;
Packit a6ee4b
Packit a6ee4b
  return TRUE;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_pos:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 *
Packit a6ee4b
 * Returns the current position of a #GstByteReader instance in bytes.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: The current position of @reader in bytes.
Packit a6ee4b
 */
Packit a6ee4b
guint
Packit a6ee4b
gst_byte_reader_get_pos (const GstByteReader * reader)
Packit a6ee4b
{
Packit a6ee4b
  return _gst_byte_reader_get_pos_inline (reader);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_remaining:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 *
Packit a6ee4b
 * Returns the remaining number of bytes of a #GstByteReader instance.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: The remaining number of bytes of @reader instance.
Packit a6ee4b
 */
Packit a6ee4b
guint
Packit a6ee4b
gst_byte_reader_get_remaining (const GstByteReader * reader)
Packit a6ee4b
{
Packit a6ee4b
  return _gst_byte_reader_get_remaining_inline (reader);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_size:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 *
Packit a6ee4b
 * Returns the total number of bytes of a #GstByteReader instance.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: The total number of bytes of @reader instance.
Packit a6ee4b
 */
Packit a6ee4b
guint
Packit a6ee4b
gst_byte_reader_get_size (const GstByteReader * reader)
Packit a6ee4b
{
Packit a6ee4b
  return _gst_byte_reader_get_size_inline (reader);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
#define gst_byte_reader_get_remaining _gst_byte_reader_get_remaining_inline
Packit a6ee4b
#define gst_byte_reader_get_size _gst_byte_reader_get_size_inline
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_skip:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @nbytes: the number of bytes to skip
Packit a6ee4b
 *
Packit a6ee4b
 * Skips @nbytes bytes of the #GstByteReader instance.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if @nbytes bytes could be skipped, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
gboolean
Packit a6ee4b
gst_byte_reader_skip (GstByteReader * reader, guint nbytes)
Packit a6ee4b
{
Packit a6ee4b
  return _gst_byte_reader_skip_inline (reader, nbytes);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_uint8:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #guint8 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read an unsigned 8 bit integer into @val and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_int8:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gint8 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a signed 8 bit integer into @val and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_uint8:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #guint8 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read an unsigned 8 bit integer into @val but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_int8:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gint8 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a signed 8 bit integer into @val but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_uint16_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #guint16 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read an unsigned 16 bit little endian integer into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_int16_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gint16 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a signed 16 bit little endian integer into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_uint16_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #guint16 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read an unsigned 16 bit little endian integer into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_int16_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gint16 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a signed 16 bit little endian integer into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_uint16_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #guint16 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read an unsigned 16 bit big endian integer into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_int16_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gint16 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a signed 16 bit big endian integer into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_uint16_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #guint16 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read an unsigned 16 bit big endian integer into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_int16_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gint16 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a signed 16 bit big endian integer into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_uint24_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #guint32 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read an unsigned 24 bit little endian integer into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_int24_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gint32 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a signed 24 bit little endian integer into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_uint24_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #guint32 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read an unsigned 24 bit little endian integer into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_int24_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gint32 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a signed 24 bit little endian integer into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_uint24_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #guint32 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read an unsigned 24 bit big endian integer into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_int24_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gint32 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a signed 24 bit big endian integer into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_uint24_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #guint32 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read an unsigned 24 bit big endian integer into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_int24_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gint32 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a signed 24 bit big endian integer into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_uint32_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #guint32 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read an unsigned 32 bit little endian integer into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_int32_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gint32 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a signed 32 bit little endian integer into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_uint32_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #guint32 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read an unsigned 32 bit little endian integer into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_int32_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gint32 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a signed 32 bit little endian integer into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_uint32_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #guint32 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read an unsigned 32 bit big endian integer into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_int32_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gint32 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a signed 32 bit big endian integer into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_uint32_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #guint32 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read an unsigned 32 bit big endian integer into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_int32_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gint32 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a signed 32 bit big endian integer into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_uint64_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #guint64 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read an unsigned 64 bit little endian integer into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_int64_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gint64 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a signed 64 bit little endian integer into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_uint64_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #guint64 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read an unsigned 64 bit little endian integer into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_int64_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gint64 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a signed 64 bit little endian integer into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_uint64_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #guint64 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read an unsigned 64 bit big endian integer into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_int64_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gint64 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a signed 64 bit big endian integer into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_uint64_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #guint64 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read an unsigned 64 bit big endian integer into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_int64_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gint64 to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a signed 64 bit big endian integer into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
#define GST_BYTE_READER_PEEK_GET(bits,type,name) \
Packit a6ee4b
gboolean \
Packit a6ee4b
gst_byte_reader_get_##name (GstByteReader * reader, type * val) \
Packit a6ee4b
{ \
Packit a6ee4b
  return _gst_byte_reader_get_##name##_inline (reader, val); \
Packit a6ee4b
} \
Packit a6ee4b
\
Packit a6ee4b
gboolean \
Packit a6ee4b
gst_byte_reader_peek_##name (const GstByteReader * reader, type * val) \
Packit a6ee4b
{ \
Packit a6ee4b
  return _gst_byte_reader_peek_##name##_inline (reader, val); \
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/* *INDENT-OFF* */
Packit a6ee4b
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(8,guint8,uint8)
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(8,gint8,int8)
Packit a6ee4b
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(16,guint16,uint16_le)
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(16,guint16,uint16_be)
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(16,gint16,int16_le)
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(16,gint16,int16_be)
Packit a6ee4b
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(24,guint32,uint24_le)
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(24,guint32,uint24_be)
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(24,gint32,int24_le)
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(24,gint32,int24_be)
Packit a6ee4b
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(32,guint32,uint32_le)
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(32,guint32,uint32_be)
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(32,gint32,int32_le)
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(32,gint32,int32_be)
Packit a6ee4b
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(64,guint64,uint64_le)
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(64,guint64,uint64_be)
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(64,gint64,int64_le)
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(64,gint64,int64_be)
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_float32_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gfloat to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a 32 bit little endian floating point value into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_float32_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gfloat to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a 32 bit little endian floating point value into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_float32_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gfloat to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a 32 bit big endian floating point value into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_float32_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gfloat to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a 32 bit big endian floating point value into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_float64_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gdouble to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a 64 bit little endian floating point value into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_float64_le:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gdouble to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a 64 bit little endian floating point value into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_float64_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gdouble to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a 64 bit big endian floating point value into @val
Packit a6ee4b
 * and update the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_float64_be:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @val: (out): Pointer to a #gdouble to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Read a 64 bit big endian floating point value into @val
Packit a6ee4b
 * but keep the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(32,gfloat,float32_le)
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(32,gfloat,float32_be)
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(64,gdouble,float64_le)
Packit a6ee4b
GST_BYTE_READER_PEEK_GET(64,gdouble,float64_be)
Packit a6ee4b
Packit a6ee4b
/* *INDENT-ON* */
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_data:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @size: Size in bytes
Packit a6ee4b
 * @val: (out) (transfer none) (array length=size): address of a
Packit a6ee4b
 *     #guint8 pointer variable in which to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Returns a constant pointer to the current data
Packit a6ee4b
 * position if at least @size bytes are left and
Packit a6ee4b
 * updates the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
gboolean
Packit a6ee4b
gst_byte_reader_get_data (GstByteReader * reader, guint size,
Packit a6ee4b
    const guint8 ** val)
Packit a6ee4b
{
Packit a6ee4b
  return _gst_byte_reader_get_data_inline (reader, size, val);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_data:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @size: Size in bytes
Packit a6ee4b
 * @val: (out) (transfer none) (array length=size): address of a
Packit a6ee4b
 *     #guint8 pointer variable in which to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Returns a constant pointer to the current data
Packit a6ee4b
 * position if at least @size bytes are left and
Packit a6ee4b
 * keeps the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
gboolean
Packit a6ee4b
gst_byte_reader_peek_data (const GstByteReader * reader, guint size,
Packit a6ee4b
    const guint8 ** val)
Packit a6ee4b
{
Packit a6ee4b
  return _gst_byte_reader_peek_data_inline (reader, size, val);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_dup_data:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @size: Size in bytes
Packit a6ee4b
 * @val: (out) (transfer full) (array length=size): address of a
Packit a6ee4b
 *     #guint8 pointer variable in which to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Free-function: g_free
Packit a6ee4b
 *
Packit a6ee4b
 * Returns a newly-allocated copy of the current data
Packit a6ee4b
 * position if at least @size bytes are left and
Packit a6ee4b
 * updates the current position. Free with g_free() when no longer needed.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if successful, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
gboolean
Packit a6ee4b
gst_byte_reader_dup_data (GstByteReader * reader, guint size, guint8 ** val)
Packit a6ee4b
{
Packit a6ee4b
  return _gst_byte_reader_dup_data_inline (reader, size, val);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/* Special optimized scan for mask 0xffffff00 and pattern 0x00000100 */
Packit a6ee4b
static inline gint
Packit a6ee4b
_scan_for_start_code (const guint8 * data, guint size)
Packit a6ee4b
{
Packit a6ee4b
  guint8 *pdata = (guint8 *) data;
Packit a6ee4b
  guint8 *pend = (guint8 *) (data + size - 4);
Packit a6ee4b
Packit a6ee4b
  while (pdata <= pend) {
Packit a6ee4b
    if (pdata[2] > 1) {
Packit a6ee4b
      pdata += 3;
Packit a6ee4b
    } else if (pdata[1]) {
Packit a6ee4b
      pdata += 2;
Packit a6ee4b
    } else if (pdata[0] || pdata[2] != 1) {
Packit a6ee4b
      pdata++;
Packit a6ee4b
    } else {
Packit a6ee4b
      return (pdata - data);
Packit a6ee4b
    }
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  /* nothing found */
Packit a6ee4b
  return -1;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
static inline guint
Packit a6ee4b
_masked_scan_uint32_peek (const GstByteReader * reader,
Packit a6ee4b
    guint32 mask, guint32 pattern, guint offset, guint size, guint32 * value)
Packit a6ee4b
{
Packit a6ee4b
  const guint8 *data;
Packit a6ee4b
  guint32 state;
Packit a6ee4b
  guint i;
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (size > 0, -1);
Packit a6ee4b
  g_return_val_if_fail ((guint64) offset + size <= reader->size - reader->byte,
Packit a6ee4b
      -1);
Packit a6ee4b
Packit a6ee4b
  /* we can't find the pattern with less than 4 bytes */
Packit a6ee4b
  if (G_UNLIKELY (size < 4))
Packit a6ee4b
    return -1;
Packit a6ee4b
Packit a6ee4b
  data = reader->data + reader->byte + offset;
Packit a6ee4b
Packit a6ee4b
  /* Handle special case found in MPEG and H264 */
Packit a6ee4b
  if ((pattern == 0x00000100) && (mask == 0xffffff00)) {
Packit a6ee4b
    gint ret = _scan_for_start_code (data, size);
Packit a6ee4b
Packit a6ee4b
    if (ret == -1)
Packit a6ee4b
      return ret;
Packit a6ee4b
Packit a6ee4b
    if (value != NULL)
Packit a6ee4b
      *value = (1 << 8) | data[ret + 3];
Packit a6ee4b
Packit a6ee4b
    return ret + offset;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  /* set the state to something that does not match */
Packit a6ee4b
  state = ~pattern;
Packit a6ee4b
Packit a6ee4b
  /* now find data */
Packit a6ee4b
  for (i = 0; i < size; i++) {
Packit a6ee4b
    /* throw away one byte and move in the next byte */
Packit a6ee4b
    state = ((state << 8) | data[i]);
Packit a6ee4b
    if (G_UNLIKELY ((state & mask) == pattern)) {
Packit a6ee4b
      /* we have a match but we need to have skipped at
Packit a6ee4b
       * least 4 bytes to fill the state. */
Packit a6ee4b
      if (G_LIKELY (i >= 3)) {
Packit a6ee4b
        if (value)
Packit a6ee4b
          *value = state;
Packit a6ee4b
        return offset + i - 3;
Packit a6ee4b
      }
Packit a6ee4b
    }
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  /* nothing found */
Packit a6ee4b
  return -1;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_masked_scan_uint32:
Packit a6ee4b
 * @reader: a #GstByteReader
Packit a6ee4b
 * @mask: mask to apply to data before matching against @pattern
Packit a6ee4b
 * @pattern: pattern to match (after mask is applied)
Packit a6ee4b
 * @offset: offset from which to start scanning, relative to the current
Packit a6ee4b
 *     position
Packit a6ee4b
 * @size: number of bytes to scan from offset
Packit a6ee4b
 *
Packit a6ee4b
 * Scan for pattern @pattern with applied mask @mask in the byte reader data,
Packit a6ee4b
 * starting from offset @offset relative to the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * The bytes in @pattern and @mask are interpreted left-to-right, regardless
Packit a6ee4b
 * of endianness.  All four bytes of the pattern must be present in the
Packit a6ee4b
 * byte reader data for it to match, even if the first or last bytes are masked
Packit a6ee4b
 * out.
Packit a6ee4b
 *
Packit a6ee4b
 * It is an error to call this function without making sure that there is
Packit a6ee4b
 * enough data (offset+size bytes) in the byte reader.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: offset of the first match, or -1 if no match was found.
Packit a6ee4b
 *
Packit a6ee4b
 * Example:
Packit a6ee4b
 * |[
Packit a6ee4b
 * // Assume the reader contains 0x00 0x01 0x02 ... 0xfe 0xff
Packit a6ee4b
 *
Packit a6ee4b
 * gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0x00010203, 0, 256);
Packit a6ee4b
 * // -> returns 0
Packit a6ee4b
 * gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0x00010203, 1, 255);
Packit a6ee4b
 * // -> returns -1
Packit a6ee4b
 * gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0x01020304, 1, 255);
Packit a6ee4b
 * // -> returns 1
Packit a6ee4b
 * gst_byte_reader_masked_scan_uint32 (reader, 0xffff, 0x0001, 0, 256);
Packit a6ee4b
 * // -> returns -1
Packit a6ee4b
 * gst_byte_reader_masked_scan_uint32 (reader, 0xffff, 0x0203, 0, 256);
Packit a6ee4b
 * // -> returns 0
Packit a6ee4b
 * gst_byte_reader_masked_scan_uint32 (reader, 0xffff0000, 0x02030000, 0, 256);
Packit a6ee4b
 * // -> returns 2
Packit a6ee4b
 * gst_byte_reader_masked_scan_uint32 (reader, 0xffff0000, 0x02030000, 0, 4);
Packit a6ee4b
 * // -> returns -1
Packit a6ee4b
 * ]|
Packit a6ee4b
 */
Packit a6ee4b
guint
Packit a6ee4b
gst_byte_reader_masked_scan_uint32 (const GstByteReader * reader, guint32 mask,
Packit a6ee4b
    guint32 pattern, guint offset, guint size)
Packit a6ee4b
{
Packit a6ee4b
  return _masked_scan_uint32_peek (reader, mask, pattern, offset, size, NULL);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_masked_scan_uint32_peek:
Packit a6ee4b
 * @reader: a #GstByteReader
Packit a6ee4b
 * @mask: mask to apply to data before matching against @pattern
Packit a6ee4b
 * @pattern: pattern to match (after mask is applied)
Packit a6ee4b
 * @offset: offset from which to start scanning, relative to the current
Packit a6ee4b
 *     position
Packit a6ee4b
 * @size: number of bytes to scan from offset
Packit a6ee4b
 * @value: (out): pointer to uint32 to return matching data
Packit a6ee4b
 *
Packit a6ee4b
 * Scan for pattern @pattern with applied mask @mask in the byte reader data,
Packit a6ee4b
 * starting from offset @offset relative to the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * The bytes in @pattern and @mask are interpreted left-to-right, regardless
Packit a6ee4b
 * of endianness.  All four bytes of the pattern must be present in the
Packit a6ee4b
 * byte reader data for it to match, even if the first or last bytes are masked
Packit a6ee4b
 * out.
Packit a6ee4b
 *
Packit a6ee4b
 * It is an error to call this function without making sure that there is
Packit a6ee4b
 * enough data (offset+size bytes) in the byte reader.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: offset of the first match, or -1 if no match was found.
Packit a6ee4b
 *
Packit a6ee4b
 * Since: 1.6
Packit a6ee4b
 */
Packit a6ee4b
guint
Packit a6ee4b
gst_byte_reader_masked_scan_uint32_peek (const GstByteReader * reader,
Packit a6ee4b
    guint32 mask, guint32 pattern, guint offset, guint size, guint32 * value)
Packit a6ee4b
{
Packit a6ee4b
  return _masked_scan_uint32_peek (reader, mask, pattern, offset, size, value);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
#define GST_BYTE_READER_SCAN_STRING(bits) \
Packit a6ee4b
static guint \
Packit a6ee4b
gst_byte_reader_scan_string_utf##bits (const GstByteReader * reader) \
Packit a6ee4b
{ \
Packit a6ee4b
  guint len, off, max_len; \
Packit a6ee4b
  \
Packit a6ee4b
  max_len = (reader->size - reader->byte) / sizeof (guint##bits); \
Packit a6ee4b
  \
Packit a6ee4b
  /* need at least a single NUL terminator */ \
Packit a6ee4b
  if (max_len < 1) \
Packit a6ee4b
    return 0; \
Packit a6ee4b
  \
Packit a6ee4b
  len = 0; \
Packit a6ee4b
  off = reader->byte; \
Packit a6ee4b
  /* endianness does not matter if we are looking for a NUL terminator */ \
Packit a6ee4b
  while (GST_READ_UINT##bits##_LE (&reader->data[off]) != 0) { \
Packit a6ee4b
    ++len; \
Packit a6ee4b
    off += sizeof (guint##bits); \
Packit a6ee4b
    /* have we reached the end without finding a NUL terminator? */ \
Packit a6ee4b
    if (len == max_len) \
Packit a6ee4b
      return 0; \
Packit a6ee4b
  } \
Packit a6ee4b
  /* return size in bytes including the NUL terminator (hence the +1) */ \
Packit a6ee4b
  return (len + 1) * sizeof (guint##bits); \
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
#define GST_READ_UINT8_LE GST_READ_UINT8
Packit a6ee4b
GST_BYTE_READER_SCAN_STRING (8);
Packit a6ee4b
#undef GST_READ_UINT8_LE
Packit a6ee4b
GST_BYTE_READER_SCAN_STRING (16);
Packit a6ee4b
GST_BYTE_READER_SCAN_STRING (32);
Packit a6ee4b
Packit a6ee4b
#define GST_BYTE_READER_SKIP_STRING(bits) \
Packit a6ee4b
gboolean \
Packit a6ee4b
gst_byte_reader_skip_string_utf##bits (GstByteReader * reader) \
Packit a6ee4b
{ \
Packit a6ee4b
  guint size; /* size in bytes including the terminator */ \
Packit a6ee4b
  \
Packit a6ee4b
  g_return_val_if_fail (reader != NULL, FALSE); \
Packit a6ee4b
  \
Packit a6ee4b
  size = gst_byte_reader_scan_string_utf##bits (reader); \
Packit a6ee4b
  reader->byte += size; \
Packit a6ee4b
  return (size > 0); \
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_skip_string:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 *
Packit a6ee4b
 * Skips a NUL-terminated string in the #GstByteReader instance, advancing
Packit a6ee4b
 * the current position to the byte after the string. This will work for
Packit a6ee4b
 * any NUL-terminated string with a character width of 8 bits, so ASCII,
Packit a6ee4b
 * UTF-8, ISO-8859-N etc.
Packit a6ee4b
 *
Packit a6ee4b
 * This function will fail if no NUL-terminator was found in in the data.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if a string could be skipped, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_skip_string_utf8:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 *
Packit a6ee4b
 * Skips a NUL-terminated string in the #GstByteReader instance, advancing
Packit a6ee4b
 * the current position to the byte after the string. This will work for
Packit a6ee4b
 * any NUL-terminated string with a character width of 8 bits, so ASCII,
Packit a6ee4b
 * UTF-8, ISO-8859-N etc. No input checking for valid UTF-8 is done.
Packit a6ee4b
 *
Packit a6ee4b
 * This function will fail if no NUL-terminator was found in in the data.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if a string could be skipped, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
GST_BYTE_READER_SKIP_STRING (8);
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_skip_string_utf16:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 *
Packit a6ee4b
 * Skips a NUL-terminated UTF-16 string in the #GstByteReader instance,
Packit a6ee4b
 * advancing the current position to the byte after the string.
Packit a6ee4b
 *
Packit a6ee4b
 * No input checking for valid UTF-16 is done.
Packit a6ee4b
 *
Packit a6ee4b
 * This function will fail if no NUL-terminator was found in in the data.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if a string could be skipped, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
GST_BYTE_READER_SKIP_STRING (16);
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_skip_string_utf32:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 *
Packit a6ee4b
 * Skips a NUL-terminated UTF-32 string in the #GstByteReader instance,
Packit a6ee4b
 * advancing the current position to the byte after the string.
Packit a6ee4b
 *
Packit a6ee4b
 * No input checking for valid UTF-32 is done.
Packit a6ee4b
 *
Packit a6ee4b
 * This function will fail if no NUL-terminator was found in in the data.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if a string could be skipped, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
GST_BYTE_READER_SKIP_STRING (32);
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_string:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @str: (out) (transfer none) (array zero-terminated=1): address of a
Packit a6ee4b
 *     #gchar pointer variable in which to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Returns a constant pointer to the current data position if there is
Packit a6ee4b
 * a NUL-terminated string in the data (this could be just a NUL terminator).
Packit a6ee4b
 * The current position will be maintained. This will work for any
Packit a6ee4b
 * NUL-terminated string with a character width of 8 bits, so ASCII,
Packit a6ee4b
 * UTF-8, ISO-8859-N etc.
Packit a6ee4b
 *
Packit a6ee4b
 * This function will fail if no NUL-terminator was found in in the data.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if a string could be skipped, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_peek_string_utf8:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @str: (out) (transfer none) (array zero-terminated=1): address of a
Packit a6ee4b
 *     #gchar pointer variable in which to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Returns a constant pointer to the current data position if there is
Packit a6ee4b
 * a NUL-terminated string in the data (this could be just a NUL terminator).
Packit a6ee4b
 * The current position will be maintained. This will work for any
Packit a6ee4b
 * NUL-terminated string with a character width of 8 bits, so ASCII,
Packit a6ee4b
 * UTF-8, ISO-8859-N etc.
Packit a6ee4b
 *
Packit a6ee4b
 * No input checking for valid UTF-8 is done.
Packit a6ee4b
 *
Packit a6ee4b
 * This function will fail if no NUL-terminator was found in in the data.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if a string could be skipped, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
gboolean
Packit a6ee4b
gst_byte_reader_peek_string_utf8 (const GstByteReader * reader,
Packit a6ee4b
    const gchar ** str)
Packit a6ee4b
{
Packit a6ee4b
  g_return_val_if_fail (reader != NULL, FALSE);
Packit a6ee4b
  g_return_val_if_fail (str != NULL, FALSE);
Packit a6ee4b
Packit a6ee4b
  if (gst_byte_reader_scan_string_utf8 (reader) > 0) {
Packit a6ee4b
    *str = (const gchar *) (reader->data + reader->byte);
Packit a6ee4b
  } else {
Packit a6ee4b
    *str = NULL;
Packit a6ee4b
  }
Packit a6ee4b
  return (*str != NULL);
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_get_string_utf8:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @str: (out) (transfer none) (array zero-terminated=1): address of a
Packit a6ee4b
 *     #gchar pointer variable in which to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Returns a constant pointer to the current data position if there is
Packit a6ee4b
 * a NUL-terminated string in the data (this could be just a NUL terminator),
Packit a6ee4b
 * advancing the current position to the byte after the string. This will work
Packit a6ee4b
 * for any NUL-terminated string with a character width of 8 bits, so ASCII,
Packit a6ee4b
 * UTF-8, ISO-8859-N etc.
Packit a6ee4b
 *
Packit a6ee4b
 * No input checking for valid UTF-8 is done.
Packit a6ee4b
 *
Packit a6ee4b
 * This function will fail if no NUL-terminator was found in in the data.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if a string could be found, %FALSE otherwise.
Packit a6ee4b
 */
Packit a6ee4b
gboolean
Packit a6ee4b
gst_byte_reader_get_string_utf8 (GstByteReader * reader, const gchar ** str)
Packit a6ee4b
{
Packit a6ee4b
  guint size;                   /* size in bytes including the terminator */
Packit a6ee4b
Packit a6ee4b
  g_return_val_if_fail (reader != NULL, FALSE);
Packit a6ee4b
  g_return_val_if_fail (str != NULL, FALSE);
Packit a6ee4b
Packit a6ee4b
  size = gst_byte_reader_scan_string_utf8 (reader);
Packit a6ee4b
  if (size == 0) {
Packit a6ee4b
    *str = NULL;
Packit a6ee4b
    return FALSE;
Packit a6ee4b
  }
Packit a6ee4b
Packit a6ee4b
  *str = (const gchar *) (reader->data + reader->byte);
Packit a6ee4b
  reader->byte += size;
Packit a6ee4b
  return TRUE;
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
#define GST_BYTE_READER_DUP_STRING(bits,type) \
Packit a6ee4b
gboolean \
Packit a6ee4b
gst_byte_reader_dup_string_utf##bits (GstByteReader * reader, type ** str) \
Packit a6ee4b
{ \
Packit a6ee4b
  guint size; /* size in bytes including the terminator */ \
Packit a6ee4b
  \
Packit a6ee4b
  g_return_val_if_fail (reader != NULL, FALSE); \
Packit a6ee4b
  g_return_val_if_fail (str != NULL, FALSE); \
Packit a6ee4b
  \
Packit a6ee4b
  size = gst_byte_reader_scan_string_utf##bits (reader); \
Packit a6ee4b
  if (size == 0) { \
Packit a6ee4b
    *str = NULL; \
Packit a6ee4b
    return FALSE; \
Packit a6ee4b
  } \
Packit a6ee4b
  *str = g_memdup (reader->data + reader->byte, size); \
Packit a6ee4b
  reader->byte += size; \
Packit a6ee4b
  return TRUE; \
Packit a6ee4b
}
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_dup_string_utf8:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @str: (out) (transfer full) (array zero-terminated=1): address of a
Packit a6ee4b
 *     #gchar pointer variable in which to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Free-function: g_free
Packit a6ee4b
 *
Packit a6ee4b
 * FIXME:Reads (copies) a NUL-terminated string in the #GstByteReader instance,
Packit a6ee4b
 * advancing the current position to the byte after the string. This will work
Packit a6ee4b
 * for any NUL-terminated string with a character width of 8 bits, so ASCII,
Packit a6ee4b
 * UTF-8, ISO-8859-N etc. No input checking for valid UTF-8 is done.
Packit a6ee4b
 *
Packit a6ee4b
 * This function will fail if no NUL-terminator was found in in the data.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if a string could be read into @str, %FALSE otherwise. The
Packit a6ee4b
 *     string put into @str must be freed with g_free() when no longer needed.
Packit a6ee4b
 */
Packit a6ee4b
GST_BYTE_READER_DUP_STRING (8, gchar);
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_dup_string_utf16:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @str: (out) (transfer full) (array zero-terminated=1): address of a
Packit a6ee4b
 *     #guint16 pointer variable in which to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Free-function: g_free
Packit a6ee4b
 *
Packit a6ee4b
 * Returns a newly-allocated copy of the current data position if there is
Packit a6ee4b
 * a NUL-terminated UTF-16 string in the data (this could be an empty string
Packit a6ee4b
 * as well), and advances the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * No input checking for valid UTF-16 is done. This function is endianness
Packit a6ee4b
 * agnostic - you should not assume the UTF-16 characters are in host
Packit a6ee4b
 * endianness.
Packit a6ee4b
 *
Packit a6ee4b
 * This function will fail if no NUL-terminator was found in in the data.
Packit a6ee4b
 *
Packit a6ee4b
 * Note: there is no peek or get variant of this function to ensure correct
Packit a6ee4b
 * byte alignment of the UTF-16 string.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if a string could be read, %FALSE otherwise. The
Packit a6ee4b
 *     string put into @str must be freed with g_free() when no longer needed.
Packit a6ee4b
 */
Packit a6ee4b
GST_BYTE_READER_DUP_STRING (16, guint16);
Packit a6ee4b
Packit a6ee4b
/**
Packit a6ee4b
 * gst_byte_reader_dup_string_utf32:
Packit a6ee4b
 * @reader: a #GstByteReader instance
Packit a6ee4b
 * @str: (out) (transfer full) (array zero-terminated=1): address of a
Packit a6ee4b
 *     #guint32 pointer variable in which to store the result
Packit a6ee4b
 *
Packit a6ee4b
 * Free-function: g_free
Packit a6ee4b
 *
Packit a6ee4b
 * Returns a newly-allocated copy of the current data position if there is
Packit a6ee4b
 * a NUL-terminated UTF-32 string in the data (this could be an empty string
Packit a6ee4b
 * as well), and advances the current position.
Packit a6ee4b
 *
Packit a6ee4b
 * No input checking for valid UTF-32 is done. This function is endianness
Packit a6ee4b
 * agnostic - you should not assume the UTF-32 characters are in host
Packit a6ee4b
 * endianness.
Packit a6ee4b
 *
Packit a6ee4b
 * This function will fail if no NUL-terminator was found in in the data.
Packit a6ee4b
 *
Packit a6ee4b
 * Note: there is no peek or get variant of this function to ensure correct
Packit a6ee4b
 * byte alignment of the UTF-32 string.
Packit a6ee4b
 *
Packit a6ee4b
 * Returns: %TRUE if a string could be read, %FALSE otherwise. The
Packit a6ee4b
 *     string put into @str must be freed with g_free() when no longer needed.
Packit a6ee4b
 */
Packit a6ee4b
GST_BYTE_READER_DUP_STRING (32, guint32);