cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
protobuf.c File Reference

Small Protobuf wire-format reader/writer helpers. More...

#include <cwist/net/grpc/protobuf.h>
#include <cwist/core/mem/alloc.h>
#include <limits.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for protobuf.c:

Go to the source code of this file.

Functions

void cwist_pb_writer_init (cwist_pb_writer *w)
 Initialize a Protobuf writer to an empty buffer.
 
void cwist_pb_writer_free (cwist_pb_writer *w)
 Release the buffer owned by a Protobuf writer.
 
int cwist_pb_writer_reserve (cwist_pb_writer *w, size_t extra)
 Ensure the writer buffer has room for extra more bytes.
 
int cwist_pb_write_varint (cwist_pb_writer *w, uint64_t value)
 Append a base-128 varint to the writer buffer.
 
int cwist_pb_write_key (cwist_pb_writer *w, uint32_t field_number, cwist_pb_wire_type_t wire_type)
 Write a field key (field number and wire type) as a varint.
 
int cwist_pb_write_uint64_field (cwist_pb_writer *w, uint32_t field_number, uint64_t value)
 Write a uint64 field as a varint.
 
int cwist_pb_write_int64_field (cwist_pb_writer *w, uint32_t field_number, int64_t value)
 Write an int64 field; negative values are encoded as ten-byte varints.
 
int cwist_pb_write_bool_field (cwist_pb_writer *w, uint32_t field_number, int value)
 Write a bool field as a 0/1 varint.
 
int cwist_pb_write_raw32 (cwist_pb_writer *w, uint32_t value)
 Append a 32-bit value in little-endian order (no key written).
 
int cwist_pb_write_raw64 (cwist_pb_writer *w, uint64_t value)
 Append a 64-bit value in little-endian order (no key written).
 
int cwist_pb_write_fixed32_field (cwist_pb_writer *w, uint32_t field_number, uint32_t value)
 Write a fixed32 field (key plus 4 little-endian bytes).
 
int cwist_pb_write_fixed64_field (cwist_pb_writer *w, uint32_t field_number, uint64_t value)
 Write a fixed64 field (key plus 8 little-endian bytes).
 
int cwist_pb_write_float_field (cwist_pb_writer *w, uint32_t field_number, float value)
 Write a float field by reinterpreting it as fixed32 bits.
 
int cwist_pb_write_double_field (cwist_pb_writer *w, uint32_t field_number, double value)
 Write a double field by reinterpreting it as fixed64 bits.
 
int cwist_pb_write_bytes_field (cwist_pb_writer *w, uint32_t field_number, const void *data, size_t len)
 Write a length-delimited bytes field (key, length varint, raw bytes).
 
int cwist_pb_write_string_field (cwist_pb_writer *w, uint32_t field_number, const char *value)
 Write a string field (length-delimited, excluding the NUL terminator).
 
void cwist_pb_reader_init (cwist_pb_reader *r, const void *data, size_t len)
 Initialize a reader over a caller-owned buffer.
 
int cwist_pb_read_varint (cwist_pb_reader *r, uint64_t *out)
 Read a base-128 varint, advancing the reader position.
 
int cwist_pb_read_field (cwist_pb_reader *r, cwist_pb_field *field)
 Read the next field key and payload from the reader.
 
int cwist_pb_skip_field (cwist_pb_reader *r, const cwist_pb_field *field)
 Validate a parsed field descriptor.
 
uint64_t cwist_pb_zigzag_encode (int64_t value)
 ZigZag-encode a signed 64-bit value for sint fields.
 
int64_t cwist_pb_zigzag_decode (uint64_t value)
 ZigZag-decode an unsigned 64-bit value back to a signed value.
 

Detailed Description

Small Protobuf wire-format reader/writer helpers.

Definition in file protobuf.c.

Function Documentation

◆ cwist_pb_read_field()

int cwist_pb_read_field ( cwist_pb_reader *  r,
cwist_pb_field *  field 
)

Read the next field key and payload from the reader.

