SofaBuffers C/C++ 0.12.0
API Documentation
Loading...
Searching...
No Matches
istream.h
Go to the documentation of this file.
1/*!
2 * @file istream.h
3 * @brief SofaBuffers C - Input stream decoder for Sofab messages.
4 *
5 * This module implements a streaming decoder for SofaBuffers (Sofab), a compact,
6 * TLV-like (Type-Length-Value) serialization format. The decoder accepts data
7 * incrementally through @ref sofab_istream_feed, interprets field headers,
8 * and automatically invokes user-provided field callbacks when new fields
9 * begin. Within these callbacks, the user must select an appropriate
10 * sofab_istream_read_*() function to bind a destination buffer or to start
11 * decoding nested sequences.
12 *
13 * Typical usage:
14 * - Initialize a sofab_istream with a user context and a field callback.
15 * - Feed incoming data incrementally using sofab_istream_feed().
16 * - Dispatch fields in the callback and select the appropriate sofab_istream_read_*() function for reception.
17 * - Continue feeding data until the message is fully consumed.
18 * - After all data has been fed, the message data is available in the user-provided buffers.
19 *
20 * SPDX-License-Identifier: MIT
21 */
22
23#ifndef SOFAB_ISTREAM_H
24#define SOFAB_ISTREAM_H
25
26/**
27 * @defgroup c_api C API
28 * @{
29 */
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35#ifndef SOFAB_ISTREAM_C
36# define SOFAB_ISTREAM_EXTERN extern
37#else
38# define SOFAB_ISTREAM_EXTERN
39#endif
40
41/* includes *******************************************************************/
42#include <stdlib.h>
43#include <stdint.h>
44#include <stdbool.h>
45#include <string.h>
46
47#include "sofab/sofab.h"
48
49/* macros *********************************************************************/
50/*
51 * Layout of @ref sofab_istream_t::target_opt (one byte):
52 *
53 * bits 0-2 field type (@ref sofab_type_t)
54 * bits 3-5 fixlen subtype (@ref sofab_fixlentype_t)
55 * bit 6 STRINGTERM — NUL-terminate the string written to the target
56 * bit 7 BOOLEAN — the target is a boolean, not an integer
57 *
58 * The low six bits are what the §7.3 tag test compares against the wire, so the
59 * two flags deliberately sit outside that 0x3F mask: neither changes which wire
60 * type the field must have, only what the decoder does with the value.
61 */
62#define SOFAB_ISTREAM_OPT_FIELDTYPE(type) ((type) & 0x07)
63#define SOFAB_ISTREAM_OPT_FIXLENTYPE(type) (((type) & 0x07) << 3)
64#define SOFAB_ISTREAM_OPT_STRINGTERM (0x40)
65/*! Store the decoded value as a boolean: `value != 0`, and no width bound.
66 * A boolean rides the unsigned varint wire type (CORELIB_PLAN §4.4), so this
67 * flag accompanies SOFAB_TYPE_VARINT_UNSIGNED / _VARINTARRAY_UNSIGNED. */
68#define SOFAB_ISTREAM_OPT_BOOLEAN (0x80)
69
70/* types **********************************************************************/
71
72/*!
73 * @brief Opaque Sofab input stream context.
74 *
75 * This structure contains all state related to the incremental parsing of
76 * Sofab-encoded data. Users should not access its fields directly; instead,
77 * they interact with it through the provided API.
78 */
80
81/*!
82 * @brief Field callback invoked whenever a new field ID is decoded.
83 *
84 * The callback is triggered once the decoder has determined a complete field
85 * ID. The user must then call the appropriate sofab_istream_read_* function
86 * to bind a destination buffer or initiate a nested sequence.
87 * If no read function is called, the field will be skipped.
88 *
89 * Binding a read whose type contradicts the field on the wire skips the field
90 * too (MESSAGE_SPEC §7.3): the destination is left untouched and decoding
91 * continues, since an id that means different things to the two peers is a
92 * disagreement between them, not a malformed message. @ref sofab_istream_skipped
93 * counts those skips.
94 *
95 * Example usage:
96 * @code
97 * void my_field_cb(sofab_istream_t *ctx, sofab_id_t id,
98 * size_t size, size_t count, void *usrptr) {
99 * switch(id) {
100 * case FIELD_TEMP: sofab_istream_read_fp32(ctx, &state.temperature); break;
101 * case FIELD_NAME: sofab_istream_read_string(ctx, buffer, sizeof(buffer)); break;
102 * case FIELD_CHILD: sofab_istream_read_sequence(ctx, &child_decoder, child_cb, NULL); break;
103 * }
104 * }
105 * @endcode
106 *
107 * @param ctx Pointer to the active input stream context.
108 * @param id Field ID that was encountered.
109 * @param size Size of the field's value in bytes (for fixed-length types).
110 * @param count Number of elements (for array types).
111 * @param usrptr Optional user-provided pointer (provided at initialization).
112 */
114 sofab_istream_t *ctx, sofab_id_t id, size_t size, size_t count, void *usrptr);
115
116/*!
117 * @brief Decoder context used for Sofab sequences.
118 *
119 * Each sequence has its own decoder instance. Decoders form a parent-child
120 * chain and inherit the decoding state related to sequence depth and skip levels.
121 */
123{
124 struct sofab_istream_decoder *parent; /*!< Pointer to parent decoder for nested levels */
125 sofab_istream_field_cb_t field_callback; /*!< User callback for handling field IDs */
126 void *usrptr; /*!< Optional user pointer for the field callback */
127 uint8_t state; /*!< Internal parsing state machine */
128 uint8_t skip_depth; /*!< Counter for skipped nested fields */
130
131/*!
132 * @brief Internal state of the Sofab input stream.
133 */
135{
136 /* Members are grouped by alignment (widest first: value-word / pointers /
137 * size_t, then the 32-bit id, then the 8-bit flags) so the context packs
138 * without internal padding — the struct is opaque, so field order carries no
139 * meaning and this keeps its footprint minimal on every target.
140 *
141 * The length/count members are size_t: they hold byte lengths and element
142 * counts bounded by SOFAB_FIXLEN_MAX / SOFAB_ARRAY_MAX (both derived from
143 * SIZE_MAX) and are fed straight from the size_t read-function arguments, so
144 * they mirror that width exactly and need no narrowing cast — smaller than a
145 * fixed uint32_t on 16-bit-size_t targets. */
146 sofab_unsigned_t varint_value; /*!< Accumulated varint value under construction */
147 sofab_istream_decoder_t default_decoder; /*!< Top-level decoder instance */
148 uint8_t *target_ptr; /*!< Pointer to output buffer for field data */
149#if SOFAB_STRICT_UTF8
150 uint8_t *utf8_start; /*!< Start of a materialized string payload to
151 *!< UTF-8-validate at completion; NULL = don't
152 *!< validate (non-string, skipped, or empty). */
153#endif
154 sofab_istream_decoder_t *decoder; /*!< Currently active decoder (may be nested) */
155 size_t fixlen_remaining; /*!< Remaining bytes to read for a fixed-length field */
156 size_t target_len; /*!< Target element size or total buffer length */
157 size_t target_count; /*!< Number of elements to read into the target array */
158 size_t array_wire_count; /*!< Element count declared on the wire (0..capacity) */
159 sofab_id_t id; /*!< Current field ID being processed */
160 uint8_t target_opt; /*!< Field options (used for type checks and flags) */
161 uint8_t varint_shift; /*!< Current shift offset for varint decoding */
162 uint8_t invalid; /*!< Sticky flag: the message was rejected — by the
163 *!< decoder or by a callback. Makes SOFAB_RET_E_INVALID_MSG
164 *!< terminal (CORELIB_PLAN §5.2). */
165#if !defined(SOFAB_DISABLE_SEQUENCE_SUPPORT)
166 uint8_t depth; /*!< Sequences currently open, bound and skipped
167 *!< alike (0..SOFAB_MAX_DEPTH). The per-decoder
168 *!< skip_depth counts only the skipped ones, so
169 *!< it cannot carry the MAX_DEPTH ceiling. */
170#endif
171#if SOFAB_SKIP_COUNTER
172 uint8_t skipped; /*!< Saturating count of type-contradicting fields
173 *!< skipped per MESSAGE_SPEC 7.3
174 *!< (@ref sofab_istream_skipped) */
175#endif
176};
177
178/* prototypes *****************************************************************/
179
180/*!
181 * @brief Initializes a Sofab input stream context.
182 *
183 * Resets all decoder state, assigns the default decoder, and registers the
184 * callback that will be triggered for each incoming field ID at the top level sequence.
185 *
186 * @param ctx Pointer to the input stream context.
187 * @param field_callback User callback invoked for each field ID.
188 * @param usrptr Optional user pointer passed to the callback.
189 */
191 sofab_istream_t *ctx, sofab_istream_field_cb_t field_callback, void *usrptr);
192
193/*!
194 * @brief Feeds raw Sofab-encoded bytes into the input stream.
195 *
196 * The decoder consumes the buffer incrementally. Field callbacks are triggered
197 * when field headers are parsed. If the callback binds a destination buffer
198 * via a sofab_istream_read_* function, subsequent bytes are written there.
199 *
200 * The decoder is resumable, so a call may end mid-field. The outcome is
201 * three-valued (there is no separate finalize step):
202 * - @ref SOFAB_RET_OK: the consumed bytes end exactly on a field boundary — a
203 * complete message so far.
204 * - @ref SOFAB_RET_INCOMPLETE: the consumed bytes end inside a field (a partial
205 * varint, a fixlen/array payload shorter than declared) or with an open
206 * sequence. This is a valid but partial decode, NOT an error: the caller owns
207 * end-of-input and may feed more bytes to continue. Truncated input is
208 * reported here — it is neither accepted as complete nor rejected as invalid.
209 * - @ref SOFAB_RET_E_INVALID_MSG: the bytes are malformed regardless of what
210 * follows (varint too wide, length/count/id over the limit, bad type, ...), or
211 * they use a wire construct this build was compiled without (see below).
212 *
213 * **INVALID is terminal and sticky (normative).** CORELIB_PLAN §5.2: no
214 * continuation of bytes can make rejected input valid. Once this function returns
215 * @ref SOFAB_RET_E_INVALID_MSG it returns it for every subsequent call on the same
216 * context, no further field callback fires, and the only way forward is
217 * @ref sofab_istream_init. That is enforcement, not just documentation: the
218 * decoder stops mid-message but keeps its position, so resuming would
219 * resynchronize on the *payload* of the field it just refused and deliver those
220 * bytes as if they were new field headers.
221 *
222 * The rejection does @b not un-deliver what already arrived. Fields decoded before
223 * the offending one are already in the caller's destinations — a streaming decoder
224 * cannot know a later field will be refused. So @ref SOFAB_RET_E_INVALID_MSG
225 * condemns the whole @b message: discard everything this stream wrote, do not use
226 * the partially-filled destinations.
227 *
228 * **A build with features compiled out rejects what it cannot decode.** Defining
229 * @c SOFAB_DISABLE_FIXLEN_SUPPORT, @c SOFAB_DISABLE_ARRAY_SUPPORT,
230 * @c SOFAB_DISABLE_SEQUENCE_SUPPORT, @c SOFAB_DISABLE_FP64_SUPPORT or
231 * @c SOFAB_DISABLE_INT64_SUPPORT removes the wire construct itself, not merely the
232 * ability to store it: a message carrying one is @ref SOFAB_RET_E_INVALID_MSG, even
233 * if the field would otherwise have been skipped. Such a build therefore only
234 * interoperates with peers that never emit the removed constructs — see the
235 * configuration notes in sofab.h before enabling one.
236 *
237 * A zero-length feed is legal and reports the outcome so far; @p data may then be
238 * NULL. That is how the all-default message arrives: MESSAGE_SPEC §2 omits a
239 * sequence-typed field equal to its default, so an all-default message *is* the
240 * empty byte string and @c sofab_istream_feed(ctx, NULL, 0) is its whole input.
241 *
242 * @param ctx Pointer to the input stream context.
243 * @param data Pointer to the raw serialized data (may be NULL when @p datalen is 0).
244 * @param datalen Length of @p data in bytes.
245 *
246 * @return SOFAB_RET_OK on a complete boundary, SOFAB_RET_INCOMPLETE on a partial
247 * (mid-field / open-sequence) decode, or another sofab_ret_t error code.
248 */
250 sofab_istream_t *ctx, const void *data, size_t datalen);
251
252/*!
253 * @brief Reject the message in progress from within a field callback.
254 *
255 * A field callback usually binds a destination (a sofab_istream_read_* call) or
256 * skips the field by binding nothing. Some declared bounds, however, can only be
257 * checked by the callback itself — most notably an element whose wire index is at
258 * or beyond a fixed-count array's capacity, delivered to the callback keyed by
259 * its wire id with no way for the core to know the schema count. For those cases
260 * a silent skip would let a malformed message decode as valid, violating
261 * MESSAGE_SPEC §7/§7.1 ("a declared bound binds every target").
262 *
263 * Calling this sets the same sticky invalid flag the decoder sets when it rejects
264 * a message itself: the enclosing @ref sofab_istream_feed (and every subsequent
265 * one) returns @ref SOFAB_RET_E_INVALID_MSG regardless of what else is decoded,
266 * and no further field callback fires. The flag is set synchronously inside the
267 * callback, so it takes effect even when the field's payload fill is deferred
268 * across chunked feeds. It is cleared only by @ref sofab_istream_init.
269 *
270 * @param ctx Pointer to the input stream context.
271 */
273
274/*!
275 * @brief Number of fields skipped because their wire type contradicted the
276 * destination a field callback bound.
277 *
278 * A field whose type on the wire does not match the type of the
279 * sofab_istream_read_* call made for it carries no value for that destination,
280 * so it is skipped like an unknown id and decoding continues (MESSAGE_SPEC
281 * §7.3). That is not an error: it is what lets a reader and a writer built from
282 * different revisions of a schema keep talking.
283 *
284 * This counter makes those skips visible. A non-zero value on a message that
285 * decoded successfully means the two sides disagree about what an id means —
286 * useful in a log or a health metric, and the signal that used to be reported as
287 * a usage error. The count saturates at 255; only zero versus non-zero
288 * carries meaning. It is cleared by @ref sofab_istream_init.
289 *
290 * @param ctx Pointer to the input stream context.
291 * @return Number of type-contradicting fields skipped, saturating at 255.
292 */
293#if SOFAB_SKIP_COUNTER
294extern uint8_t sofab_istream_skipped (const sofab_istream_t *ctx);
295#endif
296
297/* read functions *************************************************************/
298
299/*!
300 * @brief Reads a variable-length integer field.
301 *
302 * This function must only be invoked inside a field callback. The value is
303 * written to @p var using @p varlen bytes.
304 *
305 * @param ctx Pointer to the input stream context.
306 * @param var Output variable.
307 * @param varlen Size of the output variable in bytes.
308 * @param opt Field options (type and flags).
309 */
311 sofab_istream_t *ctx, void *var, size_t varlen, uint8_t opt);
312
313/* inline convenience functions ***********************************************/
314
315/*!
316 * @brief Reads an 8-bit signed integer.
317 *
318 * @param ctx Pointer to the input stream context.
319 * @param var Pointer to destination variable.
320 */
321static inline void sofab_istream_read_i8 (sofab_istream_t *ctx, int8_t *var)
322{
323 sofab_istream_read_field(ctx, var, sizeof(int8_t),
325}
326
327/*!
328 * @brief Reads an 8-bit unsigned integer.
329 *
330 * @param ctx Pointer to the input stream context.
331 * @param var Pointer to destination variable.
332 */
333static inline void sofab_istream_read_u8 (sofab_istream_t *ctx, uint8_t *var)
334{
335 sofab_istream_read_field(ctx, var, sizeof(uint8_t),
337}
338
339/*!
340 * @brief Reads a 16-bit signed integer.
341 *
342 * @param ctx Pointer to the input stream context.
343 * @param var Pointer to destination variable.
344 */
345static inline void sofab_istream_read_i16 (sofab_istream_t *ctx, int16_t *var)
346{
347 sofab_istream_read_field(ctx, var, sizeof(int16_t),
349}
350
351/*!
352 * @brief Reads a 16-bit unsigned integer.
353 *
354 * @param ctx Pointer to the input stream context.
355 * @param var Pointer to destination variable.
356 */
357static inline void sofab_istream_read_u16 (sofab_istream_t *ctx, uint16_t *var)
358{
359 sofab_istream_read_field(ctx, var, sizeof(uint16_t),
361}
362
363/*!
364 * @brief Reads a 32-bit signed integer.
365 *
366 * @param ctx Pointer to the input stream context.
367 * @param var Pointer to destination variable.
368 */
369static inline void sofab_istream_read_i32 (sofab_istream_t *ctx, int32_t *var)
370{
371 sofab_istream_read_field(ctx, var, sizeof(int32_t),
373}
374
375/*!
376 * @brief Reads a 32-bit unsigned integer.
377 *
378 * @param ctx Pointer to the input stream context.
379 * @param var Pointer to destination variable.
380 */
381static inline void sofab_istream_read_u32 (sofab_istream_t *ctx, uint32_t *var)
382{
383 sofab_istream_read_field(ctx, var, sizeof(uint32_t),
385}
386
387#if !defined(SOFAB_DISABLE_INT64_SUPPORT)
388/*!
389 * @brief Reads a 64-bit signed integer.
390 *
391 * @param ctx Pointer to the input stream context.
392 * @param var Pointer to destination variable.
393 */
394static inline void sofab_istream_read_i64 (sofab_istream_t *ctx, int64_t *var)
395{
396 sofab_istream_read_field(ctx, var, sizeof(int64_t),
398}
399
400/*!
401 * @brief Reads a 64-bit unsigned integer.
402 *
403 * @param ctx Pointer to the input stream context.
404 * @param var Pointer to destination variable.
405 */
406static inline void sofab_istream_read_u64 (sofab_istream_t *ctx, uint64_t *var)
407{
408 sofab_istream_read_field(ctx, var, sizeof(uint64_t),
410}
411#endif /* !defined(SOFAB_DISABLE_INT64_SUPPORT) */
412
413/*!
414 * @brief Reads a boolean value.
415 *
416 * Canonical on encode, tolerant on decode (CORELIB_PLAN §4.4): the wire carries
417 * a plain unsigned varint, and **every** value other than `0` reads as `true`.
418 * The value is normalized on store, so the destination always ends up holding
419 * `0` or `1` — the only two states a @c bool object may have — and a re-encode
420 * emits `1`. A boolean carries no width bound, so a value that does not fit one
421 * byte is `true`, not `INVALID`.
422 *
423 * @param ctx Pointer to the input stream context.
424 * @param var Pointer to destination variable.
425 */
432
433#if !defined(SOFAB_DISABLE_FIXLEN_SUPPORT)
434/*!
435 * @brief Reads a 32-bit floating-point value.
436 *
437 * @param ctx Pointer to the input stream context.
438 * @param var Pointer to destination variable.
439 */
446
447#if !defined(SOFAB_DISABLE_FP64_SUPPORT)
448/*!
449 * @brief Reads a 64-bit floating-point value.
450 *
451 * @param ctx Pointer to the input stream context.
452 * @param var Pointer to destination variable.
453 */
460#endif /* !defined(SOFAB_DISABLE_FP64_SUPPORT) */
461
462/*!
463 * @brief Reads a string of fixed maximum size.
464 *
465 * A null terminator is always appended to the end of the string.
466 *
467 * @param ctx Pointer to the input stream context.
468 * @param var Destination buffer.
469 * @param varlen Maximum number of bytes to read.
470 */
478
479/*!
480 * @brief Reads a string of fixed maximum size.
481 *
482 * No null terminator is appended to the end of the string.
483 *
484 * @param ctx Pointer to the input stream context.
485 * @param var Destination buffer.
486 * @param varlen Maximum number of bytes to read.
487 */
494
495/*!
496 * @brief Reads a binary blob of fixed maximum size.
497 *
498 * @param ctx Pointer to the input stream context.
499 * @param var Destination buffer.
500 * @param varlen Maximum number of bytes to read.
501 */
508#endif /* !defined(SOFAB_DISABLE_FIXLEN_SUPPORT) */
509
510/* read array functions *******************************************************/
511
512#if !defined(SOFAB_DISABLE_ARRAY_SUPPORT)
513/*!
514 * @brief Reads an array of elements.
515 *
516 * @param ctx Pointer to the input stream context.
517 * @param var Destination array.
518 * @param element_count Number of fixed-size elements.
519 * @param element_size Size of each element in bytes.
520 * @param opt Field options (type and flags).
521 */
523 sofab_istream_t *ctx, void *var, size_t element_count, size_t element_size, uint8_t opt);
524#endif /* !defined(SOFAB_DISABLE_ARRAY_SUPPORT) */
525
526/* inline convenience array functions *****************************************/
527
528#if !defined(SOFAB_DISABLE_ARRAY_SUPPORT)
529/*!
530 * @brief Reads an array of 8-bit signed integers.
531 *
532 * @param ctx Pointer to the input stream context.
533 * @param var Destination array.
534 * @param element_count Number of elements to read.
535 */
537 sofab_istream_t *ctx, int8_t *var, size_t element_count)
538{
539 sofab_istream_read_array(ctx, var, element_count, sizeof(int8_t),
541}
542
543/*!
544 * @brief Reads an array of 8-bit unsigned integers.
545 *
546 * @param ctx Pointer to the input stream context.
547 * @param var Destination array.
548 * @param element_count Number of elements to read.
549 */
551 sofab_istream_t *ctx, uint8_t *var, size_t element_count)
552{
553 sofab_istream_read_array(ctx, var, element_count, sizeof(uint8_t),
555}
556
557/*!
558 * @brief Reads an array of 16-bit signed integers.
559 *
560 * @param ctx Pointer to the input stream context.
561 * @param var Destination array.
562 * @param element_count Number of elements to read.
563 */
565 sofab_istream_t *ctx, int16_t *var, size_t element_count)
566{
567 sofab_istream_read_array(ctx, var, element_count, sizeof(int16_t),
569}
570
571/*!
572 * @brief Reads an array of 16-bit unsigned integers.
573 *
574 * @param ctx Pointer to the input stream context.
575 * @param var Destination array.
576 * @param element_count Number of elements to read.
577 */
579 sofab_istream_t *ctx, uint16_t *var, size_t element_count)
580{
581 sofab_istream_read_array(ctx, var, element_count, sizeof(uint16_t),
583}
584
585/*!
586 * @brief Reads an array of 32-bit signed integers.
587 *
588 * @param ctx Pointer to the input stream context.
589 * @param var Destination array.
590 * @param element_count Number of elements to read.
591 */
593 sofab_istream_t *ctx, int32_t *var, size_t element_count)
594{
595 sofab_istream_read_array(ctx, var, element_count, sizeof(int32_t),
597}
598
599/*!
600 * @brief Reads an array of 32-bit unsigned integers.
601 *
602 * @param ctx Pointer to the input stream context.
603 * @param var Destination array.
604 * @param element_count Number of elements to read.
605 */
607 sofab_istream_t *ctx, uint32_t *var, size_t element_count)
608{
609 sofab_istream_read_array(ctx, var, element_count, sizeof(uint32_t),
611}
612
613/*!
614 * @brief Reads an array of booleans.
615 *
616 * The elements ride the unsigned varint array wire form; each is normalized on
617 * store exactly as @ref sofab_istream_read_bool normalizes a scalar (§4.4).
618 *
619 * @param ctx Pointer to the input stream context.
620 * @param var Destination array.
621 * @param element_count Number of elements to read.
622 */
624 sofab_istream_t *ctx, bool *var, size_t element_count)
625{
626 sofab_istream_read_array(ctx, var, element_count, sizeof(bool),
629}
630
631#if !defined(SOFAB_DISABLE_INT64_SUPPORT)
632/*!
633 * @brief Reads an array of 64-bit signed integers.
634 *
635 * @param ctx Pointer to the input stream context.
636 * @param var Destination array.
637 * @param element_count Number of elements to read.
638 */
640 sofab_istream_t *ctx, int64_t *var, size_t element_count)
641{
642 sofab_istream_read_array(ctx, var, element_count, sizeof(int64_t),
644}
645
646/*!
647 * @brief Reads an array of 64-bit unsigned integers.
648 *
649 * @param ctx Pointer to the input stream context.
650 * @param var Destination array.
651 * @param element_count Number of elements to read.
652 */
654 sofab_istream_t *ctx, uint64_t *var, size_t element_count)
655{
656 sofab_istream_read_array(ctx, var, element_count, sizeof(uint64_t),
658}
659#endif /* !defined(SOFAB_DISABLE_INT64_SUPPORT) */
660
661#if !defined(SOFAB_DISABLE_FIXLEN_SUPPORT)
662/*!
663 * @brief Reads an array of 32-bit floating-point values.
664 *
665 * @param ctx Pointer to the input stream context.
666 * @param var Destination array.
667 * @param element_count Number of elements to read.
668 */
670 sofab_istream_t *ctx, float *var, size_t element_count)
671{
672 sofab_istream_read_array(ctx, var, element_count, sizeof(float),
675}
676
677#if !defined(SOFAB_DISABLE_FP64_SUPPORT)
678/*!
679 * @brief Reads an array of 64-bit floating-point values.
680 *
681 * @param ctx Pointer to the input stream context.
682 * @param var Destination array.
683 * @param element_count Number of elements to read.
684 */
686 sofab_istream_t *ctx, double *var, size_t element_count)
687{
688 sofab_istream_read_array(ctx, var, element_count, sizeof(double),
691}
692#endif /* !defined(SOFAB_DISABLE_FP64_SUPPORT) */
693#endif /* !defined(SOFAB_DISABLE_FIXLEN_SUPPORT) */
694#endif /* !defined(SOFAB_DISABLE_ARRAY_SUPPORT) */
695
696/* nested sequence functions **************************************************/
697
698#if !defined(SOFAB_DISABLE_SEQUENCE_SUPPORT)
699/*!
700 * @brief Begins decoding a nested Sofab sequence.
701 *
702 * A nested sequence has its own decoder and callback. This allows hierarchical
703 * message structures with independent interpretation of child fields.
704 *
705 * @param ctx Pointer to the input stream context.
706 * @param decoder Decoder instance used for the new sequence.
707 * @param field_callback Callback invoked for fields inside the nested sequence.
708 * @param usrptr Optional user pointer for the nested callback.
709 */
712 sofab_istream_field_cb_t field_callback, void *usrptr);
713#endif /* !defined(SOFAB_DISABLE_SEQUENCE_SUPPORT) */
714
715#ifdef __cplusplus
716}
717#endif
718
719/** @} */ // end of defgroup
720
721#endif /* SOFAB_ISTREAM_H */
static void sofab_istream_read_string(sofab_istream_t *ctx, char *var, size_t varlen)
Reads a string of fixed maximum size.
Definition istream.h:471
static void sofab_istream_read_blob(sofab_istream_t *ctx, void *var, size_t varlen)
Reads a binary blob of fixed maximum size.
Definition istream.h:502
static void sofab_istream_read_array_of_i16(sofab_istream_t *ctx, int16_t *var, size_t element_count)
Reads an array of 16-bit signed integers.
Definition istream.h:564
static void sofab_istream_read_array_of_u64(sofab_istream_t *ctx, uint64_t *var, size_t element_count)
Reads an array of 64-bit unsigned integers.
Definition istream.h:653
#define SOFAB_ISTREAM_OPT_STRINGTERM
Definition istream.h:64
static void sofab_istream_read_u16(sofab_istream_t *ctx, uint16_t *var)
Reads a 16-bit unsigned integer.
Definition istream.h:357
void sofab_istream_invalidate(sofab_istream_t *ctx)
Reject the message in progress from within a field callback.
void sofab_istream_read_field(sofab_istream_t *ctx, void *var, size_t varlen, uint8_t opt)
Number of fields skipped because their wire type contradicted the destination a field callback bound.
void sofab_istream_read_sequence(sofab_istream_t *ctx, sofab_istream_decoder_t *decoder, sofab_istream_field_cb_t field_callback, void *usrptr)
Begins decoding a nested Sofab sequence.
struct sofab_istream_decoder sofab_istream_decoder_t
Decoder context used for Sofab sequences.
static void sofab_istream_read_i64(sofab_istream_t *ctx, int64_t *var)
Reads a 64-bit signed integer.
Definition istream.h:394
static void sofab_istream_read_fp64(sofab_istream_t *ctx, double *var)
Reads a 64-bit floating-point value.
Definition istream.h:454
void(* sofab_istream_field_cb_t)(sofab_istream_t *ctx, sofab_id_t id, size_t size, size_t count, void *usrptr)
Field callback invoked whenever a new field ID is decoded.
Definition istream.h:113
static void sofab_istream_read_u64(sofab_istream_t *ctx, uint64_t *var)
Reads a 64-bit unsigned integer.
Definition istream.h:406
static void sofab_istream_read_u8(sofab_istream_t *ctx, uint8_t *var)
Reads an 8-bit unsigned integer.
Definition istream.h:333
#define SOFAB_ISTREAM_OPT_FIELDTYPE(type)
Definition istream.h:62
#define SOFAB_ISTREAM_OPT_FIXLENTYPE(type)
Definition istream.h:63
uint32_t sofab_id_t
Field identifier type.
Definition sofab.h:106
static void sofab_istream_read_array_of_bool(sofab_istream_t *ctx, bool *var, size_t element_count)
Reads an array of booleans.
Definition istream.h:623
static void sofab_istream_read_array_of_i32(sofab_istream_t *ctx, int32_t *var, size_t element_count)
Reads an array of 32-bit signed integers.
Definition istream.h:592
static void sofab_istream_read_array_of_i64(sofab_istream_t *ctx, int64_t *var, size_t element_count)
Reads an array of 64-bit signed integers.
Definition istream.h:639
uint64_t sofab_unsigned_t
Scalar varint value types.
Definition sofab.h:118
static void sofab_istream_read_string_noterm(sofab_istream_t *ctx, char *var, size_t varlen)
Reads a string of fixed maximum size.
Definition istream.h:488
static void sofab_istream_read_array_of_u32(sofab_istream_t *ctx, uint32_t *var, size_t element_count)
Reads an array of 32-bit unsigned integers.
Definition istream.h:606
static void sofab_istream_read_fp32(sofab_istream_t *ctx, float *var)
Reads a 32-bit floating-point value.
Definition istream.h:440
#define SOFAB_ISTREAM_OPT_BOOLEAN
Definition istream.h:68
static void sofab_istream_read_bool(sofab_istream_t *ctx, bool *var)
Reads a boolean value.
Definition istream.h:426
static void sofab_istream_read_array_of_u16(sofab_istream_t *ctx, uint16_t *var, size_t element_count)
Reads an array of 16-bit unsigned integers.
Definition istream.h:578
sofab_ret_t
Return status codes.
Definition sofab.h:39
void sofab_istream_init(sofab_istream_t *ctx, sofab_istream_field_cb_t field_callback, void *usrptr)
Initializes a Sofab input stream context.
static void sofab_istream_read_i16(sofab_istream_t *ctx, int16_t *var)
Reads a 16-bit signed integer.
Definition istream.h:345
static void sofab_istream_read_array_of_fp32(sofab_istream_t *ctx, float *var, size_t element_count)
Reads an array of 32-bit floating-point values.
Definition istream.h:669
void sofab_istream_read_array(sofab_istream_t *ctx, void *var, size_t element_count, size_t element_size, uint8_t opt)
Reads an array of elements.
static void sofab_istream_read_i32(sofab_istream_t *ctx, int32_t *var)
Reads a 32-bit signed integer.
Definition istream.h:369
static void sofab_istream_read_i8(sofab_istream_t *ctx, int8_t *var)
Reads an 8-bit signed integer.
Definition istream.h:321
static void sofab_istream_read_array_of_i8(sofab_istream_t *ctx, int8_t *var, size_t element_count)
Reads an array of 8-bit signed integers.
Definition istream.h:536
static void sofab_istream_read_u32(sofab_istream_t *ctx, uint32_t *var)
Reads a 32-bit unsigned integer.
Definition istream.h:381
static void sofab_istream_read_array_of_fp64(sofab_istream_t *ctx, double *var, size_t element_count)
Reads an array of 64-bit floating-point values.
Definition istream.h:685
sofab_ret_t sofab_istream_feed(sofab_istream_t *ctx, const void *data, size_t datalen)
Feeds raw Sofab-encoded bytes into the input stream.
static void sofab_istream_read_array_of_u8(sofab_istream_t *ctx, uint8_t *var, size_t element_count)
Reads an array of 8-bit unsigned integers.
Definition istream.h:550
@ SOFAB_FIXLENTYPE_FP32
Definition sofab.h:99
@ SOFAB_FIXLENTYPE_STRING
Definition sofab.h:101
@ SOFAB_FIXLENTYPE_FP64
Definition sofab.h:100
@ SOFAB_FIXLENTYPE_BLOB
Definition sofab.h:102
@ SOFAB_TYPE_FIXLEN
Definition sofab.h:88
@ SOFAB_TYPE_VARINTARRAY_SIGNED
Definition sofab.h:90
@ SOFAB_TYPE_VARINT_SIGNED
Definition sofab.h:87
@ SOFAB_TYPE_FIXLENARRAY
Definition sofab.h:91
@ SOFAB_TYPE_VARINT_UNSIGNED
Definition sofab.h:86
@ SOFAB_TYPE_VARINTARRAY_UNSIGNED
Definition sofab.h:89
SofaBuffers C - Type definitions and constants.
Decoder context used for Sofab sequences.
Definition istream.h:123
struct sofab_istream_decoder * parent
Definition istream.h:124
sofab_istream_field_cb_t field_callback
Definition istream.h:125
Internal state of the Sofab input stream.
Definition istream.h:135
size_t target_len
Definition istream.h:156
sofab_unsigned_t varint_value
Definition istream.h:146
size_t target_count
Definition istream.h:157
sofab_istream_decoder_t default_decoder
Definition istream.h:147
uint8_t invalid
Definition istream.h:162
uint8_t depth
Definition istream.h:166
uint8_t * target_ptr
Definition istream.h:148
uint8_t varint_shift
Definition istream.h:161
sofab_id_t id
Definition istream.h:159
size_t fixlen_remaining
Definition istream.h:155
sofab_istream_decoder_t * decoder
Definition istream.h:154
uint8_t target_opt
Definition istream.h:160
size_t array_wire_count
Definition istream.h:158