1 /*
2  * Copyright (c) 2009-2022, Google LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Google LLC nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef UPB_MINI_TABLE_ENCODE_INTERNAL_H_
29 #define UPB_MINI_TABLE_ENCODE_INTERNAL_H_
30 
31 #include "upb/base/descriptor_constants.h"
32 #include "upb/mini_table/common.h"
33 
34 // Must be last.
35 #include "upb/port/def.inc"
36 
37 // If the input buffer has at least this many bytes available, the encoder call
38 // is guaranteed to succeed (as long as field number order is maintained).
39 #define kUpb_MtDataEncoder_MinSize 16
40 
41 typedef struct {
42   char* end;  // Limit of the buffer passed as a parameter.
43   // Aliased to internal-only members in .cc.
44   char internal[32];
45 } upb_MtDataEncoder;
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 // Encodes field/oneof information for a given message.  The sequence of calls
52 // should look like:
53 //
54 //   upb_MtDataEncoder e;
55 //   char buf[256];
56 //   char* ptr = buf;
57 //   e.end = ptr + sizeof(buf);
58 //   unit64_t msg_mod = ...; // bitwise & of kUpb_MessageModifiers or zero
59 //   ptr = upb_MtDataEncoder_StartMessage(&e, ptr, msg_mod);
60 //   // Fields *must* be in field number order.
61 //   ptr = upb_MtDataEncoder_PutField(&e, ptr, ...);
62 //   ptr = upb_MtDataEncoder_PutField(&e, ptr, ...);
63 //   ptr = upb_MtDataEncoder_PutField(&e, ptr, ...);
64 //
65 //   // If oneofs are present.  Oneofs must be encoded after regular fields.
66 //   ptr = upb_MiniTable_StartOneof(&e, ptr)
67 //   ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
68 //   ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
69 //
70 //   ptr = upb_MiniTable_StartOneof(&e, ptr);
71 //   ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
72 //   ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
73 //
74 // Oneofs must be encoded after all regular fields.
75 char* upb_MtDataEncoder_StartMessage(upb_MtDataEncoder* e, char* ptr,
76                                      uint64_t msg_mod);
77 char* upb_MtDataEncoder_PutField(upb_MtDataEncoder* e, char* ptr,
78                                  upb_FieldType type, uint32_t field_num,
79                                  uint64_t field_mod);
80 char* upb_MtDataEncoder_StartOneof(upb_MtDataEncoder* e, char* ptr);
81 char* upb_MtDataEncoder_PutOneofField(upb_MtDataEncoder* e, char* ptr,
82                                       uint32_t field_num);
83 
84 // Encodes the set of values for a given enum. The values must be given in
85 // order (after casting to uint32_t), and repeats are not allowed.
86 char* upb_MtDataEncoder_StartEnum(upb_MtDataEncoder* e, char* ptr);
87 char* upb_MtDataEncoder_PutEnumValue(upb_MtDataEncoder* e, char* ptr,
88                                      uint32_t val);
89 char* upb_MtDataEncoder_EndEnum(upb_MtDataEncoder* e, char* ptr);
90 
91 // Encodes an entire mini descriptor for an extension.
92 char* upb_MtDataEncoder_EncodeExtension(upb_MtDataEncoder* e, char* ptr,
93                                         upb_FieldType type, uint32_t field_num,
94                                         uint64_t field_mod);
95 
96 // Encodes an entire mini descriptor for a map.
97 char* upb_MtDataEncoder_EncodeMap(upb_MtDataEncoder* e, char* ptr,
98                                   upb_FieldType key_type,
99                                   upb_FieldType value_type, uint64_t key_mod,
100                                   uint64_t value_mod);
101 
102 // Encodes an entire mini descriptor for a message set.
103 char* upb_MtDataEncoder_EncodeMessageSet(upb_MtDataEncoder* e, char* ptr);
104 
105 #ifdef __cplusplus
106 } /* extern "C" */
107 #endif
108 
109 #include "upb/port/undef.inc"
110 
111 #endif /* UPB_MINI_TABLE_ENCODE_INTERNAL_H_ */
112