fpi-byte-reader: Add support to read to a static buffer

This works through macros when the size of a buffer is statically
defined
This commit is contained in:
Marco Trevisan (Treviño)
2026-06-08 14:16:36 +02:00
committed by Marco Trevisan
parent dc48ab8b40
commit f6a8cae5b4
3 changed files with 94 additions and 0 deletions
+2
View File
@@ -198,6 +198,8 @@ fpi_byte_reader_get_float64_be
fpi_byte_reader_peek_float64_be
fpi_byte_reader_get_data
fpi_byte_reader_peek_data
fpi_byte_reader_get_data_static
fpi_byte_reader_peek_data_static
fpi_byte_reader_dup_data
fpi_byte_reader_masked_scan_uint32
fpi_byte_reader_masked_scan_uint32_peek
+35
View File
@@ -848,6 +848,41 @@ fpi_byte_reader_peek_data (const FpiByteReader * reader, guint size,
return fpi_byte_reader_peek_data_inline (reader, size, val);
}
/**
* fpi_byte_reader_get_data_fixed:
* @reader: a #FpiByteReader instance
* @size: Size in bytes
* @val: (out) (array length=size): address of a buffer to copy data into
*
* Copies @size bytes from the current data position into @val if at
* least @size bytes are left, and updates the current position.
*
* Returns: %TRUE if successful, %FALSE otherwise.
*/
gboolean
(fpi_byte_reader_get_data_static) (FpiByteReader * reader, guint size,
const guint8 * val)
{
return (fpi_byte_reader_get_data_inline_static) (reader, size, val);
}
/**
* fpi_byte_reader_peek_data_fixed:
* @reader: a #FpiByteReader instance
* @size: Size in bytes
* @val: (out) (array length=size): address of a buffer to copy data into
*
* Like fpi_byte_reader_get_data_fixed() but does not advance the position.
*
* Returns: %TRUE if successful, %FALSE otherwise.
*/
gboolean
(fpi_byte_reader_peek_data_static) (const FpiByteReader * reader, guint size,
guint8 * val)
{
return (fpi_byte_reader_peek_data_inline_static) (reader, size, val);
}
/**
* fpi_byte_reader_dup_data:
* @reader: a #FpiByteReader instance
+57
View File
@@ -222,9 +222,15 @@ gboolean fpi_byte_reader_dup_data (FpiByteReader * reader, guint s
gboolean fpi_byte_reader_get_data (FpiByteReader * reader, guint size, const guint8 ** val);
gboolean fpi_byte_reader_get_data_static (FpiByteReader * reader, guint size, const guint8 * val);
gboolean fpi_byte_reader_peek_data (const FpiByteReader * reader, guint size, const guint8 ** val);
gboolean fpi_byte_reader_peek_data_static (const FpiByteReader * reader, guint size, guint8 * val);
GBytes * fpi_byte_reader_get_bytes (FpiByteReader *reader, guint size);
@@ -681,6 +687,32 @@ fpi_byte_reader_get_bytes_inline (FpiByteReader *reader, guint size)
return g_bytes_new_static (data, size);
}
static inline gboolean
(fpi_byte_reader_get_data_inline_static) (FpiByteReader * reader, guint size, const guint8 * val)
{
g_return_val_if_fail (reader != NULL, FALSE);
g_return_val_if_fail (val != NULL, FALSE);
if (G_UNLIKELY (size > reader->size || fpi_byte_reader_get_remaining_unchecked (reader) < size))
return FALSE;
memcpy ((void *) val, fpi_byte_reader_get_data_unchecked (reader, size), size);
return TRUE;
}
static inline gboolean
(fpi_byte_reader_peek_data_inline_static) (const FpiByteReader * reader, guint size, guint8 * val)
{
g_return_val_if_fail (reader != NULL, FALSE);
g_return_val_if_fail (val != NULL, FALSE);
if (G_UNLIKELY (size > reader->size || fpi_byte_reader_get_remaining_unchecked (reader) < size))
return FALSE;
memcpy (val, fpi_byte_reader_peek_data_unchecked (reader), size);
return TRUE;
}
static inline guint
fpi_byte_reader_get_pos_inline (const FpiByteReader * reader)
{
@@ -716,6 +748,31 @@ fpi_byte_reader_skip_inline (FpiByteReader * reader, guint nbytes)
#define fpi_byte_reader_peek_bytes(reader,size) \
fpi_byte_reader_peek_bytes_inline(reader,size)
/**
* fpi_byte_reader_get_data_static:
* @reader: a #FpiByteReader
* @val: fixed-size array (e.g. `uint8_t buf[32]`)
*
* Reads @size bytes from @reader directly into @val, where @size is
* deduced via `sizeof()` - only safe with true C arrays.
*
* Returns: %TRUE on success, %FALSE otherwise.
*/
#define fpi_byte_reader_get_data_static(reader,val) \
(fpi_byte_reader_get_data_static) (reader,sizeof(val),val)
/**
* fpi_byte_reader_peek_data_static:
* @reader: a #FpiByteReader
* @val: fixed-size array (e.g. `uint8_t buf[32]`)
*
* Like fpi_byte_reader_get_data_static() but does not advance the position.
*
* Returns: %TRUE on success, %FALSE otherwise.
*/
#define fpi_byte_reader_peek_data_static(reader,val) \
(fpi_byte_reader_peek_data_static) (reader,sizeof(val),val)
#endif /* FPI_BYTE_READER_DISABLE_INLINES */
G_DEFINE_AUTOPTR_CLEANUP_FUNC (FpiByteReader, fpi_byte_reader_free);