1 /* GStreamer byte reader
3 * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>.
4 * Copyright (C) 2009 Tim-Philipp Müller <tim centricular net>
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.
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.
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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
26 #define GST_BYTE_READER_DISABLE_INLINES
27 #include "gstbytereader.h"
32 * SECTION:gstbytereader
33 * @short_description: Reads different integer, string and floating point
34 * types from a memory buffer
36 * #GstByteReader provides a byte reader that can read different integer and
37 * floating point types from a memory buffer. It provides functions for reading
38 * signed/unsigned, little/big endian integers of 8, 16, 24, 32 and 64 bits
39 * and functions for reading little/big endian floating points numbers of
40 * 32 and 64 bits. It also provides functions to read NUL-terminated strings
41 * in various character encodings.
45 * gst_byte_reader_new:
46 * @data: (in) (transfer none) (array length=size): data from which the
47 * #GstByteReader should read
48 * @size: Size of @data in bytes
50 * Create a new #GstByteReader instance, which will read from @data.
52 * Free-function: gst_byte_reader_free
54 * Returns: (transfer full): a new #GstByteReader instance
59 gst_byte_reader_new (const guint8 * data, guint size)
61 GstByteReader *ret = g_slice_new0 (GstByteReader);
70 * gst_byte_reader_new_from_buffer:
71 * @buffer: (transfer none): Buffer from which the #GstByteReader should read
73 * Create a new #GstByteReader instance, which will read from the
76 * Free-function: gst_byte_reader_free
78 * Returns: (transfer full): a new #GstByteReader instance
83 gst_byte_reader_new_from_buffer (const GstBuffer * buffer)
85 g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
87 return gst_byte_reader_new (GST_BUFFER_DATA (buffer),
88 GST_BUFFER_SIZE (buffer));
92 * gst_byte_reader_free:
93 * @reader: (in) (transfer full): a #GstByteReader instance
95 * Frees a #GstByteReader instance, which was previously allocated by
96 * gst_byte_reader_new() or gst_byte_reader_new_from_buffer().
101 gst_byte_reader_free (GstByteReader * reader)
103 g_return_if_fail (reader != NULL);
105 g_slice_free (GstByteReader, reader);
109 * gst_byte_reader_init:
110 * @reader: a #GstByteReader instance
111 * @data: (in) (transfer none) (array length=size): data from which
112 * the #GstByteReader should read
113 * @size: Size of @data in bytes
115 * Initializes a #GstByteReader instance to read from @data. This function
116 * can be called on already initialized instances.
121 gst_byte_reader_init (GstByteReader * reader, const guint8 * data, guint size)
123 g_return_if_fail (reader != NULL);
131 * gst_byte_reader_init_from_buffer:
132 * @reader: a #GstByteReader instance
133 * @buffer: (transfer none): Buffer from which the #GstByteReader should read
135 * Initializes a #GstByteReader instance to read from @buffer. This function
136 * can be called on already initialized instances.
141 gst_byte_reader_init_from_buffer (GstByteReader * reader,
142 const GstBuffer * buffer)
144 g_return_if_fail (GST_IS_BUFFER (buffer));
146 gst_byte_reader_init (reader, GST_BUFFER_DATA (buffer),
147 GST_BUFFER_SIZE (buffer));
151 * gst_byte_reader_set_pos:
152 * @reader: a #GstByteReader instance
153 * @pos: The new position in bytes
155 * Sets the new position of a #GstByteReader instance to @pos in bytes.
157 * Returns: %TRUE if the position could be set successfully, %FALSE
163 gst_byte_reader_set_pos (GstByteReader * reader, guint pos)
165 g_return_val_if_fail (reader != NULL, FALSE);
167 if (pos > reader->size)
176 * gst_byte_reader_get_pos:
177 * @reader: a #GstByteReader instance
179 * Returns the current position of a #GstByteReader instance in bytes.
181 * Returns: The current position of @reader in bytes.
186 gst_byte_reader_get_pos (const GstByteReader * reader)
188 return _gst_byte_reader_get_pos_inline (reader);
192 * gst_byte_reader_get_remaining:
193 * @reader: a #GstByteReader instance
195 * Returns the remaining number of bytes of a #GstByteReader instance.
197 * Returns: The remaining number of bytes of @reader instance.
202 gst_byte_reader_get_remaining (const GstByteReader * reader)
204 return _gst_byte_reader_get_remaining_inline (reader);
208 * gst_byte_reader_get_size:
209 * @reader: a #GstByteReader instance
211 * Returns the total number of bytes of a #GstByteReader instance.
213 * Returns: The total number of bytes of @reader instance.
218 gst_byte_reader_get_size (const GstByteReader * reader)
220 return _gst_byte_reader_get_size_inline (reader);
223 #define gst_byte_reader_get_remaining _gst_byte_reader_get_remaining_inline
224 #define gst_byte_reader_get_size _gst_byte_reader_get_size_inline
227 * gst_byte_reader_skip:
228 * @reader: a #GstByteReader instance
229 * @nbytes: the number of bytes to skip
231 * Skips @nbytes bytes of the #GstByteReader instance.
233 * Returns: %TRUE if @nbytes bytes could be skipped, %FALSE otherwise.
238 gst_byte_reader_skip (GstByteReader * reader, guint nbytes)
240 return _gst_byte_reader_skip_inline (reader, nbytes);
244 * gst_byte_reader_get_uint8:
245 * @reader: a #GstByteReader instance
246 * @val: (out): Pointer to a #guint8 to store the result
248 * Read an unsigned 8 bit integer into @val and update the current position.
250 * Returns: %TRUE if successful, %FALSE otherwise.
256 * gst_byte_reader_get_int8:
257 * @reader: a #GstByteReader instance
258 * @val: (out): Pointer to a #gint8 to store the result
260 * Read a signed 8 bit integer into @val and update the current position.
262 * Returns: %TRUE if successful, %FALSE otherwise.
268 * gst_byte_reader_peek_uint8:
269 * @reader: a #GstByteReader instance
270 * @val: (out): Pointer to a #guint8 to store the result
272 * Read an unsigned 8 bit integer into @val but keep the current position.
274 * Returns: %TRUE if successful, %FALSE otherwise.
280 * gst_byte_reader_peek_int8:
281 * @reader: a #GstByteReader instance
282 * @val: (out): Pointer to a #gint8 to store the result
284 * Read a signed 8 bit integer into @val but keep the current position.
286 * Returns: %TRUE if successful, %FALSE otherwise.
292 * gst_byte_reader_get_uint16_le:
293 * @reader: a #GstByteReader instance
294 * @val: (out): Pointer to a #guint16 to store the result
296 * Read an unsigned 16 bit little endian integer into @val
297 * and update the current position.
299 * Returns: %TRUE if successful, %FALSE otherwise.
305 * gst_byte_reader_get_int16_le:
306 * @reader: a #GstByteReader instance
307 * @val: (out): Pointer to a #gint16 to store the result
309 * Read a signed 16 bit little endian integer into @val
310 * and update the current position.
312 * Returns: %TRUE if successful, %FALSE otherwise.
318 * gst_byte_reader_peek_uint16_le:
319 * @reader: a #GstByteReader instance
320 * @val: (out): Pointer to a #guint16 to store the result
322 * Read an unsigned 16 bit little endian integer into @val
323 * but keep the current position.
325 * Returns: %TRUE if successful, %FALSE otherwise.
331 * gst_byte_reader_peek_int16_le:
332 * @reader: a #GstByteReader instance
333 * @val: (out): Pointer to a #gint16 to store the result
335 * Read a signed 16 bit little endian integer into @val
336 * but keep the current position.
338 * Returns: %TRUE if successful, %FALSE otherwise.
344 * gst_byte_reader_get_uint16_be:
345 * @reader: a #GstByteReader instance
346 * @val: (out): Pointer to a #guint16 to store the result
348 * Read an unsigned 16 bit big endian integer into @val
349 * and update the current position.
351 * Returns: %TRUE if successful, %FALSE otherwise.
357 * gst_byte_reader_get_int16_be:
358 * @reader: a #GstByteReader instance
359 * @val: (out): Pointer to a #gint16 to store the result
361 * Read a signed 16 bit big endian integer into @val
362 * and update the current position.
364 * Returns: %TRUE if successful, %FALSE otherwise.
370 * gst_byte_reader_peek_uint16_be:
371 * @reader: a #GstByteReader instance
372 * @val: (out): Pointer to a #guint16 to store the result
374 * Read an unsigned 16 bit big endian integer into @val
375 * but keep the current position.
377 * Returns: %TRUE if successful, %FALSE otherwise.
383 * gst_byte_reader_peek_int16_be:
384 * @reader: a #GstByteReader instance
385 * @val: (out): Pointer to a #gint16 to store the result
387 * Read a signed 16 bit big endian integer into @val
388 * but keep the current position.
390 * Returns: %TRUE if successful, %FALSE otherwise.
396 * gst_byte_reader_get_uint24_le:
397 * @reader: a #GstByteReader instance
398 * @val: (out): Pointer to a #guint32 to store the result
400 * Read an unsigned 24 bit little endian integer into @val
401 * and update the current position.
403 * Returns: %TRUE if successful, %FALSE otherwise.
409 * gst_byte_reader_get_int24_le:
410 * @reader: a #GstByteReader instance
411 * @val: (out): Pointer to a #gint32 to store the result
413 * Read a signed 24 bit little endian integer into @val
414 * and update the current position.
416 * Returns: %TRUE if successful, %FALSE otherwise.
422 * gst_byte_reader_peek_uint24_le:
423 * @reader: a #GstByteReader instance
424 * @val: (out): Pointer to a #guint32 to store the result
426 * Read an unsigned 24 bit little endian integer into @val
427 * but keep the current position.
429 * Returns: %TRUE if successful, %FALSE otherwise.
435 * gst_byte_reader_peek_int24_le:
436 * @reader: a #GstByteReader instance
437 * @val: (out): Pointer to a #gint32 to store the result
439 * Read a signed 24 bit little endian integer into @val
440 * but keep the current position.
442 * Returns: %TRUE if successful, %FALSE otherwise.
448 * gst_byte_reader_get_uint24_be:
449 * @reader: a #GstByteReader instance
450 * @val: (out): Pointer to a #guint32 to store the result
452 * Read an unsigned 24 bit big endian integer into @val
453 * and update the current position.
455 * Returns: %TRUE if successful, %FALSE otherwise.
461 * gst_byte_reader_get_int24_be:
462 * @reader: a #GstByteReader instance
463 * @val: (out): Pointer to a #gint32 to store the result
465 * Read a signed 24 bit big endian integer into @val
466 * and update the current position.
468 * Returns: %TRUE if successful, %FALSE otherwise.
474 * gst_byte_reader_peek_uint24_be:
475 * @reader: a #GstByteReader instance
476 * @val: (out): Pointer to a #guint32 to store the result
478 * Read an unsigned 24 bit big endian integer into @val
479 * but keep the current position.
481 * Returns: %TRUE if successful, %FALSE otherwise.
487 * gst_byte_reader_peek_int24_be:
488 * @reader: a #GstByteReader instance
489 * @val: (out): Pointer to a #gint32 to store the result
491 * Read a signed 24 bit big endian integer into @val
492 * but keep the current position.
494 * Returns: %TRUE if successful, %FALSE otherwise.
501 * gst_byte_reader_get_uint32_le:
502 * @reader: a #GstByteReader instance
503 * @val: (out): Pointer to a #guint32 to store the result
505 * Read an unsigned 32 bit little endian integer into @val
506 * and update the current position.
508 * Returns: %TRUE if successful, %FALSE otherwise.
514 * gst_byte_reader_get_int32_le:
515 * @reader: a #GstByteReader instance
516 * @val: (out): Pointer to a #gint32 to store the result
518 * Read a signed 32 bit little endian integer into @val
519 * and update the current position.
521 * Returns: %TRUE if successful, %FALSE otherwise.
527 * gst_byte_reader_peek_uint32_le:
528 * @reader: a #GstByteReader instance
529 * @val: (out): Pointer to a #guint32 to store the result
531 * Read an unsigned 32 bit little endian integer into @val
532 * but keep the current position.
534 * Returns: %TRUE if successful, %FALSE otherwise.
540 * gst_byte_reader_peek_int32_le:
541 * @reader: a #GstByteReader instance
542 * @val: (out): Pointer to a #gint32 to store the result
544 * Read a signed 32 bit little endian integer into @val
545 * but keep the current position.
547 * Returns: %TRUE if successful, %FALSE otherwise.
553 * gst_byte_reader_get_uint32_be:
554 * @reader: a #GstByteReader instance
555 * @val: (out): Pointer to a #guint32 to store the result
557 * Read an unsigned 32 bit big endian integer into @val
558 * and update the current position.
560 * Returns: %TRUE if successful, %FALSE otherwise.
566 * gst_byte_reader_get_int32_be:
567 * @reader: a #GstByteReader instance
568 * @val: (out): Pointer to a #gint32 to store the result
570 * Read a signed 32 bit big endian integer into @val
571 * and update the current position.
573 * Returns: %TRUE if successful, %FALSE otherwise.
579 * gst_byte_reader_peek_uint32_be:
580 * @reader: a #GstByteReader instance
581 * @val: (out): Pointer to a #guint32 to store the result
583 * Read an unsigned 32 bit big endian integer into @val
584 * but keep the current position.
586 * Returns: %TRUE if successful, %FALSE otherwise.
592 * gst_byte_reader_peek_int32_be:
593 * @reader: a #GstByteReader instance
594 * @val: (out): Pointer to a #gint32 to store the result
596 * Read a signed 32 bit big endian integer into @val
597 * but keep the current position.
599 * Returns: %TRUE if successful, %FALSE otherwise.
605 * gst_byte_reader_get_uint64_le:
606 * @reader: a #GstByteReader instance
607 * @val: (out): Pointer to a #guint64 to store the result
609 * Read an unsigned 64 bit little endian integer into @val
610 * and update the current position.
612 * Returns: %TRUE if successful, %FALSE otherwise.
618 * gst_byte_reader_get_int64_le:
619 * @reader: a #GstByteReader instance
620 * @val: (out): Pointer to a #gint64 to store the result
622 * Read a signed 64 bit little endian integer into @val
623 * and update the current position.
625 * Returns: %TRUE if successful, %FALSE otherwise.
631 * gst_byte_reader_peek_uint64_le:
632 * @reader: a #GstByteReader instance
633 * @val: (out): Pointer to a #guint64 to store the result
635 * Read an unsigned 64 bit little endian integer into @val
636 * but keep the current position.
638 * Returns: %TRUE if successful, %FALSE otherwise.
644 * gst_byte_reader_peek_int64_le:
645 * @reader: a #GstByteReader instance
646 * @val: (out): Pointer to a #gint64 to store the result
648 * Read a signed 64 bit little endian integer into @val
649 * but keep the current position.
651 * Returns: %TRUE if successful, %FALSE otherwise.
657 * gst_byte_reader_get_uint64_be:
658 * @reader: a #GstByteReader instance
659 * @val: (out): Pointer to a #guint64 to store the result
661 * Read an unsigned 64 bit big endian integer into @val
662 * and update the current position.
664 * Returns: %TRUE if successful, %FALSE otherwise.
670 * gst_byte_reader_get_int64_be:
671 * @reader: a #GstByteReader instance
672 * @val: (out): Pointer to a #gint64 to store the result
674 * Read a signed 64 bit big endian integer into @val
675 * and update the current position.
677 * Returns: %TRUE if successful, %FALSE otherwise.
683 * gst_byte_reader_peek_uint64_be:
684 * @reader: a #GstByteReader instance
685 * @val: (out): Pointer to a #guint64 to store the result
687 * Read an unsigned 64 bit big endian integer into @val
688 * but keep the current position.
690 * Returns: %TRUE if successful, %FALSE otherwise.
696 * gst_byte_reader_peek_int64_be:
697 * @reader: a #GstByteReader instance
698 * @val: (out): Pointer to a #gint64 to store the result
700 * Read a signed 64 bit big endian integer into @val
701 * but keep the current position.
703 * Returns: %TRUE if successful, %FALSE otherwise.
708 #define GST_BYTE_READER_PEEK_GET(bits,type,name) \
710 gst_byte_reader_get_##name (GstByteReader * reader, type * val) \
712 return _gst_byte_reader_get_##name##_inline (reader, val); \
716 gst_byte_reader_peek_##name (const GstByteReader * reader, type * val) \
718 return _gst_byte_reader_peek_##name##_inline (reader, val); \
723 GST_BYTE_READER_PEEK_GET(8,guint8,uint8)
724 GST_BYTE_READER_PEEK_GET(8,gint8,int8)
726 GST_BYTE_READER_PEEK_GET(16,guint16,uint16_le)
727 GST_BYTE_READER_PEEK_GET(16,guint16,uint16_be)
728 GST_BYTE_READER_PEEK_GET(16,gint16,int16_le)
729 GST_BYTE_READER_PEEK_GET(16,gint16,int16_be)
731 GST_BYTE_READER_PEEK_GET(24,guint32,uint24_le)
732 GST_BYTE_READER_PEEK_GET(24,guint32,uint24_be)
733 GST_BYTE_READER_PEEK_GET(24,gint32,int24_le)
734 GST_BYTE_READER_PEEK_GET(24,gint32,int24_be)
736 GST_BYTE_READER_PEEK_GET(32,guint32,uint32_le)
737 GST_BYTE_READER_PEEK_GET(32,guint32,uint32_be)
738 GST_BYTE_READER_PEEK_GET(32,gint32,int32_le)
739 GST_BYTE_READER_PEEK_GET(32,gint32,int32_be)
741 GST_BYTE_READER_PEEK_GET(64,guint64,uint64_le)
742 GST_BYTE_READER_PEEK_GET(64,guint64,uint64_be)
743 GST_BYTE_READER_PEEK_GET(64,gint64,int64_le)
744 GST_BYTE_READER_PEEK_GET(64,gint64,int64_be)
747 * gst_byte_reader_get_float32_le:
748 * @reader: a #GstByteReader instance
749 * @val: (out): Pointer to a #gfloat to store the result
751 * Read a 32 bit little endian floating point value into @val
752 * and update the current position.
754 * Returns: %TRUE if successful, %FALSE otherwise.
760 * gst_byte_reader_peek_float32_le:
761 * @reader: a #GstByteReader instance
762 * @val: (out): Pointer to a #gfloat to store the result
764 * Read a 32 bit little endian floating point value into @val
765 * but keep the current position.
767 * Returns: %TRUE if successful, %FALSE otherwise.
773 * gst_byte_reader_get_float32_be:
774 * @reader: a #GstByteReader instance
775 * @val: (out): Pointer to a #gfloat to store the result
777 * Read a 32 bit big endian floating point value into @val
778 * and update the current position.
780 * Returns: %TRUE if successful, %FALSE otherwise.
786 * gst_byte_reader_peek_float32_be:
787 * @reader: a #GstByteReader instance
788 * @val: (out): Pointer to a #gfloat to store the result
790 * Read a 32 bit big endian floating point value into @val
791 * but keep the current position.
793 * Returns: %TRUE if successful, %FALSE otherwise.
799 * gst_byte_reader_get_float64_le:
800 * @reader: a #GstByteReader instance
801 * @val: (out): Pointer to a #gdouble to store the result
803 * Read a 64 bit little endian floating point value into @val
804 * and update the current position.
806 * Returns: %TRUE if successful, %FALSE otherwise.
812 * gst_byte_reader_peek_float64_le:
813 * @reader: a #GstByteReader instance
814 * @val: (out): Pointer to a #gdouble to store the result
816 * Read a 64 bit little endian floating point value into @val
817 * but keep the current position.
819 * Returns: %TRUE if successful, %FALSE otherwise.
825 * gst_byte_reader_get_float64_be:
826 * @reader: a #GstByteReader instance
827 * @val: (out): Pointer to a #gdouble to store the result
829 * Read a 64 bit big endian floating point value into @val
830 * and update the current position.
832 * Returns: %TRUE if successful, %FALSE otherwise.
838 * gst_byte_reader_peek_float64_be:
839 * @reader: a #GstByteReader instance
840 * @val: (out): Pointer to a #gdouble to store the result
842 * Read a 64 bit big endian floating point value into @val
843 * but keep the current position.
845 * Returns: %TRUE if successful, %FALSE otherwise.
850 GST_BYTE_READER_PEEK_GET(32,gfloat,float32_le)
851 GST_BYTE_READER_PEEK_GET(32,gfloat,float32_be)
852 GST_BYTE_READER_PEEK_GET(64,gdouble,float64_le)
853 GST_BYTE_READER_PEEK_GET(64,gdouble,float64_be)
858 * gst_byte_reader_get_data:
859 * @reader: a #GstByteReader instance
860 * @size: Size in bytes
861 * @val: (out) (transfer none) (array length=size): address of a
862 * #guint8 pointer variable in which to store the result
864 * Returns a constant pointer to the current data
865 * position if at least @size bytes are left and
866 * updates the current position.
869 * Returns: %TRUE if successful, %FALSE otherwise.
874 gst_byte_reader_get_data (GstByteReader * reader, guint size,
877 return _gst_byte_reader_get_data_inline (reader, size, val);
881 * gst_byte_reader_peek_data:
882 * @reader: a #GstByteReader instance
883 * @size: Size in bytes
884 * @val: (out) (transfer none) (array length=size): address of a
885 * #guint8 pointer variable in which to store the result
887 * Returns a constant pointer to the current data
888 * position if at least @size bytes are left and
889 * keeps the current position.
892 * Returns: %TRUE if successful, %FALSE otherwise.
897 gst_byte_reader_peek_data (const GstByteReader * reader, guint size,
900 return _gst_byte_reader_peek_data_inline (reader, size, val);
904 * gst_byte_reader_dup_data:
905 * @reader: a #GstByteReader instance
906 * @size: Size in bytes
907 * @val: (out) (transfer full) (array length=size): address of a
908 * #guint8 pointer variable in which to store the result
910 * Free-function: g_free
912 * Returns a newly-allocated copy of the current data
913 * position if at least @size bytes are left and
914 * updates the current position. Free with g_free() when no longer needed.
916 * Returns: %TRUE if successful, %FALSE otherwise.
921 gst_byte_reader_dup_data (GstByteReader * reader, guint size, guint8 ** val)
923 return _gst_byte_reader_dup_data_inline (reader, size, val);
927 * gst_byte_reader_masked_scan_uint32:
928 * @reader: a #GstByteReader
929 * @mask: mask to apply to data before matching against @pattern
930 * @pattern: pattern to match (after mask is applied)
931 * @offset: offset from which to start scanning, relative to the current
933 * @size: number of bytes to scan from offset
935 * Scan for pattern @pattern with applied mask @mask in the byte reader data,
936 * starting from offset @offset relative to the current position.
938 * The bytes in @pattern and @mask are interpreted left-to-right, regardless
939 * of endianness. All four bytes of the pattern must be present in the
940 * byte reader data for it to match, even if the first or last bytes are masked
943 * It is an error to call this function without making sure that there is
944 * enough data (offset+size bytes) in the byte reader.
946 * Returns: offset of the first match, or -1 if no match was found.
950 * // Assume the reader contains 0x00 0x01 0x02 ... 0xfe 0xff
952 * gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0x00010203, 0, 256);
954 * gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0x00010203, 1, 255);
956 * gst_byte_reader_masked_scan_uint32 (reader, 0xffffffff, 0x01020304, 1, 255);
958 * gst_byte_reader_masked_scan_uint32 (reader, 0xffff, 0x0001, 0, 256);
960 * gst_byte_reader_masked_scan_uint32 (reader, 0xffff, 0x0203, 0, 256);
962 * gst_byte_reader_masked_scan_uint32 (reader, 0xffff0000, 0x02030000, 0, 256);
964 * gst_byte_reader_masked_scan_uint32 (reader, 0xffff0000, 0x02030000, 0, 4);
971 gst_byte_reader_masked_scan_uint32 (const GstByteReader * reader, guint32 mask,
972 guint32 pattern, guint offset, guint size)
978 g_return_val_if_fail (size > 0, -1);
979 g_return_val_if_fail ((guint64) offset + size <= reader->size - reader->byte,
982 /* we can't find the pattern with less than 4 bytes */
983 if (G_UNLIKELY (size < 4))
986 data = reader->data + reader->byte + offset;
988 /* set the state to something that does not match */
992 for (i = 0; i < size; i++) {
993 /* throw away one byte and move in the next byte */
994 state = ((state << 8) | data[i]);
995 if (G_UNLIKELY ((state & mask) == pattern)) {
996 /* we have a match but we need to have skipped at
997 * least 4 bytes to fill the state. */
998 if (G_LIKELY (i >= 3))
999 return offset + i - 3;
1007 #define GST_BYTE_READER_SCAN_STRING(bits) \
1009 gst_byte_reader_scan_string_utf##bits (const GstByteReader * reader) \
1011 guint len, off, max_len; \
1013 max_len = (reader->size - reader->byte) / sizeof (guint##bits); \
1015 /* need at least a single NUL terminator */ \
1020 off = reader->byte; \
1021 /* endianness does not matter if we are looking for a NUL terminator */ \
1022 while (GST_READ_UINT##bits##_LE (&reader->data[off]) != 0) { \
1024 off += sizeof (guint##bits); \
1025 /* have we reached the end without finding a NUL terminator? */ \
1026 if (len == max_len) \
1029 /* return size in bytes including the NUL terminator (hence the +1) */ \
1030 return (len + 1) * sizeof (guint##bits); \
1033 #define GST_READ_UINT8_LE GST_READ_UINT8
1034 GST_BYTE_READER_SCAN_STRING (8);
1035 #undef GST_READ_UINT8_LE
1036 GST_BYTE_READER_SCAN_STRING (16);
1037 GST_BYTE_READER_SCAN_STRING (32);
1039 #define GST_BYTE_READER_SKIP_STRING(bits) \
1041 gst_byte_reader_skip_string_utf##bits (GstByteReader * reader) \
1043 guint size; /* size in bytes including the terminator */ \
1045 g_return_val_if_fail (reader != NULL, FALSE); \
1047 size = gst_byte_reader_scan_string_utf##bits (reader); \
1048 reader->byte += size; \
1049 return (size > 0); \
1053 * gst_byte_reader_skip_string:
1054 * @reader: a #GstByteReader instance
1056 * Skips a NUL-terminated string in the #GstByteReader instance, advancing
1057 * the current position to the byte after the string. This will work for
1058 * any NUL-terminated string with a character width of 8 bits, so ASCII,
1059 * UTF-8, ISO-8859-N etc.
1061 * This function will fail if no NUL-terminator was found in in the data.
1063 * Returns: %TRUE if a string could be skipped, %FALSE otherwise.
1068 * gst_byte_reader_skip_string_utf8:
1069 * @reader: a #GstByteReader instance
1071 * Skips a NUL-terminated string in the #GstByteReader instance, advancing
1072 * the current position to the byte after the string. This will work for
1073 * any NUL-terminated string with a character width of 8 bits, so ASCII,
1074 * UTF-8, ISO-8859-N etc. No input checking for valid UTF-8 is done.
1076 * This function will fail if no NUL-terminator was found in in the data.
1078 * Returns: %TRUE if a string could be skipped, %FALSE otherwise.
1082 GST_BYTE_READER_SKIP_STRING (8);
1085 * gst_byte_reader_skip_string_utf16:
1086 * @reader: a #GstByteReader instance
1088 * Skips a NUL-terminated UTF-16 string in the #GstByteReader instance,
1089 * advancing the current position to the byte after the string.
1091 * No input checking for valid UTF-16 is done.
1093 * This function will fail if no NUL-terminator was found in in the data.
1095 * Returns: %TRUE if a string could be skipped, %FALSE otherwise.
1099 GST_BYTE_READER_SKIP_STRING (16);
1102 * gst_byte_reader_skip_string_utf32:
1103 * @reader: a #GstByteReader instance
1105 * Skips a NUL-terminated UTF-32 string in the #GstByteReader instance,
1106 * advancing the current position to the byte after the string.
1108 * No input checking for valid UTF-32 is done.
1110 * This function will fail if no NUL-terminator was found in in the data.
1112 * Returns: %TRUE if a string could be skipped, %FALSE otherwise.
1116 GST_BYTE_READER_SKIP_STRING (32);
1119 * gst_byte_reader_peek_string:
1120 * @reader: a #GstByteReader instance
1121 * @str: (out) (transfer none) (array zero-terminated=1): address of a
1122 * #gchar pointer varieble in which to store the result
1124 * Returns a constant pointer to the current data position if there is
1125 * a NUL-terminated string in the data (this could be just a NUL terminator).
1126 * The current position will be maintained. This will work for any
1127 * NUL-terminated string with a character width of 8 bits, so ASCII,
1128 * UTF-8, ISO-8859-N etc.
1130 * This function will fail if no NUL-terminator was found in in the data.
1132 * Returns: %TRUE if a string could be skipped, %FALSE otherwise.
1137 * gst_byte_reader_peek_string_utf8:
1138 * @reader: a #GstByteReader instance
1139 * @str: (out) (transfer none) (array zero-terminated=1): address of a
1140 * #gchar pointer varieble in which to store the result
1142 * Returns a constant pointer to the current data position if there is
1143 * a NUL-terminated string in the data (this could be just a NUL terminator).
1144 * The current position will be maintained. This will work for any
1145 * NUL-terminated string with a character width of 8 bits, so ASCII,
1146 * UTF-8, ISO-8859-N etc.
1148 * No input checking for valid UTF-8 is done.
1150 * This function will fail if no NUL-terminator was found in in the data.
1152 * Returns: %TRUE if a string could be skipped, %FALSE otherwise.
1157 gst_byte_reader_peek_string_utf8 (const GstByteReader * reader,
1160 g_return_val_if_fail (reader != NULL, FALSE);
1161 g_return_val_if_fail (str != NULL, FALSE);
1163 if (gst_byte_reader_scan_string_utf8 (reader) > 0) {
1164 *str = (const gchar *) (reader->data + reader->byte);
1168 return (*str != NULL);
1172 * gst_byte_reader_get_string_utf8:
1173 * @reader: a #GstByteReader instance
1174 * @str: (out) (transfer none) (array zero-terminated=1): address of a
1175 * #gchar pointer varieble in which to store the result
1177 * Returns a constant pointer to the current data position if there is
1178 * a NUL-terminated string in the data (this could be just a NUL terminator),
1179 * advancing the current position to the byte after the string. This will work
1180 * for any NUL-terminated string with a character width of 8 bits, so ASCII,
1181 * UTF-8, ISO-8859-N etc.
1183 * No input checking for valid UTF-8 is done.
1185 * This function will fail if no NUL-terminator was found in in the data.
1187 * Returns: %TRUE if a string could be found, %FALSE otherwise.
1192 gst_byte_reader_get_string_utf8 (GstByteReader * reader, const gchar ** str)
1194 guint size; /* size in bytes including the terminator */
1196 g_return_val_if_fail (reader != NULL, FALSE);
1197 g_return_val_if_fail (str != NULL, FALSE);
1199 size = gst_byte_reader_scan_string_utf8 (reader);
1205 *str = (const gchar *) (reader->data + reader->byte);
1206 reader->byte += size;
1210 #define GST_BYTE_READER_DUP_STRING(bits,type) \
1212 gst_byte_reader_dup_string_utf##bits (GstByteReader * reader, type ** str) \
1214 guint size; /* size in bytes including the terminator */ \
1216 g_return_val_if_fail (reader != NULL, FALSE); \
1217 g_return_val_if_fail (str != NULL, FALSE); \
1219 size = gst_byte_reader_scan_string_utf##bits (reader); \
1224 *str = g_memdup (reader->data + reader->byte, size); \
1225 reader->byte += size; \
1230 * gst_byte_reader_dup_string_utf8:
1231 * @reader: a #GstByteReader instance
1232 * @str: (out) (transfer full) (array zero-terminated=1): address of a
1233 * #gchar pointer varieble in which to store the result
1235 * Free-function: g_free
1237 * FIXME:Reads (copies) a NUL-terminated string in the #GstByteReader instance,
1238 * advancing the current position to the byte after the string. This will work
1239 * for any NUL-terminated string with a character width of 8 bits, so ASCII,
1240 * UTF-8, ISO-8859-N etc. No input checking for valid UTF-8 is done.
1242 * This function will fail if no NUL-terminator was found in in the data.
1244 * Returns: %TRUE if a string could be read into @str, %FALSE otherwise. The
1245 * string put into @str must be freed with g_free() when no longer needed.
1249 GST_BYTE_READER_DUP_STRING (8, gchar);
1252 * gst_byte_reader_dup_string_utf16:
1253 * @reader: a #GstByteReader instance
1254 * @str: (out) (transfer full) (array zero-terminated=1): address of a
1255 * #guint16 pointer varieble in which to store the result
1257 * Free-function: g_free
1259 * Returns a newly-allocated copy of the current data position if there is
1260 * a NUL-terminated UTF-16 string in the data (this could be an empty string
1261 * as well), and advances the current position.
1263 * No input checking for valid UTF-16 is done. This function is endianness
1264 * agnostic - you should not assume the UTF-16 characters are in host
1267 * This function will fail if no NUL-terminator was found in in the data.
1269 * Note: there is no peek or get variant of this function to ensure correct
1270 * byte alignment of the UTF-16 string.
1272 * Returns: %TRUE if a string could be read, %FALSE otherwise. The
1273 * string put into @str must be freed with g_free() when no longer needed.
1277 GST_BYTE_READER_DUP_STRING (16, guint16);
1280 * gst_byte_reader_dup_string_utf32:
1281 * @reader: a #GstByteReader instance
1282 * @str: (out) (transfer full) (array zero-terminated=1): address of a
1283 * #guint32 pointer varieble in which to store the result
1285 * Free-function: g_free
1287 * Returns a newly-allocated copy of the current data position if there is
1288 * a NUL-terminated UTF-32 string in the data (this could be an empty string
1289 * as well), and advances the current position.
1291 * No input checking for valid UTF-32 is done. This function is endianness
1292 * agnostic - you should not assume the UTF-32 characters are in host
1295 * This function will fail if no NUL-terminator was found in in the data.
1297 * Note: there is no peek or get variant of this function to ensure correct
1298 * byte alignment of the UTF-32 string.
1300 * Returns: %TRUE if a string could be read, %FALSE otherwise. The
1301 * string put into @str must be freed with g_free() when no longer needed.
1305 GST_BYTE_READER_DUP_STRING (32, guint32);