You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

decode.h 4.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_DECODE_H_
  31. #define UPB_DECODE_H_
  32. #include "upb/extension_registry.h"
  33. #include "upb/msg.h"
  34. /* Must be last. */
  35. #include "upb/port_def.inc"
  36. #ifdef __cplusplus
  37. extern "C"
  38. {
  39. #endif
  40. enum
  41. {
  42. /* If set, strings will alias the input buffer instead of copying into the
  43. * arena. */
  44. kUpb_DecodeOption_AliasString = 1,
  45. /* If set, the parse will return failure if any message is missing any
  46. * required fields when the message data ends. The parse will still continue,
  47. * and the failure will only be reported at the end.
  48. *
  49. * IMPORTANT CAVEATS:
  50. *
  51. * 1. This can throw a false positive failure if an incomplete message is seen
  52. * on the wire but is later completed when the sub-message occurs again.
  53. * For this reason, a second pass is required to verify a failure, to be
  54. * truly robust.
  55. *
  56. * 2. This can return a false success if you are decoding into a message that
  57. * already has some sub-message fields present. If the sub-message does
  58. * not occur in the binary payload, we will never visit it and discover the
  59. * incomplete sub-message. For this reason, this check is only useful for
  60. * implemting ParseFromString() semantics. For MergeFromString(), a
  61. * post-parse validation step will always be necessary. */
  62. kUpb_DecodeOption_CheckRequired = 2,
  63. };
  64. #define UPB_DECODE_MAXDEPTH(depth) ((depth) << 16)
  65. typedef enum
  66. {
  67. kUpb_DecodeStatus_Ok = 0,
  68. kUpb_DecodeStatus_Malformed = 1, // Wire format was corrupt
  69. kUpb_DecodeStatus_OutOfMemory = 2, // Arena alloc failed
  70. kUpb_DecodeStatus_BadUtf8 = 3, // String field had bad UTF-8
  71. kUpb_DecodeStatus_MaxDepthExceeded = 4, // Exceeded UPB_DECODE_MAXDEPTH
  72. // kUpb_DecodeOption_CheckRequired failed (see above), but the parse otherwise
  73. // succeeded.
  74. kUpb_DecodeStatus_MissingRequired = 5,
  75. } upb_DecodeStatus;
  76. upb_DecodeStatus upb_Decode(const char* buf, size_t size, upb_Message* msg, const upb_MiniTable* l, const upb_ExtensionRegistry* extreg, int options, upb_Arena* arena);
  77. #ifdef __cplusplus
  78. } /* extern "C" */
  79. #endif
  80. #include "upb/port_undef.inc"
  81. #endif /* UPB_DECODE_H_ */