docs: Fix typos in function/object descriptions
[platform/upstream/gstreamer.git] / libs / gst / base / gstbytewriter.c
1 /* GStreamer byte writer
2  *
3  * Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>.
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #define GST_BYTE_WRITER_DISABLE_INLINES
26 #include "gstbytewriter.h"
27
28 /**
29  * SECTION:gstbytewriter
30  * @short_description: Writes different integer, string and floating point
31  *     types to a memory buffer and allows reading
32  *
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.
39  */
40
41 /**
42  * gst_byte_writer_new:
43  *
44  * Creates a new, empty #GstByteWriter instance
45  *
46  * Free-function: gst_byte_writer_free
47  *
48  * Returns: (transfer full): a new, empty #GstByteWriter instance
49  */
50 GstByteWriter *
51 gst_byte_writer_new (void)
52 {
53   GstByteWriter *ret = g_slice_new0 (GstByteWriter);
54
55   ret->owned = TRUE;
56   return ret;
57 }
58
59 /**
60  * gst_byte_writer_new_with_size:
61  * @size: Initial size of data
62  * @fixed: If %TRUE the data can't be reallocated
63  *
64  * Creates a new #GstByteWriter instance with the given
65  * initial data size.
66  *
67  * Free-function: gst_byte_writer_free
68  *
69  * Returns: (transfer full): a new #GstByteWriter instance
70  */
71 GstByteWriter *
72 gst_byte_writer_new_with_size (guint size, gboolean fixed)
73 {
74   GstByteWriter *ret = gst_byte_writer_new ();
75
76   ret->alloc_size = size;
77   ret->parent.data = g_malloc (ret->alloc_size);
78   ret->fixed = fixed;
79   ret->owned = TRUE;
80
81   return ret;
82 }
83
84 /**
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
89  *
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.
93  *
94  * Free-function: gst_byte_writer_free
95  *
96  * Returns: (transfer full): a new #GstByteWriter instance
97  */
98 GstByteWriter *
99 gst_byte_writer_new_with_data (guint8 * data, guint size, gboolean initialized)
100 {
101   GstByteWriter *ret = gst_byte_writer_new ();
102
103   ret->parent.data = data;
104   ret->parent.size = (initialized) ? size : 0;
105   ret->alloc_size = size;
106   ret->fixed = TRUE;
107   ret->owned = FALSE;
108
109   return ret;
110 }
111
112 /**
113  * gst_byte_writer_init:
114  * @writer: #GstByteWriter instance
115  *
116  * Initializes @writer to an empty instance
117  */
118 void
119 gst_byte_writer_init (GstByteWriter * writer)
120 {
121   g_return_if_fail (writer != NULL);
122
123   memset (writer, 0, sizeof (GstByteWriter));
124
125   writer->owned = TRUE;
126 }
127
128 /**
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
133  *
134  * Initializes @writer with the given initial data size.
135  */
136 void
137 gst_byte_writer_init_with_size (GstByteWriter * writer, guint size,
138     gboolean fixed)
139 {
140   g_return_if_fail (writer != NULL);
141
142   gst_byte_writer_init (writer);
143
144   writer->parent.data = g_malloc (size);
145   writer->alloc_size = size;
146   writer->fixed = fixed;
147   writer->owned = TRUE;
148 }
149
150 /**
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
156  *
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.
160  */
161 void
162 gst_byte_writer_init_with_data (GstByteWriter * writer, guint8 * data,
163     guint size, gboolean initialized)
164 {
165   g_return_if_fail (writer != NULL);
166
167   gst_byte_writer_init (writer);
168
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;
174 }
175
176 /**
177  * gst_byte_writer_reset:
178  * @writer: #GstByteWriter instance
179  *
180  * Resets @writer and frees the data if it's
181  * owned by @writer.
182  */
183 void
184 gst_byte_writer_reset (GstByteWriter * writer)
185 {
186   g_return_if_fail (writer != NULL);
187
188   if (writer->owned)
189     g_free ((guint8 *) writer->parent.data);
190   memset (writer, 0, sizeof (GstByteWriter));
191 }
192
193 /**
194  * gst_byte_writer_reset_and_get_data:
195  * @writer: #GstByteWriter instance
196  *
197  * Resets @writer and returns the current data.
198  *
199  * Free-function: g_free
200  *
201  * Returns: (array) (transfer full): the current data. g_free() after
202  * usage.
203  */
204 guint8 *
205 gst_byte_writer_reset_and_get_data (GstByteWriter * writer)
206 {
207   guint8 *data;
208
209   g_return_val_if_fail (writer != NULL, NULL);
210
211   data = (guint8 *) writer->parent.data;
212   if (!writer->owned)
213     data = g_memdup (data, writer->parent.size);
214   writer->parent.data = NULL;
215   gst_byte_writer_reset (writer);
216
217   return data;
218 }
219
220 /**
221  * gst_byte_writer_reset_and_get_buffer:
222  * @writer: #GstByteWriter instance
223  *
224  * Resets @writer and returns the current data as buffer.
225  *
226  * Free-function: gst_buffer_unref
227  *
228  * Returns: (transfer full): the current data as buffer. gst_buffer_unref()
229  *     after usage.
230  */
231 GstBuffer *
232 gst_byte_writer_reset_and_get_buffer (GstByteWriter * writer)
233 {
234   GstBuffer *buffer;
235   gpointer data;
236   gsize size;
237
238   g_return_val_if_fail (writer != NULL, NULL);
239
240   size = writer->parent.size;
241   data = gst_byte_writer_reset_and_get_data (writer);
242
243   buffer = gst_buffer_new ();
244   if (data != NULL) {
245     gst_buffer_append_memory (buffer,
246         gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
247   }
248
249   return buffer;
250 }
251
252 /**
253  * gst_byte_writer_free:
254  * @writer: (in) (transfer full): #GstByteWriter instance
255  *
256  * Frees @writer and all memory allocated by it.
257  */
258 void
259 gst_byte_writer_free (GstByteWriter * writer)
260 {
261   g_return_if_fail (writer != NULL);
262
263   gst_byte_writer_reset (writer);
264   g_slice_free (GstByteWriter, writer);
265 }
266
267 /**
268  * gst_byte_writer_free_and_get_data:
269  * @writer: (in) (transfer full): #GstByteWriter instance
270  *
271  * Frees @writer and all memory allocated by it except
272  * the current data, which is returned.
273  *
274  * Free-function: g_free
275  *
276  * Returns: (transfer full): the current data. g_free() after usage.
277  */
278 guint8 *
279 gst_byte_writer_free_and_get_data (GstByteWriter * writer)
280 {
281   guint8 *data;
282
283   g_return_val_if_fail (writer != NULL, NULL);
284
285   data = gst_byte_writer_reset_and_get_data (writer);
286   g_slice_free (GstByteWriter, writer);
287
288   return data;
289 }
290
291 /**
292  * gst_byte_writer_free_and_get_buffer:
293  * @writer: (in) (transfer full): #GstByteWriter instance
294  *
295  * Frees @writer and all memory allocated by it except
296  * the current data, which is returned as #GstBuffer.
297  *
298  * Free-function: gst_buffer_unref
299  *
300  * Returns: (transfer full): the current data as buffer. gst_buffer_unref()
301  *     after usage.
302  */
303 GstBuffer *
304 gst_byte_writer_free_and_get_buffer (GstByteWriter * writer)
305 {
306   GstBuffer *buffer;
307
308   g_return_val_if_fail (writer != NULL, NULL);
309
310   buffer = gst_byte_writer_reset_and_get_buffer (writer);
311   g_slice_free (GstByteWriter, writer);
312
313   return buffer;
314 }
315
316 /**
317  * gst_byte_writer_get_remaining:
318  * @writer: #GstByteWriter instance
319  *
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.
322  *
323  * Returns: the remaining size of data that can still be written
324  */
325 guint
326 gst_byte_writer_get_remaining (const GstByteWriter * writer)
327 {
328   g_return_val_if_fail (writer != NULL, -1);
329
330   if (!writer->fixed)
331     return -1;
332   else
333     return writer->alloc_size - writer->parent.byte;
334 }
335
336 /**
337  * gst_byte_writer_ensure_free_space:
338  * @writer: #GstByteWriter instance
339  * @size: Number of bytes that should be available
340  *
341  * Checks if enough free space from the current write cursor is
342  * available and reallocates if necessary.
343  *
344  * Returns: %TRUE if at least @size bytes are still available
345  */
346 gboolean
347 gst_byte_writer_ensure_free_space (GstByteWriter * writer, guint size)
348 {
349   return _gst_byte_writer_ensure_free_space_inline (writer, size);
350 }
351
352
353 #define CREATE_WRITE_FUNC(bits,type,name,write_func) \
354 gboolean \
355 gst_byte_writer_put_##name (GstByteWriter *writer, type val) \
356 { \
357   return _gst_byte_writer_put_##name##_inline (writer, val); \
358 }
359
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);
378
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);
383
384 gboolean
385 gst_byte_writer_put_data (GstByteWriter * writer, const guint8 * data,
386     guint size)
387 {
388   return _gst_byte_writer_put_data_inline (writer, data, size);
389 }
390
391 gboolean
392 gst_byte_writer_fill (GstByteWriter * writer, guint8 value, guint size)
393 {
394   return _gst_byte_writer_fill_inline (writer, value, size);
395 }
396
397 #define CREATE_WRITE_STRING_FUNC(bits,type) \
398 gboolean \
399 gst_byte_writer_put_string_utf##bits (GstByteWriter *writer, const type * data) \
400 { \
401   guint size = 0; \
402   \
403   g_return_val_if_fail (writer != NULL, FALSE); \
404   \
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)) \
409       return FALSE; \
410     ++size; \
411   } \
412   ++size; \
413   \
414   if (G_UNLIKELY (!_gst_byte_writer_ensure_free_space_inline(writer, size * (bits / 8)))) \
415     return FALSE; \
416   \
417   _gst_byte_writer_put_data_inline (writer, (const guint8 *) data, size * (bits / 8)); \
418   \
419   return TRUE; \
420 }
421
422 CREATE_WRITE_STRING_FUNC (8, gchar);
423 CREATE_WRITE_STRING_FUNC (16, guint16);
424 CREATE_WRITE_STRING_FUNC (32, guint32);
425 /**
426  * gst_byte_writer_put_uint8:
427  * @writer: #GstByteWriter instance
428  * @val: Value to write
429  *
430  * Writes a unsigned 8 bit integer to @writer.
431  *
432  * Returns: %TRUE if the value could be written
433  */
434 /**
435  * gst_byte_writer_put_uint16_be:
436  * @writer: #GstByteWriter instance
437  * @val: Value to write
438  *
439  * Writes a unsigned big endian 16 bit integer to @writer.
440  *
441  * Returns: %TRUE if the value could be written
442  */
443 /**
444  * gst_byte_writer_put_uint24_be:
445  * @writer: #GstByteWriter instance
446  * @val: Value to write
447  *
448  * Writes a unsigned big endian 24 bit integer to @writer.
449  *
450  * Returns: %TRUE if the value could be written
451  */
452 /**
453  * gst_byte_writer_put_uint32_be:
454  * @writer: #GstByteWriter instance
455  * @val: Value to write
456  *
457  * Writes a unsigned big endian 32 bit integer to @writer.
458  *
459  * Returns: %TRUE if the value could be written
460  */
461 /**
462  * gst_byte_writer_put_uint64_be:
463  * @writer: #GstByteWriter instance
464  * @val: Value to write
465  *
466  * Writes a unsigned big endian 64 bit integer to @writer.
467  *
468  * Returns: %TRUE if the value could be written
469  */
470 /**
471  * gst_byte_writer_put_uint16_le:
472  * @writer: #GstByteWriter instance
473  * @val: Value to write
474  *
475  * Writes a unsigned little endian 16 bit integer to @writer.
476  *
477  * Returns: %TRUE if the value could be written
478  */
479 /**
480  * gst_byte_writer_put_uint24_le:
481  * @writer: #GstByteWriter instance
482  * @val: Value to write
483  *
484  * Writes a unsigned little endian 24 bit integer to @writer.
485  *
486  * Returns: %TRUE if the value could be written
487  */
488 /**
489  * gst_byte_writer_put_uint32_le:
490  * @writer: #GstByteWriter instance
491  * @val: Value to write
492  *
493  * Writes a unsigned little endian 32 bit integer to @writer.
494  *
495  * Returns: %TRUE if the value could be written
496  */
497 /**
498  * gst_byte_writer_put_uint64_le:
499  * @writer: #GstByteWriter instance
500  * @val: Value to write
501  *
502  * Writes a unsigned little endian 64 bit integer to @writer.
503  *
504  * Returns: %TRUE if the value could be written
505  */
506 /**
507  * gst_byte_writer_put_int8:
508  * @writer: #GstByteWriter instance
509  * @val: Value to write
510  *
511  * Writes a signed 8 bit integer to @writer.
512  *
513  * Returns: %TRUE if the value could be written
514  */
515 /**
516  * gst_byte_writer_put_int16_be:
517  * @writer: #GstByteWriter instance
518  * @val: Value to write
519  *
520  * Writes a signed big endian 16 bit integer to @writer.
521  *
522  * Returns: %TRUE if the value could be written
523  */
524 /**
525  * gst_byte_writer_put_int24_be:
526  * @writer: #GstByteWriter instance
527  * @val: Value to write
528  *
529  * Writes a signed big endian 24 bit integer to @writer.
530  *
531  * Returns: %TRUE if the value could be written
532  */
533 /**
534  * gst_byte_writer_put_int32_be:
535  * @writer: #GstByteWriter instance
536  * @val: Value to write
537  *
538  * Writes a signed big endian 32 bit integer to @writer.
539  *
540  * Returns: %TRUE if the value could be written
541  */
542 /**
543  * gst_byte_writer_put_int64_be:
544  * @writer: #GstByteWriter instance
545  * @val: Value to write
546  *
547  * Writes a signed big endian 64 bit integer to @writer.
548  *
549  * Returns: %TRUE if the value could be written
550  */
551 /**
552  * gst_byte_writer_put_int16_le:
553  * @writer: #GstByteWriter instance
554  * @val: Value to write
555  *
556  * Writes a signed little endian 16 bit integer to @writer.
557  *
558  * Returns: %TRUE if the value could be written
559  */
560 /**
561  * gst_byte_writer_put_int24_le:
562  * @writer: #GstByteWriter instance
563  * @val: Value to write
564  *
565  * Writes a signed little endian 24 bit integer to @writer.
566  *
567  * Returns: %TRUE if the value could be written
568  */
569 /**
570  * gst_byte_writer_put_int32_le:
571  * @writer: #GstByteWriter instance
572  * @val: Value to write
573  *
574  * Writes a signed little endian 32 bit integer to @writer.
575  *
576  * Returns: %TRUE if the value could be written
577  */
578 /**
579  * gst_byte_writer_put_int64_le:
580  * @writer: #GstByteWriter instance
581  * @val: Value to write
582  *
583  * Writes a signed little endian 64 bit integer to @writer.
584  *
585  * Returns: %TRUE if the value could be written
586  */
587 /**
588  * gst_byte_writer_put_float32_be:
589  * @writer: #GstByteWriter instance
590  * @val: Value to write
591  *
592  * Writes a big endian 32 bit float to @writer.
593  *
594  * Returns: %TRUE if the value could be written
595  */
596 /**
597  * gst_byte_writer_put_float64_be:
598  * @writer: #GstByteWriter instance
599  * @val: Value to write
600  *
601  * Writes a big endian 64 bit float to @writer.
602  *
603  * Returns: %TRUE if the value could be written
604  */
605 /**
606  * gst_byte_writer_put_float32_le:
607  * @writer: #GstByteWriter instance
608  * @val: Value to write
609  *
610  * Writes a little endian 32 bit float to @writer.
611  *
612  * Returns: %TRUE if the value could be written
613  */
614 /**
615  * gst_byte_writer_put_float64_le:
616  * @writer: #GstByteWriter instance
617  * @val: Value to write
618  *
619  * Writes a little endian 64 bit float to @writer.
620  *
621  * Returns: %TRUE if the value could be written
622  */
623 /**
624  * gst_byte_writer_put_string_utf8:
625  * @writer: #GstByteWriter instance
626  * @data: (transfer none) (array zero-terminated=1) (type utf8): UTF8 string to
627  *     write
628  *
629  * Writes a NUL-terminated UTF8 string to @writer (including the terminator).
630  *
631  * Returns: %TRUE if the value could be written
632  */
633 /**
634  * gst_byte_writer_put_string_utf16:
635  * @writer: #GstByteWriter instance
636  * @data: (transfer none) (array zero-terminated=1): UTF16 string to write
637  *
638  * Writes a NUL-terminated UTF16 string to @writer (including the terminator).
639  *
640  * Returns: %TRUE if the value could be written
641  */
642 /**
643  * gst_byte_writer_put_string_utf32:
644  * @writer: #GstByteWriter instance
645  * @data: (transfer none) (array zero-terminated=1): UTF32 string to write
646  *
647  * Writes a NUL-terminated UTF32 string to @writer (including the terminator).
648  *
649  * Returns: %TRUE if the value could be written
650  */
651 /**
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
656  *
657  * Writes @size bytes of @data to @writer.
658  *
659  * Returns: %TRUE if the value could be written
660  */
661 /**
662  * gst_byte_writer_fill:
663  * @writer: #GstByteWriter instance
664  * @value: Value to be written
665  * @size: Number of bytes to be written
666  *
667  * Writes @size bytes containing @value to @writer.
668  *
669  * Returns: %TRUE if the value could be written
670  */
671
672 /**
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
678  *
679  * Writes @size bytes of @data to @writer.
680  *
681  * Returns: %TRUE if the data could be written
682  *
683  */