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 #ifndef __GST_BYTE_WRITER_H__
22 #define __GST_BYTE_WRITER_H__
23
24 #include <gst/gst.h>
25 #include <gst/base/gstbytereader.h>
26
27 #include <string.h>
28
29 G_BEGIN_DECLS
30
31 #define GST_BYTE_WRITER(writer) ((GstByteWriter *) (writer))
32
33 /**
34 * GstByteWriter:
35 * @parent: #GstByteReader parent
36 * @alloc_size: Allocation size of the data
37 * @fixed: If %TRUE no reallocations are allowed
38 * @owned: If %FALSE no reallocations are allowed and copies of data are returned
39 *
40 * A byte writer instance.
41 */
42 typedef struct {
43 GstByteReader parent;
44
45 guint alloc_size;
46
47 gboolean fixed;
48 gboolean owned;
49
50 /* < private > */
51 gpointer _gst_reserved[GST_PADDING];
52 } GstByteWriter;
53
54 GST_BASE_API
55 GstByteWriter * gst_byte_writer_new (void) G_GNUC_MALLOC;
56
57 GST_BASE_API
58 GstByteWriter * gst_byte_writer_new_with_size (guint size, gboolean fixed) G_GNUC_MALLOC;
59
60 GST_BASE_API
61 GstByteWriter * gst_byte_writer_new_with_data (guint8 *data, guint size, gboolean initialized) G_GNUC_MALLOC;
62
63 GST_BASE_API
64 void gst_byte_writer_init (GstByteWriter *writer);
65
66 GST_BASE_API
67 void gst_byte_writer_init_with_size (GstByteWriter *writer, guint size, gboolean fixed);
68
69 GST_BASE_API
70 void gst_byte_writer_init_with_data (GstByteWriter *writer, guint8 *data,
71 guint size, gboolean initialized);
72 GST_BASE_API
73 void gst_byte_writer_free (GstByteWriter *writer);
74
75 GST_BASE_API
76 guint8 * gst_byte_writer_free_and_get_data (GstByteWriter *writer);
77
78 GST_BASE_API
79 GstBuffer * gst_byte_writer_free_and_get_buffer (GstByteWriter *writer) G_GNUC_MALLOC;
80
81 GST_BASE_API
82 void gst_byte_writer_reset (GstByteWriter *writer);
83
84 GST_BASE_API
85 guint8 * gst_byte_writer_reset_and_get_data (GstByteWriter *writer);
86
87 GST_BASE_API
88 GstBuffer * gst_byte_writer_reset_and_get_buffer (GstByteWriter *writer) G_GNUC_MALLOC;
89
90 /**
91 * gst_byte_writer_get_pos:
92 * @writer: #GstByteWriter instance
93 *
94 * Returns: The current position of the read/write cursor
95 */
96 /**
97 * gst_byte_writer_set_pos:
98 * @writer: #GstByteWriter instance
99 * @pos: new position
100 *
101 * Sets the current read/write cursor of @writer. The new position
102 * can only be between 0 and the current size.
103 *
104 * Returns: %TRUE if the new position could be set
105 */
106 /**
107 * gst_byte_writer_get_size:
108 * @writer: #GstByteWriter instance
109 *
110 * Returns: The current, initialized size of the data
111 */
112 static inline guint
gst_byte_writer_get_pos(const GstByteWriter * writer)113 gst_byte_writer_get_pos (const GstByteWriter *writer)
114 {
115 return gst_byte_reader_get_pos ((const GstByteReader *) writer);
116 }
117
118 static inline gboolean
gst_byte_writer_set_pos(GstByteWriter * writer,guint pos)119 gst_byte_writer_set_pos (GstByteWriter *writer, guint pos)
120 {
121 return gst_byte_reader_set_pos (GST_BYTE_READER (writer), pos);
122 }
123
124 static inline guint
gst_byte_writer_get_size(const GstByteWriter * writer)125 gst_byte_writer_get_size (const GstByteWriter *writer)
126 {
127 return gst_byte_reader_get_size ((const GstByteReader *) writer);
128 }
129
130 GST_BASE_API
131 guint gst_byte_writer_get_remaining (const GstByteWriter *writer);
132
133 GST_BASE_API
134 gboolean gst_byte_writer_ensure_free_space (GstByteWriter *writer, guint size);
135
136 GST_BASE_API
137 gboolean gst_byte_writer_put_uint8 (GstByteWriter *writer, guint8 val);
138
139 GST_BASE_API
140 gboolean gst_byte_writer_put_int8 (GstByteWriter *writer, gint8 val);
141
142 GST_BASE_API
143 gboolean gst_byte_writer_put_uint16_be (GstByteWriter *writer, guint16 val);
144
145 GST_BASE_API
146 gboolean gst_byte_writer_put_uint16_le (GstByteWriter *writer, guint16 val);
147
148 GST_BASE_API
149 gboolean gst_byte_writer_put_int16_be (GstByteWriter *writer, gint16 val);
150
151 GST_BASE_API
152 gboolean gst_byte_writer_put_int16_le (GstByteWriter *writer, gint16 val);
153
154 GST_BASE_API
155 gboolean gst_byte_writer_put_uint24_be (GstByteWriter *writer, guint32 val);
156
157 GST_BASE_API
158 gboolean gst_byte_writer_put_uint24_le (GstByteWriter *writer, guint32 val);
159
160 GST_BASE_API
161 gboolean gst_byte_writer_put_int24_be (GstByteWriter *writer, gint32 val);
162
163 GST_BASE_API
164 gboolean gst_byte_writer_put_int24_le (GstByteWriter *writer, gint32 val);
165
166 GST_BASE_API
167 gboolean gst_byte_writer_put_uint32_be (GstByteWriter *writer, guint32 val);
168
169 GST_BASE_API
170 gboolean gst_byte_writer_put_uint32_le (GstByteWriter *writer, guint32 val);
171
172 GST_BASE_API
173 gboolean gst_byte_writer_put_int32_be (GstByteWriter *writer, gint32 val);
174
175 GST_BASE_API
176 gboolean gst_byte_writer_put_int32_le (GstByteWriter *writer, gint32 val);
177
178 GST_BASE_API
179 gboolean gst_byte_writer_put_uint64_be (GstByteWriter *writer, guint64 val);
180
181 GST_BASE_API
182 gboolean gst_byte_writer_put_uint64_le (GstByteWriter *writer, guint64 val);
183
184 GST_BASE_API
185 gboolean gst_byte_writer_put_int64_be (GstByteWriter *writer, gint64 val);
186
187 GST_BASE_API
188 gboolean gst_byte_writer_put_int64_le (GstByteWriter *writer, gint64 val);
189
190 GST_BASE_API
191 gboolean gst_byte_writer_put_float32_be (GstByteWriter *writer, gfloat val);
192
193 GST_BASE_API
194 gboolean gst_byte_writer_put_float32_le (GstByteWriter *writer, gfloat val);
195
196 GST_BASE_API
197 gboolean gst_byte_writer_put_float64_be (GstByteWriter *writer, gdouble val);
198
199 GST_BASE_API
200 gboolean gst_byte_writer_put_float64_le (GstByteWriter *writer, gdouble val);
201
202 GST_BASE_API
203 gboolean gst_byte_writer_put_data (GstByteWriter *writer, const guint8 *data, guint size);
204
205 GST_BASE_API
206 gboolean gst_byte_writer_fill (GstByteWriter *writer, guint8 value, guint size);
207
208 GST_BASE_API
209 gboolean gst_byte_writer_put_string_utf8 (GstByteWriter *writer, const gchar *data);
210
211 GST_BASE_API
212 gboolean gst_byte_writer_put_string_utf16 (GstByteWriter *writer, const guint16 *data);
213
214 GST_BASE_API
215 gboolean gst_byte_writer_put_string_utf32 (GstByteWriter *writer, const guint32 *data);
216 gboolean gst_byte_writer_put_buffer (GstByteWriter *writer, GstBuffer * buffer, gsize offset, gssize size);
217
218 /**
219 * gst_byte_writer_put_string:
220 * @writer: #GstByteWriter instance
221 * @data: (in) (array zero-terminated=1): Null terminated string
222 *
223 * Write a NUL-terminated string to @writer (including the terminator). The
224 * string is assumed to be in an 8-bit encoding (e.g. ASCII,UTF-8 or
225 * ISO-8859-1).
226 *
227 * Returns: %TRUE if the string could be written
228 */
229 #define gst_byte_writer_put_string(writer, data) \
230 gst_byte_writer_put_string_utf8(writer, data)
231
232 static inline guint
_gst_byte_writer_next_pow2(guint n)233 _gst_byte_writer_next_pow2 (guint n)
234 {
235 guint ret = 16;
236
237 /* We start with 16, smaller allocations make no sense */
238
239 while (ret < n && ret > 0)
240 ret <<= 1;
241
242 return ret ? ret : n;
243 }
244
245 static inline gboolean
_gst_byte_writer_ensure_free_space_inline(GstByteWriter * writer,guint size)246 _gst_byte_writer_ensure_free_space_inline (GstByteWriter * writer, guint size)
247 {
248 gpointer data;
249
250 if (G_LIKELY (size <= writer->alloc_size - writer->parent.byte))
251 return TRUE;
252 if (G_UNLIKELY (writer->fixed || !writer->owned))
253 return FALSE;
254 if (G_UNLIKELY (writer->parent.byte > G_MAXUINT - size))
255 return FALSE;
256
257 writer->alloc_size = _gst_byte_writer_next_pow2 (writer->parent.byte + size);
258 data = g_try_realloc ((guint8 *) writer->parent.data, writer->alloc_size);
259 if (G_UNLIKELY (data == NULL))
260 return FALSE;
261
262 writer->parent.data = (guint8 *) data;
263
264 return TRUE;
265 }
266
267 #define __GST_BYTE_WRITER_CREATE_WRITE_FUNC(bits,type,name,write_func) \
268 static inline void \
269 gst_byte_writer_put_##name##_unchecked (GstByteWriter *writer, type val) \
270 { \
271 guint8 *write_data; \
272 \
273 write_data = (guint8 *) writer->parent.data + writer->parent.byte; \
274 write_func (write_data, val); \
275 writer->parent.byte += bits/8; \
276 writer->parent.size = MAX (writer->parent.size, writer->parent.byte); \
277 } \
278 \
279 static inline gboolean \
280 _gst_byte_writer_put_##name##_inline (GstByteWriter *writer, type val) \
281 { \
282 g_return_val_if_fail (writer != NULL, FALSE); \
283 \
284 if (G_UNLIKELY (!_gst_byte_writer_ensure_free_space_inline(writer, bits/8))) \
285 return FALSE; \
286 \
287 gst_byte_writer_put_##name##_unchecked (writer, val); \
288 \
289 return TRUE; \
290 }
291
292 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (8, guint8, uint8, GST_WRITE_UINT8)
293 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (8, gint8, int8, GST_WRITE_UINT8)
294 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (16, guint16, uint16_le, GST_WRITE_UINT16_LE)
295 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (16, guint16, uint16_be, GST_WRITE_UINT16_BE)
296 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (16, gint16, int16_le, GST_WRITE_UINT16_LE)
297 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (16, gint16, int16_be, GST_WRITE_UINT16_BE)
298 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (24, guint32, uint24_le, GST_WRITE_UINT24_LE)
299 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (24, guint32, uint24_be, GST_WRITE_UINT24_BE)
300 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (24, gint32, int24_le, GST_WRITE_UINT24_LE)
301 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (24, gint32, int24_be, GST_WRITE_UINT24_BE)
302 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (32, guint32, uint32_le, GST_WRITE_UINT32_LE)
303 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (32, guint32, uint32_be, GST_WRITE_UINT32_BE)
304 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (32, gint32, int32_le, GST_WRITE_UINT32_LE)
305 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (32, gint32, int32_be, GST_WRITE_UINT32_BE)
306 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (64, guint64, uint64_le, GST_WRITE_UINT64_LE)
307 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (64, guint64, uint64_be, GST_WRITE_UINT64_BE)
308 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (64, gint64, int64_le, GST_WRITE_UINT64_LE)
309 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (64, gint64, int64_be, GST_WRITE_UINT64_BE)
310
311 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (32, gfloat, float32_be, GST_WRITE_FLOAT_BE)
312 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (32, gfloat, float32_le, GST_WRITE_FLOAT_LE)
313 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (64, gdouble, float64_be, GST_WRITE_DOUBLE_BE)
314 __GST_BYTE_WRITER_CREATE_WRITE_FUNC (64, gdouble, float64_le, GST_WRITE_DOUBLE_LE)
315
316 #undef __GST_BYTE_WRITER_CREATE_WRITE_FUNC
317
318 static inline void
gst_byte_writer_put_data_unchecked(GstByteWriter * writer,const guint8 * data,guint size)319 gst_byte_writer_put_data_unchecked (GstByteWriter * writer, const guint8 * data,
320 guint size)
321 {
322 memcpy ((guint8 *) & writer->parent.data[writer->parent.byte], data, size);
323 writer->parent.byte += size;
324 writer->parent.size = MAX (writer->parent.size, writer->parent.byte);
325 }
326
327 static inline gboolean
_gst_byte_writer_put_data_inline(GstByteWriter * writer,const guint8 * data,guint size)328 _gst_byte_writer_put_data_inline (GstByteWriter * writer, const guint8 * data,
329 guint size)
330 {
331 g_return_val_if_fail (writer != NULL, FALSE);
332
333 if (G_UNLIKELY (!_gst_byte_writer_ensure_free_space_inline (writer, size)))
334 return FALSE;
335
336 gst_byte_writer_put_data_unchecked (writer, data, size);
337
338 return TRUE;
339 }
340
341 static inline void
gst_byte_writer_fill_unchecked(GstByteWriter * writer,guint8 value,guint size)342 gst_byte_writer_fill_unchecked (GstByteWriter * writer, guint8 value, guint size)
343 {
344 memset ((guint8 *) & writer->parent.data[writer->parent.byte], value, size);
345 writer->parent.byte += size;
346 writer->parent.size = MAX (writer->parent.size, writer->parent.byte);
347 }
348
349 static inline gboolean
_gst_byte_writer_fill_inline(GstByteWriter * writer,guint8 value,guint size)350 _gst_byte_writer_fill_inline (GstByteWriter * writer, guint8 value, guint size)
351 {
352 g_return_val_if_fail (writer != NULL, FALSE);
353
354 if (G_UNLIKELY (!_gst_byte_writer_ensure_free_space_inline (writer, size)))
355 return FALSE;
356
357 gst_byte_writer_fill_unchecked (writer, value, size);
358
359 return TRUE;
360 }
361
362 static inline void
gst_byte_writer_put_buffer_unchecked(GstByteWriter * writer,GstBuffer * buffer,gsize offset,gssize size)363 gst_byte_writer_put_buffer_unchecked (GstByteWriter * writer, GstBuffer * buffer,
364 gsize offset, gssize size)
365 {
366 if (size == -1) {
367 size = gst_buffer_get_size (buffer);
368
369 if (offset >= (gsize) size)
370 return;
371
372 size -= offset;
373 }
374
375 gst_buffer_extract (buffer, offset,
376 (guint8 *) & writer->parent.data[writer->parent.byte], size);
377 writer->parent.byte += size;
378 writer->parent.size = MAX (writer->parent.size, writer->parent.byte);
379 }
380
381 static inline gboolean
_gst_byte_writer_put_buffer_inline(GstByteWriter * writer,GstBuffer * buffer,gsize offset,gssize size)382 _gst_byte_writer_put_buffer_inline (GstByteWriter * writer, GstBuffer * buffer,
383 gsize offset, gssize size)
384 {
385 g_return_val_if_fail (writer != NULL, FALSE);
386 g_return_val_if_fail (size >= -1, FALSE);
387
388 if (size == -1) {
389 size = gst_buffer_get_size (buffer);
390
391 if (offset >= (gsize) size)
392 return TRUE;
393
394 size -= offset;
395 }
396
397 if (G_UNLIKELY (!_gst_byte_writer_ensure_free_space_inline (writer, size)))
398 return FALSE;
399
400 gst_byte_writer_put_buffer_unchecked (writer, buffer, offset, size);
401
402 return TRUE;
403 }
404
405 #ifndef GST_BYTE_WRITER_DISABLE_INLINES
406
407 /* we use defines here so we can add the G_LIKELY() */
408
409 #define gst_byte_writer_ensure_free_space(writer, size) \
410 G_LIKELY (_gst_byte_writer_ensure_free_space_inline (writer, size))
411 #define gst_byte_writer_put_uint8(writer, val) \
412 G_LIKELY (_gst_byte_writer_put_uint8_inline (writer, val))
413 #define gst_byte_writer_put_int8(writer, val) \
414 G_LIKELY (_gst_byte_writer_put_int8_inline (writer, val))
415 #define gst_byte_writer_put_uint16_be(writer, val) \
416 G_LIKELY (_gst_byte_writer_put_uint16_be_inline (writer, val))
417 #define gst_byte_writer_put_uint16_le(writer, val) \
418 G_LIKELY (_gst_byte_writer_put_uint16_le_inline (writer, val))
419 #define gst_byte_writer_put_int16_be(writer, val) \
420 G_LIKELY (_gst_byte_writer_put_int16_be_inline (writer, val))
421 #define gst_byte_writer_put_int16_le(writer, val) \
422 G_LIKELY (_gst_byte_writer_put_int16_le_inline (writer, val))
423 #define gst_byte_writer_put_uint24_be(writer, val) \
424 G_LIKELY (_gst_byte_writer_put_uint24_be_inline (writer, val))
425 #define gst_byte_writer_put_uint24_le(writer, val) \
426 G_LIKELY (_gst_byte_writer_put_uint24_le_inline (writer, val))
427 #define gst_byte_writer_put_int24_be(writer, val) \
428 G_LIKELY (_gst_byte_writer_put_int24_be_inline (writer, val))
429 #define gst_byte_writer_put_int24_le(writer, val) \
430 G_LIKELY (_gst_byte_writer_put_int24_le_inline (writer, val))
431 #define gst_byte_writer_put_uint32_be(writer, val) \
432 G_LIKELY (_gst_byte_writer_put_uint32_be_inline (writer, val))
433 #define gst_byte_writer_put_uint32_le(writer, val) \
434 G_LIKELY (_gst_byte_writer_put_uint32_le_inline (writer, val))
435 #define gst_byte_writer_put_int32_be(writer, val) \
436 G_LIKELY (_gst_byte_writer_put_int32_be_inline (writer, val))
437 #define gst_byte_writer_put_int32_le(writer, val) \
438 G_LIKELY (_gst_byte_writer_put_int32_le_inline (writer, val))
439 #define gst_byte_writer_put_uint64_be(writer, val) \
440 G_LIKELY (_gst_byte_writer_put_uint64_be_inline (writer, val))
441 #define gst_byte_writer_put_uint64_le(writer, val) \
442 G_LIKELY (_gst_byte_writer_put_uint64_le_inline (writer, val))
443 #define gst_byte_writer_put_int64_be(writer, val) \
444 G_LIKELY (_gst_byte_writer_put_int64_be_inline (writer, val))
445 #define gst_byte_writer_put_int64_le(writer, val) \
446 G_LIKELY (_gst_byte_writer_put_int64_le_inline (writer, val))
447
448 #define gst_byte_writer_put_float32_be(writer, val) \
449 G_LIKELY (_gst_byte_writer_put_float32_be_inline (writer, val))
450 #define gst_byte_writer_put_float32_le(writer, val) \
451 G_LIKELY (_gst_byte_writer_put_float32_le_inline (writer, val))
452 #define gst_byte_writer_put_float64_be(writer, val) \
453 G_LIKELY (_gst_byte_writer_put_float64_be_inline (writer, val))
454 #define gst_byte_writer_put_float64_le(writer, val) \
455 G_LIKELY (_gst_byte_writer_put_float64_le_inline (writer, val))
456
457 #define gst_byte_writer_put_data(writer, data, size) \
458 G_LIKELY (_gst_byte_writer_put_data_inline (writer, data, size))
459 #define gst_byte_writer_fill(writer, val, size) \
460 G_LIKELY (_gst_byte_writer_fill_inline (writer, val, size))
461 #define gst_byte_writer_put_buffer(writer, buffer, offset, size) \
462 G_LIKELY (_gst_byte_writer_put_buffer_inline (writer, buffer, offset, size))
463
464 #endif
465
466 G_END_DECLS
467
468 #endif /* __GST_BYTE_WRITER_H__ */
469