xref: /aosp_15_r20/external/grpc-grpc/third_party/upb/upb/mini_descriptor/internal/encode.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 #ifndef UPB_MINI_DESCRIPTOR_INTERNAL_ENCODE_H_
9 #define UPB_MINI_DESCRIPTOR_INTERNAL_ENCODE_H_
10 
11 #include <stdint.h>
12 
13 #include "upb/base/descriptor_constants.h"
14 
15 // Must be last.
16 #include "upb/port/def.inc"
17 
18 // If the input buffer has at least this many bytes available, the encoder call
19 // is guaranteed to succeed (as long as field number order is maintained).
20 #define kUpb_MtDataEncoder_MinSize 16
21 
22 typedef struct {
23   char* end;  // Limit of the buffer passed as a parameter.
24   // Aliased to internal-only members in .cc.
25   char internal[32];
26 } upb_MtDataEncoder;
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 // Encodes field/oneof information for a given message.  The sequence of calls
33 // should look like:
34 //
35 //   upb_MtDataEncoder e;
36 //   char buf[256];
37 //   char* ptr = buf;
38 //   e.end = ptr + sizeof(buf);
39 //   unit64_t msg_mod = ...; // bitwise & of kUpb_MessageModifiers or zero
40 //   ptr = upb_MtDataEncoder_StartMessage(&e, ptr, msg_mod);
41 //   // Fields *must* be in field number order.
42 //   ptr = upb_MtDataEncoder_PutField(&e, ptr, ...);
43 //   ptr = upb_MtDataEncoder_PutField(&e, ptr, ...);
44 //   ptr = upb_MtDataEncoder_PutField(&e, ptr, ...);
45 //
46 //   // If oneofs are present.  Oneofs must be encoded after regular fields.
47 //   ptr = upb_MiniTable_StartOneof(&e, ptr)
48 //   ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
49 //   ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
50 //
51 //   ptr = upb_MiniTable_StartOneof(&e, ptr);
52 //   ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
53 //   ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
54 //
55 // Oneofs must be encoded after all regular fields.
56 char* upb_MtDataEncoder_StartMessage(upb_MtDataEncoder* e, char* ptr,
57                                      uint64_t msg_mod);
58 char* upb_MtDataEncoder_PutField(upb_MtDataEncoder* e, char* ptr,
59                                  upb_FieldType type, uint32_t field_num,
60                                  uint64_t field_mod);
61 char* upb_MtDataEncoder_StartOneof(upb_MtDataEncoder* e, char* ptr);
62 char* upb_MtDataEncoder_PutOneofField(upb_MtDataEncoder* e, char* ptr,
63                                       uint32_t field_num);
64 
65 // Encodes the set of values for a given enum. The values must be given in
66 // order (after casting to uint32_t), and repeats are not allowed.
67 char* upb_MtDataEncoder_StartEnum(upb_MtDataEncoder* e, char* ptr);
68 char* upb_MtDataEncoder_PutEnumValue(upb_MtDataEncoder* e, char* ptr,
69                                      uint32_t val);
70 char* upb_MtDataEncoder_EndEnum(upb_MtDataEncoder* e, char* ptr);
71 
72 // Encodes an entire mini descriptor for an extension.
73 char* upb_MtDataEncoder_EncodeExtension(upb_MtDataEncoder* e, char* ptr,
74                                         upb_FieldType type, uint32_t field_num,
75                                         uint64_t field_mod);
76 
77 // Encodes an entire mini descriptor for a map.
78 char* upb_MtDataEncoder_EncodeMap(upb_MtDataEncoder* e, char* ptr,
79                                   upb_FieldType key_type,
80                                   upb_FieldType value_type, uint64_t key_mod,
81                                   uint64_t value_mod);
82 
83 // Encodes an entire mini descriptor for a message set.
84 char* upb_MtDataEncoder_EncodeMessageSet(upb_MtDataEncoder* e, char* ptr);
85 
86 #ifdef __cplusplus
87 } /* extern "C" */
88 #endif
89 
90 #include "upb/port/undef.inc"
91 
92 #endif /* UPB_MINI_DESCRIPTOR_INTERNAL_ENCODE_H_ */
93