Parameters
rReader to read from. Must not be NULL.
fieldReceives the field number, wire type, and payload descriptor. Must not be NULL.
Return values
1A field was read and field is filled in.
0The reader is exhausted (end of input); the reader position is unchanged in this case.
-1Malformed data or invalid argument.

Definition at line 284 of file protobuf.c.

References cwist_pb_field::bytes, cap, CWIST_PB_32BIT, CWIST_PB_64BIT, CWIST_PB_LEN, CWIST_PB_MAX_FIELD_NUMBER, cwist_pb_read_varint(), CWIST_PB_VARINT, cwist_pb_reader::data, cwist_pb_reader::len, cwist_pb_field::len, cwist_pb_field::number, cwist_pb_reader::pos, cwist_pb_field::varint, and cwist_pb_field::wire_type.

◆ cwist_pb_read_varint()

int cwist_pb_read_varint ( cwist_pb_reader *  r,
uint64_t *  out 
)

Read a base-128 varint, advancing the reader position.

Parameters
rReader to read from. Must not be NULL.
outReceives the decoded value. Must not be NULL.
Returns
0 on success, -1 if the reader is exhausted or the varint is malformed (unterminated or longer than 64 bits).

Definition at line 258 of file protobuf.c.

References cap, cwist_pb_reader::data, cwist_pb_reader::len, and cwist_pb_reader::pos.

Referenced by cwist_pb_read_field().

◆ cwist_pb_reader_init()

void cwist_pb_reader_init ( cwist_pb_reader *  r,
const void *  data,
size_t  len 
)

Initialize a reader over a caller-owned buffer.

Parameters
rReader to initialize. NULL is accepted and ignored.
dataBuffer to read from; not copied, so it must outlive the reader.
lenSize of data in bytes.

Definition at line 244 of file protobuf.c.

References cap, cwist_pb_reader::data, cwist_pb_reader::len, and cwist_pb_reader::pos.

◆ cwist_pb_skip_field()

int cwist_pb_skip_field ( cwist_pb_reader *  r,
const cwist_pb_field *  field 
)

Validate a parsed field descriptor.

Note
The reader position is already advanced by cwist_pb_read_field(), so this function only checks that field is non-NULL.
Parameters
rReader associated with the field; unused.
fieldField descriptor to validate. Must not be NULL.
Returns
0 if field is valid, -1 if it is NULL.

Definition at line 333 of file protobuf.c.

References cap.

◆ cwist_pb_write_bool_field()

int cwist_pb_write_bool_field ( cwist_pb_writer *  w,
uint32_t  field_number,
int  value 
)

Write a bool field as a 0/1 varint.

Parameters
wWriter to append to.
field_numberProtobuf field number; must be non-zero.
valueNon-zero encodes as 1, zero encodes as 0.
Returns
0 on success, -1 on invalid argument or allocation failure.

Definition at line 127 of file protobuf.c.

References cap, and cwist_pb_write_uint64_field().

◆ cwist_pb_write_bytes_field()

int cwist_pb_write_bytes_field ( cwist_pb_writer *  w,
uint32_t  field_number,
const void *  data,
size_t  len 
)

Write a length-delimited bytes field (key, length varint, raw bytes).

Parameters
wWriter to append to.
field_numberProtobuf field number; must be non-zero.
dataBytes to copy; may be NULL only when len is 0. The bytes are copied into the writer buffer, so the caller keeps ownership.
lenNumber of bytes to write.
Returns
0 on success, -1 on invalid argument or allocation failure.

Definition at line 215 of file protobuf.c.

References cap, CWIST_PB_LEN, cwist_pb_write_key(), cwist_pb_write_varint(), and cwist_pb_writer_reserve().

Referenced by cwist_pb_write_string_field().

◆ cwist_pb_write_double_field()

int cwist_pb_write_double_field ( cwist_pb_writer *  w,
uint32_t  field_number,
double  value 
)

Write a double field by reinterpreting it as fixed64 bits.

Parameters
wWriter to append to.
field_numberProtobuf field number; must be non-zero.
valueValue to encode.
Returns
0 on success, -1 on invalid argument or allocation failure.

