moddef-c
Loading...
Searching...
No Matches
wire.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2
3/* Minimal protobuf wire reader (spec ยง27). Read-only, allocation-free:
4 * documents are navigated in place wherever they live (memory-mapped
5 * flash, RAM). Only what the ModDef schema uses: varint (0),
6 * length-delimited (2), and skippable fixed32/64. */
7#ifndef MODDEF_WIRE_H
8#define MODDEF_WIRE_H
9
10#include <stdbool.h>
11#include <stdint.h>
12
13#include "moddef/str.h"
14
15typedef struct md_wire {
16 const uint8_t *p;
17 const uint8_t *end;
19
20static inline md_wire_t md_wire(md_bytes_t b)
21{
22 md_wire_t w = {b.p, b.p + b.len};
23 return w;
24}
25
26static inline bool md_wire_done(const md_wire_t *w)
27{
28 return w->p >= w->end;
29}
30
31/* Read the next field tag; false at end of message or on malformed data. */
32bool md_wire_tag(md_wire_t *w, uint32_t *field, uint8_t *wtype);
33
34bool md_wire_varint(md_wire_t *w, uint64_t *v);
35
36/* Length-delimited payload (strings, bytes, submessages, packed scalars). */
38
39/* Skip a field of the given wire type. */
40bool md_wire_skip(md_wire_t *w, uint8_t wtype);
41
42/* --- convenience over a whole (sub)message --------------------------------- */
43
44/* First occurrence of a length-delimited field; false if absent. */
45bool md_wire_find_len(md_bytes_t msg, uint32_t field, md_bytes_t *out);
46
47/* First occurrence of a varint field; false if absent (proto3 default). */
48bool md_wire_find_varint(md_bytes_t msg, uint32_t field, uint64_t *out);
49
50static inline md_str_t md_wire_str(md_bytes_t b)
51{
52 md_str_t s = {(const char *)b.p, (uint16_t)b.len};
53 return s;
54}
55
56/* int64 fields arrive as 64-bit two's-complement varints. */
57static inline int64_t md_wire_i64(uint64_t v)
58{
59 return (int64_t)v;
60}
61
62#endif /* MODDEF_WIRE_H */
Definition str.h:33
uint32_t len
Definition str.h:35
const uint8_t * p
Definition str.h:34
Definition str.h:13
Definition wire.h:15
const uint8_t * end
Definition wire.h:17
const uint8_t * p
Definition wire.h:16
bool md_wire_find_varint(md_bytes_t msg, uint32_t field, uint64_t *out)
bool md_wire_find_len(md_bytes_t msg, uint32_t field, md_bytes_t *out)
bool md_wire_len(md_wire_t *w, md_bytes_t *out)
bool md_wire_varint(md_wire_t *w, uint64_t *v)
bool md_wire_skip(md_wire_t *w, uint8_t wtype)
struct md_wire md_wire_t
bool md_wire_tag(md_wire_t *w, uint32_t *field, uint8_t *wtype)