GCC Code Coverage Report


Directory: ./
File: libfprint/fpi-byte-writer.h
Date: 2024-09-16 14:36:32
Exec Total Coverage
Lines: 38 68 55.9%
Functions: 6 44 13.6%
Branches: 16 112 14.3%

Line Branch Exec Source
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 #pragma once
22
23 #include "fpi-byte-reader.h"
24 #include <string.h>
25
26 G_BEGIN_DECLS
27
28 #define FPI_BYTE_WRITER(writer) ((FpiByteWriter *) (writer))
29
30 /**
31 * FpiByteWriter:
32 * @parent: #FpiByteReader parent
33 * @alloc_size: Allocation size of the data
34 * @fixed: If %TRUE no reallocations are allowed
35 * @owned: If %FALSE no reallocations are allowed and copies of data are returned
36 *
37 * A byte writer instance.
38 */
39 typedef struct {
40 FpiByteReader parent;
41
42 guint alloc_size;
43
44 gboolean fixed;
45 gboolean owned;
46
47 /* < private > */
48 } FpiByteWriter;
49
50
51 FpiByteWriter * fpi_byte_writer_new (void) G_GNUC_MALLOC;
52
53
54 FpiByteWriter * fpi_byte_writer_new_with_size (guint size, gboolean fixed) G_GNUC_MALLOC;
55
56
57 FpiByteWriter * fpi_byte_writer_new_with_data (guint8 *data, guint size, gboolean initialized) G_GNUC_MALLOC;
58
59
60 void fpi_byte_writer_init (FpiByteWriter *writer);
61
62
63 void fpi_byte_writer_init_with_size (FpiByteWriter *writer, guint size, gboolean fixed);
64
65
66 void fpi_byte_writer_init_with_data (FpiByteWriter *writer, guint8 *data,
67 guint size, gboolean initialized);
68
69 void fpi_byte_writer_free (FpiByteWriter *writer);
70
71
72 guint8 * fpi_byte_writer_free_and_get_data (FpiByteWriter *writer);
73
74
75 void fpi_byte_writer_reset (FpiByteWriter *writer);
76
77
78 guint8 * fpi_byte_writer_reset_and_get_data (FpiByteWriter *writer);
79
80 /**
81 * fpi_byte_writer_get_pos:
82 * @writer: #FpiByteWriter instance
83 *
84 * Returns: The current position of the read/write cursor
85 */
86 /**
87 * fpi_byte_writer_set_pos:
88 * @writer: #FpiByteWriter instance
89 * @pos: new position
90 *
91 * Sets the current read/write cursor of @writer. The new position
92 * can only be between 0 and the current size.
93 *
94 * Returns: %TRUE if the new position could be set
95 */
96 /**
97 * fpi_byte_writer_get_size:
98 * @writer: #FpiByteWriter instance
99 *
100 * Returns: The current, initialized size of the data
101 */
102 static inline guint
103 709 fpi_byte_writer_get_pos (const FpiByteWriter *writer)
104 {
105 709 return fpi_byte_reader_get_pos ((const FpiByteReader *) writer);
106 }
107
108 static inline gboolean
109 657 fpi_byte_writer_set_pos (FpiByteWriter *writer, guint pos)
110 {
111 657 return fpi_byte_reader_set_pos (FPI_BYTE_READER (writer), pos);
112 }
113
114 static inline gboolean
115 709 fpi_byte_writer_change_pos (FpiByteWriter *writer, gint pos)
116 {
117 709 pos = fpi_byte_writer_get_pos (writer) + pos;
118
119
1/2
✓ Branch 0 taken 709 times.
✗ Branch 1 not taken.
709 if (pos < 0)
120 return FALSE;
121
122 709 return fpi_byte_reader_set_pos (FPI_BYTE_READER (writer), pos);
123 }
124
125 static inline guint
126 8 fpi_byte_writer_get_size (const FpiByteWriter *writer)
127 {
128 8 return fpi_byte_reader_get_size ((const FpiByteReader *) writer);
129 }
130
131
132 guint fpi_byte_writer_get_remaining (const FpiByteWriter *writer);
133
134
135 gboolean fpi_byte_writer_ensure_free_space (FpiByteWriter *writer, guint size);
136
137
138 gboolean fpi_byte_writer_put_uint8 (FpiByteWriter *writer, guint8 val);
139
140
141 gboolean fpi_byte_writer_put_int8 (FpiByteWriter *writer, gint8 val);
142
143
144 gboolean fpi_byte_writer_put_uint16_be (FpiByteWriter *writer, guint16 val);
145
146
147 gboolean fpi_byte_writer_put_uint16_le (FpiByteWriter *writer, guint16 val);
148
149
150 gboolean fpi_byte_writer_put_int16_be (FpiByteWriter *writer, gint16 val);
151
152
153 gboolean fpi_byte_writer_put_int16_le (FpiByteWriter *writer, gint16 val);
154
155
156 gboolean fpi_byte_writer_put_uint24_be (FpiByteWriter *writer, guint32 val);
157
158
159 gboolean fpi_byte_writer_put_uint24_le (FpiByteWriter *writer, guint32 val);
160
161
162 gboolean fpi_byte_writer_put_int24_be (FpiByteWriter *writer, gint32 val);
163
164
165 gboolean fpi_byte_writer_put_int24_le (FpiByteWriter *writer, gint32 val);
166
167
168 gboolean fpi_byte_writer_put_uint32_be (FpiByteWriter *writer, guint32 val);
169
170
171 gboolean fpi_byte_writer_put_uint32_le (FpiByteWriter *writer, guint32 val);
172
173
174 gboolean fpi_byte_writer_put_int32_be (FpiByteWriter *writer, gint32 val);
175
176
177 gboolean fpi_byte_writer_put_int32_le (FpiByteWriter *writer, gint32 val);
178
179
180 gboolean fpi_byte_writer_put_uint64_be (FpiByteWriter *writer, guint64 val);
181
182
183 gboolean fpi_byte_writer_put_uint64_le (FpiByteWriter *writer, guint64 val);
184
185
186 gboolean fpi_byte_writer_put_int64_be (FpiByteWriter *writer, gint64 val);
187
188
189 gboolean fpi_byte_writer_put_int64_le (FpiByteWriter *writer, gint64 val);
190
191
192 gboolean fpi_byte_writer_put_float32_be (FpiByteWriter *writer, gfloat val);
193
194
195 gboolean fpi_byte_writer_put_float32_le (FpiByteWriter *writer, gfloat val);
196
197
198 gboolean fpi_byte_writer_put_float64_be (FpiByteWriter *writer, gdouble val);
199
200
201 gboolean fpi_byte_writer_put_float64_le (FpiByteWriter *writer, gdouble val);
202
203
204 gboolean fpi_byte_writer_put_data (FpiByteWriter *writer, const guint8 *data, guint size);
205
206
207 gboolean fpi_byte_writer_fill (FpiByteWriter *writer, guint8 value, guint size);
208
209
210 gboolean fpi_byte_writer_put_string_utf8 (FpiByteWriter *writer, const gchar *data);
211
212
213 gboolean fpi_byte_writer_put_string_utf16 (FpiByteWriter *writer, const guint16 *data);
214
215
216 gboolean fpi_byte_writer_put_string_utf32 (FpiByteWriter *writer, const guint32 *data);
217
218 /**
219 * fpi_byte_writer_put_string:
220 * @writer: #FpiByteWriter 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 fpi_byte_writer_put_string(writer, data) \
230 fpi_byte_writer_put_string_utf8(writer, data)
231
232 static inline guint
233 16 fpi_byte_writer_next_pow2 (guint n)
234 {
235 16 guint ret = 16;
236
237 /* We start with 16, smaller allocations make no sense */
238
239
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 16 times.
32 while (ret < n && ret > 0)
240 16 ret <<= 1;
241
242
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 return ret ? ret : n;
243 }
244
245 static inline gboolean
246 2043 fpi_byte_writer_ensure_free_space_inline (FpiByteWriter * writer, guint size)
247 {
248 2043 gpointer data;
249
250
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2027 times.
2043 if (G_LIKELY (size <= writer->alloc_size - writer->parent.byte))
251 return TRUE;
252
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 if (G_UNLIKELY (writer->fixed || !writer->owned))
253 return FALSE;
254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (G_UNLIKELY (writer->parent.byte > G_MAXUINT - size))
255 return FALSE;
256
257 16 writer->alloc_size = fpi_byte_writer_next_pow2 (writer->parent.byte + size);
258 16 data = g_try_realloc ((guint8 *) writer->parent.data, writer->alloc_size);
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (G_UNLIKELY (data == NULL))
260 return FALSE;
261
262 16 writer->parent.data = (guint8 *) data;
263
264 16 return TRUE;
265 }
266
267 #define __FPI_BYTE_WRITER_CREATE_WRITE_FUNC(bits,type,name,write_func) \
268 static inline void \
269 fpi_byte_writer_put_##name##_unchecked (FpiByteWriter *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 fpi_byte_writer_put_##name##_inline (FpiByteWriter *writer, type val) \
281 { \
282 g_return_val_if_fail (writer != NULL, FALSE); \
283 \
284 if (G_UNLIKELY (!fpi_byte_writer_ensure_free_space_inline(writer, bits/8))) \
285 return FALSE; \
286 \
287 fpi_byte_writer_put_##name##_unchecked (writer, val); \
288 \
289 return TRUE; \
290 }
291
292
2/4
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 64 times.
64 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (8, guint8, uint8, FP_WRITE_UINT8)
293 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (8, gint8, int8, FP_WRITE_UINT8)
294 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (16, guint16, uint16_le, FP_WRITE_UINT16_LE)
295
2/4
✓ Branch 0 taken 625 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 625 times.
625 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (16, guint16, uint16_be, FP_WRITE_UINT16_BE)
296 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (16, gint16, int16_le, FP_WRITE_UINT16_LE)
297 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (16, gint16, int16_be, FP_WRITE_UINT16_BE)
298 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (24, guint32, uint24_le, FP_WRITE_UINT24_LE)
299 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (24, guint32, uint24_be, FP_WRITE_UINT24_BE)
300 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (24, gint32, int24_le, FP_WRITE_UINT24_LE)
301 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (24, gint32, int24_be, FP_WRITE_UINT24_BE)
302 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (32, guint32, uint32_le, FP_WRITE_UINT32_LE)
303 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (32, guint32, uint32_be, FP_WRITE_UINT32_BE)
304 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (32, gint32, int32_le, FP_WRITE_UINT32_LE)
305 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (32, gint32, int32_be, FP_WRITE_UINT32_BE)
306 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (64, guint64, uint64_le, FP_WRITE_UINT64_LE)
307 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (64, guint64, uint64_be, FP_WRITE_UINT64_BE)
308 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (64, gint64, int64_le, FP_WRITE_UINT64_LE)
309 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (64, gint64, int64_be, FP_WRITE_UINT64_BE)
310
311 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (32, gfloat, float32_be, FP_WRITE_FLOAT_BE)
312 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (32, gfloat, float32_le, FP_WRITE_FLOAT_LE)
313 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (64, gdouble, float64_be, FP_WRITE_DOUBLE_BE)
314 __FPI_BYTE_WRITER_CREATE_WRITE_FUNC (64, gdouble, float64_le, FP_WRITE_DOUBLE_LE)
315
316 #undef __FPI_BYTE_WRITER_CREATE_WRITE_FUNC
317
318 static inline void
319 1354 fpi_byte_writer_put_data_unchecked (FpiByteWriter * writer, const guint8 * data,
320 guint size)
321 {
322 1354 memcpy ((guint8 *) & writer->parent.data[writer->parent.byte], data, size);
323 1354 writer->parent.byte += size;
324 1354 writer->parent.size = MAX (writer->parent.size, writer->parent.byte);
325 1354 }
326
327 static inline gboolean
328 1354 fpi_byte_writer_put_data_inline (FpiByteWriter * writer, const guint8 * data,
329 guint size)
330 {
331
1/2
✓ Branch 0 taken 1354 times.
✗ Branch 1 not taken.
1354 g_return_val_if_fail (writer != NULL, FALSE);
332
333
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1354 times.
1354 if (G_UNLIKELY (!fpi_byte_writer_ensure_free_space_inline (writer, size)))
334 return FALSE;
335
336 1354 fpi_byte_writer_put_data_unchecked (writer, data, size);
337
338 1354 return TRUE;
339 }
340
341 static inline void
342 fpi_byte_writer_fill_unchecked (FpiByteWriter * 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
350 fpi_byte_writer_fill_inline (FpiByteWriter * writer, guint8 value, guint size)
351 {
352 g_return_val_if_fail (writer != NULL, FALSE);
353
354 if (G_UNLIKELY (!fpi_byte_writer_ensure_free_space_inline (writer, size)))
355 return FALSE;
356
357 fpi_byte_writer_fill_unchecked (writer, value, size);
358
359 return TRUE;
360 }
361
362 #ifndef FPI_BYTE_WRITER_DISABLE_INLINES
363
364 /* we use defines here so we can add the G_LIKELY() */
365
366 #define fpi_byte_writer_ensure_free_space(writer, size) \
367 G_LIKELY (fpi_byte_writer_ensure_free_space_inline (writer, size))
368 #define fpi_byte_writer_put_uint8(writer, val) \
369 G_LIKELY (fpi_byte_writer_put_uint8_inline (writer, val))
370 #define fpi_byte_writer_put_int8(writer, val) \
371 G_LIKELY (fpi_byte_writer_put_int8_inline (writer, val))
372 #define fpi_byte_writer_put_uint16_be(writer, val) \
373 G_LIKELY (fpi_byte_writer_put_uint16_be_inline (writer, val))
374 #define fpi_byte_writer_put_uint16_le(writer, val) \
375 G_LIKELY (fpi_byte_writer_put_uint16_le_inline (writer, val))
376 #define fpi_byte_writer_put_int16_be(writer, val) \
377 G_LIKELY (fpi_byte_writer_put_int16_be_inline (writer, val))
378 #define fpi_byte_writer_put_int16_le(writer, val) \
379 G_LIKELY (fpi_byte_writer_put_int16_le_inline (writer, val))
380 #define fpi_byte_writer_put_uint24_be(writer, val) \
381 G_LIKELY (fpi_byte_writer_put_uint24_be_inline (writer, val))
382 #define fpi_byte_writer_put_uint24_le(writer, val) \
383 G_LIKELY (fpi_byte_writer_put_uint24_le_inline (writer, val))
384 #define fpi_byte_writer_put_int24_be(writer, val) \
385 G_LIKELY (fpi_byte_writer_put_int24_be_inline (writer, val))
386 #define fpi_byte_writer_put_int24_le(writer, val) \
387 G_LIKELY (fpi_byte_writer_put_int24_le_inline (writer, val))
388 #define fpi_byte_writer_put_uint32_be(writer, val) \
389 G_LIKELY (fpi_byte_writer_put_uint32_be_inline (writer, val))
390 #define fpi_byte_writer_put_uint32_le(writer, val) \
391 G_LIKELY (fpi_byte_writer_put_uint32_le_inline (writer, val))
392 #define fpi_byte_writer_put_int32_be(writer, val) \
393 G_LIKELY (fpi_byte_writer_put_int32_be_inline (writer, val))
394 #define fpi_byte_writer_put_int32_le(writer, val) \
395 G_LIKELY (fpi_byte_writer_put_int32_le_inline (writer, val))
396 #define fpi_byte_writer_put_uint64_be(writer, val) \
397 G_LIKELY (fpi_byte_writer_put_uint64_be_inline (writer, val))
398 #define fpi_byte_writer_put_uint64_le(writer, val) \
399 G_LIKELY (fpi_byte_writer_put_uint64_le_inline (writer, val))
400 #define fpi_byte_writer_put_int64_be(writer, val) \
401 G_LIKELY (fpi_byte_writer_put_int64_be_inline (writer, val))
402 #define fpi_byte_writer_put_int64_le(writer, val) \
403 G_LIKELY (fpi_byte_writer_put_int64_le_inline (writer, val))
404
405 #define fpi_byte_writer_put_float32_be(writer, val) \
406 G_LIKELY (fpi_byte_writer_put_float32_be_inline (writer, val))
407 #define fpi_byte_writer_put_float32_le(writer, val) \
408 G_LIKELY (fpi_byte_writer_put_float32_le_inline (writer, val))
409 #define fpi_byte_writer_put_float64_be(writer, val) \
410 G_LIKELY (fpi_byte_writer_put_float64_be_inline (writer, val))
411 #define fpi_byte_writer_put_float64_le(writer, val) \
412 G_LIKELY (fpi_byte_writer_put_float64_le_inline (writer, val))
413
414 #define fpi_byte_writer_put_data(writer, data, size) \
415 G_LIKELY (fpi_byte_writer_put_data_inline (writer, data, size))
416 #define fpi_byte_writer_fill(writer, val, size) \
417 G_LIKELY (fpi_byte_writer_fill_inline (writer, val, size))
418
419 #endif
420
421 G_DEFINE_AUTOPTR_CLEANUP_FUNC (FpiByteWriter, fpi_byte_writer_free);
422 1353 G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC (FpiByteWriter, fpi_byte_writer_reset);
423
424 G_END_DECLS
425