WPILibC++ 2027.0.0-alpha-3
Loading...
Searching...
No Matches
encode.h
Go to the documentation of this file.
1// Protocol Buffers - Google's data interchange format
2// Copyright 2023 Google LLC. All rights reserved.
3//
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file or at
6// https://developers.google.com/open-source/licenses/bsd
7
8// upb_Encode: parsing from a upb_Message using a upb_MiniTable.
9
10#ifndef UPB_WIRE_ENCODE_H_
11#define UPB_WIRE_ENCODE_H_
12
13#include <stddef.h>
14#include <stdint.h>
15
16#include "upb/mem/arena.h"
17#include "upb/message/message.h"
19
20// Must be last.
21#include "upb/port/def.inc"
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27enum {
28 /* If set, the results of serializing will be deterministic across all
29 * instances of this binary. There are no guarantees across different
30 * binary builds.
31 *
32 * If your proto contains maps, the encoder will need to malloc()/free()
33 * memory during encode. */
35
36 // When set, unknown fields are not encoded.
38
39 // When set, the encode will fail if any required fields are missing.
41};
42
43// LINT.IfChange
44typedef enum {
46 kUpb_EncodeStatus_OutOfMemory = 1, // Arena alloc failed
48
49 // kUpb_EncodeOption_CheckRequired failed but the parse otherwise succeeded.
52// LINT.ThenChange(//depot/google3/third_party/protobuf/rust/upb.rs:encode_status)
53
54UPB_INLINE uint32_t upb_EncodeOptions_MaxDepth(uint16_t depth) {
55 return (uint32_t)depth << 16;
56}
57
58uint16_t upb_EncodeOptions_GetEffectiveMaxDepth(uint32_t options);
59
60// Enforce an upper bound on recursion depth.
61UPB_INLINE int upb_Encode_LimitDepth(uint32_t encode_options, uint32_t limit) {
62 uint32_t max_depth = upb_EncodeOptions_GetEffectiveMaxDepth(encode_options);
63 if (max_depth > limit) max_depth = limit;
64 return upb_EncodeOptions_MaxDepth(max_depth) | (encode_options & 0xffff);
65}
66
68 const upb_MiniTable* l, int options,
69 upb_Arena* arena, char** buf, size_t* size);
70
71// Encodes the message prepended by a varint of the serialized length.
73 const upb_MiniTable* l,
74 int options, upb_Arena* arena,
75 char** buf, size_t* size);
76// Utility function for wrapper languages to get an error string from a
77// upb_EncodeStatus.
79
80#ifdef __cplusplus
81} /* extern "C" */
82#endif
83
84#include "upb/port/undef.inc"
85
86#endif /* UPB_WIRE_ENCODE_H_ */
#define UPB_INLINE
Definition def.inc:144
#define UPB_API
Definition def.inc:162
Definition arena.h:29
Definition types.h:18
Definition message.h:54
upb_EncodeStatus
Definition encode.h:44
@ kUpb_EncodeStatus_Ok
Definition encode.h:45
@ kUpb_EncodeStatus_MaxDepthExceeded
Definition encode.h:47
@ kUpb_EncodeStatus_OutOfMemory
Definition encode.h:46
@ kUpb_EncodeStatus_MissingRequired
Definition encode.h:50
UPB_API upb_EncodeStatus upb_EncodeLengthPrefixed(const upb_Message *msg, const upb_MiniTable *l, int options, upb_Arena *arena, char **buf, size_t *size)
UPB_API upb_EncodeStatus upb_Encode(const upb_Message *msg, const upb_MiniTable *l, int options, upb_Arena *arena, char **buf, size_t *size)
uint16_t upb_EncodeOptions_GetEffectiveMaxDepth(uint32_t options)
UPB_INLINE int upb_Encode_LimitDepth(uint32_t encode_options, uint32_t limit)
Definition encode.h:61
UPB_API const char * upb_EncodeStatus_String(upb_EncodeStatus status)
@ kUpb_EncodeOption_CheckRequired
Definition encode.h:40
@ kUpb_EncodeOption_SkipUnknown
Definition encode.h:37
@ kUpb_EncodeOption_Deterministic
Definition encode.h:34
UPB_INLINE uint32_t upb_EncodeOptions_MaxDepth(uint16_t depth)
Definition encode.h:54