Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:25:25

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2023 Google LLC.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 // upb_Encode: parsing from a upb_Message using a upb_MiniTable.
0009 
0010 #ifndef UPB_WIRE_ENCODE_H_
0011 #define UPB_WIRE_ENCODE_H_
0012 
0013 #include <stddef.h>
0014 #include <stdint.h>
0015 
0016 #include "upb/mem/arena.h"
0017 #include "upb/message/message.h"
0018 #include "upb/mini_table/message.h"
0019 
0020 // Must be last.
0021 #include "upb/port/def.inc"
0022 
0023 #ifdef __cplusplus
0024 extern "C" {
0025 #endif
0026 
0027 enum {
0028   /* If set, the results of serializing will be deterministic across all
0029    * instances of this binary. There are no guarantees across different
0030    * binary builds.
0031    *
0032    * If your proto contains maps, the encoder will need to malloc()/free()
0033    * memory during encode. */
0034   kUpb_EncodeOption_Deterministic = 1,
0035 
0036   // When set, unknown fields are not encoded.
0037   kUpb_EncodeOption_SkipUnknown = 2,
0038 
0039   // When set, the encode will fail if any required fields are missing.
0040   kUpb_EncodeOption_CheckRequired = 4,
0041 };
0042 
0043 // LINT.IfChange
0044 typedef enum {
0045   kUpb_EncodeStatus_Ok = 0,
0046   kUpb_EncodeStatus_OutOfMemory = 1,  // Arena alloc failed
0047   kUpb_EncodeStatus_MaxDepthExceeded = 2,
0048 
0049   // kUpb_EncodeOption_CheckRequired failed but the parse otherwise succeeded.
0050   kUpb_EncodeStatus_MissingRequired = 3,
0051 } upb_EncodeStatus;
0052 // LINT.ThenChange(//depot/google3/third_party/protobuf/rust/upb.rs:encode_status)
0053 
0054 UPB_INLINE uint32_t upb_EncodeOptions_MaxDepth(uint16_t depth) {
0055   return (uint32_t)depth << 16;
0056 }
0057 
0058 UPB_INLINE uint16_t upb_EncodeOptions_GetMaxDepth(uint32_t options) {
0059   return options >> 16;
0060 }
0061 
0062 // Enforce an upper bound on recursion depth.
0063 UPB_INLINE int upb_Encode_LimitDepth(uint32_t encode_options, uint32_t limit) {
0064   uint32_t max_depth = upb_EncodeOptions_GetMaxDepth(encode_options);
0065   if (max_depth > limit) max_depth = limit;
0066   return upb_EncodeOptions_MaxDepth(max_depth) | (encode_options & 0xffff);
0067 }
0068 
0069 UPB_API upb_EncodeStatus upb_Encode(const upb_Message* msg,
0070                                     const upb_MiniTable* l, int options,
0071                                     upb_Arena* arena, char** buf, size_t* size);
0072 
0073 // Encodes the message prepended by a varint of the serialized length.
0074 UPB_API upb_EncodeStatus upb_EncodeLengthPrefixed(const upb_Message* msg,
0075                                                   const upb_MiniTable* l,
0076                                                   int options, upb_Arena* arena,
0077                                                   char** buf, size_t* size);
0078 // Utility function for wrapper languages to get an error string from a
0079 // upb_EncodeStatus.
0080 UPB_API const char* upb_EncodeStatus_String(upb_EncodeStatus status);
0081 
0082 #ifdef __cplusplus
0083 } /* extern "C" */
0084 #endif
0085 
0086 #include "upb/port/undef.inc"
0087 
0088 #endif /* UPB_WIRE_ENCODE_H_ */