1 /* GStreamer byte writer
3 * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
25 #define GST_BYTE_WRITER_DISABLE_INLINES
26 #include "gstbytewriter.h"
29 * SECTION:gstbytewriter
30 * @short_description: Writes different integer, string and floating point
31 * types to a memory buffer and allows reading
33 * #GstByteWriter provides a byte writer and reader that can write/read different
34 * integer and floating point types to/from a memory buffer. It provides functions
35 * for writing/reading signed/unsigned, little/big endian integers of 8, 16, 24,
36 * 32 and 64 bits and functions for reading little/big endian floating points numbers of
37 * 32 and 64 bits. It also provides functions to write/read NUL-terminated strings
38 * in various character encodings.
42 * gst_byte_writer_new:
44 * Creates a new, empty #GstByteWriter instance
46 * Free-function: gst_byte_writer_free
48 * Returns: (transfer full): a new, empty #GstByteWriter instance
53 gst_byte_writer_new (void)
55 GstByteWriter *ret = g_slice_new0 (GstByteWriter);
62 * gst_byte_writer_new_with_size:
63 * @size: Initial size of data
64 * @fixed: If %TRUE the data can't be reallocated
66 * Creates a new #GstByteWriter instance with the given
69 * Free-function: gst_byte_writer_free
71 * Returns: (transfer full): a new #GstByteWriter instance
76 gst_byte_writer_new_with_size (guint size, gboolean fixed)
78 GstByteWriter *ret = gst_byte_writer_new ();
80 ret->alloc_size = size;
81 ret->parent.data = g_malloc (ret->alloc_size);
89 * gst_byte_writer_new_with_data:
90 * @data: Memory area for writing
91 * @size: Size of @data in bytes
92 * @initialized: If %TRUE the complete data can be read from the beginning
94 * Creates a new #GstByteWriter instance with the given
95 * memory area. If @initialized is %TRUE it is possible to
96 * read @size bytes from the #GstByteWriter from the beginning.
98 * Free-function: gst_byte_writer_free
100 * Returns: (transfer full): a new #GstByteWriter instance
105 gst_byte_writer_new_with_data (guint8 * data, guint size, gboolean initialized)
107 GstByteWriter *ret = gst_byte_writer_new ();
109 ret->parent.data = data;
110 ret->parent.size = (initialized) ? size : 0;
111 ret->alloc_size = size;
119 * gst_byte_writer_init:
120 * @writer: #GstByteWriter instance
122 * Initializes @writer to an empty instance
127 gst_byte_writer_init (GstByteWriter * writer)
129 g_return_if_fail (writer != NULL);
131 memset (writer, 0, sizeof (GstByteWriter));
133 writer->owned = TRUE;
137 * gst_byte_writer_init_with_size:
138 * @writer: #GstByteWriter instance
139 * @size: Initial size of data
140 * @fixed: If %TRUE the data can't be reallocated
142 * Initializes @writer with the given initial data size.
147 gst_byte_writer_init_with_size (GstByteWriter * writer, guint size,
150 g_return_if_fail (writer != NULL);
152 gst_byte_writer_init (writer);
154 writer->parent.data = g_malloc (size);
155 writer->alloc_size = size;
156 writer->fixed = fixed;
157 writer->owned = TRUE;
161 * gst_byte_writer_init_with_data:
162 * @writer: #GstByteWriter instance
163 * @data: (in callee-allocated) (array length=size) (transfer none): Memory
165 * @size: Size of @data in bytes
166 * @initialized: If %TRUE the complete data can be read from the beginning
168 * Initializes @writer with the given
169 * memory area. If @initialized is %TRUE it is possible to
170 * read @size bytes from the #GstByteWriter from the beginning.
175 gst_byte_writer_init_with_data (GstByteWriter * writer, guint8 * data,
176 guint size, gboolean initialized)
178 g_return_if_fail (writer != NULL);
180 gst_byte_writer_init (writer);
182 writer->parent.data = data;
183 writer->parent.size = (initialized) ? size : 0;
184 writer->alloc_size = size;
185 writer->fixed = TRUE;
186 writer->owned = FALSE;
190 * gst_byte_writer_reset:
191 * @writer: #GstByteWriter instance
193 * Resets @writer and frees the data if it's
199 gst_byte_writer_reset (GstByteWriter * writer)
201 g_return_if_fail (writer != NULL);
204 g_free ((guint8 *) writer->parent.data);
205 memset (writer, 0, sizeof (GstByteWriter));
209 * gst_byte_writer_reset_and_get_data:
210 * @writer: #GstByteWriter instance
212 * Resets @writer and returns the current data.
214 * Free-function: g_free
216 * Returns: (transfer full): the current data. g_free() after usage.
221 gst_byte_writer_reset_and_get_data (GstByteWriter * writer)
225 g_return_val_if_fail (writer != NULL, NULL);
227 data = (guint8 *) writer->parent.data;
229 data = g_memdup (data, writer->parent.size);
230 writer->parent.data = NULL;
231 gst_byte_writer_reset (writer);
237 * gst_byte_writer_reset_and_get_buffer:
238 * @writer: #GstByteWriter instance
240 * Resets @writer and returns the current data as buffer.
242 * Free-function: gst_buffer_unref
244 * Returns: (transfer full): the current data as buffer. gst_buffer_unref()
250 gst_byte_writer_reset_and_get_buffer (GstByteWriter * writer)
256 g_return_val_if_fail (writer != NULL, NULL);
258 size = writer->parent.size;
259 data = gst_byte_writer_reset_and_get_data (writer);
261 buffer = gst_buffer_new ();
263 gst_buffer_append_memory (buffer,
264 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
271 * gst_byte_writer_free:
272 * @writer: (in) (transfer full): #GstByteWriter instance
274 * Frees @writer and all memory allocated by it.
279 gst_byte_writer_free (GstByteWriter * writer)
281 g_return_if_fail (writer != NULL);
283 gst_byte_writer_reset (writer);
284 g_slice_free (GstByteWriter, writer);
288 * gst_byte_writer_free_and_get_data:
289 * @writer: (in) (transfer full): #GstByteWriter instance
291 * Frees @writer and all memory allocated by it except
292 * the current data, which is returned.
294 * Free-function: g_free
296 * Returns: (transfer full): the current data. g_free() after usage.
301 gst_byte_writer_free_and_get_data (GstByteWriter * writer)
305 g_return_val_if_fail (writer != NULL, NULL);
307 data = gst_byte_writer_reset_and_get_data (writer);
308 g_slice_free (GstByteWriter, writer);
314 * gst_byte_writer_free_and_get_buffer:
315 * @writer: (in) (transfer full): #GstByteWriter instance
317 * Frees @writer and all memory allocated by it except
318 * the current data, which is returned as #GstBuffer.
320 * Free-function: gst_buffer_unref
322 * Returns: (transfer full): the current data as buffer. gst_buffer_unref()
328 gst_byte_writer_free_and_get_buffer (GstByteWriter * writer)
332 g_return_val_if_fail (writer != NULL, NULL);
334 buffer = gst_byte_writer_reset_and_get_buffer (writer);
335 g_slice_free (GstByteWriter, writer);
341 * gst_byte_writer_get_remaining:
342 * @writer: #GstByteWriter instance
344 * Returns the remaining size of data that can still be written. If
345 * -1 is returned the remaining size is only limited by system resources.
347 * Returns: the remaining size of data that can still be written
352 gst_byte_writer_get_remaining (const GstByteWriter * writer)
354 g_return_val_if_fail (writer != NULL, -1);
359 return writer->alloc_size - writer->parent.byte;
363 * gst_byte_writer_ensure_free_space:
364 * @writer: #GstByteWriter instance
365 * @size: Number of bytes that should be available
367 * Checks if enough free space from the current write cursor is
368 * available and reallocates if necessary.
370 * Returns: %TRUE if at least @size bytes are still available
375 gst_byte_writer_ensure_free_space (GstByteWriter * writer, guint size)
377 return _gst_byte_writer_ensure_free_space_inline (writer, size);
381 #define CREATE_WRITE_FUNC(bits,type,name,write_func) \
383 gst_byte_writer_put_##name (GstByteWriter *writer, type val) \
385 return _gst_byte_writer_put_##name##_inline (writer, val); \
388 CREATE_WRITE_FUNC (8, guint8, uint8, GST_WRITE_UINT8);
389 CREATE_WRITE_FUNC (8, gint8, int8, GST_WRITE_UINT8);
390 CREATE_WRITE_FUNC (16, guint16, uint16_le, GST_WRITE_UINT16_LE);
391 CREATE_WRITE_FUNC (16, guint16, uint16_be, GST_WRITE_UINT16_BE);
392 CREATE_WRITE_FUNC (16, gint16, int16_le, GST_WRITE_UINT16_LE);
393 CREATE_WRITE_FUNC (16, gint16, int16_be, GST_WRITE_UINT16_BE);
394 CREATE_WRITE_FUNC (24, guint32, uint24_le, GST_WRITE_UINT24_LE);
395 CREATE_WRITE_FUNC (24, guint32, uint24_be, GST_WRITE_UINT24_BE);
396 CREATE_WRITE_FUNC (24, gint32, int24_le, GST_WRITE_UINT24_LE);
397 CREATE_WRITE_FUNC (24, gint32, int24_be, GST_WRITE_UINT24_BE);
398 CREATE_WRITE_FUNC (32, guint32, uint32_le, GST_WRITE_UINT32_LE);
399 CREATE_WRITE_FUNC (32, guint32, uint32_be, GST_WRITE_UINT32_BE);
400 CREATE_WRITE_FUNC (32, gint32, int32_le, GST_WRITE_UINT32_LE);
401 CREATE_WRITE_FUNC (32, gint32, int32_be, GST_WRITE_UINT32_BE);
402 CREATE_WRITE_FUNC (64, guint64, uint64_le, GST_WRITE_UINT64_LE);
403 CREATE_WRITE_FUNC (64, guint64, uint64_be, GST_WRITE_UINT64_BE);
404 CREATE_WRITE_FUNC (64, gint64, int64_le, GST_WRITE_UINT64_LE);
405 CREATE_WRITE_FUNC (64, gint64, int64_be, GST_WRITE_UINT64_BE);
407 CREATE_WRITE_FUNC (32, gfloat, float32_be, GST_WRITE_FLOAT_BE);
408 CREATE_WRITE_FUNC (32, gfloat, float32_le, GST_WRITE_FLOAT_LE);
409 CREATE_WRITE_FUNC (64, gdouble, float64_be, GST_WRITE_DOUBLE_BE);
410 CREATE_WRITE_FUNC (64, gdouble, float64_le, GST_WRITE_DOUBLE_LE);
413 gst_byte_writer_put_data (GstByteWriter * writer, const guint8 * data,
416 return _gst_byte_writer_put_data_inline (writer, data, size);
420 gst_byte_writer_fill (GstByteWriter * writer, guint8 value, guint size)
422 return _gst_byte_writer_fill_inline (writer, value, size);
425 #define CREATE_WRITE_STRING_FUNC(bits,type) \
427 gst_byte_writer_put_string_utf##bits (GstByteWriter *writer, const type * data) \
431 g_return_val_if_fail (writer != NULL, FALSE); \
433 /* endianness does not matter if we are looking for a NUL terminator */ \
434 while (data[size] != 0) { \
435 /* have prevent overflow */ \
436 if (G_UNLIKELY (size == G_MAXUINT)) \
442 if (G_UNLIKELY (!_gst_byte_writer_ensure_free_space_inline(writer, size * (bits / 8)))) \
445 _gst_byte_writer_put_data_inline (writer, (const guint8 *) data, size * (bits / 8)); \
450 CREATE_WRITE_STRING_FUNC (8, gchar);
451 CREATE_WRITE_STRING_FUNC (16, guint16);
452 CREATE_WRITE_STRING_FUNC (32, guint32);
454 * gst_byte_writer_put_uint8:
455 * @writer: #GstByteWriter instance
456 * @val: Value to write
458 * Writes a unsigned 8 bit integer to @writer.
460 * Returns: %TRUE if the value could be written
465 * gst_byte_writer_put_uint16_be:
466 * @writer: #GstByteWriter instance
467 * @val: Value to write
469 * Writes a unsigned big endian 16 bit integer to @writer.
471 * Returns: %TRUE if the value could be written
476 * gst_byte_writer_put_uint24_be:
477 * @writer: #GstByteWriter instance
478 * @val: Value to write
480 * Writes a unsigned big endian 24 bit integer to @writer.
482 * Returns: %TRUE if the value could be written
487 * gst_byte_writer_put_uint32_be:
488 * @writer: #GstByteWriter instance
489 * @val: Value to write
491 * Writes a unsigned big endian 32 bit integer to @writer.
493 * Returns: %TRUE if the value could be written
498 * gst_byte_writer_put_uint64_be:
499 * @writer: #GstByteWriter instance
500 * @val: Value to write
502 * Writes a unsigned big endian 64 bit integer to @writer.
504 * Returns: %TRUE if the value could be written
509 * gst_byte_writer_put_uint16_le:
510 * @writer: #GstByteWriter instance
511 * @val: Value to write
513 * Writes a unsigned little endian 16 bit integer to @writer.
515 * Returns: %TRUE if the value could be written
520 * gst_byte_writer_put_uint24_le:
521 * @writer: #GstByteWriter instance
522 * @val: Value to write
524 * Writes a unsigned little endian 24 bit integer to @writer.
526 * Returns: %TRUE if the value could be written
531 * gst_byte_writer_put_uint32_le:
532 * @writer: #GstByteWriter instance
533 * @val: Value to write
535 * Writes a unsigned little endian 32 bit integer to @writer.
537 * Returns: %TRUE if the value could be written
542 * gst_byte_writer_put_uint64_le:
543 * @writer: #GstByteWriter instance
544 * @val: Value to write
546 * Writes a unsigned little endian 64 bit integer to @writer.
548 * Returns: %TRUE if the value could be written
553 * gst_byte_writer_put_int8:
554 * @writer: #GstByteWriter instance
555 * @val: Value to write
557 * Writes a signed 8 bit integer to @writer.
559 * Returns: %TRUE if the value could be written
564 * gst_byte_writer_put_int16_be:
565 * @writer: #GstByteWriter instance
566 * @val: Value to write
568 * Writes a signed big endian 16 bit integer to @writer.
570 * Returns: %TRUE if the value could be written
575 * gst_byte_writer_put_int24_be:
576 * @writer: #GstByteWriter instance
577 * @val: Value to write
579 * Writes a signed big endian 24 bit integer to @writer.
581 * Returns: %TRUE if the value could be written
586 * gst_byte_writer_put_int32_be:
587 * @writer: #GstByteWriter instance
588 * @val: Value to write
590 * Writes a signed big endian 32 bit integer to @writer.
592 * Returns: %TRUE if the value could be written
597 * gst_byte_writer_put_int64_be:
598 * @writer: #GstByteWriter instance
599 * @val: Value to write
601 * Writes a signed big endian 64 bit integer to @writer.
603 * Returns: %TRUE if the value could be written
608 * gst_byte_writer_put_int16_le:
609 * @writer: #GstByteWriter instance
610 * @val: Value to write
612 * Writes a signed little endian 16 bit integer to @writer.
614 * Returns: %TRUE if the value could be written
619 * gst_byte_writer_put_int24_le:
620 * @writer: #GstByteWriter instance
621 * @val: Value to write
623 * Writes a signed little endian 24 bit integer to @writer.
625 * Returns: %TRUE if the value could be written
630 * gst_byte_writer_put_int32_le:
631 * @writer: #GstByteWriter instance
632 * @val: Value to write
634 * Writes a signed little endian 32 bit integer to @writer.
636 * Returns: %TRUE if the value could be written
641 * gst_byte_writer_put_int64_le:
642 * @writer: #GstByteWriter instance
643 * @val: Value to write
645 * Writes a signed little endian 64 bit integer to @writer.
647 * Returns: %TRUE if the value could be written
652 * gst_byte_writer_put_float32_be:
653 * @writer: #GstByteWriter instance
654 * @val: Value to write
656 * Writes a big endian 32 bit float to @writer.
658 * Returns: %TRUE if the value could be written
663 * gst_byte_writer_put_float64_be:
664 * @writer: #GstByteWriter instance
665 * @val: Value to write
667 * Writes a big endian 64 bit float to @writer.
669 * Returns: %TRUE if the value could be written
674 * gst_byte_writer_put_float32_le:
675 * @writer: #GstByteWriter instance
676 * @val: Value to write
678 * Writes a little endian 32 bit float to @writer.
680 * Returns: %TRUE if the value could be written
685 * gst_byte_writer_put_float64_le:
686 * @writer: #GstByteWriter instance
687 * @val: Value to write
689 * Writes a little endian 64 bit float to @writer.
691 * Returns: %TRUE if the value could be written
696 * gst_byte_writer_put_string_utf8:
697 * @writer: #GstByteWriter instance
698 * @data: (transfer none) (array zero-terminated=1) (type utf8): UTF8 string to
701 * Writes a NUL-terminated UTF8 string to @writer (including the terminator).
703 * Returns: %TRUE if the value could be written
708 * gst_byte_writer_put_string_utf16:
709 * @writer: #GstByteWriter instance
710 * @data: (transfer none) (array zero-terminated=1): UTF16 string to write
712 * Writes a NUL-terminated UTF16 string to @writer (including the terminator).
714 * Returns: %TRUE if the value could be written
719 * gst_byte_writer_put_string_utf32:
720 * @writer: #GstByteWriter instance
721 * @data: (transfer none) (array zero-terminated=1): UTF32 string to write
723 * Writes a NUL-terminated UTF32 string to @writer (including the terminator).
725 * Returns: %TRUE if the value could be written
730 * gst_byte_writer_put_data:
731 * @writer: #GstByteWriter instance
732 * @data: (transfer none) (array length=size): Data to write
733 * @size: Size of @data in bytes
735 * Writes @size bytes of @data to @writer.
737 * Returns: %TRUE if the value could be written
742 * gst_byte_writer_fill:
743 * @writer: #GstByteWriter instance
744 * @value: Value to be writen
745 * @size: Number of bytes to be writen
747 * Writes @size bytes containing @value to @writer.
749 * Returns: %TRUE if the value could be written
755 * gst_byte_writer_put_buffer:
756 * @writer: #GstByteWriter instance
757 * @buffer: (transfer none): source #GstBuffer
758 * @offset: offset to copy from
759 * @size: total size to copy. If -1, all data is copied
761 * Writes @size bytes of @data to @writer.
763 * Returns: %TRUE if the data could be written