GCC Code Coverage Report


Directory: ./
File: libfprint/fpi-byte-reader.h
Date: 2024-09-16 14:36:32
Exec Total Coverage
Lines: 40 111 36.0%
Functions: 8 70 11.4%
Branches: 21 178 11.8%

Line Branch Exec Source
1 /* GStreamer byte reader
2 *
3 * Copyright (C) 2008 Sebastian Dröge <sebastian.droege@collabora.co.uk>.
4 * Copyright (C) 2009 Tim-Philipp Müller <tim centricular net>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 #pragma once
23
24 #include <glib.h>
25 #include "fpi-compat.h"
26 #include "fpi-byte-utils.h"
27
28 G_BEGIN_DECLS
29
30 #define FPI_BYTE_READER(reader) ((FpiByteReader *) (reader))
31
32 /**
33 * FpiByteReader:
34 * @data: (array length=size): Data from which the bit reader will
35 * read
36 * @size: Size of @data in bytes
37 * @byte: Current byte position
38 *
39 * A byte reader instance.
40 */
41 typedef struct {
42 const guint8 *data;
43 guint size;
44
45 guint byte; /* Byte position */
46
47 /* < private > */
48 } FpiByteReader;
49
50
51 FpiByteReader * fpi_byte_reader_new (const guint8 *data, guint size) G_GNUC_MALLOC;
52
53
54 void fpi_byte_reader_free (FpiByteReader *reader);
55
56
57 void fpi_byte_reader_init (FpiByteReader *reader, const guint8 *data, guint size);
58
59
60 gboolean fpi_byte_reader_peek_sub_reader (FpiByteReader * reader,
61 FpiByteReader * sub_reader,
62 guint size);
63
64 gboolean fpi_byte_reader_get_sub_reader (FpiByteReader * reader,
65 FpiByteReader * sub_reader,
66 guint size);
67
68 gboolean fpi_byte_reader_set_pos (FpiByteReader *reader, guint pos);
69
70
71 guint fpi_byte_reader_get_pos (const FpiByteReader *reader);
72
73
74 guint fpi_byte_reader_get_remaining (const FpiByteReader *reader);
75
76
77 guint fpi_byte_reader_get_size (const FpiByteReader *reader);
78
79
80 gboolean fpi_byte_reader_skip (FpiByteReader *reader, guint nbytes);
81
82
83 gboolean fpi_byte_reader_get_uint8 (FpiByteReader *reader, guint8 *val);
84
85
86 gboolean fpi_byte_reader_get_int8 (FpiByteReader *reader, gint8 *val);
87
88
89 gboolean fpi_byte_reader_get_uint16_le (FpiByteReader *reader, guint16 *val);
90
91
92 gboolean fpi_byte_reader_get_int16_le (FpiByteReader *reader, gint16 *val);
93
94
95 gboolean fpi_byte_reader_get_uint16_be (FpiByteReader *reader, guint16 *val);
96
97
98 gboolean fpi_byte_reader_get_int16_be (FpiByteReader *reader, gint16 *val);
99
100
101 gboolean fpi_byte_reader_get_uint24_le (FpiByteReader *reader, guint32 *val);
102
103
104 gboolean fpi_byte_reader_get_int24_le (FpiByteReader *reader, gint32 *val);
105
106
107 gboolean fpi_byte_reader_get_uint24_be (FpiByteReader *reader, guint32 *val);
108
109
110 gboolean fpi_byte_reader_get_int24_be (FpiByteReader *reader, gint32 *val);
111
112
113 gboolean fpi_byte_reader_get_uint32_le (FpiByteReader *reader, guint32 *val);
114
115
116 gboolean fpi_byte_reader_get_int32_le (FpiByteReader *reader, gint32 *val);
117
118
119 gboolean fpi_byte_reader_get_uint32_be (FpiByteReader *reader, guint32 *val);
120
121
122 gboolean fpi_byte_reader_get_int32_be (FpiByteReader *reader, gint32 *val);
123
124
125 gboolean fpi_byte_reader_get_uint64_le (FpiByteReader *reader, guint64 *val);
126
127
128 gboolean fpi_byte_reader_get_int64_le (FpiByteReader *reader, gint64 *val);
129
130
131 gboolean fpi_byte_reader_get_uint64_be (FpiByteReader *reader, guint64 *val);
132
133
134 gboolean fpi_byte_reader_get_int64_be (FpiByteReader *reader, gint64 *val);
135
136
137 gboolean fpi_byte_reader_peek_uint8 (const FpiByteReader *reader, guint8 *val);
138
139
140 gboolean fpi_byte_reader_peek_int8 (const FpiByteReader *reader, gint8 *val);
141
142
143 gboolean fpi_byte_reader_peek_uint16_le (const FpiByteReader *reader, guint16 *val);
144
145
146 gboolean fpi_byte_reader_peek_int16_le (const FpiByteReader *reader, gint16 *val);
147
148
149 gboolean fpi_byte_reader_peek_uint16_be (const FpiByteReader *reader, guint16 *val);
150
151
152 gboolean fpi_byte_reader_peek_int16_be (const FpiByteReader *reader, gint16 *val);
153
154
155 gboolean fpi_byte_reader_peek_uint24_le (const FpiByteReader *reader, guint32 *val);
156
157
158 gboolean fpi_byte_reader_peek_int24_le (const FpiByteReader *reader, gint32 *val);
159
160
161 gboolean fpi_byte_reader_peek_uint24_be (const FpiByteReader *reader, guint32 *val);
162
163
164 gboolean fpi_byte_reader_peek_int24_be (const FpiByteReader *reader, gint32 *val);
165
166
167 gboolean fpi_byte_reader_peek_uint32_le (const FpiByteReader *reader, guint32 *val);
168
169
170 gboolean fpi_byte_reader_peek_int32_le (const FpiByteReader *reader, gint32 *val);
171
172
173 gboolean fpi_byte_reader_peek_uint32_be (const FpiByteReader *reader, guint32 *val);
174
175
176 gboolean fpi_byte_reader_peek_int32_be (const FpiByteReader *reader, gint32 *val);
177
178
179 gboolean fpi_byte_reader_peek_uint64_le (const FpiByteReader *reader, guint64 *val);
180
181
182 gboolean fpi_byte_reader_peek_int64_le (const FpiByteReader *reader, gint64 *val);
183
184
185 gboolean fpi_byte_reader_peek_uint64_be (const FpiByteReader *reader, guint64 *val);
186
187
188 gboolean fpi_byte_reader_peek_int64_be (const FpiByteReader *reader, gint64 *val);
189
190
191 gboolean fpi_byte_reader_get_float32_le (FpiByteReader *reader, gfloat *val);
192
193
194 gboolean fpi_byte_reader_get_float32_be (FpiByteReader *reader, gfloat *val);
195
196
197 gboolean fpi_byte_reader_get_float64_le (FpiByteReader *reader, gdouble *val);
198
199
200 gboolean fpi_byte_reader_get_float64_be (FpiByteReader *reader, gdouble *val);
201
202
203 gboolean fpi_byte_reader_peek_float32_le (const FpiByteReader *reader, gfloat *val);
204
205
206 gboolean fpi_byte_reader_peek_float32_be (const FpiByteReader *reader, gfloat *val);
207
208
209 gboolean fpi_byte_reader_peek_float64_le (const FpiByteReader *reader, gdouble *val);
210
211
212 gboolean fpi_byte_reader_peek_float64_be (const FpiByteReader *reader, gdouble *val);
213
214
215 gboolean fpi_byte_reader_dup_data (FpiByteReader * reader, guint size, guint8 ** val);
216
217
218 gboolean fpi_byte_reader_get_data (FpiByteReader * reader, guint size, const guint8 ** val);
219
220
221 gboolean fpi_byte_reader_peek_data (const FpiByteReader * reader, guint size, const guint8 ** val);
222
223 #define fpi_byte_reader_dup_string(reader,str) \
224 fpi_byte_reader_dup_string_utf8(reader,str)
225
226
227 gboolean fpi_byte_reader_dup_string_utf8 (FpiByteReader * reader, gchar ** str);
228
229
230 gboolean fpi_byte_reader_dup_string_utf16 (FpiByteReader * reader, guint16 ** str);
231
232
233 gboolean fpi_byte_reader_dup_string_utf32 (FpiByteReader * reader, guint32 ** str);
234
235 #define fpi_byte_reader_skip_string(reader) \
236 fpi_byte_reader_skip_string_utf8(reader)
237
238
239 gboolean fpi_byte_reader_skip_string_utf8 (FpiByteReader * reader);
240
241
242 gboolean fpi_byte_reader_skip_string_utf16 (FpiByteReader * reader);
243
244
245 gboolean fpi_byte_reader_skip_string_utf32 (FpiByteReader * reader);
246
247 #define fpi_byte_reader_get_string(reader,str) \
248 fpi_byte_reader_get_string_utf8(reader,str)
249
250 #define fpi_byte_reader_peek_string(reader,str) \
251 fpi_byte_reader_peek_string_utf8(reader,str)
252
253
254 gboolean fpi_byte_reader_get_string_utf8 (FpiByteReader * reader, const gchar ** str);
255
256
257 gboolean fpi_byte_reader_peek_string_utf8 (const FpiByteReader * reader, const gchar ** str);
258
259
260 guint fpi_byte_reader_masked_scan_uint32 (const FpiByteReader * reader,
261 guint32 mask,
262 guint32 pattern,
263 guint offset,
264 guint size);
265
266 guint fpi_byte_reader_masked_scan_uint32_peek (const FpiByteReader * reader,
267 guint32 mask,
268 guint32 pattern,
269 guint offset,
270 guint size,
271 guint32 * value);
272
273 /**
274 * FPI_BYTE_READER_INIT:
275 * @data: Data from which the #FpiByteReader should read
276 * @size: Size of @data in bytes
277 *
278 * A #FpiByteReader must be initialized with this macro, before it can be
279 * used. This macro can used be to initialize a variable, but it cannot
280 * be assigned to a variable. In that case you have to use
281 * fpi_byte_reader_init().
282 */
283 #define FPI_BYTE_READER_INIT(data, size) {data, size, 0}
284
285 /* unchecked variants */
286 static inline void
287 73 fpi_byte_reader_skip_unchecked (FpiByteReader * reader, guint nbytes)
288 {
289 73 reader->byte += nbytes;
290 }
291
292 #define __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(bits,type,lower,upper,adj) \
293 \
294 static inline type \
295 fpi_byte_reader_peek_##lower##_unchecked (const FpiByteReader * reader) \
296 { \
297 type val = (type) FP_READ_##upper (reader->data + reader->byte); \
298 adj \
299 return val; \
300 } \
301 \
302 static inline type \
303 fpi_byte_reader_get_##lower##_unchecked (FpiByteReader * reader) \
304 { \
305 type val = fpi_byte_reader_peek_##lower##_unchecked (reader); \
306 reader->byte += bits / 8; \
307 return val; \
308 }
309
310 10 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(8,guint8,uint8,UINT8,/* */)
311 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(8,gint8,int8,UINT8,/* */)
312
313 2 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(16,guint16,uint16_le,UINT16_LE,/* */)
314 7195 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(16,guint16,uint16_be,UINT16_BE,/* */)
315 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(16,gint16,int16_le,UINT16_LE,/* */)
316 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(16,gint16,int16_be,UINT16_BE,/* */)
317
318 4 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(32,guint32,uint32_le,UINT32_LE,/* */)
319 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(32,guint32,uint32_be,UINT32_BE,/* */)
320 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(32,gint32,int32_le,UINT32_LE,/* */)
321 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(32,gint32,int32_be,UINT32_BE,/* */)
322
323 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(24,guint32,uint24_le,UINT24_LE,/* */)
324 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(24,guint32,uint24_be,UINT24_BE,/* */)
325
326 /* fix up the sign for 24-bit signed ints stored in 32-bit signed ints */
327 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(24,gint32,int24_le,UINT24_LE,
328 if (val & 0x00800000) val |= 0xff000000;)
329 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(24,gint32,int24_be,UINT24_BE,
330 if (val & 0x00800000) val |= 0xff000000;)
331
332 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(64,guint64,uint64_le,UINT64_LE,/* */)
333 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(64,guint64,uint64_be,UINT64_BE,/* */)
334 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(64,gint64,int64_le,UINT64_LE,/* */)
335 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(64,gint64,int64_be,UINT64_BE,/* */)
336
337 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(32,gfloat,float32_le,FLOAT_LE,/* */)
338 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(32,gfloat,float32_be,FLOAT_BE,/* */)
339 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(64,gdouble,float64_le,DOUBLE_LE,/* */)
340 __FPI_BYTE_READER_GET_PEEK_BITS_UNCHECKED(64,gdouble,float64_be,DOUBLE_BE,/* */)
341
342 #undef __GET_PEEK_BITS_UNCHECKED
343
344 static inline const guint8 *
345 73 fpi_byte_reader_peek_data_unchecked (const FpiByteReader * reader)
346 {
347 73 return (const guint8 *) (reader->data + reader->byte);
348 }
349
350 static inline const guint8 *
351 73 fpi_byte_reader_get_data_unchecked (FpiByteReader * reader, guint size)
352 {
353 73 const guint8 *data;
354
355 73 data = fpi_byte_reader_peek_data_unchecked (reader);
356 73 fpi_byte_reader_skip_unchecked (reader, size);
357 73 return data;
358 }
359
360 static inline guint8 *
361 fpi_byte_reader_dup_data_unchecked (FpiByteReader * reader, guint size)
362 {
363 gconstpointer data = fpi_byte_reader_get_data_unchecked (reader, size);
364 return (guint8 *) g_memdup2 (data, size);
365 }
366
367 /* Unchecked variants that should not be used */
368 static inline guint
369 709 fpi_byte_reader_get_pos_unchecked (const FpiByteReader * reader)
370 {
371 709 return reader->byte;
372 }
373
374 static inline guint
375 7955 fpi_byte_reader_get_remaining_unchecked (const FpiByteReader * reader)
376 {
377 7955 return reader->size - reader->byte;
378 }
379
380 static inline guint
381 8 fpi_byte_reader_get_size_unchecked (const FpiByteReader * reader)
382 {
383 8 return reader->size;
384 }
385
386 /* inlined variants (do not use directly) */
387
388 static inline guint
389 fpi_byte_reader_get_remaining_inline (const FpiByteReader * reader)
390 {
391 g_return_val_if_fail (reader != NULL, 0);
392
393 return fpi_byte_reader_get_remaining_unchecked (reader);
394 }
395
396 static inline guint
397 8 fpi_byte_reader_get_size_inline (const FpiByteReader * reader)
398 {
399
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 g_return_val_if_fail (reader != NULL, 0);
400
401 8 return fpi_byte_reader_get_size_unchecked (reader);
402 }
403
404 #define __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(bits,type,name) \
405 \
406 static inline gboolean \
407 fpi_byte_reader_peek_##name##_inline (const FpiByteReader * reader, type * val) \
408 { \
409 g_return_val_if_fail (reader != NULL, FALSE); \
410 g_return_val_if_fail (val != NULL, FALSE); \
411 \
412 if (fpi_byte_reader_get_remaining_unchecked (reader) < (bits / 8)) \
413 return FALSE; \
414 \
415 *val = fpi_byte_reader_peek_##name##_unchecked (reader); \
416 return TRUE; \
417 } \
418 \
419 static inline gboolean \
420 fpi_byte_reader_get_##name##_inline (FpiByteReader * reader, type * val) \
421 { \
422 g_return_val_if_fail (reader != NULL, FALSE); \
423 g_return_val_if_fail (val != NULL, FALSE); \
424 \
425 if (fpi_byte_reader_get_remaining_unchecked (reader) < (bits / 8)) \
426 return FALSE; \
427 \
428 *val = fpi_byte_reader_get_##name##_unchecked (reader); \
429 return TRUE; \
430 }
431
432
3/6
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 10 times.
10 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(8,guint8,uint8)
433 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(8,gint8,int8)
434
435
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
2 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(16,guint16,uint16_le)
436
4/6
✓ Branch 0 taken 7820 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7820 times.
✓ Branch 6 taken 625 times.
✓ Branch 7 taken 7195 times.
7820 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(16,guint16,uint16_be)
437 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(16,gint16,int16_le)
438 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(16,gint16,int16_be)
439
440
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 2 times.
2 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(32,guint32,uint32_le)
441 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(32,guint32,uint32_be)
442 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(32,gint32,int32_le)
443 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(32,gint32,int32_be)
444
445 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(24,guint32,uint24_le)
446 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(24,guint32,uint24_be)
447 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(24,gint32,int24_le)
448 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(24,gint32,int24_be)
449
450 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(64,guint64,uint64_le)
451 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(64,guint64,uint64_be)
452 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(64,gint64,int64_le)
453 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(64,gint64,int64_be)
454
455 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(32,gfloat,float32_le)
456 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(32,gfloat,float32_be)
457 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(64,gdouble,float64_le)
458 __FPI_BYTE_READER_GET_PEEK_BITS_INLINE(64,gdouble,float64_be)
459
460 #undef __FPI_BYTE_READER_GET_PEEK_BITS_INLINE
461
462 #ifndef FPI_BYTE_READER_DISABLE_INLINES
463
464 #define fpi_byte_reader_init(reader,data,size) \
465 fpi_byte_reader_init_inline(reader,data,size)
466
467 #define fpi_byte_reader_get_remaining(reader) \
468 fpi_byte_reader_get_remaining_inline(reader)
469
470 #define fpi_byte_reader_get_size(reader) \
471 fpi_byte_reader_get_size_inline(reader)
472
473 #define fpi_byte_reader_get_pos(reader) \
474 fpi_byte_reader_get_pos_inline(reader)
475
476 /* we use defines here so we can add the G_LIKELY() */
477 #define fpi_byte_reader_get_uint8(reader,val) \
478 G_LIKELY(fpi_byte_reader_get_uint8_inline(reader,val))
479 #define fpi_byte_reader_get_int8(reader,val) \
480 G_LIKELY(fpi_byte_reader_get_int8_inline(reader,val))
481 #define fpi_byte_reader_get_uint16_le(reader,val) \
482 G_LIKELY(fpi_byte_reader_get_uint16_le_inline(reader,val))
483 #define fpi_byte_reader_get_int16_le(reader,val) \
484 G_LIKELY(fpi_byte_reader_get_int16_le_inline(reader,val))
485 #define fpi_byte_reader_get_uint16_be(reader,val) \
486 G_LIKELY(fpi_byte_reader_get_uint16_be_inline(reader,val))
487 #define fpi_byte_reader_get_int16_be(reader,val) \
488 G_LIKELY(fpi_byte_reader_get_int16_be_inline(reader,val))
489 #define fpi_byte_reader_get_uint24_le(reader,val) \
490 G_LIKELY(fpi_byte_reader_get_uint24_le_inline(reader,val))
491 #define fpi_byte_reader_get_int24_le(reader,val) \
492 G_LIKELY(fpi_byte_reader_get_int24_le_inline(reader,val))
493 #define fpi_byte_reader_get_uint24_be(reader,val) \
494 G_LIKELY(fpi_byte_reader_get_uint24_be_inline(reader,val))
495 #define fpi_byte_reader_get_int24_be(reader,val) \
496 G_LIKELY(fpi_byte_reader_get_int24_be_inline(reader,val))
497 #define fpi_byte_reader_get_uint32_le(reader,val) \
498 G_LIKELY(fpi_byte_reader_get_uint32_le_inline(reader,val))
499 #define fpi_byte_reader_get_int32_le(reader,val) \
500 G_LIKELY(fpi_byte_reader_get_int32_le_inline(reader,val))
501 #define fpi_byte_reader_get_uint32_be(reader,val) \
502 G_LIKELY(fpi_byte_reader_get_uint32_be_inline(reader,val))
503 #define fpi_byte_reader_get_int32_be(reader,val) \
504 G_LIKELY(fpi_byte_reader_get_int32_be_inline(reader,val))
505 #define fpi_byte_reader_get_uint64_le(reader,val) \
506 G_LIKELY(fpi_byte_reader_get_uint64_le_inline(reader,val))
507 #define fpi_byte_reader_get_int64_le(reader,val) \
508 G_LIKELY(fpi_byte_reader_get_int64_le_inline(reader,val))
509 #define fpi_byte_reader_get_uint64_be(reader,val) \
510 G_LIKELY(fpi_byte_reader_get_uint64_be_inline(reader,val))
511 #define fpi_byte_reader_get_int64_be(reader,val) \
512 G_LIKELY(fpi_byte_reader_get_int64_be_inline(reader,val))
513
514 #define fpi_byte_reader_peek_uint8(reader,val) \
515 G_LIKELY(fpi_byte_reader_peek_uint8_inline(reader,val))
516 #define fpi_byte_reader_peek_int8(reader,val) \
517 G_LIKELY(fpi_byte_reader_peek_int8_inline(reader,val))
518 #define fpi_byte_reader_peek_uint16_le(reader,val) \
519 G_LIKELY(fpi_byte_reader_peek_uint16_le_inline(reader,val))
520 #define fpi_byte_reader_peek_int16_le(reader,val) \
521 G_LIKELY(fpi_byte_reader_peek_int16_le_inline(reader,val))
522 #define fpi_byte_reader_peek_uint16_be(reader,val) \
523 G_LIKELY(fpi_byte_reader_peek_uint16_be_inline(reader,val))
524 #define fpi_byte_reader_peek_int16_be(reader,val) \
525 G_LIKELY(fpi_byte_reader_peek_int16_be_inline(reader,val))
526 #define fpi_byte_reader_peek_uint24_le(reader,val) \
527 G_LIKELY(fpi_byte_reader_peek_uint24_le_inline(reader,val))
528 #define fpi_byte_reader_peek_int24_le(reader,val) \
529 G_LIKELY(fpi_byte_reader_peek_int24_le_inline(reader,val))
530 #define fpi_byte_reader_peek_uint24_be(reader,val) \
531 G_LIKELY(fpi_byte_reader_peek_uint24_be_inline(reader,val))
532 #define fpi_byte_reader_peek_int24_be(reader,val) \
533 G_LIKELY(fpi_byte_reader_peek_int24_be_inline(reader,val))
534 #define fpi_byte_reader_peek_uint32_le(reader,val) \
535 G_LIKELY(fpi_byte_reader_peek_uint32_le_inline(reader,val))
536 #define fpi_byte_reader_peek_int32_le(reader,val) \
537 G_LIKELY(fpi_byte_reader_peek_int32_le_inline(reader,val))
538 #define fpi_byte_reader_peek_uint32_be(reader,val) \
539 G_LIKELY(fpi_byte_reader_peek_uint32_be_inline(reader,val))
540 #define fpi_byte_reader_peek_int32_be(reader,val) \
541 G_LIKELY(fpi_byte_reader_peek_int32_be_inline(reader,val))
542 #define fpi_byte_reader_peek_uint64_le(reader,val) \
543 G_LIKELY(fpi_byte_reader_peek_uint64_le_inline(reader,val))
544 #define fpi_byte_reader_peek_int64_le(reader,val) \
545 G_LIKELY(fpi_byte_reader_peek_int64_le_inline(reader,val))
546 #define fpi_byte_reader_peek_uint64_be(reader,val) \
547 G_LIKELY(fpi_byte_reader_peek_uint64_be_inline(reader,val))
548 #define fpi_byte_reader_peek_int64_be(reader,val) \
549 G_LIKELY(fpi_byte_reader_peek_int64_be_inline(reader,val))
550
551 #define fpi_byte_reader_get_float32_le(reader,val) \
552 G_LIKELY(fpi_byte_reader_get_float32_le_inline(reader,val))
553 #define fpi_byte_reader_get_float32_be(reader,val) \
554 G_LIKELY(fpi_byte_reader_get_float32_be_inline(reader,val))
555 #define fpi_byte_reader_get_float64_le(reader,val) \
556 G_LIKELY(fpi_byte_reader_get_float64_le_inline(reader,val))
557 #define fpi_byte_reader_get_float64_be(reader,val) \
558 G_LIKELY(fpi_byte_reader_get_float64_be_inline(reader,val))
559 #define fpi_byte_reader_peek_float32_le(reader,val) \
560 G_LIKELY(fpi_byte_reader_peek_float32_le_inline(reader,val))
561 #define fpi_byte_reader_peek_float32_be(reader,val) \
562 G_LIKELY(fpi_byte_reader_peek_float32_be_inline(reader,val))
563 #define fpi_byte_reader_peek_float64_le(reader,val) \
564 G_LIKELY(fpi_byte_reader_peek_float64_le_inline(reader,val))
565 #define fpi_byte_reader_peek_float64_be(reader,val) \
566 G_LIKELY(fpi_byte_reader_peek_float64_be_inline(reader,val))
567
568 #endif /* FPI_BYTE_READER_DISABLE_INLINES */
569
570 static inline void
571 61 fpi_byte_reader_init_inline (FpiByteReader * reader, const guint8 * data, guint size)
572 {
573 61 g_return_if_fail (reader != NULL);
574
575 61 reader->data = data;
576 61 reader->size = size;
577 61 reader->byte = 0;
578 }
579
580 static inline gboolean
581 fpi_byte_reader_peek_sub_reader_inline (FpiByteReader * reader,
582 FpiByteReader * sub_reader, guint size)
583 {
584 g_return_val_if_fail (reader != NULL, FALSE);
585 g_return_val_if_fail (sub_reader != NULL, FALSE);
586
587 if (fpi_byte_reader_get_remaining_unchecked (reader) < size)
588 return FALSE;
589
590 sub_reader->data = reader->data + reader->byte;
591 sub_reader->byte = 0;
592 sub_reader->size = size;
593 return TRUE;
594 }
595
596 static inline gboolean
597 fpi_byte_reader_get_sub_reader_inline (FpiByteReader * reader,
598 FpiByteReader * sub_reader, guint size)
599 {
600 if (!fpi_byte_reader_peek_sub_reader_inline (reader, sub_reader, size))
601 return FALSE;
602 fpi_byte_reader_skip_unchecked (reader, size);
603 return TRUE;
604 }
605
606 static inline gboolean
607 fpi_byte_reader_dup_data_inline (FpiByteReader * reader, guint size, guint8 ** val)
608 {
609 g_return_val_if_fail (reader != NULL, FALSE);
610 g_return_val_if_fail (val != NULL, FALSE);
611
612 if (G_UNLIKELY (size > reader->size || fpi_byte_reader_get_remaining_unchecked (reader) < size))
613 return FALSE;
614
615 *val = fpi_byte_reader_dup_data_unchecked (reader, size);
616 return TRUE;
617 }
618
619 static inline gboolean
620 133 fpi_byte_reader_get_data_inline (FpiByteReader * reader, guint size, const guint8 ** val)
621 {
622
1/2
✓ Branch 0 taken 133 times.
✗ Branch 1 not taken.
133 g_return_val_if_fail (reader != NULL, FALSE);
623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 133 times.
133 g_return_val_if_fail (val != NULL, FALSE);
624
625
4/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 121 times.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 73 times.
133 if (G_UNLIKELY (size > reader->size || fpi_byte_reader_get_remaining_unchecked (reader) < size))
626 return FALSE;
627
628 73 *val = fpi_byte_reader_get_data_unchecked (reader, size);
629 73 return TRUE;
630 }
631
632 static inline gboolean
633 fpi_byte_reader_peek_data_inline (const FpiByteReader * reader, guint size, const guint8 ** val)
634 {
635 g_return_val_if_fail (reader != NULL, FALSE);
636 g_return_val_if_fail (val != NULL, FALSE);
637
638 if (G_UNLIKELY (size > reader->size || fpi_byte_reader_get_remaining_unchecked (reader) < size))
639 return FALSE;
640
641 *val = fpi_byte_reader_peek_data_unchecked (reader);
642 return TRUE;
643 }
644
645 static inline guint
646 709 fpi_byte_reader_get_pos_inline (const FpiByteReader * reader)
647 {
648
1/2
✓ Branch 0 taken 709 times.
✗ Branch 1 not taken.
709 g_return_val_if_fail (reader != NULL, 0);
649
650 709 return fpi_byte_reader_get_pos_unchecked (reader);
651 }
652
653 static inline gboolean
654 fpi_byte_reader_skip_inline (FpiByteReader * reader, guint nbytes)
655 {
656 g_return_val_if_fail (reader != NULL, FALSE);
657
658 if (G_UNLIKELY (fpi_byte_reader_get_remaining_unchecked (reader) < nbytes))
659 return FALSE;
660
661 reader->byte += nbytes;
662 return TRUE;
663 }
664
665 #ifndef FPI_BYTE_READER_DISABLE_INLINES
666
667 #define fpi_byte_reader_dup_data(reader,size,val) \
668 G_LIKELY(fpi_byte_reader_dup_data_inline(reader,size,val))
669 #define fpi_byte_reader_get_data(reader,size,val) \
670 G_LIKELY(fpi_byte_reader_get_data_inline(reader,size,val))
671 #define fpi_byte_reader_peek_data(reader,size,val) \
672 G_LIKELY(fpi_byte_reader_peek_data_inline(reader,size,val))
673 #define fpi_byte_reader_skip(reader,nbytes) \
674 G_LIKELY(fpi_byte_reader_skip_inline(reader,nbytes))
675
676 #endif /* FPI_BYTE_READER_DISABLE_INLINES */
677
678 G_DEFINE_AUTOPTR_CLEANUP_FUNC (FpiByteReader, fpi_byte_reader_free);
679
680 G_END_DECLS
681