Definition at line 202 of file protobuf.c.

References cap, and cwist_pb_write_fixed64_field().

◆ cwist_pb_write_fixed32_field()

int cwist_pb_write_fixed32_field ( cwist_pb_writer *  w,
uint32_t  field_number,
uint32_t  value 
)

Write a fixed32 field (key plus 4 little-endian bytes).

Parameters
wWriter to append to.
field_numberProtobuf field number; must be non-zero.
valueValue to encode.
Returns
0 on success, -1 on invalid argument or allocation failure.

Definition at line 167 of file protobuf.c.

References cap, CWIST_PB_32BIT, cwist_pb_write_key(), and cwist_pb_write_raw32().

Referenced by cwist_pb_write_float_field().

◆ cwist_pb_write_fixed64_field()

int cwist_pb_write_fixed64_field ( cwist_pb_writer *  w,
uint32_t  field_number,
uint64_t  value 
)

Write a fixed64 field (key plus 8 little-endian bytes).

Parameters
wWriter to append to.
field_numberProtobuf field number; must be non-zero.
valueValue to encode.
Returns
0 on success, -1 on invalid argument or allocation failure.

Definition at line 179 of file protobuf.c.

References cap, CWIST_PB_64BIT, cwist_pb_write_key(), and cwist_pb_write_raw64().

Referenced by cwist_pb_write_double_field().

◆ cwist_pb_write_float_field()

int cwist_pb_write_float_field ( cwist_pb_writer *  w,
uint32_t  field_number,
float  value 
)

Write a float field by reinterpreting it as fixed32 bits.

Parameters
wWriter to append to.
field_numberProtobuf field number; must be non-zero.
valueValue to encode.
Returns
0 on success, -1 on invalid argument or allocation failure.

Definition at line 191 of file protobuf.c.

References cap, and cwist_pb_write_fixed32_field().

◆ cwist_pb_write_int64_field()

int cwist_pb_write_int64_field ( cwist_pb_writer *  w,
uint32_t  field_number,
int64_t  value 
)

Write an int64 field; negative values are encoded as ten-byte varints.

