|
cwist
Modern C web framework with libttak runtime
|
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>
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. | |
Small Protobuf wire-format reader/writer helpers.
Definition in file protobuf.c.
| int cwist_pb_read_field | ( | cwist_pb_reader * | r, |
| cwist_pb_field * | field | ||
| ) |
Read the next field key and payload from the reader.
| r | Reader to read from. Must not be NULL. |
| field | Receives the field number, wire type, and payload descriptor. Must not be NULL. |
| 1 | A field was read and field is filled in. |
| 0 | The reader is exhausted (end of input); the reader position is unchanged in this case. |
| -1 | Malformed 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.
| int cwist_pb_read_varint | ( | cwist_pb_reader * | r, |
| uint64_t * | out | ||
| ) |
Read a base-128 varint, advancing the reader position.
| r | Reader to read from. Must not be NULL. |
| out | Receives the decoded value. Must not be NULL. |
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().
| void cwist_pb_reader_init | ( | cwist_pb_reader * | r, |
| const void * | data, | ||
| size_t | len | ||
| ) |
Initialize a reader over a caller-owned buffer.
| r | Reader to initialize. NULL is accepted and ignored. |
| data | Buffer to read from; not copied, so it must outlive the reader. |
| len | Size 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.
| int cwist_pb_skip_field | ( | cwist_pb_reader * | r, |
| const cwist_pb_field * | field | ||
| ) |
Validate a parsed field descriptor.
field is non-NULL. | r | Reader associated with the field; unused. |
| field | Field descriptor to validate. Must not be NULL. |
field is valid, -1 if it is NULL. Definition at line 333 of file protobuf.c.
References cap.
| 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.
| w | Writer to append to. |
| field_number | Protobuf field number; must be non-zero. |
| value | Non-zero encodes as 1, zero encodes as 0. |
Definition at line 127 of file protobuf.c.
References cap, and cwist_pb_write_uint64_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).
| w | Writer to append to. |
| field_number | Protobuf field number; must be non-zero. |
| data | Bytes to copy; may be NULL only when len is 0. The bytes are copied into the writer buffer, so the caller keeps ownership. |
| len | Number of bytes to write. |
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().
| 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.
| w | Writer to append to. |
| field_number | Protobuf field number; must be non-zero. |
| value | Value to encode. |
Definition at line 202 of file protobuf.c.
References cap, and cwist_pb_write_fixed64_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).
| w | Writer to append to. |
| field_number | Protobuf field number; must be non-zero. |
| value | Value to encode. |
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().
| 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).
| w | Writer to append to. |
| field_number | Protobuf field number; must be non-zero. |
| value | Value to encode. |
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().
| 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.
| w | Writer to append to. |
| field_number | Protobuf field number; must be non-zero. |
| value | Value to encode. |
Definition at line 191 of file protobuf.c.
References cap, and cwist_pb_write_fixed32_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.
| w | Writer to append to. |
| field_number | Protobuf field number; must be non-zero. |
| value | Value to encode (two's-complement, reinterpreted as uint64). |
Definition at line 116 of file protobuf.c.
References cap, and cwist_pb_write_uint64_field().
| 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.
| w | Writer to append to. Must not be NULL. |
| field_number | Protobuf field number; must be non-zero. |
| wire_type | Wire type; must be a valid cwist_pb_wire_type_t value. |
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().
| int cwist_pb_write_raw32 | ( | cwist_pb_writer * | w, |
| uint32_t | value | ||
| ) |
Append a 32-bit value in little-endian order (no key written).
| w | Writer to append to. Must not be NULL. |
| value | Value to encode. |
Definition at line 137 of file protobuf.c.
References cap, and cwist_pb_writer_reserve().
Referenced by cwist_pb_write_fixed32_field().
| int cwist_pb_write_raw64 | ( | cwist_pb_writer * | w, |
| uint64_t | value | ||
| ) |
Append a 64-bit value in little-endian order (no key written).
| w | Writer to append to. Must not be NULL. |
| value | Value to encode. |
Definition at line 153 of file protobuf.c.
References cap, and cwist_pb_writer_reserve().
Referenced by cwist_pb_write_fixed64_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).
| w | Writer to append to. |
| field_number | Protobuf field number; must be non-zero. |
| value | NUL-terminated string to copy; must not be NULL. |
Definition at line 233 of file protobuf.c.
References cap, and cwist_pb_write_bytes_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.
| w | Writer to append to. |
| field_number | Protobuf field number; must be non-zero. |
| value | Value to encode. |
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().
| int cwist_pb_write_varint | ( | cwist_pb_writer * | w, |
| uint64_t | value | ||
| ) |
Append a base-128 varint to the writer buffer.
| w | Writer to append to. Must not be NULL. |
| value | Value to encode. |
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().
| void cwist_pb_writer_free | ( | cwist_pb_writer * | w | ) |
Release the buffer owned by a Protobuf writer.
| w | Writer whose data is freed and reset. NULL is accepted and ignored. |
Definition at line 28 of file protobuf.c.
References cap, and cwist_free().
| void cwist_pb_writer_init | ( | cwist_pb_writer * | w | ) |
Initialize a Protobuf writer to an empty buffer.
| w | Writer to reset. NULL is accepted and ignored. |
Definition at line 17 of file protobuf.c.
References cap.
| int cwist_pb_writer_reserve | ( | cwist_pb_writer * | w, |
| size_t | extra | ||
| ) |
Ensure the writer buffer has room for extra more bytes.
| w | Writer to grow. Must not be NULL. |
| extra | Number of additional bytes required beyond the current length. |
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().
ZigZag-decode an unsigned 64-bit value back to a signed value.
| value | ZigZag-encoded value, as produced by cwist_pb_zigzag_encode(). |
Definition at line 355 of file protobuf.c.
References cap.
ZigZag-encode a signed 64-bit value for sint fields.
| value | Signed value to encode. |
Definition at line 344 of file protobuf.c.
References cap.