ruby_memprofiler_pprof 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. checksums.yaml +7 -0
  2. data/ext/ruby_memprofiler_pprof/backtrace.c +429 -0
  3. data/ext/ruby_memprofiler_pprof/collector.c +1055 -0
  4. data/ext/ruby_memprofiler_pprof/compat.c +182 -0
  5. data/ext/ruby_memprofiler_pprof/extconf.rb +72 -0
  6. data/ext/ruby_memprofiler_pprof/pprof.upb.c +170 -0
  7. data/ext/ruby_memprofiler_pprof/pprof.upb.h +848 -0
  8. data/ext/ruby_memprofiler_pprof/pprof_out.c +285 -0
  9. data/ext/ruby_memprofiler_pprof/ruby_memprofiler_pprof.c +11 -0
  10. data/ext/ruby_memprofiler_pprof/ruby_memprofiler_pprof.h +301 -0
  11. data/ext/ruby_memprofiler_pprof/strtab.c +391 -0
  12. data/ext/ruby_memprofiler_pprof/vendor/upb/BUILD +719 -0
  13. data/ext/ruby_memprofiler_pprof/vendor/upb/CONTRIBUTING.md +37 -0
  14. data/ext/ruby_memprofiler_pprof/vendor/upb/DESIGN.md +201 -0
  15. data/ext/ruby_memprofiler_pprof/vendor/upb/LICENSE +26 -0
  16. data/ext/ruby_memprofiler_pprof/vendor/upb/README.md +78 -0
  17. data/ext/ruby_memprofiler_pprof/vendor/upb/WORKSPACE +58 -0
  18. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/BUILD +53 -0
  19. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/amalgamate.py +129 -0
  20. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/build_defs.bzl +160 -0
  21. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/lua.BUILD +127 -0
  22. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/protobuf.patch +54 -0
  23. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/py_proto_library.bzl +137 -0
  24. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/python_downloads.bzl +84 -0
  25. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/system_python.bzl +101 -0
  26. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/upb_proto_library.bzl +388 -0
  27. data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/workspace_deps.bzl +89 -0
  28. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/BUILD +252 -0
  29. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/BUILD.googleapis +54 -0
  30. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/benchmark.cc +333 -0
  31. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/build_defs.bzl +88 -0
  32. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/compare.py +118 -0
  33. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/descriptor.proto +888 -0
  34. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/descriptor_sv.proto +890 -0
  35. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/empty.proto +6 -0
  36. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_protobuf_binary_cc.py +64 -0
  37. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_synthetic_protos.py +118 -0
  38. data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_upb_binary_c.py +65 -0
  39. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/BUILD.bazel +102 -0
  40. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/README.md +23 -0
  41. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/build_defs.bzl +73 -0
  42. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/make_cmakelists.py +340 -0
  43. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/staleness_test.py +57 -0
  44. data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/staleness_test_lib.py +186 -0
  45. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/render.py +43 -0
  46. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/style-guide.md +65 -0
  47. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/vs-cpp-protos.md +255 -0
  48. data/ext/ruby_memprofiler_pprof/vendor/upb/docs/wrapping-upb.md +444 -0
  49. data/ext/ruby_memprofiler_pprof/vendor/upb/python/BUILD +216 -0
  50. data/ext/ruby_memprofiler_pprof/vendor/upb/python/convert.c +394 -0
  51. data/ext/ruby_memprofiler_pprof/vendor/upb/python/convert.h +63 -0
  52. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor.c +1694 -0
  53. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor.h +80 -0
  54. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_containers.c +704 -0
  55. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_containers.h +114 -0
  56. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_pool.c +650 -0
  57. data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_pool.h +48 -0
  58. data/ext/ruby_memprofiler_pprof/vendor/upb/python/dist/BUILD.bazel +193 -0
  59. data/ext/ruby_memprofiler_pprof/vendor/upb/python/dist/dist.bzl +190 -0
  60. data/ext/ruby_memprofiler_pprof/vendor/upb/python/extension_dict.c +247 -0
  61. data/ext/ruby_memprofiler_pprof/vendor/upb/python/extension_dict.h +39 -0
  62. data/ext/ruby_memprofiler_pprof/vendor/upb/python/map.c +522 -0
  63. data/ext/ruby_memprofiler_pprof/vendor/upb/python/map.h +66 -0
  64. data/ext/ruby_memprofiler_pprof/vendor/upb/python/message.c +1909 -0
  65. data/ext/ruby_memprofiler_pprof/vendor/upb/python/message.h +101 -0
  66. data/ext/ruby_memprofiler_pprof/vendor/upb/python/minimal_test.py +183 -0
  67. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/BUILD +70 -0
  68. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/README.md +11 -0
  69. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_database_test_wrapper.py +30 -0
  70. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_pool_test_wrapper.py +45 -0
  71. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_test_wrapper.py +46 -0
  72. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/generator_test_wrapper.py +30 -0
  73. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/json_format_test_wrapper.py +30 -0
  74. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/keywords_test_wrapper.py +30 -0
  75. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/message_factory_test_wrapper.py +37 -0
  76. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/message_test_wrapper.py +52 -0
  77. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/proto_builder_test_wrapper.py +32 -0
  78. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/pyproto_test_wrapper.bzl +36 -0
  79. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/reflection_test_wrapper.py +45 -0
  80. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/service_reflection_test_wrapper.py +30 -0
  81. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/symbol_database_test_wrapper.py +30 -0
  82. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/text_encoding_test_wrapper.py +30 -0
  83. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/text_format_test_wrapper.py +30 -0
  84. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/unknown_fields_test_wrapper.py +30 -0
  85. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/well_known_types_test_wrapper.py +36 -0
  86. data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/wire_format_test_wrapper.py +30 -0
  87. data/ext/ruby_memprofiler_pprof/vendor/upb/python/protobuf.c +350 -0
  88. data/ext/ruby_memprofiler_pprof/vendor/upb/python/protobuf.h +230 -0
  89. data/ext/ruby_memprofiler_pprof/vendor/upb/python/py_extension.bzl +55 -0
  90. data/ext/ruby_memprofiler_pprof/vendor/upb/python/python_api.h +61 -0
  91. data/ext/ruby_memprofiler_pprof/vendor/upb/python/repeated.c +828 -0
  92. data/ext/ruby_memprofiler_pprof/vendor/upb/python/repeated.h +69 -0
  93. data/ext/ruby_memprofiler_pprof/vendor/upb/python/unknown_fields.c +404 -0
  94. data/ext/ruby_memprofiler_pprof/vendor/upb/python/unknown_fields.h +39 -0
  95. data/ext/ruby_memprofiler_pprof/vendor/upb/python/version_script.lds +6 -0
  96. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/LICENSE +32 -0
  97. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/README.google +9 -0
  98. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/console.lua +156 -0
  99. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/lunit.lua +725 -0
  100. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/BUILD +19 -0
  101. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/LICENSE +21 -0
  102. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/naive.c +92 -0
  103. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/range2-neon.c +157 -0
  104. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/range2-sse.c +170 -0
  105. data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/utf8_range.h +9 -0
  106. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/BUILD.bazel +129 -0
  107. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/README.md +8 -0
  108. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/def.c +939 -0
  109. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/lua_proto_library.bzl +138 -0
  110. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/main.c +83 -0
  111. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/msg.c +1118 -0
  112. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/test.proto +69 -0
  113. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/test_upb.lua +846 -0
  114. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.c +258 -0
  115. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.h +132 -0
  116. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.lua +58 -0
  117. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upbc.cc +134 -0
  118. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/collections.c +192 -0
  119. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/collections.h +174 -0
  120. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/conformance_upb.c +346 -0
  121. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/conformance_upb_failures.txt +1 -0
  122. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode.c +1221 -0
  123. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode.h +94 -0
  124. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_fast.c +1055 -0
  125. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_fast.h +153 -0
  126. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_internal.h +211 -0
  127. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.c +3262 -0
  128. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.h +414 -0
  129. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.hpp +438 -0
  130. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/empty.proto +1 -0
  131. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/encode.c +604 -0
  132. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/encode.h +71 -0
  133. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/fuzz/BUILD +13 -0
  134. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/fuzz/file_descriptor_parsenew_fuzzer.cc +43 -0
  135. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_decode.c +1509 -0
  136. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_decode.h +47 -0
  137. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_encode.c +776 -0
  138. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_encode.h +62 -0
  139. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.c +1147 -0
  140. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.h +189 -0
  141. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.hpp +112 -0
  142. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors.c +363 -0
  143. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors.h +263 -0
  144. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors_internal.h +59 -0
  145. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors_test.cc +425 -0
  146. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_test.cc +230 -0
  147. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg.c +428 -0
  148. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg.h +114 -0
  149. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_internal.h +836 -0
  150. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_test.cc +491 -0
  151. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_test.proto +195 -0
  152. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/port_def.inc +261 -0
  153. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/port_undef.inc +62 -0
  154. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.c +323 -0
  155. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.h +109 -0
  156. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.hpp +37 -0
  157. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/table.c +926 -0
  158. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/table_internal.h +385 -0
  159. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test.proto +74 -0
  160. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_cpp.cc +186 -0
  161. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_cpp.proto +12 -0
  162. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_generated_code.cc +977 -0
  163. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_table.cc +580 -0
  164. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/text_encode.c +472 -0
  165. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/text_encode.h +64 -0
  166. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.c +362 -0
  167. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.h +378 -0
  168. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.hpp +115 -0
  169. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb_internal.h +68 -0
  170. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/BUILD +121 -0
  171. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/README.md +7 -0
  172. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare.c +300 -0
  173. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare.h +66 -0
  174. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare_test.cc +236 -0
  175. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto.c +572 -0
  176. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto.h +62 -0
  177. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_public_import_test.proto +32 -0
  178. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_regular_import_test.proto +36 -0
  179. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_test.cc +143 -0
  180. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_test.proto +119 -0
  181. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_weak_import_test.proto +28 -0
  182. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_wweak_import_test.proto +28 -0
  183. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields.c +311 -0
  184. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields.h +94 -0
  185. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields_test.cc +202 -0
  186. data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields_test.proto +48 -0
  187. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/BUILD +78 -0
  188. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/common.cc +77 -0
  189. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/common.h +112 -0
  190. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/protoc-gen-upb.cc +1997 -0
  191. data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/protoc-gen-upbdefs.cc +193 -0
  192. data/lib/ruby_memprofiler_pprof/atfork.rb +77 -0
  193. data/lib/ruby_memprofiler_pprof/block_flusher.rb +61 -0
  194. data/lib/ruby_memprofiler_pprof/file_flusher.rb +45 -0
  195. data/lib/ruby_memprofiler_pprof/profile_app.rb +30 -0
  196. data/lib/ruby_memprofiler_pprof/profile_data.rb +18 -0
  197. data/lib/ruby_memprofiler_pprof/version.rb +5 -0
  198. data/lib/ruby_memprofiler_pprof.rb +8 -0
  199. data/libexec/ruby_memprofiler_pprof_profile +16 -0
  200. metadata +257 -0
