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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, 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
51 gst_byte_writer_new (void)
53 GstByteWriter *ret = g_slice_new0 (GstByteWriter);
60 * gst_byte_writer_new_with_size:
61 * @size: Initial size of data
62 * @fixed: If %TRUE the data can't be reallocated
64 * Creates a new #GstByteWriter instance with the given
67 * Free-function: gst_byte_writer_free
69 * Returns: (transfer full): a new #GstByteWriter instance
72 gst_byte_writer_new_with_size (guint size, gboolean fixed)
74 GstByteWriter *ret = gst_byte_writer_new ();
76 ret->alloc_size = size;
77 ret->parent.data = g_malloc (ret->alloc_size);
85 * gst_byte_writer_new_with_data:
86 * @data: Memory area for writing
87 * @size: Size of @data in bytes
88 * @initialized: If %TRUE the complete data can be read from the beginning
90 * Creates a new #GstByteWriter instance with the given
91 * memory area. If @initialized is %TRUE it is possible to
92 * read @size bytes from the #GstByteWriter from the beginning.
94 * Free-function: gst_byte_writer_free
96 * Returns: (transfer full): a new #GstByteWriter instance
99 gst_byte_writer_new_with_data (guint8 * data, guint size, gboolean initialized)
101 GstByteWriter *ret = gst_byte_writer_new ();
103 ret->parent.data = data;
104 ret->parent.size = (initialized) ? size : 0;
105 ret->alloc_size = size;
113 * gst_byte_writer_init:
114 * @writer: #GstByteWriter instance
116 * Initializes @writer to an empty instance
119 gst_byte_writer_init (GstByteWriter * writer)
121 g_return_if_fail (writer != NULL);
123 memset (writer, 0, sizeof (GstByteWriter));
125 writer->owned = TRUE;
129 * gst_byte_writer_init_with_size:
130 * @writer: #GstByteWriter instance
131 * @size: Initial size of data
132 * @fixed: If %TRUE the data can't be reallocated
134 * Initializes @writer with the given initial data size.
137 gst_byte_writer_init_with_size (GstByteWriter * writer, guint size,
140 g_return_if_fail (writer != NULL);
142 gst_byte_writer_init (writer);
144 writer->parent.data = g_malloc (size);
145 writer->alloc_size = size;
146 writer->fixed = fixed;
147 writer->owned = TRUE;
151 * gst_byte_writer_init_with_data:
152 * @writer: #GstByteWriter instance
153 * @data: (array length=size) (transfer none): Memory area for writing
154 * @size: Size of @data in bytes
155 * @initialized: If %TRUE the complete data can be read from the beginning
157 * Initializes @writer with the given
158 * memory area. If @initialized is %TRUE it is possible to
159 * read @size bytes from the #GstByteWriter from the beginning.
162 gst_byte_writer_init_with_data (GstByteWriter * writer, guint8 * data,
163 guint size, gboolean initialized)
165 g_return_if_fail (writer != NULL);
167 gst_byte_writer_init (writer);
169 writer->parent.data = data;
170 writer->parent.size = (initialized) ? size : 0;
171 writer->alloc_size = size;
172 writer->fixed = TRUE;
173 writer->owned = FALSE;
177 * gst_byte_writer_reset:
178 * @writer: #GstByteWriter instance
180 * Resets @writer and frees the data if it's
184 gst_byte_writer_reset (GstByteWriter * writer)
186 g_return_if_fail (writer != NULL);
189 g_free ((guint8 *) writer->parent.data);
190 memset (writer, 0, sizeof (GstByteWriter));
194 * gst_byte_writer_reset_and_get_data:
195 * @writer: #GstByteWriter instance
197 * Resets @writer and returns the current data.
199 * Free-function: g_free
201 * Returns: (array) (transfer full): the current data. g_free() after
205 gst_byte_writer_reset_and_get_data (GstByteWriter * writer)
209 g_return_val_if_fail (writer != NULL, NULL);
211 data = (guint8 *) writer->parent.data;
213 data = g_memdup (data, writer->parent.size);
214 writer->parent.data = NULL;
215 gst_byte_writer_reset (writer);
221 * gst_byte_writer_reset_and_get_buffer:
222 * @writer: #GstByteWriter instance
224 * Resets @writer and returns the current data as buffer.
226 * Free-function: gst_buffer_unref
228 * Returns: (transfer full): the current data as buffer. gst_buffer_unref()
232 gst_byte_writer_reset_and_get_buffer (GstByteWriter * writer)
238 g_return_val_if_fail (writer != NULL, NULL);
240 size = writer->parent.size;
241 data = gst_byte_writer_reset_and_get_data (writer);
243 buffer = gst_buffer_new ();
245 gst_buffer_append_memory (buffer,
246 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
253 * gst_byte_writer_free:
254 * @writer: (in) (transfer full): #GstByteWriter instance
256 * Frees @writer and all memory allocated by it.
259 gst_byte_writer_free (GstByteWriter * writer)
261 g_return_if_fail (writer != NULL);
263 gst_byte_writer_reset (writer);
264 g_slice_free (GstByteWriter, writer);
268 * gst_byte_writer_free_and_get_data:
269 * @writer: (in) (transfer full): #GstByteWriter instance
271 * Frees @writer and all memory allocated by it except
272 * the current data, which is returned.
274 * Free-function: g_free
276 * Returns: (transfer full): the current data. g_free() after usage.
279 gst_byte_writer_free_and_get_data (GstByteWriter * writer)
283 g_return_val_if_fail (writer != NULL, NULL);
285 data = gst_byte_writer_reset_and_get_data (writer);
286 g_slice_free (GstByteWriter, writer);
292 * gst_byte_writer_free_and_get_buffer:
293 * @writer: (in) (transfer full): #GstByteWriter instance
295 * Frees @writer and all memory allocated by it except
296 * the current data, which is returned as #GstBuffer.
298 * Free-function: gst_buffer_unref
300 * Returns: (transfer full): the current data as buffer. gst_buffer_unref()
304 gst_byte_writer_free_and_get_buffer (GstByteWriter * writer)
308 g_return_val_if_fail (writer != NULL, NULL);
310 buffer = gst_byte_writer_reset_and_get_buffer (writer);
311 g_slice_free (GstByteWriter, writer);
317 * gst_byte_writer_get_remaining:
318 * @writer: #GstByteWriter instance
320 * Returns the remaining size of data that can still be written. If
321 * -1 is returned the remaining size is only limited by system resources.
323 * Returns: the remaining size of data that can still be written
326 gst_byte_writer_get_remaining (const GstByteWriter * writer)
328 g_return_val_if_fail (writer != NULL, -1);
333 return writer->alloc_size - writer->parent.byte;
337 * gst_byte_writer_ensure_free_space:
338 * @writer: #GstByteWriter instance
339 * @size: Number of bytes that should be available
341 * Checks if enough free space from the current write cursor is
342 * available and reallocates if necessary.
344 * Returns: %TRUE if at least @size bytes are still available
347 gst_byte_writer_ensure_free_space (GstByteWriter * writer, guint size)
349 return _gst_byte_writer_ensure_free_space_inline (writer, size);
353 #define CREATE_WRITE_FUNC(bits,type,name,write_func) \
355 gst_byte_writer_put_##name (GstByteWriter *writer, type val) \
357 return _gst_byte_writer_put_##name##_inline (writer, val); \
360 CREATE_WRITE_FUNC (8, guint8, uint8, GST_WRITE_UINT8);
361 CREATE_WRITE_FUNC (8, gint8, int8, GST_WRITE_UINT8);
362 CREATE_WRITE_FUNC (16, guint16, uint16_le, GST_WRITE_UINT16_LE);
363 CREATE_WRITE_FUNC (16, guint16, uint16_be, GST_WRITE_UINT16_BE);
364 CREATE_WRITE_FUNC (16, gint16, int16_le, GST_WRITE_UINT16_LE);
365 CREATE_WRITE_FUNC (16, gint16, int16_be, GST_WRITE_UINT16_BE);
366 CREATE_WRITE_FUNC (24, guint32, uint24_le, GST_WRITE_UINT24_LE);
367 CREATE_WRITE_FUNC (24, guint32, uint24_be, GST_WRITE_UINT24_BE);
368 CREATE_WRITE_FUNC (24, gint32, int24_le, GST_WRITE_UINT24_LE);
369 CREATE_WRITE_FUNC (24, gint32, int24_be, GST_WRITE_UINT24_BE);
370 CREATE_WRITE_FUNC (32, guint32, uint32_le, GST_WRITE_UINT32_LE);
371 CREATE_WRITE_FUNC (32, guint32, uint32_be, GST_WRITE_UINT32_BE);
372 CREATE_WRITE_FUNC (32, gint32, int32_le, GST_WRITE_UINT32_LE);
373 CREATE_WRITE_FUNC (32, gint32, int32_be, GST_WRITE_UINT32_BE);
374 CREATE_WRITE_FUNC (64, guint64, uint64_le, GST_WRITE_UINT64_LE);
375 CREATE_WRITE_FUNC (64, guint64, uint64_be, GST_WRITE_UINT64_BE);
376 CREATE_WRITE_FUNC (64, gint64, int64_le, GST_WRITE_UINT64_LE);
377 CREATE_WRITE_FUNC (64, gint64, int64_be, GST_WRITE_UINT64_BE);
379 CREATE_WRITE_FUNC (32, gfloat, float32_be, GST_WRITE_FLOAT_BE);
380 CREATE_WRITE_FUNC (32, gfloat, float32_le, GST_WRITE_FLOAT_LE);
381 CREATE_WRITE_FUNC (64, gdouble, float64_be, GST_WRITE_DOUBLE_BE);
382 CREATE_WRITE_FUNC (64, gdouble, float64_le, GST_WRITE_DOUBLE_LE);
385 gst_byte_writer_put_data (GstByteWriter * writer, const guint8 * data,
388 return _gst_byte_writer_put_data_inline (writer, data, size);
392 gst_byte_writer_fill (GstByteWriter * writer, guint8 value, guint size)
394 return _gst_byte_writer_fill_inline (writer, value, size);
397 #define CREATE_WRITE_STRING_FUNC(bits,type) \
399 gst_byte_writer_put_string_utf##bits (GstByteWriter *writer, const type * data) \
403 g_return_val_if_fail (writer != NULL, FALSE); \
405 /* endianness does not matter if we are looking for a NUL terminator */ \
406 while (data[size] != 0) { \
407 /* have prevent overflow */ \
408 if (G_UNLIKELY (size == G_MAXUINT)) \
414 if (G_UNLIKELY (!_gst_byte_writer_ensure_free_space_inline(writer, size * (bits / 8)))) \
417 _gst_byte_writer_put_data_inline (writer, (const guint8 *) data, size * (bits / 8)); \
422 CREATE_WRITE_STRING_FUNC (8, gchar);
423 CREATE_WRITE_STRING_FUNC (16, guint16);
424 CREATE_WRITE_STRING_FUNC (32, guint32);
426 * gst_byte_writer_put_uint8:
427 * @writer: #GstByteWriter instance
428 * @val: Value to write
430 * Writes a unsigned 8 bit integer to @writer.
432 * Returns: %TRUE if the value could be written
435 * gst_byte_writer_put_uint16_be:
436 * @writer: #GstByteWriter instance
437 * @val: Value to write
439 * Writes a unsigned big endian 16 bit integer to @writer.
441 * Returns: %TRUE if the value could be written
444 * gst_byte_writer_put_uint24_be:
445 * @writer: #GstByteWriter instance
446 * @val: Value to write
448 * Writes a unsigned big endian 24 bit integer to @writer.
450 * Returns: %TRUE if the value could be written
453 * gst_byte_writer_put_uint32_be:
454 * @writer: #GstByteWriter instance
455 * @val: Value to write
457 * Writes a unsigned big endian 32 bit integer to @writer.
459 * Returns: %TRUE if the value could be written
462 * gst_byte_writer_put_uint64_be:
463 * @writer: #GstByteWriter instance
464 * @val: Value to write
466 * Writes a unsigned big endian 64 bit integer to @writer.
468 * Returns: %TRUE if the value could be written
471 * gst_byte_writer_put_uint16_le:
472 * @writer: #GstByteWriter instance
473 * @val: Value to write
475 * Writes a unsigned little endian 16 bit integer to @writer.
477 * Returns: %TRUE if the value could be written
480 * gst_byte_writer_put_uint24_le:
481 * @writer: #GstByteWriter instance
482 * @val: Value to write
484 * Writes a unsigned little endian 24 bit integer to @writer.
486 * Returns: %TRUE if the value could be written
489 * gst_byte_writer_put_uint32_le:
490 * @writer: #GstByteWriter instance
491 * @val: Value to write
493 * Writes a unsigned little endian 32 bit integer to @writer.
495 * Returns: %TRUE if the value could be written
498 * gst_byte_writer_put_uint64_le:
499 * @writer: #GstByteWriter instance
500 * @val: Value to write
502 * Writes a unsigned little endian 64 bit integer to @writer.
504 * Returns: %TRUE if the value could be written
507 * gst_byte_writer_put_int8:
508 * @writer: #GstByteWriter instance
509 * @val: Value to write
511 * Writes a signed 8 bit integer to @writer.
513 * Returns: %TRUE if the value could be written
516 * gst_byte_writer_put_int16_be:
517 * @writer: #GstByteWriter instance
518 * @val: Value to write
520 * Writes a signed big endian 16 bit integer to @writer.
522 * Returns: %TRUE if the value could be written
525 * gst_byte_writer_put_int24_be:
526 * @writer: #GstByteWriter instance
527 * @val: Value to write
529 * Writes a signed big endian 24 bit integer to @writer.
531 * Returns: %TRUE if the value could be written
534 * gst_byte_writer_put_int32_be:
535 * @writer: #GstByteWriter instance
536 * @val: Value to write
538 * Writes a signed big endian 32 bit integer to @writer.
540 * Returns: %TRUE if the value could be written
543 * gst_byte_writer_put_int64_be:
544 * @writer: #GstByteWriter instance
545 * @val: Value to write
547 * Writes a signed big endian 64 bit integer to @writer.
549 * Returns: %TRUE if the value could be written
552 * gst_byte_writer_put_int16_le:
553 * @writer: #GstByteWriter instance
554 * @val: Value to write
556 * Writes a signed little endian 16 bit integer to @writer.
558 * Returns: %TRUE if the value could be written
561 * gst_byte_writer_put_int24_le:
562 * @writer: #GstByteWriter instance
563 * @val: Value to write
565 * Writes a signed little endian 24 bit integer to @writer.
567 * Returns: %TRUE if the value could be written
570 * gst_byte_writer_put_int32_le:
571 * @writer: #GstByteWriter instance
572 * @val: Value to write
574 * Writes a signed little endian 32 bit integer to @writer.
576 * Returns: %TRUE if the value could be written
579 * gst_byte_writer_put_int64_le:
580 * @writer: #GstByteWriter instance
581 * @val: Value to write
583 * Writes a signed little endian 64 bit integer to @writer.
585 * Returns: %TRUE if the value could be written
588 * gst_byte_writer_put_float32_be:
589 * @writer: #GstByteWriter instance
590 * @val: Value to write
592 * Writes a big endian 32 bit float to @writer.
594 * Returns: %TRUE if the value could be written
597 * gst_byte_writer_put_float64_be:
598 * @writer: #GstByteWriter instance
599 * @val: Value to write
601 * Writes a big endian 64 bit float to @writer.
603 * Returns: %TRUE if the value could be written
606 * gst_byte_writer_put_float32_le:
607 * @writer: #GstByteWriter instance
608 * @val: Value to write
610 * Writes a little endian 32 bit float to @writer.
612 * Returns: %TRUE if the value could be written
615 * gst_byte_writer_put_float64_le:
616 * @writer: #GstByteWriter instance
617 * @val: Value to write
619 * Writes a little endian 64 bit float to @writer.
621 * Returns: %TRUE if the value could be written
624 * gst_byte_writer_put_string_utf8:
625 * @writer: #GstByteWriter instance
626 * @data: (transfer none) (array zero-terminated=1) (type utf8): UTF8 string to
629 * Writes a NUL-terminated UTF8 string to @writer (including the terminator).
631 * Returns: %TRUE if the value could be written
634 * gst_byte_writer_put_string_utf16:
635 * @writer: #GstByteWriter instance
636 * @data: (transfer none) (array zero-terminated=1): UTF16 string to write
638 * Writes a NUL-terminated UTF16 string to @writer (including the terminator).
640 * Returns: %TRUE if the value could be written
643 * gst_byte_writer_put_string_utf32:
644 * @writer: #GstByteWriter instance
645 * @data: (transfer none) (array zero-terminated=1): UTF32 string to write
647 * Writes a NUL-terminated UTF32 string to @writer (including the terminator).
649 * Returns: %TRUE if the value could be written
652 * gst_byte_writer_put_data:
653 * @writer: #GstByteWriter instance
654 * @data: (transfer none) (array length=size): Data to write
655 * @size: Size of @data in bytes
657 * Writes @size bytes of @data to @writer.
659 * Returns: %TRUE if the value could be written
662 * gst_byte_writer_fill:
663 * @writer: #GstByteWriter instance
664 * @value: Value to be written
665 * @size: Number of bytes to be written
667 * Writes @size bytes containing @value to @writer.
669 * Returns: %TRUE if the value could be written
673 * gst_byte_writer_put_buffer:
674 * @writer: #GstByteWriter instance
675 * @buffer: (transfer none): source #GstBuffer
676 * @offset: offset to copy from
677 * @size: total size to copy. If -1, all data is copied
679 * Writes @size bytes of @data to @writer.
681 * Returns: %TRUE if the data could be written