ruby_memprofiler_pprof 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/ruby_memprofiler_pprof/backtrace.c +429 -0
- data/ext/ruby_memprofiler_pprof/collector.c +1055 -0
- data/ext/ruby_memprofiler_pprof/compat.c +182 -0
- data/ext/ruby_memprofiler_pprof/extconf.rb +72 -0
- data/ext/ruby_memprofiler_pprof/pprof.upb.c +170 -0
- data/ext/ruby_memprofiler_pprof/pprof.upb.h +848 -0
- data/ext/ruby_memprofiler_pprof/pprof_out.c +285 -0
- data/ext/ruby_memprofiler_pprof/ruby_memprofiler_pprof.c +11 -0
- data/ext/ruby_memprofiler_pprof/ruby_memprofiler_pprof.h +301 -0
- data/ext/ruby_memprofiler_pprof/strtab.c +391 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/BUILD +719 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/CONTRIBUTING.md +37 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/DESIGN.md +201 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/LICENSE +26 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/README.md +78 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/WORKSPACE +58 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/BUILD +53 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/amalgamate.py +129 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/build_defs.bzl +160 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/lua.BUILD +127 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/protobuf.patch +54 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/py_proto_library.bzl +137 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/python_downloads.bzl +84 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/system_python.bzl +101 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/upb_proto_library.bzl +388 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/bazel/workspace_deps.bzl +89 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/BUILD +252 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/BUILD.googleapis +54 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/benchmark.cc +333 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/build_defs.bzl +88 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/compare.py +118 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/descriptor.proto +888 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/descriptor_sv.proto +890 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/empty.proto +6 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_protobuf_binary_cc.py +64 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_synthetic_protos.py +118 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/benchmarks/gen_upb_binary_c.py +65 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/BUILD.bazel +102 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/README.md +23 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/build_defs.bzl +73 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/make_cmakelists.py +340 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/staleness_test.py +57 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/cmake/staleness_test_lib.py +186 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/docs/render.py +43 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/docs/style-guide.md +65 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/docs/vs-cpp-protos.md +255 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/docs/wrapping-upb.md +444 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/BUILD +216 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/convert.c +394 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/convert.h +63 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor.c +1694 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor.h +80 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_containers.c +704 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_containers.h +114 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_pool.c +650 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/descriptor_pool.h +48 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/dist/BUILD.bazel +193 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/dist/dist.bzl +190 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/extension_dict.c +247 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/extension_dict.h +39 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/map.c +522 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/map.h +66 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/message.c +1909 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/message.h +101 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/minimal_test.py +183 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/BUILD +70 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/README.md +11 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_database_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_pool_test_wrapper.py +45 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/descriptor_test_wrapper.py +46 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/generator_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/json_format_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/keywords_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/message_factory_test_wrapper.py +37 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/message_test_wrapper.py +52 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/proto_builder_test_wrapper.py +32 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/pyproto_test_wrapper.bzl +36 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/reflection_test_wrapper.py +45 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/service_reflection_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/symbol_database_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/text_encoding_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/text_format_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/unknown_fields_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/well_known_types_test_wrapper.py +36 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/pb_unit_tests/wire_format_test_wrapper.py +30 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/protobuf.c +350 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/protobuf.h +230 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/py_extension.bzl +55 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/python_api.h +61 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/repeated.c +828 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/repeated.h +69 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/unknown_fields.c +404 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/unknown_fields.h +39 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/python/version_script.lds +6 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/LICENSE +32 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/README.google +9 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/console.lua +156 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/lunit/lunit.lua +725 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/BUILD +19 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/LICENSE +21 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/naive.c +92 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/range2-neon.c +157 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/range2-sse.c +170 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/third_party/utf8_range/utf8_range.h +9 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/BUILD.bazel +129 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/README.md +8 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/def.c +939 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/lua_proto_library.bzl +138 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/main.c +83 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/msg.c +1118 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/test.proto +69 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/test_upb.lua +846 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.c +258 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.h +132 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upb.lua +58 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/bindings/lua/upbc.cc +134 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/collections.c +192 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/collections.h +174 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/conformance_upb.c +346 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/conformance_upb_failures.txt +1 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode.c +1221 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode.h +94 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_fast.c +1055 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_fast.h +153 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/decode_internal.h +211 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.c +3262 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.h +414 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/def.hpp +438 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/empty.proto +1 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/encode.c +604 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/encode.h +71 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/fuzz/BUILD +13 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/fuzz/file_descriptor_parsenew_fuzzer.cc +43 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_decode.c +1509 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_decode.h +47 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_encode.c +776 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/json_encode.h +62 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.c +1147 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.h +189 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table.hpp +112 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors.c +363 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors.h +263 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors_internal.h +59 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_accessors_test.cc +425 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/mini_table_test.cc +230 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg.c +428 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg.h +114 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_internal.h +836 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_test.cc +491 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/msg_test.proto +195 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/port_def.inc +261 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/port_undef.inc +62 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.c +323 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.h +109 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/reflection.hpp +37 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/table.c +926 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/table_internal.h +385 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test.proto +74 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_cpp.cc +186 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_cpp.proto +12 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_generated_code.cc +977 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/test_table.cc +580 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/text_encode.c +472 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/text_encode.h +64 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.c +362 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.h +378 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb.hpp +115 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/upb_internal.h +68 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/BUILD +121 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/README.md +7 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare.c +300 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare.h +66 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/compare_test.cc +236 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto.c +572 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto.h +62 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_public_import_test.proto +32 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_regular_import_test.proto +36 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_test.cc +143 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_test.proto +119 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_weak_import_test.proto +28 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/def_to_proto_wweak_import_test.proto +28 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields.c +311 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields.h +94 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields_test.cc +202 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upb/util/required_fields_test.proto +48 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/BUILD +78 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/common.cc +77 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/common.h +112 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/protoc-gen-upb.cc +1997 -0
- data/ext/ruby_memprofiler_pprof/vendor/upb/upbc/protoc-gen-upbdefs.cc +193 -0
- data/lib/ruby_memprofiler_pprof/atfork.rb +77 -0
- data/lib/ruby_memprofiler_pprof/block_flusher.rb +61 -0
- data/lib/ruby_memprofiler_pprof/file_flusher.rb +45 -0
- data/lib/ruby_memprofiler_pprof/profile_app.rb +30 -0
- data/lib/ruby_memprofiler_pprof/profile_data.rb +18 -0
- data/lib/ruby_memprofiler_pprof/version.rb +5 -0
- data/lib/ruby_memprofiler_pprof.rb +8 -0
- data/libexec/ruby_memprofiler_pprof_profile +16 -0
- metadata +257 -0
@@ -0,0 +1,604 @@
|
|
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
|
+
/* We encode backwards, to avoid pre-computing lengths (one-pass encode). */
|
29
|
+
|
30
|
+
#include "upb/encode.h"
|
31
|
+
|
32
|
+
#include <setjmp.h>
|
33
|
+
#include <string.h>
|
34
|
+
|
35
|
+
#include "upb/msg_internal.h"
|
36
|
+
#include "upb/upb.h"
|
37
|
+
|
38
|
+
/* Must be last. */
|
39
|
+
#include "upb/port_def.inc"
|
40
|
+
|
41
|
+
#define UPB_PB_VARINT_MAX_LEN 10
|
42
|
+
|
43
|
+
UPB_NOINLINE
|
44
|
+
static size_t encode_varint64(uint64_t val, char* buf) {
|
45
|
+
size_t i = 0;
|
46
|
+
do {
|
47
|
+
uint8_t byte = val & 0x7fU;
|
48
|
+
val >>= 7;
|
49
|
+
if (val) byte |= 0x80U;
|
50
|
+
buf[i++] = byte;
|
51
|
+
} while (val);
|
52
|
+
return i;
|
53
|
+
}
|
54
|
+
|
55
|
+
static uint32_t encode_zz32(int32_t n) {
|
56
|
+
return ((uint32_t)n << 1) ^ (n >> 31);
|
57
|
+
}
|
58
|
+
static uint64_t encode_zz64(int64_t n) {
|
59
|
+
return ((uint64_t)n << 1) ^ (n >> 63);
|
60
|
+
}
|
61
|
+
|
62
|
+
typedef struct {
|
63
|
+
jmp_buf err;
|
64
|
+
upb_alloc* alloc;
|
65
|
+
char *buf, *ptr, *limit;
|
66
|
+
int options;
|
67
|
+
int depth;
|
68
|
+
_upb_mapsorter sorter;
|
69
|
+
} upb_encstate;
|
70
|
+
|
71
|
+
static size_t upb_roundup_pow2(size_t bytes) {
|
72
|
+
size_t ret = 128;
|
73
|
+
while (ret < bytes) {
|
74
|
+
ret *= 2;
|
75
|
+
}
|
76
|
+
return ret;
|
77
|
+
}
|
78
|
+
|
79
|
+
UPB_NORETURN static void encode_err(upb_encstate* e) { UPB_LONGJMP(e->err, 1); }
|
80
|
+
|
81
|
+
UPB_NOINLINE
|
82
|
+
static void encode_growbuffer(upb_encstate* e, size_t bytes) {
|
83
|
+
size_t old_size = e->limit - e->buf;
|
84
|
+
size_t new_size = upb_roundup_pow2(bytes + (e->limit - e->ptr));
|
85
|
+
char* new_buf = upb_realloc(e->alloc, e->buf, old_size, new_size);
|
86
|
+
|
87
|
+
if (!new_buf) encode_err(e);
|
88
|
+
|
89
|
+
/* We want previous data at the end, realloc() put it at the beginning. */
|
90
|
+
if (old_size > 0) {
|
91
|
+
memmove(new_buf + new_size - old_size, e->buf, old_size);
|
92
|
+
}
|
93
|
+
|
94
|
+
e->ptr = new_buf + new_size - (e->limit - e->ptr);
|
95
|
+
e->limit = new_buf + new_size;
|
96
|
+
e->buf = new_buf;
|
97
|
+
|
98
|
+
e->ptr -= bytes;
|
99
|
+
}
|
100
|
+
|
101
|
+
/* Call to ensure that at least "bytes" bytes are available for writing at
|
102
|
+
* e->ptr. Returns false if the bytes could not be allocated. */
|
103
|
+
UPB_FORCEINLINE
|
104
|
+
static void encode_reserve(upb_encstate* e, size_t bytes) {
|
105
|
+
if ((size_t)(e->ptr - e->buf) < bytes) {
|
106
|
+
encode_growbuffer(e, bytes);
|
107
|
+
return;
|
108
|
+
}
|
109
|
+
|
110
|
+
e->ptr -= bytes;
|
111
|
+
}
|
112
|
+
|
113
|
+
/* Writes the given bytes to the buffer, handling reserve/advance. */
|
114
|
+
static void encode_bytes(upb_encstate* e, const void* data, size_t len) {
|
115
|
+
if (len == 0) return; /* memcpy() with zero size is UB */
|
116
|
+
encode_reserve(e, len);
|
117
|
+
memcpy(e->ptr, data, len);
|
118
|
+
}
|
119
|
+
|
120
|
+
static void encode_fixed64(upb_encstate* e, uint64_t val) {
|
121
|
+
val = _upb_BigEndian_Swap64(val);
|
122
|
+
encode_bytes(e, &val, sizeof(uint64_t));
|
123
|
+
}
|
124
|
+
|
125
|
+
static void encode_fixed32(upb_encstate* e, uint32_t val) {
|
126
|
+
val = _upb_BigEndian_Swap32(val);
|
127
|
+
encode_bytes(e, &val, sizeof(uint32_t));
|
128
|
+
}
|
129
|
+
|
130
|
+
UPB_NOINLINE
|
131
|
+
static void encode_longvarint(upb_encstate* e, uint64_t val) {
|
132
|
+
size_t len;
|
133
|
+
char* start;
|
134
|
+
|
135
|
+
encode_reserve(e, UPB_PB_VARINT_MAX_LEN);
|
136
|
+
len = encode_varint64(val, e->ptr);
|
137
|
+
start = e->ptr + UPB_PB_VARINT_MAX_LEN - len;
|
138
|
+
memmove(start, e->ptr, len);
|
139
|
+
e->ptr = start;
|
140
|
+
}
|
141
|
+
|
142
|
+
UPB_FORCEINLINE
|
143
|
+
static void encode_varint(upb_encstate* e, uint64_t val) {
|
144
|
+
if (val < 128 && e->ptr != e->buf) {
|
145
|
+
--e->ptr;
|
146
|
+
*e->ptr = val;
|
147
|
+
} else {
|
148
|
+
encode_longvarint(e, val);
|
149
|
+
}
|
150
|
+
}
|
151
|
+
|
152
|
+
static void encode_double(upb_encstate* e, double d) {
|
153
|
+
uint64_t u64;
|
154
|
+
UPB_ASSERT(sizeof(double) == sizeof(uint64_t));
|
155
|
+
memcpy(&u64, &d, sizeof(uint64_t));
|
156
|
+
encode_fixed64(e, u64);
|
157
|
+
}
|
158
|
+
|
159
|
+
static void encode_float(upb_encstate* e, float d) {
|
160
|
+
uint32_t u32;
|
161
|
+
UPB_ASSERT(sizeof(float) == sizeof(uint32_t));
|
162
|
+
memcpy(&u32, &d, sizeof(uint32_t));
|
163
|
+
encode_fixed32(e, u32);
|
164
|
+
}
|
165
|
+
|
166
|
+
static void encode_tag(upb_encstate* e, uint32_t field_number,
|
167
|
+
uint8_t wire_type) {
|
168
|
+
encode_varint(e, (field_number << 3) | wire_type);
|
169
|
+
}
|
170
|
+
|
171
|
+
static void encode_fixedarray(upb_encstate* e, const upb_Array* arr,
|
172
|
+
size_t elem_size, uint32_t tag) {
|
173
|
+
size_t bytes = arr->len * elem_size;
|
174
|
+
const char* data = _upb_array_constptr(arr);
|
175
|
+
const char* ptr = data + bytes - elem_size;
|
176
|
+
|
177
|
+
if (tag || !_upb_IsLittleEndian()) {
|
178
|
+
while (true) {
|
179
|
+
if (elem_size == 4) {
|
180
|
+
uint32_t val;
|
181
|
+
memcpy(&val, ptr, sizeof(val));
|
182
|
+
val = _upb_BigEndian_Swap32(val);
|
183
|
+
encode_bytes(e, &val, elem_size);
|
184
|
+
} else {
|
185
|
+
UPB_ASSERT(elem_size == 8);
|
186
|
+
uint64_t val;
|
187
|
+
memcpy(&val, ptr, sizeof(val));
|
188
|
+
val = _upb_BigEndian_Swap64(val);
|
189
|
+
encode_bytes(e, &val, elem_size);
|
190
|
+
}
|
191
|
+
|
192
|
+
if (tag) encode_varint(e, tag);
|
193
|
+
if (ptr == data) break;
|
194
|
+
ptr -= elem_size;
|
195
|
+
}
|
196
|
+
} else {
|
197
|
+
encode_bytes(e, data, bytes);
|
198
|
+
}
|
199
|
+
}
|
200
|
+
|
201
|
+
static void encode_message(upb_encstate* e, const upb_Message* msg,
|
202
|
+
const upb_MiniTable* m, size_t* size);
|
203
|
+
|
204
|
+
static void encode_scalar(upb_encstate* e, const void* _field_mem,
|
205
|
+
const upb_MiniTable_Sub* subs,
|
206
|
+
const upb_MiniTable_Field* f) {
|
207
|
+
const char* field_mem = _field_mem;
|
208
|
+
int wire_type;
|
209
|
+
|
210
|
+
#define CASE(ctype, type, wtype, encodeval) \
|
211
|
+
{ \
|
212
|
+
ctype val = *(ctype*)field_mem; \
|
213
|
+
encode_##type(e, encodeval); \
|
214
|
+
wire_type = wtype; \
|
215
|
+
break; \
|
216
|
+
}
|
217
|
+
|
218
|
+
switch (f->descriptortype) {
|
219
|
+
case kUpb_FieldType_Double:
|
220
|
+
CASE(double, double, kUpb_WireType_64Bit, val);
|
221
|
+
case kUpb_FieldType_Float:
|
222
|
+
CASE(float, float, kUpb_WireType_32Bit, val);
|
223
|
+
case kUpb_FieldType_Int64:
|
224
|
+
case kUpb_FieldType_UInt64:
|
225
|
+
CASE(uint64_t, varint, kUpb_WireType_Varint, val);
|
226
|
+
case kUpb_FieldType_UInt32:
|
227
|
+
CASE(uint32_t, varint, kUpb_WireType_Varint, val);
|
228
|
+
case kUpb_FieldType_Int32:
|
229
|
+
case kUpb_FieldType_Enum:
|
230
|
+
CASE(int32_t, varint, kUpb_WireType_Varint, (int64_t)val);
|
231
|
+
case kUpb_FieldType_SFixed64:
|
232
|
+
case kUpb_FieldType_Fixed64:
|
233
|
+
CASE(uint64_t, fixed64, kUpb_WireType_64Bit, val);
|
234
|
+
case kUpb_FieldType_Fixed32:
|
235
|
+
case kUpb_FieldType_SFixed32:
|
236
|
+
CASE(uint32_t, fixed32, kUpb_WireType_32Bit, val);
|
237
|
+
case kUpb_FieldType_Bool:
|
238
|
+
CASE(bool, varint, kUpb_WireType_Varint, val);
|
239
|
+
case kUpb_FieldType_SInt32:
|
240
|
+
CASE(int32_t, varint, kUpb_WireType_Varint, encode_zz32(val));
|
241
|
+
case kUpb_FieldType_SInt64:
|
242
|
+
CASE(int64_t, varint, kUpb_WireType_Varint, encode_zz64(val));
|
243
|
+
case kUpb_FieldType_String:
|
244
|
+
case kUpb_FieldType_Bytes: {
|
245
|
+
upb_StringView view = *(upb_StringView*)field_mem;
|
246
|
+
encode_bytes(e, view.data, view.size);
|
247
|
+
encode_varint(e, view.size);
|
248
|
+
wire_type = kUpb_WireType_Delimited;
|
249
|
+
break;
|
250
|
+
}
|
251
|
+
case kUpb_FieldType_Group: {
|
252
|
+
size_t size;
|
253
|
+
void* submsg = *(void**)field_mem;
|
254
|
+
const upb_MiniTable* subm = subs[f->submsg_index].submsg;
|
255
|
+
if (submsg == NULL) {
|
256
|
+
return;
|
257
|
+
}
|
258
|
+
if (--e->depth == 0) encode_err(e);
|
259
|
+
encode_tag(e, f->number, kUpb_WireType_EndGroup);
|
260
|
+
encode_message(e, submsg, subm, &size);
|
261
|
+
wire_type = kUpb_WireType_StartGroup;
|
262
|
+
e->depth++;
|
263
|
+
break;
|
264
|
+
}
|
265
|
+
case kUpb_FieldType_Message: {
|
266
|
+
size_t size;
|
267
|
+
void* submsg = *(void**)field_mem;
|
268
|
+
const upb_MiniTable* subm = subs[f->submsg_index].submsg;
|
269
|
+
if (submsg == NULL) {
|
270
|
+
return;
|
271
|
+
}
|
272
|
+
if (--e->depth == 0) encode_err(e);
|
273
|
+
encode_message(e, submsg, subm, &size);
|
274
|
+
encode_varint(e, size);
|
275
|
+
wire_type = kUpb_WireType_Delimited;
|
276
|
+
e->depth++;
|
277
|
+
break;
|
278
|
+
}
|
279
|
+
default:
|
280
|
+
UPB_UNREACHABLE();
|
281
|
+
}
|
282
|
+
#undef CASE
|
283
|
+
|
284
|
+
encode_tag(e, f->number, wire_type);
|
285
|
+
}
|
286
|
+
|
287
|
+
static void encode_array(upb_encstate* e, const upb_Message* msg,
|
288
|
+
const upb_MiniTable_Sub* subs,
|
289
|
+
const upb_MiniTable_Field* f) {
|
290
|
+
const upb_Array* arr = *UPB_PTR_AT(msg, f->offset, upb_Array*);
|
291
|
+
bool packed = f->mode & kUpb_LabelFlags_IsPacked;
|
292
|
+
size_t pre_len = e->limit - e->ptr;
|
293
|
+
|
294
|
+
if (arr == NULL || arr->len == 0) {
|
295
|
+
return;
|
296
|
+
}
|
297
|
+
|
298
|
+
#define VARINT_CASE(ctype, encode) \
|
299
|
+
{ \
|
300
|
+
const ctype* start = _upb_array_constptr(arr); \
|
301
|
+
const ctype* ptr = start + arr->len; \
|
302
|
+
uint32_t tag = packed ? 0 : (f->number << 3) | kUpb_WireType_Varint; \
|
303
|
+
do { \
|
304
|
+
ptr--; \
|
305
|
+
encode_varint(e, encode); \
|
306
|
+
if (tag) encode_varint(e, tag); \
|
307
|
+
} while (ptr != start); \
|
308
|
+
} \
|
309
|
+
break;
|
310
|
+
|
311
|
+
#define TAG(wire_type) (packed ? 0 : (f->number << 3 | wire_type))
|
312
|
+
|
313
|
+
switch (f->descriptortype) {
|
314
|
+
case kUpb_FieldType_Double:
|
315
|
+
encode_fixedarray(e, arr, sizeof(double), TAG(kUpb_WireType_64Bit));
|
316
|
+
break;
|
317
|
+
case kUpb_FieldType_Float:
|
318
|
+
encode_fixedarray(e, arr, sizeof(float), TAG(kUpb_WireType_32Bit));
|
319
|
+
break;
|
320
|
+
case kUpb_FieldType_SFixed64:
|
321
|
+
case kUpb_FieldType_Fixed64:
|
322
|
+
encode_fixedarray(e, arr, sizeof(uint64_t), TAG(kUpb_WireType_64Bit));
|
323
|
+
break;
|
324
|
+
case kUpb_FieldType_Fixed32:
|
325
|
+
case kUpb_FieldType_SFixed32:
|
326
|
+
encode_fixedarray(e, arr, sizeof(uint32_t), TAG(kUpb_WireType_32Bit));
|
327
|
+
break;
|
328
|
+
case kUpb_FieldType_Int64:
|
329
|
+
case kUpb_FieldType_UInt64:
|
330
|
+
VARINT_CASE(uint64_t, *ptr);
|
331
|
+
case kUpb_FieldType_UInt32:
|
332
|
+
VARINT_CASE(uint32_t, *ptr);
|
333
|
+
case kUpb_FieldType_Int32:
|
334
|
+
case kUpb_FieldType_Enum:
|
335
|
+
VARINT_CASE(int32_t, (int64_t)*ptr);
|
336
|
+
case kUpb_FieldType_Bool:
|
337
|
+
VARINT_CASE(bool, *ptr);
|
338
|
+
case kUpb_FieldType_SInt32:
|
339
|
+
VARINT_CASE(int32_t, encode_zz32(*ptr));
|
340
|
+
case kUpb_FieldType_SInt64:
|
341
|
+
VARINT_CASE(int64_t, encode_zz64(*ptr));
|
342
|
+
case kUpb_FieldType_String:
|
343
|
+
case kUpb_FieldType_Bytes: {
|
344
|
+
const upb_StringView* start = _upb_array_constptr(arr);
|
345
|
+
const upb_StringView* ptr = start + arr->len;
|
346
|
+
do {
|
347
|
+
ptr--;
|
348
|
+
encode_bytes(e, ptr->data, ptr->size);
|
349
|
+
encode_varint(e, ptr->size);
|
350
|
+
encode_tag(e, f->number, kUpb_WireType_Delimited);
|
351
|
+
} while (ptr != start);
|
352
|
+
return;
|
353
|
+
}
|
354
|
+
case kUpb_FieldType_Group: {
|
355
|
+
const void* const* start = _upb_array_constptr(arr);
|
356
|
+
const void* const* ptr = start + arr->len;
|
357
|
+
const upb_MiniTable* subm = subs[f->submsg_index].submsg;
|
358
|
+
if (--e->depth == 0) encode_err(e);
|
359
|
+
do {
|
360
|
+
size_t size;
|
361
|
+
ptr--;
|
362
|
+
encode_tag(e, f->number, kUpb_WireType_EndGroup);
|
363
|
+
encode_message(e, *ptr, subm, &size);
|
364
|
+
encode_tag(e, f->number, kUpb_WireType_StartGroup);
|
365
|
+
} while (ptr != start);
|
366
|
+
e->depth++;
|
367
|
+
return;
|
368
|
+
}
|
369
|
+
case kUpb_FieldType_Message: {
|
370
|
+
const void* const* start = _upb_array_constptr(arr);
|
371
|
+
const void* const* ptr = start + arr->len;
|
372
|
+
const upb_MiniTable* subm = subs[f->submsg_index].submsg;
|
373
|
+
if (--e->depth == 0) encode_err(e);
|
374
|
+
do {
|
375
|
+
size_t size;
|
376
|
+
ptr--;
|
377
|
+
encode_message(e, *ptr, subm, &size);
|
378
|
+
encode_varint(e, size);
|
379
|
+
encode_tag(e, f->number, kUpb_WireType_Delimited);
|
380
|
+
} while (ptr != start);
|
381
|
+
e->depth++;
|
382
|
+
return;
|
383
|
+
}
|
384
|
+
}
|
385
|
+
#undef VARINT_CASE
|
386
|
+
|
387
|
+
if (packed) {
|
388
|
+
encode_varint(e, e->limit - e->ptr - pre_len);
|
389
|
+
encode_tag(e, f->number, kUpb_WireType_Delimited);
|
390
|
+
}
|
391
|
+
}
|
392
|
+
|
393
|
+
static void encode_mapentry(upb_encstate* e, uint32_t number,
|
394
|
+
const upb_MiniTable* layout,
|
395
|
+
const upb_MapEntry* ent) {
|
396
|
+
const upb_MiniTable_Field* key_field = &layout->fields[0];
|
397
|
+
const upb_MiniTable_Field* val_field = &layout->fields[1];
|
398
|
+
size_t pre_len = e->limit - e->ptr;
|
399
|
+
size_t size;
|
400
|
+
encode_scalar(e, &ent->v, layout->subs, val_field);
|
401
|
+
encode_scalar(e, &ent->k, layout->subs, key_field);
|
402
|
+
size = (e->limit - e->ptr) - pre_len;
|
403
|
+
encode_varint(e, size);
|
404
|
+
encode_tag(e, number, kUpb_WireType_Delimited);
|
405
|
+
}
|
406
|
+
|
407
|
+
static void encode_map(upb_encstate* e, const upb_Message* msg,
|
408
|
+
const upb_MiniTable_Sub* subs,
|
409
|
+
const upb_MiniTable_Field* f) {
|
410
|
+
const upb_Map* map = *UPB_PTR_AT(msg, f->offset, const upb_Map*);
|
411
|
+
const upb_MiniTable* layout = subs[f->submsg_index].submsg;
|
412
|
+
UPB_ASSERT(layout->field_count == 2);
|
413
|
+
|
414
|
+
if (map == NULL) return;
|
415
|
+
|
416
|
+
if (e->options & kUpb_Encode_Deterministic) {
|
417
|
+
_upb_sortedmap sorted;
|
418
|
+
_upb_mapsorter_pushmap(&e->sorter, layout->fields[0].descriptortype, map,
|
419
|
+
&sorted);
|
420
|
+
upb_MapEntry ent;
|
421
|
+
while (_upb_sortedmap_next(&e->sorter, map, &sorted, &ent)) {
|
422
|
+
encode_mapentry(e, f->number, layout, &ent);
|
423
|
+
}
|
424
|
+
_upb_mapsorter_popmap(&e->sorter, &sorted);
|
425
|
+
} else {
|
426
|
+
upb_strtable_iter i;
|
427
|
+
upb_strtable_begin(&i, &map->table);
|
428
|
+
for (; !upb_strtable_done(&i); upb_strtable_next(&i)) {
|
429
|
+
upb_StringView key = upb_strtable_iter_key(&i);
|
430
|
+
const upb_value val = upb_strtable_iter_value(&i);
|
431
|
+
upb_MapEntry ent;
|
432
|
+
_upb_map_fromkey(key, &ent.k, map->key_size);
|
433
|
+
_upb_map_fromvalue(val, &ent.v, map->val_size);
|
434
|
+
encode_mapentry(e, f->number, layout, &ent);
|
435
|
+
}
|
436
|
+
}
|
437
|
+
}
|
438
|
+
|
439
|
+
static bool encode_shouldencode(upb_encstate* e, const upb_Message* msg,
|
440
|
+
const upb_MiniTable_Sub* subs,
|
441
|
+
const upb_MiniTable_Field* f) {
|
442
|
+
if (f->presence == 0) {
|
443
|
+
/* Proto3 presence or map/array. */
|
444
|
+
const void* mem = UPB_PTR_AT(msg, f->offset, void);
|
445
|
+
switch (f->mode >> kUpb_FieldRep_Shift) {
|
446
|
+
case kUpb_FieldRep_1Byte: {
|
447
|
+
char ch;
|
448
|
+
memcpy(&ch, mem, 1);
|
449
|
+
return ch != 0;
|
450
|
+
}
|
451
|
+
#if UINTPTR_MAX == 0xffffffff
|
452
|
+
case kUpb_FieldRep_Pointer:
|
453
|
+
#endif
|
454
|
+
case kUpb_FieldRep_4Byte: {
|
455
|
+
uint32_t u32;
|
456
|
+
memcpy(&u32, mem, 4);
|
457
|
+
return u32 != 0;
|
458
|
+
}
|
459
|
+
#if UINTPTR_MAX != 0xffffffff
|
460
|
+
case kUpb_FieldRep_Pointer:
|
461
|
+
#endif
|
462
|
+
case kUpb_FieldRep_8Byte: {
|
463
|
+
uint64_t u64;
|
464
|
+
memcpy(&u64, mem, 8);
|
465
|
+
return u64 != 0;
|
466
|
+
}
|
467
|
+
case kUpb_FieldRep_StringView: {
|
468
|
+
const upb_StringView* str = (const upb_StringView*)mem;
|
469
|
+
return str->size != 0;
|
470
|
+
}
|
471
|
+
default:
|
472
|
+
UPB_UNREACHABLE();
|
473
|
+
}
|
474
|
+
} else if (f->presence > 0) {
|
475
|
+
/* Proto2 presence: hasbit. */
|
476
|
+
return _upb_hasbit_field(msg, f);
|
477
|
+
} else {
|
478
|
+
/* Field is in a oneof. */
|
479
|
+
return _upb_getoneofcase_field(msg, f) == f->number;
|
480
|
+
}
|
481
|
+
}
|
482
|
+
|
483
|
+
static void encode_field(upb_encstate* e, const upb_Message* msg,
|
484
|
+
const upb_MiniTable_Sub* subs,
|
485
|
+
const upb_MiniTable_Field* field) {
|
486
|
+
switch (upb_FieldMode_Get(field)) {
|
487
|
+
case kUpb_FieldMode_Array:
|
488
|
+
encode_array(e, msg, subs, field);
|
489
|
+
break;
|
490
|
+
case kUpb_FieldMode_Map:
|
491
|
+
encode_map(e, msg, subs, field);
|
492
|
+
break;
|
493
|
+
case kUpb_FieldMode_Scalar:
|
494
|
+
encode_scalar(e, UPB_PTR_AT(msg, field->offset, void), subs, field);
|
495
|
+
break;
|
496
|
+
default:
|
497
|
+
UPB_UNREACHABLE();
|
498
|
+
}
|
499
|
+
}
|
500
|
+
|
501
|
+
/* message MessageSet {
|
502
|
+
* repeated group Item = 1 {
|
503
|
+
* required int32 type_id = 2;
|
504
|
+
* required string message = 3;
|
505
|
+
* }
|
506
|
+
* } */
|
507
|
+
static void encode_msgset_item(upb_encstate* e,
|
508
|
+
const upb_Message_Extension* ext) {
|
509
|
+
size_t size;
|
510
|
+
encode_tag(e, 1, kUpb_WireType_EndGroup);
|
511
|
+
encode_message(e, ext->data.ptr, ext->ext->sub.submsg, &size);
|
512
|
+
encode_varint(e, size);
|
513
|
+
encode_tag(e, 3, kUpb_WireType_Delimited);
|
514
|
+
encode_varint(e, ext->ext->field.number);
|
515
|
+
encode_tag(e, 2, kUpb_WireType_Varint);
|
516
|
+
encode_tag(e, 1, kUpb_WireType_StartGroup);
|
517
|
+
}
|
518
|
+
|
519
|
+
static void encode_message(upb_encstate* e, const upb_Message* msg,
|
520
|
+
const upb_MiniTable* m, size_t* size) {
|
521
|
+
size_t pre_len = e->limit - e->ptr;
|
522
|
+
|
523
|
+
if ((e->options & kUpb_Encode_CheckRequired) && m->required_count) {
|
524
|
+
uint64_t msg_head;
|
525
|
+
memcpy(&msg_head, msg, 8);
|
526
|
+
msg_head = _upb_BigEndian_Swap64(msg_head);
|
527
|
+
if (upb_MiniTable_requiredmask(m) & ~msg_head) {
|
528
|
+
encode_err(e);
|
529
|
+
}
|
530
|
+
}
|
531
|
+
|
532
|
+
if ((e->options & kUpb_Encode_SkipUnknown) == 0) {
|
533
|
+
size_t unknown_size;
|
534
|
+
const char* unknown = upb_Message_GetUnknown(msg, &unknown_size);
|
535
|
+
|
536
|
+
if (unknown) {
|
537
|
+
encode_bytes(e, unknown, unknown_size);
|
538
|
+
}
|
539
|
+
}
|
540
|
+
|
541
|
+
if (m->ext != kUpb_ExtMode_NonExtendable) {
|
542
|
+
/* Encode all extensions together. Unlike C++, we do not attempt to keep
|
543
|
+
* these in field number order relative to normal fields or even to each
|
544
|
+
* other. */
|
545
|
+
size_t ext_count;
|
546
|
+
const upb_Message_Extension* ext = _upb_Message_Getexts(msg, &ext_count);
|
547
|
+
if (ext_count) {
|
548
|
+
const upb_Message_Extension* end = ext + ext_count;
|
549
|
+
for (; ext != end; ext++) {
|
550
|
+
if (UPB_UNLIKELY(m->ext == kUpb_ExtMode_IsMessageSet)) {
|
551
|
+
encode_msgset_item(e, ext);
|
552
|
+
} else {
|
553
|
+
encode_field(e, &ext->data, &ext->ext->sub, &ext->ext->field);
|
554
|
+
}
|
555
|
+
}
|
556
|
+
}
|
557
|
+
}
|
558
|
+
|
559
|
+
if (m->field_count) {
|
560
|
+
const upb_MiniTable_Field* f = &m->fields[m->field_count];
|
561
|
+
const upb_MiniTable_Field* first = &m->fields[0];
|
562
|
+
while (f != first) {
|
563
|
+
f--;
|
564
|
+
if (encode_shouldencode(e, msg, m->subs, f)) {
|
565
|
+
encode_field(e, msg, m->subs, f);
|
566
|
+
}
|
567
|
+
}
|
568
|
+
}
|
569
|
+
|
570
|
+
*size = (e->limit - e->ptr) - pre_len;
|
571
|
+
}
|
572
|
+
|
573
|
+
char* upb_Encode(const void* msg, const upb_MiniTable* l, int options,
|
574
|
+
upb_Arena* arena, size_t* size) {
|
575
|
+
upb_encstate e;
|
576
|
+
unsigned depth = (unsigned)options >> 16;
|
577
|
+
|
578
|
+
e.alloc = upb_Arena_Alloc(arena);
|
579
|
+
e.buf = NULL;
|
580
|
+
e.limit = NULL;
|
581
|
+
e.ptr = NULL;
|
582
|
+
e.depth = depth ? depth : 64;
|
583
|
+
e.options = options;
|
584
|
+
_upb_mapsorter_init(&e.sorter);
|
585
|
+
char* ret = NULL;
|
586
|
+
|
587
|
+
if (UPB_SETJMP(e.err)) {
|
588
|
+
*size = 0;
|
589
|
+
ret = NULL;
|
590
|
+
} else {
|
591
|
+
encode_message(&e, msg, l, size);
|
592
|
+
*size = e.limit - e.ptr;
|
593
|
+
if (*size == 0) {
|
594
|
+
static char ch;
|
595
|
+
ret = &ch;
|
596
|
+
} else {
|
597
|
+
UPB_ASSERT(e.ptr);
|
598
|
+
ret = e.ptr;
|
599
|
+
}
|
600
|
+
}
|
601
|
+
|
602
|
+
_upb_mapsorter_destroy(&e.sorter);
|
603
|
+
return ret;
|
604
|
+
}
|
@@ -0,0 +1,71 @@
|
|
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
|
+
/*
|
29
|
+
* upb_Encode: parsing into a upb_Message using a upb_MiniTable.
|
30
|
+
*/
|
31
|
+
|
32
|
+
#ifndef UPB_ENCODE_H_
|
33
|
+
#define UPB_ENCODE_H_
|
34
|
+
|
35
|
+
#include "upb/msg.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, the results of serializing will be deterministic across all
|
46
|
+
* instances of this binary. There are no guarantees across different
|
47
|
+
* binary builds.
|
48
|
+
*
|
49
|
+
* If your proto contains maps, the encoder will need to malloc()/free()
|
50
|
+
* memory during encode. */
|
51
|
+
kUpb_Encode_Deterministic = 1,
|
52
|
+
|
53
|
+
/* When set, unknown fields are not printed. */
|
54
|
+
kUpb_Encode_SkipUnknown = 2,
|
55
|
+
|
56
|
+
/* When set, the encode will fail if any required fields are missing. */
|
57
|
+
kUpb_Encode_CheckRequired = 4,
|
58
|
+
};
|
59
|
+
|
60
|
+
#define UPB_ENCODE_MAXDEPTH(depth) ((depth) << 16)
|
61
|
+
|
62
|
+
char* upb_Encode(const void* msg, const upb_MiniTable* l, int options,
|
63
|
+
upb_Arena* arena, size_t* size);
|
64
|
+
|
65
|
+
#include "upb/port_undef.inc"
|
66
|
+
|
67
|
+
#ifdef __cplusplus
|
68
|
+
} /* extern "C" */
|
69
|
+
#endif
|
70
|
+
|
71
|
+
#endif /* UPB_ENCODE_H_ */
|
@@ -0,0 +1,13 @@
|
|
1
|
+
load("@rules_fuzzing//fuzzing:cc_defs.bzl", "cc_fuzz_test")
|
2
|
+
|
3
|
+
licenses(["notice"])
|
4
|
+
|
5
|
+
cc_fuzz_test(
|
6
|
+
name = "file_descriptor_parsenew_fuzzer",
|
7
|
+
srcs = ["file_descriptor_parsenew_fuzzer.cc"],
|
8
|
+
deps = [
|
9
|
+
"//:descriptor_upb_proto",
|
10
|
+
"//:reflection",
|
11
|
+
"//:upb",
|
12
|
+
],
|
13
|
+
)
|