@@ -0,0 +1,189 @@
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_H_
29
+ #define UPB_MINI_TABLE_H_
30
+
31
+ #include "upb/msg_internal.h"
32
+
33
+ // Must be last.
34
+ #include "upb/port_def.inc"
35
+
36
+ #ifdef __cplusplus
37
+ extern "C" {
38
+ #endif
39
+
40
+ const upb_MiniTable_Field* upb_MiniTable_FindFieldByNumber(
41
+ const upb_MiniTable* table, uint32_t number);
42
+
43
+ UPB_INLINE const upb_MiniTable* upb_MiniTable_GetSubMessageTable(
44
+ const upb_MiniTable* mini_table, const upb_MiniTable_Field* field) {
45
+ return mini_table->subs[field->submsg_index].submsg;
46
+ }
47
+
48
+ UPB_INLINE bool upb_MiniTable_Enum_CheckValue(const upb_MiniTable_Enum* e,
49
+ int32_t val) {
50
+ uint32_t uval = (uint32_t)val;
51
+ if (uval < 64) return e->mask & (1ULL << uval);
52
+ // OPT: binary search long lists?
53
+ int n = e->value_count;
54
+ for (int i = 0; i < n; i++) {
55
+ if (e->values[i] == val) return true;
56
+ }
57
+ return false;
58
+ }
59
+
60
+ /** upb_MtDataEncoder *********************************************************/
61
+
62
+ // Functions to encode a string in a format that can be loaded by
63
+ // upb_MiniTable_Build().
64
+
65
+ typedef enum {
66
+ kUpb_MessageModifier_ValidateUtf8 = 1 << 0,
67
+ kUpb_MessageModifier_DefaultIsPacked = 1 << 1,
68
+ kUpb_MessageModifier_IsExtendable = 1 << 2,
69
+ } kUpb_MessageModifier;
70
+
71
+ typedef enum {
72
+ kUpb_FieldModifier_IsRepeated = 1 << 0,
73
+ kUpb_FieldModifier_IsPacked = 1 << 1,
74
+ kUpb_FieldModifier_IsClosedEnum = 1 << 2,
75
+ kUpb_FieldModifier_IsProto3Singular = 1 << 3,
76
+ kUpb_FieldModifier_IsRequired = 1 << 4,
77
+ } kUpb_FieldModifier;
78
+
79
+ typedef struct {
80
+ char* end; // Limit of the buffer passed as a parameter.
81
+ // Aliased to internal-only members in .cc.
82
+ char internal[32];
83
+ } upb_MtDataEncoder;
84
+
85
+ // If the input buffer has at least this many bytes available, the encoder call
86
+ // is guaranteed to succeed (as long as field number order is maintained).
87
+ #define kUpb_MtDataEncoder_MinSize 16
88
+
89
+ // Encodes field/oneof information for a given message. The sequence of calls
90
+ // should look like:
91
+ //
92
+ // upb_MtDataEncoder e;
93
+ // char buf[256];
94
+ // char* ptr = buf;
95
+ // e.end = ptr + sizeof(buf);
96
+ // ptr = upb_MtDataEncoder_StartMessage(&e, ptr);
97
+ // // Fields *must* be in field number order.
98
+ // ptr = upb_MtDataEncoder_PutField(&e, ptr, ...);
99
+ // ptr = upb_MtDataEncoder_PutField(&e, ptr, ...);
100
+ // ptr = upb_MtDataEncoder_PutField(&e, ptr, ...);
101
+ //
102
+ // // If oneofs are present. Oneofs must be encoded after regular fields.
103
+ // ptr = upb_MiniTable_StartOneof(&e, ptr)
104
+ // ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
105
+ // ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
106
+ //
107
+ // ptr = upb_MiniTable_StartOneof(&e, ptr);
108
+ // ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
109
+ // ptr = upb_MiniTable_PutOneofField(&e, ptr, ...);
110
+ //
111
+ // Oneofs must be encoded after all regular fields.
112
+ char* upb_MtDataEncoder_StartMessage(upb_MtDataEncoder* e, char* ptr,
113
+ uint64_t msg_mod);
114
+ char* upb_MtDataEncoder_PutField(upb_MtDataEncoder* e, char* ptr,
115
+ upb_FieldType type, uint32_t field_num,
116
+ uint64_t field_mod);
117
+ char* upb_MtDataEncoder_StartOneof(upb_MtDataEncoder* e, char* ptr);
118
+ char* upb_MtDataEncoder_PutOneofField(upb_MtDataEncoder* e, char* ptr,
119
+ uint32_t field_num);
120
+
121
+ // Encodes the set of values for a given enum. The values must be given in
122
+ // order (after casting to uint32_t), and repeats are not allowed.
123
+ void upb_MtDataEncoder_StartEnum(upb_MtDataEncoder* e);
124
+ char* upb_MtDataEncoder_PutEnumValue(upb_MtDataEncoder* e, char* ptr,
125
+ uint32_t val);
126
+ char* upb_MtDataEncoder_EndEnum(upb_MtDataEncoder* e, char* ptr);
127
+
128
+ /** upb_MiniTable *************************************************************/
129
+
130
+ typedef enum {
131
+ kUpb_MiniTablePlatform_32Bit,
132
+ kUpb_MiniTablePlatform_64Bit,
133
+ kUpb_MiniTablePlatform_Native =
134
+ UPB_SIZE(kUpb_MiniTablePlatform_32Bit, kUpb_MiniTablePlatform_64Bit),
135
+ } upb_MiniTablePlatform;
136
+
137
+ // Builds a mini table from the data encoded in the buffer [data, len]. If any
138
+ // errors occur, returns NULL and sets a status message. In the success case,
139
+ // the caller must call upb_MiniTable_SetSub*() for all message or proto2 enum
140
+ // fields to link the table to the appropriate sub-tables.
141
+ upb_MiniTable* upb_MiniTable_Build(const char* data, size_t len,
142
+ upb_MiniTablePlatform platform,
143
+ upb_Arena* arena, upb_Status* status);
144
+ void upb_MiniTable_SetSubMessage(upb_MiniTable* table,
145
+ upb_MiniTable_Field* field,
146
+ const upb_MiniTable* sub);
147
+ void upb_MiniTable_SetSubEnum(upb_MiniTable* table, upb_MiniTable_Field* field,
148
+ const upb_MiniTable_Enum* sub);
149
+
150
+ bool upb_MiniTable_BuildExtension(const char* data, size_t len,
151
+ upb_MiniTable_Extension* ext,
152
+ upb_MiniTable_Sub sub, upb_Status* status);
153
+
154
+ // Special-case functions for MessageSet layout and map entries.
155
+ upb_MiniTable* upb_MiniTable_BuildMessageSet(upb_MiniTablePlatform platform,
156
+ upb_Arena* arena);
157
+ upb_MiniTable* upb_MiniTable_BuildMapEntry(upb_FieldType key_type,
158
+ upb_FieldType value_type,
159
+ bool value_is_proto3_enum,
160
+ upb_MiniTablePlatform platform,
161
+ upb_Arena* arena);
162
+
163
+ upb_MiniTable_Enum* upb_MiniTable_BuildEnum(const char* data, size_t len,
164
+ upb_Arena* arena,
165
+ upb_Status* status);
166
+
167
+ // Like upb_MiniTable_Build(), but the user provides a buffer of layout data so
168
+ // it can be reused from call to call, avoiding repeated realloc()/free().
169
+ //
170
+ // The caller owns `*buf` both before and after the call, and must free() it
171
+ // when it is no longer in use. The function will realloc() `*buf` as
172
+ // necessary, updating `*size` accordingly.
173
+ upb_MiniTable* upb_MiniTable_BuildWithBuf(const char* data, size_t len,
174
+ upb_MiniTablePlatform platform,
175
+ upb_Arena* arena, void** buf,
176
+ size_t* buf_size, upb_Status* status);
177
+
178
+ // For testing only.
179
+ char upb_ToBase92(int8_t ch);
180
+ char upb_FromBase92(uint8_t ch);
181
+ bool upb_IsTypePackable(upb_FieldType type);
182
+
183
+ #ifdef __cplusplus
184
+ } /* extern "C" */
185
+ #endif
186
+
187
+ #include "upb/port_undef.inc"
188
+
189
+ #endif /* UPB_MINI_TABLE_H_ */
@@ -0,0 +1,112 @@
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
+ #ifndef UPB_MINI_TABLE_HPP_
29
+ #define UPB_MINI_TABLE_HPP_
30
+
31
+ #include <assert.h>
32
+
33
+ #include <string>
34
+
35
+ #include "upb/mini_table.h"
36
+
37
+ namespace upb {
38
+
39
+ class MtDataEncoder {
40
+ public:
41
+ MtDataEncoder() : appender_(&encoder_) {}
42
+
43
+ bool StartMessage(uint64_t msg_mod) {
44
+ return appender_([=](char* buf) {
45
+ return upb_MtDataEncoder_StartMessage(&encoder_, buf, msg_mod);
46
+ });
47
+ }
48
+
49
+ bool PutField(upb_FieldType type, uint32_t field_num, uint64_t field_mod) {
50
+ return appender_([=](char* buf) {
51
+ return upb_MtDataEncoder_PutField(&encoder_, buf, type, field_num,
52
+ field_mod);
53
+ });
54
+ }
55
+
56
+ bool StartOneof() {
57
+ return appender_([=](char* buf) {
58
+ return upb_MtDataEncoder_StartOneof(&encoder_, buf);
59
+ });
60
+ }
61
+
62
+ bool PutOneofField(uint32_t field_num) {
63
+ return appender_([=](char* buf) {
64
+ return upb_MtDataEncoder_PutOneofField(&encoder_, buf, field_num);
65
+ });
66
+ }
67
+
68
+ void StartEnum() { upb_MtDataEncoder_StartEnum(&encoder_); }
69
+
70
+ bool PutEnumValue(uint32_t enum_value) {
71
+ return appender_([=](char* buf) {
72
+ return upb_MtDataEncoder_PutEnumValue(&encoder_, buf, enum_value);
73
+ });
74
+ }
75
+
76
+ bool EndEnum() {
77
+ return appender_(
78
+ [=](char* buf) { return upb_MtDataEncoder_EndEnum(&encoder_, buf); });
79
+ }
80
+
81
+ const std::string& data() const { return appender_.data(); }
82
+
83
+ private:
84
+ class StringAppender {
85
+ public:
86
+ StringAppender(upb_MtDataEncoder* e) { e->end = buf_ + sizeof(buf_); }
87
+
88
+ template <class T>
89
+ bool operator()(T&& func) {
90
+ char* end = func(buf_);
91
+ if (!end) return false;
92
+ // C++ does not guarantee that string has doubling growth behavior, but
93
+ // we need it to avoid O(n^2).
94
+ str_.reserve(_upb_Log2CeilingSize(str_.size() + (end - buf_)));
95
+ str_.append(buf_, end - buf_);
96
+ return true;
97
+ }
98
+
99
+ const std::string& data() const { return str_; }
100
+
101
+ private:
102
+ char buf_[kUpb_MtDataEncoder_MinSize];
103
+ std::string str_;
104
+ };
105
+
106
+ upb_MtDataEncoder encoder_;
107
+ StringAppender appender_;
108
+ };
109
+
110
+ } // namespace upb
111
+
112
+ #endif /* UPB_MINI_TABLE_HPP_ */
@@ -0,0 +1,363 @@
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
+ #include "upb/mini_table_accessors.h"
29
+
30
+ #include "upb/mini_table.h"
31
+ #include "upb/msg_internal.h"
32
+
33
+ // Must be last.
34
+ #include "upb/port_def.inc"
35
+
36
+ size_t upb_MiniTable_Field_GetSize(const upb_MiniTable_Field* f) {
37
+ static unsigned char sizes[] = {
38
+ 0, /* 0 */
39
+ 8, /* kUpb_FieldType_Double */
40
+ 4, /* kUpb_FieldType_Float */
41
+ 8, /* kUpb_FieldType_Int64 */
42
+ 8, /* kUpb_FieldType_UInt64 */
43
+ 4, /* kUpb_FieldType_Int32 */
44
+ 8, /* kUpb_FieldType_Fixed64 */
45
+ 4, /* kUpb_FieldType_Fixed32 */
46
+ 1, /* kUpb_FieldType_Bool */
47
+ sizeof(upb_StringView), /* kUpb_FieldType_String */
48
+ sizeof(void*), /* kUpb_FieldType_Group */
49
+ sizeof(void*), /* kUpb_FieldType_Message */
50
+ sizeof(upb_StringView), /* kUpb_FieldType_Bytes */
51
+ 4, /* kUpb_FieldType_UInt32 */
52
+ 4, /* kUpb_FieldType_Enum */
53
+ 4, /* kUpb_FieldType_SFixed32 */
54
+ 8, /* kUpb_FieldType_SFixed64 */
55
+ 4, /* kUpb_FieldType_SInt32 */
56
+ 8, /* kUpb_FieldType_SInt64 */
57
+ };
58
+ return upb_IsRepeatedOrMap(f) ? sizeof(void*) : sizes[f->descriptortype];
59
+ }
60
+
61
+ bool upb_MiniTable_HasField(const upb_Message* msg,
62
+ const upb_MiniTable_Field* field) {
63
+ if (_upb_MiniTable_Field_InOneOf(field)) {
64
+ return _upb_getoneofcase_field(msg, field) == field->number;
65
+ } else if (field->presence > 0) {
66
+ return _upb_hasbit_field(msg, field);
67
+ } else {
68
+ UPB_ASSERT(field->descriptortype == kUpb_FieldType_Message ||
69
+ field->descriptortype == kUpb_FieldType_Group);
70
+ return upb_MiniTable_GetMessage(msg, field) != NULL;
71
+ }
72
+ }
73
+
74
+ void upb_MiniTable_ClearField(upb_Message* msg,
75
+ const upb_MiniTable_Field* field) {
76
+ char* mem = UPB_PTR_AT(msg, field->offset, char);
77
+ if (field->presence > 0) {
78
+ _upb_clearhas_field(msg, field);
79
+ } else if (_upb_MiniTable_Field_InOneOf(field)) {
80
+ uint32_t* oneof_case = _upb_oneofcase_field(msg, field);
81
+ if (*oneof_case != field->number) return;
82
+ *oneof_case = 0;
83
+ }
84
+ memset(mem, 0, upb_MiniTable_Field_GetSize(field));
85
+ }
86
+
87
+ typedef struct {
88
+ const char* ptr;
89
+ uint64_t val;
90
+ } decode_vret;
91
+
92
+ UPB_NOINLINE
93
+ static decode_vret decode_longvarint64(const char* ptr, uint64_t val) {
94
+ decode_vret ret = {NULL, 0};
95
+ uint64_t byte;
96
+ int i;
97
+ for (i = 1; i < 10; i++) {
98
+ byte = (uint8_t)ptr[i];
99
+ val += (byte - 1) << (i * 7);
100
+ if (!(byte & 0x80)) {
101
+ ret.ptr = ptr + i + 1;
102
+ ret.val = val;
103
+ return ret;
104
+ }
105
+ }
106
+ return ret;
107
+ }
108
+
109
+ UPB_FORCEINLINE
110
+ static const char* decode_varint64(const char* ptr, uint64_t* val) {
111
+ uint64_t byte = (uint8_t)*ptr;
112
+ if (UPB_LIKELY((byte & 0x80) == 0)) {
113
+ *val = byte;
114
+ return ptr + 1;
115
+ } else {
116
+ decode_vret res = decode_longvarint64(ptr, byte);
117
+ if (!res.ptr) return NULL;
118
+ *val = res.val;
119
+ return res.ptr;
120
+ }
121
+ }
122
+
123
+ UPB_FORCEINLINE
124
+ static const char* decode_tag(const char* ptr, uint32_t* val) {
125
+ uint64_t byte = (uint8_t)*ptr;
126
+ if (UPB_LIKELY((byte & 0x80) == 0)) {
127
+ *val = (uint32_t)byte;
128
+ return ptr + 1;
129
+ } else {
130
+ const char* start = ptr;
131
+ decode_vret res = decode_longvarint64(ptr, byte);
132
+ if (!res.ptr || res.ptr - start > 5 || res.val > UINT32_MAX) {
133
+ return NULL; // Malformed.
134
+ }
135
+ *val = (uint32_t)res.val;
136
+ return res.ptr;
137
+ }
138
+ }
139
+
140
+ typedef enum {
141
+ kUpb_FindUnknown_Ok,
142
+ kUpb_FindUnknown_NotPresent,
143
+ kUpb_FindUnknown_ParseError,
144
+ } upb_FindUnknown_Status;
145
+
146
+ typedef struct {
147
+ upb_FindUnknown_Status status;
148
+ const char* ptr;
149
+ size_t len;
150
+ } find_unknown_ret;
151
+
152
+ static find_unknown_ret UnknownFieldSet_FindField(const upb_Message* msg,
153
+ int field_number);
154
+
155
+ upb_GetExtension_Status upb_MiniTable_GetOrPromoteExtension(
156
+ upb_Message* msg, const upb_MiniTable_Extension* ext_table,
157
+ int decode_options, upb_Arena* arena,
158
+ const upb_Message_Extension** extension) {
159
+ UPB_ASSERT(ext_table->field.descriptortype == kUpb_FieldType_Message);
160
+ *extension = _upb_Message_Getext(msg, ext_table);
161
+ if (*extension) {
162
+ return kUpb_GetExtension_Ok;
163
+ }
164
+
165
+ // Check unknown fields, if available promote.
166
+ int field_number = ext_table->field.number;
167
+ find_unknown_ret result = UnknownFieldSet_FindField(msg, field_number);
168
+ if (result.status != kUpb_FindUnknown_Ok) {
169
+ UPB_ASSERT(result.status != kUpb_GetExtension_ParseError);
170
+ return kUpb_GetExtension_NotPresent;
171
+ }
172
+ // Decode and promote from unknown.
173
+ const upb_MiniTable* extension_table = ext_table->sub.submsg;
174
+ upb_Message* extension_msg = _upb_Message_New(extension_table, arena);
175
+ if (!extension_msg) {
176
+ return kUpb_GetExtension_OutOfMemory;
177
+ }
178
+ const char* data = result.ptr;
179
+ uint32_t tag;
180
+ uint64_t message_len;
181
+ data = decode_tag(data, &tag);
182
+ data = decode_varint64(data, &message_len);
183
+ upb_DecodeStatus status =
184
+ upb_Decode(data, message_len, extension_msg, extension_table, NULL,
185
+ decode_options, arena);
186
+ if (status == kUpb_DecodeStatus_OutOfMemory) {
187
+ return kUpb_GetExtension_OutOfMemory;
188
+ }
189
+ if (status != kUpb_DecodeStatus_Ok) return kUpb_GetExtension_ParseError;
190
+ // Add to extensions.
191
+ upb_Message_Extension* ext =
192
+ _upb_Message_GetOrCreateExtension(msg, ext_table, arena);
193
+ if (!ext) {
194
+ return kUpb_GetExtension_OutOfMemory;
195
+ }
196
+ memcpy(&ext->data, &extension_msg, sizeof(extension_msg));
197
+ *extension = ext;
198
+ // Remove unknown field.
199
+ upb_Message_Internal* in = upb_Message_Getinternal(msg);
200
+ const char* internal_unknown_end =
201
+ UPB_PTR_AT(in->internal, in->internal->unknown_end, char);
202
+ if ((result.ptr + result.len) != internal_unknown_end) {
203
+ memmove((char*)result.ptr, result.ptr + result.len,
204
+ internal_unknown_end - result.ptr - result.len);
205
+ }
206
+ in->internal->unknown_end -= result.len;
207
+ return kUpb_GetExtension_Ok;
208
+ }
209
+
210
+ upb_GetExtensionAsBytes_Status upb_MiniTable_GetExtensionAsBytes(
211
+ const upb_Message* msg, const upb_MiniTable_Extension* ext_table,
212
+ int encode_options, upb_Arena* arena, const char** extension_data,
213
+ size_t* len) {
214
+ const upb_Message_Extension* msg_ext = _upb_Message_Getext(msg, ext_table);
215
+ UPB_ASSERT(ext_table->field.descriptortype == kUpb_FieldType_Message);
216
+ if (msg_ext) {
217
+ *extension_data = upb_Encode(msg_ext->data.ptr, msg_ext->ext->sub.submsg,
218
+ encode_options, arena, len);
219
+ if (extension_data) {
220
+ return kUpb_GetExtensionAsBytes_Ok;
221
+ }
222
+ return kUpb_GetExtensionAsBytes_EncodeError;
223
+ }
224
+ int field_number = ext_table->field.number;
225
+ find_unknown_ret result = UnknownFieldSet_FindField(msg, field_number);
226
+ if (result.status != kUpb_FindUnknown_Ok) {
227
+ UPB_ASSERT(result.status != kUpb_GetExtension_ParseError);
228
+ return kUpb_GetExtensionAsBytes_NotPresent;
229
+ }
230
+ const char* data = result.ptr;
231
+ uint32_t tag;
232
+ uint64_t message_len;
233
+ data = decode_tag(data, &tag);
234
+ data = decode_varint64(data, &message_len);
235
+ *extension_data = data;
236
+ *len = message_len;
237
+ return kUpb_GetExtensionAsBytes_Ok;
238
+ }
239
+
240
+ static const char* UnknownFieldSet_SkipGroup(const char* ptr, const char* end,
241
+ int group_number);
242
+
243
+ static const char* UnknownFieldSet_SkipField(const char* ptr, const char* end,
244
+ uint32_t tag) {
245
+ int field_number = tag >> 3;
246
+ int wire_type = tag & 7;
247
+ switch (wire_type) {
248
+ case kUpb_WireType_Varint: {
249
+ uint64_t val;
250
+ return decode_varint64(ptr, &val);
251
+ }
252
+ case kUpb_WireType_64Bit:
253
+ if (end - ptr < 8) return NULL;
254
+ return ptr + 8;
255
+ case kUpb_WireType_32Bit:
256
+ if (end - ptr < 4) return NULL;
257
+ return ptr + 4;
258
+ case kUpb_WireType_Delimited: {
259
+ uint64_t size;
260
+ ptr = decode_varint64(ptr, &size);
261
+ if (!ptr || end - ptr < size) return NULL;
262
+ return ptr + size;
263
+ }
264
+ case kUpb_WireType_StartGroup:
265
+ return UnknownFieldSet_SkipGroup(ptr, end, field_number);
266
+ case kUpb_WireType_EndGroup:
267
+ return NULL;
268
+ default:
269
+ assert(0);
270
+ return NULL;
271
+ }
272
+ }
273
+
274
+ static const char* UnknownFieldSet_SkipGroup(const char* ptr, const char* end,
275
+ int group_number) {
276
+ uint32_t end_tag = (group_number << 3) | kUpb_WireType_EndGroup;
277
+ while (true) {
278
+ if (ptr == end) return NULL;
279
+ uint64_t tag;
280
+ ptr = decode_varint64(ptr, &tag);
281
+ if (!ptr) return NULL;
282
+ if (tag == end_tag) return ptr;
283
+ ptr = UnknownFieldSet_SkipField(ptr, end, (uint32_t)tag);
284
+ if (!ptr) return NULL;
285
+ }
286
+ return ptr;
287
+ }
288
+
289
+ enum {
290
+ kUpb_MessageSet_StartItemTag = (1 << 3) | kUpb_WireType_StartGroup,
291
+ kUpb_MessageSet_EndItemTag = (1 << 3) | kUpb_WireType_EndGroup,
292
+ kUpb_MessageSet_TypeIdTag = (2 << 3) | kUpb_WireType_Varint,
293
+ kUpb_MessageSet_MessageTag = (3 << 3) | kUpb_WireType_Delimited,
294
+ };
295
+
296
+ static find_unknown_ret UnknownFieldSet_FindField(const upb_Message* msg,
297
+ int field_number) {
298
+ size_t size;
299
+ find_unknown_ret ret;
300
+
301
+ const char* ptr = upb_Message_GetUnknown(msg, &size);
302
+ if (size == 0) {
303
+ ret.ptr = NULL;
304
+ return ret;
305
+ }
306
+ const char* end = ptr + size;
307
+ uint64_t uint64_val;
308
+
309
+ while (ptr < end) {
310
+ uint32_t tag;
311
+ int field;
312
+ int wire_type;
313
+ const char* unknown_begin = ptr;
314
+ ptr = decode_tag(ptr, &tag);
315
+ field = tag >> 3;
316
+ wire_type = tag & 7;
317
+ switch (wire_type) {
318
+ case kUpb_WireType_EndGroup:
319
+ ret.status = kUpb_FindUnknown_ParseError;
320
+ return ret;
321
+ case kUpb_WireType_Varint:
322
+ ptr = decode_varint64(ptr, &uint64_val);
323
+ if (!ptr) {
324
+ ret.status = kUpb_FindUnknown_ParseError;
325
+ return ret;
326
+ }
327
+ break;
328
+ case kUpb_WireType_32Bit:
329
+ ptr += 4;
330
+ break;
331
+ case kUpb_WireType_64Bit:
332
+ ptr += 8;
333
+ break;
334
+ case kUpb_WireType_Delimited:
335
+ // Read size.
336
+ ptr = decode_varint64(ptr, &uint64_val);
337
+ if (uint64_val >= INT32_MAX || !ptr) {
338
+ ret.status = kUpb_FindUnknown_ParseError;
339
+ return ret;
340
+ }
341
+ ptr += uint64_val;
342
+ break;
343
+ case kUpb_WireType_StartGroup:
344
+ // tag >> 3 specifies the group number, recurse and skip
345
+ // until we see group end tag.
346
+ ptr = UnknownFieldSet_SkipGroup(ptr, end, field_number);
347
+ break;
348
+ default:
349
+ ret.status = kUpb_FindUnknown_ParseError;
350
+ return ret;
351
+ }
352
+ if (field_number == field) {
353
+ ret.status = kUpb_FindUnknown_Ok;
354
+ ret.ptr = unknown_begin;
355
+ ret.len = ptr - unknown_begin;
356
+ return ret;
357
+ }
358
+ }
359
+ ret.status = kUpb_FindUnknown_NotPresent;
360
+ ret.ptr = NULL;
361
+ ret.len = 0;
362
+ return ret;
363
+ }