Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:19

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_decode: parsing into a upb_Message using a upb_MiniTable.
0009 
0010 #ifndef UPB_WIRE_DECODE_H_
0011 #define UPB_WIRE_DECODE_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/extension_registry.h"
0019 #include "upb/mini_table/message.h"
0020 
0021 // Must be last.
0022 #include "upb/port/def.inc"
0023 
0024 #ifdef __cplusplus
0025 extern "C" {
0026 #endif
0027 
0028 enum {
0029   /* If set, strings will alias the input buffer instead of copying into the
0030    * arena. */
0031   kUpb_DecodeOption_AliasString = 1,
0032 
0033   /* If set, the parse will return failure if any message is missing any
0034    * required fields when the message data ends.  The parse will still continue,
0035    * and the failure will only be reported at the end.
0036    *
0037    * IMPORTANT CAVEATS:
0038    *
0039    * 1. This can throw a false positive failure if an incomplete message is seen
0040    *    on the wire but is later completed when the sub-message occurs again.
0041    *    For this reason, a second pass is required to verify a failure, to be
0042    *    truly robust.
0043    *
0044    * 2. This can return a false success if you are decoding into a message that
0045    *    already has some sub-message fields present.  If the sub-message does
0046    *    not occur in the binary payload, we will never visit it and discover the
0047    *    incomplete sub-message.  For this reason, this check is only useful for
0048    *    implemting ParseFromString() semantics.  For MergeFromString(), a
0049    *    post-parse validation step will always be necessary. */
0050   kUpb_DecodeOption_CheckRequired = 2,
0051 
0052   /* EXPERIMENTAL:
0053    *
0054    * If set, the parser will allow parsing of sub-message fields that were not
0055    * previously linked using upb_MiniTable_SetSubMessage().  The data will be
0056    * parsed into an internal "empty" message type that cannot be accessed
0057    * directly, but can be later promoted into the true message type if the
0058    * sub-message fields are linked at a later time.
0059    *
0060    * Users should set this option if they intend to perform dynamic tree shaking
0061    * and promoting using the interfaces in message/promote.h.  If this option is
0062    * enabled, it is important that the resulting messages are only accessed by
0063    * code that is aware of promotion rules:
0064    *
0065    * 1. Message pointers in upb_Message, upb_Array, and upb_Map are represented
0066    *    by a tagged pointer upb_TaggedMessagePointer.  The tag indicates whether
0067    *    the message uses the internal "empty" type.
0068    *
0069    * 2. Any code *reading* these message pointers must test whether the "empty"
0070    *    tag bit is set, using the interfaces in mini_table/types.h.  However
0071    *    writing of message pointers should always use plain upb_Message*, since
0072    *    users are not allowed to create "empty" messages.
0073    *
0074    * 3. It is always safe to test whether a field is present or test the array
0075    *    length; these interfaces will reflect that empty messages are present,
0076    *    even though their data cannot be accessed without promoting first.
0077    *
0078    * 4. If a message pointer is indeed tagged as empty, the message may not be
0079    *    accessed directly, only promoted through the interfaces in
0080    *    message/promote.h.
0081    *
0082    * 5. Tagged/empty messages may never be created by the user.  They may only
0083    *    be created by the parser or the message-copying logic in message/copy.h.
0084    */
0085   kUpb_DecodeOption_ExperimentalAllowUnlinked = 4,
0086 
0087   /* EXPERIMENTAL:
0088    *
0089    * If set, decoding will enforce UTF-8 validation for string fields, even for
0090    * proto2 or fields with `features.utf8_validation = NONE`. Normally, only
0091    * proto3 string fields will be validated for UTF-8. Decoding will return
0092    * kUpb_DecodeStatus_BadUtf8 for non-UTF-8 strings, which is the same behavior
0093    * as non-UTF-8 proto3 string fields.
0094    */
0095   kUpb_DecodeOption_AlwaysValidateUtf8 = 8,
0096 };
0097 
0098 UPB_INLINE uint32_t upb_DecodeOptions_MaxDepth(uint16_t depth) {
0099   return (uint32_t)depth << 16;
0100 }
0101 
0102 UPB_INLINE uint16_t upb_DecodeOptions_GetMaxDepth(uint32_t options) {
0103   return options >> 16;
0104 }
0105 
0106 // Enforce an upper bound on recursion depth.
0107 UPB_INLINE int upb_Decode_LimitDepth(uint32_t decode_options, uint32_t limit) {
0108   uint32_t max_depth = upb_DecodeOptions_GetMaxDepth(decode_options);
0109   if (max_depth > limit) max_depth = limit;
0110   return upb_DecodeOptions_MaxDepth(max_depth) | (decode_options & 0xffff);
0111 }
0112 
0113 // LINT.IfChange
0114 typedef enum {
0115   kUpb_DecodeStatus_Ok = 0,
0116   kUpb_DecodeStatus_Malformed = 1,    // Wire format was corrupt
0117   kUpb_DecodeStatus_OutOfMemory = 2,  // Arena alloc failed
0118   kUpb_DecodeStatus_BadUtf8 = 3,      // String field had bad UTF-8
0119   kUpb_DecodeStatus_MaxDepthExceeded =
0120       4,  // Exceeded upb_DecodeOptions_MaxDepth
0121 
0122   // kUpb_DecodeOption_CheckRequired failed (see above), but the parse otherwise
0123   // succeeded.
0124   kUpb_DecodeStatus_MissingRequired = 5,
0125 
0126   // Unlinked sub-message field was present, but
0127   // kUpb_DecodeOptions_ExperimentalAllowUnlinked was not specified in the list
0128   // of options.
0129   kUpb_DecodeStatus_UnlinkedSubMessage = 6,
0130 } upb_DecodeStatus;
0131 // LINT.ThenChange(//depot/google3/third_party/protobuf/rust/upb.rs:decode_status)
0132 
0133 UPB_API upb_DecodeStatus upb_Decode(const char* buf, size_t size,
0134                                     upb_Message* msg, const upb_MiniTable* mt,
0135                                     const upb_ExtensionRegistry* extreg,
0136                                     int options, upb_Arena* arena);
0137 
0138 // Same as upb_Decode but with a varint-encoded length prepended.
0139 // On success 'num_bytes_read' will be set to the how many bytes were read,
0140 // on failure the contents of num_bytes_read is undefined.
0141 UPB_API upb_DecodeStatus upb_DecodeLengthPrefixed(
0142     const char* buf, size_t size, upb_Message* msg, size_t* num_bytes_read,
0143     const upb_MiniTable* mt, const upb_ExtensionRegistry* extreg, int options,
0144     upb_Arena* arena);
0145 
0146 // Utility function for wrapper languages to get an error string from a
0147 // upb_DecodeStatus.
0148 UPB_API const char* upb_DecodeStatus_String(upb_DecodeStatus status);
0149 #ifdef __cplusplus
0150 } /* extern "C" */
0151 #endif
0152 
0153 #include "upb/port/undef.inc"
0154 
0155 #endif /* UPB_WIRE_DECODE_H_ */