1 /*
2  * Copyright (c) 2009-2021, 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 // upb_decode: parsing into a upb_Message using a upb_MiniTable.
29 
30 #ifndef UPB_WIRE_DECODE_H_
31 #define UPB_WIRE_DECODE_H_
32 
33 #include "upb/mem/arena.h"
34 #include "upb/message/message.h"
35 #include "upb/mini_table/extension_registry.h"
36 
37 // Must be last.
38 #include "upb/port/def.inc"
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 enum {
45   /* If set, strings will alias the input buffer instead of copying into the
46    * arena. */
47   kUpb_DecodeOption_AliasString = 1,
48 
49   /* If set, the parse will return failure if any message is missing any
50    * required fields when the message data ends.  The parse will still continue,
51    * and the failure will only be reported at the end.
52    *
53    * IMPORTANT CAVEATS:
54    *
55    * 1. This can throw a false positive failure if an incomplete message is seen
56    *    on the wire but is later completed when the sub-message occurs again.
57    *    For this reason, a second pass is required to verify a failure, to be
58    *    truly robust.
59    *
60    * 2. This can return a false success if you are decoding into a message that
61    *    already has some sub-message fields present.  If the sub-message does
62    *    not occur in the binary payload, we will never visit it and discover the
63    *    incomplete sub-message.  For this reason, this check is only useful for
64    *    implemting ParseFromString() semantics.  For MergeFromString(), a
65    *    post-parse validation step will always be necessary. */
66   kUpb_DecodeOption_CheckRequired = 2,
67 };
68 
upb_DecodeOptions_MaxDepth(uint16_t depth)69 UPB_INLINE uint32_t upb_DecodeOptions_MaxDepth(uint16_t depth) {
70   return (uint32_t)depth << 16;
71 }
72 
upb_DecodeOptions_GetMaxDepth(uint32_t options)73 UPB_INLINE uint16_t upb_DecodeOptions_GetMaxDepth(uint32_t options) {
74   return options >> 16;
75 }
76 
77 // Enforce an upper bound on recursion depth.
upb_Decode_LimitDepth(uint32_t decode_options,uint32_t limit)78 UPB_INLINE int upb_Decode_LimitDepth(uint32_t decode_options, uint32_t limit) {
79   uint32_t max_depth = upb_DecodeOptions_GetMaxDepth(decode_options);
80   if (max_depth > limit) max_depth = limit;
81   return upb_DecodeOptions_MaxDepth(max_depth) | (decode_options & 0xffff);
82 }
83 
84 typedef enum {
85   kUpb_DecodeStatus_Ok = 0,
86   kUpb_DecodeStatus_Malformed = 1,    // Wire format was corrupt
87   kUpb_DecodeStatus_OutOfMemory = 2,  // Arena alloc failed
88   kUpb_DecodeStatus_BadUtf8 = 3,      // String field had bad UTF-8
89   kUpb_DecodeStatus_MaxDepthExceeded =
90       4,  // Exceeded upb_DecodeOptions_MaxDepth
91 
92   // kUpb_DecodeOption_CheckRequired failed (see above), but the parse otherwise
93   // succeeded.
94   kUpb_DecodeStatus_MissingRequired = 5,
95 } upb_DecodeStatus;
96 
97 UPB_API upb_DecodeStatus upb_Decode(const char* buf, size_t size,
98                                     upb_Message* msg, const upb_MiniTable* l,
99                                     const upb_ExtensionRegistry* extreg,
100                                     int options, upb_Arena* arena);
101 
102 #ifdef __cplusplus
103 } /* extern "C" */
104 #endif
105 
106 #include "upb/port/undef.inc"
107 
108 #endif /* UPB_WIRE_DECODE_H_ */
109