Parameters
wWriter to append to.
field_numberProtobuf field number; must be non-zero.
valueValue to encode (two's-complement, reinterpreted as uint64).
Returns
0 on success, -1 on invalid argument or allocation failure.

Definition at line 116 of file protobuf.c.

References cap, and cwist_pb_write_uint64_field().

◆ cwist_pb_write_key()

int cwist_pb_write_key ( cwist_pb_writer *  w,
uint32_t  field_number,
cwist_pb_wire_type_t  wire_type 
)

Write a field key (field number and wire type) as a varint.

Parameters
wWriter to append to. Must not be NULL.
field_numberProtobuf field number; must be non-zero.
wire_typeWire type; must be a valid cwist_pb_wire_type_t value.
Returns
0 on success, -1 on invalid argument or allocation failure.

Definition at line 87 of file protobuf.c.

References cap, CWIST_PB_32BIT, CWIST_PB_64BIT, CWIST_PB_LEN, CWIST_PB_MAX_FIELD_NUMBER, CWIST_PB_VARINT, and cwist_pb_write_varint().

Referenced by cwist_pb_write_bytes_field(), cwist_pb_write_fixed32_field(), cwist_pb_write_fixed64_field(), and cwist_pb_write_uint64_field().

◆ cwist_pb_write_raw32()

int cwist_pb_write_raw32 ( cwist_pb_writer *  w,
uint32_t  value 
)

Append a 32-bit value in little-endian order (no key written).

Parameters
wWriter to append to. Must not be NULL.
valueValue to encode.
Returns
0 on success, -1 on invalid argument or allocation failure.

Definition at line 137 of file protobuf.c.

References cap, and cwist_pb_writer_reserve().

Referenced by cwist_pb_write_fixed32_field().

◆ cwist_pb_write_raw64()

int cwist_pb_write_raw64 ( cwist_pb_writer *  w,
uint64_t  value 
)

Append a 64-bit value in little-endian order (no key written).

Parameters
wWriter to append to. Must not be NULL.
valueValue to encode.
Returns
0 on success, -1 on invalid argument or allocation failure.

Definition at line 153 of file protobuf.c.

References cap, and cwist_pb_writer_reserve().

Referenced by cwist_pb_write_fixed64_field().

◆ cwist_pb_write_string_field()

int cwist_pb_write_string_field ( cwist_pb_writer *  w,
uint32_t  field_number,
const char *  value 
)

Write a string field (length-delimited, excluding the NUL terminator).

Parameters
wWriter to append to.
field_numberProtobuf field number; must be non-zero.
valueNUL-terminated string to copy; must not be NULL.
Returns
0 on success, -1 on invalid argument or allocation failure.

Definition at line 233 of file protobuf.c.

References cap, and cwist_pb_write_bytes_field().

◆ cwist_pb_write_uint64_field()

int cwist_pb_write_uint64_field ( cwist_pb_writer *  w,
uint32_t  field_number,
uint64_t  value 
)

Write a uint64 field as a varint.

Parameters
wWriter to append to.
field_numberProtobuf field number; must be non-zero.
valueValue to encode.
Returns
0 on success, -1 on invalid argument or allocation failure.

Definition at line 104 of file protobuf.c.

References cap, CWIST_PB_VARINT, cwist_pb_write_key(), and cwist_pb_write_varint().

Referenced by cwist_pb_write_bool_field(), and cwist_pb_write_int64_field().

◆ cwist_pb_write_varint()

int cwist_pb_write_varint ( cwist_pb_writer *  w,
uint64_t  value 
)

Append a base-128 varint to the writer buffer.

Parameters
wWriter to append to. Must not be NULL.
valueValue to encode.
Returns
0 on success, -1 on invalid argument or allocation failure.

Definition at line 69 of file protobuf.c.

References cap, and cwist_pb_writer_reserve().

Referenced by cwist_pb_write_bytes_field(), cwist_pb_write_key(), and cwist_pb_write_uint64_field().

◆ cwist_pb_writer_free()

void cwist_pb_writer_free ( cwist_pb_writer *  w)

Release the buffer owned by a Protobuf writer.

Parameters
wWriter whose data is freed and reset. NULL is accepted and ignored.

Definition at line 28 of file protobuf.c.

References cap, and cwist_free().

◆ cwist_pb_writer_init()

void cwist_pb_writer_init ( cwist_pb_writer *  w)

Initialize a Protobuf writer to an empty buffer.

Parameters
wWriter to reset. NULL is accepted and ignored.

Definition at line 17 of file protobuf.c.

References cap.

◆ cwist_pb_writer_reserve()

int cwist_pb_writer_reserve ( cwist_pb_writer *  w,
size_t  extra 
)

Ensure the writer buffer has room for extra more bytes.

Parameters
wWriter to grow. Must not be NULL.
extraNumber of additional bytes required beyond the current length.
Returns
0 on success, -1 on invalid argument, overflow, or allocation failure.

Definition at line 42 of file protobuf.c.

References cap, and cwist_realloc().

Referenced by cwist_pb_write_bytes_field(), cwist_pb_write_raw32(), cwist_pb_write_raw64(), and cwist_pb_write_varint().

◆ cwist_pb_zigzag_decode()

int64_t cwist_pb_zigzag_decode ( uint64_t  value)

ZigZag-decode an unsigned 64-bit value back to a signed value.

Parameters
valueZigZag-encoded value, as produced by cwist_pb_zigzag_encode().
Returns
The original signed value.

Definition at line 355 of file protobuf.c.

References cap.

◆ cwist_pb_zigzag_encode()

uint64_t cwist_pb_zigzag_encode ( int64_t  value)

ZigZag-encode a signed 64-bit value for sint fields.

Parameters
valueSigned value to encode.
Returns
The ZigZag-encoded unsigned value: small magnitudes map to small unsigned codes regardless of sign (0, -1, 1, -2, 2 -> 0, 1, 2, 3, 4).

Definition at line 344 of file protobuf.c.

References cap.